index.js 422 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const beforeExpr = true;
  4. const startsExpr = true;
  5. const isLoop = true;
  6. const isAssign = true;
  7. const prefix = true;
  8. const postfix = true;
  9. class TokenType {
  10. constructor(label, conf = {}) {
  11. this.label = void 0;
  12. this.keyword = void 0;
  13. this.beforeExpr = void 0;
  14. this.startsExpr = void 0;
  15. this.rightAssociative = void 0;
  16. this.isLoop = void 0;
  17. this.isAssign = void 0;
  18. this.prefix = void 0;
  19. this.postfix = void 0;
  20. this.binop = void 0;
  21. this.updateContext = void 0;
  22. this.label = label;
  23. this.keyword = conf.keyword;
  24. this.beforeExpr = !!conf.beforeExpr;
  25. this.startsExpr = !!conf.startsExpr;
  26. this.rightAssociative = !!conf.rightAssociative;
  27. this.isLoop = !!conf.isLoop;
  28. this.isAssign = !!conf.isAssign;
  29. this.prefix = !!conf.prefix;
  30. this.postfix = !!conf.postfix;
  31. this.binop = conf.binop != null ? conf.binop : null;
  32. this.updateContext = null;
  33. }
  34. }
  35. const keywords$1 = new Map();
  36. function createKeyword(name, options = {}) {
  37. options.keyword = name;
  38. const token = new TokenType(name, options);
  39. keywords$1.set(name, token);
  40. return token;
  41. }
  42. function createBinop(name, binop) {
  43. return new TokenType(name, {
  44. beforeExpr,
  45. binop
  46. });
  47. }
  48. const types$1 = {
  49. num: new TokenType("num", {
  50. startsExpr
  51. }),
  52. bigint: new TokenType("bigint", {
  53. startsExpr
  54. }),
  55. decimal: new TokenType("decimal", {
  56. startsExpr
  57. }),
  58. regexp: new TokenType("regexp", {
  59. startsExpr
  60. }),
  61. string: new TokenType("string", {
  62. startsExpr
  63. }),
  64. name: new TokenType("name", {
  65. startsExpr
  66. }),
  67. privateName: new TokenType("#name", {
  68. startsExpr
  69. }),
  70. eof: new TokenType("eof"),
  71. bracketL: new TokenType("[", {
  72. beforeExpr,
  73. startsExpr
  74. }),
  75. bracketHashL: new TokenType("#[", {
  76. beforeExpr,
  77. startsExpr
  78. }),
  79. bracketBarL: new TokenType("[|", {
  80. beforeExpr,
  81. startsExpr
  82. }),
  83. bracketR: new TokenType("]"),
  84. bracketBarR: new TokenType("|]"),
  85. braceL: new TokenType("{", {
  86. beforeExpr,
  87. startsExpr
  88. }),
  89. braceBarL: new TokenType("{|", {
  90. beforeExpr,
  91. startsExpr
  92. }),
  93. braceHashL: new TokenType("#{", {
  94. beforeExpr,
  95. startsExpr
  96. }),
  97. braceR: new TokenType("}"),
  98. braceBarR: new TokenType("|}"),
  99. parenL: new TokenType("(", {
  100. beforeExpr,
  101. startsExpr
  102. }),
  103. parenR: new TokenType(")"),
  104. comma: new TokenType(",", {
  105. beforeExpr
  106. }),
  107. semi: new TokenType(";", {
  108. beforeExpr
  109. }),
  110. colon: new TokenType(":", {
  111. beforeExpr
  112. }),
  113. doubleColon: new TokenType("::", {
  114. beforeExpr
  115. }),
  116. dot: new TokenType("."),
  117. question: new TokenType("?", {
  118. beforeExpr
  119. }),
  120. questionDot: new TokenType("?."),
  121. arrow: new TokenType("=>", {
  122. beforeExpr
  123. }),
  124. template: new TokenType("template"),
  125. ellipsis: new TokenType("...", {
  126. beforeExpr
  127. }),
  128. backQuote: new TokenType("`", {
  129. startsExpr
  130. }),
  131. dollarBraceL: new TokenType("${", {
  132. beforeExpr,
  133. startsExpr
  134. }),
  135. at: new TokenType("@"),
  136. hash: new TokenType("#", {
  137. startsExpr
  138. }),
  139. interpreterDirective: new TokenType("#!..."),
  140. eq: new TokenType("=", {
  141. beforeExpr,
  142. isAssign
  143. }),
  144. assign: new TokenType("_=", {
  145. beforeExpr,
  146. isAssign
  147. }),
  148. incDec: new TokenType("++/--", {
  149. prefix,
  150. postfix,
  151. startsExpr
  152. }),
  153. bang: new TokenType("!", {
  154. beforeExpr,
  155. prefix,
  156. startsExpr
  157. }),
  158. tilde: new TokenType("~", {
  159. beforeExpr,
  160. prefix,
  161. startsExpr
  162. }),
  163. pipeline: createBinop("|>", 0),
  164. nullishCoalescing: createBinop("??", 1),
  165. logicalOR: createBinop("||", 1),
  166. logicalAND: createBinop("&&", 2),
  167. bitwiseOR: createBinop("|", 3),
  168. bitwiseXOR: createBinop("^", 4),
  169. bitwiseAND: createBinop("&", 5),
  170. equality: createBinop("==/!=/===/!==", 6),
  171. relational: createBinop("</>/<=/>=", 7),
  172. bitShift: createBinop("<</>>/>>>", 8),
  173. plusMin: new TokenType("+/-", {
  174. beforeExpr,
  175. binop: 9,
  176. prefix,
  177. startsExpr
  178. }),
  179. modulo: new TokenType("%", {
  180. beforeExpr,
  181. binop: 10,
  182. startsExpr
  183. }),
  184. star: new TokenType("*", {
  185. binop: 10
  186. }),
  187. slash: createBinop("/", 10),
  188. exponent: new TokenType("**", {
  189. beforeExpr,
  190. binop: 11,
  191. rightAssociative: true
  192. }),
  193. _break: createKeyword("break"),
  194. _case: createKeyword("case", {
  195. beforeExpr
  196. }),
  197. _catch: createKeyword("catch"),
  198. _continue: createKeyword("continue"),
  199. _debugger: createKeyword("debugger"),
  200. _default: createKeyword("default", {
  201. beforeExpr
  202. }),
  203. _do: createKeyword("do", {
  204. isLoop,
  205. beforeExpr
  206. }),
  207. _else: createKeyword("else", {
  208. beforeExpr
  209. }),
  210. _finally: createKeyword("finally"),
  211. _for: createKeyword("for", {
  212. isLoop
  213. }),
  214. _function: createKeyword("function", {
  215. startsExpr
  216. }),
  217. _if: createKeyword("if"),
  218. _return: createKeyword("return", {
  219. beforeExpr
  220. }),
  221. _switch: createKeyword("switch"),
  222. _throw: createKeyword("throw", {
  223. beforeExpr,
  224. prefix,
  225. startsExpr
  226. }),
  227. _try: createKeyword("try"),
  228. _var: createKeyword("var"),
  229. _const: createKeyword("const"),
  230. _while: createKeyword("while", {
  231. isLoop
  232. }),
  233. _with: createKeyword("with"),
  234. _new: createKeyword("new", {
  235. beforeExpr,
  236. startsExpr
  237. }),
  238. _this: createKeyword("this", {
  239. startsExpr
  240. }),
  241. _super: createKeyword("super", {
  242. startsExpr
  243. }),
  244. _class: createKeyword("class", {
  245. startsExpr
  246. }),
  247. _extends: createKeyword("extends", {
  248. beforeExpr
  249. }),
  250. _export: createKeyword("export"),
  251. _import: createKeyword("import", {
  252. startsExpr
  253. }),
  254. _null: createKeyword("null", {
  255. startsExpr
  256. }),
  257. _true: createKeyword("true", {
  258. startsExpr
  259. }),
  260. _false: createKeyword("false", {
  261. startsExpr
  262. }),
  263. _in: createKeyword("in", {
  264. beforeExpr,
  265. binop: 7
  266. }),
  267. _instanceof: createKeyword("instanceof", {
  268. beforeExpr,
  269. binop: 7
  270. }),
  271. _typeof: createKeyword("typeof", {
  272. beforeExpr,
  273. prefix,
  274. startsExpr
  275. }),
  276. _void: createKeyword("void", {
  277. beforeExpr,
  278. prefix,
  279. startsExpr
  280. }),
  281. _delete: createKeyword("delete", {
  282. beforeExpr,
  283. prefix,
  284. startsExpr
  285. })
  286. };
  287. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  288. const lineBreakG = new RegExp(lineBreak.source, "g");
  289. function isNewLine(code) {
  290. switch (code) {
  291. case 10:
  292. case 13:
  293. case 8232:
  294. case 8233:
  295. return true;
  296. default:
  297. return false;
  298. }
  299. }
  300. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  301. function isWhitespace(code) {
  302. switch (code) {
  303. case 0x0009:
  304. case 0x000b:
  305. case 0x000c:
  306. case 32:
  307. case 160:
  308. case 5760:
  309. case 0x2000:
  310. case 0x2001:
  311. case 0x2002:
  312. case 0x2003:
  313. case 0x2004:
  314. case 0x2005:
  315. case 0x2006:
  316. case 0x2007:
  317. case 0x2008:
  318. case 0x2009:
  319. case 0x200a:
  320. case 0x202f:
  321. case 0x205f:
  322. case 0x3000:
  323. case 0xfeff:
  324. return true;
  325. default:
  326. return false;
  327. }
  328. }
  329. class Position {
  330. constructor(line, col) {
  331. this.line = void 0;
  332. this.column = void 0;
  333. this.line = line;
  334. this.column = col;
  335. }
  336. }
  337. class SourceLocation {
  338. constructor(start, end) {
  339. this.start = void 0;
  340. this.end = void 0;
  341. this.filename = void 0;
  342. this.identifierName = void 0;
  343. this.start = start;
  344. this.end = end;
  345. }
  346. }
  347. function getLineInfo(input, offset) {
  348. let line = 1;
  349. let lineStart = 0;
  350. let match;
  351. lineBreakG.lastIndex = 0;
  352. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  353. line++;
  354. lineStart = lineBreakG.lastIndex;
  355. }
  356. return new Position(line, offset - lineStart);
  357. }
  358. class BaseParser {
  359. constructor() {
  360. this.sawUnambiguousESM = false;
  361. this.ambiguousScriptDifferentAst = false;
  362. }
  363. hasPlugin(name) {
  364. return this.plugins.has(name);
  365. }
  366. getPluginOption(plugin, name) {
  367. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  368. }
  369. }
  370. function last(stack) {
  371. return stack[stack.length - 1];
  372. }
  373. class CommentsParser extends BaseParser {
  374. addComment(comment) {
  375. if (this.filename) comment.loc.filename = this.filename;
  376. this.state.trailingComments.push(comment);
  377. this.state.leadingComments.push(comment);
  378. }
  379. adjustCommentsAfterTrailingComma(node, elements, takeAllComments) {
  380. if (this.state.leadingComments.length === 0) {
  381. return;
  382. }
  383. let lastElement = null;
  384. let i = elements.length;
  385. while (lastElement === null && i > 0) {
  386. lastElement = elements[--i];
  387. }
  388. if (lastElement === null) {
  389. return;
  390. }
  391. for (let j = 0; j < this.state.leadingComments.length; j++) {
  392. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  393. this.state.leadingComments.splice(j, 1);
  394. j--;
  395. }
  396. }
  397. const newTrailingComments = [];
  398. for (let i = 0; i < this.state.leadingComments.length; i++) {
  399. const leadingComment = this.state.leadingComments[i];
  400. if (leadingComment.end < node.end) {
  401. newTrailingComments.push(leadingComment);
  402. if (!takeAllComments) {
  403. this.state.leadingComments.splice(i, 1);
  404. i--;
  405. }
  406. } else {
  407. if (node.trailingComments === undefined) {
  408. node.trailingComments = [];
  409. }
  410. node.trailingComments.push(leadingComment);
  411. }
  412. }
  413. if (takeAllComments) this.state.leadingComments = [];
  414. if (newTrailingComments.length > 0) {
  415. lastElement.trailingComments = newTrailingComments;
  416. } else if (lastElement.trailingComments !== undefined) {
  417. lastElement.trailingComments = [];
  418. }
  419. }
  420. processComment(node) {
  421. if (node.type === "Program" && node.body.length > 0) return;
  422. const stack = this.state.commentStack;
  423. let firstChild, lastChild, trailingComments, i, j;
  424. if (this.state.trailingComments.length > 0) {
  425. if (this.state.trailingComments[0].start >= node.end) {
  426. trailingComments = this.state.trailingComments;
  427. this.state.trailingComments = [];
  428. } else {
  429. this.state.trailingComments.length = 0;
  430. }
  431. } else if (stack.length > 0) {
  432. const lastInStack = last(stack);
  433. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  434. trailingComments = lastInStack.trailingComments;
  435. delete lastInStack.trailingComments;
  436. }
  437. }
  438. if (stack.length > 0 && last(stack).start >= node.start) {
  439. firstChild = stack.pop();
  440. }
  441. while (stack.length > 0 && last(stack).start >= node.start) {
  442. lastChild = stack.pop();
  443. }
  444. if (!lastChild && firstChild) lastChild = firstChild;
  445. if (firstChild) {
  446. switch (node.type) {
  447. case "ObjectExpression":
  448. this.adjustCommentsAfterTrailingComma(node, node.properties);
  449. break;
  450. case "ObjectPattern":
  451. this.adjustCommentsAfterTrailingComma(node, node.properties, true);
  452. break;
  453. case "CallExpression":
  454. this.adjustCommentsAfterTrailingComma(node, node.arguments);
  455. break;
  456. case "ArrayExpression":
  457. this.adjustCommentsAfterTrailingComma(node, node.elements);
  458. break;
  459. case "ArrayPattern":
  460. this.adjustCommentsAfterTrailingComma(node, node.elements, true);
  461. break;
  462. }
  463. } else if (this.state.commentPreviousNode && (this.state.commentPreviousNode.type === "ImportSpecifier" && node.type !== "ImportSpecifier" || this.state.commentPreviousNode.type === "ExportSpecifier" && node.type !== "ExportSpecifier")) {
  464. this.adjustCommentsAfterTrailingComma(node, [this.state.commentPreviousNode]);
  465. }
  466. if (lastChild) {
  467. if (lastChild.leadingComments) {
  468. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  469. node.leadingComments = lastChild.leadingComments;
  470. delete lastChild.leadingComments;
  471. } else {
  472. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  473. if (lastChild.leadingComments[i].end <= node.start) {
  474. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. } else if (this.state.leadingComments.length > 0) {
  481. if (last(this.state.leadingComments).end <= node.start) {
  482. if (this.state.commentPreviousNode) {
  483. for (j = 0; j < this.state.leadingComments.length; j++) {
  484. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  485. this.state.leadingComments.splice(j, 1);
  486. j--;
  487. }
  488. }
  489. }
  490. if (this.state.leadingComments.length > 0) {
  491. node.leadingComments = this.state.leadingComments;
  492. this.state.leadingComments = [];
  493. }
  494. } else {
  495. for (i = 0; i < this.state.leadingComments.length; i++) {
  496. if (this.state.leadingComments[i].end > node.start) {
  497. break;
  498. }
  499. }
  500. const leadingComments = this.state.leadingComments.slice(0, i);
  501. if (leadingComments.length) {
  502. node.leadingComments = leadingComments;
  503. }
  504. trailingComments = this.state.leadingComments.slice(i);
  505. if (trailingComments.length === 0) {
  506. trailingComments = null;
  507. }
  508. }
  509. }
  510. this.state.commentPreviousNode = node;
  511. if (trailingComments) {
  512. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  513. node.innerComments = trailingComments;
  514. } else {
  515. const firstTrailingCommentIndex = trailingComments.findIndex(comment => comment.end >= node.end);
  516. if (firstTrailingCommentIndex > 0) {
  517. node.innerComments = trailingComments.slice(0, firstTrailingCommentIndex);
  518. node.trailingComments = trailingComments.slice(firstTrailingCommentIndex);
  519. } else {
  520. node.trailingComments = trailingComments;
  521. }
  522. }
  523. }
  524. stack.push(node);
  525. }
  526. }
  527. const ErrorCodes = Object.freeze({
  528. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  529. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  530. });
  531. const ErrorMessages = makeErrorTemplates({
  532. AccessorIsGenerator: "A %0ter cannot be a generator.",
  533. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  534. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  535. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  536. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  537. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  538. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  539. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  540. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  541. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  542. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  543. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  544. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  545. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  546. ConstructorIsAsync: "Constructor can't be an async function.",
  547. ConstructorIsGenerator: "Constructor can't be a generator.",
  548. DeclarationMissingInitializer: "'%0' require an initialization value.",
  549. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  550. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  551. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  552. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  553. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  554. DeletePrivateField: "Deleting a private field is not allowed.",
  555. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  556. DuplicateConstructor: "Duplicate constructor in the same class.",
  557. DuplicateDefaultExport: "Only one default export allowed per module.",
  558. DuplicateExport: "`%0` has already been exported. Exported identifiers must be unique.",
  559. DuplicateProto: "Redefinition of __proto__ property.",
  560. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  561. ElementAfterRest: "Rest element must be last element.",
  562. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  563. ExportBindingIsString: "A string literal cannot be used as an exported binding without `from`.\n- Did you mean `export { '%0' as '%1' } from 'some-module'`?",
  564. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  565. ForInOfLoopInitializer: "'%0' loop variable declaration may not have an initializer.",
  566. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  567. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  568. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  569. IllegalBreakContinue: "Unsyntactic %0.",
  570. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  571. IllegalReturn: "'return' outside of function.",
  572. ImportBindingIsString: 'A string literal cannot be used as an imported binding.\n- Did you mean `import { "%0" as foo }`?',
  573. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  574. ImportCallArity: "`import()` requires exactly %0.",
  575. ImportCallNotNewExpression: "Cannot use new with import(...).",
  576. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  577. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  578. InvalidCodePoint: "Code point out of bounds.",
  579. InvalidDecimal: "Invalid decimal.",
  580. InvalidDigit: "Expected number in radix %0.",
  581. InvalidEscapeSequence: "Bad character escape sequence.",
  582. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  583. InvalidEscapedReservedWord: "Escape sequence in keyword %0.",
  584. InvalidIdentifier: "Invalid identifier %0.",
  585. InvalidLhs: "Invalid left-hand side in %0.",
  586. InvalidLhsBinding: "Binding invalid left-hand side in %0.",
  587. InvalidNumber: "Invalid number.",
  588. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  589. InvalidOrUnexpectedToken: "Unexpected character '%0'.",
  590. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  591. InvalidPrivateFieldResolution: "Private name #%0 is not defined.",
  592. InvalidPropertyBindingPattern: "Binding member expression.",
  593. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  594. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  595. LabelRedeclaration: "Label '%0' is already declared.",
  596. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  597. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  598. MalformedRegExpFlags: "Invalid regular expression flag.",
  599. MissingClassName: "A class name is required.",
  600. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  601. MissingSemicolon: "Missing semicolon.",
  602. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  603. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  604. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  605. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  606. ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes.',
  607. ModuleExportNameHasLoneSurrogate: "An export name cannot include a lone surrogate, found '\\u%0'.",
  608. ModuleExportUndefined: "Export '%0' is not defined.",
  609. MultipleDefaultsInSwitch: "Multiple default clauses.",
  610. NewlineAfterThrow: "Illegal newline after throw.",
  611. NoCatchOrFinally: "Missing catch or finally clause.",
  612. NumberIdentifier: "Identifier directly after number.",
  613. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  614. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  615. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  616. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  617. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  618. ParamDupe: "Argument name clash.",
  619. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  620. PatternHasMethod: "Object pattern can't contain methods.",
  621. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  622. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  623. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  624. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  625. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  626. PrimaryTopicRequiresSmartPipeline: "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.",
  627. PrivateInExpectedIn: "Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
  628. PrivateNameRedeclaration: "Duplicate private name #%0.",
  629. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  630. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  631. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  632. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  633. RestTrailingComma: "Unexpected trailing comma after rest element.",
  634. SloppyFunction: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  635. StaticPrototype: "Classes may not have static property named prototype.",
  636. StrictDelete: "Deleting local variable in strict mode.",
  637. StrictEvalArguments: "Assigning to '%0' in strict mode.",
  638. StrictEvalArgumentsBinding: "Binding '%0' in strict mode.",
  639. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  640. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  641. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  642. StrictWith: "'with' in strict mode.",
  643. 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?",
  644. SuperPrivateField: "Private fields can't be accessed on super.",
  645. TrailingDecorator: "Decorators must be attached to a class element.",
  646. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  647. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  648. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  649. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  650. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  651. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  652. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  653. UnexpectedKeyword: "Unexpected keyword '%0'.",
  654. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  655. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  656. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  657. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  658. UnexpectedPrivateField: "Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p).",
  659. UnexpectedReservedWord: "Unexpected reserved word '%0'.",
  660. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  661. UnexpectedToken: "Unexpected token '%0'.",
  662. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  663. UnsupportedBind: "Binding should be performed on object property.",
  664. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  665. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  666. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  667. UnsupportedMetaProperty: "The only valid meta property for %0 is %0.%1.",
  668. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  669. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  670. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  671. UnterminatedComment: "Unterminated comment.",
  672. UnterminatedRegExp: "Unterminated regular expression.",
  673. UnterminatedString: "Unterminated string constant.",
  674. UnterminatedTemplate: "Unterminated template.",
  675. VarRedeclaration: "Identifier '%0' has already been declared.",
  676. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  677. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  678. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  679. }, ErrorCodes.SyntaxError);
  680. const SourceTypeModuleErrorMessages = makeErrorTemplates({
  681. ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
  682. ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`
  683. }, ErrorCodes.SourceTypeModuleError);
  684. function makeErrorTemplates(messages, code) {
  685. const templates = {};
  686. Object.keys(messages).forEach(reasonCode => {
  687. templates[reasonCode] = Object.freeze({
  688. code,
  689. reasonCode,
  690. template: messages[reasonCode]
  691. });
  692. });
  693. return Object.freeze(templates);
  694. }
  695. class ParserError extends CommentsParser {
  696. getLocationForPosition(pos) {
  697. let loc;
  698. if (pos === this.state.start) loc = this.state.startLoc;else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;else if (pos === this.state.end) loc = this.state.endLoc;else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;else loc = getLineInfo(this.input, pos);
  699. return loc;
  700. }
  701. raise(pos, {
  702. code,
  703. reasonCode,
  704. template
  705. }, ...params) {
  706. return this.raiseWithData(pos, {
  707. code,
  708. reasonCode
  709. }, template, ...params);
  710. }
  711. raiseOverwrite(pos, {
  712. code,
  713. template
  714. }, ...params) {
  715. const loc = this.getLocationForPosition(pos);
  716. const message = template.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  717. if (this.options.errorRecovery) {
  718. const errors = this.state.errors;
  719. for (let i = errors.length - 1; i >= 0; i--) {
  720. const error = errors[i];
  721. if (error.pos === pos) {
  722. return Object.assign(error, {
  723. message
  724. });
  725. } else if (error.pos < pos) {
  726. break;
  727. }
  728. }
  729. }
  730. return this._raise({
  731. code,
  732. loc,
  733. pos
  734. }, message);
  735. }
  736. raiseWithData(pos, data, errorTemplate, ...params) {
  737. const loc = this.getLocationForPosition(pos);
  738. const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  739. return this._raise(Object.assign({
  740. loc,
  741. pos
  742. }, data), message);
  743. }
  744. _raise(errorContext, message) {
  745. const err = new SyntaxError(message);
  746. Object.assign(err, errorContext);
  747. if (this.options.errorRecovery) {
  748. if (!this.isLookahead) this.state.errors.push(err);
  749. return err;
  750. } else {
  751. throw err;
  752. }
  753. }
  754. }
  755. var estree = (superClass => class extends superClass {
  756. estreeParseRegExpLiteral({
  757. pattern,
  758. flags
  759. }) {
  760. let regex = null;
  761. try {
  762. regex = new RegExp(pattern, flags);
  763. } catch (e) {}
  764. const node = this.estreeParseLiteral(regex);
  765. node.regex = {
  766. pattern,
  767. flags
  768. };
  769. return node;
  770. }
  771. estreeParseBigIntLiteral(value) {
  772. let bigInt;
  773. try {
  774. bigInt = BigInt(value);
  775. } catch (_unused) {
  776. bigInt = null;
  777. }
  778. const node = this.estreeParseLiteral(bigInt);
  779. node.bigint = String(node.value || value);
  780. return node;
  781. }
  782. estreeParseDecimalLiteral(value) {
  783. const decimal = null;
  784. const node = this.estreeParseLiteral(decimal);
  785. node.decimal = String(node.value || value);
  786. return node;
  787. }
  788. estreeParseLiteral(value) {
  789. return this.parseLiteral(value, "Literal");
  790. }
  791. directiveToStmt(directive) {
  792. const directiveLiteral = directive.value;
  793. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  794. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  795. expression.value = directiveLiteral.extra.expressionValue;
  796. expression.raw = directiveLiteral.extra.raw;
  797. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  798. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  799. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  800. }
  801. initFunction(node, isAsync) {
  802. super.initFunction(node, isAsync);
  803. node.expression = false;
  804. }
  805. checkDeclaration(node) {
  806. if (node != null && this.isObjectProperty(node)) {
  807. this.checkDeclaration(node.value);
  808. } else {
  809. super.checkDeclaration(node);
  810. }
  811. }
  812. getObjectOrClassMethodParams(method) {
  813. return method.value.params;
  814. }
  815. isValidDirective(stmt) {
  816. var _stmt$expression$extr;
  817. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  818. }
  819. stmtToDirective(stmt) {
  820. const directive = super.stmtToDirective(stmt);
  821. const value = stmt.expression.value;
  822. this.addExtra(directive.value, "expressionValue", value);
  823. return directive;
  824. }
  825. parseBlockBody(node, ...args) {
  826. super.parseBlockBody(node, ...args);
  827. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  828. node.body = directiveStatements.concat(node.body);
  829. delete node.directives;
  830. }
  831. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  832. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  833. if (method.typeParameters) {
  834. method.value.typeParameters = method.typeParameters;
  835. delete method.typeParameters;
  836. }
  837. classBody.body.push(method);
  838. }
  839. parseExprAtom(refExpressionErrors) {
  840. switch (this.state.type) {
  841. case types$1.num:
  842. case types$1.string:
  843. return this.estreeParseLiteral(this.state.value);
  844. case types$1.regexp:
  845. return this.estreeParseRegExpLiteral(this.state.value);
  846. case types$1.bigint:
  847. return this.estreeParseBigIntLiteral(this.state.value);
  848. case types$1.decimal:
  849. return this.estreeParseDecimalLiteral(this.state.value);
  850. case types$1._null:
  851. return this.estreeParseLiteral(null);
  852. case types$1._true:
  853. return this.estreeParseLiteral(true);
  854. case types$1._false:
  855. return this.estreeParseLiteral(false);
  856. default:
  857. return super.parseExprAtom(refExpressionErrors);
  858. }
  859. }
  860. parseMaybePrivateName(...args) {
  861. const node = super.parseMaybePrivateName(...args);
  862. if (node.type === "PrivateName" && this.getPluginOption("estree", "classFeatures")) {
  863. return this.convertPrivateNameToPrivateIdentifier(node);
  864. }
  865. return node;
  866. }
  867. convertPrivateNameToPrivateIdentifier(node) {
  868. const name = super.getPrivateNameSV(node);
  869. node = node;
  870. delete node.id;
  871. node.name = name;
  872. node.type = "PrivateIdentifier";
  873. return node;
  874. }
  875. isPrivateName(node) {
  876. if (!this.getPluginOption("estree", "classFeatures")) {
  877. return super.isPrivateName(node);
  878. }
  879. return node.type === "PrivateIdentifier";
  880. }
  881. getPrivateNameSV(node) {
  882. if (!this.getPluginOption("estree", "classFeatures")) {
  883. return super.getPrivateNameSV(node);
  884. }
  885. return node.name;
  886. }
  887. parseLiteral(value, type, startPos, startLoc) {
  888. const node = super.parseLiteral(value, type, startPos, startLoc);
  889. node.raw = node.extra.raw;
  890. delete node.extra;
  891. return node;
  892. }
  893. parseFunctionBody(node, allowExpression, isMethod = false) {
  894. super.parseFunctionBody(node, allowExpression, isMethod);
  895. node.expression = node.body.type !== "BlockStatement";
  896. }
  897. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  898. let funcNode = this.startNode();
  899. funcNode.kind = node.kind;
  900. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  901. funcNode.type = "FunctionExpression";
  902. delete funcNode.kind;
  903. node.value = funcNode;
  904. if (type === "ClassPrivateMethod") {
  905. node.computed = false;
  906. }
  907. type = "MethodDefinition";
  908. return this.finishNode(node, type);
  909. }
  910. parseClassProperty(...args) {
  911. const propertyNode = super.parseClassProperty(...args);
  912. if (this.getPluginOption("estree", "classFeatures")) {
  913. propertyNode.type = "PropertyDefinition";
  914. }
  915. return propertyNode;
  916. }
  917. parseClassPrivateProperty(...args) {
  918. const propertyNode = super.parseClassPrivateProperty(...args);
  919. if (this.getPluginOption("estree", "classFeatures")) {
  920. propertyNode.type = "PropertyDefinition";
  921. propertyNode.computed = false;
  922. }
  923. return propertyNode;
  924. }
  925. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  926. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  927. if (node) {
  928. node.type = "Property";
  929. if (node.kind === "method") node.kind = "init";
  930. node.shorthand = false;
  931. }
  932. return node;
  933. }
  934. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  935. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  936. if (node) {
  937. node.kind = "init";
  938. node.type = "Property";
  939. }
  940. return node;
  941. }
  942. toAssignable(node, isLHS = false) {
  943. if (node != null && this.isObjectProperty(node)) {
  944. this.toAssignable(node.value, isLHS);
  945. return node;
  946. }
  947. return super.toAssignable(node, isLHS);
  948. }
  949. toAssignableObjectExpressionProp(prop, ...args) {
  950. if (prop.kind === "get" || prop.kind === "set") {
  951. this.raise(prop.key.start, ErrorMessages.PatternHasAccessor);
  952. } else if (prop.method) {
  953. this.raise(prop.key.start, ErrorMessages.PatternHasMethod);
  954. } else {
  955. super.toAssignableObjectExpressionProp(prop, ...args);
  956. }
  957. }
  958. finishCallExpression(node, optional) {
  959. super.finishCallExpression(node, optional);
  960. if (node.callee.type === "Import") {
  961. node.type = "ImportExpression";
  962. node.source = node.arguments[0];
  963. if (this.hasPlugin("importAssertions")) {
  964. var _node$arguments$;
  965. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  966. }
  967. delete node.arguments;
  968. delete node.callee;
  969. }
  970. return node;
  971. }
  972. toReferencedArguments(node) {
  973. if (node.type === "ImportExpression") {
  974. return;
  975. }
  976. super.toReferencedArguments(node);
  977. }
  978. parseExport(node) {
  979. super.parseExport(node);
  980. switch (node.type) {
  981. case "ExportAllDeclaration":
  982. node.exported = null;
  983. break;
  984. case "ExportNamedDeclaration":
  985. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  986. node.type = "ExportAllDeclaration";
  987. node.exported = node.specifiers[0].exported;
  988. delete node.specifiers;
  989. }
  990. break;
  991. }
  992. return node;
  993. }
  994. parseSubscript(base, startPos, startLoc, noCalls, state) {
  995. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  996. if (state.optionalChainMember) {
  997. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  998. node.type = node.type.substring(8);
  999. }
  1000. if (state.stop) {
  1001. const chain = this.startNodeAtNode(node);
  1002. chain.expression = node;
  1003. return this.finishNode(chain, "ChainExpression");
  1004. }
  1005. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  1006. node.optional = false;
  1007. }
  1008. return node;
  1009. }
  1010. hasPropertyAsPrivateName(node) {
  1011. if (node.type === "ChainExpression") {
  1012. node = node.expression;
  1013. }
  1014. return super.hasPropertyAsPrivateName(node);
  1015. }
  1016. isOptionalChain(node) {
  1017. return node.type === "ChainExpression";
  1018. }
  1019. isObjectProperty(node) {
  1020. return node.type === "Property" && node.kind === "init" && !node.method;
  1021. }
  1022. isObjectMethod(node) {
  1023. return node.method || node.kind === "get" || node.kind === "set";
  1024. }
  1025. });
  1026. class TokContext {
  1027. constructor(token, isExpr, preserveSpace, override) {
  1028. this.token = void 0;
  1029. this.isExpr = void 0;
  1030. this.preserveSpace = void 0;
  1031. this.override = void 0;
  1032. this.token = token;
  1033. this.isExpr = !!isExpr;
  1034. this.preserveSpace = !!preserveSpace;
  1035. this.override = override;
  1036. }
  1037. }
  1038. const types = {
  1039. braceStatement: new TokContext("{", false),
  1040. braceExpression: new TokContext("{", true),
  1041. recordExpression: new TokContext("#{", true),
  1042. templateQuasi: new TokContext("${", false),
  1043. parenStatement: new TokContext("(", false),
  1044. parenExpression: new TokContext("(", true),
  1045. template: new TokContext("`", true, true, p => p.readTmplToken()),
  1046. functionExpression: new TokContext("function", true),
  1047. functionStatement: new TokContext("function", false)
  1048. };
  1049. types$1.parenR.updateContext = types$1.braceR.updateContext = function () {
  1050. if (this.state.context.length === 1) {
  1051. this.state.exprAllowed = true;
  1052. return;
  1053. }
  1054. let out = this.state.context.pop();
  1055. if (out === types.braceStatement && this.curContext().token === "function") {
  1056. out = this.state.context.pop();
  1057. }
  1058. this.state.exprAllowed = !out.isExpr;
  1059. };
  1060. types$1.name.updateContext = function (prevType) {
  1061. let allowed = false;
  1062. if (prevType !== types$1.dot) {
  1063. if (this.state.value === "of" && !this.state.exprAllowed && prevType !== types$1._function && prevType !== types$1._class) {
  1064. allowed = true;
  1065. }
  1066. }
  1067. this.state.exprAllowed = allowed;
  1068. };
  1069. types$1.braceL.updateContext = function (prevType) {
  1070. this.state.context.push(this.braceIsBlock(prevType) ? types.braceStatement : types.braceExpression);
  1071. this.state.exprAllowed = true;
  1072. };
  1073. types$1.dollarBraceL.updateContext = function () {
  1074. this.state.context.push(types.templateQuasi);
  1075. this.state.exprAllowed = true;
  1076. };
  1077. types$1.parenL.updateContext = function (prevType) {
  1078. const statementParens = prevType === types$1._if || prevType === types$1._for || prevType === types$1._with || prevType === types$1._while;
  1079. this.state.context.push(statementParens ? types.parenStatement : types.parenExpression);
  1080. this.state.exprAllowed = true;
  1081. };
  1082. types$1.incDec.updateContext = function () {};
  1083. types$1._function.updateContext = types$1._class.updateContext = function (prevType) {
  1084. if (prevType.beforeExpr && prevType !== types$1.semi && prevType !== types$1._else && !(prevType === types$1._return && this.hasPrecedingLineBreak()) && !((prevType === types$1.colon || prevType === types$1.braceL) && this.curContext() === types.b_stat)) {
  1085. this.state.context.push(types.functionExpression);
  1086. } else {
  1087. this.state.context.push(types.functionStatement);
  1088. }
  1089. this.state.exprAllowed = false;
  1090. };
  1091. types$1.backQuote.updateContext = function () {
  1092. if (this.curContext() === types.template) {
  1093. this.state.context.pop();
  1094. } else {
  1095. this.state.context.push(types.template);
  1096. }
  1097. this.state.exprAllowed = false;
  1098. };
  1099. types$1.braceHashL.updateContext = function () {
  1100. this.state.context.push(types.recordExpression);
  1101. this.state.exprAllowed = true;
  1102. };
  1103. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u08a0-\u08b4\u08b6-\u08c7\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\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-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\u9ffc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7ca\ua7f5-\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";
  1104. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf\u1ac0\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\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";
  1105. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1106. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1107. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1108. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 157, 310, 10, 21, 11, 7, 153, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 107, 20, 28, 22, 13, 52, 76, 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, 85, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 230, 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, 35, 56, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 190, 0, 80, 921, 103, 110, 18, 195, 2749, 1070, 4050, 582, 8634, 568, 8, 30, 114, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8952, 286, 50, 2, 18, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 2357, 44, 11, 6, 17, 0, 370, 43, 1301, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42717, 35, 4148, 12, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938];
  1109. 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, 154, 10, 176, 2, 54, 14, 32, 9, 16, 3, 46, 10, 54, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 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, 19306, 9, 135, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 5319, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 419, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1110. function isInAstralSet(code, set) {
  1111. let pos = 0x10000;
  1112. for (let i = 0, length = set.length; i < length; i += 2) {
  1113. pos += set[i];
  1114. if (pos > code) return false;
  1115. pos += set[i + 1];
  1116. if (pos >= code) return true;
  1117. }
  1118. return false;
  1119. }
  1120. function isIdentifierStart(code) {
  1121. if (code < 65) return code === 36;
  1122. if (code <= 90) return true;
  1123. if (code < 97) return code === 95;
  1124. if (code <= 122) return true;
  1125. if (code <= 0xffff) {
  1126. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1127. }
  1128. return isInAstralSet(code, astralIdentifierStartCodes);
  1129. }
  1130. function isIdentifierChar(code) {
  1131. if (code < 48) return code === 36;
  1132. if (code < 58) return true;
  1133. if (code < 65) return false;
  1134. if (code <= 90) return true;
  1135. if (code < 97) return code === 95;
  1136. if (code <= 122) return true;
  1137. if (code <= 0xffff) {
  1138. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1139. }
  1140. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1141. }
  1142. const reservedWords = {
  1143. 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"],
  1144. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1145. strictBind: ["eval", "arguments"]
  1146. };
  1147. const keywords = new Set(reservedWords.keyword);
  1148. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1149. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1150. function isReservedWord(word, inModule) {
  1151. return inModule && word === "await" || word === "enum";
  1152. }
  1153. function isStrictReservedWord(word, inModule) {
  1154. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1155. }
  1156. function isStrictBindOnlyReservedWord(word) {
  1157. return reservedWordsStrictBindSet.has(word);
  1158. }
  1159. function isStrictBindReservedWord(word, inModule) {
  1160. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1161. }
  1162. function isKeyword(word) {
  1163. return keywords.has(word);
  1164. }
  1165. const keywordRelationalOperator = /^in(stanceof)?$/;
  1166. function isIteratorStart(current, next) {
  1167. return current === 64 && next === 64;
  1168. }
  1169. const SCOPE_OTHER = 0b000000000,
  1170. SCOPE_PROGRAM = 0b000000001,
  1171. SCOPE_FUNCTION = 0b000000010,
  1172. SCOPE_ARROW = 0b000000100,
  1173. SCOPE_SIMPLE_CATCH = 0b000001000,
  1174. SCOPE_SUPER = 0b000010000,
  1175. SCOPE_DIRECT_SUPER = 0b000100000,
  1176. SCOPE_CLASS = 0b001000000,
  1177. SCOPE_STATIC_BLOCK = 0b010000000,
  1178. SCOPE_TS_MODULE = 0b100000000,
  1179. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1180. const BIND_KIND_VALUE = 0b000000000001,
  1181. BIND_KIND_TYPE = 0b000000000010,
  1182. BIND_SCOPE_VAR = 0b000000000100,
  1183. BIND_SCOPE_LEXICAL = 0b000000001000,
  1184. BIND_SCOPE_FUNCTION = 0b000000010000,
  1185. BIND_FLAGS_NONE = 0b000001000000,
  1186. BIND_FLAGS_CLASS = 0b000010000000,
  1187. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1188. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1189. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1190. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1191. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1192. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1193. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1194. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1195. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1196. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1197. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1198. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1199. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1200. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1201. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1202. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1203. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1204. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1205. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1206. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1207. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1208. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1209. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1210. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1211. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1212. CLASS_ELEMENT_OTHER = 0;
  1213. class Scope {
  1214. constructor(flags) {
  1215. this.flags = void 0;
  1216. this.var = [];
  1217. this.lexical = [];
  1218. this.functions = [];
  1219. this.flags = flags;
  1220. }
  1221. }
  1222. class ScopeHandler {
  1223. constructor(raise, inModule) {
  1224. this.scopeStack = [];
  1225. this.undefinedExports = new Map();
  1226. this.undefinedPrivateNames = new Map();
  1227. this.raise = raise;
  1228. this.inModule = inModule;
  1229. }
  1230. get inFunction() {
  1231. return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0;
  1232. }
  1233. get allowSuper() {
  1234. return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
  1235. }
  1236. get allowDirectSuper() {
  1237. return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0;
  1238. }
  1239. get inClass() {
  1240. return (this.currentThisScope().flags & SCOPE_CLASS) > 0;
  1241. }
  1242. get inStaticBlock() {
  1243. return (this.currentThisScope().flags & SCOPE_STATIC_BLOCK) > 0;
  1244. }
  1245. get inNonArrowFunction() {
  1246. return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0;
  1247. }
  1248. get treatFunctionsAsVar() {
  1249. return this.treatFunctionsAsVarInScope(this.currentScope());
  1250. }
  1251. createScope(flags) {
  1252. return new Scope(flags);
  1253. }
  1254. enter(flags) {
  1255. this.scopeStack.push(this.createScope(flags));
  1256. }
  1257. exit() {
  1258. this.scopeStack.pop();
  1259. }
  1260. treatFunctionsAsVarInScope(scope) {
  1261. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  1262. }
  1263. declareName(name, bindingType, pos) {
  1264. let scope = this.currentScope();
  1265. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1266. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1267. if (bindingType & BIND_SCOPE_FUNCTION) {
  1268. scope.functions.push(name);
  1269. } else {
  1270. scope.lexical.push(name);
  1271. }
  1272. if (bindingType & BIND_SCOPE_LEXICAL) {
  1273. this.maybeExportDefined(scope, name);
  1274. }
  1275. } else if (bindingType & BIND_SCOPE_VAR) {
  1276. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1277. scope = this.scopeStack[i];
  1278. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1279. scope.var.push(name);
  1280. this.maybeExportDefined(scope, name);
  1281. if (scope.flags & SCOPE_VAR) break;
  1282. }
  1283. }
  1284. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1285. this.undefinedExports.delete(name);
  1286. }
  1287. }
  1288. maybeExportDefined(scope, name) {
  1289. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1290. this.undefinedExports.delete(name);
  1291. }
  1292. }
  1293. checkRedeclarationInScope(scope, name, bindingType, pos) {
  1294. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1295. this.raise(pos, ErrorMessages.VarRedeclaration, name);
  1296. }
  1297. }
  1298. isRedeclaredInScope(scope, name, bindingType) {
  1299. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1300. if (bindingType & BIND_SCOPE_LEXICAL) {
  1301. return scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  1302. }
  1303. if (bindingType & BIND_SCOPE_FUNCTION) {
  1304. return scope.lexical.indexOf(name) > -1 || !this.treatFunctionsAsVarInScope(scope) && scope.var.indexOf(name) > -1;
  1305. }
  1306. return scope.lexical.indexOf(name) > -1 && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.indexOf(name) > -1;
  1307. }
  1308. checkLocalExport(id) {
  1309. if (this.scopeStack[0].lexical.indexOf(id.name) === -1 && this.scopeStack[0].var.indexOf(id.name) === -1 && this.scopeStack[0].functions.indexOf(id.name) === -1) {
  1310. this.undefinedExports.set(id.name, id.start);
  1311. }
  1312. }
  1313. currentScope() {
  1314. return this.scopeStack[this.scopeStack.length - 1];
  1315. }
  1316. currentVarScope() {
  1317. for (let i = this.scopeStack.length - 1;; i--) {
  1318. const scope = this.scopeStack[i];
  1319. if (scope.flags & SCOPE_VAR) {
  1320. return scope;
  1321. }
  1322. }
  1323. }
  1324. currentThisScope() {
  1325. for (let i = this.scopeStack.length - 1;; i--) {
  1326. const scope = this.scopeStack[i];
  1327. if ((scope.flags & SCOPE_VAR || scope.flags & SCOPE_CLASS) && !(scope.flags & SCOPE_ARROW)) {
  1328. return scope;
  1329. }
  1330. }
  1331. }
  1332. }
  1333. class FlowScope extends Scope {
  1334. constructor(...args) {
  1335. super(...args);
  1336. this.declareFunctions = [];
  1337. }
  1338. }
  1339. class FlowScopeHandler extends ScopeHandler {
  1340. createScope(flags) {
  1341. return new FlowScope(flags);
  1342. }
  1343. declareName(name, bindingType, pos) {
  1344. const scope = this.currentScope();
  1345. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1346. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1347. this.maybeExportDefined(scope, name);
  1348. scope.declareFunctions.push(name);
  1349. return;
  1350. }
  1351. super.declareName(...arguments);
  1352. }
  1353. isRedeclaredInScope(scope, name, bindingType) {
  1354. if (super.isRedeclaredInScope(...arguments)) return true;
  1355. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1356. return !scope.declareFunctions.includes(name) && (scope.lexical.includes(name) || scope.functions.includes(name));
  1357. }
  1358. return false;
  1359. }
  1360. checkLocalExport(id) {
  1361. if (this.scopeStack[0].declareFunctions.indexOf(id.name) === -1) {
  1362. super.checkLocalExport(id);
  1363. }
  1364. }
  1365. }
  1366. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  1367. const FlowErrors = makeErrorTemplates({
  1368. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  1369. 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.",
  1370. AssignReservedType: "Cannot overwrite reserved type %0.",
  1371. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  1372. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  1373. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  1374. EnumBooleanMemberNotInitialized: "Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
  1375. EnumDuplicateMemberName: "Enum member names need to be unique, but the name `%0` has already been used before in enum `%1`.",
  1376. EnumInconsistentMemberValues: "Enum `%0` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.",
  1377. EnumInvalidExplicitType: "Enum type `%1` is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1378. EnumInvalidExplicitTypeUnknownSupplied: "Supplied enum type is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1379. EnumInvalidMemberInitializerPrimaryType: "Enum `%0` has type `%2`, so the initializer of `%1` needs to be a %2 literal.",
  1380. EnumInvalidMemberInitializerSymbolType: "Symbol enum members cannot be initialized. Use `%1,` in enum `%0`.",
  1381. EnumInvalidMemberInitializerUnknownType: "The enum member initializer for `%1` needs to be a literal (either a boolean, number, or string) in enum `%0`.",
  1382. EnumInvalidMemberName: "Enum member names cannot start with lowercase 'a' through 'z'. Instead of using `%0`, consider using `%1`, in enum `%2`.",
  1383. EnumNumberMemberNotInitialized: "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.",
  1384. EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.",
  1385. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  1386. 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.",
  1387. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  1388. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  1389. InexactVariance: "Explicit inexact syntax cannot have variance.",
  1390. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  1391. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  1392. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  1393. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  1394. OptionalBindingPattern: "A binding pattern parameter cannot be optional in an implementation signature.",
  1395. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  1396. SpreadVariance: "Spread properties cannot have variance.",
  1397. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  1398. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  1399. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  1400. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  1401. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  1402. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  1403. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  1404. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  1405. UnexpectedReservedType: "Unexpected reserved type %0.",
  1406. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  1407. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  1408. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  1409. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  1410. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  1411. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  1412. UnsupportedDeclareExportKind: "`declare export %0` is not supported. Use `%1` instead.",
  1413. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  1414. UnterminatedFlowComment: "Unterminated flow-comment."
  1415. }, ErrorCodes.SyntaxError);
  1416. function isEsModuleType(bodyElement) {
  1417. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  1418. }
  1419. function hasTypeImportKind(node) {
  1420. return node.importKind === "type" || node.importKind === "typeof";
  1421. }
  1422. function isMaybeDefaultImport(state) {
  1423. return (state.type === types$1.name || !!state.type.keyword) && state.value !== "from";
  1424. }
  1425. const exportSuggestions = {
  1426. const: "declare export var",
  1427. let: "declare export var",
  1428. type: "export type",
  1429. interface: "export interface"
  1430. };
  1431. function partition(list, test) {
  1432. const list1 = [];
  1433. const list2 = [];
  1434. for (let i = 0; i < list.length; i++) {
  1435. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  1436. }
  1437. return [list1, list2];
  1438. }
  1439. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  1440. var flow = (superClass => class extends superClass {
  1441. constructor(...args) {
  1442. super(...args);
  1443. this.flowPragma = undefined;
  1444. }
  1445. getScopeHandler() {
  1446. return FlowScopeHandler;
  1447. }
  1448. shouldParseTypes() {
  1449. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  1450. }
  1451. shouldParseEnums() {
  1452. return !!this.getPluginOption("flow", "enums");
  1453. }
  1454. finishToken(type, val) {
  1455. if (type !== types$1.string && type !== types$1.semi && type !== types$1.interpreterDirective) {
  1456. if (this.flowPragma === undefined) {
  1457. this.flowPragma = null;
  1458. }
  1459. }
  1460. return super.finishToken(type, val);
  1461. }
  1462. addComment(comment) {
  1463. if (this.flowPragma === undefined) {
  1464. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  1465. if (!matches) ; else if (matches[1] === "flow") {
  1466. this.flowPragma = "flow";
  1467. } else if (matches[1] === "noflow") {
  1468. this.flowPragma = "noflow";
  1469. } else {
  1470. throw new Error("Unexpected flow pragma");
  1471. }
  1472. }
  1473. return super.addComment(comment);
  1474. }
  1475. flowParseTypeInitialiser(tok) {
  1476. const oldInType = this.state.inType;
  1477. this.state.inType = true;
  1478. this.expect(tok || types$1.colon);
  1479. const type = this.flowParseType();
  1480. this.state.inType = oldInType;
  1481. return type;
  1482. }
  1483. flowParsePredicate() {
  1484. const node = this.startNode();
  1485. const moduloPos = this.state.start;
  1486. this.next();
  1487. this.expectContextual("checks");
  1488. if (this.state.lastTokStart > moduloPos + 1) {
  1489. this.raise(moduloPos, FlowErrors.UnexpectedSpaceBetweenModuloChecks);
  1490. }
  1491. if (this.eat(types$1.parenL)) {
  1492. node.value = this.parseExpression();
  1493. this.expect(types$1.parenR);
  1494. return this.finishNode(node, "DeclaredPredicate");
  1495. } else {
  1496. return this.finishNode(node, "InferredPredicate");
  1497. }
  1498. }
  1499. flowParseTypeAndPredicateInitialiser() {
  1500. const oldInType = this.state.inType;
  1501. this.state.inType = true;
  1502. this.expect(types$1.colon);
  1503. let type = null;
  1504. let predicate = null;
  1505. if (this.match(types$1.modulo)) {
  1506. this.state.inType = oldInType;
  1507. predicate = this.flowParsePredicate();
  1508. } else {
  1509. type = this.flowParseType();
  1510. this.state.inType = oldInType;
  1511. if (this.match(types$1.modulo)) {
  1512. predicate = this.flowParsePredicate();
  1513. }
  1514. }
  1515. return [type, predicate];
  1516. }
  1517. flowParseDeclareClass(node) {
  1518. this.next();
  1519. this.flowParseInterfaceish(node, true);
  1520. return this.finishNode(node, "DeclareClass");
  1521. }
  1522. flowParseDeclareFunction(node) {
  1523. this.next();
  1524. const id = node.id = this.parseIdentifier();
  1525. const typeNode = this.startNode();
  1526. const typeContainer = this.startNode();
  1527. if (this.isRelational("<")) {
  1528. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  1529. } else {
  1530. typeNode.typeParameters = null;
  1531. }
  1532. this.expect(types$1.parenL);
  1533. const tmp = this.flowParseFunctionTypeParams();
  1534. typeNode.params = tmp.params;
  1535. typeNode.rest = tmp.rest;
  1536. typeNode.this = tmp._this;
  1537. this.expect(types$1.parenR);
  1538. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1539. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  1540. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  1541. this.resetEndLocation(id);
  1542. this.semicolon();
  1543. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.start);
  1544. return this.finishNode(node, "DeclareFunction");
  1545. }
  1546. flowParseDeclare(node, insideModule) {
  1547. if (this.match(types$1._class)) {
  1548. return this.flowParseDeclareClass(node);
  1549. } else if (this.match(types$1._function)) {
  1550. return this.flowParseDeclareFunction(node);
  1551. } else if (this.match(types$1._var)) {
  1552. return this.flowParseDeclareVariable(node);
  1553. } else if (this.eatContextual("module")) {
  1554. if (this.match(types$1.dot)) {
  1555. return this.flowParseDeclareModuleExports(node);
  1556. } else {
  1557. if (insideModule) {
  1558. this.raise(this.state.lastTokStart, FlowErrors.NestedDeclareModule);
  1559. }
  1560. return this.flowParseDeclareModule(node);
  1561. }
  1562. } else if (this.isContextual("type")) {
  1563. return this.flowParseDeclareTypeAlias(node);
  1564. } else if (this.isContextual("opaque")) {
  1565. return this.flowParseDeclareOpaqueType(node);
  1566. } else if (this.isContextual("interface")) {
  1567. return this.flowParseDeclareInterface(node);
  1568. } else if (this.match(types$1._export)) {
  1569. return this.flowParseDeclareExportDeclaration(node, insideModule);
  1570. } else {
  1571. throw this.unexpected();
  1572. }
  1573. }
  1574. flowParseDeclareVariable(node) {
  1575. this.next();
  1576. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  1577. this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
  1578. this.semicolon();
  1579. return this.finishNode(node, "DeclareVariable");
  1580. }
  1581. flowParseDeclareModule(node) {
  1582. this.scope.enter(SCOPE_OTHER);
  1583. if (this.match(types$1.string)) {
  1584. node.id = this.parseExprAtom();
  1585. } else {
  1586. node.id = this.parseIdentifier();
  1587. }
  1588. const bodyNode = node.body = this.startNode();
  1589. const body = bodyNode.body = [];
  1590. this.expect(types$1.braceL);
  1591. while (!this.match(types$1.braceR)) {
  1592. let bodyNode = this.startNode();
  1593. if (this.match(types$1._import)) {
  1594. this.next();
  1595. if (!this.isContextual("type") && !this.match(types$1._typeof)) {
  1596. this.raise(this.state.lastTokStart, FlowErrors.InvalidNonTypeImportInDeclareModule);
  1597. }
  1598. this.parseImport(bodyNode);
  1599. } else {
  1600. this.expectContextual("declare", FlowErrors.UnsupportedStatementInDeclareModule);
  1601. bodyNode = this.flowParseDeclare(bodyNode, true);
  1602. }
  1603. body.push(bodyNode);
  1604. }
  1605. this.scope.exit();
  1606. this.expect(types$1.braceR);
  1607. this.finishNode(bodyNode, "BlockStatement");
  1608. let kind = null;
  1609. let hasModuleExport = false;
  1610. body.forEach(bodyElement => {
  1611. if (isEsModuleType(bodyElement)) {
  1612. if (kind === "CommonJS") {
  1613. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1614. }
  1615. kind = "ES";
  1616. } else if (bodyElement.type === "DeclareModuleExports") {
  1617. if (hasModuleExport) {
  1618. this.raise(bodyElement.start, FlowErrors.DuplicateDeclareModuleExports);
  1619. }
  1620. if (kind === "ES") {
  1621. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1622. }
  1623. kind = "CommonJS";
  1624. hasModuleExport = true;
  1625. }
  1626. });
  1627. node.kind = kind || "CommonJS";
  1628. return this.finishNode(node, "DeclareModule");
  1629. }
  1630. flowParseDeclareExportDeclaration(node, insideModule) {
  1631. this.expect(types$1._export);
  1632. if (this.eat(types$1._default)) {
  1633. if (this.match(types$1._function) || this.match(types$1._class)) {
  1634. node.declaration = this.flowParseDeclare(this.startNode());
  1635. } else {
  1636. node.declaration = this.flowParseType();
  1637. this.semicolon();
  1638. }
  1639. node.default = true;
  1640. return this.finishNode(node, "DeclareExportDeclaration");
  1641. } else {
  1642. if (this.match(types$1._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  1643. const label = this.state.value;
  1644. const suggestion = exportSuggestions[label];
  1645. throw this.raise(this.state.start, FlowErrors.UnsupportedDeclareExportKind, label, suggestion);
  1646. }
  1647. if (this.match(types$1._var) || this.match(types$1._function) || this.match(types$1._class) || this.isContextual("opaque")) {
  1648. node.declaration = this.flowParseDeclare(this.startNode());
  1649. node.default = false;
  1650. return this.finishNode(node, "DeclareExportDeclaration");
  1651. } else if (this.match(types$1.star) || this.match(types$1.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  1652. node = this.parseExport(node);
  1653. if (node.type === "ExportNamedDeclaration") {
  1654. node.type = "ExportDeclaration";
  1655. node.default = false;
  1656. delete node.exportKind;
  1657. }
  1658. node.type = "Declare" + node.type;
  1659. return node;
  1660. }
  1661. }
  1662. throw this.unexpected();
  1663. }
  1664. flowParseDeclareModuleExports(node) {
  1665. this.next();
  1666. this.expectContextual("exports");
  1667. node.typeAnnotation = this.flowParseTypeAnnotation();
  1668. this.semicolon();
  1669. return this.finishNode(node, "DeclareModuleExports");
  1670. }
  1671. flowParseDeclareTypeAlias(node) {
  1672. this.next();
  1673. this.flowParseTypeAlias(node);
  1674. node.type = "DeclareTypeAlias";
  1675. return node;
  1676. }
  1677. flowParseDeclareOpaqueType(node) {
  1678. this.next();
  1679. this.flowParseOpaqueType(node, true);
  1680. node.type = "DeclareOpaqueType";
  1681. return node;
  1682. }
  1683. flowParseDeclareInterface(node) {
  1684. this.next();
  1685. this.flowParseInterfaceish(node);
  1686. return this.finishNode(node, "DeclareInterface");
  1687. }
  1688. flowParseInterfaceish(node, isClass = false) {
  1689. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  1690. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.start);
  1691. if (this.isRelational("<")) {
  1692. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1693. } else {
  1694. node.typeParameters = null;
  1695. }
  1696. node.extends = [];
  1697. node.implements = [];
  1698. node.mixins = [];
  1699. if (this.eat(types$1._extends)) {
  1700. do {
  1701. node.extends.push(this.flowParseInterfaceExtends());
  1702. } while (!isClass && this.eat(types$1.comma));
  1703. }
  1704. if (this.isContextual("mixins")) {
  1705. this.next();
  1706. do {
  1707. node.mixins.push(this.flowParseInterfaceExtends());
  1708. } while (this.eat(types$1.comma));
  1709. }
  1710. if (this.isContextual("implements")) {
  1711. this.next();
  1712. do {
  1713. node.implements.push(this.flowParseInterfaceExtends());
  1714. } while (this.eat(types$1.comma));
  1715. }
  1716. node.body = this.flowParseObjectType({
  1717. allowStatic: isClass,
  1718. allowExact: false,
  1719. allowSpread: false,
  1720. allowProto: isClass,
  1721. allowInexact: false
  1722. });
  1723. }
  1724. flowParseInterfaceExtends() {
  1725. const node = this.startNode();
  1726. node.id = this.flowParseQualifiedTypeIdentifier();
  1727. if (this.isRelational("<")) {
  1728. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1729. } else {
  1730. node.typeParameters = null;
  1731. }
  1732. return this.finishNode(node, "InterfaceExtends");
  1733. }
  1734. flowParseInterface(node) {
  1735. this.flowParseInterfaceish(node);
  1736. return this.finishNode(node, "InterfaceDeclaration");
  1737. }
  1738. checkNotUnderscore(word) {
  1739. if (word === "_") {
  1740. this.raise(this.state.start, FlowErrors.UnexpectedReservedUnderscore);
  1741. }
  1742. }
  1743. checkReservedType(word, startLoc, declaration) {
  1744. if (!reservedTypes.has(word)) return;
  1745. this.raise(startLoc, declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, word);
  1746. }
  1747. flowParseRestrictedIdentifier(liberal, declaration) {
  1748. this.checkReservedType(this.state.value, this.state.start, declaration);
  1749. return this.parseIdentifier(liberal);
  1750. }
  1751. flowParseTypeAlias(node) {
  1752. node.id = this.flowParseRestrictedIdentifier(false, true);
  1753. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1754. if (this.isRelational("<")) {
  1755. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1756. } else {
  1757. node.typeParameters = null;
  1758. }
  1759. node.right = this.flowParseTypeInitialiser(types$1.eq);
  1760. this.semicolon();
  1761. return this.finishNode(node, "TypeAlias");
  1762. }
  1763. flowParseOpaqueType(node, declare) {
  1764. this.expectContextual("type");
  1765. node.id = this.flowParseRestrictedIdentifier(true, true);
  1766. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1767. if (this.isRelational("<")) {
  1768. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1769. } else {
  1770. node.typeParameters = null;
  1771. }
  1772. node.supertype = null;
  1773. if (this.match(types$1.colon)) {
  1774. node.supertype = this.flowParseTypeInitialiser(types$1.colon);
  1775. }
  1776. node.impltype = null;
  1777. if (!declare) {
  1778. node.impltype = this.flowParseTypeInitialiser(types$1.eq);
  1779. }
  1780. this.semicolon();
  1781. return this.finishNode(node, "OpaqueType");
  1782. }
  1783. flowParseTypeParameter(requireDefault = false) {
  1784. const nodeStart = this.state.start;
  1785. const node = this.startNode();
  1786. const variance = this.flowParseVariance();
  1787. const ident = this.flowParseTypeAnnotatableIdentifier();
  1788. node.name = ident.name;
  1789. node.variance = variance;
  1790. node.bound = ident.typeAnnotation;
  1791. if (this.match(types$1.eq)) {
  1792. this.eat(types$1.eq);
  1793. node.default = this.flowParseType();
  1794. } else {
  1795. if (requireDefault) {
  1796. this.raise(nodeStart, FlowErrors.MissingTypeParamDefault);
  1797. }
  1798. }
  1799. return this.finishNode(node, "TypeParameter");
  1800. }
  1801. flowParseTypeParameterDeclaration() {
  1802. const oldInType = this.state.inType;
  1803. const node = this.startNode();
  1804. node.params = [];
  1805. this.state.inType = true;
  1806. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  1807. this.next();
  1808. } else {
  1809. this.unexpected();
  1810. }
  1811. let defaultRequired = false;
  1812. do {
  1813. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  1814. node.params.push(typeParameter);
  1815. if (typeParameter.default) {
  1816. defaultRequired = true;
  1817. }
  1818. if (!this.isRelational(">")) {
  1819. this.expect(types$1.comma);
  1820. }
  1821. } while (!this.isRelational(">"));
  1822. this.expectRelational(">");
  1823. this.state.inType = oldInType;
  1824. return this.finishNode(node, "TypeParameterDeclaration");
  1825. }
  1826. flowParseTypeParameterInstantiation() {
  1827. const node = this.startNode();
  1828. const oldInType = this.state.inType;
  1829. node.params = [];
  1830. this.state.inType = true;
  1831. this.expectRelational("<");
  1832. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1833. this.state.noAnonFunctionType = false;
  1834. while (!this.isRelational(">")) {
  1835. node.params.push(this.flowParseType());
  1836. if (!this.isRelational(">")) {
  1837. this.expect(types$1.comma);
  1838. }
  1839. }
  1840. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1841. this.expectRelational(">");
  1842. this.state.inType = oldInType;
  1843. return this.finishNode(node, "TypeParameterInstantiation");
  1844. }
  1845. flowParseTypeParameterInstantiationCallOrNew() {
  1846. const node = this.startNode();
  1847. const oldInType = this.state.inType;
  1848. node.params = [];
  1849. this.state.inType = true;
  1850. this.expectRelational("<");
  1851. while (!this.isRelational(">")) {
  1852. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1853. if (!this.isRelational(">")) {
  1854. this.expect(types$1.comma);
  1855. }
  1856. }
  1857. this.expectRelational(">");
  1858. this.state.inType = oldInType;
  1859. return this.finishNode(node, "TypeParameterInstantiation");
  1860. }
  1861. flowParseInterfaceType() {
  1862. const node = this.startNode();
  1863. this.expectContextual("interface");
  1864. node.extends = [];
  1865. if (this.eat(types$1._extends)) {
  1866. do {
  1867. node.extends.push(this.flowParseInterfaceExtends());
  1868. } while (this.eat(types$1.comma));
  1869. }
  1870. node.body = this.flowParseObjectType({
  1871. allowStatic: false,
  1872. allowExact: false,
  1873. allowSpread: false,
  1874. allowProto: false,
  1875. allowInexact: false
  1876. });
  1877. return this.finishNode(node, "InterfaceTypeAnnotation");
  1878. }
  1879. flowParseObjectPropertyKey() {
  1880. return this.match(types$1.num) || this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1881. }
  1882. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1883. node.static = isStatic;
  1884. if (this.lookahead().type === types$1.colon) {
  1885. node.id = this.flowParseObjectPropertyKey();
  1886. node.key = this.flowParseTypeInitialiser();
  1887. } else {
  1888. node.id = null;
  1889. node.key = this.flowParseType();
  1890. }
  1891. this.expect(types$1.bracketR);
  1892. node.value = this.flowParseTypeInitialiser();
  1893. node.variance = variance;
  1894. return this.finishNode(node, "ObjectTypeIndexer");
  1895. }
  1896. flowParseObjectTypeInternalSlot(node, isStatic) {
  1897. node.static = isStatic;
  1898. node.id = this.flowParseObjectPropertyKey();
  1899. this.expect(types$1.bracketR);
  1900. this.expect(types$1.bracketR);
  1901. if (this.isRelational("<") || this.match(types$1.parenL)) {
  1902. node.method = true;
  1903. node.optional = false;
  1904. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1905. } else {
  1906. node.method = false;
  1907. if (this.eat(types$1.question)) {
  1908. node.optional = true;
  1909. }
  1910. node.value = this.flowParseTypeInitialiser();
  1911. }
  1912. return this.finishNode(node, "ObjectTypeInternalSlot");
  1913. }
  1914. flowParseObjectTypeMethodish(node) {
  1915. node.params = [];
  1916. node.rest = null;
  1917. node.typeParameters = null;
  1918. node.this = null;
  1919. if (this.isRelational("<")) {
  1920. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1921. }
  1922. this.expect(types$1.parenL);
  1923. if (this.match(types$1._this)) {
  1924. node.this = this.flowParseFunctionTypeParam(true);
  1925. node.this.name = null;
  1926. if (!this.match(types$1.parenR)) {
  1927. this.expect(types$1.comma);
  1928. }
  1929. }
  1930. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  1931. node.params.push(this.flowParseFunctionTypeParam(false));
  1932. if (!this.match(types$1.parenR)) {
  1933. this.expect(types$1.comma);
  1934. }
  1935. }
  1936. if (this.eat(types$1.ellipsis)) {
  1937. node.rest = this.flowParseFunctionTypeParam(false);
  1938. }
  1939. this.expect(types$1.parenR);
  1940. node.returnType = this.flowParseTypeInitialiser();
  1941. return this.finishNode(node, "FunctionTypeAnnotation");
  1942. }
  1943. flowParseObjectTypeCallProperty(node, isStatic) {
  1944. const valueNode = this.startNode();
  1945. node.static = isStatic;
  1946. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1947. return this.finishNode(node, "ObjectTypeCallProperty");
  1948. }
  1949. flowParseObjectType({
  1950. allowStatic,
  1951. allowExact,
  1952. allowSpread,
  1953. allowProto,
  1954. allowInexact
  1955. }) {
  1956. const oldInType = this.state.inType;
  1957. this.state.inType = true;
  1958. const nodeStart = this.startNode();
  1959. nodeStart.callProperties = [];
  1960. nodeStart.properties = [];
  1961. nodeStart.indexers = [];
  1962. nodeStart.internalSlots = [];
  1963. let endDelim;
  1964. let exact;
  1965. let inexact = false;
  1966. if (allowExact && this.match(types$1.braceBarL)) {
  1967. this.expect(types$1.braceBarL);
  1968. endDelim = types$1.braceBarR;
  1969. exact = true;
  1970. } else {
  1971. this.expect(types$1.braceL);
  1972. endDelim = types$1.braceR;
  1973. exact = false;
  1974. }
  1975. nodeStart.exact = exact;
  1976. while (!this.match(endDelim)) {
  1977. let isStatic = false;
  1978. let protoStart = null;
  1979. let inexactStart = null;
  1980. const node = this.startNode();
  1981. if (allowProto && this.isContextual("proto")) {
  1982. const lookahead = this.lookahead();
  1983. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1984. this.next();
  1985. protoStart = this.state.start;
  1986. allowStatic = false;
  1987. }
  1988. }
  1989. if (allowStatic && this.isContextual("static")) {
  1990. const lookahead = this.lookahead();
  1991. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1992. this.next();
  1993. isStatic = true;
  1994. }
  1995. }
  1996. const variance = this.flowParseVariance();
  1997. if (this.eat(types$1.bracketL)) {
  1998. if (protoStart != null) {
  1999. this.unexpected(protoStart);
  2000. }
  2001. if (this.eat(types$1.bracketL)) {
  2002. if (variance) {
  2003. this.unexpected(variance.start);
  2004. }
  2005. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  2006. } else {
  2007. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  2008. }
  2009. } else if (this.match(types$1.parenL) || this.isRelational("<")) {
  2010. if (protoStart != null) {
  2011. this.unexpected(protoStart);
  2012. }
  2013. if (variance) {
  2014. this.unexpected(variance.start);
  2015. }
  2016. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  2017. } else {
  2018. let kind = "init";
  2019. if (this.isContextual("get") || this.isContextual("set")) {
  2020. const lookahead = this.lookahead();
  2021. if (lookahead.type === types$1.name || lookahead.type === types$1.string || lookahead.type === types$1.num) {
  2022. kind = this.state.value;
  2023. this.next();
  2024. }
  2025. }
  2026. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  2027. if (propOrInexact === null) {
  2028. inexact = true;
  2029. inexactStart = this.state.lastTokStart;
  2030. } else {
  2031. nodeStart.properties.push(propOrInexact);
  2032. }
  2033. }
  2034. this.flowObjectTypeSemicolon();
  2035. if (inexactStart && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  2036. this.raise(inexactStart, FlowErrors.UnexpectedExplicitInexactInObject);
  2037. }
  2038. }
  2039. this.expect(endDelim);
  2040. if (allowSpread) {
  2041. nodeStart.inexact = inexact;
  2042. }
  2043. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  2044. this.state.inType = oldInType;
  2045. return out;
  2046. }
  2047. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  2048. if (this.eat(types$1.ellipsis)) {
  2049. const isInexactToken = this.match(types$1.comma) || this.match(types$1.semi) || this.match(types$1.braceR) || this.match(types$1.braceBarR);
  2050. if (isInexactToken) {
  2051. if (!allowSpread) {
  2052. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideNonObject);
  2053. } else if (!allowInexact) {
  2054. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideExact);
  2055. }
  2056. if (variance) {
  2057. this.raise(variance.start, FlowErrors.InexactVariance);
  2058. }
  2059. return null;
  2060. }
  2061. if (!allowSpread) {
  2062. this.raise(this.state.lastTokStart, FlowErrors.UnexpectedSpreadType);
  2063. }
  2064. if (protoStart != null) {
  2065. this.unexpected(protoStart);
  2066. }
  2067. if (variance) {
  2068. this.raise(variance.start, FlowErrors.SpreadVariance);
  2069. }
  2070. node.argument = this.flowParseType();
  2071. return this.finishNode(node, "ObjectTypeSpreadProperty");
  2072. } else {
  2073. node.key = this.flowParseObjectPropertyKey();
  2074. node.static = isStatic;
  2075. node.proto = protoStart != null;
  2076. node.kind = kind;
  2077. let optional = false;
  2078. if (this.isRelational("<") || this.match(types$1.parenL)) {
  2079. node.method = true;
  2080. if (protoStart != null) {
  2081. this.unexpected(protoStart);
  2082. }
  2083. if (variance) {
  2084. this.unexpected(variance.start);
  2085. }
  2086. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  2087. if (kind === "get" || kind === "set") {
  2088. this.flowCheckGetterSetterParams(node);
  2089. }
  2090. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  2091. this.raise(node.value.this.start, FlowErrors.ThisParamBannedInConstructor);
  2092. }
  2093. } else {
  2094. if (kind !== "init") this.unexpected();
  2095. node.method = false;
  2096. if (this.eat(types$1.question)) {
  2097. optional = true;
  2098. }
  2099. node.value = this.flowParseTypeInitialiser();
  2100. node.variance = variance;
  2101. }
  2102. node.optional = optional;
  2103. return this.finishNode(node, "ObjectTypeProperty");
  2104. }
  2105. }
  2106. flowCheckGetterSetterParams(property) {
  2107. const paramCount = property.kind === "get" ? 0 : 1;
  2108. const start = property.start;
  2109. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  2110. if (property.value.this) {
  2111. this.raise(property.value.this.start, property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam);
  2112. }
  2113. if (length !== paramCount) {
  2114. if (property.kind === "get") {
  2115. this.raise(start, ErrorMessages.BadGetterArity);
  2116. } else {
  2117. this.raise(start, ErrorMessages.BadSetterArity);
  2118. }
  2119. }
  2120. if (property.kind === "set" && property.value.rest) {
  2121. this.raise(start, ErrorMessages.BadSetterRestParameter);
  2122. }
  2123. }
  2124. flowObjectTypeSemicolon() {
  2125. if (!this.eat(types$1.semi) && !this.eat(types$1.comma) && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  2126. this.unexpected();
  2127. }
  2128. }
  2129. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  2130. startPos = startPos || this.state.start;
  2131. startLoc = startLoc || this.state.startLoc;
  2132. let node = id || this.flowParseRestrictedIdentifier(true);
  2133. while (this.eat(types$1.dot)) {
  2134. const node2 = this.startNodeAt(startPos, startLoc);
  2135. node2.qualification = node;
  2136. node2.id = this.flowParseRestrictedIdentifier(true);
  2137. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  2138. }
  2139. return node;
  2140. }
  2141. flowParseGenericType(startPos, startLoc, id) {
  2142. const node = this.startNodeAt(startPos, startLoc);
  2143. node.typeParameters = null;
  2144. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  2145. if (this.isRelational("<")) {
  2146. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2147. }
  2148. return this.finishNode(node, "GenericTypeAnnotation");
  2149. }
  2150. flowParseTypeofType() {
  2151. const node = this.startNode();
  2152. this.expect(types$1._typeof);
  2153. node.argument = this.flowParsePrimaryType();
  2154. return this.finishNode(node, "TypeofTypeAnnotation");
  2155. }
  2156. flowParseTupleType() {
  2157. const node = this.startNode();
  2158. node.types = [];
  2159. this.expect(types$1.bracketL);
  2160. while (this.state.pos < this.length && !this.match(types$1.bracketR)) {
  2161. node.types.push(this.flowParseType());
  2162. if (this.match(types$1.bracketR)) break;
  2163. this.expect(types$1.comma);
  2164. }
  2165. this.expect(types$1.bracketR);
  2166. return this.finishNode(node, "TupleTypeAnnotation");
  2167. }
  2168. flowParseFunctionTypeParam(first) {
  2169. let name = null;
  2170. let optional = false;
  2171. let typeAnnotation = null;
  2172. const node = this.startNode();
  2173. const lh = this.lookahead();
  2174. const isThis = this.state.type === types$1._this;
  2175. if (lh.type === types$1.colon || lh.type === types$1.question) {
  2176. if (isThis && !first) {
  2177. this.raise(node.start, FlowErrors.ThisParamMustBeFirst);
  2178. }
  2179. name = this.parseIdentifier(isThis);
  2180. if (this.eat(types$1.question)) {
  2181. optional = true;
  2182. if (isThis) {
  2183. this.raise(node.start, FlowErrors.ThisParamMayNotBeOptional);
  2184. }
  2185. }
  2186. typeAnnotation = this.flowParseTypeInitialiser();
  2187. } else {
  2188. typeAnnotation = this.flowParseType();
  2189. }
  2190. node.name = name;
  2191. node.optional = optional;
  2192. node.typeAnnotation = typeAnnotation;
  2193. return this.finishNode(node, "FunctionTypeParam");
  2194. }
  2195. reinterpretTypeAsFunctionTypeParam(type) {
  2196. const node = this.startNodeAt(type.start, type.loc.start);
  2197. node.name = null;
  2198. node.optional = false;
  2199. node.typeAnnotation = type;
  2200. return this.finishNode(node, "FunctionTypeParam");
  2201. }
  2202. flowParseFunctionTypeParams(params = []) {
  2203. let rest = null;
  2204. let _this = null;
  2205. if (this.match(types$1._this)) {
  2206. _this = this.flowParseFunctionTypeParam(true);
  2207. _this.name = null;
  2208. if (!this.match(types$1.parenR)) {
  2209. this.expect(types$1.comma);
  2210. }
  2211. }
  2212. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2213. params.push(this.flowParseFunctionTypeParam(false));
  2214. if (!this.match(types$1.parenR)) {
  2215. this.expect(types$1.comma);
  2216. }
  2217. }
  2218. if (this.eat(types$1.ellipsis)) {
  2219. rest = this.flowParseFunctionTypeParam(false);
  2220. }
  2221. return {
  2222. params,
  2223. rest,
  2224. _this
  2225. };
  2226. }
  2227. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  2228. switch (id.name) {
  2229. case "any":
  2230. return this.finishNode(node, "AnyTypeAnnotation");
  2231. case "bool":
  2232. case "boolean":
  2233. return this.finishNode(node, "BooleanTypeAnnotation");
  2234. case "mixed":
  2235. return this.finishNode(node, "MixedTypeAnnotation");
  2236. case "empty":
  2237. return this.finishNode(node, "EmptyTypeAnnotation");
  2238. case "number":
  2239. return this.finishNode(node, "NumberTypeAnnotation");
  2240. case "string":
  2241. return this.finishNode(node, "StringTypeAnnotation");
  2242. case "symbol":
  2243. return this.finishNode(node, "SymbolTypeAnnotation");
  2244. default:
  2245. this.checkNotUnderscore(id.name);
  2246. return this.flowParseGenericType(startPos, startLoc, id);
  2247. }
  2248. }
  2249. flowParsePrimaryType() {
  2250. const startPos = this.state.start;
  2251. const startLoc = this.state.startLoc;
  2252. const node = this.startNode();
  2253. let tmp;
  2254. let type;
  2255. let isGroupedType = false;
  2256. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2257. switch (this.state.type) {
  2258. case types$1.name:
  2259. if (this.isContextual("interface")) {
  2260. return this.flowParseInterfaceType();
  2261. }
  2262. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  2263. case types$1.braceL:
  2264. return this.flowParseObjectType({
  2265. allowStatic: false,
  2266. allowExact: false,
  2267. allowSpread: true,
  2268. allowProto: false,
  2269. allowInexact: true
  2270. });
  2271. case types$1.braceBarL:
  2272. return this.flowParseObjectType({
  2273. allowStatic: false,
  2274. allowExact: true,
  2275. allowSpread: true,
  2276. allowProto: false,
  2277. allowInexact: false
  2278. });
  2279. case types$1.bracketL:
  2280. this.state.noAnonFunctionType = false;
  2281. type = this.flowParseTupleType();
  2282. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2283. return type;
  2284. case types$1.relational:
  2285. if (this.state.value === "<") {
  2286. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2287. this.expect(types$1.parenL);
  2288. tmp = this.flowParseFunctionTypeParams();
  2289. node.params = tmp.params;
  2290. node.rest = tmp.rest;
  2291. node.this = tmp._this;
  2292. this.expect(types$1.parenR);
  2293. this.expect(types$1.arrow);
  2294. node.returnType = this.flowParseType();
  2295. return this.finishNode(node, "FunctionTypeAnnotation");
  2296. }
  2297. break;
  2298. case types$1.parenL:
  2299. this.next();
  2300. if (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2301. if (this.match(types$1.name) || this.match(types$1._this)) {
  2302. const token = this.lookahead().type;
  2303. isGroupedType = token !== types$1.question && token !== types$1.colon;
  2304. } else {
  2305. isGroupedType = true;
  2306. }
  2307. }
  2308. if (isGroupedType) {
  2309. this.state.noAnonFunctionType = false;
  2310. type = this.flowParseType();
  2311. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2312. if (this.state.noAnonFunctionType || !(this.match(types$1.comma) || this.match(types$1.parenR) && this.lookahead().type === types$1.arrow)) {
  2313. this.expect(types$1.parenR);
  2314. return type;
  2315. } else {
  2316. this.eat(types$1.comma);
  2317. }
  2318. }
  2319. if (type) {
  2320. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  2321. } else {
  2322. tmp = this.flowParseFunctionTypeParams();
  2323. }
  2324. node.params = tmp.params;
  2325. node.rest = tmp.rest;
  2326. node.this = tmp._this;
  2327. this.expect(types$1.parenR);
  2328. this.expect(types$1.arrow);
  2329. node.returnType = this.flowParseType();
  2330. node.typeParameters = null;
  2331. return this.finishNode(node, "FunctionTypeAnnotation");
  2332. case types$1.string:
  2333. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  2334. case types$1._true:
  2335. case types$1._false:
  2336. node.value = this.match(types$1._true);
  2337. this.next();
  2338. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  2339. case types$1.plusMin:
  2340. if (this.state.value === "-") {
  2341. this.next();
  2342. if (this.match(types$1.num)) {
  2343. return this.parseLiteral(-this.state.value, "NumberLiteralTypeAnnotation", node.start, node.loc.start);
  2344. }
  2345. if (this.match(types$1.bigint)) {
  2346. return this.parseLiteral(-this.state.value, "BigIntLiteralTypeAnnotation", node.start, node.loc.start);
  2347. }
  2348. throw this.raise(this.state.start, FlowErrors.UnexpectedSubtractionOperand);
  2349. }
  2350. throw this.unexpected();
  2351. case types$1.num:
  2352. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  2353. case types$1.bigint:
  2354. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  2355. case types$1._void:
  2356. this.next();
  2357. return this.finishNode(node, "VoidTypeAnnotation");
  2358. case types$1._null:
  2359. this.next();
  2360. return this.finishNode(node, "NullLiteralTypeAnnotation");
  2361. case types$1._this:
  2362. this.next();
  2363. return this.finishNode(node, "ThisTypeAnnotation");
  2364. case types$1.star:
  2365. this.next();
  2366. return this.finishNode(node, "ExistsTypeAnnotation");
  2367. default:
  2368. if (this.state.type.keyword === "typeof") {
  2369. return this.flowParseTypeofType();
  2370. } else if (this.state.type.keyword) {
  2371. const label = this.state.type.label;
  2372. this.next();
  2373. return super.createIdentifier(node, label);
  2374. }
  2375. }
  2376. throw this.unexpected();
  2377. }
  2378. flowParsePostfixType() {
  2379. const startPos = this.state.start;
  2380. const startLoc = this.state.startLoc;
  2381. let type = this.flowParsePrimaryType();
  2382. let seenOptionalIndexedAccess = false;
  2383. while ((this.match(types$1.bracketL) || this.match(types$1.questionDot)) && !this.canInsertSemicolon()) {
  2384. const node = this.startNodeAt(startPos, startLoc);
  2385. const optional = this.eat(types$1.questionDot);
  2386. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  2387. this.expect(types$1.bracketL);
  2388. if (!optional && this.match(types$1.bracketR)) {
  2389. node.elementType = type;
  2390. this.next();
  2391. type = this.finishNode(node, "ArrayTypeAnnotation");
  2392. } else {
  2393. node.objectType = type;
  2394. node.indexType = this.flowParseType();
  2395. this.expect(types$1.bracketR);
  2396. if (seenOptionalIndexedAccess) {
  2397. node.optional = optional;
  2398. type = this.finishNode(node, "OptionalIndexedAccessType");
  2399. } else {
  2400. type = this.finishNode(node, "IndexedAccessType");
  2401. }
  2402. }
  2403. }
  2404. return type;
  2405. }
  2406. flowParsePrefixType() {
  2407. const node = this.startNode();
  2408. if (this.eat(types$1.question)) {
  2409. node.typeAnnotation = this.flowParsePrefixType();
  2410. return this.finishNode(node, "NullableTypeAnnotation");
  2411. } else {
  2412. return this.flowParsePostfixType();
  2413. }
  2414. }
  2415. flowParseAnonFunctionWithoutParens() {
  2416. const param = this.flowParsePrefixType();
  2417. if (!this.state.noAnonFunctionType && this.eat(types$1.arrow)) {
  2418. const node = this.startNodeAt(param.start, param.loc.start);
  2419. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  2420. node.rest = null;
  2421. node.this = null;
  2422. node.returnType = this.flowParseType();
  2423. node.typeParameters = null;
  2424. return this.finishNode(node, "FunctionTypeAnnotation");
  2425. }
  2426. return param;
  2427. }
  2428. flowParseIntersectionType() {
  2429. const node = this.startNode();
  2430. this.eat(types$1.bitwiseAND);
  2431. const type = this.flowParseAnonFunctionWithoutParens();
  2432. node.types = [type];
  2433. while (this.eat(types$1.bitwiseAND)) {
  2434. node.types.push(this.flowParseAnonFunctionWithoutParens());
  2435. }
  2436. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  2437. }
  2438. flowParseUnionType() {
  2439. const node = this.startNode();
  2440. this.eat(types$1.bitwiseOR);
  2441. const type = this.flowParseIntersectionType();
  2442. node.types = [type];
  2443. while (this.eat(types$1.bitwiseOR)) {
  2444. node.types.push(this.flowParseIntersectionType());
  2445. }
  2446. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  2447. }
  2448. flowParseType() {
  2449. const oldInType = this.state.inType;
  2450. this.state.inType = true;
  2451. const type = this.flowParseUnionType();
  2452. this.state.inType = oldInType;
  2453. this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
  2454. return type;
  2455. }
  2456. flowParseTypeOrImplicitInstantiation() {
  2457. if (this.state.type === types$1.name && this.state.value === "_") {
  2458. const startPos = this.state.start;
  2459. const startLoc = this.state.startLoc;
  2460. const node = this.parseIdentifier();
  2461. return this.flowParseGenericType(startPos, startLoc, node);
  2462. } else {
  2463. return this.flowParseType();
  2464. }
  2465. }
  2466. flowParseTypeAnnotation() {
  2467. const node = this.startNode();
  2468. node.typeAnnotation = this.flowParseTypeInitialiser();
  2469. return this.finishNode(node, "TypeAnnotation");
  2470. }
  2471. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  2472. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  2473. if (this.match(types$1.colon)) {
  2474. ident.typeAnnotation = this.flowParseTypeAnnotation();
  2475. this.resetEndLocation(ident);
  2476. }
  2477. return ident;
  2478. }
  2479. typeCastToParameter(node) {
  2480. node.expression.typeAnnotation = node.typeAnnotation;
  2481. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  2482. return node.expression;
  2483. }
  2484. flowParseVariance() {
  2485. let variance = null;
  2486. if (this.match(types$1.plusMin)) {
  2487. variance = this.startNode();
  2488. if (this.state.value === "+") {
  2489. variance.kind = "plus";
  2490. } else {
  2491. variance.kind = "minus";
  2492. }
  2493. this.next();
  2494. this.finishNode(variance, "Variance");
  2495. }
  2496. return variance;
  2497. }
  2498. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  2499. if (allowExpressionBody) {
  2500. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  2501. }
  2502. return super.parseFunctionBody(node, false, isMethod);
  2503. }
  2504. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  2505. if (this.match(types$1.colon)) {
  2506. const typeNode = this.startNode();
  2507. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2508. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2509. }
  2510. super.parseFunctionBodyAndFinish(node, type, isMethod);
  2511. }
  2512. parseStatement(context, topLevel) {
  2513. if (this.state.strict && this.match(types$1.name) && this.state.value === "interface") {
  2514. const lookahead = this.lookahead();
  2515. if (lookahead.type === types$1.name || isKeyword(lookahead.value)) {
  2516. const node = this.startNode();
  2517. this.next();
  2518. return this.flowParseInterface(node);
  2519. }
  2520. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2521. const node = this.startNode();
  2522. this.next();
  2523. return this.flowParseEnumDeclaration(node);
  2524. }
  2525. const stmt = super.parseStatement(context, topLevel);
  2526. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  2527. this.flowPragma = null;
  2528. }
  2529. return stmt;
  2530. }
  2531. parseExpressionStatement(node, expr) {
  2532. if (expr.type === "Identifier") {
  2533. if (expr.name === "declare") {
  2534. if (this.match(types$1._class) || this.match(types$1.name) || this.match(types$1._function) || this.match(types$1._var) || this.match(types$1._export)) {
  2535. return this.flowParseDeclare(node);
  2536. }
  2537. } else if (this.match(types$1.name)) {
  2538. if (expr.name === "interface") {
  2539. return this.flowParseInterface(node);
  2540. } else if (expr.name === "type") {
  2541. return this.flowParseTypeAlias(node);
  2542. } else if (expr.name === "opaque") {
  2543. return this.flowParseOpaqueType(node, false);
  2544. }
  2545. }
  2546. }
  2547. return super.parseExpressionStatement(node, expr);
  2548. }
  2549. shouldParseExportDeclaration() {
  2550. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || this.shouldParseEnums() && this.isContextual("enum") || super.shouldParseExportDeclaration();
  2551. }
  2552. isExportDefaultSpecifier() {
  2553. if (this.match(types$1.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque" || this.shouldParseEnums() && this.state.value === "enum")) {
  2554. return false;
  2555. }
  2556. return super.isExportDefaultSpecifier();
  2557. }
  2558. parseExportDefaultExpression() {
  2559. if (this.shouldParseEnums() && this.isContextual("enum")) {
  2560. const node = this.startNode();
  2561. this.next();
  2562. return this.flowParseEnumDeclaration(node);
  2563. }
  2564. return super.parseExportDefaultExpression();
  2565. }
  2566. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  2567. if (!this.match(types$1.question)) return expr;
  2568. if (refNeedsArrowPos) {
  2569. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  2570. if (!result.node) {
  2571. refNeedsArrowPos.start = result.error.pos || this.state.start;
  2572. return expr;
  2573. }
  2574. if (result.error) this.state = result.failState;
  2575. return result.node;
  2576. }
  2577. this.expect(types$1.question);
  2578. const state = this.state.clone();
  2579. const originalNoArrowAt = this.state.noArrowAt;
  2580. const node = this.startNodeAt(startPos, startLoc);
  2581. let {
  2582. consequent,
  2583. failed
  2584. } = this.tryParseConditionalConsequent();
  2585. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2586. if (failed || invalid.length > 0) {
  2587. const noArrowAt = [...originalNoArrowAt];
  2588. if (invalid.length > 0) {
  2589. this.state = state;
  2590. this.state.noArrowAt = noArrowAt;
  2591. for (let i = 0; i < invalid.length; i++) {
  2592. noArrowAt.push(invalid[i].start);
  2593. }
  2594. ({
  2595. consequent,
  2596. failed
  2597. } = this.tryParseConditionalConsequent());
  2598. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2599. }
  2600. if (failed && valid.length > 1) {
  2601. this.raise(state.start, FlowErrors.AmbiguousConditionalArrow);
  2602. }
  2603. if (failed && valid.length === 1) {
  2604. this.state = state;
  2605. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  2606. ({
  2607. consequent,
  2608. failed
  2609. } = this.tryParseConditionalConsequent());
  2610. }
  2611. }
  2612. this.getArrowLikeExpressions(consequent, true);
  2613. this.state.noArrowAt = originalNoArrowAt;
  2614. this.expect(types$1.colon);
  2615. node.test = expr;
  2616. node.consequent = consequent;
  2617. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined, undefined));
  2618. return this.finishNode(node, "ConditionalExpression");
  2619. }
  2620. tryParseConditionalConsequent() {
  2621. this.state.noArrowParamsConversionAt.push(this.state.start);
  2622. const consequent = this.parseMaybeAssignAllowIn();
  2623. const failed = !this.match(types$1.colon);
  2624. this.state.noArrowParamsConversionAt.pop();
  2625. return {
  2626. consequent,
  2627. failed
  2628. };
  2629. }
  2630. getArrowLikeExpressions(node, disallowInvalid) {
  2631. const stack = [node];
  2632. const arrows = [];
  2633. while (stack.length !== 0) {
  2634. const node = stack.pop();
  2635. if (node.type === "ArrowFunctionExpression") {
  2636. if (node.typeParameters || !node.returnType) {
  2637. this.finishArrowValidation(node);
  2638. } else {
  2639. arrows.push(node);
  2640. }
  2641. stack.push(node.body);
  2642. } else if (node.type === "ConditionalExpression") {
  2643. stack.push(node.consequent);
  2644. stack.push(node.alternate);
  2645. }
  2646. }
  2647. if (disallowInvalid) {
  2648. arrows.forEach(node => this.finishArrowValidation(node));
  2649. return [arrows, []];
  2650. }
  2651. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  2652. }
  2653. finishArrowValidation(node) {
  2654. var _node$extra;
  2655. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingComma, false);
  2656. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  2657. super.checkParams(node, false, true);
  2658. this.scope.exit();
  2659. }
  2660. forwardNoArrowParamsConversionAt(node, parse) {
  2661. let result;
  2662. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2663. this.state.noArrowParamsConversionAt.push(this.state.start);
  2664. result = parse();
  2665. this.state.noArrowParamsConversionAt.pop();
  2666. } else {
  2667. result = parse();
  2668. }
  2669. return result;
  2670. }
  2671. parseParenItem(node, startPos, startLoc) {
  2672. node = super.parseParenItem(node, startPos, startLoc);
  2673. if (this.eat(types$1.question)) {
  2674. node.optional = true;
  2675. this.resetEndLocation(node);
  2676. }
  2677. if (this.match(types$1.colon)) {
  2678. const typeCastNode = this.startNodeAt(startPos, startLoc);
  2679. typeCastNode.expression = node;
  2680. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  2681. return this.finishNode(typeCastNode, "TypeCastExpression");
  2682. }
  2683. return node;
  2684. }
  2685. assertModuleNodeAllowed(node) {
  2686. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  2687. return;
  2688. }
  2689. super.assertModuleNodeAllowed(node);
  2690. }
  2691. parseExport(node) {
  2692. const decl = super.parseExport(node);
  2693. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  2694. decl.exportKind = decl.exportKind || "value";
  2695. }
  2696. return decl;
  2697. }
  2698. parseExportDeclaration(node) {
  2699. if (this.isContextual("type")) {
  2700. node.exportKind = "type";
  2701. const declarationNode = this.startNode();
  2702. this.next();
  2703. if (this.match(types$1.braceL)) {
  2704. node.specifiers = this.parseExportSpecifiers();
  2705. this.parseExportFrom(node);
  2706. return null;
  2707. } else {
  2708. return this.flowParseTypeAlias(declarationNode);
  2709. }
  2710. } else if (this.isContextual("opaque")) {
  2711. node.exportKind = "type";
  2712. const declarationNode = this.startNode();
  2713. this.next();
  2714. return this.flowParseOpaqueType(declarationNode, false);
  2715. } else if (this.isContextual("interface")) {
  2716. node.exportKind = "type";
  2717. const declarationNode = this.startNode();
  2718. this.next();
  2719. return this.flowParseInterface(declarationNode);
  2720. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2721. node.exportKind = "value";
  2722. const declarationNode = this.startNode();
  2723. this.next();
  2724. return this.flowParseEnumDeclaration(declarationNode);
  2725. } else {
  2726. return super.parseExportDeclaration(node);
  2727. }
  2728. }
  2729. eatExportStar(node) {
  2730. if (super.eatExportStar(...arguments)) return true;
  2731. if (this.isContextual("type") && this.lookahead().type === types$1.star) {
  2732. node.exportKind = "type";
  2733. this.next();
  2734. this.next();
  2735. return true;
  2736. }
  2737. return false;
  2738. }
  2739. maybeParseExportNamespaceSpecifier(node) {
  2740. const pos = this.state.start;
  2741. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  2742. if (hasNamespace && node.exportKind === "type") {
  2743. this.unexpected(pos);
  2744. }
  2745. return hasNamespace;
  2746. }
  2747. parseClassId(node, isStatement, optionalId) {
  2748. super.parseClassId(node, isStatement, optionalId);
  2749. if (this.isRelational("<")) {
  2750. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2751. }
  2752. }
  2753. parseClassMember(classBody, member, state) {
  2754. const pos = this.state.start;
  2755. if (this.isContextual("declare")) {
  2756. if (this.parseClassMemberFromModifier(classBody, member)) {
  2757. return;
  2758. }
  2759. member.declare = true;
  2760. }
  2761. super.parseClassMember(classBody, member, state);
  2762. if (member.declare) {
  2763. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  2764. this.raise(pos, FlowErrors.DeclareClassElement);
  2765. } else if (member.value) {
  2766. this.raise(member.value.start, FlowErrors.DeclareClassFieldInitializer);
  2767. }
  2768. }
  2769. }
  2770. isIterator(word) {
  2771. return word === "iterator" || word === "asyncIterator";
  2772. }
  2773. readIterator() {
  2774. const word = super.readWord1();
  2775. const fullWord = "@@" + word;
  2776. if (!this.isIterator(word) || !this.state.inType) {
  2777. this.raise(this.state.pos, ErrorMessages.InvalidIdentifier, fullWord);
  2778. }
  2779. this.finishToken(types$1.name, fullWord);
  2780. }
  2781. getTokenFromCode(code) {
  2782. const next = this.input.charCodeAt(this.state.pos + 1);
  2783. if (code === 123 && next === 124) {
  2784. return this.finishOp(types$1.braceBarL, 2);
  2785. } else if (this.state.inType && (code === 62 || code === 60)) {
  2786. return this.finishOp(types$1.relational, 1);
  2787. } else if (this.state.inType && code === 63) {
  2788. if (next === 46) {
  2789. return this.finishOp(types$1.questionDot, 2);
  2790. }
  2791. return this.finishOp(types$1.question, 1);
  2792. } else if (isIteratorStart(code, next)) {
  2793. this.state.pos += 2;
  2794. return this.readIterator();
  2795. } else {
  2796. return super.getTokenFromCode(code);
  2797. }
  2798. }
  2799. isAssignable(node, isBinding) {
  2800. switch (node.type) {
  2801. case "Identifier":
  2802. case "ObjectPattern":
  2803. case "ArrayPattern":
  2804. case "AssignmentPattern":
  2805. return true;
  2806. case "ObjectExpression":
  2807. {
  2808. const last = node.properties.length - 1;
  2809. return node.properties.every((prop, i) => {
  2810. return prop.type !== "ObjectMethod" && (i === last || prop.type === "SpreadElement") && this.isAssignable(prop);
  2811. });
  2812. }
  2813. case "ObjectProperty":
  2814. return this.isAssignable(node.value);
  2815. case "SpreadElement":
  2816. return this.isAssignable(node.argument);
  2817. case "ArrayExpression":
  2818. return node.elements.every(element => this.isAssignable(element));
  2819. case "AssignmentExpression":
  2820. return node.operator === "=";
  2821. case "ParenthesizedExpression":
  2822. case "TypeCastExpression":
  2823. return this.isAssignable(node.expression);
  2824. case "MemberExpression":
  2825. case "OptionalMemberExpression":
  2826. return !isBinding;
  2827. default:
  2828. return false;
  2829. }
  2830. }
  2831. toAssignable(node, isLHS = false) {
  2832. if (node.type === "TypeCastExpression") {
  2833. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  2834. } else {
  2835. return super.toAssignable(node, isLHS);
  2836. }
  2837. }
  2838. toAssignableList(exprList, trailingCommaPos, isLHS) {
  2839. for (let i = 0; i < exprList.length; i++) {
  2840. const expr = exprList[i];
  2841. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  2842. exprList[i] = this.typeCastToParameter(expr);
  2843. }
  2844. }
  2845. return super.toAssignableList(exprList, trailingCommaPos, isLHS);
  2846. }
  2847. toReferencedList(exprList, isParenthesizedExpr) {
  2848. for (let i = 0; i < exprList.length; i++) {
  2849. var _expr$extra;
  2850. const expr = exprList[i];
  2851. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  2852. this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern);
  2853. }
  2854. }
  2855. return exprList;
  2856. }
  2857. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  2858. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  2859. if (canBePattern && !this.state.maybeInArrowParameters) {
  2860. this.toReferencedList(node.elements);
  2861. }
  2862. return node;
  2863. }
  2864. checkLVal(expr, ...args) {
  2865. if (expr.type !== "TypeCastExpression") {
  2866. return super.checkLVal(expr, ...args);
  2867. }
  2868. }
  2869. parseClassProperty(node) {
  2870. if (this.match(types$1.colon)) {
  2871. node.typeAnnotation = this.flowParseTypeAnnotation();
  2872. }
  2873. return super.parseClassProperty(node);
  2874. }
  2875. parseClassPrivateProperty(node) {
  2876. if (this.match(types$1.colon)) {
  2877. node.typeAnnotation = this.flowParseTypeAnnotation();
  2878. }
  2879. return super.parseClassPrivateProperty(node);
  2880. }
  2881. isClassMethod() {
  2882. return this.isRelational("<") || super.isClassMethod();
  2883. }
  2884. isClassProperty() {
  2885. return this.match(types$1.colon) || super.isClassProperty();
  2886. }
  2887. isNonstaticConstructor(method) {
  2888. return !this.match(types$1.colon) && super.isNonstaticConstructor(method);
  2889. }
  2890. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  2891. if (method.variance) {
  2892. this.unexpected(method.variance.start);
  2893. }
  2894. delete method.variance;
  2895. if (this.isRelational("<")) {
  2896. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2897. }
  2898. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  2899. if (method.params && isConstructor) {
  2900. const params = method.params;
  2901. if (params.length > 0 && this.isThisParam(params[0])) {
  2902. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2903. }
  2904. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  2905. const params = method.value.params;
  2906. if (params.length > 0 && this.isThisParam(params[0])) {
  2907. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2908. }
  2909. }
  2910. }
  2911. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  2912. if (method.variance) {
  2913. this.unexpected(method.variance.start);
  2914. }
  2915. delete method.variance;
  2916. if (this.isRelational("<")) {
  2917. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2918. }
  2919. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  2920. }
  2921. parseClassSuper(node) {
  2922. super.parseClassSuper(node);
  2923. if (node.superClass && this.isRelational("<")) {
  2924. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  2925. }
  2926. if (this.isContextual("implements")) {
  2927. this.next();
  2928. const implemented = node.implements = [];
  2929. do {
  2930. const node = this.startNode();
  2931. node.id = this.flowParseRestrictedIdentifier(true);
  2932. if (this.isRelational("<")) {
  2933. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2934. } else {
  2935. node.typeParameters = null;
  2936. }
  2937. implemented.push(this.finishNode(node, "ClassImplements"));
  2938. } while (this.eat(types$1.comma));
  2939. }
  2940. }
  2941. checkGetterSetterParams(method) {
  2942. super.checkGetterSetterParams(method);
  2943. const params = this.getObjectOrClassMethodParams(method);
  2944. if (params.length > 0) {
  2945. const param = params[0];
  2946. if (this.isThisParam(param) && method.kind === "get") {
  2947. this.raise(param.start, FlowErrors.GetterMayNotHaveThisParam);
  2948. } else if (this.isThisParam(param)) {
  2949. this.raise(param.start, FlowErrors.SetterMayNotHaveThisParam);
  2950. }
  2951. }
  2952. }
  2953. parsePropertyName(node, isPrivateNameAllowed) {
  2954. const variance = this.flowParseVariance();
  2955. const key = super.parsePropertyName(node, isPrivateNameAllowed);
  2956. node.variance = variance;
  2957. return key;
  2958. }
  2959. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  2960. if (prop.variance) {
  2961. this.unexpected(prop.variance.start);
  2962. }
  2963. delete prop.variance;
  2964. let typeParameters;
  2965. if (this.isRelational("<") && !isAccessor) {
  2966. typeParameters = this.flowParseTypeParameterDeclaration();
  2967. if (!this.match(types$1.parenL)) this.unexpected();
  2968. }
  2969. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  2970. if (typeParameters) {
  2971. (prop.value || prop).typeParameters = typeParameters;
  2972. }
  2973. }
  2974. parseAssignableListItemTypes(param) {
  2975. if (this.eat(types$1.question)) {
  2976. if (param.type !== "Identifier") {
  2977. this.raise(param.start, FlowErrors.OptionalBindingPattern);
  2978. }
  2979. if (this.isThisParam(param)) {
  2980. this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional);
  2981. }
  2982. param.optional = true;
  2983. }
  2984. if (this.match(types$1.colon)) {
  2985. param.typeAnnotation = this.flowParseTypeAnnotation();
  2986. } else if (this.isThisParam(param)) {
  2987. this.raise(param.start, FlowErrors.ThisParamAnnotationRequired);
  2988. }
  2989. if (this.match(types$1.eq) && this.isThisParam(param)) {
  2990. this.raise(param.start, FlowErrors.ThisParamNoDefault);
  2991. }
  2992. this.resetEndLocation(param);
  2993. return param;
  2994. }
  2995. parseMaybeDefault(startPos, startLoc, left) {
  2996. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2997. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2998. this.raise(node.typeAnnotation.start, FlowErrors.TypeBeforeInitializer);
  2999. }
  3000. return node;
  3001. }
  3002. shouldParseDefaultImport(node) {
  3003. if (!hasTypeImportKind(node)) {
  3004. return super.shouldParseDefaultImport(node);
  3005. }
  3006. return isMaybeDefaultImport(this.state);
  3007. }
  3008. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  3009. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  3010. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  3011. node.specifiers.push(this.finishNode(specifier, type));
  3012. }
  3013. maybeParseDefaultImportSpecifier(node) {
  3014. node.importKind = "value";
  3015. let kind = null;
  3016. if (this.match(types$1._typeof)) {
  3017. kind = "typeof";
  3018. } else if (this.isContextual("type")) {
  3019. kind = "type";
  3020. }
  3021. if (kind) {
  3022. const lh = this.lookahead();
  3023. if (kind === "type" && lh.type === types$1.star) {
  3024. this.unexpected(lh.start);
  3025. }
  3026. if (isMaybeDefaultImport(lh) || lh.type === types$1.braceL || lh.type === types$1.star) {
  3027. this.next();
  3028. node.importKind = kind;
  3029. }
  3030. }
  3031. return super.maybeParseDefaultImportSpecifier(node);
  3032. }
  3033. parseImportSpecifier(node) {
  3034. const specifier = this.startNode();
  3035. const firstIdentLoc = this.state.start;
  3036. const firstIdent = this.parseModuleExportName();
  3037. let specifierTypeKind = null;
  3038. if (firstIdent.type === "Identifier") {
  3039. if (firstIdent.name === "type") {
  3040. specifierTypeKind = "type";
  3041. } else if (firstIdent.name === "typeof") {
  3042. specifierTypeKind = "typeof";
  3043. }
  3044. }
  3045. let isBinding = false;
  3046. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  3047. const as_ident = this.parseIdentifier(true);
  3048. if (specifierTypeKind !== null && !this.match(types$1.name) && !this.state.type.keyword) {
  3049. specifier.imported = as_ident;
  3050. specifier.importKind = specifierTypeKind;
  3051. specifier.local = as_ident.__clone();
  3052. } else {
  3053. specifier.imported = firstIdent;
  3054. specifier.importKind = null;
  3055. specifier.local = this.parseIdentifier();
  3056. }
  3057. } else if (specifierTypeKind !== null && (this.match(types$1.name) || this.state.type.keyword)) {
  3058. specifier.imported = this.parseIdentifier(true);
  3059. specifier.importKind = specifierTypeKind;
  3060. if (this.eatContextual("as")) {
  3061. specifier.local = this.parseIdentifier();
  3062. } else {
  3063. isBinding = true;
  3064. specifier.local = specifier.imported.__clone();
  3065. }
  3066. } else {
  3067. if (firstIdent.type === "StringLiteral") {
  3068. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, firstIdent.value);
  3069. }
  3070. isBinding = true;
  3071. specifier.imported = firstIdent;
  3072. specifier.importKind = null;
  3073. specifier.local = specifier.imported.__clone();
  3074. }
  3075. const nodeIsTypeImport = hasTypeImportKind(node);
  3076. const specifierIsTypeImport = hasTypeImportKind(specifier);
  3077. if (nodeIsTypeImport && specifierIsTypeImport) {
  3078. this.raise(firstIdentLoc, FlowErrors.ImportTypeShorthandOnlyInPureImport);
  3079. }
  3080. if (nodeIsTypeImport || specifierIsTypeImport) {
  3081. this.checkReservedType(specifier.local.name, specifier.local.start, true);
  3082. }
  3083. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  3084. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  3085. }
  3086. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  3087. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  3088. }
  3089. parseBindingAtom() {
  3090. switch (this.state.type) {
  3091. case types$1._this:
  3092. return this.parseIdentifier(true);
  3093. default:
  3094. return super.parseBindingAtom();
  3095. }
  3096. }
  3097. parseFunctionParams(node, allowModifiers) {
  3098. const kind = node.kind;
  3099. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  3100. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3101. }
  3102. super.parseFunctionParams(node, allowModifiers);
  3103. }
  3104. parseVarId(decl, kind) {
  3105. super.parseVarId(decl, kind);
  3106. if (this.match(types$1.colon)) {
  3107. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  3108. this.resetEndLocation(decl.id);
  3109. }
  3110. }
  3111. parseAsyncArrowFromCallExpression(node, call) {
  3112. if (this.match(types$1.colon)) {
  3113. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3114. this.state.noAnonFunctionType = true;
  3115. node.returnType = this.flowParseTypeAnnotation();
  3116. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3117. }
  3118. return super.parseAsyncArrowFromCallExpression(node, call);
  3119. }
  3120. shouldParseAsyncArrow() {
  3121. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  3122. }
  3123. parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  3124. var _jsx;
  3125. let state = null;
  3126. let jsx;
  3127. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  3128. state = this.state.clone();
  3129. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos), state);
  3130. if (!jsx.error) return jsx.node;
  3131. const {
  3132. context
  3133. } = this.state;
  3134. if (context[context.length - 1] === types.j_oTag) {
  3135. context.length -= 2;
  3136. } else if (context[context.length - 1] === types.j_expr) {
  3137. context.length -= 1;
  3138. }
  3139. }
  3140. if ((_jsx = jsx) != null && _jsx.error || this.isRelational("<")) {
  3141. var _jsx2, _jsx3;
  3142. state = state || this.state.clone();
  3143. let typeParameters;
  3144. const arrow = this.tryParse(abort => {
  3145. var _arrowExpression$extr;
  3146. typeParameters = this.flowParseTypeParameterDeclaration();
  3147. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  3148. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos);
  3149. this.resetStartLocationFromNode(result, typeParameters);
  3150. return result;
  3151. });
  3152. if (arrowExpression.type !== "ArrowFunctionExpression" && (_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) {
  3153. abort();
  3154. }
  3155. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  3156. expr.typeParameters = typeParameters;
  3157. this.resetStartLocationFromNode(expr, typeParameters);
  3158. return arrowExpression;
  3159. }, state);
  3160. let arrowExpression = null;
  3161. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  3162. if (!arrow.error && !arrow.aborted) {
  3163. if (arrow.node.async) {
  3164. this.raise(typeParameters.start, FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction);
  3165. }
  3166. return arrow.node;
  3167. }
  3168. arrowExpression = arrow.node;
  3169. }
  3170. if ((_jsx2 = jsx) != null && _jsx2.node) {
  3171. this.state = jsx.failState;
  3172. return jsx.node;
  3173. }
  3174. if (arrowExpression) {
  3175. this.state = arrow.failState;
  3176. return arrowExpression;
  3177. }
  3178. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  3179. if (arrow.thrown) throw arrow.error;
  3180. throw this.raise(typeParameters.start, FlowErrors.UnexpectedTokenAfterTypeParameter);
  3181. }
  3182. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos);
  3183. }
  3184. parseArrow(node) {
  3185. if (this.match(types$1.colon)) {
  3186. const result = this.tryParse(() => {
  3187. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3188. this.state.noAnonFunctionType = true;
  3189. const typeNode = this.startNode();
  3190. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3191. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3192. if (this.canInsertSemicolon()) this.unexpected();
  3193. if (!this.match(types$1.arrow)) this.unexpected();
  3194. return typeNode;
  3195. });
  3196. if (result.thrown) return null;
  3197. if (result.error) this.state = result.failState;
  3198. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  3199. }
  3200. return super.parseArrow(node);
  3201. }
  3202. shouldParseArrow() {
  3203. return this.match(types$1.colon) || super.shouldParseArrow();
  3204. }
  3205. setArrowFunctionParameters(node, params) {
  3206. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3207. node.params = params;
  3208. } else {
  3209. super.setArrowFunctionParameters(node, params);
  3210. }
  3211. }
  3212. checkParams(node, allowDuplicates, isArrowFunction) {
  3213. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3214. return;
  3215. }
  3216. for (let i = 0; i < node.params.length; i++) {
  3217. if (this.isThisParam(node.params[i]) && i > 0) {
  3218. this.raise(node.params[i].start, FlowErrors.ThisParamMustBeFirst);
  3219. }
  3220. }
  3221. return super.checkParams(...arguments);
  3222. }
  3223. parseParenAndDistinguishExpression(canBeArrow) {
  3224. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  3225. }
  3226. parseSubscripts(base, startPos, startLoc, noCalls) {
  3227. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  3228. this.next();
  3229. const node = this.startNodeAt(startPos, startLoc);
  3230. node.callee = base;
  3231. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3232. base = this.finishNode(node, "CallExpression");
  3233. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  3234. const state = this.state.clone();
  3235. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  3236. if (!arrow.error && !arrow.aborted) return arrow.node;
  3237. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  3238. if (result.node && !result.error) return result.node;
  3239. if (arrow.node) {
  3240. this.state = arrow.failState;
  3241. return arrow.node;
  3242. }
  3243. if (result.node) {
  3244. this.state = result.failState;
  3245. return result.node;
  3246. }
  3247. throw arrow.error || result.error;
  3248. }
  3249. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  3250. }
  3251. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  3252. if (this.match(types$1.questionDot) && this.isLookaheadToken_lt()) {
  3253. subscriptState.optionalChainMember = true;
  3254. if (noCalls) {
  3255. subscriptState.stop = true;
  3256. return base;
  3257. }
  3258. this.next();
  3259. const node = this.startNodeAt(startPos, startLoc);
  3260. node.callee = base;
  3261. node.typeArguments = this.flowParseTypeParameterInstantiation();
  3262. this.expect(types$1.parenL);
  3263. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3264. node.optional = true;
  3265. return this.finishCallExpression(node, true);
  3266. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  3267. const node = this.startNodeAt(startPos, startLoc);
  3268. node.callee = base;
  3269. const result = this.tryParse(() => {
  3270. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  3271. this.expect(types$1.parenL);
  3272. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3273. if (subscriptState.optionalChainMember) node.optional = false;
  3274. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  3275. });
  3276. if (result.node) {
  3277. if (result.error) this.state = result.failState;
  3278. return result.node;
  3279. }
  3280. }
  3281. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  3282. }
  3283. parseNewArguments(node) {
  3284. let targs = null;
  3285. if (this.shouldParseTypes() && this.isRelational("<")) {
  3286. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  3287. }
  3288. node.typeArguments = targs;
  3289. super.parseNewArguments(node);
  3290. }
  3291. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  3292. const node = this.startNodeAt(startPos, startLoc);
  3293. this.parseFunctionParams(node);
  3294. if (!this.parseArrow(node)) return;
  3295. return this.parseArrowExpression(node, undefined, true);
  3296. }
  3297. readToken_mult_modulo(code) {
  3298. const next = this.input.charCodeAt(this.state.pos + 1);
  3299. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  3300. this.state.hasFlowComment = false;
  3301. this.state.pos += 2;
  3302. this.nextToken();
  3303. return;
  3304. }
  3305. super.readToken_mult_modulo(code);
  3306. }
  3307. readToken_pipe_amp(code) {
  3308. const next = this.input.charCodeAt(this.state.pos + 1);
  3309. if (code === 124 && next === 125) {
  3310. this.finishOp(types$1.braceBarR, 2);
  3311. return;
  3312. }
  3313. super.readToken_pipe_amp(code);
  3314. }
  3315. parseTopLevel(file, program) {
  3316. const fileNode = super.parseTopLevel(file, program);
  3317. if (this.state.hasFlowComment) {
  3318. this.raise(this.state.pos, FlowErrors.UnterminatedFlowComment);
  3319. }
  3320. return fileNode;
  3321. }
  3322. skipBlockComment() {
  3323. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  3324. if (this.state.hasFlowComment) {
  3325. this.unexpected(null, FlowErrors.NestedFlowComment);
  3326. }
  3327. this.hasFlowCommentCompletion();
  3328. this.state.pos += this.skipFlowComment();
  3329. this.state.hasFlowComment = true;
  3330. return;
  3331. }
  3332. if (this.state.hasFlowComment) {
  3333. const end = this.input.indexOf("*-/", this.state.pos += 2);
  3334. if (end === -1) {
  3335. throw this.raise(this.state.pos - 2, ErrorMessages.UnterminatedComment);
  3336. }
  3337. this.state.pos = end + 3;
  3338. return;
  3339. }
  3340. super.skipBlockComment();
  3341. }
  3342. skipFlowComment() {
  3343. const {
  3344. pos
  3345. } = this.state;
  3346. let shiftToFirstNonWhiteSpace = 2;
  3347. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  3348. shiftToFirstNonWhiteSpace++;
  3349. }
  3350. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  3351. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  3352. if (ch2 === 58 && ch3 === 58) {
  3353. return shiftToFirstNonWhiteSpace + 2;
  3354. }
  3355. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  3356. return shiftToFirstNonWhiteSpace + 12;
  3357. }
  3358. if (ch2 === 58 && ch3 !== 58) {
  3359. return shiftToFirstNonWhiteSpace;
  3360. }
  3361. return false;
  3362. }
  3363. hasFlowCommentCompletion() {
  3364. const end = this.input.indexOf("*/", this.state.pos);
  3365. if (end === -1) {
  3366. throw this.raise(this.state.pos, ErrorMessages.UnterminatedComment);
  3367. }
  3368. }
  3369. flowEnumErrorBooleanMemberNotInitialized(pos, {
  3370. enumName,
  3371. memberName
  3372. }) {
  3373. this.raise(pos, FlowErrors.EnumBooleanMemberNotInitialized, memberName, enumName);
  3374. }
  3375. flowEnumErrorInvalidMemberName(pos, {
  3376. enumName,
  3377. memberName
  3378. }) {
  3379. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  3380. this.raise(pos, FlowErrors.EnumInvalidMemberName, memberName, suggestion, enumName);
  3381. }
  3382. flowEnumErrorDuplicateMemberName(pos, {
  3383. enumName,
  3384. memberName
  3385. }) {
  3386. this.raise(pos, FlowErrors.EnumDuplicateMemberName, memberName, enumName);
  3387. }
  3388. flowEnumErrorInconsistentMemberValues(pos, {
  3389. enumName
  3390. }) {
  3391. this.raise(pos, FlowErrors.EnumInconsistentMemberValues, enumName);
  3392. }
  3393. flowEnumErrorInvalidExplicitType(pos, {
  3394. enumName,
  3395. suppliedType
  3396. }) {
  3397. return this.raise(pos, suppliedType === null ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied : FlowErrors.EnumInvalidExplicitType, enumName, suppliedType);
  3398. }
  3399. flowEnumErrorInvalidMemberInitializer(pos, {
  3400. enumName,
  3401. explicitType,
  3402. memberName
  3403. }) {
  3404. let message = null;
  3405. switch (explicitType) {
  3406. case "boolean":
  3407. case "number":
  3408. case "string":
  3409. message = FlowErrors.EnumInvalidMemberInitializerPrimaryType;
  3410. break;
  3411. case "symbol":
  3412. message = FlowErrors.EnumInvalidMemberInitializerSymbolType;
  3413. break;
  3414. default:
  3415. message = FlowErrors.EnumInvalidMemberInitializerUnknownType;
  3416. }
  3417. return this.raise(pos, message, enumName, memberName, explicitType);
  3418. }
  3419. flowEnumErrorNumberMemberNotInitialized(pos, {
  3420. enumName,
  3421. memberName
  3422. }) {
  3423. this.raise(pos, FlowErrors.EnumNumberMemberNotInitialized, enumName, memberName);
  3424. }
  3425. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  3426. enumName
  3427. }) {
  3428. this.raise(pos, FlowErrors.EnumStringMemberInconsistentlyInitailized, enumName);
  3429. }
  3430. flowEnumMemberInit() {
  3431. const startPos = this.state.start;
  3432. const endOfInit = () => this.match(types$1.comma) || this.match(types$1.braceR);
  3433. switch (this.state.type) {
  3434. case types$1.num:
  3435. {
  3436. const literal = this.parseLiteral(this.state.value, "NumericLiteral");
  3437. if (endOfInit()) {
  3438. return {
  3439. type: "number",
  3440. pos: literal.start,
  3441. value: literal
  3442. };
  3443. }
  3444. return {
  3445. type: "invalid",
  3446. pos: startPos
  3447. };
  3448. }
  3449. case types$1.string:
  3450. {
  3451. const literal = this.parseLiteral(this.state.value, "StringLiteral");
  3452. if (endOfInit()) {
  3453. return {
  3454. type: "string",
  3455. pos: literal.start,
  3456. value: literal
  3457. };
  3458. }
  3459. return {
  3460. type: "invalid",
  3461. pos: startPos
  3462. };
  3463. }
  3464. case types$1._true:
  3465. case types$1._false:
  3466. {
  3467. const literal = this.parseBooleanLiteral();
  3468. if (endOfInit()) {
  3469. return {
  3470. type: "boolean",
  3471. pos: literal.start,
  3472. value: literal
  3473. };
  3474. }
  3475. return {
  3476. type: "invalid",
  3477. pos: startPos
  3478. };
  3479. }
  3480. default:
  3481. return {
  3482. type: "invalid",
  3483. pos: startPos
  3484. };
  3485. }
  3486. }
  3487. flowEnumMemberRaw() {
  3488. const pos = this.state.start;
  3489. const id = this.parseIdentifier(true);
  3490. const init = this.eat(types$1.eq) ? this.flowEnumMemberInit() : {
  3491. type: "none",
  3492. pos
  3493. };
  3494. return {
  3495. id,
  3496. init
  3497. };
  3498. }
  3499. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  3500. const {
  3501. explicitType
  3502. } = context;
  3503. if (explicitType === null) {
  3504. return;
  3505. }
  3506. if (explicitType !== expectedType) {
  3507. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  3508. }
  3509. }
  3510. flowEnumMembers({
  3511. enumName,
  3512. explicitType
  3513. }) {
  3514. const seenNames = new Set();
  3515. const members = {
  3516. booleanMembers: [],
  3517. numberMembers: [],
  3518. stringMembers: [],
  3519. defaultedMembers: []
  3520. };
  3521. let hasUnknownMembers = false;
  3522. while (!this.match(types$1.braceR)) {
  3523. if (this.eat(types$1.ellipsis)) {
  3524. hasUnknownMembers = true;
  3525. break;
  3526. }
  3527. const memberNode = this.startNode();
  3528. const {
  3529. id,
  3530. init
  3531. } = this.flowEnumMemberRaw();
  3532. const memberName = id.name;
  3533. if (memberName === "") {
  3534. continue;
  3535. }
  3536. if (/^[a-z]/.test(memberName)) {
  3537. this.flowEnumErrorInvalidMemberName(id.start, {
  3538. enumName,
  3539. memberName
  3540. });
  3541. }
  3542. if (seenNames.has(memberName)) {
  3543. this.flowEnumErrorDuplicateMemberName(id.start, {
  3544. enumName,
  3545. memberName
  3546. });
  3547. }
  3548. seenNames.add(memberName);
  3549. const context = {
  3550. enumName,
  3551. explicitType,
  3552. memberName
  3553. };
  3554. memberNode.id = id;
  3555. switch (init.type) {
  3556. case "boolean":
  3557. {
  3558. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  3559. memberNode.init = init.value;
  3560. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  3561. break;
  3562. }
  3563. case "number":
  3564. {
  3565. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  3566. memberNode.init = init.value;
  3567. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  3568. break;
  3569. }
  3570. case "string":
  3571. {
  3572. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  3573. memberNode.init = init.value;
  3574. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  3575. break;
  3576. }
  3577. case "invalid":
  3578. {
  3579. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  3580. }
  3581. case "none":
  3582. {
  3583. switch (explicitType) {
  3584. case "boolean":
  3585. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  3586. break;
  3587. case "number":
  3588. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  3589. break;
  3590. default:
  3591. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  3592. }
  3593. }
  3594. }
  3595. if (!this.match(types$1.braceR)) {
  3596. this.expect(types$1.comma);
  3597. }
  3598. }
  3599. return {
  3600. members,
  3601. hasUnknownMembers
  3602. };
  3603. }
  3604. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  3605. enumName
  3606. }) {
  3607. if (initializedMembers.length === 0) {
  3608. return defaultedMembers;
  3609. } else if (defaultedMembers.length === 0) {
  3610. return initializedMembers;
  3611. } else if (defaultedMembers.length > initializedMembers.length) {
  3612. for (const member of initializedMembers) {
  3613. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3614. enumName
  3615. });
  3616. }
  3617. return defaultedMembers;
  3618. } else {
  3619. for (const member of defaultedMembers) {
  3620. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3621. enumName
  3622. });
  3623. }
  3624. return initializedMembers;
  3625. }
  3626. }
  3627. flowEnumParseExplicitType({
  3628. enumName
  3629. }) {
  3630. if (this.eatContextual("of")) {
  3631. if (!this.match(types$1.name)) {
  3632. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3633. enumName,
  3634. suppliedType: null
  3635. });
  3636. }
  3637. const {
  3638. value
  3639. } = this.state;
  3640. this.next();
  3641. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  3642. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3643. enumName,
  3644. suppliedType: value
  3645. });
  3646. }
  3647. return value;
  3648. }
  3649. return null;
  3650. }
  3651. flowEnumBody(node, {
  3652. enumName,
  3653. nameLoc
  3654. }) {
  3655. const explicitType = this.flowEnumParseExplicitType({
  3656. enumName
  3657. });
  3658. this.expect(types$1.braceL);
  3659. const {
  3660. members,
  3661. hasUnknownMembers
  3662. } = this.flowEnumMembers({
  3663. enumName,
  3664. explicitType
  3665. });
  3666. node.hasUnknownMembers = hasUnknownMembers;
  3667. switch (explicitType) {
  3668. case "boolean":
  3669. node.explicitType = true;
  3670. node.members = members.booleanMembers;
  3671. this.expect(types$1.braceR);
  3672. return this.finishNode(node, "EnumBooleanBody");
  3673. case "number":
  3674. node.explicitType = true;
  3675. node.members = members.numberMembers;
  3676. this.expect(types$1.braceR);
  3677. return this.finishNode(node, "EnumNumberBody");
  3678. case "string":
  3679. node.explicitType = true;
  3680. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3681. enumName
  3682. });
  3683. this.expect(types$1.braceR);
  3684. return this.finishNode(node, "EnumStringBody");
  3685. case "symbol":
  3686. node.members = members.defaultedMembers;
  3687. this.expect(types$1.braceR);
  3688. return this.finishNode(node, "EnumSymbolBody");
  3689. default:
  3690. {
  3691. const empty = () => {
  3692. node.members = [];
  3693. this.expect(types$1.braceR);
  3694. return this.finishNode(node, "EnumStringBody");
  3695. };
  3696. node.explicitType = false;
  3697. const boolsLen = members.booleanMembers.length;
  3698. const numsLen = members.numberMembers.length;
  3699. const strsLen = members.stringMembers.length;
  3700. const defaultedLen = members.defaultedMembers.length;
  3701. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  3702. return empty();
  3703. } else if (!boolsLen && !numsLen) {
  3704. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3705. enumName
  3706. });
  3707. this.expect(types$1.braceR);
  3708. return this.finishNode(node, "EnumStringBody");
  3709. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  3710. for (const member of members.defaultedMembers) {
  3711. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  3712. enumName,
  3713. memberName: member.id.name
  3714. });
  3715. }
  3716. node.members = members.booleanMembers;
  3717. this.expect(types$1.braceR);
  3718. return this.finishNode(node, "EnumBooleanBody");
  3719. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  3720. for (const member of members.defaultedMembers) {
  3721. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  3722. enumName,
  3723. memberName: member.id.name
  3724. });
  3725. }
  3726. node.members = members.numberMembers;
  3727. this.expect(types$1.braceR);
  3728. return this.finishNode(node, "EnumNumberBody");
  3729. } else {
  3730. this.flowEnumErrorInconsistentMemberValues(nameLoc, {
  3731. enumName
  3732. });
  3733. return empty();
  3734. }
  3735. }
  3736. }
  3737. }
  3738. flowParseEnumDeclaration(node) {
  3739. const id = this.parseIdentifier();
  3740. node.id = id;
  3741. node.body = this.flowEnumBody(this.startNode(), {
  3742. enumName: id.name,
  3743. nameLoc: id.start
  3744. });
  3745. return this.finishNode(node, "EnumDeclaration");
  3746. }
  3747. updateContext(prevType) {
  3748. if (this.match(types$1.name) && this.state.value === "of" && prevType === types$1.name && this.input.slice(this.state.lastTokStart, this.state.lastTokEnd) === "interface") {
  3749. this.state.exprAllowed = false;
  3750. } else {
  3751. super.updateContext(prevType);
  3752. }
  3753. }
  3754. isLookaheadToken_lt() {
  3755. const next = this.nextTokenStart();
  3756. if (this.input.charCodeAt(next) === 60) {
  3757. const afterNext = this.input.charCodeAt(next + 1);
  3758. return afterNext !== 60 && afterNext !== 61;
  3759. }
  3760. return false;
  3761. }
  3762. maybeUnwrapTypeCastExpression(node) {
  3763. return node.type === "TypeCastExpression" ? node.expression : node;
  3764. }
  3765. });
  3766. const entities = {
  3767. quot: "\u0022",
  3768. amp: "&",
  3769. apos: "\u0027",
  3770. lt: "<",
  3771. gt: ">",
  3772. nbsp: "\u00A0",
  3773. iexcl: "\u00A1",
  3774. cent: "\u00A2",
  3775. pound: "\u00A3",
  3776. curren: "\u00A4",
  3777. yen: "\u00A5",
  3778. brvbar: "\u00A6",
  3779. sect: "\u00A7",
  3780. uml: "\u00A8",
  3781. copy: "\u00A9",
  3782. ordf: "\u00AA",
  3783. laquo: "\u00AB",
  3784. not: "\u00AC",
  3785. shy: "\u00AD",
  3786. reg: "\u00AE",
  3787. macr: "\u00AF",
  3788. deg: "\u00B0",
  3789. plusmn: "\u00B1",
  3790. sup2: "\u00B2",
  3791. sup3: "\u00B3",
  3792. acute: "\u00B4",
  3793. micro: "\u00B5",
  3794. para: "\u00B6",
  3795. middot: "\u00B7",
  3796. cedil: "\u00B8",
  3797. sup1: "\u00B9",
  3798. ordm: "\u00BA",
  3799. raquo: "\u00BB",
  3800. frac14: "\u00BC",
  3801. frac12: "\u00BD",
  3802. frac34: "\u00BE",
  3803. iquest: "\u00BF",
  3804. Agrave: "\u00C0",
  3805. Aacute: "\u00C1",
  3806. Acirc: "\u00C2",
  3807. Atilde: "\u00C3",
  3808. Auml: "\u00C4",
  3809. Aring: "\u00C5",
  3810. AElig: "\u00C6",
  3811. Ccedil: "\u00C7",
  3812. Egrave: "\u00C8",
  3813. Eacute: "\u00C9",
  3814. Ecirc: "\u00CA",
  3815. Euml: "\u00CB",
  3816. Igrave: "\u00CC",
  3817. Iacute: "\u00CD",
  3818. Icirc: "\u00CE",
  3819. Iuml: "\u00CF",
  3820. ETH: "\u00D0",
  3821. Ntilde: "\u00D1",
  3822. Ograve: "\u00D2",
  3823. Oacute: "\u00D3",
  3824. Ocirc: "\u00D4",
  3825. Otilde: "\u00D5",
  3826. Ouml: "\u00D6",
  3827. times: "\u00D7",
  3828. Oslash: "\u00D8",
  3829. Ugrave: "\u00D9",
  3830. Uacute: "\u00DA",
  3831. Ucirc: "\u00DB",
  3832. Uuml: "\u00DC",
  3833. Yacute: "\u00DD",
  3834. THORN: "\u00DE",
  3835. szlig: "\u00DF",
  3836. agrave: "\u00E0",
  3837. aacute: "\u00E1",
  3838. acirc: "\u00E2",
  3839. atilde: "\u00E3",
  3840. auml: "\u00E4",
  3841. aring: "\u00E5",
  3842. aelig: "\u00E6",
  3843. ccedil: "\u00E7",
  3844. egrave: "\u00E8",
  3845. eacute: "\u00E9",
  3846. ecirc: "\u00EA",
  3847. euml: "\u00EB",
  3848. igrave: "\u00EC",
  3849. iacute: "\u00ED",
  3850. icirc: "\u00EE",
  3851. iuml: "\u00EF",
  3852. eth: "\u00F0",
  3853. ntilde: "\u00F1",
  3854. ograve: "\u00F2",
  3855. oacute: "\u00F3",
  3856. ocirc: "\u00F4",
  3857. otilde: "\u00F5",
  3858. ouml: "\u00F6",
  3859. divide: "\u00F7",
  3860. oslash: "\u00F8",
  3861. ugrave: "\u00F9",
  3862. uacute: "\u00FA",
  3863. ucirc: "\u00FB",
  3864. uuml: "\u00FC",
  3865. yacute: "\u00FD",
  3866. thorn: "\u00FE",
  3867. yuml: "\u00FF",
  3868. OElig: "\u0152",
  3869. oelig: "\u0153",
  3870. Scaron: "\u0160",
  3871. scaron: "\u0161",
  3872. Yuml: "\u0178",
  3873. fnof: "\u0192",
  3874. circ: "\u02C6",
  3875. tilde: "\u02DC",
  3876. Alpha: "\u0391",
  3877. Beta: "\u0392",
  3878. Gamma: "\u0393",
  3879. Delta: "\u0394",
  3880. Epsilon: "\u0395",
  3881. Zeta: "\u0396",
  3882. Eta: "\u0397",
  3883. Theta: "\u0398",
  3884. Iota: "\u0399",
  3885. Kappa: "\u039A",
  3886. Lambda: "\u039B",
  3887. Mu: "\u039C",
  3888. Nu: "\u039D",
  3889. Xi: "\u039E",
  3890. Omicron: "\u039F",
  3891. Pi: "\u03A0",
  3892. Rho: "\u03A1",
  3893. Sigma: "\u03A3",
  3894. Tau: "\u03A4",
  3895. Upsilon: "\u03A5",
  3896. Phi: "\u03A6",
  3897. Chi: "\u03A7",
  3898. Psi: "\u03A8",
  3899. Omega: "\u03A9",
  3900. alpha: "\u03B1",
  3901. beta: "\u03B2",
  3902. gamma: "\u03B3",
  3903. delta: "\u03B4",
  3904. epsilon: "\u03B5",
  3905. zeta: "\u03B6",
  3906. eta: "\u03B7",
  3907. theta: "\u03B8",
  3908. iota: "\u03B9",
  3909. kappa: "\u03BA",
  3910. lambda: "\u03BB",
  3911. mu: "\u03BC",
  3912. nu: "\u03BD",
  3913. xi: "\u03BE",
  3914. omicron: "\u03BF",
  3915. pi: "\u03C0",
  3916. rho: "\u03C1",
  3917. sigmaf: "\u03C2",
  3918. sigma: "\u03C3",
  3919. tau: "\u03C4",
  3920. upsilon: "\u03C5",
  3921. phi: "\u03C6",
  3922. chi: "\u03C7",
  3923. psi: "\u03C8",
  3924. omega: "\u03C9",
  3925. thetasym: "\u03D1",
  3926. upsih: "\u03D2",
  3927. piv: "\u03D6",
  3928. ensp: "\u2002",
  3929. emsp: "\u2003",
  3930. thinsp: "\u2009",
  3931. zwnj: "\u200C",
  3932. zwj: "\u200D",
  3933. lrm: "\u200E",
  3934. rlm: "\u200F",
  3935. ndash: "\u2013",
  3936. mdash: "\u2014",
  3937. lsquo: "\u2018",
  3938. rsquo: "\u2019",
  3939. sbquo: "\u201A",
  3940. ldquo: "\u201C",
  3941. rdquo: "\u201D",
  3942. bdquo: "\u201E",
  3943. dagger: "\u2020",
  3944. Dagger: "\u2021",
  3945. bull: "\u2022",
  3946. hellip: "\u2026",
  3947. permil: "\u2030",
  3948. prime: "\u2032",
  3949. Prime: "\u2033",
  3950. lsaquo: "\u2039",
  3951. rsaquo: "\u203A",
  3952. oline: "\u203E",
  3953. frasl: "\u2044",
  3954. euro: "\u20AC",
  3955. image: "\u2111",
  3956. weierp: "\u2118",
  3957. real: "\u211C",
  3958. trade: "\u2122",
  3959. alefsym: "\u2135",
  3960. larr: "\u2190",
  3961. uarr: "\u2191",
  3962. rarr: "\u2192",
  3963. darr: "\u2193",
  3964. harr: "\u2194",
  3965. crarr: "\u21B5",
  3966. lArr: "\u21D0",
  3967. uArr: "\u21D1",
  3968. rArr: "\u21D2",
  3969. dArr: "\u21D3",
  3970. hArr: "\u21D4",
  3971. forall: "\u2200",
  3972. part: "\u2202",
  3973. exist: "\u2203",
  3974. empty: "\u2205",
  3975. nabla: "\u2207",
  3976. isin: "\u2208",
  3977. notin: "\u2209",
  3978. ni: "\u220B",
  3979. prod: "\u220F",
  3980. sum: "\u2211",
  3981. minus: "\u2212",
  3982. lowast: "\u2217",
  3983. radic: "\u221A",
  3984. prop: "\u221D",
  3985. infin: "\u221E",
  3986. ang: "\u2220",
  3987. and: "\u2227",
  3988. or: "\u2228",
  3989. cap: "\u2229",
  3990. cup: "\u222A",
  3991. int: "\u222B",
  3992. there4: "\u2234",
  3993. sim: "\u223C",
  3994. cong: "\u2245",
  3995. asymp: "\u2248",
  3996. ne: "\u2260",
  3997. equiv: "\u2261",
  3998. le: "\u2264",
  3999. ge: "\u2265",
  4000. sub: "\u2282",
  4001. sup: "\u2283",
  4002. nsub: "\u2284",
  4003. sube: "\u2286",
  4004. supe: "\u2287",
  4005. oplus: "\u2295",
  4006. otimes: "\u2297",
  4007. perp: "\u22A5",
  4008. sdot: "\u22C5",
  4009. lceil: "\u2308",
  4010. rceil: "\u2309",
  4011. lfloor: "\u230A",
  4012. rfloor: "\u230B",
  4013. lang: "\u2329",
  4014. rang: "\u232A",
  4015. loz: "\u25CA",
  4016. spades: "\u2660",
  4017. clubs: "\u2663",
  4018. hearts: "\u2665",
  4019. diams: "\u2666"
  4020. };
  4021. const HEX_NUMBER = /^[\da-fA-F]+$/;
  4022. const DECIMAL_NUMBER = /^\d+$/;
  4023. const JsxErrors = makeErrorTemplates({
  4024. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  4025. MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>.",
  4026. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  4027. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  4028. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  4029. UnterminatedJsxContent: "Unterminated JSX contents.",
  4030. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  4031. }, ErrorCodes.SyntaxError);
  4032. types.j_oTag = new TokContext("<tag", false);
  4033. types.j_cTag = new TokContext("</tag", false);
  4034. types.j_expr = new TokContext("<tag>...</tag>", true, true);
  4035. types$1.jsxName = new TokenType("jsxName");
  4036. types$1.jsxText = new TokenType("jsxText", {
  4037. beforeExpr: true
  4038. });
  4039. types$1.jsxTagStart = new TokenType("jsxTagStart", {
  4040. startsExpr: true
  4041. });
  4042. types$1.jsxTagEnd = new TokenType("jsxTagEnd");
  4043. types$1.jsxTagStart.updateContext = function () {
  4044. this.state.context.push(types.j_expr);
  4045. this.state.context.push(types.j_oTag);
  4046. this.state.exprAllowed = false;
  4047. };
  4048. types$1.jsxTagEnd.updateContext = function (prevType) {
  4049. const out = this.state.context.pop();
  4050. if (out === types.j_oTag && prevType === types$1.slash || out === types.j_cTag) {
  4051. this.state.context.pop();
  4052. this.state.exprAllowed = this.curContext() === types.j_expr;
  4053. } else {
  4054. this.state.exprAllowed = true;
  4055. }
  4056. };
  4057. function isFragment(object) {
  4058. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  4059. }
  4060. function getQualifiedJSXName(object) {
  4061. if (object.type === "JSXIdentifier") {
  4062. return object.name;
  4063. }
  4064. if (object.type === "JSXNamespacedName") {
  4065. return object.namespace.name + ":" + object.name.name;
  4066. }
  4067. if (object.type === "JSXMemberExpression") {
  4068. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  4069. }
  4070. throw new Error("Node had unexpected type: " + object.type);
  4071. }
  4072. var jsx = (superClass => class extends superClass {
  4073. jsxReadToken() {
  4074. let out = "";
  4075. let chunkStart = this.state.pos;
  4076. for (;;) {
  4077. if (this.state.pos >= this.length) {
  4078. throw this.raise(this.state.start, JsxErrors.UnterminatedJsxContent);
  4079. }
  4080. const ch = this.input.charCodeAt(this.state.pos);
  4081. switch (ch) {
  4082. case 60:
  4083. case 123:
  4084. if (this.state.pos === this.state.start) {
  4085. if (ch === 60 && this.state.exprAllowed) {
  4086. ++this.state.pos;
  4087. return this.finishToken(types$1.jsxTagStart);
  4088. }
  4089. return super.getTokenFromCode(ch);
  4090. }
  4091. out += this.input.slice(chunkStart, this.state.pos);
  4092. return this.finishToken(types$1.jsxText, out);
  4093. case 38:
  4094. out += this.input.slice(chunkStart, this.state.pos);
  4095. out += this.jsxReadEntity();
  4096. chunkStart = this.state.pos;
  4097. break;
  4098. case 62:
  4099. case 125:
  4100. default:
  4101. if (isNewLine(ch)) {
  4102. out += this.input.slice(chunkStart, this.state.pos);
  4103. out += this.jsxReadNewLine(true);
  4104. chunkStart = this.state.pos;
  4105. } else {
  4106. ++this.state.pos;
  4107. }
  4108. }
  4109. }
  4110. }
  4111. jsxReadNewLine(normalizeCRLF) {
  4112. const ch = this.input.charCodeAt(this.state.pos);
  4113. let out;
  4114. ++this.state.pos;
  4115. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  4116. ++this.state.pos;
  4117. out = normalizeCRLF ? "\n" : "\r\n";
  4118. } else {
  4119. out = String.fromCharCode(ch);
  4120. }
  4121. ++this.state.curLine;
  4122. this.state.lineStart = this.state.pos;
  4123. return out;
  4124. }
  4125. jsxReadString(quote) {
  4126. let out = "";
  4127. let chunkStart = ++this.state.pos;
  4128. for (;;) {
  4129. if (this.state.pos >= this.length) {
  4130. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  4131. }
  4132. const ch = this.input.charCodeAt(this.state.pos);
  4133. if (ch === quote) break;
  4134. if (ch === 38) {
  4135. out += this.input.slice(chunkStart, this.state.pos);
  4136. out += this.jsxReadEntity();
  4137. chunkStart = this.state.pos;
  4138. } else if (isNewLine(ch)) {
  4139. out += this.input.slice(chunkStart, this.state.pos);
  4140. out += this.jsxReadNewLine(false);
  4141. chunkStart = this.state.pos;
  4142. } else {
  4143. ++this.state.pos;
  4144. }
  4145. }
  4146. out += this.input.slice(chunkStart, this.state.pos++);
  4147. return this.finishToken(types$1.string, out);
  4148. }
  4149. jsxReadEntity() {
  4150. let str = "";
  4151. let count = 0;
  4152. let entity;
  4153. let ch = this.input[this.state.pos];
  4154. const startPos = ++this.state.pos;
  4155. while (this.state.pos < this.length && count++ < 10) {
  4156. ch = this.input[this.state.pos++];
  4157. if (ch === ";") {
  4158. if (str[0] === "#") {
  4159. if (str[1] === "x") {
  4160. str = str.substr(2);
  4161. if (HEX_NUMBER.test(str)) {
  4162. entity = String.fromCodePoint(parseInt(str, 16));
  4163. }
  4164. } else {
  4165. str = str.substr(1);
  4166. if (DECIMAL_NUMBER.test(str)) {
  4167. entity = String.fromCodePoint(parseInt(str, 10));
  4168. }
  4169. }
  4170. } else {
  4171. entity = entities[str];
  4172. }
  4173. break;
  4174. }
  4175. str += ch;
  4176. }
  4177. if (!entity) {
  4178. this.state.pos = startPos;
  4179. return "&";
  4180. }
  4181. return entity;
  4182. }
  4183. jsxReadWord() {
  4184. let ch;
  4185. const start = this.state.pos;
  4186. do {
  4187. ch = this.input.charCodeAt(++this.state.pos);
  4188. } while (isIdentifierChar(ch) || ch === 45);
  4189. return this.finishToken(types$1.jsxName, this.input.slice(start, this.state.pos));
  4190. }
  4191. jsxParseIdentifier() {
  4192. const node = this.startNode();
  4193. if (this.match(types$1.jsxName)) {
  4194. node.name = this.state.value;
  4195. } else if (this.state.type.keyword) {
  4196. node.name = this.state.type.keyword;
  4197. } else {
  4198. this.unexpected();
  4199. }
  4200. this.next();
  4201. return this.finishNode(node, "JSXIdentifier");
  4202. }
  4203. jsxParseNamespacedName() {
  4204. const startPos = this.state.start;
  4205. const startLoc = this.state.startLoc;
  4206. const name = this.jsxParseIdentifier();
  4207. if (!this.eat(types$1.colon)) return name;
  4208. const node = this.startNodeAt(startPos, startLoc);
  4209. node.namespace = name;
  4210. node.name = this.jsxParseIdentifier();
  4211. return this.finishNode(node, "JSXNamespacedName");
  4212. }
  4213. jsxParseElementName() {
  4214. const startPos = this.state.start;
  4215. const startLoc = this.state.startLoc;
  4216. let node = this.jsxParseNamespacedName();
  4217. if (node.type === "JSXNamespacedName") {
  4218. return node;
  4219. }
  4220. while (this.eat(types$1.dot)) {
  4221. const newNode = this.startNodeAt(startPos, startLoc);
  4222. newNode.object = node;
  4223. newNode.property = this.jsxParseIdentifier();
  4224. node = this.finishNode(newNode, "JSXMemberExpression");
  4225. }
  4226. return node;
  4227. }
  4228. jsxParseAttributeValue() {
  4229. let node;
  4230. switch (this.state.type) {
  4231. case types$1.braceL:
  4232. node = this.startNode();
  4233. this.next();
  4234. node = this.jsxParseExpressionContainer(node);
  4235. if (node.expression.type === "JSXEmptyExpression") {
  4236. this.raise(node.start, JsxErrors.AttributeIsEmpty);
  4237. }
  4238. return node;
  4239. case types$1.jsxTagStart:
  4240. case types$1.string:
  4241. return this.parseExprAtom();
  4242. default:
  4243. throw this.raise(this.state.start, JsxErrors.UnsupportedJsxValue);
  4244. }
  4245. }
  4246. jsxParseEmptyExpression() {
  4247. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  4248. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  4249. }
  4250. jsxParseSpreadChild(node) {
  4251. this.next();
  4252. node.expression = this.parseExpression();
  4253. this.expect(types$1.braceR);
  4254. return this.finishNode(node, "JSXSpreadChild");
  4255. }
  4256. jsxParseExpressionContainer(node) {
  4257. if (this.match(types$1.braceR)) {
  4258. node.expression = this.jsxParseEmptyExpression();
  4259. } else {
  4260. const expression = this.parseExpression();
  4261. node.expression = expression;
  4262. }
  4263. this.expect(types$1.braceR);
  4264. return this.finishNode(node, "JSXExpressionContainer");
  4265. }
  4266. jsxParseAttribute() {
  4267. const node = this.startNode();
  4268. if (this.eat(types$1.braceL)) {
  4269. this.expect(types$1.ellipsis);
  4270. node.argument = this.parseMaybeAssignAllowIn();
  4271. this.expect(types$1.braceR);
  4272. return this.finishNode(node, "JSXSpreadAttribute");
  4273. }
  4274. node.name = this.jsxParseNamespacedName();
  4275. node.value = this.eat(types$1.eq) ? this.jsxParseAttributeValue() : null;
  4276. return this.finishNode(node, "JSXAttribute");
  4277. }
  4278. jsxParseOpeningElementAt(startPos, startLoc) {
  4279. const node = this.startNodeAt(startPos, startLoc);
  4280. if (this.match(types$1.jsxTagEnd)) {
  4281. this.expect(types$1.jsxTagEnd);
  4282. return this.finishNode(node, "JSXOpeningFragment");
  4283. }
  4284. node.name = this.jsxParseElementName();
  4285. return this.jsxParseOpeningElementAfterName(node);
  4286. }
  4287. jsxParseOpeningElementAfterName(node) {
  4288. const attributes = [];
  4289. while (!this.match(types$1.slash) && !this.match(types$1.jsxTagEnd)) {
  4290. attributes.push(this.jsxParseAttribute());
  4291. }
  4292. node.attributes = attributes;
  4293. node.selfClosing = this.eat(types$1.slash);
  4294. this.expect(types$1.jsxTagEnd);
  4295. return this.finishNode(node, "JSXOpeningElement");
  4296. }
  4297. jsxParseClosingElementAt(startPos, startLoc) {
  4298. const node = this.startNodeAt(startPos, startLoc);
  4299. if (this.match(types$1.jsxTagEnd)) {
  4300. this.expect(types$1.jsxTagEnd);
  4301. return this.finishNode(node, "JSXClosingFragment");
  4302. }
  4303. node.name = this.jsxParseElementName();
  4304. this.expect(types$1.jsxTagEnd);
  4305. return this.finishNode(node, "JSXClosingElement");
  4306. }
  4307. jsxParseElementAt(startPos, startLoc) {
  4308. const node = this.startNodeAt(startPos, startLoc);
  4309. const children = [];
  4310. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  4311. let closingElement = null;
  4312. if (!openingElement.selfClosing) {
  4313. contents: for (;;) {
  4314. switch (this.state.type) {
  4315. case types$1.jsxTagStart:
  4316. startPos = this.state.start;
  4317. startLoc = this.state.startLoc;
  4318. this.next();
  4319. if (this.eat(types$1.slash)) {
  4320. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  4321. break contents;
  4322. }
  4323. children.push(this.jsxParseElementAt(startPos, startLoc));
  4324. break;
  4325. case types$1.jsxText:
  4326. children.push(this.parseExprAtom());
  4327. break;
  4328. case types$1.braceL:
  4329. {
  4330. const node = this.startNode();
  4331. this.next();
  4332. if (this.match(types$1.ellipsis)) {
  4333. children.push(this.jsxParseSpreadChild(node));
  4334. } else {
  4335. children.push(this.jsxParseExpressionContainer(node));
  4336. }
  4337. break;
  4338. }
  4339. default:
  4340. throw this.unexpected();
  4341. }
  4342. }
  4343. if (isFragment(openingElement) && !isFragment(closingElement)) {
  4344. this.raise(closingElement.start, JsxErrors.MissingClosingTagFragment);
  4345. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  4346. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4347. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  4348. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  4349. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4350. }
  4351. }
  4352. }
  4353. if (isFragment(openingElement)) {
  4354. node.openingFragment = openingElement;
  4355. node.closingFragment = closingElement;
  4356. } else {
  4357. node.openingElement = openingElement;
  4358. node.closingElement = closingElement;
  4359. }
  4360. node.children = children;
  4361. if (this.isRelational("<")) {
  4362. throw this.raise(this.state.start, JsxErrors.UnwrappedAdjacentJSXElements);
  4363. }
  4364. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  4365. }
  4366. jsxParseElement() {
  4367. const startPos = this.state.start;
  4368. const startLoc = this.state.startLoc;
  4369. this.next();
  4370. return this.jsxParseElementAt(startPos, startLoc);
  4371. }
  4372. parseExprAtom(refExpressionErrors) {
  4373. if (this.match(types$1.jsxText)) {
  4374. return this.parseLiteral(this.state.value, "JSXText");
  4375. } else if (this.match(types$1.jsxTagStart)) {
  4376. return this.jsxParseElement();
  4377. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  4378. this.finishToken(types$1.jsxTagStart);
  4379. return this.jsxParseElement();
  4380. } else {
  4381. return super.parseExprAtom(refExpressionErrors);
  4382. }
  4383. }
  4384. getTokenFromCode(code) {
  4385. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  4386. const context = this.curContext();
  4387. if (context === types.j_expr) {
  4388. return this.jsxReadToken();
  4389. }
  4390. if (context === types.j_oTag || context === types.j_cTag) {
  4391. if (isIdentifierStart(code)) {
  4392. return this.jsxReadWord();
  4393. }
  4394. if (code === 62) {
  4395. ++this.state.pos;
  4396. return this.finishToken(types$1.jsxTagEnd);
  4397. }
  4398. if ((code === 34 || code === 39) && context === types.j_oTag) {
  4399. return this.jsxReadString(code);
  4400. }
  4401. }
  4402. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  4403. ++this.state.pos;
  4404. return this.finishToken(types$1.jsxTagStart);
  4405. }
  4406. return super.getTokenFromCode(code);
  4407. }
  4408. updateContext(prevType) {
  4409. if (this.match(types$1.braceL)) {
  4410. const curContext = this.curContext();
  4411. if (curContext === types.j_oTag) {
  4412. this.state.context.push(types.braceExpression);
  4413. } else if (curContext === types.j_expr) {
  4414. this.state.context.push(types.templateQuasi);
  4415. } else {
  4416. super.updateContext(prevType);
  4417. }
  4418. this.state.exprAllowed = true;
  4419. } else if (this.match(types$1.slash) && prevType === types$1.jsxTagStart) {
  4420. this.state.context.length -= 2;
  4421. this.state.context.push(types.j_cTag);
  4422. this.state.exprAllowed = false;
  4423. } else {
  4424. return super.updateContext(prevType);
  4425. }
  4426. }
  4427. });
  4428. class TypeScriptScope extends Scope {
  4429. constructor(...args) {
  4430. super(...args);
  4431. this.types = [];
  4432. this.enums = [];
  4433. this.constEnums = [];
  4434. this.classes = [];
  4435. this.exportOnlyBindings = [];
  4436. }
  4437. }
  4438. class TypeScriptScopeHandler extends ScopeHandler {
  4439. createScope(flags) {
  4440. return new TypeScriptScope(flags);
  4441. }
  4442. declareName(name, bindingType, pos) {
  4443. const scope = this.currentScope();
  4444. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  4445. this.maybeExportDefined(scope, name);
  4446. scope.exportOnlyBindings.push(name);
  4447. return;
  4448. }
  4449. super.declareName(...arguments);
  4450. if (bindingType & BIND_KIND_TYPE) {
  4451. if (!(bindingType & BIND_KIND_VALUE)) {
  4452. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  4453. this.maybeExportDefined(scope, name);
  4454. }
  4455. scope.types.push(name);
  4456. }
  4457. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.push(name);
  4458. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.push(name);
  4459. if (bindingType & BIND_FLAGS_CLASS) scope.classes.push(name);
  4460. }
  4461. isRedeclaredInScope(scope, name, bindingType) {
  4462. if (scope.enums.indexOf(name) > -1) {
  4463. if (bindingType & BIND_FLAGS_TS_ENUM) {
  4464. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  4465. const wasConst = scope.constEnums.indexOf(name) > -1;
  4466. return isConst !== wasConst;
  4467. }
  4468. return true;
  4469. }
  4470. if (bindingType & BIND_FLAGS_CLASS && scope.classes.indexOf(name) > -1) {
  4471. if (scope.lexical.indexOf(name) > -1) {
  4472. return !!(bindingType & BIND_KIND_VALUE);
  4473. } else {
  4474. return false;
  4475. }
  4476. }
  4477. if (bindingType & BIND_KIND_TYPE && scope.types.indexOf(name) > -1) {
  4478. return true;
  4479. }
  4480. return super.isRedeclaredInScope(...arguments);
  4481. }
  4482. checkLocalExport(id) {
  4483. if (this.scopeStack[0].types.indexOf(id.name) === -1 && this.scopeStack[0].exportOnlyBindings.indexOf(id.name) === -1) {
  4484. super.checkLocalExport(id);
  4485. }
  4486. }
  4487. }
  4488. const PARAM = 0b0000,
  4489. PARAM_YIELD = 0b0001,
  4490. PARAM_AWAIT = 0b0010,
  4491. PARAM_RETURN = 0b0100,
  4492. PARAM_IN = 0b1000;
  4493. class ProductionParameterHandler {
  4494. constructor() {
  4495. this.stacks = [];
  4496. }
  4497. enter(flags) {
  4498. this.stacks.push(flags);
  4499. }
  4500. exit() {
  4501. this.stacks.pop();
  4502. }
  4503. currentFlags() {
  4504. return this.stacks[this.stacks.length - 1];
  4505. }
  4506. get hasAwait() {
  4507. return (this.currentFlags() & PARAM_AWAIT) > 0;
  4508. }
  4509. get hasYield() {
  4510. return (this.currentFlags() & PARAM_YIELD) > 0;
  4511. }
  4512. get hasReturn() {
  4513. return (this.currentFlags() & PARAM_RETURN) > 0;
  4514. }
  4515. get hasIn() {
  4516. return (this.currentFlags() & PARAM_IN) > 0;
  4517. }
  4518. }
  4519. function functionFlags(isAsync, isGenerator) {
  4520. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  4521. }
  4522. function nonNull(x) {
  4523. if (x == null) {
  4524. throw new Error(`Unexpected ${x} value.`);
  4525. }
  4526. return x;
  4527. }
  4528. function assert(x) {
  4529. if (!x) {
  4530. throw new Error("Assert fail");
  4531. }
  4532. }
  4533. const TSErrors = makeErrorTemplates({
  4534. AbstractMethodHasImplementation: "Method '%0' cannot have an implementation because it is marked abstract.",
  4535. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  4536. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  4537. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  4538. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  4539. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  4540. DeclareAccessor: "'declare' is not allowed in %0ters.",
  4541. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  4542. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  4543. DuplicateAccessibilityModifier: "Accessibility modifier already seen.",
  4544. DuplicateModifier: "Duplicate modifier: '%0'.",
  4545. EmptyHeritageClauseType: "'%0' list cannot be empty.",
  4546. EmptyTypeArguments: "Type argument list cannot be empty.",
  4547. EmptyTypeParameters: "Type parameter list cannot be empty.",
  4548. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  4549. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  4550. IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.",
  4551. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  4552. IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0').",
  4553. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  4554. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  4555. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  4556. InvalidModifierOnTypeMember: "'%0' modifier cannot appear on a type member.",
  4557. InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
  4558. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  4559. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  4560. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  4561. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  4562. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  4563. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  4564. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  4565. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  4566. PrivateElementHasAccessibility: "Private elements cannot have an accessibility modifier ('%0').",
  4567. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  4568. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  4569. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  4570. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  4571. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  4572. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  4573. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  4574. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  4575. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  4576. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  4577. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  4578. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  4579. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  4580. UnsupportedSignatureParameterKind: "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0."
  4581. }, ErrorCodes.SyntaxError);
  4582. function keywordTypeFromName(value) {
  4583. switch (value) {
  4584. case "any":
  4585. return "TSAnyKeyword";
  4586. case "boolean":
  4587. return "TSBooleanKeyword";
  4588. case "bigint":
  4589. return "TSBigIntKeyword";
  4590. case "never":
  4591. return "TSNeverKeyword";
  4592. case "number":
  4593. return "TSNumberKeyword";
  4594. case "object":
  4595. return "TSObjectKeyword";
  4596. case "string":
  4597. return "TSStringKeyword";
  4598. case "symbol":
  4599. return "TSSymbolKeyword";
  4600. case "undefined":
  4601. return "TSUndefinedKeyword";
  4602. case "unknown":
  4603. return "TSUnknownKeyword";
  4604. default:
  4605. return undefined;
  4606. }
  4607. }
  4608. function tsIsAccessModifier(modifier) {
  4609. return modifier === "private" || modifier === "public" || modifier === "protected";
  4610. }
  4611. var typescript = (superClass => class extends superClass {
  4612. getScopeHandler() {
  4613. return TypeScriptScopeHandler;
  4614. }
  4615. tsIsIdentifier() {
  4616. return this.match(types$1.name);
  4617. }
  4618. tsTokenCanFollowModifier() {
  4619. return (this.match(types$1.bracketL) || this.match(types$1.braceL) || this.match(types$1.star) || this.match(types$1.ellipsis) || this.match(types$1.privateName) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  4620. }
  4621. tsNextTokenCanFollowModifier() {
  4622. this.next();
  4623. return this.tsTokenCanFollowModifier();
  4624. }
  4625. tsParseModifier(allowedModifiers) {
  4626. if (!this.match(types$1.name)) {
  4627. return undefined;
  4628. }
  4629. const modifier = this.state.value;
  4630. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  4631. return modifier;
  4632. }
  4633. return undefined;
  4634. }
  4635. tsParseModifiers(modified, allowedModifiers, disallowedModifiers, errorTemplate) {
  4636. const enforceOrder = (pos, modifier, before, after) => {
  4637. if (modifier === before && modified[after]) {
  4638. this.raise(pos, TSErrors.InvalidModifiersOrder, before, after);
  4639. }
  4640. };
  4641. const incompatible = (pos, modifier, mod1, mod2) => {
  4642. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  4643. this.raise(pos, TSErrors.IncompatibleModifiers, mod1, mod2);
  4644. }
  4645. };
  4646. for (;;) {
  4647. const startPos = this.state.start;
  4648. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []));
  4649. if (!modifier) break;
  4650. if (tsIsAccessModifier(modifier)) {
  4651. if (modified.accessibility) {
  4652. this.raise(startPos, TSErrors.DuplicateAccessibilityModifier);
  4653. } else {
  4654. enforceOrder(startPos, modifier, modifier, "override");
  4655. enforceOrder(startPos, modifier, modifier, "static");
  4656. modified.accessibility = modifier;
  4657. }
  4658. } else {
  4659. if (Object.hasOwnProperty.call(modified, modifier)) {
  4660. this.raise(startPos, TSErrors.DuplicateModifier, modifier);
  4661. } else {
  4662. enforceOrder(startPos, modifier, "static", "readonly");
  4663. enforceOrder(startPos, modifier, "static", "override");
  4664. enforceOrder(startPos, modifier, "override", "readonly");
  4665. enforceOrder(startPos, modifier, "abstract", "override");
  4666. incompatible(startPos, modifier, "declare", "override");
  4667. incompatible(startPos, modifier, "static", "abstract");
  4668. }
  4669. modified[modifier] = true;
  4670. }
  4671. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  4672. this.raise(startPos, errorTemplate, modifier);
  4673. }
  4674. }
  4675. }
  4676. tsIsListTerminator(kind) {
  4677. switch (kind) {
  4678. case "EnumMembers":
  4679. case "TypeMembers":
  4680. return this.match(types$1.braceR);
  4681. case "HeritageClauseElement":
  4682. return this.match(types$1.braceL);
  4683. case "TupleElementTypes":
  4684. return this.match(types$1.bracketR);
  4685. case "TypeParametersOrArguments":
  4686. return this.isRelational(">");
  4687. }
  4688. throw new Error("Unreachable");
  4689. }
  4690. tsParseList(kind, parseElement) {
  4691. const result = [];
  4692. while (!this.tsIsListTerminator(kind)) {
  4693. result.push(parseElement());
  4694. }
  4695. return result;
  4696. }
  4697. tsParseDelimitedList(kind, parseElement) {
  4698. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  4699. }
  4700. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  4701. const result = [];
  4702. for (;;) {
  4703. if (this.tsIsListTerminator(kind)) {
  4704. break;
  4705. }
  4706. const element = parseElement();
  4707. if (element == null) {
  4708. return undefined;
  4709. }
  4710. result.push(element);
  4711. if (this.eat(types$1.comma)) {
  4712. continue;
  4713. }
  4714. if (this.tsIsListTerminator(kind)) {
  4715. break;
  4716. }
  4717. if (expectSuccess) {
  4718. this.expect(types$1.comma);
  4719. }
  4720. return undefined;
  4721. }
  4722. return result;
  4723. }
  4724. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  4725. if (!skipFirstToken) {
  4726. if (bracket) {
  4727. this.expect(types$1.bracketL);
  4728. } else {
  4729. this.expectRelational("<");
  4730. }
  4731. }
  4732. const result = this.tsParseDelimitedList(kind, parseElement);
  4733. if (bracket) {
  4734. this.expect(types$1.bracketR);
  4735. } else {
  4736. this.expectRelational(">");
  4737. }
  4738. return result;
  4739. }
  4740. tsParseImportType() {
  4741. const node = this.startNode();
  4742. this.expect(types$1._import);
  4743. this.expect(types$1.parenL);
  4744. if (!this.match(types$1.string)) {
  4745. this.raise(this.state.start, TSErrors.UnsupportedImportTypeArgument);
  4746. }
  4747. node.argument = this.parseExprAtom();
  4748. this.expect(types$1.parenR);
  4749. if (this.eat(types$1.dot)) {
  4750. node.qualifier = this.tsParseEntityName(true);
  4751. }
  4752. if (this.isRelational("<")) {
  4753. node.typeParameters = this.tsParseTypeArguments();
  4754. }
  4755. return this.finishNode(node, "TSImportType");
  4756. }
  4757. tsParseEntityName(allowReservedWords) {
  4758. let entity = this.parseIdentifier();
  4759. while (this.eat(types$1.dot)) {
  4760. const node = this.startNodeAtNode(entity);
  4761. node.left = entity;
  4762. node.right = this.parseIdentifier(allowReservedWords);
  4763. entity = this.finishNode(node, "TSQualifiedName");
  4764. }
  4765. return entity;
  4766. }
  4767. tsParseTypeReference() {
  4768. const node = this.startNode();
  4769. node.typeName = this.tsParseEntityName(false);
  4770. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  4771. node.typeParameters = this.tsParseTypeArguments();
  4772. }
  4773. return this.finishNode(node, "TSTypeReference");
  4774. }
  4775. tsParseThisTypePredicate(lhs) {
  4776. this.next();
  4777. const node = this.startNodeAtNode(lhs);
  4778. node.parameterName = lhs;
  4779. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  4780. node.asserts = false;
  4781. return this.finishNode(node, "TSTypePredicate");
  4782. }
  4783. tsParseThisTypeNode() {
  4784. const node = this.startNode();
  4785. this.next();
  4786. return this.finishNode(node, "TSThisType");
  4787. }
  4788. tsParseTypeQuery() {
  4789. const node = this.startNode();
  4790. this.expect(types$1._typeof);
  4791. if (this.match(types$1._import)) {
  4792. node.exprName = this.tsParseImportType();
  4793. } else {
  4794. node.exprName = this.tsParseEntityName(true);
  4795. }
  4796. return this.finishNode(node, "TSTypeQuery");
  4797. }
  4798. tsParseTypeParameter() {
  4799. const node = this.startNode();
  4800. node.name = this.parseIdentifierName(node.start);
  4801. node.constraint = this.tsEatThenParseType(types$1._extends);
  4802. node.default = this.tsEatThenParseType(types$1.eq);
  4803. return this.finishNode(node, "TSTypeParameter");
  4804. }
  4805. tsTryParseTypeParameters() {
  4806. if (this.isRelational("<")) {
  4807. return this.tsParseTypeParameters();
  4808. }
  4809. }
  4810. tsParseTypeParameters() {
  4811. const node = this.startNode();
  4812. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  4813. this.next();
  4814. } else {
  4815. this.unexpected();
  4816. }
  4817. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  4818. if (node.params.length === 0) {
  4819. this.raise(node.start, TSErrors.EmptyTypeParameters);
  4820. }
  4821. return this.finishNode(node, "TSTypeParameterDeclaration");
  4822. }
  4823. tsTryNextParseConstantContext() {
  4824. if (this.lookahead().type === types$1._const) {
  4825. this.next();
  4826. return this.tsParseTypeReference();
  4827. }
  4828. return null;
  4829. }
  4830. tsFillSignature(returnToken, signature) {
  4831. const returnTokenRequired = returnToken === types$1.arrow;
  4832. signature.typeParameters = this.tsTryParseTypeParameters();
  4833. this.expect(types$1.parenL);
  4834. signature.parameters = this.tsParseBindingListForSignature();
  4835. if (returnTokenRequired) {
  4836. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4837. } else if (this.match(returnToken)) {
  4838. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4839. }
  4840. }
  4841. tsParseBindingListForSignature() {
  4842. return this.parseBindingList(types$1.parenR, 41).map(pattern => {
  4843. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  4844. this.raise(pattern.start, TSErrors.UnsupportedSignatureParameterKind, pattern.type);
  4845. }
  4846. return pattern;
  4847. });
  4848. }
  4849. tsParseTypeMemberSemicolon() {
  4850. if (!this.eat(types$1.comma) && !this.isLineTerminator()) {
  4851. this.expect(types$1.semi);
  4852. }
  4853. }
  4854. tsParseSignatureMember(kind, node) {
  4855. this.tsFillSignature(types$1.colon, node);
  4856. this.tsParseTypeMemberSemicolon();
  4857. return this.finishNode(node, kind);
  4858. }
  4859. tsIsUnambiguouslyIndexSignature() {
  4860. this.next();
  4861. return this.eat(types$1.name) && this.match(types$1.colon);
  4862. }
  4863. tsTryParseIndexSignature(node) {
  4864. if (!(this.match(types$1.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  4865. return undefined;
  4866. }
  4867. this.expect(types$1.bracketL);
  4868. const id = this.parseIdentifier();
  4869. id.typeAnnotation = this.tsParseTypeAnnotation();
  4870. this.resetEndLocation(id);
  4871. this.expect(types$1.bracketR);
  4872. node.parameters = [id];
  4873. const type = this.tsTryParseTypeAnnotation();
  4874. if (type) node.typeAnnotation = type;
  4875. this.tsParseTypeMemberSemicolon();
  4876. return this.finishNode(node, "TSIndexSignature");
  4877. }
  4878. tsParsePropertyOrMethodSignature(node, readonly) {
  4879. if (this.eat(types$1.question)) node.optional = true;
  4880. const nodeAny = node;
  4881. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4882. if (readonly) {
  4883. this.raise(node.start, TSErrors.ReadonlyForMethodSignature);
  4884. }
  4885. const method = nodeAny;
  4886. if (method.kind && this.isRelational("<")) {
  4887. this.raise(this.state.pos, TSErrors.AccesorCannotHaveTypeParameters);
  4888. }
  4889. this.tsFillSignature(types$1.colon, method);
  4890. this.tsParseTypeMemberSemicolon();
  4891. if (method.kind === "get") {
  4892. if (method.parameters.length > 0) {
  4893. this.raise(this.state.pos, ErrorMessages.BadGetterArity);
  4894. if (this.isThisParam(method.parameters[0])) {
  4895. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4896. }
  4897. }
  4898. } else if (method.kind === "set") {
  4899. if (method.parameters.length !== 1) {
  4900. this.raise(this.state.pos, ErrorMessages.BadSetterArity);
  4901. } else {
  4902. const firstParameter = method.parameters[0];
  4903. if (this.isThisParam(firstParameter)) {
  4904. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4905. }
  4906. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  4907. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveOptionalParameter);
  4908. }
  4909. if (firstParameter.type === "RestElement") {
  4910. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveRestParameter);
  4911. }
  4912. }
  4913. if (method.typeAnnotation) {
  4914. this.raise(method.typeAnnotation.start, TSErrors.SetAccesorCannotHaveReturnType);
  4915. }
  4916. } else {
  4917. method.kind = "method";
  4918. }
  4919. return this.finishNode(method, "TSMethodSignature");
  4920. } else {
  4921. const property = nodeAny;
  4922. if (readonly) property.readonly = true;
  4923. const type = this.tsTryParseTypeAnnotation();
  4924. if (type) property.typeAnnotation = type;
  4925. this.tsParseTypeMemberSemicolon();
  4926. return this.finishNode(property, "TSPropertySignature");
  4927. }
  4928. }
  4929. tsParseTypeMember() {
  4930. const node = this.startNode();
  4931. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4932. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  4933. }
  4934. if (this.match(types$1._new)) {
  4935. const id = this.startNode();
  4936. this.next();
  4937. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4938. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  4939. } else {
  4940. node.key = this.createIdentifier(id, "new");
  4941. return this.tsParsePropertyOrMethodSignature(node, false);
  4942. }
  4943. }
  4944. this.tsParseModifiers(node, ["readonly"], ["declare", "abstract", "private", "protected", "public", "static", "override"], TSErrors.InvalidModifierOnTypeMember);
  4945. const idx = this.tsTryParseIndexSignature(node);
  4946. if (idx) {
  4947. return idx;
  4948. }
  4949. this.parsePropertyName(node, false);
  4950. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  4951. node.kind = node.key.name;
  4952. this.parsePropertyName(node, false);
  4953. }
  4954. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  4955. }
  4956. tsParseTypeLiteral() {
  4957. const node = this.startNode();
  4958. node.members = this.tsParseObjectTypeMembers();
  4959. return this.finishNode(node, "TSTypeLiteral");
  4960. }
  4961. tsParseObjectTypeMembers() {
  4962. this.expect(types$1.braceL);
  4963. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  4964. this.expect(types$1.braceR);
  4965. return members;
  4966. }
  4967. tsIsStartOfMappedType() {
  4968. this.next();
  4969. if (this.eat(types$1.plusMin)) {
  4970. return this.isContextual("readonly");
  4971. }
  4972. if (this.isContextual("readonly")) {
  4973. this.next();
  4974. }
  4975. if (!this.match(types$1.bracketL)) {
  4976. return false;
  4977. }
  4978. this.next();
  4979. if (!this.tsIsIdentifier()) {
  4980. return false;
  4981. }
  4982. this.next();
  4983. return this.match(types$1._in);
  4984. }
  4985. tsParseMappedTypeParameter() {
  4986. const node = this.startNode();
  4987. node.name = this.parseIdentifierName(node.start);
  4988. node.constraint = this.tsExpectThenParseType(types$1._in);
  4989. return this.finishNode(node, "TSTypeParameter");
  4990. }
  4991. tsParseMappedType() {
  4992. const node = this.startNode();
  4993. this.expect(types$1.braceL);
  4994. if (this.match(types$1.plusMin)) {
  4995. node.readonly = this.state.value;
  4996. this.next();
  4997. this.expectContextual("readonly");
  4998. } else if (this.eatContextual("readonly")) {
  4999. node.readonly = true;
  5000. }
  5001. this.expect(types$1.bracketL);
  5002. node.typeParameter = this.tsParseMappedTypeParameter();
  5003. node.nameType = this.eatContextual("as") ? this.tsParseType() : null;
  5004. this.expect(types$1.bracketR);
  5005. if (this.match(types$1.plusMin)) {
  5006. node.optional = this.state.value;
  5007. this.next();
  5008. this.expect(types$1.question);
  5009. } else if (this.eat(types$1.question)) {
  5010. node.optional = true;
  5011. }
  5012. node.typeAnnotation = this.tsTryParseType();
  5013. this.semicolon();
  5014. this.expect(types$1.braceR);
  5015. return this.finishNode(node, "TSMappedType");
  5016. }
  5017. tsParseTupleType() {
  5018. const node = this.startNode();
  5019. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  5020. let seenOptionalElement = false;
  5021. let labeledElements = null;
  5022. node.elementTypes.forEach(elementNode => {
  5023. var _labeledElements;
  5024. let {
  5025. type
  5026. } = elementNode;
  5027. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  5028. this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired);
  5029. }
  5030. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  5031. if (type === "TSRestType") {
  5032. elementNode = elementNode.typeAnnotation;
  5033. type = elementNode.type;
  5034. }
  5035. const isLabeled = type === "TSNamedTupleMember";
  5036. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  5037. if (labeledElements !== isLabeled) {
  5038. this.raise(elementNode.start, TSErrors.MixedLabeledAndUnlabeledElements);
  5039. }
  5040. });
  5041. return this.finishNode(node, "TSTupleType");
  5042. }
  5043. tsParseTupleElementType() {
  5044. const {
  5045. start: startPos,
  5046. startLoc
  5047. } = this.state;
  5048. const rest = this.eat(types$1.ellipsis);
  5049. let type = this.tsParseType();
  5050. const optional = this.eat(types$1.question);
  5051. const labeled = this.eat(types$1.colon);
  5052. if (labeled) {
  5053. const labeledNode = this.startNodeAtNode(type);
  5054. labeledNode.optional = optional;
  5055. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  5056. labeledNode.label = type.typeName;
  5057. } else {
  5058. this.raise(type.start, TSErrors.InvalidTupleMemberLabel);
  5059. labeledNode.label = type;
  5060. }
  5061. labeledNode.elementType = this.tsParseType();
  5062. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  5063. } else if (optional) {
  5064. const optionalTypeNode = this.startNodeAtNode(type);
  5065. optionalTypeNode.typeAnnotation = type;
  5066. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  5067. }
  5068. if (rest) {
  5069. const restNode = this.startNodeAt(startPos, startLoc);
  5070. restNode.typeAnnotation = type;
  5071. type = this.finishNode(restNode, "TSRestType");
  5072. }
  5073. return type;
  5074. }
  5075. tsParseParenthesizedType() {
  5076. const node = this.startNode();
  5077. this.expect(types$1.parenL);
  5078. node.typeAnnotation = this.tsParseType();
  5079. this.expect(types$1.parenR);
  5080. return this.finishNode(node, "TSParenthesizedType");
  5081. }
  5082. tsParseFunctionOrConstructorType(type, abstract) {
  5083. const node = this.startNode();
  5084. if (type === "TSConstructorType") {
  5085. node.abstract = !!abstract;
  5086. if (abstract) this.next();
  5087. this.next();
  5088. }
  5089. this.tsFillSignature(types$1.arrow, node);
  5090. return this.finishNode(node, type);
  5091. }
  5092. tsParseLiteralTypeNode() {
  5093. const node = this.startNode();
  5094. node.literal = (() => {
  5095. switch (this.state.type) {
  5096. case types$1.num:
  5097. case types$1.bigint:
  5098. case types$1.string:
  5099. case types$1._true:
  5100. case types$1._false:
  5101. return this.parseExprAtom();
  5102. default:
  5103. throw this.unexpected();
  5104. }
  5105. })();
  5106. return this.finishNode(node, "TSLiteralType");
  5107. }
  5108. tsParseTemplateLiteralType() {
  5109. const node = this.startNode();
  5110. node.literal = this.parseTemplate(false);
  5111. return this.finishNode(node, "TSLiteralType");
  5112. }
  5113. parseTemplateSubstitution() {
  5114. if (this.state.inType) return this.tsParseType();
  5115. return super.parseTemplateSubstitution();
  5116. }
  5117. tsParseThisTypeOrThisTypePredicate() {
  5118. const thisKeyword = this.tsParseThisTypeNode();
  5119. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5120. return this.tsParseThisTypePredicate(thisKeyword);
  5121. } else {
  5122. return thisKeyword;
  5123. }
  5124. }
  5125. tsParseNonArrayType() {
  5126. switch (this.state.type) {
  5127. case types$1.name:
  5128. case types$1._void:
  5129. case types$1._null:
  5130. {
  5131. const type = this.match(types$1._void) ? "TSVoidKeyword" : this.match(types$1._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  5132. if (type !== undefined && this.lookaheadCharCode() !== 46) {
  5133. const node = this.startNode();
  5134. this.next();
  5135. return this.finishNode(node, type);
  5136. }
  5137. return this.tsParseTypeReference();
  5138. }
  5139. case types$1.string:
  5140. case types$1.num:
  5141. case types$1.bigint:
  5142. case types$1._true:
  5143. case types$1._false:
  5144. return this.tsParseLiteralTypeNode();
  5145. case types$1.plusMin:
  5146. if (this.state.value === "-") {
  5147. const node = this.startNode();
  5148. const nextToken = this.lookahead();
  5149. if (nextToken.type !== types$1.num && nextToken.type !== types$1.bigint) {
  5150. throw this.unexpected();
  5151. }
  5152. node.literal = this.parseMaybeUnary();
  5153. return this.finishNode(node, "TSLiteralType");
  5154. }
  5155. break;
  5156. case types$1._this:
  5157. return this.tsParseThisTypeOrThisTypePredicate();
  5158. case types$1._typeof:
  5159. return this.tsParseTypeQuery();
  5160. case types$1._import:
  5161. return this.tsParseImportType();
  5162. case types$1.braceL:
  5163. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  5164. case types$1.bracketL:
  5165. return this.tsParseTupleType();
  5166. case types$1.parenL:
  5167. return this.tsParseParenthesizedType();
  5168. case types$1.backQuote:
  5169. return this.tsParseTemplateLiteralType();
  5170. }
  5171. throw this.unexpected();
  5172. }
  5173. tsParseArrayTypeOrHigher() {
  5174. let type = this.tsParseNonArrayType();
  5175. while (!this.hasPrecedingLineBreak() && this.eat(types$1.bracketL)) {
  5176. if (this.match(types$1.bracketR)) {
  5177. const node = this.startNodeAtNode(type);
  5178. node.elementType = type;
  5179. this.expect(types$1.bracketR);
  5180. type = this.finishNode(node, "TSArrayType");
  5181. } else {
  5182. const node = this.startNodeAtNode(type);
  5183. node.objectType = type;
  5184. node.indexType = this.tsParseType();
  5185. this.expect(types$1.bracketR);
  5186. type = this.finishNode(node, "TSIndexedAccessType");
  5187. }
  5188. }
  5189. return type;
  5190. }
  5191. tsParseTypeOperator(operator) {
  5192. const node = this.startNode();
  5193. this.expectContextual(operator);
  5194. node.operator = operator;
  5195. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  5196. if (operator === "readonly") {
  5197. this.tsCheckTypeAnnotationForReadOnly(node);
  5198. }
  5199. return this.finishNode(node, "TSTypeOperator");
  5200. }
  5201. tsCheckTypeAnnotationForReadOnly(node) {
  5202. switch (node.typeAnnotation.type) {
  5203. case "TSTupleType":
  5204. case "TSArrayType":
  5205. return;
  5206. default:
  5207. this.raise(node.start, TSErrors.UnexpectedReadonly);
  5208. }
  5209. }
  5210. tsParseInferType() {
  5211. const node = this.startNode();
  5212. this.expectContextual("infer");
  5213. const typeParameter = this.startNode();
  5214. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  5215. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  5216. return this.finishNode(node, "TSInferType");
  5217. }
  5218. tsParseTypeOperatorOrHigher() {
  5219. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  5220. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  5221. }
  5222. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  5223. const node = this.startNode();
  5224. const hasLeadingOperator = this.eat(operator);
  5225. const types = [];
  5226. do {
  5227. types.push(parseConstituentType());
  5228. } while (this.eat(operator));
  5229. if (types.length === 1 && !hasLeadingOperator) {
  5230. return types[0];
  5231. }
  5232. node.types = types;
  5233. return this.finishNode(node, kind);
  5234. }
  5235. tsParseIntersectionTypeOrHigher() {
  5236. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types$1.bitwiseAND);
  5237. }
  5238. tsParseUnionTypeOrHigher() {
  5239. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types$1.bitwiseOR);
  5240. }
  5241. tsIsStartOfFunctionType() {
  5242. if (this.isRelational("<")) {
  5243. return true;
  5244. }
  5245. return this.match(types$1.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  5246. }
  5247. tsSkipParameterStart() {
  5248. if (this.match(types$1.name) || this.match(types$1._this)) {
  5249. this.next();
  5250. return true;
  5251. }
  5252. if (this.match(types$1.braceL)) {
  5253. let braceStackCounter = 1;
  5254. this.next();
  5255. while (braceStackCounter > 0) {
  5256. if (this.match(types$1.braceL)) {
  5257. ++braceStackCounter;
  5258. } else if (this.match(types$1.braceR)) {
  5259. --braceStackCounter;
  5260. }
  5261. this.next();
  5262. }
  5263. return true;
  5264. }
  5265. if (this.match(types$1.bracketL)) {
  5266. let braceStackCounter = 1;
  5267. this.next();
  5268. while (braceStackCounter > 0) {
  5269. if (this.match(types$1.bracketL)) {
  5270. ++braceStackCounter;
  5271. } else if (this.match(types$1.bracketR)) {
  5272. --braceStackCounter;
  5273. }
  5274. this.next();
  5275. }
  5276. return true;
  5277. }
  5278. return false;
  5279. }
  5280. tsIsUnambiguouslyStartOfFunctionType() {
  5281. this.next();
  5282. if (this.match(types$1.parenR) || this.match(types$1.ellipsis)) {
  5283. return true;
  5284. }
  5285. if (this.tsSkipParameterStart()) {
  5286. if (this.match(types$1.colon) || this.match(types$1.comma) || this.match(types$1.question) || this.match(types$1.eq)) {
  5287. return true;
  5288. }
  5289. if (this.match(types$1.parenR)) {
  5290. this.next();
  5291. if (this.match(types$1.arrow)) {
  5292. return true;
  5293. }
  5294. }
  5295. }
  5296. return false;
  5297. }
  5298. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  5299. return this.tsInType(() => {
  5300. const t = this.startNode();
  5301. this.expect(returnToken);
  5302. const node = this.startNode();
  5303. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  5304. if (asserts && this.match(types$1._this)) {
  5305. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  5306. if (thisTypePredicate.type === "TSThisType") {
  5307. node.parameterName = thisTypePredicate;
  5308. node.asserts = true;
  5309. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  5310. } else {
  5311. this.resetStartLocationFromNode(thisTypePredicate, node);
  5312. thisTypePredicate.asserts = true;
  5313. }
  5314. t.typeAnnotation = thisTypePredicate;
  5315. return this.finishNode(t, "TSTypeAnnotation");
  5316. }
  5317. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  5318. if (!typePredicateVariable) {
  5319. if (!asserts) {
  5320. return this.tsParseTypeAnnotation(false, t);
  5321. }
  5322. node.parameterName = this.parseIdentifier();
  5323. node.asserts = asserts;
  5324. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5325. return this.finishNode(t, "TSTypeAnnotation");
  5326. }
  5327. const type = this.tsParseTypeAnnotation(false);
  5328. node.parameterName = typePredicateVariable;
  5329. node.typeAnnotation = type;
  5330. node.asserts = asserts;
  5331. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5332. return this.finishNode(t, "TSTypeAnnotation");
  5333. });
  5334. }
  5335. tsTryParseTypeOrTypePredicateAnnotation() {
  5336. return this.match(types$1.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types$1.colon) : undefined;
  5337. }
  5338. tsTryParseTypeAnnotation() {
  5339. return this.match(types$1.colon) ? this.tsParseTypeAnnotation() : undefined;
  5340. }
  5341. tsTryParseType() {
  5342. return this.tsEatThenParseType(types$1.colon);
  5343. }
  5344. tsParseTypePredicatePrefix() {
  5345. const id = this.parseIdentifier();
  5346. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5347. this.next();
  5348. return id;
  5349. }
  5350. }
  5351. tsParseTypePredicateAsserts() {
  5352. if (!this.match(types$1.name) || this.state.value !== "asserts" || this.hasPrecedingLineBreak()) {
  5353. return false;
  5354. }
  5355. const containsEsc = this.state.containsEsc;
  5356. this.next();
  5357. if (!this.match(types$1.name) && !this.match(types$1._this)) {
  5358. return false;
  5359. }
  5360. if (containsEsc) {
  5361. this.raise(this.state.lastTokStart, ErrorMessages.InvalidEscapedReservedWord, "asserts");
  5362. }
  5363. return true;
  5364. }
  5365. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  5366. this.tsInType(() => {
  5367. if (eatColon) this.expect(types$1.colon);
  5368. t.typeAnnotation = this.tsParseType();
  5369. });
  5370. return this.finishNode(t, "TSTypeAnnotation");
  5371. }
  5372. tsParseType() {
  5373. assert(this.state.inType);
  5374. const type = this.tsParseNonConditionalType();
  5375. if (this.hasPrecedingLineBreak() || !this.eat(types$1._extends)) {
  5376. return type;
  5377. }
  5378. const node = this.startNodeAtNode(type);
  5379. node.checkType = type;
  5380. node.extendsType = this.tsParseNonConditionalType();
  5381. this.expect(types$1.question);
  5382. node.trueType = this.tsParseType();
  5383. this.expect(types$1.colon);
  5384. node.falseType = this.tsParseType();
  5385. return this.finishNode(node, "TSConditionalType");
  5386. }
  5387. isAbstractConstructorSignature() {
  5388. return this.isContextual("abstract") && this.lookahead().type === types$1._new;
  5389. }
  5390. tsParseNonConditionalType() {
  5391. if (this.tsIsStartOfFunctionType()) {
  5392. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  5393. }
  5394. if (this.match(types$1._new)) {
  5395. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  5396. } else if (this.isAbstractConstructorSignature()) {
  5397. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  5398. }
  5399. return this.tsParseUnionTypeOrHigher();
  5400. }
  5401. tsParseTypeAssertion() {
  5402. const node = this.startNode();
  5403. const _const = this.tsTryNextParseConstantContext();
  5404. node.typeAnnotation = _const || this.tsNextThenParseType();
  5405. this.expectRelational(">");
  5406. node.expression = this.parseMaybeUnary();
  5407. return this.finishNode(node, "TSTypeAssertion");
  5408. }
  5409. tsParseHeritageClause(descriptor) {
  5410. const originalStart = this.state.start;
  5411. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  5412. if (!delimitedList.length) {
  5413. this.raise(originalStart, TSErrors.EmptyHeritageClauseType, descriptor);
  5414. }
  5415. return delimitedList;
  5416. }
  5417. tsParseExpressionWithTypeArguments() {
  5418. const node = this.startNode();
  5419. node.expression = this.tsParseEntityName(false);
  5420. if (this.isRelational("<")) {
  5421. node.typeParameters = this.tsParseTypeArguments();
  5422. }
  5423. return this.finishNode(node, "TSExpressionWithTypeArguments");
  5424. }
  5425. tsParseInterfaceDeclaration(node) {
  5426. node.id = this.parseIdentifier();
  5427. this.checkLVal(node.id, "typescript interface declaration", BIND_TS_INTERFACE);
  5428. node.typeParameters = this.tsTryParseTypeParameters();
  5429. if (this.eat(types$1._extends)) {
  5430. node.extends = this.tsParseHeritageClause("extends");
  5431. }
  5432. const body = this.startNode();
  5433. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  5434. node.body = this.finishNode(body, "TSInterfaceBody");
  5435. return this.finishNode(node, "TSInterfaceDeclaration");
  5436. }
  5437. tsParseTypeAliasDeclaration(node) {
  5438. node.id = this.parseIdentifier();
  5439. this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
  5440. node.typeParameters = this.tsTryParseTypeParameters();
  5441. node.typeAnnotation = this.tsInType(() => {
  5442. this.expect(types$1.eq);
  5443. if (this.isContextual("intrinsic") && this.lookahead().type !== types$1.dot) {
  5444. const node = this.startNode();
  5445. this.next();
  5446. return this.finishNode(node, "TSIntrinsicKeyword");
  5447. }
  5448. return this.tsParseType();
  5449. });
  5450. this.semicolon();
  5451. return this.finishNode(node, "TSTypeAliasDeclaration");
  5452. }
  5453. tsInNoContext(cb) {
  5454. const oldContext = this.state.context;
  5455. this.state.context = [oldContext[0]];
  5456. try {
  5457. return cb();
  5458. } finally {
  5459. this.state.context = oldContext;
  5460. }
  5461. }
  5462. tsInType(cb) {
  5463. const oldInType = this.state.inType;
  5464. this.state.inType = true;
  5465. try {
  5466. return cb();
  5467. } finally {
  5468. this.state.inType = oldInType;
  5469. }
  5470. }
  5471. tsEatThenParseType(token) {
  5472. return !this.match(token) ? undefined : this.tsNextThenParseType();
  5473. }
  5474. tsExpectThenParseType(token) {
  5475. return this.tsDoThenParseType(() => this.expect(token));
  5476. }
  5477. tsNextThenParseType() {
  5478. return this.tsDoThenParseType(() => this.next());
  5479. }
  5480. tsDoThenParseType(cb) {
  5481. return this.tsInType(() => {
  5482. cb();
  5483. return this.tsParseType();
  5484. });
  5485. }
  5486. tsParseEnumMember() {
  5487. const node = this.startNode();
  5488. node.id = this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  5489. if (this.eat(types$1.eq)) {
  5490. node.initializer = this.parseMaybeAssignAllowIn();
  5491. }
  5492. return this.finishNode(node, "TSEnumMember");
  5493. }
  5494. tsParseEnumDeclaration(node, isConst) {
  5495. if (isConst) node.const = true;
  5496. node.id = this.parseIdentifier();
  5497. this.checkLVal(node.id, "typescript enum declaration", isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  5498. this.expect(types$1.braceL);
  5499. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  5500. this.expect(types$1.braceR);
  5501. return this.finishNode(node, "TSEnumDeclaration");
  5502. }
  5503. tsParseModuleBlock() {
  5504. const node = this.startNode();
  5505. this.scope.enter(SCOPE_OTHER);
  5506. this.expect(types$1.braceL);
  5507. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types$1.braceR);
  5508. this.scope.exit();
  5509. return this.finishNode(node, "TSModuleBlock");
  5510. }
  5511. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  5512. node.id = this.parseIdentifier();
  5513. if (!nested) {
  5514. this.checkLVal(node.id, "module or namespace declaration", BIND_TS_NAMESPACE);
  5515. }
  5516. if (this.eat(types$1.dot)) {
  5517. const inner = this.startNode();
  5518. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  5519. node.body = inner;
  5520. } else {
  5521. this.scope.enter(SCOPE_TS_MODULE);
  5522. this.prodParam.enter(PARAM);
  5523. node.body = this.tsParseModuleBlock();
  5524. this.prodParam.exit();
  5525. this.scope.exit();
  5526. }
  5527. return this.finishNode(node, "TSModuleDeclaration");
  5528. }
  5529. tsParseAmbientExternalModuleDeclaration(node) {
  5530. if (this.isContextual("global")) {
  5531. node.global = true;
  5532. node.id = this.parseIdentifier();
  5533. } else if (this.match(types$1.string)) {
  5534. node.id = this.parseExprAtom();
  5535. } else {
  5536. this.unexpected();
  5537. }
  5538. if (this.match(types$1.braceL)) {
  5539. this.scope.enter(SCOPE_TS_MODULE);
  5540. this.prodParam.enter(PARAM);
  5541. node.body = this.tsParseModuleBlock();
  5542. this.prodParam.exit();
  5543. this.scope.exit();
  5544. } else {
  5545. this.semicolon();
  5546. }
  5547. return this.finishNode(node, "TSModuleDeclaration");
  5548. }
  5549. tsParseImportEqualsDeclaration(node, isExport) {
  5550. node.isExport = isExport || false;
  5551. node.id = this.parseIdentifier();
  5552. this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
  5553. this.expect(types$1.eq);
  5554. const moduleReference = this.tsParseModuleReference();
  5555. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  5556. this.raise(moduleReference.start, TSErrors.ImportAliasHasImportType);
  5557. }
  5558. node.moduleReference = moduleReference;
  5559. this.semicolon();
  5560. return this.finishNode(node, "TSImportEqualsDeclaration");
  5561. }
  5562. tsIsExternalModuleReference() {
  5563. return this.isContextual("require") && this.lookaheadCharCode() === 40;
  5564. }
  5565. tsParseModuleReference() {
  5566. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  5567. }
  5568. tsParseExternalModuleReference() {
  5569. const node = this.startNode();
  5570. this.expectContextual("require");
  5571. this.expect(types$1.parenL);
  5572. if (!this.match(types$1.string)) {
  5573. throw this.unexpected();
  5574. }
  5575. node.expression = this.parseExprAtom();
  5576. this.expect(types$1.parenR);
  5577. return this.finishNode(node, "TSExternalModuleReference");
  5578. }
  5579. tsLookAhead(f) {
  5580. const state = this.state.clone();
  5581. const res = f();
  5582. this.state = state;
  5583. return res;
  5584. }
  5585. tsTryParseAndCatch(f) {
  5586. const result = this.tryParse(abort => f() || abort());
  5587. if (result.aborted || !result.node) return undefined;
  5588. if (result.error) this.state = result.failState;
  5589. return result.node;
  5590. }
  5591. tsTryParse(f) {
  5592. const state = this.state.clone();
  5593. const result = f();
  5594. if (result !== undefined && result !== false) {
  5595. return result;
  5596. } else {
  5597. this.state = state;
  5598. return undefined;
  5599. }
  5600. }
  5601. tsTryParseDeclare(nany) {
  5602. if (this.isLineTerminator()) {
  5603. return;
  5604. }
  5605. let starttype = this.state.type;
  5606. let kind;
  5607. if (this.isContextual("let")) {
  5608. starttype = types$1._var;
  5609. kind = "let";
  5610. }
  5611. return this.tsInAmbientContext(() => {
  5612. switch (starttype) {
  5613. case types$1._function:
  5614. nany.declare = true;
  5615. return this.parseFunctionStatement(nany, false, true);
  5616. case types$1._class:
  5617. nany.declare = true;
  5618. return this.parseClass(nany, true, false);
  5619. case types$1._const:
  5620. if (this.match(types$1._const) && this.isLookaheadContextual("enum")) {
  5621. this.expect(types$1._const);
  5622. this.expectContextual("enum");
  5623. return this.tsParseEnumDeclaration(nany, true);
  5624. }
  5625. case types$1._var:
  5626. kind = kind || this.state.value;
  5627. return this.parseVarStatement(nany, kind);
  5628. case types$1.name:
  5629. {
  5630. const value = this.state.value;
  5631. if (value === "global") {
  5632. return this.tsParseAmbientExternalModuleDeclaration(nany);
  5633. } else {
  5634. return this.tsParseDeclaration(nany, value, true);
  5635. }
  5636. }
  5637. }
  5638. });
  5639. }
  5640. tsTryParseExportDeclaration() {
  5641. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  5642. }
  5643. tsParseExpressionStatement(node, expr) {
  5644. switch (expr.name) {
  5645. case "declare":
  5646. {
  5647. const declaration = this.tsTryParseDeclare(node);
  5648. if (declaration) {
  5649. declaration.declare = true;
  5650. return declaration;
  5651. }
  5652. break;
  5653. }
  5654. case "global":
  5655. if (this.match(types$1.braceL)) {
  5656. this.scope.enter(SCOPE_TS_MODULE);
  5657. this.prodParam.enter(PARAM);
  5658. const mod = node;
  5659. mod.global = true;
  5660. mod.id = expr;
  5661. mod.body = this.tsParseModuleBlock();
  5662. this.scope.exit();
  5663. this.prodParam.exit();
  5664. return this.finishNode(mod, "TSModuleDeclaration");
  5665. }
  5666. break;
  5667. default:
  5668. return this.tsParseDeclaration(node, expr.name, false);
  5669. }
  5670. }
  5671. tsParseDeclaration(node, value, next) {
  5672. switch (value) {
  5673. case "abstract":
  5674. if (this.tsCheckLineTerminator(next) && (this.match(types$1._class) || this.match(types$1.name))) {
  5675. return this.tsParseAbstractDeclaration(node);
  5676. }
  5677. break;
  5678. case "enum":
  5679. if (next || this.match(types$1.name)) {
  5680. if (next) this.next();
  5681. return this.tsParseEnumDeclaration(node, false);
  5682. }
  5683. break;
  5684. case "interface":
  5685. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5686. return this.tsParseInterfaceDeclaration(node);
  5687. }
  5688. break;
  5689. case "module":
  5690. if (this.tsCheckLineTerminator(next)) {
  5691. if (this.match(types$1.string)) {
  5692. return this.tsParseAmbientExternalModuleDeclaration(node);
  5693. } else if (this.match(types$1.name)) {
  5694. return this.tsParseModuleOrNamespaceDeclaration(node);
  5695. }
  5696. }
  5697. break;
  5698. case "namespace":
  5699. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5700. return this.tsParseModuleOrNamespaceDeclaration(node);
  5701. }
  5702. break;
  5703. case "type":
  5704. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5705. return this.tsParseTypeAliasDeclaration(node);
  5706. }
  5707. break;
  5708. }
  5709. }
  5710. tsCheckLineTerminator(next) {
  5711. if (next) {
  5712. if (this.hasFollowingLineBreak()) return false;
  5713. this.next();
  5714. return true;
  5715. }
  5716. return !this.isLineTerminator();
  5717. }
  5718. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  5719. if (!this.isRelational("<")) {
  5720. return undefined;
  5721. }
  5722. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  5723. this.state.maybeInArrowParameters = true;
  5724. const res = this.tsTryParseAndCatch(() => {
  5725. const node = this.startNodeAt(startPos, startLoc);
  5726. node.typeParameters = this.tsParseTypeParameters();
  5727. super.parseFunctionParams(node);
  5728. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  5729. this.expect(types$1.arrow);
  5730. return node;
  5731. });
  5732. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  5733. if (!res) {
  5734. return undefined;
  5735. }
  5736. return this.parseArrowExpression(res, null, true);
  5737. }
  5738. tsParseTypeArguments() {
  5739. const node = this.startNode();
  5740. node.params = this.tsInType(() => this.tsInNoContext(() => {
  5741. this.expectRelational("<");
  5742. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  5743. }));
  5744. if (node.params.length === 0) {
  5745. this.raise(node.start, TSErrors.EmptyTypeArguments);
  5746. }
  5747. this.state.exprAllowed = false;
  5748. this.expectRelational(">");
  5749. return this.finishNode(node, "TSTypeParameterInstantiation");
  5750. }
  5751. tsIsDeclarationStart() {
  5752. if (this.match(types$1.name)) {
  5753. switch (this.state.value) {
  5754. case "abstract":
  5755. case "declare":
  5756. case "enum":
  5757. case "interface":
  5758. case "module":
  5759. case "namespace":
  5760. case "type":
  5761. return true;
  5762. }
  5763. }
  5764. return false;
  5765. }
  5766. isExportDefaultSpecifier() {
  5767. if (this.tsIsDeclarationStart()) return false;
  5768. return super.isExportDefaultSpecifier();
  5769. }
  5770. parseAssignableListItem(allowModifiers, decorators) {
  5771. const startPos = this.state.start;
  5772. const startLoc = this.state.startLoc;
  5773. let accessibility;
  5774. let readonly = false;
  5775. if (allowModifiers !== undefined) {
  5776. accessibility = this.parseAccessModifier();
  5777. readonly = !!this.tsParseModifier(["readonly"]);
  5778. if (allowModifiers === false && (accessibility || readonly)) {
  5779. this.raise(startPos, TSErrors.UnexpectedParameterModifier);
  5780. }
  5781. }
  5782. const left = this.parseMaybeDefault();
  5783. this.parseAssignableListItemTypes(left);
  5784. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  5785. if (accessibility || readonly) {
  5786. const pp = this.startNodeAt(startPos, startLoc);
  5787. if (decorators.length) {
  5788. pp.decorators = decorators;
  5789. }
  5790. if (accessibility) pp.accessibility = accessibility;
  5791. if (readonly) pp.readonly = readonly;
  5792. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  5793. this.raise(pp.start, TSErrors.UnsupportedParameterPropertyKind);
  5794. }
  5795. pp.parameter = elt;
  5796. return this.finishNode(pp, "TSParameterProperty");
  5797. }
  5798. if (decorators.length) {
  5799. left.decorators = decorators;
  5800. }
  5801. return elt;
  5802. }
  5803. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5804. if (this.match(types$1.colon)) {
  5805. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  5806. }
  5807. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  5808. if (bodilessType && !this.match(types$1.braceL) && this.isLineTerminator()) {
  5809. this.finishNode(node, bodilessType);
  5810. return;
  5811. }
  5812. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  5813. this.raise(node.start, TSErrors.DeclareFunctionHasImplementation);
  5814. if (node.declare) {
  5815. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  5816. return;
  5817. }
  5818. }
  5819. super.parseFunctionBodyAndFinish(node, type, isMethod);
  5820. }
  5821. registerFunctionStatementId(node) {
  5822. if (!node.body && node.id) {
  5823. this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
  5824. } else {
  5825. super.registerFunctionStatementId(...arguments);
  5826. }
  5827. }
  5828. tsCheckForInvalidTypeCasts(items) {
  5829. items.forEach(node => {
  5830. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  5831. this.raise(node.typeAnnotation.start, TSErrors.UnexpectedTypeAnnotation);
  5832. }
  5833. });
  5834. }
  5835. toReferencedList(exprList, isInParens) {
  5836. this.tsCheckForInvalidTypeCasts(exprList);
  5837. return exprList;
  5838. }
  5839. parseArrayLike(...args) {
  5840. const node = super.parseArrayLike(...args);
  5841. if (node.type === "ArrayExpression") {
  5842. this.tsCheckForInvalidTypeCasts(node.elements);
  5843. }
  5844. return node;
  5845. }
  5846. parseSubscript(base, startPos, startLoc, noCalls, state) {
  5847. if (!this.hasPrecedingLineBreak() && this.match(types$1.bang)) {
  5848. this.state.exprAllowed = false;
  5849. this.next();
  5850. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  5851. nonNullExpression.expression = base;
  5852. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  5853. }
  5854. if (this.isRelational("<")) {
  5855. const result = this.tsTryParseAndCatch(() => {
  5856. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  5857. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  5858. if (asyncArrowFn) {
  5859. return asyncArrowFn;
  5860. }
  5861. }
  5862. const node = this.startNodeAt(startPos, startLoc);
  5863. node.callee = base;
  5864. const typeArguments = this.tsParseTypeArguments();
  5865. if (typeArguments) {
  5866. if (!noCalls && this.eat(types$1.parenL)) {
  5867. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  5868. this.tsCheckForInvalidTypeCasts(node.arguments);
  5869. node.typeParameters = typeArguments;
  5870. if (state.optionalChainMember) {
  5871. node.optional = false;
  5872. }
  5873. return this.finishCallExpression(node, state.optionalChainMember);
  5874. } else if (this.match(types$1.backQuote)) {
  5875. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  5876. result.typeParameters = typeArguments;
  5877. return result;
  5878. }
  5879. }
  5880. this.unexpected();
  5881. });
  5882. if (result) return result;
  5883. }
  5884. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  5885. }
  5886. parseNewArguments(node) {
  5887. if (this.isRelational("<")) {
  5888. const typeParameters = this.tsTryParseAndCatch(() => {
  5889. const args = this.tsParseTypeArguments();
  5890. if (!this.match(types$1.parenL)) this.unexpected();
  5891. return args;
  5892. });
  5893. if (typeParameters) {
  5894. node.typeParameters = typeParameters;
  5895. }
  5896. }
  5897. super.parseNewArguments(node);
  5898. }
  5899. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  5900. if (nonNull(types$1._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  5901. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  5902. node.expression = left;
  5903. const _const = this.tsTryNextParseConstantContext();
  5904. if (_const) {
  5905. node.typeAnnotation = _const;
  5906. } else {
  5907. node.typeAnnotation = this.tsNextThenParseType();
  5908. }
  5909. this.finishNode(node, "TSAsExpression");
  5910. this.reScan_lt_gt();
  5911. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  5912. }
  5913. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  5914. }
  5915. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  5916. checkDuplicateExports() {}
  5917. parseImport(node) {
  5918. node.importKind = "value";
  5919. if (this.match(types$1.name) || this.match(types$1.star) || this.match(types$1.braceL)) {
  5920. let ahead = this.lookahead();
  5921. if (this.isContextual("type") && ahead.type !== types$1.comma && !(ahead.type === types$1.name && ahead.value === "from") && ahead.type !== types$1.eq) {
  5922. node.importKind = "type";
  5923. this.next();
  5924. ahead = this.lookahead();
  5925. }
  5926. if (this.match(types$1.name) && ahead.type === types$1.eq) {
  5927. return this.tsParseImportEqualsDeclaration(node);
  5928. }
  5929. }
  5930. const importNode = super.parseImport(node);
  5931. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  5932. this.raise(importNode.start, TSErrors.TypeImportCannotSpecifyDefaultAndNamed);
  5933. }
  5934. return importNode;
  5935. }
  5936. parseExport(node) {
  5937. if (this.match(types$1._import)) {
  5938. this.next();
  5939. if (this.isContextual("type") && this.lookaheadCharCode() !== 61) {
  5940. node.importKind = "type";
  5941. this.next();
  5942. } else {
  5943. node.importKind = "value";
  5944. }
  5945. return this.tsParseImportEqualsDeclaration(node, true);
  5946. } else if (this.eat(types$1.eq)) {
  5947. const assign = node;
  5948. assign.expression = this.parseExpression();
  5949. this.semicolon();
  5950. return this.finishNode(assign, "TSExportAssignment");
  5951. } else if (this.eatContextual("as")) {
  5952. const decl = node;
  5953. this.expectContextual("namespace");
  5954. decl.id = this.parseIdentifier();
  5955. this.semicolon();
  5956. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  5957. } else {
  5958. if (this.isContextual("type") && this.lookahead().type === types$1.braceL) {
  5959. this.next();
  5960. node.exportKind = "type";
  5961. } else {
  5962. node.exportKind = "value";
  5963. }
  5964. return super.parseExport(node);
  5965. }
  5966. }
  5967. isAbstractClass() {
  5968. return this.isContextual("abstract") && this.lookahead().type === types$1._class;
  5969. }
  5970. parseExportDefaultExpression() {
  5971. if (this.isAbstractClass()) {
  5972. const cls = this.startNode();
  5973. this.next();
  5974. cls.abstract = true;
  5975. this.parseClass(cls, true, true);
  5976. return cls;
  5977. }
  5978. if (this.state.value === "interface") {
  5979. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  5980. if (result) return result;
  5981. }
  5982. return super.parseExportDefaultExpression();
  5983. }
  5984. parseStatementContent(context, topLevel) {
  5985. if (this.state.type === types$1._const) {
  5986. const ahead = this.lookahead();
  5987. if (ahead.type === types$1.name && ahead.value === "enum") {
  5988. const node = this.startNode();
  5989. this.expect(types$1._const);
  5990. this.expectContextual("enum");
  5991. return this.tsParseEnumDeclaration(node, true);
  5992. }
  5993. }
  5994. return super.parseStatementContent(context, topLevel);
  5995. }
  5996. parseAccessModifier() {
  5997. return this.tsParseModifier(["public", "protected", "private"]);
  5998. }
  5999. tsHasSomeModifiers(member, modifiers) {
  6000. return modifiers.some(modifier => {
  6001. if (tsIsAccessModifier(modifier)) {
  6002. return member.accessibility === modifier;
  6003. }
  6004. return !!member[modifier];
  6005. });
  6006. }
  6007. parseClassMember(classBody, member, state) {
  6008. const invalidModifersForStaticBlocks = ["declare", "private", "public", "protected", "override", "abstract", "readonly"];
  6009. this.tsParseModifiers(member, invalidModifersForStaticBlocks.concat(["static"]));
  6010. const callParseClassMemberWithIsStatic = () => {
  6011. const isStatic = !!member.static;
  6012. if (isStatic && this.eat(types$1.braceL)) {
  6013. if (this.tsHasSomeModifiers(member, invalidModifersForStaticBlocks)) {
  6014. this.raise(this.state.pos, TSErrors.StaticBlockCannotHaveModifier);
  6015. }
  6016. this.parseClassStaticBlock(classBody, member);
  6017. } else {
  6018. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6019. }
  6020. };
  6021. if (member.declare) {
  6022. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  6023. } else {
  6024. callParseClassMemberWithIsStatic();
  6025. }
  6026. }
  6027. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  6028. const idx = this.tsTryParseIndexSignature(member);
  6029. if (idx) {
  6030. classBody.body.push(idx);
  6031. if (member.abstract) {
  6032. this.raise(member.start, TSErrors.IndexSignatureHasAbstract);
  6033. }
  6034. if (member.accessibility) {
  6035. this.raise(member.start, TSErrors.IndexSignatureHasAccessibility, member.accessibility);
  6036. }
  6037. if (member.declare) {
  6038. this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
  6039. }
  6040. if (member.override) {
  6041. this.raise(member.start, TSErrors.IndexSignatureHasOverride);
  6042. }
  6043. return;
  6044. }
  6045. if (!this.state.inAbstractClass && member.abstract) {
  6046. this.raise(member.start, TSErrors.NonAbstractClassHasAbstractMethod);
  6047. }
  6048. if (member.override) {
  6049. if (!state.hadSuperClass) {
  6050. this.raise(member.start, TSErrors.OverrideNotInSubClass);
  6051. }
  6052. }
  6053. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6054. }
  6055. parsePostMemberNameModifiers(methodOrProp) {
  6056. const optional = this.eat(types$1.question);
  6057. if (optional) methodOrProp.optional = true;
  6058. if (methodOrProp.readonly && this.match(types$1.parenL)) {
  6059. this.raise(methodOrProp.start, TSErrors.ClassMethodHasReadonly);
  6060. }
  6061. if (methodOrProp.declare && this.match(types$1.parenL)) {
  6062. this.raise(methodOrProp.start, TSErrors.ClassMethodHasDeclare);
  6063. }
  6064. }
  6065. parseExpressionStatement(node, expr) {
  6066. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  6067. return decl || super.parseExpressionStatement(node, expr);
  6068. }
  6069. shouldParseExportDeclaration() {
  6070. if (this.tsIsDeclarationStart()) return true;
  6071. return super.shouldParseExportDeclaration();
  6072. }
  6073. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  6074. if (!refNeedsArrowPos || !this.match(types$1.question)) {
  6075. return super.parseConditional(expr, startPos, startLoc, refNeedsArrowPos);
  6076. }
  6077. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  6078. if (!result.node) {
  6079. refNeedsArrowPos.start = result.error.pos || this.state.start;
  6080. return expr;
  6081. }
  6082. if (result.error) this.state = result.failState;
  6083. return result.node;
  6084. }
  6085. parseParenItem(node, startPos, startLoc) {
  6086. node = super.parseParenItem(node, startPos, startLoc);
  6087. if (this.eat(types$1.question)) {
  6088. node.optional = true;
  6089. this.resetEndLocation(node);
  6090. }
  6091. if (this.match(types$1.colon)) {
  6092. const typeCastNode = this.startNodeAt(startPos, startLoc);
  6093. typeCastNode.expression = node;
  6094. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  6095. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  6096. }
  6097. return node;
  6098. }
  6099. parseExportDeclaration(node) {
  6100. const startPos = this.state.start;
  6101. const startLoc = this.state.startLoc;
  6102. const isDeclare = this.eatContextual("declare");
  6103. if (isDeclare && (this.isContextual("declare") || !this.shouldParseExportDeclaration())) {
  6104. throw this.raise(this.state.start, TSErrors.ExpectedAmbientAfterExportDeclare);
  6105. }
  6106. let declaration;
  6107. if (this.match(types$1.name)) {
  6108. declaration = this.tsTryParseExportDeclaration();
  6109. }
  6110. if (!declaration) {
  6111. declaration = super.parseExportDeclaration(node);
  6112. }
  6113. if (declaration && (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare)) {
  6114. node.exportKind = "type";
  6115. }
  6116. if (declaration && isDeclare) {
  6117. this.resetStartLocation(declaration, startPos, startLoc);
  6118. declaration.declare = true;
  6119. }
  6120. return declaration;
  6121. }
  6122. parseClassId(node, isStatement, optionalId) {
  6123. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  6124. return;
  6125. }
  6126. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  6127. const typeParameters = this.tsTryParseTypeParameters();
  6128. if (typeParameters) node.typeParameters = typeParameters;
  6129. }
  6130. parseClassPropertyAnnotation(node) {
  6131. if (!node.optional && this.eat(types$1.bang)) {
  6132. node.definite = true;
  6133. }
  6134. const type = this.tsTryParseTypeAnnotation();
  6135. if (type) node.typeAnnotation = type;
  6136. }
  6137. parseClassProperty(node) {
  6138. this.parseClassPropertyAnnotation(node);
  6139. if (this.state.isAmbientContext && this.match(types$1.eq)) {
  6140. this.raise(this.state.start, TSErrors.DeclareClassFieldHasInitializer);
  6141. }
  6142. return super.parseClassProperty(node);
  6143. }
  6144. parseClassPrivateProperty(node) {
  6145. if (node.abstract) {
  6146. this.raise(node.start, TSErrors.PrivateElementHasAbstract);
  6147. }
  6148. if (node.accessibility) {
  6149. this.raise(node.start, TSErrors.PrivateElementHasAccessibility, node.accessibility);
  6150. }
  6151. this.parseClassPropertyAnnotation(node);
  6152. return super.parseClassPrivateProperty(node);
  6153. }
  6154. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  6155. const typeParameters = this.tsTryParseTypeParameters();
  6156. if (typeParameters && isConstructor) {
  6157. this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
  6158. }
  6159. if (method.declare && (method.kind === "get" || method.kind === "set")) {
  6160. this.raise(method.start, TSErrors.DeclareAccessor, method.kind);
  6161. }
  6162. if (typeParameters) method.typeParameters = typeParameters;
  6163. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  6164. }
  6165. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  6166. const typeParameters = this.tsTryParseTypeParameters();
  6167. if (typeParameters) method.typeParameters = typeParameters;
  6168. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  6169. }
  6170. parseClassSuper(node) {
  6171. super.parseClassSuper(node);
  6172. if (node.superClass && this.isRelational("<")) {
  6173. node.superTypeParameters = this.tsParseTypeArguments();
  6174. }
  6175. if (this.eatContextual("implements")) {
  6176. node.implements = this.tsParseHeritageClause("implements");
  6177. }
  6178. }
  6179. parseObjPropValue(prop, ...args) {
  6180. const typeParameters = this.tsTryParseTypeParameters();
  6181. if (typeParameters) prop.typeParameters = typeParameters;
  6182. super.parseObjPropValue(prop, ...args);
  6183. }
  6184. parseFunctionParams(node, allowModifiers) {
  6185. const typeParameters = this.tsTryParseTypeParameters();
  6186. if (typeParameters) node.typeParameters = typeParameters;
  6187. super.parseFunctionParams(node, allowModifiers);
  6188. }
  6189. parseVarId(decl, kind) {
  6190. super.parseVarId(decl, kind);
  6191. if (decl.id.type === "Identifier" && this.eat(types$1.bang)) {
  6192. decl.definite = true;
  6193. }
  6194. const type = this.tsTryParseTypeAnnotation();
  6195. if (type) {
  6196. decl.id.typeAnnotation = type;
  6197. this.resetEndLocation(decl.id);
  6198. }
  6199. }
  6200. parseAsyncArrowFromCallExpression(node, call) {
  6201. if (this.match(types$1.colon)) {
  6202. node.returnType = this.tsParseTypeAnnotation();
  6203. }
  6204. return super.parseAsyncArrowFromCallExpression(node, call);
  6205. }
  6206. parseMaybeAssign(...args) {
  6207. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  6208. let state;
  6209. let jsx;
  6210. let typeCast;
  6211. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  6212. state = this.state.clone();
  6213. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6214. if (!jsx.error) return jsx.node;
  6215. const {
  6216. context
  6217. } = this.state;
  6218. if (context[context.length - 1] === types.j_oTag) {
  6219. context.length -= 2;
  6220. } else if (context[context.length - 1] === types.j_expr) {
  6221. context.length -= 1;
  6222. }
  6223. }
  6224. if (!((_jsx = jsx) != null && _jsx.error) && !this.isRelational("<")) {
  6225. return super.parseMaybeAssign(...args);
  6226. }
  6227. let typeParameters;
  6228. state = state || this.state.clone();
  6229. const arrow = this.tryParse(abort => {
  6230. var _expr$extra, _typeParameters;
  6231. typeParameters = this.tsParseTypeParameters();
  6232. const expr = super.parseMaybeAssign(...args);
  6233. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  6234. abort();
  6235. }
  6236. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  6237. this.resetStartLocationFromNode(expr, typeParameters);
  6238. }
  6239. expr.typeParameters = typeParameters;
  6240. return expr;
  6241. }, state);
  6242. if (!arrow.error && !arrow.aborted) return arrow.node;
  6243. if (!jsx) {
  6244. assert(!this.hasPlugin("jsx"));
  6245. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6246. if (!typeCast.error) return typeCast.node;
  6247. }
  6248. if ((_jsx2 = jsx) != null && _jsx2.node) {
  6249. this.state = jsx.failState;
  6250. return jsx.node;
  6251. }
  6252. if (arrow.node) {
  6253. this.state = arrow.failState;
  6254. return arrow.node;
  6255. }
  6256. if ((_typeCast = typeCast) != null && _typeCast.node) {
  6257. this.state = typeCast.failState;
  6258. return typeCast.node;
  6259. }
  6260. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  6261. if (arrow.thrown) throw arrow.error;
  6262. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  6263. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  6264. }
  6265. parseMaybeUnary(refExpressionErrors) {
  6266. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  6267. return this.tsParseTypeAssertion();
  6268. } else {
  6269. return super.parseMaybeUnary(refExpressionErrors);
  6270. }
  6271. }
  6272. parseArrow(node) {
  6273. if (this.match(types$1.colon)) {
  6274. const result = this.tryParse(abort => {
  6275. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  6276. if (this.canInsertSemicolon() || !this.match(types$1.arrow)) abort();
  6277. return returnType;
  6278. });
  6279. if (result.aborted) return;
  6280. if (!result.thrown) {
  6281. if (result.error) this.state = result.failState;
  6282. node.returnType = result.node;
  6283. }
  6284. }
  6285. return super.parseArrow(node);
  6286. }
  6287. parseAssignableListItemTypes(param) {
  6288. if (this.eat(types$1.question)) {
  6289. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  6290. this.raise(param.start, TSErrors.PatternIsOptional);
  6291. }
  6292. param.optional = true;
  6293. }
  6294. const type = this.tsTryParseTypeAnnotation();
  6295. if (type) param.typeAnnotation = type;
  6296. this.resetEndLocation(param);
  6297. return param;
  6298. }
  6299. toAssignable(node, isLHS = false) {
  6300. switch (node.type) {
  6301. case "TSTypeCastExpression":
  6302. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  6303. case "TSParameterProperty":
  6304. return super.toAssignable(node, isLHS);
  6305. case "ParenthesizedExpression":
  6306. return this.toAssignableParenthesizedExpression(node, isLHS);
  6307. case "TSAsExpression":
  6308. case "TSNonNullExpression":
  6309. case "TSTypeAssertion":
  6310. node.expression = this.toAssignable(node.expression, isLHS);
  6311. return node;
  6312. default:
  6313. return super.toAssignable(node, isLHS);
  6314. }
  6315. }
  6316. toAssignableParenthesizedExpression(node, isLHS) {
  6317. switch (node.expression.type) {
  6318. case "TSAsExpression":
  6319. case "TSNonNullExpression":
  6320. case "TSTypeAssertion":
  6321. case "ParenthesizedExpression":
  6322. node.expression = this.toAssignable(node.expression, isLHS);
  6323. return node;
  6324. default:
  6325. return super.toAssignable(node, isLHS);
  6326. }
  6327. }
  6328. checkLVal(expr, contextDescription, ...args) {
  6329. var _expr$extra2;
  6330. switch (expr.type) {
  6331. case "TSTypeCastExpression":
  6332. return;
  6333. case "TSParameterProperty":
  6334. this.checkLVal(expr.parameter, "parameter property", ...args);
  6335. return;
  6336. case "TSAsExpression":
  6337. case "TSTypeAssertion":
  6338. if (!args[0] && contextDescription !== "parenthesized expression" && !((_expr$extra2 = expr.extra) != null && _expr$extra2.parenthesized)) {
  6339. this.raise(expr.start, ErrorMessages.InvalidLhs, contextDescription);
  6340. break;
  6341. }
  6342. this.checkLVal(expr.expression, "parenthesized expression", ...args);
  6343. return;
  6344. case "TSNonNullExpression":
  6345. this.checkLVal(expr.expression, contextDescription, ...args);
  6346. return;
  6347. default:
  6348. super.checkLVal(expr, contextDescription, ...args);
  6349. return;
  6350. }
  6351. }
  6352. parseBindingAtom() {
  6353. switch (this.state.type) {
  6354. case types$1._this:
  6355. return this.parseIdentifier(true);
  6356. default:
  6357. return super.parseBindingAtom();
  6358. }
  6359. }
  6360. parseMaybeDecoratorArguments(expr) {
  6361. if (this.isRelational("<")) {
  6362. const typeArguments = this.tsParseTypeArguments();
  6363. if (this.match(types$1.parenL)) {
  6364. const call = super.parseMaybeDecoratorArguments(expr);
  6365. call.typeParameters = typeArguments;
  6366. return call;
  6367. }
  6368. this.unexpected(this.state.start, types$1.parenL);
  6369. }
  6370. return super.parseMaybeDecoratorArguments(expr);
  6371. }
  6372. checkCommaAfterRest(close) {
  6373. if (this.state.isAmbientContext && this.match(types$1.comma) && this.lookaheadCharCode() === close) {
  6374. this.next();
  6375. } else {
  6376. super.checkCommaAfterRest(close);
  6377. }
  6378. }
  6379. isClassMethod() {
  6380. return this.isRelational("<") || super.isClassMethod();
  6381. }
  6382. isClassProperty() {
  6383. return this.match(types$1.bang) || this.match(types$1.colon) || super.isClassProperty();
  6384. }
  6385. parseMaybeDefault(...args) {
  6386. const node = super.parseMaybeDefault(...args);
  6387. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  6388. this.raise(node.typeAnnotation.start, TSErrors.TypeAnnotationAfterAssign);
  6389. }
  6390. return node;
  6391. }
  6392. getTokenFromCode(code) {
  6393. if (this.state.inType && (code === 62 || code === 60)) {
  6394. return this.finishOp(types$1.relational, 1);
  6395. } else {
  6396. return super.getTokenFromCode(code);
  6397. }
  6398. }
  6399. reScan_lt_gt() {
  6400. if (this.match(types$1.relational)) {
  6401. const code = this.input.charCodeAt(this.state.start);
  6402. if (code === 60 || code === 62) {
  6403. this.state.pos -= 1;
  6404. this.readToken_lt_gt(code);
  6405. }
  6406. }
  6407. }
  6408. toAssignableList(exprList) {
  6409. for (let i = 0; i < exprList.length; i++) {
  6410. const expr = exprList[i];
  6411. if (!expr) continue;
  6412. switch (expr.type) {
  6413. case "TSTypeCastExpression":
  6414. exprList[i] = this.typeCastToParameter(expr);
  6415. break;
  6416. case "TSAsExpression":
  6417. case "TSTypeAssertion":
  6418. if (!this.state.maybeInArrowParameters) {
  6419. exprList[i] = this.typeCastToParameter(expr);
  6420. } else {
  6421. this.raise(expr.start, TSErrors.UnexpectedTypeCastInParameter);
  6422. }
  6423. break;
  6424. }
  6425. }
  6426. return super.toAssignableList(...arguments);
  6427. }
  6428. typeCastToParameter(node) {
  6429. node.expression.typeAnnotation = node.typeAnnotation;
  6430. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  6431. return node.expression;
  6432. }
  6433. shouldParseArrow() {
  6434. return this.match(types$1.colon) || super.shouldParseArrow();
  6435. }
  6436. shouldParseAsyncArrow() {
  6437. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  6438. }
  6439. canHaveLeadingDecorator() {
  6440. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  6441. }
  6442. jsxParseOpeningElementAfterName(node) {
  6443. if (this.isRelational("<")) {
  6444. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  6445. if (typeArguments) node.typeParameters = typeArguments;
  6446. }
  6447. return super.jsxParseOpeningElementAfterName(node);
  6448. }
  6449. getGetterSetterExpectedParamCount(method) {
  6450. const baseCount = super.getGetterSetterExpectedParamCount(method);
  6451. const params = this.getObjectOrClassMethodParams(method);
  6452. const firstParam = params[0];
  6453. const hasContextParam = firstParam && this.isThisParam(firstParam);
  6454. return hasContextParam ? baseCount + 1 : baseCount;
  6455. }
  6456. parseCatchClauseParam() {
  6457. const param = super.parseCatchClauseParam();
  6458. const type = this.tsTryParseTypeAnnotation();
  6459. if (type) {
  6460. param.typeAnnotation = type;
  6461. this.resetEndLocation(param);
  6462. }
  6463. return param;
  6464. }
  6465. tsInAmbientContext(cb) {
  6466. const oldIsAmbientContext = this.state.isAmbientContext;
  6467. this.state.isAmbientContext = true;
  6468. try {
  6469. return cb();
  6470. } finally {
  6471. this.state.isAmbientContext = oldIsAmbientContext;
  6472. }
  6473. }
  6474. parseClass(node, ...args) {
  6475. const oldInAbstractClass = this.state.inAbstractClass;
  6476. this.state.inAbstractClass = !!node.abstract;
  6477. try {
  6478. return super.parseClass(node, ...args);
  6479. } finally {
  6480. this.state.inAbstractClass = oldInAbstractClass;
  6481. }
  6482. }
  6483. tsParseAbstractDeclaration(node) {
  6484. if (this.match(types$1._class)) {
  6485. node.abstract = true;
  6486. return this.parseClass(node, true, false);
  6487. } else if (this.isContextual("interface")) {
  6488. if (!this.hasFollowingLineBreak()) {
  6489. node.abstract = true;
  6490. this.raise(node.start, TSErrors.NonClassMethodPropertyHasAbstractModifer);
  6491. this.next();
  6492. return this.tsParseInterfaceDeclaration(node);
  6493. }
  6494. } else {
  6495. this.unexpected(null, types$1._class);
  6496. }
  6497. }
  6498. parseMethod(...args) {
  6499. const method = super.parseMethod(...args);
  6500. if (method.abstract) {
  6501. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  6502. if (hasBody) {
  6503. const {
  6504. key
  6505. } = method;
  6506. this.raise(method.start, TSErrors.AbstractMethodHasImplementation, key.type === "Identifier" ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  6507. }
  6508. }
  6509. return method;
  6510. }
  6511. shouldParseAsAmbientContext() {
  6512. return !!this.getPluginOption("typescript", "dts");
  6513. }
  6514. parse() {
  6515. if (this.shouldParseAsAmbientContext()) {
  6516. this.state.isAmbientContext = true;
  6517. }
  6518. return super.parse();
  6519. }
  6520. getExpression() {
  6521. if (this.shouldParseAsAmbientContext()) {
  6522. this.state.isAmbientContext = true;
  6523. }
  6524. return super.getExpression();
  6525. }
  6526. });
  6527. types$1.placeholder = new TokenType("%%", {
  6528. startsExpr: true
  6529. });
  6530. const PlaceHolderErrors = makeErrorTemplates({
  6531. ClassNameIsRequired: "A class name is required."
  6532. }, ErrorCodes.SyntaxError);
  6533. var placeholders = (superClass => class extends superClass {
  6534. parsePlaceholder(expectedNode) {
  6535. if (this.match(types$1.placeholder)) {
  6536. const node = this.startNode();
  6537. this.next();
  6538. this.assertNoSpace("Unexpected space in placeholder.");
  6539. node.name = super.parseIdentifier(true);
  6540. this.assertNoSpace("Unexpected space in placeholder.");
  6541. this.expect(types$1.placeholder);
  6542. return this.finishPlaceholder(node, expectedNode);
  6543. }
  6544. }
  6545. finishPlaceholder(node, expectedNode) {
  6546. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  6547. node.expectedNode = expectedNode;
  6548. return isFinished ? node : this.finishNode(node, "Placeholder");
  6549. }
  6550. getTokenFromCode(code) {
  6551. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  6552. return this.finishOp(types$1.placeholder, 2);
  6553. }
  6554. return super.getTokenFromCode(...arguments);
  6555. }
  6556. parseExprAtom() {
  6557. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  6558. }
  6559. parseIdentifier() {
  6560. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  6561. }
  6562. checkReservedWord(word) {
  6563. if (word !== undefined) super.checkReservedWord(...arguments);
  6564. }
  6565. parseBindingAtom() {
  6566. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  6567. }
  6568. checkLVal(expr) {
  6569. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  6570. }
  6571. toAssignable(node) {
  6572. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  6573. node.expectedNode = "Pattern";
  6574. return node;
  6575. }
  6576. return super.toAssignable(...arguments);
  6577. }
  6578. isLet(context) {
  6579. if (super.isLet(context)) {
  6580. return true;
  6581. }
  6582. if (!this.isContextual("let")) {
  6583. return false;
  6584. }
  6585. if (context) return false;
  6586. const nextToken = this.lookahead();
  6587. if (nextToken.type === types$1.placeholder) {
  6588. return true;
  6589. }
  6590. return false;
  6591. }
  6592. verifyBreakContinue(node) {
  6593. if (node.label && node.label.type === "Placeholder") return;
  6594. super.verifyBreakContinue(...arguments);
  6595. }
  6596. parseExpressionStatement(node, expr) {
  6597. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  6598. return super.parseExpressionStatement(...arguments);
  6599. }
  6600. if (this.match(types$1.colon)) {
  6601. const stmt = node;
  6602. stmt.label = this.finishPlaceholder(expr, "Identifier");
  6603. this.next();
  6604. stmt.body = this.parseStatement("label");
  6605. return this.finishNode(stmt, "LabeledStatement");
  6606. }
  6607. this.semicolon();
  6608. node.name = expr.name;
  6609. return this.finishPlaceholder(node, "Statement");
  6610. }
  6611. parseBlock() {
  6612. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  6613. }
  6614. parseFunctionId() {
  6615. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  6616. }
  6617. parseClass(node, isStatement, optionalId) {
  6618. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  6619. this.next();
  6620. this.takeDecorators(node);
  6621. const oldStrict = this.state.strict;
  6622. const placeholder = this.parsePlaceholder("Identifier");
  6623. if (placeholder) {
  6624. if (this.match(types$1._extends) || this.match(types$1.placeholder) || this.match(types$1.braceL)) {
  6625. node.id = placeholder;
  6626. } else if (optionalId || !isStatement) {
  6627. node.id = null;
  6628. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  6629. return this.finishNode(node, type);
  6630. } else {
  6631. this.unexpected(null, PlaceHolderErrors.ClassNameIsRequired);
  6632. }
  6633. } else {
  6634. this.parseClassId(node, isStatement, optionalId);
  6635. }
  6636. this.parseClassSuper(node);
  6637. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  6638. return this.finishNode(node, type);
  6639. }
  6640. parseExport(node) {
  6641. const placeholder = this.parsePlaceholder("Identifier");
  6642. if (!placeholder) return super.parseExport(...arguments);
  6643. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6644. node.specifiers = [];
  6645. node.source = null;
  6646. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  6647. return this.finishNode(node, "ExportNamedDeclaration");
  6648. }
  6649. this.expectPlugin("exportDefaultFrom");
  6650. const specifier = this.startNode();
  6651. specifier.exported = placeholder;
  6652. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  6653. return super.parseExport(node);
  6654. }
  6655. isExportDefaultSpecifier() {
  6656. if (this.match(types$1._default)) {
  6657. const next = this.nextTokenStart();
  6658. if (this.isUnparsedContextual(next, "from")) {
  6659. if (this.input.startsWith(types$1.placeholder.label, this.nextTokenStartSince(next + 4))) {
  6660. return true;
  6661. }
  6662. }
  6663. }
  6664. return super.isExportDefaultSpecifier();
  6665. }
  6666. maybeParseExportDefaultSpecifier(node) {
  6667. if (node.specifiers && node.specifiers.length > 0) {
  6668. return true;
  6669. }
  6670. return super.maybeParseExportDefaultSpecifier(...arguments);
  6671. }
  6672. checkExport(node) {
  6673. const {
  6674. specifiers
  6675. } = node;
  6676. if (specifiers != null && specifiers.length) {
  6677. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  6678. }
  6679. super.checkExport(node);
  6680. node.specifiers = specifiers;
  6681. }
  6682. parseImport(node) {
  6683. const placeholder = this.parsePlaceholder("Identifier");
  6684. if (!placeholder) return super.parseImport(...arguments);
  6685. node.specifiers = [];
  6686. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6687. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  6688. this.semicolon();
  6689. return this.finishNode(node, "ImportDeclaration");
  6690. }
  6691. const specifier = this.startNodeAtNode(placeholder);
  6692. specifier.local = placeholder;
  6693. this.finishNode(specifier, "ImportDefaultSpecifier");
  6694. node.specifiers.push(specifier);
  6695. if (this.eat(types$1.comma)) {
  6696. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  6697. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  6698. }
  6699. this.expectContextual("from");
  6700. node.source = this.parseImportSource();
  6701. this.semicolon();
  6702. return this.finishNode(node, "ImportDeclaration");
  6703. }
  6704. parseImportSource() {
  6705. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  6706. }
  6707. });
  6708. var v8intrinsic = (superClass => class extends superClass {
  6709. parseV8Intrinsic() {
  6710. if (this.match(types$1.modulo)) {
  6711. const v8IntrinsicStart = this.state.start;
  6712. const node = this.startNode();
  6713. this.eat(types$1.modulo);
  6714. if (this.match(types$1.name)) {
  6715. const name = this.parseIdentifierName(this.state.start);
  6716. const identifier = this.createIdentifier(node, name);
  6717. identifier.type = "V8IntrinsicIdentifier";
  6718. if (this.match(types$1.parenL)) {
  6719. return identifier;
  6720. }
  6721. }
  6722. this.unexpected(v8IntrinsicStart);
  6723. }
  6724. }
  6725. parseExprAtom() {
  6726. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  6727. }
  6728. });
  6729. function hasPlugin(plugins, name) {
  6730. return plugins.some(plugin => {
  6731. if (Array.isArray(plugin)) {
  6732. return plugin[0] === name;
  6733. } else {
  6734. return plugin === name;
  6735. }
  6736. });
  6737. }
  6738. function getPluginOption(plugins, name, option) {
  6739. const plugin = plugins.find(plugin => {
  6740. if (Array.isArray(plugin)) {
  6741. return plugin[0] === name;
  6742. } else {
  6743. return plugin === name;
  6744. }
  6745. });
  6746. if (plugin && Array.isArray(plugin)) {
  6747. return plugin[1][option];
  6748. }
  6749. return null;
  6750. }
  6751. const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
  6752. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  6753. function validatePlugins(plugins) {
  6754. if (hasPlugin(plugins, "decorators")) {
  6755. if (hasPlugin(plugins, "decorators-legacy")) {
  6756. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  6757. }
  6758. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  6759. if (decoratorsBeforeExport == null) {
  6760. throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
  6761. } else if (typeof decoratorsBeforeExport !== "boolean") {
  6762. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  6763. }
  6764. }
  6765. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  6766. throw new Error("Cannot combine flow and typescript plugins.");
  6767. }
  6768. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  6769. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  6770. }
  6771. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  6772. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  6773. }
  6774. if (hasPlugin(plugins, "moduleAttributes")) {
  6775. if (hasPlugin(plugins, "importAssertions")) {
  6776. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  6777. }
  6778. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  6779. if (moduleAttributesVerionPluginOption !== "may-2020") {
  6780. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  6781. }
  6782. }
  6783. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  6784. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  6785. }
  6786. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  6787. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  6788. error.missingPlugins = "doExpressions";
  6789. throw error;
  6790. }
  6791. }
  6792. const mixinPlugins = {
  6793. estree,
  6794. jsx,
  6795. flow,
  6796. typescript,
  6797. v8intrinsic,
  6798. placeholders
  6799. };
  6800. const mixinPluginNames = Object.keys(mixinPlugins);
  6801. const defaultOptions = {
  6802. sourceType: "script",
  6803. sourceFilename: undefined,
  6804. startLine: 1,
  6805. allowAwaitOutsideFunction: false,
  6806. allowReturnOutsideFunction: false,
  6807. allowImportExportEverywhere: false,
  6808. allowSuperOutsideMethod: false,
  6809. allowUndeclaredExports: false,
  6810. plugins: [],
  6811. strictMode: null,
  6812. ranges: false,
  6813. tokens: false,
  6814. createParenthesizedExpressions: false,
  6815. errorRecovery: false
  6816. };
  6817. function getOptions(opts) {
  6818. const options = {};
  6819. for (const key of Object.keys(defaultOptions)) {
  6820. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  6821. }
  6822. return options;
  6823. }
  6824. class State {
  6825. constructor() {
  6826. this.strict = void 0;
  6827. this.curLine = void 0;
  6828. this.startLoc = void 0;
  6829. this.endLoc = void 0;
  6830. this.errors = [];
  6831. this.potentialArrowAt = -1;
  6832. this.noArrowAt = [];
  6833. this.noArrowParamsConversionAt = [];
  6834. this.maybeInArrowParameters = false;
  6835. this.inPipeline = false;
  6836. this.inType = false;
  6837. this.noAnonFunctionType = false;
  6838. this.inPropertyName = false;
  6839. this.hasFlowComment = false;
  6840. this.isAmbientContext = false;
  6841. this.inAbstractClass = false;
  6842. this.topicContext = {
  6843. maxNumOfResolvableTopics: 0,
  6844. maxTopicIndex: null
  6845. };
  6846. this.soloAwait = false;
  6847. this.inFSharpPipelineDirectBody = false;
  6848. this.labels = [];
  6849. this.decoratorStack = [[]];
  6850. this.comments = [];
  6851. this.trailingComments = [];
  6852. this.leadingComments = [];
  6853. this.commentStack = [];
  6854. this.commentPreviousNode = null;
  6855. this.pos = 0;
  6856. this.lineStart = 0;
  6857. this.type = types$1.eof;
  6858. this.value = null;
  6859. this.start = 0;
  6860. this.end = 0;
  6861. this.lastTokEndLoc = null;
  6862. this.lastTokStartLoc = null;
  6863. this.lastTokStart = 0;
  6864. this.lastTokEnd = 0;
  6865. this.context = [types.braceStatement];
  6866. this.exprAllowed = true;
  6867. this.containsEsc = false;
  6868. this.strictErrors = new Map();
  6869. this.exportedIdentifiers = [];
  6870. this.tokensLength = 0;
  6871. }
  6872. init(options) {
  6873. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  6874. this.curLine = options.startLine;
  6875. this.startLoc = this.endLoc = this.curPosition();
  6876. }
  6877. curPosition() {
  6878. return new Position(this.curLine, this.pos - this.lineStart);
  6879. }
  6880. clone(skipArrays) {
  6881. const state = new State();
  6882. const keys = Object.keys(this);
  6883. for (let i = 0, length = keys.length; i < length; i++) {
  6884. const key = keys[i];
  6885. let val = this[key];
  6886. if (!skipArrays && Array.isArray(val)) {
  6887. val = val.slice();
  6888. }
  6889. state[key] = val;
  6890. }
  6891. return state;
  6892. }
  6893. }
  6894. var _isDigit = function isDigit(code) {
  6895. return code >= 48 && code <= 57;
  6896. };
  6897. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
  6898. const forbiddenNumericSeparatorSiblings = {
  6899. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  6900. hex: [46, 88, 95, 120]
  6901. };
  6902. const allowedNumericSeparatorSiblings = {};
  6903. allowedNumericSeparatorSiblings.bin = [48, 49];
  6904. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  6905. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  6906. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  6907. class Token {
  6908. constructor(state) {
  6909. this.type = state.type;
  6910. this.value = state.value;
  6911. this.start = state.start;
  6912. this.end = state.end;
  6913. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  6914. }
  6915. }
  6916. class Tokenizer extends ParserError {
  6917. constructor(options, input) {
  6918. super();
  6919. this.isLookahead = void 0;
  6920. this.tokens = [];
  6921. this.state = new State();
  6922. this.state.init(options);
  6923. this.input = input;
  6924. this.length = input.length;
  6925. this.isLookahead = false;
  6926. }
  6927. pushToken(token) {
  6928. this.tokens.length = this.state.tokensLength;
  6929. this.tokens.push(token);
  6930. ++this.state.tokensLength;
  6931. }
  6932. next() {
  6933. if (!this.isLookahead) {
  6934. this.checkKeywordEscapes();
  6935. if (this.options.tokens) {
  6936. this.pushToken(new Token(this.state));
  6937. }
  6938. }
  6939. this.state.lastTokEnd = this.state.end;
  6940. this.state.lastTokStart = this.state.start;
  6941. this.state.lastTokEndLoc = this.state.endLoc;
  6942. this.state.lastTokStartLoc = this.state.startLoc;
  6943. this.nextToken();
  6944. }
  6945. eat(type) {
  6946. if (this.match(type)) {
  6947. this.next();
  6948. return true;
  6949. } else {
  6950. return false;
  6951. }
  6952. }
  6953. match(type) {
  6954. return this.state.type === type;
  6955. }
  6956. lookahead() {
  6957. const old = this.state;
  6958. this.state = old.clone(true);
  6959. this.isLookahead = true;
  6960. this.next();
  6961. this.isLookahead = false;
  6962. const curr = this.state;
  6963. this.state = old;
  6964. return curr;
  6965. }
  6966. nextTokenStart() {
  6967. return this.nextTokenStartSince(this.state.pos);
  6968. }
  6969. nextTokenStartSince(pos) {
  6970. skipWhiteSpace.lastIndex = pos;
  6971. const skip = skipWhiteSpace.exec(this.input);
  6972. return pos + skip[0].length;
  6973. }
  6974. lookaheadCharCode() {
  6975. return this.input.charCodeAt(this.nextTokenStart());
  6976. }
  6977. codePointAtPos(pos) {
  6978. let cp = this.input.charCodeAt(pos);
  6979. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  6980. const trail = this.input.charCodeAt(pos);
  6981. if ((trail & 0xfc00) === 0xdc00) {
  6982. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  6983. }
  6984. }
  6985. return cp;
  6986. }
  6987. setStrict(strict) {
  6988. this.state.strict = strict;
  6989. if (strict) {
  6990. this.state.strictErrors.forEach((message, pos) => this.raise(pos, message));
  6991. this.state.strictErrors.clear();
  6992. }
  6993. }
  6994. curContext() {
  6995. return this.state.context[this.state.context.length - 1];
  6996. }
  6997. nextToken() {
  6998. const curContext = this.curContext();
  6999. if (!(curContext != null && curContext.preserveSpace)) this.skipSpace();
  7000. this.state.start = this.state.pos;
  7001. this.state.startLoc = this.state.curPosition();
  7002. if (this.state.pos >= this.length) {
  7003. this.finishToken(types$1.eof);
  7004. return;
  7005. }
  7006. const override = curContext == null ? void 0 : curContext.override;
  7007. if (override) {
  7008. override(this);
  7009. } else {
  7010. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  7011. }
  7012. }
  7013. pushComment(block, text, start, end, startLoc, endLoc) {
  7014. const comment = {
  7015. type: block ? "CommentBlock" : "CommentLine",
  7016. value: text,
  7017. start: start,
  7018. end: end,
  7019. loc: new SourceLocation(startLoc, endLoc)
  7020. };
  7021. if (this.options.tokens) this.pushToken(comment);
  7022. this.state.comments.push(comment);
  7023. this.addComment(comment);
  7024. }
  7025. skipBlockComment() {
  7026. const startLoc = this.state.curPosition();
  7027. const start = this.state.pos;
  7028. const end = this.input.indexOf("*/", this.state.pos + 2);
  7029. if (end === -1) throw this.raise(start, ErrorMessages.UnterminatedComment);
  7030. this.state.pos = end + 2;
  7031. lineBreakG.lastIndex = start;
  7032. let match;
  7033. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  7034. ++this.state.curLine;
  7035. this.state.lineStart = match.index + match[0].length;
  7036. }
  7037. if (this.isLookahead) return;
  7038. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  7039. }
  7040. skipLineComment(startSkip) {
  7041. const start = this.state.pos;
  7042. const startLoc = this.state.curPosition();
  7043. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  7044. if (this.state.pos < this.length) {
  7045. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7046. ch = this.input.charCodeAt(this.state.pos);
  7047. }
  7048. }
  7049. if (this.isLookahead) return;
  7050. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  7051. }
  7052. skipSpace() {
  7053. loop: while (this.state.pos < this.length) {
  7054. const ch = this.input.charCodeAt(this.state.pos);
  7055. switch (ch) {
  7056. case 32:
  7057. case 160:
  7058. case 9:
  7059. ++this.state.pos;
  7060. break;
  7061. case 13:
  7062. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  7063. ++this.state.pos;
  7064. }
  7065. case 10:
  7066. case 8232:
  7067. case 8233:
  7068. ++this.state.pos;
  7069. ++this.state.curLine;
  7070. this.state.lineStart = this.state.pos;
  7071. break;
  7072. case 47:
  7073. switch (this.input.charCodeAt(this.state.pos + 1)) {
  7074. case 42:
  7075. this.skipBlockComment();
  7076. break;
  7077. case 47:
  7078. this.skipLineComment(2);
  7079. break;
  7080. default:
  7081. break loop;
  7082. }
  7083. break;
  7084. default:
  7085. if (isWhitespace(ch)) {
  7086. ++this.state.pos;
  7087. } else {
  7088. break loop;
  7089. }
  7090. }
  7091. }
  7092. }
  7093. finishToken(type, val) {
  7094. this.state.end = this.state.pos;
  7095. this.state.endLoc = this.state.curPosition();
  7096. const prevType = this.state.type;
  7097. this.state.type = type;
  7098. this.state.value = val;
  7099. if (!this.isLookahead) this.updateContext(prevType);
  7100. }
  7101. readToken_numberSign() {
  7102. if (this.state.pos === 0 && this.readToken_interpreter()) {
  7103. return;
  7104. }
  7105. const nextPos = this.state.pos + 1;
  7106. const next = this.codePointAtPos(nextPos);
  7107. if (next >= 48 && next <= 57) {
  7108. throw this.raise(this.state.pos, ErrorMessages.UnexpectedDigitAfterHash);
  7109. }
  7110. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  7111. this.expectPlugin("recordAndTuple");
  7112. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  7113. throw this.raise(this.state.pos, next === 123 ? ErrorMessages.RecordExpressionHashIncorrectStartSyntaxType : ErrorMessages.TupleExpressionHashIncorrectStartSyntaxType);
  7114. }
  7115. if (next === 123) {
  7116. this.finishToken(types$1.braceHashL);
  7117. } else {
  7118. this.finishToken(types$1.bracketHashL);
  7119. }
  7120. this.state.pos += 2;
  7121. } else if (isIdentifierStart(next)) {
  7122. ++this.state.pos;
  7123. this.finishToken(types$1.privateName, this.readWord1(next));
  7124. } else if (next === 92) {
  7125. ++this.state.pos;
  7126. this.finishToken(types$1.privateName, this.readWord1());
  7127. } else {
  7128. this.finishOp(types$1.hash, 1);
  7129. }
  7130. }
  7131. readToken_dot() {
  7132. const next = this.input.charCodeAt(this.state.pos + 1);
  7133. if (next >= 48 && next <= 57) {
  7134. this.readNumber(true);
  7135. return;
  7136. }
  7137. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  7138. this.state.pos += 3;
  7139. this.finishToken(types$1.ellipsis);
  7140. } else {
  7141. ++this.state.pos;
  7142. this.finishToken(types$1.dot);
  7143. }
  7144. }
  7145. readToken_slash() {
  7146. if (this.state.exprAllowed && !this.state.inType) {
  7147. ++this.state.pos;
  7148. this.readRegexp();
  7149. return;
  7150. }
  7151. const next = this.input.charCodeAt(this.state.pos + 1);
  7152. if (next === 61) {
  7153. this.finishOp(types$1.assign, 2);
  7154. } else {
  7155. this.finishOp(types$1.slash, 1);
  7156. }
  7157. }
  7158. readToken_interpreter() {
  7159. if (this.state.pos !== 0 || this.length < 2) return false;
  7160. let ch = this.input.charCodeAt(this.state.pos + 1);
  7161. if (ch !== 33) return false;
  7162. const start = this.state.pos;
  7163. this.state.pos += 1;
  7164. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7165. ch = this.input.charCodeAt(this.state.pos);
  7166. }
  7167. const value = this.input.slice(start + 2, this.state.pos);
  7168. this.finishToken(types$1.interpreterDirective, value);
  7169. return true;
  7170. }
  7171. readToken_mult_modulo(code) {
  7172. let type = code === 42 ? types$1.star : types$1.modulo;
  7173. let width = 1;
  7174. let next = this.input.charCodeAt(this.state.pos + 1);
  7175. const exprAllowed = this.state.exprAllowed;
  7176. if (code === 42 && next === 42) {
  7177. width++;
  7178. next = this.input.charCodeAt(this.state.pos + 2);
  7179. type = types$1.exponent;
  7180. }
  7181. if (next === 61 && !exprAllowed) {
  7182. width++;
  7183. type = types$1.assign;
  7184. }
  7185. this.finishOp(type, width);
  7186. }
  7187. readToken_pipe_amp(code) {
  7188. const next = this.input.charCodeAt(this.state.pos + 1);
  7189. if (next === code) {
  7190. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  7191. this.finishOp(types$1.assign, 3);
  7192. } else {
  7193. this.finishOp(code === 124 ? types$1.logicalOR : types$1.logicalAND, 2);
  7194. }
  7195. return;
  7196. }
  7197. if (code === 124) {
  7198. if (next === 62) {
  7199. this.finishOp(types$1.pipeline, 2);
  7200. return;
  7201. }
  7202. if (this.hasPlugin("recordAndTuple") && next === 125) {
  7203. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7204. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectEndSyntaxType);
  7205. }
  7206. this.finishOp(types$1.braceBarR, 2);
  7207. return;
  7208. }
  7209. if (this.hasPlugin("recordAndTuple") && next === 93) {
  7210. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7211. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectEndSyntaxType);
  7212. }
  7213. this.finishOp(types$1.bracketBarR, 2);
  7214. return;
  7215. }
  7216. }
  7217. if (next === 61) {
  7218. this.finishOp(types$1.assign, 2);
  7219. return;
  7220. }
  7221. this.finishOp(code === 124 ? types$1.bitwiseOR : types$1.bitwiseAND, 1);
  7222. }
  7223. readToken_caret() {
  7224. const next = this.input.charCodeAt(this.state.pos + 1);
  7225. if (next === 61) {
  7226. this.finishOp(types$1.assign, 2);
  7227. } else {
  7228. this.finishOp(types$1.bitwiseXOR, 1);
  7229. }
  7230. }
  7231. readToken_plus_min(code) {
  7232. const next = this.input.charCodeAt(this.state.pos + 1);
  7233. if (next === code) {
  7234. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || this.hasPrecedingLineBreak())) {
  7235. this.skipLineComment(3);
  7236. this.skipSpace();
  7237. this.nextToken();
  7238. return;
  7239. }
  7240. this.finishOp(types$1.incDec, 2);
  7241. return;
  7242. }
  7243. if (next === 61) {
  7244. this.finishOp(types$1.assign, 2);
  7245. } else {
  7246. this.finishOp(types$1.plusMin, 1);
  7247. }
  7248. }
  7249. readToken_lt_gt(code) {
  7250. const next = this.input.charCodeAt(this.state.pos + 1);
  7251. let size = 1;
  7252. if (next === code) {
  7253. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  7254. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  7255. this.finishOp(types$1.assign, size + 1);
  7256. return;
  7257. }
  7258. this.finishOp(types$1.bitShift, size);
  7259. return;
  7260. }
  7261. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  7262. this.skipLineComment(4);
  7263. this.skipSpace();
  7264. this.nextToken();
  7265. return;
  7266. }
  7267. if (next === 61) {
  7268. size = 2;
  7269. }
  7270. this.finishOp(types$1.relational, size);
  7271. }
  7272. readToken_eq_excl(code) {
  7273. const next = this.input.charCodeAt(this.state.pos + 1);
  7274. if (next === 61) {
  7275. this.finishOp(types$1.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  7276. return;
  7277. }
  7278. if (code === 61 && next === 62) {
  7279. this.state.pos += 2;
  7280. this.finishToken(types$1.arrow);
  7281. return;
  7282. }
  7283. this.finishOp(code === 61 ? types$1.eq : types$1.bang, 1);
  7284. }
  7285. readToken_question() {
  7286. const next = this.input.charCodeAt(this.state.pos + 1);
  7287. const next2 = this.input.charCodeAt(this.state.pos + 2);
  7288. if (next === 63) {
  7289. if (next2 === 61) {
  7290. this.finishOp(types$1.assign, 3);
  7291. } else {
  7292. this.finishOp(types$1.nullishCoalescing, 2);
  7293. }
  7294. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  7295. this.state.pos += 2;
  7296. this.finishToken(types$1.questionDot);
  7297. } else {
  7298. ++this.state.pos;
  7299. this.finishToken(types$1.question);
  7300. }
  7301. }
  7302. getTokenFromCode(code) {
  7303. switch (code) {
  7304. case 46:
  7305. this.readToken_dot();
  7306. return;
  7307. case 40:
  7308. ++this.state.pos;
  7309. this.finishToken(types$1.parenL);
  7310. return;
  7311. case 41:
  7312. ++this.state.pos;
  7313. this.finishToken(types$1.parenR);
  7314. return;
  7315. case 59:
  7316. ++this.state.pos;
  7317. this.finishToken(types$1.semi);
  7318. return;
  7319. case 44:
  7320. ++this.state.pos;
  7321. this.finishToken(types$1.comma);
  7322. return;
  7323. case 91:
  7324. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7325. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7326. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectStartSyntaxType);
  7327. }
  7328. this.finishToken(types$1.bracketBarL);
  7329. this.state.pos += 2;
  7330. } else {
  7331. ++this.state.pos;
  7332. this.finishToken(types$1.bracketL);
  7333. }
  7334. return;
  7335. case 93:
  7336. ++this.state.pos;
  7337. this.finishToken(types$1.bracketR);
  7338. return;
  7339. case 123:
  7340. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7341. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7342. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectStartSyntaxType);
  7343. }
  7344. this.finishToken(types$1.braceBarL);
  7345. this.state.pos += 2;
  7346. } else {
  7347. ++this.state.pos;
  7348. this.finishToken(types$1.braceL);
  7349. }
  7350. return;
  7351. case 125:
  7352. ++this.state.pos;
  7353. this.finishToken(types$1.braceR);
  7354. return;
  7355. case 58:
  7356. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  7357. this.finishOp(types$1.doubleColon, 2);
  7358. } else {
  7359. ++this.state.pos;
  7360. this.finishToken(types$1.colon);
  7361. }
  7362. return;
  7363. case 63:
  7364. this.readToken_question();
  7365. return;
  7366. case 96:
  7367. ++this.state.pos;
  7368. this.finishToken(types$1.backQuote);
  7369. return;
  7370. case 48:
  7371. {
  7372. const next = this.input.charCodeAt(this.state.pos + 1);
  7373. if (next === 120 || next === 88) {
  7374. this.readRadixNumber(16);
  7375. return;
  7376. }
  7377. if (next === 111 || next === 79) {
  7378. this.readRadixNumber(8);
  7379. return;
  7380. }
  7381. if (next === 98 || next === 66) {
  7382. this.readRadixNumber(2);
  7383. return;
  7384. }
  7385. }
  7386. case 49:
  7387. case 50:
  7388. case 51:
  7389. case 52:
  7390. case 53:
  7391. case 54:
  7392. case 55:
  7393. case 56:
  7394. case 57:
  7395. this.readNumber(false);
  7396. return;
  7397. case 34:
  7398. case 39:
  7399. this.readString(code);
  7400. return;
  7401. case 47:
  7402. this.readToken_slash();
  7403. return;
  7404. case 37:
  7405. case 42:
  7406. this.readToken_mult_modulo(code);
  7407. return;
  7408. case 124:
  7409. case 38:
  7410. this.readToken_pipe_amp(code);
  7411. return;
  7412. case 94:
  7413. this.readToken_caret();
  7414. return;
  7415. case 43:
  7416. case 45:
  7417. this.readToken_plus_min(code);
  7418. return;
  7419. case 60:
  7420. case 62:
  7421. this.readToken_lt_gt(code);
  7422. return;
  7423. case 61:
  7424. case 33:
  7425. this.readToken_eq_excl(code);
  7426. return;
  7427. case 126:
  7428. this.finishOp(types$1.tilde, 1);
  7429. return;
  7430. case 64:
  7431. ++this.state.pos;
  7432. this.finishToken(types$1.at);
  7433. return;
  7434. case 35:
  7435. this.readToken_numberSign();
  7436. return;
  7437. case 92:
  7438. this.readWord();
  7439. return;
  7440. default:
  7441. if (isIdentifierStart(code)) {
  7442. this.readWord(code);
  7443. return;
  7444. }
  7445. }
  7446. throw this.raise(this.state.pos, ErrorMessages.InvalidOrUnexpectedToken, String.fromCodePoint(code));
  7447. }
  7448. finishOp(type, size) {
  7449. const str = this.input.slice(this.state.pos, this.state.pos + size);
  7450. this.state.pos += size;
  7451. this.finishToken(type, str);
  7452. }
  7453. readRegexp() {
  7454. const start = this.state.pos;
  7455. let escaped, inClass;
  7456. for (;;) {
  7457. if (this.state.pos >= this.length) {
  7458. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7459. }
  7460. const ch = this.input.charAt(this.state.pos);
  7461. if (lineBreak.test(ch)) {
  7462. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7463. }
  7464. if (escaped) {
  7465. escaped = false;
  7466. } else {
  7467. if (ch === "[") {
  7468. inClass = true;
  7469. } else if (ch === "]" && inClass) {
  7470. inClass = false;
  7471. } else if (ch === "/" && !inClass) {
  7472. break;
  7473. }
  7474. escaped = ch === "\\";
  7475. }
  7476. ++this.state.pos;
  7477. }
  7478. const content = this.input.slice(start, this.state.pos);
  7479. ++this.state.pos;
  7480. let mods = "";
  7481. while (this.state.pos < this.length) {
  7482. const char = this.input[this.state.pos];
  7483. const charCode = this.codePointAtPos(this.state.pos);
  7484. if (VALID_REGEX_FLAGS.has(char)) {
  7485. if (mods.indexOf(char) > -1) {
  7486. this.raise(this.state.pos + 1, ErrorMessages.DuplicateRegExpFlags);
  7487. }
  7488. } else if (isIdentifierChar(charCode) || charCode === 92) {
  7489. this.raise(this.state.pos + 1, ErrorMessages.MalformedRegExpFlags);
  7490. } else {
  7491. break;
  7492. }
  7493. ++this.state.pos;
  7494. mods += char;
  7495. }
  7496. this.finishToken(types$1.regexp, {
  7497. pattern: content,
  7498. flags: mods
  7499. });
  7500. }
  7501. readInt(radix, len, forceLen, allowNumSeparator = true) {
  7502. const start = this.state.pos;
  7503. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  7504. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  7505. let invalid = false;
  7506. let total = 0;
  7507. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  7508. const code = this.input.charCodeAt(this.state.pos);
  7509. let val;
  7510. if (code === 95) {
  7511. const prev = this.input.charCodeAt(this.state.pos - 1);
  7512. const next = this.input.charCodeAt(this.state.pos + 1);
  7513. if (allowedSiblings.indexOf(next) === -1) {
  7514. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7515. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  7516. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7517. }
  7518. if (!allowNumSeparator) {
  7519. this.raise(this.state.pos, ErrorMessages.NumericSeparatorInEscapeSequence);
  7520. }
  7521. ++this.state.pos;
  7522. continue;
  7523. }
  7524. if (code >= 97) {
  7525. val = code - 97 + 10;
  7526. } else if (code >= 65) {
  7527. val = code - 65 + 10;
  7528. } else if (_isDigit(code)) {
  7529. val = code - 48;
  7530. } else {
  7531. val = Infinity;
  7532. }
  7533. if (val >= radix) {
  7534. if (this.options.errorRecovery && val <= 9) {
  7535. val = 0;
  7536. this.raise(this.state.start + i + 2, ErrorMessages.InvalidDigit, radix);
  7537. } else if (forceLen) {
  7538. val = 0;
  7539. invalid = true;
  7540. } else {
  7541. break;
  7542. }
  7543. }
  7544. ++this.state.pos;
  7545. total = total * radix + val;
  7546. }
  7547. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  7548. return null;
  7549. }
  7550. return total;
  7551. }
  7552. readRadixNumber(radix) {
  7553. const start = this.state.pos;
  7554. let isBigInt = false;
  7555. this.state.pos += 2;
  7556. const val = this.readInt(radix);
  7557. if (val == null) {
  7558. this.raise(this.state.start + 2, ErrorMessages.InvalidDigit, radix);
  7559. }
  7560. const next = this.input.charCodeAt(this.state.pos);
  7561. if (next === 110) {
  7562. ++this.state.pos;
  7563. isBigInt = true;
  7564. } else if (next === 109) {
  7565. throw this.raise(start, ErrorMessages.InvalidDecimal);
  7566. }
  7567. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7568. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7569. }
  7570. if (isBigInt) {
  7571. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  7572. this.finishToken(types$1.bigint, str);
  7573. return;
  7574. }
  7575. this.finishToken(types$1.num, val);
  7576. }
  7577. readNumber(startsWithDot) {
  7578. const start = this.state.pos;
  7579. let isFloat = false;
  7580. let isBigInt = false;
  7581. let isDecimal = false;
  7582. let hasExponent = false;
  7583. let isOctal = false;
  7584. if (!startsWithDot && this.readInt(10) === null) {
  7585. this.raise(start, ErrorMessages.InvalidNumber);
  7586. }
  7587. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  7588. if (hasLeadingZero) {
  7589. const integer = this.input.slice(start, this.state.pos);
  7590. this.recordStrictModeErrors(start, ErrorMessages.StrictOctalLiteral);
  7591. if (!this.state.strict) {
  7592. const underscorePos = integer.indexOf("_");
  7593. if (underscorePos > 0) {
  7594. this.raise(underscorePos + start, ErrorMessages.ZeroDigitNumericSeparator);
  7595. }
  7596. }
  7597. isOctal = hasLeadingZero && !/[89]/.test(integer);
  7598. }
  7599. let next = this.input.charCodeAt(this.state.pos);
  7600. if (next === 46 && !isOctal) {
  7601. ++this.state.pos;
  7602. this.readInt(10);
  7603. isFloat = true;
  7604. next = this.input.charCodeAt(this.state.pos);
  7605. }
  7606. if ((next === 69 || next === 101) && !isOctal) {
  7607. next = this.input.charCodeAt(++this.state.pos);
  7608. if (next === 43 || next === 45) {
  7609. ++this.state.pos;
  7610. }
  7611. if (this.readInt(10) === null) {
  7612. this.raise(start, ErrorMessages.InvalidOrMissingExponent);
  7613. }
  7614. isFloat = true;
  7615. hasExponent = true;
  7616. next = this.input.charCodeAt(this.state.pos);
  7617. }
  7618. if (next === 110) {
  7619. if (isFloat || hasLeadingZero) {
  7620. this.raise(start, ErrorMessages.InvalidBigIntLiteral);
  7621. }
  7622. ++this.state.pos;
  7623. isBigInt = true;
  7624. }
  7625. if (next === 109) {
  7626. this.expectPlugin("decimal", this.state.pos);
  7627. if (hasExponent || hasLeadingZero) {
  7628. this.raise(start, ErrorMessages.InvalidDecimal);
  7629. }
  7630. ++this.state.pos;
  7631. isDecimal = true;
  7632. }
  7633. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7634. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7635. }
  7636. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  7637. if (isBigInt) {
  7638. this.finishToken(types$1.bigint, str);
  7639. return;
  7640. }
  7641. if (isDecimal) {
  7642. this.finishToken(types$1.decimal, str);
  7643. return;
  7644. }
  7645. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  7646. this.finishToken(types$1.num, val);
  7647. }
  7648. readCodePoint(throwOnInvalid) {
  7649. const ch = this.input.charCodeAt(this.state.pos);
  7650. let code;
  7651. if (ch === 123) {
  7652. const codePos = ++this.state.pos;
  7653. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  7654. ++this.state.pos;
  7655. if (code !== null && code > 0x10ffff) {
  7656. if (throwOnInvalid) {
  7657. this.raise(codePos, ErrorMessages.InvalidCodePoint);
  7658. } else {
  7659. return null;
  7660. }
  7661. }
  7662. } else {
  7663. code = this.readHexChar(4, false, throwOnInvalid);
  7664. }
  7665. return code;
  7666. }
  7667. readString(quote) {
  7668. let out = "",
  7669. chunkStart = ++this.state.pos;
  7670. for (;;) {
  7671. if (this.state.pos >= this.length) {
  7672. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7673. }
  7674. const ch = this.input.charCodeAt(this.state.pos);
  7675. if (ch === quote) break;
  7676. if (ch === 92) {
  7677. out += this.input.slice(chunkStart, this.state.pos);
  7678. out += this.readEscapedChar(false);
  7679. chunkStart = this.state.pos;
  7680. } else if (ch === 8232 || ch === 8233) {
  7681. ++this.state.pos;
  7682. ++this.state.curLine;
  7683. this.state.lineStart = this.state.pos;
  7684. } else if (isNewLine(ch)) {
  7685. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7686. } else {
  7687. ++this.state.pos;
  7688. }
  7689. }
  7690. out += this.input.slice(chunkStart, this.state.pos++);
  7691. this.finishToken(types$1.string, out);
  7692. }
  7693. readTmplToken() {
  7694. let out = "",
  7695. chunkStart = this.state.pos,
  7696. containsInvalid = false;
  7697. for (;;) {
  7698. if (this.state.pos >= this.length) {
  7699. throw this.raise(this.state.start, ErrorMessages.UnterminatedTemplate);
  7700. }
  7701. const ch = this.input.charCodeAt(this.state.pos);
  7702. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  7703. if (this.state.pos === this.state.start && this.match(types$1.template)) {
  7704. if (ch === 36) {
  7705. this.state.pos += 2;
  7706. this.finishToken(types$1.dollarBraceL);
  7707. return;
  7708. } else {
  7709. ++this.state.pos;
  7710. this.finishToken(types$1.backQuote);
  7711. return;
  7712. }
  7713. }
  7714. out += this.input.slice(chunkStart, this.state.pos);
  7715. this.finishToken(types$1.template, containsInvalid ? null : out);
  7716. return;
  7717. }
  7718. if (ch === 92) {
  7719. out += this.input.slice(chunkStart, this.state.pos);
  7720. const escaped = this.readEscapedChar(true);
  7721. if (escaped === null) {
  7722. containsInvalid = true;
  7723. } else {
  7724. out += escaped;
  7725. }
  7726. chunkStart = this.state.pos;
  7727. } else if (isNewLine(ch)) {
  7728. out += this.input.slice(chunkStart, this.state.pos);
  7729. ++this.state.pos;
  7730. switch (ch) {
  7731. case 13:
  7732. if (this.input.charCodeAt(this.state.pos) === 10) {
  7733. ++this.state.pos;
  7734. }
  7735. case 10:
  7736. out += "\n";
  7737. break;
  7738. default:
  7739. out += String.fromCharCode(ch);
  7740. break;
  7741. }
  7742. ++this.state.curLine;
  7743. this.state.lineStart = this.state.pos;
  7744. chunkStart = this.state.pos;
  7745. } else {
  7746. ++this.state.pos;
  7747. }
  7748. }
  7749. }
  7750. recordStrictModeErrors(pos, message) {
  7751. if (this.state.strict && !this.state.strictErrors.has(pos)) {
  7752. this.raise(pos, message);
  7753. } else {
  7754. this.state.strictErrors.set(pos, message);
  7755. }
  7756. }
  7757. readEscapedChar(inTemplate) {
  7758. const throwOnInvalid = !inTemplate;
  7759. const ch = this.input.charCodeAt(++this.state.pos);
  7760. ++this.state.pos;
  7761. switch (ch) {
  7762. case 110:
  7763. return "\n";
  7764. case 114:
  7765. return "\r";
  7766. case 120:
  7767. {
  7768. const code = this.readHexChar(2, false, throwOnInvalid);
  7769. return code === null ? null : String.fromCharCode(code);
  7770. }
  7771. case 117:
  7772. {
  7773. const code = this.readCodePoint(throwOnInvalid);
  7774. return code === null ? null : String.fromCodePoint(code);
  7775. }
  7776. case 116:
  7777. return "\t";
  7778. case 98:
  7779. return "\b";
  7780. case 118:
  7781. return "\u000b";
  7782. case 102:
  7783. return "\f";
  7784. case 13:
  7785. if (this.input.charCodeAt(this.state.pos) === 10) {
  7786. ++this.state.pos;
  7787. }
  7788. case 10:
  7789. this.state.lineStart = this.state.pos;
  7790. ++this.state.curLine;
  7791. case 8232:
  7792. case 8233:
  7793. return "";
  7794. case 56:
  7795. case 57:
  7796. if (inTemplate) {
  7797. return null;
  7798. } else {
  7799. this.recordStrictModeErrors(this.state.pos - 1, ErrorMessages.StrictNumericEscape);
  7800. }
  7801. default:
  7802. if (ch >= 48 && ch <= 55) {
  7803. const codePos = this.state.pos - 1;
  7804. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  7805. let octalStr = match[0];
  7806. let octal = parseInt(octalStr, 8);
  7807. if (octal > 255) {
  7808. octalStr = octalStr.slice(0, -1);
  7809. octal = parseInt(octalStr, 8);
  7810. }
  7811. this.state.pos += octalStr.length - 1;
  7812. const next = this.input.charCodeAt(this.state.pos);
  7813. if (octalStr !== "0" || next === 56 || next === 57) {
  7814. if (inTemplate) {
  7815. return null;
  7816. } else {
  7817. this.recordStrictModeErrors(codePos, ErrorMessages.StrictNumericEscape);
  7818. }
  7819. }
  7820. return String.fromCharCode(octal);
  7821. }
  7822. return String.fromCharCode(ch);
  7823. }
  7824. }
  7825. readHexChar(len, forceLen, throwOnInvalid) {
  7826. const codePos = this.state.pos;
  7827. const n = this.readInt(16, len, forceLen, false);
  7828. if (n === null) {
  7829. if (throwOnInvalid) {
  7830. this.raise(codePos, ErrorMessages.InvalidEscapeSequence);
  7831. } else {
  7832. this.state.pos = codePos - 1;
  7833. }
  7834. }
  7835. return n;
  7836. }
  7837. readWord1(firstCode) {
  7838. this.state.containsEsc = false;
  7839. let word = "";
  7840. const start = this.state.pos;
  7841. let chunkStart = this.state.pos;
  7842. if (firstCode !== undefined) {
  7843. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  7844. }
  7845. while (this.state.pos < this.length) {
  7846. const ch = this.codePointAtPos(this.state.pos);
  7847. if (isIdentifierChar(ch)) {
  7848. this.state.pos += ch <= 0xffff ? 1 : 2;
  7849. } else if (ch === 92) {
  7850. this.state.containsEsc = true;
  7851. word += this.input.slice(chunkStart, this.state.pos);
  7852. const escStart = this.state.pos;
  7853. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  7854. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  7855. this.raise(this.state.pos, ErrorMessages.MissingUnicodeEscape);
  7856. chunkStart = this.state.pos - 1;
  7857. continue;
  7858. }
  7859. ++this.state.pos;
  7860. const esc = this.readCodePoint(true);
  7861. if (esc !== null) {
  7862. if (!identifierCheck(esc)) {
  7863. this.raise(escStart, ErrorMessages.EscapedCharNotAnIdentifier);
  7864. }
  7865. word += String.fromCodePoint(esc);
  7866. }
  7867. chunkStart = this.state.pos;
  7868. } else {
  7869. break;
  7870. }
  7871. }
  7872. return word + this.input.slice(chunkStart, this.state.pos);
  7873. }
  7874. readWord(firstCode) {
  7875. const word = this.readWord1(firstCode);
  7876. const type = keywords$1.get(word) || types$1.name;
  7877. this.finishToken(type, word);
  7878. }
  7879. checkKeywordEscapes() {
  7880. const kw = this.state.type.keyword;
  7881. if (kw && this.state.containsEsc) {
  7882. this.raise(this.state.start, ErrorMessages.InvalidEscapedReservedWord, kw);
  7883. }
  7884. }
  7885. braceIsBlock(prevType) {
  7886. const parent = this.curContext();
  7887. if (parent === types.functionExpression || parent === types.functionStatement) {
  7888. return true;
  7889. }
  7890. if (prevType === types$1.colon && (parent === types.braceStatement || parent === types.braceExpression)) {
  7891. return !parent.isExpr;
  7892. }
  7893. if (prevType === types$1._return || prevType === types$1.name && this.state.exprAllowed) {
  7894. return this.hasPrecedingLineBreak();
  7895. }
  7896. if (prevType === types$1._else || prevType === types$1.semi || prevType === types$1.eof || prevType === types$1.parenR || prevType === types$1.arrow) {
  7897. return true;
  7898. }
  7899. if (prevType === types$1.braceL) {
  7900. return parent === types.braceStatement;
  7901. }
  7902. if (prevType === types$1._var || prevType === types$1._const || prevType === types$1.name) {
  7903. return false;
  7904. }
  7905. if (prevType === types$1.relational) {
  7906. return true;
  7907. }
  7908. return !this.state.exprAllowed;
  7909. }
  7910. updateContext(prevType) {
  7911. const type = this.state.type;
  7912. let update;
  7913. if (type.keyword && (prevType === types$1.dot || prevType === types$1.questionDot)) {
  7914. this.state.exprAllowed = false;
  7915. } else if (update = type.updateContext) {
  7916. update.call(this, prevType);
  7917. } else {
  7918. this.state.exprAllowed = type.beforeExpr;
  7919. }
  7920. }
  7921. }
  7922. class ClassScope {
  7923. constructor() {
  7924. this.privateNames = new Set();
  7925. this.loneAccessors = new Map();
  7926. this.undefinedPrivateNames = new Map();
  7927. }
  7928. }
  7929. class ClassScopeHandler {
  7930. constructor(raise) {
  7931. this.stack = [];
  7932. this.undefinedPrivateNames = new Map();
  7933. this.raise = raise;
  7934. }
  7935. current() {
  7936. return this.stack[this.stack.length - 1];
  7937. }
  7938. enter() {
  7939. this.stack.push(new ClassScope());
  7940. }
  7941. exit() {
  7942. const oldClassScope = this.stack.pop();
  7943. const current = this.current();
  7944. for (const [name, pos] of Array.from(oldClassScope.undefinedPrivateNames)) {
  7945. if (current) {
  7946. if (!current.undefinedPrivateNames.has(name)) {
  7947. current.undefinedPrivateNames.set(name, pos);
  7948. }
  7949. } else {
  7950. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7951. }
  7952. }
  7953. }
  7954. declarePrivateName(name, elementType, pos) {
  7955. const classScope = this.current();
  7956. let redefined = classScope.privateNames.has(name);
  7957. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  7958. const accessor = redefined && classScope.loneAccessors.get(name);
  7959. if (accessor) {
  7960. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  7961. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  7962. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  7963. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  7964. redefined = oldKind === newKind || oldStatic !== newStatic;
  7965. if (!redefined) classScope.loneAccessors.delete(name);
  7966. } else if (!redefined) {
  7967. classScope.loneAccessors.set(name, elementType);
  7968. }
  7969. }
  7970. if (redefined) {
  7971. this.raise(pos, ErrorMessages.PrivateNameRedeclaration, name);
  7972. }
  7973. classScope.privateNames.add(name);
  7974. classScope.undefinedPrivateNames.delete(name);
  7975. }
  7976. usePrivateName(name, pos) {
  7977. let classScope;
  7978. for (classScope of this.stack) {
  7979. if (classScope.privateNames.has(name)) return;
  7980. }
  7981. if (classScope) {
  7982. classScope.undefinedPrivateNames.set(name, pos);
  7983. } else {
  7984. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7985. }
  7986. }
  7987. }
  7988. const kExpression = 0,
  7989. kMaybeArrowParameterDeclaration = 1,
  7990. kMaybeAsyncArrowParameterDeclaration = 2,
  7991. kParameterDeclaration = 3;
  7992. class ExpressionScope {
  7993. constructor(type = kExpression) {
  7994. this.type = void 0;
  7995. this.type = type;
  7996. }
  7997. canBeArrowParameterDeclaration() {
  7998. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  7999. }
  8000. isCertainlyParameterDeclaration() {
  8001. return this.type === kParameterDeclaration;
  8002. }
  8003. }
  8004. class ArrowHeadParsingScope extends ExpressionScope {
  8005. constructor(type) {
  8006. super(type);
  8007. this.errors = new Map();
  8008. }
  8009. recordDeclarationError(pos, template) {
  8010. this.errors.set(pos, template);
  8011. }
  8012. clearDeclarationError(pos) {
  8013. this.errors.delete(pos);
  8014. }
  8015. iterateErrors(iterator) {
  8016. this.errors.forEach(iterator);
  8017. }
  8018. }
  8019. class ExpressionScopeHandler {
  8020. constructor(raise) {
  8021. this.stack = [new ExpressionScope()];
  8022. this.raise = raise;
  8023. }
  8024. enter(scope) {
  8025. this.stack.push(scope);
  8026. }
  8027. exit() {
  8028. this.stack.pop();
  8029. }
  8030. recordParameterInitializerError(pos, template) {
  8031. const {
  8032. stack
  8033. } = this;
  8034. let i = stack.length - 1;
  8035. let scope = stack[i];
  8036. while (!scope.isCertainlyParameterDeclaration()) {
  8037. if (scope.canBeArrowParameterDeclaration()) {
  8038. scope.recordDeclarationError(pos, template);
  8039. } else {
  8040. return;
  8041. }
  8042. scope = stack[--i];
  8043. }
  8044. this.raise(pos, template);
  8045. }
  8046. recordParenthesizedIdentifierError(pos, template) {
  8047. const {
  8048. stack
  8049. } = this;
  8050. const scope = stack[stack.length - 1];
  8051. if (scope.isCertainlyParameterDeclaration()) {
  8052. this.raise(pos, template);
  8053. } else if (scope.canBeArrowParameterDeclaration()) {
  8054. scope.recordDeclarationError(pos, template);
  8055. } else {
  8056. return;
  8057. }
  8058. }
  8059. recordAsyncArrowParametersError(pos, template) {
  8060. const {
  8061. stack
  8062. } = this;
  8063. let i = stack.length - 1;
  8064. let scope = stack[i];
  8065. while (scope.canBeArrowParameterDeclaration()) {
  8066. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  8067. scope.recordDeclarationError(pos, template);
  8068. }
  8069. scope = stack[--i];
  8070. }
  8071. }
  8072. validateAsPattern() {
  8073. const {
  8074. stack
  8075. } = this;
  8076. const currentScope = stack[stack.length - 1];
  8077. if (!currentScope.canBeArrowParameterDeclaration()) return;
  8078. currentScope.iterateErrors((template, pos) => {
  8079. this.raise(pos, template);
  8080. let i = stack.length - 2;
  8081. let scope = stack[i];
  8082. while (scope.canBeArrowParameterDeclaration()) {
  8083. scope.clearDeclarationError(pos);
  8084. scope = stack[--i];
  8085. }
  8086. });
  8087. }
  8088. }
  8089. function newParameterDeclarationScope() {
  8090. return new ExpressionScope(kParameterDeclaration);
  8091. }
  8092. function newArrowHeadScope() {
  8093. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  8094. }
  8095. function newAsyncArrowScope() {
  8096. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  8097. }
  8098. function newExpressionScope() {
  8099. return new ExpressionScope();
  8100. }
  8101. class UtilParser extends Tokenizer {
  8102. addExtra(node, key, val) {
  8103. if (!node) return;
  8104. const extra = node.extra = node.extra || {};
  8105. extra[key] = val;
  8106. }
  8107. isRelational(op) {
  8108. return this.match(types$1.relational) && this.state.value === op;
  8109. }
  8110. expectRelational(op) {
  8111. if (this.isRelational(op)) {
  8112. this.next();
  8113. } else {
  8114. this.unexpected(null, types$1.relational);
  8115. }
  8116. }
  8117. isContextual(name) {
  8118. return this.match(types$1.name) && this.state.value === name && !this.state.containsEsc;
  8119. }
  8120. isUnparsedContextual(nameStart, name) {
  8121. const nameEnd = nameStart + name.length;
  8122. return this.input.slice(nameStart, nameEnd) === name && (nameEnd === this.input.length || !isIdentifierChar(this.input.charCodeAt(nameEnd)));
  8123. }
  8124. isLookaheadContextual(name) {
  8125. const next = this.nextTokenStart();
  8126. return this.isUnparsedContextual(next, name);
  8127. }
  8128. eatContextual(name) {
  8129. return this.isContextual(name) && this.eat(types$1.name);
  8130. }
  8131. expectContextual(name, template) {
  8132. if (!this.eatContextual(name)) this.unexpected(null, template);
  8133. }
  8134. canInsertSemicolon() {
  8135. return this.match(types$1.eof) || this.match(types$1.braceR) || this.hasPrecedingLineBreak();
  8136. }
  8137. hasPrecedingLineBreak() {
  8138. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  8139. }
  8140. hasFollowingLineBreak() {
  8141. return lineBreak.test(this.input.slice(this.state.end, this.nextTokenStart()));
  8142. }
  8143. isLineTerminator() {
  8144. return this.eat(types$1.semi) || this.canInsertSemicolon();
  8145. }
  8146. semicolon(allowAsi = true) {
  8147. if (allowAsi ? this.isLineTerminator() : this.eat(types$1.semi)) return;
  8148. this.raise(this.state.lastTokEnd, ErrorMessages.MissingSemicolon);
  8149. }
  8150. expect(type, pos) {
  8151. this.eat(type) || this.unexpected(pos, type);
  8152. }
  8153. assertNoSpace(message = "Unexpected space.") {
  8154. if (this.state.start > this.state.lastTokEnd) {
  8155. this.raise(this.state.lastTokEnd, {
  8156. code: ErrorCodes.SyntaxError,
  8157. reasonCode: "UnexpectedSpace",
  8158. template: message
  8159. });
  8160. }
  8161. }
  8162. unexpected(pos, messageOrType = {
  8163. code: ErrorCodes.SyntaxError,
  8164. reasonCode: "UnexpectedToken",
  8165. template: "Unexpected token"
  8166. }) {
  8167. if (messageOrType instanceof TokenType) {
  8168. messageOrType = {
  8169. code: ErrorCodes.SyntaxError,
  8170. reasonCode: "UnexpectedToken",
  8171. template: `Unexpected token, expected "${messageOrType.label}"`
  8172. };
  8173. }
  8174. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  8175. }
  8176. expectPlugin(name, pos) {
  8177. if (!this.hasPlugin(name)) {
  8178. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8179. missingPlugin: [name]
  8180. }, `This experimental syntax requires enabling the parser plugin: '${name}'`);
  8181. }
  8182. return true;
  8183. }
  8184. expectOnePlugin(names, pos) {
  8185. if (!names.some(n => this.hasPlugin(n))) {
  8186. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8187. missingPlugin: names
  8188. }, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`);
  8189. }
  8190. }
  8191. tryParse(fn, oldState = this.state.clone()) {
  8192. const abortSignal = {
  8193. node: null
  8194. };
  8195. try {
  8196. const node = fn((node = null) => {
  8197. abortSignal.node = node;
  8198. throw abortSignal;
  8199. });
  8200. if (this.state.errors.length > oldState.errors.length) {
  8201. const failState = this.state;
  8202. this.state = oldState;
  8203. return {
  8204. node,
  8205. error: failState.errors[oldState.errors.length],
  8206. thrown: false,
  8207. aborted: false,
  8208. failState
  8209. };
  8210. }
  8211. return {
  8212. node,
  8213. error: null,
  8214. thrown: false,
  8215. aborted: false,
  8216. failState: null
  8217. };
  8218. } catch (error) {
  8219. const failState = this.state;
  8220. this.state = oldState;
  8221. if (error instanceof SyntaxError) {
  8222. return {
  8223. node: null,
  8224. error,
  8225. thrown: true,
  8226. aborted: false,
  8227. failState
  8228. };
  8229. }
  8230. if (error === abortSignal) {
  8231. return {
  8232. node: abortSignal.node,
  8233. error: null,
  8234. thrown: false,
  8235. aborted: true,
  8236. failState
  8237. };
  8238. }
  8239. throw error;
  8240. }
  8241. }
  8242. checkExpressionErrors(refExpressionErrors, andThrow) {
  8243. if (!refExpressionErrors) return false;
  8244. const {
  8245. shorthandAssign,
  8246. doubleProto
  8247. } = refExpressionErrors;
  8248. if (!andThrow) return shorthandAssign >= 0 || doubleProto >= 0;
  8249. if (shorthandAssign >= 0) {
  8250. this.unexpected(shorthandAssign);
  8251. }
  8252. if (doubleProto >= 0) {
  8253. this.raise(doubleProto, ErrorMessages.DuplicateProto);
  8254. }
  8255. }
  8256. isLiteralPropertyName() {
  8257. return this.match(types$1.name) || !!this.state.type.keyword || this.match(types$1.string) || this.match(types$1.num) || this.match(types$1.bigint) || this.match(types$1.decimal);
  8258. }
  8259. isPrivateName(node) {
  8260. return node.type === "PrivateName";
  8261. }
  8262. getPrivateNameSV(node) {
  8263. return node.id.name;
  8264. }
  8265. hasPropertyAsPrivateName(node) {
  8266. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  8267. }
  8268. isOptionalChain(node) {
  8269. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  8270. }
  8271. isObjectProperty(node) {
  8272. return node.type === "ObjectProperty";
  8273. }
  8274. isObjectMethod(node) {
  8275. return node.type === "ObjectMethod";
  8276. }
  8277. initializeScopes(inModule = this.options.sourceType === "module") {
  8278. const oldLabels = this.state.labels;
  8279. this.state.labels = [];
  8280. const oldExportedIdentifiers = this.state.exportedIdentifiers;
  8281. this.state.exportedIdentifiers = [];
  8282. const oldInModule = this.inModule;
  8283. this.inModule = inModule;
  8284. const oldScope = this.scope;
  8285. const ScopeHandler = this.getScopeHandler();
  8286. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  8287. const oldProdParam = this.prodParam;
  8288. this.prodParam = new ProductionParameterHandler();
  8289. const oldClassScope = this.classScope;
  8290. this.classScope = new ClassScopeHandler(this.raise.bind(this));
  8291. const oldExpressionScope = this.expressionScope;
  8292. this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
  8293. return () => {
  8294. this.state.labels = oldLabels;
  8295. this.state.exportedIdentifiers = oldExportedIdentifiers;
  8296. this.inModule = oldInModule;
  8297. this.scope = oldScope;
  8298. this.prodParam = oldProdParam;
  8299. this.classScope = oldClassScope;
  8300. this.expressionScope = oldExpressionScope;
  8301. };
  8302. }
  8303. enterInitialScopes() {
  8304. let paramFlags = PARAM;
  8305. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8306. paramFlags |= PARAM_AWAIT;
  8307. }
  8308. this.scope.enter(SCOPE_PROGRAM);
  8309. this.prodParam.enter(paramFlags);
  8310. }
  8311. }
  8312. class ExpressionErrors {
  8313. constructor() {
  8314. this.shorthandAssign = -1;
  8315. this.doubleProto = -1;
  8316. }
  8317. }
  8318. class Node {
  8319. constructor(parser, pos, loc) {
  8320. this.type = void 0;
  8321. this.start = void 0;
  8322. this.end = void 0;
  8323. this.loc = void 0;
  8324. this.range = void 0;
  8325. this.leadingComments = void 0;
  8326. this.trailingComments = void 0;
  8327. this.innerComments = void 0;
  8328. this.extra = void 0;
  8329. this.type = "";
  8330. this.start = pos;
  8331. this.end = 0;
  8332. this.loc = new SourceLocation(loc);
  8333. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  8334. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  8335. }
  8336. __clone() {
  8337. const newNode = new Node();
  8338. const keys = Object.keys(this);
  8339. for (let i = 0, length = keys.length; i < length; i++) {
  8340. const key = keys[i];
  8341. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  8342. newNode[key] = this[key];
  8343. }
  8344. }
  8345. return newNode;
  8346. }
  8347. }
  8348. class NodeUtils extends UtilParser {
  8349. startNode() {
  8350. return new Node(this, this.state.start, this.state.startLoc);
  8351. }
  8352. startNodeAt(pos, loc) {
  8353. return new Node(this, pos, loc);
  8354. }
  8355. startNodeAtNode(type) {
  8356. return this.startNodeAt(type.start, type.loc.start);
  8357. }
  8358. finishNode(node, type) {
  8359. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  8360. }
  8361. finishNodeAt(node, type, pos, loc) {
  8362. node.type = type;
  8363. node.end = pos;
  8364. node.loc.end = loc;
  8365. if (this.options.ranges) node.range[1] = pos;
  8366. this.processComment(node);
  8367. return node;
  8368. }
  8369. resetStartLocation(node, start, startLoc) {
  8370. node.start = start;
  8371. node.loc.start = startLoc;
  8372. if (this.options.ranges) node.range[0] = start;
  8373. }
  8374. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  8375. node.end = end;
  8376. node.loc.end = endLoc;
  8377. if (this.options.ranges) node.range[1] = end;
  8378. }
  8379. resetStartLocationFromNode(node, locationNode) {
  8380. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  8381. }
  8382. }
  8383. const unwrapParenthesizedExpression = node => {
  8384. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  8385. };
  8386. class LValParser extends NodeUtils {
  8387. toAssignable(node, isLHS = false) {
  8388. var _node$extra, _node$extra3;
  8389. let parenthesized = undefined;
  8390. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  8391. parenthesized = unwrapParenthesizedExpression(node);
  8392. if (isLHS) {
  8393. if (parenthesized.type === "Identifier") {
  8394. this.expressionScope.recordParenthesizedIdentifierError(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8395. } else if (parenthesized.type !== "MemberExpression") {
  8396. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8397. }
  8398. } else {
  8399. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8400. }
  8401. }
  8402. switch (node.type) {
  8403. case "Identifier":
  8404. case "ObjectPattern":
  8405. case "ArrayPattern":
  8406. case "AssignmentPattern":
  8407. break;
  8408. case "ObjectExpression":
  8409. node.type = "ObjectPattern";
  8410. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  8411. var _node$extra2;
  8412. const prop = node.properties[i];
  8413. const isLast = i === last;
  8414. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  8415. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingComma) {
  8416. this.raiseRestNotLast(node.extra.trailingComma);
  8417. }
  8418. }
  8419. break;
  8420. case "ObjectProperty":
  8421. this.toAssignable(node.value, isLHS);
  8422. break;
  8423. case "SpreadElement":
  8424. {
  8425. this.checkToRestConversion(node);
  8426. node.type = "RestElement";
  8427. const arg = node.argument;
  8428. this.toAssignable(arg, isLHS);
  8429. break;
  8430. }
  8431. case "ArrayExpression":
  8432. node.type = "ArrayPattern";
  8433. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingComma, isLHS);
  8434. break;
  8435. case "AssignmentExpression":
  8436. if (node.operator !== "=") {
  8437. this.raise(node.left.end, ErrorMessages.MissingEqInAssignment);
  8438. }
  8439. node.type = "AssignmentPattern";
  8440. delete node.operator;
  8441. this.toAssignable(node.left, isLHS);
  8442. break;
  8443. case "ParenthesizedExpression":
  8444. this.toAssignable(parenthesized, isLHS);
  8445. break;
  8446. }
  8447. return node;
  8448. }
  8449. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  8450. if (prop.type === "ObjectMethod") {
  8451. const error = prop.kind === "get" || prop.kind === "set" ? ErrorMessages.PatternHasAccessor : ErrorMessages.PatternHasMethod;
  8452. this.raise(prop.key.start, error);
  8453. } else if (prop.type === "SpreadElement" && !isLast) {
  8454. this.raiseRestNotLast(prop.start);
  8455. } else {
  8456. this.toAssignable(prop, isLHS);
  8457. }
  8458. }
  8459. toAssignableList(exprList, trailingCommaPos, isLHS) {
  8460. let end = exprList.length;
  8461. if (end) {
  8462. const last = exprList[end - 1];
  8463. if ((last == null ? void 0 : last.type) === "RestElement") {
  8464. --end;
  8465. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  8466. last.type = "RestElement";
  8467. let arg = last.argument;
  8468. this.toAssignable(arg, isLHS);
  8469. arg = unwrapParenthesizedExpression(arg);
  8470. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  8471. this.unexpected(arg.start);
  8472. }
  8473. if (trailingCommaPos) {
  8474. this.raiseTrailingCommaAfterRest(trailingCommaPos);
  8475. }
  8476. --end;
  8477. }
  8478. }
  8479. for (let i = 0; i < end; i++) {
  8480. const elt = exprList[i];
  8481. if (elt) {
  8482. this.toAssignable(elt, isLHS);
  8483. if (elt.type === "RestElement") {
  8484. this.raiseRestNotLast(elt.start);
  8485. }
  8486. }
  8487. }
  8488. return exprList;
  8489. }
  8490. toReferencedList(exprList, isParenthesizedExpr) {
  8491. return exprList;
  8492. }
  8493. toReferencedListDeep(exprList, isParenthesizedExpr) {
  8494. this.toReferencedList(exprList, isParenthesizedExpr);
  8495. for (const expr of exprList) {
  8496. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  8497. this.toReferencedListDeep(expr.elements);
  8498. }
  8499. }
  8500. }
  8501. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  8502. const node = this.startNode();
  8503. this.next();
  8504. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  8505. return this.finishNode(node, "SpreadElement");
  8506. }
  8507. parseRestBinding() {
  8508. const node = this.startNode();
  8509. this.next();
  8510. node.argument = this.parseBindingAtom();
  8511. return this.finishNode(node, "RestElement");
  8512. }
  8513. parseBindingAtom() {
  8514. switch (this.state.type) {
  8515. case types$1.bracketL:
  8516. {
  8517. const node = this.startNode();
  8518. this.next();
  8519. node.elements = this.parseBindingList(types$1.bracketR, 93, true);
  8520. return this.finishNode(node, "ArrayPattern");
  8521. }
  8522. case types$1.braceL:
  8523. return this.parseObjectLike(types$1.braceR, true);
  8524. }
  8525. return this.parseIdentifier();
  8526. }
  8527. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  8528. const elts = [];
  8529. let first = true;
  8530. while (!this.eat(close)) {
  8531. if (first) {
  8532. first = false;
  8533. } else {
  8534. this.expect(types$1.comma);
  8535. }
  8536. if (allowEmpty && this.match(types$1.comma)) {
  8537. elts.push(null);
  8538. } else if (this.eat(close)) {
  8539. break;
  8540. } else if (this.match(types$1.ellipsis)) {
  8541. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  8542. this.checkCommaAfterRest(closeCharCode);
  8543. this.expect(close);
  8544. break;
  8545. } else {
  8546. const decorators = [];
  8547. if (this.match(types$1.at) && this.hasPlugin("decorators")) {
  8548. this.raise(this.state.start, ErrorMessages.UnsupportedParameterDecorator);
  8549. }
  8550. while (this.match(types$1.at)) {
  8551. decorators.push(this.parseDecorator());
  8552. }
  8553. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  8554. }
  8555. }
  8556. return elts;
  8557. }
  8558. parseAssignableListItem(allowModifiers, decorators) {
  8559. const left = this.parseMaybeDefault();
  8560. this.parseAssignableListItemTypes(left);
  8561. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8562. if (decorators.length) {
  8563. left.decorators = decorators;
  8564. }
  8565. return elt;
  8566. }
  8567. parseAssignableListItemTypes(param) {
  8568. return param;
  8569. }
  8570. parseMaybeDefault(startPos, startLoc, left) {
  8571. var _startLoc, _startPos, _left;
  8572. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  8573. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  8574. left = (_left = left) != null ? _left : this.parseBindingAtom();
  8575. if (!this.eat(types$1.eq)) return left;
  8576. const node = this.startNodeAt(startPos, startLoc);
  8577. node.left = left;
  8578. node.right = this.parseMaybeAssignAllowIn();
  8579. return this.finishNode(node, "AssignmentPattern");
  8580. }
  8581. checkLVal(expr, contextDescription, bindingType = BIND_NONE, checkClashes, disallowLetBinding, strictModeChanged = false) {
  8582. switch (expr.type) {
  8583. case "Identifier":
  8584. {
  8585. const {
  8586. name
  8587. } = expr;
  8588. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(name, this.inModule) : isStrictBindOnlyReservedWord(name))) {
  8589. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.StrictEvalArguments : ErrorMessages.StrictEvalArgumentsBinding, name);
  8590. }
  8591. if (checkClashes) {
  8592. if (checkClashes.has(name)) {
  8593. this.raise(expr.start, ErrorMessages.ParamDupe);
  8594. } else {
  8595. checkClashes.add(name);
  8596. }
  8597. }
  8598. if (disallowLetBinding && name === "let") {
  8599. this.raise(expr.start, ErrorMessages.LetInLexicalBinding);
  8600. }
  8601. if (!(bindingType & BIND_NONE)) {
  8602. this.scope.declareName(name, bindingType, expr.start);
  8603. }
  8604. break;
  8605. }
  8606. case "MemberExpression":
  8607. if (bindingType !== BIND_NONE) {
  8608. this.raise(expr.start, ErrorMessages.InvalidPropertyBindingPattern);
  8609. }
  8610. break;
  8611. case "ObjectPattern":
  8612. for (let prop of expr.properties) {
  8613. if (this.isObjectProperty(prop)) prop = prop.value;else if (this.isObjectMethod(prop)) continue;
  8614. this.checkLVal(prop, "object destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8615. }
  8616. break;
  8617. case "ArrayPattern":
  8618. for (const elem of expr.elements) {
  8619. if (elem) {
  8620. this.checkLVal(elem, "array destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8621. }
  8622. }
  8623. break;
  8624. case "AssignmentPattern":
  8625. this.checkLVal(expr.left, "assignment pattern", bindingType, checkClashes);
  8626. break;
  8627. case "RestElement":
  8628. this.checkLVal(expr.argument, "rest element", bindingType, checkClashes);
  8629. break;
  8630. case "ParenthesizedExpression":
  8631. this.checkLVal(expr.expression, "parenthesized expression", bindingType, checkClashes);
  8632. break;
  8633. default:
  8634. {
  8635. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.InvalidLhs : ErrorMessages.InvalidLhsBinding, contextDescription);
  8636. }
  8637. }
  8638. }
  8639. checkToRestConversion(node) {
  8640. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  8641. this.raise(node.argument.start, ErrorMessages.InvalidRestAssignmentPattern);
  8642. }
  8643. }
  8644. checkCommaAfterRest(close) {
  8645. if (this.match(types$1.comma)) {
  8646. if (this.lookaheadCharCode() === close) {
  8647. this.raiseTrailingCommaAfterRest(this.state.start);
  8648. } else {
  8649. this.raiseRestNotLast(this.state.start);
  8650. }
  8651. }
  8652. }
  8653. raiseRestNotLast(pos) {
  8654. throw this.raise(pos, ErrorMessages.ElementAfterRest);
  8655. }
  8656. raiseTrailingCommaAfterRest(pos) {
  8657. this.raise(pos, ErrorMessages.RestTrailingComma);
  8658. }
  8659. }
  8660. class ExpressionParser extends LValParser {
  8661. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  8662. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  8663. return;
  8664. }
  8665. const key = prop.key;
  8666. const name = key.type === "Identifier" ? key.name : key.value;
  8667. if (name === "__proto__") {
  8668. if (isRecord) {
  8669. this.raise(key.start, ErrorMessages.RecordNoProto);
  8670. return;
  8671. }
  8672. if (protoRef.used) {
  8673. if (refExpressionErrors) {
  8674. if (refExpressionErrors.doubleProto === -1) {
  8675. refExpressionErrors.doubleProto = key.start;
  8676. }
  8677. } else {
  8678. this.raise(key.start, ErrorMessages.DuplicateProto);
  8679. }
  8680. }
  8681. protoRef.used = true;
  8682. }
  8683. }
  8684. shouldExitDescending(expr, potentialArrowAt) {
  8685. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  8686. }
  8687. getExpression() {
  8688. let paramFlags = PARAM;
  8689. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8690. paramFlags |= PARAM_AWAIT;
  8691. }
  8692. this.scope.enter(SCOPE_PROGRAM);
  8693. this.prodParam.enter(paramFlags);
  8694. this.nextToken();
  8695. const expr = this.parseExpression();
  8696. if (!this.match(types$1.eof)) {
  8697. this.unexpected();
  8698. }
  8699. expr.comments = this.state.comments;
  8700. expr.errors = this.state.errors;
  8701. if (this.options.tokens) {
  8702. expr.tokens = this.tokens;
  8703. }
  8704. return expr;
  8705. }
  8706. parseExpression(disallowIn, refExpressionErrors) {
  8707. if (disallowIn) {
  8708. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8709. }
  8710. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8711. }
  8712. parseExpressionBase(refExpressionErrors) {
  8713. const startPos = this.state.start;
  8714. const startLoc = this.state.startLoc;
  8715. const expr = this.parseMaybeAssign(refExpressionErrors);
  8716. if (this.match(types$1.comma)) {
  8717. const node = this.startNodeAt(startPos, startLoc);
  8718. node.expressions = [expr];
  8719. while (this.eat(types$1.comma)) {
  8720. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  8721. }
  8722. this.toReferencedList(node.expressions);
  8723. return this.finishNode(node, "SequenceExpression");
  8724. }
  8725. return expr;
  8726. }
  8727. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8728. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
  8729. }
  8730. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8731. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
  8732. }
  8733. parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8734. const startPos = this.state.start;
  8735. const startLoc = this.state.startLoc;
  8736. if (this.isContextual("yield")) {
  8737. if (this.prodParam.hasYield) {
  8738. this.state.exprAllowed = true;
  8739. let left = this.parseYield();
  8740. if (afterLeftParse) {
  8741. left = afterLeftParse.call(this, left, startPos, startLoc);
  8742. }
  8743. return left;
  8744. }
  8745. }
  8746. let ownExpressionErrors;
  8747. if (refExpressionErrors) {
  8748. ownExpressionErrors = false;
  8749. } else {
  8750. refExpressionErrors = new ExpressionErrors();
  8751. ownExpressionErrors = true;
  8752. }
  8753. if (this.match(types$1.parenL) || this.match(types$1.name)) {
  8754. this.state.potentialArrowAt = this.state.start;
  8755. }
  8756. let left = this.parseMaybeConditional(refExpressionErrors, refNeedsArrowPos);
  8757. if (afterLeftParse) {
  8758. left = afterLeftParse.call(this, left, startPos, startLoc);
  8759. }
  8760. if (this.state.type.isAssign) {
  8761. const node = this.startNodeAt(startPos, startLoc);
  8762. const operator = this.state.value;
  8763. node.operator = operator;
  8764. if (this.match(types$1.eq)) {
  8765. node.left = this.toAssignable(left, true);
  8766. refExpressionErrors.doubleProto = -1;
  8767. } else {
  8768. node.left = left;
  8769. }
  8770. if (refExpressionErrors.shorthandAssign >= node.left.start) {
  8771. refExpressionErrors.shorthandAssign = -1;
  8772. }
  8773. this.checkLVal(left, "assignment expression");
  8774. this.next();
  8775. node.right = this.parseMaybeAssign();
  8776. return this.finishNode(node, "AssignmentExpression");
  8777. } else if (ownExpressionErrors) {
  8778. this.checkExpressionErrors(refExpressionErrors, true);
  8779. }
  8780. return left;
  8781. }
  8782. parseMaybeConditional(refExpressionErrors, refNeedsArrowPos) {
  8783. const startPos = this.state.start;
  8784. const startLoc = this.state.startLoc;
  8785. const potentialArrowAt = this.state.potentialArrowAt;
  8786. const expr = this.parseExprOps(refExpressionErrors);
  8787. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8788. return expr;
  8789. }
  8790. return this.parseConditional(expr, startPos, startLoc, refNeedsArrowPos);
  8791. }
  8792. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  8793. if (this.eat(types$1.question)) {
  8794. const node = this.startNodeAt(startPos, startLoc);
  8795. node.test = expr;
  8796. node.consequent = this.parseMaybeAssignAllowIn();
  8797. this.expect(types$1.colon);
  8798. node.alternate = this.parseMaybeAssign();
  8799. return this.finishNode(node, "ConditionalExpression");
  8800. }
  8801. return expr;
  8802. }
  8803. parseExprOps(refExpressionErrors) {
  8804. const startPos = this.state.start;
  8805. const startLoc = this.state.startLoc;
  8806. const potentialArrowAt = this.state.potentialArrowAt;
  8807. const expr = this.parseMaybeUnary(refExpressionErrors);
  8808. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8809. return expr;
  8810. }
  8811. return this.parseExprOp(expr, startPos, startLoc, -1);
  8812. }
  8813. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8814. let prec = this.state.type.binop;
  8815. if (prec != null && (this.prodParam.hasIn || !this.match(types$1._in))) {
  8816. if (prec > minPrec) {
  8817. const op = this.state.type;
  8818. if (op === types$1.pipeline) {
  8819. this.expectPlugin("pipelineOperator");
  8820. if (this.state.inFSharpPipelineDirectBody) {
  8821. return left;
  8822. }
  8823. this.state.inPipeline = true;
  8824. this.checkPipelineAtInfixOperator(left, leftStartPos);
  8825. }
  8826. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8827. node.left = left;
  8828. node.operator = this.state.value;
  8829. const logical = op === types$1.logicalOR || op === types$1.logicalAND;
  8830. const coalesce = op === types$1.nullishCoalescing;
  8831. if (coalesce) {
  8832. prec = types$1.logicalAND.binop;
  8833. }
  8834. this.next();
  8835. if (op === types$1.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  8836. if (this.match(types$1.name) && this.state.value === "await" && this.prodParam.hasAwait) {
  8837. throw this.raise(this.state.start, ErrorMessages.UnexpectedAwaitAfterPipelineBody);
  8838. }
  8839. }
  8840. node.right = this.parseExprOpRightExpr(op, prec);
  8841. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  8842. const nextOp = this.state.type;
  8843. if (coalesce && (nextOp === types$1.logicalOR || nextOp === types$1.logicalAND) || logical && nextOp === types$1.nullishCoalescing) {
  8844. throw this.raise(this.state.start, ErrorMessages.MixingCoalesceWithLogical);
  8845. }
  8846. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8847. }
  8848. }
  8849. return left;
  8850. }
  8851. parseExprOpRightExpr(op, prec) {
  8852. const startPos = this.state.start;
  8853. const startLoc = this.state.startLoc;
  8854. switch (op) {
  8855. case types$1.pipeline:
  8856. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  8857. case "smart":
  8858. return this.withTopicPermittingContext(() => {
  8859. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  8860. });
  8861. case "fsharp":
  8862. return this.withSoloAwaitPermittingContext(() => {
  8863. return this.parseFSharpPipelineBody(prec);
  8864. });
  8865. }
  8866. default:
  8867. return this.parseExprOpBaseRightExpr(op, prec);
  8868. }
  8869. }
  8870. parseExprOpBaseRightExpr(op, prec) {
  8871. const startPos = this.state.start;
  8872. const startLoc = this.state.startLoc;
  8873. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec);
  8874. }
  8875. checkExponentialAfterUnary(node) {
  8876. if (this.match(types$1.exponent)) {
  8877. this.raise(node.argument.start, ErrorMessages.UnexpectedTokenUnaryExponentiation);
  8878. }
  8879. }
  8880. parseMaybeUnary(refExpressionErrors, sawUnary) {
  8881. const startPos = this.state.start;
  8882. const startLoc = this.state.startLoc;
  8883. const isAwait = this.isContextual("await");
  8884. if (isAwait && this.isAwaitAllowed()) {
  8885. this.next();
  8886. const expr = this.parseAwait(startPos, startLoc);
  8887. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  8888. return expr;
  8889. }
  8890. if (this.isContextual("module") && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  8891. return this.parseModuleExpression();
  8892. }
  8893. const update = this.match(types$1.incDec);
  8894. const node = this.startNode();
  8895. if (this.state.type.prefix) {
  8896. node.operator = this.state.value;
  8897. node.prefix = true;
  8898. if (this.match(types$1._throw)) {
  8899. this.expectPlugin("throwExpressions");
  8900. }
  8901. const isDelete = this.match(types$1._delete);
  8902. this.next();
  8903. node.argument = this.parseMaybeUnary(null, true);
  8904. this.checkExpressionErrors(refExpressionErrors, true);
  8905. if (this.state.strict && isDelete) {
  8906. const arg = node.argument;
  8907. if (arg.type === "Identifier") {
  8908. this.raise(node.start, ErrorMessages.StrictDelete);
  8909. } else if (this.hasPropertyAsPrivateName(arg)) {
  8910. this.raise(node.start, ErrorMessages.DeletePrivateField);
  8911. }
  8912. }
  8913. if (!update) {
  8914. if (!sawUnary) this.checkExponentialAfterUnary(node);
  8915. return this.finishNode(node, "UnaryExpression");
  8916. }
  8917. }
  8918. const expr = this.parseUpdate(node, update, refExpressionErrors);
  8919. if (isAwait) {
  8920. const startsExpr = this.hasPlugin("v8intrinsic") ? this.state.type.startsExpr : this.state.type.startsExpr && !this.match(types$1.modulo);
  8921. if (startsExpr && !this.isAmbiguousAwait()) {
  8922. this.raiseOverwrite(startPos, this.hasPlugin("topLevelAwait") ? ErrorMessages.AwaitNotInAsyncContext : ErrorMessages.AwaitNotInAsyncFunction);
  8923. return this.parseAwait(startPos, startLoc);
  8924. }
  8925. }
  8926. return expr;
  8927. }
  8928. parseUpdate(node, update, refExpressionErrors) {
  8929. if (update) {
  8930. this.checkLVal(node.argument, "prefix operation");
  8931. return this.finishNode(node, "UpdateExpression");
  8932. }
  8933. const startPos = this.state.start;
  8934. const startLoc = this.state.startLoc;
  8935. let expr = this.parseExprSubscripts(refExpressionErrors);
  8936. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  8937. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  8938. const node = this.startNodeAt(startPos, startLoc);
  8939. node.operator = this.state.value;
  8940. node.prefix = false;
  8941. node.argument = expr;
  8942. this.checkLVal(expr, "postfix operation");
  8943. this.next();
  8944. expr = this.finishNode(node, "UpdateExpression");
  8945. }
  8946. return expr;
  8947. }
  8948. parseExprSubscripts(refExpressionErrors) {
  8949. const startPos = this.state.start;
  8950. const startLoc = this.state.startLoc;
  8951. const potentialArrowAt = this.state.potentialArrowAt;
  8952. const expr = this.parseExprAtom(refExpressionErrors);
  8953. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8954. return expr;
  8955. }
  8956. return this.parseSubscripts(expr, startPos, startLoc);
  8957. }
  8958. parseSubscripts(base, startPos, startLoc, noCalls) {
  8959. const state = {
  8960. optionalChainMember: false,
  8961. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  8962. stop: false
  8963. };
  8964. do {
  8965. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  8966. state.maybeAsyncArrow = false;
  8967. } while (!state.stop);
  8968. return base;
  8969. }
  8970. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8971. if (!noCalls && this.eat(types$1.doubleColon)) {
  8972. return this.parseBind(base, startPos, startLoc, noCalls, state);
  8973. } else if (this.match(types$1.backQuote)) {
  8974. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8975. }
  8976. let optional = false;
  8977. if (this.match(types$1.questionDot)) {
  8978. if (noCalls && this.lookaheadCharCode() === 40) {
  8979. state.stop = true;
  8980. return base;
  8981. }
  8982. state.optionalChainMember = optional = true;
  8983. this.next();
  8984. }
  8985. if (!noCalls && this.match(types$1.parenL)) {
  8986. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  8987. } else if (optional || this.match(types$1.bracketL) || this.eat(types$1.dot)) {
  8988. return this.parseMember(base, startPos, startLoc, state, optional);
  8989. } else {
  8990. state.stop = true;
  8991. return base;
  8992. }
  8993. }
  8994. parseMember(base, startPos, startLoc, state, optional) {
  8995. const node = this.startNodeAt(startPos, startLoc);
  8996. const computed = this.eat(types$1.bracketL);
  8997. node.object = base;
  8998. node.computed = computed;
  8999. const privateName = !computed && this.match(types$1.privateName) && this.state.value;
  9000. const property = computed ? this.parseExpression() : privateName ? this.parsePrivateName() : this.parseIdentifier(true);
  9001. if (privateName !== false) {
  9002. if (node.object.type === "Super") {
  9003. this.raise(startPos, ErrorMessages.SuperPrivateField);
  9004. }
  9005. this.classScope.usePrivateName(privateName, property.start);
  9006. }
  9007. node.property = property;
  9008. if (computed) {
  9009. this.expect(types$1.bracketR);
  9010. }
  9011. if (state.optionalChainMember) {
  9012. node.optional = optional;
  9013. return this.finishNode(node, "OptionalMemberExpression");
  9014. } else {
  9015. return this.finishNode(node, "MemberExpression");
  9016. }
  9017. }
  9018. parseBind(base, startPos, startLoc, noCalls, state) {
  9019. const node = this.startNodeAt(startPos, startLoc);
  9020. node.object = base;
  9021. node.callee = this.parseNoCallExpr();
  9022. state.stop = true;
  9023. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  9024. }
  9025. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  9026. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9027. this.state.maybeInArrowParameters = true;
  9028. this.next();
  9029. let node = this.startNodeAt(startPos, startLoc);
  9030. node.callee = base;
  9031. if (state.maybeAsyncArrow) {
  9032. this.expressionScope.enter(newAsyncArrowScope());
  9033. }
  9034. if (state.optionalChainMember) {
  9035. node.optional = optional;
  9036. }
  9037. if (optional) {
  9038. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  9039. } else {
  9040. node.arguments = this.parseCallExpressionArguments(types$1.parenR, state.maybeAsyncArrow, base.type === "Import", base.type !== "Super", node);
  9041. }
  9042. this.finishCallExpression(node, state.optionalChainMember);
  9043. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  9044. state.stop = true;
  9045. this.expressionScope.validateAsPattern();
  9046. this.expressionScope.exit();
  9047. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  9048. } else {
  9049. if (state.maybeAsyncArrow) {
  9050. this.expressionScope.exit();
  9051. }
  9052. this.toReferencedArguments(node);
  9053. }
  9054. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9055. return node;
  9056. }
  9057. toReferencedArguments(node, isParenthesizedExpr) {
  9058. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  9059. }
  9060. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  9061. const node = this.startNodeAt(startPos, startLoc);
  9062. node.tag = base;
  9063. node.quasi = this.parseTemplate(true);
  9064. if (state.optionalChainMember) {
  9065. this.raise(startPos, ErrorMessages.OptionalChainingNoTemplate);
  9066. }
  9067. return this.finishNode(node, "TaggedTemplateExpression");
  9068. }
  9069. atPossibleAsyncArrow(base) {
  9070. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  9071. }
  9072. finishCallExpression(node, optional) {
  9073. if (node.callee.type === "Import") {
  9074. if (node.arguments.length === 2) {
  9075. if (!this.hasPlugin("moduleAttributes")) {
  9076. this.expectPlugin("importAssertions");
  9077. }
  9078. }
  9079. if (node.arguments.length === 0 || node.arguments.length > 2) {
  9080. this.raise(node.start, ErrorMessages.ImportCallArity, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
  9081. } else {
  9082. for (const arg of node.arguments) {
  9083. if (arg.type === "SpreadElement") {
  9084. this.raise(arg.start, ErrorMessages.ImportCallSpreadArgument);
  9085. }
  9086. }
  9087. }
  9088. }
  9089. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  9090. }
  9091. parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport, allowPlaceholder, nodeForExtra) {
  9092. const elts = [];
  9093. let first = true;
  9094. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9095. this.state.inFSharpPipelineDirectBody = false;
  9096. while (!this.eat(close)) {
  9097. if (first) {
  9098. first = false;
  9099. } else {
  9100. this.expect(types$1.comma);
  9101. if (this.match(close)) {
  9102. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  9103. this.raise(this.state.lastTokStart, ErrorMessages.ImportCallArgumentTrailingComma);
  9104. }
  9105. if (nodeForExtra) {
  9106. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9107. }
  9108. this.next();
  9109. break;
  9110. }
  9111. }
  9112. elts.push(this.parseExprListItem(false, possibleAsyncArrow ? new ExpressionErrors() : undefined, possibleAsyncArrow ? {
  9113. start: 0
  9114. } : undefined, allowPlaceholder));
  9115. }
  9116. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9117. return elts;
  9118. }
  9119. shouldParseAsyncArrow() {
  9120. return this.match(types$1.arrow) && !this.canInsertSemicolon();
  9121. }
  9122. parseAsyncArrowFromCallExpression(node, call) {
  9123. var _call$extra;
  9124. this.expect(types$1.arrow);
  9125. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingComma);
  9126. return node;
  9127. }
  9128. parseNoCallExpr() {
  9129. const startPos = this.state.start;
  9130. const startLoc = this.state.startLoc;
  9131. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  9132. }
  9133. parseExprAtom(refExpressionErrors) {
  9134. if (this.state.type === types$1.slash) this.readRegexp();
  9135. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9136. let node;
  9137. switch (this.state.type) {
  9138. case types$1._super:
  9139. return this.parseSuper();
  9140. case types$1._import:
  9141. node = this.startNode();
  9142. this.next();
  9143. if (this.match(types$1.dot)) {
  9144. return this.parseImportMetaProperty(node);
  9145. }
  9146. if (!this.match(types$1.parenL)) {
  9147. this.raise(this.state.lastTokStart, ErrorMessages.UnsupportedImport);
  9148. }
  9149. return this.finishNode(node, "Import");
  9150. case types$1._this:
  9151. node = this.startNode();
  9152. this.next();
  9153. return this.finishNode(node, "ThisExpression");
  9154. case types$1.name:
  9155. {
  9156. const containsEsc = this.state.containsEsc;
  9157. const id = this.parseIdentifier();
  9158. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  9159. if (this.match(types$1._function)) {
  9160. const last = this.state.context.length - 1;
  9161. if (this.state.context[last] !== types.functionStatement) {
  9162. throw new Error("Internal error");
  9163. }
  9164. this.state.context[last] = types.functionExpression;
  9165. this.next();
  9166. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  9167. } else if (this.match(types$1.name)) {
  9168. if (this.lookaheadCharCode() === 61) {
  9169. return this.parseAsyncArrowUnaryFunction(id);
  9170. } else {
  9171. return id;
  9172. }
  9173. } else if (this.match(types$1._do)) {
  9174. return this.parseDo(true);
  9175. }
  9176. }
  9177. if (canBeArrow && this.match(types$1.arrow) && !this.canInsertSemicolon()) {
  9178. this.next();
  9179. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  9180. }
  9181. return id;
  9182. }
  9183. case types$1._do:
  9184. {
  9185. return this.parseDo(false);
  9186. }
  9187. case types$1.regexp:
  9188. {
  9189. const value = this.state.value;
  9190. node = this.parseLiteral(value.value, "RegExpLiteral");
  9191. node.pattern = value.pattern;
  9192. node.flags = value.flags;
  9193. return node;
  9194. }
  9195. case types$1.num:
  9196. return this.parseLiteral(this.state.value, "NumericLiteral");
  9197. case types$1.bigint:
  9198. return this.parseLiteral(this.state.value, "BigIntLiteral");
  9199. case types$1.decimal:
  9200. return this.parseLiteral(this.state.value, "DecimalLiteral");
  9201. case types$1.string:
  9202. return this.parseLiteral(this.state.value, "StringLiteral");
  9203. case types$1._null:
  9204. node = this.startNode();
  9205. this.next();
  9206. return this.finishNode(node, "NullLiteral");
  9207. case types$1._true:
  9208. case types$1._false:
  9209. return this.parseBooleanLiteral();
  9210. case types$1.parenL:
  9211. return this.parseParenAndDistinguishExpression(canBeArrow);
  9212. case types$1.bracketBarL:
  9213. case types$1.bracketHashL:
  9214. {
  9215. return this.parseArrayLike(this.state.type === types$1.bracketBarL ? types$1.bracketBarR : types$1.bracketR, false, true, refExpressionErrors);
  9216. }
  9217. case types$1.bracketL:
  9218. {
  9219. return this.parseArrayLike(types$1.bracketR, true, false, refExpressionErrors);
  9220. }
  9221. case types$1.braceBarL:
  9222. case types$1.braceHashL:
  9223. {
  9224. return this.parseObjectLike(this.state.type === types$1.braceBarL ? types$1.braceBarR : types$1.braceR, false, true, refExpressionErrors);
  9225. }
  9226. case types$1.braceL:
  9227. {
  9228. return this.parseObjectLike(types$1.braceR, false, false, refExpressionErrors);
  9229. }
  9230. case types$1._function:
  9231. return this.parseFunctionOrFunctionSent();
  9232. case types$1.at:
  9233. this.parseDecorators();
  9234. case types$1._class:
  9235. node = this.startNode();
  9236. this.takeDecorators(node);
  9237. return this.parseClass(node, false);
  9238. case types$1._new:
  9239. return this.parseNewOrNewTarget();
  9240. case types$1.backQuote:
  9241. return this.parseTemplate(false);
  9242. case types$1.doubleColon:
  9243. {
  9244. node = this.startNode();
  9245. this.next();
  9246. node.object = null;
  9247. const callee = node.callee = this.parseNoCallExpr();
  9248. if (callee.type === "MemberExpression") {
  9249. return this.finishNode(node, "BindExpression");
  9250. } else {
  9251. throw this.raise(callee.start, ErrorMessages.UnsupportedBind);
  9252. }
  9253. }
  9254. case types$1.privateName:
  9255. {
  9256. const start = this.state.start;
  9257. const value = this.state.value;
  9258. node = this.parsePrivateName();
  9259. if (this.match(types$1._in)) {
  9260. this.expectPlugin("privateIn");
  9261. this.classScope.usePrivateName(value, node.start);
  9262. } else if (this.hasPlugin("privateIn")) {
  9263. this.raise(this.state.start, ErrorMessages.PrivateInExpectedIn, value);
  9264. } else {
  9265. throw this.unexpected(start);
  9266. }
  9267. return node;
  9268. }
  9269. case types$1.hash:
  9270. {
  9271. if (this.state.inPipeline) {
  9272. node = this.startNode();
  9273. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  9274. this.raise(node.start, ErrorMessages.PrimaryTopicRequiresSmartPipeline);
  9275. }
  9276. this.next();
  9277. if (!this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  9278. this.raise(node.start, ErrorMessages.PrimaryTopicNotAllowed);
  9279. }
  9280. this.registerTopicReference();
  9281. return this.finishNode(node, "PipelinePrimaryTopicReference");
  9282. }
  9283. }
  9284. case types$1.relational:
  9285. {
  9286. if (this.state.value === "<") {
  9287. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  9288. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  9289. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  9290. }
  9291. }
  9292. }
  9293. default:
  9294. throw this.unexpected();
  9295. }
  9296. }
  9297. parseAsyncArrowUnaryFunction(id) {
  9298. const node = this.startNodeAtNode(id);
  9299. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  9300. const params = [this.parseIdentifier()];
  9301. this.prodParam.exit();
  9302. if (this.hasPrecedingLineBreak()) {
  9303. this.raise(this.state.pos, ErrorMessages.LineTerminatorBeforeArrow);
  9304. }
  9305. this.expect(types$1.arrow);
  9306. this.parseArrowExpression(node, params, true);
  9307. return node;
  9308. }
  9309. parseDo(isAsync) {
  9310. this.expectPlugin("doExpressions");
  9311. if (isAsync) {
  9312. this.expectPlugin("asyncDoExpressions");
  9313. }
  9314. const node = this.startNode();
  9315. node.async = isAsync;
  9316. this.next();
  9317. const oldLabels = this.state.labels;
  9318. this.state.labels = [];
  9319. if (isAsync) {
  9320. this.prodParam.enter(PARAM_AWAIT);
  9321. node.body = this.parseBlock();
  9322. this.prodParam.exit();
  9323. } else {
  9324. node.body = this.parseBlock();
  9325. }
  9326. this.state.labels = oldLabels;
  9327. return this.finishNode(node, "DoExpression");
  9328. }
  9329. parseSuper() {
  9330. const node = this.startNode();
  9331. this.next();
  9332. if (this.match(types$1.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  9333. this.raise(node.start, ErrorMessages.SuperNotAllowed);
  9334. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  9335. this.raise(node.start, ErrorMessages.UnexpectedSuper);
  9336. }
  9337. if (!this.match(types$1.parenL) && !this.match(types$1.bracketL) && !this.match(types$1.dot)) {
  9338. this.raise(node.start, ErrorMessages.UnsupportedSuper);
  9339. }
  9340. return this.finishNode(node, "Super");
  9341. }
  9342. parseBooleanLiteral() {
  9343. const node = this.startNode();
  9344. node.value = this.match(types$1._true);
  9345. this.next();
  9346. return this.finishNode(node, "BooleanLiteral");
  9347. }
  9348. parseMaybePrivateName(isPrivateNameAllowed) {
  9349. const isPrivate = this.match(types$1.privateName);
  9350. if (isPrivate) {
  9351. if (!isPrivateNameAllowed) {
  9352. this.raise(this.state.start + 1, ErrorMessages.UnexpectedPrivateField);
  9353. }
  9354. return this.parsePrivateName();
  9355. } else {
  9356. return this.parseIdentifier(true);
  9357. }
  9358. }
  9359. parsePrivateName() {
  9360. const node = this.startNode();
  9361. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart));
  9362. const name = this.state.value;
  9363. this.next();
  9364. node.id = this.createIdentifier(id, name);
  9365. return this.finishNode(node, "PrivateName");
  9366. }
  9367. parseFunctionOrFunctionSent() {
  9368. const node = this.startNode();
  9369. this.next();
  9370. if (this.prodParam.hasYield && this.match(types$1.dot)) {
  9371. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  9372. this.next();
  9373. return this.parseMetaProperty(node, meta, "sent");
  9374. }
  9375. return this.parseFunction(node);
  9376. }
  9377. parseMetaProperty(node, meta, propertyName) {
  9378. node.meta = meta;
  9379. if (meta.name === "function" && propertyName === "sent") {
  9380. if (this.isContextual(propertyName)) {
  9381. this.expectPlugin("functionSent");
  9382. } else if (!this.hasPlugin("functionSent")) {
  9383. this.unexpected();
  9384. }
  9385. }
  9386. const containsEsc = this.state.containsEsc;
  9387. node.property = this.parseIdentifier(true);
  9388. if (node.property.name !== propertyName || containsEsc) {
  9389. this.raise(node.property.start, ErrorMessages.UnsupportedMetaProperty, meta.name, propertyName);
  9390. }
  9391. return this.finishNode(node, "MetaProperty");
  9392. }
  9393. parseImportMetaProperty(node) {
  9394. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  9395. this.next();
  9396. if (this.isContextual("meta")) {
  9397. if (!this.inModule) {
  9398. this.raise(id.start, SourceTypeModuleErrorMessages.ImportMetaOutsideModule);
  9399. }
  9400. this.sawUnambiguousESM = true;
  9401. }
  9402. return this.parseMetaProperty(node, id, "meta");
  9403. }
  9404. parseLiteral(value, type, startPos, startLoc) {
  9405. startPos = startPos || this.state.start;
  9406. startLoc = startLoc || this.state.startLoc;
  9407. const node = this.startNodeAt(startPos, startLoc);
  9408. this.addExtra(node, "rawValue", value);
  9409. this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
  9410. node.value = value;
  9411. this.next();
  9412. return this.finishNode(node, type);
  9413. }
  9414. parseParenAndDistinguishExpression(canBeArrow) {
  9415. const startPos = this.state.start;
  9416. const startLoc = this.state.startLoc;
  9417. let val;
  9418. this.next();
  9419. this.expressionScope.enter(newArrowHeadScope());
  9420. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9421. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9422. this.state.maybeInArrowParameters = true;
  9423. this.state.inFSharpPipelineDirectBody = false;
  9424. const innerStartPos = this.state.start;
  9425. const innerStartLoc = this.state.startLoc;
  9426. const exprList = [];
  9427. const refExpressionErrors = new ExpressionErrors();
  9428. const refNeedsArrowPos = {
  9429. start: 0
  9430. };
  9431. let first = true;
  9432. let spreadStart;
  9433. let optionalCommaStart;
  9434. while (!this.match(types$1.parenR)) {
  9435. if (first) {
  9436. first = false;
  9437. } else {
  9438. this.expect(types$1.comma, refNeedsArrowPos.start || null);
  9439. if (this.match(types$1.parenR)) {
  9440. optionalCommaStart = this.state.start;
  9441. break;
  9442. }
  9443. }
  9444. if (this.match(types$1.ellipsis)) {
  9445. const spreadNodeStartPos = this.state.start;
  9446. const spreadNodeStartLoc = this.state.startLoc;
  9447. spreadStart = this.state.start;
  9448. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  9449. this.checkCommaAfterRest(41);
  9450. break;
  9451. } else {
  9452. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos));
  9453. }
  9454. }
  9455. const innerEndPos = this.state.lastTokEnd;
  9456. const innerEndLoc = this.state.lastTokEndLoc;
  9457. this.expect(types$1.parenR);
  9458. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9459. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9460. let arrowNode = this.startNodeAt(startPos, startLoc);
  9461. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  9462. this.expressionScope.validateAsPattern();
  9463. this.expressionScope.exit();
  9464. this.parseArrowExpression(arrowNode, exprList, false);
  9465. return arrowNode;
  9466. }
  9467. this.expressionScope.exit();
  9468. if (!exprList.length) {
  9469. this.unexpected(this.state.lastTokStart);
  9470. }
  9471. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  9472. if (spreadStart) this.unexpected(spreadStart);
  9473. this.checkExpressionErrors(refExpressionErrors, true);
  9474. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  9475. this.toReferencedListDeep(exprList, true);
  9476. if (exprList.length > 1) {
  9477. val = this.startNodeAt(innerStartPos, innerStartLoc);
  9478. val.expressions = exprList;
  9479. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  9480. } else {
  9481. val = exprList[0];
  9482. }
  9483. if (!this.options.createParenthesizedExpressions) {
  9484. this.addExtra(val, "parenthesized", true);
  9485. this.addExtra(val, "parenStart", startPos);
  9486. return val;
  9487. }
  9488. const parenExpression = this.startNodeAt(startPos, startLoc);
  9489. parenExpression.expression = val;
  9490. this.finishNode(parenExpression, "ParenthesizedExpression");
  9491. return parenExpression;
  9492. }
  9493. shouldParseArrow() {
  9494. return !this.canInsertSemicolon();
  9495. }
  9496. parseArrow(node) {
  9497. if (this.eat(types$1.arrow)) {
  9498. return node;
  9499. }
  9500. }
  9501. parseParenItem(node, startPos, startLoc) {
  9502. return node;
  9503. }
  9504. parseNewOrNewTarget() {
  9505. const node = this.startNode();
  9506. this.next();
  9507. if (this.match(types$1.dot)) {
  9508. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  9509. this.next();
  9510. const metaProp = this.parseMetaProperty(node, meta, "target");
  9511. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  9512. this.raise(metaProp.start, ErrorMessages.UnexpectedNewTarget);
  9513. }
  9514. return metaProp;
  9515. }
  9516. return this.parseNew(node);
  9517. }
  9518. parseNew(node) {
  9519. node.callee = this.parseNoCallExpr();
  9520. if (node.callee.type === "Import") {
  9521. this.raise(node.callee.start, ErrorMessages.ImportCallNotNewExpression);
  9522. } else if (this.isOptionalChain(node.callee)) {
  9523. this.raise(this.state.lastTokEnd, ErrorMessages.OptionalChainingNoNew);
  9524. } else if (this.eat(types$1.questionDot)) {
  9525. this.raise(this.state.start, ErrorMessages.OptionalChainingNoNew);
  9526. }
  9527. this.parseNewArguments(node);
  9528. return this.finishNode(node, "NewExpression");
  9529. }
  9530. parseNewArguments(node) {
  9531. if (this.eat(types$1.parenL)) {
  9532. const args = this.parseExprList(types$1.parenR);
  9533. this.toReferencedList(args);
  9534. node.arguments = args;
  9535. } else {
  9536. node.arguments = [];
  9537. }
  9538. }
  9539. parseTemplateElement(isTagged) {
  9540. const elem = this.startNode();
  9541. if (this.state.value === null) {
  9542. if (!isTagged) {
  9543. this.raise(this.state.start + 1, ErrorMessages.InvalidEscapeSequenceTemplate);
  9544. }
  9545. }
  9546. elem.value = {
  9547. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  9548. cooked: this.state.value
  9549. };
  9550. this.next();
  9551. elem.tail = this.match(types$1.backQuote);
  9552. return this.finishNode(elem, "TemplateElement");
  9553. }
  9554. parseTemplate(isTagged) {
  9555. const node = this.startNode();
  9556. this.next();
  9557. node.expressions = [];
  9558. let curElt = this.parseTemplateElement(isTagged);
  9559. node.quasis = [curElt];
  9560. while (!curElt.tail) {
  9561. this.expect(types$1.dollarBraceL);
  9562. node.expressions.push(this.parseTemplateSubstitution());
  9563. this.expect(types$1.braceR);
  9564. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  9565. }
  9566. this.next();
  9567. return this.finishNode(node, "TemplateLiteral");
  9568. }
  9569. parseTemplateSubstitution() {
  9570. return this.parseExpression();
  9571. }
  9572. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  9573. if (isRecord) {
  9574. this.expectPlugin("recordAndTuple");
  9575. }
  9576. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9577. this.state.inFSharpPipelineDirectBody = false;
  9578. const propHash = Object.create(null);
  9579. let first = true;
  9580. const node = this.startNode();
  9581. node.properties = [];
  9582. this.next();
  9583. while (!this.match(close)) {
  9584. if (first) {
  9585. first = false;
  9586. } else {
  9587. this.expect(types$1.comma);
  9588. if (this.match(close)) {
  9589. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  9590. break;
  9591. }
  9592. }
  9593. const prop = this.parsePropertyDefinition(isPattern, refExpressionErrors);
  9594. if (!isPattern) {
  9595. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  9596. }
  9597. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  9598. this.raise(prop.start, ErrorMessages.InvalidRecordProperty);
  9599. }
  9600. if (prop.shorthand) {
  9601. this.addExtra(prop, "shorthand", true);
  9602. }
  9603. node.properties.push(prop);
  9604. }
  9605. this.state.exprAllowed = false;
  9606. this.next();
  9607. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9608. let type = "ObjectExpression";
  9609. if (isPattern) {
  9610. type = "ObjectPattern";
  9611. } else if (isRecord) {
  9612. type = "RecordExpression";
  9613. }
  9614. return this.finishNode(node, type);
  9615. }
  9616. maybeAsyncOrAccessorProp(prop) {
  9617. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(types$1.bracketL) || this.match(types$1.star));
  9618. }
  9619. parsePropertyDefinition(isPattern, refExpressionErrors) {
  9620. let decorators = [];
  9621. if (this.match(types$1.at)) {
  9622. if (this.hasPlugin("decorators")) {
  9623. this.raise(this.state.start, ErrorMessages.UnsupportedPropertyDecorator);
  9624. }
  9625. while (this.match(types$1.at)) {
  9626. decorators.push(this.parseDecorator());
  9627. }
  9628. }
  9629. const prop = this.startNode();
  9630. let isGenerator = false;
  9631. let isAsync = false;
  9632. let isAccessor = false;
  9633. let startPos;
  9634. let startLoc;
  9635. if (this.match(types$1.ellipsis)) {
  9636. if (decorators.length) this.unexpected();
  9637. if (isPattern) {
  9638. this.next();
  9639. prop.argument = this.parseIdentifier();
  9640. this.checkCommaAfterRest(125);
  9641. return this.finishNode(prop, "RestElement");
  9642. }
  9643. return this.parseSpread();
  9644. }
  9645. if (decorators.length) {
  9646. prop.decorators = decorators;
  9647. decorators = [];
  9648. }
  9649. prop.method = false;
  9650. if (isPattern || refExpressionErrors) {
  9651. startPos = this.state.start;
  9652. startLoc = this.state.startLoc;
  9653. }
  9654. if (!isPattern) {
  9655. isGenerator = this.eat(types$1.star);
  9656. }
  9657. const containsEsc = this.state.containsEsc;
  9658. const key = this.parsePropertyName(prop, false);
  9659. if (!isPattern && !isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  9660. const keyName = key.name;
  9661. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  9662. isAsync = true;
  9663. isGenerator = this.eat(types$1.star);
  9664. this.parsePropertyName(prop, false);
  9665. }
  9666. if (keyName === "get" || keyName === "set") {
  9667. isAccessor = true;
  9668. prop.kind = keyName;
  9669. if (this.match(types$1.star)) {
  9670. isGenerator = true;
  9671. this.raise(this.state.pos, ErrorMessages.AccessorIsGenerator, keyName);
  9672. this.next();
  9673. }
  9674. this.parsePropertyName(prop, false);
  9675. }
  9676. }
  9677. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9678. return prop;
  9679. }
  9680. getGetterSetterExpectedParamCount(method) {
  9681. return method.kind === "get" ? 0 : 1;
  9682. }
  9683. getObjectOrClassMethodParams(method) {
  9684. return method.params;
  9685. }
  9686. checkGetterSetterParams(method) {
  9687. var _params;
  9688. const paramCount = this.getGetterSetterExpectedParamCount(method);
  9689. const params = this.getObjectOrClassMethodParams(method);
  9690. const start = method.start;
  9691. if (params.length !== paramCount) {
  9692. if (method.kind === "get") {
  9693. this.raise(start, ErrorMessages.BadGetterArity);
  9694. } else {
  9695. this.raise(start, ErrorMessages.BadSetterArity);
  9696. }
  9697. }
  9698. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  9699. this.raise(start, ErrorMessages.BadSetterRestParameter);
  9700. }
  9701. }
  9702. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  9703. if (isAccessor) {
  9704. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  9705. this.checkGetterSetterParams(prop);
  9706. return prop;
  9707. }
  9708. if (isAsync || isGenerator || this.match(types$1.parenL)) {
  9709. if (isPattern) this.unexpected();
  9710. prop.kind = "method";
  9711. prop.method = true;
  9712. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  9713. }
  9714. }
  9715. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  9716. prop.shorthand = false;
  9717. if (this.eat(types$1.colon)) {
  9718. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  9719. return this.finishNode(prop, "ObjectProperty");
  9720. }
  9721. if (!prop.computed && prop.key.type === "Identifier") {
  9722. this.checkReservedWord(prop.key.name, prop.key.start, true, false);
  9723. if (isPattern) {
  9724. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9725. } else if (this.match(types$1.eq) && refExpressionErrors) {
  9726. if (refExpressionErrors.shorthandAssign === -1) {
  9727. refExpressionErrors.shorthandAssign = this.state.start;
  9728. }
  9729. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9730. } else {
  9731. prop.value = prop.key.__clone();
  9732. }
  9733. prop.shorthand = true;
  9734. return this.finishNode(prop, "ObjectProperty");
  9735. }
  9736. }
  9737. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9738. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  9739. if (!node) this.unexpected();
  9740. return node;
  9741. }
  9742. parsePropertyName(prop, isPrivateNameAllowed) {
  9743. if (this.eat(types$1.bracketL)) {
  9744. prop.computed = true;
  9745. prop.key = this.parseMaybeAssignAllowIn();
  9746. this.expect(types$1.bracketR);
  9747. } else {
  9748. const oldInPropertyName = this.state.inPropertyName;
  9749. this.state.inPropertyName = true;
  9750. const type = this.state.type;
  9751. prop.key = type === types$1.num || type === types$1.string || type === types$1.bigint || type === types$1.decimal ? this.parseExprAtom() : this.parseMaybePrivateName(isPrivateNameAllowed);
  9752. if (type !== types$1.privateName) {
  9753. prop.computed = false;
  9754. }
  9755. this.state.inPropertyName = oldInPropertyName;
  9756. }
  9757. return prop.key;
  9758. }
  9759. initFunction(node, isAsync) {
  9760. node.id = null;
  9761. node.generator = false;
  9762. node.async = !!isAsync;
  9763. }
  9764. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  9765. this.initFunction(node, isAsync);
  9766. node.generator = !!isGenerator;
  9767. const allowModifiers = isConstructor;
  9768. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  9769. this.prodParam.enter(functionFlags(isAsync, node.generator));
  9770. this.parseFunctionParams(node, allowModifiers);
  9771. this.parseFunctionBodyAndFinish(node, type, true);
  9772. this.prodParam.exit();
  9773. this.scope.exit();
  9774. return node;
  9775. }
  9776. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9777. if (isTuple) {
  9778. this.expectPlugin("recordAndTuple");
  9779. }
  9780. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9781. this.state.inFSharpPipelineDirectBody = false;
  9782. const node = this.startNode();
  9783. this.next();
  9784. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  9785. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9786. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  9787. }
  9788. parseArrowExpression(node, params, isAsync, trailingCommaPos) {
  9789. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  9790. let flags = functionFlags(isAsync, false);
  9791. if (!this.match(types$1.bracketL) && this.prodParam.hasIn) {
  9792. flags |= PARAM_IN;
  9793. }
  9794. this.prodParam.enter(flags);
  9795. this.initFunction(node, isAsync);
  9796. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9797. if (params) {
  9798. this.state.maybeInArrowParameters = true;
  9799. this.setArrowFunctionParameters(node, params, trailingCommaPos);
  9800. }
  9801. this.state.maybeInArrowParameters = false;
  9802. this.parseFunctionBody(node, true);
  9803. this.prodParam.exit();
  9804. this.scope.exit();
  9805. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9806. return this.finishNode(node, "ArrowFunctionExpression");
  9807. }
  9808. setArrowFunctionParameters(node, params, trailingCommaPos) {
  9809. node.params = this.toAssignableList(params, trailingCommaPos, false);
  9810. }
  9811. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9812. this.parseFunctionBody(node, false, isMethod);
  9813. this.finishNode(node, type);
  9814. }
  9815. parseFunctionBody(node, allowExpression, isMethod = false) {
  9816. const isExpression = allowExpression && !this.match(types$1.braceL);
  9817. this.expressionScope.enter(newExpressionScope());
  9818. if (isExpression) {
  9819. node.body = this.parseMaybeAssign();
  9820. this.checkParams(node, false, allowExpression, false);
  9821. } else {
  9822. const oldStrict = this.state.strict;
  9823. const oldLabels = this.state.labels;
  9824. this.state.labels = [];
  9825. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  9826. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  9827. const nonSimple = !this.isSimpleParamList(node.params);
  9828. if (hasStrictModeDirective && nonSimple) {
  9829. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  9830. this.raise(errorPos, ErrorMessages.IllegalLanguageModeDirective);
  9831. }
  9832. const strictModeChanged = !oldStrict && this.state.strict;
  9833. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  9834. if (this.state.strict && node.id) {
  9835. this.checkLVal(node.id, "function name", BIND_OUTSIDE, undefined, undefined, strictModeChanged);
  9836. }
  9837. });
  9838. this.prodParam.exit();
  9839. this.expressionScope.exit();
  9840. this.state.labels = oldLabels;
  9841. }
  9842. }
  9843. isSimpleParamList(params) {
  9844. for (let i = 0, len = params.length; i < len; i++) {
  9845. if (params[i].type !== "Identifier") return false;
  9846. }
  9847. return true;
  9848. }
  9849. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  9850. const checkClashes = new Set();
  9851. for (const param of node.params) {
  9852. this.checkLVal(param, "function parameter list", BIND_VAR, allowDuplicates ? null : checkClashes, undefined, strictModeChanged);
  9853. }
  9854. }
  9855. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  9856. const elts = [];
  9857. let first = true;
  9858. while (!this.eat(close)) {
  9859. if (first) {
  9860. first = false;
  9861. } else {
  9862. this.expect(types$1.comma);
  9863. if (this.match(close)) {
  9864. if (nodeForExtra) {
  9865. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9866. }
  9867. this.next();
  9868. break;
  9869. }
  9870. }
  9871. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  9872. }
  9873. return elts;
  9874. }
  9875. parseExprListItem(allowEmpty, refExpressionErrors, refNeedsArrowPos, allowPlaceholder) {
  9876. let elt;
  9877. if (this.match(types$1.comma)) {
  9878. if (!allowEmpty) {
  9879. this.raise(this.state.pos, ErrorMessages.UnexpectedToken, ",");
  9880. }
  9881. elt = null;
  9882. } else if (this.match(types$1.ellipsis)) {
  9883. const spreadNodeStartPos = this.state.start;
  9884. const spreadNodeStartLoc = this.state.startLoc;
  9885. elt = this.parseParenItem(this.parseSpread(refExpressionErrors, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
  9886. } else if (this.match(types$1.question)) {
  9887. this.expectPlugin("partialApplication");
  9888. if (!allowPlaceholder) {
  9889. this.raise(this.state.start, ErrorMessages.UnexpectedArgumentPlaceholder);
  9890. }
  9891. const node = this.startNode();
  9892. this.next();
  9893. elt = this.finishNode(node, "ArgumentPlaceholder");
  9894. } else {
  9895. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos);
  9896. }
  9897. return elt;
  9898. }
  9899. parseIdentifier(liberal) {
  9900. const node = this.startNode();
  9901. const name = this.parseIdentifierName(node.start, liberal);
  9902. return this.createIdentifier(node, name);
  9903. }
  9904. createIdentifier(node, name) {
  9905. node.name = name;
  9906. node.loc.identifierName = name;
  9907. return this.finishNode(node, "Identifier");
  9908. }
  9909. parseIdentifierName(pos, liberal) {
  9910. let name;
  9911. const {
  9912. start,
  9913. type
  9914. } = this.state;
  9915. if (type === types$1.name) {
  9916. name = this.state.value;
  9917. } else if (type.keyword) {
  9918. name = type.keyword;
  9919. const curContext = this.curContext();
  9920. if ((type === types$1._class || type === types$1._function) && (curContext === types.functionStatement || curContext === types.functionExpression)) {
  9921. this.state.context.pop();
  9922. }
  9923. } else {
  9924. throw this.unexpected();
  9925. }
  9926. if (liberal) {
  9927. this.state.type = types$1.name;
  9928. } else {
  9929. this.checkReservedWord(name, start, !!type.keyword, false);
  9930. }
  9931. this.next();
  9932. return name;
  9933. }
  9934. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9935. if (this.prodParam.hasYield && word === "yield") {
  9936. this.raise(startLoc, ErrorMessages.YieldBindingIdentifier);
  9937. return;
  9938. }
  9939. if (word === "await") {
  9940. if (this.prodParam.hasAwait) {
  9941. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9942. return;
  9943. } else if (this.scope.inStaticBlock && !this.scope.inNonArrowFunction) {
  9944. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifierInStaticBlock);
  9945. return;
  9946. } else {
  9947. this.expressionScope.recordAsyncArrowParametersError(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9948. }
  9949. }
  9950. if (this.scope.inClass && !this.scope.inNonArrowFunction && word === "arguments") {
  9951. this.raise(startLoc, ErrorMessages.ArgumentsInClass);
  9952. return;
  9953. }
  9954. if (checkKeywords && isKeyword(word)) {
  9955. this.raise(startLoc, ErrorMessages.UnexpectedKeyword, word);
  9956. return;
  9957. }
  9958. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  9959. if (reservedTest(word, this.inModule)) {
  9960. this.raise(startLoc, ErrorMessages.UnexpectedReservedWord, word);
  9961. }
  9962. }
  9963. isAwaitAllowed() {
  9964. if (this.prodParam.hasAwait) return true;
  9965. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  9966. return true;
  9967. }
  9968. return false;
  9969. }
  9970. parseAwait(startPos, startLoc) {
  9971. const node = this.startNodeAt(startPos, startLoc);
  9972. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.AwaitExpressionFormalParameter);
  9973. if (this.eat(types$1.star)) {
  9974. this.raise(node.start, ErrorMessages.ObsoleteAwaitStar);
  9975. }
  9976. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  9977. if (this.isAmbiguousAwait()) {
  9978. this.ambiguousScriptDifferentAst = true;
  9979. } else {
  9980. this.sawUnambiguousESM = true;
  9981. }
  9982. }
  9983. if (!this.state.soloAwait) {
  9984. node.argument = this.parseMaybeUnary(null, true);
  9985. }
  9986. return this.finishNode(node, "AwaitExpression");
  9987. }
  9988. isAmbiguousAwait() {
  9989. return this.hasPrecedingLineBreak() || this.match(types$1.plusMin) || this.match(types$1.parenL) || this.match(types$1.bracketL) || this.match(types$1.backQuote) || this.match(types$1.regexp) || this.match(types$1.slash) || this.hasPlugin("v8intrinsic") && this.match(types$1.modulo);
  9990. }
  9991. parseYield() {
  9992. const node = this.startNode();
  9993. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.YieldInParameter);
  9994. this.next();
  9995. if (this.match(types$1.semi) || !this.match(types$1.star) && !this.state.type.startsExpr || this.hasPrecedingLineBreak()) {
  9996. node.delegate = false;
  9997. node.argument = null;
  9998. } else {
  9999. node.delegate = this.eat(types$1.star);
  10000. node.argument = this.parseMaybeAssign();
  10001. }
  10002. return this.finishNode(node, "YieldExpression");
  10003. }
  10004. checkPipelineAtInfixOperator(left, leftStartPos) {
  10005. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  10006. if (left.type === "SequenceExpression") {
  10007. this.raise(leftStartPos, ErrorMessages.PipelineHeadSequenceExpression);
  10008. }
  10009. }
  10010. }
  10011. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  10012. this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);
  10013. return this.parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc);
  10014. }
  10015. checkSmartPipelineBodyEarlyErrors(childExpression, startPos) {
  10016. if (this.match(types$1.arrow)) {
  10017. throw this.raise(this.state.start, ErrorMessages.PipelineBodyNoArrow);
  10018. } else if (childExpression.type === "SequenceExpression") {
  10019. this.raise(startPos, ErrorMessages.PipelineBodySequenceExpression);
  10020. }
  10021. }
  10022. parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc) {
  10023. const bodyNode = this.startNodeAt(startPos, startLoc);
  10024. const isSimpleReference = this.isSimpleReference(childExpression);
  10025. if (isSimpleReference) {
  10026. bodyNode.callee = childExpression;
  10027. } else {
  10028. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  10029. this.raise(startPos, ErrorMessages.PipelineTopicUnused);
  10030. }
  10031. bodyNode.expression = childExpression;
  10032. }
  10033. return this.finishNode(bodyNode, isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression");
  10034. }
  10035. isSimpleReference(expression) {
  10036. switch (expression.type) {
  10037. case "MemberExpression":
  10038. return !expression.computed && this.isSimpleReference(expression.object);
  10039. case "Identifier":
  10040. return true;
  10041. default:
  10042. return false;
  10043. }
  10044. }
  10045. withTopicPermittingContext(callback) {
  10046. const outerContextTopicState = this.state.topicContext;
  10047. this.state.topicContext = {
  10048. maxNumOfResolvableTopics: 1,
  10049. maxTopicIndex: null
  10050. };
  10051. try {
  10052. return callback();
  10053. } finally {
  10054. this.state.topicContext = outerContextTopicState;
  10055. }
  10056. }
  10057. withTopicForbiddingContext(callback) {
  10058. const outerContextTopicState = this.state.topicContext;
  10059. this.state.topicContext = {
  10060. maxNumOfResolvableTopics: 0,
  10061. maxTopicIndex: null
  10062. };
  10063. try {
  10064. return callback();
  10065. } finally {
  10066. this.state.topicContext = outerContextTopicState;
  10067. }
  10068. }
  10069. withSoloAwaitPermittingContext(callback) {
  10070. const outerContextSoloAwaitState = this.state.soloAwait;
  10071. this.state.soloAwait = true;
  10072. try {
  10073. return callback();
  10074. } finally {
  10075. this.state.soloAwait = outerContextSoloAwaitState;
  10076. }
  10077. }
  10078. allowInAnd(callback) {
  10079. const flags = this.prodParam.currentFlags();
  10080. const prodParamToSet = PARAM_IN & ~flags;
  10081. if (prodParamToSet) {
  10082. this.prodParam.enter(flags | PARAM_IN);
  10083. try {
  10084. return callback();
  10085. } finally {
  10086. this.prodParam.exit();
  10087. }
  10088. }
  10089. return callback();
  10090. }
  10091. disallowInAnd(callback) {
  10092. const flags = this.prodParam.currentFlags();
  10093. const prodParamToClear = PARAM_IN & flags;
  10094. if (prodParamToClear) {
  10095. this.prodParam.enter(flags & ~PARAM_IN);
  10096. try {
  10097. return callback();
  10098. } finally {
  10099. this.prodParam.exit();
  10100. }
  10101. }
  10102. return callback();
  10103. }
  10104. registerTopicReference() {
  10105. this.state.topicContext.maxTopicIndex = 0;
  10106. }
  10107. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  10108. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  10109. }
  10110. topicReferenceWasUsedInCurrentTopicContext() {
  10111. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  10112. }
  10113. parseFSharpPipelineBody(prec) {
  10114. const startPos = this.state.start;
  10115. const startLoc = this.state.startLoc;
  10116. this.state.potentialArrowAt = this.state.start;
  10117. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10118. this.state.inFSharpPipelineDirectBody = true;
  10119. const ret = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec);
  10120. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10121. return ret;
  10122. }
  10123. parseModuleExpression() {
  10124. this.expectPlugin("moduleBlocks");
  10125. const node = this.startNode();
  10126. this.next();
  10127. this.eat(types$1.braceL);
  10128. const revertScopes = this.initializeScopes(true);
  10129. this.enterInitialScopes();
  10130. const program = this.startNode();
  10131. try {
  10132. node.body = this.parseProgram(program, types$1.braceR, "module");
  10133. } finally {
  10134. revertScopes();
  10135. }
  10136. this.eat(types$1.braceR);
  10137. return this.finishNode(node, "ModuleExpression");
  10138. }
  10139. }
  10140. const loopLabel = {
  10141. kind: "loop"
  10142. },
  10143. switchLabel = {
  10144. kind: "switch"
  10145. };
  10146. const FUNC_NO_FLAGS = 0b000,
  10147. FUNC_STATEMENT = 0b001,
  10148. FUNC_HANGING_STATEMENT = 0b010,
  10149. FUNC_NULLABLE_ID = 0b100;
  10150. const loneSurrogate = /[\uD800-\uDFFF]/u;
  10151. function babel7CompatTokens(tokens) {
  10152. {
  10153. for (let i = 0; i < tokens.length; i++) {
  10154. const token = tokens[i];
  10155. if (token.type === types$1.privateName) {
  10156. const {
  10157. loc,
  10158. start,
  10159. value,
  10160. end
  10161. } = token;
  10162. const hashEndPos = start + 1;
  10163. const hashEndLoc = new Position(loc.start.line, loc.start.column + 1);
  10164. tokens.splice(i, 1, new Token({
  10165. type: types$1.hash,
  10166. value: "#",
  10167. start: start,
  10168. end: hashEndPos,
  10169. startLoc: loc.start,
  10170. endLoc: hashEndLoc
  10171. }), new Token({
  10172. type: types$1.name,
  10173. value: value,
  10174. start: hashEndPos,
  10175. end: end,
  10176. startLoc: hashEndLoc,
  10177. endLoc: loc.end
  10178. }));
  10179. }
  10180. }
  10181. }
  10182. return tokens;
  10183. }
  10184. class StatementParser extends ExpressionParser {
  10185. parseTopLevel(file, program) {
  10186. file.program = this.parseProgram(program);
  10187. file.comments = this.state.comments;
  10188. if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens);
  10189. return this.finishNode(file, "File");
  10190. }
  10191. parseProgram(program, end = types$1.eof, sourceType = this.options.sourceType) {
  10192. program.sourceType = sourceType;
  10193. program.interpreter = this.parseInterpreterDirective();
  10194. this.parseBlockBody(program, true, true, end);
  10195. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  10196. for (const [name] of Array.from(this.scope.undefinedExports)) {
  10197. const pos = this.scope.undefinedExports.get(name);
  10198. this.raise(pos, ErrorMessages.ModuleExportUndefined, name);
  10199. }
  10200. }
  10201. return this.finishNode(program, "Program");
  10202. }
  10203. stmtToDirective(stmt) {
  10204. const expr = stmt.expression;
  10205. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  10206. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  10207. const raw = this.input.slice(expr.start, expr.end);
  10208. const val = directiveLiteral.value = raw.slice(1, -1);
  10209. this.addExtra(directiveLiteral, "raw", raw);
  10210. this.addExtra(directiveLiteral, "rawValue", val);
  10211. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  10212. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  10213. }
  10214. parseInterpreterDirective() {
  10215. if (!this.match(types$1.interpreterDirective)) {
  10216. return null;
  10217. }
  10218. const node = this.startNode();
  10219. node.value = this.state.value;
  10220. this.next();
  10221. return this.finishNode(node, "InterpreterDirective");
  10222. }
  10223. isLet(context) {
  10224. if (!this.isContextual("let")) {
  10225. return false;
  10226. }
  10227. return this.isLetKeyword(context);
  10228. }
  10229. isLetKeyword(context) {
  10230. const next = this.nextTokenStart();
  10231. const nextCh = this.input.charCodeAt(next);
  10232. if (nextCh === 91) return true;
  10233. if (context) return false;
  10234. if (nextCh === 123) return true;
  10235. if (isIdentifierStart(nextCh)) {
  10236. let pos = next + 1;
  10237. while (isIdentifierChar(this.input.charCodeAt(pos))) {
  10238. ++pos;
  10239. }
  10240. const ident = this.input.slice(next, pos);
  10241. if (!keywordRelationalOperator.test(ident)) return true;
  10242. }
  10243. return false;
  10244. }
  10245. parseStatement(context, topLevel) {
  10246. if (this.match(types$1.at)) {
  10247. this.parseDecorators(true);
  10248. }
  10249. return this.parseStatementContent(context, topLevel);
  10250. }
  10251. parseStatementContent(context, topLevel) {
  10252. let starttype = this.state.type;
  10253. const node = this.startNode();
  10254. let kind;
  10255. if (this.isLet(context)) {
  10256. starttype = types$1._var;
  10257. kind = "let";
  10258. }
  10259. switch (starttype) {
  10260. case types$1._break:
  10261. case types$1._continue:
  10262. return this.parseBreakContinueStatement(node, starttype.keyword);
  10263. case types$1._debugger:
  10264. return this.parseDebuggerStatement(node);
  10265. case types$1._do:
  10266. return this.parseDoStatement(node);
  10267. case types$1._for:
  10268. return this.parseForStatement(node);
  10269. case types$1._function:
  10270. if (this.lookaheadCharCode() === 46) break;
  10271. if (context) {
  10272. if (this.state.strict) {
  10273. this.raise(this.state.start, ErrorMessages.StrictFunction);
  10274. } else if (context !== "if" && context !== "label") {
  10275. this.raise(this.state.start, ErrorMessages.SloppyFunction);
  10276. }
  10277. }
  10278. return this.parseFunctionStatement(node, false, !context);
  10279. case types$1._class:
  10280. if (context) this.unexpected();
  10281. return this.parseClass(node, true);
  10282. case types$1._if:
  10283. return this.parseIfStatement(node);
  10284. case types$1._return:
  10285. return this.parseReturnStatement(node);
  10286. case types$1._switch:
  10287. return this.parseSwitchStatement(node);
  10288. case types$1._throw:
  10289. return this.parseThrowStatement(node);
  10290. case types$1._try:
  10291. return this.parseTryStatement(node);
  10292. case types$1._const:
  10293. case types$1._var:
  10294. kind = kind || this.state.value;
  10295. if (context && kind !== "var") {
  10296. this.raise(this.state.start, ErrorMessages.UnexpectedLexicalDeclaration);
  10297. }
  10298. return this.parseVarStatement(node, kind);
  10299. case types$1._while:
  10300. return this.parseWhileStatement(node);
  10301. case types$1._with:
  10302. return this.parseWithStatement(node);
  10303. case types$1.braceL:
  10304. return this.parseBlock();
  10305. case types$1.semi:
  10306. return this.parseEmptyStatement(node);
  10307. case types$1._import:
  10308. {
  10309. const nextTokenCharCode = this.lookaheadCharCode();
  10310. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  10311. break;
  10312. }
  10313. }
  10314. case types$1._export:
  10315. {
  10316. if (!this.options.allowImportExportEverywhere && !topLevel) {
  10317. this.raise(this.state.start, ErrorMessages.UnexpectedImportExport);
  10318. }
  10319. this.next();
  10320. let result;
  10321. if (starttype === types$1._import) {
  10322. result = this.parseImport(node);
  10323. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  10324. this.sawUnambiguousESM = true;
  10325. }
  10326. } else {
  10327. result = this.parseExport(node);
  10328. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  10329. this.sawUnambiguousESM = true;
  10330. }
  10331. }
  10332. this.assertModuleNodeAllowed(node);
  10333. return result;
  10334. }
  10335. default:
  10336. {
  10337. if (this.isAsyncFunction()) {
  10338. if (context) {
  10339. this.raise(this.state.start, ErrorMessages.AsyncFunctionInSingleStatementContext);
  10340. }
  10341. this.next();
  10342. return this.parseFunctionStatement(node, true, !context);
  10343. }
  10344. }
  10345. }
  10346. const maybeName = this.state.value;
  10347. const expr = this.parseExpression();
  10348. if (starttype === types$1.name && expr.type === "Identifier" && this.eat(types$1.colon)) {
  10349. return this.parseLabeledStatement(node, maybeName, expr, context);
  10350. } else {
  10351. return this.parseExpressionStatement(node, expr);
  10352. }
  10353. }
  10354. assertModuleNodeAllowed(node) {
  10355. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  10356. this.raise(node.start, SourceTypeModuleErrorMessages.ImportOutsideModule);
  10357. }
  10358. }
  10359. takeDecorators(node) {
  10360. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10361. if (decorators.length) {
  10362. node.decorators = decorators;
  10363. this.resetStartLocationFromNode(node, decorators[0]);
  10364. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  10365. }
  10366. }
  10367. canHaveLeadingDecorator() {
  10368. return this.match(types$1._class);
  10369. }
  10370. parseDecorators(allowExport) {
  10371. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10372. while (this.match(types$1.at)) {
  10373. const decorator = this.parseDecorator();
  10374. currentContextDecorators.push(decorator);
  10375. }
  10376. if (this.match(types$1._export)) {
  10377. if (!allowExport) {
  10378. this.unexpected();
  10379. }
  10380. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  10381. this.raise(this.state.start, ErrorMessages.DecoratorExportClass);
  10382. }
  10383. } else if (!this.canHaveLeadingDecorator()) {
  10384. throw this.raise(this.state.start, ErrorMessages.UnexpectedLeadingDecorator);
  10385. }
  10386. }
  10387. parseDecorator() {
  10388. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  10389. const node = this.startNode();
  10390. this.next();
  10391. if (this.hasPlugin("decorators")) {
  10392. this.state.decoratorStack.push([]);
  10393. const startPos = this.state.start;
  10394. const startLoc = this.state.startLoc;
  10395. let expr;
  10396. if (this.eat(types$1.parenL)) {
  10397. expr = this.parseExpression();
  10398. this.expect(types$1.parenR);
  10399. } else {
  10400. expr = this.parseIdentifier(false);
  10401. while (this.eat(types$1.dot)) {
  10402. const node = this.startNodeAt(startPos, startLoc);
  10403. node.object = expr;
  10404. node.property = this.parseIdentifier(true);
  10405. node.computed = false;
  10406. expr = this.finishNode(node, "MemberExpression");
  10407. }
  10408. }
  10409. node.expression = this.parseMaybeDecoratorArguments(expr);
  10410. this.state.decoratorStack.pop();
  10411. } else {
  10412. node.expression = this.parseExprSubscripts();
  10413. }
  10414. return this.finishNode(node, "Decorator");
  10415. }
  10416. parseMaybeDecoratorArguments(expr) {
  10417. if (this.eat(types$1.parenL)) {
  10418. const node = this.startNodeAtNode(expr);
  10419. node.callee = expr;
  10420. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  10421. this.toReferencedList(node.arguments);
  10422. return this.finishNode(node, "CallExpression");
  10423. }
  10424. return expr;
  10425. }
  10426. parseBreakContinueStatement(node, keyword) {
  10427. const isBreak = keyword === "break";
  10428. this.next();
  10429. if (this.isLineTerminator()) {
  10430. node.label = null;
  10431. } else {
  10432. node.label = this.parseIdentifier();
  10433. this.semicolon();
  10434. }
  10435. this.verifyBreakContinue(node, keyword);
  10436. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  10437. }
  10438. verifyBreakContinue(node, keyword) {
  10439. const isBreak = keyword === "break";
  10440. let i;
  10441. for (i = 0; i < this.state.labels.length; ++i) {
  10442. const lab = this.state.labels[i];
  10443. if (node.label == null || lab.name === node.label.name) {
  10444. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  10445. if (node.label && isBreak) break;
  10446. }
  10447. }
  10448. if (i === this.state.labels.length) {
  10449. this.raise(node.start, ErrorMessages.IllegalBreakContinue, keyword);
  10450. }
  10451. }
  10452. parseDebuggerStatement(node) {
  10453. this.next();
  10454. this.semicolon();
  10455. return this.finishNode(node, "DebuggerStatement");
  10456. }
  10457. parseHeaderExpression() {
  10458. this.expect(types$1.parenL);
  10459. const val = this.parseExpression();
  10460. this.expect(types$1.parenR);
  10461. return val;
  10462. }
  10463. parseDoStatement(node) {
  10464. this.next();
  10465. this.state.labels.push(loopLabel);
  10466. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  10467. this.state.labels.pop();
  10468. this.expect(types$1._while);
  10469. node.test = this.parseHeaderExpression();
  10470. this.eat(types$1.semi);
  10471. return this.finishNode(node, "DoWhileStatement");
  10472. }
  10473. parseForStatement(node) {
  10474. this.next();
  10475. this.state.labels.push(loopLabel);
  10476. let awaitAt = -1;
  10477. if (this.isAwaitAllowed() && this.eatContextual("await")) {
  10478. awaitAt = this.state.lastTokStart;
  10479. }
  10480. this.scope.enter(SCOPE_OTHER);
  10481. this.expect(types$1.parenL);
  10482. if (this.match(types$1.semi)) {
  10483. if (awaitAt > -1) {
  10484. this.unexpected(awaitAt);
  10485. }
  10486. return this.parseFor(node, null);
  10487. }
  10488. const startsWithLet = this.isContextual("let");
  10489. const isLet = startsWithLet && this.isLetKeyword();
  10490. if (this.match(types$1._var) || this.match(types$1._const) || isLet) {
  10491. const init = this.startNode();
  10492. const kind = isLet ? "let" : this.state.value;
  10493. this.next();
  10494. this.parseVar(init, true, kind);
  10495. this.finishNode(init, "VariableDeclaration");
  10496. if ((this.match(types$1._in) || this.isContextual("of")) && init.declarations.length === 1) {
  10497. return this.parseForIn(node, init, awaitAt);
  10498. }
  10499. if (awaitAt > -1) {
  10500. this.unexpected(awaitAt);
  10501. }
  10502. return this.parseFor(node, init);
  10503. }
  10504. const startsWithUnescapedName = this.match(types$1.name) && !this.state.containsEsc;
  10505. const refExpressionErrors = new ExpressionErrors();
  10506. const init = this.parseExpression(true, refExpressionErrors);
  10507. const isForOf = this.isContextual("of");
  10508. if (isForOf) {
  10509. if (startsWithLet) {
  10510. this.raise(init.start, ErrorMessages.ForOfLet);
  10511. } else if (awaitAt === -1 && startsWithUnescapedName && init.type === "Identifier" && init.name === "async") {
  10512. this.raise(init.start, ErrorMessages.ForOfAsync);
  10513. }
  10514. }
  10515. if (isForOf || this.match(types$1._in)) {
  10516. this.toAssignable(init, true);
  10517. const description = isForOf ? "for-of statement" : "for-in statement";
  10518. this.checkLVal(init, description);
  10519. return this.parseForIn(node, init, awaitAt);
  10520. } else {
  10521. this.checkExpressionErrors(refExpressionErrors, true);
  10522. }
  10523. if (awaitAt > -1) {
  10524. this.unexpected(awaitAt);
  10525. }
  10526. return this.parseFor(node, init);
  10527. }
  10528. parseFunctionStatement(node, isAsync, declarationPosition) {
  10529. this.next();
  10530. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  10531. }
  10532. parseIfStatement(node) {
  10533. this.next();
  10534. node.test = this.parseHeaderExpression();
  10535. node.consequent = this.parseStatement("if");
  10536. node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
  10537. return this.finishNode(node, "IfStatement");
  10538. }
  10539. parseReturnStatement(node) {
  10540. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  10541. this.raise(this.state.start, ErrorMessages.IllegalReturn);
  10542. }
  10543. this.next();
  10544. if (this.isLineTerminator()) {
  10545. node.argument = null;
  10546. } else {
  10547. node.argument = this.parseExpression();
  10548. this.semicolon();
  10549. }
  10550. return this.finishNode(node, "ReturnStatement");
  10551. }
  10552. parseSwitchStatement(node) {
  10553. this.next();
  10554. node.discriminant = this.parseHeaderExpression();
  10555. const cases = node.cases = [];
  10556. this.expect(types$1.braceL);
  10557. this.state.labels.push(switchLabel);
  10558. this.scope.enter(SCOPE_OTHER);
  10559. let cur;
  10560. for (let sawDefault; !this.match(types$1.braceR);) {
  10561. if (this.match(types$1._case) || this.match(types$1._default)) {
  10562. const isCase = this.match(types$1._case);
  10563. if (cur) this.finishNode(cur, "SwitchCase");
  10564. cases.push(cur = this.startNode());
  10565. cur.consequent = [];
  10566. this.next();
  10567. if (isCase) {
  10568. cur.test = this.parseExpression();
  10569. } else {
  10570. if (sawDefault) {
  10571. this.raise(this.state.lastTokStart, ErrorMessages.MultipleDefaultsInSwitch);
  10572. }
  10573. sawDefault = true;
  10574. cur.test = null;
  10575. }
  10576. this.expect(types$1.colon);
  10577. } else {
  10578. if (cur) {
  10579. cur.consequent.push(this.parseStatement(null));
  10580. } else {
  10581. this.unexpected();
  10582. }
  10583. }
  10584. }
  10585. this.scope.exit();
  10586. if (cur) this.finishNode(cur, "SwitchCase");
  10587. this.next();
  10588. this.state.labels.pop();
  10589. return this.finishNode(node, "SwitchStatement");
  10590. }
  10591. parseThrowStatement(node) {
  10592. this.next();
  10593. if (this.hasPrecedingLineBreak()) {
  10594. this.raise(this.state.lastTokEnd, ErrorMessages.NewlineAfterThrow);
  10595. }
  10596. node.argument = this.parseExpression();
  10597. this.semicolon();
  10598. return this.finishNode(node, "ThrowStatement");
  10599. }
  10600. parseCatchClauseParam() {
  10601. const param = this.parseBindingAtom();
  10602. const simple = param.type === "Identifier";
  10603. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  10604. this.checkLVal(param, "catch clause", BIND_LEXICAL);
  10605. return param;
  10606. }
  10607. parseTryStatement(node) {
  10608. this.next();
  10609. node.block = this.parseBlock();
  10610. node.handler = null;
  10611. if (this.match(types$1._catch)) {
  10612. const clause = this.startNode();
  10613. this.next();
  10614. if (this.match(types$1.parenL)) {
  10615. this.expect(types$1.parenL);
  10616. clause.param = this.parseCatchClauseParam();
  10617. this.expect(types$1.parenR);
  10618. } else {
  10619. clause.param = null;
  10620. this.scope.enter(SCOPE_OTHER);
  10621. }
  10622. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  10623. this.scope.exit();
  10624. node.handler = this.finishNode(clause, "CatchClause");
  10625. }
  10626. node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
  10627. if (!node.handler && !node.finalizer) {
  10628. this.raise(node.start, ErrorMessages.NoCatchOrFinally);
  10629. }
  10630. return this.finishNode(node, "TryStatement");
  10631. }
  10632. parseVarStatement(node, kind) {
  10633. this.next();
  10634. this.parseVar(node, false, kind);
  10635. this.semicolon();
  10636. return this.finishNode(node, "VariableDeclaration");
  10637. }
  10638. parseWhileStatement(node) {
  10639. this.next();
  10640. node.test = this.parseHeaderExpression();
  10641. this.state.labels.push(loopLabel);
  10642. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  10643. this.state.labels.pop();
  10644. return this.finishNode(node, "WhileStatement");
  10645. }
  10646. parseWithStatement(node) {
  10647. if (this.state.strict) {
  10648. this.raise(this.state.start, ErrorMessages.StrictWith);
  10649. }
  10650. this.next();
  10651. node.object = this.parseHeaderExpression();
  10652. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  10653. return this.finishNode(node, "WithStatement");
  10654. }
  10655. parseEmptyStatement(node) {
  10656. this.next();
  10657. return this.finishNode(node, "EmptyStatement");
  10658. }
  10659. parseLabeledStatement(node, maybeName, expr, context) {
  10660. for (const label of this.state.labels) {
  10661. if (label.name === maybeName) {
  10662. this.raise(expr.start, ErrorMessages.LabelRedeclaration, maybeName);
  10663. }
  10664. }
  10665. const kind = this.state.type.isLoop ? "loop" : this.match(types$1._switch) ? "switch" : null;
  10666. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  10667. const label = this.state.labels[i];
  10668. if (label.statementStart === node.start) {
  10669. label.statementStart = this.state.start;
  10670. label.kind = kind;
  10671. } else {
  10672. break;
  10673. }
  10674. }
  10675. this.state.labels.push({
  10676. name: maybeName,
  10677. kind: kind,
  10678. statementStart: this.state.start
  10679. });
  10680. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  10681. this.state.labels.pop();
  10682. node.label = expr;
  10683. return this.finishNode(node, "LabeledStatement");
  10684. }
  10685. parseExpressionStatement(node, expr) {
  10686. node.expression = expr;
  10687. this.semicolon();
  10688. return this.finishNode(node, "ExpressionStatement");
  10689. }
  10690. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  10691. const node = this.startNode();
  10692. if (allowDirectives) {
  10693. this.state.strictErrors.clear();
  10694. }
  10695. this.expect(types$1.braceL);
  10696. if (createNewLexicalScope) {
  10697. this.scope.enter(SCOPE_OTHER);
  10698. }
  10699. this.parseBlockBody(node, allowDirectives, false, types$1.braceR, afterBlockParse);
  10700. if (createNewLexicalScope) {
  10701. this.scope.exit();
  10702. }
  10703. return this.finishNode(node, "BlockStatement");
  10704. }
  10705. isValidDirective(stmt) {
  10706. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  10707. }
  10708. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  10709. const body = node.body = [];
  10710. const directives = node.directives = [];
  10711. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  10712. }
  10713. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  10714. const oldStrict = this.state.strict;
  10715. let hasStrictModeDirective = false;
  10716. let parsedNonDirective = false;
  10717. while (!this.match(end)) {
  10718. const stmt = this.parseStatement(null, topLevel);
  10719. if (directives && !parsedNonDirective) {
  10720. if (this.isValidDirective(stmt)) {
  10721. const directive = this.stmtToDirective(stmt);
  10722. directives.push(directive);
  10723. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  10724. hasStrictModeDirective = true;
  10725. this.setStrict(true);
  10726. }
  10727. continue;
  10728. }
  10729. parsedNonDirective = true;
  10730. this.state.strictErrors.clear();
  10731. }
  10732. body.push(stmt);
  10733. }
  10734. if (afterBlockParse) {
  10735. afterBlockParse.call(this, hasStrictModeDirective);
  10736. }
  10737. if (!oldStrict) {
  10738. this.setStrict(false);
  10739. }
  10740. this.next();
  10741. }
  10742. parseFor(node, init) {
  10743. node.init = init;
  10744. this.semicolon(false);
  10745. node.test = this.match(types$1.semi) ? null : this.parseExpression();
  10746. this.semicolon(false);
  10747. node.update = this.match(types$1.parenR) ? null : this.parseExpression();
  10748. this.expect(types$1.parenR);
  10749. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10750. this.scope.exit();
  10751. this.state.labels.pop();
  10752. return this.finishNode(node, "ForStatement");
  10753. }
  10754. parseForIn(node, init, awaitAt) {
  10755. const isForIn = this.match(types$1._in);
  10756. this.next();
  10757. if (isForIn) {
  10758. if (awaitAt > -1) this.unexpected(awaitAt);
  10759. } else {
  10760. node.await = awaitAt > -1;
  10761. }
  10762. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  10763. this.raise(init.start, ErrorMessages.ForInOfLoopInitializer, isForIn ? "for-in" : "for-of");
  10764. } else if (init.type === "AssignmentPattern") {
  10765. this.raise(init.start, ErrorMessages.InvalidLhs, "for-loop");
  10766. }
  10767. node.left = init;
  10768. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  10769. this.expect(types$1.parenR);
  10770. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10771. this.scope.exit();
  10772. this.state.labels.pop();
  10773. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  10774. }
  10775. parseVar(node, isFor, kind) {
  10776. const declarations = node.declarations = [];
  10777. const isTypescript = this.hasPlugin("typescript");
  10778. node.kind = kind;
  10779. for (;;) {
  10780. const decl = this.startNode();
  10781. this.parseVarId(decl, kind);
  10782. if (this.eat(types$1.eq)) {
  10783. decl.init = isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  10784. } else {
  10785. if (kind === "const" && !(this.match(types$1._in) || this.isContextual("of"))) {
  10786. if (!isTypescript) {
  10787. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Const declarations");
  10788. }
  10789. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types$1._in) || this.isContextual("of")))) {
  10790. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Complex binding patterns");
  10791. }
  10792. decl.init = null;
  10793. }
  10794. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  10795. if (!this.eat(types$1.comma)) break;
  10796. }
  10797. return node;
  10798. }
  10799. parseVarId(decl, kind) {
  10800. decl.id = this.parseBindingAtom();
  10801. this.checkLVal(decl.id, "variable declaration", kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, kind !== "var");
  10802. }
  10803. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  10804. const isStatement = statement & FUNC_STATEMENT;
  10805. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  10806. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  10807. this.initFunction(node, isAsync);
  10808. if (this.match(types$1.star) && isHangingStatement) {
  10809. this.raise(this.state.start, ErrorMessages.GeneratorInSingleStatementContext);
  10810. }
  10811. node.generator = this.eat(types$1.star);
  10812. if (isStatement) {
  10813. node.id = this.parseFunctionId(requireId);
  10814. }
  10815. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10816. this.state.maybeInArrowParameters = false;
  10817. this.scope.enter(SCOPE_FUNCTION);
  10818. this.prodParam.enter(functionFlags(isAsync, node.generator));
  10819. if (!isStatement) {
  10820. node.id = this.parseFunctionId();
  10821. }
  10822. this.parseFunctionParams(node, false);
  10823. this.withTopicForbiddingContext(() => {
  10824. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  10825. });
  10826. this.prodParam.exit();
  10827. this.scope.exit();
  10828. if (isStatement && !isHangingStatement) {
  10829. this.registerFunctionStatementId(node);
  10830. }
  10831. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10832. return node;
  10833. }
  10834. parseFunctionId(requireId) {
  10835. return requireId || this.match(types$1.name) ? this.parseIdentifier() : null;
  10836. }
  10837. parseFunctionParams(node, allowModifiers) {
  10838. this.expect(types$1.parenL);
  10839. this.expressionScope.enter(newParameterDeclarationScope());
  10840. node.params = this.parseBindingList(types$1.parenR, 41, false, allowModifiers);
  10841. this.expressionScope.exit();
  10842. }
  10843. registerFunctionStatementId(node) {
  10844. if (!node.id) return;
  10845. this.scope.declareName(node.id.name, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.start);
  10846. }
  10847. parseClass(node, isStatement, optionalId) {
  10848. this.next();
  10849. this.takeDecorators(node);
  10850. const oldStrict = this.state.strict;
  10851. this.state.strict = true;
  10852. this.parseClassId(node, isStatement, optionalId);
  10853. this.parseClassSuper(node);
  10854. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  10855. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  10856. }
  10857. isClassProperty() {
  10858. return this.match(types$1.eq) || this.match(types$1.semi) || this.match(types$1.braceR);
  10859. }
  10860. isClassMethod() {
  10861. return this.match(types$1.parenL);
  10862. }
  10863. isNonstaticConstructor(method) {
  10864. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  10865. }
  10866. parseClassBody(hadSuperClass, oldStrict) {
  10867. this.classScope.enter();
  10868. const state = {
  10869. hadConstructor: false,
  10870. hadSuperClass
  10871. };
  10872. let decorators = [];
  10873. const classBody = this.startNode();
  10874. classBody.body = [];
  10875. this.expect(types$1.braceL);
  10876. this.withTopicForbiddingContext(() => {
  10877. while (!this.match(types$1.braceR)) {
  10878. if (this.eat(types$1.semi)) {
  10879. if (decorators.length > 0) {
  10880. throw this.raise(this.state.lastTokEnd, ErrorMessages.DecoratorSemicolon);
  10881. }
  10882. continue;
  10883. }
  10884. if (this.match(types$1.at)) {
  10885. decorators.push(this.parseDecorator());
  10886. continue;
  10887. }
  10888. const member = this.startNode();
  10889. if (decorators.length) {
  10890. member.decorators = decorators;
  10891. this.resetStartLocationFromNode(member, decorators[0]);
  10892. decorators = [];
  10893. }
  10894. this.parseClassMember(classBody, member, state);
  10895. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  10896. this.raise(member.start, ErrorMessages.DecoratorConstructor);
  10897. }
  10898. }
  10899. });
  10900. this.state.strict = oldStrict;
  10901. this.next();
  10902. if (decorators.length) {
  10903. throw this.raise(this.state.start, ErrorMessages.TrailingDecorator);
  10904. }
  10905. this.classScope.exit();
  10906. return this.finishNode(classBody, "ClassBody");
  10907. }
  10908. parseClassMemberFromModifier(classBody, member) {
  10909. const key = this.parseIdentifier(true);
  10910. if (this.isClassMethod()) {
  10911. const method = member;
  10912. method.kind = "method";
  10913. method.computed = false;
  10914. method.key = key;
  10915. method.static = false;
  10916. this.pushClassMethod(classBody, method, false, false, false, false);
  10917. return true;
  10918. } else if (this.isClassProperty()) {
  10919. const prop = member;
  10920. prop.computed = false;
  10921. prop.key = key;
  10922. prop.static = false;
  10923. classBody.body.push(this.parseClassProperty(prop));
  10924. return true;
  10925. }
  10926. return false;
  10927. }
  10928. parseClassMember(classBody, member, state) {
  10929. const isStatic = this.isContextual("static");
  10930. if (isStatic) {
  10931. if (this.parseClassMemberFromModifier(classBody, member)) {
  10932. return;
  10933. }
  10934. if (this.eat(types$1.braceL)) {
  10935. this.parseClassStaticBlock(classBody, member);
  10936. return;
  10937. }
  10938. }
  10939. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  10940. }
  10941. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  10942. const publicMethod = member;
  10943. const privateMethod = member;
  10944. const publicProp = member;
  10945. const privateProp = member;
  10946. const method = publicMethod;
  10947. const publicMember = publicMethod;
  10948. member.static = isStatic;
  10949. if (this.eat(types$1.star)) {
  10950. method.kind = "method";
  10951. const isPrivateName = this.match(types$1.privateName);
  10952. this.parseClassElementName(method);
  10953. if (isPrivateName) {
  10954. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  10955. return;
  10956. }
  10957. if (this.isNonstaticConstructor(publicMethod)) {
  10958. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsGenerator);
  10959. }
  10960. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  10961. return;
  10962. }
  10963. const containsEsc = this.state.containsEsc;
  10964. const isPrivate = this.match(types$1.privateName);
  10965. const key = this.parseClassElementName(member);
  10966. const isSimple = key.type === "Identifier";
  10967. const maybeQuestionTokenStart = this.state.start;
  10968. this.parsePostMemberNameModifiers(publicMember);
  10969. if (this.isClassMethod()) {
  10970. method.kind = "method";
  10971. if (isPrivate) {
  10972. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  10973. return;
  10974. }
  10975. const isConstructor = this.isNonstaticConstructor(publicMethod);
  10976. let allowsDirectSuper = false;
  10977. if (isConstructor) {
  10978. publicMethod.kind = "constructor";
  10979. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  10980. this.raise(key.start, ErrorMessages.DuplicateConstructor);
  10981. }
  10982. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  10983. this.raise(key.start, ErrorMessages.OverrideOnConstructor);
  10984. }
  10985. state.hadConstructor = true;
  10986. allowsDirectSuper = state.hadSuperClass;
  10987. }
  10988. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  10989. } else if (this.isClassProperty()) {
  10990. if (isPrivate) {
  10991. this.pushClassPrivateProperty(classBody, privateProp);
  10992. } else {
  10993. this.pushClassProperty(classBody, publicProp);
  10994. }
  10995. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  10996. const isGenerator = this.eat(types$1.star);
  10997. if (publicMember.optional) {
  10998. this.unexpected(maybeQuestionTokenStart);
  10999. }
  11000. method.kind = "method";
  11001. const isPrivate = this.match(types$1.privateName);
  11002. this.parseClassElementName(method);
  11003. this.parsePostMemberNameModifiers(publicMember);
  11004. if (isPrivate) {
  11005. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  11006. } else {
  11007. if (this.isNonstaticConstructor(publicMethod)) {
  11008. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAsync);
  11009. }
  11010. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  11011. }
  11012. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types$1.star) && this.isLineTerminator())) {
  11013. method.kind = key.name;
  11014. const isPrivate = this.match(types$1.privateName);
  11015. this.parseClassElementName(publicMethod);
  11016. if (isPrivate) {
  11017. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11018. } else {
  11019. if (this.isNonstaticConstructor(publicMethod)) {
  11020. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAccessor);
  11021. }
  11022. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  11023. }
  11024. this.checkGetterSetterParams(publicMethod);
  11025. } else if (this.isLineTerminator()) {
  11026. if (isPrivate) {
  11027. this.pushClassPrivateProperty(classBody, privateProp);
  11028. } else {
  11029. this.pushClassProperty(classBody, publicProp);
  11030. }
  11031. } else {
  11032. this.unexpected();
  11033. }
  11034. }
  11035. parseClassElementName(member) {
  11036. const {
  11037. type,
  11038. value,
  11039. start
  11040. } = this.state;
  11041. if ((type === types$1.name || type === types$1.string) && member.static && value === "prototype") {
  11042. this.raise(start, ErrorMessages.StaticPrototype);
  11043. }
  11044. if (type === types$1.privateName && value === "constructor") {
  11045. this.raise(start, ErrorMessages.ConstructorClassPrivateField);
  11046. }
  11047. return this.parsePropertyName(member, true);
  11048. }
  11049. parseClassStaticBlock(classBody, member) {
  11050. var _member$decorators;
  11051. this.expectPlugin("classStaticBlock", member.start);
  11052. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  11053. const oldLabels = this.state.labels;
  11054. this.state.labels = [];
  11055. this.prodParam.enter(PARAM);
  11056. const body = member.body = [];
  11057. this.parseBlockOrModuleBlockBody(body, undefined, false, types$1.braceR);
  11058. this.prodParam.exit();
  11059. this.scope.exit();
  11060. this.state.labels = oldLabels;
  11061. classBody.body.push(this.finishNode(member, "StaticBlock"));
  11062. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  11063. this.raise(member.start, ErrorMessages.DecoratorStaticBlock);
  11064. }
  11065. }
  11066. pushClassProperty(classBody, prop) {
  11067. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  11068. this.raise(prop.key.start, ErrorMessages.ConstructorClassField);
  11069. }
  11070. classBody.body.push(this.parseClassProperty(prop));
  11071. }
  11072. pushClassPrivateProperty(classBody, prop) {
  11073. const node = this.parseClassPrivateProperty(prop);
  11074. classBody.body.push(node);
  11075. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.start);
  11076. }
  11077. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  11078. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  11079. }
  11080. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  11081. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  11082. classBody.body.push(node);
  11083. 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;
  11084. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.start);
  11085. }
  11086. parsePostMemberNameModifiers(methodOrProp) {}
  11087. parseClassPrivateProperty(node) {
  11088. this.parseInitializer(node);
  11089. this.semicolon();
  11090. return this.finishNode(node, "ClassPrivateProperty");
  11091. }
  11092. parseClassProperty(node) {
  11093. this.parseInitializer(node);
  11094. this.semicolon();
  11095. return this.finishNode(node, "ClassProperty");
  11096. }
  11097. parseInitializer(node) {
  11098. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  11099. this.expressionScope.enter(newExpressionScope());
  11100. this.prodParam.enter(PARAM);
  11101. node.value = this.eat(types$1.eq) ? this.parseMaybeAssignAllowIn() : null;
  11102. this.expressionScope.exit();
  11103. this.prodParam.exit();
  11104. this.scope.exit();
  11105. }
  11106. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  11107. if (this.match(types$1.name)) {
  11108. node.id = this.parseIdentifier();
  11109. if (isStatement) {
  11110. this.checkLVal(node.id, "class name", bindingType);
  11111. }
  11112. } else {
  11113. if (optionalId || !isStatement) {
  11114. node.id = null;
  11115. } else {
  11116. this.unexpected(null, ErrorMessages.MissingClassName);
  11117. }
  11118. }
  11119. }
  11120. parseClassSuper(node) {
  11121. node.superClass = this.eat(types$1._extends) ? this.parseExprSubscripts() : null;
  11122. }
  11123. parseExport(node) {
  11124. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  11125. const parseAfterDefault = !hasDefault || this.eat(types$1.comma);
  11126. const hasStar = parseAfterDefault && this.eatExportStar(node);
  11127. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  11128. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types$1.comma));
  11129. const isFromRequired = hasDefault || hasStar;
  11130. if (hasStar && !hasNamespace) {
  11131. if (hasDefault) this.unexpected();
  11132. this.parseExportFrom(node, true);
  11133. return this.finishNode(node, "ExportAllDeclaration");
  11134. }
  11135. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  11136. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  11137. throw this.unexpected(null, types$1.braceL);
  11138. }
  11139. let hasDeclaration;
  11140. if (isFromRequired || hasSpecifiers) {
  11141. hasDeclaration = false;
  11142. this.parseExportFrom(node, isFromRequired);
  11143. } else {
  11144. hasDeclaration = this.maybeParseExportDeclaration(node);
  11145. }
  11146. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  11147. this.checkExport(node, true, false, !!node.source);
  11148. return this.finishNode(node, "ExportNamedDeclaration");
  11149. }
  11150. if (this.eat(types$1._default)) {
  11151. node.declaration = this.parseExportDefaultExpression();
  11152. this.checkExport(node, true, true);
  11153. return this.finishNode(node, "ExportDefaultDeclaration");
  11154. }
  11155. throw this.unexpected(null, types$1.braceL);
  11156. }
  11157. eatExportStar(node) {
  11158. return this.eat(types$1.star);
  11159. }
  11160. maybeParseExportDefaultSpecifier(node) {
  11161. if (this.isExportDefaultSpecifier()) {
  11162. this.expectPlugin("exportDefaultFrom");
  11163. const specifier = this.startNode();
  11164. specifier.exported = this.parseIdentifier(true);
  11165. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  11166. return true;
  11167. }
  11168. return false;
  11169. }
  11170. maybeParseExportNamespaceSpecifier(node) {
  11171. if (this.isContextual("as")) {
  11172. if (!node.specifiers) node.specifiers = [];
  11173. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  11174. this.next();
  11175. specifier.exported = this.parseModuleExportName();
  11176. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  11177. return true;
  11178. }
  11179. return false;
  11180. }
  11181. maybeParseExportNamedSpecifiers(node) {
  11182. if (this.match(types$1.braceL)) {
  11183. if (!node.specifiers) node.specifiers = [];
  11184. node.specifiers.push(...this.parseExportSpecifiers());
  11185. node.source = null;
  11186. node.declaration = null;
  11187. return true;
  11188. }
  11189. return false;
  11190. }
  11191. maybeParseExportDeclaration(node) {
  11192. if (this.shouldParseExportDeclaration()) {
  11193. node.specifiers = [];
  11194. node.source = null;
  11195. node.declaration = this.parseExportDeclaration(node);
  11196. return true;
  11197. }
  11198. return false;
  11199. }
  11200. isAsyncFunction() {
  11201. if (!this.isContextual("async")) return false;
  11202. const next = this.nextTokenStart();
  11203. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  11204. }
  11205. parseExportDefaultExpression() {
  11206. const expr = this.startNode();
  11207. const isAsync = this.isAsyncFunction();
  11208. if (this.match(types$1._function) || isAsync) {
  11209. this.next();
  11210. if (isAsync) {
  11211. this.next();
  11212. }
  11213. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  11214. } else if (this.match(types$1._class)) {
  11215. return this.parseClass(expr, true, true);
  11216. } else if (this.match(types$1.at)) {
  11217. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11218. this.raise(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11219. }
  11220. this.parseDecorators(false);
  11221. return this.parseClass(expr, true, true);
  11222. } else if (this.match(types$1._const) || this.match(types$1._var) || this.isLet()) {
  11223. throw this.raise(this.state.start, ErrorMessages.UnsupportedDefaultExport);
  11224. } else {
  11225. const res = this.parseMaybeAssignAllowIn();
  11226. this.semicolon();
  11227. return res;
  11228. }
  11229. }
  11230. parseExportDeclaration(node) {
  11231. return this.parseStatement(null);
  11232. }
  11233. isExportDefaultSpecifier() {
  11234. if (this.match(types$1.name)) {
  11235. const value = this.state.value;
  11236. if (value === "async" && !this.state.containsEsc || value === "let") {
  11237. return false;
  11238. }
  11239. if ((value === "type" || value === "interface") && !this.state.containsEsc) {
  11240. const l = this.lookahead();
  11241. if (l.type === types$1.name && l.value !== "from" || l.type === types$1.braceL) {
  11242. this.expectOnePlugin(["flow", "typescript"]);
  11243. return false;
  11244. }
  11245. }
  11246. } else if (!this.match(types$1._default)) {
  11247. return false;
  11248. }
  11249. const next = this.nextTokenStart();
  11250. const hasFrom = this.isUnparsedContextual(next, "from");
  11251. if (this.input.charCodeAt(next) === 44 || this.match(types$1.name) && hasFrom) {
  11252. return true;
  11253. }
  11254. if (this.match(types$1._default) && hasFrom) {
  11255. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  11256. return nextAfterFrom === 34 || nextAfterFrom === 39;
  11257. }
  11258. return false;
  11259. }
  11260. parseExportFrom(node, expect) {
  11261. if (this.eatContextual("from")) {
  11262. node.source = this.parseImportSource();
  11263. this.checkExport(node);
  11264. const assertions = this.maybeParseImportAssertions();
  11265. if (assertions) {
  11266. node.assertions = assertions;
  11267. }
  11268. } else {
  11269. if (expect) {
  11270. this.unexpected();
  11271. } else {
  11272. node.source = null;
  11273. }
  11274. }
  11275. this.semicolon();
  11276. }
  11277. shouldParseExportDeclaration() {
  11278. if (this.match(types$1.at)) {
  11279. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  11280. if (this.hasPlugin("decorators")) {
  11281. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11282. this.unexpected(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11283. } else {
  11284. return true;
  11285. }
  11286. }
  11287. }
  11288. return this.state.type.keyword === "var" || this.state.type.keyword === "const" || this.state.type.keyword === "function" || this.state.type.keyword === "class" || this.isLet() || this.isAsyncFunction();
  11289. }
  11290. checkExport(node, checkNames, isDefault, isFrom) {
  11291. if (checkNames) {
  11292. if (isDefault) {
  11293. this.checkDuplicateExports(node, "default");
  11294. if (this.hasPlugin("exportDefaultFrom")) {
  11295. var _declaration$extra;
  11296. const declaration = node.declaration;
  11297. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  11298. this.raise(declaration.start, ErrorMessages.ExportDefaultFromAsIdentifier);
  11299. }
  11300. }
  11301. } else if (node.specifiers && node.specifiers.length) {
  11302. for (const specifier of node.specifiers) {
  11303. const {
  11304. exported
  11305. } = specifier;
  11306. const exportedName = exported.type === "Identifier" ? exported.name : exported.value;
  11307. this.checkDuplicateExports(specifier, exportedName);
  11308. if (!isFrom && specifier.local) {
  11309. const {
  11310. local
  11311. } = specifier;
  11312. if (local.type === "StringLiteral") {
  11313. this.raise(specifier.start, ErrorMessages.ExportBindingIsString, local.value, exportedName);
  11314. } else {
  11315. this.checkReservedWord(local.name, local.start, true, false);
  11316. this.scope.checkLocalExport(local);
  11317. }
  11318. }
  11319. }
  11320. } else if (node.declaration) {
  11321. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  11322. const id = node.declaration.id;
  11323. if (!id) throw new Error("Assertion failure");
  11324. this.checkDuplicateExports(node, id.name);
  11325. } else if (node.declaration.type === "VariableDeclaration") {
  11326. for (const declaration of node.declaration.declarations) {
  11327. this.checkDeclaration(declaration.id);
  11328. }
  11329. }
  11330. }
  11331. }
  11332. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11333. if (currentContextDecorators.length) {
  11334. throw this.raise(node.start, ErrorMessages.UnsupportedDecoratorExport);
  11335. }
  11336. }
  11337. checkDeclaration(node) {
  11338. if (node.type === "Identifier") {
  11339. this.checkDuplicateExports(node, node.name);
  11340. } else if (node.type === "ObjectPattern") {
  11341. for (const prop of node.properties) {
  11342. this.checkDeclaration(prop);
  11343. }
  11344. } else if (node.type === "ArrayPattern") {
  11345. for (const elem of node.elements) {
  11346. if (elem) {
  11347. this.checkDeclaration(elem);
  11348. }
  11349. }
  11350. } else if (node.type === "ObjectProperty") {
  11351. this.checkDeclaration(node.value);
  11352. } else if (node.type === "RestElement") {
  11353. this.checkDeclaration(node.argument);
  11354. } else if (node.type === "AssignmentPattern") {
  11355. this.checkDeclaration(node.left);
  11356. }
  11357. }
  11358. checkDuplicateExports(node, name) {
  11359. if (this.state.exportedIdentifiers.indexOf(name) > -1) {
  11360. this.raise(node.start, name === "default" ? ErrorMessages.DuplicateDefaultExport : ErrorMessages.DuplicateExport, name);
  11361. }
  11362. this.state.exportedIdentifiers.push(name);
  11363. }
  11364. parseExportSpecifiers() {
  11365. const nodes = [];
  11366. let first = true;
  11367. this.expect(types$1.braceL);
  11368. while (!this.eat(types$1.braceR)) {
  11369. if (first) {
  11370. first = false;
  11371. } else {
  11372. this.expect(types$1.comma);
  11373. if (this.eat(types$1.braceR)) break;
  11374. }
  11375. const node = this.startNode();
  11376. node.local = this.parseModuleExportName();
  11377. node.exported = this.eatContextual("as") ? this.parseModuleExportName() : node.local.__clone();
  11378. nodes.push(this.finishNode(node, "ExportSpecifier"));
  11379. }
  11380. return nodes;
  11381. }
  11382. parseModuleExportName() {
  11383. if (this.match(types$1.string)) {
  11384. const result = this.parseLiteral(this.state.value, "StringLiteral");
  11385. const surrogate = result.value.match(loneSurrogate);
  11386. if (surrogate) {
  11387. this.raise(result.start, ErrorMessages.ModuleExportNameHasLoneSurrogate, surrogate[0].charCodeAt(0).toString(16));
  11388. }
  11389. return result;
  11390. }
  11391. return this.parseIdentifier(true);
  11392. }
  11393. parseImport(node) {
  11394. node.specifiers = [];
  11395. if (!this.match(types$1.string)) {
  11396. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  11397. const parseNext = !hasDefault || this.eat(types$1.comma);
  11398. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  11399. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  11400. this.expectContextual("from");
  11401. }
  11402. node.source = this.parseImportSource();
  11403. const assertions = this.maybeParseImportAssertions();
  11404. if (assertions) {
  11405. node.assertions = assertions;
  11406. } else {
  11407. const attributes = this.maybeParseModuleAttributes();
  11408. if (attributes) {
  11409. node.attributes = attributes;
  11410. }
  11411. }
  11412. this.semicolon();
  11413. return this.finishNode(node, "ImportDeclaration");
  11414. }
  11415. parseImportSource() {
  11416. if (!this.match(types$1.string)) this.unexpected();
  11417. return this.parseExprAtom();
  11418. }
  11419. shouldParseDefaultImport(node) {
  11420. return this.match(types$1.name);
  11421. }
  11422. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  11423. specifier.local = this.parseIdentifier();
  11424. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  11425. node.specifiers.push(this.finishNode(specifier, type));
  11426. }
  11427. parseAssertEntries() {
  11428. const attrs = [];
  11429. const attrNames = new Set();
  11430. do {
  11431. if (this.match(types$1.braceR)) {
  11432. break;
  11433. }
  11434. const node = this.startNode();
  11435. const keyName = this.state.value;
  11436. if (this.match(types$1.string)) {
  11437. node.key = this.parseLiteral(keyName, "StringLiteral");
  11438. } else {
  11439. node.key = this.parseIdentifier(true);
  11440. }
  11441. this.expect(types$1.colon);
  11442. if (keyName !== "type") {
  11443. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, keyName);
  11444. }
  11445. if (attrNames.has(keyName)) {
  11446. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, keyName);
  11447. }
  11448. attrNames.add(keyName);
  11449. if (!this.match(types$1.string)) {
  11450. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11451. }
  11452. node.value = this.parseLiteral(this.state.value, "StringLiteral");
  11453. this.finishNode(node, "ImportAttribute");
  11454. attrs.push(node);
  11455. } while (this.eat(types$1.comma));
  11456. return attrs;
  11457. }
  11458. maybeParseModuleAttributes() {
  11459. if (this.match(types$1._with) && !this.hasPrecedingLineBreak()) {
  11460. this.expectPlugin("moduleAttributes");
  11461. this.next();
  11462. } else {
  11463. if (this.hasPlugin("moduleAttributes")) return [];
  11464. return null;
  11465. }
  11466. const attrs = [];
  11467. const attributes = new Set();
  11468. do {
  11469. const node = this.startNode();
  11470. node.key = this.parseIdentifier(true);
  11471. if (node.key.name !== "type") {
  11472. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, node.key.name);
  11473. }
  11474. if (attributes.has(node.key.name)) {
  11475. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, node.key.name);
  11476. }
  11477. attributes.add(node.key.name);
  11478. this.expect(types$1.colon);
  11479. if (!this.match(types$1.string)) {
  11480. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11481. }
  11482. node.value = this.parseLiteral(this.state.value, "StringLiteral");
  11483. this.finishNode(node, "ImportAttribute");
  11484. attrs.push(node);
  11485. } while (this.eat(types$1.comma));
  11486. return attrs;
  11487. }
  11488. maybeParseImportAssertions() {
  11489. if (this.isContextual("assert") && !this.hasPrecedingLineBreak()) {
  11490. this.expectPlugin("importAssertions");
  11491. this.next();
  11492. } else {
  11493. if (this.hasPlugin("importAssertions")) return [];
  11494. return null;
  11495. }
  11496. this.eat(types$1.braceL);
  11497. const attrs = this.parseAssertEntries();
  11498. this.eat(types$1.braceR);
  11499. return attrs;
  11500. }
  11501. maybeParseDefaultImportSpecifier(node) {
  11502. if (this.shouldParseDefaultImport(node)) {
  11503. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  11504. return true;
  11505. }
  11506. return false;
  11507. }
  11508. maybeParseStarImportSpecifier(node) {
  11509. if (this.match(types$1.star)) {
  11510. const specifier = this.startNode();
  11511. this.next();
  11512. this.expectContextual("as");
  11513. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  11514. return true;
  11515. }
  11516. return false;
  11517. }
  11518. parseNamedImportSpecifiers(node) {
  11519. let first = true;
  11520. this.expect(types$1.braceL);
  11521. while (!this.eat(types$1.braceR)) {
  11522. if (first) {
  11523. first = false;
  11524. } else {
  11525. if (this.eat(types$1.colon)) {
  11526. throw this.raise(this.state.start, ErrorMessages.DestructureNamedImport);
  11527. }
  11528. this.expect(types$1.comma);
  11529. if (this.eat(types$1.braceR)) break;
  11530. }
  11531. this.parseImportSpecifier(node);
  11532. }
  11533. }
  11534. parseImportSpecifier(node) {
  11535. const specifier = this.startNode();
  11536. specifier.imported = this.parseModuleExportName();
  11537. if (this.eatContextual("as")) {
  11538. specifier.local = this.parseIdentifier();
  11539. } else {
  11540. const {
  11541. imported
  11542. } = specifier;
  11543. if (imported.type === "StringLiteral") {
  11544. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, imported.value);
  11545. }
  11546. this.checkReservedWord(imported.name, specifier.start, true, true);
  11547. specifier.local = imported.__clone();
  11548. }
  11549. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  11550. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  11551. }
  11552. isThisParam(param) {
  11553. return param.type === "Identifier" && param.name === "this";
  11554. }
  11555. }
  11556. class Parser extends StatementParser {
  11557. constructor(options, input) {
  11558. options = getOptions(options);
  11559. super(options, input);
  11560. this.options = options;
  11561. this.initializeScopes();
  11562. this.plugins = pluginsMap(this.options.plugins);
  11563. this.filename = options.sourceFilename;
  11564. }
  11565. getScopeHandler() {
  11566. return ScopeHandler;
  11567. }
  11568. parse() {
  11569. this.enterInitialScopes();
  11570. const file = this.startNode();
  11571. const program = this.startNode();
  11572. this.nextToken();
  11573. file.errors = null;
  11574. this.parseTopLevel(file, program);
  11575. file.errors = this.state.errors;
  11576. return file;
  11577. }
  11578. }
  11579. function pluginsMap(plugins) {
  11580. const pluginMap = new Map();
  11581. for (const plugin of plugins) {
  11582. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  11583. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  11584. }
  11585. return pluginMap;
  11586. }
  11587. function parse(input, options) {
  11588. var _options;
  11589. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  11590. options = Object.assign({}, options);
  11591. try {
  11592. options.sourceType = "module";
  11593. const parser = getParser(options, input);
  11594. const ast = parser.parse();
  11595. if (parser.sawUnambiguousESM) {
  11596. return ast;
  11597. }
  11598. if (parser.ambiguousScriptDifferentAst) {
  11599. try {
  11600. options.sourceType = "script";
  11601. return getParser(options, input).parse();
  11602. } catch (_unused) {}
  11603. } else {
  11604. ast.program.sourceType = "script";
  11605. }
  11606. return ast;
  11607. } catch (moduleError) {
  11608. try {
  11609. options.sourceType = "script";
  11610. return getParser(options, input).parse();
  11611. } catch (_unused2) {}
  11612. throw moduleError;
  11613. }
  11614. } else {
  11615. return getParser(options, input).parse();
  11616. }
  11617. }
  11618. function parseExpression(input, options) {
  11619. const parser = getParser(options, input);
  11620. if (parser.options.strictMode) {
  11621. parser.state.strict = true;
  11622. }
  11623. return parser.getExpression();
  11624. }
  11625. function getParser(options, input) {
  11626. let cls = Parser;
  11627. if (options != null && options.plugins) {
  11628. validatePlugins(options.plugins);
  11629. cls = getParserClass(options.plugins);
  11630. }
  11631. return new cls(options, input);
  11632. }
  11633. const parserClassCache = {};
  11634. function getParserClass(pluginsFromOptions) {
  11635. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  11636. const key = pluginList.join("/");
  11637. let cls = parserClassCache[key];
  11638. if (!cls) {
  11639. cls = Parser;
  11640. for (const plugin of pluginList) {
  11641. cls = mixinPlugins[plugin](cls);
  11642. }
  11643. parserClassCache[key] = cls;
  11644. }
  11645. return cls;
  11646. }
  11647. exports.parse = parse;
  11648. exports.parseExpression = parseExpression;
  11649. exports.tokTypes = types$1;
  11650. //# sourceMappingURL=index.js.map