index.js 476 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _objectWithoutPropertiesLoose(source, excluded) {
  4. if (source == null) return {};
  5. var target = {};
  6. var sourceKeys = Object.keys(source);
  7. var key, i;
  8. for (i = 0; i < sourceKeys.length; i++) {
  9. key = sourceKeys[i];
  10. if (excluded.indexOf(key) >= 0) continue;
  11. target[key] = source[key];
  12. }
  13. return target;
  14. }
  15. class Position {
  16. constructor(line, col, index) {
  17. this.line = void 0;
  18. this.column = void 0;
  19. this.index = void 0;
  20. this.line = line;
  21. this.column = col;
  22. this.index = index;
  23. }
  24. }
  25. class SourceLocation {
  26. constructor(start, end) {
  27. this.start = void 0;
  28. this.end = void 0;
  29. this.filename = void 0;
  30. this.identifierName = void 0;
  31. this.start = start;
  32. this.end = end;
  33. }
  34. }
  35. function createPositionWithColumnOffset(position, columnOffset) {
  36. const {
  37. line,
  38. column,
  39. index
  40. } = position;
  41. return new Position(line, column + columnOffset, index + columnOffset);
  42. }
  43. var ParseErrorCode = {
  44. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  45. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  46. };
  47. const reflect = (keys, last = keys.length - 1) => ({
  48. get() {
  49. return keys.reduce((object, key) => object[key], this);
  50. },
  51. set(value) {
  52. keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
  53. }
  54. });
  55. const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
  56. value: descriptor,
  57. enumerable: false
  58. } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
  59. configurable: true
  60. }, descriptor)), Object.assign(new constructor(), properties));
  61. var ModuleErrors = {
  62. ImportMetaOutsideModule: {
  63. message: `import.meta may appear only with 'sourceType: "module"'`,
  64. code: ParseErrorCode.SourceTypeModuleError
  65. },
  66. ImportOutsideModule: {
  67. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  68. code: ParseErrorCode.SourceTypeModuleError
  69. }
  70. };
  71. const NodeDescriptions = {
  72. ArrayPattern: "array destructuring pattern",
  73. AssignmentExpression: "assignment expression",
  74. AssignmentPattern: "assignment expression",
  75. ArrowFunctionExpression: "arrow function expression",
  76. ConditionalExpression: "conditional expression",
  77. CatchClause: "catch clause",
  78. ForOfStatement: "for-of statement",
  79. ForInStatement: "for-in statement",
  80. ForStatement: "for-loop",
  81. FormalParameters: "function parameter list",
  82. Identifier: "identifier",
  83. ImportSpecifier: "import specifier",
  84. ImportDefaultSpecifier: "import default specifier",
  85. ImportNamespaceSpecifier: "import namespace specifier",
  86. ObjectPattern: "object destructuring pattern",
  87. ParenthesizedExpression: "parenthesized expression",
  88. RestElement: "rest element",
  89. UpdateExpression: {
  90. true: "prefix operation",
  91. false: "postfix operation"
  92. },
  93. VariableDeclarator: "variable declaration",
  94. YieldExpression: "yield expression"
  95. };
  96. const toNodeDescription = ({
  97. type,
  98. prefix
  99. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  100. var StandardErrors = {
  101. AccessorIsGenerator: ({
  102. kind
  103. }) => `A ${kind}ter cannot be a generator.`,
  104. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  105. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  106. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  107. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  108. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  109. AwaitInUsingBinding: "'await' is not allowed to be used as a name in 'using' declarations.",
  110. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  111. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  112. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  113. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  114. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  115. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  116. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  117. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  118. ConstructorIsAsync: "Constructor can't be an async function.",
  119. ConstructorIsGenerator: "Constructor can't be a generator.",
  120. DeclarationMissingInitializer: ({
  121. kind
  122. }) => `Missing initializer in ${kind} declaration.`,
  123. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  124. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  125. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  126. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  127. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  128. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  129. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  130. DeletePrivateField: "Deleting a private field is not allowed.",
  131. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  132. DuplicateConstructor: "Duplicate constructor in the same class.",
  133. DuplicateDefaultExport: "Only one default export allowed per module.",
  134. DuplicateExport: ({
  135. exportName
  136. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  137. DuplicateProto: "Redefinition of __proto__ property.",
  138. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  139. ElementAfterRest: "Rest element must be last element.",
  140. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  141. ExportBindingIsString: ({
  142. localName,
  143. exportName
  144. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  145. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  146. ForInOfLoopInitializer: ({
  147. type
  148. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  149. ForInUsing: "For-in loop may not start with 'using' declaration.",
  150. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  151. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  152. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  153. IllegalBreakContinue: ({
  154. type
  155. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  156. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  157. IllegalReturn: "'return' outside of function.",
  158. ImportBindingIsString: ({
  159. importName
  160. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  161. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  162. ImportCallArity: ({
  163. maxArgumentCount
  164. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  165. ImportCallNotNewExpression: "Cannot use new with import(...).",
  166. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  167. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  168. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  169. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  170. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  171. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  172. InvalidCodePoint: "Code point out of bounds.",
  173. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  174. InvalidDecimal: "Invalid decimal.",
  175. InvalidDigit: ({
  176. radix
  177. }) => `Expected number in radix ${radix}.`,
  178. InvalidEscapeSequence: "Bad character escape sequence.",
  179. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  180. InvalidEscapedReservedWord: ({
  181. reservedWord
  182. }) => `Escape sequence in keyword ${reservedWord}.`,
  183. InvalidIdentifier: ({
  184. identifierName
  185. }) => `Invalid identifier ${identifierName}.`,
  186. InvalidLhs: ({
  187. ancestor
  188. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  189. InvalidLhsBinding: ({
  190. ancestor
  191. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  192. InvalidNumber: "Invalid number.",
  193. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  194. InvalidOrUnexpectedToken: ({
  195. unexpected
  196. }) => `Unexpected character '${unexpected}'.`,
  197. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  198. InvalidPrivateFieldResolution: ({
  199. identifierName
  200. }) => `Private name #${identifierName} is not defined.`,
  201. InvalidPropertyBindingPattern: "Binding member expression.",
  202. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  203. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  204. LabelRedeclaration: ({
  205. labelName
  206. }) => `Label '${labelName}' is already declared.`,
  207. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  208. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  209. MalformedRegExpFlags: "Invalid regular expression flag.",
  210. MissingClassName: "A class name is required.",
  211. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  212. MissingSemicolon: "Missing semicolon.",
  213. MissingPlugin: ({
  214. missingPlugin
  215. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  216. MissingOneOfPlugins: ({
  217. missingPlugin
  218. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  219. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  220. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  221. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  222. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  223. ModuleAttributesWithDuplicateKeys: ({
  224. key
  225. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  226. ModuleExportNameHasLoneSurrogate: ({
  227. surrogateCharCode
  228. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  229. ModuleExportUndefined: ({
  230. localName
  231. }) => `Export '${localName}' is not defined.`,
  232. MultipleDefaultsInSwitch: "Multiple default clauses.",
  233. NewlineAfterThrow: "Illegal newline after throw.",
  234. NoCatchOrFinally: "Missing catch or finally clause.",
  235. NumberIdentifier: "Identifier directly after number.",
  236. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  237. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  238. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  239. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  240. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  241. ParamDupe: "Argument name clash.",
  242. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  243. PatternHasMethod: "Object pattern can't contain methods.",
  244. PrivateInExpectedIn: ({
  245. identifierName
  246. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  247. PrivateNameRedeclaration: ({
  248. identifierName
  249. }) => `Duplicate private name #${identifierName}.`,
  250. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  251. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  252. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  253. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  254. RestTrailingComma: "Unexpected trailing comma after rest element.",
  255. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  256. SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  257. StaticPrototype: "Classes may not have static property named prototype.",
  258. SuperNotAllowed: "`super()` is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class?",
  259. SuperPrivateField: "Private fields can't be accessed on super.",
  260. TrailingDecorator: "Decorators must be attached to a class element.",
  261. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  262. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  263. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  264. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  265. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  266. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  267. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  268. UnexpectedKeyword: ({
  269. keyword
  270. }) => `Unexpected keyword '${keyword}'.`,
  271. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  272. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  273. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  274. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  275. UnexpectedPrivateField: "Unexpected private name.",
  276. UnexpectedReservedWord: ({
  277. reservedWord
  278. }) => `Unexpected reserved word '${reservedWord}'.`,
  279. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  280. UnexpectedToken: ({
  281. expected,
  282. unexpected
  283. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  284. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  285. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
  286. UnsupportedBind: "Binding should be performed on object property.",
  287. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  288. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  289. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  290. UnsupportedMetaProperty: ({
  291. target,
  292. onlyValidPropertyName
  293. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  294. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  295. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  296. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  297. UnterminatedComment: "Unterminated comment.",
  298. UnterminatedRegExp: "Unterminated regular expression.",
  299. UnterminatedString: "Unterminated string constant.",
  300. UnterminatedTemplate: "Unterminated template.",
  301. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  302. VarRedeclaration: ({
  303. identifierName
  304. }) => `Identifier '${identifierName}' has already been declared.`,
  305. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  306. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  307. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  308. };
  309. var StrictModeErrors = {
  310. StrictDelete: "Deleting local variable in strict mode.",
  311. StrictEvalArguments: ({
  312. referenceName
  313. }) => `Assigning to '${referenceName}' in strict mode.`,
  314. StrictEvalArgumentsBinding: ({
  315. bindingName
  316. }) => `Binding '${bindingName}' in strict mode.`,
  317. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  318. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  319. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  320. StrictWith: "'with' in strict mode."
  321. };
  322. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  323. var PipelineOperatorErrors = {
  324. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  325. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  326. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  327. PipeTopicUnconfiguredToken: ({
  328. token
  329. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  330. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  331. PipeUnparenthesizedBody: ({
  332. type
  333. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  334. type
  335. })}; please wrap it in parentheses.`,
  336. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  337. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  338. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  339. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  340. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  341. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  342. };
  343. const _excluded$1 = ["toMessage"],
  344. _excluded2$1 = ["message"];
  345. function toParseErrorConstructor(_ref) {
  346. let {
  347. toMessage
  348. } = _ref,
  349. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  350. return function constructor({
  351. loc,
  352. details
  353. }) {
  354. return instantiate(SyntaxError, Object.assign({}, properties, {
  355. loc
  356. }), {
  357. clone(overrides = {}) {
  358. const loc = overrides.loc || {};
  359. return constructor({
  360. loc: new Position("line" in loc ? loc.line : this.loc.line, "column" in loc ? loc.column : this.loc.column, "index" in loc ? loc.index : this.loc.index),
  361. details: Object.assign({}, this.details, overrides.details)
  362. });
  363. },
  364. details: {
  365. value: details,
  366. enumerable: false
  367. },
  368. message: {
  369. get() {
  370. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  371. },
  372. set(value) {
  373. Object.defineProperty(this, "message", {
  374. value
  375. });
  376. }
  377. },
  378. pos: {
  379. reflect: "loc.index",
  380. enumerable: true
  381. },
  382. missingPlugin: "missingPlugin" in details && {
  383. reflect: "details.missingPlugin",
  384. enumerable: true
  385. }
  386. });
  387. };
  388. }
  389. function ParseErrorEnum(argument, syntaxPlugin) {
  390. if (Array.isArray(argument)) {
  391. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  392. }
  393. const ParseErrorConstructors = {};
  394. for (const reasonCode of Object.keys(argument)) {
  395. const template = argument[reasonCode];
  396. const _ref2 = typeof template === "string" ? {
  397. message: () => template
  398. } : typeof template === "function" ? {
  399. message: template
  400. } : template,
  401. {
  402. message
  403. } = _ref2,
  404. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
  405. const toMessage = typeof message === "string" ? () => message : message;
  406. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  407. code: ParseErrorCode.SyntaxError,
  408. reasonCode,
  409. toMessage
  410. }, syntaxPlugin ? {
  411. syntaxPlugin
  412. } : {}, rest));
  413. }
  414. return ParseErrorConstructors;
  415. }
  416. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  417. const {
  418. defineProperty
  419. } = Object;
  420. const toUnenumerable = (object, key) => defineProperty(object, key, {
  421. enumerable: false,
  422. value: object[key]
  423. });
  424. function toESTreeLocation(node) {
  425. node.loc.start && toUnenumerable(node.loc.start, "index");
  426. node.loc.end && toUnenumerable(node.loc.end, "index");
  427. return node;
  428. }
  429. var estree = (superClass => class ESTreeParserMixin extends superClass {
  430. parse() {
  431. const file = toESTreeLocation(super.parse());
  432. if (this.options.tokens) {
  433. file.tokens = file.tokens.map(toESTreeLocation);
  434. }
  435. return file;
  436. }
  437. parseRegExpLiteral({
  438. pattern,
  439. flags
  440. }) {
  441. let regex = null;
  442. try {
  443. regex = new RegExp(pattern, flags);
  444. } catch (e) {}
  445. const node = this.estreeParseLiteral(regex);
  446. node.regex = {
  447. pattern,
  448. flags
  449. };
  450. return node;
  451. }
  452. parseBigIntLiteral(value) {
  453. let bigInt;
  454. try {
  455. bigInt = BigInt(value);
  456. } catch (_unused) {
  457. bigInt = null;
  458. }
  459. const node = this.estreeParseLiteral(bigInt);
  460. node.bigint = String(node.value || value);
  461. return node;
  462. }
  463. parseDecimalLiteral(value) {
  464. const decimal = null;
  465. const node = this.estreeParseLiteral(decimal);
  466. node.decimal = String(node.value || value);
  467. return node;
  468. }
  469. estreeParseLiteral(value) {
  470. return this.parseLiteral(value, "Literal");
  471. }
  472. parseStringLiteral(value) {
  473. return this.estreeParseLiteral(value);
  474. }
  475. parseNumericLiteral(value) {
  476. return this.estreeParseLiteral(value);
  477. }
  478. parseNullLiteral() {
  479. return this.estreeParseLiteral(null);
  480. }
  481. parseBooleanLiteral(value) {
  482. return this.estreeParseLiteral(value);
  483. }
  484. directiveToStmt(directive) {
  485. const expression = directive.value;
  486. delete directive.value;
  487. expression.type = "Literal";
  488. expression.raw = expression.extra.raw;
  489. expression.value = expression.extra.expressionValue;
  490. const stmt = directive;
  491. stmt.type = "ExpressionStatement";
  492. stmt.expression = expression;
  493. stmt.directive = expression.extra.rawValue;
  494. delete expression.extra;
  495. return stmt;
  496. }
  497. initFunction(node, isAsync) {
  498. super.initFunction(node, isAsync);
  499. node.expression = false;
  500. }
  501. checkDeclaration(node) {
  502. if (node != null && this.isObjectProperty(node)) {
  503. this.checkDeclaration(node.value);
  504. } else {
  505. super.checkDeclaration(node);
  506. }
  507. }
  508. getObjectOrClassMethodParams(method) {
  509. return method.value.params;
  510. }
  511. isValidDirective(stmt) {
  512. var _stmt$expression$extr;
  513. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  514. }
  515. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  516. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  517. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  518. node.body = directiveStatements.concat(node.body);
  519. delete node.directives;
  520. }
  521. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  522. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  523. if (method.typeParameters) {
  524. method.value.typeParameters = method.typeParameters;
  525. delete method.typeParameters;
  526. }
  527. classBody.body.push(method);
  528. }
  529. parsePrivateName() {
  530. const node = super.parsePrivateName();
  531. {
  532. if (!this.getPluginOption("estree", "classFeatures")) {
  533. return node;
  534. }
  535. }
  536. return this.convertPrivateNameToPrivateIdentifier(node);
  537. }
  538. convertPrivateNameToPrivateIdentifier(node) {
  539. const name = super.getPrivateNameSV(node);
  540. node = node;
  541. delete node.id;
  542. node.name = name;
  543. node.type = "PrivateIdentifier";
  544. return node;
  545. }
  546. isPrivateName(node) {
  547. {
  548. if (!this.getPluginOption("estree", "classFeatures")) {
  549. return super.isPrivateName(node);
  550. }
  551. }
  552. return node.type === "PrivateIdentifier";
  553. }
  554. getPrivateNameSV(node) {
  555. {
  556. if (!this.getPluginOption("estree", "classFeatures")) {
  557. return super.getPrivateNameSV(node);
  558. }
  559. }
  560. return node.name;
  561. }
  562. parseLiteral(value, type) {
  563. const node = super.parseLiteral(value, type);
  564. node.raw = node.extra.raw;
  565. delete node.extra;
  566. return node;
  567. }
  568. parseFunctionBody(node, allowExpression, isMethod = false) {
  569. super.parseFunctionBody(node, allowExpression, isMethod);
  570. node.expression = node.body.type !== "BlockStatement";
  571. }
  572. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  573. let funcNode = this.startNode();
  574. funcNode.kind = node.kind;
  575. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  576. funcNode.type = "FunctionExpression";
  577. delete funcNode.kind;
  578. node.value = funcNode;
  579. if (type === "ClassPrivateMethod") {
  580. node.computed = false;
  581. }
  582. return this.finishNode(node, "MethodDefinition");
  583. }
  584. parseClassProperty(...args) {
  585. const propertyNode = super.parseClassProperty(...args);
  586. {
  587. if (!this.getPluginOption("estree", "classFeatures")) {
  588. return propertyNode;
  589. }
  590. }
  591. propertyNode.type = "PropertyDefinition";
  592. return propertyNode;
  593. }
  594. parseClassPrivateProperty(...args) {
  595. const propertyNode = super.parseClassPrivateProperty(...args);
  596. {
  597. if (!this.getPluginOption("estree", "classFeatures")) {
  598. return propertyNode;
  599. }
  600. }
  601. propertyNode.type = "PropertyDefinition";
  602. propertyNode.computed = false;
  603. return propertyNode;
  604. }
  605. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  606. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  607. if (node) {
  608. node.type = "Property";
  609. if (node.kind === "method") {
  610. node.kind = "init";
  611. }
  612. node.shorthand = false;
  613. }
  614. return node;
  615. }
  616. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  617. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  618. if (node) {
  619. node.kind = "init";
  620. node.type = "Property";
  621. }
  622. return node;
  623. }
  624. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  625. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  626. }
  627. isAssignable(node, isBinding) {
  628. if (node != null && this.isObjectProperty(node)) {
  629. return this.isAssignable(node.value, isBinding);
  630. }
  631. return super.isAssignable(node, isBinding);
  632. }
  633. toAssignable(node, isLHS = false) {
  634. if (node != null && this.isObjectProperty(node)) {
  635. const {
  636. key,
  637. value
  638. } = node;
  639. if (this.isPrivateName(key)) {
  640. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  641. }
  642. this.toAssignable(value, isLHS);
  643. } else {
  644. super.toAssignable(node, isLHS);
  645. }
  646. }
  647. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  648. if (prop.kind === "get" || prop.kind === "set") {
  649. this.raise(Errors.PatternHasAccessor, {
  650. at: prop.key
  651. });
  652. } else if (prop.method) {
  653. this.raise(Errors.PatternHasMethod, {
  654. at: prop.key
  655. });
  656. } else {
  657. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  658. }
  659. }
  660. finishCallExpression(unfinished, optional) {
  661. const node = super.finishCallExpression(unfinished, optional);
  662. if (node.callee.type === "Import") {
  663. node.type = "ImportExpression";
  664. node.source = node.arguments[0];
  665. if (this.hasPlugin("importAssertions")) {
  666. var _node$arguments$;
  667. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  668. }
  669. delete node.arguments;
  670. delete node.callee;
  671. }
  672. return node;
  673. }
  674. toReferencedArguments(node) {
  675. if (node.type === "ImportExpression") {
  676. return;
  677. }
  678. super.toReferencedArguments(node);
  679. }
  680. parseExport(unfinished, decorators) {
  681. const exportStartLoc = this.state.lastTokStartLoc;
  682. const node = super.parseExport(unfinished, decorators);
  683. switch (node.type) {
  684. case "ExportAllDeclaration":
  685. node.exported = null;
  686. break;
  687. case "ExportNamedDeclaration":
  688. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  689. node.type = "ExportAllDeclaration";
  690. node.exported = node.specifiers[0].exported;
  691. delete node.specifiers;
  692. }
  693. case "ExportDefaultDeclaration":
  694. {
  695. var _declaration$decorato;
  696. const {
  697. declaration
  698. } = node;
  699. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  700. this.resetStartLocation(node, exportStartLoc);
  701. }
  702. }
  703. break;
  704. }
  705. return node;
  706. }
  707. parseSubscript(base, startLoc, noCalls, state) {
  708. const node = super.parseSubscript(base, startLoc, noCalls, state);
  709. if (state.optionalChainMember) {
  710. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  711. node.type = node.type.substring(8);
  712. }
  713. if (state.stop) {
  714. const chain = this.startNodeAtNode(node);
  715. chain.expression = node;
  716. return this.finishNode(chain, "ChainExpression");
  717. }
  718. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  719. node.optional = false;
  720. }
  721. return node;
  722. }
  723. hasPropertyAsPrivateName(node) {
  724. if (node.type === "ChainExpression") {
  725. node = node.expression;
  726. }
  727. return super.hasPropertyAsPrivateName(node);
  728. }
  729. isObjectProperty(node) {
  730. return node.type === "Property" && node.kind === "init" && !node.method;
  731. }
  732. isObjectMethod(node) {
  733. return node.method || node.kind === "get" || node.kind === "set";
  734. }
  735. finishNodeAt(node, type, endLoc) {
  736. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  737. }
  738. resetStartLocation(node, startLoc) {
  739. super.resetStartLocation(node, startLoc);
  740. toESTreeLocation(node);
  741. }
  742. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  743. super.resetEndLocation(node, endLoc);
  744. toESTreeLocation(node);
  745. }
  746. });
  747. class TokContext {
  748. constructor(token, preserveSpace) {
  749. this.token = void 0;
  750. this.preserveSpace = void 0;
  751. this.token = token;
  752. this.preserveSpace = !!preserveSpace;
  753. }
  754. }
  755. const types = {
  756. brace: new TokContext("{"),
  757. j_oTag: new TokContext("<tag"),
  758. j_cTag: new TokContext("</tag"),
  759. j_expr: new TokContext("<tag>...</tag>", true)
  760. };
  761. {
  762. types.template = new TokContext("`", true);
  763. }
  764. const beforeExpr = true;
  765. const startsExpr = true;
  766. const isLoop = true;
  767. const isAssign = true;
  768. const prefix = true;
  769. const postfix = true;
  770. class ExportedTokenType {
  771. constructor(label, conf = {}) {
  772. this.label = void 0;
  773. this.keyword = void 0;
  774. this.beforeExpr = void 0;
  775. this.startsExpr = void 0;
  776. this.rightAssociative = void 0;
  777. this.isLoop = void 0;
  778. this.isAssign = void 0;
  779. this.prefix = void 0;
  780. this.postfix = void 0;
  781. this.binop = void 0;
  782. this.label = label;
  783. this.keyword = conf.keyword;
  784. this.beforeExpr = !!conf.beforeExpr;
  785. this.startsExpr = !!conf.startsExpr;
  786. this.rightAssociative = !!conf.rightAssociative;
  787. this.isLoop = !!conf.isLoop;
  788. this.isAssign = !!conf.isAssign;
  789. this.prefix = !!conf.prefix;
  790. this.postfix = !!conf.postfix;
  791. this.binop = conf.binop != null ? conf.binop : null;
  792. {
  793. this.updateContext = null;
  794. }
  795. }
  796. }
  797. const keywords$1 = new Map();
  798. function createKeyword(name, options = {}) {
  799. options.keyword = name;
  800. const token = createToken(name, options);
  801. keywords$1.set(name, token);
  802. return token;
  803. }
  804. function createBinop(name, binop) {
  805. return createToken(name, {
  806. beforeExpr,
  807. binop
  808. });
  809. }
  810. let tokenTypeCounter = -1;
  811. const tokenTypes = [];
  812. const tokenLabels = [];
  813. const tokenBinops = [];
  814. const tokenBeforeExprs = [];
  815. const tokenStartsExprs = [];
  816. const tokenPrefixes = [];
  817. function createToken(name, options = {}) {
  818. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  819. ++tokenTypeCounter;
  820. tokenLabels.push(name);
  821. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  822. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  823. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  824. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  825. tokenTypes.push(new ExportedTokenType(name, options));
  826. return tokenTypeCounter;
  827. }
  828. function createKeywordLike(name, options = {}) {
  829. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  830. ++tokenTypeCounter;
  831. keywords$1.set(name, tokenTypeCounter);
  832. tokenLabels.push(name);
  833. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  834. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  835. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  836. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  837. tokenTypes.push(new ExportedTokenType("name", options));
  838. return tokenTypeCounter;
  839. }
  840. const tt = {
  841. bracketL: createToken("[", {
  842. beforeExpr,
  843. startsExpr
  844. }),
  845. bracketHashL: createToken("#[", {
  846. beforeExpr,
  847. startsExpr
  848. }),
  849. bracketBarL: createToken("[|", {
  850. beforeExpr,
  851. startsExpr
  852. }),
  853. bracketR: createToken("]"),
  854. bracketBarR: createToken("|]"),
  855. braceL: createToken("{", {
  856. beforeExpr,
  857. startsExpr
  858. }),
  859. braceBarL: createToken("{|", {
  860. beforeExpr,
  861. startsExpr
  862. }),
  863. braceHashL: createToken("#{", {
  864. beforeExpr,
  865. startsExpr
  866. }),
  867. braceR: createToken("}"),
  868. braceBarR: createToken("|}"),
  869. parenL: createToken("(", {
  870. beforeExpr,
  871. startsExpr
  872. }),
  873. parenR: createToken(")"),
  874. comma: createToken(",", {
  875. beforeExpr
  876. }),
  877. semi: createToken(";", {
  878. beforeExpr
  879. }),
  880. colon: createToken(":", {
  881. beforeExpr
  882. }),
  883. doubleColon: createToken("::", {
  884. beforeExpr
  885. }),
  886. dot: createToken("."),
  887. question: createToken("?", {
  888. beforeExpr
  889. }),
  890. questionDot: createToken("?."),
  891. arrow: createToken("=>", {
  892. beforeExpr
  893. }),
  894. template: createToken("template"),
  895. ellipsis: createToken("...", {
  896. beforeExpr
  897. }),
  898. backQuote: createToken("`", {
  899. startsExpr
  900. }),
  901. dollarBraceL: createToken("${", {
  902. beforeExpr,
  903. startsExpr
  904. }),
  905. templateTail: createToken("...`", {
  906. startsExpr
  907. }),
  908. templateNonTail: createToken("...${", {
  909. beforeExpr,
  910. startsExpr
  911. }),
  912. at: createToken("@"),
  913. hash: createToken("#", {
  914. startsExpr
  915. }),
  916. interpreterDirective: createToken("#!..."),
  917. eq: createToken("=", {
  918. beforeExpr,
  919. isAssign
  920. }),
  921. assign: createToken("_=", {
  922. beforeExpr,
  923. isAssign
  924. }),
  925. slashAssign: createToken("_=", {
  926. beforeExpr,
  927. isAssign
  928. }),
  929. xorAssign: createToken("_=", {
  930. beforeExpr,
  931. isAssign
  932. }),
  933. moduloAssign: createToken("_=", {
  934. beforeExpr,
  935. isAssign
  936. }),
  937. incDec: createToken("++/--", {
  938. prefix,
  939. postfix,
  940. startsExpr
  941. }),
  942. bang: createToken("!", {
  943. beforeExpr,
  944. prefix,
  945. startsExpr
  946. }),
  947. tilde: createToken("~", {
  948. beforeExpr,
  949. prefix,
  950. startsExpr
  951. }),
  952. doubleCaret: createToken("^^", {
  953. startsExpr
  954. }),
  955. doubleAt: createToken("@@", {
  956. startsExpr
  957. }),
  958. pipeline: createBinop("|>", 0),
  959. nullishCoalescing: createBinop("??", 1),
  960. logicalOR: createBinop("||", 1),
  961. logicalAND: createBinop("&&", 2),
  962. bitwiseOR: createBinop("|", 3),
  963. bitwiseXOR: createBinop("^", 4),
  964. bitwiseAND: createBinop("&", 5),
  965. equality: createBinop("==/!=/===/!==", 6),
  966. lt: createBinop("</>/<=/>=", 7),
  967. gt: createBinop("</>/<=/>=", 7),
  968. relational: createBinop("</>/<=/>=", 7),
  969. bitShift: createBinop("<</>>/>>>", 8),
  970. bitShiftL: createBinop("<</>>/>>>", 8),
  971. bitShiftR: createBinop("<</>>/>>>", 8),
  972. plusMin: createToken("+/-", {
  973. beforeExpr,
  974. binop: 9,
  975. prefix,
  976. startsExpr
  977. }),
  978. modulo: createToken("%", {
  979. binop: 10,
  980. startsExpr
  981. }),
  982. star: createToken("*", {
  983. binop: 10
  984. }),
  985. slash: createBinop("/", 10),
  986. exponent: createToken("**", {
  987. beforeExpr,
  988. binop: 11,
  989. rightAssociative: true
  990. }),
  991. _in: createKeyword("in", {
  992. beforeExpr,
  993. binop: 7
  994. }),
  995. _instanceof: createKeyword("instanceof", {
  996. beforeExpr,
  997. binop: 7
  998. }),
  999. _break: createKeyword("break"),
  1000. _case: createKeyword("case", {
  1001. beforeExpr
  1002. }),
  1003. _catch: createKeyword("catch"),
  1004. _continue: createKeyword("continue"),
  1005. _debugger: createKeyword("debugger"),
  1006. _default: createKeyword("default", {
  1007. beforeExpr
  1008. }),
  1009. _else: createKeyword("else", {
  1010. beforeExpr
  1011. }),
  1012. _finally: createKeyword("finally"),
  1013. _function: createKeyword("function", {
  1014. startsExpr
  1015. }),
  1016. _if: createKeyword("if"),
  1017. _return: createKeyword("return", {
  1018. beforeExpr
  1019. }),
  1020. _switch: createKeyword("switch"),
  1021. _throw: createKeyword("throw", {
  1022. beforeExpr,
  1023. prefix,
  1024. startsExpr
  1025. }),
  1026. _try: createKeyword("try"),
  1027. _var: createKeyword("var"),
  1028. _const: createKeyword("const"),
  1029. _with: createKeyword("with"),
  1030. _new: createKeyword("new", {
  1031. beforeExpr,
  1032. startsExpr
  1033. }),
  1034. _this: createKeyword("this", {
  1035. startsExpr
  1036. }),
  1037. _super: createKeyword("super", {
  1038. startsExpr
  1039. }),
  1040. _class: createKeyword("class", {
  1041. startsExpr
  1042. }),
  1043. _extends: createKeyword("extends", {
  1044. beforeExpr
  1045. }),
  1046. _export: createKeyword("export"),
  1047. _import: createKeyword("import", {
  1048. startsExpr
  1049. }),
  1050. _null: createKeyword("null", {
  1051. startsExpr
  1052. }),
  1053. _true: createKeyword("true", {
  1054. startsExpr
  1055. }),
  1056. _false: createKeyword("false", {
  1057. startsExpr
  1058. }),
  1059. _typeof: createKeyword("typeof", {
  1060. beforeExpr,
  1061. prefix,
  1062. startsExpr
  1063. }),
  1064. _void: createKeyword("void", {
  1065. beforeExpr,
  1066. prefix,
  1067. startsExpr
  1068. }),
  1069. _delete: createKeyword("delete", {
  1070. beforeExpr,
  1071. prefix,
  1072. startsExpr
  1073. }),
  1074. _do: createKeyword("do", {
  1075. isLoop,
  1076. beforeExpr
  1077. }),
  1078. _for: createKeyword("for", {
  1079. isLoop
  1080. }),
  1081. _while: createKeyword("while", {
  1082. isLoop
  1083. }),
  1084. _as: createKeywordLike("as", {
  1085. startsExpr
  1086. }),
  1087. _assert: createKeywordLike("assert", {
  1088. startsExpr
  1089. }),
  1090. _async: createKeywordLike("async", {
  1091. startsExpr
  1092. }),
  1093. _await: createKeywordLike("await", {
  1094. startsExpr
  1095. }),
  1096. _from: createKeywordLike("from", {
  1097. startsExpr
  1098. }),
  1099. _get: createKeywordLike("get", {
  1100. startsExpr
  1101. }),
  1102. _let: createKeywordLike("let", {
  1103. startsExpr
  1104. }),
  1105. _meta: createKeywordLike("meta", {
  1106. startsExpr
  1107. }),
  1108. _of: createKeywordLike("of", {
  1109. startsExpr
  1110. }),
  1111. _sent: createKeywordLike("sent", {
  1112. startsExpr
  1113. }),
  1114. _set: createKeywordLike("set", {
  1115. startsExpr
  1116. }),
  1117. _static: createKeywordLike("static", {
  1118. startsExpr
  1119. }),
  1120. _using: createKeywordLike("using", {
  1121. startsExpr
  1122. }),
  1123. _yield: createKeywordLike("yield", {
  1124. startsExpr
  1125. }),
  1126. _asserts: createKeywordLike("asserts", {
  1127. startsExpr
  1128. }),
  1129. _checks: createKeywordLike("checks", {
  1130. startsExpr
  1131. }),
  1132. _exports: createKeywordLike("exports", {
  1133. startsExpr
  1134. }),
  1135. _global: createKeywordLike("global", {
  1136. startsExpr
  1137. }),
  1138. _implements: createKeywordLike("implements", {
  1139. startsExpr
  1140. }),
  1141. _intrinsic: createKeywordLike("intrinsic", {
  1142. startsExpr
  1143. }),
  1144. _infer: createKeywordLike("infer", {
  1145. startsExpr
  1146. }),
  1147. _is: createKeywordLike("is", {
  1148. startsExpr
  1149. }),
  1150. _mixins: createKeywordLike("mixins", {
  1151. startsExpr
  1152. }),
  1153. _proto: createKeywordLike("proto", {
  1154. startsExpr
  1155. }),
  1156. _require: createKeywordLike("require", {
  1157. startsExpr
  1158. }),
  1159. _satisfies: createKeywordLike("satisfies", {
  1160. startsExpr
  1161. }),
  1162. _keyof: createKeywordLike("keyof", {
  1163. startsExpr
  1164. }),
  1165. _readonly: createKeywordLike("readonly", {
  1166. startsExpr
  1167. }),
  1168. _unique: createKeywordLike("unique", {
  1169. startsExpr
  1170. }),
  1171. _abstract: createKeywordLike("abstract", {
  1172. startsExpr
  1173. }),
  1174. _declare: createKeywordLike("declare", {
  1175. startsExpr
  1176. }),
  1177. _enum: createKeywordLike("enum", {
  1178. startsExpr
  1179. }),
  1180. _module: createKeywordLike("module", {
  1181. startsExpr
  1182. }),
  1183. _namespace: createKeywordLike("namespace", {
  1184. startsExpr
  1185. }),
  1186. _interface: createKeywordLike("interface", {
  1187. startsExpr
  1188. }),
  1189. _type: createKeywordLike("type", {
  1190. startsExpr
  1191. }),
  1192. _opaque: createKeywordLike("opaque", {
  1193. startsExpr
  1194. }),
  1195. name: createToken("name", {
  1196. startsExpr
  1197. }),
  1198. string: createToken("string", {
  1199. startsExpr
  1200. }),
  1201. num: createToken("num", {
  1202. startsExpr
  1203. }),
  1204. bigint: createToken("bigint", {
  1205. startsExpr
  1206. }),
  1207. decimal: createToken("decimal", {
  1208. startsExpr
  1209. }),
  1210. regexp: createToken("regexp", {
  1211. startsExpr
  1212. }),
  1213. privateName: createToken("#name", {
  1214. startsExpr
  1215. }),
  1216. eof: createToken("eof"),
  1217. jsxName: createToken("jsxName"),
  1218. jsxText: createToken("jsxText", {
  1219. beforeExpr: true
  1220. }),
  1221. jsxTagStart: createToken("jsxTagStart", {
  1222. startsExpr: true
  1223. }),
  1224. jsxTagEnd: createToken("jsxTagEnd"),
  1225. placeholder: createToken("%%", {
  1226. startsExpr: true
  1227. })
  1228. };
  1229. function tokenIsIdentifier(token) {
  1230. return token >= 93 && token <= 130;
  1231. }
  1232. function tokenKeywordOrIdentifierIsKeyword(token) {
  1233. return token <= 92;
  1234. }
  1235. function tokenIsKeywordOrIdentifier(token) {
  1236. return token >= 58 && token <= 130;
  1237. }
  1238. function tokenIsLiteralPropertyName(token) {
  1239. return token >= 58 && token <= 134;
  1240. }
  1241. function tokenComesBeforeExpression(token) {
  1242. return tokenBeforeExprs[token];
  1243. }
  1244. function tokenCanStartExpression(token) {
  1245. return tokenStartsExprs[token];
  1246. }
  1247. function tokenIsAssignment(token) {
  1248. return token >= 29 && token <= 33;
  1249. }
  1250. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1251. return token >= 127 && token <= 129;
  1252. }
  1253. function tokenIsLoop(token) {
  1254. return token >= 90 && token <= 92;
  1255. }
  1256. function tokenIsKeyword(token) {
  1257. return token >= 58 && token <= 92;
  1258. }
  1259. function tokenIsOperator(token) {
  1260. return token >= 39 && token <= 59;
  1261. }
  1262. function tokenIsPostfix(token) {
  1263. return token === 34;
  1264. }
  1265. function tokenIsPrefix(token) {
  1266. return tokenPrefixes[token];
  1267. }
  1268. function tokenIsTSTypeOperator(token) {
  1269. return token >= 119 && token <= 121;
  1270. }
  1271. function tokenIsTSDeclarationStart(token) {
  1272. return token >= 122 && token <= 128;
  1273. }
  1274. function tokenLabelName(token) {
  1275. return tokenLabels[token];
  1276. }
  1277. function tokenOperatorPrecedence(token) {
  1278. return tokenBinops[token];
  1279. }
  1280. function tokenIsRightAssociative(token) {
  1281. return token === 57;
  1282. }
  1283. function tokenIsTemplate(token) {
  1284. return token >= 24 && token <= 25;
  1285. }
  1286. function getExportedToken(token) {
  1287. return tokenTypes[token];
  1288. }
  1289. {
  1290. tokenTypes[8].updateContext = context => {
  1291. context.pop();
  1292. };
  1293. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1294. context.push(types.brace);
  1295. };
  1296. tokenTypes[22].updateContext = context => {
  1297. if (context[context.length - 1] === types.template) {
  1298. context.pop();
  1299. } else {
  1300. context.push(types.template);
  1301. }
  1302. };
  1303. tokenTypes[140].updateContext = context => {
  1304. context.push(types.j_expr, types.j_oTag);
  1305. };
  1306. }
  1307. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  1308. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  1309. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1310. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1311. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1312. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938, 6, 4191];
  1313. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1314. function isInAstralSet(code, set) {
  1315. let pos = 0x10000;
  1316. for (let i = 0, length = set.length; i < length; i += 2) {
  1317. pos += set[i];
  1318. if (pos > code) return false;
  1319. pos += set[i + 1];
  1320. if (pos >= code) return true;
  1321. }
  1322. return false;
  1323. }
  1324. function isIdentifierStart(code) {
  1325. if (code < 65) return code === 36;
  1326. if (code <= 90) return true;
  1327. if (code < 97) return code === 95;
  1328. if (code <= 122) return true;
  1329. if (code <= 0xffff) {
  1330. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1331. }
  1332. return isInAstralSet(code, astralIdentifierStartCodes);
  1333. }
  1334. function isIdentifierChar(code) {
  1335. if (code < 48) return code === 36;
  1336. if (code < 58) return true;
  1337. if (code < 65) return false;
  1338. if (code <= 90) return true;
  1339. if (code < 97) return code === 95;
  1340. if (code <= 122) return true;
  1341. if (code <= 0xffff) {
  1342. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1343. }
  1344. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1345. }
  1346. const reservedWords = {
  1347. 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"],
  1348. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1349. strictBind: ["eval", "arguments"]
  1350. };
  1351. const keywords = new Set(reservedWords.keyword);
  1352. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1353. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1354. function isReservedWord(word, inModule) {
  1355. return inModule && word === "await" || word === "enum";
  1356. }
  1357. function isStrictReservedWord(word, inModule) {
  1358. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1359. }
  1360. function isStrictBindOnlyReservedWord(word) {
  1361. return reservedWordsStrictBindSet.has(word);
  1362. }
  1363. function isStrictBindReservedWord(word, inModule) {
  1364. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1365. }
  1366. function isKeyword(word) {
  1367. return keywords.has(word);
  1368. }
  1369. function isIteratorStart(current, next, next2) {
  1370. return current === 64 && next === 64 && isIdentifierStart(next2);
  1371. }
  1372. 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"]);
  1373. function canBeReservedWord(word) {
  1374. return reservedWordLikeSet.has(word);
  1375. }
  1376. const SCOPE_OTHER = 0b000000000,
  1377. SCOPE_PROGRAM = 0b000000001,
  1378. SCOPE_FUNCTION = 0b000000010,
  1379. SCOPE_ARROW = 0b000000100,
  1380. SCOPE_SIMPLE_CATCH = 0b000001000,
  1381. SCOPE_SUPER = 0b000010000,
  1382. SCOPE_DIRECT_SUPER = 0b000100000,
  1383. SCOPE_CLASS = 0b001000000,
  1384. SCOPE_STATIC_BLOCK = 0b010000000,
  1385. SCOPE_TS_MODULE = 0b100000000,
  1386. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_STATIC_BLOCK | SCOPE_TS_MODULE;
  1387. const BIND_KIND_VALUE = 0b0000000000001,
  1388. BIND_KIND_TYPE = 0b0000000000010,
  1389. BIND_SCOPE_VAR = 0b0000000000100,
  1390. BIND_SCOPE_LEXICAL = 0b0000000001000,
  1391. BIND_SCOPE_FUNCTION = 0b0000000010000,
  1392. BIND_FLAGS_NONE = 0b00000001000000,
  1393. BIND_FLAGS_CLASS = 0b00000010000000,
  1394. BIND_FLAGS_TS_ENUM = 0b00000100000000,
  1395. BIND_FLAGS_TS_CONST_ENUM = 0b00001000000000,
  1396. BIND_FLAGS_TS_EXPORT_ONLY = 0b00010000000000,
  1397. BIND_FLAGS_FLOW_DECLARE_FN = 0b00100000000000,
  1398. BIND_FLAGS_TS_IMPORT = 0b01000000000000,
  1399. BIND_FLAGS_NO_LET_IN_LEXICAL = 0b10000000000000;
  1400. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1401. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1402. BIND_CATCH_PARAM = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1403. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1404. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1405. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1406. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1407. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1408. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1409. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1410. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1411. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1412. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1413. BIND_TS_TYPE_IMPORT = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
  1414. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1415. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1416. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1417. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1418. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1419. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1420. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1421. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1422. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1423. CLASS_ELEMENT_OTHER = 0;
  1424. class Scope {
  1425. constructor(flags) {
  1426. this.var = new Set();
  1427. this.lexical = new Set();
  1428. this.functions = new Set();
  1429. this.flags = flags;
  1430. }
  1431. }
  1432. class ScopeHandler {
  1433. constructor(parser, inModule) {
  1434. this.parser = void 0;
  1435. this.scopeStack = [];
  1436. this.inModule = void 0;
  1437. this.undefinedExports = new Map();
  1438. this.parser = parser;
  1439. this.inModule = inModule;
  1440. }
  1441. get inTopLevel() {
  1442. return (this.currentScope().flags & SCOPE_PROGRAM) > 0;
  1443. }
  1444. get inFunction() {
  1445. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1446. }
  1447. get allowSuper() {
  1448. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1449. }
  1450. get allowDirectSuper() {
  1451. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1452. }
  1453. get inClass() {
  1454. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1455. }
  1456. get inClassAndNotInNonArrowFunction() {
  1457. const flags = this.currentThisScopeFlags();
  1458. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1459. }
  1460. get inStaticBlock() {
  1461. for (let i = this.scopeStack.length - 1;; i--) {
  1462. const {
  1463. flags
  1464. } = this.scopeStack[i];
  1465. if (flags & SCOPE_STATIC_BLOCK) {
  1466. return true;
  1467. }
  1468. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1469. return false;
  1470. }
  1471. }
  1472. }
  1473. get inNonArrowFunction() {
  1474. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1475. }
  1476. get treatFunctionsAsVar() {
  1477. return this.treatFunctionsAsVarInScope(this.currentScope());
  1478. }
  1479. createScope(flags) {
  1480. return new Scope(flags);
  1481. }
  1482. enter(flags) {
  1483. this.scopeStack.push(this.createScope(flags));
  1484. }
  1485. exit() {
  1486. const scope = this.scopeStack.pop();
  1487. return scope.flags;
  1488. }
  1489. treatFunctionsAsVarInScope(scope) {
  1490. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  1491. }
  1492. declareName(name, bindingType, loc) {
  1493. let scope = this.currentScope();
  1494. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1495. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1496. if (bindingType & BIND_SCOPE_FUNCTION) {
  1497. scope.functions.add(name);
  1498. } else {
  1499. scope.lexical.add(name);
  1500. }
  1501. if (bindingType & BIND_SCOPE_LEXICAL) {
  1502. this.maybeExportDefined(scope, name);
  1503. }
  1504. } else if (bindingType & BIND_SCOPE_VAR) {
  1505. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1506. scope = this.scopeStack[i];
  1507. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1508. scope.var.add(name);
  1509. this.maybeExportDefined(scope, name);
  1510. if (scope.flags & SCOPE_VAR) break;
  1511. }
  1512. }
  1513. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1514. this.undefinedExports.delete(name);
  1515. }
  1516. }
  1517. maybeExportDefined(scope, name) {
  1518. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1519. this.undefinedExports.delete(name);
  1520. }
  1521. }
  1522. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1523. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1524. this.parser.raise(Errors.VarRedeclaration, {
  1525. at: loc,
  1526. identifierName: name
  1527. });
  1528. }
  1529. }
  1530. isRedeclaredInScope(scope, name, bindingType) {
  1531. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1532. if (bindingType & BIND_SCOPE_LEXICAL) {
  1533. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1534. }
  1535. if (bindingType & BIND_SCOPE_FUNCTION) {
  1536. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1537. }
  1538. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1539. }
  1540. checkLocalExport(id) {
  1541. const {
  1542. name
  1543. } = id;
  1544. const topLevelScope = this.scopeStack[0];
  1545. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1546. this.undefinedExports.set(name, id.loc.start);
  1547. }
  1548. }
  1549. currentScope() {
  1550. return this.scopeStack[this.scopeStack.length - 1];
  1551. }
  1552. currentVarScopeFlags() {
  1553. for (let i = this.scopeStack.length - 1;; i--) {
  1554. const {
  1555. flags
  1556. } = this.scopeStack[i];
  1557. if (flags & SCOPE_VAR) {
  1558. return flags;
  1559. }
  1560. }
  1561. }
  1562. currentThisScopeFlags() {
  1563. for (let i = this.scopeStack.length - 1;; i--) {
  1564. const {
  1565. flags
  1566. } = this.scopeStack[i];
  1567. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1568. return flags;
  1569. }
  1570. }
  1571. }
  1572. }
  1573. class FlowScope extends Scope {
  1574. constructor(...args) {
  1575. super(...args);
  1576. this.declareFunctions = new Set();
  1577. }
  1578. }
  1579. class FlowScopeHandler extends ScopeHandler {
  1580. createScope(flags) {
  1581. return new FlowScope(flags);
  1582. }
  1583. declareName(name, bindingType, loc) {
  1584. const scope = this.currentScope();
  1585. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1586. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1587. this.maybeExportDefined(scope, name);
  1588. scope.declareFunctions.add(name);
  1589. return;
  1590. }
  1591. super.declareName(name, bindingType, loc);
  1592. }
  1593. isRedeclaredInScope(scope, name, bindingType) {
  1594. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1595. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1596. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1597. }
  1598. return false;
  1599. }
  1600. checkLocalExport(id) {
  1601. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1602. super.checkLocalExport(id);
  1603. }
  1604. }
  1605. }
  1606. class BaseParser {
  1607. constructor() {
  1608. this.sawUnambiguousESM = false;
  1609. this.ambiguousScriptDifferentAst = false;
  1610. }
  1611. hasPlugin(pluginConfig) {
  1612. if (typeof pluginConfig === "string") {
  1613. return this.plugins.has(pluginConfig);
  1614. } else {
  1615. const [pluginName, pluginOptions] = pluginConfig;
  1616. if (!this.hasPlugin(pluginName)) {
  1617. return false;
  1618. }
  1619. const actualOptions = this.plugins.get(pluginName);
  1620. for (const key of Object.keys(pluginOptions)) {
  1621. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1622. return false;
  1623. }
  1624. }
  1625. return true;
  1626. }
  1627. }
  1628. getPluginOption(plugin, name) {
  1629. var _this$plugins$get;
  1630. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1631. }
  1632. }
  1633. function setTrailingComments(node, comments) {
  1634. if (node.trailingComments === undefined) {
  1635. node.trailingComments = comments;
  1636. } else {
  1637. node.trailingComments.unshift(...comments);
  1638. }
  1639. }
  1640. function setLeadingComments(node, comments) {
  1641. if (node.leadingComments === undefined) {
  1642. node.leadingComments = comments;
  1643. } else {
  1644. node.leadingComments.unshift(...comments);
  1645. }
  1646. }
  1647. function setInnerComments(node, comments) {
  1648. if (node.innerComments === undefined) {
  1649. node.innerComments = comments;
  1650. } else {
  1651. node.innerComments.unshift(...comments);
  1652. }
  1653. }
  1654. function adjustInnerComments(node, elements, commentWS) {
  1655. let lastElement = null;
  1656. let i = elements.length;
  1657. while (lastElement === null && i > 0) {
  1658. lastElement = elements[--i];
  1659. }
  1660. if (lastElement === null || lastElement.start > commentWS.start) {
  1661. setInnerComments(node, commentWS.comments);
  1662. } else {
  1663. setTrailingComments(lastElement, commentWS.comments);
  1664. }
  1665. }
  1666. class CommentsParser extends BaseParser {
  1667. addComment(comment) {
  1668. if (this.filename) comment.loc.filename = this.filename;
  1669. this.state.comments.push(comment);
  1670. }
  1671. processComment(node) {
  1672. const {
  1673. commentStack
  1674. } = this.state;
  1675. const commentStackLength = commentStack.length;
  1676. if (commentStackLength === 0) return;
  1677. let i = commentStackLength - 1;
  1678. const lastCommentWS = commentStack[i];
  1679. if (lastCommentWS.start === node.end) {
  1680. lastCommentWS.leadingNode = node;
  1681. i--;
  1682. }
  1683. const {
  1684. start: nodeStart
  1685. } = node;
  1686. for (; i >= 0; i--) {
  1687. const commentWS = commentStack[i];
  1688. const commentEnd = commentWS.end;
  1689. if (commentEnd > nodeStart) {
  1690. commentWS.containingNode = node;
  1691. this.finalizeComment(commentWS);
  1692. commentStack.splice(i, 1);
  1693. } else {
  1694. if (commentEnd === nodeStart) {
  1695. commentWS.trailingNode = node;
  1696. }
  1697. break;
  1698. }
  1699. }
  1700. }
  1701. finalizeComment(commentWS) {
  1702. const {
  1703. comments
  1704. } = commentWS;
  1705. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1706. if (commentWS.leadingNode !== null) {
  1707. setTrailingComments(commentWS.leadingNode, comments);
  1708. }
  1709. if (commentWS.trailingNode !== null) {
  1710. setLeadingComments(commentWS.trailingNode, comments);
  1711. }
  1712. } else {
  1713. const {
  1714. containingNode: node,
  1715. start: commentStart
  1716. } = commentWS;
  1717. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1718. switch (node.type) {
  1719. case "ObjectExpression":
  1720. case "ObjectPattern":
  1721. case "RecordExpression":
  1722. adjustInnerComments(node, node.properties, commentWS);
  1723. break;
  1724. case "CallExpression":
  1725. case "OptionalCallExpression":
  1726. adjustInnerComments(node, node.arguments, commentWS);
  1727. break;
  1728. case "FunctionDeclaration":
  1729. case "FunctionExpression":
  1730. case "ArrowFunctionExpression":
  1731. case "ObjectMethod":
  1732. case "ClassMethod":
  1733. case "ClassPrivateMethod":
  1734. adjustInnerComments(node, node.params, commentWS);
  1735. break;
  1736. case "ArrayExpression":
  1737. case "ArrayPattern":
  1738. case "TupleExpression":
  1739. adjustInnerComments(node, node.elements, commentWS);
  1740. break;
  1741. case "ExportNamedDeclaration":
  1742. case "ImportDeclaration":
  1743. adjustInnerComments(node, node.specifiers, commentWS);
  1744. break;
  1745. default:
  1746. {
  1747. setInnerComments(node, comments);
  1748. }
  1749. }
  1750. } else {
  1751. setInnerComments(node, comments);
  1752. }
  1753. }
  1754. }
  1755. finalizeRemainingComments() {
  1756. const {
  1757. commentStack
  1758. } = this.state;
  1759. for (let i = commentStack.length - 1; i >= 0; i--) {
  1760. this.finalizeComment(commentStack[i]);
  1761. }
  1762. this.state.commentStack = [];
  1763. }
  1764. resetPreviousNodeTrailingComments(node) {
  1765. const {
  1766. commentStack
  1767. } = this.state;
  1768. const {
  1769. length
  1770. } = commentStack;
  1771. if (length === 0) return;
  1772. const commentWS = commentStack[length - 1];
  1773. if (commentWS.leadingNode === node) {
  1774. commentWS.leadingNode = null;
  1775. }
  1776. }
  1777. takeSurroundingComments(node, start, end) {
  1778. const {
  1779. commentStack
  1780. } = this.state;
  1781. const commentStackLength = commentStack.length;
  1782. if (commentStackLength === 0) return;
  1783. let i = commentStackLength - 1;
  1784. for (; i >= 0; i--) {
  1785. const commentWS = commentStack[i];
  1786. const commentEnd = commentWS.end;
  1787. const commentStart = commentWS.start;
  1788. if (commentStart === end) {
  1789. commentWS.leadingNode = node;
  1790. } else if (commentEnd === start) {
  1791. commentWS.trailingNode = node;
  1792. } else if (commentEnd < start) {
  1793. break;
  1794. }
  1795. }
  1796. }
  1797. }
  1798. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1799. const lineBreakG = new RegExp(lineBreak.source, "g");
  1800. function isNewLine(code) {
  1801. switch (code) {
  1802. case 10:
  1803. case 13:
  1804. case 8232:
  1805. case 8233:
  1806. return true;
  1807. default:
  1808. return false;
  1809. }
  1810. }
  1811. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1812. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1813. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1814. function isWhitespace(code) {
  1815. switch (code) {
  1816. case 0x0009:
  1817. case 0x000b:
  1818. case 0x000c:
  1819. case 32:
  1820. case 160:
  1821. case 5760:
  1822. case 0x2000:
  1823. case 0x2001:
  1824. case 0x2002:
  1825. case 0x2003:
  1826. case 0x2004:
  1827. case 0x2005:
  1828. case 0x2006:
  1829. case 0x2007:
  1830. case 0x2008:
  1831. case 0x2009:
  1832. case 0x200a:
  1833. case 0x202f:
  1834. case 0x205f:
  1835. case 0x3000:
  1836. case 0xfeff:
  1837. return true;
  1838. default:
  1839. return false;
  1840. }
  1841. }
  1842. class State {
  1843. constructor() {
  1844. this.strict = void 0;
  1845. this.curLine = void 0;
  1846. this.lineStart = void 0;
  1847. this.startLoc = void 0;
  1848. this.endLoc = void 0;
  1849. this.errors = [];
  1850. this.potentialArrowAt = -1;
  1851. this.noArrowAt = [];
  1852. this.noArrowParamsConversionAt = [];
  1853. this.maybeInArrowParameters = false;
  1854. this.inType = false;
  1855. this.noAnonFunctionType = false;
  1856. this.hasFlowComment = false;
  1857. this.isAmbientContext = false;
  1858. this.inAbstractClass = false;
  1859. this.inDisallowConditionalTypesContext = false;
  1860. this.topicContext = {
  1861. maxNumOfResolvableTopics: 0,
  1862. maxTopicIndex: null
  1863. };
  1864. this.soloAwait = false;
  1865. this.inFSharpPipelineDirectBody = false;
  1866. this.labels = [];
  1867. this.comments = [];
  1868. this.commentStack = [];
  1869. this.pos = 0;
  1870. this.type = 137;
  1871. this.value = null;
  1872. this.start = 0;
  1873. this.end = 0;
  1874. this.lastTokEndLoc = null;
  1875. this.lastTokStartLoc = null;
  1876. this.lastTokStart = 0;
  1877. this.context = [types.brace];
  1878. this.canStartJSXElement = true;
  1879. this.containsEsc = false;
  1880. this.firstInvalidTemplateEscapePos = null;
  1881. this.strictErrors = new Map();
  1882. this.tokensLength = 0;
  1883. }
  1884. init({
  1885. strictMode,
  1886. sourceType,
  1887. startLine,
  1888. startColumn
  1889. }) {
  1890. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1891. this.curLine = startLine;
  1892. this.lineStart = -startColumn;
  1893. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1894. }
  1895. curPosition() {
  1896. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1897. }
  1898. clone(skipArrays) {
  1899. const state = new State();
  1900. const keys = Object.keys(this);
  1901. for (let i = 0, length = keys.length; i < length; i++) {
  1902. const key = keys[i];
  1903. let val = this[key];
  1904. if (!skipArrays && Array.isArray(val)) {
  1905. val = val.slice();
  1906. }
  1907. state[key] = val;
  1908. }
  1909. return state;
  1910. }
  1911. }
  1912. var _isDigit = function isDigit(code) {
  1913. return code >= 48 && code <= 57;
  1914. };
  1915. const forbiddenNumericSeparatorSiblings = {
  1916. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1917. hex: new Set([46, 88, 95, 120])
  1918. };
  1919. const isAllowedNumericSeparatorSibling = {
  1920. bin: ch => ch === 48 || ch === 49,
  1921. oct: ch => ch >= 48 && ch <= 55,
  1922. dec: ch => ch >= 48 && ch <= 57,
  1923. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1924. };
  1925. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1926. const initialPos = pos;
  1927. const initialLineStart = lineStart;
  1928. const initialCurLine = curLine;
  1929. let out = "";
  1930. let firstInvalidLoc = null;
  1931. let chunkStart = pos;
  1932. const {
  1933. length
  1934. } = input;
  1935. for (;;) {
  1936. if (pos >= length) {
  1937. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1938. out += input.slice(chunkStart, pos);
  1939. break;
  1940. }
  1941. const ch = input.charCodeAt(pos);
  1942. if (isStringEnd(type, ch, input, pos)) {
  1943. out += input.slice(chunkStart, pos);
  1944. break;
  1945. }
  1946. if (ch === 92) {
  1947. out += input.slice(chunkStart, pos);
  1948. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  1949. if (res.ch === null && !firstInvalidLoc) {
  1950. firstInvalidLoc = {
  1951. pos,
  1952. lineStart,
  1953. curLine
  1954. };
  1955. } else {
  1956. out += res.ch;
  1957. }
  1958. ({
  1959. pos,
  1960. lineStart,
  1961. curLine
  1962. } = res);
  1963. chunkStart = pos;
  1964. } else if (ch === 8232 || ch === 8233) {
  1965. ++pos;
  1966. ++curLine;
  1967. lineStart = pos;
  1968. } else if (ch === 10 || ch === 13) {
  1969. if (type === "template") {
  1970. out += input.slice(chunkStart, pos) + "\n";
  1971. ++pos;
  1972. if (ch === 13 && input.charCodeAt(pos) === 10) {
  1973. ++pos;
  1974. }
  1975. ++curLine;
  1976. chunkStart = lineStart = pos;
  1977. } else {
  1978. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1979. }
  1980. } else {
  1981. ++pos;
  1982. }
  1983. }
  1984. return {
  1985. pos,
  1986. str: out,
  1987. firstInvalidLoc,
  1988. lineStart,
  1989. curLine,
  1990. containsInvalid: !!firstInvalidLoc
  1991. };
  1992. }
  1993. function isStringEnd(type, ch, input, pos) {
  1994. if (type === "template") {
  1995. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  1996. }
  1997. return ch === (type === "double" ? 34 : 39);
  1998. }
  1999. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  2000. const throwOnInvalid = !inTemplate;
  2001. pos++;
  2002. const res = ch => ({
  2003. pos,
  2004. ch,
  2005. lineStart,
  2006. curLine
  2007. });
  2008. const ch = input.charCodeAt(pos++);
  2009. switch (ch) {
  2010. case 110:
  2011. return res("\n");
  2012. case 114:
  2013. return res("\r");
  2014. case 120:
  2015. {
  2016. let code;
  2017. ({
  2018. code,
  2019. pos
  2020. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  2021. return res(code === null ? null : String.fromCharCode(code));
  2022. }
  2023. case 117:
  2024. {
  2025. let code;
  2026. ({
  2027. code,
  2028. pos
  2029. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2030. return res(code === null ? null : String.fromCodePoint(code));
  2031. }
  2032. case 116:
  2033. return res("\t");
  2034. case 98:
  2035. return res("\b");
  2036. case 118:
  2037. return res("\u000b");
  2038. case 102:
  2039. return res("\f");
  2040. case 13:
  2041. if (input.charCodeAt(pos) === 10) {
  2042. ++pos;
  2043. }
  2044. case 10:
  2045. lineStart = pos;
  2046. ++curLine;
  2047. case 8232:
  2048. case 8233:
  2049. return res("");
  2050. case 56:
  2051. case 57:
  2052. if (inTemplate) {
  2053. return res(null);
  2054. } else {
  2055. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2056. }
  2057. default:
  2058. if (ch >= 48 && ch <= 55) {
  2059. const startPos = pos - 1;
  2060. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  2061. let octalStr = match[0];
  2062. let octal = parseInt(octalStr, 8);
  2063. if (octal > 255) {
  2064. octalStr = octalStr.slice(0, -1);
  2065. octal = parseInt(octalStr, 8);
  2066. }
  2067. pos += octalStr.length - 1;
  2068. const next = input.charCodeAt(pos);
  2069. if (octalStr !== "0" || next === 56 || next === 57) {
  2070. if (inTemplate) {
  2071. return res(null);
  2072. } else {
  2073. errors.strictNumericEscape(startPos, lineStart, curLine);
  2074. }
  2075. }
  2076. return res(String.fromCharCode(octal));
  2077. }
  2078. return res(String.fromCharCode(ch));
  2079. }
  2080. }
  2081. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2082. const initialPos = pos;
  2083. let n;
  2084. ({
  2085. n,
  2086. pos
  2087. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2088. if (n === null) {
  2089. if (throwOnInvalid) {
  2090. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2091. } else {
  2092. pos = initialPos - 1;
  2093. }
  2094. }
  2095. return {
  2096. code: n,
  2097. pos
  2098. };
  2099. }
  2100. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2101. const start = pos;
  2102. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2103. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2104. let invalid = false;
  2105. let total = 0;
  2106. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2107. const code = input.charCodeAt(pos);
  2108. let val;
  2109. if (code === 95 && allowNumSeparator !== "bail") {
  2110. const prev = input.charCodeAt(pos - 1);
  2111. const next = input.charCodeAt(pos + 1);
  2112. if (!allowNumSeparator) {
  2113. if (bailOnError) return {
  2114. n: null,
  2115. pos
  2116. };
  2117. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2118. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2119. if (bailOnError) return {
  2120. n: null,
  2121. pos
  2122. };
  2123. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2124. }
  2125. ++pos;
  2126. continue;
  2127. }
  2128. if (code >= 97) {
  2129. val = code - 97 + 10;
  2130. } else if (code >= 65) {
  2131. val = code - 65 + 10;
  2132. } else if (_isDigit(code)) {
  2133. val = code - 48;
  2134. } else {
  2135. val = Infinity;
  2136. }
  2137. if (val >= radix) {
  2138. if (val <= 9 && bailOnError) {
  2139. return {
  2140. n: null,
  2141. pos
  2142. };
  2143. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2144. val = 0;
  2145. } else if (forceLen) {
  2146. val = 0;
  2147. invalid = true;
  2148. } else {
  2149. break;
  2150. }
  2151. }
  2152. ++pos;
  2153. total = total * radix + val;
  2154. }
  2155. if (pos === start || len != null && pos - start !== len || invalid) {
  2156. return {
  2157. n: null,
  2158. pos
  2159. };
  2160. }
  2161. return {
  2162. n: total,
  2163. pos
  2164. };
  2165. }
  2166. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2167. const ch = input.charCodeAt(pos);
  2168. let code;
  2169. if (ch === 123) {
  2170. ++pos;
  2171. ({
  2172. code,
  2173. pos
  2174. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2175. ++pos;
  2176. if (code !== null && code > 0x10ffff) {
  2177. if (throwOnInvalid) {
  2178. errors.invalidCodePoint(pos, lineStart, curLine);
  2179. } else {
  2180. return {
  2181. code: null,
  2182. pos
  2183. };
  2184. }
  2185. }
  2186. } else {
  2187. ({
  2188. code,
  2189. pos
  2190. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2191. }
  2192. return {
  2193. code,
  2194. pos
  2195. };
  2196. }
  2197. const _excluded = ["at"],
  2198. _excluded2 = ["at"];
  2199. function buildPosition(pos, lineStart, curLine) {
  2200. return new Position(curLine, pos - lineStart, pos);
  2201. }
  2202. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2203. class Token {
  2204. constructor(state) {
  2205. this.type = state.type;
  2206. this.value = state.value;
  2207. this.start = state.start;
  2208. this.end = state.end;
  2209. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2210. }
  2211. }
  2212. class Tokenizer extends CommentsParser {
  2213. constructor(options, input) {
  2214. super();
  2215. this.isLookahead = void 0;
  2216. this.tokens = [];
  2217. this.errorHandlers_readInt = {
  2218. invalidDigit: (pos, lineStart, curLine, radix) => {
  2219. if (!this.options.errorRecovery) return false;
  2220. this.raise(Errors.InvalidDigit, {
  2221. at: buildPosition(pos, lineStart, curLine),
  2222. radix
  2223. });
  2224. return true;
  2225. },
  2226. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2227. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2228. };
  2229. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2230. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2231. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2232. });
  2233. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2234. strictNumericEscape: (pos, lineStart, curLine) => {
  2235. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2236. at: buildPosition(pos, lineStart, curLine)
  2237. });
  2238. },
  2239. unterminated: (pos, lineStart, curLine) => {
  2240. throw this.raise(Errors.UnterminatedString, {
  2241. at: buildPosition(pos - 1, lineStart, curLine)
  2242. });
  2243. }
  2244. });
  2245. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2246. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2247. unterminated: (pos, lineStart, curLine) => {
  2248. throw this.raise(Errors.UnterminatedTemplate, {
  2249. at: buildPosition(pos, lineStart, curLine)
  2250. });
  2251. }
  2252. });
  2253. this.state = new State();
  2254. this.state.init(options);
  2255. this.input = input;
  2256. this.length = input.length;
  2257. this.isLookahead = false;
  2258. }
  2259. pushToken(token) {
  2260. this.tokens.length = this.state.tokensLength;
  2261. this.tokens.push(token);
  2262. ++this.state.tokensLength;
  2263. }
  2264. next() {
  2265. this.checkKeywordEscapes();
  2266. if (this.options.tokens) {
  2267. this.pushToken(new Token(this.state));
  2268. }
  2269. this.state.lastTokStart = this.state.start;
  2270. this.state.lastTokEndLoc = this.state.endLoc;
  2271. this.state.lastTokStartLoc = this.state.startLoc;
  2272. this.nextToken();
  2273. }
  2274. eat(type) {
  2275. if (this.match(type)) {
  2276. this.next();
  2277. return true;
  2278. } else {
  2279. return false;
  2280. }
  2281. }
  2282. match(type) {
  2283. return this.state.type === type;
  2284. }
  2285. createLookaheadState(state) {
  2286. return {
  2287. pos: state.pos,
  2288. value: null,
  2289. type: state.type,
  2290. start: state.start,
  2291. end: state.end,
  2292. context: [this.curContext()],
  2293. inType: state.inType,
  2294. startLoc: state.startLoc,
  2295. lastTokEndLoc: state.lastTokEndLoc,
  2296. curLine: state.curLine,
  2297. lineStart: state.lineStart,
  2298. curPosition: state.curPosition
  2299. };
  2300. }
  2301. lookahead() {
  2302. const old = this.state;
  2303. this.state = this.createLookaheadState(old);
  2304. this.isLookahead = true;
  2305. this.nextToken();
  2306. this.isLookahead = false;
  2307. const curr = this.state;
  2308. this.state = old;
  2309. return curr;
  2310. }
  2311. nextTokenStart() {
  2312. return this.nextTokenStartSince(this.state.pos);
  2313. }
  2314. nextTokenStartSince(pos) {
  2315. skipWhiteSpace.lastIndex = pos;
  2316. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2317. }
  2318. lookaheadCharCode() {
  2319. return this.input.charCodeAt(this.nextTokenStart());
  2320. }
  2321. codePointAtPos(pos) {
  2322. let cp = this.input.charCodeAt(pos);
  2323. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2324. const trail = this.input.charCodeAt(pos);
  2325. if ((trail & 0xfc00) === 0xdc00) {
  2326. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2327. }
  2328. }
  2329. return cp;
  2330. }
  2331. setStrict(strict) {
  2332. this.state.strict = strict;
  2333. if (strict) {
  2334. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  2335. at
  2336. }));
  2337. this.state.strictErrors.clear();
  2338. }
  2339. }
  2340. curContext() {
  2341. return this.state.context[this.state.context.length - 1];
  2342. }
  2343. nextToken() {
  2344. this.skipSpace();
  2345. this.state.start = this.state.pos;
  2346. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2347. if (this.state.pos >= this.length) {
  2348. this.finishToken(137);
  2349. return;
  2350. }
  2351. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2352. }
  2353. skipBlockComment(commentEnd) {
  2354. let startLoc;
  2355. if (!this.isLookahead) startLoc = this.state.curPosition();
  2356. const start = this.state.pos;
  2357. const end = this.input.indexOf(commentEnd, start + 2);
  2358. if (end === -1) {
  2359. throw this.raise(Errors.UnterminatedComment, {
  2360. at: this.state.curPosition()
  2361. });
  2362. }
  2363. this.state.pos = end + commentEnd.length;
  2364. lineBreakG.lastIndex = start + 2;
  2365. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2366. ++this.state.curLine;
  2367. this.state.lineStart = lineBreakG.lastIndex;
  2368. }
  2369. if (this.isLookahead) return;
  2370. const comment = {
  2371. type: "CommentBlock",
  2372. value: this.input.slice(start + 2, end),
  2373. start,
  2374. end: end + commentEnd.length,
  2375. loc: new SourceLocation(startLoc, this.state.curPosition())
  2376. };
  2377. if (this.options.tokens) this.pushToken(comment);
  2378. return comment;
  2379. }
  2380. skipLineComment(startSkip) {
  2381. const start = this.state.pos;
  2382. let startLoc;
  2383. if (!this.isLookahead) startLoc = this.state.curPosition();
  2384. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2385. if (this.state.pos < this.length) {
  2386. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2387. ch = this.input.charCodeAt(this.state.pos);
  2388. }
  2389. }
  2390. if (this.isLookahead) return;
  2391. const end = this.state.pos;
  2392. const value = this.input.slice(start + startSkip, end);
  2393. const comment = {
  2394. type: "CommentLine",
  2395. value,
  2396. start,
  2397. end,
  2398. loc: new SourceLocation(startLoc, this.state.curPosition())
  2399. };
  2400. if (this.options.tokens) this.pushToken(comment);
  2401. return comment;
  2402. }
  2403. skipSpace() {
  2404. const spaceStart = this.state.pos;
  2405. const comments = [];
  2406. loop: while (this.state.pos < this.length) {
  2407. const ch = this.input.charCodeAt(this.state.pos);
  2408. switch (ch) {
  2409. case 32:
  2410. case 160:
  2411. case 9:
  2412. ++this.state.pos;
  2413. break;
  2414. case 13:
  2415. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2416. ++this.state.pos;
  2417. }
  2418. case 10:
  2419. case 8232:
  2420. case 8233:
  2421. ++this.state.pos;
  2422. ++this.state.curLine;
  2423. this.state.lineStart = this.state.pos;
  2424. break;
  2425. case 47:
  2426. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2427. case 42:
  2428. {
  2429. const comment = this.skipBlockComment("*/");
  2430. if (comment !== undefined) {
  2431. this.addComment(comment);
  2432. if (this.options.attachComment) comments.push(comment);
  2433. }
  2434. break;
  2435. }
  2436. case 47:
  2437. {
  2438. const comment = this.skipLineComment(2);
  2439. if (comment !== undefined) {
  2440. this.addComment(comment);
  2441. if (this.options.attachComment) comments.push(comment);
  2442. }
  2443. break;
  2444. }
  2445. default:
  2446. break loop;
  2447. }
  2448. break;
  2449. default:
  2450. if (isWhitespace(ch)) {
  2451. ++this.state.pos;
  2452. } else if (ch === 45 && !this.inModule && this.options.annexB) {
  2453. const pos = this.state.pos;
  2454. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2455. const comment = this.skipLineComment(3);
  2456. if (comment !== undefined) {
  2457. this.addComment(comment);
  2458. if (this.options.attachComment) comments.push(comment);
  2459. }
  2460. } else {
  2461. break loop;
  2462. }
  2463. } else if (ch === 60 && !this.inModule && this.options.annexB) {
  2464. const pos = this.state.pos;
  2465. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2466. const comment = this.skipLineComment(4);
  2467. if (comment !== undefined) {
  2468. this.addComment(comment);
  2469. if (this.options.attachComment) comments.push(comment);
  2470. }
  2471. } else {
  2472. break loop;
  2473. }
  2474. } else {
  2475. break loop;
  2476. }
  2477. }
  2478. }
  2479. if (comments.length > 0) {
  2480. const end = this.state.pos;
  2481. const commentWhitespace = {
  2482. start: spaceStart,
  2483. end,
  2484. comments,
  2485. leadingNode: null,
  2486. trailingNode: null,
  2487. containingNode: null
  2488. };
  2489. this.state.commentStack.push(commentWhitespace);
  2490. }
  2491. }
  2492. finishToken(type, val) {
  2493. this.state.end = this.state.pos;
  2494. this.state.endLoc = this.state.curPosition();
  2495. const prevType = this.state.type;
  2496. this.state.type = type;
  2497. this.state.value = val;
  2498. if (!this.isLookahead) {
  2499. this.updateContext(prevType);
  2500. }
  2501. }
  2502. replaceToken(type) {
  2503. this.state.type = type;
  2504. this.updateContext();
  2505. }
  2506. readToken_numberSign() {
  2507. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2508. return;
  2509. }
  2510. const nextPos = this.state.pos + 1;
  2511. const next = this.codePointAtPos(nextPos);
  2512. if (next >= 48 && next <= 57) {
  2513. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  2514. at: this.state.curPosition()
  2515. });
  2516. }
  2517. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2518. this.expectPlugin("recordAndTuple");
  2519. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2520. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  2521. at: this.state.curPosition()
  2522. });
  2523. }
  2524. this.state.pos += 2;
  2525. if (next === 123) {
  2526. this.finishToken(7);
  2527. } else {
  2528. this.finishToken(1);
  2529. }
  2530. } else if (isIdentifierStart(next)) {
  2531. ++this.state.pos;
  2532. this.finishToken(136, this.readWord1(next));
  2533. } else if (next === 92) {
  2534. ++this.state.pos;
  2535. this.finishToken(136, this.readWord1());
  2536. } else {
  2537. this.finishOp(27, 1);
  2538. }
  2539. }
  2540. readToken_dot() {
  2541. const next = this.input.charCodeAt(this.state.pos + 1);
  2542. if (next >= 48 && next <= 57) {
  2543. this.readNumber(true);
  2544. return;
  2545. }
  2546. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2547. this.state.pos += 3;
  2548. this.finishToken(21);
  2549. } else {
  2550. ++this.state.pos;
  2551. this.finishToken(16);
  2552. }
  2553. }
  2554. readToken_slash() {
  2555. const next = this.input.charCodeAt(this.state.pos + 1);
  2556. if (next === 61) {
  2557. this.finishOp(31, 2);
  2558. } else {
  2559. this.finishOp(56, 1);
  2560. }
  2561. }
  2562. readToken_interpreter() {
  2563. if (this.state.pos !== 0 || this.length < 2) return false;
  2564. let ch = this.input.charCodeAt(this.state.pos + 1);
  2565. if (ch !== 33) return false;
  2566. const start = this.state.pos;
  2567. this.state.pos += 1;
  2568. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2569. ch = this.input.charCodeAt(this.state.pos);
  2570. }
  2571. const value = this.input.slice(start + 2, this.state.pos);
  2572. this.finishToken(28, value);
  2573. return true;
  2574. }
  2575. readToken_mult_modulo(code) {
  2576. let type = code === 42 ? 55 : 54;
  2577. let width = 1;
  2578. let next = this.input.charCodeAt(this.state.pos + 1);
  2579. if (code === 42 && next === 42) {
  2580. width++;
  2581. next = this.input.charCodeAt(this.state.pos + 2);
  2582. type = 57;
  2583. }
  2584. if (next === 61 && !this.state.inType) {
  2585. width++;
  2586. type = code === 37 ? 33 : 30;
  2587. }
  2588. this.finishOp(type, width);
  2589. }
  2590. readToken_pipe_amp(code) {
  2591. const next = this.input.charCodeAt(this.state.pos + 1);
  2592. if (next === code) {
  2593. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2594. this.finishOp(30, 3);
  2595. } else {
  2596. this.finishOp(code === 124 ? 41 : 42, 2);
  2597. }
  2598. return;
  2599. }
  2600. if (code === 124) {
  2601. if (next === 62) {
  2602. this.finishOp(39, 2);
  2603. return;
  2604. }
  2605. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2606. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2607. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2608. at: this.state.curPosition()
  2609. });
  2610. }
  2611. this.state.pos += 2;
  2612. this.finishToken(9);
  2613. return;
  2614. }
  2615. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2616. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2617. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2618. at: this.state.curPosition()
  2619. });
  2620. }
  2621. this.state.pos += 2;
  2622. this.finishToken(4);
  2623. return;
  2624. }
  2625. }
  2626. if (next === 61) {
  2627. this.finishOp(30, 2);
  2628. return;
  2629. }
  2630. this.finishOp(code === 124 ? 43 : 45, 1);
  2631. }
  2632. readToken_caret() {
  2633. const next = this.input.charCodeAt(this.state.pos + 1);
  2634. if (next === 61 && !this.state.inType) {
  2635. this.finishOp(32, 2);
  2636. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2637. proposal: "hack",
  2638. topicToken: "^^"
  2639. }])) {
  2640. this.finishOp(37, 2);
  2641. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2642. if (lookaheadCh === 94) {
  2643. throw this.unexpected();
  2644. }
  2645. } else {
  2646. this.finishOp(44, 1);
  2647. }
  2648. }
  2649. readToken_atSign() {
  2650. const next = this.input.charCodeAt(this.state.pos + 1);
  2651. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2652. proposal: "hack",
  2653. topicToken: "@@"
  2654. }])) {
  2655. this.finishOp(38, 2);
  2656. } else {
  2657. this.finishOp(26, 1);
  2658. }
  2659. }
  2660. readToken_plus_min(code) {
  2661. const next = this.input.charCodeAt(this.state.pos + 1);
  2662. if (next === code) {
  2663. this.finishOp(34, 2);
  2664. return;
  2665. }
  2666. if (next === 61) {
  2667. this.finishOp(30, 2);
  2668. } else {
  2669. this.finishOp(53, 1);
  2670. }
  2671. }
  2672. readToken_lt() {
  2673. const {
  2674. pos
  2675. } = this.state;
  2676. const next = this.input.charCodeAt(pos + 1);
  2677. if (next === 60) {
  2678. if (this.input.charCodeAt(pos + 2) === 61) {
  2679. this.finishOp(30, 3);
  2680. return;
  2681. }
  2682. this.finishOp(51, 2);
  2683. return;
  2684. }
  2685. if (next === 61) {
  2686. this.finishOp(49, 2);
  2687. return;
  2688. }
  2689. this.finishOp(47, 1);
  2690. }
  2691. readToken_gt() {
  2692. const {
  2693. pos
  2694. } = this.state;
  2695. const next = this.input.charCodeAt(pos + 1);
  2696. if (next === 62) {
  2697. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2698. if (this.input.charCodeAt(pos + size) === 61) {
  2699. this.finishOp(30, size + 1);
  2700. return;
  2701. }
  2702. this.finishOp(52, size);
  2703. return;
  2704. }
  2705. if (next === 61) {
  2706. this.finishOp(49, 2);
  2707. return;
  2708. }
  2709. this.finishOp(48, 1);
  2710. }
  2711. readToken_eq_excl(code) {
  2712. const next = this.input.charCodeAt(this.state.pos + 1);
  2713. if (next === 61) {
  2714. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2715. return;
  2716. }
  2717. if (code === 61 && next === 62) {
  2718. this.state.pos += 2;
  2719. this.finishToken(19);
  2720. return;
  2721. }
  2722. this.finishOp(code === 61 ? 29 : 35, 1);
  2723. }
  2724. readToken_question() {
  2725. const next = this.input.charCodeAt(this.state.pos + 1);
  2726. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2727. if (next === 63) {
  2728. if (next2 === 61) {
  2729. this.finishOp(30, 3);
  2730. } else {
  2731. this.finishOp(40, 2);
  2732. }
  2733. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2734. this.state.pos += 2;
  2735. this.finishToken(18);
  2736. } else {
  2737. ++this.state.pos;
  2738. this.finishToken(17);
  2739. }
  2740. }
  2741. getTokenFromCode(code) {
  2742. switch (code) {
  2743. case 46:
  2744. this.readToken_dot();
  2745. return;
  2746. case 40:
  2747. ++this.state.pos;
  2748. this.finishToken(10);
  2749. return;
  2750. case 41:
  2751. ++this.state.pos;
  2752. this.finishToken(11);
  2753. return;
  2754. case 59:
  2755. ++this.state.pos;
  2756. this.finishToken(13);
  2757. return;
  2758. case 44:
  2759. ++this.state.pos;
  2760. this.finishToken(12);
  2761. return;
  2762. case 91:
  2763. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2764. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2765. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2766. at: this.state.curPosition()
  2767. });
  2768. }
  2769. this.state.pos += 2;
  2770. this.finishToken(2);
  2771. } else {
  2772. ++this.state.pos;
  2773. this.finishToken(0);
  2774. }
  2775. return;
  2776. case 93:
  2777. ++this.state.pos;
  2778. this.finishToken(3);
  2779. return;
  2780. case 123:
  2781. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2782. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2783. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2784. at: this.state.curPosition()
  2785. });
  2786. }
  2787. this.state.pos += 2;
  2788. this.finishToken(6);
  2789. } else {
  2790. ++this.state.pos;
  2791. this.finishToken(5);
  2792. }
  2793. return;
  2794. case 125:
  2795. ++this.state.pos;
  2796. this.finishToken(8);
  2797. return;
  2798. case 58:
  2799. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2800. this.finishOp(15, 2);
  2801. } else {
  2802. ++this.state.pos;
  2803. this.finishToken(14);
  2804. }
  2805. return;
  2806. case 63:
  2807. this.readToken_question();
  2808. return;
  2809. case 96:
  2810. this.readTemplateToken();
  2811. return;
  2812. case 48:
  2813. {
  2814. const next = this.input.charCodeAt(this.state.pos + 1);
  2815. if (next === 120 || next === 88) {
  2816. this.readRadixNumber(16);
  2817. return;
  2818. }
  2819. if (next === 111 || next === 79) {
  2820. this.readRadixNumber(8);
  2821. return;
  2822. }
  2823. if (next === 98 || next === 66) {
  2824. this.readRadixNumber(2);
  2825. return;
  2826. }
  2827. }
  2828. case 49:
  2829. case 50:
  2830. case 51:
  2831. case 52:
  2832. case 53:
  2833. case 54:
  2834. case 55:
  2835. case 56:
  2836. case 57:
  2837. this.readNumber(false);
  2838. return;
  2839. case 34:
  2840. case 39:
  2841. this.readString(code);
  2842. return;
  2843. case 47:
  2844. this.readToken_slash();
  2845. return;
  2846. case 37:
  2847. case 42:
  2848. this.readToken_mult_modulo(code);
  2849. return;
  2850. case 124:
  2851. case 38:
  2852. this.readToken_pipe_amp(code);
  2853. return;
  2854. case 94:
  2855. this.readToken_caret();
  2856. return;
  2857. case 43:
  2858. case 45:
  2859. this.readToken_plus_min(code);
  2860. return;
  2861. case 60:
  2862. this.readToken_lt();
  2863. return;
  2864. case 62:
  2865. this.readToken_gt();
  2866. return;
  2867. case 61:
  2868. case 33:
  2869. this.readToken_eq_excl(code);
  2870. return;
  2871. case 126:
  2872. this.finishOp(36, 1);
  2873. return;
  2874. case 64:
  2875. this.readToken_atSign();
  2876. return;
  2877. case 35:
  2878. this.readToken_numberSign();
  2879. return;
  2880. case 92:
  2881. this.readWord();
  2882. return;
  2883. default:
  2884. if (isIdentifierStart(code)) {
  2885. this.readWord(code);
  2886. return;
  2887. }
  2888. }
  2889. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2890. at: this.state.curPosition(),
  2891. unexpected: String.fromCodePoint(code)
  2892. });
  2893. }
  2894. finishOp(type, size) {
  2895. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2896. this.state.pos += size;
  2897. this.finishToken(type, str);
  2898. }
  2899. readRegexp() {
  2900. const startLoc = this.state.startLoc;
  2901. const start = this.state.start + 1;
  2902. let escaped, inClass;
  2903. let {
  2904. pos
  2905. } = this.state;
  2906. for (;; ++pos) {
  2907. if (pos >= this.length) {
  2908. throw this.raise(Errors.UnterminatedRegExp, {
  2909. at: createPositionWithColumnOffset(startLoc, 1)
  2910. });
  2911. }
  2912. const ch = this.input.charCodeAt(pos);
  2913. if (isNewLine(ch)) {
  2914. throw this.raise(Errors.UnterminatedRegExp, {
  2915. at: createPositionWithColumnOffset(startLoc, 1)
  2916. });
  2917. }
  2918. if (escaped) {
  2919. escaped = false;
  2920. } else {
  2921. if (ch === 91) {
  2922. inClass = true;
  2923. } else if (ch === 93 && inClass) {
  2924. inClass = false;
  2925. } else if (ch === 47 && !inClass) {
  2926. break;
  2927. }
  2928. escaped = ch === 92;
  2929. }
  2930. }
  2931. const content = this.input.slice(start, pos);
  2932. ++pos;
  2933. let mods = "";
  2934. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2935. while (pos < this.length) {
  2936. const cp = this.codePointAtPos(pos);
  2937. const char = String.fromCharCode(cp);
  2938. if (VALID_REGEX_FLAGS.has(cp)) {
  2939. if (cp === 118) {
  2940. this.expectPlugin("regexpUnicodeSets", nextPos());
  2941. if (mods.includes("u")) {
  2942. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2943. at: nextPos()
  2944. });
  2945. }
  2946. } else if (cp === 117) {
  2947. if (mods.includes("v")) {
  2948. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2949. at: nextPos()
  2950. });
  2951. }
  2952. }
  2953. if (mods.includes(char)) {
  2954. this.raise(Errors.DuplicateRegExpFlags, {
  2955. at: nextPos()
  2956. });
  2957. }
  2958. } else if (isIdentifierChar(cp) || cp === 92) {
  2959. this.raise(Errors.MalformedRegExpFlags, {
  2960. at: nextPos()
  2961. });
  2962. } else {
  2963. break;
  2964. }
  2965. ++pos;
  2966. mods += char;
  2967. }
  2968. this.state.pos = pos;
  2969. this.finishToken(135, {
  2970. pattern: content,
  2971. flags: mods
  2972. });
  2973. }
  2974. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  2975. const {
  2976. n,
  2977. pos
  2978. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  2979. this.state.pos = pos;
  2980. return n;
  2981. }
  2982. readRadixNumber(radix) {
  2983. const startLoc = this.state.curPosition();
  2984. let isBigInt = false;
  2985. this.state.pos += 2;
  2986. const val = this.readInt(radix);
  2987. if (val == null) {
  2988. this.raise(Errors.InvalidDigit, {
  2989. at: createPositionWithColumnOffset(startLoc, 2),
  2990. radix
  2991. });
  2992. }
  2993. const next = this.input.charCodeAt(this.state.pos);
  2994. if (next === 110) {
  2995. ++this.state.pos;
  2996. isBigInt = true;
  2997. } else if (next === 109) {
  2998. throw this.raise(Errors.InvalidDecimal, {
  2999. at: startLoc
  3000. });
  3001. }
  3002. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3003. throw this.raise(Errors.NumberIdentifier, {
  3004. at: this.state.curPosition()
  3005. });
  3006. }
  3007. if (isBigInt) {
  3008. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  3009. this.finishToken(133, str);
  3010. return;
  3011. }
  3012. this.finishToken(132, val);
  3013. }
  3014. readNumber(startsWithDot) {
  3015. const start = this.state.pos;
  3016. const startLoc = this.state.curPosition();
  3017. let isFloat = false;
  3018. let isBigInt = false;
  3019. let isDecimal = false;
  3020. let hasExponent = false;
  3021. let isOctal = false;
  3022. if (!startsWithDot && this.readInt(10) === null) {
  3023. this.raise(Errors.InvalidNumber, {
  3024. at: this.state.curPosition()
  3025. });
  3026. }
  3027. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3028. if (hasLeadingZero) {
  3029. const integer = this.input.slice(start, this.state.pos);
  3030. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  3031. at: startLoc
  3032. });
  3033. if (!this.state.strict) {
  3034. const underscorePos = integer.indexOf("_");
  3035. if (underscorePos > 0) {
  3036. this.raise(Errors.ZeroDigitNumericSeparator, {
  3037. at: createPositionWithColumnOffset(startLoc, underscorePos)
  3038. });
  3039. }
  3040. }
  3041. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3042. }
  3043. let next = this.input.charCodeAt(this.state.pos);
  3044. if (next === 46 && !isOctal) {
  3045. ++this.state.pos;
  3046. this.readInt(10);
  3047. isFloat = true;
  3048. next = this.input.charCodeAt(this.state.pos);
  3049. }
  3050. if ((next === 69 || next === 101) && !isOctal) {
  3051. next = this.input.charCodeAt(++this.state.pos);
  3052. if (next === 43 || next === 45) {
  3053. ++this.state.pos;
  3054. }
  3055. if (this.readInt(10) === null) {
  3056. this.raise(Errors.InvalidOrMissingExponent, {
  3057. at: startLoc
  3058. });
  3059. }
  3060. isFloat = true;
  3061. hasExponent = true;
  3062. next = this.input.charCodeAt(this.state.pos);
  3063. }
  3064. if (next === 110) {
  3065. if (isFloat || hasLeadingZero) {
  3066. this.raise(Errors.InvalidBigIntLiteral, {
  3067. at: startLoc
  3068. });
  3069. }
  3070. ++this.state.pos;
  3071. isBigInt = true;
  3072. }
  3073. if (next === 109) {
  3074. this.expectPlugin("decimal", this.state.curPosition());
  3075. if (hasExponent || hasLeadingZero) {
  3076. this.raise(Errors.InvalidDecimal, {
  3077. at: startLoc
  3078. });
  3079. }
  3080. ++this.state.pos;
  3081. isDecimal = true;
  3082. }
  3083. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3084. throw this.raise(Errors.NumberIdentifier, {
  3085. at: this.state.curPosition()
  3086. });
  3087. }
  3088. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3089. if (isBigInt) {
  3090. this.finishToken(133, str);
  3091. return;
  3092. }
  3093. if (isDecimal) {
  3094. this.finishToken(134, str);
  3095. return;
  3096. }
  3097. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3098. this.finishToken(132, val);
  3099. }
  3100. readCodePoint(throwOnInvalid) {
  3101. const {
  3102. code,
  3103. pos
  3104. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3105. this.state.pos = pos;
  3106. return code;
  3107. }
  3108. readString(quote) {
  3109. const {
  3110. str,
  3111. pos,
  3112. curLine,
  3113. lineStart
  3114. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3115. this.state.pos = pos + 1;
  3116. this.state.lineStart = lineStart;
  3117. this.state.curLine = curLine;
  3118. this.finishToken(131, str);
  3119. }
  3120. readTemplateContinuation() {
  3121. if (!this.match(8)) {
  3122. this.unexpected(null, 8);
  3123. }
  3124. this.state.pos--;
  3125. this.readTemplateToken();
  3126. }
  3127. readTemplateToken() {
  3128. const opening = this.input[this.state.pos];
  3129. const {
  3130. str,
  3131. firstInvalidLoc,
  3132. pos,
  3133. curLine,
  3134. lineStart
  3135. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3136. this.state.pos = pos + 1;
  3137. this.state.lineStart = lineStart;
  3138. this.state.curLine = curLine;
  3139. if (firstInvalidLoc) {
  3140. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3141. }
  3142. if (this.input.codePointAt(pos) === 96) {
  3143. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3144. } else {
  3145. this.state.pos++;
  3146. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3147. }
  3148. }
  3149. recordStrictModeErrors(toParseError, {
  3150. at
  3151. }) {
  3152. const index = at.index;
  3153. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3154. this.raise(toParseError, {
  3155. at
  3156. });
  3157. } else {
  3158. this.state.strictErrors.set(index, [toParseError, at]);
  3159. }
  3160. }
  3161. readWord1(firstCode) {
  3162. this.state.containsEsc = false;
  3163. let word = "";
  3164. const start = this.state.pos;
  3165. let chunkStart = this.state.pos;
  3166. if (firstCode !== undefined) {
  3167. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3168. }
  3169. while (this.state.pos < this.length) {
  3170. const ch = this.codePointAtPos(this.state.pos);
  3171. if (isIdentifierChar(ch)) {
  3172. this.state.pos += ch <= 0xffff ? 1 : 2;
  3173. } else if (ch === 92) {
  3174. this.state.containsEsc = true;
  3175. word += this.input.slice(chunkStart, this.state.pos);
  3176. const escStart = this.state.curPosition();
  3177. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3178. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3179. this.raise(Errors.MissingUnicodeEscape, {
  3180. at: this.state.curPosition()
  3181. });
  3182. chunkStart = this.state.pos - 1;
  3183. continue;
  3184. }
  3185. ++this.state.pos;
  3186. const esc = this.readCodePoint(true);
  3187. if (esc !== null) {
  3188. if (!identifierCheck(esc)) {
  3189. this.raise(Errors.EscapedCharNotAnIdentifier, {
  3190. at: escStart
  3191. });
  3192. }
  3193. word += String.fromCodePoint(esc);
  3194. }
  3195. chunkStart = this.state.pos;
  3196. } else {
  3197. break;
  3198. }
  3199. }
  3200. return word + this.input.slice(chunkStart, this.state.pos);
  3201. }
  3202. readWord(firstCode) {
  3203. const word = this.readWord1(firstCode);
  3204. const type = keywords$1.get(word);
  3205. if (type !== undefined) {
  3206. this.finishToken(type, tokenLabelName(type));
  3207. } else {
  3208. this.finishToken(130, word);
  3209. }
  3210. }
  3211. checkKeywordEscapes() {
  3212. const {
  3213. type
  3214. } = this.state;
  3215. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3216. this.raise(Errors.InvalidEscapedReservedWord, {
  3217. at: this.state.startLoc,
  3218. reservedWord: tokenLabelName(type)
  3219. });
  3220. }
  3221. }
  3222. raise(toParseError, raiseProperties) {
  3223. const {
  3224. at
  3225. } = raiseProperties,
  3226. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  3227. const loc = at instanceof Position ? at : at.loc.start;
  3228. const error = toParseError({
  3229. loc,
  3230. details
  3231. });
  3232. if (!this.options.errorRecovery) throw error;
  3233. if (!this.isLookahead) this.state.errors.push(error);
  3234. return error;
  3235. }
  3236. raiseOverwrite(toParseError, raiseProperties) {
  3237. const {
  3238. at
  3239. } = raiseProperties,
  3240. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  3241. const loc = at instanceof Position ? at : at.loc.start;
  3242. const pos = loc.index;
  3243. const errors = this.state.errors;
  3244. for (let i = errors.length - 1; i >= 0; i--) {
  3245. const error = errors[i];
  3246. if (error.loc.index === pos) {
  3247. return errors[i] = toParseError({
  3248. loc,
  3249. details
  3250. });
  3251. }
  3252. if (error.loc.index < pos) break;
  3253. }
  3254. return this.raise(toParseError, raiseProperties);
  3255. }
  3256. updateContext(prevType) {}
  3257. unexpected(loc, type) {
  3258. throw this.raise(Errors.UnexpectedToken, {
  3259. expected: type ? tokenLabelName(type) : null,
  3260. at: loc != null ? loc : this.state.startLoc
  3261. });
  3262. }
  3263. expectPlugin(pluginName, loc) {
  3264. if (this.hasPlugin(pluginName)) {
  3265. return true;
  3266. }
  3267. throw this.raise(Errors.MissingPlugin, {
  3268. at: loc != null ? loc : this.state.startLoc,
  3269. missingPlugin: [pluginName]
  3270. });
  3271. }
  3272. expectOnePlugin(pluginNames) {
  3273. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3274. throw this.raise(Errors.MissingOneOfPlugins, {
  3275. at: this.state.startLoc,
  3276. missingPlugin: pluginNames
  3277. });
  3278. }
  3279. }
  3280. errorBuilder(error) {
  3281. return (pos, lineStart, curLine) => {
  3282. this.raise(error, {
  3283. at: buildPosition(pos, lineStart, curLine)
  3284. });
  3285. };
  3286. }
  3287. }
  3288. class ClassScope {
  3289. constructor() {
  3290. this.privateNames = new Set();
  3291. this.loneAccessors = new Map();
  3292. this.undefinedPrivateNames = new Map();
  3293. }
  3294. }
  3295. class ClassScopeHandler {
  3296. constructor(parser) {
  3297. this.parser = void 0;
  3298. this.stack = [];
  3299. this.undefinedPrivateNames = new Map();
  3300. this.parser = parser;
  3301. }
  3302. current() {
  3303. return this.stack[this.stack.length - 1];
  3304. }
  3305. enter() {
  3306. this.stack.push(new ClassScope());
  3307. }
  3308. exit() {
  3309. const oldClassScope = this.stack.pop();
  3310. const current = this.current();
  3311. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3312. if (current) {
  3313. if (!current.undefinedPrivateNames.has(name)) {
  3314. current.undefinedPrivateNames.set(name, loc);
  3315. }
  3316. } else {
  3317. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3318. at: loc,
  3319. identifierName: name
  3320. });
  3321. }
  3322. }
  3323. }
  3324. declarePrivateName(name, elementType, loc) {
  3325. const {
  3326. privateNames,
  3327. loneAccessors,
  3328. undefinedPrivateNames
  3329. } = this.current();
  3330. let redefined = privateNames.has(name);
  3331. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3332. const accessor = redefined && loneAccessors.get(name);
  3333. if (accessor) {
  3334. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3335. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3336. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3337. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3338. redefined = oldKind === newKind || oldStatic !== newStatic;
  3339. if (!redefined) loneAccessors.delete(name);
  3340. } else if (!redefined) {
  3341. loneAccessors.set(name, elementType);
  3342. }
  3343. }
  3344. if (redefined) {
  3345. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3346. at: loc,
  3347. identifierName: name
  3348. });
  3349. }
  3350. privateNames.add(name);
  3351. undefinedPrivateNames.delete(name);
  3352. }
  3353. usePrivateName(name, loc) {
  3354. let classScope;
  3355. for (classScope of this.stack) {
  3356. if (classScope.privateNames.has(name)) return;
  3357. }
  3358. if (classScope) {
  3359. classScope.undefinedPrivateNames.set(name, loc);
  3360. } else {
  3361. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3362. at: loc,
  3363. identifierName: name
  3364. });
  3365. }
  3366. }
  3367. }
  3368. const kExpression = 0,
  3369. kMaybeArrowParameterDeclaration = 1,
  3370. kMaybeAsyncArrowParameterDeclaration = 2,
  3371. kParameterDeclaration = 3;
  3372. class ExpressionScope {
  3373. constructor(type = kExpression) {
  3374. this.type = void 0;
  3375. this.type = type;
  3376. }
  3377. canBeArrowParameterDeclaration() {
  3378. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3379. }
  3380. isCertainlyParameterDeclaration() {
  3381. return this.type === kParameterDeclaration;
  3382. }
  3383. }
  3384. class ArrowHeadParsingScope extends ExpressionScope {
  3385. constructor(type) {
  3386. super(type);
  3387. this.declarationErrors = new Map();
  3388. }
  3389. recordDeclarationError(ParsingErrorClass, {
  3390. at
  3391. }) {
  3392. const index = at.index;
  3393. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3394. }
  3395. clearDeclarationError(index) {
  3396. this.declarationErrors.delete(index);
  3397. }
  3398. iterateErrors(iterator) {
  3399. this.declarationErrors.forEach(iterator);
  3400. }
  3401. }
  3402. class ExpressionScopeHandler {
  3403. constructor(parser) {
  3404. this.parser = void 0;
  3405. this.stack = [new ExpressionScope()];
  3406. this.parser = parser;
  3407. }
  3408. enter(scope) {
  3409. this.stack.push(scope);
  3410. }
  3411. exit() {
  3412. this.stack.pop();
  3413. }
  3414. recordParameterInitializerError(toParseError, {
  3415. at: node
  3416. }) {
  3417. const origin = {
  3418. at: node.loc.start
  3419. };
  3420. const {
  3421. stack
  3422. } = this;
  3423. let i = stack.length - 1;
  3424. let scope = stack[i];
  3425. while (!scope.isCertainlyParameterDeclaration()) {
  3426. if (scope.canBeArrowParameterDeclaration()) {
  3427. scope.recordDeclarationError(toParseError, origin);
  3428. } else {
  3429. return;
  3430. }
  3431. scope = stack[--i];
  3432. }
  3433. this.parser.raise(toParseError, origin);
  3434. }
  3435. recordArrowParameterBindingError(error, {
  3436. at: node
  3437. }) {
  3438. const {
  3439. stack
  3440. } = this;
  3441. const scope = stack[stack.length - 1];
  3442. const origin = {
  3443. at: node.loc.start
  3444. };
  3445. if (scope.isCertainlyParameterDeclaration()) {
  3446. this.parser.raise(error, origin);
  3447. } else if (scope.canBeArrowParameterDeclaration()) {
  3448. scope.recordDeclarationError(error, origin);
  3449. } else {
  3450. return;
  3451. }
  3452. }
  3453. recordAsyncArrowParametersError({
  3454. at
  3455. }) {
  3456. const {
  3457. stack
  3458. } = this;
  3459. let i = stack.length - 1;
  3460. let scope = stack[i];
  3461. while (scope.canBeArrowParameterDeclaration()) {
  3462. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3463. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3464. at
  3465. });
  3466. }
  3467. scope = stack[--i];
  3468. }
  3469. }
  3470. validateAsPattern() {
  3471. const {
  3472. stack
  3473. } = this;
  3474. const currentScope = stack[stack.length - 1];
  3475. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3476. currentScope.iterateErrors(([toParseError, loc]) => {
  3477. this.parser.raise(toParseError, {
  3478. at: loc
  3479. });
  3480. let i = stack.length - 2;
  3481. let scope = stack[i];
  3482. while (scope.canBeArrowParameterDeclaration()) {
  3483. scope.clearDeclarationError(loc.index);
  3484. scope = stack[--i];
  3485. }
  3486. });
  3487. }
  3488. }
  3489. function newParameterDeclarationScope() {
  3490. return new ExpressionScope(kParameterDeclaration);
  3491. }
  3492. function newArrowHeadScope() {
  3493. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3494. }
  3495. function newAsyncArrowScope() {
  3496. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3497. }
  3498. function newExpressionScope() {
  3499. return new ExpressionScope();
  3500. }
  3501. const PARAM = 0b0000,
  3502. PARAM_YIELD = 0b0001,
  3503. PARAM_AWAIT = 0b0010,
  3504. PARAM_RETURN = 0b0100,
  3505. PARAM_IN = 0b1000;
  3506. class ProductionParameterHandler {
  3507. constructor() {
  3508. this.stacks = [];
  3509. }
  3510. enter(flags) {
  3511. this.stacks.push(flags);
  3512. }
  3513. exit() {
  3514. this.stacks.pop();
  3515. }
  3516. currentFlags() {
  3517. return this.stacks[this.stacks.length - 1];
  3518. }
  3519. get hasAwait() {
  3520. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3521. }
  3522. get hasYield() {
  3523. return (this.currentFlags() & PARAM_YIELD) > 0;
  3524. }
  3525. get hasReturn() {
  3526. return (this.currentFlags() & PARAM_RETURN) > 0;
  3527. }
  3528. get hasIn() {
  3529. return (this.currentFlags() & PARAM_IN) > 0;
  3530. }
  3531. }
  3532. function functionFlags(isAsync, isGenerator) {
  3533. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3534. }
  3535. class UtilParser extends Tokenizer {
  3536. addExtra(node, key, value, enumerable = true) {
  3537. if (!node) return;
  3538. const extra = node.extra = node.extra || {};
  3539. if (enumerable) {
  3540. extra[key] = value;
  3541. } else {
  3542. Object.defineProperty(extra, key, {
  3543. enumerable,
  3544. value
  3545. });
  3546. }
  3547. }
  3548. isContextual(token) {
  3549. return this.state.type === token && !this.state.containsEsc;
  3550. }
  3551. isUnparsedContextual(nameStart, name) {
  3552. const nameEnd = nameStart + name.length;
  3553. if (this.input.slice(nameStart, nameEnd) === name) {
  3554. const nextCh = this.input.charCodeAt(nameEnd);
  3555. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3556. }
  3557. return false;
  3558. }
  3559. isLookaheadContextual(name) {
  3560. const next = this.nextTokenStart();
  3561. return this.isUnparsedContextual(next, name);
  3562. }
  3563. eatContextual(token) {
  3564. if (this.isContextual(token)) {
  3565. this.next();
  3566. return true;
  3567. }
  3568. return false;
  3569. }
  3570. expectContextual(token, toParseError) {
  3571. if (!this.eatContextual(token)) {
  3572. if (toParseError != null) {
  3573. throw this.raise(toParseError, {
  3574. at: this.state.startLoc
  3575. });
  3576. }
  3577. throw this.unexpected(null, token);
  3578. }
  3579. }
  3580. canInsertSemicolon() {
  3581. return this.match(137) || this.match(8) || this.hasPrecedingLineBreak();
  3582. }
  3583. hasPrecedingLineBreak() {
  3584. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3585. }
  3586. hasFollowingLineBreak() {
  3587. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3588. return skipWhiteSpaceToLineBreak.test(this.input);
  3589. }
  3590. isLineTerminator() {
  3591. return this.eat(13) || this.canInsertSemicolon();
  3592. }
  3593. semicolon(allowAsi = true) {
  3594. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3595. this.raise(Errors.MissingSemicolon, {
  3596. at: this.state.lastTokEndLoc
  3597. });
  3598. }
  3599. expect(type, loc) {
  3600. this.eat(type) || this.unexpected(loc, type);
  3601. }
  3602. tryParse(fn, oldState = this.state.clone()) {
  3603. const abortSignal = {
  3604. node: null
  3605. };
  3606. try {
  3607. const node = fn((node = null) => {
  3608. abortSignal.node = node;
  3609. throw abortSignal;
  3610. });
  3611. if (this.state.errors.length > oldState.errors.length) {
  3612. const failState = this.state;
  3613. this.state = oldState;
  3614. this.state.tokensLength = failState.tokensLength;
  3615. return {
  3616. node,
  3617. error: failState.errors[oldState.errors.length],
  3618. thrown: false,
  3619. aborted: false,
  3620. failState
  3621. };
  3622. }
  3623. return {
  3624. node,
  3625. error: null,
  3626. thrown: false,
  3627. aborted: false,
  3628. failState: null
  3629. };
  3630. } catch (error) {
  3631. const failState = this.state;
  3632. this.state = oldState;
  3633. if (error instanceof SyntaxError) {
  3634. return {
  3635. node: null,
  3636. error,
  3637. thrown: true,
  3638. aborted: false,
  3639. failState
  3640. };
  3641. }
  3642. if (error === abortSignal) {
  3643. return {
  3644. node: abortSignal.node,
  3645. error: null,
  3646. thrown: false,
  3647. aborted: true,
  3648. failState
  3649. };
  3650. }
  3651. throw error;
  3652. }
  3653. }
  3654. checkExpressionErrors(refExpressionErrors, andThrow) {
  3655. if (!refExpressionErrors) return false;
  3656. const {
  3657. shorthandAssignLoc,
  3658. doubleProtoLoc,
  3659. privateKeyLoc,
  3660. optionalParametersLoc
  3661. } = refExpressionErrors;
  3662. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3663. if (!andThrow) {
  3664. return hasErrors;
  3665. }
  3666. if (shorthandAssignLoc != null) {
  3667. this.raise(Errors.InvalidCoverInitializedName, {
  3668. at: shorthandAssignLoc
  3669. });
  3670. }
  3671. if (doubleProtoLoc != null) {
  3672. this.raise(Errors.DuplicateProto, {
  3673. at: doubleProtoLoc
  3674. });
  3675. }
  3676. if (privateKeyLoc != null) {
  3677. this.raise(Errors.UnexpectedPrivateField, {
  3678. at: privateKeyLoc
  3679. });
  3680. }
  3681. if (optionalParametersLoc != null) {
  3682. this.unexpected(optionalParametersLoc);
  3683. }
  3684. }
  3685. isLiteralPropertyName() {
  3686. return tokenIsLiteralPropertyName(this.state.type);
  3687. }
  3688. isPrivateName(node) {
  3689. return node.type === "PrivateName";
  3690. }
  3691. getPrivateNameSV(node) {
  3692. return node.id.name;
  3693. }
  3694. hasPropertyAsPrivateName(node) {
  3695. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3696. }
  3697. isObjectProperty(node) {
  3698. return node.type === "ObjectProperty";
  3699. }
  3700. isObjectMethod(node) {
  3701. return node.type === "ObjectMethod";
  3702. }
  3703. initializeScopes(inModule = this.options.sourceType === "module") {
  3704. const oldLabels = this.state.labels;
  3705. this.state.labels = [];
  3706. const oldExportedIdentifiers = this.exportedIdentifiers;
  3707. this.exportedIdentifiers = new Set();
  3708. const oldInModule = this.inModule;
  3709. this.inModule = inModule;
  3710. const oldScope = this.scope;
  3711. const ScopeHandler = this.getScopeHandler();
  3712. this.scope = new ScopeHandler(this, inModule);
  3713. const oldProdParam = this.prodParam;
  3714. this.prodParam = new ProductionParameterHandler();
  3715. const oldClassScope = this.classScope;
  3716. this.classScope = new ClassScopeHandler(this);
  3717. const oldExpressionScope = this.expressionScope;
  3718. this.expressionScope = new ExpressionScopeHandler(this);
  3719. return () => {
  3720. this.state.labels = oldLabels;
  3721. this.exportedIdentifiers = oldExportedIdentifiers;
  3722. this.inModule = oldInModule;
  3723. this.scope = oldScope;
  3724. this.prodParam = oldProdParam;
  3725. this.classScope = oldClassScope;
  3726. this.expressionScope = oldExpressionScope;
  3727. };
  3728. }
  3729. enterInitialScopes() {
  3730. let paramFlags = PARAM;
  3731. if (this.inModule) {
  3732. paramFlags |= PARAM_AWAIT;
  3733. }
  3734. this.scope.enter(SCOPE_PROGRAM);
  3735. this.prodParam.enter(paramFlags);
  3736. }
  3737. checkDestructuringPrivate(refExpressionErrors) {
  3738. const {
  3739. privateKeyLoc
  3740. } = refExpressionErrors;
  3741. if (privateKeyLoc !== null) {
  3742. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3743. }
  3744. }
  3745. }
  3746. class ExpressionErrors {
  3747. constructor() {
  3748. this.shorthandAssignLoc = null;
  3749. this.doubleProtoLoc = null;
  3750. this.privateKeyLoc = null;
  3751. this.optionalParametersLoc = null;
  3752. }
  3753. }
  3754. class Node {
  3755. constructor(parser, pos, loc) {
  3756. this.type = "";
  3757. this.start = pos;
  3758. this.end = 0;
  3759. this.loc = new SourceLocation(loc);
  3760. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3761. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3762. }
  3763. }
  3764. const NodePrototype = Node.prototype;
  3765. {
  3766. NodePrototype.__clone = function () {
  3767. const newNode = new Node(undefined, this.start, this.loc.start);
  3768. const keys = Object.keys(this);
  3769. for (let i = 0, length = keys.length; i < length; i++) {
  3770. const key = keys[i];
  3771. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3772. newNode[key] = this[key];
  3773. }
  3774. }
  3775. return newNode;
  3776. };
  3777. }
  3778. function clonePlaceholder(node) {
  3779. return cloneIdentifier(node);
  3780. }
  3781. function cloneIdentifier(node) {
  3782. const {
  3783. type,
  3784. start,
  3785. end,
  3786. loc,
  3787. range,
  3788. extra,
  3789. name
  3790. } = node;
  3791. const cloned = Object.create(NodePrototype);
  3792. cloned.type = type;
  3793. cloned.start = start;
  3794. cloned.end = end;
  3795. cloned.loc = loc;
  3796. cloned.range = range;
  3797. cloned.extra = extra;
  3798. cloned.name = name;
  3799. if (type === "Placeholder") {
  3800. cloned.expectedNode = node.expectedNode;
  3801. }
  3802. return cloned;
  3803. }
  3804. function cloneStringLiteral(node) {
  3805. const {
  3806. type,
  3807. start,
  3808. end,
  3809. loc,
  3810. range,
  3811. extra
  3812. } = node;
  3813. if (type === "Placeholder") {
  3814. return clonePlaceholder(node);
  3815. }
  3816. const cloned = Object.create(NodePrototype);
  3817. cloned.type = type;
  3818. cloned.start = start;
  3819. cloned.end = end;
  3820. cloned.loc = loc;
  3821. cloned.range = range;
  3822. if (node.raw !== undefined) {
  3823. cloned.raw = node.raw;
  3824. } else {
  3825. cloned.extra = extra;
  3826. }
  3827. cloned.value = node.value;
  3828. return cloned;
  3829. }
  3830. class NodeUtils extends UtilParser {
  3831. startNode() {
  3832. return new Node(this, this.state.start, this.state.startLoc);
  3833. }
  3834. startNodeAt(loc) {
  3835. return new Node(this, loc.index, loc);
  3836. }
  3837. startNodeAtNode(type) {
  3838. return this.startNodeAt(type.loc.start);
  3839. }
  3840. finishNode(node, type) {
  3841. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3842. }
  3843. finishNodeAt(node, type, endLoc) {
  3844. node.type = type;
  3845. node.end = endLoc.index;
  3846. node.loc.end = endLoc;
  3847. if (this.options.ranges) node.range[1] = endLoc.index;
  3848. if (this.options.attachComment) this.processComment(node);
  3849. return node;
  3850. }
  3851. resetStartLocation(node, startLoc) {
  3852. node.start = startLoc.index;
  3853. node.loc.start = startLoc;
  3854. if (this.options.ranges) node.range[0] = startLoc.index;
  3855. }
  3856. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3857. node.end = endLoc.index;
  3858. node.loc.end = endLoc;
  3859. if (this.options.ranges) node.range[1] = endLoc.index;
  3860. }
  3861. resetStartLocationFromNode(node, locationNode) {
  3862. this.resetStartLocation(node, locationNode.loc.start);
  3863. }
  3864. }
  3865. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3866. const FlowErrors = ParseErrorEnum`flow`({
  3867. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3868. 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.",
  3869. AssignReservedType: ({
  3870. reservedType
  3871. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3872. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3873. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3874. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3875. EnumBooleanMemberNotInitialized: ({
  3876. memberName,
  3877. enumName
  3878. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3879. EnumDuplicateMemberName: ({
  3880. memberName,
  3881. enumName
  3882. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3883. EnumInconsistentMemberValues: ({
  3884. enumName
  3885. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3886. EnumInvalidExplicitType: ({
  3887. invalidEnumType,
  3888. enumName
  3889. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3890. EnumInvalidExplicitTypeUnknownSupplied: ({
  3891. enumName
  3892. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3893. EnumInvalidMemberInitializerPrimaryType: ({
  3894. enumName,
  3895. memberName,
  3896. explicitType
  3897. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3898. EnumInvalidMemberInitializerSymbolType: ({
  3899. enumName,
  3900. memberName
  3901. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3902. EnumInvalidMemberInitializerUnknownType: ({
  3903. enumName,
  3904. memberName
  3905. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3906. EnumInvalidMemberName: ({
  3907. enumName,
  3908. memberName,
  3909. suggestion
  3910. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3911. EnumNumberMemberNotInitialized: ({
  3912. enumName,
  3913. memberName
  3914. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3915. EnumStringMemberInconsistentlyInitailized: ({
  3916. enumName
  3917. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3918. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3919. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  3920. 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.",
  3921. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3922. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3923. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3924. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3925. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3926. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3927. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3928. PatternIsOptional: Object.assign({
  3929. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3930. }, {
  3931. reasonCode: "OptionalBindingPattern"
  3932. }),
  3933. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3934. SpreadVariance: "Spread properties cannot have variance.",
  3935. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3936. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3937. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3938. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3939. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3940. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3941. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3942. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3943. UnexpectedReservedType: ({
  3944. reservedType
  3945. }) => `Unexpected reserved type ${reservedType}.`,
  3946. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3947. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3948. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3949. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3950. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3951. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3952. UnsupportedDeclareExportKind: ({
  3953. unsupportedExportKind,
  3954. suggestion
  3955. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3956. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3957. UnterminatedFlowComment: "Unterminated flow-comment."
  3958. });
  3959. function isEsModuleType(bodyElement) {
  3960. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3961. }
  3962. function hasTypeImportKind(node) {
  3963. return node.importKind === "type" || node.importKind === "typeof";
  3964. }
  3965. function isMaybeDefaultImport(type) {
  3966. return tokenIsKeywordOrIdentifier(type) && type !== 97;
  3967. }
  3968. const exportSuggestions = {
  3969. const: "declare export var",
  3970. let: "declare export var",
  3971. type: "export type",
  3972. interface: "export interface"
  3973. };
  3974. function partition(list, test) {
  3975. const list1 = [];
  3976. const list2 = [];
  3977. for (let i = 0; i < list.length; i++) {
  3978. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3979. }
  3980. return [list1, list2];
  3981. }
  3982. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3983. var flow = (superClass => class FlowParserMixin extends superClass {
  3984. constructor(...args) {
  3985. super(...args);
  3986. this.flowPragma = undefined;
  3987. }
  3988. getScopeHandler() {
  3989. return FlowScopeHandler;
  3990. }
  3991. shouldParseTypes() {
  3992. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3993. }
  3994. shouldParseEnums() {
  3995. return !!this.getPluginOption("flow", "enums");
  3996. }
  3997. finishToken(type, val) {
  3998. if (type !== 131 && type !== 13 && type !== 28) {
  3999. if (this.flowPragma === undefined) {
  4000. this.flowPragma = null;
  4001. }
  4002. }
  4003. return super.finishToken(type, val);
  4004. }
  4005. addComment(comment) {
  4006. if (this.flowPragma === undefined) {
  4007. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  4008. if (!matches) ; else if (matches[1] === "flow") {
  4009. this.flowPragma = "flow";
  4010. } else if (matches[1] === "noflow") {
  4011. this.flowPragma = "noflow";
  4012. } else {
  4013. throw new Error("Unexpected flow pragma");
  4014. }
  4015. }
  4016. return super.addComment(comment);
  4017. }
  4018. flowParseTypeInitialiser(tok) {
  4019. const oldInType = this.state.inType;
  4020. this.state.inType = true;
  4021. this.expect(tok || 14);
  4022. const type = this.flowParseType();
  4023. this.state.inType = oldInType;
  4024. return type;
  4025. }
  4026. flowParsePredicate() {
  4027. const node = this.startNode();
  4028. const moduloLoc = this.state.startLoc;
  4029. this.next();
  4030. this.expectContextual(108);
  4031. if (this.state.lastTokStart > moduloLoc.index + 1) {
  4032. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  4033. at: moduloLoc
  4034. });
  4035. }
  4036. if (this.eat(10)) {
  4037. node.value = super.parseExpression();
  4038. this.expect(11);
  4039. return this.finishNode(node, "DeclaredPredicate");
  4040. } else {
  4041. return this.finishNode(node, "InferredPredicate");
  4042. }
  4043. }
  4044. flowParseTypeAndPredicateInitialiser() {
  4045. const oldInType = this.state.inType;
  4046. this.state.inType = true;
  4047. this.expect(14);
  4048. let type = null;
  4049. let predicate = null;
  4050. if (this.match(54)) {
  4051. this.state.inType = oldInType;
  4052. predicate = this.flowParsePredicate();
  4053. } else {
  4054. type = this.flowParseType();
  4055. this.state.inType = oldInType;
  4056. if (this.match(54)) {
  4057. predicate = this.flowParsePredicate();
  4058. }
  4059. }
  4060. return [type, predicate];
  4061. }
  4062. flowParseDeclareClass(node) {
  4063. this.next();
  4064. this.flowParseInterfaceish(node, true);
  4065. return this.finishNode(node, "DeclareClass");
  4066. }
  4067. flowParseDeclareFunction(node) {
  4068. this.next();
  4069. const id = node.id = this.parseIdentifier();
  4070. const typeNode = this.startNode();
  4071. const typeContainer = this.startNode();
  4072. if (this.match(47)) {
  4073. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4074. } else {
  4075. typeNode.typeParameters = null;
  4076. }
  4077. this.expect(10);
  4078. const tmp = this.flowParseFunctionTypeParams();
  4079. typeNode.params = tmp.params;
  4080. typeNode.rest = tmp.rest;
  4081. typeNode.this = tmp._this;
  4082. this.expect(11);
  4083. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4084. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4085. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4086. this.resetEndLocation(id);
  4087. this.semicolon();
  4088. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  4089. return this.finishNode(node, "DeclareFunction");
  4090. }
  4091. flowParseDeclare(node, insideModule) {
  4092. if (this.match(80)) {
  4093. return this.flowParseDeclareClass(node);
  4094. } else if (this.match(68)) {
  4095. return this.flowParseDeclareFunction(node);
  4096. } else if (this.match(74)) {
  4097. return this.flowParseDeclareVariable(node);
  4098. } else if (this.eatContextual(125)) {
  4099. if (this.match(16)) {
  4100. return this.flowParseDeclareModuleExports(node);
  4101. } else {
  4102. if (insideModule) {
  4103. this.raise(FlowErrors.NestedDeclareModule, {
  4104. at: this.state.lastTokStartLoc
  4105. });
  4106. }
  4107. return this.flowParseDeclareModule(node);
  4108. }
  4109. } else if (this.isContextual(128)) {
  4110. return this.flowParseDeclareTypeAlias(node);
  4111. } else if (this.isContextual(129)) {
  4112. return this.flowParseDeclareOpaqueType(node);
  4113. } else if (this.isContextual(127)) {
  4114. return this.flowParseDeclareInterface(node);
  4115. } else if (this.match(82)) {
  4116. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4117. } else {
  4118. throw this.unexpected();
  4119. }
  4120. }
  4121. flowParseDeclareVariable(node) {
  4122. this.next();
  4123. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4124. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  4125. this.semicolon();
  4126. return this.finishNode(node, "DeclareVariable");
  4127. }
  4128. flowParseDeclareModule(node) {
  4129. this.scope.enter(SCOPE_OTHER);
  4130. if (this.match(131)) {
  4131. node.id = super.parseExprAtom();
  4132. } else {
  4133. node.id = this.parseIdentifier();
  4134. }
  4135. const bodyNode = node.body = this.startNode();
  4136. const body = bodyNode.body = [];
  4137. this.expect(5);
  4138. while (!this.match(8)) {
  4139. let bodyNode = this.startNode();
  4140. if (this.match(83)) {
  4141. this.next();
  4142. if (!this.isContextual(128) && !this.match(87)) {
  4143. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  4144. at: this.state.lastTokStartLoc
  4145. });
  4146. }
  4147. super.parseImport(bodyNode);
  4148. } else {
  4149. this.expectContextual(123, FlowErrors.UnsupportedStatementInDeclareModule);
  4150. bodyNode = this.flowParseDeclare(bodyNode, true);
  4151. }
  4152. body.push(bodyNode);
  4153. }
  4154. this.scope.exit();
  4155. this.expect(8);
  4156. this.finishNode(bodyNode, "BlockStatement");
  4157. let kind = null;
  4158. let hasModuleExport = false;
  4159. body.forEach(bodyElement => {
  4160. if (isEsModuleType(bodyElement)) {
  4161. if (kind === "CommonJS") {
  4162. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4163. at: bodyElement
  4164. });
  4165. }
  4166. kind = "ES";
  4167. } else if (bodyElement.type === "DeclareModuleExports") {
  4168. if (hasModuleExport) {
  4169. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  4170. at: bodyElement
  4171. });
  4172. }
  4173. if (kind === "ES") {
  4174. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4175. at: bodyElement
  4176. });
  4177. }
  4178. kind = "CommonJS";
  4179. hasModuleExport = true;
  4180. }
  4181. });
  4182. node.kind = kind || "CommonJS";
  4183. return this.finishNode(node, "DeclareModule");
  4184. }
  4185. flowParseDeclareExportDeclaration(node, insideModule) {
  4186. this.expect(82);
  4187. if (this.eat(65)) {
  4188. if (this.match(68) || this.match(80)) {
  4189. node.declaration = this.flowParseDeclare(this.startNode());
  4190. } else {
  4191. node.declaration = this.flowParseType();
  4192. this.semicolon();
  4193. }
  4194. node.default = true;
  4195. return this.finishNode(node, "DeclareExportDeclaration");
  4196. } else {
  4197. if (this.match(75) || this.isLet() || (this.isContextual(128) || this.isContextual(127)) && !insideModule) {
  4198. const label = this.state.value;
  4199. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4200. at: this.state.startLoc,
  4201. unsupportedExportKind: label,
  4202. suggestion: exportSuggestions[label]
  4203. });
  4204. }
  4205. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(129)) {
  4206. node.declaration = this.flowParseDeclare(this.startNode());
  4207. node.default = false;
  4208. return this.finishNode(node, "DeclareExportDeclaration");
  4209. } else if (this.match(55) || this.match(5) || this.isContextual(127) || this.isContextual(128) || this.isContextual(129)) {
  4210. node = this.parseExport(node, null);
  4211. if (node.type === "ExportNamedDeclaration") {
  4212. node.type = "ExportDeclaration";
  4213. node.default = false;
  4214. delete node.exportKind;
  4215. }
  4216. node.type = "Declare" + node.type;
  4217. return node;
  4218. }
  4219. }
  4220. throw this.unexpected();
  4221. }
  4222. flowParseDeclareModuleExports(node) {
  4223. this.next();
  4224. this.expectContextual(109);
  4225. node.typeAnnotation = this.flowParseTypeAnnotation();
  4226. this.semicolon();
  4227. return this.finishNode(node, "DeclareModuleExports");
  4228. }
  4229. flowParseDeclareTypeAlias(node) {
  4230. this.next();
  4231. const finished = this.flowParseTypeAlias(node);
  4232. finished.type = "DeclareTypeAlias";
  4233. return finished;
  4234. }
  4235. flowParseDeclareOpaqueType(node) {
  4236. this.next();
  4237. const finished = this.flowParseOpaqueType(node, true);
  4238. finished.type = "DeclareOpaqueType";
  4239. return finished;
  4240. }
  4241. flowParseDeclareInterface(node) {
  4242. this.next();
  4243. this.flowParseInterfaceish(node);
  4244. return this.finishNode(node, "DeclareInterface");
  4245. }
  4246. flowParseInterfaceish(node, isClass = false) {
  4247. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4248. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4249. if (this.match(47)) {
  4250. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4251. } else {
  4252. node.typeParameters = null;
  4253. }
  4254. node.extends = [];
  4255. node.implements = [];
  4256. node.mixins = [];
  4257. if (this.eat(81)) {
  4258. do {
  4259. node.extends.push(this.flowParseInterfaceExtends());
  4260. } while (!isClass && this.eat(12));
  4261. }
  4262. if (this.isContextual(115)) {
  4263. this.next();
  4264. do {
  4265. node.mixins.push(this.flowParseInterfaceExtends());
  4266. } while (this.eat(12));
  4267. }
  4268. if (this.isContextual(111)) {
  4269. this.next();
  4270. do {
  4271. node.implements.push(this.flowParseInterfaceExtends());
  4272. } while (this.eat(12));
  4273. }
  4274. node.body = this.flowParseObjectType({
  4275. allowStatic: isClass,
  4276. allowExact: false,
  4277. allowSpread: false,
  4278. allowProto: isClass,
  4279. allowInexact: false
  4280. });
  4281. }
  4282. flowParseInterfaceExtends() {
  4283. const node = this.startNode();
  4284. node.id = this.flowParseQualifiedTypeIdentifier();
  4285. if (this.match(47)) {
  4286. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4287. } else {
  4288. node.typeParameters = null;
  4289. }
  4290. return this.finishNode(node, "InterfaceExtends");
  4291. }
  4292. flowParseInterface(node) {
  4293. this.flowParseInterfaceish(node);
  4294. return this.finishNode(node, "InterfaceDeclaration");
  4295. }
  4296. checkNotUnderscore(word) {
  4297. if (word === "_") {
  4298. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4299. at: this.state.startLoc
  4300. });
  4301. }
  4302. }
  4303. checkReservedType(word, startLoc, declaration) {
  4304. if (!reservedTypes.has(word)) return;
  4305. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4306. at: startLoc,
  4307. reservedType: word
  4308. });
  4309. }
  4310. flowParseRestrictedIdentifier(liberal, declaration) {
  4311. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4312. return this.parseIdentifier(liberal);
  4313. }
  4314. flowParseTypeAlias(node) {
  4315. node.id = this.flowParseRestrictedIdentifier(false, true);
  4316. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4317. if (this.match(47)) {
  4318. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4319. } else {
  4320. node.typeParameters = null;
  4321. }
  4322. node.right = this.flowParseTypeInitialiser(29);
  4323. this.semicolon();
  4324. return this.finishNode(node, "TypeAlias");
  4325. }
  4326. flowParseOpaqueType(node, declare) {
  4327. this.expectContextual(128);
  4328. node.id = this.flowParseRestrictedIdentifier(true, true);
  4329. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4330. if (this.match(47)) {
  4331. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4332. } else {
  4333. node.typeParameters = null;
  4334. }
  4335. node.supertype = null;
  4336. if (this.match(14)) {
  4337. node.supertype = this.flowParseTypeInitialiser(14);
  4338. }
  4339. node.impltype = null;
  4340. if (!declare) {
  4341. node.impltype = this.flowParseTypeInitialiser(29);
  4342. }
  4343. this.semicolon();
  4344. return this.finishNode(node, "OpaqueType");
  4345. }
  4346. flowParseTypeParameter(requireDefault = false) {
  4347. const nodeStartLoc = this.state.startLoc;
  4348. const node = this.startNode();
  4349. const variance = this.flowParseVariance();
  4350. const ident = this.flowParseTypeAnnotatableIdentifier();
  4351. node.name = ident.name;
  4352. node.variance = variance;
  4353. node.bound = ident.typeAnnotation;
  4354. if (this.match(29)) {
  4355. this.eat(29);
  4356. node.default = this.flowParseType();
  4357. } else {
  4358. if (requireDefault) {
  4359. this.raise(FlowErrors.MissingTypeParamDefault, {
  4360. at: nodeStartLoc
  4361. });
  4362. }
  4363. }
  4364. return this.finishNode(node, "TypeParameter");
  4365. }
  4366. flowParseTypeParameterDeclaration() {
  4367. const oldInType = this.state.inType;
  4368. const node = this.startNode();
  4369. node.params = [];
  4370. this.state.inType = true;
  4371. if (this.match(47) || this.match(140)) {
  4372. this.next();
  4373. } else {
  4374. this.unexpected();
  4375. }
  4376. let defaultRequired = false;
  4377. do {
  4378. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4379. node.params.push(typeParameter);
  4380. if (typeParameter.default) {
  4381. defaultRequired = true;
  4382. }
  4383. if (!this.match(48)) {
  4384. this.expect(12);
  4385. }
  4386. } while (!this.match(48));
  4387. this.expect(48);
  4388. this.state.inType = oldInType;
  4389. return this.finishNode(node, "TypeParameterDeclaration");
  4390. }
  4391. flowParseTypeParameterInstantiation() {
  4392. const node = this.startNode();
  4393. const oldInType = this.state.inType;
  4394. node.params = [];
  4395. this.state.inType = true;
  4396. this.expect(47);
  4397. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4398. this.state.noAnonFunctionType = false;
  4399. while (!this.match(48)) {
  4400. node.params.push(this.flowParseType());
  4401. if (!this.match(48)) {
  4402. this.expect(12);
  4403. }
  4404. }
  4405. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4406. this.expect(48);
  4407. this.state.inType = oldInType;
  4408. return this.finishNode(node, "TypeParameterInstantiation");
  4409. }
  4410. flowParseTypeParameterInstantiationCallOrNew() {
  4411. const node = this.startNode();
  4412. const oldInType = this.state.inType;
  4413. node.params = [];
  4414. this.state.inType = true;
  4415. this.expect(47);
  4416. while (!this.match(48)) {
  4417. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4418. if (!this.match(48)) {
  4419. this.expect(12);
  4420. }
  4421. }
  4422. this.expect(48);
  4423. this.state.inType = oldInType;
  4424. return this.finishNode(node, "TypeParameterInstantiation");
  4425. }
  4426. flowParseInterfaceType() {
  4427. const node = this.startNode();
  4428. this.expectContextual(127);
  4429. node.extends = [];
  4430. if (this.eat(81)) {
  4431. do {
  4432. node.extends.push(this.flowParseInterfaceExtends());
  4433. } while (this.eat(12));
  4434. }
  4435. node.body = this.flowParseObjectType({
  4436. allowStatic: false,
  4437. allowExact: false,
  4438. allowSpread: false,
  4439. allowProto: false,
  4440. allowInexact: false
  4441. });
  4442. return this.finishNode(node, "InterfaceTypeAnnotation");
  4443. }
  4444. flowParseObjectPropertyKey() {
  4445. return this.match(132) || this.match(131) ? super.parseExprAtom() : this.parseIdentifier(true);
  4446. }
  4447. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4448. node.static = isStatic;
  4449. if (this.lookahead().type === 14) {
  4450. node.id = this.flowParseObjectPropertyKey();
  4451. node.key = this.flowParseTypeInitialiser();
  4452. } else {
  4453. node.id = null;
  4454. node.key = this.flowParseType();
  4455. }
  4456. this.expect(3);
  4457. node.value = this.flowParseTypeInitialiser();
  4458. node.variance = variance;
  4459. return this.finishNode(node, "ObjectTypeIndexer");
  4460. }
  4461. flowParseObjectTypeInternalSlot(node, isStatic) {
  4462. node.static = isStatic;
  4463. node.id = this.flowParseObjectPropertyKey();
  4464. this.expect(3);
  4465. this.expect(3);
  4466. if (this.match(47) || this.match(10)) {
  4467. node.method = true;
  4468. node.optional = false;
  4469. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4470. } else {
  4471. node.method = false;
  4472. if (this.eat(17)) {
  4473. node.optional = true;
  4474. }
  4475. node.value = this.flowParseTypeInitialiser();
  4476. }
  4477. return this.finishNode(node, "ObjectTypeInternalSlot");
  4478. }
  4479. flowParseObjectTypeMethodish(node) {
  4480. node.params = [];
  4481. node.rest = null;
  4482. node.typeParameters = null;
  4483. node.this = null;
  4484. if (this.match(47)) {
  4485. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4486. }
  4487. this.expect(10);
  4488. if (this.match(78)) {
  4489. node.this = this.flowParseFunctionTypeParam(true);
  4490. node.this.name = null;
  4491. if (!this.match(11)) {
  4492. this.expect(12);
  4493. }
  4494. }
  4495. while (!this.match(11) && !this.match(21)) {
  4496. node.params.push(this.flowParseFunctionTypeParam(false));
  4497. if (!this.match(11)) {
  4498. this.expect(12);
  4499. }
  4500. }
  4501. if (this.eat(21)) {
  4502. node.rest = this.flowParseFunctionTypeParam(false);
  4503. }
  4504. this.expect(11);
  4505. node.returnType = this.flowParseTypeInitialiser();
  4506. return this.finishNode(node, "FunctionTypeAnnotation");
  4507. }
  4508. flowParseObjectTypeCallProperty(node, isStatic) {
  4509. const valueNode = this.startNode();
  4510. node.static = isStatic;
  4511. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4512. return this.finishNode(node, "ObjectTypeCallProperty");
  4513. }
  4514. flowParseObjectType({
  4515. allowStatic,
  4516. allowExact,
  4517. allowSpread,
  4518. allowProto,
  4519. allowInexact
  4520. }) {
  4521. const oldInType = this.state.inType;
  4522. this.state.inType = true;
  4523. const nodeStart = this.startNode();
  4524. nodeStart.callProperties = [];
  4525. nodeStart.properties = [];
  4526. nodeStart.indexers = [];
  4527. nodeStart.internalSlots = [];
  4528. let endDelim;
  4529. let exact;
  4530. let inexact = false;
  4531. if (allowExact && this.match(6)) {
  4532. this.expect(6);
  4533. endDelim = 9;
  4534. exact = true;
  4535. } else {
  4536. this.expect(5);
  4537. endDelim = 8;
  4538. exact = false;
  4539. }
  4540. nodeStart.exact = exact;
  4541. while (!this.match(endDelim)) {
  4542. let isStatic = false;
  4543. let protoStartLoc = null;
  4544. let inexactStartLoc = null;
  4545. const node = this.startNode();
  4546. if (allowProto && this.isContextual(116)) {
  4547. const lookahead = this.lookahead();
  4548. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4549. this.next();
  4550. protoStartLoc = this.state.startLoc;
  4551. allowStatic = false;
  4552. }
  4553. }
  4554. if (allowStatic && this.isContextual(104)) {
  4555. const lookahead = this.lookahead();
  4556. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4557. this.next();
  4558. isStatic = true;
  4559. }
  4560. }
  4561. const variance = this.flowParseVariance();
  4562. if (this.eat(0)) {
  4563. if (protoStartLoc != null) {
  4564. this.unexpected(protoStartLoc);
  4565. }
  4566. if (this.eat(0)) {
  4567. if (variance) {
  4568. this.unexpected(variance.loc.start);
  4569. }
  4570. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4571. } else {
  4572. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4573. }
  4574. } else if (this.match(10) || this.match(47)) {
  4575. if (protoStartLoc != null) {
  4576. this.unexpected(protoStartLoc);
  4577. }
  4578. if (variance) {
  4579. this.unexpected(variance.loc.start);
  4580. }
  4581. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4582. } else {
  4583. let kind = "init";
  4584. if (this.isContextual(98) || this.isContextual(103)) {
  4585. const lookahead = this.lookahead();
  4586. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4587. kind = this.state.value;
  4588. this.next();
  4589. }
  4590. }
  4591. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4592. if (propOrInexact === null) {
  4593. inexact = true;
  4594. inexactStartLoc = this.state.lastTokStartLoc;
  4595. } else {
  4596. nodeStart.properties.push(propOrInexact);
  4597. }
  4598. }
  4599. this.flowObjectTypeSemicolon();
  4600. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4601. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4602. at: inexactStartLoc
  4603. });
  4604. }
  4605. }
  4606. this.expect(endDelim);
  4607. if (allowSpread) {
  4608. nodeStart.inexact = inexact;
  4609. }
  4610. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4611. this.state.inType = oldInType;
  4612. return out;
  4613. }
  4614. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4615. if (this.eat(21)) {
  4616. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4617. if (isInexactToken) {
  4618. if (!allowSpread) {
  4619. this.raise(FlowErrors.InexactInsideNonObject, {
  4620. at: this.state.lastTokStartLoc
  4621. });
  4622. } else if (!allowInexact) {
  4623. this.raise(FlowErrors.InexactInsideExact, {
  4624. at: this.state.lastTokStartLoc
  4625. });
  4626. }
  4627. if (variance) {
  4628. this.raise(FlowErrors.InexactVariance, {
  4629. at: variance
  4630. });
  4631. }
  4632. return null;
  4633. }
  4634. if (!allowSpread) {
  4635. this.raise(FlowErrors.UnexpectedSpreadType, {
  4636. at: this.state.lastTokStartLoc
  4637. });
  4638. }
  4639. if (protoStartLoc != null) {
  4640. this.unexpected(protoStartLoc);
  4641. }
  4642. if (variance) {
  4643. this.raise(FlowErrors.SpreadVariance, {
  4644. at: variance
  4645. });
  4646. }
  4647. node.argument = this.flowParseType();
  4648. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4649. } else {
  4650. node.key = this.flowParseObjectPropertyKey();
  4651. node.static = isStatic;
  4652. node.proto = protoStartLoc != null;
  4653. node.kind = kind;
  4654. let optional = false;
  4655. if (this.match(47) || this.match(10)) {
  4656. node.method = true;
  4657. if (protoStartLoc != null) {
  4658. this.unexpected(protoStartLoc);
  4659. }
  4660. if (variance) {
  4661. this.unexpected(variance.loc.start);
  4662. }
  4663. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4664. if (kind === "get" || kind === "set") {
  4665. this.flowCheckGetterSetterParams(node);
  4666. }
  4667. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4668. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4669. at: node.value.this
  4670. });
  4671. }
  4672. } else {
  4673. if (kind !== "init") this.unexpected();
  4674. node.method = false;
  4675. if (this.eat(17)) {
  4676. optional = true;
  4677. }
  4678. node.value = this.flowParseTypeInitialiser();
  4679. node.variance = variance;
  4680. }
  4681. node.optional = optional;
  4682. return this.finishNode(node, "ObjectTypeProperty");
  4683. }
  4684. }
  4685. flowCheckGetterSetterParams(property) {
  4686. const paramCount = property.kind === "get" ? 0 : 1;
  4687. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4688. if (property.value.this) {
  4689. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4690. at: property.value.this
  4691. });
  4692. }
  4693. if (length !== paramCount) {
  4694. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4695. at: property
  4696. });
  4697. }
  4698. if (property.kind === "set" && property.value.rest) {
  4699. this.raise(Errors.BadSetterRestParameter, {
  4700. at: property
  4701. });
  4702. }
  4703. }
  4704. flowObjectTypeSemicolon() {
  4705. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4706. this.unexpected();
  4707. }
  4708. }
  4709. flowParseQualifiedTypeIdentifier(startLoc, id) {
  4710. var _startLoc;
  4711. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  4712. let node = id || this.flowParseRestrictedIdentifier(true);
  4713. while (this.eat(16)) {
  4714. const node2 = this.startNodeAt(startLoc);
  4715. node2.qualification = node;
  4716. node2.id = this.flowParseRestrictedIdentifier(true);
  4717. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4718. }
  4719. return node;
  4720. }
  4721. flowParseGenericType(startLoc, id) {
  4722. const node = this.startNodeAt(startLoc);
  4723. node.typeParameters = null;
  4724. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  4725. if (this.match(47)) {
  4726. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4727. }
  4728. return this.finishNode(node, "GenericTypeAnnotation");
  4729. }
  4730. flowParseTypeofType() {
  4731. const node = this.startNode();
  4732. this.expect(87);
  4733. node.argument = this.flowParsePrimaryType();
  4734. return this.finishNode(node, "TypeofTypeAnnotation");
  4735. }
  4736. flowParseTupleType() {
  4737. const node = this.startNode();
  4738. node.types = [];
  4739. this.expect(0);
  4740. while (this.state.pos < this.length && !this.match(3)) {
  4741. node.types.push(this.flowParseType());
  4742. if (this.match(3)) break;
  4743. this.expect(12);
  4744. }
  4745. this.expect(3);
  4746. return this.finishNode(node, "TupleTypeAnnotation");
  4747. }
  4748. flowParseFunctionTypeParam(first) {
  4749. let name = null;
  4750. let optional = false;
  4751. let typeAnnotation = null;
  4752. const node = this.startNode();
  4753. const lh = this.lookahead();
  4754. const isThis = this.state.type === 78;
  4755. if (lh.type === 14 || lh.type === 17) {
  4756. if (isThis && !first) {
  4757. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4758. at: node
  4759. });
  4760. }
  4761. name = this.parseIdentifier(isThis);
  4762. if (this.eat(17)) {
  4763. optional = true;
  4764. if (isThis) {
  4765. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4766. at: node
  4767. });
  4768. }
  4769. }
  4770. typeAnnotation = this.flowParseTypeInitialiser();
  4771. } else {
  4772. typeAnnotation = this.flowParseType();
  4773. }
  4774. node.name = name;
  4775. node.optional = optional;
  4776. node.typeAnnotation = typeAnnotation;
  4777. return this.finishNode(node, "FunctionTypeParam");
  4778. }
  4779. reinterpretTypeAsFunctionTypeParam(type) {
  4780. const node = this.startNodeAt(type.loc.start);
  4781. node.name = null;
  4782. node.optional = false;
  4783. node.typeAnnotation = type;
  4784. return this.finishNode(node, "FunctionTypeParam");
  4785. }
  4786. flowParseFunctionTypeParams(params = []) {
  4787. let rest = null;
  4788. let _this = null;
  4789. if (this.match(78)) {
  4790. _this = this.flowParseFunctionTypeParam(true);
  4791. _this.name = null;
  4792. if (!this.match(11)) {
  4793. this.expect(12);
  4794. }
  4795. }
  4796. while (!this.match(11) && !this.match(21)) {
  4797. params.push(this.flowParseFunctionTypeParam(false));
  4798. if (!this.match(11)) {
  4799. this.expect(12);
  4800. }
  4801. }
  4802. if (this.eat(21)) {
  4803. rest = this.flowParseFunctionTypeParam(false);
  4804. }
  4805. return {
  4806. params,
  4807. rest,
  4808. _this
  4809. };
  4810. }
  4811. flowIdentToTypeAnnotation(startLoc, node, id) {
  4812. switch (id.name) {
  4813. case "any":
  4814. return this.finishNode(node, "AnyTypeAnnotation");
  4815. case "bool":
  4816. case "boolean":
  4817. return this.finishNode(node, "BooleanTypeAnnotation");
  4818. case "mixed":
  4819. return this.finishNode(node, "MixedTypeAnnotation");
  4820. case "empty":
  4821. return this.finishNode(node, "EmptyTypeAnnotation");
  4822. case "number":
  4823. return this.finishNode(node, "NumberTypeAnnotation");
  4824. case "string":
  4825. return this.finishNode(node, "StringTypeAnnotation");
  4826. case "symbol":
  4827. return this.finishNode(node, "SymbolTypeAnnotation");
  4828. default:
  4829. this.checkNotUnderscore(id.name);
  4830. return this.flowParseGenericType(startLoc, id);
  4831. }
  4832. }
  4833. flowParsePrimaryType() {
  4834. const startLoc = this.state.startLoc;
  4835. const node = this.startNode();
  4836. let tmp;
  4837. let type;
  4838. let isGroupedType = false;
  4839. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4840. switch (this.state.type) {
  4841. case 5:
  4842. return this.flowParseObjectType({
  4843. allowStatic: false,
  4844. allowExact: false,
  4845. allowSpread: true,
  4846. allowProto: false,
  4847. allowInexact: true
  4848. });
  4849. case 6:
  4850. return this.flowParseObjectType({
  4851. allowStatic: false,
  4852. allowExact: true,
  4853. allowSpread: true,
  4854. allowProto: false,
  4855. allowInexact: false
  4856. });
  4857. case 0:
  4858. this.state.noAnonFunctionType = false;
  4859. type = this.flowParseTupleType();
  4860. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4861. return type;
  4862. case 47:
  4863. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4864. this.expect(10);
  4865. tmp = this.flowParseFunctionTypeParams();
  4866. node.params = tmp.params;
  4867. node.rest = tmp.rest;
  4868. node.this = tmp._this;
  4869. this.expect(11);
  4870. this.expect(19);
  4871. node.returnType = this.flowParseType();
  4872. return this.finishNode(node, "FunctionTypeAnnotation");
  4873. case 10:
  4874. this.next();
  4875. if (!this.match(11) && !this.match(21)) {
  4876. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4877. const token = this.lookahead().type;
  4878. isGroupedType = token !== 17 && token !== 14;
  4879. } else {
  4880. isGroupedType = true;
  4881. }
  4882. }
  4883. if (isGroupedType) {
  4884. this.state.noAnonFunctionType = false;
  4885. type = this.flowParseType();
  4886. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4887. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4888. this.expect(11);
  4889. return type;
  4890. } else {
  4891. this.eat(12);
  4892. }
  4893. }
  4894. if (type) {
  4895. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4896. } else {
  4897. tmp = this.flowParseFunctionTypeParams();
  4898. }
  4899. node.params = tmp.params;
  4900. node.rest = tmp.rest;
  4901. node.this = tmp._this;
  4902. this.expect(11);
  4903. this.expect(19);
  4904. node.returnType = this.flowParseType();
  4905. node.typeParameters = null;
  4906. return this.finishNode(node, "FunctionTypeAnnotation");
  4907. case 131:
  4908. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4909. case 85:
  4910. case 86:
  4911. node.value = this.match(85);
  4912. this.next();
  4913. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4914. case 53:
  4915. if (this.state.value === "-") {
  4916. this.next();
  4917. if (this.match(132)) {
  4918. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4919. }
  4920. if (this.match(133)) {
  4921. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4922. }
  4923. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4924. at: this.state.startLoc
  4925. });
  4926. }
  4927. throw this.unexpected();
  4928. case 132:
  4929. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4930. case 133:
  4931. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4932. case 88:
  4933. this.next();
  4934. return this.finishNode(node, "VoidTypeAnnotation");
  4935. case 84:
  4936. this.next();
  4937. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4938. case 78:
  4939. this.next();
  4940. return this.finishNode(node, "ThisTypeAnnotation");
  4941. case 55:
  4942. this.next();
  4943. return this.finishNode(node, "ExistsTypeAnnotation");
  4944. case 87:
  4945. return this.flowParseTypeofType();
  4946. default:
  4947. if (tokenIsKeyword(this.state.type)) {
  4948. const label = tokenLabelName(this.state.type);
  4949. this.next();
  4950. return super.createIdentifier(node, label);
  4951. } else if (tokenIsIdentifier(this.state.type)) {
  4952. if (this.isContextual(127)) {
  4953. return this.flowParseInterfaceType();
  4954. }
  4955. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  4956. }
  4957. }
  4958. throw this.unexpected();
  4959. }
  4960. flowParsePostfixType() {
  4961. const startLoc = this.state.startLoc;
  4962. let type = this.flowParsePrimaryType();
  4963. let seenOptionalIndexedAccess = false;
  4964. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4965. const node = this.startNodeAt(startLoc);
  4966. const optional = this.eat(18);
  4967. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4968. this.expect(0);
  4969. if (!optional && this.match(3)) {
  4970. node.elementType = type;
  4971. this.next();
  4972. type = this.finishNode(node, "ArrayTypeAnnotation");
  4973. } else {
  4974. node.objectType = type;
  4975. node.indexType = this.flowParseType();
  4976. this.expect(3);
  4977. if (seenOptionalIndexedAccess) {
  4978. node.optional = optional;
  4979. type = this.finishNode(node, "OptionalIndexedAccessType");
  4980. } else {
  4981. type = this.finishNode(node, "IndexedAccessType");
  4982. }
  4983. }
  4984. }
  4985. return type;
  4986. }
  4987. flowParsePrefixType() {
  4988. const node = this.startNode();
  4989. if (this.eat(17)) {
  4990. node.typeAnnotation = this.flowParsePrefixType();
  4991. return this.finishNode(node, "NullableTypeAnnotation");
  4992. } else {
  4993. return this.flowParsePostfixType();
  4994. }
  4995. }
  4996. flowParseAnonFunctionWithoutParens() {
  4997. const param = this.flowParsePrefixType();
  4998. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4999. const node = this.startNodeAt(param.loc.start);
  5000. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  5001. node.rest = null;
  5002. node.this = null;
  5003. node.returnType = this.flowParseType();
  5004. node.typeParameters = null;
  5005. return this.finishNode(node, "FunctionTypeAnnotation");
  5006. }
  5007. return param;
  5008. }
  5009. flowParseIntersectionType() {
  5010. const node = this.startNode();
  5011. this.eat(45);
  5012. const type = this.flowParseAnonFunctionWithoutParens();
  5013. node.types = [type];
  5014. while (this.eat(45)) {
  5015. node.types.push(this.flowParseAnonFunctionWithoutParens());
  5016. }
  5017. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  5018. }
  5019. flowParseUnionType() {
  5020. const node = this.startNode();
  5021. this.eat(43);
  5022. const type = this.flowParseIntersectionType();
  5023. node.types = [type];
  5024. while (this.eat(43)) {
  5025. node.types.push(this.flowParseIntersectionType());
  5026. }
  5027. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  5028. }
  5029. flowParseType() {
  5030. const oldInType = this.state.inType;
  5031. this.state.inType = true;
  5032. const type = this.flowParseUnionType();
  5033. this.state.inType = oldInType;
  5034. return type;
  5035. }
  5036. flowParseTypeOrImplicitInstantiation() {
  5037. if (this.state.type === 130 && this.state.value === "_") {
  5038. const startLoc = this.state.startLoc;
  5039. const node = this.parseIdentifier();
  5040. return this.flowParseGenericType(startLoc, node);
  5041. } else {
  5042. return this.flowParseType();
  5043. }
  5044. }
  5045. flowParseTypeAnnotation() {
  5046. const node = this.startNode();
  5047. node.typeAnnotation = this.flowParseTypeInitialiser();
  5048. return this.finishNode(node, "TypeAnnotation");
  5049. }
  5050. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  5051. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  5052. if (this.match(14)) {
  5053. ident.typeAnnotation = this.flowParseTypeAnnotation();
  5054. this.resetEndLocation(ident);
  5055. }
  5056. return ident;
  5057. }
  5058. typeCastToParameter(node) {
  5059. node.expression.typeAnnotation = node.typeAnnotation;
  5060. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5061. return node.expression;
  5062. }
  5063. flowParseVariance() {
  5064. let variance = null;
  5065. if (this.match(53)) {
  5066. variance = this.startNode();
  5067. if (this.state.value === "+") {
  5068. variance.kind = "plus";
  5069. } else {
  5070. variance.kind = "minus";
  5071. }
  5072. this.next();
  5073. return this.finishNode(variance, "Variance");
  5074. }
  5075. return variance;
  5076. }
  5077. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5078. if (allowExpressionBody) {
  5079. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5080. }
  5081. return super.parseFunctionBody(node, false, isMethod);
  5082. }
  5083. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5084. if (this.match(14)) {
  5085. const typeNode = this.startNode();
  5086. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5087. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5088. }
  5089. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5090. }
  5091. parseStatementLike(flags) {
  5092. if (this.state.strict && this.isContextual(127)) {
  5093. const lookahead = this.lookahead();
  5094. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5095. const node = this.startNode();
  5096. this.next();
  5097. return this.flowParseInterface(node);
  5098. }
  5099. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5100. const node = this.startNode();
  5101. this.next();
  5102. return this.flowParseEnumDeclaration(node);
  5103. }
  5104. const stmt = super.parseStatementLike(flags);
  5105. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5106. this.flowPragma = null;
  5107. }
  5108. return stmt;
  5109. }
  5110. parseExpressionStatement(node, expr, decorators) {
  5111. if (expr.type === "Identifier") {
  5112. if (expr.name === "declare") {
  5113. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5114. return this.flowParseDeclare(node);
  5115. }
  5116. } else if (tokenIsIdentifier(this.state.type)) {
  5117. if (expr.name === "interface") {
  5118. return this.flowParseInterface(node);
  5119. } else if (expr.name === "type") {
  5120. return this.flowParseTypeAlias(node);
  5121. } else if (expr.name === "opaque") {
  5122. return this.flowParseOpaqueType(node, false);
  5123. }
  5124. }
  5125. }
  5126. return super.parseExpressionStatement(node, expr, decorators);
  5127. }
  5128. shouldParseExportDeclaration() {
  5129. const {
  5130. type
  5131. } = this.state;
  5132. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5133. return !this.state.containsEsc;
  5134. }
  5135. return super.shouldParseExportDeclaration();
  5136. }
  5137. isExportDefaultSpecifier() {
  5138. const {
  5139. type
  5140. } = this.state;
  5141. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5142. return this.state.containsEsc;
  5143. }
  5144. return super.isExportDefaultSpecifier();
  5145. }
  5146. parseExportDefaultExpression() {
  5147. if (this.shouldParseEnums() && this.isContextual(124)) {
  5148. const node = this.startNode();
  5149. this.next();
  5150. return this.flowParseEnumDeclaration(node);
  5151. }
  5152. return super.parseExportDefaultExpression();
  5153. }
  5154. parseConditional(expr, startLoc, refExpressionErrors) {
  5155. if (!this.match(17)) return expr;
  5156. if (this.state.maybeInArrowParameters) {
  5157. const nextCh = this.lookaheadCharCode();
  5158. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5159. this.setOptionalParametersError(refExpressionErrors);
  5160. return expr;
  5161. }
  5162. }
  5163. this.expect(17);
  5164. const state = this.state.clone();
  5165. const originalNoArrowAt = this.state.noArrowAt;
  5166. const node = this.startNodeAt(startLoc);
  5167. let {
  5168. consequent,
  5169. failed
  5170. } = this.tryParseConditionalConsequent();
  5171. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5172. if (failed || invalid.length > 0) {
  5173. const noArrowAt = [...originalNoArrowAt];
  5174. if (invalid.length > 0) {
  5175. this.state = state;
  5176. this.state.noArrowAt = noArrowAt;
  5177. for (let i = 0; i < invalid.length; i++) {
  5178. noArrowAt.push(invalid[i].start);
  5179. }
  5180. ({
  5181. consequent,
  5182. failed
  5183. } = this.tryParseConditionalConsequent());
  5184. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5185. }
  5186. if (failed && valid.length > 1) {
  5187. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5188. at: state.startLoc
  5189. });
  5190. }
  5191. if (failed && valid.length === 1) {
  5192. this.state = state;
  5193. noArrowAt.push(valid[0].start);
  5194. this.state.noArrowAt = noArrowAt;
  5195. ({
  5196. consequent,
  5197. failed
  5198. } = this.tryParseConditionalConsequent());
  5199. }
  5200. }
  5201. this.getArrowLikeExpressions(consequent, true);
  5202. this.state.noArrowAt = originalNoArrowAt;
  5203. this.expect(14);
  5204. node.test = expr;
  5205. node.consequent = consequent;
  5206. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5207. return this.finishNode(node, "ConditionalExpression");
  5208. }
  5209. tryParseConditionalConsequent() {
  5210. this.state.noArrowParamsConversionAt.push(this.state.start);
  5211. const consequent = this.parseMaybeAssignAllowIn();
  5212. const failed = !this.match(14);
  5213. this.state.noArrowParamsConversionAt.pop();
  5214. return {
  5215. consequent,
  5216. failed
  5217. };
  5218. }
  5219. getArrowLikeExpressions(node, disallowInvalid) {
  5220. const stack = [node];
  5221. const arrows = [];
  5222. while (stack.length !== 0) {
  5223. const node = stack.pop();
  5224. if (node.type === "ArrowFunctionExpression") {
  5225. if (node.typeParameters || !node.returnType) {
  5226. this.finishArrowValidation(node);
  5227. } else {
  5228. arrows.push(node);
  5229. }
  5230. stack.push(node.body);
  5231. } else if (node.type === "ConditionalExpression") {
  5232. stack.push(node.consequent);
  5233. stack.push(node.alternate);
  5234. }
  5235. }
  5236. if (disallowInvalid) {
  5237. arrows.forEach(node => this.finishArrowValidation(node));
  5238. return [arrows, []];
  5239. }
  5240. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5241. }
  5242. finishArrowValidation(node) {
  5243. var _node$extra;
  5244. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5245. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5246. super.checkParams(node, false, true);
  5247. this.scope.exit();
  5248. }
  5249. forwardNoArrowParamsConversionAt(node, parse) {
  5250. let result;
  5251. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5252. this.state.noArrowParamsConversionAt.push(this.state.start);
  5253. result = parse();
  5254. this.state.noArrowParamsConversionAt.pop();
  5255. } else {
  5256. result = parse();
  5257. }
  5258. return result;
  5259. }
  5260. parseParenItem(node, startLoc) {
  5261. node = super.parseParenItem(node, startLoc);
  5262. if (this.eat(17)) {
  5263. node.optional = true;
  5264. this.resetEndLocation(node);
  5265. }
  5266. if (this.match(14)) {
  5267. const typeCastNode = this.startNodeAt(startLoc);
  5268. typeCastNode.expression = node;
  5269. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5270. return this.finishNode(typeCastNode, "TypeCastExpression");
  5271. }
  5272. return node;
  5273. }
  5274. assertModuleNodeAllowed(node) {
  5275. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5276. return;
  5277. }
  5278. super.assertModuleNodeAllowed(node);
  5279. }
  5280. parseExport(node, decorators) {
  5281. const decl = super.parseExport(node, decorators);
  5282. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  5283. decl.exportKind = decl.exportKind || "value";
  5284. }
  5285. return decl;
  5286. }
  5287. parseExportDeclaration(node) {
  5288. if (this.isContextual(128)) {
  5289. node.exportKind = "type";
  5290. const declarationNode = this.startNode();
  5291. this.next();
  5292. if (this.match(5)) {
  5293. node.specifiers = this.parseExportSpecifiers(true);
  5294. super.parseExportFrom(node);
  5295. return null;
  5296. } else {
  5297. return this.flowParseTypeAlias(declarationNode);
  5298. }
  5299. } else if (this.isContextual(129)) {
  5300. node.exportKind = "type";
  5301. const declarationNode = this.startNode();
  5302. this.next();
  5303. return this.flowParseOpaqueType(declarationNode, false);
  5304. } else if (this.isContextual(127)) {
  5305. node.exportKind = "type";
  5306. const declarationNode = this.startNode();
  5307. this.next();
  5308. return this.flowParseInterface(declarationNode);
  5309. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5310. node.exportKind = "value";
  5311. const declarationNode = this.startNode();
  5312. this.next();
  5313. return this.flowParseEnumDeclaration(declarationNode);
  5314. } else {
  5315. return super.parseExportDeclaration(node);
  5316. }
  5317. }
  5318. eatExportStar(node) {
  5319. if (super.eatExportStar(node)) return true;
  5320. if (this.isContextual(128) && this.lookahead().type === 55) {
  5321. node.exportKind = "type";
  5322. this.next();
  5323. this.next();
  5324. return true;
  5325. }
  5326. return false;
  5327. }
  5328. maybeParseExportNamespaceSpecifier(node) {
  5329. const {
  5330. startLoc
  5331. } = this.state;
  5332. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5333. if (hasNamespace && node.exportKind === "type") {
  5334. this.unexpected(startLoc);
  5335. }
  5336. return hasNamespace;
  5337. }
  5338. parseClassId(node, isStatement, optionalId) {
  5339. super.parseClassId(node, isStatement, optionalId);
  5340. if (this.match(47)) {
  5341. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5342. }
  5343. }
  5344. parseClassMember(classBody, member, state) {
  5345. const {
  5346. startLoc
  5347. } = this.state;
  5348. if (this.isContextual(123)) {
  5349. if (super.parseClassMemberFromModifier(classBody, member)) {
  5350. return;
  5351. }
  5352. member.declare = true;
  5353. }
  5354. super.parseClassMember(classBody, member, state);
  5355. if (member.declare) {
  5356. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5357. this.raise(FlowErrors.DeclareClassElement, {
  5358. at: startLoc
  5359. });
  5360. } else if (member.value) {
  5361. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5362. at: member.value
  5363. });
  5364. }
  5365. }
  5366. }
  5367. isIterator(word) {
  5368. return word === "iterator" || word === "asyncIterator";
  5369. }
  5370. readIterator() {
  5371. const word = super.readWord1();
  5372. const fullWord = "@@" + word;
  5373. if (!this.isIterator(word) || !this.state.inType) {
  5374. this.raise(Errors.InvalidIdentifier, {
  5375. at: this.state.curPosition(),
  5376. identifierName: fullWord
  5377. });
  5378. }
  5379. this.finishToken(130, fullWord);
  5380. }
  5381. getTokenFromCode(code) {
  5382. const next = this.input.charCodeAt(this.state.pos + 1);
  5383. if (code === 123 && next === 124) {
  5384. return this.finishOp(6, 2);
  5385. } else if (this.state.inType && (code === 62 || code === 60)) {
  5386. return this.finishOp(code === 62 ? 48 : 47, 1);
  5387. } else if (this.state.inType && code === 63) {
  5388. if (next === 46) {
  5389. return this.finishOp(18, 2);
  5390. }
  5391. return this.finishOp(17, 1);
  5392. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5393. this.state.pos += 2;
  5394. return this.readIterator();
  5395. } else {
  5396. return super.getTokenFromCode(code);
  5397. }
  5398. }
  5399. isAssignable(node, isBinding) {
  5400. if (node.type === "TypeCastExpression") {
  5401. return this.isAssignable(node.expression, isBinding);
  5402. } else {
  5403. return super.isAssignable(node, isBinding);
  5404. }
  5405. }
  5406. toAssignable(node, isLHS = false) {
  5407. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5408. node.left = this.typeCastToParameter(node.left);
  5409. }
  5410. super.toAssignable(node, isLHS);
  5411. }
  5412. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5413. for (let i = 0; i < exprList.length; i++) {
  5414. const expr = exprList[i];
  5415. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5416. exprList[i] = this.typeCastToParameter(expr);
  5417. }
  5418. }
  5419. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5420. }
  5421. toReferencedList(exprList, isParenthesizedExpr) {
  5422. for (let i = 0; i < exprList.length; i++) {
  5423. var _expr$extra;
  5424. const expr = exprList[i];
  5425. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5426. this.raise(FlowErrors.TypeCastInPattern, {
  5427. at: expr.typeAnnotation
  5428. });
  5429. }
  5430. }
  5431. return exprList;
  5432. }
  5433. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5434. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5435. if (canBePattern && !this.state.maybeInArrowParameters) {
  5436. this.toReferencedList(node.elements);
  5437. }
  5438. return node;
  5439. }
  5440. isValidLVal(type, isParenthesized, binding) {
  5441. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5442. }
  5443. parseClassProperty(node) {
  5444. if (this.match(14)) {
  5445. node.typeAnnotation = this.flowParseTypeAnnotation();
  5446. }
  5447. return super.parseClassProperty(node);
  5448. }
  5449. parseClassPrivateProperty(node) {
  5450. if (this.match(14)) {
  5451. node.typeAnnotation = this.flowParseTypeAnnotation();
  5452. }
  5453. return super.parseClassPrivateProperty(node);
  5454. }
  5455. isClassMethod() {
  5456. return this.match(47) || super.isClassMethod();
  5457. }
  5458. isClassProperty() {
  5459. return this.match(14) || super.isClassProperty();
  5460. }
  5461. isNonstaticConstructor(method) {
  5462. return !this.match(14) && super.isNonstaticConstructor(method);
  5463. }
  5464. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5465. if (method.variance) {
  5466. this.unexpected(method.variance.loc.start);
  5467. }
  5468. delete method.variance;
  5469. if (this.match(47)) {
  5470. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5471. }
  5472. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5473. if (method.params && isConstructor) {
  5474. const params = method.params;
  5475. if (params.length > 0 && this.isThisParam(params[0])) {
  5476. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5477. at: method
  5478. });
  5479. }
  5480. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5481. const params = method.value.params;
  5482. if (params.length > 0 && this.isThisParam(params[0])) {
  5483. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5484. at: method
  5485. });
  5486. }
  5487. }
  5488. }
  5489. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5490. if (method.variance) {
  5491. this.unexpected(method.variance.loc.start);
  5492. }
  5493. delete method.variance;
  5494. if (this.match(47)) {
  5495. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5496. }
  5497. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5498. }
  5499. parseClassSuper(node) {
  5500. super.parseClassSuper(node);
  5501. if (node.superClass && this.match(47)) {
  5502. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5503. }
  5504. if (this.isContextual(111)) {
  5505. this.next();
  5506. const implemented = node.implements = [];
  5507. do {
  5508. const node = this.startNode();
  5509. node.id = this.flowParseRestrictedIdentifier(true);
  5510. if (this.match(47)) {
  5511. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5512. } else {
  5513. node.typeParameters = null;
  5514. }
  5515. implemented.push(this.finishNode(node, "ClassImplements"));
  5516. } while (this.eat(12));
  5517. }
  5518. }
  5519. checkGetterSetterParams(method) {
  5520. super.checkGetterSetterParams(method);
  5521. const params = this.getObjectOrClassMethodParams(method);
  5522. if (params.length > 0) {
  5523. const param = params[0];
  5524. if (this.isThisParam(param) && method.kind === "get") {
  5525. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5526. at: param
  5527. });
  5528. } else if (this.isThisParam(param)) {
  5529. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5530. at: param
  5531. });
  5532. }
  5533. }
  5534. }
  5535. parsePropertyNamePrefixOperator(node) {
  5536. node.variance = this.flowParseVariance();
  5537. }
  5538. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5539. if (prop.variance) {
  5540. this.unexpected(prop.variance.loc.start);
  5541. }
  5542. delete prop.variance;
  5543. let typeParameters;
  5544. if (this.match(47) && !isAccessor) {
  5545. typeParameters = this.flowParseTypeParameterDeclaration();
  5546. if (!this.match(10)) this.unexpected();
  5547. }
  5548. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5549. if (typeParameters) {
  5550. (result.value || result).typeParameters = typeParameters;
  5551. }
  5552. return result;
  5553. }
  5554. parseAssignableListItemTypes(param) {
  5555. if (this.eat(17)) {
  5556. if (param.type !== "Identifier") {
  5557. this.raise(FlowErrors.PatternIsOptional, {
  5558. at: param
  5559. });
  5560. }
  5561. if (this.isThisParam(param)) {
  5562. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5563. at: param
  5564. });
  5565. }
  5566. param.optional = true;
  5567. }
  5568. if (this.match(14)) {
  5569. param.typeAnnotation = this.flowParseTypeAnnotation();
  5570. } else if (this.isThisParam(param)) {
  5571. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5572. at: param
  5573. });
  5574. }
  5575. if (this.match(29) && this.isThisParam(param)) {
  5576. this.raise(FlowErrors.ThisParamNoDefault, {
  5577. at: param
  5578. });
  5579. }
  5580. this.resetEndLocation(param);
  5581. return param;
  5582. }
  5583. parseMaybeDefault(startLoc, left) {
  5584. const node = super.parseMaybeDefault(startLoc, left);
  5585. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5586. this.raise(FlowErrors.TypeBeforeInitializer, {
  5587. at: node.typeAnnotation
  5588. });
  5589. }
  5590. return node;
  5591. }
  5592. shouldParseDefaultImport(node) {
  5593. if (!hasTypeImportKind(node)) {
  5594. return super.shouldParseDefaultImport(node);
  5595. }
  5596. return isMaybeDefaultImport(this.state.type);
  5597. }
  5598. checkImportReflection(node) {
  5599. super.checkImportReflection(node);
  5600. if (node.module && node.importKind !== "value") {
  5601. this.raise(FlowErrors.ImportReflectionHasImportType, {
  5602. at: node.specifiers[0].loc.start
  5603. });
  5604. }
  5605. }
  5606. parseImportSpecifierLocal(node, specifier, type) {
  5607. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5608. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5609. }
  5610. maybeParseDefaultImportSpecifier(node) {
  5611. node.importKind = "value";
  5612. let kind = null;
  5613. if (this.match(87)) {
  5614. kind = "typeof";
  5615. } else if (this.isContextual(128)) {
  5616. kind = "type";
  5617. }
  5618. if (kind) {
  5619. const lh = this.lookahead();
  5620. const {
  5621. type
  5622. } = lh;
  5623. if (kind === "type" && type === 55) {
  5624. this.unexpected(null, lh.type);
  5625. }
  5626. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5627. this.next();
  5628. node.importKind = kind;
  5629. }
  5630. }
  5631. return super.maybeParseDefaultImportSpecifier(node);
  5632. }
  5633. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5634. const firstIdent = specifier.imported;
  5635. let specifierTypeKind = null;
  5636. if (firstIdent.type === "Identifier") {
  5637. if (firstIdent.name === "type") {
  5638. specifierTypeKind = "type";
  5639. } else if (firstIdent.name === "typeof") {
  5640. specifierTypeKind = "typeof";
  5641. }
  5642. }
  5643. let isBinding = false;
  5644. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5645. const as_ident = this.parseIdentifier(true);
  5646. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5647. specifier.imported = as_ident;
  5648. specifier.importKind = specifierTypeKind;
  5649. specifier.local = cloneIdentifier(as_ident);
  5650. } else {
  5651. specifier.imported = firstIdent;
  5652. specifier.importKind = null;
  5653. specifier.local = this.parseIdentifier();
  5654. }
  5655. } else {
  5656. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5657. specifier.imported = this.parseIdentifier(true);
  5658. specifier.importKind = specifierTypeKind;
  5659. } else {
  5660. if (importedIsString) {
  5661. throw this.raise(Errors.ImportBindingIsString, {
  5662. at: specifier,
  5663. importName: firstIdent.value
  5664. });
  5665. }
  5666. specifier.imported = firstIdent;
  5667. specifier.importKind = null;
  5668. }
  5669. if (this.eatContextual(93)) {
  5670. specifier.local = this.parseIdentifier();
  5671. } else {
  5672. isBinding = true;
  5673. specifier.local = cloneIdentifier(specifier.imported);
  5674. }
  5675. }
  5676. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5677. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5678. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5679. at: specifier
  5680. });
  5681. }
  5682. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5683. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5684. }
  5685. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5686. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5687. }
  5688. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5689. }
  5690. parseBindingAtom() {
  5691. switch (this.state.type) {
  5692. case 78:
  5693. return this.parseIdentifier(true);
  5694. default:
  5695. return super.parseBindingAtom();
  5696. }
  5697. }
  5698. parseFunctionParams(node, isConstructor) {
  5699. const kind = node.kind;
  5700. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5701. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5702. }
  5703. super.parseFunctionParams(node, isConstructor);
  5704. }
  5705. parseVarId(decl, kind) {
  5706. super.parseVarId(decl, kind);
  5707. if (this.match(14)) {
  5708. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5709. this.resetEndLocation(decl.id);
  5710. }
  5711. }
  5712. parseAsyncArrowFromCallExpression(node, call) {
  5713. if (this.match(14)) {
  5714. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5715. this.state.noAnonFunctionType = true;
  5716. node.returnType = this.flowParseTypeAnnotation();
  5717. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5718. }
  5719. return super.parseAsyncArrowFromCallExpression(node, call);
  5720. }
  5721. shouldParseAsyncArrow() {
  5722. return this.match(14) || super.shouldParseAsyncArrow();
  5723. }
  5724. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5725. var _jsx;
  5726. let state = null;
  5727. let jsx;
  5728. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  5729. state = this.state.clone();
  5730. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5731. if (!jsx.error) return jsx.node;
  5732. const {
  5733. context
  5734. } = this.state;
  5735. const currentContext = context[context.length - 1];
  5736. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5737. context.pop();
  5738. }
  5739. }
  5740. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5741. var _jsx2, _jsx3;
  5742. state = state || this.state.clone();
  5743. let typeParameters;
  5744. const arrow = this.tryParse(abort => {
  5745. var _arrowExpression$extr;
  5746. typeParameters = this.flowParseTypeParameterDeclaration();
  5747. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5748. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5749. this.resetStartLocationFromNode(result, typeParameters);
  5750. return result;
  5751. });
  5752. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5753. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5754. if (expr.type !== "ArrowFunctionExpression") abort();
  5755. expr.typeParameters = typeParameters;
  5756. this.resetStartLocationFromNode(expr, typeParameters);
  5757. return arrowExpression;
  5758. }, state);
  5759. let arrowExpression = null;
  5760. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5761. if (!arrow.error && !arrow.aborted) {
  5762. if (arrow.node.async) {
  5763. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5764. at: typeParameters
  5765. });
  5766. }
  5767. return arrow.node;
  5768. }
  5769. arrowExpression = arrow.node;
  5770. }
  5771. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5772. this.state = jsx.failState;
  5773. return jsx.node;
  5774. }
  5775. if (arrowExpression) {
  5776. this.state = arrow.failState;
  5777. return arrowExpression;
  5778. }
  5779. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5780. if (arrow.thrown) throw arrow.error;
  5781. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5782. at: typeParameters
  5783. });
  5784. }
  5785. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5786. }
  5787. parseArrow(node) {
  5788. if (this.match(14)) {
  5789. const result = this.tryParse(() => {
  5790. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5791. this.state.noAnonFunctionType = true;
  5792. const typeNode = this.startNode();
  5793. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5794. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5795. if (this.canInsertSemicolon()) this.unexpected();
  5796. if (!this.match(19)) this.unexpected();
  5797. return typeNode;
  5798. });
  5799. if (result.thrown) return null;
  5800. if (result.error) this.state = result.failState;
  5801. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5802. }
  5803. return super.parseArrow(node);
  5804. }
  5805. shouldParseArrow(params) {
  5806. return this.match(14) || super.shouldParseArrow(params);
  5807. }
  5808. setArrowFunctionParameters(node, params) {
  5809. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5810. node.params = params;
  5811. } else {
  5812. super.setArrowFunctionParameters(node, params);
  5813. }
  5814. }
  5815. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5816. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5817. return;
  5818. }
  5819. for (let i = 0; i < node.params.length; i++) {
  5820. if (this.isThisParam(node.params[i]) && i > 0) {
  5821. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5822. at: node.params[i]
  5823. });
  5824. }
  5825. }
  5826. return super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5827. }
  5828. parseParenAndDistinguishExpression(canBeArrow) {
  5829. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5830. }
  5831. parseSubscripts(base, startLoc, noCalls) {
  5832. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startLoc.index) !== -1) {
  5833. this.next();
  5834. const node = this.startNodeAt(startLoc);
  5835. node.callee = base;
  5836. node.arguments = super.parseCallExpressionArguments(11, false);
  5837. base = this.finishNode(node, "CallExpression");
  5838. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5839. const state = this.state.clone();
  5840. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  5841. if (!arrow.error && !arrow.aborted) return arrow.node;
  5842. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  5843. if (result.node && !result.error) return result.node;
  5844. if (arrow.node) {
  5845. this.state = arrow.failState;
  5846. return arrow.node;
  5847. }
  5848. if (result.node) {
  5849. this.state = result.failState;
  5850. return result.node;
  5851. }
  5852. throw arrow.error || result.error;
  5853. }
  5854. return super.parseSubscripts(base, startLoc, noCalls);
  5855. }
  5856. parseSubscript(base, startLoc, noCalls, subscriptState) {
  5857. if (this.match(18) && this.isLookaheadToken_lt()) {
  5858. subscriptState.optionalChainMember = true;
  5859. if (noCalls) {
  5860. subscriptState.stop = true;
  5861. return base;
  5862. }
  5863. this.next();
  5864. const node = this.startNodeAt(startLoc);
  5865. node.callee = base;
  5866. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5867. this.expect(10);
  5868. node.arguments = this.parseCallExpressionArguments(11, false);
  5869. node.optional = true;
  5870. return this.finishCallExpression(node, true);
  5871. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5872. const node = this.startNodeAt(startLoc);
  5873. node.callee = base;
  5874. const result = this.tryParse(() => {
  5875. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5876. this.expect(10);
  5877. node.arguments = super.parseCallExpressionArguments(11, false);
  5878. if (subscriptState.optionalChainMember) {
  5879. node.optional = false;
  5880. }
  5881. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5882. });
  5883. if (result.node) {
  5884. if (result.error) this.state = result.failState;
  5885. return result.node;
  5886. }
  5887. }
  5888. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  5889. }
  5890. parseNewCallee(node) {
  5891. super.parseNewCallee(node);
  5892. let targs = null;
  5893. if (this.shouldParseTypes() && this.match(47)) {
  5894. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5895. }
  5896. node.typeArguments = targs;
  5897. }
  5898. parseAsyncArrowWithTypeParameters(startLoc) {
  5899. const node = this.startNodeAt(startLoc);
  5900. this.parseFunctionParams(node, false);
  5901. if (!this.parseArrow(node)) return;
  5902. return super.parseArrowExpression(node, undefined, true);
  5903. }
  5904. readToken_mult_modulo(code) {
  5905. const next = this.input.charCodeAt(this.state.pos + 1);
  5906. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5907. this.state.hasFlowComment = false;
  5908. this.state.pos += 2;
  5909. this.nextToken();
  5910. return;
  5911. }
  5912. super.readToken_mult_modulo(code);
  5913. }
  5914. readToken_pipe_amp(code) {
  5915. const next = this.input.charCodeAt(this.state.pos + 1);
  5916. if (code === 124 && next === 125) {
  5917. this.finishOp(9, 2);
  5918. return;
  5919. }
  5920. super.readToken_pipe_amp(code);
  5921. }
  5922. parseTopLevel(file, program) {
  5923. const fileNode = super.parseTopLevel(file, program);
  5924. if (this.state.hasFlowComment) {
  5925. this.raise(FlowErrors.UnterminatedFlowComment, {
  5926. at: this.state.curPosition()
  5927. });
  5928. }
  5929. return fileNode;
  5930. }
  5931. skipBlockComment() {
  5932. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5933. if (this.state.hasFlowComment) {
  5934. throw this.raise(FlowErrors.NestedFlowComment, {
  5935. at: this.state.startLoc
  5936. });
  5937. }
  5938. this.hasFlowCommentCompletion();
  5939. const commentSkip = this.skipFlowComment();
  5940. if (commentSkip) {
  5941. this.state.pos += commentSkip;
  5942. this.state.hasFlowComment = true;
  5943. }
  5944. return;
  5945. }
  5946. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  5947. }
  5948. skipFlowComment() {
  5949. const {
  5950. pos
  5951. } = this.state;
  5952. let shiftToFirstNonWhiteSpace = 2;
  5953. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5954. shiftToFirstNonWhiteSpace++;
  5955. }
  5956. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5957. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5958. if (ch2 === 58 && ch3 === 58) {
  5959. return shiftToFirstNonWhiteSpace + 2;
  5960. }
  5961. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5962. return shiftToFirstNonWhiteSpace + 12;
  5963. }
  5964. if (ch2 === 58 && ch3 !== 58) {
  5965. return shiftToFirstNonWhiteSpace;
  5966. }
  5967. return false;
  5968. }
  5969. hasFlowCommentCompletion() {
  5970. const end = this.input.indexOf("*/", this.state.pos);
  5971. if (end === -1) {
  5972. throw this.raise(Errors.UnterminatedComment, {
  5973. at: this.state.curPosition()
  5974. });
  5975. }
  5976. }
  5977. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5978. enumName,
  5979. memberName
  5980. }) {
  5981. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5982. at: loc,
  5983. memberName,
  5984. enumName
  5985. });
  5986. }
  5987. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5988. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  5989. at: loc
  5990. }, enumContext));
  5991. }
  5992. flowEnumErrorNumberMemberNotInitialized(loc, {
  5993. enumName,
  5994. memberName
  5995. }) {
  5996. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  5997. at: loc,
  5998. enumName,
  5999. memberName
  6000. });
  6001. }
  6002. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  6003. enumName
  6004. }) {
  6005. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  6006. at: node,
  6007. enumName
  6008. });
  6009. }
  6010. flowEnumMemberInit() {
  6011. const startLoc = this.state.startLoc;
  6012. const endOfInit = () => this.match(12) || this.match(8);
  6013. switch (this.state.type) {
  6014. case 132:
  6015. {
  6016. const literal = this.parseNumericLiteral(this.state.value);
  6017. if (endOfInit()) {
  6018. return {
  6019. type: "number",
  6020. loc: literal.loc.start,
  6021. value: literal
  6022. };
  6023. }
  6024. return {
  6025. type: "invalid",
  6026. loc: startLoc
  6027. };
  6028. }
  6029. case 131:
  6030. {
  6031. const literal = this.parseStringLiteral(this.state.value);
  6032. if (endOfInit()) {
  6033. return {
  6034. type: "string",
  6035. loc: literal.loc.start,
  6036. value: literal
  6037. };
  6038. }
  6039. return {
  6040. type: "invalid",
  6041. loc: startLoc
  6042. };
  6043. }
  6044. case 85:
  6045. case 86:
  6046. {
  6047. const literal = this.parseBooleanLiteral(this.match(85));
  6048. if (endOfInit()) {
  6049. return {
  6050. type: "boolean",
  6051. loc: literal.loc.start,
  6052. value: literal
  6053. };
  6054. }
  6055. return {
  6056. type: "invalid",
  6057. loc: startLoc
  6058. };
  6059. }
  6060. default:
  6061. return {
  6062. type: "invalid",
  6063. loc: startLoc
  6064. };
  6065. }
  6066. }
  6067. flowEnumMemberRaw() {
  6068. const loc = this.state.startLoc;
  6069. const id = this.parseIdentifier(true);
  6070. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  6071. type: "none",
  6072. loc
  6073. };
  6074. return {
  6075. id,
  6076. init
  6077. };
  6078. }
  6079. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  6080. const {
  6081. explicitType
  6082. } = context;
  6083. if (explicitType === null) {
  6084. return;
  6085. }
  6086. if (explicitType !== expectedType) {
  6087. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  6088. }
  6089. }
  6090. flowEnumMembers({
  6091. enumName,
  6092. explicitType
  6093. }) {
  6094. const seenNames = new Set();
  6095. const members = {
  6096. booleanMembers: [],
  6097. numberMembers: [],
  6098. stringMembers: [],
  6099. defaultedMembers: []
  6100. };
  6101. let hasUnknownMembers = false;
  6102. while (!this.match(8)) {
  6103. if (this.eat(21)) {
  6104. hasUnknownMembers = true;
  6105. break;
  6106. }
  6107. const memberNode = this.startNode();
  6108. const {
  6109. id,
  6110. init
  6111. } = this.flowEnumMemberRaw();
  6112. const memberName = id.name;
  6113. if (memberName === "") {
  6114. continue;
  6115. }
  6116. if (/^[a-z]/.test(memberName)) {
  6117. this.raise(FlowErrors.EnumInvalidMemberName, {
  6118. at: id,
  6119. memberName,
  6120. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  6121. enumName
  6122. });
  6123. }
  6124. if (seenNames.has(memberName)) {
  6125. this.raise(FlowErrors.EnumDuplicateMemberName, {
  6126. at: id,
  6127. memberName,
  6128. enumName
  6129. });
  6130. }
  6131. seenNames.add(memberName);
  6132. const context = {
  6133. enumName,
  6134. explicitType,
  6135. memberName
  6136. };
  6137. memberNode.id = id;
  6138. switch (init.type) {
  6139. case "boolean":
  6140. {
  6141. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6142. memberNode.init = init.value;
  6143. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6144. break;
  6145. }
  6146. case "number":
  6147. {
  6148. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6149. memberNode.init = init.value;
  6150. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6151. break;
  6152. }
  6153. case "string":
  6154. {
  6155. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6156. memberNode.init = init.value;
  6157. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6158. break;
  6159. }
  6160. case "invalid":
  6161. {
  6162. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6163. }
  6164. case "none":
  6165. {
  6166. switch (explicitType) {
  6167. case "boolean":
  6168. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6169. break;
  6170. case "number":
  6171. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6172. break;
  6173. default:
  6174. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6175. }
  6176. }
  6177. }
  6178. if (!this.match(8)) {
  6179. this.expect(12);
  6180. }
  6181. }
  6182. return {
  6183. members,
  6184. hasUnknownMembers
  6185. };
  6186. }
  6187. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6188. enumName
  6189. }) {
  6190. if (initializedMembers.length === 0) {
  6191. return defaultedMembers;
  6192. } else if (defaultedMembers.length === 0) {
  6193. return initializedMembers;
  6194. } else if (defaultedMembers.length > initializedMembers.length) {
  6195. for (const member of initializedMembers) {
  6196. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6197. enumName
  6198. });
  6199. }
  6200. return defaultedMembers;
  6201. } else {
  6202. for (const member of defaultedMembers) {
  6203. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6204. enumName
  6205. });
  6206. }
  6207. return initializedMembers;
  6208. }
  6209. }
  6210. flowEnumParseExplicitType({
  6211. enumName
  6212. }) {
  6213. if (!this.eatContextual(101)) return null;
  6214. if (!tokenIsIdentifier(this.state.type)) {
  6215. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6216. at: this.state.startLoc,
  6217. enumName
  6218. });
  6219. }
  6220. const {
  6221. value
  6222. } = this.state;
  6223. this.next();
  6224. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6225. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6226. at: this.state.startLoc,
  6227. enumName,
  6228. invalidEnumType: value
  6229. });
  6230. }
  6231. return value;
  6232. }
  6233. flowEnumBody(node, id) {
  6234. const enumName = id.name;
  6235. const nameLoc = id.loc.start;
  6236. const explicitType = this.flowEnumParseExplicitType({
  6237. enumName
  6238. });
  6239. this.expect(5);
  6240. const {
  6241. members,
  6242. hasUnknownMembers
  6243. } = this.flowEnumMembers({
  6244. enumName,
  6245. explicitType
  6246. });
  6247. node.hasUnknownMembers = hasUnknownMembers;
  6248. switch (explicitType) {
  6249. case "boolean":
  6250. node.explicitType = true;
  6251. node.members = members.booleanMembers;
  6252. this.expect(8);
  6253. return this.finishNode(node, "EnumBooleanBody");
  6254. case "number":
  6255. node.explicitType = true;
  6256. node.members = members.numberMembers;
  6257. this.expect(8);
  6258. return this.finishNode(node, "EnumNumberBody");
  6259. case "string":
  6260. node.explicitType = true;
  6261. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6262. enumName
  6263. });
  6264. this.expect(8);
  6265. return this.finishNode(node, "EnumStringBody");
  6266. case "symbol":
  6267. node.members = members.defaultedMembers;
  6268. this.expect(8);
  6269. return this.finishNode(node, "EnumSymbolBody");
  6270. default:
  6271. {
  6272. const empty = () => {
  6273. node.members = [];
  6274. this.expect(8);
  6275. return this.finishNode(node, "EnumStringBody");
  6276. };
  6277. node.explicitType = false;
  6278. const boolsLen = members.booleanMembers.length;
  6279. const numsLen = members.numberMembers.length;
  6280. const strsLen = members.stringMembers.length;
  6281. const defaultedLen = members.defaultedMembers.length;
  6282. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6283. return empty();
  6284. } else if (!boolsLen && !numsLen) {
  6285. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6286. enumName
  6287. });
  6288. this.expect(8);
  6289. return this.finishNode(node, "EnumStringBody");
  6290. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6291. for (const member of members.defaultedMembers) {
  6292. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6293. enumName,
  6294. memberName: member.id.name
  6295. });
  6296. }
  6297. node.members = members.booleanMembers;
  6298. this.expect(8);
  6299. return this.finishNode(node, "EnumBooleanBody");
  6300. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6301. for (const member of members.defaultedMembers) {
  6302. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6303. enumName,
  6304. memberName: member.id.name
  6305. });
  6306. }
  6307. node.members = members.numberMembers;
  6308. this.expect(8);
  6309. return this.finishNode(node, "EnumNumberBody");
  6310. } else {
  6311. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6312. at: nameLoc,
  6313. enumName
  6314. });
  6315. return empty();
  6316. }
  6317. }
  6318. }
  6319. }
  6320. flowParseEnumDeclaration(node) {
  6321. const id = this.parseIdentifier();
  6322. node.id = id;
  6323. node.body = this.flowEnumBody(this.startNode(), id);
  6324. return this.finishNode(node, "EnumDeclaration");
  6325. }
  6326. isLookaheadToken_lt() {
  6327. const next = this.nextTokenStart();
  6328. if (this.input.charCodeAt(next) === 60) {
  6329. const afterNext = this.input.charCodeAt(next + 1);
  6330. return afterNext !== 60 && afterNext !== 61;
  6331. }
  6332. return false;
  6333. }
  6334. maybeUnwrapTypeCastExpression(node) {
  6335. return node.type === "TypeCastExpression" ? node.expression : node;
  6336. }
  6337. });
  6338. const entities = {
  6339. __proto__: null,
  6340. quot: "\u0022",
  6341. amp: "&",
  6342. apos: "\u0027",
  6343. lt: "<",
  6344. gt: ">",
  6345. nbsp: "\u00A0",
  6346. iexcl: "\u00A1",
  6347. cent: "\u00A2",
  6348. pound: "\u00A3",
  6349. curren: "\u00A4",
  6350. yen: "\u00A5",
  6351. brvbar: "\u00A6",
  6352. sect: "\u00A7",
  6353. uml: "\u00A8",
  6354. copy: "\u00A9",
  6355. ordf: "\u00AA",
  6356. laquo: "\u00AB",
  6357. not: "\u00AC",
  6358. shy: "\u00AD",
  6359. reg: "\u00AE",
  6360. macr: "\u00AF",
  6361. deg: "\u00B0",
  6362. plusmn: "\u00B1",
  6363. sup2: "\u00B2",
  6364. sup3: "\u00B3",
  6365. acute: "\u00B4",
  6366. micro: "\u00B5",
  6367. para: "\u00B6",
  6368. middot: "\u00B7",
  6369. cedil: "\u00B8",
  6370. sup1: "\u00B9",
  6371. ordm: "\u00BA",
  6372. raquo: "\u00BB",
  6373. frac14: "\u00BC",
  6374. frac12: "\u00BD",
  6375. frac34: "\u00BE",
  6376. iquest: "\u00BF",
  6377. Agrave: "\u00C0",
  6378. Aacute: "\u00C1",
  6379. Acirc: "\u00C2",
  6380. Atilde: "\u00C3",
  6381. Auml: "\u00C4",
  6382. Aring: "\u00C5",
  6383. AElig: "\u00C6",
  6384. Ccedil: "\u00C7",
  6385. Egrave: "\u00C8",
  6386. Eacute: "\u00C9",
  6387. Ecirc: "\u00CA",
  6388. Euml: "\u00CB",
  6389. Igrave: "\u00CC",
  6390. Iacute: "\u00CD",
  6391. Icirc: "\u00CE",
  6392. Iuml: "\u00CF",
  6393. ETH: "\u00D0",
  6394. Ntilde: "\u00D1",
  6395. Ograve: "\u00D2",
  6396. Oacute: "\u00D3",
  6397. Ocirc: "\u00D4",
  6398. Otilde: "\u00D5",
  6399. Ouml: "\u00D6",
  6400. times: "\u00D7",
  6401. Oslash: "\u00D8",
  6402. Ugrave: "\u00D9",
  6403. Uacute: "\u00DA",
  6404. Ucirc: "\u00DB",
  6405. Uuml: "\u00DC",
  6406. Yacute: "\u00DD",
  6407. THORN: "\u00DE",
  6408. szlig: "\u00DF",
  6409. agrave: "\u00E0",
  6410. aacute: "\u00E1",
  6411. acirc: "\u00E2",
  6412. atilde: "\u00E3",
  6413. auml: "\u00E4",
  6414. aring: "\u00E5",
  6415. aelig: "\u00E6",
  6416. ccedil: "\u00E7",
  6417. egrave: "\u00E8",
  6418. eacute: "\u00E9",
  6419. ecirc: "\u00EA",
  6420. euml: "\u00EB",
  6421. igrave: "\u00EC",
  6422. iacute: "\u00ED",
  6423. icirc: "\u00EE",
  6424. iuml: "\u00EF",
  6425. eth: "\u00F0",
  6426. ntilde: "\u00F1",
  6427. ograve: "\u00F2",
  6428. oacute: "\u00F3",
  6429. ocirc: "\u00F4",
  6430. otilde: "\u00F5",
  6431. ouml: "\u00F6",
  6432. divide: "\u00F7",
  6433. oslash: "\u00F8",
  6434. ugrave: "\u00F9",
  6435. uacute: "\u00FA",
  6436. ucirc: "\u00FB",
  6437. uuml: "\u00FC",
  6438. yacute: "\u00FD",
  6439. thorn: "\u00FE",
  6440. yuml: "\u00FF",
  6441. OElig: "\u0152",
  6442. oelig: "\u0153",
  6443. Scaron: "\u0160",
  6444. scaron: "\u0161",
  6445. Yuml: "\u0178",
  6446. fnof: "\u0192",
  6447. circ: "\u02C6",
  6448. tilde: "\u02DC",
  6449. Alpha: "\u0391",
  6450. Beta: "\u0392",
  6451. Gamma: "\u0393",
  6452. Delta: "\u0394",
  6453. Epsilon: "\u0395",
  6454. Zeta: "\u0396",
  6455. Eta: "\u0397",
  6456. Theta: "\u0398",
  6457. Iota: "\u0399",
  6458. Kappa: "\u039A",
  6459. Lambda: "\u039B",
  6460. Mu: "\u039C",
  6461. Nu: "\u039D",
  6462. Xi: "\u039E",
  6463. Omicron: "\u039F",
  6464. Pi: "\u03A0",
  6465. Rho: "\u03A1",
  6466. Sigma: "\u03A3",
  6467. Tau: "\u03A4",
  6468. Upsilon: "\u03A5",
  6469. Phi: "\u03A6",
  6470. Chi: "\u03A7",
  6471. Psi: "\u03A8",
  6472. Omega: "\u03A9",
  6473. alpha: "\u03B1",
  6474. beta: "\u03B2",
  6475. gamma: "\u03B3",
  6476. delta: "\u03B4",
  6477. epsilon: "\u03B5",
  6478. zeta: "\u03B6",
  6479. eta: "\u03B7",
  6480. theta: "\u03B8",
  6481. iota: "\u03B9",
  6482. kappa: "\u03BA",
  6483. lambda: "\u03BB",
  6484. mu: "\u03BC",
  6485. nu: "\u03BD",
  6486. xi: "\u03BE",
  6487. omicron: "\u03BF",
  6488. pi: "\u03C0",
  6489. rho: "\u03C1",
  6490. sigmaf: "\u03C2",
  6491. sigma: "\u03C3",
  6492. tau: "\u03C4",
  6493. upsilon: "\u03C5",
  6494. phi: "\u03C6",
  6495. chi: "\u03C7",
  6496. psi: "\u03C8",
  6497. omega: "\u03C9",
  6498. thetasym: "\u03D1",
  6499. upsih: "\u03D2",
  6500. piv: "\u03D6",
  6501. ensp: "\u2002",
  6502. emsp: "\u2003",
  6503. thinsp: "\u2009",
  6504. zwnj: "\u200C",
  6505. zwj: "\u200D",
  6506. lrm: "\u200E",
  6507. rlm: "\u200F",
  6508. ndash: "\u2013",
  6509. mdash: "\u2014",
  6510. lsquo: "\u2018",
  6511. rsquo: "\u2019",
  6512. sbquo: "\u201A",
  6513. ldquo: "\u201C",
  6514. rdquo: "\u201D",
  6515. bdquo: "\u201E",
  6516. dagger: "\u2020",
  6517. Dagger: "\u2021",
  6518. bull: "\u2022",
  6519. hellip: "\u2026",
  6520. permil: "\u2030",
  6521. prime: "\u2032",
  6522. Prime: "\u2033",
  6523. lsaquo: "\u2039",
  6524. rsaquo: "\u203A",
  6525. oline: "\u203E",
  6526. frasl: "\u2044",
  6527. euro: "\u20AC",
  6528. image: "\u2111",
  6529. weierp: "\u2118",
  6530. real: "\u211C",
  6531. trade: "\u2122",
  6532. alefsym: "\u2135",
  6533. larr: "\u2190",
  6534. uarr: "\u2191",
  6535. rarr: "\u2192",
  6536. darr: "\u2193",
  6537. harr: "\u2194",
  6538. crarr: "\u21B5",
  6539. lArr: "\u21D0",
  6540. uArr: "\u21D1",
  6541. rArr: "\u21D2",
  6542. dArr: "\u21D3",
  6543. hArr: "\u21D4",
  6544. forall: "\u2200",
  6545. part: "\u2202",
  6546. exist: "\u2203",
  6547. empty: "\u2205",
  6548. nabla: "\u2207",
  6549. isin: "\u2208",
  6550. notin: "\u2209",
  6551. ni: "\u220B",
  6552. prod: "\u220F",
  6553. sum: "\u2211",
  6554. minus: "\u2212",
  6555. lowast: "\u2217",
  6556. radic: "\u221A",
  6557. prop: "\u221D",
  6558. infin: "\u221E",
  6559. ang: "\u2220",
  6560. and: "\u2227",
  6561. or: "\u2228",
  6562. cap: "\u2229",
  6563. cup: "\u222A",
  6564. int: "\u222B",
  6565. there4: "\u2234",
  6566. sim: "\u223C",
  6567. cong: "\u2245",
  6568. asymp: "\u2248",
  6569. ne: "\u2260",
  6570. equiv: "\u2261",
  6571. le: "\u2264",
  6572. ge: "\u2265",
  6573. sub: "\u2282",
  6574. sup: "\u2283",
  6575. nsub: "\u2284",
  6576. sube: "\u2286",
  6577. supe: "\u2287",
  6578. oplus: "\u2295",
  6579. otimes: "\u2297",
  6580. perp: "\u22A5",
  6581. sdot: "\u22C5",
  6582. lceil: "\u2308",
  6583. rceil: "\u2309",
  6584. lfloor: "\u230A",
  6585. rfloor: "\u230B",
  6586. lang: "\u2329",
  6587. rang: "\u232A",
  6588. loz: "\u25CA",
  6589. spades: "\u2660",
  6590. clubs: "\u2663",
  6591. hearts: "\u2665",
  6592. diams: "\u2666"
  6593. };
  6594. const JsxErrors = ParseErrorEnum`jsx`({
  6595. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6596. MissingClosingTagElement: ({
  6597. openingTagName
  6598. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6599. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6600. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6601. UnexpectedToken: ({
  6602. unexpected,
  6603. HTMLEntity
  6604. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6605. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6606. UnterminatedJsxContent: "Unterminated JSX contents.",
  6607. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6608. });
  6609. function isFragment(object) {
  6610. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6611. }
  6612. function getQualifiedJSXName(object) {
  6613. if (object.type === "JSXIdentifier") {
  6614. return object.name;
  6615. }
  6616. if (object.type === "JSXNamespacedName") {
  6617. return object.namespace.name + ":" + object.name.name;
  6618. }
  6619. if (object.type === "JSXMemberExpression") {
  6620. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6621. }
  6622. throw new Error("Node had unexpected type: " + object.type);
  6623. }
  6624. var jsx = (superClass => class JSXParserMixin extends superClass {
  6625. jsxReadToken() {
  6626. let out = "";
  6627. let chunkStart = this.state.pos;
  6628. for (;;) {
  6629. if (this.state.pos >= this.length) {
  6630. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6631. at: this.state.startLoc
  6632. });
  6633. }
  6634. const ch = this.input.charCodeAt(this.state.pos);
  6635. switch (ch) {
  6636. case 60:
  6637. case 123:
  6638. if (this.state.pos === this.state.start) {
  6639. if (ch === 60 && this.state.canStartJSXElement) {
  6640. ++this.state.pos;
  6641. return this.finishToken(140);
  6642. }
  6643. return super.getTokenFromCode(ch);
  6644. }
  6645. out += this.input.slice(chunkStart, this.state.pos);
  6646. return this.finishToken(139, out);
  6647. case 38:
  6648. out += this.input.slice(chunkStart, this.state.pos);
  6649. out += this.jsxReadEntity();
  6650. chunkStart = this.state.pos;
  6651. break;
  6652. case 62:
  6653. case 125:
  6654. default:
  6655. if (isNewLine(ch)) {
  6656. out += this.input.slice(chunkStart, this.state.pos);
  6657. out += this.jsxReadNewLine(true);
  6658. chunkStart = this.state.pos;
  6659. } else {
  6660. ++this.state.pos;
  6661. }
  6662. }
  6663. }
  6664. }
  6665. jsxReadNewLine(normalizeCRLF) {
  6666. const ch = this.input.charCodeAt(this.state.pos);
  6667. let out;
  6668. ++this.state.pos;
  6669. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6670. ++this.state.pos;
  6671. out = normalizeCRLF ? "\n" : "\r\n";
  6672. } else {
  6673. out = String.fromCharCode(ch);
  6674. }
  6675. ++this.state.curLine;
  6676. this.state.lineStart = this.state.pos;
  6677. return out;
  6678. }
  6679. jsxReadString(quote) {
  6680. let out = "";
  6681. let chunkStart = ++this.state.pos;
  6682. for (;;) {
  6683. if (this.state.pos >= this.length) {
  6684. throw this.raise(Errors.UnterminatedString, {
  6685. at: this.state.startLoc
  6686. });
  6687. }
  6688. const ch = this.input.charCodeAt(this.state.pos);
  6689. if (ch === quote) break;
  6690. if (ch === 38) {
  6691. out += this.input.slice(chunkStart, this.state.pos);
  6692. out += this.jsxReadEntity();
  6693. chunkStart = this.state.pos;
  6694. } else if (isNewLine(ch)) {
  6695. out += this.input.slice(chunkStart, this.state.pos);
  6696. out += this.jsxReadNewLine(false);
  6697. chunkStart = this.state.pos;
  6698. } else {
  6699. ++this.state.pos;
  6700. }
  6701. }
  6702. out += this.input.slice(chunkStart, this.state.pos++);
  6703. return this.finishToken(131, out);
  6704. }
  6705. jsxReadEntity() {
  6706. const startPos = ++this.state.pos;
  6707. if (this.codePointAtPos(this.state.pos) === 35) {
  6708. ++this.state.pos;
  6709. let radix = 10;
  6710. if (this.codePointAtPos(this.state.pos) === 120) {
  6711. radix = 16;
  6712. ++this.state.pos;
  6713. }
  6714. const codePoint = this.readInt(radix, undefined, false, "bail");
  6715. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6716. ++this.state.pos;
  6717. return String.fromCodePoint(codePoint);
  6718. }
  6719. } else {
  6720. let count = 0;
  6721. let semi = false;
  6722. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6723. ++this.state.pos;
  6724. }
  6725. if (semi) {
  6726. const desc = this.input.slice(startPos, this.state.pos);
  6727. const entity = entities[desc];
  6728. ++this.state.pos;
  6729. if (entity) {
  6730. return entity;
  6731. }
  6732. }
  6733. }
  6734. this.state.pos = startPos;
  6735. return "&";
  6736. }
  6737. jsxReadWord() {
  6738. let ch;
  6739. const start = this.state.pos;
  6740. do {
  6741. ch = this.input.charCodeAt(++this.state.pos);
  6742. } while (isIdentifierChar(ch) || ch === 45);
  6743. return this.finishToken(138, this.input.slice(start, this.state.pos));
  6744. }
  6745. jsxParseIdentifier() {
  6746. const node = this.startNode();
  6747. if (this.match(138)) {
  6748. node.name = this.state.value;
  6749. } else if (tokenIsKeyword(this.state.type)) {
  6750. node.name = tokenLabelName(this.state.type);
  6751. } else {
  6752. this.unexpected();
  6753. }
  6754. this.next();
  6755. return this.finishNode(node, "JSXIdentifier");
  6756. }
  6757. jsxParseNamespacedName() {
  6758. const startLoc = this.state.startLoc;
  6759. const name = this.jsxParseIdentifier();
  6760. if (!this.eat(14)) return name;
  6761. const node = this.startNodeAt(startLoc);
  6762. node.namespace = name;
  6763. node.name = this.jsxParseIdentifier();
  6764. return this.finishNode(node, "JSXNamespacedName");
  6765. }
  6766. jsxParseElementName() {
  6767. const startLoc = this.state.startLoc;
  6768. let node = this.jsxParseNamespacedName();
  6769. if (node.type === "JSXNamespacedName") {
  6770. return node;
  6771. }
  6772. while (this.eat(16)) {
  6773. const newNode = this.startNodeAt(startLoc);
  6774. newNode.object = node;
  6775. newNode.property = this.jsxParseIdentifier();
  6776. node = this.finishNode(newNode, "JSXMemberExpression");
  6777. }
  6778. return node;
  6779. }
  6780. jsxParseAttributeValue() {
  6781. let node;
  6782. switch (this.state.type) {
  6783. case 5:
  6784. node = this.startNode();
  6785. this.setContext(types.brace);
  6786. this.next();
  6787. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6788. if (node.expression.type === "JSXEmptyExpression") {
  6789. this.raise(JsxErrors.AttributeIsEmpty, {
  6790. at: node
  6791. });
  6792. }
  6793. return node;
  6794. case 140:
  6795. case 131:
  6796. return this.parseExprAtom();
  6797. default:
  6798. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6799. at: this.state.startLoc
  6800. });
  6801. }
  6802. }
  6803. jsxParseEmptyExpression() {
  6804. const node = this.startNodeAt(this.state.lastTokEndLoc);
  6805. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6806. }
  6807. jsxParseSpreadChild(node) {
  6808. this.next();
  6809. node.expression = this.parseExpression();
  6810. this.setContext(types.j_expr);
  6811. this.state.canStartJSXElement = true;
  6812. this.expect(8);
  6813. return this.finishNode(node, "JSXSpreadChild");
  6814. }
  6815. jsxParseExpressionContainer(node, previousContext) {
  6816. if (this.match(8)) {
  6817. node.expression = this.jsxParseEmptyExpression();
  6818. } else {
  6819. const expression = this.parseExpression();
  6820. node.expression = expression;
  6821. }
  6822. this.setContext(previousContext);
  6823. this.state.canStartJSXElement = true;
  6824. this.expect(8);
  6825. return this.finishNode(node, "JSXExpressionContainer");
  6826. }
  6827. jsxParseAttribute() {
  6828. const node = this.startNode();
  6829. if (this.match(5)) {
  6830. this.setContext(types.brace);
  6831. this.next();
  6832. this.expect(21);
  6833. node.argument = this.parseMaybeAssignAllowIn();
  6834. this.setContext(types.j_oTag);
  6835. this.state.canStartJSXElement = true;
  6836. this.expect(8);
  6837. return this.finishNode(node, "JSXSpreadAttribute");
  6838. }
  6839. node.name = this.jsxParseNamespacedName();
  6840. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6841. return this.finishNode(node, "JSXAttribute");
  6842. }
  6843. jsxParseOpeningElementAt(startLoc) {
  6844. const node = this.startNodeAt(startLoc);
  6845. if (this.eat(141)) {
  6846. return this.finishNode(node, "JSXOpeningFragment");
  6847. }
  6848. node.name = this.jsxParseElementName();
  6849. return this.jsxParseOpeningElementAfterName(node);
  6850. }
  6851. jsxParseOpeningElementAfterName(node) {
  6852. const attributes = [];
  6853. while (!this.match(56) && !this.match(141)) {
  6854. attributes.push(this.jsxParseAttribute());
  6855. }
  6856. node.attributes = attributes;
  6857. node.selfClosing = this.eat(56);
  6858. this.expect(141);
  6859. return this.finishNode(node, "JSXOpeningElement");
  6860. }
  6861. jsxParseClosingElementAt(startLoc) {
  6862. const node = this.startNodeAt(startLoc);
  6863. if (this.eat(141)) {
  6864. return this.finishNode(node, "JSXClosingFragment");
  6865. }
  6866. node.name = this.jsxParseElementName();
  6867. this.expect(141);
  6868. return this.finishNode(node, "JSXClosingElement");
  6869. }
  6870. jsxParseElementAt(startLoc) {
  6871. const node = this.startNodeAt(startLoc);
  6872. const children = [];
  6873. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  6874. let closingElement = null;
  6875. if (!openingElement.selfClosing) {
  6876. contents: for (;;) {
  6877. switch (this.state.type) {
  6878. case 140:
  6879. startLoc = this.state.startLoc;
  6880. this.next();
  6881. if (this.eat(56)) {
  6882. closingElement = this.jsxParseClosingElementAt(startLoc);
  6883. break contents;
  6884. }
  6885. children.push(this.jsxParseElementAt(startLoc));
  6886. break;
  6887. case 139:
  6888. children.push(this.parseExprAtom());
  6889. break;
  6890. case 5:
  6891. {
  6892. const node = this.startNode();
  6893. this.setContext(types.brace);
  6894. this.next();
  6895. if (this.match(21)) {
  6896. children.push(this.jsxParseSpreadChild(node));
  6897. } else {
  6898. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6899. }
  6900. break;
  6901. }
  6902. default:
  6903. throw this.unexpected();
  6904. }
  6905. }
  6906. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6907. this.raise(JsxErrors.MissingClosingTagFragment, {
  6908. at: closingElement
  6909. });
  6910. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6911. this.raise(JsxErrors.MissingClosingTagElement, {
  6912. at: closingElement,
  6913. openingTagName: getQualifiedJSXName(openingElement.name)
  6914. });
  6915. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6916. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6917. this.raise(JsxErrors.MissingClosingTagElement, {
  6918. at: closingElement,
  6919. openingTagName: getQualifiedJSXName(openingElement.name)
  6920. });
  6921. }
  6922. }
  6923. }
  6924. if (isFragment(openingElement)) {
  6925. node.openingFragment = openingElement;
  6926. node.closingFragment = closingElement;
  6927. } else {
  6928. node.openingElement = openingElement;
  6929. node.closingElement = closingElement;
  6930. }
  6931. node.children = children;
  6932. if (this.match(47)) {
  6933. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6934. at: this.state.startLoc
  6935. });
  6936. }
  6937. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6938. }
  6939. jsxParseElement() {
  6940. const startLoc = this.state.startLoc;
  6941. this.next();
  6942. return this.jsxParseElementAt(startLoc);
  6943. }
  6944. setContext(newContext) {
  6945. const {
  6946. context
  6947. } = this.state;
  6948. context[context.length - 1] = newContext;
  6949. }
  6950. parseExprAtom(refExpressionErrors) {
  6951. if (this.match(139)) {
  6952. return this.parseLiteral(this.state.value, "JSXText");
  6953. } else if (this.match(140)) {
  6954. return this.jsxParseElement();
  6955. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6956. this.replaceToken(140);
  6957. return this.jsxParseElement();
  6958. } else {
  6959. return super.parseExprAtom(refExpressionErrors);
  6960. }
  6961. }
  6962. skipSpace() {
  6963. const curContext = this.curContext();
  6964. if (!curContext.preserveSpace) super.skipSpace();
  6965. }
  6966. getTokenFromCode(code) {
  6967. const context = this.curContext();
  6968. if (context === types.j_expr) {
  6969. return this.jsxReadToken();
  6970. }
  6971. if (context === types.j_oTag || context === types.j_cTag) {
  6972. if (isIdentifierStart(code)) {
  6973. return this.jsxReadWord();
  6974. }
  6975. if (code === 62) {
  6976. ++this.state.pos;
  6977. return this.finishToken(141);
  6978. }
  6979. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6980. return this.jsxReadString(code);
  6981. }
  6982. }
  6983. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6984. ++this.state.pos;
  6985. return this.finishToken(140);
  6986. }
  6987. return super.getTokenFromCode(code);
  6988. }
  6989. updateContext(prevType) {
  6990. const {
  6991. context,
  6992. type
  6993. } = this.state;
  6994. if (type === 56 && prevType === 140) {
  6995. context.splice(-2, 2, types.j_cTag);
  6996. this.state.canStartJSXElement = false;
  6997. } else if (type === 140) {
  6998. context.push(types.j_oTag);
  6999. } else if (type === 141) {
  7000. const out = context[context.length - 1];
  7001. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  7002. context.pop();
  7003. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  7004. } else {
  7005. this.setContext(types.j_expr);
  7006. this.state.canStartJSXElement = true;
  7007. }
  7008. } else {
  7009. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  7010. }
  7011. }
  7012. });
  7013. class TypeScriptScope extends Scope {
  7014. constructor(...args) {
  7015. super(...args);
  7016. this.types = new Set();
  7017. this.enums = new Set();
  7018. this.constEnums = new Set();
  7019. this.classes = new Set();
  7020. this.exportOnlyBindings = new Set();
  7021. }
  7022. }
  7023. class TypeScriptScopeHandler extends ScopeHandler {
  7024. constructor(...args) {
  7025. super(...args);
  7026. this.importsStack = [];
  7027. }
  7028. createScope(flags) {
  7029. this.importsStack.push(new Set());
  7030. return new TypeScriptScope(flags);
  7031. }
  7032. enter(flags) {
  7033. if (flags == SCOPE_TS_MODULE) {
  7034. this.importsStack.push(new Set());
  7035. }
  7036. super.enter(flags);
  7037. }
  7038. exit() {
  7039. const flags = super.exit();
  7040. if (flags == SCOPE_TS_MODULE) {
  7041. this.importsStack.pop();
  7042. }
  7043. return flags;
  7044. }
  7045. hasImport(name, allowShadow) {
  7046. const len = this.importsStack.length;
  7047. if (this.importsStack[len - 1].has(name)) {
  7048. return true;
  7049. }
  7050. if (!allowShadow && len > 1) {
  7051. for (let i = 0; i < len - 1; i++) {
  7052. if (this.importsStack[i].has(name)) return true;
  7053. }
  7054. }
  7055. return false;
  7056. }
  7057. declareName(name, bindingType, loc) {
  7058. if (bindingType & BIND_FLAGS_TS_IMPORT) {
  7059. if (this.hasImport(name, true)) {
  7060. this.parser.raise(Errors.VarRedeclaration, {
  7061. at: loc,
  7062. identifierName: name
  7063. });
  7064. }
  7065. this.importsStack[this.importsStack.length - 1].add(name);
  7066. return;
  7067. }
  7068. const scope = this.currentScope();
  7069. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  7070. this.maybeExportDefined(scope, name);
  7071. scope.exportOnlyBindings.add(name);
  7072. return;
  7073. }
  7074. super.declareName(name, bindingType, loc);
  7075. if (bindingType & BIND_KIND_TYPE) {
  7076. if (!(bindingType & BIND_KIND_VALUE)) {
  7077. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  7078. this.maybeExportDefined(scope, name);
  7079. }
  7080. scope.types.add(name);
  7081. }
  7082. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  7083. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  7084. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  7085. }
  7086. isRedeclaredInScope(scope, name, bindingType) {
  7087. if (scope.enums.has(name)) {
  7088. if (bindingType & BIND_FLAGS_TS_ENUM) {
  7089. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  7090. const wasConst = scope.constEnums.has(name);
  7091. return isConst !== wasConst;
  7092. }
  7093. return true;
  7094. }
  7095. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  7096. if (scope.lexical.has(name)) {
  7097. return !!(bindingType & BIND_KIND_VALUE);
  7098. } else {
  7099. return false;
  7100. }
  7101. }
  7102. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  7103. return true;
  7104. }
  7105. return super.isRedeclaredInScope(scope, name, bindingType);
  7106. }
  7107. checkLocalExport(id) {
  7108. const {
  7109. name
  7110. } = id;
  7111. if (this.hasImport(name)) return;
  7112. const len = this.scopeStack.length;
  7113. for (let i = len - 1; i >= 0; i--) {
  7114. const scope = this.scopeStack[i];
  7115. if (scope.types.has(name) || scope.exportOnlyBindings.has(name)) return;
  7116. }
  7117. super.checkLocalExport(id);
  7118. }
  7119. }
  7120. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7121. const unwrapParenthesizedExpression = node => {
  7122. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  7123. };
  7124. var ParseBindingListFlags = {
  7125. ALLOW_EMPTY: 1,
  7126. IS_FUNCTION_PARAMS: 2,
  7127. IS_CONSTRUCTOR_PARAMS: 4
  7128. };
  7129. class LValParser extends NodeUtils {
  7130. toAssignable(node, isLHS = false) {
  7131. var _node$extra, _node$extra3;
  7132. let parenthesized = undefined;
  7133. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  7134. parenthesized = unwrapParenthesizedExpression(node);
  7135. if (isLHS) {
  7136. if (parenthesized.type === "Identifier") {
  7137. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, {
  7138. at: node
  7139. });
  7140. } else if (parenthesized.type !== "MemberExpression") {
  7141. this.raise(Errors.InvalidParenthesizedAssignment, {
  7142. at: node
  7143. });
  7144. }
  7145. } else {
  7146. this.raise(Errors.InvalidParenthesizedAssignment, {
  7147. at: node
  7148. });
  7149. }
  7150. }
  7151. switch (node.type) {
  7152. case "Identifier":
  7153. case "ObjectPattern":
  7154. case "ArrayPattern":
  7155. case "AssignmentPattern":
  7156. case "RestElement":
  7157. break;
  7158. case "ObjectExpression":
  7159. node.type = "ObjectPattern";
  7160. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  7161. var _node$extra2;
  7162. const prop = node.properties[i];
  7163. const isLast = i === last;
  7164. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  7165. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  7166. this.raise(Errors.RestTrailingComma, {
  7167. at: node.extra.trailingCommaLoc
  7168. });
  7169. }
  7170. }
  7171. break;
  7172. case "ObjectProperty":
  7173. {
  7174. const {
  7175. key,
  7176. value
  7177. } = node;
  7178. if (this.isPrivateName(key)) {
  7179. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7180. }
  7181. this.toAssignable(value, isLHS);
  7182. break;
  7183. }
  7184. case "SpreadElement":
  7185. {
  7186. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7187. }
  7188. case "ArrayExpression":
  7189. node.type = "ArrayPattern";
  7190. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7191. break;
  7192. case "AssignmentExpression":
  7193. if (node.operator !== "=") {
  7194. this.raise(Errors.MissingEqInAssignment, {
  7195. at: node.left.loc.end
  7196. });
  7197. }
  7198. node.type = "AssignmentPattern";
  7199. delete node.operator;
  7200. this.toAssignable(node.left, isLHS);
  7201. break;
  7202. case "ParenthesizedExpression":
  7203. this.toAssignable(parenthesized, isLHS);
  7204. break;
  7205. }
  7206. }
  7207. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7208. if (prop.type === "ObjectMethod") {
  7209. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  7210. at: prop.key
  7211. });
  7212. } else if (prop.type === "SpreadElement") {
  7213. prop.type = "RestElement";
  7214. const arg = prop.argument;
  7215. this.checkToRestConversion(arg, false);
  7216. this.toAssignable(arg, isLHS);
  7217. if (!isLast) {
  7218. this.raise(Errors.RestTrailingComma, {
  7219. at: prop
  7220. });
  7221. }
  7222. } else {
  7223. this.toAssignable(prop, isLHS);
  7224. }
  7225. }
  7226. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7227. const end = exprList.length - 1;
  7228. for (let i = 0; i <= end; i++) {
  7229. const elt = exprList[i];
  7230. if (!elt) continue;
  7231. if (elt.type === "SpreadElement") {
  7232. elt.type = "RestElement";
  7233. const arg = elt.argument;
  7234. this.checkToRestConversion(arg, true);
  7235. this.toAssignable(arg, isLHS);
  7236. } else {
  7237. this.toAssignable(elt, isLHS);
  7238. }
  7239. if (elt.type === "RestElement") {
  7240. if (i < end) {
  7241. this.raise(Errors.RestTrailingComma, {
  7242. at: elt
  7243. });
  7244. } else if (trailingCommaLoc) {
  7245. this.raise(Errors.RestTrailingComma, {
  7246. at: trailingCommaLoc
  7247. });
  7248. }
  7249. }
  7250. }
  7251. }
  7252. isAssignable(node, isBinding) {
  7253. switch (node.type) {
  7254. case "Identifier":
  7255. case "ObjectPattern":
  7256. case "ArrayPattern":
  7257. case "AssignmentPattern":
  7258. case "RestElement":
  7259. return true;
  7260. case "ObjectExpression":
  7261. {
  7262. const last = node.properties.length - 1;
  7263. return node.properties.every((prop, i) => {
  7264. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7265. });
  7266. }
  7267. case "ObjectProperty":
  7268. return this.isAssignable(node.value);
  7269. case "SpreadElement":
  7270. return this.isAssignable(node.argument);
  7271. case "ArrayExpression":
  7272. return node.elements.every(element => element === null || this.isAssignable(element));
  7273. case "AssignmentExpression":
  7274. return node.operator === "=";
  7275. case "ParenthesizedExpression":
  7276. return this.isAssignable(node.expression);
  7277. case "MemberExpression":
  7278. case "OptionalMemberExpression":
  7279. return !isBinding;
  7280. default:
  7281. return false;
  7282. }
  7283. }
  7284. toReferencedList(exprList, isParenthesizedExpr) {
  7285. return exprList;
  7286. }
  7287. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7288. this.toReferencedList(exprList, isParenthesizedExpr);
  7289. for (const expr of exprList) {
  7290. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7291. this.toReferencedListDeep(expr.elements);
  7292. }
  7293. }
  7294. }
  7295. parseSpread(refExpressionErrors) {
  7296. const node = this.startNode();
  7297. this.next();
  7298. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7299. return this.finishNode(node, "SpreadElement");
  7300. }
  7301. parseRestBinding() {
  7302. const node = this.startNode();
  7303. this.next();
  7304. node.argument = this.parseBindingAtom();
  7305. return this.finishNode(node, "RestElement");
  7306. }
  7307. parseBindingAtom() {
  7308. switch (this.state.type) {
  7309. case 0:
  7310. {
  7311. const node = this.startNode();
  7312. this.next();
  7313. node.elements = this.parseBindingList(3, 93, ParseBindingListFlags.ALLOW_EMPTY);
  7314. return this.finishNode(node, "ArrayPattern");
  7315. }
  7316. case 5:
  7317. return this.parseObjectLike(8, true);
  7318. }
  7319. return this.parseIdentifier();
  7320. }
  7321. parseBindingList(close, closeCharCode, flags) {
  7322. const allowEmpty = flags & ParseBindingListFlags.ALLOW_EMPTY;
  7323. const elts = [];
  7324. let first = true;
  7325. while (!this.eat(close)) {
  7326. if (first) {
  7327. first = false;
  7328. } else {
  7329. this.expect(12);
  7330. }
  7331. if (allowEmpty && this.match(12)) {
  7332. elts.push(null);
  7333. } else if (this.eat(close)) {
  7334. break;
  7335. } else if (this.match(21)) {
  7336. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding(), flags));
  7337. if (!this.checkCommaAfterRest(closeCharCode)) {
  7338. this.expect(close);
  7339. break;
  7340. }
  7341. } else {
  7342. const decorators = [];
  7343. if (this.match(26) && this.hasPlugin("decorators")) {
  7344. this.raise(Errors.UnsupportedParameterDecorator, {
  7345. at: this.state.startLoc
  7346. });
  7347. }
  7348. while (this.match(26)) {
  7349. decorators.push(this.parseDecorator());
  7350. }
  7351. elts.push(this.parseAssignableListItem(flags, decorators));
  7352. }
  7353. }
  7354. return elts;
  7355. }
  7356. parseBindingRestProperty(prop) {
  7357. this.next();
  7358. prop.argument = this.parseIdentifier();
  7359. this.checkCommaAfterRest(125);
  7360. return this.finishNode(prop, "RestElement");
  7361. }
  7362. parseBindingProperty() {
  7363. const prop = this.startNode();
  7364. const {
  7365. type,
  7366. startLoc
  7367. } = this.state;
  7368. if (type === 21) {
  7369. return this.parseBindingRestProperty(prop);
  7370. } else if (type === 136) {
  7371. this.expectPlugin("destructuringPrivate", startLoc);
  7372. this.classScope.usePrivateName(this.state.value, startLoc);
  7373. prop.key = this.parsePrivateName();
  7374. } else {
  7375. this.parsePropertyName(prop);
  7376. }
  7377. prop.method = false;
  7378. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7379. }
  7380. parseAssignableListItem(flags, decorators) {
  7381. const left = this.parseMaybeDefault();
  7382. this.parseAssignableListItemTypes(left, flags);
  7383. const elt = this.parseMaybeDefault(left.loc.start, left);
  7384. if (decorators.length) {
  7385. left.decorators = decorators;
  7386. }
  7387. return elt;
  7388. }
  7389. parseAssignableListItemTypes(param, flags) {
  7390. return param;
  7391. }
  7392. parseMaybeDefault(startLoc, left) {
  7393. var _startLoc, _left;
  7394. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  7395. left = (_left = left) != null ? _left : this.parseBindingAtom();
  7396. if (!this.eat(29)) return left;
  7397. const node = this.startNodeAt(startLoc);
  7398. node.left = left;
  7399. node.right = this.parseMaybeAssignAllowIn();
  7400. return this.finishNode(node, "AssignmentPattern");
  7401. }
  7402. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  7403. return getOwn$1({
  7404. AssignmentPattern: "left",
  7405. RestElement: "argument",
  7406. ObjectProperty: "value",
  7407. ParenthesizedExpression: "expression",
  7408. ArrayPattern: "elements",
  7409. ObjectPattern: "properties"
  7410. }, type);
  7411. }
  7412. checkLVal(expression, {
  7413. in: ancestor,
  7414. binding = BIND_NONE,
  7415. checkClashes = false,
  7416. strictModeChanged = false,
  7417. hasParenthesizedAncestor = false
  7418. }) {
  7419. var _expression$extra;
  7420. const type = expression.type;
  7421. if (this.isObjectMethod(expression)) return;
  7422. if (type === "MemberExpression") {
  7423. if (binding !== BIND_NONE) {
  7424. this.raise(Errors.InvalidPropertyBindingPattern, {
  7425. at: expression
  7426. });
  7427. }
  7428. return;
  7429. }
  7430. if (type === "Identifier") {
  7431. this.checkIdentifier(expression, binding, strictModeChanged);
  7432. const {
  7433. name
  7434. } = expression;
  7435. if (checkClashes) {
  7436. if (checkClashes.has(name)) {
  7437. this.raise(Errors.ParamDupe, {
  7438. at: expression
  7439. });
  7440. } else {
  7441. checkClashes.add(name);
  7442. }
  7443. }
  7444. return;
  7445. }
  7446. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7447. if (validity === true) return;
  7448. if (validity === false) {
  7449. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7450. this.raise(ParseErrorClass, {
  7451. at: expression,
  7452. ancestor
  7453. });
  7454. return;
  7455. }
  7456. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  7457. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" || type === "ParenthesizedExpression" ? {
  7458. type
  7459. } : ancestor;
  7460. for (const child of [].concat(expression[key])) {
  7461. if (child) {
  7462. this.checkLVal(child, {
  7463. in: nextAncestor,
  7464. binding,
  7465. checkClashes,
  7466. strictModeChanged,
  7467. hasParenthesizedAncestor: isParenthesizedExpression
  7468. });
  7469. }
  7470. }
  7471. }
  7472. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7473. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7474. if (bindingType === BIND_NONE) {
  7475. this.raise(Errors.StrictEvalArguments, {
  7476. at,
  7477. referenceName: at.name
  7478. });
  7479. } else {
  7480. this.raise(Errors.StrictEvalArgumentsBinding, {
  7481. at,
  7482. bindingName: at.name
  7483. });
  7484. }
  7485. }
  7486. if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
  7487. this.raise(Errors.LetInLexicalBinding, {
  7488. at
  7489. });
  7490. }
  7491. if (!(bindingType & BIND_NONE)) {
  7492. this.declareNameFromIdentifier(at, bindingType);
  7493. }
  7494. }
  7495. declareNameFromIdentifier(identifier, binding) {
  7496. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7497. }
  7498. checkToRestConversion(node, allowPattern) {
  7499. switch (node.type) {
  7500. case "ParenthesizedExpression":
  7501. this.checkToRestConversion(node.expression, allowPattern);
  7502. break;
  7503. case "Identifier":
  7504. case "MemberExpression":
  7505. break;
  7506. case "ArrayExpression":
  7507. case "ObjectExpression":
  7508. if (allowPattern) break;
  7509. default:
  7510. this.raise(Errors.InvalidRestAssignmentPattern, {
  7511. at: node
  7512. });
  7513. }
  7514. }
  7515. checkCommaAfterRest(close) {
  7516. if (!this.match(12)) {
  7517. return false;
  7518. }
  7519. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  7520. at: this.state.startLoc
  7521. });
  7522. return true;
  7523. }
  7524. }
  7525. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7526. function nonNull(x) {
  7527. if (x == null) {
  7528. throw new Error(`Unexpected ${x} value.`);
  7529. }
  7530. return x;
  7531. }
  7532. function assert(x) {
  7533. if (!x) {
  7534. throw new Error("Assert fail");
  7535. }
  7536. }
  7537. const TSErrors = ParseErrorEnum`typescript`({
  7538. AbstractMethodHasImplementation: ({
  7539. methodName
  7540. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7541. AbstractPropertyHasInitializer: ({
  7542. propertyName
  7543. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7544. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7545. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7546. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7547. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7548. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7549. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7550. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7551. DeclareAccessor: ({
  7552. kind
  7553. }) => `'declare' is not allowed in ${kind}ters.`,
  7554. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7555. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7556. DuplicateAccessibilityModifier: ({
  7557. modifier
  7558. }) => `Accessibility modifier already seen.`,
  7559. DuplicateModifier: ({
  7560. modifier
  7561. }) => `Duplicate modifier: '${modifier}'.`,
  7562. EmptyHeritageClauseType: ({
  7563. token
  7564. }) => `'${token}' list cannot be empty.`,
  7565. EmptyTypeArguments: "Type argument list cannot be empty.",
  7566. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7567. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7568. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7569. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7570. IncompatibleModifiers: ({
  7571. modifiers
  7572. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7573. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7574. IndexSignatureHasAccessibility: ({
  7575. modifier
  7576. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7577. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7578. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7579. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7580. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7581. InvalidModifierOnTypeMember: ({
  7582. modifier
  7583. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7584. InvalidModifierOnTypeParameter: ({
  7585. modifier
  7586. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7587. InvalidModifierOnTypeParameterPositions: ({
  7588. modifier
  7589. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7590. InvalidModifiersOrder: ({
  7591. orderedModifiers
  7592. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7593. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7594. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7595. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7596. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  7597. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7598. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7599. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7600. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7601. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7602. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7603. PrivateElementHasAccessibility: ({
  7604. modifier
  7605. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7606. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7607. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7608. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7609. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7610. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7611. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7612. SingleTypeParameterWithoutTrailingComma: ({
  7613. typeParameterName
  7614. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7615. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7616. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7617. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7618. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7619. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7620. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7621. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7622. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7623. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7624. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7625. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7626. UnsupportedSignatureParameterKind: ({
  7627. type
  7628. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7629. });
  7630. function keywordTypeFromName(value) {
  7631. switch (value) {
  7632. case "any":
  7633. return "TSAnyKeyword";
  7634. case "boolean":
  7635. return "TSBooleanKeyword";
  7636. case "bigint":
  7637. return "TSBigIntKeyword";
  7638. case "never":
  7639. return "TSNeverKeyword";
  7640. case "number":
  7641. return "TSNumberKeyword";
  7642. case "object":
  7643. return "TSObjectKeyword";
  7644. case "string":
  7645. return "TSStringKeyword";
  7646. case "symbol":
  7647. return "TSSymbolKeyword";
  7648. case "undefined":
  7649. return "TSUndefinedKeyword";
  7650. case "unknown":
  7651. return "TSUnknownKeyword";
  7652. default:
  7653. return undefined;
  7654. }
  7655. }
  7656. function tsIsAccessModifier(modifier) {
  7657. return modifier === "private" || modifier === "public" || modifier === "protected";
  7658. }
  7659. function tsIsVarianceAnnotations(modifier) {
  7660. return modifier === "in" || modifier === "out";
  7661. }
  7662. var typescript = (superClass => class TypeScriptParserMixin extends superClass {
  7663. constructor(...args) {
  7664. super(...args);
  7665. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7666. allowedModifiers: ["in", "out"],
  7667. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7668. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7669. });
  7670. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7671. allowedModifiers: ["const"],
  7672. disallowedModifiers: ["in", "out"],
  7673. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7674. });
  7675. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7676. allowedModifiers: ["in", "out", "const"],
  7677. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7678. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7679. });
  7680. }
  7681. getScopeHandler() {
  7682. return TypeScriptScopeHandler;
  7683. }
  7684. tsIsIdentifier() {
  7685. return tokenIsIdentifier(this.state.type);
  7686. }
  7687. tsTokenCanFollowModifier() {
  7688. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(136) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7689. }
  7690. tsNextTokenCanFollowModifier() {
  7691. this.next();
  7692. return this.tsTokenCanFollowModifier();
  7693. }
  7694. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7695. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7696. return undefined;
  7697. }
  7698. const modifier = this.state.value;
  7699. if (allowedModifiers.indexOf(modifier) !== -1) {
  7700. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7701. return undefined;
  7702. }
  7703. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7704. return modifier;
  7705. }
  7706. }
  7707. return undefined;
  7708. }
  7709. tsParseModifiers({
  7710. allowedModifiers,
  7711. disallowedModifiers,
  7712. stopOnStartOfClassStaticBlock,
  7713. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7714. }, modified) {
  7715. const enforceOrder = (loc, modifier, before, after) => {
  7716. if (modifier === before && modified[after]) {
  7717. this.raise(TSErrors.InvalidModifiersOrder, {
  7718. at: loc,
  7719. orderedModifiers: [before, after]
  7720. });
  7721. }
  7722. };
  7723. const incompatible = (loc, modifier, mod1, mod2) => {
  7724. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7725. this.raise(TSErrors.IncompatibleModifiers, {
  7726. at: loc,
  7727. modifiers: [mod1, mod2]
  7728. });
  7729. }
  7730. };
  7731. for (;;) {
  7732. const {
  7733. startLoc
  7734. } = this.state;
  7735. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7736. if (!modifier) break;
  7737. if (tsIsAccessModifier(modifier)) {
  7738. if (modified.accessibility) {
  7739. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7740. at: startLoc,
  7741. modifier
  7742. });
  7743. } else {
  7744. enforceOrder(startLoc, modifier, modifier, "override");
  7745. enforceOrder(startLoc, modifier, modifier, "static");
  7746. enforceOrder(startLoc, modifier, modifier, "readonly");
  7747. modified.accessibility = modifier;
  7748. }
  7749. } else if (tsIsVarianceAnnotations(modifier)) {
  7750. if (modified[modifier]) {
  7751. this.raise(TSErrors.DuplicateModifier, {
  7752. at: startLoc,
  7753. modifier
  7754. });
  7755. }
  7756. modified[modifier] = true;
  7757. enforceOrder(startLoc, modifier, "in", "out");
  7758. } else {
  7759. if (Object.hasOwnProperty.call(modified, modifier)) {
  7760. this.raise(TSErrors.DuplicateModifier, {
  7761. at: startLoc,
  7762. modifier
  7763. });
  7764. } else {
  7765. enforceOrder(startLoc, modifier, "static", "readonly");
  7766. enforceOrder(startLoc, modifier, "static", "override");
  7767. enforceOrder(startLoc, modifier, "override", "readonly");
  7768. enforceOrder(startLoc, modifier, "abstract", "override");
  7769. incompatible(startLoc, modifier, "declare", "override");
  7770. incompatible(startLoc, modifier, "static", "abstract");
  7771. }
  7772. modified[modifier] = true;
  7773. }
  7774. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7775. this.raise(errorTemplate, {
  7776. at: startLoc,
  7777. modifier
  7778. });
  7779. }
  7780. }
  7781. }
  7782. tsIsListTerminator(kind) {
  7783. switch (kind) {
  7784. case "EnumMembers":
  7785. case "TypeMembers":
  7786. return this.match(8);
  7787. case "HeritageClauseElement":
  7788. return this.match(5);
  7789. case "TupleElementTypes":
  7790. return this.match(3);
  7791. case "TypeParametersOrArguments":
  7792. return this.match(48);
  7793. }
  7794. throw new Error("Unreachable");
  7795. }
  7796. tsParseList(kind, parseElement) {
  7797. const result = [];
  7798. while (!this.tsIsListTerminator(kind)) {
  7799. result.push(parseElement());
  7800. }
  7801. return result;
  7802. }
  7803. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7804. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7805. }
  7806. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7807. const result = [];
  7808. let trailingCommaPos = -1;
  7809. for (;;) {
  7810. if (this.tsIsListTerminator(kind)) {
  7811. break;
  7812. }
  7813. trailingCommaPos = -1;
  7814. const element = parseElement();
  7815. if (element == null) {
  7816. return undefined;
  7817. }
  7818. result.push(element);
  7819. if (this.eat(12)) {
  7820. trailingCommaPos = this.state.lastTokStart;
  7821. continue;
  7822. }
  7823. if (this.tsIsListTerminator(kind)) {
  7824. break;
  7825. }
  7826. if (expectSuccess) {
  7827. this.expect(12);
  7828. }
  7829. return undefined;
  7830. }
  7831. if (refTrailingCommaPos) {
  7832. refTrailingCommaPos.value = trailingCommaPos;
  7833. }
  7834. return result;
  7835. }
  7836. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7837. if (!skipFirstToken) {
  7838. if (bracket) {
  7839. this.expect(0);
  7840. } else {
  7841. this.expect(47);
  7842. }
  7843. }
  7844. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7845. if (bracket) {
  7846. this.expect(3);
  7847. } else {
  7848. this.expect(48);
  7849. }
  7850. return result;
  7851. }
  7852. tsParseImportType() {
  7853. const node = this.startNode();
  7854. this.expect(83);
  7855. this.expect(10);
  7856. if (!this.match(131)) {
  7857. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7858. at: this.state.startLoc
  7859. });
  7860. }
  7861. node.argument = super.parseExprAtom();
  7862. this.expect(11);
  7863. if (this.eat(16)) {
  7864. node.qualifier = this.tsParseEntityName();
  7865. }
  7866. if (this.match(47)) {
  7867. node.typeParameters = this.tsParseTypeArguments();
  7868. }
  7869. return this.finishNode(node, "TSImportType");
  7870. }
  7871. tsParseEntityName(allowReservedWords = true) {
  7872. let entity = this.parseIdentifier(allowReservedWords);
  7873. while (this.eat(16)) {
  7874. const node = this.startNodeAtNode(entity);
  7875. node.left = entity;
  7876. node.right = this.parseIdentifier(allowReservedWords);
  7877. entity = this.finishNode(node, "TSQualifiedName");
  7878. }
  7879. return entity;
  7880. }
  7881. tsParseTypeReference() {
  7882. const node = this.startNode();
  7883. node.typeName = this.tsParseEntityName();
  7884. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7885. node.typeParameters = this.tsParseTypeArguments();
  7886. }
  7887. return this.finishNode(node, "TSTypeReference");
  7888. }
  7889. tsParseThisTypePredicate(lhs) {
  7890. this.next();
  7891. const node = this.startNodeAtNode(lhs);
  7892. node.parameterName = lhs;
  7893. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7894. node.asserts = false;
  7895. return this.finishNode(node, "TSTypePredicate");
  7896. }
  7897. tsParseThisTypeNode() {
  7898. const node = this.startNode();
  7899. this.next();
  7900. return this.finishNode(node, "TSThisType");
  7901. }
  7902. tsParseTypeQuery() {
  7903. const node = this.startNode();
  7904. this.expect(87);
  7905. if (this.match(83)) {
  7906. node.exprName = this.tsParseImportType();
  7907. } else {
  7908. node.exprName = this.tsParseEntityName();
  7909. }
  7910. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7911. node.typeParameters = this.tsParseTypeArguments();
  7912. }
  7913. return this.finishNode(node, "TSTypeQuery");
  7914. }
  7915. tsParseTypeParameter(parseModifiers) {
  7916. const node = this.startNode();
  7917. parseModifiers(node);
  7918. node.name = this.tsParseTypeParameterName();
  7919. node.constraint = this.tsEatThenParseType(81);
  7920. node.default = this.tsEatThenParseType(29);
  7921. return this.finishNode(node, "TSTypeParameter");
  7922. }
  7923. tsTryParseTypeParameters(parseModifiers) {
  7924. if (this.match(47)) {
  7925. return this.tsParseTypeParameters(parseModifiers);
  7926. }
  7927. }
  7928. tsParseTypeParameters(parseModifiers) {
  7929. const node = this.startNode();
  7930. if (this.match(47) || this.match(140)) {
  7931. this.next();
  7932. } else {
  7933. this.unexpected();
  7934. }
  7935. const refTrailingCommaPos = {
  7936. value: -1
  7937. };
  7938. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7939. if (node.params.length === 0) {
  7940. this.raise(TSErrors.EmptyTypeParameters, {
  7941. at: node
  7942. });
  7943. }
  7944. if (refTrailingCommaPos.value !== -1) {
  7945. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7946. }
  7947. return this.finishNode(node, "TSTypeParameterDeclaration");
  7948. }
  7949. tsFillSignature(returnToken, signature) {
  7950. const returnTokenRequired = returnToken === 19;
  7951. const paramsKey = "parameters";
  7952. const returnTypeKey = "typeAnnotation";
  7953. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  7954. this.expect(10);
  7955. signature[paramsKey] = this.tsParseBindingListForSignature();
  7956. if (returnTokenRequired) {
  7957. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7958. } else if (this.match(returnToken)) {
  7959. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7960. }
  7961. }
  7962. tsParseBindingListForSignature() {
  7963. return super.parseBindingList(11, 41, ParseBindingListFlags.IS_FUNCTION_PARAMS).map(pattern => {
  7964. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7965. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7966. at: pattern,
  7967. type: pattern.type
  7968. });
  7969. }
  7970. return pattern;
  7971. });
  7972. }
  7973. tsParseTypeMemberSemicolon() {
  7974. if (!this.eat(12) && !this.isLineTerminator()) {
  7975. this.expect(13);
  7976. }
  7977. }
  7978. tsParseSignatureMember(kind, node) {
  7979. this.tsFillSignature(14, node);
  7980. this.tsParseTypeMemberSemicolon();
  7981. return this.finishNode(node, kind);
  7982. }
  7983. tsIsUnambiguouslyIndexSignature() {
  7984. this.next();
  7985. if (tokenIsIdentifier(this.state.type)) {
  7986. this.next();
  7987. return this.match(14);
  7988. }
  7989. return false;
  7990. }
  7991. tsTryParseIndexSignature(node) {
  7992. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7993. return undefined;
  7994. }
  7995. this.expect(0);
  7996. const id = this.parseIdentifier();
  7997. id.typeAnnotation = this.tsParseTypeAnnotation();
  7998. this.resetEndLocation(id);
  7999. this.expect(3);
  8000. node.parameters = [id];
  8001. const type = this.tsTryParseTypeAnnotation();
  8002. if (type) node.typeAnnotation = type;
  8003. this.tsParseTypeMemberSemicolon();
  8004. return this.finishNode(node, "TSIndexSignature");
  8005. }
  8006. tsParsePropertyOrMethodSignature(node, readonly) {
  8007. if (this.eat(17)) node.optional = true;
  8008. const nodeAny = node;
  8009. if (this.match(10) || this.match(47)) {
  8010. if (readonly) {
  8011. this.raise(TSErrors.ReadonlyForMethodSignature, {
  8012. at: node
  8013. });
  8014. }
  8015. const method = nodeAny;
  8016. if (method.kind && this.match(47)) {
  8017. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  8018. at: this.state.curPosition()
  8019. });
  8020. }
  8021. this.tsFillSignature(14, method);
  8022. this.tsParseTypeMemberSemicolon();
  8023. const paramsKey = "parameters";
  8024. const returnTypeKey = "typeAnnotation";
  8025. if (method.kind === "get") {
  8026. if (method[paramsKey].length > 0) {
  8027. this.raise(Errors.BadGetterArity, {
  8028. at: this.state.curPosition()
  8029. });
  8030. if (this.isThisParam(method[paramsKey][0])) {
  8031. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8032. at: this.state.curPosition()
  8033. });
  8034. }
  8035. }
  8036. } else if (method.kind === "set") {
  8037. if (method[paramsKey].length !== 1) {
  8038. this.raise(Errors.BadSetterArity, {
  8039. at: this.state.curPosition()
  8040. });
  8041. } else {
  8042. const firstParameter = method[paramsKey][0];
  8043. if (this.isThisParam(firstParameter)) {
  8044. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8045. at: this.state.curPosition()
  8046. });
  8047. }
  8048. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  8049. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  8050. at: this.state.curPosition()
  8051. });
  8052. }
  8053. if (firstParameter.type === "RestElement") {
  8054. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  8055. at: this.state.curPosition()
  8056. });
  8057. }
  8058. }
  8059. if (method[returnTypeKey]) {
  8060. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  8061. at: method[returnTypeKey]
  8062. });
  8063. }
  8064. } else {
  8065. method.kind = "method";
  8066. }
  8067. return this.finishNode(method, "TSMethodSignature");
  8068. } else {
  8069. const property = nodeAny;
  8070. if (readonly) property.readonly = true;
  8071. const type = this.tsTryParseTypeAnnotation();
  8072. if (type) property.typeAnnotation = type;
  8073. this.tsParseTypeMemberSemicolon();
  8074. return this.finishNode(property, "TSPropertySignature");
  8075. }
  8076. }
  8077. tsParseTypeMember() {
  8078. const node = this.startNode();
  8079. if (this.match(10) || this.match(47)) {
  8080. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  8081. }
  8082. if (this.match(77)) {
  8083. const id = this.startNode();
  8084. this.next();
  8085. if (this.match(10) || this.match(47)) {
  8086. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  8087. } else {
  8088. node.key = this.createIdentifier(id, "new");
  8089. return this.tsParsePropertyOrMethodSignature(node, false);
  8090. }
  8091. }
  8092. this.tsParseModifiers({
  8093. allowedModifiers: ["readonly"],
  8094. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  8095. }, node);
  8096. const idx = this.tsTryParseIndexSignature(node);
  8097. if (idx) {
  8098. return idx;
  8099. }
  8100. super.parsePropertyName(node);
  8101. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  8102. node.kind = node.key.name;
  8103. super.parsePropertyName(node);
  8104. }
  8105. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  8106. }
  8107. tsParseTypeLiteral() {
  8108. const node = this.startNode();
  8109. node.members = this.tsParseObjectTypeMembers();
  8110. return this.finishNode(node, "TSTypeLiteral");
  8111. }
  8112. tsParseObjectTypeMembers() {
  8113. this.expect(5);
  8114. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  8115. this.expect(8);
  8116. return members;
  8117. }
  8118. tsIsStartOfMappedType() {
  8119. this.next();
  8120. if (this.eat(53)) {
  8121. return this.isContextual(120);
  8122. }
  8123. if (this.isContextual(120)) {
  8124. this.next();
  8125. }
  8126. if (!this.match(0)) {
  8127. return false;
  8128. }
  8129. this.next();
  8130. if (!this.tsIsIdentifier()) {
  8131. return false;
  8132. }
  8133. this.next();
  8134. return this.match(58);
  8135. }
  8136. tsParseMappedTypeParameter() {
  8137. const node = this.startNode();
  8138. node.name = this.tsParseTypeParameterName();
  8139. node.constraint = this.tsExpectThenParseType(58);
  8140. return this.finishNode(node, "TSTypeParameter");
  8141. }
  8142. tsParseMappedType() {
  8143. const node = this.startNode();
  8144. this.expect(5);
  8145. if (this.match(53)) {
  8146. node.readonly = this.state.value;
  8147. this.next();
  8148. this.expectContextual(120);
  8149. } else if (this.eatContextual(120)) {
  8150. node.readonly = true;
  8151. }
  8152. this.expect(0);
  8153. node.typeParameter = this.tsParseMappedTypeParameter();
  8154. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  8155. this.expect(3);
  8156. if (this.match(53)) {
  8157. node.optional = this.state.value;
  8158. this.next();
  8159. this.expect(17);
  8160. } else if (this.eat(17)) {
  8161. node.optional = true;
  8162. }
  8163. node.typeAnnotation = this.tsTryParseType();
  8164. this.semicolon();
  8165. this.expect(8);
  8166. return this.finishNode(node, "TSMappedType");
  8167. }
  8168. tsParseTupleType() {
  8169. const node = this.startNode();
  8170. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  8171. let seenOptionalElement = false;
  8172. let labeledElements = null;
  8173. node.elementTypes.forEach(elementNode => {
  8174. var _labeledElements;
  8175. const {
  8176. type
  8177. } = elementNode;
  8178. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  8179. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  8180. at: elementNode
  8181. });
  8182. }
  8183. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  8184. let checkType = type;
  8185. if (type === "TSRestType") {
  8186. elementNode = elementNode.typeAnnotation;
  8187. checkType = elementNode.type;
  8188. }
  8189. const isLabeled = checkType === "TSNamedTupleMember";
  8190. (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
  8191. if (labeledElements !== isLabeled) {
  8192. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  8193. at: elementNode
  8194. });
  8195. }
  8196. });
  8197. return this.finishNode(node, "TSTupleType");
  8198. }
  8199. tsParseTupleElementType() {
  8200. const {
  8201. startLoc
  8202. } = this.state;
  8203. const rest = this.eat(21);
  8204. let type = this.tsParseType();
  8205. const optional = this.eat(17);
  8206. const labeled = this.eat(14);
  8207. if (labeled) {
  8208. const labeledNode = this.startNodeAtNode(type);
  8209. labeledNode.optional = optional;
  8210. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  8211. labeledNode.label = type.typeName;
  8212. } else {
  8213. this.raise(TSErrors.InvalidTupleMemberLabel, {
  8214. at: type
  8215. });
  8216. labeledNode.label = type;
  8217. }
  8218. labeledNode.elementType = this.tsParseType();
  8219. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8220. } else if (optional) {
  8221. const optionalTypeNode = this.startNodeAtNode(type);
  8222. optionalTypeNode.typeAnnotation = type;
  8223. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8224. }
  8225. if (rest) {
  8226. const restNode = this.startNodeAt(startLoc);
  8227. restNode.typeAnnotation = type;
  8228. type = this.finishNode(restNode, "TSRestType");
  8229. }
  8230. return type;
  8231. }
  8232. tsParseParenthesizedType() {
  8233. const node = this.startNode();
  8234. this.expect(10);
  8235. node.typeAnnotation = this.tsParseType();
  8236. this.expect(11);
  8237. return this.finishNode(node, "TSParenthesizedType");
  8238. }
  8239. tsParseFunctionOrConstructorType(type, abstract) {
  8240. const node = this.startNode();
  8241. if (type === "TSConstructorType") {
  8242. node.abstract = !!abstract;
  8243. if (abstract) this.next();
  8244. this.next();
  8245. }
  8246. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8247. return this.finishNode(node, type);
  8248. }
  8249. tsParseLiteralTypeNode() {
  8250. const node = this.startNode();
  8251. node.literal = (() => {
  8252. switch (this.state.type) {
  8253. case 132:
  8254. case 133:
  8255. case 131:
  8256. case 85:
  8257. case 86:
  8258. return super.parseExprAtom();
  8259. default:
  8260. throw this.unexpected();
  8261. }
  8262. })();
  8263. return this.finishNode(node, "TSLiteralType");
  8264. }
  8265. tsParseTemplateLiteralType() {
  8266. const node = this.startNode();
  8267. node.literal = super.parseTemplate(false);
  8268. return this.finishNode(node, "TSLiteralType");
  8269. }
  8270. parseTemplateSubstitution() {
  8271. if (this.state.inType) return this.tsParseType();
  8272. return super.parseTemplateSubstitution();
  8273. }
  8274. tsParseThisTypeOrThisTypePredicate() {
  8275. const thisKeyword = this.tsParseThisTypeNode();
  8276. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8277. return this.tsParseThisTypePredicate(thisKeyword);
  8278. } else {
  8279. return thisKeyword;
  8280. }
  8281. }
  8282. tsParseNonArrayType() {
  8283. switch (this.state.type) {
  8284. case 131:
  8285. case 132:
  8286. case 133:
  8287. case 85:
  8288. case 86:
  8289. return this.tsParseLiteralTypeNode();
  8290. case 53:
  8291. if (this.state.value === "-") {
  8292. const node = this.startNode();
  8293. const nextToken = this.lookahead();
  8294. if (nextToken.type !== 132 && nextToken.type !== 133) {
  8295. throw this.unexpected();
  8296. }
  8297. node.literal = this.parseMaybeUnary();
  8298. return this.finishNode(node, "TSLiteralType");
  8299. }
  8300. break;
  8301. case 78:
  8302. return this.tsParseThisTypeOrThisTypePredicate();
  8303. case 87:
  8304. return this.tsParseTypeQuery();
  8305. case 83:
  8306. return this.tsParseImportType();
  8307. case 5:
  8308. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8309. case 0:
  8310. return this.tsParseTupleType();
  8311. case 10:
  8312. return this.tsParseParenthesizedType();
  8313. case 25:
  8314. case 24:
  8315. return this.tsParseTemplateLiteralType();
  8316. default:
  8317. {
  8318. const {
  8319. type
  8320. } = this.state;
  8321. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8322. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8323. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8324. const node = this.startNode();
  8325. this.next();
  8326. return this.finishNode(node, nodeType);
  8327. }
  8328. return this.tsParseTypeReference();
  8329. }
  8330. }
  8331. }
  8332. throw this.unexpected();
  8333. }
  8334. tsParseArrayTypeOrHigher() {
  8335. let type = this.tsParseNonArrayType();
  8336. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8337. if (this.match(3)) {
  8338. const node = this.startNodeAtNode(type);
  8339. node.elementType = type;
  8340. this.expect(3);
  8341. type = this.finishNode(node, "TSArrayType");
  8342. } else {
  8343. const node = this.startNodeAtNode(type);
  8344. node.objectType = type;
  8345. node.indexType = this.tsParseType();
  8346. this.expect(3);
  8347. type = this.finishNode(node, "TSIndexedAccessType");
  8348. }
  8349. }
  8350. return type;
  8351. }
  8352. tsParseTypeOperator() {
  8353. const node = this.startNode();
  8354. const operator = this.state.value;
  8355. this.next();
  8356. node.operator = operator;
  8357. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8358. if (operator === "readonly") {
  8359. this.tsCheckTypeAnnotationForReadOnly(node);
  8360. }
  8361. return this.finishNode(node, "TSTypeOperator");
  8362. }
  8363. tsCheckTypeAnnotationForReadOnly(node) {
  8364. switch (node.typeAnnotation.type) {
  8365. case "TSTupleType":
  8366. case "TSArrayType":
  8367. return;
  8368. default:
  8369. this.raise(TSErrors.UnexpectedReadonly, {
  8370. at: node
  8371. });
  8372. }
  8373. }
  8374. tsParseInferType() {
  8375. const node = this.startNode();
  8376. this.expectContextual(113);
  8377. const typeParameter = this.startNode();
  8378. typeParameter.name = this.tsParseTypeParameterName();
  8379. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8380. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8381. return this.finishNode(node, "TSInferType");
  8382. }
  8383. tsParseConstraintForInferType() {
  8384. if (this.eat(81)) {
  8385. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8386. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8387. return constraint;
  8388. }
  8389. }
  8390. }
  8391. tsParseTypeOperatorOrHigher() {
  8392. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8393. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(113) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8394. }
  8395. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8396. const node = this.startNode();
  8397. const hasLeadingOperator = this.eat(operator);
  8398. const types = [];
  8399. do {
  8400. types.push(parseConstituentType());
  8401. } while (this.eat(operator));
  8402. if (types.length === 1 && !hasLeadingOperator) {
  8403. return types[0];
  8404. }
  8405. node.types = types;
  8406. return this.finishNode(node, kind);
  8407. }
  8408. tsParseIntersectionTypeOrHigher() {
  8409. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8410. }
  8411. tsParseUnionTypeOrHigher() {
  8412. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8413. }
  8414. tsIsStartOfFunctionType() {
  8415. if (this.match(47)) {
  8416. return true;
  8417. }
  8418. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8419. }
  8420. tsSkipParameterStart() {
  8421. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8422. this.next();
  8423. return true;
  8424. }
  8425. if (this.match(5)) {
  8426. const {
  8427. errors
  8428. } = this.state;
  8429. const previousErrorCount = errors.length;
  8430. try {
  8431. this.parseObjectLike(8, true);
  8432. return errors.length === previousErrorCount;
  8433. } catch (_unused) {
  8434. return false;
  8435. }
  8436. }
  8437. if (this.match(0)) {
  8438. this.next();
  8439. const {
  8440. errors
  8441. } = this.state;
  8442. const previousErrorCount = errors.length;
  8443. try {
  8444. super.parseBindingList(3, 93, ParseBindingListFlags.ALLOW_EMPTY);
  8445. return errors.length === previousErrorCount;
  8446. } catch (_unused2) {
  8447. return false;
  8448. }
  8449. }
  8450. return false;
  8451. }
  8452. tsIsUnambiguouslyStartOfFunctionType() {
  8453. this.next();
  8454. if (this.match(11) || this.match(21)) {
  8455. return true;
  8456. }
  8457. if (this.tsSkipParameterStart()) {
  8458. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8459. return true;
  8460. }
  8461. if (this.match(11)) {
  8462. this.next();
  8463. if (this.match(19)) {
  8464. return true;
  8465. }
  8466. }
  8467. }
  8468. return false;
  8469. }
  8470. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8471. return this.tsInType(() => {
  8472. const t = this.startNode();
  8473. this.expect(returnToken);
  8474. const node = this.startNode();
  8475. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8476. if (asserts && this.match(78)) {
  8477. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8478. if (thisTypePredicate.type === "TSThisType") {
  8479. node.parameterName = thisTypePredicate;
  8480. node.asserts = true;
  8481. node.typeAnnotation = null;
  8482. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8483. } else {
  8484. this.resetStartLocationFromNode(thisTypePredicate, node);
  8485. thisTypePredicate.asserts = true;
  8486. }
  8487. t.typeAnnotation = thisTypePredicate;
  8488. return this.finishNode(t, "TSTypeAnnotation");
  8489. }
  8490. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8491. if (!typePredicateVariable) {
  8492. if (!asserts) {
  8493. return this.tsParseTypeAnnotation(false, t);
  8494. }
  8495. node.parameterName = this.parseIdentifier();
  8496. node.asserts = asserts;
  8497. node.typeAnnotation = null;
  8498. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8499. return this.finishNode(t, "TSTypeAnnotation");
  8500. }
  8501. const type = this.tsParseTypeAnnotation(false);
  8502. node.parameterName = typePredicateVariable;
  8503. node.typeAnnotation = type;
  8504. node.asserts = asserts;
  8505. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8506. return this.finishNode(t, "TSTypeAnnotation");
  8507. });
  8508. }
  8509. tsTryParseTypeOrTypePredicateAnnotation() {
  8510. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  8511. }
  8512. tsTryParseTypeAnnotation() {
  8513. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  8514. }
  8515. tsTryParseType() {
  8516. return this.tsEatThenParseType(14);
  8517. }
  8518. tsParseTypePredicatePrefix() {
  8519. const id = this.parseIdentifier();
  8520. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8521. this.next();
  8522. return id;
  8523. }
  8524. }
  8525. tsParseTypePredicateAsserts() {
  8526. if (this.state.type !== 107) {
  8527. return false;
  8528. }
  8529. const containsEsc = this.state.containsEsc;
  8530. this.next();
  8531. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8532. return false;
  8533. }
  8534. if (containsEsc) {
  8535. this.raise(Errors.InvalidEscapedReservedWord, {
  8536. at: this.state.lastTokStartLoc,
  8537. reservedWord: "asserts"
  8538. });
  8539. }
  8540. return true;
  8541. }
  8542. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8543. this.tsInType(() => {
  8544. if (eatColon) this.expect(14);
  8545. t.typeAnnotation = this.tsParseType();
  8546. });
  8547. return this.finishNode(t, "TSTypeAnnotation");
  8548. }
  8549. tsParseType() {
  8550. assert(this.state.inType);
  8551. const type = this.tsParseNonConditionalType();
  8552. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8553. return type;
  8554. }
  8555. const node = this.startNodeAtNode(type);
  8556. node.checkType = type;
  8557. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8558. this.expect(17);
  8559. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8560. this.expect(14);
  8561. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8562. return this.finishNode(node, "TSConditionalType");
  8563. }
  8564. isAbstractConstructorSignature() {
  8565. return this.isContextual(122) && this.lookahead().type === 77;
  8566. }
  8567. tsParseNonConditionalType() {
  8568. if (this.tsIsStartOfFunctionType()) {
  8569. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8570. }
  8571. if (this.match(77)) {
  8572. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8573. } else if (this.isAbstractConstructorSignature()) {
  8574. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8575. }
  8576. return this.tsParseUnionTypeOrHigher();
  8577. }
  8578. tsParseTypeAssertion() {
  8579. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8580. this.raise(TSErrors.ReservedTypeAssertion, {
  8581. at: this.state.startLoc
  8582. });
  8583. }
  8584. const node = this.startNode();
  8585. node.typeAnnotation = this.tsInType(() => {
  8586. this.next();
  8587. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8588. });
  8589. this.expect(48);
  8590. node.expression = this.parseMaybeUnary();
  8591. return this.finishNode(node, "TSTypeAssertion");
  8592. }
  8593. tsParseHeritageClause(token) {
  8594. const originalStartLoc = this.state.startLoc;
  8595. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8596. const node = this.startNode();
  8597. node.expression = this.tsParseEntityName();
  8598. if (this.match(47)) {
  8599. node.typeParameters = this.tsParseTypeArguments();
  8600. }
  8601. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8602. });
  8603. if (!delimitedList.length) {
  8604. this.raise(TSErrors.EmptyHeritageClauseType, {
  8605. at: originalStartLoc,
  8606. token
  8607. });
  8608. }
  8609. return delimitedList;
  8610. }
  8611. tsParseInterfaceDeclaration(node, properties = {}) {
  8612. if (this.hasFollowingLineBreak()) return null;
  8613. this.expectContextual(127);
  8614. if (properties.declare) node.declare = true;
  8615. if (tokenIsIdentifier(this.state.type)) {
  8616. node.id = this.parseIdentifier();
  8617. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  8618. } else {
  8619. node.id = null;
  8620. this.raise(TSErrors.MissingInterfaceName, {
  8621. at: this.state.startLoc
  8622. });
  8623. }
  8624. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8625. if (this.eat(81)) {
  8626. node.extends = this.tsParseHeritageClause("extends");
  8627. }
  8628. const body = this.startNode();
  8629. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8630. node.body = this.finishNode(body, "TSInterfaceBody");
  8631. return this.finishNode(node, "TSInterfaceDeclaration");
  8632. }
  8633. tsParseTypeAliasDeclaration(node) {
  8634. node.id = this.parseIdentifier();
  8635. this.checkIdentifier(node.id, BIND_TS_TYPE);
  8636. node.typeAnnotation = this.tsInType(() => {
  8637. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8638. this.expect(29);
  8639. if (this.isContextual(112) && this.lookahead().type !== 16) {
  8640. const node = this.startNode();
  8641. this.next();
  8642. return this.finishNode(node, "TSIntrinsicKeyword");
  8643. }
  8644. return this.tsParseType();
  8645. });
  8646. this.semicolon();
  8647. return this.finishNode(node, "TSTypeAliasDeclaration");
  8648. }
  8649. tsInNoContext(cb) {
  8650. const oldContext = this.state.context;
  8651. this.state.context = [oldContext[0]];
  8652. try {
  8653. return cb();
  8654. } finally {
  8655. this.state.context = oldContext;
  8656. }
  8657. }
  8658. tsInType(cb) {
  8659. const oldInType = this.state.inType;
  8660. this.state.inType = true;
  8661. try {
  8662. return cb();
  8663. } finally {
  8664. this.state.inType = oldInType;
  8665. }
  8666. }
  8667. tsInDisallowConditionalTypesContext(cb) {
  8668. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8669. this.state.inDisallowConditionalTypesContext = true;
  8670. try {
  8671. return cb();
  8672. } finally {
  8673. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8674. }
  8675. }
  8676. tsInAllowConditionalTypesContext(cb) {
  8677. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8678. this.state.inDisallowConditionalTypesContext = false;
  8679. try {
  8680. return cb();
  8681. } finally {
  8682. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8683. }
  8684. }
  8685. tsEatThenParseType(token) {
  8686. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8687. }
  8688. tsExpectThenParseType(token) {
  8689. return this.tsDoThenParseType(() => this.expect(token));
  8690. }
  8691. tsNextThenParseType() {
  8692. return this.tsDoThenParseType(() => this.next());
  8693. }
  8694. tsDoThenParseType(cb) {
  8695. return this.tsInType(() => {
  8696. cb();
  8697. return this.tsParseType();
  8698. });
  8699. }
  8700. tsParseEnumMember() {
  8701. const node = this.startNode();
  8702. node.id = this.match(131) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8703. if (this.eat(29)) {
  8704. node.initializer = super.parseMaybeAssignAllowIn();
  8705. }
  8706. return this.finishNode(node, "TSEnumMember");
  8707. }
  8708. tsParseEnumDeclaration(node, properties = {}) {
  8709. if (properties.const) node.const = true;
  8710. if (properties.declare) node.declare = true;
  8711. this.expectContextual(124);
  8712. node.id = this.parseIdentifier();
  8713. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8714. this.expect(5);
  8715. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8716. this.expect(8);
  8717. return this.finishNode(node, "TSEnumDeclaration");
  8718. }
  8719. tsParseModuleBlock() {
  8720. const node = this.startNode();
  8721. this.scope.enter(SCOPE_OTHER);
  8722. this.expect(5);
  8723. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8724. this.scope.exit();
  8725. return this.finishNode(node, "TSModuleBlock");
  8726. }
  8727. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8728. node.id = this.parseIdentifier();
  8729. if (!nested) {
  8730. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8731. }
  8732. if (this.eat(16)) {
  8733. const inner = this.startNode();
  8734. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8735. node.body = inner;
  8736. } else {
  8737. this.scope.enter(SCOPE_TS_MODULE);
  8738. this.prodParam.enter(PARAM);
  8739. node.body = this.tsParseModuleBlock();
  8740. this.prodParam.exit();
  8741. this.scope.exit();
  8742. }
  8743. return this.finishNode(node, "TSModuleDeclaration");
  8744. }
  8745. tsParseAmbientExternalModuleDeclaration(node) {
  8746. if (this.isContextual(110)) {
  8747. node.global = true;
  8748. node.id = this.parseIdentifier();
  8749. } else if (this.match(131)) {
  8750. node.id = super.parseStringLiteral(this.state.value);
  8751. } else {
  8752. this.unexpected();
  8753. }
  8754. if (this.match(5)) {
  8755. this.scope.enter(SCOPE_TS_MODULE);
  8756. this.prodParam.enter(PARAM);
  8757. node.body = this.tsParseModuleBlock();
  8758. this.prodParam.exit();
  8759. this.scope.exit();
  8760. } else {
  8761. this.semicolon();
  8762. }
  8763. return this.finishNode(node, "TSModuleDeclaration");
  8764. }
  8765. tsParseImportEqualsDeclaration(node, isExport) {
  8766. node.isExport = isExport || false;
  8767. node.id = this.parseIdentifier();
  8768. this.checkIdentifier(node.id, BIND_FLAGS_TS_IMPORT);
  8769. this.expect(29);
  8770. const moduleReference = this.tsParseModuleReference();
  8771. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8772. this.raise(TSErrors.ImportAliasHasImportType, {
  8773. at: moduleReference
  8774. });
  8775. }
  8776. node.moduleReference = moduleReference;
  8777. this.semicolon();
  8778. return this.finishNode(node, "TSImportEqualsDeclaration");
  8779. }
  8780. tsIsExternalModuleReference() {
  8781. return this.isContextual(117) && this.lookaheadCharCode() === 40;
  8782. }
  8783. tsParseModuleReference() {
  8784. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8785. }
  8786. tsParseExternalModuleReference() {
  8787. const node = this.startNode();
  8788. this.expectContextual(117);
  8789. this.expect(10);
  8790. if (!this.match(131)) {
  8791. throw this.unexpected();
  8792. }
  8793. node.expression = super.parseExprAtom();
  8794. this.expect(11);
  8795. return this.finishNode(node, "TSExternalModuleReference");
  8796. }
  8797. tsLookAhead(f) {
  8798. const state = this.state.clone();
  8799. const res = f();
  8800. this.state = state;
  8801. return res;
  8802. }
  8803. tsTryParseAndCatch(f) {
  8804. const result = this.tryParse(abort => f() || abort());
  8805. if (result.aborted || !result.node) return undefined;
  8806. if (result.error) this.state = result.failState;
  8807. return result.node;
  8808. }
  8809. tsTryParse(f) {
  8810. const state = this.state.clone();
  8811. const result = f();
  8812. if (result !== undefined && result !== false) {
  8813. return result;
  8814. } else {
  8815. this.state = state;
  8816. return undefined;
  8817. }
  8818. }
  8819. tsTryParseDeclare(nany) {
  8820. if (this.isLineTerminator()) {
  8821. return;
  8822. }
  8823. let starttype = this.state.type;
  8824. let kind;
  8825. if (this.isContextual(99)) {
  8826. starttype = 74;
  8827. kind = "let";
  8828. }
  8829. return this.tsInAmbientContext(() => {
  8830. if (starttype === 68) {
  8831. nany.declare = true;
  8832. return super.parseFunctionStatement(nany, false, false);
  8833. }
  8834. if (starttype === 80) {
  8835. nany.declare = true;
  8836. return this.parseClass(nany, true, false);
  8837. }
  8838. if (starttype === 124) {
  8839. return this.tsParseEnumDeclaration(nany, {
  8840. declare: true
  8841. });
  8842. }
  8843. if (starttype === 110) {
  8844. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8845. }
  8846. if (starttype === 75 || starttype === 74) {
  8847. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8848. nany.declare = true;
  8849. return this.parseVarStatement(nany, kind || this.state.value, true);
  8850. }
  8851. this.expect(75);
  8852. return this.tsParseEnumDeclaration(nany, {
  8853. const: true,
  8854. declare: true
  8855. });
  8856. }
  8857. if (starttype === 127) {
  8858. const result = this.tsParseInterfaceDeclaration(nany, {
  8859. declare: true
  8860. });
  8861. if (result) return result;
  8862. }
  8863. if (tokenIsIdentifier(starttype)) {
  8864. return this.tsParseDeclaration(nany, this.state.value, true, null);
  8865. }
  8866. });
  8867. }
  8868. tsTryParseExportDeclaration() {
  8869. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  8870. }
  8871. tsParseExpressionStatement(node, expr, decorators) {
  8872. switch (expr.name) {
  8873. case "declare":
  8874. {
  8875. const declaration = this.tsTryParseDeclare(node);
  8876. if (declaration) {
  8877. declaration.declare = true;
  8878. return declaration;
  8879. }
  8880. break;
  8881. }
  8882. case "global":
  8883. if (this.match(5)) {
  8884. this.scope.enter(SCOPE_TS_MODULE);
  8885. this.prodParam.enter(PARAM);
  8886. const mod = node;
  8887. mod.global = true;
  8888. mod.id = expr;
  8889. mod.body = this.tsParseModuleBlock();
  8890. this.scope.exit();
  8891. this.prodParam.exit();
  8892. return this.finishNode(mod, "TSModuleDeclaration");
  8893. }
  8894. break;
  8895. default:
  8896. return this.tsParseDeclaration(node, expr.name, false, decorators);
  8897. }
  8898. }
  8899. tsParseDeclaration(node, value, next, decorators) {
  8900. switch (value) {
  8901. case "abstract":
  8902. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8903. return this.tsParseAbstractDeclaration(node, decorators);
  8904. }
  8905. break;
  8906. case "module":
  8907. if (this.tsCheckLineTerminator(next)) {
  8908. if (this.match(131)) {
  8909. return this.tsParseAmbientExternalModuleDeclaration(node);
  8910. } else if (tokenIsIdentifier(this.state.type)) {
  8911. return this.tsParseModuleOrNamespaceDeclaration(node);
  8912. }
  8913. }
  8914. break;
  8915. case "namespace":
  8916. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8917. return this.tsParseModuleOrNamespaceDeclaration(node);
  8918. }
  8919. break;
  8920. case "type":
  8921. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8922. return this.tsParseTypeAliasDeclaration(node);
  8923. }
  8924. break;
  8925. }
  8926. }
  8927. tsCheckLineTerminator(next) {
  8928. if (next) {
  8929. if (this.hasFollowingLineBreak()) return false;
  8930. this.next();
  8931. return true;
  8932. }
  8933. return !this.isLineTerminator();
  8934. }
  8935. tsTryParseGenericAsyncArrowFunction(startLoc) {
  8936. if (!this.match(47)) {
  8937. return undefined;
  8938. }
  8939. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8940. this.state.maybeInArrowParameters = true;
  8941. const res = this.tsTryParseAndCatch(() => {
  8942. const node = this.startNodeAt(startLoc);
  8943. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  8944. super.parseFunctionParams(node);
  8945. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8946. this.expect(19);
  8947. return node;
  8948. });
  8949. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8950. if (!res) {
  8951. return undefined;
  8952. }
  8953. return super.parseArrowExpression(res, null, true);
  8954. }
  8955. tsParseTypeArgumentsInExpression() {
  8956. if (this.reScan_lt() !== 47) {
  8957. return undefined;
  8958. }
  8959. return this.tsParseTypeArguments();
  8960. }
  8961. tsParseTypeArguments() {
  8962. const node = this.startNode();
  8963. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8964. this.expect(47);
  8965. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8966. }));
  8967. if (node.params.length === 0) {
  8968. this.raise(TSErrors.EmptyTypeArguments, {
  8969. at: node
  8970. });
  8971. }
  8972. this.expect(48);
  8973. return this.finishNode(node, "TSTypeParameterInstantiation");
  8974. }
  8975. tsIsDeclarationStart() {
  8976. return tokenIsTSDeclarationStart(this.state.type);
  8977. }
  8978. isExportDefaultSpecifier() {
  8979. if (this.tsIsDeclarationStart()) return false;
  8980. return super.isExportDefaultSpecifier();
  8981. }
  8982. parseAssignableListItem(flags, decorators) {
  8983. const startLoc = this.state.startLoc;
  8984. const modified = {};
  8985. this.tsParseModifiers({
  8986. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8987. }, modified);
  8988. const accessibility = modified.accessibility;
  8989. const override = modified.override;
  8990. const readonly = modified.readonly;
  8991. if (!(flags & ParseBindingListFlags.IS_CONSTRUCTOR_PARAMS) && (accessibility || readonly || override)) {
  8992. this.raise(TSErrors.UnexpectedParameterModifier, {
  8993. at: startLoc
  8994. });
  8995. }
  8996. const left = this.parseMaybeDefault();
  8997. this.parseAssignableListItemTypes(left, flags);
  8998. const elt = this.parseMaybeDefault(left.loc.start, left);
  8999. if (accessibility || readonly || override) {
  9000. const pp = this.startNodeAt(startLoc);
  9001. if (decorators.length) {
  9002. pp.decorators = decorators;
  9003. }
  9004. if (accessibility) pp.accessibility = accessibility;
  9005. if (readonly) pp.readonly = readonly;
  9006. if (override) pp.override = override;
  9007. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  9008. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  9009. at: pp
  9010. });
  9011. }
  9012. pp.parameter = elt;
  9013. return this.finishNode(pp, "TSParameterProperty");
  9014. }
  9015. if (decorators.length) {
  9016. left.decorators = decorators;
  9017. }
  9018. return elt;
  9019. }
  9020. isSimpleParameter(node) {
  9021. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  9022. }
  9023. tsDisallowOptionalPattern(node) {
  9024. for (const param of node.params) {
  9025. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  9026. this.raise(TSErrors.PatternIsOptional, {
  9027. at: param
  9028. });
  9029. }
  9030. }
  9031. }
  9032. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  9033. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  9034. this.tsDisallowOptionalPattern(node);
  9035. }
  9036. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9037. if (this.match(14)) {
  9038. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9039. }
  9040. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  9041. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  9042. return this.finishNode(node, bodilessType);
  9043. }
  9044. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  9045. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  9046. at: node
  9047. });
  9048. if (node.declare) {
  9049. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  9050. }
  9051. }
  9052. this.tsDisallowOptionalPattern(node);
  9053. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  9054. }
  9055. registerFunctionStatementId(node) {
  9056. if (!node.body && node.id) {
  9057. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  9058. } else {
  9059. super.registerFunctionStatementId(node);
  9060. }
  9061. }
  9062. tsCheckForInvalidTypeCasts(items) {
  9063. items.forEach(node => {
  9064. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  9065. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  9066. at: node.typeAnnotation
  9067. });
  9068. }
  9069. });
  9070. }
  9071. toReferencedList(exprList, isInParens) {
  9072. this.tsCheckForInvalidTypeCasts(exprList);
  9073. return exprList;
  9074. }
  9075. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9076. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  9077. if (node.type === "ArrayExpression") {
  9078. this.tsCheckForInvalidTypeCasts(node.elements);
  9079. }
  9080. return node;
  9081. }
  9082. parseSubscript(base, startLoc, noCalls, state) {
  9083. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  9084. this.state.canStartJSXElement = false;
  9085. this.next();
  9086. const nonNullExpression = this.startNodeAt(startLoc);
  9087. nonNullExpression.expression = base;
  9088. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  9089. }
  9090. let isOptionalCall = false;
  9091. if (this.match(18) && this.lookaheadCharCode() === 60) {
  9092. if (noCalls) {
  9093. state.stop = true;
  9094. return base;
  9095. }
  9096. state.optionalChainMember = isOptionalCall = true;
  9097. this.next();
  9098. }
  9099. if (this.match(47) || this.match(51)) {
  9100. let missingParenErrorLoc;
  9101. const result = this.tsTryParseAndCatch(() => {
  9102. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  9103. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  9104. if (asyncArrowFn) {
  9105. return asyncArrowFn;
  9106. }
  9107. }
  9108. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9109. if (!typeArguments) return;
  9110. if (isOptionalCall && !this.match(10)) {
  9111. missingParenErrorLoc = this.state.curPosition();
  9112. return;
  9113. }
  9114. if (tokenIsTemplate(this.state.type)) {
  9115. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  9116. result.typeParameters = typeArguments;
  9117. return result;
  9118. }
  9119. if (!noCalls && this.eat(10)) {
  9120. const node = this.startNodeAt(startLoc);
  9121. node.callee = base;
  9122. node.arguments = this.parseCallExpressionArguments(11, false);
  9123. this.tsCheckForInvalidTypeCasts(node.arguments);
  9124. node.typeParameters = typeArguments;
  9125. if (state.optionalChainMember) {
  9126. node.optional = isOptionalCall;
  9127. }
  9128. return this.finishCallExpression(node, state.optionalChainMember);
  9129. }
  9130. const tokenType = this.state.type;
  9131. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  9132. return;
  9133. }
  9134. const node = this.startNodeAt(startLoc);
  9135. node.expression = base;
  9136. node.typeParameters = typeArguments;
  9137. return this.finishNode(node, "TSInstantiationExpression");
  9138. });
  9139. if (missingParenErrorLoc) {
  9140. this.unexpected(missingParenErrorLoc, 10);
  9141. }
  9142. if (result) {
  9143. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  9144. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
  9145. at: this.state.startLoc
  9146. });
  9147. }
  9148. return result;
  9149. }
  9150. }
  9151. return super.parseSubscript(base, startLoc, noCalls, state);
  9152. }
  9153. parseNewCallee(node) {
  9154. var _callee$extra;
  9155. super.parseNewCallee(node);
  9156. const {
  9157. callee
  9158. } = node;
  9159. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  9160. node.typeParameters = callee.typeParameters;
  9161. node.callee = callee.expression;
  9162. }
  9163. }
  9164. parseExprOp(left, leftStartLoc, minPrec) {
  9165. let isSatisfies;
  9166. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(118)))) {
  9167. const node = this.startNodeAt(leftStartLoc);
  9168. node.expression = left;
  9169. node.typeAnnotation = this.tsInType(() => {
  9170. this.next();
  9171. if (this.match(75)) {
  9172. if (isSatisfies) {
  9173. this.raise(Errors.UnexpectedKeyword, {
  9174. at: this.state.startLoc,
  9175. keyword: "const"
  9176. });
  9177. }
  9178. return this.tsParseTypeReference();
  9179. }
  9180. return this.tsParseType();
  9181. });
  9182. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  9183. this.reScan_lt_gt();
  9184. return this.parseExprOp(node, leftStartLoc, minPrec);
  9185. }
  9186. return super.parseExprOp(left, leftStartLoc, minPrec);
  9187. }
  9188. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9189. if (!this.state.isAmbientContext) {
  9190. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9191. }
  9192. }
  9193. checkImportReflection(node) {
  9194. super.checkImportReflection(node);
  9195. if (node.module && node.importKind !== "value") {
  9196. this.raise(TSErrors.ImportReflectionHasImportType, {
  9197. at: node.specifiers[0].loc.start
  9198. });
  9199. }
  9200. }
  9201. checkDuplicateExports() {}
  9202. parseImport(node) {
  9203. node.importKind = "value";
  9204. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  9205. let ahead = this.lookahead();
  9206. if (this.isContextual(128) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  9207. node.importKind = "type";
  9208. this.next();
  9209. ahead = this.lookahead();
  9210. }
  9211. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  9212. return this.tsParseImportEqualsDeclaration(node);
  9213. }
  9214. }
  9215. const importNode = super.parseImport(node);
  9216. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9217. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  9218. at: importNode
  9219. });
  9220. }
  9221. return importNode;
  9222. }
  9223. parseExport(node, decorators) {
  9224. if (this.match(83)) {
  9225. this.next();
  9226. if (this.isContextual(128) && this.lookaheadCharCode() !== 61) {
  9227. node.importKind = "type";
  9228. this.next();
  9229. } else {
  9230. node.importKind = "value";
  9231. }
  9232. return this.tsParseImportEqualsDeclaration(node, true);
  9233. } else if (this.eat(29)) {
  9234. const assign = node;
  9235. assign.expression = super.parseExpression();
  9236. this.semicolon();
  9237. return this.finishNode(assign, "TSExportAssignment");
  9238. } else if (this.eatContextual(93)) {
  9239. const decl = node;
  9240. this.expectContextual(126);
  9241. decl.id = this.parseIdentifier();
  9242. this.semicolon();
  9243. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9244. } else {
  9245. node.exportKind = "value";
  9246. if (this.isContextual(128)) {
  9247. const ch = this.lookaheadCharCode();
  9248. if (ch === 123 || ch === 42) {
  9249. this.next();
  9250. node.exportKind = "type";
  9251. }
  9252. }
  9253. return super.parseExport(node, decorators);
  9254. }
  9255. }
  9256. isAbstractClass() {
  9257. return this.isContextual(122) && this.lookahead().type === 80;
  9258. }
  9259. parseExportDefaultExpression() {
  9260. if (this.isAbstractClass()) {
  9261. const cls = this.startNode();
  9262. this.next();
  9263. cls.abstract = true;
  9264. return this.parseClass(cls, true, true);
  9265. }
  9266. if (this.match(127)) {
  9267. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9268. if (result) return result;
  9269. }
  9270. return super.parseExportDefaultExpression();
  9271. }
  9272. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9273. const {
  9274. isAmbientContext
  9275. } = this.state;
  9276. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9277. if (!isAmbientContext) return declaration;
  9278. for (const {
  9279. id,
  9280. init
  9281. } of declaration.declarations) {
  9282. if (!init) continue;
  9283. if (kind !== "const" || !!id.typeAnnotation) {
  9284. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  9285. at: init
  9286. });
  9287. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9288. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  9289. at: init
  9290. });
  9291. }
  9292. }
  9293. return declaration;
  9294. }
  9295. parseStatementContent(flags, decorators) {
  9296. if (this.match(75) && this.isLookaheadContextual("enum")) {
  9297. const node = this.startNode();
  9298. this.expect(75);
  9299. return this.tsParseEnumDeclaration(node, {
  9300. const: true
  9301. });
  9302. }
  9303. if (this.isContextual(124)) {
  9304. return this.tsParseEnumDeclaration(this.startNode());
  9305. }
  9306. if (this.isContextual(127)) {
  9307. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9308. if (result) return result;
  9309. }
  9310. return super.parseStatementContent(flags, decorators);
  9311. }
  9312. parseAccessModifier() {
  9313. return this.tsParseModifier(["public", "protected", "private"]);
  9314. }
  9315. tsHasSomeModifiers(member, modifiers) {
  9316. return modifiers.some(modifier => {
  9317. if (tsIsAccessModifier(modifier)) {
  9318. return member.accessibility === modifier;
  9319. }
  9320. return !!member[modifier];
  9321. });
  9322. }
  9323. tsIsStartOfStaticBlocks() {
  9324. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  9325. }
  9326. parseClassMember(classBody, member, state) {
  9327. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9328. this.tsParseModifiers({
  9329. allowedModifiers: modifiers,
  9330. disallowedModifiers: ["in", "out"],
  9331. stopOnStartOfClassStaticBlock: true,
  9332. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9333. }, member);
  9334. const callParseClassMemberWithIsStatic = () => {
  9335. if (this.tsIsStartOfStaticBlocks()) {
  9336. this.next();
  9337. this.next();
  9338. if (this.tsHasSomeModifiers(member, modifiers)) {
  9339. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  9340. at: this.state.curPosition()
  9341. });
  9342. }
  9343. super.parseClassStaticBlock(classBody, member);
  9344. } else {
  9345. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9346. }
  9347. };
  9348. if (member.declare) {
  9349. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9350. } else {
  9351. callParseClassMemberWithIsStatic();
  9352. }
  9353. }
  9354. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9355. const idx = this.tsTryParseIndexSignature(member);
  9356. if (idx) {
  9357. classBody.body.push(idx);
  9358. if (member.abstract) {
  9359. this.raise(TSErrors.IndexSignatureHasAbstract, {
  9360. at: member
  9361. });
  9362. }
  9363. if (member.accessibility) {
  9364. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  9365. at: member,
  9366. modifier: member.accessibility
  9367. });
  9368. }
  9369. if (member.declare) {
  9370. this.raise(TSErrors.IndexSignatureHasDeclare, {
  9371. at: member
  9372. });
  9373. }
  9374. if (member.override) {
  9375. this.raise(TSErrors.IndexSignatureHasOverride, {
  9376. at: member
  9377. });
  9378. }
  9379. return;
  9380. }
  9381. if (!this.state.inAbstractClass && member.abstract) {
  9382. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  9383. at: member
  9384. });
  9385. }
  9386. if (member.override) {
  9387. if (!state.hadSuperClass) {
  9388. this.raise(TSErrors.OverrideNotInSubClass, {
  9389. at: member
  9390. });
  9391. }
  9392. }
  9393. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9394. }
  9395. parsePostMemberNameModifiers(methodOrProp) {
  9396. const optional = this.eat(17);
  9397. if (optional) methodOrProp.optional = true;
  9398. if (methodOrProp.readonly && this.match(10)) {
  9399. this.raise(TSErrors.ClassMethodHasReadonly, {
  9400. at: methodOrProp
  9401. });
  9402. }
  9403. if (methodOrProp.declare && this.match(10)) {
  9404. this.raise(TSErrors.ClassMethodHasDeclare, {
  9405. at: methodOrProp
  9406. });
  9407. }
  9408. }
  9409. parseExpressionStatement(node, expr, decorators) {
  9410. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9411. return decl || super.parseExpressionStatement(node, expr, decorators);
  9412. }
  9413. shouldParseExportDeclaration() {
  9414. if (this.tsIsDeclarationStart()) return true;
  9415. return super.shouldParseExportDeclaration();
  9416. }
  9417. parseConditional(expr, startLoc, refExpressionErrors) {
  9418. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  9419. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9420. }
  9421. const result = this.tryParse(() => super.parseConditional(expr, startLoc));
  9422. if (!result.node) {
  9423. if (result.error) {
  9424. super.setOptionalParametersError(refExpressionErrors, result.error);
  9425. }
  9426. return expr;
  9427. }
  9428. if (result.error) this.state = result.failState;
  9429. return result.node;
  9430. }
  9431. parseParenItem(node, startLoc) {
  9432. node = super.parseParenItem(node, startLoc);
  9433. if (this.eat(17)) {
  9434. node.optional = true;
  9435. this.resetEndLocation(node);
  9436. }
  9437. if (this.match(14)) {
  9438. const typeCastNode = this.startNodeAt(startLoc);
  9439. typeCastNode.expression = node;
  9440. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9441. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9442. }
  9443. return node;
  9444. }
  9445. parseExportDeclaration(node) {
  9446. if (!this.state.isAmbientContext && this.isContextual(123)) {
  9447. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9448. }
  9449. const startLoc = this.state.startLoc;
  9450. const isDeclare = this.eatContextual(123);
  9451. if (isDeclare && (this.isContextual(123) || !this.shouldParseExportDeclaration())) {
  9452. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  9453. at: this.state.startLoc
  9454. });
  9455. }
  9456. const isIdentifier = tokenIsIdentifier(this.state.type);
  9457. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9458. if (!declaration) return null;
  9459. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9460. node.exportKind = "type";
  9461. }
  9462. if (isDeclare) {
  9463. this.resetStartLocation(declaration, startLoc);
  9464. declaration.declare = true;
  9465. }
  9466. return declaration;
  9467. }
  9468. parseClassId(node, isStatement, optionalId, bindingType) {
  9469. if ((!isStatement || optionalId) && this.isContextual(111)) {
  9470. return;
  9471. }
  9472. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  9473. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9474. if (typeParameters) node.typeParameters = typeParameters;
  9475. }
  9476. parseClassPropertyAnnotation(node) {
  9477. if (!node.optional) {
  9478. if (this.eat(35)) {
  9479. node.definite = true;
  9480. } else if (this.eat(17)) {
  9481. node.optional = true;
  9482. }
  9483. }
  9484. const type = this.tsTryParseTypeAnnotation();
  9485. if (type) node.typeAnnotation = type;
  9486. }
  9487. parseClassProperty(node) {
  9488. this.parseClassPropertyAnnotation(node);
  9489. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9490. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  9491. at: this.state.startLoc
  9492. });
  9493. }
  9494. if (node.abstract && this.match(29)) {
  9495. const {
  9496. key
  9497. } = node;
  9498. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  9499. at: this.state.startLoc,
  9500. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9501. });
  9502. }
  9503. return super.parseClassProperty(node);
  9504. }
  9505. parseClassPrivateProperty(node) {
  9506. if (node.abstract) {
  9507. this.raise(TSErrors.PrivateElementHasAbstract, {
  9508. at: node
  9509. });
  9510. }
  9511. if (node.accessibility) {
  9512. this.raise(TSErrors.PrivateElementHasAccessibility, {
  9513. at: node,
  9514. modifier: node.accessibility
  9515. });
  9516. }
  9517. this.parseClassPropertyAnnotation(node);
  9518. return super.parseClassPrivateProperty(node);
  9519. }
  9520. parseClassAccessorProperty(node) {
  9521. this.parseClassPropertyAnnotation(node);
  9522. if (node.optional) {
  9523. this.raise(TSErrors.AccessorCannotBeOptional, {
  9524. at: node
  9525. });
  9526. }
  9527. return super.parseClassAccessorProperty(node);
  9528. }
  9529. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9530. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9531. if (typeParameters && isConstructor) {
  9532. this.raise(TSErrors.ConstructorHasTypeParameters, {
  9533. at: typeParameters
  9534. });
  9535. }
  9536. const {
  9537. declare = false,
  9538. kind
  9539. } = method;
  9540. if (declare && (kind === "get" || kind === "set")) {
  9541. this.raise(TSErrors.DeclareAccessor, {
  9542. at: method,
  9543. kind
  9544. });
  9545. }
  9546. if (typeParameters) method.typeParameters = typeParameters;
  9547. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9548. }
  9549. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9550. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9551. if (typeParameters) method.typeParameters = typeParameters;
  9552. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9553. }
  9554. declareClassPrivateMethodInScope(node, kind) {
  9555. if (node.type === "TSDeclareMethod") return;
  9556. if (node.type === "MethodDefinition" && !node.value.body) return;
  9557. super.declareClassPrivateMethodInScope(node, kind);
  9558. }
  9559. parseClassSuper(node) {
  9560. super.parseClassSuper(node);
  9561. if (node.superClass && (this.match(47) || this.match(51))) {
  9562. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9563. }
  9564. if (this.eatContextual(111)) {
  9565. node.implements = this.tsParseHeritageClause("implements");
  9566. }
  9567. }
  9568. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9569. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9570. if (typeParameters) prop.typeParameters = typeParameters;
  9571. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9572. }
  9573. parseFunctionParams(node, isConstructor) {
  9574. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9575. if (typeParameters) node.typeParameters = typeParameters;
  9576. super.parseFunctionParams(node, isConstructor);
  9577. }
  9578. parseVarId(decl, kind) {
  9579. super.parseVarId(decl, kind);
  9580. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9581. decl.definite = true;
  9582. }
  9583. const type = this.tsTryParseTypeAnnotation();
  9584. if (type) {
  9585. decl.id.typeAnnotation = type;
  9586. this.resetEndLocation(decl.id);
  9587. }
  9588. }
  9589. parseAsyncArrowFromCallExpression(node, call) {
  9590. if (this.match(14)) {
  9591. node.returnType = this.tsParseTypeAnnotation();
  9592. }
  9593. return super.parseAsyncArrowFromCallExpression(node, call);
  9594. }
  9595. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9596. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  9597. let state;
  9598. let jsx;
  9599. let typeCast;
  9600. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  9601. state = this.state.clone();
  9602. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9603. if (!jsx.error) return jsx.node;
  9604. const {
  9605. context
  9606. } = this.state;
  9607. const currentContext = context[context.length - 1];
  9608. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9609. context.pop();
  9610. }
  9611. }
  9612. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9613. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9614. }
  9615. if (!state || state === this.state) state = this.state.clone();
  9616. let typeParameters;
  9617. const arrow = this.tryParse(abort => {
  9618. var _expr$extra, _typeParameters;
  9619. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9620. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9621. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9622. abort();
  9623. }
  9624. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9625. this.resetStartLocationFromNode(expr, typeParameters);
  9626. }
  9627. expr.typeParameters = typeParameters;
  9628. return expr;
  9629. }, state);
  9630. if (!arrow.error && !arrow.aborted) {
  9631. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9632. return arrow.node;
  9633. }
  9634. if (!jsx) {
  9635. assert(!this.hasPlugin("jsx"));
  9636. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9637. if (!typeCast.error) return typeCast.node;
  9638. }
  9639. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9640. this.state = jsx.failState;
  9641. return jsx.node;
  9642. }
  9643. if (arrow.node) {
  9644. this.state = arrow.failState;
  9645. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9646. return arrow.node;
  9647. }
  9648. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9649. this.state = typeCast.failState;
  9650. return typeCast.node;
  9651. }
  9652. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  9653. if (arrow.thrown) throw arrow.error;
  9654. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  9655. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  9656. }
  9657. reportReservedArrowTypeParam(node) {
  9658. var _node$extra;
  9659. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9660. this.raise(TSErrors.ReservedArrowTypeParam, {
  9661. at: node
  9662. });
  9663. }
  9664. }
  9665. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9666. if (!this.hasPlugin("jsx") && this.match(47)) {
  9667. return this.tsParseTypeAssertion();
  9668. } else {
  9669. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9670. }
  9671. }
  9672. parseArrow(node) {
  9673. if (this.match(14)) {
  9674. const result = this.tryParse(abort => {
  9675. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9676. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9677. return returnType;
  9678. });
  9679. if (result.aborted) return;
  9680. if (!result.thrown) {
  9681. if (result.error) this.state = result.failState;
  9682. node.returnType = result.node;
  9683. }
  9684. }
  9685. return super.parseArrow(node);
  9686. }
  9687. parseAssignableListItemTypes(param, flags) {
  9688. if (!(flags & ParseBindingListFlags.IS_FUNCTION_PARAMS)) return param;
  9689. if (this.eat(17)) {
  9690. param.optional = true;
  9691. }
  9692. const type = this.tsTryParseTypeAnnotation();
  9693. if (type) param.typeAnnotation = type;
  9694. this.resetEndLocation(param);
  9695. return param;
  9696. }
  9697. isAssignable(node, isBinding) {
  9698. switch (node.type) {
  9699. case "TSTypeCastExpression":
  9700. return this.isAssignable(node.expression, isBinding);
  9701. case "TSParameterProperty":
  9702. return true;
  9703. default:
  9704. return super.isAssignable(node, isBinding);
  9705. }
  9706. }
  9707. toAssignable(node, isLHS = false) {
  9708. switch (node.type) {
  9709. case "ParenthesizedExpression":
  9710. this.toAssignableParenthesizedExpression(node, isLHS);
  9711. break;
  9712. case "TSAsExpression":
  9713. case "TSSatisfiesExpression":
  9714. case "TSNonNullExpression":
  9715. case "TSTypeAssertion":
  9716. if (isLHS) {
  9717. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9718. at: node
  9719. });
  9720. } else {
  9721. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9722. at: node
  9723. });
  9724. }
  9725. this.toAssignable(node.expression, isLHS);
  9726. break;
  9727. case "AssignmentExpression":
  9728. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9729. node.left = this.typeCastToParameter(node.left);
  9730. }
  9731. default:
  9732. super.toAssignable(node, isLHS);
  9733. }
  9734. }
  9735. toAssignableParenthesizedExpression(node, isLHS) {
  9736. switch (node.expression.type) {
  9737. case "TSAsExpression":
  9738. case "TSSatisfiesExpression":
  9739. case "TSNonNullExpression":
  9740. case "TSTypeAssertion":
  9741. case "ParenthesizedExpression":
  9742. this.toAssignable(node.expression, isLHS);
  9743. break;
  9744. default:
  9745. super.toAssignable(node, isLHS);
  9746. }
  9747. }
  9748. checkToRestConversion(node, allowPattern) {
  9749. switch (node.type) {
  9750. case "TSAsExpression":
  9751. case "TSSatisfiesExpression":
  9752. case "TSTypeAssertion":
  9753. case "TSNonNullExpression":
  9754. this.checkToRestConversion(node.expression, false);
  9755. break;
  9756. default:
  9757. super.checkToRestConversion(node, allowPattern);
  9758. }
  9759. }
  9760. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9761. return getOwn({
  9762. TSTypeCastExpression: true,
  9763. TSParameterProperty: "parameter",
  9764. TSNonNullExpression: "expression",
  9765. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9766. TSSatisfiesExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9767. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9768. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9769. }
  9770. parseBindingAtom() {
  9771. switch (this.state.type) {
  9772. case 78:
  9773. return this.parseIdentifier(true);
  9774. default:
  9775. return super.parseBindingAtom();
  9776. }
  9777. }
  9778. parseMaybeDecoratorArguments(expr) {
  9779. if (this.match(47) || this.match(51)) {
  9780. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9781. if (this.match(10)) {
  9782. const call = super.parseMaybeDecoratorArguments(expr);
  9783. call.typeParameters = typeArguments;
  9784. return call;
  9785. }
  9786. this.unexpected(null, 10);
  9787. }
  9788. return super.parseMaybeDecoratorArguments(expr);
  9789. }
  9790. checkCommaAfterRest(close) {
  9791. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9792. this.next();
  9793. return false;
  9794. } else {
  9795. return super.checkCommaAfterRest(close);
  9796. }
  9797. }
  9798. isClassMethod() {
  9799. return this.match(47) || super.isClassMethod();
  9800. }
  9801. isClassProperty() {
  9802. return this.match(35) || this.match(14) || super.isClassProperty();
  9803. }
  9804. parseMaybeDefault(startLoc, left) {
  9805. const node = super.parseMaybeDefault(startLoc, left);
  9806. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9807. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9808. at: node.typeAnnotation
  9809. });
  9810. }
  9811. return node;
  9812. }
  9813. getTokenFromCode(code) {
  9814. if (this.state.inType) {
  9815. if (code === 62) {
  9816. return this.finishOp(48, 1);
  9817. }
  9818. if (code === 60) {
  9819. return this.finishOp(47, 1);
  9820. }
  9821. }
  9822. return super.getTokenFromCode(code);
  9823. }
  9824. reScan_lt_gt() {
  9825. const {
  9826. type
  9827. } = this.state;
  9828. if (type === 47) {
  9829. this.state.pos -= 1;
  9830. this.readToken_lt();
  9831. } else if (type === 48) {
  9832. this.state.pos -= 1;
  9833. this.readToken_gt();
  9834. }
  9835. }
  9836. reScan_lt() {
  9837. const {
  9838. type
  9839. } = this.state;
  9840. if (type === 51) {
  9841. this.state.pos -= 2;
  9842. this.finishOp(47, 1);
  9843. return 47;
  9844. }
  9845. return type;
  9846. }
  9847. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9848. for (let i = 0; i < exprList.length; i++) {
  9849. const expr = exprList[i];
  9850. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9851. exprList[i] = this.typeCastToParameter(expr);
  9852. }
  9853. }
  9854. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9855. }
  9856. typeCastToParameter(node) {
  9857. node.expression.typeAnnotation = node.typeAnnotation;
  9858. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9859. return node.expression;
  9860. }
  9861. shouldParseArrow(params) {
  9862. if (this.match(14)) {
  9863. return params.every(expr => this.isAssignable(expr, true));
  9864. }
  9865. return super.shouldParseArrow(params);
  9866. }
  9867. shouldParseAsyncArrow() {
  9868. return this.match(14) || super.shouldParseAsyncArrow();
  9869. }
  9870. canHaveLeadingDecorator() {
  9871. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9872. }
  9873. jsxParseOpeningElementAfterName(node) {
  9874. if (this.match(47) || this.match(51)) {
  9875. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9876. if (typeArguments) node.typeParameters = typeArguments;
  9877. }
  9878. return super.jsxParseOpeningElementAfterName(node);
  9879. }
  9880. getGetterSetterExpectedParamCount(method) {
  9881. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9882. const params = this.getObjectOrClassMethodParams(method);
  9883. const firstParam = params[0];
  9884. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9885. return hasContextParam ? baseCount + 1 : baseCount;
  9886. }
  9887. parseCatchClauseParam() {
  9888. const param = super.parseCatchClauseParam();
  9889. const type = this.tsTryParseTypeAnnotation();
  9890. if (type) {
  9891. param.typeAnnotation = type;
  9892. this.resetEndLocation(param);
  9893. }
  9894. return param;
  9895. }
  9896. tsInAmbientContext(cb) {
  9897. const oldIsAmbientContext = this.state.isAmbientContext;
  9898. this.state.isAmbientContext = true;
  9899. try {
  9900. return cb();
  9901. } finally {
  9902. this.state.isAmbientContext = oldIsAmbientContext;
  9903. }
  9904. }
  9905. parseClass(node, isStatement, optionalId) {
  9906. const oldInAbstractClass = this.state.inAbstractClass;
  9907. this.state.inAbstractClass = !!node.abstract;
  9908. try {
  9909. return super.parseClass(node, isStatement, optionalId);
  9910. } finally {
  9911. this.state.inAbstractClass = oldInAbstractClass;
  9912. }
  9913. }
  9914. tsParseAbstractDeclaration(node, decorators) {
  9915. if (this.match(80)) {
  9916. node.abstract = true;
  9917. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  9918. } else if (this.isContextual(127)) {
  9919. if (!this.hasFollowingLineBreak()) {
  9920. node.abstract = true;
  9921. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9922. at: node
  9923. });
  9924. return this.tsParseInterfaceDeclaration(node);
  9925. }
  9926. } else {
  9927. this.unexpected(null, 80);
  9928. }
  9929. }
  9930. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9931. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9932. if (method.abstract) {
  9933. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9934. if (hasBody) {
  9935. const {
  9936. key
  9937. } = method;
  9938. this.raise(TSErrors.AbstractMethodHasImplementation, {
  9939. at: method,
  9940. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9941. });
  9942. }
  9943. }
  9944. return method;
  9945. }
  9946. tsParseTypeParameterName() {
  9947. const typeName = this.parseIdentifier();
  9948. return typeName.name;
  9949. }
  9950. shouldParseAsAmbientContext() {
  9951. return !!this.getPluginOption("typescript", "dts");
  9952. }
  9953. parse() {
  9954. if (this.shouldParseAsAmbientContext()) {
  9955. this.state.isAmbientContext = true;
  9956. }
  9957. return super.parse();
  9958. }
  9959. getExpression() {
  9960. if (this.shouldParseAsAmbientContext()) {
  9961. this.state.isAmbientContext = true;
  9962. }
  9963. return super.getExpression();
  9964. }
  9965. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9966. if (!isString && isMaybeTypeOnly) {
  9967. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9968. return this.finishNode(node, "ExportSpecifier");
  9969. }
  9970. node.exportKind = "value";
  9971. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9972. }
  9973. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  9974. if (!importedIsString && isMaybeTypeOnly) {
  9975. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9976. return this.finishNode(specifier, "ImportSpecifier");
  9977. }
  9978. specifier.importKind = "value";
  9979. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  9980. }
  9981. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9982. const leftOfAsKey = isImport ? "imported" : "local";
  9983. const rightOfAsKey = isImport ? "local" : "exported";
  9984. let leftOfAs = node[leftOfAsKey];
  9985. let rightOfAs;
  9986. let hasTypeSpecifier = false;
  9987. let canParseAsKeyword = true;
  9988. const loc = leftOfAs.loc.start;
  9989. if (this.isContextual(93)) {
  9990. const firstAs = this.parseIdentifier();
  9991. if (this.isContextual(93)) {
  9992. const secondAs = this.parseIdentifier();
  9993. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9994. hasTypeSpecifier = true;
  9995. leftOfAs = firstAs;
  9996. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9997. canParseAsKeyword = false;
  9998. } else {
  9999. rightOfAs = secondAs;
  10000. canParseAsKeyword = false;
  10001. }
  10002. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10003. canParseAsKeyword = false;
  10004. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10005. } else {
  10006. hasTypeSpecifier = true;
  10007. leftOfAs = firstAs;
  10008. }
  10009. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10010. hasTypeSpecifier = true;
  10011. if (isImport) {
  10012. leftOfAs = this.parseIdentifier(true);
  10013. if (!this.isContextual(93)) {
  10014. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  10015. }
  10016. } else {
  10017. leftOfAs = this.parseModuleExportName();
  10018. }
  10019. }
  10020. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  10021. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  10022. at: loc
  10023. });
  10024. }
  10025. node[leftOfAsKey] = leftOfAs;
  10026. node[rightOfAsKey] = rightOfAs;
  10027. const kindKey = isImport ? "importKind" : "exportKind";
  10028. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  10029. if (canParseAsKeyword && this.eatContextual(93)) {
  10030. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10031. }
  10032. if (!node[rightOfAsKey]) {
  10033. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  10034. }
  10035. if (isImport) {
  10036. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  10037. }
  10038. }
  10039. });
  10040. function isPossiblyLiteralEnum(expression) {
  10041. if (expression.type !== "MemberExpression") return false;
  10042. const {
  10043. computed,
  10044. property
  10045. } = expression;
  10046. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  10047. return false;
  10048. }
  10049. return isUncomputedMemberExpressionChain(expression.object);
  10050. }
  10051. function isValidAmbientConstInitializer(expression, estree) {
  10052. var _expression$extra;
  10053. const {
  10054. type
  10055. } = expression;
  10056. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  10057. return false;
  10058. }
  10059. if (estree) {
  10060. if (type === "Literal") {
  10061. const {
  10062. value
  10063. } = expression;
  10064. if (typeof value === "string" || typeof value === "boolean") {
  10065. return true;
  10066. }
  10067. }
  10068. } else {
  10069. if (type === "StringLiteral" || type === "BooleanLiteral") {
  10070. return true;
  10071. }
  10072. }
  10073. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  10074. return true;
  10075. }
  10076. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  10077. return true;
  10078. }
  10079. if (isPossiblyLiteralEnum(expression)) {
  10080. return true;
  10081. }
  10082. return false;
  10083. }
  10084. function isNumber(expression, estree) {
  10085. if (estree) {
  10086. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  10087. } else {
  10088. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  10089. }
  10090. }
  10091. function isNegativeNumber(expression, estree) {
  10092. if (expression.type === "UnaryExpression") {
  10093. const {
  10094. operator,
  10095. argument
  10096. } = expression;
  10097. if (operator === "-" && isNumber(argument, estree)) {
  10098. return true;
  10099. }
  10100. }
  10101. return false;
  10102. }
  10103. function isUncomputedMemberExpressionChain(expression) {
  10104. if (expression.type === "Identifier") return true;
  10105. if (expression.type !== "MemberExpression") return false;
  10106. if (expression.computed) return false;
  10107. return isUncomputedMemberExpressionChain(expression.object);
  10108. }
  10109. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  10110. ClassNameIsRequired: "A class name is required.",
  10111. UnexpectedSpace: "Unexpected space in placeholder."
  10112. });
  10113. var placeholders = (superClass => class PlaceholdersParserMixin extends superClass {
  10114. parsePlaceholder(expectedNode) {
  10115. if (this.match(142)) {
  10116. const node = this.startNode();
  10117. this.next();
  10118. this.assertNoSpace();
  10119. node.name = super.parseIdentifier(true);
  10120. this.assertNoSpace();
  10121. this.expect(142);
  10122. return this.finishPlaceholder(node, expectedNode);
  10123. }
  10124. }
  10125. finishPlaceholder(node, expectedNode) {
  10126. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  10127. node.expectedNode = expectedNode;
  10128. return isFinished ? node : this.finishNode(node, "Placeholder");
  10129. }
  10130. getTokenFromCode(code) {
  10131. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  10132. return this.finishOp(142, 2);
  10133. }
  10134. return super.getTokenFromCode(code);
  10135. }
  10136. parseExprAtom(refExpressionErrors) {
  10137. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  10138. }
  10139. parseIdentifier(liberal) {
  10140. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  10141. }
  10142. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10143. if (word !== undefined) {
  10144. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  10145. }
  10146. }
  10147. parseBindingAtom() {
  10148. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  10149. }
  10150. isValidLVal(type, isParenthesized, binding) {
  10151. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  10152. }
  10153. toAssignable(node, isLHS) {
  10154. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  10155. node.expectedNode = "Pattern";
  10156. } else {
  10157. super.toAssignable(node, isLHS);
  10158. }
  10159. }
  10160. chStartsBindingIdentifier(ch, pos) {
  10161. if (super.chStartsBindingIdentifier(ch, pos)) {
  10162. return true;
  10163. }
  10164. const nextToken = this.lookahead();
  10165. if (nextToken.type === 142) {
  10166. return true;
  10167. }
  10168. return false;
  10169. }
  10170. verifyBreakContinue(node, isBreak) {
  10171. if (node.label && node.label.type === "Placeholder") return;
  10172. super.verifyBreakContinue(node, isBreak);
  10173. }
  10174. parseExpressionStatement(node, expr) {
  10175. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  10176. return super.parseExpressionStatement(node, expr);
  10177. }
  10178. if (this.match(14)) {
  10179. const stmt = node;
  10180. stmt.label = this.finishPlaceholder(expr, "Identifier");
  10181. this.next();
  10182. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  10183. return this.finishNode(stmt, "LabeledStatement");
  10184. }
  10185. this.semicolon();
  10186. node.name = expr.name;
  10187. return this.finishPlaceholder(node, "Statement");
  10188. }
  10189. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  10190. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  10191. }
  10192. parseFunctionId(requireId) {
  10193. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  10194. }
  10195. parseClass(node, isStatement, optionalId) {
  10196. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  10197. this.next();
  10198. const oldStrict = this.state.strict;
  10199. const placeholder = this.parsePlaceholder("Identifier");
  10200. if (placeholder) {
  10201. if (this.match(81) || this.match(142) || this.match(5)) {
  10202. node.id = placeholder;
  10203. } else if (optionalId || !isStatement) {
  10204. node.id = null;
  10205. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  10206. return this.finishNode(node, type);
  10207. } else {
  10208. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  10209. at: this.state.startLoc
  10210. });
  10211. }
  10212. } else {
  10213. this.parseClassId(node, isStatement, optionalId);
  10214. }
  10215. super.parseClassSuper(node);
  10216. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  10217. return this.finishNode(node, type);
  10218. }
  10219. parseExport(node, decorators) {
  10220. const placeholder = this.parsePlaceholder("Identifier");
  10221. if (!placeholder) return super.parseExport(node, decorators);
  10222. if (!this.isContextual(97) && !this.match(12)) {
  10223. node.specifiers = [];
  10224. node.source = null;
  10225. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  10226. return this.finishNode(node, "ExportNamedDeclaration");
  10227. }
  10228. this.expectPlugin("exportDefaultFrom");
  10229. const specifier = this.startNode();
  10230. specifier.exported = placeholder;
  10231. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  10232. return super.parseExport(node, decorators);
  10233. }
  10234. isExportDefaultSpecifier() {
  10235. if (this.match(65)) {
  10236. const next = this.nextTokenStart();
  10237. if (this.isUnparsedContextual(next, "from")) {
  10238. if (this.input.startsWith(tokenLabelName(142), this.nextTokenStartSince(next + 4))) {
  10239. return true;
  10240. }
  10241. }
  10242. }
  10243. return super.isExportDefaultSpecifier();
  10244. }
  10245. maybeParseExportDefaultSpecifier(node) {
  10246. if (node.specifiers && node.specifiers.length > 0) {
  10247. return true;
  10248. }
  10249. return super.maybeParseExportDefaultSpecifier(node);
  10250. }
  10251. checkExport(node) {
  10252. const {
  10253. specifiers
  10254. } = node;
  10255. if (specifiers != null && specifiers.length) {
  10256. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10257. }
  10258. super.checkExport(node);
  10259. node.specifiers = specifiers;
  10260. }
  10261. parseImport(node) {
  10262. const placeholder = this.parsePlaceholder("Identifier");
  10263. if (!placeholder) return super.parseImport(node);
  10264. node.specifiers = [];
  10265. if (!this.isContextual(97) && !this.match(12)) {
  10266. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10267. this.semicolon();
  10268. return this.finishNode(node, "ImportDeclaration");
  10269. }
  10270. const specifier = this.startNodeAtNode(placeholder);
  10271. specifier.local = placeholder;
  10272. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10273. if (this.eat(12)) {
  10274. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10275. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10276. }
  10277. this.expectContextual(97);
  10278. node.source = this.parseImportSource();
  10279. this.semicolon();
  10280. return this.finishNode(node, "ImportDeclaration");
  10281. }
  10282. parseImportSource() {
  10283. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10284. }
  10285. assertNoSpace() {
  10286. if (this.state.start > this.state.lastTokEndLoc.index) {
  10287. this.raise(PlaceholderErrors.UnexpectedSpace, {
  10288. at: this.state.lastTokEndLoc
  10289. });
  10290. }
  10291. }
  10292. });
  10293. var v8intrinsic = (superClass => class V8IntrinsicMixin extends superClass {
  10294. parseV8Intrinsic() {
  10295. if (this.match(54)) {
  10296. const v8IntrinsicStartLoc = this.state.startLoc;
  10297. const node = this.startNode();
  10298. this.next();
  10299. if (tokenIsIdentifier(this.state.type)) {
  10300. const name = this.parseIdentifierName();
  10301. const identifier = this.createIdentifier(node, name);
  10302. identifier.type = "V8IntrinsicIdentifier";
  10303. if (this.match(10)) {
  10304. return identifier;
  10305. }
  10306. }
  10307. this.unexpected(v8IntrinsicStartLoc);
  10308. }
  10309. }
  10310. parseExprAtom(refExpressionErrors) {
  10311. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10312. }
  10313. });
  10314. function hasPlugin(plugins, expectedConfig) {
  10315. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  10316. const expectedKeys = Object.keys(expectedOptions);
  10317. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  10318. return plugins.some(p => {
  10319. if (typeof p === "string") {
  10320. return expectedOptionsIsEmpty && p === expectedName;
  10321. } else {
  10322. const [pluginName, pluginOptions] = p;
  10323. if (pluginName !== expectedName) {
  10324. return false;
  10325. }
  10326. for (const key of expectedKeys) {
  10327. if (pluginOptions[key] !== expectedOptions[key]) {
  10328. return false;
  10329. }
  10330. }
  10331. return true;
  10332. }
  10333. });
  10334. }
  10335. function getPluginOption(plugins, name, option) {
  10336. const plugin = plugins.find(plugin => {
  10337. if (Array.isArray(plugin)) {
  10338. return plugin[0] === name;
  10339. } else {
  10340. return plugin === name;
  10341. }
  10342. });
  10343. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  10344. return plugin[1][option];
  10345. }
  10346. return null;
  10347. }
  10348. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10349. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10350. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10351. function validatePlugins(plugins) {
  10352. if (hasPlugin(plugins, "decorators")) {
  10353. if (hasPlugin(plugins, "decorators-legacy")) {
  10354. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10355. }
  10356. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  10357. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10358. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10359. }
  10360. const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
  10361. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10362. throw new Error("'allowCallParenthesized' must be a boolean.");
  10363. }
  10364. }
  10365. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  10366. throw new Error("Cannot combine flow and typescript plugins.");
  10367. }
  10368. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  10369. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10370. }
  10371. if (hasPlugin(plugins, "pipelineOperator")) {
  10372. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  10373. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10374. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10375. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10376. }
  10377. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  10378. syntaxType: "hash"
  10379. }]);
  10380. if (proposal === "hack") {
  10381. if (hasPlugin(plugins, "placeholders")) {
  10382. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10383. }
  10384. if (hasPlugin(plugins, "v8intrinsic")) {
  10385. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10386. }
  10387. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  10388. if (!TOPIC_TOKENS.includes(topicToken)) {
  10389. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10390. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10391. }
  10392. if (topicToken === "#" && tupleSyntaxIsHash) {
  10393. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10394. }
  10395. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  10396. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10397. }
  10398. }
  10399. if (hasPlugin(plugins, "moduleAttributes")) {
  10400. {
  10401. if (hasPlugin(plugins, "importAssertions")) {
  10402. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  10403. }
  10404. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  10405. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10406. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10407. }
  10408. }
  10409. }
  10410. if (hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") != null && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  10411. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10412. }
  10413. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  10414. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10415. error.missingPlugins = "doExpressions";
  10416. throw error;
  10417. }
  10418. }
  10419. const mixinPlugins = {
  10420. estree,
  10421. jsx,
  10422. flow,
  10423. typescript,
  10424. v8intrinsic,
  10425. placeholders
  10426. };
  10427. const mixinPluginNames = Object.keys(mixinPlugins);
  10428. const defaultOptions = {
  10429. sourceType: "script",
  10430. sourceFilename: undefined,
  10431. startColumn: 0,
  10432. startLine: 1,
  10433. allowAwaitOutsideFunction: false,
  10434. allowReturnOutsideFunction: false,
  10435. allowNewTargetOutsideFunction: false,
  10436. allowImportExportEverywhere: false,
  10437. allowSuperOutsideMethod: false,
  10438. allowUndeclaredExports: false,
  10439. plugins: [],
  10440. strictMode: null,
  10441. ranges: false,
  10442. tokens: false,
  10443. createParenthesizedExpressions: false,
  10444. errorRecovery: false,
  10445. attachComment: true,
  10446. annexB: true
  10447. };
  10448. function getOptions(opts) {
  10449. if (opts && opts.annexB != null && opts.annexB !== false) {
  10450. throw new Error("The `annexB` option can only be set to `false`.");
  10451. }
  10452. const options = {};
  10453. for (const key of Object.keys(defaultOptions)) {
  10454. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  10455. }
  10456. return options;
  10457. }
  10458. class ExpressionParser extends LValParser {
  10459. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10460. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10461. return;
  10462. }
  10463. const key = prop.key;
  10464. const name = key.type === "Identifier" ? key.name : key.value;
  10465. if (name === "__proto__") {
  10466. if (isRecord) {
  10467. this.raise(Errors.RecordNoProto, {
  10468. at: key
  10469. });
  10470. return;
  10471. }
  10472. if (protoRef.used) {
  10473. if (refExpressionErrors) {
  10474. if (refExpressionErrors.doubleProtoLoc === null) {
  10475. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10476. }
  10477. } else {
  10478. this.raise(Errors.DuplicateProto, {
  10479. at: key
  10480. });
  10481. }
  10482. }
  10483. protoRef.used = true;
  10484. }
  10485. }
  10486. shouldExitDescending(expr, potentialArrowAt) {
  10487. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10488. }
  10489. getExpression() {
  10490. this.enterInitialScopes();
  10491. this.nextToken();
  10492. const expr = this.parseExpression();
  10493. if (!this.match(137)) {
  10494. this.unexpected();
  10495. }
  10496. this.finalizeRemainingComments();
  10497. expr.comments = this.state.comments;
  10498. expr.errors = this.state.errors;
  10499. if (this.options.tokens) {
  10500. expr.tokens = this.tokens;
  10501. }
  10502. return expr;
  10503. }
  10504. parseExpression(disallowIn, refExpressionErrors) {
  10505. if (disallowIn) {
  10506. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10507. }
  10508. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10509. }
  10510. parseExpressionBase(refExpressionErrors) {
  10511. const startLoc = this.state.startLoc;
  10512. const expr = this.parseMaybeAssign(refExpressionErrors);
  10513. if (this.match(12)) {
  10514. const node = this.startNodeAt(startLoc);
  10515. node.expressions = [expr];
  10516. while (this.eat(12)) {
  10517. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10518. }
  10519. this.toReferencedList(node.expressions);
  10520. return this.finishNode(node, "SequenceExpression");
  10521. }
  10522. return expr;
  10523. }
  10524. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10525. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10526. }
  10527. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10528. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10529. }
  10530. setOptionalParametersError(refExpressionErrors, resultError) {
  10531. var _resultError$loc;
  10532. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10533. }
  10534. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10535. const startLoc = this.state.startLoc;
  10536. if (this.isContextual(106)) {
  10537. if (this.prodParam.hasYield) {
  10538. let left = this.parseYield();
  10539. if (afterLeftParse) {
  10540. left = afterLeftParse.call(this, left, startLoc);
  10541. }
  10542. return left;
  10543. }
  10544. }
  10545. let ownExpressionErrors;
  10546. if (refExpressionErrors) {
  10547. ownExpressionErrors = false;
  10548. } else {
  10549. refExpressionErrors = new ExpressionErrors();
  10550. ownExpressionErrors = true;
  10551. }
  10552. const {
  10553. type
  10554. } = this.state;
  10555. if (type === 10 || tokenIsIdentifier(type)) {
  10556. this.state.potentialArrowAt = this.state.start;
  10557. }
  10558. let left = this.parseMaybeConditional(refExpressionErrors);
  10559. if (afterLeftParse) {
  10560. left = afterLeftParse.call(this, left, startLoc);
  10561. }
  10562. if (tokenIsAssignment(this.state.type)) {
  10563. const node = this.startNodeAt(startLoc);
  10564. const operator = this.state.value;
  10565. node.operator = operator;
  10566. if (this.match(29)) {
  10567. this.toAssignable(left, true);
  10568. node.left = left;
  10569. const startIndex = startLoc.index;
  10570. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10571. refExpressionErrors.doubleProtoLoc = null;
  10572. }
  10573. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10574. refExpressionErrors.shorthandAssignLoc = null;
  10575. }
  10576. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10577. this.checkDestructuringPrivate(refExpressionErrors);
  10578. refExpressionErrors.privateKeyLoc = null;
  10579. }
  10580. } else {
  10581. node.left = left;
  10582. }
  10583. this.next();
  10584. node.right = this.parseMaybeAssign();
  10585. this.checkLVal(left, {
  10586. in: this.finishNode(node, "AssignmentExpression")
  10587. });
  10588. return node;
  10589. } else if (ownExpressionErrors) {
  10590. this.checkExpressionErrors(refExpressionErrors, true);
  10591. }
  10592. return left;
  10593. }
  10594. parseMaybeConditional(refExpressionErrors) {
  10595. const startLoc = this.state.startLoc;
  10596. const potentialArrowAt = this.state.potentialArrowAt;
  10597. const expr = this.parseExprOps(refExpressionErrors);
  10598. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10599. return expr;
  10600. }
  10601. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10602. }
  10603. parseConditional(expr, startLoc, refExpressionErrors) {
  10604. if (this.eat(17)) {
  10605. const node = this.startNodeAt(startLoc);
  10606. node.test = expr;
  10607. node.consequent = this.parseMaybeAssignAllowIn();
  10608. this.expect(14);
  10609. node.alternate = this.parseMaybeAssign();
  10610. return this.finishNode(node, "ConditionalExpression");
  10611. }
  10612. return expr;
  10613. }
  10614. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10615. return this.match(136) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10616. }
  10617. parseExprOps(refExpressionErrors) {
  10618. const startLoc = this.state.startLoc;
  10619. const potentialArrowAt = this.state.potentialArrowAt;
  10620. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10621. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10622. return expr;
  10623. }
  10624. return this.parseExprOp(expr, startLoc, -1);
  10625. }
  10626. parseExprOp(left, leftStartLoc, minPrec) {
  10627. if (this.isPrivateName(left)) {
  10628. const value = this.getPrivateNameSV(left);
  10629. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10630. this.raise(Errors.PrivateInExpectedIn, {
  10631. at: left,
  10632. identifierName: value
  10633. });
  10634. }
  10635. this.classScope.usePrivateName(value, left.loc.start);
  10636. }
  10637. const op = this.state.type;
  10638. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10639. let prec = tokenOperatorPrecedence(op);
  10640. if (prec > minPrec) {
  10641. if (op === 39) {
  10642. this.expectPlugin("pipelineOperator");
  10643. if (this.state.inFSharpPipelineDirectBody) {
  10644. return left;
  10645. }
  10646. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10647. }
  10648. const node = this.startNodeAt(leftStartLoc);
  10649. node.left = left;
  10650. node.operator = this.state.value;
  10651. const logical = op === 41 || op === 42;
  10652. const coalesce = op === 40;
  10653. if (coalesce) {
  10654. prec = tokenOperatorPrecedence(42);
  10655. }
  10656. this.next();
  10657. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10658. proposal: "minimal"
  10659. }])) {
  10660. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10661. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10662. at: this.state.startLoc
  10663. });
  10664. }
  10665. }
  10666. node.right = this.parseExprOpRightExpr(op, prec);
  10667. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10668. const nextOp = this.state.type;
  10669. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10670. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10671. at: this.state.startLoc
  10672. });
  10673. }
  10674. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10675. }
  10676. }
  10677. return left;
  10678. }
  10679. parseExprOpRightExpr(op, prec) {
  10680. const startLoc = this.state.startLoc;
  10681. switch (op) {
  10682. case 39:
  10683. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10684. case "hack":
  10685. return this.withTopicBindingContext(() => {
  10686. return this.parseHackPipeBody();
  10687. });
  10688. case "smart":
  10689. return this.withTopicBindingContext(() => {
  10690. if (this.prodParam.hasYield && this.isContextual(106)) {
  10691. throw this.raise(Errors.PipeBodyIsTighter, {
  10692. at: this.state.startLoc
  10693. });
  10694. }
  10695. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10696. });
  10697. case "fsharp":
  10698. return this.withSoloAwaitPermittingContext(() => {
  10699. return this.parseFSharpPipelineBody(prec);
  10700. });
  10701. }
  10702. default:
  10703. return this.parseExprOpBaseRightExpr(op, prec);
  10704. }
  10705. }
  10706. parseExprOpBaseRightExpr(op, prec) {
  10707. const startLoc = this.state.startLoc;
  10708. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10709. }
  10710. parseHackPipeBody() {
  10711. var _body$extra;
  10712. const {
  10713. startLoc
  10714. } = this.state;
  10715. const body = this.parseMaybeAssign();
  10716. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10717. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10718. this.raise(Errors.PipeUnparenthesizedBody, {
  10719. at: startLoc,
  10720. type: body.type
  10721. });
  10722. }
  10723. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10724. this.raise(Errors.PipeTopicUnused, {
  10725. at: startLoc
  10726. });
  10727. }
  10728. return body;
  10729. }
  10730. checkExponentialAfterUnary(node) {
  10731. if (this.match(57)) {
  10732. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10733. at: node.argument
  10734. });
  10735. }
  10736. }
  10737. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10738. const startLoc = this.state.startLoc;
  10739. const isAwait = this.isContextual(96);
  10740. if (isAwait && this.isAwaitAllowed()) {
  10741. this.next();
  10742. const expr = this.parseAwait(startLoc);
  10743. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10744. return expr;
  10745. }
  10746. const update = this.match(34);
  10747. const node = this.startNode();
  10748. if (tokenIsPrefix(this.state.type)) {
  10749. node.operator = this.state.value;
  10750. node.prefix = true;
  10751. if (this.match(72)) {
  10752. this.expectPlugin("throwExpressions");
  10753. }
  10754. const isDelete = this.match(89);
  10755. this.next();
  10756. node.argument = this.parseMaybeUnary(null, true);
  10757. this.checkExpressionErrors(refExpressionErrors, true);
  10758. if (this.state.strict && isDelete) {
  10759. const arg = node.argument;
  10760. if (arg.type === "Identifier") {
  10761. this.raise(Errors.StrictDelete, {
  10762. at: node
  10763. });
  10764. } else if (this.hasPropertyAsPrivateName(arg)) {
  10765. this.raise(Errors.DeletePrivateField, {
  10766. at: node
  10767. });
  10768. }
  10769. }
  10770. if (!update) {
  10771. if (!sawUnary) {
  10772. this.checkExponentialAfterUnary(node);
  10773. }
  10774. return this.finishNode(node, "UnaryExpression");
  10775. }
  10776. }
  10777. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10778. if (isAwait) {
  10779. const {
  10780. type
  10781. } = this.state;
  10782. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10783. if (startsExpr && !this.isAmbiguousAwait()) {
  10784. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10785. at: startLoc
  10786. });
  10787. return this.parseAwait(startLoc);
  10788. }
  10789. }
  10790. return expr;
  10791. }
  10792. parseUpdate(node, update, refExpressionErrors) {
  10793. if (update) {
  10794. const updateExpressionNode = node;
  10795. this.checkLVal(updateExpressionNode.argument, {
  10796. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10797. });
  10798. return node;
  10799. }
  10800. const startLoc = this.state.startLoc;
  10801. let expr = this.parseExprSubscripts(refExpressionErrors);
  10802. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10803. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10804. const node = this.startNodeAt(startLoc);
  10805. node.operator = this.state.value;
  10806. node.prefix = false;
  10807. node.argument = expr;
  10808. this.next();
  10809. this.checkLVal(expr, {
  10810. in: expr = this.finishNode(node, "UpdateExpression")
  10811. });
  10812. }
  10813. return expr;
  10814. }
  10815. parseExprSubscripts(refExpressionErrors) {
  10816. const startLoc = this.state.startLoc;
  10817. const potentialArrowAt = this.state.potentialArrowAt;
  10818. const expr = this.parseExprAtom(refExpressionErrors);
  10819. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10820. return expr;
  10821. }
  10822. return this.parseSubscripts(expr, startLoc);
  10823. }
  10824. parseSubscripts(base, startLoc, noCalls) {
  10825. const state = {
  10826. optionalChainMember: false,
  10827. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10828. stop: false
  10829. };
  10830. do {
  10831. base = this.parseSubscript(base, startLoc, noCalls, state);
  10832. state.maybeAsyncArrow = false;
  10833. } while (!state.stop);
  10834. return base;
  10835. }
  10836. parseSubscript(base, startLoc, noCalls, state) {
  10837. const {
  10838. type
  10839. } = this.state;
  10840. if (!noCalls && type === 15) {
  10841. return this.parseBind(base, startLoc, noCalls, state);
  10842. } else if (tokenIsTemplate(type)) {
  10843. return this.parseTaggedTemplateExpression(base, startLoc, state);
  10844. }
  10845. let optional = false;
  10846. if (type === 18) {
  10847. if (noCalls) {
  10848. this.raise(Errors.OptionalChainingNoNew, {
  10849. at: this.state.startLoc
  10850. });
  10851. if (this.lookaheadCharCode() === 40) {
  10852. state.stop = true;
  10853. return base;
  10854. }
  10855. }
  10856. state.optionalChainMember = optional = true;
  10857. this.next();
  10858. }
  10859. if (!noCalls && this.match(10)) {
  10860. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  10861. } else {
  10862. const computed = this.eat(0);
  10863. if (computed || optional || this.eat(16)) {
  10864. return this.parseMember(base, startLoc, state, computed, optional);
  10865. } else {
  10866. state.stop = true;
  10867. return base;
  10868. }
  10869. }
  10870. }
  10871. parseMember(base, startLoc, state, computed, optional) {
  10872. const node = this.startNodeAt(startLoc);
  10873. node.object = base;
  10874. node.computed = computed;
  10875. if (computed) {
  10876. node.property = this.parseExpression();
  10877. this.expect(3);
  10878. } else if (this.match(136)) {
  10879. if (base.type === "Super") {
  10880. this.raise(Errors.SuperPrivateField, {
  10881. at: startLoc
  10882. });
  10883. }
  10884. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10885. node.property = this.parsePrivateName();
  10886. } else {
  10887. node.property = this.parseIdentifier(true);
  10888. }
  10889. if (state.optionalChainMember) {
  10890. node.optional = optional;
  10891. return this.finishNode(node, "OptionalMemberExpression");
  10892. } else {
  10893. return this.finishNode(node, "MemberExpression");
  10894. }
  10895. }
  10896. parseBind(base, startLoc, noCalls, state) {
  10897. const node = this.startNodeAt(startLoc);
  10898. node.object = base;
  10899. this.next();
  10900. node.callee = this.parseNoCallExpr();
  10901. state.stop = true;
  10902. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  10903. }
  10904. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  10905. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10906. let refExpressionErrors = null;
  10907. this.state.maybeInArrowParameters = true;
  10908. this.next();
  10909. const node = this.startNodeAt(startLoc);
  10910. node.callee = base;
  10911. const {
  10912. maybeAsyncArrow,
  10913. optionalChainMember
  10914. } = state;
  10915. if (maybeAsyncArrow) {
  10916. this.expressionScope.enter(newAsyncArrowScope());
  10917. refExpressionErrors = new ExpressionErrors();
  10918. }
  10919. if (optionalChainMember) {
  10920. node.optional = optional;
  10921. }
  10922. if (optional) {
  10923. node.arguments = this.parseCallExpressionArguments(11);
  10924. } else {
  10925. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10926. }
  10927. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10928. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10929. state.stop = true;
  10930. this.checkDestructuringPrivate(refExpressionErrors);
  10931. this.expressionScope.validateAsPattern();
  10932. this.expressionScope.exit();
  10933. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  10934. } else {
  10935. if (maybeAsyncArrow) {
  10936. this.checkExpressionErrors(refExpressionErrors, true);
  10937. this.expressionScope.exit();
  10938. }
  10939. this.toReferencedArguments(finishedNode);
  10940. }
  10941. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10942. return finishedNode;
  10943. }
  10944. toReferencedArguments(node, isParenthesizedExpr) {
  10945. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10946. }
  10947. parseTaggedTemplateExpression(base, startLoc, state) {
  10948. const node = this.startNodeAt(startLoc);
  10949. node.tag = base;
  10950. node.quasi = this.parseTemplate(true);
  10951. if (state.optionalChainMember) {
  10952. this.raise(Errors.OptionalChainingNoTemplate, {
  10953. at: startLoc
  10954. });
  10955. }
  10956. return this.finishNode(node, "TaggedTemplateExpression");
  10957. }
  10958. atPossibleAsyncArrow(base) {
  10959. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  10960. }
  10961. finishCallExpression(node, optional) {
  10962. if (node.callee.type === "Import") {
  10963. if (node.arguments.length === 2) {
  10964. {
  10965. if (!this.hasPlugin("moduleAttributes")) {
  10966. this.expectPlugin("importAssertions");
  10967. }
  10968. }
  10969. }
  10970. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10971. this.raise(Errors.ImportCallArity, {
  10972. at: node,
  10973. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10974. });
  10975. } else {
  10976. for (const arg of node.arguments) {
  10977. if (arg.type === "SpreadElement") {
  10978. this.raise(Errors.ImportCallSpreadArgument, {
  10979. at: arg
  10980. });
  10981. }
  10982. }
  10983. }
  10984. }
  10985. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10986. }
  10987. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10988. const elts = [];
  10989. let first = true;
  10990. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10991. this.state.inFSharpPipelineDirectBody = false;
  10992. while (!this.eat(close)) {
  10993. if (first) {
  10994. first = false;
  10995. } else {
  10996. this.expect(12);
  10997. if (this.match(close)) {
  10998. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10999. this.raise(Errors.ImportCallArgumentTrailingComma, {
  11000. at: this.state.lastTokStartLoc
  11001. });
  11002. }
  11003. if (nodeForExtra) {
  11004. this.addTrailingCommaExtraToNode(nodeForExtra);
  11005. }
  11006. this.next();
  11007. break;
  11008. }
  11009. }
  11010. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  11011. }
  11012. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11013. return elts;
  11014. }
  11015. shouldParseAsyncArrow() {
  11016. return this.match(19) && !this.canInsertSemicolon();
  11017. }
  11018. parseAsyncArrowFromCallExpression(node, call) {
  11019. var _call$extra;
  11020. this.resetPreviousNodeTrailingComments(call);
  11021. this.expect(19);
  11022. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  11023. if (call.innerComments) {
  11024. setInnerComments(node, call.innerComments);
  11025. }
  11026. if (call.callee.trailingComments) {
  11027. setInnerComments(node, call.callee.trailingComments);
  11028. }
  11029. return node;
  11030. }
  11031. parseNoCallExpr() {
  11032. const startLoc = this.state.startLoc;
  11033. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  11034. }
  11035. parseExprAtom(refExpressionErrors) {
  11036. let node;
  11037. let decorators = null;
  11038. const {
  11039. type
  11040. } = this.state;
  11041. switch (type) {
  11042. case 79:
  11043. return this.parseSuper();
  11044. case 83:
  11045. node = this.startNode();
  11046. this.next();
  11047. if (this.match(16)) {
  11048. return this.parseImportMetaProperty(node);
  11049. }
  11050. if (!this.match(10)) {
  11051. this.raise(Errors.UnsupportedImport, {
  11052. at: this.state.lastTokStartLoc
  11053. });
  11054. }
  11055. return this.finishNode(node, "Import");
  11056. case 78:
  11057. node = this.startNode();
  11058. this.next();
  11059. return this.finishNode(node, "ThisExpression");
  11060. case 90:
  11061. {
  11062. return this.parseDo(this.startNode(), false);
  11063. }
  11064. case 56:
  11065. case 31:
  11066. {
  11067. this.readRegexp();
  11068. return this.parseRegExpLiteral(this.state.value);
  11069. }
  11070. case 132:
  11071. return this.parseNumericLiteral(this.state.value);
  11072. case 133:
  11073. return this.parseBigIntLiteral(this.state.value);
  11074. case 134:
  11075. return this.parseDecimalLiteral(this.state.value);
  11076. case 131:
  11077. return this.parseStringLiteral(this.state.value);
  11078. case 84:
  11079. return this.parseNullLiteral();
  11080. case 85:
  11081. return this.parseBooleanLiteral(true);
  11082. case 86:
  11083. return this.parseBooleanLiteral(false);
  11084. case 10:
  11085. {
  11086. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11087. return this.parseParenAndDistinguishExpression(canBeArrow);
  11088. }
  11089. case 2:
  11090. case 1:
  11091. {
  11092. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  11093. }
  11094. case 0:
  11095. {
  11096. return this.parseArrayLike(3, true, false, refExpressionErrors);
  11097. }
  11098. case 6:
  11099. case 7:
  11100. {
  11101. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11102. }
  11103. case 5:
  11104. {
  11105. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11106. }
  11107. case 68:
  11108. return this.parseFunctionOrFunctionSent();
  11109. case 26:
  11110. decorators = this.parseDecorators();
  11111. case 80:
  11112. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  11113. case 77:
  11114. return this.parseNewOrNewTarget();
  11115. case 25:
  11116. case 24:
  11117. return this.parseTemplate(false);
  11118. case 15:
  11119. {
  11120. node = this.startNode();
  11121. this.next();
  11122. node.object = null;
  11123. const callee = node.callee = this.parseNoCallExpr();
  11124. if (callee.type === "MemberExpression") {
  11125. return this.finishNode(node, "BindExpression");
  11126. } else {
  11127. throw this.raise(Errors.UnsupportedBind, {
  11128. at: callee
  11129. });
  11130. }
  11131. }
  11132. case 136:
  11133. {
  11134. this.raise(Errors.PrivateInExpectedIn, {
  11135. at: this.state.startLoc,
  11136. identifierName: this.state.value
  11137. });
  11138. return this.parsePrivateName();
  11139. }
  11140. case 33:
  11141. {
  11142. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11143. }
  11144. case 32:
  11145. {
  11146. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11147. }
  11148. case 37:
  11149. case 38:
  11150. {
  11151. return this.parseTopicReference("hack");
  11152. }
  11153. case 44:
  11154. case 54:
  11155. case 27:
  11156. {
  11157. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11158. if (pipeProposal) {
  11159. return this.parseTopicReference(pipeProposal);
  11160. } else {
  11161. throw this.unexpected();
  11162. }
  11163. }
  11164. case 47:
  11165. {
  11166. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11167. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11168. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11169. break;
  11170. } else {
  11171. throw this.unexpected();
  11172. }
  11173. }
  11174. default:
  11175. if (tokenIsIdentifier(type)) {
  11176. if (this.isContextual(125) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  11177. return this.parseModuleExpression();
  11178. }
  11179. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11180. const containsEsc = this.state.containsEsc;
  11181. const id = this.parseIdentifier();
  11182. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11183. const {
  11184. type
  11185. } = this.state;
  11186. if (type === 68) {
  11187. this.resetPreviousNodeTrailingComments(id);
  11188. this.next();
  11189. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  11190. } else if (tokenIsIdentifier(type)) {
  11191. if (this.lookaheadCharCode() === 61) {
  11192. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11193. } else {
  11194. return id;
  11195. }
  11196. } else if (type === 90) {
  11197. this.resetPreviousNodeTrailingComments(id);
  11198. return this.parseDo(this.startNodeAtNode(id), true);
  11199. }
  11200. }
  11201. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11202. this.next();
  11203. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11204. }
  11205. return id;
  11206. } else {
  11207. throw this.unexpected();
  11208. }
  11209. }
  11210. }
  11211. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11212. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11213. if (pipeProposal) {
  11214. this.state.type = topicTokenType;
  11215. this.state.value = topicTokenValue;
  11216. this.state.pos--;
  11217. this.state.end--;
  11218. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11219. return this.parseTopicReference(pipeProposal);
  11220. } else {
  11221. throw this.unexpected();
  11222. }
  11223. }
  11224. parseTopicReference(pipeProposal) {
  11225. const node = this.startNode();
  11226. const startLoc = this.state.startLoc;
  11227. const tokenType = this.state.type;
  11228. this.next();
  11229. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11230. }
  11231. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11232. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11233. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  11234. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11235. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  11236. at: startLoc
  11237. });
  11238. }
  11239. this.registerTopicReference();
  11240. return this.finishNode(node, nodeType);
  11241. } else {
  11242. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  11243. at: startLoc,
  11244. token: tokenLabelName(tokenType)
  11245. });
  11246. }
  11247. }
  11248. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11249. switch (pipeProposal) {
  11250. case "hack":
  11251. {
  11252. return this.hasPlugin(["pipelineOperator", {
  11253. topicToken: tokenLabelName(tokenType)
  11254. }]);
  11255. }
  11256. case "smart":
  11257. return tokenType === 27;
  11258. default:
  11259. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  11260. at: startLoc
  11261. });
  11262. }
  11263. }
  11264. parseAsyncArrowUnaryFunction(node) {
  11265. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11266. const params = [this.parseIdentifier()];
  11267. this.prodParam.exit();
  11268. if (this.hasPrecedingLineBreak()) {
  11269. this.raise(Errors.LineTerminatorBeforeArrow, {
  11270. at: this.state.curPosition()
  11271. });
  11272. }
  11273. this.expect(19);
  11274. return this.parseArrowExpression(node, params, true);
  11275. }
  11276. parseDo(node, isAsync) {
  11277. this.expectPlugin("doExpressions");
  11278. if (isAsync) {
  11279. this.expectPlugin("asyncDoExpressions");
  11280. }
  11281. node.async = isAsync;
  11282. this.next();
  11283. const oldLabels = this.state.labels;
  11284. this.state.labels = [];
  11285. if (isAsync) {
  11286. this.prodParam.enter(PARAM_AWAIT);
  11287. node.body = this.parseBlock();
  11288. this.prodParam.exit();
  11289. } else {
  11290. node.body = this.parseBlock();
  11291. }
  11292. this.state.labels = oldLabels;
  11293. return this.finishNode(node, "DoExpression");
  11294. }
  11295. parseSuper() {
  11296. const node = this.startNode();
  11297. this.next();
  11298. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11299. this.raise(Errors.SuperNotAllowed, {
  11300. at: node
  11301. });
  11302. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11303. this.raise(Errors.UnexpectedSuper, {
  11304. at: node
  11305. });
  11306. }
  11307. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11308. this.raise(Errors.UnsupportedSuper, {
  11309. at: node
  11310. });
  11311. }
  11312. return this.finishNode(node, "Super");
  11313. }
  11314. parsePrivateName() {
  11315. const node = this.startNode();
  11316. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11317. const name = this.state.value;
  11318. this.next();
  11319. node.id = this.createIdentifier(id, name);
  11320. return this.finishNode(node, "PrivateName");
  11321. }
  11322. parseFunctionOrFunctionSent() {
  11323. const node = this.startNode();
  11324. this.next();
  11325. if (this.prodParam.hasYield && this.match(16)) {
  11326. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11327. this.next();
  11328. if (this.match(102)) {
  11329. this.expectPlugin("functionSent");
  11330. } else if (!this.hasPlugin("functionSent")) {
  11331. this.unexpected();
  11332. }
  11333. return this.parseMetaProperty(node, meta, "sent");
  11334. }
  11335. return this.parseFunction(node);
  11336. }
  11337. parseMetaProperty(node, meta, propertyName) {
  11338. node.meta = meta;
  11339. const containsEsc = this.state.containsEsc;
  11340. node.property = this.parseIdentifier(true);
  11341. if (node.property.name !== propertyName || containsEsc) {
  11342. this.raise(Errors.UnsupportedMetaProperty, {
  11343. at: node.property,
  11344. target: meta.name,
  11345. onlyValidPropertyName: propertyName
  11346. });
  11347. }
  11348. return this.finishNode(node, "MetaProperty");
  11349. }
  11350. parseImportMetaProperty(node) {
  11351. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11352. this.next();
  11353. if (this.isContextual(100)) {
  11354. if (!this.inModule) {
  11355. this.raise(Errors.ImportMetaOutsideModule, {
  11356. at: id
  11357. });
  11358. }
  11359. this.sawUnambiguousESM = true;
  11360. }
  11361. return this.parseMetaProperty(node, id, "meta");
  11362. }
  11363. parseLiteralAtNode(value, type, node) {
  11364. this.addExtra(node, "rawValue", value);
  11365. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11366. node.value = value;
  11367. this.next();
  11368. return this.finishNode(node, type);
  11369. }
  11370. parseLiteral(value, type) {
  11371. const node = this.startNode();
  11372. return this.parseLiteralAtNode(value, type, node);
  11373. }
  11374. parseStringLiteral(value) {
  11375. return this.parseLiteral(value, "StringLiteral");
  11376. }
  11377. parseNumericLiteral(value) {
  11378. return this.parseLiteral(value, "NumericLiteral");
  11379. }
  11380. parseBigIntLiteral(value) {
  11381. return this.parseLiteral(value, "BigIntLiteral");
  11382. }
  11383. parseDecimalLiteral(value) {
  11384. return this.parseLiteral(value, "DecimalLiteral");
  11385. }
  11386. parseRegExpLiteral(value) {
  11387. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11388. node.pattern = value.pattern;
  11389. node.flags = value.flags;
  11390. return node;
  11391. }
  11392. parseBooleanLiteral(value) {
  11393. const node = this.startNode();
  11394. node.value = value;
  11395. this.next();
  11396. return this.finishNode(node, "BooleanLiteral");
  11397. }
  11398. parseNullLiteral() {
  11399. const node = this.startNode();
  11400. this.next();
  11401. return this.finishNode(node, "NullLiteral");
  11402. }
  11403. parseParenAndDistinguishExpression(canBeArrow) {
  11404. const startLoc = this.state.startLoc;
  11405. let val;
  11406. this.next();
  11407. this.expressionScope.enter(newArrowHeadScope());
  11408. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11409. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11410. this.state.maybeInArrowParameters = true;
  11411. this.state.inFSharpPipelineDirectBody = false;
  11412. const innerStartLoc = this.state.startLoc;
  11413. const exprList = [];
  11414. const refExpressionErrors = new ExpressionErrors();
  11415. let first = true;
  11416. let spreadStartLoc;
  11417. let optionalCommaStartLoc;
  11418. while (!this.match(11)) {
  11419. if (first) {
  11420. first = false;
  11421. } else {
  11422. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11423. if (this.match(11)) {
  11424. optionalCommaStartLoc = this.state.startLoc;
  11425. break;
  11426. }
  11427. }
  11428. if (this.match(21)) {
  11429. const spreadNodeStartLoc = this.state.startLoc;
  11430. spreadStartLoc = this.state.startLoc;
  11431. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11432. if (!this.checkCommaAfterRest(41)) {
  11433. break;
  11434. }
  11435. } else {
  11436. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11437. }
  11438. }
  11439. const innerEndLoc = this.state.lastTokEndLoc;
  11440. this.expect(11);
  11441. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11442. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11443. let arrowNode = this.startNodeAt(startLoc);
  11444. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11445. this.checkDestructuringPrivate(refExpressionErrors);
  11446. this.expressionScope.validateAsPattern();
  11447. this.expressionScope.exit();
  11448. this.parseArrowExpression(arrowNode, exprList, false);
  11449. return arrowNode;
  11450. }
  11451. this.expressionScope.exit();
  11452. if (!exprList.length) {
  11453. this.unexpected(this.state.lastTokStartLoc);
  11454. }
  11455. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11456. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11457. this.checkExpressionErrors(refExpressionErrors, true);
  11458. this.toReferencedListDeep(exprList, true);
  11459. if (exprList.length > 1) {
  11460. val = this.startNodeAt(innerStartLoc);
  11461. val.expressions = exprList;
  11462. this.finishNode(val, "SequenceExpression");
  11463. this.resetEndLocation(val, innerEndLoc);
  11464. } else {
  11465. val = exprList[0];
  11466. }
  11467. return this.wrapParenthesis(startLoc, val);
  11468. }
  11469. wrapParenthesis(startLoc, expression) {
  11470. if (!this.options.createParenthesizedExpressions) {
  11471. this.addExtra(expression, "parenthesized", true);
  11472. this.addExtra(expression, "parenStart", startLoc.index);
  11473. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11474. return expression;
  11475. }
  11476. const parenExpression = this.startNodeAt(startLoc);
  11477. parenExpression.expression = expression;
  11478. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11479. }
  11480. shouldParseArrow(params) {
  11481. return !this.canInsertSemicolon();
  11482. }
  11483. parseArrow(node) {
  11484. if (this.eat(19)) {
  11485. return node;
  11486. }
  11487. }
  11488. parseParenItem(node, startLoc) {
  11489. return node;
  11490. }
  11491. parseNewOrNewTarget() {
  11492. const node = this.startNode();
  11493. this.next();
  11494. if (this.match(16)) {
  11495. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11496. this.next();
  11497. const metaProp = this.parseMetaProperty(node, meta, "target");
  11498. if (!this.scope.inNonArrowFunction && !this.scope.inClass && !this.options.allowNewTargetOutsideFunction) {
  11499. this.raise(Errors.UnexpectedNewTarget, {
  11500. at: metaProp
  11501. });
  11502. }
  11503. return metaProp;
  11504. }
  11505. return this.parseNew(node);
  11506. }
  11507. parseNew(node) {
  11508. this.parseNewCallee(node);
  11509. if (this.eat(10)) {
  11510. const args = this.parseExprList(11);
  11511. this.toReferencedList(args);
  11512. node.arguments = args;
  11513. } else {
  11514. node.arguments = [];
  11515. }
  11516. return this.finishNode(node, "NewExpression");
  11517. }
  11518. parseNewCallee(node) {
  11519. node.callee = this.parseNoCallExpr();
  11520. if (node.callee.type === "Import") {
  11521. this.raise(Errors.ImportCallNotNewExpression, {
  11522. at: node.callee
  11523. });
  11524. }
  11525. }
  11526. parseTemplateElement(isTagged) {
  11527. const {
  11528. start,
  11529. startLoc,
  11530. end,
  11531. value
  11532. } = this.state;
  11533. const elemStart = start + 1;
  11534. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11535. if (value === null) {
  11536. if (!isTagged) {
  11537. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11538. at: createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1)
  11539. });
  11540. }
  11541. }
  11542. const isTail = this.match(24);
  11543. const endOffset = isTail ? -1 : -2;
  11544. const elemEnd = end + endOffset;
  11545. elem.value = {
  11546. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11547. cooked: value === null ? null : value.slice(1, endOffset)
  11548. };
  11549. elem.tail = isTail;
  11550. this.next();
  11551. const finishedNode = this.finishNode(elem, "TemplateElement");
  11552. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11553. return finishedNode;
  11554. }
  11555. parseTemplate(isTagged) {
  11556. const node = this.startNode();
  11557. node.expressions = [];
  11558. let curElt = this.parseTemplateElement(isTagged);
  11559. node.quasis = [curElt];
  11560. while (!curElt.tail) {
  11561. node.expressions.push(this.parseTemplateSubstitution());
  11562. this.readTemplateContinuation();
  11563. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11564. }
  11565. return this.finishNode(node, "TemplateLiteral");
  11566. }
  11567. parseTemplateSubstitution() {
  11568. return this.parseExpression();
  11569. }
  11570. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11571. if (isRecord) {
  11572. this.expectPlugin("recordAndTuple");
  11573. }
  11574. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11575. this.state.inFSharpPipelineDirectBody = false;
  11576. const propHash = Object.create(null);
  11577. let first = true;
  11578. const node = this.startNode();
  11579. node.properties = [];
  11580. this.next();
  11581. while (!this.match(close)) {
  11582. if (first) {
  11583. first = false;
  11584. } else {
  11585. this.expect(12);
  11586. if (this.match(close)) {
  11587. this.addTrailingCommaExtraToNode(node);
  11588. break;
  11589. }
  11590. }
  11591. let prop;
  11592. if (isPattern) {
  11593. prop = this.parseBindingProperty();
  11594. } else {
  11595. prop = this.parsePropertyDefinition(refExpressionErrors);
  11596. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11597. }
  11598. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11599. this.raise(Errors.InvalidRecordProperty, {
  11600. at: prop
  11601. });
  11602. }
  11603. if (prop.shorthand) {
  11604. this.addExtra(prop, "shorthand", true);
  11605. }
  11606. node.properties.push(prop);
  11607. }
  11608. this.next();
  11609. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11610. let type = "ObjectExpression";
  11611. if (isPattern) {
  11612. type = "ObjectPattern";
  11613. } else if (isRecord) {
  11614. type = "RecordExpression";
  11615. }
  11616. return this.finishNode(node, type);
  11617. }
  11618. addTrailingCommaExtraToNode(node) {
  11619. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11620. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11621. }
  11622. maybeAsyncOrAccessorProp(prop) {
  11623. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11624. }
  11625. parsePropertyDefinition(refExpressionErrors) {
  11626. let decorators = [];
  11627. if (this.match(26)) {
  11628. if (this.hasPlugin("decorators")) {
  11629. this.raise(Errors.UnsupportedPropertyDecorator, {
  11630. at: this.state.startLoc
  11631. });
  11632. }
  11633. while (this.match(26)) {
  11634. decorators.push(this.parseDecorator());
  11635. }
  11636. }
  11637. const prop = this.startNode();
  11638. let isAsync = false;
  11639. let isAccessor = false;
  11640. let startLoc;
  11641. if (this.match(21)) {
  11642. if (decorators.length) this.unexpected();
  11643. return this.parseSpread();
  11644. }
  11645. if (decorators.length) {
  11646. prop.decorators = decorators;
  11647. decorators = [];
  11648. }
  11649. prop.method = false;
  11650. if (refExpressionErrors) {
  11651. startLoc = this.state.startLoc;
  11652. }
  11653. let isGenerator = this.eat(55);
  11654. this.parsePropertyNamePrefixOperator(prop);
  11655. const containsEsc = this.state.containsEsc;
  11656. const key = this.parsePropertyName(prop, refExpressionErrors);
  11657. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11658. const keyName = key.name;
  11659. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11660. isAsync = true;
  11661. this.resetPreviousNodeTrailingComments(key);
  11662. isGenerator = this.eat(55);
  11663. this.parsePropertyName(prop);
  11664. }
  11665. if (keyName === "get" || keyName === "set") {
  11666. isAccessor = true;
  11667. this.resetPreviousNodeTrailingComments(key);
  11668. prop.kind = keyName;
  11669. if (this.match(55)) {
  11670. isGenerator = true;
  11671. this.raise(Errors.AccessorIsGenerator, {
  11672. at: this.state.curPosition(),
  11673. kind: keyName
  11674. });
  11675. this.next();
  11676. }
  11677. this.parsePropertyName(prop);
  11678. }
  11679. }
  11680. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11681. }
  11682. getGetterSetterExpectedParamCount(method) {
  11683. return method.kind === "get" ? 0 : 1;
  11684. }
  11685. getObjectOrClassMethodParams(method) {
  11686. return method.params;
  11687. }
  11688. checkGetterSetterParams(method) {
  11689. var _params;
  11690. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11691. const params = this.getObjectOrClassMethodParams(method);
  11692. if (params.length !== paramCount) {
  11693. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11694. at: method
  11695. });
  11696. }
  11697. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11698. this.raise(Errors.BadSetterRestParameter, {
  11699. at: method
  11700. });
  11701. }
  11702. }
  11703. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11704. if (isAccessor) {
  11705. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11706. this.checkGetterSetterParams(finishedProp);
  11707. return finishedProp;
  11708. }
  11709. if (isAsync || isGenerator || this.match(10)) {
  11710. if (isPattern) this.unexpected();
  11711. prop.kind = "method";
  11712. prop.method = true;
  11713. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11714. }
  11715. }
  11716. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11717. prop.shorthand = false;
  11718. if (this.eat(14)) {
  11719. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11720. return this.finishNode(prop, "ObjectProperty");
  11721. }
  11722. if (!prop.computed && prop.key.type === "Identifier") {
  11723. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11724. if (isPattern) {
  11725. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11726. } else if (this.match(29)) {
  11727. const shorthandAssignLoc = this.state.startLoc;
  11728. if (refExpressionErrors != null) {
  11729. if (refExpressionErrors.shorthandAssignLoc === null) {
  11730. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11731. }
  11732. } else {
  11733. this.raise(Errors.InvalidCoverInitializedName, {
  11734. at: shorthandAssignLoc
  11735. });
  11736. }
  11737. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11738. } else {
  11739. prop.value = cloneIdentifier(prop.key);
  11740. }
  11741. prop.shorthand = true;
  11742. return this.finishNode(prop, "ObjectProperty");
  11743. }
  11744. }
  11745. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11746. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11747. if (!node) this.unexpected();
  11748. return node;
  11749. }
  11750. parsePropertyName(prop, refExpressionErrors) {
  11751. if (this.eat(0)) {
  11752. prop.computed = true;
  11753. prop.key = this.parseMaybeAssignAllowIn();
  11754. this.expect(3);
  11755. } else {
  11756. const {
  11757. type,
  11758. value
  11759. } = this.state;
  11760. let key;
  11761. if (tokenIsKeywordOrIdentifier(type)) {
  11762. key = this.parseIdentifier(true);
  11763. } else {
  11764. switch (type) {
  11765. case 132:
  11766. key = this.parseNumericLiteral(value);
  11767. break;
  11768. case 131:
  11769. key = this.parseStringLiteral(value);
  11770. break;
  11771. case 133:
  11772. key = this.parseBigIntLiteral(value);
  11773. break;
  11774. case 134:
  11775. key = this.parseDecimalLiteral(value);
  11776. break;
  11777. case 136:
  11778. {
  11779. const privateKeyLoc = this.state.startLoc;
  11780. if (refExpressionErrors != null) {
  11781. if (refExpressionErrors.privateKeyLoc === null) {
  11782. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11783. }
  11784. } else {
  11785. this.raise(Errors.UnexpectedPrivateField, {
  11786. at: privateKeyLoc
  11787. });
  11788. }
  11789. key = this.parsePrivateName();
  11790. break;
  11791. }
  11792. default:
  11793. throw this.unexpected();
  11794. }
  11795. }
  11796. prop.key = key;
  11797. if (type !== 136) {
  11798. prop.computed = false;
  11799. }
  11800. }
  11801. return prop.key;
  11802. }
  11803. initFunction(node, isAsync) {
  11804. node.id = null;
  11805. node.generator = false;
  11806. node.async = isAsync;
  11807. }
  11808. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11809. this.initFunction(node, isAsync);
  11810. node.generator = isGenerator;
  11811. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11812. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11813. this.parseFunctionParams(node, isConstructor);
  11814. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11815. this.prodParam.exit();
  11816. this.scope.exit();
  11817. return finishedNode;
  11818. }
  11819. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11820. if (isTuple) {
  11821. this.expectPlugin("recordAndTuple");
  11822. }
  11823. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11824. this.state.inFSharpPipelineDirectBody = false;
  11825. const node = this.startNode();
  11826. this.next();
  11827. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11828. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11829. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11830. }
  11831. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11832. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11833. let flags = functionFlags(isAsync, false);
  11834. if (!this.match(5) && this.prodParam.hasIn) {
  11835. flags |= PARAM_IN;
  11836. }
  11837. this.prodParam.enter(flags);
  11838. this.initFunction(node, isAsync);
  11839. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11840. if (params) {
  11841. this.state.maybeInArrowParameters = true;
  11842. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11843. }
  11844. this.state.maybeInArrowParameters = false;
  11845. this.parseFunctionBody(node, true);
  11846. this.prodParam.exit();
  11847. this.scope.exit();
  11848. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11849. return this.finishNode(node, "ArrowFunctionExpression");
  11850. }
  11851. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11852. this.toAssignableList(params, trailingCommaLoc, false);
  11853. node.params = params;
  11854. }
  11855. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11856. this.parseFunctionBody(node, false, isMethod);
  11857. return this.finishNode(node, type);
  11858. }
  11859. parseFunctionBody(node, allowExpression, isMethod = false) {
  11860. const isExpression = allowExpression && !this.match(5);
  11861. this.expressionScope.enter(newExpressionScope());
  11862. if (isExpression) {
  11863. node.body = this.parseMaybeAssign();
  11864. this.checkParams(node, false, allowExpression, false);
  11865. } else {
  11866. const oldStrict = this.state.strict;
  11867. const oldLabels = this.state.labels;
  11868. this.state.labels = [];
  11869. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11870. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11871. const nonSimple = !this.isSimpleParamList(node.params);
  11872. if (hasStrictModeDirective && nonSimple) {
  11873. this.raise(Errors.IllegalLanguageModeDirective, {
  11874. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11875. });
  11876. }
  11877. const strictModeChanged = !oldStrict && this.state.strict;
  11878. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11879. if (this.state.strict && node.id) {
  11880. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11881. }
  11882. });
  11883. this.prodParam.exit();
  11884. this.state.labels = oldLabels;
  11885. }
  11886. this.expressionScope.exit();
  11887. }
  11888. isSimpleParameter(node) {
  11889. return node.type === "Identifier";
  11890. }
  11891. isSimpleParamList(params) {
  11892. for (let i = 0, len = params.length; i < len; i++) {
  11893. if (!this.isSimpleParameter(params[i])) return false;
  11894. }
  11895. return true;
  11896. }
  11897. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11898. const checkClashes = !allowDuplicates && new Set();
  11899. const formalParameters = {
  11900. type: "FormalParameters"
  11901. };
  11902. for (const param of node.params) {
  11903. this.checkLVal(param, {
  11904. in: formalParameters,
  11905. binding: BIND_VAR,
  11906. checkClashes,
  11907. strictModeChanged
  11908. });
  11909. }
  11910. }
  11911. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11912. const elts = [];
  11913. let first = true;
  11914. while (!this.eat(close)) {
  11915. if (first) {
  11916. first = false;
  11917. } else {
  11918. this.expect(12);
  11919. if (this.match(close)) {
  11920. if (nodeForExtra) {
  11921. this.addTrailingCommaExtraToNode(nodeForExtra);
  11922. }
  11923. this.next();
  11924. break;
  11925. }
  11926. }
  11927. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11928. }
  11929. return elts;
  11930. }
  11931. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11932. let elt;
  11933. if (this.match(12)) {
  11934. if (!allowEmpty) {
  11935. this.raise(Errors.UnexpectedToken, {
  11936. at: this.state.curPosition(),
  11937. unexpected: ","
  11938. });
  11939. }
  11940. elt = null;
  11941. } else if (this.match(21)) {
  11942. const spreadNodeStartLoc = this.state.startLoc;
  11943. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  11944. } else if (this.match(17)) {
  11945. this.expectPlugin("partialApplication");
  11946. if (!allowPlaceholder) {
  11947. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11948. at: this.state.startLoc
  11949. });
  11950. }
  11951. const node = this.startNode();
  11952. this.next();
  11953. elt = this.finishNode(node, "ArgumentPlaceholder");
  11954. } else {
  11955. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11956. }
  11957. return elt;
  11958. }
  11959. parseIdentifier(liberal) {
  11960. const node = this.startNode();
  11961. const name = this.parseIdentifierName(liberal);
  11962. return this.createIdentifier(node, name);
  11963. }
  11964. createIdentifier(node, name) {
  11965. node.name = name;
  11966. node.loc.identifierName = name;
  11967. return this.finishNode(node, "Identifier");
  11968. }
  11969. parseIdentifierName(liberal) {
  11970. let name;
  11971. const {
  11972. startLoc,
  11973. type
  11974. } = this.state;
  11975. if (tokenIsKeywordOrIdentifier(type)) {
  11976. name = this.state.value;
  11977. } else {
  11978. throw this.unexpected();
  11979. }
  11980. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11981. if (liberal) {
  11982. if (tokenIsKeyword) {
  11983. this.replaceToken(130);
  11984. }
  11985. } else {
  11986. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11987. }
  11988. this.next();
  11989. return name;
  11990. }
  11991. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11992. if (word.length > 10) {
  11993. return;
  11994. }
  11995. if (!canBeReservedWord(word)) {
  11996. return;
  11997. }
  11998. if (checkKeywords && isKeyword(word)) {
  11999. this.raise(Errors.UnexpectedKeyword, {
  12000. at: startLoc,
  12001. keyword: word
  12002. });
  12003. return;
  12004. }
  12005. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  12006. if (reservedTest(word, this.inModule)) {
  12007. this.raise(Errors.UnexpectedReservedWord, {
  12008. at: startLoc,
  12009. reservedWord: word
  12010. });
  12011. return;
  12012. } else if (word === "yield") {
  12013. if (this.prodParam.hasYield) {
  12014. this.raise(Errors.YieldBindingIdentifier, {
  12015. at: startLoc
  12016. });
  12017. return;
  12018. }
  12019. } else if (word === "await") {
  12020. if (this.prodParam.hasAwait) {
  12021. this.raise(Errors.AwaitBindingIdentifier, {
  12022. at: startLoc
  12023. });
  12024. return;
  12025. }
  12026. if (this.scope.inStaticBlock) {
  12027. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  12028. at: startLoc
  12029. });
  12030. return;
  12031. }
  12032. this.expressionScope.recordAsyncArrowParametersError({
  12033. at: startLoc
  12034. });
  12035. } else if (word === "arguments") {
  12036. if (this.scope.inClassAndNotInNonArrowFunction) {
  12037. this.raise(Errors.ArgumentsInClass, {
  12038. at: startLoc
  12039. });
  12040. return;
  12041. }
  12042. }
  12043. }
  12044. isAwaitAllowed() {
  12045. if (this.prodParam.hasAwait) return true;
  12046. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  12047. return true;
  12048. }
  12049. return false;
  12050. }
  12051. parseAwait(startLoc) {
  12052. const node = this.startNodeAt(startLoc);
  12053. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  12054. at: node
  12055. });
  12056. if (this.eat(55)) {
  12057. this.raise(Errors.ObsoleteAwaitStar, {
  12058. at: node
  12059. });
  12060. }
  12061. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  12062. if (this.isAmbiguousAwait()) {
  12063. this.ambiguousScriptDifferentAst = true;
  12064. } else {
  12065. this.sawUnambiguousESM = true;
  12066. }
  12067. }
  12068. if (!this.state.soloAwait) {
  12069. node.argument = this.parseMaybeUnary(null, true);
  12070. }
  12071. return this.finishNode(node, "AwaitExpression");
  12072. }
  12073. isAmbiguousAwait() {
  12074. if (this.hasPrecedingLineBreak()) return true;
  12075. const {
  12076. type
  12077. } = this.state;
  12078. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 101 && !this.state.containsEsc || type === 135 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12079. }
  12080. parseYield() {
  12081. const node = this.startNode();
  12082. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  12083. at: node
  12084. });
  12085. this.next();
  12086. let delegating = false;
  12087. let argument = null;
  12088. if (!this.hasPrecedingLineBreak()) {
  12089. delegating = this.eat(55);
  12090. switch (this.state.type) {
  12091. case 13:
  12092. case 137:
  12093. case 8:
  12094. case 11:
  12095. case 3:
  12096. case 9:
  12097. case 14:
  12098. case 12:
  12099. if (!delegating) break;
  12100. default:
  12101. argument = this.parseMaybeAssign();
  12102. }
  12103. }
  12104. node.delegate = delegating;
  12105. node.argument = argument;
  12106. return this.finishNode(node, "YieldExpression");
  12107. }
  12108. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12109. if (this.hasPlugin(["pipelineOperator", {
  12110. proposal: "smart"
  12111. }])) {
  12112. if (left.type === "SequenceExpression") {
  12113. this.raise(Errors.PipelineHeadSequenceExpression, {
  12114. at: leftStartLoc
  12115. });
  12116. }
  12117. }
  12118. }
  12119. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  12120. if (this.isSimpleReference(childExpr)) {
  12121. const bodyNode = this.startNodeAt(startLoc);
  12122. bodyNode.callee = childExpr;
  12123. return this.finishNode(bodyNode, "PipelineBareFunction");
  12124. } else {
  12125. const bodyNode = this.startNodeAt(startLoc);
  12126. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12127. bodyNode.expression = childExpr;
  12128. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12129. }
  12130. }
  12131. isSimpleReference(expression) {
  12132. switch (expression.type) {
  12133. case "MemberExpression":
  12134. return !expression.computed && this.isSimpleReference(expression.object);
  12135. case "Identifier":
  12136. return true;
  12137. default:
  12138. return false;
  12139. }
  12140. }
  12141. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12142. if (this.match(19)) {
  12143. throw this.raise(Errors.PipelineBodyNoArrow, {
  12144. at: this.state.startLoc
  12145. });
  12146. }
  12147. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12148. this.raise(Errors.PipelineTopicUnused, {
  12149. at: startLoc
  12150. });
  12151. }
  12152. }
  12153. withTopicBindingContext(callback) {
  12154. const outerContextTopicState = this.state.topicContext;
  12155. this.state.topicContext = {
  12156. maxNumOfResolvableTopics: 1,
  12157. maxTopicIndex: null
  12158. };
  12159. try {
  12160. return callback();
  12161. } finally {
  12162. this.state.topicContext = outerContextTopicState;
  12163. }
  12164. }
  12165. withSmartMixTopicForbiddingContext(callback) {
  12166. if (this.hasPlugin(["pipelineOperator", {
  12167. proposal: "smart"
  12168. }])) {
  12169. const outerContextTopicState = this.state.topicContext;
  12170. this.state.topicContext = {
  12171. maxNumOfResolvableTopics: 0,
  12172. maxTopicIndex: null
  12173. };
  12174. try {
  12175. return callback();
  12176. } finally {
  12177. this.state.topicContext = outerContextTopicState;
  12178. }
  12179. } else {
  12180. return callback();
  12181. }
  12182. }
  12183. withSoloAwaitPermittingContext(callback) {
  12184. const outerContextSoloAwaitState = this.state.soloAwait;
  12185. this.state.soloAwait = true;
  12186. try {
  12187. return callback();
  12188. } finally {
  12189. this.state.soloAwait = outerContextSoloAwaitState;
  12190. }
  12191. }
  12192. allowInAnd(callback) {
  12193. const flags = this.prodParam.currentFlags();
  12194. const prodParamToSet = PARAM_IN & ~flags;
  12195. if (prodParamToSet) {
  12196. this.prodParam.enter(flags | PARAM_IN);
  12197. try {
  12198. return callback();
  12199. } finally {
  12200. this.prodParam.exit();
  12201. }
  12202. }
  12203. return callback();
  12204. }
  12205. disallowInAnd(callback) {
  12206. const flags = this.prodParam.currentFlags();
  12207. const prodParamToClear = PARAM_IN & flags;
  12208. if (prodParamToClear) {
  12209. this.prodParam.enter(flags & ~PARAM_IN);
  12210. try {
  12211. return callback();
  12212. } finally {
  12213. this.prodParam.exit();
  12214. }
  12215. }
  12216. return callback();
  12217. }
  12218. registerTopicReference() {
  12219. this.state.topicContext.maxTopicIndex = 0;
  12220. }
  12221. topicReferenceIsAllowedInCurrentContext() {
  12222. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12223. }
  12224. topicReferenceWasUsedInCurrentContext() {
  12225. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12226. }
  12227. parseFSharpPipelineBody(prec) {
  12228. const startLoc = this.state.startLoc;
  12229. this.state.potentialArrowAt = this.state.start;
  12230. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12231. this.state.inFSharpPipelineDirectBody = true;
  12232. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  12233. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12234. return ret;
  12235. }
  12236. parseModuleExpression() {
  12237. this.expectPlugin("moduleBlocks");
  12238. const node = this.startNode();
  12239. this.next();
  12240. if (!this.match(5)) {
  12241. this.unexpected(null, 5);
  12242. }
  12243. const program = this.startNodeAt(this.state.endLoc);
  12244. this.next();
  12245. const revertScopes = this.initializeScopes(true);
  12246. this.enterInitialScopes();
  12247. try {
  12248. node.body = this.parseProgram(program, 8, "module");
  12249. } finally {
  12250. revertScopes();
  12251. }
  12252. return this.finishNode(node, "ModuleExpression");
  12253. }
  12254. parsePropertyNamePrefixOperator(prop) {}
  12255. }
  12256. const loopLabel = {
  12257. kind: "loop"
  12258. },
  12259. switchLabel = {
  12260. kind: "switch"
  12261. };
  12262. var ParseFunctionFlag = {
  12263. Expression: 0,
  12264. Declaration: 1,
  12265. HangingDeclaration: 2,
  12266. NullableId: 4,
  12267. Async: 8
  12268. };
  12269. var ParseStatementFlag = {
  12270. StatementOnly: 0,
  12271. AllowImportExport: 1,
  12272. AllowDeclaration: 2,
  12273. AllowFunctionDeclaration: 4,
  12274. AllowLabeledFunction: 8
  12275. };
  12276. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12277. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12278. function babel7CompatTokens(tokens, input) {
  12279. for (let i = 0; i < tokens.length; i++) {
  12280. const token = tokens[i];
  12281. const {
  12282. type
  12283. } = token;
  12284. if (typeof type === "number") {
  12285. {
  12286. if (type === 136) {
  12287. const {
  12288. loc,
  12289. start,
  12290. value,
  12291. end
  12292. } = token;
  12293. const hashEndPos = start + 1;
  12294. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12295. tokens.splice(i, 1, new Token({
  12296. type: getExportedToken(27),
  12297. value: "#",
  12298. start: start,
  12299. end: hashEndPos,
  12300. startLoc: loc.start,
  12301. endLoc: hashEndLoc
  12302. }), new Token({
  12303. type: getExportedToken(130),
  12304. value: value,
  12305. start: hashEndPos,
  12306. end: end,
  12307. startLoc: hashEndLoc,
  12308. endLoc: loc.end
  12309. }));
  12310. i++;
  12311. continue;
  12312. }
  12313. if (tokenIsTemplate(type)) {
  12314. const {
  12315. loc,
  12316. start,
  12317. value,
  12318. end
  12319. } = token;
  12320. const backquoteEnd = start + 1;
  12321. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12322. let startToken;
  12323. if (input.charCodeAt(start) === 96) {
  12324. startToken = new Token({
  12325. type: getExportedToken(22),
  12326. value: "`",
  12327. start: start,
  12328. end: backquoteEnd,
  12329. startLoc: loc.start,
  12330. endLoc: backquoteEndLoc
  12331. });
  12332. } else {
  12333. startToken = new Token({
  12334. type: getExportedToken(8),
  12335. value: "}",
  12336. start: start,
  12337. end: backquoteEnd,
  12338. startLoc: loc.start,
  12339. endLoc: backquoteEndLoc
  12340. });
  12341. }
  12342. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12343. if (type === 24) {
  12344. templateElementEnd = end - 1;
  12345. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12346. templateValue = value === null ? null : value.slice(1, -1);
  12347. endToken = new Token({
  12348. type: getExportedToken(22),
  12349. value: "`",
  12350. start: templateElementEnd,
  12351. end: end,
  12352. startLoc: templateElementEndLoc,
  12353. endLoc: loc.end
  12354. });
  12355. } else {
  12356. templateElementEnd = end - 2;
  12357. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12358. templateValue = value === null ? null : value.slice(1, -2);
  12359. endToken = new Token({
  12360. type: getExportedToken(23),
  12361. value: "${",
  12362. start: templateElementEnd,
  12363. end: end,
  12364. startLoc: templateElementEndLoc,
  12365. endLoc: loc.end
  12366. });
  12367. }
  12368. tokens.splice(i, 1, startToken, new Token({
  12369. type: getExportedToken(20),
  12370. value: templateValue,
  12371. start: backquoteEnd,
  12372. end: templateElementEnd,
  12373. startLoc: backquoteEndLoc,
  12374. endLoc: templateElementEndLoc
  12375. }), endToken);
  12376. i += 2;
  12377. continue;
  12378. }
  12379. }
  12380. token.type = getExportedToken(type);
  12381. }
  12382. }
  12383. return tokens;
  12384. }
  12385. class StatementParser extends ExpressionParser {
  12386. parseTopLevel(file, program) {
  12387. file.program = this.parseProgram(program);
  12388. file.comments = this.state.comments;
  12389. if (this.options.tokens) {
  12390. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12391. }
  12392. return this.finishNode(file, "File");
  12393. }
  12394. parseProgram(program, end = 137, sourceType = this.options.sourceType) {
  12395. program.sourceType = sourceType;
  12396. program.interpreter = this.parseInterpreterDirective();
  12397. this.parseBlockBody(program, true, true, end);
  12398. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12399. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12400. this.raise(Errors.ModuleExportUndefined, {
  12401. at,
  12402. localName
  12403. });
  12404. }
  12405. }
  12406. let finishedProgram;
  12407. if (end === 137) {
  12408. finishedProgram = this.finishNode(program, "Program");
  12409. } else {
  12410. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12411. }
  12412. return finishedProgram;
  12413. }
  12414. stmtToDirective(stmt) {
  12415. const directive = stmt;
  12416. directive.type = "Directive";
  12417. directive.value = directive.expression;
  12418. delete directive.expression;
  12419. const directiveLiteral = directive.value;
  12420. const expressionValue = directiveLiteral.value;
  12421. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12422. const val = directiveLiteral.value = raw.slice(1, -1);
  12423. this.addExtra(directiveLiteral, "raw", raw);
  12424. this.addExtra(directiveLiteral, "rawValue", val);
  12425. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12426. directiveLiteral.type = "DirectiveLiteral";
  12427. return directive;
  12428. }
  12429. parseInterpreterDirective() {
  12430. if (!this.match(28)) {
  12431. return null;
  12432. }
  12433. const node = this.startNode();
  12434. node.value = this.state.value;
  12435. this.next();
  12436. return this.finishNode(node, "InterpreterDirective");
  12437. }
  12438. isLet() {
  12439. if (!this.isContextual(99)) {
  12440. return false;
  12441. }
  12442. return this.hasFollowingBindingAtom();
  12443. }
  12444. chStartsBindingIdentifier(ch, pos) {
  12445. if (isIdentifierStart(ch)) {
  12446. keywordRelationalOperator.lastIndex = pos;
  12447. if (keywordRelationalOperator.test(this.input)) {
  12448. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12449. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12450. return false;
  12451. }
  12452. }
  12453. return true;
  12454. } else if (ch === 92) {
  12455. return true;
  12456. } else {
  12457. return false;
  12458. }
  12459. }
  12460. chStartsBindingPattern(ch) {
  12461. return ch === 91 || ch === 123;
  12462. }
  12463. hasFollowingBindingAtom() {
  12464. const next = this.nextTokenStart();
  12465. const nextCh = this.codePointAtPos(next);
  12466. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12467. }
  12468. hasFollowingBindingIdentifier() {
  12469. const next = this.nextTokenStart();
  12470. const nextCh = this.codePointAtPos(next);
  12471. return this.chStartsBindingIdentifier(nextCh, next);
  12472. }
  12473. startsUsingForOf() {
  12474. const lookahead = this.lookahead();
  12475. if (lookahead.type === 101 && !lookahead.containsEsc) {
  12476. return false;
  12477. } else {
  12478. this.expectPlugin("explicitResourceManagement");
  12479. return true;
  12480. }
  12481. }
  12482. parseModuleItem() {
  12483. return this.parseStatementLike(ParseStatementFlag.AllowImportExport | ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | ParseStatementFlag.AllowLabeledFunction);
  12484. }
  12485. parseStatementListItem() {
  12486. return this.parseStatementLike(ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | (!this.options.annexB || this.state.strict ? 0 : ParseStatementFlag.AllowLabeledFunction));
  12487. }
  12488. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12489. let flags = ParseStatementFlag.StatementOnly;
  12490. if (this.options.annexB && !this.state.strict) {
  12491. flags |= ParseStatementFlag.AllowFunctionDeclaration;
  12492. if (allowLabeledFunction) {
  12493. flags |= ParseStatementFlag.AllowLabeledFunction;
  12494. }
  12495. }
  12496. return this.parseStatementLike(flags);
  12497. }
  12498. parseStatement() {
  12499. return this.parseStatementLike(ParseStatementFlag.StatementOnly);
  12500. }
  12501. parseStatementLike(flags) {
  12502. let decorators = null;
  12503. if (this.match(26)) {
  12504. decorators = this.parseDecorators(true);
  12505. }
  12506. return this.parseStatementContent(flags, decorators);
  12507. }
  12508. parseStatementContent(flags, decorators) {
  12509. const starttype = this.state.type;
  12510. const node = this.startNode();
  12511. const allowDeclaration = !!(flags & ParseStatementFlag.AllowDeclaration);
  12512. const allowFunctionDeclaration = !!(flags & ParseStatementFlag.AllowFunctionDeclaration);
  12513. const topLevel = flags & ParseStatementFlag.AllowImportExport;
  12514. switch (starttype) {
  12515. case 60:
  12516. return this.parseBreakContinueStatement(node, true);
  12517. case 63:
  12518. return this.parseBreakContinueStatement(node, false);
  12519. case 64:
  12520. return this.parseDebuggerStatement(node);
  12521. case 90:
  12522. return this.parseDoWhileStatement(node);
  12523. case 91:
  12524. return this.parseForStatement(node);
  12525. case 68:
  12526. if (this.lookaheadCharCode() === 46) break;
  12527. if (!allowFunctionDeclaration) {
  12528. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, {
  12529. at: this.state.startLoc
  12530. });
  12531. }
  12532. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12533. case 80:
  12534. if (!allowDeclaration) this.unexpected();
  12535. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12536. case 69:
  12537. return this.parseIfStatement(node);
  12538. case 70:
  12539. return this.parseReturnStatement(node);
  12540. case 71:
  12541. return this.parseSwitchStatement(node);
  12542. case 72:
  12543. return this.parseThrowStatement(node);
  12544. case 73:
  12545. return this.parseTryStatement(node);
  12546. case 105:
  12547. if (this.hasFollowingLineBreak() || this.state.containsEsc || !this.hasFollowingBindingIdentifier()) {
  12548. break;
  12549. }
  12550. this.expectPlugin("explicitResourceManagement");
  12551. if (!this.scope.inModule && this.scope.inTopLevel) {
  12552. this.raise(Errors.UnexpectedUsingDeclaration, {
  12553. at: this.state.startLoc
  12554. });
  12555. } else if (!allowDeclaration) {
  12556. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12557. at: this.state.startLoc
  12558. });
  12559. }
  12560. return this.parseVarStatement(node, "using");
  12561. case 99:
  12562. {
  12563. if (this.state.containsEsc) {
  12564. break;
  12565. }
  12566. const next = this.nextTokenStart();
  12567. const nextCh = this.codePointAtPos(next);
  12568. if (nextCh !== 91) {
  12569. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12570. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12571. break;
  12572. }
  12573. }
  12574. }
  12575. case 75:
  12576. {
  12577. if (!allowDeclaration) {
  12578. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12579. at: this.state.startLoc
  12580. });
  12581. }
  12582. }
  12583. case 74:
  12584. {
  12585. const kind = this.state.value;
  12586. return this.parseVarStatement(node, kind);
  12587. }
  12588. case 92:
  12589. return this.parseWhileStatement(node);
  12590. case 76:
  12591. return this.parseWithStatement(node);
  12592. case 5:
  12593. return this.parseBlock();
  12594. case 13:
  12595. return this.parseEmptyStatement(node);
  12596. case 83:
  12597. {
  12598. const nextTokenCharCode = this.lookaheadCharCode();
  12599. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12600. break;
  12601. }
  12602. }
  12603. case 82:
  12604. {
  12605. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12606. this.raise(Errors.UnexpectedImportExport, {
  12607. at: this.state.startLoc
  12608. });
  12609. }
  12610. this.next();
  12611. let result;
  12612. if (starttype === 83) {
  12613. result = this.parseImport(node);
  12614. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12615. this.sawUnambiguousESM = true;
  12616. }
  12617. } else {
  12618. result = this.parseExport(node, decorators);
  12619. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12620. this.sawUnambiguousESM = true;
  12621. }
  12622. }
  12623. this.assertModuleNodeAllowed(result);
  12624. return result;
  12625. }
  12626. default:
  12627. {
  12628. if (this.isAsyncFunction()) {
  12629. if (!allowDeclaration) {
  12630. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12631. at: this.state.startLoc
  12632. });
  12633. }
  12634. this.next();
  12635. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12636. }
  12637. }
  12638. }
  12639. const maybeName = this.state.value;
  12640. const expr = this.parseExpression();
  12641. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12642. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12643. } else {
  12644. return this.parseExpressionStatement(node, expr, decorators);
  12645. }
  12646. }
  12647. assertModuleNodeAllowed(node) {
  12648. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12649. this.raise(Errors.ImportOutsideModule, {
  12650. at: node
  12651. });
  12652. }
  12653. }
  12654. decoratorsEnabledBeforeExport() {
  12655. if (this.hasPlugin("decorators-legacy")) return true;
  12656. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12657. }
  12658. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12659. if (maybeDecorators) {
  12660. if (classNode.decorators && classNode.decorators.length > 0) {
  12661. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12662. this.raise(Errors.DecoratorsBeforeAfterExport, {
  12663. at: classNode.decorators[0]
  12664. });
  12665. }
  12666. classNode.decorators.unshift(...maybeDecorators);
  12667. } else {
  12668. classNode.decorators = maybeDecorators;
  12669. }
  12670. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12671. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12672. }
  12673. return classNode;
  12674. }
  12675. canHaveLeadingDecorator() {
  12676. return this.match(80);
  12677. }
  12678. parseDecorators(allowExport) {
  12679. const decorators = [];
  12680. do {
  12681. decorators.push(this.parseDecorator());
  12682. } while (this.match(26));
  12683. if (this.match(82)) {
  12684. if (!allowExport) {
  12685. this.unexpected();
  12686. }
  12687. if (!this.decoratorsEnabledBeforeExport()) {
  12688. this.raise(Errors.DecoratorExportClass, {
  12689. at: this.state.startLoc
  12690. });
  12691. }
  12692. } else if (!this.canHaveLeadingDecorator()) {
  12693. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12694. at: this.state.startLoc
  12695. });
  12696. }
  12697. return decorators;
  12698. }
  12699. parseDecorator() {
  12700. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12701. const node = this.startNode();
  12702. this.next();
  12703. if (this.hasPlugin("decorators")) {
  12704. const startLoc = this.state.startLoc;
  12705. let expr;
  12706. if (this.match(10)) {
  12707. const startLoc = this.state.startLoc;
  12708. this.next();
  12709. expr = this.parseExpression();
  12710. this.expect(11);
  12711. expr = this.wrapParenthesis(startLoc, expr);
  12712. const paramsStartLoc = this.state.startLoc;
  12713. node.expression = this.parseMaybeDecoratorArguments(expr);
  12714. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12715. this.raise(Errors.DecoratorArgumentsOutsideParentheses, {
  12716. at: paramsStartLoc
  12717. });
  12718. }
  12719. } else {
  12720. expr = this.parseIdentifier(false);
  12721. while (this.eat(16)) {
  12722. const node = this.startNodeAt(startLoc);
  12723. node.object = expr;
  12724. if (this.match(136)) {
  12725. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12726. node.property = this.parsePrivateName();
  12727. } else {
  12728. node.property = this.parseIdentifier(true);
  12729. }
  12730. node.computed = false;
  12731. expr = this.finishNode(node, "MemberExpression");
  12732. }
  12733. node.expression = this.parseMaybeDecoratorArguments(expr);
  12734. }
  12735. } else {
  12736. node.expression = this.parseExprSubscripts();
  12737. }
  12738. return this.finishNode(node, "Decorator");
  12739. }
  12740. parseMaybeDecoratorArguments(expr) {
  12741. if (this.eat(10)) {
  12742. const node = this.startNodeAtNode(expr);
  12743. node.callee = expr;
  12744. node.arguments = this.parseCallExpressionArguments(11, false);
  12745. this.toReferencedList(node.arguments);
  12746. return this.finishNode(node, "CallExpression");
  12747. }
  12748. return expr;
  12749. }
  12750. parseBreakContinueStatement(node, isBreak) {
  12751. this.next();
  12752. if (this.isLineTerminator()) {
  12753. node.label = null;
  12754. } else {
  12755. node.label = this.parseIdentifier();
  12756. this.semicolon();
  12757. }
  12758. this.verifyBreakContinue(node, isBreak);
  12759. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12760. }
  12761. verifyBreakContinue(node, isBreak) {
  12762. let i;
  12763. for (i = 0; i < this.state.labels.length; ++i) {
  12764. const lab = this.state.labels[i];
  12765. if (node.label == null || lab.name === node.label.name) {
  12766. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12767. if (node.label && isBreak) break;
  12768. }
  12769. }
  12770. if (i === this.state.labels.length) {
  12771. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12772. this.raise(Errors.IllegalBreakContinue, {
  12773. at: node,
  12774. type
  12775. });
  12776. }
  12777. }
  12778. parseDebuggerStatement(node) {
  12779. this.next();
  12780. this.semicolon();
  12781. return this.finishNode(node, "DebuggerStatement");
  12782. }
  12783. parseHeaderExpression() {
  12784. this.expect(10);
  12785. const val = this.parseExpression();
  12786. this.expect(11);
  12787. return val;
  12788. }
  12789. parseDoWhileStatement(node) {
  12790. this.next();
  12791. this.state.labels.push(loopLabel);
  12792. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12793. this.state.labels.pop();
  12794. this.expect(92);
  12795. node.test = this.parseHeaderExpression();
  12796. this.eat(13);
  12797. return this.finishNode(node, "DoWhileStatement");
  12798. }
  12799. parseForStatement(node) {
  12800. this.next();
  12801. this.state.labels.push(loopLabel);
  12802. let awaitAt = null;
  12803. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12804. awaitAt = this.state.lastTokStartLoc;
  12805. }
  12806. this.scope.enter(SCOPE_OTHER);
  12807. this.expect(10);
  12808. if (this.match(13)) {
  12809. if (awaitAt !== null) {
  12810. this.unexpected(awaitAt);
  12811. }
  12812. return this.parseFor(node, null);
  12813. }
  12814. const startsWithLet = this.isContextual(99);
  12815. const startsWithUsing = this.isContextual(105) && !this.hasFollowingLineBreak();
  12816. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || startsWithUsing && this.hasFollowingBindingIdentifier() && this.startsUsingForOf();
  12817. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12818. const initNode = this.startNode();
  12819. const kind = this.state.value;
  12820. this.next();
  12821. this.parseVar(initNode, true, kind);
  12822. const init = this.finishNode(initNode, "VariableDeclaration");
  12823. const isForIn = this.match(58);
  12824. if (isForIn && startsWithUsing) {
  12825. this.raise(Errors.ForInUsing, {
  12826. at: init
  12827. });
  12828. }
  12829. if ((isForIn || this.isContextual(101)) && init.declarations.length === 1) {
  12830. return this.parseForIn(node, init, awaitAt);
  12831. }
  12832. if (awaitAt !== null) {
  12833. this.unexpected(awaitAt);
  12834. }
  12835. return this.parseFor(node, init);
  12836. }
  12837. const startsWithAsync = this.isContextual(95);
  12838. const refExpressionErrors = new ExpressionErrors();
  12839. const init = this.parseExpression(true, refExpressionErrors);
  12840. const isForOf = this.isContextual(101);
  12841. if (isForOf) {
  12842. if (startsWithLet) {
  12843. this.raise(Errors.ForOfLet, {
  12844. at: init
  12845. });
  12846. }
  12847. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12848. this.raise(Errors.ForOfAsync, {
  12849. at: init
  12850. });
  12851. }
  12852. }
  12853. if (isForOf || this.match(58)) {
  12854. this.checkDestructuringPrivate(refExpressionErrors);
  12855. this.toAssignable(init, true);
  12856. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12857. this.checkLVal(init, {
  12858. in: {
  12859. type
  12860. }
  12861. });
  12862. return this.parseForIn(node, init, awaitAt);
  12863. } else {
  12864. this.checkExpressionErrors(refExpressionErrors, true);
  12865. }
  12866. if (awaitAt !== null) {
  12867. this.unexpected(awaitAt);
  12868. }
  12869. return this.parseFor(node, init);
  12870. }
  12871. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  12872. this.next();
  12873. return this.parseFunction(node, ParseFunctionFlag.Declaration | (isHangingDeclaration ? ParseFunctionFlag.HangingDeclaration : 0) | (isAsync ? ParseFunctionFlag.Async : 0));
  12874. }
  12875. parseIfStatement(node) {
  12876. this.next();
  12877. node.test = this.parseHeaderExpression();
  12878. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  12879. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  12880. return this.finishNode(node, "IfStatement");
  12881. }
  12882. parseReturnStatement(node) {
  12883. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12884. this.raise(Errors.IllegalReturn, {
  12885. at: this.state.startLoc
  12886. });
  12887. }
  12888. this.next();
  12889. if (this.isLineTerminator()) {
  12890. node.argument = null;
  12891. } else {
  12892. node.argument = this.parseExpression();
  12893. this.semicolon();
  12894. }
  12895. return this.finishNode(node, "ReturnStatement");
  12896. }
  12897. parseSwitchStatement(node) {
  12898. this.next();
  12899. node.discriminant = this.parseHeaderExpression();
  12900. const cases = node.cases = [];
  12901. this.expect(5);
  12902. this.state.labels.push(switchLabel);
  12903. this.scope.enter(SCOPE_OTHER);
  12904. let cur;
  12905. for (let sawDefault; !this.match(8);) {
  12906. if (this.match(61) || this.match(65)) {
  12907. const isCase = this.match(61);
  12908. if (cur) this.finishNode(cur, "SwitchCase");
  12909. cases.push(cur = this.startNode());
  12910. cur.consequent = [];
  12911. this.next();
  12912. if (isCase) {
  12913. cur.test = this.parseExpression();
  12914. } else {
  12915. if (sawDefault) {
  12916. this.raise(Errors.MultipleDefaultsInSwitch, {
  12917. at: this.state.lastTokStartLoc
  12918. });
  12919. }
  12920. sawDefault = true;
  12921. cur.test = null;
  12922. }
  12923. this.expect(14);
  12924. } else {
  12925. if (cur) {
  12926. cur.consequent.push(this.parseStatementListItem());
  12927. } else {
  12928. this.unexpected();
  12929. }
  12930. }
  12931. }
  12932. this.scope.exit();
  12933. if (cur) this.finishNode(cur, "SwitchCase");
  12934. this.next();
  12935. this.state.labels.pop();
  12936. return this.finishNode(node, "SwitchStatement");
  12937. }
  12938. parseThrowStatement(node) {
  12939. this.next();
  12940. if (this.hasPrecedingLineBreak()) {
  12941. this.raise(Errors.NewlineAfterThrow, {
  12942. at: this.state.lastTokEndLoc
  12943. });
  12944. }
  12945. node.argument = this.parseExpression();
  12946. this.semicolon();
  12947. return this.finishNode(node, "ThrowStatement");
  12948. }
  12949. parseCatchClauseParam() {
  12950. const param = this.parseBindingAtom();
  12951. this.scope.enter(this.options.annexB && param.type === "Identifier" ? SCOPE_SIMPLE_CATCH : 0);
  12952. this.checkLVal(param, {
  12953. in: {
  12954. type: "CatchClause"
  12955. },
  12956. binding: BIND_CATCH_PARAM
  12957. });
  12958. return param;
  12959. }
  12960. parseTryStatement(node) {
  12961. this.next();
  12962. node.block = this.parseBlock();
  12963. node.handler = null;
  12964. if (this.match(62)) {
  12965. const clause = this.startNode();
  12966. this.next();
  12967. if (this.match(10)) {
  12968. this.expect(10);
  12969. clause.param = this.parseCatchClauseParam();
  12970. this.expect(11);
  12971. } else {
  12972. clause.param = null;
  12973. this.scope.enter(SCOPE_OTHER);
  12974. }
  12975. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12976. this.scope.exit();
  12977. node.handler = this.finishNode(clause, "CatchClause");
  12978. }
  12979. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12980. if (!node.handler && !node.finalizer) {
  12981. this.raise(Errors.NoCatchOrFinally, {
  12982. at: node
  12983. });
  12984. }
  12985. return this.finishNode(node, "TryStatement");
  12986. }
  12987. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12988. this.next();
  12989. this.parseVar(node, false, kind, allowMissingInitializer);
  12990. this.semicolon();
  12991. return this.finishNode(node, "VariableDeclaration");
  12992. }
  12993. parseWhileStatement(node) {
  12994. this.next();
  12995. node.test = this.parseHeaderExpression();
  12996. this.state.labels.push(loopLabel);
  12997. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12998. this.state.labels.pop();
  12999. return this.finishNode(node, "WhileStatement");
  13000. }
  13001. parseWithStatement(node) {
  13002. if (this.state.strict) {
  13003. this.raise(Errors.StrictWith, {
  13004. at: this.state.startLoc
  13005. });
  13006. }
  13007. this.next();
  13008. node.object = this.parseHeaderExpression();
  13009. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13010. return this.finishNode(node, "WithStatement");
  13011. }
  13012. parseEmptyStatement(node) {
  13013. this.next();
  13014. return this.finishNode(node, "EmptyStatement");
  13015. }
  13016. parseLabeledStatement(node, maybeName, expr, flags) {
  13017. for (const label of this.state.labels) {
  13018. if (label.name === maybeName) {
  13019. this.raise(Errors.LabelRedeclaration, {
  13020. at: expr,
  13021. labelName: maybeName
  13022. });
  13023. }
  13024. }
  13025. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  13026. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  13027. const label = this.state.labels[i];
  13028. if (label.statementStart === node.start) {
  13029. label.statementStart = this.state.start;
  13030. label.kind = kind;
  13031. } else {
  13032. break;
  13033. }
  13034. }
  13035. this.state.labels.push({
  13036. name: maybeName,
  13037. kind: kind,
  13038. statementStart: this.state.start
  13039. });
  13040. node.body = flags & ParseStatementFlag.AllowLabeledFunction ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  13041. this.state.labels.pop();
  13042. node.label = expr;
  13043. return this.finishNode(node, "LabeledStatement");
  13044. }
  13045. parseExpressionStatement(node, expr, decorators) {
  13046. node.expression = expr;
  13047. this.semicolon();
  13048. return this.finishNode(node, "ExpressionStatement");
  13049. }
  13050. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  13051. const node = this.startNode();
  13052. if (allowDirectives) {
  13053. this.state.strictErrors.clear();
  13054. }
  13055. this.expect(5);
  13056. if (createNewLexicalScope) {
  13057. this.scope.enter(SCOPE_OTHER);
  13058. }
  13059. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  13060. if (createNewLexicalScope) {
  13061. this.scope.exit();
  13062. }
  13063. return this.finishNode(node, "BlockStatement");
  13064. }
  13065. isValidDirective(stmt) {
  13066. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  13067. }
  13068. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  13069. const body = node.body = [];
  13070. const directives = node.directives = [];
  13071. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  13072. }
  13073. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  13074. const oldStrict = this.state.strict;
  13075. let hasStrictModeDirective = false;
  13076. let parsedNonDirective = false;
  13077. while (!this.match(end)) {
  13078. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  13079. if (directives && !parsedNonDirective) {
  13080. if (this.isValidDirective(stmt)) {
  13081. const directive = this.stmtToDirective(stmt);
  13082. directives.push(directive);
  13083. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  13084. hasStrictModeDirective = true;
  13085. this.setStrict(true);
  13086. }
  13087. continue;
  13088. }
  13089. parsedNonDirective = true;
  13090. this.state.strictErrors.clear();
  13091. }
  13092. body.push(stmt);
  13093. }
  13094. if (afterBlockParse) {
  13095. afterBlockParse.call(this, hasStrictModeDirective);
  13096. }
  13097. if (!oldStrict) {
  13098. this.setStrict(false);
  13099. }
  13100. this.next();
  13101. }
  13102. parseFor(node, init) {
  13103. node.init = init;
  13104. this.semicolon(false);
  13105. node.test = this.match(13) ? null : this.parseExpression();
  13106. this.semicolon(false);
  13107. node.update = this.match(11) ? null : this.parseExpression();
  13108. this.expect(11);
  13109. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13110. this.scope.exit();
  13111. this.state.labels.pop();
  13112. return this.finishNode(node, "ForStatement");
  13113. }
  13114. parseForIn(node, init, awaitAt) {
  13115. const isForIn = this.match(58);
  13116. this.next();
  13117. if (isForIn) {
  13118. if (awaitAt !== null) this.unexpected(awaitAt);
  13119. } else {
  13120. node.await = awaitAt !== null;
  13121. }
  13122. 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")) {
  13123. this.raise(Errors.ForInOfLoopInitializer, {
  13124. at: init,
  13125. type: isForIn ? "ForInStatement" : "ForOfStatement"
  13126. });
  13127. }
  13128. if (init.type === "AssignmentPattern") {
  13129. this.raise(Errors.InvalidLhs, {
  13130. at: init,
  13131. ancestor: {
  13132. type: "ForStatement"
  13133. }
  13134. });
  13135. }
  13136. node.left = init;
  13137. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  13138. this.expect(11);
  13139. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13140. this.scope.exit();
  13141. this.state.labels.pop();
  13142. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  13143. }
  13144. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  13145. const declarations = node.declarations = [];
  13146. node.kind = kind;
  13147. for (;;) {
  13148. const decl = this.startNode();
  13149. this.parseVarId(decl, kind);
  13150. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  13151. if (decl.init === null && !allowMissingInitializer) {
  13152. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  13153. this.raise(Errors.DeclarationMissingInitializer, {
  13154. at: this.state.lastTokEndLoc,
  13155. kind: "destructuring"
  13156. });
  13157. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  13158. this.raise(Errors.DeclarationMissingInitializer, {
  13159. at: this.state.lastTokEndLoc,
  13160. kind: "const"
  13161. });
  13162. }
  13163. }
  13164. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13165. if (!this.eat(12)) break;
  13166. }
  13167. return node;
  13168. }
  13169. parseVarId(decl, kind) {
  13170. if (kind === "using" && !this.inModule && this.match(96)) {
  13171. this.raise(Errors.AwaitInUsingBinding, {
  13172. at: this.state.startLoc
  13173. });
  13174. }
  13175. const id = this.parseBindingAtom();
  13176. this.checkLVal(id, {
  13177. in: {
  13178. type: "VariableDeclarator"
  13179. },
  13180. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  13181. });
  13182. decl.id = id;
  13183. }
  13184. parseAsyncFunctionExpression(node) {
  13185. return this.parseFunction(node, ParseFunctionFlag.Async);
  13186. }
  13187. parseFunction(node, flags = ParseFunctionFlag.Expression) {
  13188. const hangingDeclaration = flags & ParseFunctionFlag.HangingDeclaration;
  13189. const isDeclaration = !!(flags & ParseFunctionFlag.Declaration);
  13190. const requireId = isDeclaration && !(flags & ParseFunctionFlag.NullableId);
  13191. const isAsync = !!(flags & ParseFunctionFlag.Async);
  13192. this.initFunction(node, isAsync);
  13193. if (this.match(55)) {
  13194. if (hangingDeclaration) {
  13195. this.raise(Errors.GeneratorInSingleStatementContext, {
  13196. at: this.state.startLoc
  13197. });
  13198. }
  13199. this.next();
  13200. node.generator = true;
  13201. }
  13202. if (isDeclaration) {
  13203. node.id = this.parseFunctionId(requireId);
  13204. }
  13205. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13206. this.state.maybeInArrowParameters = false;
  13207. this.scope.enter(SCOPE_FUNCTION);
  13208. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13209. if (!isDeclaration) {
  13210. node.id = this.parseFunctionId();
  13211. }
  13212. this.parseFunctionParams(node, false);
  13213. this.withSmartMixTopicForbiddingContext(() => {
  13214. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  13215. });
  13216. this.prodParam.exit();
  13217. this.scope.exit();
  13218. if (isDeclaration && !hangingDeclaration) {
  13219. this.registerFunctionStatementId(node);
  13220. }
  13221. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13222. return node;
  13223. }
  13224. parseFunctionId(requireId) {
  13225. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13226. }
  13227. parseFunctionParams(node, isConstructor) {
  13228. this.expect(10);
  13229. this.expressionScope.enter(newParameterDeclarationScope());
  13230. node.params = this.parseBindingList(11, 41, ParseBindingListFlags.IS_FUNCTION_PARAMS | (isConstructor ? ParseBindingListFlags.IS_CONSTRUCTOR_PARAMS : 0));
  13231. this.expressionScope.exit();
  13232. }
  13233. registerFunctionStatementId(node) {
  13234. if (!node.id) return;
  13235. this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
  13236. }
  13237. parseClass(node, isStatement, optionalId) {
  13238. this.next();
  13239. const oldStrict = this.state.strict;
  13240. this.state.strict = true;
  13241. this.parseClassId(node, isStatement, optionalId);
  13242. this.parseClassSuper(node);
  13243. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13244. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13245. }
  13246. isClassProperty() {
  13247. return this.match(29) || this.match(13) || this.match(8);
  13248. }
  13249. isClassMethod() {
  13250. return this.match(10);
  13251. }
  13252. isNonstaticConstructor(method) {
  13253. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  13254. }
  13255. parseClassBody(hadSuperClass, oldStrict) {
  13256. this.classScope.enter();
  13257. const state = {
  13258. hadConstructor: false,
  13259. hadSuperClass
  13260. };
  13261. let decorators = [];
  13262. const classBody = this.startNode();
  13263. classBody.body = [];
  13264. this.expect(5);
  13265. this.withSmartMixTopicForbiddingContext(() => {
  13266. while (!this.match(8)) {
  13267. if (this.eat(13)) {
  13268. if (decorators.length > 0) {
  13269. throw this.raise(Errors.DecoratorSemicolon, {
  13270. at: this.state.lastTokEndLoc
  13271. });
  13272. }
  13273. continue;
  13274. }
  13275. if (this.match(26)) {
  13276. decorators.push(this.parseDecorator());
  13277. continue;
  13278. }
  13279. const member = this.startNode();
  13280. if (decorators.length) {
  13281. member.decorators = decorators;
  13282. this.resetStartLocationFromNode(member, decorators[0]);
  13283. decorators = [];
  13284. }
  13285. this.parseClassMember(classBody, member, state);
  13286. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13287. this.raise(Errors.DecoratorConstructor, {
  13288. at: member
  13289. });
  13290. }
  13291. }
  13292. });
  13293. this.state.strict = oldStrict;
  13294. this.next();
  13295. if (decorators.length) {
  13296. throw this.raise(Errors.TrailingDecorator, {
  13297. at: this.state.startLoc
  13298. });
  13299. }
  13300. this.classScope.exit();
  13301. return this.finishNode(classBody, "ClassBody");
  13302. }
  13303. parseClassMemberFromModifier(classBody, member) {
  13304. const key = this.parseIdentifier(true);
  13305. if (this.isClassMethod()) {
  13306. const method = member;
  13307. method.kind = "method";
  13308. method.computed = false;
  13309. method.key = key;
  13310. method.static = false;
  13311. this.pushClassMethod(classBody, method, false, false, false, false);
  13312. return true;
  13313. } else if (this.isClassProperty()) {
  13314. const prop = member;
  13315. prop.computed = false;
  13316. prop.key = key;
  13317. prop.static = false;
  13318. classBody.body.push(this.parseClassProperty(prop));
  13319. return true;
  13320. }
  13321. this.resetPreviousNodeTrailingComments(key);
  13322. return false;
  13323. }
  13324. parseClassMember(classBody, member, state) {
  13325. const isStatic = this.isContextual(104);
  13326. if (isStatic) {
  13327. if (this.parseClassMemberFromModifier(classBody, member)) {
  13328. return;
  13329. }
  13330. if (this.eat(5)) {
  13331. this.parseClassStaticBlock(classBody, member);
  13332. return;
  13333. }
  13334. }
  13335. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13336. }
  13337. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13338. const publicMethod = member;
  13339. const privateMethod = member;
  13340. const publicProp = member;
  13341. const privateProp = member;
  13342. const accessorProp = member;
  13343. const method = publicMethod;
  13344. const publicMember = publicMethod;
  13345. member.static = isStatic;
  13346. this.parsePropertyNamePrefixOperator(member);
  13347. if (this.eat(55)) {
  13348. method.kind = "method";
  13349. const isPrivateName = this.match(136);
  13350. this.parseClassElementName(method);
  13351. if (isPrivateName) {
  13352. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13353. return;
  13354. }
  13355. if (this.isNonstaticConstructor(publicMethod)) {
  13356. this.raise(Errors.ConstructorIsGenerator, {
  13357. at: publicMethod.key
  13358. });
  13359. }
  13360. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13361. return;
  13362. }
  13363. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  13364. const isPrivate = this.match(136);
  13365. const key = this.parseClassElementName(member);
  13366. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13367. this.parsePostMemberNameModifiers(publicMember);
  13368. if (this.isClassMethod()) {
  13369. method.kind = "method";
  13370. if (isPrivate) {
  13371. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13372. return;
  13373. }
  13374. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13375. let allowsDirectSuper = false;
  13376. if (isConstructor) {
  13377. publicMethod.kind = "constructor";
  13378. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13379. this.raise(Errors.DuplicateConstructor, {
  13380. at: key
  13381. });
  13382. }
  13383. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13384. this.raise(Errors.OverrideOnConstructor, {
  13385. at: key
  13386. });
  13387. }
  13388. state.hadConstructor = true;
  13389. allowsDirectSuper = state.hadSuperClass;
  13390. }
  13391. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13392. } else if (this.isClassProperty()) {
  13393. if (isPrivate) {
  13394. this.pushClassPrivateProperty(classBody, privateProp);
  13395. } else {
  13396. this.pushClassProperty(classBody, publicProp);
  13397. }
  13398. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  13399. this.resetPreviousNodeTrailingComments(key);
  13400. const isGenerator = this.eat(55);
  13401. if (publicMember.optional) {
  13402. this.unexpected(maybeQuestionTokenStartLoc);
  13403. }
  13404. method.kind = "method";
  13405. const isPrivate = this.match(136);
  13406. this.parseClassElementName(method);
  13407. this.parsePostMemberNameModifiers(publicMember);
  13408. if (isPrivate) {
  13409. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13410. } else {
  13411. if (this.isNonstaticConstructor(publicMethod)) {
  13412. this.raise(Errors.ConstructorIsAsync, {
  13413. at: publicMethod.key
  13414. });
  13415. }
  13416. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13417. }
  13418. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  13419. this.resetPreviousNodeTrailingComments(key);
  13420. method.kind = key.name;
  13421. const isPrivate = this.match(136);
  13422. this.parseClassElementName(publicMethod);
  13423. if (isPrivate) {
  13424. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13425. } else {
  13426. if (this.isNonstaticConstructor(publicMethod)) {
  13427. this.raise(Errors.ConstructorIsAccessor, {
  13428. at: publicMethod.key
  13429. });
  13430. }
  13431. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13432. }
  13433. this.checkGetterSetterParams(publicMethod);
  13434. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  13435. this.expectPlugin("decoratorAutoAccessors");
  13436. this.resetPreviousNodeTrailingComments(key);
  13437. const isPrivate = this.match(136);
  13438. this.parseClassElementName(publicProp);
  13439. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13440. } else if (this.isLineTerminator()) {
  13441. if (isPrivate) {
  13442. this.pushClassPrivateProperty(classBody, privateProp);
  13443. } else {
  13444. this.pushClassProperty(classBody, publicProp);
  13445. }
  13446. } else {
  13447. this.unexpected();
  13448. }
  13449. }
  13450. parseClassElementName(member) {
  13451. const {
  13452. type,
  13453. value
  13454. } = this.state;
  13455. if ((type === 130 || type === 131) && member.static && value === "prototype") {
  13456. this.raise(Errors.StaticPrototype, {
  13457. at: this.state.startLoc
  13458. });
  13459. }
  13460. if (type === 136) {
  13461. if (value === "constructor") {
  13462. this.raise(Errors.ConstructorClassPrivateField, {
  13463. at: this.state.startLoc
  13464. });
  13465. }
  13466. const key = this.parsePrivateName();
  13467. member.key = key;
  13468. return key;
  13469. }
  13470. return this.parsePropertyName(member);
  13471. }
  13472. parseClassStaticBlock(classBody, member) {
  13473. var _member$decorators;
  13474. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13475. const oldLabels = this.state.labels;
  13476. this.state.labels = [];
  13477. this.prodParam.enter(PARAM);
  13478. const body = member.body = [];
  13479. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13480. this.prodParam.exit();
  13481. this.scope.exit();
  13482. this.state.labels = oldLabels;
  13483. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13484. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13485. this.raise(Errors.DecoratorStaticBlock, {
  13486. at: member
  13487. });
  13488. }
  13489. }
  13490. pushClassProperty(classBody, prop) {
  13491. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13492. this.raise(Errors.ConstructorClassField, {
  13493. at: prop.key
  13494. });
  13495. }
  13496. classBody.body.push(this.parseClassProperty(prop));
  13497. }
  13498. pushClassPrivateProperty(classBody, prop) {
  13499. const node = this.parseClassPrivateProperty(prop);
  13500. classBody.body.push(node);
  13501. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13502. }
  13503. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13504. if (!isPrivate && !prop.computed) {
  13505. const key = prop.key;
  13506. if (key.name === "constructor" || key.value === "constructor") {
  13507. this.raise(Errors.ConstructorClassField, {
  13508. at: key
  13509. });
  13510. }
  13511. }
  13512. const node = this.parseClassAccessorProperty(prop);
  13513. classBody.body.push(node);
  13514. if (isPrivate) {
  13515. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13516. }
  13517. }
  13518. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13519. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13520. }
  13521. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13522. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13523. classBody.body.push(node);
  13524. const kind = node.kind === "get" ? node.static ? CLASS_ELEMENT_STATIC_GETTER : CLASS_ELEMENT_INSTANCE_GETTER : node.kind === "set" ? node.static ? CLASS_ELEMENT_STATIC_SETTER : CLASS_ELEMENT_INSTANCE_SETTER : CLASS_ELEMENT_OTHER;
  13525. this.declareClassPrivateMethodInScope(node, kind);
  13526. }
  13527. declareClassPrivateMethodInScope(node, kind) {
  13528. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13529. }
  13530. parsePostMemberNameModifiers(methodOrProp) {}
  13531. parseClassPrivateProperty(node) {
  13532. this.parseInitializer(node);
  13533. this.semicolon();
  13534. return this.finishNode(node, "ClassPrivateProperty");
  13535. }
  13536. parseClassProperty(node) {
  13537. this.parseInitializer(node);
  13538. this.semicolon();
  13539. return this.finishNode(node, "ClassProperty");
  13540. }
  13541. parseClassAccessorProperty(node) {
  13542. this.parseInitializer(node);
  13543. this.semicolon();
  13544. return this.finishNode(node, "ClassAccessorProperty");
  13545. }
  13546. parseInitializer(node) {
  13547. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13548. this.expressionScope.enter(newExpressionScope());
  13549. this.prodParam.enter(PARAM);
  13550. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13551. this.expressionScope.exit();
  13552. this.prodParam.exit();
  13553. this.scope.exit();
  13554. }
  13555. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13556. if (tokenIsIdentifier(this.state.type)) {
  13557. node.id = this.parseIdentifier();
  13558. if (isStatement) {
  13559. this.declareNameFromIdentifier(node.id, bindingType);
  13560. }
  13561. } else {
  13562. if (optionalId || !isStatement) {
  13563. node.id = null;
  13564. } else {
  13565. throw this.raise(Errors.MissingClassName, {
  13566. at: this.state.startLoc
  13567. });
  13568. }
  13569. }
  13570. }
  13571. parseClassSuper(node) {
  13572. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13573. }
  13574. parseExport(node, decorators) {
  13575. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13576. const parseAfterDefault = !hasDefault || this.eat(12);
  13577. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13578. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13579. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13580. const isFromRequired = hasDefault || hasStar;
  13581. if (hasStar && !hasNamespace) {
  13582. if (hasDefault) this.unexpected();
  13583. if (decorators) {
  13584. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13585. at: node
  13586. });
  13587. }
  13588. this.parseExportFrom(node, true);
  13589. return this.finishNode(node, "ExportAllDeclaration");
  13590. }
  13591. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13592. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13593. throw this.unexpected(null, 5);
  13594. }
  13595. if (hasNamespace && parseAfterNamespace) {
  13596. throw this.unexpected(null, 97);
  13597. }
  13598. let hasDeclaration;
  13599. if (isFromRequired || hasSpecifiers) {
  13600. hasDeclaration = false;
  13601. if (decorators) {
  13602. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13603. at: node
  13604. });
  13605. }
  13606. this.parseExportFrom(node, isFromRequired);
  13607. } else {
  13608. hasDeclaration = this.maybeParseExportDeclaration(node);
  13609. }
  13610. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13611. var _node2$declaration;
  13612. const node2 = node;
  13613. this.checkExport(node2, true, false, !!node2.source);
  13614. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13615. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13616. } else if (decorators) {
  13617. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13618. at: node
  13619. });
  13620. }
  13621. return this.finishNode(node2, "ExportNamedDeclaration");
  13622. }
  13623. if (this.eat(65)) {
  13624. const node2 = node;
  13625. const decl = this.parseExportDefaultExpression();
  13626. node2.declaration = decl;
  13627. if (decl.type === "ClassDeclaration") {
  13628. this.maybeTakeDecorators(decorators, decl, node2);
  13629. } else if (decorators) {
  13630. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13631. at: node
  13632. });
  13633. }
  13634. this.checkExport(node2, true, true);
  13635. return this.finishNode(node2, "ExportDefaultDeclaration");
  13636. }
  13637. throw this.unexpected(null, 5);
  13638. }
  13639. eatExportStar(node) {
  13640. return this.eat(55);
  13641. }
  13642. maybeParseExportDefaultSpecifier(node) {
  13643. if (this.isExportDefaultSpecifier()) {
  13644. this.expectPlugin("exportDefaultFrom");
  13645. const specifier = this.startNode();
  13646. specifier.exported = this.parseIdentifier(true);
  13647. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13648. return true;
  13649. }
  13650. return false;
  13651. }
  13652. maybeParseExportNamespaceSpecifier(node) {
  13653. if (this.isContextual(93)) {
  13654. if (!node.specifiers) node.specifiers = [];
  13655. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13656. this.next();
  13657. specifier.exported = this.parseModuleExportName();
  13658. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13659. return true;
  13660. }
  13661. return false;
  13662. }
  13663. maybeParseExportNamedSpecifiers(node) {
  13664. if (this.match(5)) {
  13665. if (!node.specifiers) node.specifiers = [];
  13666. const isTypeExport = node.exportKind === "type";
  13667. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13668. node.source = null;
  13669. node.declaration = null;
  13670. if (this.hasPlugin("importAssertions")) {
  13671. node.assertions = [];
  13672. }
  13673. return true;
  13674. }
  13675. return false;
  13676. }
  13677. maybeParseExportDeclaration(node) {
  13678. if (this.shouldParseExportDeclaration()) {
  13679. node.specifiers = [];
  13680. node.source = null;
  13681. if (this.hasPlugin("importAssertions")) {
  13682. node.assertions = [];
  13683. }
  13684. node.declaration = this.parseExportDeclaration(node);
  13685. return true;
  13686. }
  13687. return false;
  13688. }
  13689. isAsyncFunction() {
  13690. if (!this.isContextual(95)) return false;
  13691. const next = this.nextTokenStart();
  13692. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13693. }
  13694. parseExportDefaultExpression() {
  13695. const expr = this.startNode();
  13696. if (this.match(68)) {
  13697. this.next();
  13698. return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId);
  13699. } else if (this.isAsyncFunction()) {
  13700. this.next();
  13701. this.next();
  13702. return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId | ParseFunctionFlag.Async);
  13703. }
  13704. if (this.match(80)) {
  13705. return this.parseClass(expr, true, true);
  13706. }
  13707. if (this.match(26)) {
  13708. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13709. this.raise(Errors.DecoratorBeforeExport, {
  13710. at: this.state.startLoc
  13711. });
  13712. }
  13713. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13714. }
  13715. if (this.match(75) || this.match(74) || this.isLet()) {
  13716. throw this.raise(Errors.UnsupportedDefaultExport, {
  13717. at: this.state.startLoc
  13718. });
  13719. }
  13720. const res = this.parseMaybeAssignAllowIn();
  13721. this.semicolon();
  13722. return res;
  13723. }
  13724. parseExportDeclaration(node) {
  13725. if (this.match(80)) {
  13726. const node = this.parseClass(this.startNode(), true, false);
  13727. return node;
  13728. }
  13729. return this.parseStatementListItem();
  13730. }
  13731. isExportDefaultSpecifier() {
  13732. const {
  13733. type
  13734. } = this.state;
  13735. if (tokenIsIdentifier(type)) {
  13736. if (type === 95 && !this.state.containsEsc || type === 99) {
  13737. return false;
  13738. }
  13739. if ((type === 128 || type === 127) && !this.state.containsEsc) {
  13740. const {
  13741. type: nextType
  13742. } = this.lookahead();
  13743. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13744. this.expectOnePlugin(["flow", "typescript"]);
  13745. return false;
  13746. }
  13747. }
  13748. } else if (!this.match(65)) {
  13749. return false;
  13750. }
  13751. const next = this.nextTokenStart();
  13752. const hasFrom = this.isUnparsedContextual(next, "from");
  13753. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13754. return true;
  13755. }
  13756. if (this.match(65) && hasFrom) {
  13757. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13758. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13759. }
  13760. return false;
  13761. }
  13762. parseExportFrom(node, expect) {
  13763. if (this.eatContextual(97)) {
  13764. node.source = this.parseImportSource();
  13765. this.checkExport(node);
  13766. const assertions = this.maybeParseImportAssertions();
  13767. if (assertions) {
  13768. node.assertions = assertions;
  13769. this.checkJSONModuleImport(node);
  13770. }
  13771. } else if (expect) {
  13772. this.unexpected();
  13773. }
  13774. this.semicolon();
  13775. }
  13776. shouldParseExportDeclaration() {
  13777. const {
  13778. type
  13779. } = this.state;
  13780. if (type === 26) {
  13781. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13782. if (this.hasPlugin("decorators")) {
  13783. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13784. this.raise(Errors.DecoratorBeforeExport, {
  13785. at: this.state.startLoc
  13786. });
  13787. }
  13788. return true;
  13789. }
  13790. }
  13791. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13792. }
  13793. checkExport(node, checkNames, isDefault, isFrom) {
  13794. if (checkNames) {
  13795. if (isDefault) {
  13796. this.checkDuplicateExports(node, "default");
  13797. if (this.hasPlugin("exportDefaultFrom")) {
  13798. var _declaration$extra;
  13799. const declaration = node.declaration;
  13800. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13801. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13802. at: declaration
  13803. });
  13804. }
  13805. }
  13806. } else if (node.specifiers && node.specifiers.length) {
  13807. for (const specifier of node.specifiers) {
  13808. const {
  13809. exported
  13810. } = specifier;
  13811. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13812. this.checkDuplicateExports(specifier, exportName);
  13813. if (!isFrom && specifier.local) {
  13814. const {
  13815. local
  13816. } = specifier;
  13817. if (local.type !== "Identifier") {
  13818. this.raise(Errors.ExportBindingIsString, {
  13819. at: specifier,
  13820. localName: local.value,
  13821. exportName
  13822. });
  13823. } else {
  13824. this.checkReservedWord(local.name, local.loc.start, true, false);
  13825. this.scope.checkLocalExport(local);
  13826. }
  13827. }
  13828. }
  13829. } else if (node.declaration) {
  13830. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13831. const id = node.declaration.id;
  13832. if (!id) throw new Error("Assertion failure");
  13833. this.checkDuplicateExports(node, id.name);
  13834. } else if (node.declaration.type === "VariableDeclaration") {
  13835. for (const declaration of node.declaration.declarations) {
  13836. this.checkDeclaration(declaration.id);
  13837. }
  13838. }
  13839. }
  13840. }
  13841. }
  13842. checkDeclaration(node) {
  13843. if (node.type === "Identifier") {
  13844. this.checkDuplicateExports(node, node.name);
  13845. } else if (node.type === "ObjectPattern") {
  13846. for (const prop of node.properties) {
  13847. this.checkDeclaration(prop);
  13848. }
  13849. } else if (node.type === "ArrayPattern") {
  13850. for (const elem of node.elements) {
  13851. if (elem) {
  13852. this.checkDeclaration(elem);
  13853. }
  13854. }
  13855. } else if (node.type === "ObjectProperty") {
  13856. this.checkDeclaration(node.value);
  13857. } else if (node.type === "RestElement") {
  13858. this.checkDeclaration(node.argument);
  13859. } else if (node.type === "AssignmentPattern") {
  13860. this.checkDeclaration(node.left);
  13861. }
  13862. }
  13863. checkDuplicateExports(node, exportName) {
  13864. if (this.exportedIdentifiers.has(exportName)) {
  13865. if (exportName === "default") {
  13866. this.raise(Errors.DuplicateDefaultExport, {
  13867. at: node
  13868. });
  13869. } else {
  13870. this.raise(Errors.DuplicateExport, {
  13871. at: node,
  13872. exportName
  13873. });
  13874. }
  13875. }
  13876. this.exportedIdentifiers.add(exportName);
  13877. }
  13878. parseExportSpecifiers(isInTypeExport) {
  13879. const nodes = [];
  13880. let first = true;
  13881. this.expect(5);
  13882. while (!this.eat(8)) {
  13883. if (first) {
  13884. first = false;
  13885. } else {
  13886. this.expect(12);
  13887. if (this.eat(8)) break;
  13888. }
  13889. const isMaybeTypeOnly = this.isContextual(128);
  13890. const isString = this.match(131);
  13891. const node = this.startNode();
  13892. node.local = this.parseModuleExportName();
  13893. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13894. }
  13895. return nodes;
  13896. }
  13897. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13898. if (this.eatContextual(93)) {
  13899. node.exported = this.parseModuleExportName();
  13900. } else if (isString) {
  13901. node.exported = cloneStringLiteral(node.local);
  13902. } else if (!node.exported) {
  13903. node.exported = cloneIdentifier(node.local);
  13904. }
  13905. return this.finishNode(node, "ExportSpecifier");
  13906. }
  13907. parseModuleExportName() {
  13908. if (this.match(131)) {
  13909. const result = this.parseStringLiteral(this.state.value);
  13910. const surrogate = result.value.match(loneSurrogate);
  13911. if (surrogate) {
  13912. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13913. at: result,
  13914. surrogateCharCode: surrogate[0].charCodeAt(0)
  13915. });
  13916. }
  13917. return result;
  13918. }
  13919. return this.parseIdentifier(true);
  13920. }
  13921. isJSONModuleImport(node) {
  13922. if (node.assertions != null) {
  13923. return node.assertions.some(({
  13924. key,
  13925. value
  13926. }) => {
  13927. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13928. });
  13929. }
  13930. return false;
  13931. }
  13932. checkImportReflection(node) {
  13933. if (node.module) {
  13934. var _node$assertions;
  13935. if (node.specifiers.length !== 1 || node.specifiers[0].type !== "ImportDefaultSpecifier") {
  13936. this.raise(Errors.ImportReflectionNotBinding, {
  13937. at: node.specifiers[0].loc.start
  13938. });
  13939. }
  13940. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  13941. this.raise(Errors.ImportReflectionHasAssertion, {
  13942. at: node.specifiers[0].loc.start
  13943. });
  13944. }
  13945. }
  13946. }
  13947. checkJSONModuleImport(node) {
  13948. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13949. const {
  13950. specifiers
  13951. } = node;
  13952. if (specifiers != null) {
  13953. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13954. let imported;
  13955. if (specifier.type === "ExportSpecifier") {
  13956. imported = specifier.local;
  13957. } else if (specifier.type === "ImportSpecifier") {
  13958. imported = specifier.imported;
  13959. }
  13960. if (imported !== undefined) {
  13961. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13962. }
  13963. });
  13964. if (nonDefaultNamedSpecifier !== undefined) {
  13965. this.raise(Errors.ImportJSONBindingNotDefault, {
  13966. at: nonDefaultNamedSpecifier.loc.start
  13967. });
  13968. }
  13969. }
  13970. }
  13971. }
  13972. parseMaybeImportReflection(node) {
  13973. let isImportReflection = false;
  13974. if (this.isContextual(125)) {
  13975. const lookahead = this.lookahead();
  13976. const nextType = lookahead.type;
  13977. if (tokenIsIdentifier(nextType)) {
  13978. if (nextType !== 97) {
  13979. isImportReflection = true;
  13980. } else {
  13981. const nextNextTokenFirstChar = this.input.charCodeAt(this.nextTokenStartSince(lookahead.end));
  13982. if (nextNextTokenFirstChar === 102) {
  13983. isImportReflection = true;
  13984. }
  13985. }
  13986. } else if (nextType !== 12) {
  13987. isImportReflection = true;
  13988. }
  13989. }
  13990. if (isImportReflection) {
  13991. this.expectPlugin("importReflection");
  13992. this.next();
  13993. node.module = true;
  13994. } else if (this.hasPlugin("importReflection")) {
  13995. node.module = false;
  13996. }
  13997. }
  13998. parseImport(node) {
  13999. node.specifiers = [];
  14000. if (!this.match(131)) {
  14001. this.parseMaybeImportReflection(node);
  14002. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  14003. const parseNext = !hasDefault || this.eat(12);
  14004. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  14005. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  14006. this.expectContextual(97);
  14007. }
  14008. node.source = this.parseImportSource();
  14009. const assertions = this.maybeParseImportAssertions();
  14010. if (assertions) {
  14011. node.assertions = assertions;
  14012. } else {
  14013. const attributes = this.maybeParseModuleAttributes();
  14014. if (attributes) {
  14015. node.attributes = attributes;
  14016. }
  14017. }
  14018. this.checkImportReflection(node);
  14019. this.checkJSONModuleImport(node);
  14020. this.semicolon();
  14021. return this.finishNode(node, "ImportDeclaration");
  14022. }
  14023. parseImportSource() {
  14024. if (!this.match(131)) this.unexpected();
  14025. return this.parseExprAtom();
  14026. }
  14027. shouldParseDefaultImport(node) {
  14028. return tokenIsIdentifier(this.state.type);
  14029. }
  14030. parseImportSpecifierLocal(node, specifier, type) {
  14031. specifier.local = this.parseIdentifier();
  14032. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  14033. }
  14034. finishImportSpecifier(specifier, type, bindingType = BIND_LEXICAL) {
  14035. this.checkLVal(specifier.local, {
  14036. in: {
  14037. type
  14038. },
  14039. binding: bindingType
  14040. });
  14041. return this.finishNode(specifier, type);
  14042. }
  14043. parseAssertEntries() {
  14044. const attrs = [];
  14045. const attrNames = new Set();
  14046. do {
  14047. if (this.match(8)) {
  14048. break;
  14049. }
  14050. const node = this.startNode();
  14051. const keyName = this.state.value;
  14052. if (attrNames.has(keyName)) {
  14053. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14054. at: this.state.startLoc,
  14055. key: keyName
  14056. });
  14057. }
  14058. attrNames.add(keyName);
  14059. if (this.match(131)) {
  14060. node.key = this.parseStringLiteral(keyName);
  14061. } else {
  14062. node.key = this.parseIdentifier(true);
  14063. }
  14064. this.expect(14);
  14065. if (!this.match(131)) {
  14066. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14067. at: this.state.startLoc
  14068. });
  14069. }
  14070. node.value = this.parseStringLiteral(this.state.value);
  14071. attrs.push(this.finishNode(node, "ImportAttribute"));
  14072. } while (this.eat(12));
  14073. return attrs;
  14074. }
  14075. maybeParseModuleAttributes() {
  14076. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  14077. this.expectPlugin("moduleAttributes");
  14078. this.next();
  14079. } else {
  14080. if (this.hasPlugin("moduleAttributes")) return [];
  14081. return null;
  14082. }
  14083. const attrs = [];
  14084. const attributes = new Set();
  14085. do {
  14086. const node = this.startNode();
  14087. node.key = this.parseIdentifier(true);
  14088. if (node.key.name !== "type") {
  14089. this.raise(Errors.ModuleAttributeDifferentFromType, {
  14090. at: node.key
  14091. });
  14092. }
  14093. if (attributes.has(node.key.name)) {
  14094. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14095. at: node.key,
  14096. key: node.key.name
  14097. });
  14098. }
  14099. attributes.add(node.key.name);
  14100. this.expect(14);
  14101. if (!this.match(131)) {
  14102. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14103. at: this.state.startLoc
  14104. });
  14105. }
  14106. node.value = this.parseStringLiteral(this.state.value);
  14107. this.finishNode(node, "ImportAttribute");
  14108. attrs.push(node);
  14109. } while (this.eat(12));
  14110. return attrs;
  14111. }
  14112. maybeParseImportAssertions() {
  14113. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  14114. this.expectPlugin("importAssertions");
  14115. this.next();
  14116. } else {
  14117. if (this.hasPlugin("importAssertions")) return [];
  14118. return null;
  14119. }
  14120. this.eat(5);
  14121. const attrs = this.parseAssertEntries();
  14122. this.eat(8);
  14123. return attrs;
  14124. }
  14125. maybeParseDefaultImportSpecifier(node) {
  14126. if (this.shouldParseDefaultImport(node)) {
  14127. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  14128. return true;
  14129. }
  14130. return false;
  14131. }
  14132. maybeParseStarImportSpecifier(node) {
  14133. if (this.match(55)) {
  14134. const specifier = this.startNode();
  14135. this.next();
  14136. this.expectContextual(93);
  14137. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  14138. return true;
  14139. }
  14140. return false;
  14141. }
  14142. parseNamedImportSpecifiers(node) {
  14143. let first = true;
  14144. this.expect(5);
  14145. while (!this.eat(8)) {
  14146. if (first) {
  14147. first = false;
  14148. } else {
  14149. if (this.eat(14)) {
  14150. throw this.raise(Errors.DestructureNamedImport, {
  14151. at: this.state.startLoc
  14152. });
  14153. }
  14154. this.expect(12);
  14155. if (this.eat(8)) break;
  14156. }
  14157. const specifier = this.startNode();
  14158. const importedIsString = this.match(131);
  14159. const isMaybeTypeOnly = this.isContextual(128);
  14160. specifier.imported = this.parseModuleExportName();
  14161. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  14162. node.specifiers.push(importSpecifier);
  14163. }
  14164. }
  14165. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  14166. if (this.eatContextual(93)) {
  14167. specifier.local = this.parseIdentifier();
  14168. } else {
  14169. const {
  14170. imported
  14171. } = specifier;
  14172. if (importedIsString) {
  14173. throw this.raise(Errors.ImportBindingIsString, {
  14174. at: specifier,
  14175. importName: imported.value
  14176. });
  14177. }
  14178. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  14179. if (!specifier.local) {
  14180. specifier.local = cloneIdentifier(imported);
  14181. }
  14182. }
  14183. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  14184. }
  14185. isThisParam(param) {
  14186. return param.type === "Identifier" && param.name === "this";
  14187. }
  14188. }
  14189. class Parser extends StatementParser {
  14190. constructor(options, input) {
  14191. options = getOptions(options);
  14192. super(options, input);
  14193. this.options = options;
  14194. this.initializeScopes();
  14195. this.plugins = pluginsMap(this.options.plugins);
  14196. this.filename = options.sourceFilename;
  14197. }
  14198. getScopeHandler() {
  14199. return ScopeHandler;
  14200. }
  14201. parse() {
  14202. this.enterInitialScopes();
  14203. const file = this.startNode();
  14204. const program = this.startNode();
  14205. this.nextToken();
  14206. file.errors = null;
  14207. this.parseTopLevel(file, program);
  14208. file.errors = this.state.errors;
  14209. return file;
  14210. }
  14211. }
  14212. function pluginsMap(plugins) {
  14213. const pluginMap = new Map();
  14214. for (const plugin of plugins) {
  14215. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  14216. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  14217. }
  14218. return pluginMap;
  14219. }
  14220. function parse(input, options) {
  14221. var _options;
  14222. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  14223. options = Object.assign({}, options);
  14224. try {
  14225. options.sourceType = "module";
  14226. const parser = getParser(options, input);
  14227. const ast = parser.parse();
  14228. if (parser.sawUnambiguousESM) {
  14229. return ast;
  14230. }
  14231. if (parser.ambiguousScriptDifferentAst) {
  14232. try {
  14233. options.sourceType = "script";
  14234. return getParser(options, input).parse();
  14235. } catch (_unused) {}
  14236. } else {
  14237. ast.program.sourceType = "script";
  14238. }
  14239. return ast;
  14240. } catch (moduleError) {
  14241. try {
  14242. options.sourceType = "script";
  14243. return getParser(options, input).parse();
  14244. } catch (_unused2) {}
  14245. throw moduleError;
  14246. }
  14247. } else {
  14248. return getParser(options, input).parse();
  14249. }
  14250. }
  14251. function parseExpression(input, options) {
  14252. const parser = getParser(options, input);
  14253. if (parser.options.strictMode) {
  14254. parser.state.strict = true;
  14255. }
  14256. return parser.getExpression();
  14257. }
  14258. function generateExportedTokenTypes(internalTokenTypes) {
  14259. const tokenTypes = {};
  14260. for (const typeName of Object.keys(internalTokenTypes)) {
  14261. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14262. }
  14263. return tokenTypes;
  14264. }
  14265. const tokTypes = generateExportedTokenTypes(tt);
  14266. function getParser(options, input) {
  14267. let cls = Parser;
  14268. if (options != null && options.plugins) {
  14269. validatePlugins(options.plugins);
  14270. cls = getParserClass(options.plugins);
  14271. }
  14272. return new cls(options, input);
  14273. }
  14274. const parserClassCache = {};
  14275. function getParserClass(pluginsFromOptions) {
  14276. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  14277. const key = pluginList.join("/");
  14278. let cls = parserClassCache[key];
  14279. if (!cls) {
  14280. cls = Parser;
  14281. for (const plugin of pluginList) {
  14282. cls = mixinPlugins[plugin](cls);
  14283. }
  14284. parserClassCache[key] = cls;
  14285. }
  14286. return cls;
  14287. }
  14288. exports.parse = parse;
  14289. exports.parseExpression = parseExpression;
  14290. exports.tokTypes = tokTypes;
  14291. //# sourceMappingURL=index.js.map