index.js 420 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276
  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. beforeExpr
  99. }),
  100. braceBarR: new TokenType("|}"),
  101. parenL: new TokenType("(", {
  102. beforeExpr,
  103. startsExpr
  104. }),
  105. parenR: new TokenType(")"),
  106. comma: new TokenType(",", {
  107. beforeExpr
  108. }),
  109. semi: new TokenType(";", {
  110. beforeExpr
  111. }),
  112. colon: new TokenType(":", {
  113. beforeExpr
  114. }),
  115. doubleColon: new TokenType("::", {
  116. beforeExpr
  117. }),
  118. dot: new TokenType("."),
  119. question: new TokenType("?", {
  120. beforeExpr
  121. }),
  122. questionDot: new TokenType("?."),
  123. arrow: new TokenType("=>", {
  124. beforeExpr
  125. }),
  126. template: new TokenType("template"),
  127. ellipsis: new TokenType("...", {
  128. beforeExpr
  129. }),
  130. backQuote: new TokenType("`", {
  131. startsExpr
  132. }),
  133. dollarBraceL: new TokenType("${", {
  134. beforeExpr,
  135. startsExpr
  136. }),
  137. at: new TokenType("@"),
  138. hash: new TokenType("#", {
  139. startsExpr
  140. }),
  141. interpreterDirective: new TokenType("#!..."),
  142. eq: new TokenType("=", {
  143. beforeExpr,
  144. isAssign
  145. }),
  146. assign: new TokenType("_=", {
  147. beforeExpr,
  148. isAssign
  149. }),
  150. slashAssign: new TokenType("_=", {
  151. beforeExpr,
  152. isAssign
  153. }),
  154. incDec: new TokenType("++/--", {
  155. prefix,
  156. postfix,
  157. startsExpr
  158. }),
  159. bang: new TokenType("!", {
  160. beforeExpr,
  161. prefix,
  162. startsExpr
  163. }),
  164. tilde: new TokenType("~", {
  165. beforeExpr,
  166. prefix,
  167. startsExpr
  168. }),
  169. pipeline: createBinop("|>", 0),
  170. nullishCoalescing: createBinop("??", 1),
  171. logicalOR: createBinop("||", 1),
  172. logicalAND: createBinop("&&", 2),
  173. bitwiseOR: createBinop("|", 3),
  174. bitwiseXOR: createBinop("^", 4),
  175. bitwiseAND: createBinop("&", 5),
  176. equality: createBinop("==/!=/===/!==", 6),
  177. relational: createBinop("</>/<=/>=", 7),
  178. bitShift: createBinop("<</>>/>>>", 8),
  179. plusMin: new TokenType("+/-", {
  180. beforeExpr,
  181. binop: 9,
  182. prefix,
  183. startsExpr
  184. }),
  185. modulo: new TokenType("%", {
  186. beforeExpr,
  187. binop: 10,
  188. startsExpr
  189. }),
  190. star: new TokenType("*", {
  191. binop: 10
  192. }),
  193. slash: createBinop("/", 10),
  194. exponent: new TokenType("**", {
  195. beforeExpr,
  196. binop: 11,
  197. rightAssociative: true
  198. }),
  199. _break: createKeyword("break"),
  200. _case: createKeyword("case", {
  201. beforeExpr
  202. }),
  203. _catch: createKeyword("catch"),
  204. _continue: createKeyword("continue"),
  205. _debugger: createKeyword("debugger"),
  206. _default: createKeyword("default", {
  207. beforeExpr
  208. }),
  209. _do: createKeyword("do", {
  210. isLoop,
  211. beforeExpr
  212. }),
  213. _else: createKeyword("else", {
  214. beforeExpr
  215. }),
  216. _finally: createKeyword("finally"),
  217. _for: createKeyword("for", {
  218. isLoop
  219. }),
  220. _function: createKeyword("function", {
  221. startsExpr
  222. }),
  223. _if: createKeyword("if"),
  224. _return: createKeyword("return", {
  225. beforeExpr
  226. }),
  227. _switch: createKeyword("switch"),
  228. _throw: createKeyword("throw", {
  229. beforeExpr,
  230. prefix,
  231. startsExpr
  232. }),
  233. _try: createKeyword("try"),
  234. _var: createKeyword("var"),
  235. _const: createKeyword("const"),
  236. _while: createKeyword("while", {
  237. isLoop
  238. }),
  239. _with: createKeyword("with"),
  240. _new: createKeyword("new", {
  241. beforeExpr,
  242. startsExpr
  243. }),
  244. _this: createKeyword("this", {
  245. startsExpr
  246. }),
  247. _super: createKeyword("super", {
  248. startsExpr
  249. }),
  250. _class: createKeyword("class", {
  251. startsExpr
  252. }),
  253. _extends: createKeyword("extends", {
  254. beforeExpr
  255. }),
  256. _export: createKeyword("export"),
  257. _import: createKeyword("import", {
  258. startsExpr
  259. }),
  260. _null: createKeyword("null", {
  261. startsExpr
  262. }),
  263. _true: createKeyword("true", {
  264. startsExpr
  265. }),
  266. _false: createKeyword("false", {
  267. startsExpr
  268. }),
  269. _in: createKeyword("in", {
  270. beforeExpr,
  271. binop: 7
  272. }),
  273. _instanceof: createKeyword("instanceof", {
  274. beforeExpr,
  275. binop: 7
  276. }),
  277. _typeof: createKeyword("typeof", {
  278. beforeExpr,
  279. prefix,
  280. startsExpr
  281. }),
  282. _void: createKeyword("void", {
  283. beforeExpr,
  284. prefix,
  285. startsExpr
  286. }),
  287. _delete: createKeyword("delete", {
  288. beforeExpr,
  289. prefix,
  290. startsExpr
  291. })
  292. };
  293. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  294. const lineBreakG = new RegExp(lineBreak.source, "g");
  295. function isNewLine(code) {
  296. switch (code) {
  297. case 10:
  298. case 13:
  299. case 8232:
  300. case 8233:
  301. return true;
  302. default:
  303. return false;
  304. }
  305. }
  306. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  307. function isWhitespace(code) {
  308. switch (code) {
  309. case 0x0009:
  310. case 0x000b:
  311. case 0x000c:
  312. case 32:
  313. case 160:
  314. case 5760:
  315. case 0x2000:
  316. case 0x2001:
  317. case 0x2002:
  318. case 0x2003:
  319. case 0x2004:
  320. case 0x2005:
  321. case 0x2006:
  322. case 0x2007:
  323. case 0x2008:
  324. case 0x2009:
  325. case 0x200a:
  326. case 0x202f:
  327. case 0x205f:
  328. case 0x3000:
  329. case 0xfeff:
  330. return true;
  331. default:
  332. return false;
  333. }
  334. }
  335. class Position {
  336. constructor(line, col) {
  337. this.line = void 0;
  338. this.column = void 0;
  339. this.line = line;
  340. this.column = col;
  341. }
  342. }
  343. class SourceLocation {
  344. constructor(start, end) {
  345. this.start = void 0;
  346. this.end = void 0;
  347. this.filename = void 0;
  348. this.identifierName = void 0;
  349. this.start = start;
  350. this.end = end;
  351. }
  352. }
  353. function getLineInfo(input, offset) {
  354. let line = 1;
  355. let lineStart = 0;
  356. let match;
  357. lineBreakG.lastIndex = 0;
  358. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  359. line++;
  360. lineStart = lineBreakG.lastIndex;
  361. }
  362. return new Position(line, offset - lineStart);
  363. }
  364. class BaseParser {
  365. constructor() {
  366. this.sawUnambiguousESM = false;
  367. this.ambiguousScriptDifferentAst = false;
  368. }
  369. hasPlugin(name) {
  370. return this.plugins.has(name);
  371. }
  372. getPluginOption(plugin, name) {
  373. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  374. }
  375. }
  376. function last(stack) {
  377. return stack[stack.length - 1];
  378. }
  379. class CommentsParser extends BaseParser {
  380. addComment(comment) {
  381. if (this.filename) comment.loc.filename = this.filename;
  382. this.state.trailingComments.push(comment);
  383. this.state.leadingComments.push(comment);
  384. }
  385. adjustCommentsAfterTrailingComma(node, elements, takeAllComments) {
  386. if (this.state.leadingComments.length === 0) {
  387. return;
  388. }
  389. let lastElement = null;
  390. let i = elements.length;
  391. while (lastElement === null && i > 0) {
  392. lastElement = elements[--i];
  393. }
  394. if (lastElement === null) {
  395. return;
  396. }
  397. for (let j = 0; j < this.state.leadingComments.length; j++) {
  398. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  399. this.state.leadingComments.splice(j, 1);
  400. j--;
  401. }
  402. }
  403. const newTrailingComments = [];
  404. for (let i = 0; i < this.state.leadingComments.length; i++) {
  405. const leadingComment = this.state.leadingComments[i];
  406. if (leadingComment.end < node.end) {
  407. newTrailingComments.push(leadingComment);
  408. if (!takeAllComments) {
  409. this.state.leadingComments.splice(i, 1);
  410. i--;
  411. }
  412. } else {
  413. if (node.trailingComments === undefined) {
  414. node.trailingComments = [];
  415. }
  416. node.trailingComments.push(leadingComment);
  417. }
  418. }
  419. if (takeAllComments) this.state.leadingComments = [];
  420. if (newTrailingComments.length > 0) {
  421. lastElement.trailingComments = newTrailingComments;
  422. } else if (lastElement.trailingComments !== undefined) {
  423. lastElement.trailingComments = [];
  424. }
  425. }
  426. processComment(node) {
  427. if (node.type === "Program" && node.body.length > 0) return;
  428. const stack = this.state.commentStack;
  429. let firstChild, lastChild, trailingComments, i, j;
  430. if (this.state.trailingComments.length > 0) {
  431. if (this.state.trailingComments[0].start >= node.end) {
  432. trailingComments = this.state.trailingComments;
  433. this.state.trailingComments = [];
  434. } else {
  435. this.state.trailingComments.length = 0;
  436. }
  437. } else if (stack.length > 0) {
  438. const lastInStack = last(stack);
  439. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  440. trailingComments = lastInStack.trailingComments;
  441. delete lastInStack.trailingComments;
  442. }
  443. }
  444. if (stack.length > 0 && last(stack).start >= node.start) {
  445. firstChild = stack.pop();
  446. }
  447. while (stack.length > 0 && last(stack).start >= node.start) {
  448. lastChild = stack.pop();
  449. }
  450. if (!lastChild && firstChild) lastChild = firstChild;
  451. if (firstChild) {
  452. switch (node.type) {
  453. case "ObjectExpression":
  454. this.adjustCommentsAfterTrailingComma(node, node.properties);
  455. break;
  456. case "ObjectPattern":
  457. this.adjustCommentsAfterTrailingComma(node, node.properties, true);
  458. break;
  459. case "CallExpression":
  460. this.adjustCommentsAfterTrailingComma(node, node.arguments);
  461. break;
  462. case "ArrayExpression":
  463. this.adjustCommentsAfterTrailingComma(node, node.elements);
  464. break;
  465. case "ArrayPattern":
  466. this.adjustCommentsAfterTrailingComma(node, node.elements, true);
  467. break;
  468. }
  469. } else if (this.state.commentPreviousNode && (this.state.commentPreviousNode.type === "ImportSpecifier" && node.type !== "ImportSpecifier" || this.state.commentPreviousNode.type === "ExportSpecifier" && node.type !== "ExportSpecifier")) {
  470. this.adjustCommentsAfterTrailingComma(node, [this.state.commentPreviousNode]);
  471. }
  472. if (lastChild) {
  473. if (lastChild.leadingComments) {
  474. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  475. node.leadingComments = lastChild.leadingComments;
  476. delete lastChild.leadingComments;
  477. } else {
  478. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  479. if (lastChild.leadingComments[i].end <= node.start) {
  480. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  481. break;
  482. }
  483. }
  484. }
  485. }
  486. } else if (this.state.leadingComments.length > 0) {
  487. if (last(this.state.leadingComments).end <= node.start) {
  488. if (this.state.commentPreviousNode) {
  489. for (j = 0; j < this.state.leadingComments.length; j++) {
  490. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  491. this.state.leadingComments.splice(j, 1);
  492. j--;
  493. }
  494. }
  495. }
  496. if (this.state.leadingComments.length > 0) {
  497. node.leadingComments = this.state.leadingComments;
  498. this.state.leadingComments = [];
  499. }
  500. } else {
  501. for (i = 0; i < this.state.leadingComments.length; i++) {
  502. if (this.state.leadingComments[i].end > node.start) {
  503. break;
  504. }
  505. }
  506. const leadingComments = this.state.leadingComments.slice(0, i);
  507. if (leadingComments.length) {
  508. node.leadingComments = leadingComments;
  509. }
  510. trailingComments = this.state.leadingComments.slice(i);
  511. if (trailingComments.length === 0) {
  512. trailingComments = null;
  513. }
  514. }
  515. }
  516. this.state.commentPreviousNode = node;
  517. if (trailingComments) {
  518. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  519. node.innerComments = trailingComments;
  520. } else {
  521. const firstTrailingCommentIndex = trailingComments.findIndex(comment => comment.end >= node.end);
  522. if (firstTrailingCommentIndex > 0) {
  523. node.innerComments = trailingComments.slice(0, firstTrailingCommentIndex);
  524. node.trailingComments = trailingComments.slice(firstTrailingCommentIndex);
  525. } else {
  526. node.trailingComments = trailingComments;
  527. }
  528. }
  529. }
  530. stack.push(node);
  531. }
  532. }
  533. const ErrorCodes = Object.freeze({
  534. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  535. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  536. });
  537. const ErrorMessages = makeErrorTemplates({
  538. AccessorIsGenerator: "A %0ter cannot be a generator.",
  539. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  540. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  541. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  542. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  543. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  544. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  545. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  546. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  547. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  548. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  549. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  550. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  551. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  552. ConstructorIsAsync: "Constructor can't be an async function.",
  553. ConstructorIsGenerator: "Constructor can't be a generator.",
  554. DeclarationMissingInitializer: "'%0' require an initialization value.",
  555. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  556. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  557. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  558. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  559. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  560. DeletePrivateField: "Deleting a private field is not allowed.",
  561. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  562. DuplicateConstructor: "Duplicate constructor in the same class.",
  563. DuplicateDefaultExport: "Only one default export allowed per module.",
  564. DuplicateExport: "`%0` has already been exported. Exported identifiers must be unique.",
  565. DuplicateProto: "Redefinition of __proto__ property.",
  566. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  567. ElementAfterRest: "Rest element must be last element.",
  568. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  569. ExportBindingIsString: "A string literal cannot be used as an exported binding without `from`.\n- Did you mean `export { '%0' as '%1' } from 'some-module'`?",
  570. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  571. ForInOfLoopInitializer: "'%0' loop variable declaration may not have an initializer.",
  572. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  573. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  574. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  575. IllegalBreakContinue: "Unsyntactic %0.",
  576. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  577. IllegalReturn: "'return' outside of function.",
  578. ImportBindingIsString: 'A string literal cannot be used as an imported binding.\n- Did you mean `import { "%0" as foo }`?',
  579. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  580. ImportCallArity: "`import()` requires exactly %0.",
  581. ImportCallNotNewExpression: "Cannot use new with import(...).",
  582. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  583. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  584. InvalidCodePoint: "Code point out of bounds.",
  585. InvalidDecimal: "Invalid decimal.",
  586. InvalidDigit: "Expected number in radix %0.",
  587. InvalidEscapeSequence: "Bad character escape sequence.",
  588. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  589. InvalidEscapedReservedWord: "Escape sequence in keyword %0.",
  590. InvalidIdentifier: "Invalid identifier %0.",
  591. InvalidLhs: "Invalid left-hand side in %0.",
  592. InvalidLhsBinding: "Binding invalid left-hand side in %0.",
  593. InvalidNumber: "Invalid number.",
  594. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  595. InvalidOrUnexpectedToken: "Unexpected character '%0'.",
  596. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  597. InvalidPrivateFieldResolution: "Private name #%0 is not defined.",
  598. InvalidPropertyBindingPattern: "Binding member expression.",
  599. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  600. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  601. LabelRedeclaration: "Label '%0' is already declared.",
  602. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  603. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  604. MalformedRegExpFlags: "Invalid regular expression flag.",
  605. MissingClassName: "A class name is required.",
  606. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  607. MissingSemicolon: "Missing semicolon.",
  608. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  609. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  610. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  611. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  612. ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes.',
  613. ModuleExportNameHasLoneSurrogate: "An export name cannot include a lone surrogate, found '\\u%0'.",
  614. ModuleExportUndefined: "Export '%0' is not defined.",
  615. MultipleDefaultsInSwitch: "Multiple default clauses.",
  616. NewlineAfterThrow: "Illegal newline after throw.",
  617. NoCatchOrFinally: "Missing catch or finally clause.",
  618. NumberIdentifier: "Identifier directly after number.",
  619. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  620. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  621. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  622. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  623. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  624. ParamDupe: "Argument name clash.",
  625. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  626. PatternHasMethod: "Object pattern can't contain methods.",
  627. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  628. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  629. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  630. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  631. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  632. PrimaryTopicRequiresSmartPipeline: "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.",
  633. PrivateInExpectedIn: "Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
  634. PrivateNameRedeclaration: "Duplicate private name #%0.",
  635. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  636. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  637. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  638. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  639. RestTrailingComma: "Unexpected trailing comma after rest element.",
  640. 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.",
  641. StaticPrototype: "Classes may not have static property named prototype.",
  642. StrictDelete: "Deleting local variable in strict mode.",
  643. StrictEvalArguments: "Assigning to '%0' in strict mode.",
  644. StrictEvalArgumentsBinding: "Binding '%0' in strict mode.",
  645. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  646. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  647. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  648. StrictWith: "'with' in strict mode.",
  649. 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?",
  650. SuperPrivateField: "Private fields can't be accessed on super.",
  651. TrailingDecorator: "Decorators must be attached to a class element.",
  652. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  653. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  654. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  655. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  656. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  657. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  658. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  659. UnexpectedKeyword: "Unexpected keyword '%0'.",
  660. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  661. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  662. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  663. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  664. 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).",
  665. UnexpectedReservedWord: "Unexpected reserved word '%0'.",
  666. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  667. UnexpectedToken: "Unexpected token '%0'.",
  668. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  669. UnsupportedBind: "Binding should be performed on object property.",
  670. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  671. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  672. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  673. UnsupportedMetaProperty: "The only valid meta property for %0 is %0.%1.",
  674. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  675. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  676. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  677. UnterminatedComment: "Unterminated comment.",
  678. UnterminatedRegExp: "Unterminated regular expression.",
  679. UnterminatedString: "Unterminated string constant.",
  680. UnterminatedTemplate: "Unterminated template.",
  681. VarRedeclaration: "Identifier '%0' has already been declared.",
  682. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  683. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  684. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  685. }, ErrorCodes.SyntaxError);
  686. const SourceTypeModuleErrorMessages = makeErrorTemplates({
  687. ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
  688. ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`
  689. }, ErrorCodes.SourceTypeModuleError);
  690. function makeErrorTemplates(messages, code) {
  691. const templates = {};
  692. Object.keys(messages).forEach(reasonCode => {
  693. templates[reasonCode] = Object.freeze({
  694. code,
  695. reasonCode,
  696. template: messages[reasonCode]
  697. });
  698. });
  699. return Object.freeze(templates);
  700. }
  701. class ParserError extends CommentsParser {
  702. getLocationForPosition(pos) {
  703. let loc;
  704. 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);
  705. return loc;
  706. }
  707. raise(pos, {
  708. code,
  709. reasonCode,
  710. template
  711. }, ...params) {
  712. return this.raiseWithData(pos, {
  713. code,
  714. reasonCode
  715. }, template, ...params);
  716. }
  717. raiseOverwrite(pos, {
  718. code,
  719. template
  720. }, ...params) {
  721. const loc = this.getLocationForPosition(pos);
  722. const message = template.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  723. if (this.options.errorRecovery) {
  724. const errors = this.state.errors;
  725. for (let i = errors.length - 1; i >= 0; i--) {
  726. const error = errors[i];
  727. if (error.pos === pos) {
  728. return Object.assign(error, {
  729. message
  730. });
  731. } else if (error.pos < pos) {
  732. break;
  733. }
  734. }
  735. }
  736. return this._raise({
  737. code,
  738. loc,
  739. pos
  740. }, message);
  741. }
  742. raiseWithData(pos, data, errorTemplate, ...params) {
  743. const loc = this.getLocationForPosition(pos);
  744. const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  745. return this._raise(Object.assign({
  746. loc,
  747. pos
  748. }, data), message);
  749. }
  750. _raise(errorContext, message) {
  751. const err = new SyntaxError(message);
  752. Object.assign(err, errorContext);
  753. if (this.options.errorRecovery) {
  754. if (!this.isLookahead) this.state.errors.push(err);
  755. return err;
  756. } else {
  757. throw err;
  758. }
  759. }
  760. }
  761. var estree = (superClass => class extends superClass {
  762. parseRegExpLiteral({
  763. pattern,
  764. flags
  765. }) {
  766. let regex = null;
  767. try {
  768. regex = new RegExp(pattern, flags);
  769. } catch (e) {}
  770. const node = this.estreeParseLiteral(regex);
  771. node.regex = {
  772. pattern,
  773. flags
  774. };
  775. return node;
  776. }
  777. parseBigIntLiteral(value) {
  778. let bigInt;
  779. try {
  780. bigInt = BigInt(value);
  781. } catch (_unused) {
  782. bigInt = null;
  783. }
  784. const node = this.estreeParseLiteral(bigInt);
  785. node.bigint = String(node.value || value);
  786. return node;
  787. }
  788. parseDecimalLiteral(value) {
  789. const decimal = null;
  790. const node = this.estreeParseLiteral(decimal);
  791. node.decimal = String(node.value || value);
  792. return node;
  793. }
  794. estreeParseLiteral(value) {
  795. return this.parseLiteral(value, "Literal");
  796. }
  797. parseStringLiteral(value) {
  798. return this.estreeParseLiteral(value);
  799. }
  800. parseNumericLiteral(value) {
  801. return this.estreeParseLiteral(value);
  802. }
  803. parseNullLiteral() {
  804. return this.estreeParseLiteral(null);
  805. }
  806. parseBooleanLiteral(value) {
  807. return this.estreeParseLiteral(value);
  808. }
  809. directiveToStmt(directive) {
  810. const directiveLiteral = directive.value;
  811. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  812. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  813. expression.value = directiveLiteral.extra.expressionValue;
  814. expression.raw = directiveLiteral.extra.raw;
  815. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  816. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  817. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  818. }
  819. initFunction(node, isAsync) {
  820. super.initFunction(node, isAsync);
  821. node.expression = false;
  822. }
  823. checkDeclaration(node) {
  824. if (node != null && this.isObjectProperty(node)) {
  825. this.checkDeclaration(node.value);
  826. } else {
  827. super.checkDeclaration(node);
  828. }
  829. }
  830. getObjectOrClassMethodParams(method) {
  831. return method.value.params;
  832. }
  833. isValidDirective(stmt) {
  834. var _stmt$expression$extr;
  835. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  836. }
  837. stmtToDirective(stmt) {
  838. const directive = super.stmtToDirective(stmt);
  839. const value = stmt.expression.value;
  840. this.addExtra(directive.value, "expressionValue", value);
  841. return directive;
  842. }
  843. parseBlockBody(node, ...args) {
  844. super.parseBlockBody(node, ...args);
  845. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  846. node.body = directiveStatements.concat(node.body);
  847. delete node.directives;
  848. }
  849. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  850. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  851. if (method.typeParameters) {
  852. method.value.typeParameters = method.typeParameters;
  853. delete method.typeParameters;
  854. }
  855. classBody.body.push(method);
  856. }
  857. parseMaybePrivateName(...args) {
  858. const node = super.parseMaybePrivateName(...args);
  859. if (node.type === "PrivateName" && this.getPluginOption("estree", "classFeatures")) {
  860. return this.convertPrivateNameToPrivateIdentifier(node);
  861. }
  862. return node;
  863. }
  864. convertPrivateNameToPrivateIdentifier(node) {
  865. const name = super.getPrivateNameSV(node);
  866. node = node;
  867. delete node.id;
  868. node.name = name;
  869. node.type = "PrivateIdentifier";
  870. return node;
  871. }
  872. isPrivateName(node) {
  873. if (!this.getPluginOption("estree", "classFeatures")) {
  874. return super.isPrivateName(node);
  875. }
  876. return node.type === "PrivateIdentifier";
  877. }
  878. getPrivateNameSV(node) {
  879. if (!this.getPluginOption("estree", "classFeatures")) {
  880. return super.getPrivateNameSV(node);
  881. }
  882. return node.name;
  883. }
  884. parseLiteral(value, type) {
  885. const node = super.parseLiteral(value, type);
  886. node.raw = node.extra.raw;
  887. delete node.extra;
  888. return node;
  889. }
  890. parseFunctionBody(node, allowExpression, isMethod = false) {
  891. super.parseFunctionBody(node, allowExpression, isMethod);
  892. node.expression = node.body.type !== "BlockStatement";
  893. }
  894. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  895. let funcNode = this.startNode();
  896. funcNode.kind = node.kind;
  897. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  898. funcNode.type = "FunctionExpression";
  899. delete funcNode.kind;
  900. node.value = funcNode;
  901. if (type === "ClassPrivateMethod") {
  902. node.computed = false;
  903. }
  904. type = "MethodDefinition";
  905. return this.finishNode(node, type);
  906. }
  907. parseClassProperty(...args) {
  908. const propertyNode = super.parseClassProperty(...args);
  909. if (this.getPluginOption("estree", "classFeatures")) {
  910. propertyNode.type = "PropertyDefinition";
  911. }
  912. return propertyNode;
  913. }
  914. parseClassPrivateProperty(...args) {
  915. const propertyNode = super.parseClassPrivateProperty(...args);
  916. if (this.getPluginOption("estree", "classFeatures")) {
  917. propertyNode.type = "PropertyDefinition";
  918. propertyNode.computed = false;
  919. }
  920. return propertyNode;
  921. }
  922. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  923. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  924. if (node) {
  925. node.type = "Property";
  926. if (node.kind === "method") node.kind = "init";
  927. node.shorthand = false;
  928. }
  929. return node;
  930. }
  931. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  932. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  933. if (node) {
  934. node.kind = "init";
  935. node.type = "Property";
  936. }
  937. return node;
  938. }
  939. toAssignable(node, isLHS = false) {
  940. if (node != null && this.isObjectProperty(node)) {
  941. this.toAssignable(node.value, isLHS);
  942. return node;
  943. }
  944. return super.toAssignable(node, isLHS);
  945. }
  946. toAssignableObjectExpressionProp(prop, ...args) {
  947. if (prop.kind === "get" || prop.kind === "set") {
  948. this.raise(prop.key.start, ErrorMessages.PatternHasAccessor);
  949. } else if (prop.method) {
  950. this.raise(prop.key.start, ErrorMessages.PatternHasMethod);
  951. } else {
  952. super.toAssignableObjectExpressionProp(prop, ...args);
  953. }
  954. }
  955. finishCallExpression(node, optional) {
  956. super.finishCallExpression(node, optional);
  957. if (node.callee.type === "Import") {
  958. node.type = "ImportExpression";
  959. node.source = node.arguments[0];
  960. if (this.hasPlugin("importAssertions")) {
  961. var _node$arguments$;
  962. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  963. }
  964. delete node.arguments;
  965. delete node.callee;
  966. }
  967. return node;
  968. }
  969. toReferencedArguments(node) {
  970. if (node.type === "ImportExpression") {
  971. return;
  972. }
  973. super.toReferencedArguments(node);
  974. }
  975. parseExport(node) {
  976. super.parseExport(node);
  977. switch (node.type) {
  978. case "ExportAllDeclaration":
  979. node.exported = null;
  980. break;
  981. case "ExportNamedDeclaration":
  982. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  983. node.type = "ExportAllDeclaration";
  984. node.exported = node.specifiers[0].exported;
  985. delete node.specifiers;
  986. }
  987. break;
  988. }
  989. return node;
  990. }
  991. parseSubscript(base, startPos, startLoc, noCalls, state) {
  992. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  993. if (state.optionalChainMember) {
  994. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  995. node.type = node.type.substring(8);
  996. }
  997. if (state.stop) {
  998. const chain = this.startNodeAtNode(node);
  999. chain.expression = node;
  1000. return this.finishNode(chain, "ChainExpression");
  1001. }
  1002. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  1003. node.optional = false;
  1004. }
  1005. return node;
  1006. }
  1007. hasPropertyAsPrivateName(node) {
  1008. if (node.type === "ChainExpression") {
  1009. node = node.expression;
  1010. }
  1011. return super.hasPropertyAsPrivateName(node);
  1012. }
  1013. isOptionalChain(node) {
  1014. return node.type === "ChainExpression";
  1015. }
  1016. isObjectProperty(node) {
  1017. return node.type === "Property" && node.kind === "init" && !node.method;
  1018. }
  1019. isObjectMethod(node) {
  1020. return node.method || node.kind === "get" || node.kind === "set";
  1021. }
  1022. });
  1023. class TokContext {
  1024. constructor(token, preserveSpace) {
  1025. this.token = void 0;
  1026. this.preserveSpace = void 0;
  1027. this.token = token;
  1028. this.preserveSpace = !!preserveSpace;
  1029. }
  1030. }
  1031. const types = {
  1032. brace: new TokContext("{"),
  1033. templateQuasi: new TokContext("${"),
  1034. template: new TokContext("`", true)
  1035. };
  1036. types$1.braceR.updateContext = context => {
  1037. if (context.length > 1) {
  1038. context.pop();
  1039. }
  1040. };
  1041. types$1.braceL.updateContext = types$1.braceHashL.updateContext = context => {
  1042. context.push(types.brace);
  1043. };
  1044. types$1.dollarBraceL.updateContext = context => {
  1045. context.push(types.templateQuasi);
  1046. };
  1047. types$1.backQuote.updateContext = context => {
  1048. if (context[context.length - 1] === types.template) {
  1049. context.pop();
  1050. } else {
  1051. context.push(types.template);
  1052. }
  1053. };
  1054. 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";
  1055. 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";
  1056. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1057. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1058. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1059. 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];
  1060. 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];
  1061. function isInAstralSet(code, set) {
  1062. let pos = 0x10000;
  1063. for (let i = 0, length = set.length; i < length; i += 2) {
  1064. pos += set[i];
  1065. if (pos > code) return false;
  1066. pos += set[i + 1];
  1067. if (pos >= code) return true;
  1068. }
  1069. return false;
  1070. }
  1071. function isIdentifierStart(code) {
  1072. if (code < 65) return code === 36;
  1073. if (code <= 90) return true;
  1074. if (code < 97) return code === 95;
  1075. if (code <= 122) return true;
  1076. if (code <= 0xffff) {
  1077. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1078. }
  1079. return isInAstralSet(code, astralIdentifierStartCodes);
  1080. }
  1081. function isIdentifierChar(code) {
  1082. if (code < 48) return code === 36;
  1083. if (code < 58) return true;
  1084. if (code < 65) return false;
  1085. if (code <= 90) return true;
  1086. if (code < 97) return code === 95;
  1087. if (code <= 122) return true;
  1088. if (code <= 0xffff) {
  1089. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1090. }
  1091. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1092. }
  1093. const reservedWords = {
  1094. 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"],
  1095. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1096. strictBind: ["eval", "arguments"]
  1097. };
  1098. const keywords = new Set(reservedWords.keyword);
  1099. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1100. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1101. function isReservedWord(word, inModule) {
  1102. return inModule && word === "await" || word === "enum";
  1103. }
  1104. function isStrictReservedWord(word, inModule) {
  1105. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1106. }
  1107. function isStrictBindOnlyReservedWord(word) {
  1108. return reservedWordsStrictBindSet.has(word);
  1109. }
  1110. function isStrictBindReservedWord(word, inModule) {
  1111. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1112. }
  1113. function isKeyword(word) {
  1114. return keywords.has(word);
  1115. }
  1116. function isIteratorStart(current, next) {
  1117. return current === 64 && next === 64;
  1118. }
  1119. const reservedWordLikeSet = new Set(["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "eval", "arguments", "enum", "await"]);
  1120. function canBeReservedWord(word) {
  1121. return reservedWordLikeSet.has(word);
  1122. }
  1123. const SCOPE_OTHER = 0b000000000,
  1124. SCOPE_PROGRAM = 0b000000001,
  1125. SCOPE_FUNCTION = 0b000000010,
  1126. SCOPE_ARROW = 0b000000100,
  1127. SCOPE_SIMPLE_CATCH = 0b000001000,
  1128. SCOPE_SUPER = 0b000010000,
  1129. SCOPE_DIRECT_SUPER = 0b000100000,
  1130. SCOPE_CLASS = 0b001000000,
  1131. SCOPE_STATIC_BLOCK = 0b010000000,
  1132. SCOPE_TS_MODULE = 0b100000000,
  1133. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1134. const BIND_KIND_VALUE = 0b000000000001,
  1135. BIND_KIND_TYPE = 0b000000000010,
  1136. BIND_SCOPE_VAR = 0b000000000100,
  1137. BIND_SCOPE_LEXICAL = 0b000000001000,
  1138. BIND_SCOPE_FUNCTION = 0b000000010000,
  1139. BIND_FLAGS_NONE = 0b000001000000,
  1140. BIND_FLAGS_CLASS = 0b000010000000,
  1141. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1142. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1143. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1144. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1145. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1146. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1147. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1148. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1149. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1150. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1151. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1152. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1153. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1154. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1155. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1156. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1157. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1158. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1159. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1160. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1161. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1162. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1163. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1164. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1165. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1166. CLASS_ELEMENT_OTHER = 0;
  1167. class Scope {
  1168. constructor(flags) {
  1169. this.var = new Set();
  1170. this.lexical = new Set();
  1171. this.functions = new Set();
  1172. this.flags = flags;
  1173. }
  1174. }
  1175. class ScopeHandler {
  1176. constructor(raise, inModule) {
  1177. this.scopeStack = [];
  1178. this.undefinedExports = new Map();
  1179. this.undefinedPrivateNames = new Map();
  1180. this.raise = raise;
  1181. this.inModule = inModule;
  1182. }
  1183. get inFunction() {
  1184. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1185. }
  1186. get allowSuper() {
  1187. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1188. }
  1189. get allowDirectSuper() {
  1190. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1191. }
  1192. get inClass() {
  1193. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1194. }
  1195. get inClassAndNotInNonArrowFunction() {
  1196. const flags = this.currentThisScopeFlags();
  1197. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1198. }
  1199. get inStaticBlock() {
  1200. return (this.currentThisScopeFlags() & SCOPE_STATIC_BLOCK) > 0;
  1201. }
  1202. get inNonArrowFunction() {
  1203. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1204. }
  1205. get treatFunctionsAsVar() {
  1206. return this.treatFunctionsAsVarInScope(this.currentScope());
  1207. }
  1208. createScope(flags) {
  1209. return new Scope(flags);
  1210. }
  1211. enter(flags) {
  1212. this.scopeStack.push(this.createScope(flags));
  1213. }
  1214. exit() {
  1215. this.scopeStack.pop();
  1216. }
  1217. treatFunctionsAsVarInScope(scope) {
  1218. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  1219. }
  1220. declareName(name, bindingType, pos) {
  1221. let scope = this.currentScope();
  1222. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1223. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1224. if (bindingType & BIND_SCOPE_FUNCTION) {
  1225. scope.functions.add(name);
  1226. } else {
  1227. scope.lexical.add(name);
  1228. }
  1229. if (bindingType & BIND_SCOPE_LEXICAL) {
  1230. this.maybeExportDefined(scope, name);
  1231. }
  1232. } else if (bindingType & BIND_SCOPE_VAR) {
  1233. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1234. scope = this.scopeStack[i];
  1235. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1236. scope.var.add(name);
  1237. this.maybeExportDefined(scope, name);
  1238. if (scope.flags & SCOPE_VAR) break;
  1239. }
  1240. }
  1241. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1242. this.undefinedExports.delete(name);
  1243. }
  1244. }
  1245. maybeExportDefined(scope, name) {
  1246. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1247. this.undefinedExports.delete(name);
  1248. }
  1249. }
  1250. checkRedeclarationInScope(scope, name, bindingType, pos) {
  1251. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1252. this.raise(pos, ErrorMessages.VarRedeclaration, name);
  1253. }
  1254. }
  1255. isRedeclaredInScope(scope, name, bindingType) {
  1256. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1257. if (bindingType & BIND_SCOPE_LEXICAL) {
  1258. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1259. }
  1260. if (bindingType & BIND_SCOPE_FUNCTION) {
  1261. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1262. }
  1263. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1264. }
  1265. checkLocalExport(id) {
  1266. const {
  1267. name
  1268. } = id;
  1269. const topLevelScope = this.scopeStack[0];
  1270. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1271. this.undefinedExports.set(name, id.start);
  1272. }
  1273. }
  1274. currentScope() {
  1275. return this.scopeStack[this.scopeStack.length - 1];
  1276. }
  1277. currentVarScopeFlags() {
  1278. for (let i = this.scopeStack.length - 1;; i--) {
  1279. const {
  1280. flags
  1281. } = this.scopeStack[i];
  1282. if (flags & SCOPE_VAR) {
  1283. return flags;
  1284. }
  1285. }
  1286. }
  1287. currentThisScopeFlags() {
  1288. for (let i = this.scopeStack.length - 1;; i--) {
  1289. const {
  1290. flags
  1291. } = this.scopeStack[i];
  1292. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1293. return flags;
  1294. }
  1295. }
  1296. }
  1297. }
  1298. class FlowScope extends Scope {
  1299. constructor(...args) {
  1300. super(...args);
  1301. this.declareFunctions = new Set();
  1302. }
  1303. }
  1304. class FlowScopeHandler extends ScopeHandler {
  1305. createScope(flags) {
  1306. return new FlowScope(flags);
  1307. }
  1308. declareName(name, bindingType, pos) {
  1309. const scope = this.currentScope();
  1310. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1311. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1312. this.maybeExportDefined(scope, name);
  1313. scope.declareFunctions.add(name);
  1314. return;
  1315. }
  1316. super.declareName(...arguments);
  1317. }
  1318. isRedeclaredInScope(scope, name, bindingType) {
  1319. if (super.isRedeclaredInScope(...arguments)) return true;
  1320. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1321. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1322. }
  1323. return false;
  1324. }
  1325. checkLocalExport(id) {
  1326. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1327. super.checkLocalExport(id);
  1328. }
  1329. }
  1330. }
  1331. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  1332. const FlowErrors = makeErrorTemplates({
  1333. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  1334. 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.",
  1335. AssignReservedType: "Cannot overwrite reserved type %0.",
  1336. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  1337. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  1338. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  1339. EnumBooleanMemberNotInitialized: "Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
  1340. EnumDuplicateMemberName: "Enum member names need to be unique, but the name `%0` has already been used before in enum `%1`.",
  1341. EnumInconsistentMemberValues: "Enum `%0` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.",
  1342. EnumInvalidExplicitType: "Enum type `%1` is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1343. EnumInvalidExplicitTypeUnknownSupplied: "Supplied enum type is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  1344. EnumInvalidMemberInitializerPrimaryType: "Enum `%0` has type `%2`, so the initializer of `%1` needs to be a %2 literal.",
  1345. EnumInvalidMemberInitializerSymbolType: "Symbol enum members cannot be initialized. Use `%1,` in enum `%0`.",
  1346. EnumInvalidMemberInitializerUnknownType: "The enum member initializer for `%1` needs to be a literal (either a boolean, number, or string) in enum `%0`.",
  1347. EnumInvalidMemberName: "Enum member names cannot start with lowercase 'a' through 'z'. Instead of using `%0`, consider using `%1`, in enum `%2`.",
  1348. EnumNumberMemberNotInitialized: "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.",
  1349. EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.",
  1350. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  1351. 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.",
  1352. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  1353. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  1354. InexactVariance: "Explicit inexact syntax cannot have variance.",
  1355. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  1356. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  1357. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  1358. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  1359. OptionalBindingPattern: "A binding pattern parameter cannot be optional in an implementation signature.",
  1360. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  1361. SpreadVariance: "Spread properties cannot have variance.",
  1362. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  1363. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  1364. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  1365. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  1366. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  1367. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  1368. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  1369. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  1370. UnexpectedReservedType: "Unexpected reserved type %0.",
  1371. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  1372. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  1373. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  1374. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  1375. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  1376. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  1377. UnsupportedDeclareExportKind: "`declare export %0` is not supported. Use `%1` instead.",
  1378. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  1379. UnterminatedFlowComment: "Unterminated flow-comment."
  1380. }, ErrorCodes.SyntaxError);
  1381. function isEsModuleType(bodyElement) {
  1382. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  1383. }
  1384. function hasTypeImportKind(node) {
  1385. return node.importKind === "type" || node.importKind === "typeof";
  1386. }
  1387. function isMaybeDefaultImport(state) {
  1388. return (state.type === types$1.name || !!state.type.keyword) && state.value !== "from";
  1389. }
  1390. const exportSuggestions = {
  1391. const: "declare export var",
  1392. let: "declare export var",
  1393. type: "export type",
  1394. interface: "export interface"
  1395. };
  1396. function partition(list, test) {
  1397. const list1 = [];
  1398. const list2 = [];
  1399. for (let i = 0; i < list.length; i++) {
  1400. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  1401. }
  1402. return [list1, list2];
  1403. }
  1404. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  1405. var flow = (superClass => class extends superClass {
  1406. constructor(...args) {
  1407. super(...args);
  1408. this.flowPragma = undefined;
  1409. }
  1410. getScopeHandler() {
  1411. return FlowScopeHandler;
  1412. }
  1413. shouldParseTypes() {
  1414. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  1415. }
  1416. shouldParseEnums() {
  1417. return !!this.getPluginOption("flow", "enums");
  1418. }
  1419. finishToken(type, val) {
  1420. if (type !== types$1.string && type !== types$1.semi && type !== types$1.interpreterDirective) {
  1421. if (this.flowPragma === undefined) {
  1422. this.flowPragma = null;
  1423. }
  1424. }
  1425. return super.finishToken(type, val);
  1426. }
  1427. addComment(comment) {
  1428. if (this.flowPragma === undefined) {
  1429. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  1430. if (!matches) ; else if (matches[1] === "flow") {
  1431. this.flowPragma = "flow";
  1432. } else if (matches[1] === "noflow") {
  1433. this.flowPragma = "noflow";
  1434. } else {
  1435. throw new Error("Unexpected flow pragma");
  1436. }
  1437. }
  1438. return super.addComment(comment);
  1439. }
  1440. flowParseTypeInitialiser(tok) {
  1441. const oldInType = this.state.inType;
  1442. this.state.inType = true;
  1443. this.expect(tok || types$1.colon);
  1444. const type = this.flowParseType();
  1445. this.state.inType = oldInType;
  1446. return type;
  1447. }
  1448. flowParsePredicate() {
  1449. const node = this.startNode();
  1450. const moduloPos = this.state.start;
  1451. this.next();
  1452. this.expectContextual("checks");
  1453. if (this.state.lastTokStart > moduloPos + 1) {
  1454. this.raise(moduloPos, FlowErrors.UnexpectedSpaceBetweenModuloChecks);
  1455. }
  1456. if (this.eat(types$1.parenL)) {
  1457. node.value = this.parseExpression();
  1458. this.expect(types$1.parenR);
  1459. return this.finishNode(node, "DeclaredPredicate");
  1460. } else {
  1461. return this.finishNode(node, "InferredPredicate");
  1462. }
  1463. }
  1464. flowParseTypeAndPredicateInitialiser() {
  1465. const oldInType = this.state.inType;
  1466. this.state.inType = true;
  1467. this.expect(types$1.colon);
  1468. let type = null;
  1469. let predicate = null;
  1470. if (this.match(types$1.modulo)) {
  1471. this.state.inType = oldInType;
  1472. predicate = this.flowParsePredicate();
  1473. } else {
  1474. type = this.flowParseType();
  1475. this.state.inType = oldInType;
  1476. if (this.match(types$1.modulo)) {
  1477. predicate = this.flowParsePredicate();
  1478. }
  1479. }
  1480. return [type, predicate];
  1481. }
  1482. flowParseDeclareClass(node) {
  1483. this.next();
  1484. this.flowParseInterfaceish(node, true);
  1485. return this.finishNode(node, "DeclareClass");
  1486. }
  1487. flowParseDeclareFunction(node) {
  1488. this.next();
  1489. const id = node.id = this.parseIdentifier();
  1490. const typeNode = this.startNode();
  1491. const typeContainer = this.startNode();
  1492. if (this.isRelational("<")) {
  1493. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  1494. } else {
  1495. typeNode.typeParameters = null;
  1496. }
  1497. this.expect(types$1.parenL);
  1498. const tmp = this.flowParseFunctionTypeParams();
  1499. typeNode.params = tmp.params;
  1500. typeNode.rest = tmp.rest;
  1501. typeNode.this = tmp._this;
  1502. this.expect(types$1.parenR);
  1503. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1504. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  1505. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  1506. this.resetEndLocation(id);
  1507. this.semicolon();
  1508. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.start);
  1509. return this.finishNode(node, "DeclareFunction");
  1510. }
  1511. flowParseDeclare(node, insideModule) {
  1512. if (this.match(types$1._class)) {
  1513. return this.flowParseDeclareClass(node);
  1514. } else if (this.match(types$1._function)) {
  1515. return this.flowParseDeclareFunction(node);
  1516. } else if (this.match(types$1._var)) {
  1517. return this.flowParseDeclareVariable(node);
  1518. } else if (this.eatContextual("module")) {
  1519. if (this.match(types$1.dot)) {
  1520. return this.flowParseDeclareModuleExports(node);
  1521. } else {
  1522. if (insideModule) {
  1523. this.raise(this.state.lastTokStart, FlowErrors.NestedDeclareModule);
  1524. }
  1525. return this.flowParseDeclareModule(node);
  1526. }
  1527. } else if (this.isContextual("type")) {
  1528. return this.flowParseDeclareTypeAlias(node);
  1529. } else if (this.isContextual("opaque")) {
  1530. return this.flowParseDeclareOpaqueType(node);
  1531. } else if (this.isContextual("interface")) {
  1532. return this.flowParseDeclareInterface(node);
  1533. } else if (this.match(types$1._export)) {
  1534. return this.flowParseDeclareExportDeclaration(node, insideModule);
  1535. } else {
  1536. throw this.unexpected();
  1537. }
  1538. }
  1539. flowParseDeclareVariable(node) {
  1540. this.next();
  1541. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  1542. this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
  1543. this.semicolon();
  1544. return this.finishNode(node, "DeclareVariable");
  1545. }
  1546. flowParseDeclareModule(node) {
  1547. this.scope.enter(SCOPE_OTHER);
  1548. if (this.match(types$1.string)) {
  1549. node.id = this.parseExprAtom();
  1550. } else {
  1551. node.id = this.parseIdentifier();
  1552. }
  1553. const bodyNode = node.body = this.startNode();
  1554. const body = bodyNode.body = [];
  1555. this.expect(types$1.braceL);
  1556. while (!this.match(types$1.braceR)) {
  1557. let bodyNode = this.startNode();
  1558. if (this.match(types$1._import)) {
  1559. this.next();
  1560. if (!this.isContextual("type") && !this.match(types$1._typeof)) {
  1561. this.raise(this.state.lastTokStart, FlowErrors.InvalidNonTypeImportInDeclareModule);
  1562. }
  1563. this.parseImport(bodyNode);
  1564. } else {
  1565. this.expectContextual("declare", FlowErrors.UnsupportedStatementInDeclareModule);
  1566. bodyNode = this.flowParseDeclare(bodyNode, true);
  1567. }
  1568. body.push(bodyNode);
  1569. }
  1570. this.scope.exit();
  1571. this.expect(types$1.braceR);
  1572. this.finishNode(bodyNode, "BlockStatement");
  1573. let kind = null;
  1574. let hasModuleExport = false;
  1575. body.forEach(bodyElement => {
  1576. if (isEsModuleType(bodyElement)) {
  1577. if (kind === "CommonJS") {
  1578. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1579. }
  1580. kind = "ES";
  1581. } else if (bodyElement.type === "DeclareModuleExports") {
  1582. if (hasModuleExport) {
  1583. this.raise(bodyElement.start, FlowErrors.DuplicateDeclareModuleExports);
  1584. }
  1585. if (kind === "ES") {
  1586. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  1587. }
  1588. kind = "CommonJS";
  1589. hasModuleExport = true;
  1590. }
  1591. });
  1592. node.kind = kind || "CommonJS";
  1593. return this.finishNode(node, "DeclareModule");
  1594. }
  1595. flowParseDeclareExportDeclaration(node, insideModule) {
  1596. this.expect(types$1._export);
  1597. if (this.eat(types$1._default)) {
  1598. if (this.match(types$1._function) || this.match(types$1._class)) {
  1599. node.declaration = this.flowParseDeclare(this.startNode());
  1600. } else {
  1601. node.declaration = this.flowParseType();
  1602. this.semicolon();
  1603. }
  1604. node.default = true;
  1605. return this.finishNode(node, "DeclareExportDeclaration");
  1606. } else {
  1607. if (this.match(types$1._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  1608. const label = this.state.value;
  1609. const suggestion = exportSuggestions[label];
  1610. throw this.raise(this.state.start, FlowErrors.UnsupportedDeclareExportKind, label, suggestion);
  1611. }
  1612. if (this.match(types$1._var) || this.match(types$1._function) || this.match(types$1._class) || this.isContextual("opaque")) {
  1613. node.declaration = this.flowParseDeclare(this.startNode());
  1614. node.default = false;
  1615. return this.finishNode(node, "DeclareExportDeclaration");
  1616. } else if (this.match(types$1.star) || this.match(types$1.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  1617. node = this.parseExport(node);
  1618. if (node.type === "ExportNamedDeclaration") {
  1619. node.type = "ExportDeclaration";
  1620. node.default = false;
  1621. delete node.exportKind;
  1622. }
  1623. node.type = "Declare" + node.type;
  1624. return node;
  1625. }
  1626. }
  1627. throw this.unexpected();
  1628. }
  1629. flowParseDeclareModuleExports(node) {
  1630. this.next();
  1631. this.expectContextual("exports");
  1632. node.typeAnnotation = this.flowParseTypeAnnotation();
  1633. this.semicolon();
  1634. return this.finishNode(node, "DeclareModuleExports");
  1635. }
  1636. flowParseDeclareTypeAlias(node) {
  1637. this.next();
  1638. this.flowParseTypeAlias(node);
  1639. node.type = "DeclareTypeAlias";
  1640. return node;
  1641. }
  1642. flowParseDeclareOpaqueType(node) {
  1643. this.next();
  1644. this.flowParseOpaqueType(node, true);
  1645. node.type = "DeclareOpaqueType";
  1646. return node;
  1647. }
  1648. flowParseDeclareInterface(node) {
  1649. this.next();
  1650. this.flowParseInterfaceish(node);
  1651. return this.finishNode(node, "DeclareInterface");
  1652. }
  1653. flowParseInterfaceish(node, isClass = false) {
  1654. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  1655. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.start);
  1656. if (this.isRelational("<")) {
  1657. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1658. } else {
  1659. node.typeParameters = null;
  1660. }
  1661. node.extends = [];
  1662. node.implements = [];
  1663. node.mixins = [];
  1664. if (this.eat(types$1._extends)) {
  1665. do {
  1666. node.extends.push(this.flowParseInterfaceExtends());
  1667. } while (!isClass && this.eat(types$1.comma));
  1668. }
  1669. if (this.isContextual("mixins")) {
  1670. this.next();
  1671. do {
  1672. node.mixins.push(this.flowParseInterfaceExtends());
  1673. } while (this.eat(types$1.comma));
  1674. }
  1675. if (this.isContextual("implements")) {
  1676. this.next();
  1677. do {
  1678. node.implements.push(this.flowParseInterfaceExtends());
  1679. } while (this.eat(types$1.comma));
  1680. }
  1681. node.body = this.flowParseObjectType({
  1682. allowStatic: isClass,
  1683. allowExact: false,
  1684. allowSpread: false,
  1685. allowProto: isClass,
  1686. allowInexact: false
  1687. });
  1688. }
  1689. flowParseInterfaceExtends() {
  1690. const node = this.startNode();
  1691. node.id = this.flowParseQualifiedTypeIdentifier();
  1692. if (this.isRelational("<")) {
  1693. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1694. } else {
  1695. node.typeParameters = null;
  1696. }
  1697. return this.finishNode(node, "InterfaceExtends");
  1698. }
  1699. flowParseInterface(node) {
  1700. this.flowParseInterfaceish(node);
  1701. return this.finishNode(node, "InterfaceDeclaration");
  1702. }
  1703. checkNotUnderscore(word) {
  1704. if (word === "_") {
  1705. this.raise(this.state.start, FlowErrors.UnexpectedReservedUnderscore);
  1706. }
  1707. }
  1708. checkReservedType(word, startLoc, declaration) {
  1709. if (!reservedTypes.has(word)) return;
  1710. this.raise(startLoc, declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, word);
  1711. }
  1712. flowParseRestrictedIdentifier(liberal, declaration) {
  1713. this.checkReservedType(this.state.value, this.state.start, declaration);
  1714. return this.parseIdentifier(liberal);
  1715. }
  1716. flowParseTypeAlias(node) {
  1717. node.id = this.flowParseRestrictedIdentifier(false, true);
  1718. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1719. if (this.isRelational("<")) {
  1720. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1721. } else {
  1722. node.typeParameters = null;
  1723. }
  1724. node.right = this.flowParseTypeInitialiser(types$1.eq);
  1725. this.semicolon();
  1726. return this.finishNode(node, "TypeAlias");
  1727. }
  1728. flowParseOpaqueType(node, declare) {
  1729. this.expectContextual("type");
  1730. node.id = this.flowParseRestrictedIdentifier(true, true);
  1731. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1732. if (this.isRelational("<")) {
  1733. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1734. } else {
  1735. node.typeParameters = null;
  1736. }
  1737. node.supertype = null;
  1738. if (this.match(types$1.colon)) {
  1739. node.supertype = this.flowParseTypeInitialiser(types$1.colon);
  1740. }
  1741. node.impltype = null;
  1742. if (!declare) {
  1743. node.impltype = this.flowParseTypeInitialiser(types$1.eq);
  1744. }
  1745. this.semicolon();
  1746. return this.finishNode(node, "OpaqueType");
  1747. }
  1748. flowParseTypeParameter(requireDefault = false) {
  1749. const nodeStart = this.state.start;
  1750. const node = this.startNode();
  1751. const variance = this.flowParseVariance();
  1752. const ident = this.flowParseTypeAnnotatableIdentifier();
  1753. node.name = ident.name;
  1754. node.variance = variance;
  1755. node.bound = ident.typeAnnotation;
  1756. if (this.match(types$1.eq)) {
  1757. this.eat(types$1.eq);
  1758. node.default = this.flowParseType();
  1759. } else {
  1760. if (requireDefault) {
  1761. this.raise(nodeStart, FlowErrors.MissingTypeParamDefault);
  1762. }
  1763. }
  1764. return this.finishNode(node, "TypeParameter");
  1765. }
  1766. flowParseTypeParameterDeclaration() {
  1767. const oldInType = this.state.inType;
  1768. const node = this.startNode();
  1769. node.params = [];
  1770. this.state.inType = true;
  1771. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  1772. this.next();
  1773. } else {
  1774. this.unexpected();
  1775. }
  1776. let defaultRequired = false;
  1777. do {
  1778. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  1779. node.params.push(typeParameter);
  1780. if (typeParameter.default) {
  1781. defaultRequired = true;
  1782. }
  1783. if (!this.isRelational(">")) {
  1784. this.expect(types$1.comma);
  1785. }
  1786. } while (!this.isRelational(">"));
  1787. this.expectRelational(">");
  1788. this.state.inType = oldInType;
  1789. return this.finishNode(node, "TypeParameterDeclaration");
  1790. }
  1791. flowParseTypeParameterInstantiation() {
  1792. const node = this.startNode();
  1793. const oldInType = this.state.inType;
  1794. node.params = [];
  1795. this.state.inType = true;
  1796. this.expectRelational("<");
  1797. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1798. this.state.noAnonFunctionType = false;
  1799. while (!this.isRelational(">")) {
  1800. node.params.push(this.flowParseType());
  1801. if (!this.isRelational(">")) {
  1802. this.expect(types$1.comma);
  1803. }
  1804. }
  1805. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1806. this.expectRelational(">");
  1807. this.state.inType = oldInType;
  1808. return this.finishNode(node, "TypeParameterInstantiation");
  1809. }
  1810. flowParseTypeParameterInstantiationCallOrNew() {
  1811. const node = this.startNode();
  1812. const oldInType = this.state.inType;
  1813. node.params = [];
  1814. this.state.inType = true;
  1815. this.expectRelational("<");
  1816. while (!this.isRelational(">")) {
  1817. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1818. if (!this.isRelational(">")) {
  1819. this.expect(types$1.comma);
  1820. }
  1821. }
  1822. this.expectRelational(">");
  1823. this.state.inType = oldInType;
  1824. return this.finishNode(node, "TypeParameterInstantiation");
  1825. }
  1826. flowParseInterfaceType() {
  1827. const node = this.startNode();
  1828. this.expectContextual("interface");
  1829. node.extends = [];
  1830. if (this.eat(types$1._extends)) {
  1831. do {
  1832. node.extends.push(this.flowParseInterfaceExtends());
  1833. } while (this.eat(types$1.comma));
  1834. }
  1835. node.body = this.flowParseObjectType({
  1836. allowStatic: false,
  1837. allowExact: false,
  1838. allowSpread: false,
  1839. allowProto: false,
  1840. allowInexact: false
  1841. });
  1842. return this.finishNode(node, "InterfaceTypeAnnotation");
  1843. }
  1844. flowParseObjectPropertyKey() {
  1845. return this.match(types$1.num) || this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1846. }
  1847. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1848. node.static = isStatic;
  1849. if (this.lookahead().type === types$1.colon) {
  1850. node.id = this.flowParseObjectPropertyKey();
  1851. node.key = this.flowParseTypeInitialiser();
  1852. } else {
  1853. node.id = null;
  1854. node.key = this.flowParseType();
  1855. }
  1856. this.expect(types$1.bracketR);
  1857. node.value = this.flowParseTypeInitialiser();
  1858. node.variance = variance;
  1859. return this.finishNode(node, "ObjectTypeIndexer");
  1860. }
  1861. flowParseObjectTypeInternalSlot(node, isStatic) {
  1862. node.static = isStatic;
  1863. node.id = this.flowParseObjectPropertyKey();
  1864. this.expect(types$1.bracketR);
  1865. this.expect(types$1.bracketR);
  1866. if (this.isRelational("<") || this.match(types$1.parenL)) {
  1867. node.method = true;
  1868. node.optional = false;
  1869. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1870. } else {
  1871. node.method = false;
  1872. if (this.eat(types$1.question)) {
  1873. node.optional = true;
  1874. }
  1875. node.value = this.flowParseTypeInitialiser();
  1876. }
  1877. return this.finishNode(node, "ObjectTypeInternalSlot");
  1878. }
  1879. flowParseObjectTypeMethodish(node) {
  1880. node.params = [];
  1881. node.rest = null;
  1882. node.typeParameters = null;
  1883. node.this = null;
  1884. if (this.isRelational("<")) {
  1885. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1886. }
  1887. this.expect(types$1.parenL);
  1888. if (this.match(types$1._this)) {
  1889. node.this = this.flowParseFunctionTypeParam(true);
  1890. node.this.name = null;
  1891. if (!this.match(types$1.parenR)) {
  1892. this.expect(types$1.comma);
  1893. }
  1894. }
  1895. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  1896. node.params.push(this.flowParseFunctionTypeParam(false));
  1897. if (!this.match(types$1.parenR)) {
  1898. this.expect(types$1.comma);
  1899. }
  1900. }
  1901. if (this.eat(types$1.ellipsis)) {
  1902. node.rest = this.flowParseFunctionTypeParam(false);
  1903. }
  1904. this.expect(types$1.parenR);
  1905. node.returnType = this.flowParseTypeInitialiser();
  1906. return this.finishNode(node, "FunctionTypeAnnotation");
  1907. }
  1908. flowParseObjectTypeCallProperty(node, isStatic) {
  1909. const valueNode = this.startNode();
  1910. node.static = isStatic;
  1911. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1912. return this.finishNode(node, "ObjectTypeCallProperty");
  1913. }
  1914. flowParseObjectType({
  1915. allowStatic,
  1916. allowExact,
  1917. allowSpread,
  1918. allowProto,
  1919. allowInexact
  1920. }) {
  1921. const oldInType = this.state.inType;
  1922. this.state.inType = true;
  1923. const nodeStart = this.startNode();
  1924. nodeStart.callProperties = [];
  1925. nodeStart.properties = [];
  1926. nodeStart.indexers = [];
  1927. nodeStart.internalSlots = [];
  1928. let endDelim;
  1929. let exact;
  1930. let inexact = false;
  1931. if (allowExact && this.match(types$1.braceBarL)) {
  1932. this.expect(types$1.braceBarL);
  1933. endDelim = types$1.braceBarR;
  1934. exact = true;
  1935. } else {
  1936. this.expect(types$1.braceL);
  1937. endDelim = types$1.braceR;
  1938. exact = false;
  1939. }
  1940. nodeStart.exact = exact;
  1941. while (!this.match(endDelim)) {
  1942. let isStatic = false;
  1943. let protoStart = null;
  1944. let inexactStart = null;
  1945. const node = this.startNode();
  1946. if (allowProto && this.isContextual("proto")) {
  1947. const lookahead = this.lookahead();
  1948. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1949. this.next();
  1950. protoStart = this.state.start;
  1951. allowStatic = false;
  1952. }
  1953. }
  1954. if (allowStatic && this.isContextual("static")) {
  1955. const lookahead = this.lookahead();
  1956. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  1957. this.next();
  1958. isStatic = true;
  1959. }
  1960. }
  1961. const variance = this.flowParseVariance();
  1962. if (this.eat(types$1.bracketL)) {
  1963. if (protoStart != null) {
  1964. this.unexpected(protoStart);
  1965. }
  1966. if (this.eat(types$1.bracketL)) {
  1967. if (variance) {
  1968. this.unexpected(variance.start);
  1969. }
  1970. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  1971. } else {
  1972. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  1973. }
  1974. } else if (this.match(types$1.parenL) || this.isRelational("<")) {
  1975. if (protoStart != null) {
  1976. this.unexpected(protoStart);
  1977. }
  1978. if (variance) {
  1979. this.unexpected(variance.start);
  1980. }
  1981. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  1982. } else {
  1983. let kind = "init";
  1984. if (this.isContextual("get") || this.isContextual("set")) {
  1985. const lookahead = this.lookahead();
  1986. if (lookahead.type === types$1.name || lookahead.type === types$1.string || lookahead.type === types$1.num) {
  1987. kind = this.state.value;
  1988. this.next();
  1989. }
  1990. }
  1991. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  1992. if (propOrInexact === null) {
  1993. inexact = true;
  1994. inexactStart = this.state.lastTokStart;
  1995. } else {
  1996. nodeStart.properties.push(propOrInexact);
  1997. }
  1998. }
  1999. this.flowObjectTypeSemicolon();
  2000. if (inexactStart && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  2001. this.raise(inexactStart, FlowErrors.UnexpectedExplicitInexactInObject);
  2002. }
  2003. }
  2004. this.expect(endDelim);
  2005. if (allowSpread) {
  2006. nodeStart.inexact = inexact;
  2007. }
  2008. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  2009. this.state.inType = oldInType;
  2010. return out;
  2011. }
  2012. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  2013. if (this.eat(types$1.ellipsis)) {
  2014. const isInexactToken = this.match(types$1.comma) || this.match(types$1.semi) || this.match(types$1.braceR) || this.match(types$1.braceBarR);
  2015. if (isInexactToken) {
  2016. if (!allowSpread) {
  2017. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideNonObject);
  2018. } else if (!allowInexact) {
  2019. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideExact);
  2020. }
  2021. if (variance) {
  2022. this.raise(variance.start, FlowErrors.InexactVariance);
  2023. }
  2024. return null;
  2025. }
  2026. if (!allowSpread) {
  2027. this.raise(this.state.lastTokStart, FlowErrors.UnexpectedSpreadType);
  2028. }
  2029. if (protoStart != null) {
  2030. this.unexpected(protoStart);
  2031. }
  2032. if (variance) {
  2033. this.raise(variance.start, FlowErrors.SpreadVariance);
  2034. }
  2035. node.argument = this.flowParseType();
  2036. return this.finishNode(node, "ObjectTypeSpreadProperty");
  2037. } else {
  2038. node.key = this.flowParseObjectPropertyKey();
  2039. node.static = isStatic;
  2040. node.proto = protoStart != null;
  2041. node.kind = kind;
  2042. let optional = false;
  2043. if (this.isRelational("<") || this.match(types$1.parenL)) {
  2044. node.method = true;
  2045. if (protoStart != null) {
  2046. this.unexpected(protoStart);
  2047. }
  2048. if (variance) {
  2049. this.unexpected(variance.start);
  2050. }
  2051. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  2052. if (kind === "get" || kind === "set") {
  2053. this.flowCheckGetterSetterParams(node);
  2054. }
  2055. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  2056. this.raise(node.value.this.start, FlowErrors.ThisParamBannedInConstructor);
  2057. }
  2058. } else {
  2059. if (kind !== "init") this.unexpected();
  2060. node.method = false;
  2061. if (this.eat(types$1.question)) {
  2062. optional = true;
  2063. }
  2064. node.value = this.flowParseTypeInitialiser();
  2065. node.variance = variance;
  2066. }
  2067. node.optional = optional;
  2068. return this.finishNode(node, "ObjectTypeProperty");
  2069. }
  2070. }
  2071. flowCheckGetterSetterParams(property) {
  2072. const paramCount = property.kind === "get" ? 0 : 1;
  2073. const start = property.start;
  2074. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  2075. if (property.value.this) {
  2076. this.raise(property.value.this.start, property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam);
  2077. }
  2078. if (length !== paramCount) {
  2079. if (property.kind === "get") {
  2080. this.raise(start, ErrorMessages.BadGetterArity);
  2081. } else {
  2082. this.raise(start, ErrorMessages.BadSetterArity);
  2083. }
  2084. }
  2085. if (property.kind === "set" && property.value.rest) {
  2086. this.raise(start, ErrorMessages.BadSetterRestParameter);
  2087. }
  2088. }
  2089. flowObjectTypeSemicolon() {
  2090. if (!this.eat(types$1.semi) && !this.eat(types$1.comma) && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  2091. this.unexpected();
  2092. }
  2093. }
  2094. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  2095. startPos = startPos || this.state.start;
  2096. startLoc = startLoc || this.state.startLoc;
  2097. let node = id || this.flowParseRestrictedIdentifier(true);
  2098. while (this.eat(types$1.dot)) {
  2099. const node2 = this.startNodeAt(startPos, startLoc);
  2100. node2.qualification = node;
  2101. node2.id = this.flowParseRestrictedIdentifier(true);
  2102. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  2103. }
  2104. return node;
  2105. }
  2106. flowParseGenericType(startPos, startLoc, id) {
  2107. const node = this.startNodeAt(startPos, startLoc);
  2108. node.typeParameters = null;
  2109. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  2110. if (this.isRelational("<")) {
  2111. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2112. }
  2113. return this.finishNode(node, "GenericTypeAnnotation");
  2114. }
  2115. flowParseTypeofType() {
  2116. const node = this.startNode();
  2117. this.expect(types$1._typeof);
  2118. node.argument = this.flowParsePrimaryType();
  2119. return this.finishNode(node, "TypeofTypeAnnotation");
  2120. }
  2121. flowParseTupleType() {
  2122. const node = this.startNode();
  2123. node.types = [];
  2124. this.expect(types$1.bracketL);
  2125. while (this.state.pos < this.length && !this.match(types$1.bracketR)) {
  2126. node.types.push(this.flowParseType());
  2127. if (this.match(types$1.bracketR)) break;
  2128. this.expect(types$1.comma);
  2129. }
  2130. this.expect(types$1.bracketR);
  2131. return this.finishNode(node, "TupleTypeAnnotation");
  2132. }
  2133. flowParseFunctionTypeParam(first) {
  2134. let name = null;
  2135. let optional = false;
  2136. let typeAnnotation = null;
  2137. const node = this.startNode();
  2138. const lh = this.lookahead();
  2139. const isThis = this.state.type === types$1._this;
  2140. if (lh.type === types$1.colon || lh.type === types$1.question) {
  2141. if (isThis && !first) {
  2142. this.raise(node.start, FlowErrors.ThisParamMustBeFirst);
  2143. }
  2144. name = this.parseIdentifier(isThis);
  2145. if (this.eat(types$1.question)) {
  2146. optional = true;
  2147. if (isThis) {
  2148. this.raise(node.start, FlowErrors.ThisParamMayNotBeOptional);
  2149. }
  2150. }
  2151. typeAnnotation = this.flowParseTypeInitialiser();
  2152. } else {
  2153. typeAnnotation = this.flowParseType();
  2154. }
  2155. node.name = name;
  2156. node.optional = optional;
  2157. node.typeAnnotation = typeAnnotation;
  2158. return this.finishNode(node, "FunctionTypeParam");
  2159. }
  2160. reinterpretTypeAsFunctionTypeParam(type) {
  2161. const node = this.startNodeAt(type.start, type.loc.start);
  2162. node.name = null;
  2163. node.optional = false;
  2164. node.typeAnnotation = type;
  2165. return this.finishNode(node, "FunctionTypeParam");
  2166. }
  2167. flowParseFunctionTypeParams(params = []) {
  2168. let rest = null;
  2169. let _this = null;
  2170. if (this.match(types$1._this)) {
  2171. _this = this.flowParseFunctionTypeParam(true);
  2172. _this.name = null;
  2173. if (!this.match(types$1.parenR)) {
  2174. this.expect(types$1.comma);
  2175. }
  2176. }
  2177. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2178. params.push(this.flowParseFunctionTypeParam(false));
  2179. if (!this.match(types$1.parenR)) {
  2180. this.expect(types$1.comma);
  2181. }
  2182. }
  2183. if (this.eat(types$1.ellipsis)) {
  2184. rest = this.flowParseFunctionTypeParam(false);
  2185. }
  2186. return {
  2187. params,
  2188. rest,
  2189. _this
  2190. };
  2191. }
  2192. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  2193. switch (id.name) {
  2194. case "any":
  2195. return this.finishNode(node, "AnyTypeAnnotation");
  2196. case "bool":
  2197. case "boolean":
  2198. return this.finishNode(node, "BooleanTypeAnnotation");
  2199. case "mixed":
  2200. return this.finishNode(node, "MixedTypeAnnotation");
  2201. case "empty":
  2202. return this.finishNode(node, "EmptyTypeAnnotation");
  2203. case "number":
  2204. return this.finishNode(node, "NumberTypeAnnotation");
  2205. case "string":
  2206. return this.finishNode(node, "StringTypeAnnotation");
  2207. case "symbol":
  2208. return this.finishNode(node, "SymbolTypeAnnotation");
  2209. default:
  2210. this.checkNotUnderscore(id.name);
  2211. return this.flowParseGenericType(startPos, startLoc, id);
  2212. }
  2213. }
  2214. flowParsePrimaryType() {
  2215. const startPos = this.state.start;
  2216. const startLoc = this.state.startLoc;
  2217. const node = this.startNode();
  2218. let tmp;
  2219. let type;
  2220. let isGroupedType = false;
  2221. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2222. switch (this.state.type) {
  2223. case types$1.name:
  2224. if (this.isContextual("interface")) {
  2225. return this.flowParseInterfaceType();
  2226. }
  2227. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  2228. case types$1.braceL:
  2229. return this.flowParseObjectType({
  2230. allowStatic: false,
  2231. allowExact: false,
  2232. allowSpread: true,
  2233. allowProto: false,
  2234. allowInexact: true
  2235. });
  2236. case types$1.braceBarL:
  2237. return this.flowParseObjectType({
  2238. allowStatic: false,
  2239. allowExact: true,
  2240. allowSpread: true,
  2241. allowProto: false,
  2242. allowInexact: false
  2243. });
  2244. case types$1.bracketL:
  2245. this.state.noAnonFunctionType = false;
  2246. type = this.flowParseTupleType();
  2247. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2248. return type;
  2249. case types$1.relational:
  2250. if (this.state.value === "<") {
  2251. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2252. this.expect(types$1.parenL);
  2253. tmp = this.flowParseFunctionTypeParams();
  2254. node.params = tmp.params;
  2255. node.rest = tmp.rest;
  2256. node.this = tmp._this;
  2257. this.expect(types$1.parenR);
  2258. this.expect(types$1.arrow);
  2259. node.returnType = this.flowParseType();
  2260. return this.finishNode(node, "FunctionTypeAnnotation");
  2261. }
  2262. break;
  2263. case types$1.parenL:
  2264. this.next();
  2265. if (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  2266. if (this.match(types$1.name) || this.match(types$1._this)) {
  2267. const token = this.lookahead().type;
  2268. isGroupedType = token !== types$1.question && token !== types$1.colon;
  2269. } else {
  2270. isGroupedType = true;
  2271. }
  2272. }
  2273. if (isGroupedType) {
  2274. this.state.noAnonFunctionType = false;
  2275. type = this.flowParseType();
  2276. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2277. if (this.state.noAnonFunctionType || !(this.match(types$1.comma) || this.match(types$1.parenR) && this.lookahead().type === types$1.arrow)) {
  2278. this.expect(types$1.parenR);
  2279. return type;
  2280. } else {
  2281. this.eat(types$1.comma);
  2282. }
  2283. }
  2284. if (type) {
  2285. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  2286. } else {
  2287. tmp = this.flowParseFunctionTypeParams();
  2288. }
  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. node.typeParameters = null;
  2296. return this.finishNode(node, "FunctionTypeAnnotation");
  2297. case types$1.string:
  2298. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  2299. case types$1._true:
  2300. case types$1._false:
  2301. node.value = this.match(types$1._true);
  2302. this.next();
  2303. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  2304. case types$1.plusMin:
  2305. if (this.state.value === "-") {
  2306. this.next();
  2307. if (this.match(types$1.num)) {
  2308. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  2309. }
  2310. if (this.match(types$1.bigint)) {
  2311. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  2312. }
  2313. throw this.raise(this.state.start, FlowErrors.UnexpectedSubtractionOperand);
  2314. }
  2315. throw this.unexpected();
  2316. case types$1.num:
  2317. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  2318. case types$1.bigint:
  2319. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  2320. case types$1._void:
  2321. this.next();
  2322. return this.finishNode(node, "VoidTypeAnnotation");
  2323. case types$1._null:
  2324. this.next();
  2325. return this.finishNode(node, "NullLiteralTypeAnnotation");
  2326. case types$1._this:
  2327. this.next();
  2328. return this.finishNode(node, "ThisTypeAnnotation");
  2329. case types$1.star:
  2330. this.next();
  2331. return this.finishNode(node, "ExistsTypeAnnotation");
  2332. default:
  2333. if (this.state.type.keyword === "typeof") {
  2334. return this.flowParseTypeofType();
  2335. } else if (this.state.type.keyword) {
  2336. const label = this.state.type.label;
  2337. this.next();
  2338. return super.createIdentifier(node, label);
  2339. }
  2340. }
  2341. throw this.unexpected();
  2342. }
  2343. flowParsePostfixType() {
  2344. const startPos = this.state.start;
  2345. const startLoc = this.state.startLoc;
  2346. let type = this.flowParsePrimaryType();
  2347. let seenOptionalIndexedAccess = false;
  2348. while ((this.match(types$1.bracketL) || this.match(types$1.questionDot)) && !this.canInsertSemicolon()) {
  2349. const node = this.startNodeAt(startPos, startLoc);
  2350. const optional = this.eat(types$1.questionDot);
  2351. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  2352. this.expect(types$1.bracketL);
  2353. if (!optional && this.match(types$1.bracketR)) {
  2354. node.elementType = type;
  2355. this.next();
  2356. type = this.finishNode(node, "ArrayTypeAnnotation");
  2357. } else {
  2358. node.objectType = type;
  2359. node.indexType = this.flowParseType();
  2360. this.expect(types$1.bracketR);
  2361. if (seenOptionalIndexedAccess) {
  2362. node.optional = optional;
  2363. type = this.finishNode(node, "OptionalIndexedAccessType");
  2364. } else {
  2365. type = this.finishNode(node, "IndexedAccessType");
  2366. }
  2367. }
  2368. }
  2369. return type;
  2370. }
  2371. flowParsePrefixType() {
  2372. const node = this.startNode();
  2373. if (this.eat(types$1.question)) {
  2374. node.typeAnnotation = this.flowParsePrefixType();
  2375. return this.finishNode(node, "NullableTypeAnnotation");
  2376. } else {
  2377. return this.flowParsePostfixType();
  2378. }
  2379. }
  2380. flowParseAnonFunctionWithoutParens() {
  2381. const param = this.flowParsePrefixType();
  2382. if (!this.state.noAnonFunctionType && this.eat(types$1.arrow)) {
  2383. const node = this.startNodeAt(param.start, param.loc.start);
  2384. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  2385. node.rest = null;
  2386. node.this = null;
  2387. node.returnType = this.flowParseType();
  2388. node.typeParameters = null;
  2389. return this.finishNode(node, "FunctionTypeAnnotation");
  2390. }
  2391. return param;
  2392. }
  2393. flowParseIntersectionType() {
  2394. const node = this.startNode();
  2395. this.eat(types$1.bitwiseAND);
  2396. const type = this.flowParseAnonFunctionWithoutParens();
  2397. node.types = [type];
  2398. while (this.eat(types$1.bitwiseAND)) {
  2399. node.types.push(this.flowParseAnonFunctionWithoutParens());
  2400. }
  2401. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  2402. }
  2403. flowParseUnionType() {
  2404. const node = this.startNode();
  2405. this.eat(types$1.bitwiseOR);
  2406. const type = this.flowParseIntersectionType();
  2407. node.types = [type];
  2408. while (this.eat(types$1.bitwiseOR)) {
  2409. node.types.push(this.flowParseIntersectionType());
  2410. }
  2411. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  2412. }
  2413. flowParseType() {
  2414. const oldInType = this.state.inType;
  2415. this.state.inType = true;
  2416. const type = this.flowParseUnionType();
  2417. this.state.inType = oldInType;
  2418. return type;
  2419. }
  2420. flowParseTypeOrImplicitInstantiation() {
  2421. if (this.state.type === types$1.name && this.state.value === "_") {
  2422. const startPos = this.state.start;
  2423. const startLoc = this.state.startLoc;
  2424. const node = this.parseIdentifier();
  2425. return this.flowParseGenericType(startPos, startLoc, node);
  2426. } else {
  2427. return this.flowParseType();
  2428. }
  2429. }
  2430. flowParseTypeAnnotation() {
  2431. const node = this.startNode();
  2432. node.typeAnnotation = this.flowParseTypeInitialiser();
  2433. return this.finishNode(node, "TypeAnnotation");
  2434. }
  2435. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  2436. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  2437. if (this.match(types$1.colon)) {
  2438. ident.typeAnnotation = this.flowParseTypeAnnotation();
  2439. this.resetEndLocation(ident);
  2440. }
  2441. return ident;
  2442. }
  2443. typeCastToParameter(node) {
  2444. node.expression.typeAnnotation = node.typeAnnotation;
  2445. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  2446. return node.expression;
  2447. }
  2448. flowParseVariance() {
  2449. let variance = null;
  2450. if (this.match(types$1.plusMin)) {
  2451. variance = this.startNode();
  2452. if (this.state.value === "+") {
  2453. variance.kind = "plus";
  2454. } else {
  2455. variance.kind = "minus";
  2456. }
  2457. this.next();
  2458. this.finishNode(variance, "Variance");
  2459. }
  2460. return variance;
  2461. }
  2462. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  2463. if (allowExpressionBody) {
  2464. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  2465. }
  2466. return super.parseFunctionBody(node, false, isMethod);
  2467. }
  2468. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  2469. if (this.match(types$1.colon)) {
  2470. const typeNode = this.startNode();
  2471. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2472. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2473. }
  2474. super.parseFunctionBodyAndFinish(node, type, isMethod);
  2475. }
  2476. parseStatement(context, topLevel) {
  2477. if (this.state.strict && this.match(types$1.name) && this.state.value === "interface") {
  2478. const lookahead = this.lookahead();
  2479. if (lookahead.type === types$1.name || isKeyword(lookahead.value)) {
  2480. const node = this.startNode();
  2481. this.next();
  2482. return this.flowParseInterface(node);
  2483. }
  2484. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2485. const node = this.startNode();
  2486. this.next();
  2487. return this.flowParseEnumDeclaration(node);
  2488. }
  2489. const stmt = super.parseStatement(context, topLevel);
  2490. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  2491. this.flowPragma = null;
  2492. }
  2493. return stmt;
  2494. }
  2495. parseExpressionStatement(node, expr) {
  2496. if (expr.type === "Identifier") {
  2497. if (expr.name === "declare") {
  2498. 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)) {
  2499. return this.flowParseDeclare(node);
  2500. }
  2501. } else if (this.match(types$1.name)) {
  2502. if (expr.name === "interface") {
  2503. return this.flowParseInterface(node);
  2504. } else if (expr.name === "type") {
  2505. return this.flowParseTypeAlias(node);
  2506. } else if (expr.name === "opaque") {
  2507. return this.flowParseOpaqueType(node, false);
  2508. }
  2509. }
  2510. }
  2511. return super.parseExpressionStatement(node, expr);
  2512. }
  2513. shouldParseExportDeclaration() {
  2514. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || this.shouldParseEnums() && this.isContextual("enum") || super.shouldParseExportDeclaration();
  2515. }
  2516. isExportDefaultSpecifier() {
  2517. if (this.match(types$1.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque" || this.shouldParseEnums() && this.state.value === "enum")) {
  2518. return false;
  2519. }
  2520. return super.isExportDefaultSpecifier();
  2521. }
  2522. parseExportDefaultExpression() {
  2523. if (this.shouldParseEnums() && this.isContextual("enum")) {
  2524. const node = this.startNode();
  2525. this.next();
  2526. return this.flowParseEnumDeclaration(node);
  2527. }
  2528. return super.parseExportDefaultExpression();
  2529. }
  2530. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  2531. if (!this.match(types$1.question)) return expr;
  2532. if (refNeedsArrowPos) {
  2533. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  2534. if (!result.node) {
  2535. refNeedsArrowPos.start = result.error.pos || this.state.start;
  2536. return expr;
  2537. }
  2538. if (result.error) this.state = result.failState;
  2539. return result.node;
  2540. }
  2541. this.expect(types$1.question);
  2542. const state = this.state.clone();
  2543. const originalNoArrowAt = this.state.noArrowAt;
  2544. const node = this.startNodeAt(startPos, startLoc);
  2545. let {
  2546. consequent,
  2547. failed
  2548. } = this.tryParseConditionalConsequent();
  2549. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2550. if (failed || invalid.length > 0) {
  2551. const noArrowAt = [...originalNoArrowAt];
  2552. if (invalid.length > 0) {
  2553. this.state = state;
  2554. this.state.noArrowAt = noArrowAt;
  2555. for (let i = 0; i < invalid.length; i++) {
  2556. noArrowAt.push(invalid[i].start);
  2557. }
  2558. ({
  2559. consequent,
  2560. failed
  2561. } = this.tryParseConditionalConsequent());
  2562. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  2563. }
  2564. if (failed && valid.length > 1) {
  2565. this.raise(state.start, FlowErrors.AmbiguousConditionalArrow);
  2566. }
  2567. if (failed && valid.length === 1) {
  2568. this.state = state;
  2569. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  2570. ({
  2571. consequent,
  2572. failed
  2573. } = this.tryParseConditionalConsequent());
  2574. }
  2575. }
  2576. this.getArrowLikeExpressions(consequent, true);
  2577. this.state.noArrowAt = originalNoArrowAt;
  2578. this.expect(types$1.colon);
  2579. node.test = expr;
  2580. node.consequent = consequent;
  2581. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined, undefined));
  2582. return this.finishNode(node, "ConditionalExpression");
  2583. }
  2584. tryParseConditionalConsequent() {
  2585. this.state.noArrowParamsConversionAt.push(this.state.start);
  2586. const consequent = this.parseMaybeAssignAllowIn();
  2587. const failed = !this.match(types$1.colon);
  2588. this.state.noArrowParamsConversionAt.pop();
  2589. return {
  2590. consequent,
  2591. failed
  2592. };
  2593. }
  2594. getArrowLikeExpressions(node, disallowInvalid) {
  2595. const stack = [node];
  2596. const arrows = [];
  2597. while (stack.length !== 0) {
  2598. const node = stack.pop();
  2599. if (node.type === "ArrowFunctionExpression") {
  2600. if (node.typeParameters || !node.returnType) {
  2601. this.finishArrowValidation(node);
  2602. } else {
  2603. arrows.push(node);
  2604. }
  2605. stack.push(node.body);
  2606. } else if (node.type === "ConditionalExpression") {
  2607. stack.push(node.consequent);
  2608. stack.push(node.alternate);
  2609. }
  2610. }
  2611. if (disallowInvalid) {
  2612. arrows.forEach(node => this.finishArrowValidation(node));
  2613. return [arrows, []];
  2614. }
  2615. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  2616. }
  2617. finishArrowValidation(node) {
  2618. var _node$extra;
  2619. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingComma, false);
  2620. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  2621. super.checkParams(node, false, true);
  2622. this.scope.exit();
  2623. }
  2624. forwardNoArrowParamsConversionAt(node, parse) {
  2625. let result;
  2626. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2627. this.state.noArrowParamsConversionAt.push(this.state.start);
  2628. result = parse();
  2629. this.state.noArrowParamsConversionAt.pop();
  2630. } else {
  2631. result = parse();
  2632. }
  2633. return result;
  2634. }
  2635. parseParenItem(node, startPos, startLoc) {
  2636. node = super.parseParenItem(node, startPos, startLoc);
  2637. if (this.eat(types$1.question)) {
  2638. node.optional = true;
  2639. this.resetEndLocation(node);
  2640. }
  2641. if (this.match(types$1.colon)) {
  2642. const typeCastNode = this.startNodeAt(startPos, startLoc);
  2643. typeCastNode.expression = node;
  2644. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  2645. return this.finishNode(typeCastNode, "TypeCastExpression");
  2646. }
  2647. return node;
  2648. }
  2649. assertModuleNodeAllowed(node) {
  2650. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  2651. return;
  2652. }
  2653. super.assertModuleNodeAllowed(node);
  2654. }
  2655. parseExport(node) {
  2656. const decl = super.parseExport(node);
  2657. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  2658. decl.exportKind = decl.exportKind || "value";
  2659. }
  2660. return decl;
  2661. }
  2662. parseExportDeclaration(node) {
  2663. if (this.isContextual("type")) {
  2664. node.exportKind = "type";
  2665. const declarationNode = this.startNode();
  2666. this.next();
  2667. if (this.match(types$1.braceL)) {
  2668. node.specifiers = this.parseExportSpecifiers();
  2669. this.parseExportFrom(node);
  2670. return null;
  2671. } else {
  2672. return this.flowParseTypeAlias(declarationNode);
  2673. }
  2674. } else if (this.isContextual("opaque")) {
  2675. node.exportKind = "type";
  2676. const declarationNode = this.startNode();
  2677. this.next();
  2678. return this.flowParseOpaqueType(declarationNode, false);
  2679. } else if (this.isContextual("interface")) {
  2680. node.exportKind = "type";
  2681. const declarationNode = this.startNode();
  2682. this.next();
  2683. return this.flowParseInterface(declarationNode);
  2684. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  2685. node.exportKind = "value";
  2686. const declarationNode = this.startNode();
  2687. this.next();
  2688. return this.flowParseEnumDeclaration(declarationNode);
  2689. } else {
  2690. return super.parseExportDeclaration(node);
  2691. }
  2692. }
  2693. eatExportStar(node) {
  2694. if (super.eatExportStar(...arguments)) return true;
  2695. if (this.isContextual("type") && this.lookahead().type === types$1.star) {
  2696. node.exportKind = "type";
  2697. this.next();
  2698. this.next();
  2699. return true;
  2700. }
  2701. return false;
  2702. }
  2703. maybeParseExportNamespaceSpecifier(node) {
  2704. const pos = this.state.start;
  2705. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  2706. if (hasNamespace && node.exportKind === "type") {
  2707. this.unexpected(pos);
  2708. }
  2709. return hasNamespace;
  2710. }
  2711. parseClassId(node, isStatement, optionalId) {
  2712. super.parseClassId(node, isStatement, optionalId);
  2713. if (this.isRelational("<")) {
  2714. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2715. }
  2716. }
  2717. parseClassMember(classBody, member, state) {
  2718. const pos = this.state.start;
  2719. if (this.isContextual("declare")) {
  2720. if (this.parseClassMemberFromModifier(classBody, member)) {
  2721. return;
  2722. }
  2723. member.declare = true;
  2724. }
  2725. super.parseClassMember(classBody, member, state);
  2726. if (member.declare) {
  2727. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  2728. this.raise(pos, FlowErrors.DeclareClassElement);
  2729. } else if (member.value) {
  2730. this.raise(member.value.start, FlowErrors.DeclareClassFieldInitializer);
  2731. }
  2732. }
  2733. }
  2734. isIterator(word) {
  2735. return word === "iterator" || word === "asyncIterator";
  2736. }
  2737. readIterator() {
  2738. const word = super.readWord1();
  2739. const fullWord = "@@" + word;
  2740. if (!this.isIterator(word) || !this.state.inType) {
  2741. this.raise(this.state.pos, ErrorMessages.InvalidIdentifier, fullWord);
  2742. }
  2743. this.finishToken(types$1.name, fullWord);
  2744. }
  2745. getTokenFromCode(code) {
  2746. const next = this.input.charCodeAt(this.state.pos + 1);
  2747. if (code === 123 && next === 124) {
  2748. return this.finishOp(types$1.braceBarL, 2);
  2749. } else if (this.state.inType && (code === 62 || code === 60)) {
  2750. return this.finishOp(types$1.relational, 1);
  2751. } else if (this.state.inType && code === 63) {
  2752. if (next === 46) {
  2753. return this.finishOp(types$1.questionDot, 2);
  2754. }
  2755. return this.finishOp(types$1.question, 1);
  2756. } else if (isIteratorStart(code, next)) {
  2757. this.state.pos += 2;
  2758. return this.readIterator();
  2759. } else {
  2760. return super.getTokenFromCode(code);
  2761. }
  2762. }
  2763. isAssignable(node, isBinding) {
  2764. switch (node.type) {
  2765. case "Identifier":
  2766. case "ObjectPattern":
  2767. case "ArrayPattern":
  2768. case "AssignmentPattern":
  2769. return true;
  2770. case "ObjectExpression":
  2771. {
  2772. const last = node.properties.length - 1;
  2773. return node.properties.every((prop, i) => {
  2774. return prop.type !== "ObjectMethod" && (i === last || prop.type === "SpreadElement") && this.isAssignable(prop);
  2775. });
  2776. }
  2777. case "ObjectProperty":
  2778. return this.isAssignable(node.value);
  2779. case "SpreadElement":
  2780. return this.isAssignable(node.argument);
  2781. case "ArrayExpression":
  2782. return node.elements.every(element => this.isAssignable(element));
  2783. case "AssignmentExpression":
  2784. return node.operator === "=";
  2785. case "ParenthesizedExpression":
  2786. case "TypeCastExpression":
  2787. return this.isAssignable(node.expression);
  2788. case "MemberExpression":
  2789. case "OptionalMemberExpression":
  2790. return !isBinding;
  2791. default:
  2792. return false;
  2793. }
  2794. }
  2795. toAssignable(node, isLHS = false) {
  2796. if (node.type === "TypeCastExpression") {
  2797. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  2798. } else {
  2799. return super.toAssignable(node, isLHS);
  2800. }
  2801. }
  2802. toAssignableList(exprList, trailingCommaPos, isLHS) {
  2803. for (let i = 0; i < exprList.length; i++) {
  2804. const expr = exprList[i];
  2805. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  2806. exprList[i] = this.typeCastToParameter(expr);
  2807. }
  2808. }
  2809. return super.toAssignableList(exprList, trailingCommaPos, isLHS);
  2810. }
  2811. toReferencedList(exprList, isParenthesizedExpr) {
  2812. for (let i = 0; i < exprList.length; i++) {
  2813. var _expr$extra;
  2814. const expr = exprList[i];
  2815. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  2816. this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern);
  2817. }
  2818. }
  2819. return exprList;
  2820. }
  2821. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  2822. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  2823. if (canBePattern && !this.state.maybeInArrowParameters) {
  2824. this.toReferencedList(node.elements);
  2825. }
  2826. return node;
  2827. }
  2828. checkLVal(expr, ...args) {
  2829. if (expr.type !== "TypeCastExpression") {
  2830. return super.checkLVal(expr, ...args);
  2831. }
  2832. }
  2833. parseClassProperty(node) {
  2834. if (this.match(types$1.colon)) {
  2835. node.typeAnnotation = this.flowParseTypeAnnotation();
  2836. }
  2837. return super.parseClassProperty(node);
  2838. }
  2839. parseClassPrivateProperty(node) {
  2840. if (this.match(types$1.colon)) {
  2841. node.typeAnnotation = this.flowParseTypeAnnotation();
  2842. }
  2843. return super.parseClassPrivateProperty(node);
  2844. }
  2845. isClassMethod() {
  2846. return this.isRelational("<") || super.isClassMethod();
  2847. }
  2848. isClassProperty() {
  2849. return this.match(types$1.colon) || super.isClassProperty();
  2850. }
  2851. isNonstaticConstructor(method) {
  2852. return !this.match(types$1.colon) && super.isNonstaticConstructor(method);
  2853. }
  2854. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  2855. if (method.variance) {
  2856. this.unexpected(method.variance.start);
  2857. }
  2858. delete method.variance;
  2859. if (this.isRelational("<")) {
  2860. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2861. }
  2862. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  2863. if (method.params && isConstructor) {
  2864. const params = method.params;
  2865. if (params.length > 0 && this.isThisParam(params[0])) {
  2866. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2867. }
  2868. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  2869. const params = method.value.params;
  2870. if (params.length > 0 && this.isThisParam(params[0])) {
  2871. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  2872. }
  2873. }
  2874. }
  2875. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  2876. if (method.variance) {
  2877. this.unexpected(method.variance.start);
  2878. }
  2879. delete method.variance;
  2880. if (this.isRelational("<")) {
  2881. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2882. }
  2883. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  2884. }
  2885. parseClassSuper(node) {
  2886. super.parseClassSuper(node);
  2887. if (node.superClass && this.isRelational("<")) {
  2888. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  2889. }
  2890. if (this.isContextual("implements")) {
  2891. this.next();
  2892. const implemented = node.implements = [];
  2893. do {
  2894. const node = this.startNode();
  2895. node.id = this.flowParseRestrictedIdentifier(true);
  2896. if (this.isRelational("<")) {
  2897. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2898. } else {
  2899. node.typeParameters = null;
  2900. }
  2901. implemented.push(this.finishNode(node, "ClassImplements"));
  2902. } while (this.eat(types$1.comma));
  2903. }
  2904. }
  2905. checkGetterSetterParams(method) {
  2906. super.checkGetterSetterParams(method);
  2907. const params = this.getObjectOrClassMethodParams(method);
  2908. if (params.length > 0) {
  2909. const param = params[0];
  2910. if (this.isThisParam(param) && method.kind === "get") {
  2911. this.raise(param.start, FlowErrors.GetterMayNotHaveThisParam);
  2912. } else if (this.isThisParam(param)) {
  2913. this.raise(param.start, FlowErrors.SetterMayNotHaveThisParam);
  2914. }
  2915. }
  2916. }
  2917. parsePropertyName(node, isPrivateNameAllowed) {
  2918. const variance = this.flowParseVariance();
  2919. const key = super.parsePropertyName(node, isPrivateNameAllowed);
  2920. node.variance = variance;
  2921. return key;
  2922. }
  2923. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  2924. if (prop.variance) {
  2925. this.unexpected(prop.variance.start);
  2926. }
  2927. delete prop.variance;
  2928. let typeParameters;
  2929. if (this.isRelational("<") && !isAccessor) {
  2930. typeParameters = this.flowParseTypeParameterDeclaration();
  2931. if (!this.match(types$1.parenL)) this.unexpected();
  2932. }
  2933. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  2934. if (typeParameters) {
  2935. (prop.value || prop).typeParameters = typeParameters;
  2936. }
  2937. }
  2938. parseAssignableListItemTypes(param) {
  2939. if (this.eat(types$1.question)) {
  2940. if (param.type !== "Identifier") {
  2941. this.raise(param.start, FlowErrors.OptionalBindingPattern);
  2942. }
  2943. if (this.isThisParam(param)) {
  2944. this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional);
  2945. }
  2946. param.optional = true;
  2947. }
  2948. if (this.match(types$1.colon)) {
  2949. param.typeAnnotation = this.flowParseTypeAnnotation();
  2950. } else if (this.isThisParam(param)) {
  2951. this.raise(param.start, FlowErrors.ThisParamAnnotationRequired);
  2952. }
  2953. if (this.match(types$1.eq) && this.isThisParam(param)) {
  2954. this.raise(param.start, FlowErrors.ThisParamNoDefault);
  2955. }
  2956. this.resetEndLocation(param);
  2957. return param;
  2958. }
  2959. parseMaybeDefault(startPos, startLoc, left) {
  2960. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2961. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2962. this.raise(node.typeAnnotation.start, FlowErrors.TypeBeforeInitializer);
  2963. }
  2964. return node;
  2965. }
  2966. shouldParseDefaultImport(node) {
  2967. if (!hasTypeImportKind(node)) {
  2968. return super.shouldParseDefaultImport(node);
  2969. }
  2970. return isMaybeDefaultImport(this.state);
  2971. }
  2972. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  2973. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  2974. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  2975. node.specifiers.push(this.finishNode(specifier, type));
  2976. }
  2977. maybeParseDefaultImportSpecifier(node) {
  2978. node.importKind = "value";
  2979. let kind = null;
  2980. if (this.match(types$1._typeof)) {
  2981. kind = "typeof";
  2982. } else if (this.isContextual("type")) {
  2983. kind = "type";
  2984. }
  2985. if (kind) {
  2986. const lh = this.lookahead();
  2987. if (kind === "type" && lh.type === types$1.star) {
  2988. this.unexpected(lh.start);
  2989. }
  2990. if (isMaybeDefaultImport(lh) || lh.type === types$1.braceL || lh.type === types$1.star) {
  2991. this.next();
  2992. node.importKind = kind;
  2993. }
  2994. }
  2995. return super.maybeParseDefaultImportSpecifier(node);
  2996. }
  2997. parseImportSpecifier(node) {
  2998. const specifier = this.startNode();
  2999. const firstIdentIsString = this.match(types$1.string);
  3000. const firstIdent = this.parseModuleExportName();
  3001. let specifierTypeKind = null;
  3002. if (firstIdent.type === "Identifier") {
  3003. if (firstIdent.name === "type") {
  3004. specifierTypeKind = "type";
  3005. } else if (firstIdent.name === "typeof") {
  3006. specifierTypeKind = "typeof";
  3007. }
  3008. }
  3009. let isBinding = false;
  3010. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  3011. const as_ident = this.parseIdentifier(true);
  3012. if (specifierTypeKind !== null && !this.match(types$1.name) && !this.state.type.keyword) {
  3013. specifier.imported = as_ident;
  3014. specifier.importKind = specifierTypeKind;
  3015. specifier.local = as_ident.__clone();
  3016. } else {
  3017. specifier.imported = firstIdent;
  3018. specifier.importKind = null;
  3019. specifier.local = this.parseIdentifier();
  3020. }
  3021. } else if (specifierTypeKind !== null && (this.match(types$1.name) || this.state.type.keyword)) {
  3022. specifier.imported = this.parseIdentifier(true);
  3023. specifier.importKind = specifierTypeKind;
  3024. if (this.eatContextual("as")) {
  3025. specifier.local = this.parseIdentifier();
  3026. } else {
  3027. isBinding = true;
  3028. specifier.local = specifier.imported.__clone();
  3029. }
  3030. } else {
  3031. if (firstIdentIsString) {
  3032. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, firstIdent.value);
  3033. }
  3034. isBinding = true;
  3035. specifier.imported = firstIdent;
  3036. specifier.importKind = null;
  3037. specifier.local = specifier.imported.__clone();
  3038. }
  3039. const nodeIsTypeImport = hasTypeImportKind(node);
  3040. const specifierIsTypeImport = hasTypeImportKind(specifier);
  3041. if (nodeIsTypeImport && specifierIsTypeImport) {
  3042. this.raise(specifier.start, FlowErrors.ImportTypeShorthandOnlyInPureImport);
  3043. }
  3044. if (nodeIsTypeImport || specifierIsTypeImport) {
  3045. this.checkReservedType(specifier.local.name, specifier.local.start, true);
  3046. }
  3047. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  3048. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  3049. }
  3050. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  3051. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  3052. }
  3053. parseBindingAtom() {
  3054. switch (this.state.type) {
  3055. case types$1._this:
  3056. return this.parseIdentifier(true);
  3057. default:
  3058. return super.parseBindingAtom();
  3059. }
  3060. }
  3061. parseFunctionParams(node, allowModifiers) {
  3062. const kind = node.kind;
  3063. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  3064. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3065. }
  3066. super.parseFunctionParams(node, allowModifiers);
  3067. }
  3068. parseVarId(decl, kind) {
  3069. super.parseVarId(decl, kind);
  3070. if (this.match(types$1.colon)) {
  3071. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  3072. this.resetEndLocation(decl.id);
  3073. }
  3074. }
  3075. parseAsyncArrowFromCallExpression(node, call) {
  3076. if (this.match(types$1.colon)) {
  3077. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3078. this.state.noAnonFunctionType = true;
  3079. node.returnType = this.flowParseTypeAnnotation();
  3080. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3081. }
  3082. return super.parseAsyncArrowFromCallExpression(node, call);
  3083. }
  3084. shouldParseAsyncArrow() {
  3085. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  3086. }
  3087. parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  3088. var _jsx;
  3089. let state = null;
  3090. let jsx;
  3091. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  3092. state = this.state.clone();
  3093. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos), state);
  3094. if (!jsx.error) return jsx.node;
  3095. const {
  3096. context
  3097. } = this.state;
  3098. if (context[context.length - 1] === types.j_oTag) {
  3099. context.length -= 2;
  3100. } else if (context[context.length - 1] === types.j_expr) {
  3101. context.length -= 1;
  3102. }
  3103. }
  3104. if ((_jsx = jsx) != null && _jsx.error || this.isRelational("<")) {
  3105. var _jsx2, _jsx3;
  3106. state = state || this.state.clone();
  3107. let typeParameters;
  3108. const arrow = this.tryParse(abort => {
  3109. var _arrowExpression$extr;
  3110. typeParameters = this.flowParseTypeParameterDeclaration();
  3111. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  3112. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos);
  3113. this.resetStartLocationFromNode(result, typeParameters);
  3114. return result;
  3115. });
  3116. if (arrowExpression.type !== "ArrowFunctionExpression" && (_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) {
  3117. abort();
  3118. }
  3119. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  3120. expr.typeParameters = typeParameters;
  3121. this.resetStartLocationFromNode(expr, typeParameters);
  3122. return arrowExpression;
  3123. }, state);
  3124. let arrowExpression = null;
  3125. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  3126. if (!arrow.error && !arrow.aborted) {
  3127. if (arrow.node.async) {
  3128. this.raise(typeParameters.start, FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction);
  3129. }
  3130. return arrow.node;
  3131. }
  3132. arrowExpression = arrow.node;
  3133. }
  3134. if ((_jsx2 = jsx) != null && _jsx2.node) {
  3135. this.state = jsx.failState;
  3136. return jsx.node;
  3137. }
  3138. if (arrowExpression) {
  3139. this.state = arrow.failState;
  3140. return arrowExpression;
  3141. }
  3142. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  3143. if (arrow.thrown) throw arrow.error;
  3144. throw this.raise(typeParameters.start, FlowErrors.UnexpectedTokenAfterTypeParameter);
  3145. }
  3146. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos);
  3147. }
  3148. parseArrow(node) {
  3149. if (this.match(types$1.colon)) {
  3150. const result = this.tryParse(() => {
  3151. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3152. this.state.noAnonFunctionType = true;
  3153. const typeNode = this.startNode();
  3154. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3155. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3156. if (this.canInsertSemicolon()) this.unexpected();
  3157. if (!this.match(types$1.arrow)) this.unexpected();
  3158. return typeNode;
  3159. });
  3160. if (result.thrown) return null;
  3161. if (result.error) this.state = result.failState;
  3162. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  3163. }
  3164. return super.parseArrow(node);
  3165. }
  3166. shouldParseArrow() {
  3167. return this.match(types$1.colon) || super.shouldParseArrow();
  3168. }
  3169. setArrowFunctionParameters(node, params) {
  3170. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3171. node.params = params;
  3172. } else {
  3173. super.setArrowFunctionParameters(node, params);
  3174. }
  3175. }
  3176. checkParams(node, allowDuplicates, isArrowFunction) {
  3177. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  3178. return;
  3179. }
  3180. for (let i = 0; i < node.params.length; i++) {
  3181. if (this.isThisParam(node.params[i]) && i > 0) {
  3182. this.raise(node.params[i].start, FlowErrors.ThisParamMustBeFirst);
  3183. }
  3184. }
  3185. return super.checkParams(...arguments);
  3186. }
  3187. parseParenAndDistinguishExpression(canBeArrow) {
  3188. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  3189. }
  3190. parseSubscripts(base, startPos, startLoc, noCalls) {
  3191. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  3192. this.next();
  3193. const node = this.startNodeAt(startPos, startLoc);
  3194. node.callee = base;
  3195. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3196. base = this.finishNode(node, "CallExpression");
  3197. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  3198. const state = this.state.clone();
  3199. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  3200. if (!arrow.error && !arrow.aborted) return arrow.node;
  3201. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  3202. if (result.node && !result.error) return result.node;
  3203. if (arrow.node) {
  3204. this.state = arrow.failState;
  3205. return arrow.node;
  3206. }
  3207. if (result.node) {
  3208. this.state = result.failState;
  3209. return result.node;
  3210. }
  3211. throw arrow.error || result.error;
  3212. }
  3213. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  3214. }
  3215. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  3216. if (this.match(types$1.questionDot) && this.isLookaheadToken_lt()) {
  3217. subscriptState.optionalChainMember = true;
  3218. if (noCalls) {
  3219. subscriptState.stop = true;
  3220. return base;
  3221. }
  3222. this.next();
  3223. const node = this.startNodeAt(startPos, startLoc);
  3224. node.callee = base;
  3225. node.typeArguments = this.flowParseTypeParameterInstantiation();
  3226. this.expect(types$1.parenL);
  3227. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3228. node.optional = true;
  3229. return this.finishCallExpression(node, true);
  3230. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  3231. const node = this.startNodeAt(startPos, startLoc);
  3232. node.callee = base;
  3233. const result = this.tryParse(() => {
  3234. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  3235. this.expect(types$1.parenL);
  3236. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  3237. if (subscriptState.optionalChainMember) node.optional = false;
  3238. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  3239. });
  3240. if (result.node) {
  3241. if (result.error) this.state = result.failState;
  3242. return result.node;
  3243. }
  3244. }
  3245. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  3246. }
  3247. parseNewArguments(node) {
  3248. let targs = null;
  3249. if (this.shouldParseTypes() && this.isRelational("<")) {
  3250. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  3251. }
  3252. node.typeArguments = targs;
  3253. super.parseNewArguments(node);
  3254. }
  3255. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  3256. const node = this.startNodeAt(startPos, startLoc);
  3257. this.parseFunctionParams(node);
  3258. if (!this.parseArrow(node)) return;
  3259. return this.parseArrowExpression(node, undefined, true);
  3260. }
  3261. readToken_mult_modulo(code) {
  3262. const next = this.input.charCodeAt(this.state.pos + 1);
  3263. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  3264. this.state.hasFlowComment = false;
  3265. this.state.pos += 2;
  3266. this.nextToken();
  3267. return;
  3268. }
  3269. super.readToken_mult_modulo(code);
  3270. }
  3271. readToken_pipe_amp(code) {
  3272. const next = this.input.charCodeAt(this.state.pos + 1);
  3273. if (code === 124 && next === 125) {
  3274. this.finishOp(types$1.braceBarR, 2);
  3275. return;
  3276. }
  3277. super.readToken_pipe_amp(code);
  3278. }
  3279. parseTopLevel(file, program) {
  3280. const fileNode = super.parseTopLevel(file, program);
  3281. if (this.state.hasFlowComment) {
  3282. this.raise(this.state.pos, FlowErrors.UnterminatedFlowComment);
  3283. }
  3284. return fileNode;
  3285. }
  3286. skipBlockComment() {
  3287. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  3288. if (this.state.hasFlowComment) {
  3289. this.unexpected(null, FlowErrors.NestedFlowComment);
  3290. }
  3291. this.hasFlowCommentCompletion();
  3292. this.state.pos += this.skipFlowComment();
  3293. this.state.hasFlowComment = true;
  3294. return;
  3295. }
  3296. if (this.state.hasFlowComment) {
  3297. const end = this.input.indexOf("*-/", this.state.pos += 2);
  3298. if (end === -1) {
  3299. throw this.raise(this.state.pos - 2, ErrorMessages.UnterminatedComment);
  3300. }
  3301. this.state.pos = end + 3;
  3302. return;
  3303. }
  3304. super.skipBlockComment();
  3305. }
  3306. skipFlowComment() {
  3307. const {
  3308. pos
  3309. } = this.state;
  3310. let shiftToFirstNonWhiteSpace = 2;
  3311. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  3312. shiftToFirstNonWhiteSpace++;
  3313. }
  3314. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  3315. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  3316. if (ch2 === 58 && ch3 === 58) {
  3317. return shiftToFirstNonWhiteSpace + 2;
  3318. }
  3319. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  3320. return shiftToFirstNonWhiteSpace + 12;
  3321. }
  3322. if (ch2 === 58 && ch3 !== 58) {
  3323. return shiftToFirstNonWhiteSpace;
  3324. }
  3325. return false;
  3326. }
  3327. hasFlowCommentCompletion() {
  3328. const end = this.input.indexOf("*/", this.state.pos);
  3329. if (end === -1) {
  3330. throw this.raise(this.state.pos, ErrorMessages.UnterminatedComment);
  3331. }
  3332. }
  3333. flowEnumErrorBooleanMemberNotInitialized(pos, {
  3334. enumName,
  3335. memberName
  3336. }) {
  3337. this.raise(pos, FlowErrors.EnumBooleanMemberNotInitialized, memberName, enumName);
  3338. }
  3339. flowEnumErrorInvalidMemberName(pos, {
  3340. enumName,
  3341. memberName
  3342. }) {
  3343. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  3344. this.raise(pos, FlowErrors.EnumInvalidMemberName, memberName, suggestion, enumName);
  3345. }
  3346. flowEnumErrorDuplicateMemberName(pos, {
  3347. enumName,
  3348. memberName
  3349. }) {
  3350. this.raise(pos, FlowErrors.EnumDuplicateMemberName, memberName, enumName);
  3351. }
  3352. flowEnumErrorInconsistentMemberValues(pos, {
  3353. enumName
  3354. }) {
  3355. this.raise(pos, FlowErrors.EnumInconsistentMemberValues, enumName);
  3356. }
  3357. flowEnumErrorInvalidExplicitType(pos, {
  3358. enumName,
  3359. suppliedType
  3360. }) {
  3361. return this.raise(pos, suppliedType === null ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied : FlowErrors.EnumInvalidExplicitType, enumName, suppliedType);
  3362. }
  3363. flowEnumErrorInvalidMemberInitializer(pos, {
  3364. enumName,
  3365. explicitType,
  3366. memberName
  3367. }) {
  3368. let message = null;
  3369. switch (explicitType) {
  3370. case "boolean":
  3371. case "number":
  3372. case "string":
  3373. message = FlowErrors.EnumInvalidMemberInitializerPrimaryType;
  3374. break;
  3375. case "symbol":
  3376. message = FlowErrors.EnumInvalidMemberInitializerSymbolType;
  3377. break;
  3378. default:
  3379. message = FlowErrors.EnumInvalidMemberInitializerUnknownType;
  3380. }
  3381. return this.raise(pos, message, enumName, memberName, explicitType);
  3382. }
  3383. flowEnumErrorNumberMemberNotInitialized(pos, {
  3384. enumName,
  3385. memberName
  3386. }) {
  3387. this.raise(pos, FlowErrors.EnumNumberMemberNotInitialized, enumName, memberName);
  3388. }
  3389. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  3390. enumName
  3391. }) {
  3392. this.raise(pos, FlowErrors.EnumStringMemberInconsistentlyInitailized, enumName);
  3393. }
  3394. flowEnumMemberInit() {
  3395. const startPos = this.state.start;
  3396. const endOfInit = () => this.match(types$1.comma) || this.match(types$1.braceR);
  3397. switch (this.state.type) {
  3398. case types$1.num:
  3399. {
  3400. const literal = this.parseNumericLiteral(this.state.value);
  3401. if (endOfInit()) {
  3402. return {
  3403. type: "number",
  3404. pos: literal.start,
  3405. value: literal
  3406. };
  3407. }
  3408. return {
  3409. type: "invalid",
  3410. pos: startPos
  3411. };
  3412. }
  3413. case types$1.string:
  3414. {
  3415. const literal = this.parseStringLiteral(this.state.value);
  3416. if (endOfInit()) {
  3417. return {
  3418. type: "string",
  3419. pos: literal.start,
  3420. value: literal
  3421. };
  3422. }
  3423. return {
  3424. type: "invalid",
  3425. pos: startPos
  3426. };
  3427. }
  3428. case types$1._true:
  3429. case types$1._false:
  3430. {
  3431. const literal = this.parseBooleanLiteral(this.match(types$1._true));
  3432. if (endOfInit()) {
  3433. return {
  3434. type: "boolean",
  3435. pos: literal.start,
  3436. value: literal
  3437. };
  3438. }
  3439. return {
  3440. type: "invalid",
  3441. pos: startPos
  3442. };
  3443. }
  3444. default:
  3445. return {
  3446. type: "invalid",
  3447. pos: startPos
  3448. };
  3449. }
  3450. }
  3451. flowEnumMemberRaw() {
  3452. const pos = this.state.start;
  3453. const id = this.parseIdentifier(true);
  3454. const init = this.eat(types$1.eq) ? this.flowEnumMemberInit() : {
  3455. type: "none",
  3456. pos
  3457. };
  3458. return {
  3459. id,
  3460. init
  3461. };
  3462. }
  3463. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  3464. const {
  3465. explicitType
  3466. } = context;
  3467. if (explicitType === null) {
  3468. return;
  3469. }
  3470. if (explicitType !== expectedType) {
  3471. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  3472. }
  3473. }
  3474. flowEnumMembers({
  3475. enumName,
  3476. explicitType
  3477. }) {
  3478. const seenNames = new Set();
  3479. const members = {
  3480. booleanMembers: [],
  3481. numberMembers: [],
  3482. stringMembers: [],
  3483. defaultedMembers: []
  3484. };
  3485. let hasUnknownMembers = false;
  3486. while (!this.match(types$1.braceR)) {
  3487. if (this.eat(types$1.ellipsis)) {
  3488. hasUnknownMembers = true;
  3489. break;
  3490. }
  3491. const memberNode = this.startNode();
  3492. const {
  3493. id,
  3494. init
  3495. } = this.flowEnumMemberRaw();
  3496. const memberName = id.name;
  3497. if (memberName === "") {
  3498. continue;
  3499. }
  3500. if (/^[a-z]/.test(memberName)) {
  3501. this.flowEnumErrorInvalidMemberName(id.start, {
  3502. enumName,
  3503. memberName
  3504. });
  3505. }
  3506. if (seenNames.has(memberName)) {
  3507. this.flowEnumErrorDuplicateMemberName(id.start, {
  3508. enumName,
  3509. memberName
  3510. });
  3511. }
  3512. seenNames.add(memberName);
  3513. const context = {
  3514. enumName,
  3515. explicitType,
  3516. memberName
  3517. };
  3518. memberNode.id = id;
  3519. switch (init.type) {
  3520. case "boolean":
  3521. {
  3522. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  3523. memberNode.init = init.value;
  3524. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  3525. break;
  3526. }
  3527. case "number":
  3528. {
  3529. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  3530. memberNode.init = init.value;
  3531. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  3532. break;
  3533. }
  3534. case "string":
  3535. {
  3536. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  3537. memberNode.init = init.value;
  3538. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  3539. break;
  3540. }
  3541. case "invalid":
  3542. {
  3543. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  3544. }
  3545. case "none":
  3546. {
  3547. switch (explicitType) {
  3548. case "boolean":
  3549. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  3550. break;
  3551. case "number":
  3552. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  3553. break;
  3554. default:
  3555. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  3556. }
  3557. }
  3558. }
  3559. if (!this.match(types$1.braceR)) {
  3560. this.expect(types$1.comma);
  3561. }
  3562. }
  3563. return {
  3564. members,
  3565. hasUnknownMembers
  3566. };
  3567. }
  3568. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  3569. enumName
  3570. }) {
  3571. if (initializedMembers.length === 0) {
  3572. return defaultedMembers;
  3573. } else if (defaultedMembers.length === 0) {
  3574. return initializedMembers;
  3575. } else if (defaultedMembers.length > initializedMembers.length) {
  3576. for (const member of initializedMembers) {
  3577. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3578. enumName
  3579. });
  3580. }
  3581. return defaultedMembers;
  3582. } else {
  3583. for (const member of defaultedMembers) {
  3584. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  3585. enumName
  3586. });
  3587. }
  3588. return initializedMembers;
  3589. }
  3590. }
  3591. flowEnumParseExplicitType({
  3592. enumName
  3593. }) {
  3594. if (this.eatContextual("of")) {
  3595. if (!this.match(types$1.name)) {
  3596. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3597. enumName,
  3598. suppliedType: null
  3599. });
  3600. }
  3601. const {
  3602. value
  3603. } = this.state;
  3604. this.next();
  3605. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  3606. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  3607. enumName,
  3608. suppliedType: value
  3609. });
  3610. }
  3611. return value;
  3612. }
  3613. return null;
  3614. }
  3615. flowEnumBody(node, {
  3616. enumName,
  3617. nameLoc
  3618. }) {
  3619. const explicitType = this.flowEnumParseExplicitType({
  3620. enumName
  3621. });
  3622. this.expect(types$1.braceL);
  3623. const {
  3624. members,
  3625. hasUnknownMembers
  3626. } = this.flowEnumMembers({
  3627. enumName,
  3628. explicitType
  3629. });
  3630. node.hasUnknownMembers = hasUnknownMembers;
  3631. switch (explicitType) {
  3632. case "boolean":
  3633. node.explicitType = true;
  3634. node.members = members.booleanMembers;
  3635. this.expect(types$1.braceR);
  3636. return this.finishNode(node, "EnumBooleanBody");
  3637. case "number":
  3638. node.explicitType = true;
  3639. node.members = members.numberMembers;
  3640. this.expect(types$1.braceR);
  3641. return this.finishNode(node, "EnumNumberBody");
  3642. case "string":
  3643. node.explicitType = true;
  3644. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3645. enumName
  3646. });
  3647. this.expect(types$1.braceR);
  3648. return this.finishNode(node, "EnumStringBody");
  3649. case "symbol":
  3650. node.members = members.defaultedMembers;
  3651. this.expect(types$1.braceR);
  3652. return this.finishNode(node, "EnumSymbolBody");
  3653. default:
  3654. {
  3655. const empty = () => {
  3656. node.members = [];
  3657. this.expect(types$1.braceR);
  3658. return this.finishNode(node, "EnumStringBody");
  3659. };
  3660. node.explicitType = false;
  3661. const boolsLen = members.booleanMembers.length;
  3662. const numsLen = members.numberMembers.length;
  3663. const strsLen = members.stringMembers.length;
  3664. const defaultedLen = members.defaultedMembers.length;
  3665. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  3666. return empty();
  3667. } else if (!boolsLen && !numsLen) {
  3668. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  3669. enumName
  3670. });
  3671. this.expect(types$1.braceR);
  3672. return this.finishNode(node, "EnumStringBody");
  3673. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  3674. for (const member of members.defaultedMembers) {
  3675. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  3676. enumName,
  3677. memberName: member.id.name
  3678. });
  3679. }
  3680. node.members = members.booleanMembers;
  3681. this.expect(types$1.braceR);
  3682. return this.finishNode(node, "EnumBooleanBody");
  3683. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  3684. for (const member of members.defaultedMembers) {
  3685. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  3686. enumName,
  3687. memberName: member.id.name
  3688. });
  3689. }
  3690. node.members = members.numberMembers;
  3691. this.expect(types$1.braceR);
  3692. return this.finishNode(node, "EnumNumberBody");
  3693. } else {
  3694. this.flowEnumErrorInconsistentMemberValues(nameLoc, {
  3695. enumName
  3696. });
  3697. return empty();
  3698. }
  3699. }
  3700. }
  3701. }
  3702. flowParseEnumDeclaration(node) {
  3703. const id = this.parseIdentifier();
  3704. node.id = id;
  3705. node.body = this.flowEnumBody(this.startNode(), {
  3706. enumName: id.name,
  3707. nameLoc: id.start
  3708. });
  3709. return this.finishNode(node, "EnumDeclaration");
  3710. }
  3711. isLookaheadToken_lt() {
  3712. const next = this.nextTokenStart();
  3713. if (this.input.charCodeAt(next) === 60) {
  3714. const afterNext = this.input.charCodeAt(next + 1);
  3715. return afterNext !== 60 && afterNext !== 61;
  3716. }
  3717. return false;
  3718. }
  3719. maybeUnwrapTypeCastExpression(node) {
  3720. return node.type === "TypeCastExpression" ? node.expression : node;
  3721. }
  3722. });
  3723. const entities = {
  3724. quot: "\u0022",
  3725. amp: "&",
  3726. apos: "\u0027",
  3727. lt: "<",
  3728. gt: ">",
  3729. nbsp: "\u00A0",
  3730. iexcl: "\u00A1",
  3731. cent: "\u00A2",
  3732. pound: "\u00A3",
  3733. curren: "\u00A4",
  3734. yen: "\u00A5",
  3735. brvbar: "\u00A6",
  3736. sect: "\u00A7",
  3737. uml: "\u00A8",
  3738. copy: "\u00A9",
  3739. ordf: "\u00AA",
  3740. laquo: "\u00AB",
  3741. not: "\u00AC",
  3742. shy: "\u00AD",
  3743. reg: "\u00AE",
  3744. macr: "\u00AF",
  3745. deg: "\u00B0",
  3746. plusmn: "\u00B1",
  3747. sup2: "\u00B2",
  3748. sup3: "\u00B3",
  3749. acute: "\u00B4",
  3750. micro: "\u00B5",
  3751. para: "\u00B6",
  3752. middot: "\u00B7",
  3753. cedil: "\u00B8",
  3754. sup1: "\u00B9",
  3755. ordm: "\u00BA",
  3756. raquo: "\u00BB",
  3757. frac14: "\u00BC",
  3758. frac12: "\u00BD",
  3759. frac34: "\u00BE",
  3760. iquest: "\u00BF",
  3761. Agrave: "\u00C0",
  3762. Aacute: "\u00C1",
  3763. Acirc: "\u00C2",
  3764. Atilde: "\u00C3",
  3765. Auml: "\u00C4",
  3766. Aring: "\u00C5",
  3767. AElig: "\u00C6",
  3768. Ccedil: "\u00C7",
  3769. Egrave: "\u00C8",
  3770. Eacute: "\u00C9",
  3771. Ecirc: "\u00CA",
  3772. Euml: "\u00CB",
  3773. Igrave: "\u00CC",
  3774. Iacute: "\u00CD",
  3775. Icirc: "\u00CE",
  3776. Iuml: "\u00CF",
  3777. ETH: "\u00D0",
  3778. Ntilde: "\u00D1",
  3779. Ograve: "\u00D2",
  3780. Oacute: "\u00D3",
  3781. Ocirc: "\u00D4",
  3782. Otilde: "\u00D5",
  3783. Ouml: "\u00D6",
  3784. times: "\u00D7",
  3785. Oslash: "\u00D8",
  3786. Ugrave: "\u00D9",
  3787. Uacute: "\u00DA",
  3788. Ucirc: "\u00DB",
  3789. Uuml: "\u00DC",
  3790. Yacute: "\u00DD",
  3791. THORN: "\u00DE",
  3792. szlig: "\u00DF",
  3793. agrave: "\u00E0",
  3794. aacute: "\u00E1",
  3795. acirc: "\u00E2",
  3796. atilde: "\u00E3",
  3797. auml: "\u00E4",
  3798. aring: "\u00E5",
  3799. aelig: "\u00E6",
  3800. ccedil: "\u00E7",
  3801. egrave: "\u00E8",
  3802. eacute: "\u00E9",
  3803. ecirc: "\u00EA",
  3804. euml: "\u00EB",
  3805. igrave: "\u00EC",
  3806. iacute: "\u00ED",
  3807. icirc: "\u00EE",
  3808. iuml: "\u00EF",
  3809. eth: "\u00F0",
  3810. ntilde: "\u00F1",
  3811. ograve: "\u00F2",
  3812. oacute: "\u00F3",
  3813. ocirc: "\u00F4",
  3814. otilde: "\u00F5",
  3815. ouml: "\u00F6",
  3816. divide: "\u00F7",
  3817. oslash: "\u00F8",
  3818. ugrave: "\u00F9",
  3819. uacute: "\u00FA",
  3820. ucirc: "\u00FB",
  3821. uuml: "\u00FC",
  3822. yacute: "\u00FD",
  3823. thorn: "\u00FE",
  3824. yuml: "\u00FF",
  3825. OElig: "\u0152",
  3826. oelig: "\u0153",
  3827. Scaron: "\u0160",
  3828. scaron: "\u0161",
  3829. Yuml: "\u0178",
  3830. fnof: "\u0192",
  3831. circ: "\u02C6",
  3832. tilde: "\u02DC",
  3833. Alpha: "\u0391",
  3834. Beta: "\u0392",
  3835. Gamma: "\u0393",
  3836. Delta: "\u0394",
  3837. Epsilon: "\u0395",
  3838. Zeta: "\u0396",
  3839. Eta: "\u0397",
  3840. Theta: "\u0398",
  3841. Iota: "\u0399",
  3842. Kappa: "\u039A",
  3843. Lambda: "\u039B",
  3844. Mu: "\u039C",
  3845. Nu: "\u039D",
  3846. Xi: "\u039E",
  3847. Omicron: "\u039F",
  3848. Pi: "\u03A0",
  3849. Rho: "\u03A1",
  3850. Sigma: "\u03A3",
  3851. Tau: "\u03A4",
  3852. Upsilon: "\u03A5",
  3853. Phi: "\u03A6",
  3854. Chi: "\u03A7",
  3855. Psi: "\u03A8",
  3856. Omega: "\u03A9",
  3857. alpha: "\u03B1",
  3858. beta: "\u03B2",
  3859. gamma: "\u03B3",
  3860. delta: "\u03B4",
  3861. epsilon: "\u03B5",
  3862. zeta: "\u03B6",
  3863. eta: "\u03B7",
  3864. theta: "\u03B8",
  3865. iota: "\u03B9",
  3866. kappa: "\u03BA",
  3867. lambda: "\u03BB",
  3868. mu: "\u03BC",
  3869. nu: "\u03BD",
  3870. xi: "\u03BE",
  3871. omicron: "\u03BF",
  3872. pi: "\u03C0",
  3873. rho: "\u03C1",
  3874. sigmaf: "\u03C2",
  3875. sigma: "\u03C3",
  3876. tau: "\u03C4",
  3877. upsilon: "\u03C5",
  3878. phi: "\u03C6",
  3879. chi: "\u03C7",
  3880. psi: "\u03C8",
  3881. omega: "\u03C9",
  3882. thetasym: "\u03D1",
  3883. upsih: "\u03D2",
  3884. piv: "\u03D6",
  3885. ensp: "\u2002",
  3886. emsp: "\u2003",
  3887. thinsp: "\u2009",
  3888. zwnj: "\u200C",
  3889. zwj: "\u200D",
  3890. lrm: "\u200E",
  3891. rlm: "\u200F",
  3892. ndash: "\u2013",
  3893. mdash: "\u2014",
  3894. lsquo: "\u2018",
  3895. rsquo: "\u2019",
  3896. sbquo: "\u201A",
  3897. ldquo: "\u201C",
  3898. rdquo: "\u201D",
  3899. bdquo: "\u201E",
  3900. dagger: "\u2020",
  3901. Dagger: "\u2021",
  3902. bull: "\u2022",
  3903. hellip: "\u2026",
  3904. permil: "\u2030",
  3905. prime: "\u2032",
  3906. Prime: "\u2033",
  3907. lsaquo: "\u2039",
  3908. rsaquo: "\u203A",
  3909. oline: "\u203E",
  3910. frasl: "\u2044",
  3911. euro: "\u20AC",
  3912. image: "\u2111",
  3913. weierp: "\u2118",
  3914. real: "\u211C",
  3915. trade: "\u2122",
  3916. alefsym: "\u2135",
  3917. larr: "\u2190",
  3918. uarr: "\u2191",
  3919. rarr: "\u2192",
  3920. darr: "\u2193",
  3921. harr: "\u2194",
  3922. crarr: "\u21B5",
  3923. lArr: "\u21D0",
  3924. uArr: "\u21D1",
  3925. rArr: "\u21D2",
  3926. dArr: "\u21D3",
  3927. hArr: "\u21D4",
  3928. forall: "\u2200",
  3929. part: "\u2202",
  3930. exist: "\u2203",
  3931. empty: "\u2205",
  3932. nabla: "\u2207",
  3933. isin: "\u2208",
  3934. notin: "\u2209",
  3935. ni: "\u220B",
  3936. prod: "\u220F",
  3937. sum: "\u2211",
  3938. minus: "\u2212",
  3939. lowast: "\u2217",
  3940. radic: "\u221A",
  3941. prop: "\u221D",
  3942. infin: "\u221E",
  3943. ang: "\u2220",
  3944. and: "\u2227",
  3945. or: "\u2228",
  3946. cap: "\u2229",
  3947. cup: "\u222A",
  3948. int: "\u222B",
  3949. there4: "\u2234",
  3950. sim: "\u223C",
  3951. cong: "\u2245",
  3952. asymp: "\u2248",
  3953. ne: "\u2260",
  3954. equiv: "\u2261",
  3955. le: "\u2264",
  3956. ge: "\u2265",
  3957. sub: "\u2282",
  3958. sup: "\u2283",
  3959. nsub: "\u2284",
  3960. sube: "\u2286",
  3961. supe: "\u2287",
  3962. oplus: "\u2295",
  3963. otimes: "\u2297",
  3964. perp: "\u22A5",
  3965. sdot: "\u22C5",
  3966. lceil: "\u2308",
  3967. rceil: "\u2309",
  3968. lfloor: "\u230A",
  3969. rfloor: "\u230B",
  3970. lang: "\u2329",
  3971. rang: "\u232A",
  3972. loz: "\u25CA",
  3973. spades: "\u2660",
  3974. clubs: "\u2663",
  3975. hearts: "\u2665",
  3976. diams: "\u2666"
  3977. };
  3978. class State {
  3979. constructor() {
  3980. this.strict = void 0;
  3981. this.curLine = void 0;
  3982. this.startLoc = void 0;
  3983. this.endLoc = void 0;
  3984. this.errors = [];
  3985. this.potentialArrowAt = -1;
  3986. this.noArrowAt = [];
  3987. this.noArrowParamsConversionAt = [];
  3988. this.maybeInArrowParameters = false;
  3989. this.inPipeline = false;
  3990. this.inType = false;
  3991. this.noAnonFunctionType = false;
  3992. this.inPropertyName = false;
  3993. this.hasFlowComment = false;
  3994. this.isAmbientContext = false;
  3995. this.inAbstractClass = false;
  3996. this.topicContext = {
  3997. maxNumOfResolvableTopics: 0,
  3998. maxTopicIndex: null
  3999. };
  4000. this.soloAwait = false;
  4001. this.inFSharpPipelineDirectBody = false;
  4002. this.labels = [];
  4003. this.decoratorStack = [[]];
  4004. this.comments = [];
  4005. this.trailingComments = [];
  4006. this.leadingComments = [];
  4007. this.commentStack = [];
  4008. this.commentPreviousNode = null;
  4009. this.pos = 0;
  4010. this.lineStart = 0;
  4011. this.type = types$1.eof;
  4012. this.value = null;
  4013. this.start = 0;
  4014. this.end = 0;
  4015. this.lastTokEndLoc = null;
  4016. this.lastTokStartLoc = null;
  4017. this.lastTokStart = 0;
  4018. this.lastTokEnd = 0;
  4019. this.context = [types.brace];
  4020. this.exprAllowed = true;
  4021. this.containsEsc = false;
  4022. this.strictErrors = new Map();
  4023. this.tokensLength = 0;
  4024. }
  4025. init(options) {
  4026. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  4027. this.curLine = options.startLine;
  4028. this.startLoc = this.endLoc = this.curPosition();
  4029. }
  4030. curPosition() {
  4031. return new Position(this.curLine, this.pos - this.lineStart);
  4032. }
  4033. clone(skipArrays) {
  4034. const state = new State();
  4035. const keys = Object.keys(this);
  4036. for (let i = 0, length = keys.length; i < length; i++) {
  4037. const key = keys[i];
  4038. let val = this[key];
  4039. if (!skipArrays && Array.isArray(val)) {
  4040. val = val.slice();
  4041. }
  4042. state[key] = val;
  4043. }
  4044. return state;
  4045. }
  4046. }
  4047. const HEX_NUMBER = /^[\da-fA-F]+$/;
  4048. const DECIMAL_NUMBER = /^\d+$/;
  4049. const JsxErrors = makeErrorTemplates({
  4050. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  4051. MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>.",
  4052. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  4053. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  4054. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  4055. UnterminatedJsxContent: "Unterminated JSX contents.",
  4056. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  4057. }, ErrorCodes.SyntaxError);
  4058. types.j_oTag = new TokContext("<tag");
  4059. types.j_cTag = new TokContext("</tag");
  4060. types.j_expr = new TokContext("<tag>...</tag>", true);
  4061. types$1.jsxName = new TokenType("jsxName");
  4062. types$1.jsxText = new TokenType("jsxText", {
  4063. beforeExpr: true
  4064. });
  4065. types$1.jsxTagStart = new TokenType("jsxTagStart", {
  4066. startsExpr: true
  4067. });
  4068. types$1.jsxTagEnd = new TokenType("jsxTagEnd");
  4069. types$1.jsxTagStart.updateContext = context => {
  4070. context.push(types.j_expr);
  4071. context.push(types.j_oTag);
  4072. };
  4073. function isFragment(object) {
  4074. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  4075. }
  4076. function getQualifiedJSXName(object) {
  4077. if (object.type === "JSXIdentifier") {
  4078. return object.name;
  4079. }
  4080. if (object.type === "JSXNamespacedName") {
  4081. return object.namespace.name + ":" + object.name.name;
  4082. }
  4083. if (object.type === "JSXMemberExpression") {
  4084. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  4085. }
  4086. throw new Error("Node had unexpected type: " + object.type);
  4087. }
  4088. var jsx = (superClass => class extends superClass {
  4089. jsxReadToken() {
  4090. let out = "";
  4091. let chunkStart = this.state.pos;
  4092. for (;;) {
  4093. if (this.state.pos >= this.length) {
  4094. throw this.raise(this.state.start, JsxErrors.UnterminatedJsxContent);
  4095. }
  4096. const ch = this.input.charCodeAt(this.state.pos);
  4097. switch (ch) {
  4098. case 60:
  4099. case 123:
  4100. if (this.state.pos === this.state.start) {
  4101. if (ch === 60 && this.state.exprAllowed) {
  4102. ++this.state.pos;
  4103. return this.finishToken(types$1.jsxTagStart);
  4104. }
  4105. return super.getTokenFromCode(ch);
  4106. }
  4107. out += this.input.slice(chunkStart, this.state.pos);
  4108. return this.finishToken(types$1.jsxText, out);
  4109. case 38:
  4110. out += this.input.slice(chunkStart, this.state.pos);
  4111. out += this.jsxReadEntity();
  4112. chunkStart = this.state.pos;
  4113. break;
  4114. case 62:
  4115. case 125:
  4116. default:
  4117. if (isNewLine(ch)) {
  4118. out += this.input.slice(chunkStart, this.state.pos);
  4119. out += this.jsxReadNewLine(true);
  4120. chunkStart = this.state.pos;
  4121. } else {
  4122. ++this.state.pos;
  4123. }
  4124. }
  4125. }
  4126. }
  4127. jsxReadNewLine(normalizeCRLF) {
  4128. const ch = this.input.charCodeAt(this.state.pos);
  4129. let out;
  4130. ++this.state.pos;
  4131. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  4132. ++this.state.pos;
  4133. out = normalizeCRLF ? "\n" : "\r\n";
  4134. } else {
  4135. out = String.fromCharCode(ch);
  4136. }
  4137. ++this.state.curLine;
  4138. this.state.lineStart = this.state.pos;
  4139. return out;
  4140. }
  4141. jsxReadString(quote) {
  4142. let out = "";
  4143. let chunkStart = ++this.state.pos;
  4144. for (;;) {
  4145. if (this.state.pos >= this.length) {
  4146. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  4147. }
  4148. const ch = this.input.charCodeAt(this.state.pos);
  4149. if (ch === quote) break;
  4150. if (ch === 38) {
  4151. out += this.input.slice(chunkStart, this.state.pos);
  4152. out += this.jsxReadEntity();
  4153. chunkStart = this.state.pos;
  4154. } else if (isNewLine(ch)) {
  4155. out += this.input.slice(chunkStart, this.state.pos);
  4156. out += this.jsxReadNewLine(false);
  4157. chunkStart = this.state.pos;
  4158. } else {
  4159. ++this.state.pos;
  4160. }
  4161. }
  4162. out += this.input.slice(chunkStart, this.state.pos++);
  4163. return this.finishToken(types$1.string, out);
  4164. }
  4165. jsxReadEntity() {
  4166. let str = "";
  4167. let count = 0;
  4168. let entity;
  4169. let ch = this.input[this.state.pos];
  4170. const startPos = ++this.state.pos;
  4171. while (this.state.pos < this.length && count++ < 10) {
  4172. ch = this.input[this.state.pos++];
  4173. if (ch === ";") {
  4174. if (str[0] === "#") {
  4175. if (str[1] === "x") {
  4176. str = str.substr(2);
  4177. if (HEX_NUMBER.test(str)) {
  4178. entity = String.fromCodePoint(parseInt(str, 16));
  4179. }
  4180. } else {
  4181. str = str.substr(1);
  4182. if (DECIMAL_NUMBER.test(str)) {
  4183. entity = String.fromCodePoint(parseInt(str, 10));
  4184. }
  4185. }
  4186. } else {
  4187. entity = entities[str];
  4188. }
  4189. break;
  4190. }
  4191. str += ch;
  4192. }
  4193. if (!entity) {
  4194. this.state.pos = startPos;
  4195. return "&";
  4196. }
  4197. return entity;
  4198. }
  4199. jsxReadWord() {
  4200. let ch;
  4201. const start = this.state.pos;
  4202. do {
  4203. ch = this.input.charCodeAt(++this.state.pos);
  4204. } while (isIdentifierChar(ch) || ch === 45);
  4205. return this.finishToken(types$1.jsxName, this.input.slice(start, this.state.pos));
  4206. }
  4207. jsxParseIdentifier() {
  4208. const node = this.startNode();
  4209. if (this.match(types$1.jsxName)) {
  4210. node.name = this.state.value;
  4211. } else if (this.state.type.keyword) {
  4212. node.name = this.state.type.keyword;
  4213. } else {
  4214. this.unexpected();
  4215. }
  4216. this.next();
  4217. return this.finishNode(node, "JSXIdentifier");
  4218. }
  4219. jsxParseNamespacedName() {
  4220. const startPos = this.state.start;
  4221. const startLoc = this.state.startLoc;
  4222. const name = this.jsxParseIdentifier();
  4223. if (!this.eat(types$1.colon)) return name;
  4224. const node = this.startNodeAt(startPos, startLoc);
  4225. node.namespace = name;
  4226. node.name = this.jsxParseIdentifier();
  4227. return this.finishNode(node, "JSXNamespacedName");
  4228. }
  4229. jsxParseElementName() {
  4230. const startPos = this.state.start;
  4231. const startLoc = this.state.startLoc;
  4232. let node = this.jsxParseNamespacedName();
  4233. if (node.type === "JSXNamespacedName") {
  4234. return node;
  4235. }
  4236. while (this.eat(types$1.dot)) {
  4237. const newNode = this.startNodeAt(startPos, startLoc);
  4238. newNode.object = node;
  4239. newNode.property = this.jsxParseIdentifier();
  4240. node = this.finishNode(newNode, "JSXMemberExpression");
  4241. }
  4242. return node;
  4243. }
  4244. jsxParseAttributeValue() {
  4245. let node;
  4246. switch (this.state.type) {
  4247. case types$1.braceL:
  4248. node = this.startNode();
  4249. this.next();
  4250. node = this.jsxParseExpressionContainer(node);
  4251. if (node.expression.type === "JSXEmptyExpression") {
  4252. this.raise(node.start, JsxErrors.AttributeIsEmpty);
  4253. }
  4254. return node;
  4255. case types$1.jsxTagStart:
  4256. case types$1.string:
  4257. return this.parseExprAtom();
  4258. default:
  4259. throw this.raise(this.state.start, JsxErrors.UnsupportedJsxValue);
  4260. }
  4261. }
  4262. jsxParseEmptyExpression() {
  4263. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  4264. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  4265. }
  4266. jsxParseSpreadChild(node) {
  4267. this.next();
  4268. node.expression = this.parseExpression();
  4269. this.expect(types$1.braceR);
  4270. return this.finishNode(node, "JSXSpreadChild");
  4271. }
  4272. jsxParseExpressionContainer(node) {
  4273. if (this.match(types$1.braceR)) {
  4274. node.expression = this.jsxParseEmptyExpression();
  4275. } else {
  4276. const expression = this.parseExpression();
  4277. node.expression = expression;
  4278. }
  4279. this.expect(types$1.braceR);
  4280. return this.finishNode(node, "JSXExpressionContainer");
  4281. }
  4282. jsxParseAttribute() {
  4283. const node = this.startNode();
  4284. if (this.eat(types$1.braceL)) {
  4285. this.expect(types$1.ellipsis);
  4286. node.argument = this.parseMaybeAssignAllowIn();
  4287. this.expect(types$1.braceR);
  4288. return this.finishNode(node, "JSXSpreadAttribute");
  4289. }
  4290. node.name = this.jsxParseNamespacedName();
  4291. node.value = this.eat(types$1.eq) ? this.jsxParseAttributeValue() : null;
  4292. return this.finishNode(node, "JSXAttribute");
  4293. }
  4294. jsxParseOpeningElementAt(startPos, startLoc) {
  4295. const node = this.startNodeAt(startPos, startLoc);
  4296. if (this.match(types$1.jsxTagEnd)) {
  4297. this.expect(types$1.jsxTagEnd);
  4298. return this.finishNode(node, "JSXOpeningFragment");
  4299. }
  4300. node.name = this.jsxParseElementName();
  4301. return this.jsxParseOpeningElementAfterName(node);
  4302. }
  4303. jsxParseOpeningElementAfterName(node) {
  4304. const attributes = [];
  4305. while (!this.match(types$1.slash) && !this.match(types$1.jsxTagEnd)) {
  4306. attributes.push(this.jsxParseAttribute());
  4307. }
  4308. node.attributes = attributes;
  4309. node.selfClosing = this.eat(types$1.slash);
  4310. this.expect(types$1.jsxTagEnd);
  4311. return this.finishNode(node, "JSXOpeningElement");
  4312. }
  4313. jsxParseClosingElementAt(startPos, startLoc) {
  4314. const node = this.startNodeAt(startPos, startLoc);
  4315. if (this.match(types$1.jsxTagEnd)) {
  4316. this.expect(types$1.jsxTagEnd);
  4317. return this.finishNode(node, "JSXClosingFragment");
  4318. }
  4319. node.name = this.jsxParseElementName();
  4320. this.expect(types$1.jsxTagEnd);
  4321. return this.finishNode(node, "JSXClosingElement");
  4322. }
  4323. jsxParseElementAt(startPos, startLoc) {
  4324. const node = this.startNodeAt(startPos, startLoc);
  4325. const children = [];
  4326. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  4327. let closingElement = null;
  4328. if (!openingElement.selfClosing) {
  4329. contents: for (;;) {
  4330. switch (this.state.type) {
  4331. case types$1.jsxTagStart:
  4332. startPos = this.state.start;
  4333. startLoc = this.state.startLoc;
  4334. this.next();
  4335. if (this.eat(types$1.slash)) {
  4336. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  4337. break contents;
  4338. }
  4339. children.push(this.jsxParseElementAt(startPos, startLoc));
  4340. break;
  4341. case types$1.jsxText:
  4342. children.push(this.parseExprAtom());
  4343. break;
  4344. case types$1.braceL:
  4345. {
  4346. const node = this.startNode();
  4347. this.next();
  4348. if (this.match(types$1.ellipsis)) {
  4349. children.push(this.jsxParseSpreadChild(node));
  4350. } else {
  4351. children.push(this.jsxParseExpressionContainer(node));
  4352. }
  4353. break;
  4354. }
  4355. default:
  4356. throw this.unexpected();
  4357. }
  4358. }
  4359. if (isFragment(openingElement) && !isFragment(closingElement)) {
  4360. this.raise(closingElement.start, JsxErrors.MissingClosingTagFragment);
  4361. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  4362. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4363. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  4364. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  4365. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  4366. }
  4367. }
  4368. }
  4369. if (isFragment(openingElement)) {
  4370. node.openingFragment = openingElement;
  4371. node.closingFragment = closingElement;
  4372. } else {
  4373. node.openingElement = openingElement;
  4374. node.closingElement = closingElement;
  4375. }
  4376. node.children = children;
  4377. if (this.isRelational("<")) {
  4378. throw this.raise(this.state.start, JsxErrors.UnwrappedAdjacentJSXElements);
  4379. }
  4380. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  4381. }
  4382. jsxParseElement() {
  4383. const startPos = this.state.start;
  4384. const startLoc = this.state.startLoc;
  4385. this.next();
  4386. return this.jsxParseElementAt(startPos, startLoc);
  4387. }
  4388. parseExprAtom(refExpressionErrors) {
  4389. if (this.match(types$1.jsxText)) {
  4390. return this.parseLiteral(this.state.value, "JSXText");
  4391. } else if (this.match(types$1.jsxTagStart)) {
  4392. return this.jsxParseElement();
  4393. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  4394. this.finishToken(types$1.jsxTagStart);
  4395. return this.jsxParseElement();
  4396. } else {
  4397. return super.parseExprAtom(refExpressionErrors);
  4398. }
  4399. }
  4400. createLookaheadState(state) {
  4401. const lookaheadState = super.createLookaheadState(state);
  4402. lookaheadState.inPropertyName = state.inPropertyName;
  4403. return lookaheadState;
  4404. }
  4405. getTokenFromCode(code) {
  4406. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  4407. const context = this.curContext();
  4408. if (context === types.j_expr) {
  4409. return this.jsxReadToken();
  4410. }
  4411. if (context === types.j_oTag || context === types.j_cTag) {
  4412. if (isIdentifierStart(code)) {
  4413. return this.jsxReadWord();
  4414. }
  4415. if (code === 62) {
  4416. ++this.state.pos;
  4417. return this.finishToken(types$1.jsxTagEnd);
  4418. }
  4419. if ((code === 34 || code === 39) && context === types.j_oTag) {
  4420. return this.jsxReadString(code);
  4421. }
  4422. }
  4423. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  4424. ++this.state.pos;
  4425. return this.finishToken(types$1.jsxTagStart);
  4426. }
  4427. return super.getTokenFromCode(code);
  4428. }
  4429. updateContext(prevType) {
  4430. super.updateContext(prevType);
  4431. const {
  4432. context,
  4433. type
  4434. } = this.state;
  4435. if (type === types$1.braceL) {
  4436. const curContext = context[context.length - 1];
  4437. if (curContext === types.j_oTag) {
  4438. context.push(types.brace);
  4439. } else if (curContext === types.j_expr) {
  4440. context.push(types.templateQuasi);
  4441. }
  4442. this.state.exprAllowed = true;
  4443. } else if (type === types$1.slash && prevType === types$1.jsxTagStart) {
  4444. context.length -= 2;
  4445. context.push(types.j_cTag);
  4446. this.state.exprAllowed = false;
  4447. } else if (type === types$1.jsxTagEnd) {
  4448. const out = context.pop();
  4449. if (out === types.j_oTag && prevType === types$1.slash || out === types.j_cTag) {
  4450. context.pop();
  4451. this.state.exprAllowed = context[context.length - 1] === types.j_expr;
  4452. } else {
  4453. this.state.exprAllowed = true;
  4454. }
  4455. } else if (type.keyword && (prevType === types$1.dot || prevType === types$1.questionDot)) {
  4456. this.state.exprAllowed = false;
  4457. } else {
  4458. this.state.exprAllowed = type.beforeExpr;
  4459. }
  4460. }
  4461. });
  4462. class TypeScriptScope extends Scope {
  4463. constructor(...args) {
  4464. super(...args);
  4465. this.types = new Set();
  4466. this.enums = new Set();
  4467. this.constEnums = new Set();
  4468. this.classes = new Set();
  4469. this.exportOnlyBindings = new Set();
  4470. }
  4471. }
  4472. class TypeScriptScopeHandler extends ScopeHandler {
  4473. createScope(flags) {
  4474. return new TypeScriptScope(flags);
  4475. }
  4476. declareName(name, bindingType, pos) {
  4477. const scope = this.currentScope();
  4478. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  4479. this.maybeExportDefined(scope, name);
  4480. scope.exportOnlyBindings.add(name);
  4481. return;
  4482. }
  4483. super.declareName(...arguments);
  4484. if (bindingType & BIND_KIND_TYPE) {
  4485. if (!(bindingType & BIND_KIND_VALUE)) {
  4486. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  4487. this.maybeExportDefined(scope, name);
  4488. }
  4489. scope.types.add(name);
  4490. }
  4491. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  4492. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  4493. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  4494. }
  4495. isRedeclaredInScope(scope, name, bindingType) {
  4496. if (scope.enums.has(name)) {
  4497. if (bindingType & BIND_FLAGS_TS_ENUM) {
  4498. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  4499. const wasConst = scope.constEnums.has(name);
  4500. return isConst !== wasConst;
  4501. }
  4502. return true;
  4503. }
  4504. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  4505. if (scope.lexical.has(name)) {
  4506. return !!(bindingType & BIND_KIND_VALUE);
  4507. } else {
  4508. return false;
  4509. }
  4510. }
  4511. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  4512. return true;
  4513. }
  4514. return super.isRedeclaredInScope(...arguments);
  4515. }
  4516. checkLocalExport(id) {
  4517. const topLevelScope = this.scopeStack[0];
  4518. const {
  4519. name
  4520. } = id;
  4521. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  4522. super.checkLocalExport(id);
  4523. }
  4524. }
  4525. }
  4526. const PARAM = 0b0000,
  4527. PARAM_YIELD = 0b0001,
  4528. PARAM_AWAIT = 0b0010,
  4529. PARAM_RETURN = 0b0100,
  4530. PARAM_IN = 0b1000;
  4531. class ProductionParameterHandler {
  4532. constructor() {
  4533. this.stacks = [];
  4534. }
  4535. enter(flags) {
  4536. this.stacks.push(flags);
  4537. }
  4538. exit() {
  4539. this.stacks.pop();
  4540. }
  4541. currentFlags() {
  4542. return this.stacks[this.stacks.length - 1];
  4543. }
  4544. get hasAwait() {
  4545. return (this.currentFlags() & PARAM_AWAIT) > 0;
  4546. }
  4547. get hasYield() {
  4548. return (this.currentFlags() & PARAM_YIELD) > 0;
  4549. }
  4550. get hasReturn() {
  4551. return (this.currentFlags() & PARAM_RETURN) > 0;
  4552. }
  4553. get hasIn() {
  4554. return (this.currentFlags() & PARAM_IN) > 0;
  4555. }
  4556. }
  4557. function functionFlags(isAsync, isGenerator) {
  4558. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  4559. }
  4560. function nonNull(x) {
  4561. if (x == null) {
  4562. throw new Error(`Unexpected ${x} value.`);
  4563. }
  4564. return x;
  4565. }
  4566. function assert(x) {
  4567. if (!x) {
  4568. throw new Error("Assert fail");
  4569. }
  4570. }
  4571. const TSErrors = makeErrorTemplates({
  4572. AbstractMethodHasImplementation: "Method '%0' cannot have an implementation because it is marked abstract.",
  4573. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  4574. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  4575. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  4576. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  4577. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  4578. DeclareAccessor: "'declare' is not allowed in %0ters.",
  4579. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  4580. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  4581. DuplicateAccessibilityModifier: "Accessibility modifier already seen.",
  4582. DuplicateModifier: "Duplicate modifier: '%0'.",
  4583. EmptyHeritageClauseType: "'%0' list cannot be empty.",
  4584. EmptyTypeArguments: "Type argument list cannot be empty.",
  4585. EmptyTypeParameters: "Type parameter list cannot be empty.",
  4586. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  4587. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  4588. IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.",
  4589. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  4590. IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0').",
  4591. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  4592. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  4593. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  4594. InvalidModifierOnTypeMember: "'%0' modifier cannot appear on a type member.",
  4595. InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
  4596. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  4597. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  4598. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  4599. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  4600. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  4601. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  4602. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  4603. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  4604. PrivateElementHasAccessibility: "Private elements cannot have an accessibility modifier ('%0').",
  4605. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  4606. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  4607. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  4608. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  4609. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  4610. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  4611. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  4612. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  4613. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  4614. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  4615. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  4616. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  4617. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  4618. UnsupportedSignatureParameterKind: "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0."
  4619. }, ErrorCodes.SyntaxError);
  4620. function keywordTypeFromName(value) {
  4621. switch (value) {
  4622. case "any":
  4623. return "TSAnyKeyword";
  4624. case "boolean":
  4625. return "TSBooleanKeyword";
  4626. case "bigint":
  4627. return "TSBigIntKeyword";
  4628. case "never":
  4629. return "TSNeverKeyword";
  4630. case "number":
  4631. return "TSNumberKeyword";
  4632. case "object":
  4633. return "TSObjectKeyword";
  4634. case "string":
  4635. return "TSStringKeyword";
  4636. case "symbol":
  4637. return "TSSymbolKeyword";
  4638. case "undefined":
  4639. return "TSUndefinedKeyword";
  4640. case "unknown":
  4641. return "TSUnknownKeyword";
  4642. default:
  4643. return undefined;
  4644. }
  4645. }
  4646. function tsIsAccessModifier(modifier) {
  4647. return modifier === "private" || modifier === "public" || modifier === "protected";
  4648. }
  4649. var typescript = (superClass => class extends superClass {
  4650. getScopeHandler() {
  4651. return TypeScriptScopeHandler;
  4652. }
  4653. tsIsIdentifier() {
  4654. return this.match(types$1.name);
  4655. }
  4656. tsTokenCanFollowModifier() {
  4657. 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();
  4658. }
  4659. tsNextTokenCanFollowModifier() {
  4660. this.next();
  4661. return this.tsTokenCanFollowModifier();
  4662. }
  4663. tsParseModifier(allowedModifiers) {
  4664. if (!this.match(types$1.name)) {
  4665. return undefined;
  4666. }
  4667. const modifier = this.state.value;
  4668. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  4669. return modifier;
  4670. }
  4671. return undefined;
  4672. }
  4673. tsParseModifiers(modified, allowedModifiers, disallowedModifiers, errorTemplate) {
  4674. const enforceOrder = (pos, modifier, before, after) => {
  4675. if (modifier === before && modified[after]) {
  4676. this.raise(pos, TSErrors.InvalidModifiersOrder, before, after);
  4677. }
  4678. };
  4679. const incompatible = (pos, modifier, mod1, mod2) => {
  4680. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  4681. this.raise(pos, TSErrors.IncompatibleModifiers, mod1, mod2);
  4682. }
  4683. };
  4684. for (;;) {
  4685. const startPos = this.state.start;
  4686. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []));
  4687. if (!modifier) break;
  4688. if (tsIsAccessModifier(modifier)) {
  4689. if (modified.accessibility) {
  4690. this.raise(startPos, TSErrors.DuplicateAccessibilityModifier);
  4691. } else {
  4692. enforceOrder(startPos, modifier, modifier, "override");
  4693. enforceOrder(startPos, modifier, modifier, "static");
  4694. modified.accessibility = modifier;
  4695. }
  4696. } else {
  4697. if (Object.hasOwnProperty.call(modified, modifier)) {
  4698. this.raise(startPos, TSErrors.DuplicateModifier, modifier);
  4699. } else {
  4700. enforceOrder(startPos, modifier, "static", "readonly");
  4701. enforceOrder(startPos, modifier, "static", "override");
  4702. enforceOrder(startPos, modifier, "override", "readonly");
  4703. enforceOrder(startPos, modifier, "abstract", "override");
  4704. incompatible(startPos, modifier, "declare", "override");
  4705. incompatible(startPos, modifier, "static", "abstract");
  4706. }
  4707. modified[modifier] = true;
  4708. }
  4709. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  4710. this.raise(startPos, errorTemplate, modifier);
  4711. }
  4712. }
  4713. }
  4714. tsIsListTerminator(kind) {
  4715. switch (kind) {
  4716. case "EnumMembers":
  4717. case "TypeMembers":
  4718. return this.match(types$1.braceR);
  4719. case "HeritageClauseElement":
  4720. return this.match(types$1.braceL);
  4721. case "TupleElementTypes":
  4722. return this.match(types$1.bracketR);
  4723. case "TypeParametersOrArguments":
  4724. return this.isRelational(">");
  4725. }
  4726. throw new Error("Unreachable");
  4727. }
  4728. tsParseList(kind, parseElement) {
  4729. const result = [];
  4730. while (!this.tsIsListTerminator(kind)) {
  4731. result.push(parseElement());
  4732. }
  4733. return result;
  4734. }
  4735. tsParseDelimitedList(kind, parseElement) {
  4736. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  4737. }
  4738. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  4739. const result = [];
  4740. for (;;) {
  4741. if (this.tsIsListTerminator(kind)) {
  4742. break;
  4743. }
  4744. const element = parseElement();
  4745. if (element == null) {
  4746. return undefined;
  4747. }
  4748. result.push(element);
  4749. if (this.eat(types$1.comma)) {
  4750. continue;
  4751. }
  4752. if (this.tsIsListTerminator(kind)) {
  4753. break;
  4754. }
  4755. if (expectSuccess) {
  4756. this.expect(types$1.comma);
  4757. }
  4758. return undefined;
  4759. }
  4760. return result;
  4761. }
  4762. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  4763. if (!skipFirstToken) {
  4764. if (bracket) {
  4765. this.expect(types$1.bracketL);
  4766. } else {
  4767. this.expectRelational("<");
  4768. }
  4769. }
  4770. const result = this.tsParseDelimitedList(kind, parseElement);
  4771. if (bracket) {
  4772. this.expect(types$1.bracketR);
  4773. } else {
  4774. this.expectRelational(">");
  4775. }
  4776. return result;
  4777. }
  4778. tsParseImportType() {
  4779. const node = this.startNode();
  4780. this.expect(types$1._import);
  4781. this.expect(types$1.parenL);
  4782. if (!this.match(types$1.string)) {
  4783. this.raise(this.state.start, TSErrors.UnsupportedImportTypeArgument);
  4784. }
  4785. node.argument = this.parseExprAtom();
  4786. this.expect(types$1.parenR);
  4787. if (this.eat(types$1.dot)) {
  4788. node.qualifier = this.tsParseEntityName(true);
  4789. }
  4790. if (this.isRelational("<")) {
  4791. node.typeParameters = this.tsParseTypeArguments();
  4792. }
  4793. return this.finishNode(node, "TSImportType");
  4794. }
  4795. tsParseEntityName(allowReservedWords) {
  4796. let entity = this.parseIdentifier();
  4797. while (this.eat(types$1.dot)) {
  4798. const node = this.startNodeAtNode(entity);
  4799. node.left = entity;
  4800. node.right = this.parseIdentifier(allowReservedWords);
  4801. entity = this.finishNode(node, "TSQualifiedName");
  4802. }
  4803. return entity;
  4804. }
  4805. tsParseTypeReference() {
  4806. const node = this.startNode();
  4807. node.typeName = this.tsParseEntityName(false);
  4808. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  4809. node.typeParameters = this.tsParseTypeArguments();
  4810. }
  4811. return this.finishNode(node, "TSTypeReference");
  4812. }
  4813. tsParseThisTypePredicate(lhs) {
  4814. this.next();
  4815. const node = this.startNodeAtNode(lhs);
  4816. node.parameterName = lhs;
  4817. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  4818. node.asserts = false;
  4819. return this.finishNode(node, "TSTypePredicate");
  4820. }
  4821. tsParseThisTypeNode() {
  4822. const node = this.startNode();
  4823. this.next();
  4824. return this.finishNode(node, "TSThisType");
  4825. }
  4826. tsParseTypeQuery() {
  4827. const node = this.startNode();
  4828. this.expect(types$1._typeof);
  4829. if (this.match(types$1._import)) {
  4830. node.exprName = this.tsParseImportType();
  4831. } else {
  4832. node.exprName = this.tsParseEntityName(true);
  4833. }
  4834. return this.finishNode(node, "TSTypeQuery");
  4835. }
  4836. tsParseTypeParameter() {
  4837. const node = this.startNode();
  4838. node.name = this.parseIdentifierName(node.start);
  4839. node.constraint = this.tsEatThenParseType(types$1._extends);
  4840. node.default = this.tsEatThenParseType(types$1.eq);
  4841. return this.finishNode(node, "TSTypeParameter");
  4842. }
  4843. tsTryParseTypeParameters() {
  4844. if (this.isRelational("<")) {
  4845. return this.tsParseTypeParameters();
  4846. }
  4847. }
  4848. tsParseTypeParameters() {
  4849. const node = this.startNode();
  4850. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  4851. this.next();
  4852. } else {
  4853. this.unexpected();
  4854. }
  4855. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  4856. if (node.params.length === 0) {
  4857. this.raise(node.start, TSErrors.EmptyTypeParameters);
  4858. }
  4859. return this.finishNode(node, "TSTypeParameterDeclaration");
  4860. }
  4861. tsTryNextParseConstantContext() {
  4862. if (this.lookahead().type === types$1._const) {
  4863. this.next();
  4864. return this.tsParseTypeReference();
  4865. }
  4866. return null;
  4867. }
  4868. tsFillSignature(returnToken, signature) {
  4869. const returnTokenRequired = returnToken === types$1.arrow;
  4870. signature.typeParameters = this.tsTryParseTypeParameters();
  4871. this.expect(types$1.parenL);
  4872. signature.parameters = this.tsParseBindingListForSignature();
  4873. if (returnTokenRequired) {
  4874. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4875. } else if (this.match(returnToken)) {
  4876. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  4877. }
  4878. }
  4879. tsParseBindingListForSignature() {
  4880. return this.parseBindingList(types$1.parenR, 41).map(pattern => {
  4881. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  4882. this.raise(pattern.start, TSErrors.UnsupportedSignatureParameterKind, pattern.type);
  4883. }
  4884. return pattern;
  4885. });
  4886. }
  4887. tsParseTypeMemberSemicolon() {
  4888. if (!this.eat(types$1.comma) && !this.isLineTerminator()) {
  4889. this.expect(types$1.semi);
  4890. }
  4891. }
  4892. tsParseSignatureMember(kind, node) {
  4893. this.tsFillSignature(types$1.colon, node);
  4894. this.tsParseTypeMemberSemicolon();
  4895. return this.finishNode(node, kind);
  4896. }
  4897. tsIsUnambiguouslyIndexSignature() {
  4898. this.next();
  4899. return this.eat(types$1.name) && this.match(types$1.colon);
  4900. }
  4901. tsTryParseIndexSignature(node) {
  4902. if (!(this.match(types$1.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  4903. return undefined;
  4904. }
  4905. this.expect(types$1.bracketL);
  4906. const id = this.parseIdentifier();
  4907. id.typeAnnotation = this.tsParseTypeAnnotation();
  4908. this.resetEndLocation(id);
  4909. this.expect(types$1.bracketR);
  4910. node.parameters = [id];
  4911. const type = this.tsTryParseTypeAnnotation();
  4912. if (type) node.typeAnnotation = type;
  4913. this.tsParseTypeMemberSemicolon();
  4914. return this.finishNode(node, "TSIndexSignature");
  4915. }
  4916. tsParsePropertyOrMethodSignature(node, readonly) {
  4917. if (this.eat(types$1.question)) node.optional = true;
  4918. const nodeAny = node;
  4919. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4920. if (readonly) {
  4921. this.raise(node.start, TSErrors.ReadonlyForMethodSignature);
  4922. }
  4923. const method = nodeAny;
  4924. if (method.kind && this.isRelational("<")) {
  4925. this.raise(this.state.pos, TSErrors.AccesorCannotHaveTypeParameters);
  4926. }
  4927. this.tsFillSignature(types$1.colon, method);
  4928. this.tsParseTypeMemberSemicolon();
  4929. if (method.kind === "get") {
  4930. if (method.parameters.length > 0) {
  4931. this.raise(this.state.pos, ErrorMessages.BadGetterArity);
  4932. if (this.isThisParam(method.parameters[0])) {
  4933. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4934. }
  4935. }
  4936. } else if (method.kind === "set") {
  4937. if (method.parameters.length !== 1) {
  4938. this.raise(this.state.pos, ErrorMessages.BadSetterArity);
  4939. } else {
  4940. const firstParameter = method.parameters[0];
  4941. if (this.isThisParam(firstParameter)) {
  4942. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  4943. }
  4944. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  4945. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveOptionalParameter);
  4946. }
  4947. if (firstParameter.type === "RestElement") {
  4948. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveRestParameter);
  4949. }
  4950. }
  4951. if (method.typeAnnotation) {
  4952. this.raise(method.typeAnnotation.start, TSErrors.SetAccesorCannotHaveReturnType);
  4953. }
  4954. } else {
  4955. method.kind = "method";
  4956. }
  4957. return this.finishNode(method, "TSMethodSignature");
  4958. } else {
  4959. const property = nodeAny;
  4960. if (readonly) property.readonly = true;
  4961. const type = this.tsTryParseTypeAnnotation();
  4962. if (type) property.typeAnnotation = type;
  4963. this.tsParseTypeMemberSemicolon();
  4964. return this.finishNode(property, "TSPropertySignature");
  4965. }
  4966. }
  4967. tsParseTypeMember() {
  4968. const node = this.startNode();
  4969. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4970. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  4971. }
  4972. if (this.match(types$1._new)) {
  4973. const id = this.startNode();
  4974. this.next();
  4975. if (this.match(types$1.parenL) || this.isRelational("<")) {
  4976. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  4977. } else {
  4978. node.key = this.createIdentifier(id, "new");
  4979. return this.tsParsePropertyOrMethodSignature(node, false);
  4980. }
  4981. }
  4982. this.tsParseModifiers(node, ["readonly"], ["declare", "abstract", "private", "protected", "public", "static", "override"], TSErrors.InvalidModifierOnTypeMember);
  4983. const idx = this.tsTryParseIndexSignature(node);
  4984. if (idx) {
  4985. return idx;
  4986. }
  4987. this.parsePropertyName(node, false);
  4988. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  4989. node.kind = node.key.name;
  4990. this.parsePropertyName(node, false);
  4991. }
  4992. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  4993. }
  4994. tsParseTypeLiteral() {
  4995. const node = this.startNode();
  4996. node.members = this.tsParseObjectTypeMembers();
  4997. return this.finishNode(node, "TSTypeLiteral");
  4998. }
  4999. tsParseObjectTypeMembers() {
  5000. this.expect(types$1.braceL);
  5001. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  5002. this.expect(types$1.braceR);
  5003. return members;
  5004. }
  5005. tsIsStartOfMappedType() {
  5006. this.next();
  5007. if (this.eat(types$1.plusMin)) {
  5008. return this.isContextual("readonly");
  5009. }
  5010. if (this.isContextual("readonly")) {
  5011. this.next();
  5012. }
  5013. if (!this.match(types$1.bracketL)) {
  5014. return false;
  5015. }
  5016. this.next();
  5017. if (!this.tsIsIdentifier()) {
  5018. return false;
  5019. }
  5020. this.next();
  5021. return this.match(types$1._in);
  5022. }
  5023. tsParseMappedTypeParameter() {
  5024. const node = this.startNode();
  5025. node.name = this.parseIdentifierName(node.start);
  5026. node.constraint = this.tsExpectThenParseType(types$1._in);
  5027. return this.finishNode(node, "TSTypeParameter");
  5028. }
  5029. tsParseMappedType() {
  5030. const node = this.startNode();
  5031. this.expect(types$1.braceL);
  5032. if (this.match(types$1.plusMin)) {
  5033. node.readonly = this.state.value;
  5034. this.next();
  5035. this.expectContextual("readonly");
  5036. } else if (this.eatContextual("readonly")) {
  5037. node.readonly = true;
  5038. }
  5039. this.expect(types$1.bracketL);
  5040. node.typeParameter = this.tsParseMappedTypeParameter();
  5041. node.nameType = this.eatContextual("as") ? this.tsParseType() : null;
  5042. this.expect(types$1.bracketR);
  5043. if (this.match(types$1.plusMin)) {
  5044. node.optional = this.state.value;
  5045. this.next();
  5046. this.expect(types$1.question);
  5047. } else if (this.eat(types$1.question)) {
  5048. node.optional = true;
  5049. }
  5050. node.typeAnnotation = this.tsTryParseType();
  5051. this.semicolon();
  5052. this.expect(types$1.braceR);
  5053. return this.finishNode(node, "TSMappedType");
  5054. }
  5055. tsParseTupleType() {
  5056. const node = this.startNode();
  5057. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  5058. let seenOptionalElement = false;
  5059. let labeledElements = null;
  5060. node.elementTypes.forEach(elementNode => {
  5061. var _labeledElements;
  5062. let {
  5063. type
  5064. } = elementNode;
  5065. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  5066. this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired);
  5067. }
  5068. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  5069. if (type === "TSRestType") {
  5070. elementNode = elementNode.typeAnnotation;
  5071. type = elementNode.type;
  5072. }
  5073. const isLabeled = type === "TSNamedTupleMember";
  5074. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  5075. if (labeledElements !== isLabeled) {
  5076. this.raise(elementNode.start, TSErrors.MixedLabeledAndUnlabeledElements);
  5077. }
  5078. });
  5079. return this.finishNode(node, "TSTupleType");
  5080. }
  5081. tsParseTupleElementType() {
  5082. const {
  5083. start: startPos,
  5084. startLoc
  5085. } = this.state;
  5086. const rest = this.eat(types$1.ellipsis);
  5087. let type = this.tsParseType();
  5088. const optional = this.eat(types$1.question);
  5089. const labeled = this.eat(types$1.colon);
  5090. if (labeled) {
  5091. const labeledNode = this.startNodeAtNode(type);
  5092. labeledNode.optional = optional;
  5093. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  5094. labeledNode.label = type.typeName;
  5095. } else {
  5096. this.raise(type.start, TSErrors.InvalidTupleMemberLabel);
  5097. labeledNode.label = type;
  5098. }
  5099. labeledNode.elementType = this.tsParseType();
  5100. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  5101. } else if (optional) {
  5102. const optionalTypeNode = this.startNodeAtNode(type);
  5103. optionalTypeNode.typeAnnotation = type;
  5104. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  5105. }
  5106. if (rest) {
  5107. const restNode = this.startNodeAt(startPos, startLoc);
  5108. restNode.typeAnnotation = type;
  5109. type = this.finishNode(restNode, "TSRestType");
  5110. }
  5111. return type;
  5112. }
  5113. tsParseParenthesizedType() {
  5114. const node = this.startNode();
  5115. this.expect(types$1.parenL);
  5116. node.typeAnnotation = this.tsParseType();
  5117. this.expect(types$1.parenR);
  5118. return this.finishNode(node, "TSParenthesizedType");
  5119. }
  5120. tsParseFunctionOrConstructorType(type, abstract) {
  5121. const node = this.startNode();
  5122. if (type === "TSConstructorType") {
  5123. node.abstract = !!abstract;
  5124. if (abstract) this.next();
  5125. this.next();
  5126. }
  5127. this.tsFillSignature(types$1.arrow, node);
  5128. return this.finishNode(node, type);
  5129. }
  5130. tsParseLiteralTypeNode() {
  5131. const node = this.startNode();
  5132. node.literal = (() => {
  5133. switch (this.state.type) {
  5134. case types$1.num:
  5135. case types$1.bigint:
  5136. case types$1.string:
  5137. case types$1._true:
  5138. case types$1._false:
  5139. return this.parseExprAtom();
  5140. default:
  5141. throw this.unexpected();
  5142. }
  5143. })();
  5144. return this.finishNode(node, "TSLiteralType");
  5145. }
  5146. tsParseTemplateLiteralType() {
  5147. const node = this.startNode();
  5148. node.literal = this.parseTemplate(false);
  5149. return this.finishNode(node, "TSLiteralType");
  5150. }
  5151. parseTemplateSubstitution() {
  5152. if (this.state.inType) return this.tsParseType();
  5153. return super.parseTemplateSubstitution();
  5154. }
  5155. tsParseThisTypeOrThisTypePredicate() {
  5156. const thisKeyword = this.tsParseThisTypeNode();
  5157. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5158. return this.tsParseThisTypePredicate(thisKeyword);
  5159. } else {
  5160. return thisKeyword;
  5161. }
  5162. }
  5163. tsParseNonArrayType() {
  5164. switch (this.state.type) {
  5165. case types$1.name:
  5166. case types$1._void:
  5167. case types$1._null:
  5168. {
  5169. const type = this.match(types$1._void) ? "TSVoidKeyword" : this.match(types$1._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  5170. if (type !== undefined && this.lookaheadCharCode() !== 46) {
  5171. const node = this.startNode();
  5172. this.next();
  5173. return this.finishNode(node, type);
  5174. }
  5175. return this.tsParseTypeReference();
  5176. }
  5177. case types$1.string:
  5178. case types$1.num:
  5179. case types$1.bigint:
  5180. case types$1._true:
  5181. case types$1._false:
  5182. return this.tsParseLiteralTypeNode();
  5183. case types$1.plusMin:
  5184. if (this.state.value === "-") {
  5185. const node = this.startNode();
  5186. const nextToken = this.lookahead();
  5187. if (nextToken.type !== types$1.num && nextToken.type !== types$1.bigint) {
  5188. throw this.unexpected();
  5189. }
  5190. node.literal = this.parseMaybeUnary();
  5191. return this.finishNode(node, "TSLiteralType");
  5192. }
  5193. break;
  5194. case types$1._this:
  5195. return this.tsParseThisTypeOrThisTypePredicate();
  5196. case types$1._typeof:
  5197. return this.tsParseTypeQuery();
  5198. case types$1._import:
  5199. return this.tsParseImportType();
  5200. case types$1.braceL:
  5201. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  5202. case types$1.bracketL:
  5203. return this.tsParseTupleType();
  5204. case types$1.parenL:
  5205. return this.tsParseParenthesizedType();
  5206. case types$1.backQuote:
  5207. return this.tsParseTemplateLiteralType();
  5208. }
  5209. throw this.unexpected();
  5210. }
  5211. tsParseArrayTypeOrHigher() {
  5212. let type = this.tsParseNonArrayType();
  5213. while (!this.hasPrecedingLineBreak() && this.eat(types$1.bracketL)) {
  5214. if (this.match(types$1.bracketR)) {
  5215. const node = this.startNodeAtNode(type);
  5216. node.elementType = type;
  5217. this.expect(types$1.bracketR);
  5218. type = this.finishNode(node, "TSArrayType");
  5219. } else {
  5220. const node = this.startNodeAtNode(type);
  5221. node.objectType = type;
  5222. node.indexType = this.tsParseType();
  5223. this.expect(types$1.bracketR);
  5224. type = this.finishNode(node, "TSIndexedAccessType");
  5225. }
  5226. }
  5227. return type;
  5228. }
  5229. tsParseTypeOperator(operator) {
  5230. const node = this.startNode();
  5231. this.expectContextual(operator);
  5232. node.operator = operator;
  5233. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  5234. if (operator === "readonly") {
  5235. this.tsCheckTypeAnnotationForReadOnly(node);
  5236. }
  5237. return this.finishNode(node, "TSTypeOperator");
  5238. }
  5239. tsCheckTypeAnnotationForReadOnly(node) {
  5240. switch (node.typeAnnotation.type) {
  5241. case "TSTupleType":
  5242. case "TSArrayType":
  5243. return;
  5244. default:
  5245. this.raise(node.start, TSErrors.UnexpectedReadonly);
  5246. }
  5247. }
  5248. tsParseInferType() {
  5249. const node = this.startNode();
  5250. this.expectContextual("infer");
  5251. const typeParameter = this.startNode();
  5252. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  5253. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  5254. return this.finishNode(node, "TSInferType");
  5255. }
  5256. tsParseTypeOperatorOrHigher() {
  5257. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  5258. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  5259. }
  5260. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  5261. const node = this.startNode();
  5262. const hasLeadingOperator = this.eat(operator);
  5263. const types = [];
  5264. do {
  5265. types.push(parseConstituentType());
  5266. } while (this.eat(operator));
  5267. if (types.length === 1 && !hasLeadingOperator) {
  5268. return types[0];
  5269. }
  5270. node.types = types;
  5271. return this.finishNode(node, kind);
  5272. }
  5273. tsParseIntersectionTypeOrHigher() {
  5274. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types$1.bitwiseAND);
  5275. }
  5276. tsParseUnionTypeOrHigher() {
  5277. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types$1.bitwiseOR);
  5278. }
  5279. tsIsStartOfFunctionType() {
  5280. if (this.isRelational("<")) {
  5281. return true;
  5282. }
  5283. return this.match(types$1.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  5284. }
  5285. tsSkipParameterStart() {
  5286. if (this.match(types$1.name) || this.match(types$1._this)) {
  5287. this.next();
  5288. return true;
  5289. }
  5290. if (this.match(types$1.braceL)) {
  5291. let braceStackCounter = 1;
  5292. this.next();
  5293. while (braceStackCounter > 0) {
  5294. if (this.match(types$1.braceL)) {
  5295. ++braceStackCounter;
  5296. } else if (this.match(types$1.braceR)) {
  5297. --braceStackCounter;
  5298. }
  5299. this.next();
  5300. }
  5301. return true;
  5302. }
  5303. if (this.match(types$1.bracketL)) {
  5304. let braceStackCounter = 1;
  5305. this.next();
  5306. while (braceStackCounter > 0) {
  5307. if (this.match(types$1.bracketL)) {
  5308. ++braceStackCounter;
  5309. } else if (this.match(types$1.bracketR)) {
  5310. --braceStackCounter;
  5311. }
  5312. this.next();
  5313. }
  5314. return true;
  5315. }
  5316. return false;
  5317. }
  5318. tsIsUnambiguouslyStartOfFunctionType() {
  5319. this.next();
  5320. if (this.match(types$1.parenR) || this.match(types$1.ellipsis)) {
  5321. return true;
  5322. }
  5323. if (this.tsSkipParameterStart()) {
  5324. if (this.match(types$1.colon) || this.match(types$1.comma) || this.match(types$1.question) || this.match(types$1.eq)) {
  5325. return true;
  5326. }
  5327. if (this.match(types$1.parenR)) {
  5328. this.next();
  5329. if (this.match(types$1.arrow)) {
  5330. return true;
  5331. }
  5332. }
  5333. }
  5334. return false;
  5335. }
  5336. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  5337. return this.tsInType(() => {
  5338. const t = this.startNode();
  5339. this.expect(returnToken);
  5340. const node = this.startNode();
  5341. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  5342. if (asserts && this.match(types$1._this)) {
  5343. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  5344. if (thisTypePredicate.type === "TSThisType") {
  5345. node.parameterName = thisTypePredicate;
  5346. node.asserts = true;
  5347. node.typeAnnotation = null;
  5348. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  5349. } else {
  5350. this.resetStartLocationFromNode(thisTypePredicate, node);
  5351. thisTypePredicate.asserts = true;
  5352. }
  5353. t.typeAnnotation = thisTypePredicate;
  5354. return this.finishNode(t, "TSTypeAnnotation");
  5355. }
  5356. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  5357. if (!typePredicateVariable) {
  5358. if (!asserts) {
  5359. return this.tsParseTypeAnnotation(false, t);
  5360. }
  5361. node.parameterName = this.parseIdentifier();
  5362. node.asserts = asserts;
  5363. node.typeAnnotation = null;
  5364. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5365. return this.finishNode(t, "TSTypeAnnotation");
  5366. }
  5367. const type = this.tsParseTypeAnnotation(false);
  5368. node.parameterName = typePredicateVariable;
  5369. node.typeAnnotation = type;
  5370. node.asserts = asserts;
  5371. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  5372. return this.finishNode(t, "TSTypeAnnotation");
  5373. });
  5374. }
  5375. tsTryParseTypeOrTypePredicateAnnotation() {
  5376. return this.match(types$1.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types$1.colon) : undefined;
  5377. }
  5378. tsTryParseTypeAnnotation() {
  5379. return this.match(types$1.colon) ? this.tsParseTypeAnnotation() : undefined;
  5380. }
  5381. tsTryParseType() {
  5382. return this.tsEatThenParseType(types$1.colon);
  5383. }
  5384. tsParseTypePredicatePrefix() {
  5385. const id = this.parseIdentifier();
  5386. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  5387. this.next();
  5388. return id;
  5389. }
  5390. }
  5391. tsParseTypePredicateAsserts() {
  5392. if (!this.match(types$1.name) || this.state.value !== "asserts" || this.hasPrecedingLineBreak()) {
  5393. return false;
  5394. }
  5395. const containsEsc = this.state.containsEsc;
  5396. this.next();
  5397. if (!this.match(types$1.name) && !this.match(types$1._this)) {
  5398. return false;
  5399. }
  5400. if (containsEsc) {
  5401. this.raise(this.state.lastTokStart, ErrorMessages.InvalidEscapedReservedWord, "asserts");
  5402. }
  5403. return true;
  5404. }
  5405. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  5406. this.tsInType(() => {
  5407. if (eatColon) this.expect(types$1.colon);
  5408. t.typeAnnotation = this.tsParseType();
  5409. });
  5410. return this.finishNode(t, "TSTypeAnnotation");
  5411. }
  5412. tsParseType() {
  5413. assert(this.state.inType);
  5414. const type = this.tsParseNonConditionalType();
  5415. if (this.hasPrecedingLineBreak() || !this.eat(types$1._extends)) {
  5416. return type;
  5417. }
  5418. const node = this.startNodeAtNode(type);
  5419. node.checkType = type;
  5420. node.extendsType = this.tsParseNonConditionalType();
  5421. this.expect(types$1.question);
  5422. node.trueType = this.tsParseType();
  5423. this.expect(types$1.colon);
  5424. node.falseType = this.tsParseType();
  5425. return this.finishNode(node, "TSConditionalType");
  5426. }
  5427. isAbstractConstructorSignature() {
  5428. return this.isContextual("abstract") && this.lookahead().type === types$1._new;
  5429. }
  5430. tsParseNonConditionalType() {
  5431. if (this.tsIsStartOfFunctionType()) {
  5432. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  5433. }
  5434. if (this.match(types$1._new)) {
  5435. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  5436. } else if (this.isAbstractConstructorSignature()) {
  5437. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  5438. }
  5439. return this.tsParseUnionTypeOrHigher();
  5440. }
  5441. tsParseTypeAssertion() {
  5442. const node = this.startNode();
  5443. const _const = this.tsTryNextParseConstantContext();
  5444. node.typeAnnotation = _const || this.tsNextThenParseType();
  5445. this.expectRelational(">");
  5446. node.expression = this.parseMaybeUnary();
  5447. return this.finishNode(node, "TSTypeAssertion");
  5448. }
  5449. tsParseHeritageClause(descriptor) {
  5450. const originalStart = this.state.start;
  5451. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  5452. if (!delimitedList.length) {
  5453. this.raise(originalStart, TSErrors.EmptyHeritageClauseType, descriptor);
  5454. }
  5455. return delimitedList;
  5456. }
  5457. tsParseExpressionWithTypeArguments() {
  5458. const node = this.startNode();
  5459. node.expression = this.tsParseEntityName(false);
  5460. if (this.isRelational("<")) {
  5461. node.typeParameters = this.tsParseTypeArguments();
  5462. }
  5463. return this.finishNode(node, "TSExpressionWithTypeArguments");
  5464. }
  5465. tsParseInterfaceDeclaration(node) {
  5466. node.id = this.parseIdentifier();
  5467. this.checkLVal(node.id, "typescript interface declaration", BIND_TS_INTERFACE);
  5468. node.typeParameters = this.tsTryParseTypeParameters();
  5469. if (this.eat(types$1._extends)) {
  5470. node.extends = this.tsParseHeritageClause("extends");
  5471. }
  5472. const body = this.startNode();
  5473. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  5474. node.body = this.finishNode(body, "TSInterfaceBody");
  5475. return this.finishNode(node, "TSInterfaceDeclaration");
  5476. }
  5477. tsParseTypeAliasDeclaration(node) {
  5478. node.id = this.parseIdentifier();
  5479. this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
  5480. node.typeParameters = this.tsTryParseTypeParameters();
  5481. node.typeAnnotation = this.tsInType(() => {
  5482. this.expect(types$1.eq);
  5483. if (this.isContextual("intrinsic") && this.lookahead().type !== types$1.dot) {
  5484. const node = this.startNode();
  5485. this.next();
  5486. return this.finishNode(node, "TSIntrinsicKeyword");
  5487. }
  5488. return this.tsParseType();
  5489. });
  5490. this.semicolon();
  5491. return this.finishNode(node, "TSTypeAliasDeclaration");
  5492. }
  5493. tsInNoContext(cb) {
  5494. const oldContext = this.state.context;
  5495. this.state.context = [oldContext[0]];
  5496. try {
  5497. return cb();
  5498. } finally {
  5499. this.state.context = oldContext;
  5500. }
  5501. }
  5502. tsInType(cb) {
  5503. const oldInType = this.state.inType;
  5504. this.state.inType = true;
  5505. try {
  5506. return cb();
  5507. } finally {
  5508. this.state.inType = oldInType;
  5509. }
  5510. }
  5511. tsEatThenParseType(token) {
  5512. return !this.match(token) ? undefined : this.tsNextThenParseType();
  5513. }
  5514. tsExpectThenParseType(token) {
  5515. return this.tsDoThenParseType(() => this.expect(token));
  5516. }
  5517. tsNextThenParseType() {
  5518. return this.tsDoThenParseType(() => this.next());
  5519. }
  5520. tsDoThenParseType(cb) {
  5521. return this.tsInType(() => {
  5522. cb();
  5523. return this.tsParseType();
  5524. });
  5525. }
  5526. tsParseEnumMember() {
  5527. const node = this.startNode();
  5528. node.id = this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  5529. if (this.eat(types$1.eq)) {
  5530. node.initializer = this.parseMaybeAssignAllowIn();
  5531. }
  5532. return this.finishNode(node, "TSEnumMember");
  5533. }
  5534. tsParseEnumDeclaration(node, isConst) {
  5535. if (isConst) node.const = true;
  5536. node.id = this.parseIdentifier();
  5537. this.checkLVal(node.id, "typescript enum declaration", isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  5538. this.expect(types$1.braceL);
  5539. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  5540. this.expect(types$1.braceR);
  5541. return this.finishNode(node, "TSEnumDeclaration");
  5542. }
  5543. tsParseModuleBlock() {
  5544. const node = this.startNode();
  5545. this.scope.enter(SCOPE_OTHER);
  5546. this.expect(types$1.braceL);
  5547. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types$1.braceR);
  5548. this.scope.exit();
  5549. return this.finishNode(node, "TSModuleBlock");
  5550. }
  5551. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  5552. node.id = this.parseIdentifier();
  5553. if (!nested) {
  5554. this.checkLVal(node.id, "module or namespace declaration", BIND_TS_NAMESPACE);
  5555. }
  5556. if (this.eat(types$1.dot)) {
  5557. const inner = this.startNode();
  5558. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  5559. node.body = inner;
  5560. } else {
  5561. this.scope.enter(SCOPE_TS_MODULE);
  5562. this.prodParam.enter(PARAM);
  5563. node.body = this.tsParseModuleBlock();
  5564. this.prodParam.exit();
  5565. this.scope.exit();
  5566. }
  5567. return this.finishNode(node, "TSModuleDeclaration");
  5568. }
  5569. tsParseAmbientExternalModuleDeclaration(node) {
  5570. if (this.isContextual("global")) {
  5571. node.global = true;
  5572. node.id = this.parseIdentifier();
  5573. } else if (this.match(types$1.string)) {
  5574. node.id = this.parseExprAtom();
  5575. } else {
  5576. this.unexpected();
  5577. }
  5578. if (this.match(types$1.braceL)) {
  5579. this.scope.enter(SCOPE_TS_MODULE);
  5580. this.prodParam.enter(PARAM);
  5581. node.body = this.tsParseModuleBlock();
  5582. this.prodParam.exit();
  5583. this.scope.exit();
  5584. } else {
  5585. this.semicolon();
  5586. }
  5587. return this.finishNode(node, "TSModuleDeclaration");
  5588. }
  5589. tsParseImportEqualsDeclaration(node, isExport) {
  5590. node.isExport = isExport || false;
  5591. node.id = this.parseIdentifier();
  5592. this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
  5593. this.expect(types$1.eq);
  5594. const moduleReference = this.tsParseModuleReference();
  5595. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  5596. this.raise(moduleReference.start, TSErrors.ImportAliasHasImportType);
  5597. }
  5598. node.moduleReference = moduleReference;
  5599. this.semicolon();
  5600. return this.finishNode(node, "TSImportEqualsDeclaration");
  5601. }
  5602. tsIsExternalModuleReference() {
  5603. return this.isContextual("require") && this.lookaheadCharCode() === 40;
  5604. }
  5605. tsParseModuleReference() {
  5606. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  5607. }
  5608. tsParseExternalModuleReference() {
  5609. const node = this.startNode();
  5610. this.expectContextual("require");
  5611. this.expect(types$1.parenL);
  5612. if (!this.match(types$1.string)) {
  5613. throw this.unexpected();
  5614. }
  5615. node.expression = this.parseExprAtom();
  5616. this.expect(types$1.parenR);
  5617. return this.finishNode(node, "TSExternalModuleReference");
  5618. }
  5619. tsLookAhead(f) {
  5620. const state = this.state.clone();
  5621. const res = f();
  5622. this.state = state;
  5623. return res;
  5624. }
  5625. tsTryParseAndCatch(f) {
  5626. const result = this.tryParse(abort => f() || abort());
  5627. if (result.aborted || !result.node) return undefined;
  5628. if (result.error) this.state = result.failState;
  5629. return result.node;
  5630. }
  5631. tsTryParse(f) {
  5632. const state = this.state.clone();
  5633. const result = f();
  5634. if (result !== undefined && result !== false) {
  5635. return result;
  5636. } else {
  5637. this.state = state;
  5638. return undefined;
  5639. }
  5640. }
  5641. tsTryParseDeclare(nany) {
  5642. if (this.isLineTerminator()) {
  5643. return;
  5644. }
  5645. let starttype = this.state.type;
  5646. let kind;
  5647. if (this.isContextual("let")) {
  5648. starttype = types$1._var;
  5649. kind = "let";
  5650. }
  5651. return this.tsInAmbientContext(() => {
  5652. switch (starttype) {
  5653. case types$1._function:
  5654. nany.declare = true;
  5655. return this.parseFunctionStatement(nany, false, true);
  5656. case types$1._class:
  5657. nany.declare = true;
  5658. return this.parseClass(nany, true, false);
  5659. case types$1._const:
  5660. if (this.match(types$1._const) && this.isLookaheadContextual("enum")) {
  5661. this.expect(types$1._const);
  5662. this.expectContextual("enum");
  5663. return this.tsParseEnumDeclaration(nany, true);
  5664. }
  5665. case types$1._var:
  5666. kind = kind || this.state.value;
  5667. return this.parseVarStatement(nany, kind);
  5668. case types$1.name:
  5669. {
  5670. const value = this.state.value;
  5671. if (value === "global") {
  5672. return this.tsParseAmbientExternalModuleDeclaration(nany);
  5673. } else {
  5674. return this.tsParseDeclaration(nany, value, true);
  5675. }
  5676. }
  5677. }
  5678. });
  5679. }
  5680. tsTryParseExportDeclaration() {
  5681. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  5682. }
  5683. tsParseExpressionStatement(node, expr) {
  5684. switch (expr.name) {
  5685. case "declare":
  5686. {
  5687. const declaration = this.tsTryParseDeclare(node);
  5688. if (declaration) {
  5689. declaration.declare = true;
  5690. return declaration;
  5691. }
  5692. break;
  5693. }
  5694. case "global":
  5695. if (this.match(types$1.braceL)) {
  5696. this.scope.enter(SCOPE_TS_MODULE);
  5697. this.prodParam.enter(PARAM);
  5698. const mod = node;
  5699. mod.global = true;
  5700. mod.id = expr;
  5701. mod.body = this.tsParseModuleBlock();
  5702. this.scope.exit();
  5703. this.prodParam.exit();
  5704. return this.finishNode(mod, "TSModuleDeclaration");
  5705. }
  5706. break;
  5707. default:
  5708. return this.tsParseDeclaration(node, expr.name, false);
  5709. }
  5710. }
  5711. tsParseDeclaration(node, value, next) {
  5712. switch (value) {
  5713. case "abstract":
  5714. if (this.tsCheckLineTerminator(next) && (this.match(types$1._class) || this.match(types$1.name))) {
  5715. return this.tsParseAbstractDeclaration(node);
  5716. }
  5717. break;
  5718. case "enum":
  5719. if (next || this.match(types$1.name)) {
  5720. if (next) this.next();
  5721. return this.tsParseEnumDeclaration(node, false);
  5722. }
  5723. break;
  5724. case "interface":
  5725. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5726. return this.tsParseInterfaceDeclaration(node);
  5727. }
  5728. break;
  5729. case "module":
  5730. if (this.tsCheckLineTerminator(next)) {
  5731. if (this.match(types$1.string)) {
  5732. return this.tsParseAmbientExternalModuleDeclaration(node);
  5733. } else if (this.match(types$1.name)) {
  5734. return this.tsParseModuleOrNamespaceDeclaration(node);
  5735. }
  5736. }
  5737. break;
  5738. case "namespace":
  5739. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5740. return this.tsParseModuleOrNamespaceDeclaration(node);
  5741. }
  5742. break;
  5743. case "type":
  5744. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  5745. return this.tsParseTypeAliasDeclaration(node);
  5746. }
  5747. break;
  5748. }
  5749. }
  5750. tsCheckLineTerminator(next) {
  5751. if (next) {
  5752. if (this.hasFollowingLineBreak()) return false;
  5753. this.next();
  5754. return true;
  5755. }
  5756. return !this.isLineTerminator();
  5757. }
  5758. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  5759. if (!this.isRelational("<")) {
  5760. return undefined;
  5761. }
  5762. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  5763. this.state.maybeInArrowParameters = true;
  5764. const res = this.tsTryParseAndCatch(() => {
  5765. const node = this.startNodeAt(startPos, startLoc);
  5766. node.typeParameters = this.tsParseTypeParameters();
  5767. super.parseFunctionParams(node);
  5768. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  5769. this.expect(types$1.arrow);
  5770. return node;
  5771. });
  5772. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  5773. if (!res) {
  5774. return undefined;
  5775. }
  5776. return this.parseArrowExpression(res, null, true);
  5777. }
  5778. tsParseTypeArguments() {
  5779. const node = this.startNode();
  5780. node.params = this.tsInType(() => this.tsInNoContext(() => {
  5781. this.expectRelational("<");
  5782. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  5783. }));
  5784. if (node.params.length === 0) {
  5785. this.raise(node.start, TSErrors.EmptyTypeArguments);
  5786. }
  5787. this.expectRelational(">");
  5788. return this.finishNode(node, "TSTypeParameterInstantiation");
  5789. }
  5790. tsIsDeclarationStart() {
  5791. if (this.match(types$1.name)) {
  5792. switch (this.state.value) {
  5793. case "abstract":
  5794. case "declare":
  5795. case "enum":
  5796. case "interface":
  5797. case "module":
  5798. case "namespace":
  5799. case "type":
  5800. return true;
  5801. }
  5802. }
  5803. return false;
  5804. }
  5805. isExportDefaultSpecifier() {
  5806. if (this.tsIsDeclarationStart()) return false;
  5807. return super.isExportDefaultSpecifier();
  5808. }
  5809. parseAssignableListItem(allowModifiers, decorators) {
  5810. const startPos = this.state.start;
  5811. const startLoc = this.state.startLoc;
  5812. let accessibility;
  5813. let readonly = false;
  5814. if (allowModifiers !== undefined) {
  5815. accessibility = this.parseAccessModifier();
  5816. readonly = !!this.tsParseModifier(["readonly"]);
  5817. if (allowModifiers === false && (accessibility || readonly)) {
  5818. this.raise(startPos, TSErrors.UnexpectedParameterModifier);
  5819. }
  5820. }
  5821. const left = this.parseMaybeDefault();
  5822. this.parseAssignableListItemTypes(left);
  5823. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  5824. if (accessibility || readonly) {
  5825. const pp = this.startNodeAt(startPos, startLoc);
  5826. if (decorators.length) {
  5827. pp.decorators = decorators;
  5828. }
  5829. if (accessibility) pp.accessibility = accessibility;
  5830. if (readonly) pp.readonly = readonly;
  5831. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  5832. this.raise(pp.start, TSErrors.UnsupportedParameterPropertyKind);
  5833. }
  5834. pp.parameter = elt;
  5835. return this.finishNode(pp, "TSParameterProperty");
  5836. }
  5837. if (decorators.length) {
  5838. left.decorators = decorators;
  5839. }
  5840. return elt;
  5841. }
  5842. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5843. if (this.match(types$1.colon)) {
  5844. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  5845. }
  5846. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  5847. if (bodilessType && !this.match(types$1.braceL) && this.isLineTerminator()) {
  5848. this.finishNode(node, bodilessType);
  5849. return;
  5850. }
  5851. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  5852. this.raise(node.start, TSErrors.DeclareFunctionHasImplementation);
  5853. if (node.declare) {
  5854. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  5855. return;
  5856. }
  5857. }
  5858. super.parseFunctionBodyAndFinish(node, type, isMethod);
  5859. }
  5860. registerFunctionStatementId(node) {
  5861. if (!node.body && node.id) {
  5862. this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
  5863. } else {
  5864. super.registerFunctionStatementId(...arguments);
  5865. }
  5866. }
  5867. tsCheckForInvalidTypeCasts(items) {
  5868. items.forEach(node => {
  5869. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  5870. this.raise(node.typeAnnotation.start, TSErrors.UnexpectedTypeAnnotation);
  5871. }
  5872. });
  5873. }
  5874. toReferencedList(exprList, isInParens) {
  5875. this.tsCheckForInvalidTypeCasts(exprList);
  5876. return exprList;
  5877. }
  5878. parseArrayLike(...args) {
  5879. const node = super.parseArrayLike(...args);
  5880. if (node.type === "ArrayExpression") {
  5881. this.tsCheckForInvalidTypeCasts(node.elements);
  5882. }
  5883. return node;
  5884. }
  5885. parseSubscript(base, startPos, startLoc, noCalls, state) {
  5886. if (!this.hasPrecedingLineBreak() && this.match(types$1.bang)) {
  5887. this.next();
  5888. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  5889. nonNullExpression.expression = base;
  5890. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  5891. }
  5892. if (this.isRelational("<")) {
  5893. const result = this.tsTryParseAndCatch(() => {
  5894. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  5895. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  5896. if (asyncArrowFn) {
  5897. return asyncArrowFn;
  5898. }
  5899. }
  5900. const node = this.startNodeAt(startPos, startLoc);
  5901. node.callee = base;
  5902. const typeArguments = this.tsParseTypeArguments();
  5903. if (typeArguments) {
  5904. if (!noCalls && this.eat(types$1.parenL)) {
  5905. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  5906. this.tsCheckForInvalidTypeCasts(node.arguments);
  5907. node.typeParameters = typeArguments;
  5908. if (state.optionalChainMember) {
  5909. node.optional = false;
  5910. }
  5911. return this.finishCallExpression(node, state.optionalChainMember);
  5912. } else if (this.match(types$1.backQuote)) {
  5913. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  5914. result.typeParameters = typeArguments;
  5915. return result;
  5916. }
  5917. }
  5918. this.unexpected();
  5919. });
  5920. if (result) return result;
  5921. }
  5922. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  5923. }
  5924. parseNewArguments(node) {
  5925. if (this.isRelational("<")) {
  5926. const typeParameters = this.tsTryParseAndCatch(() => {
  5927. const args = this.tsParseTypeArguments();
  5928. if (!this.match(types$1.parenL)) this.unexpected();
  5929. return args;
  5930. });
  5931. if (typeParameters) {
  5932. node.typeParameters = typeParameters;
  5933. }
  5934. }
  5935. super.parseNewArguments(node);
  5936. }
  5937. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  5938. if (nonNull(types$1._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  5939. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  5940. node.expression = left;
  5941. const _const = this.tsTryNextParseConstantContext();
  5942. if (_const) {
  5943. node.typeAnnotation = _const;
  5944. } else {
  5945. node.typeAnnotation = this.tsNextThenParseType();
  5946. }
  5947. this.finishNode(node, "TSAsExpression");
  5948. this.reScan_lt_gt();
  5949. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  5950. }
  5951. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  5952. }
  5953. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  5954. checkDuplicateExports() {}
  5955. parseImport(node) {
  5956. node.importKind = "value";
  5957. if (this.match(types$1.name) || this.match(types$1.star) || this.match(types$1.braceL)) {
  5958. let ahead = this.lookahead();
  5959. if (this.isContextual("type") && ahead.type !== types$1.comma && !(ahead.type === types$1.name && ahead.value === "from") && ahead.type !== types$1.eq) {
  5960. node.importKind = "type";
  5961. this.next();
  5962. ahead = this.lookahead();
  5963. }
  5964. if (this.match(types$1.name) && ahead.type === types$1.eq) {
  5965. return this.tsParseImportEqualsDeclaration(node);
  5966. }
  5967. }
  5968. const importNode = super.parseImport(node);
  5969. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  5970. this.raise(importNode.start, TSErrors.TypeImportCannotSpecifyDefaultAndNamed);
  5971. }
  5972. return importNode;
  5973. }
  5974. parseExport(node) {
  5975. if (this.match(types$1._import)) {
  5976. this.next();
  5977. if (this.isContextual("type") && this.lookaheadCharCode() !== 61) {
  5978. node.importKind = "type";
  5979. this.next();
  5980. } else {
  5981. node.importKind = "value";
  5982. }
  5983. return this.tsParseImportEqualsDeclaration(node, true);
  5984. } else if (this.eat(types$1.eq)) {
  5985. const assign = node;
  5986. assign.expression = this.parseExpression();
  5987. this.semicolon();
  5988. return this.finishNode(assign, "TSExportAssignment");
  5989. } else if (this.eatContextual("as")) {
  5990. const decl = node;
  5991. this.expectContextual("namespace");
  5992. decl.id = this.parseIdentifier();
  5993. this.semicolon();
  5994. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  5995. } else {
  5996. if (this.isContextual("type") && this.lookahead().type === types$1.braceL) {
  5997. this.next();
  5998. node.exportKind = "type";
  5999. } else {
  6000. node.exportKind = "value";
  6001. }
  6002. return super.parseExport(node);
  6003. }
  6004. }
  6005. isAbstractClass() {
  6006. return this.isContextual("abstract") && this.lookahead().type === types$1._class;
  6007. }
  6008. parseExportDefaultExpression() {
  6009. if (this.isAbstractClass()) {
  6010. const cls = this.startNode();
  6011. this.next();
  6012. cls.abstract = true;
  6013. this.parseClass(cls, true, true);
  6014. return cls;
  6015. }
  6016. if (this.state.value === "interface") {
  6017. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  6018. if (result) return result;
  6019. }
  6020. return super.parseExportDefaultExpression();
  6021. }
  6022. parseStatementContent(context, topLevel) {
  6023. if (this.state.type === types$1._const) {
  6024. const ahead = this.lookahead();
  6025. if (ahead.type === types$1.name && ahead.value === "enum") {
  6026. const node = this.startNode();
  6027. this.expect(types$1._const);
  6028. this.expectContextual("enum");
  6029. return this.tsParseEnumDeclaration(node, true);
  6030. }
  6031. }
  6032. return super.parseStatementContent(context, topLevel);
  6033. }
  6034. parseAccessModifier() {
  6035. return this.tsParseModifier(["public", "protected", "private"]);
  6036. }
  6037. tsHasSomeModifiers(member, modifiers) {
  6038. return modifiers.some(modifier => {
  6039. if (tsIsAccessModifier(modifier)) {
  6040. return member.accessibility === modifier;
  6041. }
  6042. return !!member[modifier];
  6043. });
  6044. }
  6045. parseClassMember(classBody, member, state) {
  6046. const invalidModifersForStaticBlocks = ["declare", "private", "public", "protected", "override", "abstract", "readonly"];
  6047. this.tsParseModifiers(member, invalidModifersForStaticBlocks.concat(["static"]));
  6048. const callParseClassMemberWithIsStatic = () => {
  6049. const isStatic = !!member.static;
  6050. if (isStatic && this.eat(types$1.braceL)) {
  6051. if (this.tsHasSomeModifiers(member, invalidModifersForStaticBlocks)) {
  6052. this.raise(this.state.pos, TSErrors.StaticBlockCannotHaveModifier);
  6053. }
  6054. this.parseClassStaticBlock(classBody, member);
  6055. } else {
  6056. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6057. }
  6058. };
  6059. if (member.declare) {
  6060. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  6061. } else {
  6062. callParseClassMemberWithIsStatic();
  6063. }
  6064. }
  6065. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  6066. const idx = this.tsTryParseIndexSignature(member);
  6067. if (idx) {
  6068. classBody.body.push(idx);
  6069. if (member.abstract) {
  6070. this.raise(member.start, TSErrors.IndexSignatureHasAbstract);
  6071. }
  6072. if (member.accessibility) {
  6073. this.raise(member.start, TSErrors.IndexSignatureHasAccessibility, member.accessibility);
  6074. }
  6075. if (member.declare) {
  6076. this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
  6077. }
  6078. if (member.override) {
  6079. this.raise(member.start, TSErrors.IndexSignatureHasOverride);
  6080. }
  6081. return;
  6082. }
  6083. if (!this.state.inAbstractClass && member.abstract) {
  6084. this.raise(member.start, TSErrors.NonAbstractClassHasAbstractMethod);
  6085. }
  6086. if (member.override) {
  6087. if (!state.hadSuperClass) {
  6088. this.raise(member.start, TSErrors.OverrideNotInSubClass);
  6089. }
  6090. }
  6091. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  6092. }
  6093. parsePostMemberNameModifiers(methodOrProp) {
  6094. const optional = this.eat(types$1.question);
  6095. if (optional) methodOrProp.optional = true;
  6096. if (methodOrProp.readonly && this.match(types$1.parenL)) {
  6097. this.raise(methodOrProp.start, TSErrors.ClassMethodHasReadonly);
  6098. }
  6099. if (methodOrProp.declare && this.match(types$1.parenL)) {
  6100. this.raise(methodOrProp.start, TSErrors.ClassMethodHasDeclare);
  6101. }
  6102. }
  6103. parseExpressionStatement(node, expr) {
  6104. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  6105. return decl || super.parseExpressionStatement(node, expr);
  6106. }
  6107. shouldParseExportDeclaration() {
  6108. if (this.tsIsDeclarationStart()) return true;
  6109. return super.shouldParseExportDeclaration();
  6110. }
  6111. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  6112. if (!refNeedsArrowPos || !this.match(types$1.question)) {
  6113. return super.parseConditional(expr, startPos, startLoc, refNeedsArrowPos);
  6114. }
  6115. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  6116. if (!result.node) {
  6117. refNeedsArrowPos.start = result.error.pos || this.state.start;
  6118. return expr;
  6119. }
  6120. if (result.error) this.state = result.failState;
  6121. return result.node;
  6122. }
  6123. parseParenItem(node, startPos, startLoc) {
  6124. node = super.parseParenItem(node, startPos, startLoc);
  6125. if (this.eat(types$1.question)) {
  6126. node.optional = true;
  6127. this.resetEndLocation(node);
  6128. }
  6129. if (this.match(types$1.colon)) {
  6130. const typeCastNode = this.startNodeAt(startPos, startLoc);
  6131. typeCastNode.expression = node;
  6132. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  6133. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  6134. }
  6135. return node;
  6136. }
  6137. parseExportDeclaration(node) {
  6138. const startPos = this.state.start;
  6139. const startLoc = this.state.startLoc;
  6140. const isDeclare = this.eatContextual("declare");
  6141. if (isDeclare && (this.isContextual("declare") || !this.shouldParseExportDeclaration())) {
  6142. throw this.raise(this.state.start, TSErrors.ExpectedAmbientAfterExportDeclare);
  6143. }
  6144. let declaration;
  6145. if (this.match(types$1.name)) {
  6146. declaration = this.tsTryParseExportDeclaration();
  6147. }
  6148. if (!declaration) {
  6149. declaration = super.parseExportDeclaration(node);
  6150. }
  6151. if (declaration && (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare)) {
  6152. node.exportKind = "type";
  6153. }
  6154. if (declaration && isDeclare) {
  6155. this.resetStartLocation(declaration, startPos, startLoc);
  6156. declaration.declare = true;
  6157. }
  6158. return declaration;
  6159. }
  6160. parseClassId(node, isStatement, optionalId) {
  6161. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  6162. return;
  6163. }
  6164. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  6165. const typeParameters = this.tsTryParseTypeParameters();
  6166. if (typeParameters) node.typeParameters = typeParameters;
  6167. }
  6168. parseClassPropertyAnnotation(node) {
  6169. if (!node.optional && this.eat(types$1.bang)) {
  6170. node.definite = true;
  6171. }
  6172. const type = this.tsTryParseTypeAnnotation();
  6173. if (type) node.typeAnnotation = type;
  6174. }
  6175. parseClassProperty(node) {
  6176. this.parseClassPropertyAnnotation(node);
  6177. if (this.state.isAmbientContext && this.match(types$1.eq)) {
  6178. this.raise(this.state.start, TSErrors.DeclareClassFieldHasInitializer);
  6179. }
  6180. return super.parseClassProperty(node);
  6181. }
  6182. parseClassPrivateProperty(node) {
  6183. if (node.abstract) {
  6184. this.raise(node.start, TSErrors.PrivateElementHasAbstract);
  6185. }
  6186. if (node.accessibility) {
  6187. this.raise(node.start, TSErrors.PrivateElementHasAccessibility, node.accessibility);
  6188. }
  6189. this.parseClassPropertyAnnotation(node);
  6190. return super.parseClassPrivateProperty(node);
  6191. }
  6192. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  6193. const typeParameters = this.tsTryParseTypeParameters();
  6194. if (typeParameters && isConstructor) {
  6195. this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
  6196. }
  6197. if (method.declare && (method.kind === "get" || method.kind === "set")) {
  6198. this.raise(method.start, TSErrors.DeclareAccessor, method.kind);
  6199. }
  6200. if (typeParameters) method.typeParameters = typeParameters;
  6201. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  6202. }
  6203. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  6204. const typeParameters = this.tsTryParseTypeParameters();
  6205. if (typeParameters) method.typeParameters = typeParameters;
  6206. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  6207. }
  6208. parseClassSuper(node) {
  6209. super.parseClassSuper(node);
  6210. if (node.superClass && this.isRelational("<")) {
  6211. node.superTypeParameters = this.tsParseTypeArguments();
  6212. }
  6213. if (this.eatContextual("implements")) {
  6214. node.implements = this.tsParseHeritageClause("implements");
  6215. }
  6216. }
  6217. parseObjPropValue(prop, ...args) {
  6218. const typeParameters = this.tsTryParseTypeParameters();
  6219. if (typeParameters) prop.typeParameters = typeParameters;
  6220. super.parseObjPropValue(prop, ...args);
  6221. }
  6222. parseFunctionParams(node, allowModifiers) {
  6223. const typeParameters = this.tsTryParseTypeParameters();
  6224. if (typeParameters) node.typeParameters = typeParameters;
  6225. super.parseFunctionParams(node, allowModifiers);
  6226. }
  6227. parseVarId(decl, kind) {
  6228. super.parseVarId(decl, kind);
  6229. if (decl.id.type === "Identifier" && this.eat(types$1.bang)) {
  6230. decl.definite = true;
  6231. }
  6232. const type = this.tsTryParseTypeAnnotation();
  6233. if (type) {
  6234. decl.id.typeAnnotation = type;
  6235. this.resetEndLocation(decl.id);
  6236. }
  6237. }
  6238. parseAsyncArrowFromCallExpression(node, call) {
  6239. if (this.match(types$1.colon)) {
  6240. node.returnType = this.tsParseTypeAnnotation();
  6241. }
  6242. return super.parseAsyncArrowFromCallExpression(node, call);
  6243. }
  6244. parseMaybeAssign(...args) {
  6245. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  6246. let state;
  6247. let jsx;
  6248. let typeCast;
  6249. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  6250. state = this.state.clone();
  6251. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6252. if (!jsx.error) return jsx.node;
  6253. const {
  6254. context
  6255. } = this.state;
  6256. if (context[context.length - 1] === types.j_oTag) {
  6257. context.length -= 2;
  6258. } else if (context[context.length - 1] === types.j_expr) {
  6259. context.length -= 1;
  6260. }
  6261. }
  6262. if (!((_jsx = jsx) != null && _jsx.error) && !this.isRelational("<")) {
  6263. return super.parseMaybeAssign(...args);
  6264. }
  6265. let typeParameters;
  6266. state = state || this.state.clone();
  6267. const arrow = this.tryParse(abort => {
  6268. var _expr$extra, _typeParameters;
  6269. typeParameters = this.tsParseTypeParameters();
  6270. const expr = super.parseMaybeAssign(...args);
  6271. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  6272. abort();
  6273. }
  6274. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  6275. this.resetStartLocationFromNode(expr, typeParameters);
  6276. }
  6277. expr.typeParameters = typeParameters;
  6278. return expr;
  6279. }, state);
  6280. if (!arrow.error && !arrow.aborted) return arrow.node;
  6281. if (!jsx) {
  6282. assert(!this.hasPlugin("jsx"));
  6283. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  6284. if (!typeCast.error) return typeCast.node;
  6285. }
  6286. if ((_jsx2 = jsx) != null && _jsx2.node) {
  6287. this.state = jsx.failState;
  6288. return jsx.node;
  6289. }
  6290. if (arrow.node) {
  6291. this.state = arrow.failState;
  6292. return arrow.node;
  6293. }
  6294. if ((_typeCast = typeCast) != null && _typeCast.node) {
  6295. this.state = typeCast.failState;
  6296. return typeCast.node;
  6297. }
  6298. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  6299. if (arrow.thrown) throw arrow.error;
  6300. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  6301. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  6302. }
  6303. parseMaybeUnary(refExpressionErrors) {
  6304. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  6305. return this.tsParseTypeAssertion();
  6306. } else {
  6307. return super.parseMaybeUnary(refExpressionErrors);
  6308. }
  6309. }
  6310. parseArrow(node) {
  6311. if (this.match(types$1.colon)) {
  6312. const result = this.tryParse(abort => {
  6313. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  6314. if (this.canInsertSemicolon() || !this.match(types$1.arrow)) abort();
  6315. return returnType;
  6316. });
  6317. if (result.aborted) return;
  6318. if (!result.thrown) {
  6319. if (result.error) this.state = result.failState;
  6320. node.returnType = result.node;
  6321. }
  6322. }
  6323. return super.parseArrow(node);
  6324. }
  6325. parseAssignableListItemTypes(param) {
  6326. if (this.eat(types$1.question)) {
  6327. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  6328. this.raise(param.start, TSErrors.PatternIsOptional);
  6329. }
  6330. param.optional = true;
  6331. }
  6332. const type = this.tsTryParseTypeAnnotation();
  6333. if (type) param.typeAnnotation = type;
  6334. this.resetEndLocation(param);
  6335. return param;
  6336. }
  6337. toAssignable(node, isLHS = false) {
  6338. switch (node.type) {
  6339. case "TSTypeCastExpression":
  6340. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  6341. case "TSParameterProperty":
  6342. return super.toAssignable(node, isLHS);
  6343. case "ParenthesizedExpression":
  6344. return this.toAssignableParenthesizedExpression(node, isLHS);
  6345. case "TSAsExpression":
  6346. case "TSNonNullExpression":
  6347. case "TSTypeAssertion":
  6348. node.expression = this.toAssignable(node.expression, isLHS);
  6349. return node;
  6350. default:
  6351. return super.toAssignable(node, isLHS);
  6352. }
  6353. }
  6354. toAssignableParenthesizedExpression(node, isLHS) {
  6355. switch (node.expression.type) {
  6356. case "TSAsExpression":
  6357. case "TSNonNullExpression":
  6358. case "TSTypeAssertion":
  6359. case "ParenthesizedExpression":
  6360. node.expression = this.toAssignable(node.expression, isLHS);
  6361. return node;
  6362. default:
  6363. return super.toAssignable(node, isLHS);
  6364. }
  6365. }
  6366. checkLVal(expr, contextDescription, ...args) {
  6367. var _expr$extra2;
  6368. switch (expr.type) {
  6369. case "TSTypeCastExpression":
  6370. return;
  6371. case "TSParameterProperty":
  6372. this.checkLVal(expr.parameter, "parameter property", ...args);
  6373. return;
  6374. case "TSAsExpression":
  6375. case "TSTypeAssertion":
  6376. if (!args[0] && contextDescription !== "parenthesized expression" && !((_expr$extra2 = expr.extra) != null && _expr$extra2.parenthesized)) {
  6377. this.raise(expr.start, ErrorMessages.InvalidLhs, contextDescription);
  6378. break;
  6379. }
  6380. this.checkLVal(expr.expression, "parenthesized expression", ...args);
  6381. return;
  6382. case "TSNonNullExpression":
  6383. this.checkLVal(expr.expression, contextDescription, ...args);
  6384. return;
  6385. default:
  6386. super.checkLVal(expr, contextDescription, ...args);
  6387. return;
  6388. }
  6389. }
  6390. parseBindingAtom() {
  6391. switch (this.state.type) {
  6392. case types$1._this:
  6393. return this.parseIdentifier(true);
  6394. default:
  6395. return super.parseBindingAtom();
  6396. }
  6397. }
  6398. parseMaybeDecoratorArguments(expr) {
  6399. if (this.isRelational("<")) {
  6400. const typeArguments = this.tsParseTypeArguments();
  6401. if (this.match(types$1.parenL)) {
  6402. const call = super.parseMaybeDecoratorArguments(expr);
  6403. call.typeParameters = typeArguments;
  6404. return call;
  6405. }
  6406. this.unexpected(this.state.start, types$1.parenL);
  6407. }
  6408. return super.parseMaybeDecoratorArguments(expr);
  6409. }
  6410. checkCommaAfterRest(close) {
  6411. if (this.state.isAmbientContext && this.match(types$1.comma) && this.lookaheadCharCode() === close) {
  6412. this.next();
  6413. } else {
  6414. super.checkCommaAfterRest(close);
  6415. }
  6416. }
  6417. isClassMethod() {
  6418. return this.isRelational("<") || super.isClassMethod();
  6419. }
  6420. isClassProperty() {
  6421. return this.match(types$1.bang) || this.match(types$1.colon) || super.isClassProperty();
  6422. }
  6423. parseMaybeDefault(...args) {
  6424. const node = super.parseMaybeDefault(...args);
  6425. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  6426. this.raise(node.typeAnnotation.start, TSErrors.TypeAnnotationAfterAssign);
  6427. }
  6428. return node;
  6429. }
  6430. getTokenFromCode(code) {
  6431. if (this.state.inType && (code === 62 || code === 60)) {
  6432. return this.finishOp(types$1.relational, 1);
  6433. } else {
  6434. return super.getTokenFromCode(code);
  6435. }
  6436. }
  6437. reScan_lt_gt() {
  6438. if (this.match(types$1.relational)) {
  6439. const code = this.input.charCodeAt(this.state.start);
  6440. if (code === 60 || code === 62) {
  6441. this.state.pos -= 1;
  6442. this.readToken_lt_gt(code);
  6443. }
  6444. }
  6445. }
  6446. toAssignableList(exprList) {
  6447. for (let i = 0; i < exprList.length; i++) {
  6448. const expr = exprList[i];
  6449. if (!expr) continue;
  6450. switch (expr.type) {
  6451. case "TSTypeCastExpression":
  6452. exprList[i] = this.typeCastToParameter(expr);
  6453. break;
  6454. case "TSAsExpression":
  6455. case "TSTypeAssertion":
  6456. if (!this.state.maybeInArrowParameters) {
  6457. exprList[i] = this.typeCastToParameter(expr);
  6458. } else {
  6459. this.raise(expr.start, TSErrors.UnexpectedTypeCastInParameter);
  6460. }
  6461. break;
  6462. }
  6463. }
  6464. return super.toAssignableList(...arguments);
  6465. }
  6466. typeCastToParameter(node) {
  6467. node.expression.typeAnnotation = node.typeAnnotation;
  6468. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  6469. return node.expression;
  6470. }
  6471. shouldParseArrow() {
  6472. return this.match(types$1.colon) || super.shouldParseArrow();
  6473. }
  6474. shouldParseAsyncArrow() {
  6475. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  6476. }
  6477. canHaveLeadingDecorator() {
  6478. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  6479. }
  6480. jsxParseOpeningElementAfterName(node) {
  6481. if (this.isRelational("<")) {
  6482. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  6483. if (typeArguments) node.typeParameters = typeArguments;
  6484. }
  6485. return super.jsxParseOpeningElementAfterName(node);
  6486. }
  6487. getGetterSetterExpectedParamCount(method) {
  6488. const baseCount = super.getGetterSetterExpectedParamCount(method);
  6489. const params = this.getObjectOrClassMethodParams(method);
  6490. const firstParam = params[0];
  6491. const hasContextParam = firstParam && this.isThisParam(firstParam);
  6492. return hasContextParam ? baseCount + 1 : baseCount;
  6493. }
  6494. parseCatchClauseParam() {
  6495. const param = super.parseCatchClauseParam();
  6496. const type = this.tsTryParseTypeAnnotation();
  6497. if (type) {
  6498. param.typeAnnotation = type;
  6499. this.resetEndLocation(param);
  6500. }
  6501. return param;
  6502. }
  6503. tsInAmbientContext(cb) {
  6504. const oldIsAmbientContext = this.state.isAmbientContext;
  6505. this.state.isAmbientContext = true;
  6506. try {
  6507. return cb();
  6508. } finally {
  6509. this.state.isAmbientContext = oldIsAmbientContext;
  6510. }
  6511. }
  6512. parseClass(node, ...args) {
  6513. const oldInAbstractClass = this.state.inAbstractClass;
  6514. this.state.inAbstractClass = !!node.abstract;
  6515. try {
  6516. return super.parseClass(node, ...args);
  6517. } finally {
  6518. this.state.inAbstractClass = oldInAbstractClass;
  6519. }
  6520. }
  6521. tsParseAbstractDeclaration(node) {
  6522. if (this.match(types$1._class)) {
  6523. node.abstract = true;
  6524. return this.parseClass(node, true, false);
  6525. } else if (this.isContextual("interface")) {
  6526. if (!this.hasFollowingLineBreak()) {
  6527. node.abstract = true;
  6528. this.raise(node.start, TSErrors.NonClassMethodPropertyHasAbstractModifer);
  6529. this.next();
  6530. return this.tsParseInterfaceDeclaration(node);
  6531. }
  6532. } else {
  6533. this.unexpected(null, types$1._class);
  6534. }
  6535. }
  6536. parseMethod(...args) {
  6537. const method = super.parseMethod(...args);
  6538. if (method.abstract) {
  6539. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  6540. if (hasBody) {
  6541. const {
  6542. key
  6543. } = method;
  6544. this.raise(method.start, TSErrors.AbstractMethodHasImplementation, key.type === "Identifier" ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  6545. }
  6546. }
  6547. return method;
  6548. }
  6549. shouldParseAsAmbientContext() {
  6550. return !!this.getPluginOption("typescript", "dts");
  6551. }
  6552. parse() {
  6553. if (this.shouldParseAsAmbientContext()) {
  6554. this.state.isAmbientContext = true;
  6555. }
  6556. return super.parse();
  6557. }
  6558. getExpression() {
  6559. if (this.shouldParseAsAmbientContext()) {
  6560. this.state.isAmbientContext = true;
  6561. }
  6562. return super.getExpression();
  6563. }
  6564. });
  6565. types$1.placeholder = new TokenType("%%", {
  6566. startsExpr: true
  6567. });
  6568. const PlaceHolderErrors = makeErrorTemplates({
  6569. ClassNameIsRequired: "A class name is required."
  6570. }, ErrorCodes.SyntaxError);
  6571. var placeholders = (superClass => class extends superClass {
  6572. parsePlaceholder(expectedNode) {
  6573. if (this.match(types$1.placeholder)) {
  6574. const node = this.startNode();
  6575. this.next();
  6576. this.assertNoSpace("Unexpected space in placeholder.");
  6577. node.name = super.parseIdentifier(true);
  6578. this.assertNoSpace("Unexpected space in placeholder.");
  6579. this.expect(types$1.placeholder);
  6580. return this.finishPlaceholder(node, expectedNode);
  6581. }
  6582. }
  6583. finishPlaceholder(node, expectedNode) {
  6584. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  6585. node.expectedNode = expectedNode;
  6586. return isFinished ? node : this.finishNode(node, "Placeholder");
  6587. }
  6588. getTokenFromCode(code) {
  6589. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  6590. return this.finishOp(types$1.placeholder, 2);
  6591. }
  6592. return super.getTokenFromCode(...arguments);
  6593. }
  6594. parseExprAtom() {
  6595. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  6596. }
  6597. parseIdentifier() {
  6598. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  6599. }
  6600. checkReservedWord(word) {
  6601. if (word !== undefined) super.checkReservedWord(...arguments);
  6602. }
  6603. parseBindingAtom() {
  6604. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  6605. }
  6606. checkLVal(expr) {
  6607. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  6608. }
  6609. toAssignable(node) {
  6610. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  6611. node.expectedNode = "Pattern";
  6612. return node;
  6613. }
  6614. return super.toAssignable(...arguments);
  6615. }
  6616. isLet(context) {
  6617. if (super.isLet(context)) {
  6618. return true;
  6619. }
  6620. if (!this.isContextual("let")) {
  6621. return false;
  6622. }
  6623. if (context) return false;
  6624. const nextToken = this.lookahead();
  6625. if (nextToken.type === types$1.placeholder) {
  6626. return true;
  6627. }
  6628. return false;
  6629. }
  6630. verifyBreakContinue(node) {
  6631. if (node.label && node.label.type === "Placeholder") return;
  6632. super.verifyBreakContinue(...arguments);
  6633. }
  6634. parseExpressionStatement(node, expr) {
  6635. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  6636. return super.parseExpressionStatement(...arguments);
  6637. }
  6638. if (this.match(types$1.colon)) {
  6639. const stmt = node;
  6640. stmt.label = this.finishPlaceholder(expr, "Identifier");
  6641. this.next();
  6642. stmt.body = this.parseStatement("label");
  6643. return this.finishNode(stmt, "LabeledStatement");
  6644. }
  6645. this.semicolon();
  6646. node.name = expr.name;
  6647. return this.finishPlaceholder(node, "Statement");
  6648. }
  6649. parseBlock() {
  6650. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  6651. }
  6652. parseFunctionId() {
  6653. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  6654. }
  6655. parseClass(node, isStatement, optionalId) {
  6656. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  6657. this.next();
  6658. this.takeDecorators(node);
  6659. const oldStrict = this.state.strict;
  6660. const placeholder = this.parsePlaceholder("Identifier");
  6661. if (placeholder) {
  6662. if (this.match(types$1._extends) || this.match(types$1.placeholder) || this.match(types$1.braceL)) {
  6663. node.id = placeholder;
  6664. } else if (optionalId || !isStatement) {
  6665. node.id = null;
  6666. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  6667. return this.finishNode(node, type);
  6668. } else {
  6669. this.unexpected(null, PlaceHolderErrors.ClassNameIsRequired);
  6670. }
  6671. } else {
  6672. this.parseClassId(node, isStatement, optionalId);
  6673. }
  6674. this.parseClassSuper(node);
  6675. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  6676. return this.finishNode(node, type);
  6677. }
  6678. parseExport(node) {
  6679. const placeholder = this.parsePlaceholder("Identifier");
  6680. if (!placeholder) return super.parseExport(...arguments);
  6681. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6682. node.specifiers = [];
  6683. node.source = null;
  6684. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  6685. return this.finishNode(node, "ExportNamedDeclaration");
  6686. }
  6687. this.expectPlugin("exportDefaultFrom");
  6688. const specifier = this.startNode();
  6689. specifier.exported = placeholder;
  6690. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  6691. return super.parseExport(node);
  6692. }
  6693. isExportDefaultSpecifier() {
  6694. if (this.match(types$1._default)) {
  6695. const next = this.nextTokenStart();
  6696. if (this.isUnparsedContextual(next, "from")) {
  6697. if (this.input.startsWith(types$1.placeholder.label, this.nextTokenStartSince(next + 4))) {
  6698. return true;
  6699. }
  6700. }
  6701. }
  6702. return super.isExportDefaultSpecifier();
  6703. }
  6704. maybeParseExportDefaultSpecifier(node) {
  6705. if (node.specifiers && node.specifiers.length > 0) {
  6706. return true;
  6707. }
  6708. return super.maybeParseExportDefaultSpecifier(...arguments);
  6709. }
  6710. checkExport(node) {
  6711. const {
  6712. specifiers
  6713. } = node;
  6714. if (specifiers != null && specifiers.length) {
  6715. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  6716. }
  6717. super.checkExport(node);
  6718. node.specifiers = specifiers;
  6719. }
  6720. parseImport(node) {
  6721. const placeholder = this.parsePlaceholder("Identifier");
  6722. if (!placeholder) return super.parseImport(...arguments);
  6723. node.specifiers = [];
  6724. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  6725. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  6726. this.semicolon();
  6727. return this.finishNode(node, "ImportDeclaration");
  6728. }
  6729. const specifier = this.startNodeAtNode(placeholder);
  6730. specifier.local = placeholder;
  6731. this.finishNode(specifier, "ImportDefaultSpecifier");
  6732. node.specifiers.push(specifier);
  6733. if (this.eat(types$1.comma)) {
  6734. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  6735. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  6736. }
  6737. this.expectContextual("from");
  6738. node.source = this.parseImportSource();
  6739. this.semicolon();
  6740. return this.finishNode(node, "ImportDeclaration");
  6741. }
  6742. parseImportSource() {
  6743. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  6744. }
  6745. });
  6746. var v8intrinsic = (superClass => class extends superClass {
  6747. parseV8Intrinsic() {
  6748. if (this.match(types$1.modulo)) {
  6749. const v8IntrinsicStart = this.state.start;
  6750. const node = this.startNode();
  6751. this.eat(types$1.modulo);
  6752. if (this.match(types$1.name)) {
  6753. const name = this.parseIdentifierName(this.state.start);
  6754. const identifier = this.createIdentifier(node, name);
  6755. identifier.type = "V8IntrinsicIdentifier";
  6756. if (this.match(types$1.parenL)) {
  6757. return identifier;
  6758. }
  6759. }
  6760. this.unexpected(v8IntrinsicStart);
  6761. }
  6762. }
  6763. parseExprAtom() {
  6764. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  6765. }
  6766. });
  6767. function hasPlugin(plugins, name) {
  6768. return plugins.some(plugin => {
  6769. if (Array.isArray(plugin)) {
  6770. return plugin[0] === name;
  6771. } else {
  6772. return plugin === name;
  6773. }
  6774. });
  6775. }
  6776. function getPluginOption(plugins, name, option) {
  6777. const plugin = plugins.find(plugin => {
  6778. if (Array.isArray(plugin)) {
  6779. return plugin[0] === name;
  6780. } else {
  6781. return plugin === name;
  6782. }
  6783. });
  6784. if (plugin && Array.isArray(plugin)) {
  6785. return plugin[1][option];
  6786. }
  6787. return null;
  6788. }
  6789. const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
  6790. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  6791. function validatePlugins(plugins) {
  6792. if (hasPlugin(plugins, "decorators")) {
  6793. if (hasPlugin(plugins, "decorators-legacy")) {
  6794. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  6795. }
  6796. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  6797. if (decoratorsBeforeExport == null) {
  6798. 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'.");
  6799. } else if (typeof decoratorsBeforeExport !== "boolean") {
  6800. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  6801. }
  6802. }
  6803. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  6804. throw new Error("Cannot combine flow and typescript plugins.");
  6805. }
  6806. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  6807. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  6808. }
  6809. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  6810. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  6811. }
  6812. if (hasPlugin(plugins, "moduleAttributes")) {
  6813. {
  6814. if (hasPlugin(plugins, "importAssertions")) {
  6815. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  6816. }
  6817. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  6818. if (moduleAttributesVerionPluginOption !== "may-2020") {
  6819. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  6820. }
  6821. }
  6822. }
  6823. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  6824. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  6825. }
  6826. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  6827. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  6828. error.missingPlugins = "doExpressions";
  6829. throw error;
  6830. }
  6831. }
  6832. const mixinPlugins = {
  6833. estree,
  6834. jsx,
  6835. flow,
  6836. typescript,
  6837. v8intrinsic,
  6838. placeholders
  6839. };
  6840. const mixinPluginNames = Object.keys(mixinPlugins);
  6841. const defaultOptions = {
  6842. sourceType: "script",
  6843. sourceFilename: undefined,
  6844. startLine: 1,
  6845. allowAwaitOutsideFunction: false,
  6846. allowReturnOutsideFunction: false,
  6847. allowImportExportEverywhere: false,
  6848. allowSuperOutsideMethod: false,
  6849. allowUndeclaredExports: false,
  6850. plugins: [],
  6851. strictMode: null,
  6852. ranges: false,
  6853. tokens: false,
  6854. createParenthesizedExpressions: false,
  6855. errorRecovery: false
  6856. };
  6857. function getOptions(opts) {
  6858. const options = {};
  6859. for (const key of Object.keys(defaultOptions)) {
  6860. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  6861. }
  6862. return options;
  6863. }
  6864. var _isDigit = function isDigit(code) {
  6865. return code >= 48 && code <= 57;
  6866. };
  6867. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u", "d"]);
  6868. const forbiddenNumericSeparatorSiblings = {
  6869. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  6870. hex: [46, 88, 95, 120]
  6871. };
  6872. const allowedNumericSeparatorSiblings = {};
  6873. allowedNumericSeparatorSiblings.bin = [48, 49];
  6874. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  6875. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  6876. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  6877. class Token {
  6878. constructor(state) {
  6879. this.type = state.type;
  6880. this.value = state.value;
  6881. this.start = state.start;
  6882. this.end = state.end;
  6883. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  6884. }
  6885. }
  6886. class Tokenizer extends ParserError {
  6887. constructor(options, input) {
  6888. super();
  6889. this.isLookahead = void 0;
  6890. this.tokens = [];
  6891. this.state = new State();
  6892. this.state.init(options);
  6893. this.input = input;
  6894. this.length = input.length;
  6895. this.isLookahead = false;
  6896. }
  6897. pushToken(token) {
  6898. this.tokens.length = this.state.tokensLength;
  6899. this.tokens.push(token);
  6900. ++this.state.tokensLength;
  6901. }
  6902. next() {
  6903. this.checkKeywordEscapes();
  6904. if (this.options.tokens) {
  6905. this.pushToken(new Token(this.state));
  6906. }
  6907. this.state.lastTokEnd = this.state.end;
  6908. this.state.lastTokStart = this.state.start;
  6909. this.state.lastTokEndLoc = this.state.endLoc;
  6910. this.state.lastTokStartLoc = this.state.startLoc;
  6911. this.nextToken();
  6912. }
  6913. eat(type) {
  6914. if (this.match(type)) {
  6915. this.next();
  6916. return true;
  6917. } else {
  6918. return false;
  6919. }
  6920. }
  6921. match(type) {
  6922. return this.state.type === type;
  6923. }
  6924. createLookaheadState(state) {
  6925. return {
  6926. pos: state.pos,
  6927. value: null,
  6928. type: state.type,
  6929. start: state.start,
  6930. end: state.end,
  6931. lastTokEnd: state.end,
  6932. context: [this.curContext()],
  6933. inType: state.inType
  6934. };
  6935. }
  6936. lookahead() {
  6937. const old = this.state;
  6938. this.state = this.createLookaheadState(old);
  6939. this.isLookahead = true;
  6940. this.nextToken();
  6941. this.isLookahead = false;
  6942. const curr = this.state;
  6943. this.state = old;
  6944. return curr;
  6945. }
  6946. nextTokenStart() {
  6947. return this.nextTokenStartSince(this.state.pos);
  6948. }
  6949. nextTokenStartSince(pos) {
  6950. skipWhiteSpace.lastIndex = pos;
  6951. const skip = skipWhiteSpace.exec(this.input);
  6952. return pos + skip[0].length;
  6953. }
  6954. lookaheadCharCode() {
  6955. return this.input.charCodeAt(this.nextTokenStart());
  6956. }
  6957. codePointAtPos(pos) {
  6958. let cp = this.input.charCodeAt(pos);
  6959. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  6960. const trail = this.input.charCodeAt(pos);
  6961. if ((trail & 0xfc00) === 0xdc00) {
  6962. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  6963. }
  6964. }
  6965. return cp;
  6966. }
  6967. setStrict(strict) {
  6968. this.state.strict = strict;
  6969. if (strict) {
  6970. this.state.strictErrors.forEach((message, pos) => this.raise(pos, message));
  6971. this.state.strictErrors.clear();
  6972. }
  6973. }
  6974. curContext() {
  6975. return this.state.context[this.state.context.length - 1];
  6976. }
  6977. nextToken() {
  6978. const curContext = this.curContext();
  6979. if (!curContext.preserveSpace) this.skipSpace();
  6980. this.state.start = this.state.pos;
  6981. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  6982. if (this.state.pos >= this.length) {
  6983. this.finishToken(types$1.eof);
  6984. return;
  6985. }
  6986. if (curContext === types.template) {
  6987. this.readTmplToken();
  6988. } else {
  6989. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  6990. }
  6991. }
  6992. pushComment(block, text, start, end, startLoc, endLoc) {
  6993. const comment = {
  6994. type: block ? "CommentBlock" : "CommentLine",
  6995. value: text,
  6996. start: start,
  6997. end: end,
  6998. loc: new SourceLocation(startLoc, endLoc)
  6999. };
  7000. if (this.options.tokens) this.pushToken(comment);
  7001. this.state.comments.push(comment);
  7002. this.addComment(comment);
  7003. }
  7004. skipBlockComment() {
  7005. let startLoc;
  7006. if (!this.isLookahead) startLoc = this.state.curPosition();
  7007. const start = this.state.pos;
  7008. const end = this.input.indexOf("*/", this.state.pos + 2);
  7009. if (end === -1) throw this.raise(start, ErrorMessages.UnterminatedComment);
  7010. this.state.pos = end + 2;
  7011. lineBreakG.lastIndex = start;
  7012. let match;
  7013. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  7014. ++this.state.curLine;
  7015. this.state.lineStart = match.index + match[0].length;
  7016. }
  7017. if (this.isLookahead) return;
  7018. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  7019. }
  7020. skipLineComment(startSkip) {
  7021. const start = this.state.pos;
  7022. let startLoc;
  7023. if (!this.isLookahead) startLoc = this.state.curPosition();
  7024. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  7025. if (this.state.pos < this.length) {
  7026. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7027. ch = this.input.charCodeAt(this.state.pos);
  7028. }
  7029. }
  7030. if (this.isLookahead) return;
  7031. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  7032. }
  7033. skipSpace() {
  7034. loop: while (this.state.pos < this.length) {
  7035. const ch = this.input.charCodeAt(this.state.pos);
  7036. switch (ch) {
  7037. case 32:
  7038. case 160:
  7039. case 9:
  7040. ++this.state.pos;
  7041. break;
  7042. case 13:
  7043. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  7044. ++this.state.pos;
  7045. }
  7046. case 10:
  7047. case 8232:
  7048. case 8233:
  7049. ++this.state.pos;
  7050. ++this.state.curLine;
  7051. this.state.lineStart = this.state.pos;
  7052. break;
  7053. case 47:
  7054. switch (this.input.charCodeAt(this.state.pos + 1)) {
  7055. case 42:
  7056. this.skipBlockComment();
  7057. break;
  7058. case 47:
  7059. this.skipLineComment(2);
  7060. break;
  7061. default:
  7062. break loop;
  7063. }
  7064. break;
  7065. default:
  7066. if (isWhitespace(ch)) {
  7067. ++this.state.pos;
  7068. } else {
  7069. break loop;
  7070. }
  7071. }
  7072. }
  7073. }
  7074. finishToken(type, val) {
  7075. this.state.end = this.state.pos;
  7076. const prevType = this.state.type;
  7077. this.state.type = type;
  7078. this.state.value = val;
  7079. if (!this.isLookahead) {
  7080. this.state.endLoc = this.state.curPosition();
  7081. this.updateContext(prevType);
  7082. }
  7083. }
  7084. readToken_numberSign() {
  7085. if (this.state.pos === 0 && this.readToken_interpreter()) {
  7086. return;
  7087. }
  7088. const nextPos = this.state.pos + 1;
  7089. const next = this.codePointAtPos(nextPos);
  7090. if (next >= 48 && next <= 57) {
  7091. throw this.raise(this.state.pos, ErrorMessages.UnexpectedDigitAfterHash);
  7092. }
  7093. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  7094. this.expectPlugin("recordAndTuple");
  7095. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  7096. throw this.raise(this.state.pos, next === 123 ? ErrorMessages.RecordExpressionHashIncorrectStartSyntaxType : ErrorMessages.TupleExpressionHashIncorrectStartSyntaxType);
  7097. }
  7098. this.state.pos += 2;
  7099. if (next === 123) {
  7100. this.finishToken(types$1.braceHashL);
  7101. } else {
  7102. this.finishToken(types$1.bracketHashL);
  7103. }
  7104. } else if (isIdentifierStart(next)) {
  7105. ++this.state.pos;
  7106. this.finishToken(types$1.privateName, this.readWord1(next));
  7107. } else if (next === 92) {
  7108. ++this.state.pos;
  7109. this.finishToken(types$1.privateName, this.readWord1());
  7110. } else {
  7111. this.finishOp(types$1.hash, 1);
  7112. }
  7113. }
  7114. readToken_dot() {
  7115. const next = this.input.charCodeAt(this.state.pos + 1);
  7116. if (next >= 48 && next <= 57) {
  7117. this.readNumber(true);
  7118. return;
  7119. }
  7120. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  7121. this.state.pos += 3;
  7122. this.finishToken(types$1.ellipsis);
  7123. } else {
  7124. ++this.state.pos;
  7125. this.finishToken(types$1.dot);
  7126. }
  7127. }
  7128. readToken_slash() {
  7129. const next = this.input.charCodeAt(this.state.pos + 1);
  7130. if (next === 61) {
  7131. this.finishOp(types$1.slashAssign, 2);
  7132. } else {
  7133. this.finishOp(types$1.slash, 1);
  7134. }
  7135. }
  7136. readToken_interpreter() {
  7137. if (this.state.pos !== 0 || this.length < 2) return false;
  7138. let ch = this.input.charCodeAt(this.state.pos + 1);
  7139. if (ch !== 33) return false;
  7140. const start = this.state.pos;
  7141. this.state.pos += 1;
  7142. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  7143. ch = this.input.charCodeAt(this.state.pos);
  7144. }
  7145. const value = this.input.slice(start + 2, this.state.pos);
  7146. this.finishToken(types$1.interpreterDirective, value);
  7147. return true;
  7148. }
  7149. readToken_mult_modulo(code) {
  7150. let type = code === 42 ? types$1.star : types$1.modulo;
  7151. let width = 1;
  7152. let next = this.input.charCodeAt(this.state.pos + 1);
  7153. if (code === 42 && next === 42) {
  7154. width++;
  7155. next = this.input.charCodeAt(this.state.pos + 2);
  7156. type = types$1.exponent;
  7157. }
  7158. if (next === 61 && !this.state.inType) {
  7159. width++;
  7160. type = types$1.assign;
  7161. }
  7162. this.finishOp(type, width);
  7163. }
  7164. readToken_pipe_amp(code) {
  7165. const next = this.input.charCodeAt(this.state.pos + 1);
  7166. if (next === code) {
  7167. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  7168. this.finishOp(types$1.assign, 3);
  7169. } else {
  7170. this.finishOp(code === 124 ? types$1.logicalOR : types$1.logicalAND, 2);
  7171. }
  7172. return;
  7173. }
  7174. if (code === 124) {
  7175. if (next === 62) {
  7176. this.finishOp(types$1.pipeline, 2);
  7177. return;
  7178. }
  7179. if (this.hasPlugin("recordAndTuple") && next === 125) {
  7180. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7181. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectEndSyntaxType);
  7182. }
  7183. this.state.pos += 2;
  7184. this.finishToken(types$1.braceBarR);
  7185. return;
  7186. }
  7187. if (this.hasPlugin("recordAndTuple") && next === 93) {
  7188. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7189. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectEndSyntaxType);
  7190. }
  7191. this.state.pos += 2;
  7192. this.finishToken(types$1.bracketBarR);
  7193. return;
  7194. }
  7195. }
  7196. if (next === 61) {
  7197. this.finishOp(types$1.assign, 2);
  7198. return;
  7199. }
  7200. this.finishOp(code === 124 ? types$1.bitwiseOR : types$1.bitwiseAND, 1);
  7201. }
  7202. readToken_caret() {
  7203. const next = this.input.charCodeAt(this.state.pos + 1);
  7204. if (next === 61) {
  7205. this.finishOp(types$1.assign, 2);
  7206. } else {
  7207. this.finishOp(types$1.bitwiseXOR, 1);
  7208. }
  7209. }
  7210. readToken_plus_min(code) {
  7211. const next = this.input.charCodeAt(this.state.pos + 1);
  7212. if (next === code) {
  7213. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || this.hasPrecedingLineBreak())) {
  7214. this.skipLineComment(3);
  7215. this.skipSpace();
  7216. this.nextToken();
  7217. return;
  7218. }
  7219. this.finishOp(types$1.incDec, 2);
  7220. return;
  7221. }
  7222. if (next === 61) {
  7223. this.finishOp(types$1.assign, 2);
  7224. } else {
  7225. this.finishOp(types$1.plusMin, 1);
  7226. }
  7227. }
  7228. readToken_lt_gt(code) {
  7229. const next = this.input.charCodeAt(this.state.pos + 1);
  7230. let size = 1;
  7231. if (next === code) {
  7232. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  7233. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  7234. this.finishOp(types$1.assign, size + 1);
  7235. return;
  7236. }
  7237. this.finishOp(types$1.bitShift, size);
  7238. return;
  7239. }
  7240. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  7241. this.skipLineComment(4);
  7242. this.skipSpace();
  7243. this.nextToken();
  7244. return;
  7245. }
  7246. if (next === 61) {
  7247. size = 2;
  7248. }
  7249. this.finishOp(types$1.relational, size);
  7250. }
  7251. readToken_eq_excl(code) {
  7252. const next = this.input.charCodeAt(this.state.pos + 1);
  7253. if (next === 61) {
  7254. this.finishOp(types$1.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  7255. return;
  7256. }
  7257. if (code === 61 && next === 62) {
  7258. this.state.pos += 2;
  7259. this.finishToken(types$1.arrow);
  7260. return;
  7261. }
  7262. this.finishOp(code === 61 ? types$1.eq : types$1.bang, 1);
  7263. }
  7264. readToken_question() {
  7265. const next = this.input.charCodeAt(this.state.pos + 1);
  7266. const next2 = this.input.charCodeAt(this.state.pos + 2);
  7267. if (next === 63) {
  7268. if (next2 === 61) {
  7269. this.finishOp(types$1.assign, 3);
  7270. } else {
  7271. this.finishOp(types$1.nullishCoalescing, 2);
  7272. }
  7273. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  7274. this.state.pos += 2;
  7275. this.finishToken(types$1.questionDot);
  7276. } else {
  7277. ++this.state.pos;
  7278. this.finishToken(types$1.question);
  7279. }
  7280. }
  7281. getTokenFromCode(code) {
  7282. switch (code) {
  7283. case 46:
  7284. this.readToken_dot();
  7285. return;
  7286. case 40:
  7287. ++this.state.pos;
  7288. this.finishToken(types$1.parenL);
  7289. return;
  7290. case 41:
  7291. ++this.state.pos;
  7292. this.finishToken(types$1.parenR);
  7293. return;
  7294. case 59:
  7295. ++this.state.pos;
  7296. this.finishToken(types$1.semi);
  7297. return;
  7298. case 44:
  7299. ++this.state.pos;
  7300. this.finishToken(types$1.comma);
  7301. return;
  7302. case 91:
  7303. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7304. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7305. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectStartSyntaxType);
  7306. }
  7307. this.state.pos += 2;
  7308. this.finishToken(types$1.bracketBarL);
  7309. } else {
  7310. ++this.state.pos;
  7311. this.finishToken(types$1.bracketL);
  7312. }
  7313. return;
  7314. case 93:
  7315. ++this.state.pos;
  7316. this.finishToken(types$1.bracketR);
  7317. return;
  7318. case 123:
  7319. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  7320. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  7321. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectStartSyntaxType);
  7322. }
  7323. this.state.pos += 2;
  7324. this.finishToken(types$1.braceBarL);
  7325. } else {
  7326. ++this.state.pos;
  7327. this.finishToken(types$1.braceL);
  7328. }
  7329. return;
  7330. case 125:
  7331. ++this.state.pos;
  7332. this.finishToken(types$1.braceR);
  7333. return;
  7334. case 58:
  7335. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  7336. this.finishOp(types$1.doubleColon, 2);
  7337. } else {
  7338. ++this.state.pos;
  7339. this.finishToken(types$1.colon);
  7340. }
  7341. return;
  7342. case 63:
  7343. this.readToken_question();
  7344. return;
  7345. case 96:
  7346. ++this.state.pos;
  7347. this.finishToken(types$1.backQuote);
  7348. return;
  7349. case 48:
  7350. {
  7351. const next = this.input.charCodeAt(this.state.pos + 1);
  7352. if (next === 120 || next === 88) {
  7353. this.readRadixNumber(16);
  7354. return;
  7355. }
  7356. if (next === 111 || next === 79) {
  7357. this.readRadixNumber(8);
  7358. return;
  7359. }
  7360. if (next === 98 || next === 66) {
  7361. this.readRadixNumber(2);
  7362. return;
  7363. }
  7364. }
  7365. case 49:
  7366. case 50:
  7367. case 51:
  7368. case 52:
  7369. case 53:
  7370. case 54:
  7371. case 55:
  7372. case 56:
  7373. case 57:
  7374. this.readNumber(false);
  7375. return;
  7376. case 34:
  7377. case 39:
  7378. this.readString(code);
  7379. return;
  7380. case 47:
  7381. this.readToken_slash();
  7382. return;
  7383. case 37:
  7384. case 42:
  7385. this.readToken_mult_modulo(code);
  7386. return;
  7387. case 124:
  7388. case 38:
  7389. this.readToken_pipe_amp(code);
  7390. return;
  7391. case 94:
  7392. this.readToken_caret();
  7393. return;
  7394. case 43:
  7395. case 45:
  7396. this.readToken_plus_min(code);
  7397. return;
  7398. case 60:
  7399. case 62:
  7400. this.readToken_lt_gt(code);
  7401. return;
  7402. case 61:
  7403. case 33:
  7404. this.readToken_eq_excl(code);
  7405. return;
  7406. case 126:
  7407. this.finishOp(types$1.tilde, 1);
  7408. return;
  7409. case 64:
  7410. ++this.state.pos;
  7411. this.finishToken(types$1.at);
  7412. return;
  7413. case 35:
  7414. this.readToken_numberSign();
  7415. return;
  7416. case 92:
  7417. this.readWord();
  7418. return;
  7419. default:
  7420. if (isIdentifierStart(code)) {
  7421. this.readWord(code);
  7422. return;
  7423. }
  7424. }
  7425. throw this.raise(this.state.pos, ErrorMessages.InvalidOrUnexpectedToken, String.fromCodePoint(code));
  7426. }
  7427. finishOp(type, size) {
  7428. const str = this.input.slice(this.state.pos, this.state.pos + size);
  7429. this.state.pos += size;
  7430. this.finishToken(type, str);
  7431. }
  7432. readRegexp() {
  7433. const start = this.state.start + 1;
  7434. let escaped, inClass;
  7435. for (;;) {
  7436. if (this.state.pos >= this.length) {
  7437. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7438. }
  7439. const ch = this.input.charAt(this.state.pos);
  7440. if (lineBreak.test(ch)) {
  7441. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  7442. }
  7443. if (escaped) {
  7444. escaped = false;
  7445. } else {
  7446. if (ch === "[") {
  7447. inClass = true;
  7448. } else if (ch === "]" && inClass) {
  7449. inClass = false;
  7450. } else if (ch === "/" && !inClass) {
  7451. break;
  7452. }
  7453. escaped = ch === "\\";
  7454. }
  7455. ++this.state.pos;
  7456. }
  7457. const content = this.input.slice(start, this.state.pos);
  7458. ++this.state.pos;
  7459. let mods = "";
  7460. while (this.state.pos < this.length) {
  7461. const char = this.input[this.state.pos];
  7462. const charCode = this.codePointAtPos(this.state.pos);
  7463. if (VALID_REGEX_FLAGS.has(char)) {
  7464. if (mods.indexOf(char) > -1) {
  7465. this.raise(this.state.pos + 1, ErrorMessages.DuplicateRegExpFlags);
  7466. }
  7467. } else if (isIdentifierChar(charCode) || charCode === 92) {
  7468. this.raise(this.state.pos + 1, ErrorMessages.MalformedRegExpFlags);
  7469. } else {
  7470. break;
  7471. }
  7472. ++this.state.pos;
  7473. mods += char;
  7474. }
  7475. this.finishToken(types$1.regexp, {
  7476. pattern: content,
  7477. flags: mods
  7478. });
  7479. }
  7480. readInt(radix, len, forceLen, allowNumSeparator = true) {
  7481. const start = this.state.pos;
  7482. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  7483. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  7484. let invalid = false;
  7485. let total = 0;
  7486. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  7487. const code = this.input.charCodeAt(this.state.pos);
  7488. let val;
  7489. if (code === 95) {
  7490. const prev = this.input.charCodeAt(this.state.pos - 1);
  7491. const next = this.input.charCodeAt(this.state.pos + 1);
  7492. if (allowedSiblings.indexOf(next) === -1) {
  7493. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7494. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  7495. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  7496. }
  7497. if (!allowNumSeparator) {
  7498. this.raise(this.state.pos, ErrorMessages.NumericSeparatorInEscapeSequence);
  7499. }
  7500. ++this.state.pos;
  7501. continue;
  7502. }
  7503. if (code >= 97) {
  7504. val = code - 97 + 10;
  7505. } else if (code >= 65) {
  7506. val = code - 65 + 10;
  7507. } else if (_isDigit(code)) {
  7508. val = code - 48;
  7509. } else {
  7510. val = Infinity;
  7511. }
  7512. if (val >= radix) {
  7513. if (this.options.errorRecovery && val <= 9) {
  7514. val = 0;
  7515. this.raise(this.state.start + i + 2, ErrorMessages.InvalidDigit, radix);
  7516. } else if (forceLen) {
  7517. val = 0;
  7518. invalid = true;
  7519. } else {
  7520. break;
  7521. }
  7522. }
  7523. ++this.state.pos;
  7524. total = total * radix + val;
  7525. }
  7526. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  7527. return null;
  7528. }
  7529. return total;
  7530. }
  7531. readRadixNumber(radix) {
  7532. const start = this.state.pos;
  7533. let isBigInt = false;
  7534. this.state.pos += 2;
  7535. const val = this.readInt(radix);
  7536. if (val == null) {
  7537. this.raise(this.state.start + 2, ErrorMessages.InvalidDigit, radix);
  7538. }
  7539. const next = this.input.charCodeAt(this.state.pos);
  7540. if (next === 110) {
  7541. ++this.state.pos;
  7542. isBigInt = true;
  7543. } else if (next === 109) {
  7544. throw this.raise(start, ErrorMessages.InvalidDecimal);
  7545. }
  7546. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7547. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7548. }
  7549. if (isBigInt) {
  7550. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  7551. this.finishToken(types$1.bigint, str);
  7552. return;
  7553. }
  7554. this.finishToken(types$1.num, val);
  7555. }
  7556. readNumber(startsWithDot) {
  7557. const start = this.state.pos;
  7558. let isFloat = false;
  7559. let isBigInt = false;
  7560. let isDecimal = false;
  7561. let hasExponent = false;
  7562. let isOctal = false;
  7563. if (!startsWithDot && this.readInt(10) === null) {
  7564. this.raise(start, ErrorMessages.InvalidNumber);
  7565. }
  7566. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  7567. if (hasLeadingZero) {
  7568. const integer = this.input.slice(start, this.state.pos);
  7569. this.recordStrictModeErrors(start, ErrorMessages.StrictOctalLiteral);
  7570. if (!this.state.strict) {
  7571. const underscorePos = integer.indexOf("_");
  7572. if (underscorePos > 0) {
  7573. this.raise(underscorePos + start, ErrorMessages.ZeroDigitNumericSeparator);
  7574. }
  7575. }
  7576. isOctal = hasLeadingZero && !/[89]/.test(integer);
  7577. }
  7578. let next = this.input.charCodeAt(this.state.pos);
  7579. if (next === 46 && !isOctal) {
  7580. ++this.state.pos;
  7581. this.readInt(10);
  7582. isFloat = true;
  7583. next = this.input.charCodeAt(this.state.pos);
  7584. }
  7585. if ((next === 69 || next === 101) && !isOctal) {
  7586. next = this.input.charCodeAt(++this.state.pos);
  7587. if (next === 43 || next === 45) {
  7588. ++this.state.pos;
  7589. }
  7590. if (this.readInt(10) === null) {
  7591. this.raise(start, ErrorMessages.InvalidOrMissingExponent);
  7592. }
  7593. isFloat = true;
  7594. hasExponent = true;
  7595. next = this.input.charCodeAt(this.state.pos);
  7596. }
  7597. if (next === 110) {
  7598. if (isFloat || hasLeadingZero) {
  7599. this.raise(start, ErrorMessages.InvalidBigIntLiteral);
  7600. }
  7601. ++this.state.pos;
  7602. isBigInt = true;
  7603. }
  7604. if (next === 109) {
  7605. this.expectPlugin("decimal", this.state.pos);
  7606. if (hasExponent || hasLeadingZero) {
  7607. this.raise(start, ErrorMessages.InvalidDecimal);
  7608. }
  7609. ++this.state.pos;
  7610. isDecimal = true;
  7611. }
  7612. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  7613. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  7614. }
  7615. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  7616. if (isBigInt) {
  7617. this.finishToken(types$1.bigint, str);
  7618. return;
  7619. }
  7620. if (isDecimal) {
  7621. this.finishToken(types$1.decimal, str);
  7622. return;
  7623. }
  7624. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  7625. this.finishToken(types$1.num, val);
  7626. }
  7627. readCodePoint(throwOnInvalid) {
  7628. const ch = this.input.charCodeAt(this.state.pos);
  7629. let code;
  7630. if (ch === 123) {
  7631. const codePos = ++this.state.pos;
  7632. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  7633. ++this.state.pos;
  7634. if (code !== null && code > 0x10ffff) {
  7635. if (throwOnInvalid) {
  7636. this.raise(codePos, ErrorMessages.InvalidCodePoint);
  7637. } else {
  7638. return null;
  7639. }
  7640. }
  7641. } else {
  7642. code = this.readHexChar(4, false, throwOnInvalid);
  7643. }
  7644. return code;
  7645. }
  7646. readString(quote) {
  7647. let out = "",
  7648. chunkStart = ++this.state.pos;
  7649. for (;;) {
  7650. if (this.state.pos >= this.length) {
  7651. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7652. }
  7653. const ch = this.input.charCodeAt(this.state.pos);
  7654. if (ch === quote) break;
  7655. if (ch === 92) {
  7656. out += this.input.slice(chunkStart, this.state.pos);
  7657. out += this.readEscapedChar(false);
  7658. chunkStart = this.state.pos;
  7659. } else if (ch === 8232 || ch === 8233) {
  7660. ++this.state.pos;
  7661. ++this.state.curLine;
  7662. this.state.lineStart = this.state.pos;
  7663. } else if (isNewLine(ch)) {
  7664. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  7665. } else {
  7666. ++this.state.pos;
  7667. }
  7668. }
  7669. out += this.input.slice(chunkStart, this.state.pos++);
  7670. this.finishToken(types$1.string, out);
  7671. }
  7672. readTmplToken() {
  7673. let out = "",
  7674. chunkStart = this.state.pos,
  7675. containsInvalid = false;
  7676. for (;;) {
  7677. if (this.state.pos >= this.length) {
  7678. throw this.raise(this.state.start, ErrorMessages.UnterminatedTemplate);
  7679. }
  7680. const ch = this.input.charCodeAt(this.state.pos);
  7681. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  7682. if (this.state.pos === this.state.start && this.match(types$1.template)) {
  7683. if (ch === 36) {
  7684. this.state.pos += 2;
  7685. this.finishToken(types$1.dollarBraceL);
  7686. return;
  7687. } else {
  7688. ++this.state.pos;
  7689. this.finishToken(types$1.backQuote);
  7690. return;
  7691. }
  7692. }
  7693. out += this.input.slice(chunkStart, this.state.pos);
  7694. this.finishToken(types$1.template, containsInvalid ? null : out);
  7695. return;
  7696. }
  7697. if (ch === 92) {
  7698. out += this.input.slice(chunkStart, this.state.pos);
  7699. const escaped = this.readEscapedChar(true);
  7700. if (escaped === null) {
  7701. containsInvalid = true;
  7702. } else {
  7703. out += escaped;
  7704. }
  7705. chunkStart = this.state.pos;
  7706. } else if (isNewLine(ch)) {
  7707. out += this.input.slice(chunkStart, this.state.pos);
  7708. ++this.state.pos;
  7709. switch (ch) {
  7710. case 13:
  7711. if (this.input.charCodeAt(this.state.pos) === 10) {
  7712. ++this.state.pos;
  7713. }
  7714. case 10:
  7715. out += "\n";
  7716. break;
  7717. default:
  7718. out += String.fromCharCode(ch);
  7719. break;
  7720. }
  7721. ++this.state.curLine;
  7722. this.state.lineStart = this.state.pos;
  7723. chunkStart = this.state.pos;
  7724. } else {
  7725. ++this.state.pos;
  7726. }
  7727. }
  7728. }
  7729. recordStrictModeErrors(pos, message) {
  7730. if (this.state.strict && !this.state.strictErrors.has(pos)) {
  7731. this.raise(pos, message);
  7732. } else {
  7733. this.state.strictErrors.set(pos, message);
  7734. }
  7735. }
  7736. readEscapedChar(inTemplate) {
  7737. const throwOnInvalid = !inTemplate;
  7738. const ch = this.input.charCodeAt(++this.state.pos);
  7739. ++this.state.pos;
  7740. switch (ch) {
  7741. case 110:
  7742. return "\n";
  7743. case 114:
  7744. return "\r";
  7745. case 120:
  7746. {
  7747. const code = this.readHexChar(2, false, throwOnInvalid);
  7748. return code === null ? null : String.fromCharCode(code);
  7749. }
  7750. case 117:
  7751. {
  7752. const code = this.readCodePoint(throwOnInvalid);
  7753. return code === null ? null : String.fromCodePoint(code);
  7754. }
  7755. case 116:
  7756. return "\t";
  7757. case 98:
  7758. return "\b";
  7759. case 118:
  7760. return "\u000b";
  7761. case 102:
  7762. return "\f";
  7763. case 13:
  7764. if (this.input.charCodeAt(this.state.pos) === 10) {
  7765. ++this.state.pos;
  7766. }
  7767. case 10:
  7768. this.state.lineStart = this.state.pos;
  7769. ++this.state.curLine;
  7770. case 8232:
  7771. case 8233:
  7772. return "";
  7773. case 56:
  7774. case 57:
  7775. if (inTemplate) {
  7776. return null;
  7777. } else {
  7778. this.recordStrictModeErrors(this.state.pos - 1, ErrorMessages.StrictNumericEscape);
  7779. }
  7780. default:
  7781. if (ch >= 48 && ch <= 55) {
  7782. const codePos = this.state.pos - 1;
  7783. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  7784. let octalStr = match[0];
  7785. let octal = parseInt(octalStr, 8);
  7786. if (octal > 255) {
  7787. octalStr = octalStr.slice(0, -1);
  7788. octal = parseInt(octalStr, 8);
  7789. }
  7790. this.state.pos += octalStr.length - 1;
  7791. const next = this.input.charCodeAt(this.state.pos);
  7792. if (octalStr !== "0" || next === 56 || next === 57) {
  7793. if (inTemplate) {
  7794. return null;
  7795. } else {
  7796. this.recordStrictModeErrors(codePos, ErrorMessages.StrictNumericEscape);
  7797. }
  7798. }
  7799. return String.fromCharCode(octal);
  7800. }
  7801. return String.fromCharCode(ch);
  7802. }
  7803. }
  7804. readHexChar(len, forceLen, throwOnInvalid) {
  7805. const codePos = this.state.pos;
  7806. const n = this.readInt(16, len, forceLen, false);
  7807. if (n === null) {
  7808. if (throwOnInvalid) {
  7809. this.raise(codePos, ErrorMessages.InvalidEscapeSequence);
  7810. } else {
  7811. this.state.pos = codePos - 1;
  7812. }
  7813. }
  7814. return n;
  7815. }
  7816. readWord1(firstCode) {
  7817. this.state.containsEsc = false;
  7818. let word = "";
  7819. const start = this.state.pos;
  7820. let chunkStart = this.state.pos;
  7821. if (firstCode !== undefined) {
  7822. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  7823. }
  7824. while (this.state.pos < this.length) {
  7825. const ch = this.codePointAtPos(this.state.pos);
  7826. if (isIdentifierChar(ch)) {
  7827. this.state.pos += ch <= 0xffff ? 1 : 2;
  7828. } else if (ch === 92) {
  7829. this.state.containsEsc = true;
  7830. word += this.input.slice(chunkStart, this.state.pos);
  7831. const escStart = this.state.pos;
  7832. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  7833. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  7834. this.raise(this.state.pos, ErrorMessages.MissingUnicodeEscape);
  7835. chunkStart = this.state.pos - 1;
  7836. continue;
  7837. }
  7838. ++this.state.pos;
  7839. const esc = this.readCodePoint(true);
  7840. if (esc !== null) {
  7841. if (!identifierCheck(esc)) {
  7842. this.raise(escStart, ErrorMessages.EscapedCharNotAnIdentifier);
  7843. }
  7844. word += String.fromCodePoint(esc);
  7845. }
  7846. chunkStart = this.state.pos;
  7847. } else {
  7848. break;
  7849. }
  7850. }
  7851. return word + this.input.slice(chunkStart, this.state.pos);
  7852. }
  7853. readWord(firstCode) {
  7854. const word = this.readWord1(firstCode);
  7855. const type = keywords$1.get(word) || types$1.name;
  7856. this.finishToken(type, word);
  7857. }
  7858. checkKeywordEscapes() {
  7859. const kw = this.state.type.keyword;
  7860. if (kw && this.state.containsEsc) {
  7861. this.raise(this.state.start, ErrorMessages.InvalidEscapedReservedWord, kw);
  7862. }
  7863. }
  7864. updateContext(prevType) {
  7865. var _this$state$type$upda, _this$state$type;
  7866. (_this$state$type$upda = (_this$state$type = this.state.type).updateContext) == null ? void 0 : _this$state$type$upda.call(_this$state$type, this.state.context);
  7867. }
  7868. }
  7869. class ClassScope {
  7870. constructor() {
  7871. this.privateNames = new Set();
  7872. this.loneAccessors = new Map();
  7873. this.undefinedPrivateNames = new Map();
  7874. }
  7875. }
  7876. class ClassScopeHandler {
  7877. constructor(raise) {
  7878. this.stack = [];
  7879. this.undefinedPrivateNames = new Map();
  7880. this.raise = raise;
  7881. }
  7882. current() {
  7883. return this.stack[this.stack.length - 1];
  7884. }
  7885. enter() {
  7886. this.stack.push(new ClassScope());
  7887. }
  7888. exit() {
  7889. const oldClassScope = this.stack.pop();
  7890. const current = this.current();
  7891. for (const [name, pos] of Array.from(oldClassScope.undefinedPrivateNames)) {
  7892. if (current) {
  7893. if (!current.undefinedPrivateNames.has(name)) {
  7894. current.undefinedPrivateNames.set(name, pos);
  7895. }
  7896. } else {
  7897. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7898. }
  7899. }
  7900. }
  7901. declarePrivateName(name, elementType, pos) {
  7902. const classScope = this.current();
  7903. let redefined = classScope.privateNames.has(name);
  7904. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  7905. const accessor = redefined && classScope.loneAccessors.get(name);
  7906. if (accessor) {
  7907. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  7908. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  7909. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  7910. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  7911. redefined = oldKind === newKind || oldStatic !== newStatic;
  7912. if (!redefined) classScope.loneAccessors.delete(name);
  7913. } else if (!redefined) {
  7914. classScope.loneAccessors.set(name, elementType);
  7915. }
  7916. }
  7917. if (redefined) {
  7918. this.raise(pos, ErrorMessages.PrivateNameRedeclaration, name);
  7919. }
  7920. classScope.privateNames.add(name);
  7921. classScope.undefinedPrivateNames.delete(name);
  7922. }
  7923. usePrivateName(name, pos) {
  7924. let classScope;
  7925. for (classScope of this.stack) {
  7926. if (classScope.privateNames.has(name)) return;
  7927. }
  7928. if (classScope) {
  7929. classScope.undefinedPrivateNames.set(name, pos);
  7930. } else {
  7931. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  7932. }
  7933. }
  7934. }
  7935. const kExpression = 0,
  7936. kMaybeArrowParameterDeclaration = 1,
  7937. kMaybeAsyncArrowParameterDeclaration = 2,
  7938. kParameterDeclaration = 3;
  7939. class ExpressionScope {
  7940. constructor(type = kExpression) {
  7941. this.type = void 0;
  7942. this.type = type;
  7943. }
  7944. canBeArrowParameterDeclaration() {
  7945. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  7946. }
  7947. isCertainlyParameterDeclaration() {
  7948. return this.type === kParameterDeclaration;
  7949. }
  7950. }
  7951. class ArrowHeadParsingScope extends ExpressionScope {
  7952. constructor(type) {
  7953. super(type);
  7954. this.errors = new Map();
  7955. }
  7956. recordDeclarationError(pos, template) {
  7957. this.errors.set(pos, template);
  7958. }
  7959. clearDeclarationError(pos) {
  7960. this.errors.delete(pos);
  7961. }
  7962. iterateErrors(iterator) {
  7963. this.errors.forEach(iterator);
  7964. }
  7965. }
  7966. class ExpressionScopeHandler {
  7967. constructor(raise) {
  7968. this.stack = [new ExpressionScope()];
  7969. this.raise = raise;
  7970. }
  7971. enter(scope) {
  7972. this.stack.push(scope);
  7973. }
  7974. exit() {
  7975. this.stack.pop();
  7976. }
  7977. recordParameterInitializerError(pos, template) {
  7978. const {
  7979. stack
  7980. } = this;
  7981. let i = stack.length - 1;
  7982. let scope = stack[i];
  7983. while (!scope.isCertainlyParameterDeclaration()) {
  7984. if (scope.canBeArrowParameterDeclaration()) {
  7985. scope.recordDeclarationError(pos, template);
  7986. } else {
  7987. return;
  7988. }
  7989. scope = stack[--i];
  7990. }
  7991. this.raise(pos, template);
  7992. }
  7993. recordParenthesizedIdentifierError(pos, template) {
  7994. const {
  7995. stack
  7996. } = this;
  7997. const scope = stack[stack.length - 1];
  7998. if (scope.isCertainlyParameterDeclaration()) {
  7999. this.raise(pos, template);
  8000. } else if (scope.canBeArrowParameterDeclaration()) {
  8001. scope.recordDeclarationError(pos, template);
  8002. } else {
  8003. return;
  8004. }
  8005. }
  8006. recordAsyncArrowParametersError(pos, template) {
  8007. const {
  8008. stack
  8009. } = this;
  8010. let i = stack.length - 1;
  8011. let scope = stack[i];
  8012. while (scope.canBeArrowParameterDeclaration()) {
  8013. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  8014. scope.recordDeclarationError(pos, template);
  8015. }
  8016. scope = stack[--i];
  8017. }
  8018. }
  8019. validateAsPattern() {
  8020. const {
  8021. stack
  8022. } = this;
  8023. const currentScope = stack[stack.length - 1];
  8024. if (!currentScope.canBeArrowParameterDeclaration()) return;
  8025. currentScope.iterateErrors((template, pos) => {
  8026. this.raise(pos, template);
  8027. let i = stack.length - 2;
  8028. let scope = stack[i];
  8029. while (scope.canBeArrowParameterDeclaration()) {
  8030. scope.clearDeclarationError(pos);
  8031. scope = stack[--i];
  8032. }
  8033. });
  8034. }
  8035. }
  8036. function newParameterDeclarationScope() {
  8037. return new ExpressionScope(kParameterDeclaration);
  8038. }
  8039. function newArrowHeadScope() {
  8040. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  8041. }
  8042. function newAsyncArrowScope() {
  8043. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  8044. }
  8045. function newExpressionScope() {
  8046. return new ExpressionScope();
  8047. }
  8048. class UtilParser extends Tokenizer {
  8049. addExtra(node, key, val) {
  8050. if (!node) return;
  8051. const extra = node.extra = node.extra || {};
  8052. extra[key] = val;
  8053. }
  8054. isRelational(op) {
  8055. return this.match(types$1.relational) && this.state.value === op;
  8056. }
  8057. expectRelational(op) {
  8058. if (this.isRelational(op)) {
  8059. this.next();
  8060. } else {
  8061. this.unexpected(null, types$1.relational);
  8062. }
  8063. }
  8064. isContextual(name) {
  8065. return this.match(types$1.name) && this.state.value === name && !this.state.containsEsc;
  8066. }
  8067. isUnparsedContextual(nameStart, name) {
  8068. const nameEnd = nameStart + name.length;
  8069. if (this.input.slice(nameStart, nameEnd) === name) {
  8070. const nextCh = this.input.charCodeAt(nameEnd);
  8071. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  8072. }
  8073. return false;
  8074. }
  8075. isLookaheadContextual(name) {
  8076. const next = this.nextTokenStart();
  8077. return this.isUnparsedContextual(next, name);
  8078. }
  8079. eatContextual(name) {
  8080. return this.isContextual(name) && this.eat(types$1.name);
  8081. }
  8082. expectContextual(name, template) {
  8083. if (!this.eatContextual(name)) this.unexpected(null, template);
  8084. }
  8085. canInsertSemicolon() {
  8086. return this.match(types$1.eof) || this.match(types$1.braceR) || this.hasPrecedingLineBreak();
  8087. }
  8088. hasPrecedingLineBreak() {
  8089. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  8090. }
  8091. hasFollowingLineBreak() {
  8092. return lineBreak.test(this.input.slice(this.state.end, this.nextTokenStart()));
  8093. }
  8094. isLineTerminator() {
  8095. return this.eat(types$1.semi) || this.canInsertSemicolon();
  8096. }
  8097. semicolon(allowAsi = true) {
  8098. if (allowAsi ? this.isLineTerminator() : this.eat(types$1.semi)) return;
  8099. this.raise(this.state.lastTokEnd, ErrorMessages.MissingSemicolon);
  8100. }
  8101. expect(type, pos) {
  8102. this.eat(type) || this.unexpected(pos, type);
  8103. }
  8104. assertNoSpace(message = "Unexpected space.") {
  8105. if (this.state.start > this.state.lastTokEnd) {
  8106. this.raise(this.state.lastTokEnd, {
  8107. code: ErrorCodes.SyntaxError,
  8108. reasonCode: "UnexpectedSpace",
  8109. template: message
  8110. });
  8111. }
  8112. }
  8113. unexpected(pos, messageOrType = {
  8114. code: ErrorCodes.SyntaxError,
  8115. reasonCode: "UnexpectedToken",
  8116. template: "Unexpected token"
  8117. }) {
  8118. if (messageOrType instanceof TokenType) {
  8119. messageOrType = {
  8120. code: ErrorCodes.SyntaxError,
  8121. reasonCode: "UnexpectedToken",
  8122. template: `Unexpected token, expected "${messageOrType.label}"`
  8123. };
  8124. }
  8125. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  8126. }
  8127. expectPlugin(name, pos) {
  8128. if (!this.hasPlugin(name)) {
  8129. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8130. missingPlugin: [name]
  8131. }, `This experimental syntax requires enabling the parser plugin: '${name}'`);
  8132. }
  8133. return true;
  8134. }
  8135. expectOnePlugin(names, pos) {
  8136. if (!names.some(n => this.hasPlugin(n))) {
  8137. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  8138. missingPlugin: names
  8139. }, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`);
  8140. }
  8141. }
  8142. tryParse(fn, oldState = this.state.clone()) {
  8143. const abortSignal = {
  8144. node: null
  8145. };
  8146. try {
  8147. const node = fn((node = null) => {
  8148. abortSignal.node = node;
  8149. throw abortSignal;
  8150. });
  8151. if (this.state.errors.length > oldState.errors.length) {
  8152. const failState = this.state;
  8153. this.state = oldState;
  8154. this.state.tokensLength = failState.tokensLength;
  8155. return {
  8156. node,
  8157. error: failState.errors[oldState.errors.length],
  8158. thrown: false,
  8159. aborted: false,
  8160. failState
  8161. };
  8162. }
  8163. return {
  8164. node,
  8165. error: null,
  8166. thrown: false,
  8167. aborted: false,
  8168. failState: null
  8169. };
  8170. } catch (error) {
  8171. const failState = this.state;
  8172. this.state = oldState;
  8173. if (error instanceof SyntaxError) {
  8174. return {
  8175. node: null,
  8176. error,
  8177. thrown: true,
  8178. aborted: false,
  8179. failState
  8180. };
  8181. }
  8182. if (error === abortSignal) {
  8183. return {
  8184. node: abortSignal.node,
  8185. error: null,
  8186. thrown: false,
  8187. aborted: true,
  8188. failState
  8189. };
  8190. }
  8191. throw error;
  8192. }
  8193. }
  8194. checkExpressionErrors(refExpressionErrors, andThrow) {
  8195. if (!refExpressionErrors) return false;
  8196. const {
  8197. shorthandAssign,
  8198. doubleProto
  8199. } = refExpressionErrors;
  8200. if (!andThrow) return shorthandAssign >= 0 || doubleProto >= 0;
  8201. if (shorthandAssign >= 0) {
  8202. this.unexpected(shorthandAssign);
  8203. }
  8204. if (doubleProto >= 0) {
  8205. this.raise(doubleProto, ErrorMessages.DuplicateProto);
  8206. }
  8207. }
  8208. isLiteralPropertyName() {
  8209. 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);
  8210. }
  8211. isPrivateName(node) {
  8212. return node.type === "PrivateName";
  8213. }
  8214. getPrivateNameSV(node) {
  8215. return node.id.name;
  8216. }
  8217. hasPropertyAsPrivateName(node) {
  8218. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  8219. }
  8220. isOptionalChain(node) {
  8221. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  8222. }
  8223. isObjectProperty(node) {
  8224. return node.type === "ObjectProperty";
  8225. }
  8226. isObjectMethod(node) {
  8227. return node.type === "ObjectMethod";
  8228. }
  8229. initializeScopes(inModule = this.options.sourceType === "module") {
  8230. const oldLabels = this.state.labels;
  8231. this.state.labels = [];
  8232. const oldExportedIdentifiers = this.exportedIdentifiers;
  8233. this.exportedIdentifiers = new Set();
  8234. const oldInModule = this.inModule;
  8235. this.inModule = inModule;
  8236. const oldScope = this.scope;
  8237. const ScopeHandler = this.getScopeHandler();
  8238. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  8239. const oldProdParam = this.prodParam;
  8240. this.prodParam = new ProductionParameterHandler();
  8241. const oldClassScope = this.classScope;
  8242. this.classScope = new ClassScopeHandler(this.raise.bind(this));
  8243. const oldExpressionScope = this.expressionScope;
  8244. this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
  8245. return () => {
  8246. this.state.labels = oldLabels;
  8247. this.exportedIdentifiers = oldExportedIdentifiers;
  8248. this.inModule = oldInModule;
  8249. this.scope = oldScope;
  8250. this.prodParam = oldProdParam;
  8251. this.classScope = oldClassScope;
  8252. this.expressionScope = oldExpressionScope;
  8253. };
  8254. }
  8255. enterInitialScopes() {
  8256. let paramFlags = PARAM;
  8257. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8258. paramFlags |= PARAM_AWAIT;
  8259. }
  8260. this.scope.enter(SCOPE_PROGRAM);
  8261. this.prodParam.enter(paramFlags);
  8262. }
  8263. }
  8264. class ExpressionErrors {
  8265. constructor() {
  8266. this.shorthandAssign = -1;
  8267. this.doubleProto = -1;
  8268. }
  8269. }
  8270. class Node {
  8271. constructor(parser, pos, loc) {
  8272. this.type = void 0;
  8273. this.start = void 0;
  8274. this.end = void 0;
  8275. this.loc = void 0;
  8276. this.range = void 0;
  8277. this.leadingComments = void 0;
  8278. this.trailingComments = void 0;
  8279. this.innerComments = void 0;
  8280. this.extra = void 0;
  8281. this.type = "";
  8282. this.start = pos;
  8283. this.end = 0;
  8284. this.loc = new SourceLocation(loc);
  8285. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  8286. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  8287. }
  8288. __clone() {
  8289. const newNode = new Node();
  8290. const keys = Object.keys(this);
  8291. for (let i = 0, length = keys.length; i < length; i++) {
  8292. const key = keys[i];
  8293. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  8294. newNode[key] = this[key];
  8295. }
  8296. }
  8297. return newNode;
  8298. }
  8299. }
  8300. class NodeUtils extends UtilParser {
  8301. startNode() {
  8302. return new Node(this, this.state.start, this.state.startLoc);
  8303. }
  8304. startNodeAt(pos, loc) {
  8305. return new Node(this, pos, loc);
  8306. }
  8307. startNodeAtNode(type) {
  8308. return this.startNodeAt(type.start, type.loc.start);
  8309. }
  8310. finishNode(node, type) {
  8311. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  8312. }
  8313. finishNodeAt(node, type, pos, loc) {
  8314. node.type = type;
  8315. node.end = pos;
  8316. node.loc.end = loc;
  8317. if (this.options.ranges) node.range[1] = pos;
  8318. this.processComment(node);
  8319. return node;
  8320. }
  8321. resetStartLocation(node, start, startLoc) {
  8322. node.start = start;
  8323. node.loc.start = startLoc;
  8324. if (this.options.ranges) node.range[0] = start;
  8325. }
  8326. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  8327. node.end = end;
  8328. node.loc.end = endLoc;
  8329. if (this.options.ranges) node.range[1] = end;
  8330. }
  8331. resetStartLocationFromNode(node, locationNode) {
  8332. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  8333. }
  8334. }
  8335. const unwrapParenthesizedExpression = node => {
  8336. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  8337. };
  8338. class LValParser extends NodeUtils {
  8339. toAssignable(node, isLHS = false) {
  8340. var _node$extra, _node$extra3;
  8341. let parenthesized = undefined;
  8342. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  8343. parenthesized = unwrapParenthesizedExpression(node);
  8344. if (isLHS) {
  8345. if (parenthesized.type === "Identifier") {
  8346. this.expressionScope.recordParenthesizedIdentifierError(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8347. } else if (parenthesized.type !== "MemberExpression") {
  8348. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8349. }
  8350. } else {
  8351. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8352. }
  8353. }
  8354. switch (node.type) {
  8355. case "Identifier":
  8356. case "ObjectPattern":
  8357. case "ArrayPattern":
  8358. case "AssignmentPattern":
  8359. break;
  8360. case "ObjectExpression":
  8361. node.type = "ObjectPattern";
  8362. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  8363. var _node$extra2;
  8364. const prop = node.properties[i];
  8365. const isLast = i === last;
  8366. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  8367. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingComma) {
  8368. this.raiseRestNotLast(node.extra.trailingComma);
  8369. }
  8370. }
  8371. break;
  8372. case "ObjectProperty":
  8373. this.toAssignable(node.value, isLHS);
  8374. break;
  8375. case "SpreadElement":
  8376. {
  8377. this.checkToRestConversion(node);
  8378. node.type = "RestElement";
  8379. const arg = node.argument;
  8380. this.toAssignable(arg, isLHS);
  8381. break;
  8382. }
  8383. case "ArrayExpression":
  8384. node.type = "ArrayPattern";
  8385. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingComma, isLHS);
  8386. break;
  8387. case "AssignmentExpression":
  8388. if (node.operator !== "=") {
  8389. this.raise(node.left.end, ErrorMessages.MissingEqInAssignment);
  8390. }
  8391. node.type = "AssignmentPattern";
  8392. delete node.operator;
  8393. this.toAssignable(node.left, isLHS);
  8394. break;
  8395. case "ParenthesizedExpression":
  8396. this.toAssignable(parenthesized, isLHS);
  8397. break;
  8398. }
  8399. return node;
  8400. }
  8401. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  8402. if (prop.type === "ObjectMethod") {
  8403. const error = prop.kind === "get" || prop.kind === "set" ? ErrorMessages.PatternHasAccessor : ErrorMessages.PatternHasMethod;
  8404. this.raise(prop.key.start, error);
  8405. } else if (prop.type === "SpreadElement" && !isLast) {
  8406. this.raiseRestNotLast(prop.start);
  8407. } else {
  8408. this.toAssignable(prop, isLHS);
  8409. }
  8410. }
  8411. toAssignableList(exprList, trailingCommaPos, isLHS) {
  8412. let end = exprList.length;
  8413. if (end) {
  8414. const last = exprList[end - 1];
  8415. if ((last == null ? void 0 : last.type) === "RestElement") {
  8416. --end;
  8417. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  8418. last.type = "RestElement";
  8419. let arg = last.argument;
  8420. this.toAssignable(arg, isLHS);
  8421. arg = unwrapParenthesizedExpression(arg);
  8422. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  8423. this.unexpected(arg.start);
  8424. }
  8425. if (trailingCommaPos) {
  8426. this.raiseTrailingCommaAfterRest(trailingCommaPos);
  8427. }
  8428. --end;
  8429. }
  8430. }
  8431. for (let i = 0; i < end; i++) {
  8432. const elt = exprList[i];
  8433. if (elt) {
  8434. this.toAssignable(elt, isLHS);
  8435. if (elt.type === "RestElement") {
  8436. this.raiseRestNotLast(elt.start);
  8437. }
  8438. }
  8439. }
  8440. return exprList;
  8441. }
  8442. toReferencedList(exprList, isParenthesizedExpr) {
  8443. return exprList;
  8444. }
  8445. toReferencedListDeep(exprList, isParenthesizedExpr) {
  8446. this.toReferencedList(exprList, isParenthesizedExpr);
  8447. for (const expr of exprList) {
  8448. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  8449. this.toReferencedListDeep(expr.elements);
  8450. }
  8451. }
  8452. }
  8453. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  8454. const node = this.startNode();
  8455. this.next();
  8456. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  8457. return this.finishNode(node, "SpreadElement");
  8458. }
  8459. parseRestBinding() {
  8460. const node = this.startNode();
  8461. this.next();
  8462. node.argument = this.parseBindingAtom();
  8463. return this.finishNode(node, "RestElement");
  8464. }
  8465. parseBindingAtom() {
  8466. switch (this.state.type) {
  8467. case types$1.bracketL:
  8468. {
  8469. const node = this.startNode();
  8470. this.next();
  8471. node.elements = this.parseBindingList(types$1.bracketR, 93, true);
  8472. return this.finishNode(node, "ArrayPattern");
  8473. }
  8474. case types$1.braceL:
  8475. return this.parseObjectLike(types$1.braceR, true);
  8476. }
  8477. return this.parseIdentifier();
  8478. }
  8479. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  8480. const elts = [];
  8481. let first = true;
  8482. while (!this.eat(close)) {
  8483. if (first) {
  8484. first = false;
  8485. } else {
  8486. this.expect(types$1.comma);
  8487. }
  8488. if (allowEmpty && this.match(types$1.comma)) {
  8489. elts.push(null);
  8490. } else if (this.eat(close)) {
  8491. break;
  8492. } else if (this.match(types$1.ellipsis)) {
  8493. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  8494. this.checkCommaAfterRest(closeCharCode);
  8495. this.expect(close);
  8496. break;
  8497. } else {
  8498. const decorators = [];
  8499. if (this.match(types$1.at) && this.hasPlugin("decorators")) {
  8500. this.raise(this.state.start, ErrorMessages.UnsupportedParameterDecorator);
  8501. }
  8502. while (this.match(types$1.at)) {
  8503. decorators.push(this.parseDecorator());
  8504. }
  8505. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  8506. }
  8507. }
  8508. return elts;
  8509. }
  8510. parseAssignableListItem(allowModifiers, decorators) {
  8511. const left = this.parseMaybeDefault();
  8512. this.parseAssignableListItemTypes(left);
  8513. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8514. if (decorators.length) {
  8515. left.decorators = decorators;
  8516. }
  8517. return elt;
  8518. }
  8519. parseAssignableListItemTypes(param) {
  8520. return param;
  8521. }
  8522. parseMaybeDefault(startPos, startLoc, left) {
  8523. var _startLoc, _startPos, _left;
  8524. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  8525. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  8526. left = (_left = left) != null ? _left : this.parseBindingAtom();
  8527. if (!this.eat(types$1.eq)) return left;
  8528. const node = this.startNodeAt(startPos, startLoc);
  8529. node.left = left;
  8530. node.right = this.parseMaybeAssignAllowIn();
  8531. return this.finishNode(node, "AssignmentPattern");
  8532. }
  8533. checkLVal(expr, contextDescription, bindingType = BIND_NONE, checkClashes, disallowLetBinding, strictModeChanged = false) {
  8534. switch (expr.type) {
  8535. case "Identifier":
  8536. {
  8537. const {
  8538. name
  8539. } = expr;
  8540. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(name, this.inModule) : isStrictBindOnlyReservedWord(name))) {
  8541. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.StrictEvalArguments : ErrorMessages.StrictEvalArgumentsBinding, name);
  8542. }
  8543. if (checkClashes) {
  8544. if (checkClashes.has(name)) {
  8545. this.raise(expr.start, ErrorMessages.ParamDupe);
  8546. } else {
  8547. checkClashes.add(name);
  8548. }
  8549. }
  8550. if (disallowLetBinding && name === "let") {
  8551. this.raise(expr.start, ErrorMessages.LetInLexicalBinding);
  8552. }
  8553. if (!(bindingType & BIND_NONE)) {
  8554. this.scope.declareName(name, bindingType, expr.start);
  8555. }
  8556. break;
  8557. }
  8558. case "MemberExpression":
  8559. if (bindingType !== BIND_NONE) {
  8560. this.raise(expr.start, ErrorMessages.InvalidPropertyBindingPattern);
  8561. }
  8562. break;
  8563. case "ObjectPattern":
  8564. for (let prop of expr.properties) {
  8565. if (this.isObjectProperty(prop)) prop = prop.value;else if (this.isObjectMethod(prop)) continue;
  8566. this.checkLVal(prop, "object destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8567. }
  8568. break;
  8569. case "ArrayPattern":
  8570. for (const elem of expr.elements) {
  8571. if (elem) {
  8572. this.checkLVal(elem, "array destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8573. }
  8574. }
  8575. break;
  8576. case "AssignmentPattern":
  8577. this.checkLVal(expr.left, "assignment pattern", bindingType, checkClashes);
  8578. break;
  8579. case "RestElement":
  8580. this.checkLVal(expr.argument, "rest element", bindingType, checkClashes);
  8581. break;
  8582. case "ParenthesizedExpression":
  8583. this.checkLVal(expr.expression, "parenthesized expression", bindingType, checkClashes);
  8584. break;
  8585. default:
  8586. {
  8587. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.InvalidLhs : ErrorMessages.InvalidLhsBinding, contextDescription);
  8588. }
  8589. }
  8590. }
  8591. checkToRestConversion(node) {
  8592. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  8593. this.raise(node.argument.start, ErrorMessages.InvalidRestAssignmentPattern);
  8594. }
  8595. }
  8596. checkCommaAfterRest(close) {
  8597. if (this.match(types$1.comma)) {
  8598. if (this.lookaheadCharCode() === close) {
  8599. this.raiseTrailingCommaAfterRest(this.state.start);
  8600. } else {
  8601. this.raiseRestNotLast(this.state.start);
  8602. }
  8603. }
  8604. }
  8605. raiseRestNotLast(pos) {
  8606. throw this.raise(pos, ErrorMessages.ElementAfterRest);
  8607. }
  8608. raiseTrailingCommaAfterRest(pos) {
  8609. this.raise(pos, ErrorMessages.RestTrailingComma);
  8610. }
  8611. }
  8612. class ExpressionParser extends LValParser {
  8613. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  8614. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  8615. return;
  8616. }
  8617. const key = prop.key;
  8618. const name = key.type === "Identifier" ? key.name : key.value;
  8619. if (name === "__proto__") {
  8620. if (isRecord) {
  8621. this.raise(key.start, ErrorMessages.RecordNoProto);
  8622. return;
  8623. }
  8624. if (protoRef.used) {
  8625. if (refExpressionErrors) {
  8626. if (refExpressionErrors.doubleProto === -1) {
  8627. refExpressionErrors.doubleProto = key.start;
  8628. }
  8629. } else {
  8630. this.raise(key.start, ErrorMessages.DuplicateProto);
  8631. }
  8632. }
  8633. protoRef.used = true;
  8634. }
  8635. }
  8636. shouldExitDescending(expr, potentialArrowAt) {
  8637. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  8638. }
  8639. getExpression() {
  8640. let paramFlags = PARAM;
  8641. if (this.hasPlugin("topLevelAwait") && this.inModule) {
  8642. paramFlags |= PARAM_AWAIT;
  8643. }
  8644. this.scope.enter(SCOPE_PROGRAM);
  8645. this.prodParam.enter(paramFlags);
  8646. this.nextToken();
  8647. const expr = this.parseExpression();
  8648. if (!this.match(types$1.eof)) {
  8649. this.unexpected();
  8650. }
  8651. expr.comments = this.state.comments;
  8652. expr.errors = this.state.errors;
  8653. if (this.options.tokens) {
  8654. expr.tokens = this.tokens;
  8655. }
  8656. return expr;
  8657. }
  8658. parseExpression(disallowIn, refExpressionErrors) {
  8659. if (disallowIn) {
  8660. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8661. }
  8662. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8663. }
  8664. parseExpressionBase(refExpressionErrors) {
  8665. const startPos = this.state.start;
  8666. const startLoc = this.state.startLoc;
  8667. const expr = this.parseMaybeAssign(refExpressionErrors);
  8668. if (this.match(types$1.comma)) {
  8669. const node = this.startNodeAt(startPos, startLoc);
  8670. node.expressions = [expr];
  8671. while (this.eat(types$1.comma)) {
  8672. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  8673. }
  8674. this.toReferencedList(node.expressions);
  8675. return this.finishNode(node, "SequenceExpression");
  8676. }
  8677. return expr;
  8678. }
  8679. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8680. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
  8681. }
  8682. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8683. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
  8684. }
  8685. parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
  8686. const startPos = this.state.start;
  8687. const startLoc = this.state.startLoc;
  8688. if (this.isContextual("yield")) {
  8689. if (this.prodParam.hasYield) {
  8690. let left = this.parseYield();
  8691. if (afterLeftParse) {
  8692. left = afterLeftParse.call(this, left, startPos, startLoc);
  8693. }
  8694. return left;
  8695. }
  8696. }
  8697. let ownExpressionErrors;
  8698. if (refExpressionErrors) {
  8699. ownExpressionErrors = false;
  8700. } else {
  8701. refExpressionErrors = new ExpressionErrors();
  8702. ownExpressionErrors = true;
  8703. }
  8704. if (this.match(types$1.parenL) || this.match(types$1.name)) {
  8705. this.state.potentialArrowAt = this.state.start;
  8706. }
  8707. let left = this.parseMaybeConditional(refExpressionErrors, refNeedsArrowPos);
  8708. if (afterLeftParse) {
  8709. left = afterLeftParse.call(this, left, startPos, startLoc);
  8710. }
  8711. if (this.state.type.isAssign) {
  8712. const node = this.startNodeAt(startPos, startLoc);
  8713. const operator = this.state.value;
  8714. node.operator = operator;
  8715. if (this.match(types$1.eq)) {
  8716. node.left = this.toAssignable(left, true);
  8717. refExpressionErrors.doubleProto = -1;
  8718. } else {
  8719. node.left = left;
  8720. }
  8721. if (refExpressionErrors.shorthandAssign >= node.left.start) {
  8722. refExpressionErrors.shorthandAssign = -1;
  8723. }
  8724. this.checkLVal(left, "assignment expression");
  8725. this.next();
  8726. node.right = this.parseMaybeAssign();
  8727. return this.finishNode(node, "AssignmentExpression");
  8728. } else if (ownExpressionErrors) {
  8729. this.checkExpressionErrors(refExpressionErrors, true);
  8730. }
  8731. return left;
  8732. }
  8733. parseMaybeConditional(refExpressionErrors, refNeedsArrowPos) {
  8734. const startPos = this.state.start;
  8735. const startLoc = this.state.startLoc;
  8736. const potentialArrowAt = this.state.potentialArrowAt;
  8737. const expr = this.parseExprOps(refExpressionErrors);
  8738. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8739. return expr;
  8740. }
  8741. return this.parseConditional(expr, startPos, startLoc, refNeedsArrowPos);
  8742. }
  8743. parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
  8744. if (this.eat(types$1.question)) {
  8745. const node = this.startNodeAt(startPos, startLoc);
  8746. node.test = expr;
  8747. node.consequent = this.parseMaybeAssignAllowIn();
  8748. this.expect(types$1.colon);
  8749. node.alternate = this.parseMaybeAssign();
  8750. return this.finishNode(node, "ConditionalExpression");
  8751. }
  8752. return expr;
  8753. }
  8754. parseExprOps(refExpressionErrors) {
  8755. const startPos = this.state.start;
  8756. const startLoc = this.state.startLoc;
  8757. const potentialArrowAt = this.state.potentialArrowAt;
  8758. const expr = this.parseMaybeUnary(refExpressionErrors);
  8759. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8760. return expr;
  8761. }
  8762. return this.parseExprOp(expr, startPos, startLoc, -1);
  8763. }
  8764. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8765. let prec = this.state.type.binop;
  8766. if (prec != null && (this.prodParam.hasIn || !this.match(types$1._in))) {
  8767. if (prec > minPrec) {
  8768. const op = this.state.type;
  8769. if (op === types$1.pipeline) {
  8770. this.expectPlugin("pipelineOperator");
  8771. if (this.state.inFSharpPipelineDirectBody) {
  8772. return left;
  8773. }
  8774. this.state.inPipeline = true;
  8775. this.checkPipelineAtInfixOperator(left, leftStartPos);
  8776. }
  8777. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8778. node.left = left;
  8779. node.operator = this.state.value;
  8780. const logical = op === types$1.logicalOR || op === types$1.logicalAND;
  8781. const coalesce = op === types$1.nullishCoalescing;
  8782. if (coalesce) {
  8783. prec = types$1.logicalAND.binop;
  8784. }
  8785. this.next();
  8786. if (op === types$1.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  8787. if (this.match(types$1.name) && this.state.value === "await" && this.prodParam.hasAwait) {
  8788. throw this.raise(this.state.start, ErrorMessages.UnexpectedAwaitAfterPipelineBody);
  8789. }
  8790. }
  8791. node.right = this.parseExprOpRightExpr(op, prec);
  8792. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  8793. const nextOp = this.state.type;
  8794. if (coalesce && (nextOp === types$1.logicalOR || nextOp === types$1.logicalAND) || logical && nextOp === types$1.nullishCoalescing) {
  8795. throw this.raise(this.state.start, ErrorMessages.MixingCoalesceWithLogical);
  8796. }
  8797. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8798. }
  8799. }
  8800. return left;
  8801. }
  8802. parseExprOpRightExpr(op, prec) {
  8803. const startPos = this.state.start;
  8804. const startLoc = this.state.startLoc;
  8805. switch (op) {
  8806. case types$1.pipeline:
  8807. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  8808. case "smart":
  8809. return this.withTopicPermittingContext(() => {
  8810. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  8811. });
  8812. case "fsharp":
  8813. return this.withSoloAwaitPermittingContext(() => {
  8814. return this.parseFSharpPipelineBody(prec);
  8815. });
  8816. }
  8817. default:
  8818. return this.parseExprOpBaseRightExpr(op, prec);
  8819. }
  8820. }
  8821. parseExprOpBaseRightExpr(op, prec) {
  8822. const startPos = this.state.start;
  8823. const startLoc = this.state.startLoc;
  8824. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec);
  8825. }
  8826. checkExponentialAfterUnary(node) {
  8827. if (this.match(types$1.exponent)) {
  8828. this.raise(node.argument.start, ErrorMessages.UnexpectedTokenUnaryExponentiation);
  8829. }
  8830. }
  8831. parseMaybeUnary(refExpressionErrors, sawUnary) {
  8832. const startPos = this.state.start;
  8833. const startLoc = this.state.startLoc;
  8834. const isAwait = this.isContextual("await");
  8835. if (isAwait && this.isAwaitAllowed()) {
  8836. this.next();
  8837. const expr = this.parseAwait(startPos, startLoc);
  8838. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  8839. return expr;
  8840. }
  8841. if (this.isContextual("module") && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  8842. return this.parseModuleExpression();
  8843. }
  8844. const update = this.match(types$1.incDec);
  8845. const node = this.startNode();
  8846. if (this.state.type.prefix) {
  8847. node.operator = this.state.value;
  8848. node.prefix = true;
  8849. if (this.match(types$1._throw)) {
  8850. this.expectPlugin("throwExpressions");
  8851. }
  8852. const isDelete = this.match(types$1._delete);
  8853. this.next();
  8854. node.argument = this.parseMaybeUnary(null, true);
  8855. this.checkExpressionErrors(refExpressionErrors, true);
  8856. if (this.state.strict && isDelete) {
  8857. const arg = node.argument;
  8858. if (arg.type === "Identifier") {
  8859. this.raise(node.start, ErrorMessages.StrictDelete);
  8860. } else if (this.hasPropertyAsPrivateName(arg)) {
  8861. this.raise(node.start, ErrorMessages.DeletePrivateField);
  8862. }
  8863. }
  8864. if (!update) {
  8865. if (!sawUnary) this.checkExponentialAfterUnary(node);
  8866. return this.finishNode(node, "UnaryExpression");
  8867. }
  8868. }
  8869. const expr = this.parseUpdate(node, update, refExpressionErrors);
  8870. if (isAwait) {
  8871. const startsExpr = this.hasPlugin("v8intrinsic") ? this.state.type.startsExpr : this.state.type.startsExpr && !this.match(types$1.modulo);
  8872. if (startsExpr && !this.isAmbiguousAwait()) {
  8873. this.raiseOverwrite(startPos, this.hasPlugin("topLevelAwait") ? ErrorMessages.AwaitNotInAsyncContext : ErrorMessages.AwaitNotInAsyncFunction);
  8874. return this.parseAwait(startPos, startLoc);
  8875. }
  8876. }
  8877. return expr;
  8878. }
  8879. parseUpdate(node, update, refExpressionErrors) {
  8880. if (update) {
  8881. this.checkLVal(node.argument, "prefix operation");
  8882. return this.finishNode(node, "UpdateExpression");
  8883. }
  8884. const startPos = this.state.start;
  8885. const startLoc = this.state.startLoc;
  8886. let expr = this.parseExprSubscripts(refExpressionErrors);
  8887. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  8888. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  8889. const node = this.startNodeAt(startPos, startLoc);
  8890. node.operator = this.state.value;
  8891. node.prefix = false;
  8892. node.argument = expr;
  8893. this.checkLVal(expr, "postfix operation");
  8894. this.next();
  8895. expr = this.finishNode(node, "UpdateExpression");
  8896. }
  8897. return expr;
  8898. }
  8899. parseExprSubscripts(refExpressionErrors) {
  8900. const startPos = this.state.start;
  8901. const startLoc = this.state.startLoc;
  8902. const potentialArrowAt = this.state.potentialArrowAt;
  8903. const expr = this.parseExprAtom(refExpressionErrors);
  8904. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8905. return expr;
  8906. }
  8907. return this.parseSubscripts(expr, startPos, startLoc);
  8908. }
  8909. parseSubscripts(base, startPos, startLoc, noCalls) {
  8910. const state = {
  8911. optionalChainMember: false,
  8912. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  8913. stop: false
  8914. };
  8915. do {
  8916. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  8917. state.maybeAsyncArrow = false;
  8918. } while (!state.stop);
  8919. return base;
  8920. }
  8921. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8922. if (!noCalls && this.eat(types$1.doubleColon)) {
  8923. return this.parseBind(base, startPos, startLoc, noCalls, state);
  8924. } else if (this.match(types$1.backQuote)) {
  8925. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8926. }
  8927. let optional = false;
  8928. if (this.match(types$1.questionDot)) {
  8929. if (noCalls && this.lookaheadCharCode() === 40) {
  8930. state.stop = true;
  8931. return base;
  8932. }
  8933. state.optionalChainMember = optional = true;
  8934. this.next();
  8935. }
  8936. if (!noCalls && this.match(types$1.parenL)) {
  8937. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  8938. } else if (optional || this.match(types$1.bracketL) || this.eat(types$1.dot)) {
  8939. return this.parseMember(base, startPos, startLoc, state, optional);
  8940. } else {
  8941. state.stop = true;
  8942. return base;
  8943. }
  8944. }
  8945. parseMember(base, startPos, startLoc, state, optional) {
  8946. const node = this.startNodeAt(startPos, startLoc);
  8947. const computed = this.eat(types$1.bracketL);
  8948. node.object = base;
  8949. node.computed = computed;
  8950. const privateName = !computed && this.match(types$1.privateName) && this.state.value;
  8951. const property = computed ? this.parseExpression() : privateName ? this.parsePrivateName() : this.parseIdentifier(true);
  8952. if (privateName !== false) {
  8953. if (node.object.type === "Super") {
  8954. this.raise(startPos, ErrorMessages.SuperPrivateField);
  8955. }
  8956. this.classScope.usePrivateName(privateName, property.start);
  8957. }
  8958. node.property = property;
  8959. if (computed) {
  8960. this.expect(types$1.bracketR);
  8961. }
  8962. if (state.optionalChainMember) {
  8963. node.optional = optional;
  8964. return this.finishNode(node, "OptionalMemberExpression");
  8965. } else {
  8966. return this.finishNode(node, "MemberExpression");
  8967. }
  8968. }
  8969. parseBind(base, startPos, startLoc, noCalls, state) {
  8970. const node = this.startNodeAt(startPos, startLoc);
  8971. node.object = base;
  8972. node.callee = this.parseNoCallExpr();
  8973. state.stop = true;
  8974. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  8975. }
  8976. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  8977. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8978. let refExpressionErrors = null;
  8979. this.state.maybeInArrowParameters = true;
  8980. this.next();
  8981. let node = this.startNodeAt(startPos, startLoc);
  8982. node.callee = base;
  8983. if (state.maybeAsyncArrow) {
  8984. this.expressionScope.enter(newAsyncArrowScope());
  8985. refExpressionErrors = new ExpressionErrors();
  8986. }
  8987. if (state.optionalChainMember) {
  8988. node.optional = optional;
  8989. }
  8990. if (optional) {
  8991. node.arguments = this.parseCallExpressionArguments(types$1.parenR);
  8992. } else {
  8993. node.arguments = this.parseCallExpressionArguments(types$1.parenR, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  8994. }
  8995. this.finishCallExpression(node, state.optionalChainMember);
  8996. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  8997. state.stop = true;
  8998. this.expressionScope.validateAsPattern();
  8999. this.expressionScope.exit();
  9000. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  9001. } else {
  9002. if (state.maybeAsyncArrow) {
  9003. this.checkExpressionErrors(refExpressionErrors, true);
  9004. this.expressionScope.exit();
  9005. }
  9006. this.toReferencedArguments(node);
  9007. }
  9008. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9009. return node;
  9010. }
  9011. toReferencedArguments(node, isParenthesizedExpr) {
  9012. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  9013. }
  9014. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  9015. const node = this.startNodeAt(startPos, startLoc);
  9016. node.tag = base;
  9017. node.quasi = this.parseTemplate(true);
  9018. if (state.optionalChainMember) {
  9019. this.raise(startPos, ErrorMessages.OptionalChainingNoTemplate);
  9020. }
  9021. return this.finishNode(node, "TaggedTemplateExpression");
  9022. }
  9023. atPossibleAsyncArrow(base) {
  9024. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  9025. }
  9026. finishCallExpression(node, optional) {
  9027. if (node.callee.type === "Import") {
  9028. if (node.arguments.length === 2) {
  9029. {
  9030. if (!this.hasPlugin("moduleAttributes")) {
  9031. this.expectPlugin("importAssertions");
  9032. }
  9033. }
  9034. }
  9035. if (node.arguments.length === 0 || node.arguments.length > 2) {
  9036. this.raise(node.start, ErrorMessages.ImportCallArity, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
  9037. } else {
  9038. for (const arg of node.arguments) {
  9039. if (arg.type === "SpreadElement") {
  9040. this.raise(arg.start, ErrorMessages.ImportCallSpreadArgument);
  9041. }
  9042. }
  9043. }
  9044. }
  9045. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  9046. }
  9047. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  9048. const elts = [];
  9049. let first = true;
  9050. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9051. this.state.inFSharpPipelineDirectBody = false;
  9052. while (!this.eat(close)) {
  9053. if (first) {
  9054. first = false;
  9055. } else {
  9056. this.expect(types$1.comma);
  9057. if (this.match(close)) {
  9058. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  9059. this.raise(this.state.lastTokStart, ErrorMessages.ImportCallArgumentTrailingComma);
  9060. }
  9061. if (nodeForExtra) {
  9062. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9063. }
  9064. this.next();
  9065. break;
  9066. }
  9067. }
  9068. elts.push(this.parseExprListItem(false, refExpressionErrors, {
  9069. start: 0
  9070. }, allowPlaceholder));
  9071. }
  9072. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9073. return elts;
  9074. }
  9075. shouldParseAsyncArrow() {
  9076. return this.match(types$1.arrow) && !this.canInsertSemicolon();
  9077. }
  9078. parseAsyncArrowFromCallExpression(node, call) {
  9079. var _call$extra;
  9080. this.expect(types$1.arrow);
  9081. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingComma);
  9082. return node;
  9083. }
  9084. parseNoCallExpr() {
  9085. const startPos = this.state.start;
  9086. const startLoc = this.state.startLoc;
  9087. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  9088. }
  9089. parseExprAtom(refExpressionErrors) {
  9090. let node;
  9091. switch (this.state.type) {
  9092. case types$1._super:
  9093. return this.parseSuper();
  9094. case types$1._import:
  9095. node = this.startNode();
  9096. this.next();
  9097. if (this.match(types$1.dot)) {
  9098. return this.parseImportMetaProperty(node);
  9099. }
  9100. if (!this.match(types$1.parenL)) {
  9101. this.raise(this.state.lastTokStart, ErrorMessages.UnsupportedImport);
  9102. }
  9103. return this.finishNode(node, "Import");
  9104. case types$1._this:
  9105. node = this.startNode();
  9106. this.next();
  9107. return this.finishNode(node, "ThisExpression");
  9108. case types$1.name:
  9109. {
  9110. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9111. const containsEsc = this.state.containsEsc;
  9112. const id = this.parseIdentifier();
  9113. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  9114. if (this.match(types$1._function)) {
  9115. this.next();
  9116. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  9117. } else if (this.match(types$1.name)) {
  9118. if (this.lookaheadCharCode() === 61) {
  9119. return this.parseAsyncArrowUnaryFunction(id);
  9120. } else {
  9121. return id;
  9122. }
  9123. } else if (this.match(types$1._do)) {
  9124. return this.parseDo(true);
  9125. }
  9126. }
  9127. if (canBeArrow && this.match(types$1.arrow) && !this.canInsertSemicolon()) {
  9128. this.next();
  9129. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  9130. }
  9131. return id;
  9132. }
  9133. case types$1._do:
  9134. {
  9135. return this.parseDo(false);
  9136. }
  9137. case types$1.slash:
  9138. case types$1.slashAssign:
  9139. {
  9140. this.readRegexp();
  9141. return this.parseRegExpLiteral(this.state.value);
  9142. }
  9143. case types$1.num:
  9144. return this.parseNumericLiteral(this.state.value);
  9145. case types$1.bigint:
  9146. return this.parseBigIntLiteral(this.state.value);
  9147. case types$1.decimal:
  9148. return this.parseDecimalLiteral(this.state.value);
  9149. case types$1.string:
  9150. return this.parseStringLiteral(this.state.value);
  9151. case types$1._null:
  9152. return this.parseNullLiteral();
  9153. case types$1._true:
  9154. return this.parseBooleanLiteral(true);
  9155. case types$1._false:
  9156. return this.parseBooleanLiteral(false);
  9157. case types$1.parenL:
  9158. {
  9159. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9160. return this.parseParenAndDistinguishExpression(canBeArrow);
  9161. }
  9162. case types$1.bracketBarL:
  9163. case types$1.bracketHashL:
  9164. {
  9165. return this.parseArrayLike(this.state.type === types$1.bracketBarL ? types$1.bracketBarR : types$1.bracketR, false, true, refExpressionErrors);
  9166. }
  9167. case types$1.bracketL:
  9168. {
  9169. return this.parseArrayLike(types$1.bracketR, true, false, refExpressionErrors);
  9170. }
  9171. case types$1.braceBarL:
  9172. case types$1.braceHashL:
  9173. {
  9174. return this.parseObjectLike(this.state.type === types$1.braceBarL ? types$1.braceBarR : types$1.braceR, false, true, refExpressionErrors);
  9175. }
  9176. case types$1.braceL:
  9177. {
  9178. return this.parseObjectLike(types$1.braceR, false, false, refExpressionErrors);
  9179. }
  9180. case types$1._function:
  9181. return this.parseFunctionOrFunctionSent();
  9182. case types$1.at:
  9183. this.parseDecorators();
  9184. case types$1._class:
  9185. node = this.startNode();
  9186. this.takeDecorators(node);
  9187. return this.parseClass(node, false);
  9188. case types$1._new:
  9189. return this.parseNewOrNewTarget();
  9190. case types$1.backQuote:
  9191. return this.parseTemplate(false);
  9192. case types$1.doubleColon:
  9193. {
  9194. node = this.startNode();
  9195. this.next();
  9196. node.object = null;
  9197. const callee = node.callee = this.parseNoCallExpr();
  9198. if (callee.type === "MemberExpression") {
  9199. return this.finishNode(node, "BindExpression");
  9200. } else {
  9201. throw this.raise(callee.start, ErrorMessages.UnsupportedBind);
  9202. }
  9203. }
  9204. case types$1.privateName:
  9205. {
  9206. const start = this.state.start;
  9207. const value = this.state.value;
  9208. node = this.parsePrivateName();
  9209. if (this.match(types$1._in)) {
  9210. this.expectPlugin("privateIn");
  9211. this.classScope.usePrivateName(value, node.start);
  9212. } else if (this.hasPlugin("privateIn")) {
  9213. this.raise(this.state.start, ErrorMessages.PrivateInExpectedIn, value);
  9214. } else {
  9215. throw this.unexpected(start);
  9216. }
  9217. return node;
  9218. }
  9219. case types$1.hash:
  9220. {
  9221. if (this.state.inPipeline) {
  9222. node = this.startNode();
  9223. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  9224. this.raise(node.start, ErrorMessages.PrimaryTopicRequiresSmartPipeline);
  9225. }
  9226. this.next();
  9227. if (!this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  9228. this.raise(node.start, ErrorMessages.PrimaryTopicNotAllowed);
  9229. }
  9230. this.registerTopicReference();
  9231. return this.finishNode(node, "PipelinePrimaryTopicReference");
  9232. }
  9233. }
  9234. case types$1.relational:
  9235. {
  9236. if (this.state.value === "<") {
  9237. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  9238. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  9239. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  9240. }
  9241. }
  9242. }
  9243. default:
  9244. throw this.unexpected();
  9245. }
  9246. }
  9247. parseAsyncArrowUnaryFunction(id) {
  9248. const node = this.startNodeAtNode(id);
  9249. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  9250. const params = [this.parseIdentifier()];
  9251. this.prodParam.exit();
  9252. if (this.hasPrecedingLineBreak()) {
  9253. this.raise(this.state.pos, ErrorMessages.LineTerminatorBeforeArrow);
  9254. }
  9255. this.expect(types$1.arrow);
  9256. this.parseArrowExpression(node, params, true);
  9257. return node;
  9258. }
  9259. parseDo(isAsync) {
  9260. this.expectPlugin("doExpressions");
  9261. if (isAsync) {
  9262. this.expectPlugin("asyncDoExpressions");
  9263. }
  9264. const node = this.startNode();
  9265. node.async = isAsync;
  9266. this.next();
  9267. const oldLabels = this.state.labels;
  9268. this.state.labels = [];
  9269. if (isAsync) {
  9270. this.prodParam.enter(PARAM_AWAIT);
  9271. node.body = this.parseBlock();
  9272. this.prodParam.exit();
  9273. } else {
  9274. node.body = this.parseBlock();
  9275. }
  9276. this.state.labels = oldLabels;
  9277. return this.finishNode(node, "DoExpression");
  9278. }
  9279. parseSuper() {
  9280. const node = this.startNode();
  9281. this.next();
  9282. if (this.match(types$1.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  9283. this.raise(node.start, ErrorMessages.SuperNotAllowed);
  9284. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  9285. this.raise(node.start, ErrorMessages.UnexpectedSuper);
  9286. }
  9287. if (!this.match(types$1.parenL) && !this.match(types$1.bracketL) && !this.match(types$1.dot)) {
  9288. this.raise(node.start, ErrorMessages.UnsupportedSuper);
  9289. }
  9290. return this.finishNode(node, "Super");
  9291. }
  9292. parseMaybePrivateName(isPrivateNameAllowed) {
  9293. const isPrivate = this.match(types$1.privateName);
  9294. if (isPrivate) {
  9295. if (!isPrivateNameAllowed) {
  9296. this.raise(this.state.start + 1, ErrorMessages.UnexpectedPrivateField);
  9297. }
  9298. return this.parsePrivateName();
  9299. } else {
  9300. return this.parseIdentifier(true);
  9301. }
  9302. }
  9303. parsePrivateName() {
  9304. const node = this.startNode();
  9305. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart));
  9306. const name = this.state.value;
  9307. this.next();
  9308. node.id = this.createIdentifier(id, name);
  9309. return this.finishNode(node, "PrivateName");
  9310. }
  9311. parseFunctionOrFunctionSent() {
  9312. const node = this.startNode();
  9313. this.next();
  9314. if (this.prodParam.hasYield && this.match(types$1.dot)) {
  9315. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  9316. this.next();
  9317. return this.parseMetaProperty(node, meta, "sent");
  9318. }
  9319. return this.parseFunction(node);
  9320. }
  9321. parseMetaProperty(node, meta, propertyName) {
  9322. node.meta = meta;
  9323. if (meta.name === "function" && propertyName === "sent") {
  9324. if (this.isContextual(propertyName)) {
  9325. this.expectPlugin("functionSent");
  9326. } else if (!this.hasPlugin("functionSent")) {
  9327. this.unexpected();
  9328. }
  9329. }
  9330. const containsEsc = this.state.containsEsc;
  9331. node.property = this.parseIdentifier(true);
  9332. if (node.property.name !== propertyName || containsEsc) {
  9333. this.raise(node.property.start, ErrorMessages.UnsupportedMetaProperty, meta.name, propertyName);
  9334. }
  9335. return this.finishNode(node, "MetaProperty");
  9336. }
  9337. parseImportMetaProperty(node) {
  9338. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  9339. this.next();
  9340. if (this.isContextual("meta")) {
  9341. if (!this.inModule) {
  9342. this.raise(id.start, SourceTypeModuleErrorMessages.ImportMetaOutsideModule);
  9343. }
  9344. this.sawUnambiguousESM = true;
  9345. }
  9346. return this.parseMetaProperty(node, id, "meta");
  9347. }
  9348. parseLiteralAtNode(value, type, node) {
  9349. this.addExtra(node, "rawValue", value);
  9350. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  9351. node.value = value;
  9352. this.next();
  9353. return this.finishNode(node, type);
  9354. }
  9355. parseLiteral(value, type) {
  9356. const node = this.startNode();
  9357. return this.parseLiteralAtNode(value, type, node);
  9358. }
  9359. parseStringLiteral(value) {
  9360. return this.parseLiteral(value, "StringLiteral");
  9361. }
  9362. parseNumericLiteral(value) {
  9363. return this.parseLiteral(value, "NumericLiteral");
  9364. }
  9365. parseBigIntLiteral(value) {
  9366. return this.parseLiteral(value, "BigIntLiteral");
  9367. }
  9368. parseDecimalLiteral(value) {
  9369. return this.parseLiteral(value, "DecimalLiteral");
  9370. }
  9371. parseRegExpLiteral(value) {
  9372. const node = this.parseLiteral(value.value, "RegExpLiteral");
  9373. node.pattern = value.pattern;
  9374. node.flags = value.flags;
  9375. return node;
  9376. }
  9377. parseBooleanLiteral(value) {
  9378. const node = this.startNode();
  9379. node.value = value;
  9380. this.next();
  9381. return this.finishNode(node, "BooleanLiteral");
  9382. }
  9383. parseNullLiteral() {
  9384. const node = this.startNode();
  9385. this.next();
  9386. return this.finishNode(node, "NullLiteral");
  9387. }
  9388. parseParenAndDistinguishExpression(canBeArrow) {
  9389. const startPos = this.state.start;
  9390. const startLoc = this.state.startLoc;
  9391. let val;
  9392. this.next();
  9393. this.expressionScope.enter(newArrowHeadScope());
  9394. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9395. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9396. this.state.maybeInArrowParameters = true;
  9397. this.state.inFSharpPipelineDirectBody = false;
  9398. const innerStartPos = this.state.start;
  9399. const innerStartLoc = this.state.startLoc;
  9400. const exprList = [];
  9401. const refExpressionErrors = new ExpressionErrors();
  9402. const refNeedsArrowPos = {
  9403. start: 0
  9404. };
  9405. let first = true;
  9406. let spreadStart;
  9407. let optionalCommaStart;
  9408. while (!this.match(types$1.parenR)) {
  9409. if (first) {
  9410. first = false;
  9411. } else {
  9412. this.expect(types$1.comma, refNeedsArrowPos.start || null);
  9413. if (this.match(types$1.parenR)) {
  9414. optionalCommaStart = this.state.start;
  9415. break;
  9416. }
  9417. }
  9418. if (this.match(types$1.ellipsis)) {
  9419. const spreadNodeStartPos = this.state.start;
  9420. const spreadNodeStartLoc = this.state.startLoc;
  9421. spreadStart = this.state.start;
  9422. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  9423. this.checkCommaAfterRest(41);
  9424. break;
  9425. } else {
  9426. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos));
  9427. }
  9428. }
  9429. const innerEndPos = this.state.lastTokEnd;
  9430. const innerEndLoc = this.state.lastTokEndLoc;
  9431. this.expect(types$1.parenR);
  9432. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9433. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9434. let arrowNode = this.startNodeAt(startPos, startLoc);
  9435. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  9436. this.expressionScope.validateAsPattern();
  9437. this.expressionScope.exit();
  9438. this.parseArrowExpression(arrowNode, exprList, false);
  9439. return arrowNode;
  9440. }
  9441. this.expressionScope.exit();
  9442. if (!exprList.length) {
  9443. this.unexpected(this.state.lastTokStart);
  9444. }
  9445. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  9446. if (spreadStart) this.unexpected(spreadStart);
  9447. this.checkExpressionErrors(refExpressionErrors, true);
  9448. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  9449. this.toReferencedListDeep(exprList, true);
  9450. if (exprList.length > 1) {
  9451. val = this.startNodeAt(innerStartPos, innerStartLoc);
  9452. val.expressions = exprList;
  9453. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  9454. } else {
  9455. val = exprList[0];
  9456. }
  9457. if (!this.options.createParenthesizedExpressions) {
  9458. this.addExtra(val, "parenthesized", true);
  9459. this.addExtra(val, "parenStart", startPos);
  9460. return val;
  9461. }
  9462. const parenExpression = this.startNodeAt(startPos, startLoc);
  9463. parenExpression.expression = val;
  9464. this.finishNode(parenExpression, "ParenthesizedExpression");
  9465. return parenExpression;
  9466. }
  9467. shouldParseArrow() {
  9468. return !this.canInsertSemicolon();
  9469. }
  9470. parseArrow(node) {
  9471. if (this.eat(types$1.arrow)) {
  9472. return node;
  9473. }
  9474. }
  9475. parseParenItem(node, startPos, startLoc) {
  9476. return node;
  9477. }
  9478. parseNewOrNewTarget() {
  9479. const node = this.startNode();
  9480. this.next();
  9481. if (this.match(types$1.dot)) {
  9482. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  9483. this.next();
  9484. const metaProp = this.parseMetaProperty(node, meta, "target");
  9485. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  9486. this.raise(metaProp.start, ErrorMessages.UnexpectedNewTarget);
  9487. }
  9488. return metaProp;
  9489. }
  9490. return this.parseNew(node);
  9491. }
  9492. parseNew(node) {
  9493. node.callee = this.parseNoCallExpr();
  9494. if (node.callee.type === "Import") {
  9495. this.raise(node.callee.start, ErrorMessages.ImportCallNotNewExpression);
  9496. } else if (this.isOptionalChain(node.callee)) {
  9497. this.raise(this.state.lastTokEnd, ErrorMessages.OptionalChainingNoNew);
  9498. } else if (this.eat(types$1.questionDot)) {
  9499. this.raise(this.state.start, ErrorMessages.OptionalChainingNoNew);
  9500. }
  9501. this.parseNewArguments(node);
  9502. return this.finishNode(node, "NewExpression");
  9503. }
  9504. parseNewArguments(node) {
  9505. if (this.eat(types$1.parenL)) {
  9506. const args = this.parseExprList(types$1.parenR);
  9507. this.toReferencedList(args);
  9508. node.arguments = args;
  9509. } else {
  9510. node.arguments = [];
  9511. }
  9512. }
  9513. parseTemplateElement(isTagged) {
  9514. const elem = this.startNode();
  9515. if (this.state.value === null) {
  9516. if (!isTagged) {
  9517. this.raise(this.state.start + 1, ErrorMessages.InvalidEscapeSequenceTemplate);
  9518. }
  9519. }
  9520. elem.value = {
  9521. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  9522. cooked: this.state.value
  9523. };
  9524. this.next();
  9525. elem.tail = this.match(types$1.backQuote);
  9526. return this.finishNode(elem, "TemplateElement");
  9527. }
  9528. parseTemplate(isTagged) {
  9529. const node = this.startNode();
  9530. this.next();
  9531. node.expressions = [];
  9532. let curElt = this.parseTemplateElement(isTagged);
  9533. node.quasis = [curElt];
  9534. while (!curElt.tail) {
  9535. this.expect(types$1.dollarBraceL);
  9536. node.expressions.push(this.parseTemplateSubstitution());
  9537. this.expect(types$1.braceR);
  9538. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  9539. }
  9540. this.next();
  9541. return this.finishNode(node, "TemplateLiteral");
  9542. }
  9543. parseTemplateSubstitution() {
  9544. return this.parseExpression();
  9545. }
  9546. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  9547. if (isRecord) {
  9548. this.expectPlugin("recordAndTuple");
  9549. }
  9550. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9551. this.state.inFSharpPipelineDirectBody = false;
  9552. const propHash = Object.create(null);
  9553. let first = true;
  9554. const node = this.startNode();
  9555. node.properties = [];
  9556. this.next();
  9557. while (!this.match(close)) {
  9558. if (first) {
  9559. first = false;
  9560. } else {
  9561. this.expect(types$1.comma);
  9562. if (this.match(close)) {
  9563. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  9564. break;
  9565. }
  9566. }
  9567. const prop = this.parsePropertyDefinition(isPattern, refExpressionErrors);
  9568. if (!isPattern) {
  9569. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  9570. }
  9571. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  9572. this.raise(prop.start, ErrorMessages.InvalidRecordProperty);
  9573. }
  9574. if (prop.shorthand) {
  9575. this.addExtra(prop, "shorthand", true);
  9576. }
  9577. node.properties.push(prop);
  9578. }
  9579. this.next();
  9580. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9581. let type = "ObjectExpression";
  9582. if (isPattern) {
  9583. type = "ObjectPattern";
  9584. } else if (isRecord) {
  9585. type = "RecordExpression";
  9586. }
  9587. return this.finishNode(node, type);
  9588. }
  9589. maybeAsyncOrAccessorProp(prop) {
  9590. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(types$1.bracketL) || this.match(types$1.star));
  9591. }
  9592. parsePropertyDefinition(isPattern, refExpressionErrors) {
  9593. let decorators = [];
  9594. if (this.match(types$1.at)) {
  9595. if (this.hasPlugin("decorators")) {
  9596. this.raise(this.state.start, ErrorMessages.UnsupportedPropertyDecorator);
  9597. }
  9598. while (this.match(types$1.at)) {
  9599. decorators.push(this.parseDecorator());
  9600. }
  9601. }
  9602. const prop = this.startNode();
  9603. let isGenerator = false;
  9604. let isAsync = false;
  9605. let isAccessor = false;
  9606. let startPos;
  9607. let startLoc;
  9608. if (this.match(types$1.ellipsis)) {
  9609. if (decorators.length) this.unexpected();
  9610. if (isPattern) {
  9611. this.next();
  9612. prop.argument = this.parseIdentifier();
  9613. this.checkCommaAfterRest(125);
  9614. return this.finishNode(prop, "RestElement");
  9615. }
  9616. return this.parseSpread();
  9617. }
  9618. if (decorators.length) {
  9619. prop.decorators = decorators;
  9620. decorators = [];
  9621. }
  9622. prop.method = false;
  9623. if (isPattern || refExpressionErrors) {
  9624. startPos = this.state.start;
  9625. startLoc = this.state.startLoc;
  9626. }
  9627. if (!isPattern) {
  9628. isGenerator = this.eat(types$1.star);
  9629. }
  9630. const containsEsc = this.state.containsEsc;
  9631. const key = this.parsePropertyName(prop, false);
  9632. if (!isPattern && !isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  9633. const keyName = key.name;
  9634. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  9635. isAsync = true;
  9636. isGenerator = this.eat(types$1.star);
  9637. this.parsePropertyName(prop, false);
  9638. }
  9639. if (keyName === "get" || keyName === "set") {
  9640. isAccessor = true;
  9641. prop.kind = keyName;
  9642. if (this.match(types$1.star)) {
  9643. isGenerator = true;
  9644. this.raise(this.state.pos, ErrorMessages.AccessorIsGenerator, keyName);
  9645. this.next();
  9646. }
  9647. this.parsePropertyName(prop, false);
  9648. }
  9649. }
  9650. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9651. return prop;
  9652. }
  9653. getGetterSetterExpectedParamCount(method) {
  9654. return method.kind === "get" ? 0 : 1;
  9655. }
  9656. getObjectOrClassMethodParams(method) {
  9657. return method.params;
  9658. }
  9659. checkGetterSetterParams(method) {
  9660. var _params;
  9661. const paramCount = this.getGetterSetterExpectedParamCount(method);
  9662. const params = this.getObjectOrClassMethodParams(method);
  9663. const start = method.start;
  9664. if (params.length !== paramCount) {
  9665. if (method.kind === "get") {
  9666. this.raise(start, ErrorMessages.BadGetterArity);
  9667. } else {
  9668. this.raise(start, ErrorMessages.BadSetterArity);
  9669. }
  9670. }
  9671. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  9672. this.raise(start, ErrorMessages.BadSetterRestParameter);
  9673. }
  9674. }
  9675. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  9676. if (isAccessor) {
  9677. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  9678. this.checkGetterSetterParams(prop);
  9679. return prop;
  9680. }
  9681. if (isAsync || isGenerator || this.match(types$1.parenL)) {
  9682. if (isPattern) this.unexpected();
  9683. prop.kind = "method";
  9684. prop.method = true;
  9685. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  9686. }
  9687. }
  9688. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  9689. prop.shorthand = false;
  9690. if (this.eat(types$1.colon)) {
  9691. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  9692. return this.finishNode(prop, "ObjectProperty");
  9693. }
  9694. if (!prop.computed && prop.key.type === "Identifier") {
  9695. this.checkReservedWord(prop.key.name, prop.key.start, true, false);
  9696. if (isPattern) {
  9697. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9698. } else if (this.match(types$1.eq) && refExpressionErrors) {
  9699. if (refExpressionErrors.shorthandAssign === -1) {
  9700. refExpressionErrors.shorthandAssign = this.state.start;
  9701. }
  9702. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  9703. } else {
  9704. prop.value = prop.key.__clone();
  9705. }
  9706. prop.shorthand = true;
  9707. return this.finishNode(prop, "ObjectProperty");
  9708. }
  9709. }
  9710. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9711. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  9712. if (!node) this.unexpected();
  9713. return node;
  9714. }
  9715. parsePropertyName(prop, isPrivateNameAllowed) {
  9716. if (this.eat(types$1.bracketL)) {
  9717. prop.computed = true;
  9718. prop.key = this.parseMaybeAssignAllowIn();
  9719. this.expect(types$1.bracketR);
  9720. } else {
  9721. const oldInPropertyName = this.state.inPropertyName;
  9722. this.state.inPropertyName = true;
  9723. const type = this.state.type;
  9724. prop.key = type === types$1.num || type === types$1.string || type === types$1.bigint || type === types$1.decimal ? this.parseExprAtom() : this.parseMaybePrivateName(isPrivateNameAllowed);
  9725. if (type !== types$1.privateName) {
  9726. prop.computed = false;
  9727. }
  9728. this.state.inPropertyName = oldInPropertyName;
  9729. }
  9730. return prop.key;
  9731. }
  9732. initFunction(node, isAsync) {
  9733. node.id = null;
  9734. node.generator = false;
  9735. node.async = !!isAsync;
  9736. }
  9737. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  9738. this.initFunction(node, isAsync);
  9739. node.generator = !!isGenerator;
  9740. const allowModifiers = isConstructor;
  9741. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  9742. this.prodParam.enter(functionFlags(isAsync, node.generator));
  9743. this.parseFunctionParams(node, allowModifiers);
  9744. this.parseFunctionBodyAndFinish(node, type, true);
  9745. this.prodParam.exit();
  9746. this.scope.exit();
  9747. return node;
  9748. }
  9749. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9750. if (isTuple) {
  9751. this.expectPlugin("recordAndTuple");
  9752. }
  9753. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9754. this.state.inFSharpPipelineDirectBody = false;
  9755. const node = this.startNode();
  9756. this.next();
  9757. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  9758. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9759. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  9760. }
  9761. parseArrowExpression(node, params, isAsync, trailingCommaPos) {
  9762. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  9763. let flags = functionFlags(isAsync, false);
  9764. if (!this.match(types$1.bracketL) && this.prodParam.hasIn) {
  9765. flags |= PARAM_IN;
  9766. }
  9767. this.prodParam.enter(flags);
  9768. this.initFunction(node, isAsync);
  9769. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9770. if (params) {
  9771. this.state.maybeInArrowParameters = true;
  9772. this.setArrowFunctionParameters(node, params, trailingCommaPos);
  9773. }
  9774. this.state.maybeInArrowParameters = false;
  9775. this.parseFunctionBody(node, true);
  9776. this.prodParam.exit();
  9777. this.scope.exit();
  9778. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9779. return this.finishNode(node, "ArrowFunctionExpression");
  9780. }
  9781. setArrowFunctionParameters(node, params, trailingCommaPos) {
  9782. node.params = this.toAssignableList(params, trailingCommaPos, false);
  9783. }
  9784. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9785. this.parseFunctionBody(node, false, isMethod);
  9786. this.finishNode(node, type);
  9787. }
  9788. parseFunctionBody(node, allowExpression, isMethod = false) {
  9789. const isExpression = allowExpression && !this.match(types$1.braceL);
  9790. this.expressionScope.enter(newExpressionScope());
  9791. if (isExpression) {
  9792. node.body = this.parseMaybeAssign();
  9793. this.checkParams(node, false, allowExpression, false);
  9794. } else {
  9795. const oldStrict = this.state.strict;
  9796. const oldLabels = this.state.labels;
  9797. this.state.labels = [];
  9798. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  9799. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  9800. const nonSimple = !this.isSimpleParamList(node.params);
  9801. if (hasStrictModeDirective && nonSimple) {
  9802. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  9803. this.raise(errorPos, ErrorMessages.IllegalLanguageModeDirective);
  9804. }
  9805. const strictModeChanged = !oldStrict && this.state.strict;
  9806. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  9807. if (this.state.strict && node.id) {
  9808. this.checkLVal(node.id, "function name", BIND_OUTSIDE, undefined, undefined, strictModeChanged);
  9809. }
  9810. });
  9811. this.prodParam.exit();
  9812. this.expressionScope.exit();
  9813. this.state.labels = oldLabels;
  9814. }
  9815. }
  9816. isSimpleParamList(params) {
  9817. for (let i = 0, len = params.length; i < len; i++) {
  9818. if (params[i].type !== "Identifier") return false;
  9819. }
  9820. return true;
  9821. }
  9822. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  9823. const checkClashes = new Set();
  9824. for (const param of node.params) {
  9825. this.checkLVal(param, "function parameter list", BIND_VAR, allowDuplicates ? null : checkClashes, undefined, strictModeChanged);
  9826. }
  9827. }
  9828. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  9829. const elts = [];
  9830. let first = true;
  9831. while (!this.eat(close)) {
  9832. if (first) {
  9833. first = false;
  9834. } else {
  9835. this.expect(types$1.comma);
  9836. if (this.match(close)) {
  9837. if (nodeForExtra) {
  9838. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9839. }
  9840. this.next();
  9841. break;
  9842. }
  9843. }
  9844. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  9845. }
  9846. return elts;
  9847. }
  9848. parseExprListItem(allowEmpty, refExpressionErrors, refNeedsArrowPos, allowPlaceholder) {
  9849. let elt;
  9850. if (this.match(types$1.comma)) {
  9851. if (!allowEmpty) {
  9852. this.raise(this.state.pos, ErrorMessages.UnexpectedToken, ",");
  9853. }
  9854. elt = null;
  9855. } else if (this.match(types$1.ellipsis)) {
  9856. const spreadNodeStartPos = this.state.start;
  9857. const spreadNodeStartLoc = this.state.startLoc;
  9858. elt = this.parseParenItem(this.parseSpread(refExpressionErrors, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
  9859. } else if (this.match(types$1.question)) {
  9860. this.expectPlugin("partialApplication");
  9861. if (!allowPlaceholder) {
  9862. this.raise(this.state.start, ErrorMessages.UnexpectedArgumentPlaceholder);
  9863. }
  9864. const node = this.startNode();
  9865. this.next();
  9866. elt = this.finishNode(node, "ArgumentPlaceholder");
  9867. } else {
  9868. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos);
  9869. }
  9870. return elt;
  9871. }
  9872. parseIdentifier(liberal) {
  9873. const node = this.startNode();
  9874. const name = this.parseIdentifierName(node.start, liberal);
  9875. return this.createIdentifier(node, name);
  9876. }
  9877. createIdentifier(node, name) {
  9878. node.name = name;
  9879. node.loc.identifierName = name;
  9880. return this.finishNode(node, "Identifier");
  9881. }
  9882. parseIdentifierName(pos, liberal) {
  9883. let name;
  9884. const {
  9885. start,
  9886. type
  9887. } = this.state;
  9888. if (type === types$1.name) {
  9889. name = this.state.value;
  9890. } else if (type.keyword) {
  9891. name = type.keyword;
  9892. if (type === types$1._class || type === types$1._function) {
  9893. const curContext = this.curContext();
  9894. if (curContext === types.functionStatement || curContext === types.functionExpression) {
  9895. this.state.context.pop();
  9896. }
  9897. }
  9898. } else {
  9899. throw this.unexpected();
  9900. }
  9901. if (liberal) {
  9902. this.state.type = types$1.name;
  9903. } else {
  9904. this.checkReservedWord(name, start, !!type.keyword, false);
  9905. }
  9906. this.next();
  9907. return name;
  9908. }
  9909. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9910. if (word.length > 10) {
  9911. return;
  9912. }
  9913. if (!canBeReservedWord(word)) {
  9914. return;
  9915. }
  9916. if (word === "yield") {
  9917. if (this.prodParam.hasYield) {
  9918. this.raise(startLoc, ErrorMessages.YieldBindingIdentifier);
  9919. return;
  9920. }
  9921. } else if (word === "await") {
  9922. if (this.prodParam.hasAwait) {
  9923. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9924. return;
  9925. } else if (this.scope.inStaticBlock && !this.scope.inNonArrowFunction) {
  9926. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifierInStaticBlock);
  9927. return;
  9928. } else {
  9929. this.expressionScope.recordAsyncArrowParametersError(startLoc, ErrorMessages.AwaitBindingIdentifier);
  9930. }
  9931. } else if (word === "arguments") {
  9932. if (this.scope.inClassAndNotInNonArrowFunction) {
  9933. this.raise(startLoc, ErrorMessages.ArgumentsInClass);
  9934. return;
  9935. }
  9936. }
  9937. if (checkKeywords && isKeyword(word)) {
  9938. this.raise(startLoc, ErrorMessages.UnexpectedKeyword, word);
  9939. return;
  9940. }
  9941. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  9942. if (reservedTest(word, this.inModule)) {
  9943. this.raise(startLoc, ErrorMessages.UnexpectedReservedWord, word);
  9944. }
  9945. }
  9946. isAwaitAllowed() {
  9947. if (this.prodParam.hasAwait) return true;
  9948. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  9949. return true;
  9950. }
  9951. return false;
  9952. }
  9953. parseAwait(startPos, startLoc) {
  9954. const node = this.startNodeAt(startPos, startLoc);
  9955. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.AwaitExpressionFormalParameter);
  9956. if (this.eat(types$1.star)) {
  9957. this.raise(node.start, ErrorMessages.ObsoleteAwaitStar);
  9958. }
  9959. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  9960. if (this.isAmbiguousAwait()) {
  9961. this.ambiguousScriptDifferentAst = true;
  9962. } else {
  9963. this.sawUnambiguousESM = true;
  9964. }
  9965. }
  9966. if (!this.state.soloAwait) {
  9967. node.argument = this.parseMaybeUnary(null, true);
  9968. }
  9969. return this.finishNode(node, "AwaitExpression");
  9970. }
  9971. isAmbiguousAwait() {
  9972. 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);
  9973. }
  9974. parseYield() {
  9975. const node = this.startNode();
  9976. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.YieldInParameter);
  9977. this.next();
  9978. let delegating = false;
  9979. let argument = null;
  9980. if (!this.hasPrecedingLineBreak()) {
  9981. delegating = this.eat(types$1.star);
  9982. switch (this.state.type) {
  9983. case types$1.semi:
  9984. case types$1.eof:
  9985. case types$1.braceR:
  9986. case types$1.parenR:
  9987. case types$1.bracketR:
  9988. case types$1.braceBarR:
  9989. case types$1.colon:
  9990. case types$1.comma:
  9991. if (!delegating) break;
  9992. default:
  9993. argument = this.parseMaybeAssign();
  9994. }
  9995. }
  9996. node.delegate = delegating;
  9997. node.argument = argument;
  9998. return this.finishNode(node, "YieldExpression");
  9999. }
  10000. checkPipelineAtInfixOperator(left, leftStartPos) {
  10001. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  10002. if (left.type === "SequenceExpression") {
  10003. this.raise(leftStartPos, ErrorMessages.PipelineHeadSequenceExpression);
  10004. }
  10005. }
  10006. }
  10007. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  10008. this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);
  10009. return this.parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc);
  10010. }
  10011. checkSmartPipelineBodyEarlyErrors(childExpression, startPos) {
  10012. if (this.match(types$1.arrow)) {
  10013. throw this.raise(this.state.start, ErrorMessages.PipelineBodyNoArrow);
  10014. } else if (childExpression.type === "SequenceExpression") {
  10015. this.raise(startPos, ErrorMessages.PipelineBodySequenceExpression);
  10016. }
  10017. }
  10018. parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc) {
  10019. const bodyNode = this.startNodeAt(startPos, startLoc);
  10020. const isSimpleReference = this.isSimpleReference(childExpression);
  10021. if (isSimpleReference) {
  10022. bodyNode.callee = childExpression;
  10023. } else {
  10024. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  10025. this.raise(startPos, ErrorMessages.PipelineTopicUnused);
  10026. }
  10027. bodyNode.expression = childExpression;
  10028. }
  10029. return this.finishNode(bodyNode, isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression");
  10030. }
  10031. isSimpleReference(expression) {
  10032. switch (expression.type) {
  10033. case "MemberExpression":
  10034. return !expression.computed && this.isSimpleReference(expression.object);
  10035. case "Identifier":
  10036. return true;
  10037. default:
  10038. return false;
  10039. }
  10040. }
  10041. withTopicPermittingContext(callback) {
  10042. const outerContextTopicState = this.state.topicContext;
  10043. this.state.topicContext = {
  10044. maxNumOfResolvableTopics: 1,
  10045. maxTopicIndex: null
  10046. };
  10047. try {
  10048. return callback();
  10049. } finally {
  10050. this.state.topicContext = outerContextTopicState;
  10051. }
  10052. }
  10053. withTopicForbiddingContext(callback) {
  10054. const outerContextTopicState = this.state.topicContext;
  10055. this.state.topicContext = {
  10056. maxNumOfResolvableTopics: 0,
  10057. maxTopicIndex: null
  10058. };
  10059. try {
  10060. return callback();
  10061. } finally {
  10062. this.state.topicContext = outerContextTopicState;
  10063. }
  10064. }
  10065. withSoloAwaitPermittingContext(callback) {
  10066. const outerContextSoloAwaitState = this.state.soloAwait;
  10067. this.state.soloAwait = true;
  10068. try {
  10069. return callback();
  10070. } finally {
  10071. this.state.soloAwait = outerContextSoloAwaitState;
  10072. }
  10073. }
  10074. allowInAnd(callback) {
  10075. const flags = this.prodParam.currentFlags();
  10076. const prodParamToSet = PARAM_IN & ~flags;
  10077. if (prodParamToSet) {
  10078. this.prodParam.enter(flags | PARAM_IN);
  10079. try {
  10080. return callback();
  10081. } finally {
  10082. this.prodParam.exit();
  10083. }
  10084. }
  10085. return callback();
  10086. }
  10087. disallowInAnd(callback) {
  10088. const flags = this.prodParam.currentFlags();
  10089. const prodParamToClear = PARAM_IN & flags;
  10090. if (prodParamToClear) {
  10091. this.prodParam.enter(flags & ~PARAM_IN);
  10092. try {
  10093. return callback();
  10094. } finally {
  10095. this.prodParam.exit();
  10096. }
  10097. }
  10098. return callback();
  10099. }
  10100. registerTopicReference() {
  10101. this.state.topicContext.maxTopicIndex = 0;
  10102. }
  10103. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  10104. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  10105. }
  10106. topicReferenceWasUsedInCurrentTopicContext() {
  10107. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  10108. }
  10109. parseFSharpPipelineBody(prec) {
  10110. const startPos = this.state.start;
  10111. const startLoc = this.state.startLoc;
  10112. this.state.potentialArrowAt = this.state.start;
  10113. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10114. this.state.inFSharpPipelineDirectBody = true;
  10115. const ret = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec);
  10116. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10117. return ret;
  10118. }
  10119. parseModuleExpression() {
  10120. this.expectPlugin("moduleBlocks");
  10121. const node = this.startNode();
  10122. this.next();
  10123. this.eat(types$1.braceL);
  10124. const revertScopes = this.initializeScopes(true);
  10125. this.enterInitialScopes();
  10126. const program = this.startNode();
  10127. try {
  10128. node.body = this.parseProgram(program, types$1.braceR, "module");
  10129. } finally {
  10130. revertScopes();
  10131. }
  10132. this.eat(types$1.braceR);
  10133. return this.finishNode(node, "ModuleExpression");
  10134. }
  10135. }
  10136. const loopLabel = {
  10137. kind: "loop"
  10138. },
  10139. switchLabel = {
  10140. kind: "switch"
  10141. };
  10142. const FUNC_NO_FLAGS = 0b000,
  10143. FUNC_STATEMENT = 0b001,
  10144. FUNC_HANGING_STATEMENT = 0b010,
  10145. FUNC_NULLABLE_ID = 0b100;
  10146. const loneSurrogate = /[\uD800-\uDFFF]/u;
  10147. const keywordRelationalOperator = /in(?:stanceof)?/y;
  10148. function babel7CompatTokens(tokens) {
  10149. {
  10150. for (let i = 0; i < tokens.length; i++) {
  10151. const token = tokens[i];
  10152. if (token.type === types$1.privateName) {
  10153. const {
  10154. loc,
  10155. start,
  10156. value,
  10157. end
  10158. } = token;
  10159. const hashEndPos = start + 1;
  10160. const hashEndLoc = new Position(loc.start.line, loc.start.column + 1);
  10161. tokens.splice(i, 1, new Token({
  10162. type: types$1.hash,
  10163. value: "#",
  10164. start: start,
  10165. end: hashEndPos,
  10166. startLoc: loc.start,
  10167. endLoc: hashEndLoc
  10168. }), new Token({
  10169. type: types$1.name,
  10170. value: value,
  10171. start: hashEndPos,
  10172. end: end,
  10173. startLoc: hashEndLoc,
  10174. endLoc: loc.end
  10175. }));
  10176. }
  10177. }
  10178. }
  10179. return tokens;
  10180. }
  10181. class StatementParser extends ExpressionParser {
  10182. parseTopLevel(file, program) {
  10183. file.program = this.parseProgram(program);
  10184. file.comments = this.state.comments;
  10185. if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens);
  10186. return this.finishNode(file, "File");
  10187. }
  10188. parseProgram(program, end = types$1.eof, sourceType = this.options.sourceType) {
  10189. program.sourceType = sourceType;
  10190. program.interpreter = this.parseInterpreterDirective();
  10191. this.parseBlockBody(program, true, true, end);
  10192. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  10193. for (const [name] of Array.from(this.scope.undefinedExports)) {
  10194. const pos = this.scope.undefinedExports.get(name);
  10195. this.raise(pos, ErrorMessages.ModuleExportUndefined, name);
  10196. }
  10197. }
  10198. return this.finishNode(program, "Program");
  10199. }
  10200. stmtToDirective(stmt) {
  10201. const expr = stmt.expression;
  10202. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  10203. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  10204. const raw = this.input.slice(expr.start, expr.end);
  10205. const val = directiveLiteral.value = raw.slice(1, -1);
  10206. this.addExtra(directiveLiteral, "raw", raw);
  10207. this.addExtra(directiveLiteral, "rawValue", val);
  10208. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  10209. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  10210. }
  10211. parseInterpreterDirective() {
  10212. if (!this.match(types$1.interpreterDirective)) {
  10213. return null;
  10214. }
  10215. const node = this.startNode();
  10216. node.value = this.state.value;
  10217. this.next();
  10218. return this.finishNode(node, "InterpreterDirective");
  10219. }
  10220. isLet(context) {
  10221. if (!this.isContextual("let")) {
  10222. return false;
  10223. }
  10224. return this.isLetKeyword(context);
  10225. }
  10226. isLetKeyword(context) {
  10227. const next = this.nextTokenStart();
  10228. const nextCh = this.codePointAtPos(next);
  10229. if (nextCh === 92 || nextCh === 91) {
  10230. return true;
  10231. }
  10232. if (context) return false;
  10233. if (nextCh === 123) return true;
  10234. if (isIdentifierStart(nextCh)) {
  10235. keywordRelationalOperator.lastIndex = next;
  10236. const matched = keywordRelationalOperator.exec(this.input);
  10237. if (matched !== null) {
  10238. const endCh = this.codePointAtPos(next + matched[0].length);
  10239. if (!isIdentifierChar(endCh) && endCh !== 92) {
  10240. return false;
  10241. }
  10242. }
  10243. return true;
  10244. }
  10245. return false;
  10246. }
  10247. parseStatement(context, topLevel) {
  10248. if (this.match(types$1.at)) {
  10249. this.parseDecorators(true);
  10250. }
  10251. return this.parseStatementContent(context, topLevel);
  10252. }
  10253. parseStatementContent(context, topLevel) {
  10254. let starttype = this.state.type;
  10255. const node = this.startNode();
  10256. let kind;
  10257. if (this.isLet(context)) {
  10258. starttype = types$1._var;
  10259. kind = "let";
  10260. }
  10261. switch (starttype) {
  10262. case types$1._break:
  10263. case types$1._continue:
  10264. return this.parseBreakContinueStatement(node, starttype.keyword);
  10265. case types$1._debugger:
  10266. return this.parseDebuggerStatement(node);
  10267. case types$1._do:
  10268. return this.parseDoStatement(node);
  10269. case types$1._for:
  10270. return this.parseForStatement(node);
  10271. case types$1._function:
  10272. if (this.lookaheadCharCode() === 46) break;
  10273. if (context) {
  10274. if (this.state.strict) {
  10275. this.raise(this.state.start, ErrorMessages.StrictFunction);
  10276. } else if (context !== "if" && context !== "label") {
  10277. this.raise(this.state.start, ErrorMessages.SloppyFunction);
  10278. }
  10279. }
  10280. return this.parseFunctionStatement(node, false, !context);
  10281. case types$1._class:
  10282. if (context) this.unexpected();
  10283. return this.parseClass(node, true);
  10284. case types$1._if:
  10285. return this.parseIfStatement(node);
  10286. case types$1._return:
  10287. return this.parseReturnStatement(node);
  10288. case types$1._switch:
  10289. return this.parseSwitchStatement(node);
  10290. case types$1._throw:
  10291. return this.parseThrowStatement(node);
  10292. case types$1._try:
  10293. return this.parseTryStatement(node);
  10294. case types$1._const:
  10295. case types$1._var:
  10296. kind = kind || this.state.value;
  10297. if (context && kind !== "var") {
  10298. this.raise(this.state.start, ErrorMessages.UnexpectedLexicalDeclaration);
  10299. }
  10300. return this.parseVarStatement(node, kind);
  10301. case types$1._while:
  10302. return this.parseWhileStatement(node);
  10303. case types$1._with:
  10304. return this.parseWithStatement(node);
  10305. case types$1.braceL:
  10306. return this.parseBlock();
  10307. case types$1.semi:
  10308. return this.parseEmptyStatement(node);
  10309. case types$1._import:
  10310. {
  10311. const nextTokenCharCode = this.lookaheadCharCode();
  10312. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  10313. break;
  10314. }
  10315. }
  10316. case types$1._export:
  10317. {
  10318. if (!this.options.allowImportExportEverywhere && !topLevel) {
  10319. this.raise(this.state.start, ErrorMessages.UnexpectedImportExport);
  10320. }
  10321. this.next();
  10322. let result;
  10323. if (starttype === types$1._import) {
  10324. result = this.parseImport(node);
  10325. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  10326. this.sawUnambiguousESM = true;
  10327. }
  10328. } else {
  10329. result = this.parseExport(node);
  10330. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  10331. this.sawUnambiguousESM = true;
  10332. }
  10333. }
  10334. this.assertModuleNodeAllowed(node);
  10335. return result;
  10336. }
  10337. default:
  10338. {
  10339. if (this.isAsyncFunction()) {
  10340. if (context) {
  10341. this.raise(this.state.start, ErrorMessages.AsyncFunctionInSingleStatementContext);
  10342. }
  10343. this.next();
  10344. return this.parseFunctionStatement(node, true, !context);
  10345. }
  10346. }
  10347. }
  10348. const maybeName = this.state.value;
  10349. const expr = this.parseExpression();
  10350. if (starttype === types$1.name && expr.type === "Identifier" && this.eat(types$1.colon)) {
  10351. return this.parseLabeledStatement(node, maybeName, expr, context);
  10352. } else {
  10353. return this.parseExpressionStatement(node, expr);
  10354. }
  10355. }
  10356. assertModuleNodeAllowed(node) {
  10357. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  10358. this.raise(node.start, SourceTypeModuleErrorMessages.ImportOutsideModule);
  10359. }
  10360. }
  10361. takeDecorators(node) {
  10362. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10363. if (decorators.length) {
  10364. node.decorators = decorators;
  10365. this.resetStartLocationFromNode(node, decorators[0]);
  10366. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  10367. }
  10368. }
  10369. canHaveLeadingDecorator() {
  10370. return this.match(types$1._class);
  10371. }
  10372. parseDecorators(allowExport) {
  10373. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10374. while (this.match(types$1.at)) {
  10375. const decorator = this.parseDecorator();
  10376. currentContextDecorators.push(decorator);
  10377. }
  10378. if (this.match(types$1._export)) {
  10379. if (!allowExport) {
  10380. this.unexpected();
  10381. }
  10382. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  10383. this.raise(this.state.start, ErrorMessages.DecoratorExportClass);
  10384. }
  10385. } else if (!this.canHaveLeadingDecorator()) {
  10386. throw this.raise(this.state.start, ErrorMessages.UnexpectedLeadingDecorator);
  10387. }
  10388. }
  10389. parseDecorator() {
  10390. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  10391. const node = this.startNode();
  10392. this.next();
  10393. if (this.hasPlugin("decorators")) {
  10394. this.state.decoratorStack.push([]);
  10395. const startPos = this.state.start;
  10396. const startLoc = this.state.startLoc;
  10397. let expr;
  10398. if (this.eat(types$1.parenL)) {
  10399. expr = this.parseExpression();
  10400. this.expect(types$1.parenR);
  10401. } else {
  10402. expr = this.parseIdentifier(false);
  10403. while (this.eat(types$1.dot)) {
  10404. const node = this.startNodeAt(startPos, startLoc);
  10405. node.object = expr;
  10406. node.property = this.parseIdentifier(true);
  10407. node.computed = false;
  10408. expr = this.finishNode(node, "MemberExpression");
  10409. }
  10410. }
  10411. node.expression = this.parseMaybeDecoratorArguments(expr);
  10412. this.state.decoratorStack.pop();
  10413. } else {
  10414. node.expression = this.parseExprSubscripts();
  10415. }
  10416. return this.finishNode(node, "Decorator");
  10417. }
  10418. parseMaybeDecoratorArguments(expr) {
  10419. if (this.eat(types$1.parenL)) {
  10420. const node = this.startNodeAtNode(expr);
  10421. node.callee = expr;
  10422. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  10423. this.toReferencedList(node.arguments);
  10424. return this.finishNode(node, "CallExpression");
  10425. }
  10426. return expr;
  10427. }
  10428. parseBreakContinueStatement(node, keyword) {
  10429. const isBreak = keyword === "break";
  10430. this.next();
  10431. if (this.isLineTerminator()) {
  10432. node.label = null;
  10433. } else {
  10434. node.label = this.parseIdentifier();
  10435. this.semicolon();
  10436. }
  10437. this.verifyBreakContinue(node, keyword);
  10438. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  10439. }
  10440. verifyBreakContinue(node, keyword) {
  10441. const isBreak = keyword === "break";
  10442. let i;
  10443. for (i = 0; i < this.state.labels.length; ++i) {
  10444. const lab = this.state.labels[i];
  10445. if (node.label == null || lab.name === node.label.name) {
  10446. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  10447. if (node.label && isBreak) break;
  10448. }
  10449. }
  10450. if (i === this.state.labels.length) {
  10451. this.raise(node.start, ErrorMessages.IllegalBreakContinue, keyword);
  10452. }
  10453. }
  10454. parseDebuggerStatement(node) {
  10455. this.next();
  10456. this.semicolon();
  10457. return this.finishNode(node, "DebuggerStatement");
  10458. }
  10459. parseHeaderExpression() {
  10460. this.expect(types$1.parenL);
  10461. const val = this.parseExpression();
  10462. this.expect(types$1.parenR);
  10463. return val;
  10464. }
  10465. parseDoStatement(node) {
  10466. this.next();
  10467. this.state.labels.push(loopLabel);
  10468. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  10469. this.state.labels.pop();
  10470. this.expect(types$1._while);
  10471. node.test = this.parseHeaderExpression();
  10472. this.eat(types$1.semi);
  10473. return this.finishNode(node, "DoWhileStatement");
  10474. }
  10475. parseForStatement(node) {
  10476. this.next();
  10477. this.state.labels.push(loopLabel);
  10478. let awaitAt = -1;
  10479. if (this.isAwaitAllowed() && this.eatContextual("await")) {
  10480. awaitAt = this.state.lastTokStart;
  10481. }
  10482. this.scope.enter(SCOPE_OTHER);
  10483. this.expect(types$1.parenL);
  10484. if (this.match(types$1.semi)) {
  10485. if (awaitAt > -1) {
  10486. this.unexpected(awaitAt);
  10487. }
  10488. return this.parseFor(node, null);
  10489. }
  10490. const startsWithLet = this.isContextual("let");
  10491. const isLet = startsWithLet && this.isLetKeyword();
  10492. if (this.match(types$1._var) || this.match(types$1._const) || isLet) {
  10493. const init = this.startNode();
  10494. const kind = isLet ? "let" : this.state.value;
  10495. this.next();
  10496. this.parseVar(init, true, kind);
  10497. this.finishNode(init, "VariableDeclaration");
  10498. if ((this.match(types$1._in) || this.isContextual("of")) && init.declarations.length === 1) {
  10499. return this.parseForIn(node, init, awaitAt);
  10500. }
  10501. if (awaitAt > -1) {
  10502. this.unexpected(awaitAt);
  10503. }
  10504. return this.parseFor(node, init);
  10505. }
  10506. const startsWithUnescapedName = this.match(types$1.name) && !this.state.containsEsc;
  10507. const refExpressionErrors = new ExpressionErrors();
  10508. const init = this.parseExpression(true, refExpressionErrors);
  10509. const isForOf = this.isContextual("of");
  10510. if (isForOf) {
  10511. if (startsWithLet) {
  10512. this.raise(init.start, ErrorMessages.ForOfLet);
  10513. } else if (awaitAt === -1 && startsWithUnescapedName && init.type === "Identifier" && init.name === "async") {
  10514. this.raise(init.start, ErrorMessages.ForOfAsync);
  10515. }
  10516. }
  10517. if (isForOf || this.match(types$1._in)) {
  10518. this.toAssignable(init, true);
  10519. const description = isForOf ? "for-of statement" : "for-in statement";
  10520. this.checkLVal(init, description);
  10521. return this.parseForIn(node, init, awaitAt);
  10522. } else {
  10523. this.checkExpressionErrors(refExpressionErrors, true);
  10524. }
  10525. if (awaitAt > -1) {
  10526. this.unexpected(awaitAt);
  10527. }
  10528. return this.parseFor(node, init);
  10529. }
  10530. parseFunctionStatement(node, isAsync, declarationPosition) {
  10531. this.next();
  10532. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  10533. }
  10534. parseIfStatement(node) {
  10535. this.next();
  10536. node.test = this.parseHeaderExpression();
  10537. node.consequent = this.parseStatement("if");
  10538. node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
  10539. return this.finishNode(node, "IfStatement");
  10540. }
  10541. parseReturnStatement(node) {
  10542. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  10543. this.raise(this.state.start, ErrorMessages.IllegalReturn);
  10544. }
  10545. this.next();
  10546. if (this.isLineTerminator()) {
  10547. node.argument = null;
  10548. } else {
  10549. node.argument = this.parseExpression();
  10550. this.semicolon();
  10551. }
  10552. return this.finishNode(node, "ReturnStatement");
  10553. }
  10554. parseSwitchStatement(node) {
  10555. this.next();
  10556. node.discriminant = this.parseHeaderExpression();
  10557. const cases = node.cases = [];
  10558. this.expect(types$1.braceL);
  10559. this.state.labels.push(switchLabel);
  10560. this.scope.enter(SCOPE_OTHER);
  10561. let cur;
  10562. for (let sawDefault; !this.match(types$1.braceR);) {
  10563. if (this.match(types$1._case) || this.match(types$1._default)) {
  10564. const isCase = this.match(types$1._case);
  10565. if (cur) this.finishNode(cur, "SwitchCase");
  10566. cases.push(cur = this.startNode());
  10567. cur.consequent = [];
  10568. this.next();
  10569. if (isCase) {
  10570. cur.test = this.parseExpression();
  10571. } else {
  10572. if (sawDefault) {
  10573. this.raise(this.state.lastTokStart, ErrorMessages.MultipleDefaultsInSwitch);
  10574. }
  10575. sawDefault = true;
  10576. cur.test = null;
  10577. }
  10578. this.expect(types$1.colon);
  10579. } else {
  10580. if (cur) {
  10581. cur.consequent.push(this.parseStatement(null));
  10582. } else {
  10583. this.unexpected();
  10584. }
  10585. }
  10586. }
  10587. this.scope.exit();
  10588. if (cur) this.finishNode(cur, "SwitchCase");
  10589. this.next();
  10590. this.state.labels.pop();
  10591. return this.finishNode(node, "SwitchStatement");
  10592. }
  10593. parseThrowStatement(node) {
  10594. this.next();
  10595. if (this.hasPrecedingLineBreak()) {
  10596. this.raise(this.state.lastTokEnd, ErrorMessages.NewlineAfterThrow);
  10597. }
  10598. node.argument = this.parseExpression();
  10599. this.semicolon();
  10600. return this.finishNode(node, "ThrowStatement");
  10601. }
  10602. parseCatchClauseParam() {
  10603. const param = this.parseBindingAtom();
  10604. const simple = param.type === "Identifier";
  10605. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  10606. this.checkLVal(param, "catch clause", BIND_LEXICAL);
  10607. return param;
  10608. }
  10609. parseTryStatement(node) {
  10610. this.next();
  10611. node.block = this.parseBlock();
  10612. node.handler = null;
  10613. if (this.match(types$1._catch)) {
  10614. const clause = this.startNode();
  10615. this.next();
  10616. if (this.match(types$1.parenL)) {
  10617. this.expect(types$1.parenL);
  10618. clause.param = this.parseCatchClauseParam();
  10619. this.expect(types$1.parenR);
  10620. } else {
  10621. clause.param = null;
  10622. this.scope.enter(SCOPE_OTHER);
  10623. }
  10624. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  10625. this.scope.exit();
  10626. node.handler = this.finishNode(clause, "CatchClause");
  10627. }
  10628. node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
  10629. if (!node.handler && !node.finalizer) {
  10630. this.raise(node.start, ErrorMessages.NoCatchOrFinally);
  10631. }
  10632. return this.finishNode(node, "TryStatement");
  10633. }
  10634. parseVarStatement(node, kind) {
  10635. this.next();
  10636. this.parseVar(node, false, kind);
  10637. this.semicolon();
  10638. return this.finishNode(node, "VariableDeclaration");
  10639. }
  10640. parseWhileStatement(node) {
  10641. this.next();
  10642. node.test = this.parseHeaderExpression();
  10643. this.state.labels.push(loopLabel);
  10644. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  10645. this.state.labels.pop();
  10646. return this.finishNode(node, "WhileStatement");
  10647. }
  10648. parseWithStatement(node) {
  10649. if (this.state.strict) {
  10650. this.raise(this.state.start, ErrorMessages.StrictWith);
  10651. }
  10652. this.next();
  10653. node.object = this.parseHeaderExpression();
  10654. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  10655. return this.finishNode(node, "WithStatement");
  10656. }
  10657. parseEmptyStatement(node) {
  10658. this.next();
  10659. return this.finishNode(node, "EmptyStatement");
  10660. }
  10661. parseLabeledStatement(node, maybeName, expr, context) {
  10662. for (const label of this.state.labels) {
  10663. if (label.name === maybeName) {
  10664. this.raise(expr.start, ErrorMessages.LabelRedeclaration, maybeName);
  10665. }
  10666. }
  10667. const kind = this.state.type.isLoop ? "loop" : this.match(types$1._switch) ? "switch" : null;
  10668. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  10669. const label = this.state.labels[i];
  10670. if (label.statementStart === node.start) {
  10671. label.statementStart = this.state.start;
  10672. label.kind = kind;
  10673. } else {
  10674. break;
  10675. }
  10676. }
  10677. this.state.labels.push({
  10678. name: maybeName,
  10679. kind: kind,
  10680. statementStart: this.state.start
  10681. });
  10682. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  10683. this.state.labels.pop();
  10684. node.label = expr;
  10685. return this.finishNode(node, "LabeledStatement");
  10686. }
  10687. parseExpressionStatement(node, expr) {
  10688. node.expression = expr;
  10689. this.semicolon();
  10690. return this.finishNode(node, "ExpressionStatement");
  10691. }
  10692. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  10693. const node = this.startNode();
  10694. if (allowDirectives) {
  10695. this.state.strictErrors.clear();
  10696. }
  10697. this.expect(types$1.braceL);
  10698. if (createNewLexicalScope) {
  10699. this.scope.enter(SCOPE_OTHER);
  10700. }
  10701. this.parseBlockBody(node, allowDirectives, false, types$1.braceR, afterBlockParse);
  10702. if (createNewLexicalScope) {
  10703. this.scope.exit();
  10704. }
  10705. return this.finishNode(node, "BlockStatement");
  10706. }
  10707. isValidDirective(stmt) {
  10708. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  10709. }
  10710. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  10711. const body = node.body = [];
  10712. const directives = node.directives = [];
  10713. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  10714. }
  10715. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  10716. const oldStrict = this.state.strict;
  10717. let hasStrictModeDirective = false;
  10718. let parsedNonDirective = false;
  10719. while (!this.match(end)) {
  10720. const stmt = this.parseStatement(null, topLevel);
  10721. if (directives && !parsedNonDirective) {
  10722. if (this.isValidDirective(stmt)) {
  10723. const directive = this.stmtToDirective(stmt);
  10724. directives.push(directive);
  10725. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  10726. hasStrictModeDirective = true;
  10727. this.setStrict(true);
  10728. }
  10729. continue;
  10730. }
  10731. parsedNonDirective = true;
  10732. this.state.strictErrors.clear();
  10733. }
  10734. body.push(stmt);
  10735. }
  10736. if (afterBlockParse) {
  10737. afterBlockParse.call(this, hasStrictModeDirective);
  10738. }
  10739. if (!oldStrict) {
  10740. this.setStrict(false);
  10741. }
  10742. this.next();
  10743. }
  10744. parseFor(node, init) {
  10745. node.init = init;
  10746. this.semicolon(false);
  10747. node.test = this.match(types$1.semi) ? null : this.parseExpression();
  10748. this.semicolon(false);
  10749. node.update = this.match(types$1.parenR) ? null : this.parseExpression();
  10750. this.expect(types$1.parenR);
  10751. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10752. this.scope.exit();
  10753. this.state.labels.pop();
  10754. return this.finishNode(node, "ForStatement");
  10755. }
  10756. parseForIn(node, init, awaitAt) {
  10757. const isForIn = this.match(types$1._in);
  10758. this.next();
  10759. if (isForIn) {
  10760. if (awaitAt > -1) this.unexpected(awaitAt);
  10761. } else {
  10762. node.await = awaitAt > -1;
  10763. }
  10764. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  10765. this.raise(init.start, ErrorMessages.ForInOfLoopInitializer, isForIn ? "for-in" : "for-of");
  10766. } else if (init.type === "AssignmentPattern") {
  10767. this.raise(init.start, ErrorMessages.InvalidLhs, "for-loop");
  10768. }
  10769. node.left = init;
  10770. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  10771. this.expect(types$1.parenR);
  10772. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  10773. this.scope.exit();
  10774. this.state.labels.pop();
  10775. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  10776. }
  10777. parseVar(node, isFor, kind) {
  10778. const declarations = node.declarations = [];
  10779. const isTypescript = this.hasPlugin("typescript");
  10780. node.kind = kind;
  10781. for (;;) {
  10782. const decl = this.startNode();
  10783. this.parseVarId(decl, kind);
  10784. if (this.eat(types$1.eq)) {
  10785. decl.init = isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  10786. } else {
  10787. if (kind === "const" && !(this.match(types$1._in) || this.isContextual("of"))) {
  10788. if (!isTypescript) {
  10789. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Const declarations");
  10790. }
  10791. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types$1._in) || this.isContextual("of")))) {
  10792. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Complex binding patterns");
  10793. }
  10794. decl.init = null;
  10795. }
  10796. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  10797. if (!this.eat(types$1.comma)) break;
  10798. }
  10799. return node;
  10800. }
  10801. parseVarId(decl, kind) {
  10802. decl.id = this.parseBindingAtom();
  10803. this.checkLVal(decl.id, "variable declaration", kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, kind !== "var");
  10804. }
  10805. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  10806. const isStatement = statement & FUNC_STATEMENT;
  10807. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  10808. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  10809. this.initFunction(node, isAsync);
  10810. if (this.match(types$1.star) && isHangingStatement) {
  10811. this.raise(this.state.start, ErrorMessages.GeneratorInSingleStatementContext);
  10812. }
  10813. node.generator = this.eat(types$1.star);
  10814. if (isStatement) {
  10815. node.id = this.parseFunctionId(requireId);
  10816. }
  10817. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10818. this.state.maybeInArrowParameters = false;
  10819. this.scope.enter(SCOPE_FUNCTION);
  10820. this.prodParam.enter(functionFlags(isAsync, node.generator));
  10821. if (!isStatement) {
  10822. node.id = this.parseFunctionId();
  10823. }
  10824. this.parseFunctionParams(node, false);
  10825. this.withTopicForbiddingContext(() => {
  10826. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  10827. });
  10828. this.prodParam.exit();
  10829. this.scope.exit();
  10830. if (isStatement && !isHangingStatement) {
  10831. this.registerFunctionStatementId(node);
  10832. }
  10833. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10834. return node;
  10835. }
  10836. parseFunctionId(requireId) {
  10837. return requireId || this.match(types$1.name) ? this.parseIdentifier() : null;
  10838. }
  10839. parseFunctionParams(node, allowModifiers) {
  10840. this.expect(types$1.parenL);
  10841. this.expressionScope.enter(newParameterDeclarationScope());
  10842. node.params = this.parseBindingList(types$1.parenR, 41, false, allowModifiers);
  10843. this.expressionScope.exit();
  10844. }
  10845. registerFunctionStatementId(node) {
  10846. if (!node.id) return;
  10847. 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);
  10848. }
  10849. parseClass(node, isStatement, optionalId) {
  10850. this.next();
  10851. this.takeDecorators(node);
  10852. const oldStrict = this.state.strict;
  10853. this.state.strict = true;
  10854. this.parseClassId(node, isStatement, optionalId);
  10855. this.parseClassSuper(node);
  10856. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  10857. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  10858. }
  10859. isClassProperty() {
  10860. return this.match(types$1.eq) || this.match(types$1.semi) || this.match(types$1.braceR);
  10861. }
  10862. isClassMethod() {
  10863. return this.match(types$1.parenL);
  10864. }
  10865. isNonstaticConstructor(method) {
  10866. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  10867. }
  10868. parseClassBody(hadSuperClass, oldStrict) {
  10869. this.classScope.enter();
  10870. const state = {
  10871. hadConstructor: false,
  10872. hadSuperClass
  10873. };
  10874. let decorators = [];
  10875. const classBody = this.startNode();
  10876. classBody.body = [];
  10877. this.expect(types$1.braceL);
  10878. this.withTopicForbiddingContext(() => {
  10879. while (!this.match(types$1.braceR)) {
  10880. if (this.eat(types$1.semi)) {
  10881. if (decorators.length > 0) {
  10882. throw this.raise(this.state.lastTokEnd, ErrorMessages.DecoratorSemicolon);
  10883. }
  10884. continue;
  10885. }
  10886. if (this.match(types$1.at)) {
  10887. decorators.push(this.parseDecorator());
  10888. continue;
  10889. }
  10890. const member = this.startNode();
  10891. if (decorators.length) {
  10892. member.decorators = decorators;
  10893. this.resetStartLocationFromNode(member, decorators[0]);
  10894. decorators = [];
  10895. }
  10896. this.parseClassMember(classBody, member, state);
  10897. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  10898. this.raise(member.start, ErrorMessages.DecoratorConstructor);
  10899. }
  10900. }
  10901. });
  10902. this.state.strict = oldStrict;
  10903. this.next();
  10904. if (decorators.length) {
  10905. throw this.raise(this.state.start, ErrorMessages.TrailingDecorator);
  10906. }
  10907. this.classScope.exit();
  10908. return this.finishNode(classBody, "ClassBody");
  10909. }
  10910. parseClassMemberFromModifier(classBody, member) {
  10911. const key = this.parseIdentifier(true);
  10912. if (this.isClassMethod()) {
  10913. const method = member;
  10914. method.kind = "method";
  10915. method.computed = false;
  10916. method.key = key;
  10917. method.static = false;
  10918. this.pushClassMethod(classBody, method, false, false, false, false);
  10919. return true;
  10920. } else if (this.isClassProperty()) {
  10921. const prop = member;
  10922. prop.computed = false;
  10923. prop.key = key;
  10924. prop.static = false;
  10925. classBody.body.push(this.parseClassProperty(prop));
  10926. return true;
  10927. }
  10928. return false;
  10929. }
  10930. parseClassMember(classBody, member, state) {
  10931. const isStatic = this.isContextual("static");
  10932. if (isStatic) {
  10933. if (this.parseClassMemberFromModifier(classBody, member)) {
  10934. return;
  10935. }
  10936. if (this.eat(types$1.braceL)) {
  10937. this.parseClassStaticBlock(classBody, member);
  10938. return;
  10939. }
  10940. }
  10941. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  10942. }
  10943. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  10944. const publicMethod = member;
  10945. const privateMethod = member;
  10946. const publicProp = member;
  10947. const privateProp = member;
  10948. const method = publicMethod;
  10949. const publicMember = publicMethod;
  10950. member.static = isStatic;
  10951. if (this.eat(types$1.star)) {
  10952. method.kind = "method";
  10953. const isPrivateName = this.match(types$1.privateName);
  10954. this.parseClassElementName(method);
  10955. if (isPrivateName) {
  10956. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  10957. return;
  10958. }
  10959. if (this.isNonstaticConstructor(publicMethod)) {
  10960. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsGenerator);
  10961. }
  10962. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  10963. return;
  10964. }
  10965. const containsEsc = this.state.containsEsc;
  10966. const isPrivate = this.match(types$1.privateName);
  10967. const key = this.parseClassElementName(member);
  10968. const isSimple = key.type === "Identifier";
  10969. const maybeQuestionTokenStart = this.state.start;
  10970. this.parsePostMemberNameModifiers(publicMember);
  10971. if (this.isClassMethod()) {
  10972. method.kind = "method";
  10973. if (isPrivate) {
  10974. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  10975. return;
  10976. }
  10977. const isConstructor = this.isNonstaticConstructor(publicMethod);
  10978. let allowsDirectSuper = false;
  10979. if (isConstructor) {
  10980. publicMethod.kind = "constructor";
  10981. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  10982. this.raise(key.start, ErrorMessages.DuplicateConstructor);
  10983. }
  10984. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  10985. this.raise(key.start, ErrorMessages.OverrideOnConstructor);
  10986. }
  10987. state.hadConstructor = true;
  10988. allowsDirectSuper = state.hadSuperClass;
  10989. }
  10990. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  10991. } else if (this.isClassProperty()) {
  10992. if (isPrivate) {
  10993. this.pushClassPrivateProperty(classBody, privateProp);
  10994. } else {
  10995. this.pushClassProperty(classBody, publicProp);
  10996. }
  10997. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  10998. const isGenerator = this.eat(types$1.star);
  10999. if (publicMember.optional) {
  11000. this.unexpected(maybeQuestionTokenStart);
  11001. }
  11002. method.kind = "method";
  11003. const isPrivate = this.match(types$1.privateName);
  11004. this.parseClassElementName(method);
  11005. this.parsePostMemberNameModifiers(publicMember);
  11006. if (isPrivate) {
  11007. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  11008. } else {
  11009. if (this.isNonstaticConstructor(publicMethod)) {
  11010. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAsync);
  11011. }
  11012. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  11013. }
  11014. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types$1.star) && this.isLineTerminator())) {
  11015. method.kind = key.name;
  11016. const isPrivate = this.match(types$1.privateName);
  11017. this.parseClassElementName(publicMethod);
  11018. if (isPrivate) {
  11019. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11020. } else {
  11021. if (this.isNonstaticConstructor(publicMethod)) {
  11022. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAccessor);
  11023. }
  11024. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  11025. }
  11026. this.checkGetterSetterParams(publicMethod);
  11027. } else if (this.isLineTerminator()) {
  11028. if (isPrivate) {
  11029. this.pushClassPrivateProperty(classBody, privateProp);
  11030. } else {
  11031. this.pushClassProperty(classBody, publicProp);
  11032. }
  11033. } else {
  11034. this.unexpected();
  11035. }
  11036. }
  11037. parseClassElementName(member) {
  11038. const {
  11039. type,
  11040. value,
  11041. start
  11042. } = this.state;
  11043. if ((type === types$1.name || type === types$1.string) && member.static && value === "prototype") {
  11044. this.raise(start, ErrorMessages.StaticPrototype);
  11045. }
  11046. if (type === types$1.privateName && value === "constructor") {
  11047. this.raise(start, ErrorMessages.ConstructorClassPrivateField);
  11048. }
  11049. return this.parsePropertyName(member, true);
  11050. }
  11051. parseClassStaticBlock(classBody, member) {
  11052. var _member$decorators;
  11053. this.expectPlugin("classStaticBlock", member.start);
  11054. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  11055. const oldLabels = this.state.labels;
  11056. this.state.labels = [];
  11057. this.prodParam.enter(PARAM);
  11058. const body = member.body = [];
  11059. this.parseBlockOrModuleBlockBody(body, undefined, false, types$1.braceR);
  11060. this.prodParam.exit();
  11061. this.scope.exit();
  11062. this.state.labels = oldLabels;
  11063. classBody.body.push(this.finishNode(member, "StaticBlock"));
  11064. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  11065. this.raise(member.start, ErrorMessages.DecoratorStaticBlock);
  11066. }
  11067. }
  11068. pushClassProperty(classBody, prop) {
  11069. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  11070. this.raise(prop.key.start, ErrorMessages.ConstructorClassField);
  11071. }
  11072. classBody.body.push(this.parseClassProperty(prop));
  11073. }
  11074. pushClassPrivateProperty(classBody, prop) {
  11075. const node = this.parseClassPrivateProperty(prop);
  11076. classBody.body.push(node);
  11077. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.start);
  11078. }
  11079. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  11080. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  11081. }
  11082. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  11083. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  11084. classBody.body.push(node);
  11085. 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;
  11086. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.start);
  11087. }
  11088. parsePostMemberNameModifiers(methodOrProp) {}
  11089. parseClassPrivateProperty(node) {
  11090. this.parseInitializer(node);
  11091. this.semicolon();
  11092. return this.finishNode(node, "ClassPrivateProperty");
  11093. }
  11094. parseClassProperty(node) {
  11095. this.parseInitializer(node);
  11096. this.semicolon();
  11097. return this.finishNode(node, "ClassProperty");
  11098. }
  11099. parseInitializer(node) {
  11100. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  11101. this.expressionScope.enter(newExpressionScope());
  11102. this.prodParam.enter(PARAM);
  11103. node.value = this.eat(types$1.eq) ? this.parseMaybeAssignAllowIn() : null;
  11104. this.expressionScope.exit();
  11105. this.prodParam.exit();
  11106. this.scope.exit();
  11107. }
  11108. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  11109. if (this.match(types$1.name)) {
  11110. node.id = this.parseIdentifier();
  11111. if (isStatement) {
  11112. this.checkLVal(node.id, "class name", bindingType);
  11113. }
  11114. } else {
  11115. if (optionalId || !isStatement) {
  11116. node.id = null;
  11117. } else {
  11118. this.unexpected(null, ErrorMessages.MissingClassName);
  11119. }
  11120. }
  11121. }
  11122. parseClassSuper(node) {
  11123. node.superClass = this.eat(types$1._extends) ? this.parseExprSubscripts() : null;
  11124. }
  11125. parseExport(node) {
  11126. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  11127. const parseAfterDefault = !hasDefault || this.eat(types$1.comma);
  11128. const hasStar = parseAfterDefault && this.eatExportStar(node);
  11129. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  11130. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types$1.comma));
  11131. const isFromRequired = hasDefault || hasStar;
  11132. if (hasStar && !hasNamespace) {
  11133. if (hasDefault) this.unexpected();
  11134. this.parseExportFrom(node, true);
  11135. return this.finishNode(node, "ExportAllDeclaration");
  11136. }
  11137. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  11138. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  11139. throw this.unexpected(null, types$1.braceL);
  11140. }
  11141. let hasDeclaration;
  11142. if (isFromRequired || hasSpecifiers) {
  11143. hasDeclaration = false;
  11144. this.parseExportFrom(node, isFromRequired);
  11145. } else {
  11146. hasDeclaration = this.maybeParseExportDeclaration(node);
  11147. }
  11148. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  11149. this.checkExport(node, true, false, !!node.source);
  11150. return this.finishNode(node, "ExportNamedDeclaration");
  11151. }
  11152. if (this.eat(types$1._default)) {
  11153. node.declaration = this.parseExportDefaultExpression();
  11154. this.checkExport(node, true, true);
  11155. return this.finishNode(node, "ExportDefaultDeclaration");
  11156. }
  11157. throw this.unexpected(null, types$1.braceL);
  11158. }
  11159. eatExportStar(node) {
  11160. return this.eat(types$1.star);
  11161. }
  11162. maybeParseExportDefaultSpecifier(node) {
  11163. if (this.isExportDefaultSpecifier()) {
  11164. this.expectPlugin("exportDefaultFrom");
  11165. const specifier = this.startNode();
  11166. specifier.exported = this.parseIdentifier(true);
  11167. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  11168. return true;
  11169. }
  11170. return false;
  11171. }
  11172. maybeParseExportNamespaceSpecifier(node) {
  11173. if (this.isContextual("as")) {
  11174. if (!node.specifiers) node.specifiers = [];
  11175. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  11176. this.next();
  11177. specifier.exported = this.parseModuleExportName();
  11178. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  11179. return true;
  11180. }
  11181. return false;
  11182. }
  11183. maybeParseExportNamedSpecifiers(node) {
  11184. if (this.match(types$1.braceL)) {
  11185. if (!node.specifiers) node.specifiers = [];
  11186. node.specifiers.push(...this.parseExportSpecifiers());
  11187. node.source = null;
  11188. node.declaration = null;
  11189. return true;
  11190. }
  11191. return false;
  11192. }
  11193. maybeParseExportDeclaration(node) {
  11194. if (this.shouldParseExportDeclaration()) {
  11195. node.specifiers = [];
  11196. node.source = null;
  11197. node.declaration = this.parseExportDeclaration(node);
  11198. return true;
  11199. }
  11200. return false;
  11201. }
  11202. isAsyncFunction() {
  11203. if (!this.isContextual("async")) return false;
  11204. const next = this.nextTokenStart();
  11205. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  11206. }
  11207. parseExportDefaultExpression() {
  11208. const expr = this.startNode();
  11209. const isAsync = this.isAsyncFunction();
  11210. if (this.match(types$1._function) || isAsync) {
  11211. this.next();
  11212. if (isAsync) {
  11213. this.next();
  11214. }
  11215. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  11216. } else if (this.match(types$1._class)) {
  11217. return this.parseClass(expr, true, true);
  11218. } else if (this.match(types$1.at)) {
  11219. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11220. this.raise(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11221. }
  11222. this.parseDecorators(false);
  11223. return this.parseClass(expr, true, true);
  11224. } else if (this.match(types$1._const) || this.match(types$1._var) || this.isLet()) {
  11225. throw this.raise(this.state.start, ErrorMessages.UnsupportedDefaultExport);
  11226. } else {
  11227. const res = this.parseMaybeAssignAllowIn();
  11228. this.semicolon();
  11229. return res;
  11230. }
  11231. }
  11232. parseExportDeclaration(node) {
  11233. return this.parseStatement(null);
  11234. }
  11235. isExportDefaultSpecifier() {
  11236. if (this.match(types$1.name)) {
  11237. const value = this.state.value;
  11238. if (value === "async" && !this.state.containsEsc || value === "let") {
  11239. return false;
  11240. }
  11241. if ((value === "type" || value === "interface") && !this.state.containsEsc) {
  11242. const l = this.lookahead();
  11243. if (l.type === types$1.name && l.value !== "from" || l.type === types$1.braceL) {
  11244. this.expectOnePlugin(["flow", "typescript"]);
  11245. return false;
  11246. }
  11247. }
  11248. } else if (!this.match(types$1._default)) {
  11249. return false;
  11250. }
  11251. const next = this.nextTokenStart();
  11252. const hasFrom = this.isUnparsedContextual(next, "from");
  11253. if (this.input.charCodeAt(next) === 44 || this.match(types$1.name) && hasFrom) {
  11254. return true;
  11255. }
  11256. if (this.match(types$1._default) && hasFrom) {
  11257. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  11258. return nextAfterFrom === 34 || nextAfterFrom === 39;
  11259. }
  11260. return false;
  11261. }
  11262. parseExportFrom(node, expect) {
  11263. if (this.eatContextual("from")) {
  11264. node.source = this.parseImportSource();
  11265. this.checkExport(node);
  11266. const assertions = this.maybeParseImportAssertions();
  11267. if (assertions) {
  11268. node.assertions = assertions;
  11269. }
  11270. } else {
  11271. if (expect) {
  11272. this.unexpected();
  11273. } else {
  11274. node.source = null;
  11275. }
  11276. }
  11277. this.semicolon();
  11278. }
  11279. shouldParseExportDeclaration() {
  11280. if (this.match(types$1.at)) {
  11281. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  11282. if (this.hasPlugin("decorators")) {
  11283. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11284. this.unexpected(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11285. } else {
  11286. return true;
  11287. }
  11288. }
  11289. }
  11290. 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();
  11291. }
  11292. checkExport(node, checkNames, isDefault, isFrom) {
  11293. if (checkNames) {
  11294. if (isDefault) {
  11295. this.checkDuplicateExports(node, "default");
  11296. if (this.hasPlugin("exportDefaultFrom")) {
  11297. var _declaration$extra;
  11298. const declaration = node.declaration;
  11299. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  11300. this.raise(declaration.start, ErrorMessages.ExportDefaultFromAsIdentifier);
  11301. }
  11302. }
  11303. } else if (node.specifiers && node.specifiers.length) {
  11304. for (const specifier of node.specifiers) {
  11305. const {
  11306. exported
  11307. } = specifier;
  11308. const exportedName = exported.type === "Identifier" ? exported.name : exported.value;
  11309. this.checkDuplicateExports(specifier, exportedName);
  11310. if (!isFrom && specifier.local) {
  11311. const {
  11312. local
  11313. } = specifier;
  11314. if (local.type !== "Identifier") {
  11315. this.raise(specifier.start, ErrorMessages.ExportBindingIsString, local.value, exportedName);
  11316. } else {
  11317. this.checkReservedWord(local.name, local.start, true, false);
  11318. this.scope.checkLocalExport(local);
  11319. }
  11320. }
  11321. }
  11322. } else if (node.declaration) {
  11323. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  11324. const id = node.declaration.id;
  11325. if (!id) throw new Error("Assertion failure");
  11326. this.checkDuplicateExports(node, id.name);
  11327. } else if (node.declaration.type === "VariableDeclaration") {
  11328. for (const declaration of node.declaration.declarations) {
  11329. this.checkDeclaration(declaration.id);
  11330. }
  11331. }
  11332. }
  11333. }
  11334. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11335. if (currentContextDecorators.length) {
  11336. throw this.raise(node.start, ErrorMessages.UnsupportedDecoratorExport);
  11337. }
  11338. }
  11339. checkDeclaration(node) {
  11340. if (node.type === "Identifier") {
  11341. this.checkDuplicateExports(node, node.name);
  11342. } else if (node.type === "ObjectPattern") {
  11343. for (const prop of node.properties) {
  11344. this.checkDeclaration(prop);
  11345. }
  11346. } else if (node.type === "ArrayPattern") {
  11347. for (const elem of node.elements) {
  11348. if (elem) {
  11349. this.checkDeclaration(elem);
  11350. }
  11351. }
  11352. } else if (node.type === "ObjectProperty") {
  11353. this.checkDeclaration(node.value);
  11354. } else if (node.type === "RestElement") {
  11355. this.checkDeclaration(node.argument);
  11356. } else if (node.type === "AssignmentPattern") {
  11357. this.checkDeclaration(node.left);
  11358. }
  11359. }
  11360. checkDuplicateExports(node, name) {
  11361. if (this.exportedIdentifiers.has(name)) {
  11362. this.raise(node.start, name === "default" ? ErrorMessages.DuplicateDefaultExport : ErrorMessages.DuplicateExport, name);
  11363. }
  11364. this.exportedIdentifiers.add(name);
  11365. }
  11366. parseExportSpecifiers() {
  11367. const nodes = [];
  11368. let first = true;
  11369. this.expect(types$1.braceL);
  11370. while (!this.eat(types$1.braceR)) {
  11371. if (first) {
  11372. first = false;
  11373. } else {
  11374. this.expect(types$1.comma);
  11375. if (this.eat(types$1.braceR)) break;
  11376. }
  11377. const node = this.startNode();
  11378. node.local = this.parseModuleExportName();
  11379. node.exported = this.eatContextual("as") ? this.parseModuleExportName() : node.local.__clone();
  11380. nodes.push(this.finishNode(node, "ExportSpecifier"));
  11381. }
  11382. return nodes;
  11383. }
  11384. parseModuleExportName() {
  11385. if (this.match(types$1.string)) {
  11386. const result = this.parseStringLiteral(this.state.value);
  11387. const surrogate = result.value.match(loneSurrogate);
  11388. if (surrogate) {
  11389. this.raise(result.start, ErrorMessages.ModuleExportNameHasLoneSurrogate, surrogate[0].charCodeAt(0).toString(16));
  11390. }
  11391. return result;
  11392. }
  11393. return this.parseIdentifier(true);
  11394. }
  11395. parseImport(node) {
  11396. node.specifiers = [];
  11397. if (!this.match(types$1.string)) {
  11398. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  11399. const parseNext = !hasDefault || this.eat(types$1.comma);
  11400. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  11401. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  11402. this.expectContextual("from");
  11403. }
  11404. node.source = this.parseImportSource();
  11405. const assertions = this.maybeParseImportAssertions();
  11406. if (assertions) {
  11407. node.assertions = assertions;
  11408. } else {
  11409. const attributes = this.maybeParseModuleAttributes();
  11410. if (attributes) {
  11411. node.attributes = attributes;
  11412. }
  11413. }
  11414. this.semicolon();
  11415. return this.finishNode(node, "ImportDeclaration");
  11416. }
  11417. parseImportSource() {
  11418. if (!this.match(types$1.string)) this.unexpected();
  11419. return this.parseExprAtom();
  11420. }
  11421. shouldParseDefaultImport(node) {
  11422. return this.match(types$1.name);
  11423. }
  11424. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  11425. specifier.local = this.parseIdentifier();
  11426. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  11427. node.specifiers.push(this.finishNode(specifier, type));
  11428. }
  11429. parseAssertEntries() {
  11430. const attrs = [];
  11431. const attrNames = new Set();
  11432. do {
  11433. if (this.match(types$1.braceR)) {
  11434. break;
  11435. }
  11436. const node = this.startNode();
  11437. const keyName = this.state.value;
  11438. if (attrNames.has(keyName)) {
  11439. this.raise(this.state.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, keyName);
  11440. }
  11441. attrNames.add(keyName);
  11442. if (this.match(types$1.string)) {
  11443. node.key = this.parseStringLiteral(keyName);
  11444. } else {
  11445. node.key = this.parseIdentifier(true);
  11446. }
  11447. this.expect(types$1.colon);
  11448. if (!this.match(types$1.string)) {
  11449. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11450. }
  11451. node.value = this.parseStringLiteral(this.state.value);
  11452. this.finishNode(node, "ImportAttribute");
  11453. attrs.push(node);
  11454. } while (this.eat(types$1.comma));
  11455. return attrs;
  11456. }
  11457. maybeParseModuleAttributes() {
  11458. if (this.match(types$1._with) && !this.hasPrecedingLineBreak()) {
  11459. this.expectPlugin("moduleAttributes");
  11460. this.next();
  11461. } else {
  11462. if (this.hasPlugin("moduleAttributes")) return [];
  11463. return null;
  11464. }
  11465. const attrs = [];
  11466. const attributes = new Set();
  11467. do {
  11468. const node = this.startNode();
  11469. node.key = this.parseIdentifier(true);
  11470. if (node.key.name !== "type") {
  11471. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, node.key.name);
  11472. }
  11473. if (attributes.has(node.key.name)) {
  11474. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, node.key.name);
  11475. }
  11476. attributes.add(node.key.name);
  11477. this.expect(types$1.colon);
  11478. if (!this.match(types$1.string)) {
  11479. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11480. }
  11481. node.value = this.parseStringLiteral(this.state.value);
  11482. this.finishNode(node, "ImportAttribute");
  11483. attrs.push(node);
  11484. } while (this.eat(types$1.comma));
  11485. return attrs;
  11486. }
  11487. maybeParseImportAssertions() {
  11488. if (this.isContextual("assert") && !this.hasPrecedingLineBreak()) {
  11489. this.expectPlugin("importAssertions");
  11490. this.next();
  11491. } else {
  11492. if (this.hasPlugin("importAssertions")) return [];
  11493. return null;
  11494. }
  11495. this.eat(types$1.braceL);
  11496. const attrs = this.parseAssertEntries();
  11497. this.eat(types$1.braceR);
  11498. return attrs;
  11499. }
  11500. maybeParseDefaultImportSpecifier(node) {
  11501. if (this.shouldParseDefaultImport(node)) {
  11502. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  11503. return true;
  11504. }
  11505. return false;
  11506. }
  11507. maybeParseStarImportSpecifier(node) {
  11508. if (this.match(types$1.star)) {
  11509. const specifier = this.startNode();
  11510. this.next();
  11511. this.expectContextual("as");
  11512. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  11513. return true;
  11514. }
  11515. return false;
  11516. }
  11517. parseNamedImportSpecifiers(node) {
  11518. let first = true;
  11519. this.expect(types$1.braceL);
  11520. while (!this.eat(types$1.braceR)) {
  11521. if (first) {
  11522. first = false;
  11523. } else {
  11524. if (this.eat(types$1.colon)) {
  11525. throw this.raise(this.state.start, ErrorMessages.DestructureNamedImport);
  11526. }
  11527. this.expect(types$1.comma);
  11528. if (this.eat(types$1.braceR)) break;
  11529. }
  11530. this.parseImportSpecifier(node);
  11531. }
  11532. }
  11533. parseImportSpecifier(node) {
  11534. const specifier = this.startNode();
  11535. const importedIsString = this.match(types$1.string);
  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 (importedIsString) {
  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