Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149
  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 = label;
  12. this.keyword = conf.keyword;
  13. this.beforeExpr = !!conf.beforeExpr;
  14. this.startsExpr = !!conf.startsExpr;
  15. this.rightAssociative = !!conf.rightAssociative;
  16. this.isLoop = !!conf.isLoop;
  17. this.isAssign = !!conf.isAssign;
  18. this.prefix = !!conf.prefix;
  19. this.postfix = !!conf.postfix;
  20. this.binop = conf.binop != null ? conf.binop : null;
  21. this.updateContext = null;
  22. }
  23. }
  24. const keywords = new Map();
  25. function createKeyword(name, options = {}) {
  26. options.keyword = name;
  27. const token = new TokenType(name, options);
  28. keywords.set(name, token);
  29. return token;
  30. }
  31. function createBinop(name, binop) {
  32. return new TokenType(name, {
  33. beforeExpr,
  34. binop
  35. });
  36. }
  37. const types = {
  38. num: new TokenType("num", {
  39. startsExpr
  40. }),
  41. bigint: new TokenType("bigint", {
  42. startsExpr
  43. }),
  44. regexp: new TokenType("regexp", {
  45. startsExpr
  46. }),
  47. string: new TokenType("string", {
  48. startsExpr
  49. }),
  50. name: new TokenType("name", {
  51. startsExpr
  52. }),
  53. eof: new TokenType("eof"),
  54. bracketL: new TokenType("[", {
  55. beforeExpr,
  56. startsExpr
  57. }),
  58. bracketR: new TokenType("]"),
  59. braceL: new TokenType("{", {
  60. beforeExpr,
  61. startsExpr
  62. }),
  63. braceBarL: new TokenType("{|", {
  64. beforeExpr,
  65. startsExpr
  66. }),
  67. braceR: new TokenType("}"),
  68. braceBarR: new TokenType("|}"),
  69. parenL: new TokenType("(", {
  70. beforeExpr,
  71. startsExpr
  72. }),
  73. parenR: new TokenType(")"),
  74. comma: new TokenType(",", {
  75. beforeExpr
  76. }),
  77. semi: new TokenType(";", {
  78. beforeExpr
  79. }),
  80. colon: new TokenType(":", {
  81. beforeExpr
  82. }),
  83. doubleColon: new TokenType("::", {
  84. beforeExpr
  85. }),
  86. dot: new TokenType("."),
  87. question: new TokenType("?", {
  88. beforeExpr
  89. }),
  90. questionDot: new TokenType("?."),
  91. arrow: new TokenType("=>", {
  92. beforeExpr
  93. }),
  94. template: new TokenType("template"),
  95. ellipsis: new TokenType("...", {
  96. beforeExpr
  97. }),
  98. backQuote: new TokenType("`", {
  99. startsExpr
  100. }),
  101. dollarBraceL: new TokenType("${", {
  102. beforeExpr,
  103. startsExpr
  104. }),
  105. at: new TokenType("@"),
  106. hash: new TokenType("#", {
  107. startsExpr
  108. }),
  109. interpreterDirective: new TokenType("#!..."),
  110. eq: new TokenType("=", {
  111. beforeExpr,
  112. isAssign
  113. }),
  114. assign: new TokenType("_=", {
  115. beforeExpr,
  116. isAssign
  117. }),
  118. incDec: new TokenType("++/--", {
  119. prefix,
  120. postfix,
  121. startsExpr
  122. }),
  123. bang: new TokenType("!", {
  124. beforeExpr,
  125. prefix,
  126. startsExpr
  127. }),
  128. tilde: new TokenType("~", {
  129. beforeExpr,
  130. prefix,
  131. startsExpr
  132. }),
  133. pipeline: createBinop("|>", 0),
  134. nullishCoalescing: createBinop("??", 1),
  135. logicalOR: createBinop("||", 2),
  136. logicalAND: createBinop("&&", 3),
  137. bitwiseOR: createBinop("|", 4),
  138. bitwiseXOR: createBinop("^", 5),
  139. bitwiseAND: createBinop("&", 6),
  140. equality: createBinop("==/!=/===/!==", 7),
  141. relational: createBinop("</>/<=/>=", 8),
  142. bitShift: createBinop("<</>>/>>>", 9),
  143. plusMin: new TokenType("+/-", {
  144. beforeExpr,
  145. binop: 10,
  146. prefix,
  147. startsExpr
  148. }),
  149. modulo: new TokenType("%", {
  150. beforeExpr,
  151. binop: 11,
  152. startsExpr
  153. }),
  154. star: createBinop("*", 11),
  155. slash: createBinop("/", 11),
  156. exponent: new TokenType("**", {
  157. beforeExpr,
  158. binop: 12,
  159. rightAssociative: true
  160. }),
  161. _break: createKeyword("break"),
  162. _case: createKeyword("case", {
  163. beforeExpr
  164. }),
  165. _catch: createKeyword("catch"),
  166. _continue: createKeyword("continue"),
  167. _debugger: createKeyword("debugger"),
  168. _default: createKeyword("default", {
  169. beforeExpr
  170. }),
  171. _do: createKeyword("do", {
  172. isLoop,
  173. beforeExpr
  174. }),
  175. _else: createKeyword("else", {
  176. beforeExpr
  177. }),
  178. _finally: createKeyword("finally"),
  179. _for: createKeyword("for", {
  180. isLoop
  181. }),
  182. _function: createKeyword("function", {
  183. startsExpr
  184. }),
  185. _if: createKeyword("if"),
  186. _return: createKeyword("return", {
  187. beforeExpr
  188. }),
  189. _switch: createKeyword("switch"),
  190. _throw: createKeyword("throw", {
  191. beforeExpr,
  192. prefix,
  193. startsExpr
  194. }),
  195. _try: createKeyword("try"),
  196. _var: createKeyword("var"),
  197. _const: createKeyword("const"),
  198. _while: createKeyword("while", {
  199. isLoop
  200. }),
  201. _with: createKeyword("with"),
  202. _new: createKeyword("new", {
  203. beforeExpr,
  204. startsExpr
  205. }),
  206. _this: createKeyword("this", {
  207. startsExpr
  208. }),
  209. _super: createKeyword("super", {
  210. startsExpr
  211. }),
  212. _class: createKeyword("class", {
  213. startsExpr
  214. }),
  215. _extends: createKeyword("extends", {
  216. beforeExpr
  217. }),
  218. _export: createKeyword("export"),
  219. _import: createKeyword("import", {
  220. startsExpr
  221. }),
  222. _null: createKeyword("null", {
  223. startsExpr
  224. }),
  225. _true: createKeyword("true", {
  226. startsExpr
  227. }),
  228. _false: createKeyword("false", {
  229. startsExpr
  230. }),
  231. _in: createKeyword("in", {
  232. beforeExpr,
  233. binop: 8
  234. }),
  235. _instanceof: createKeyword("instanceof", {
  236. beforeExpr,
  237. binop: 8
  238. }),
  239. _typeof: createKeyword("typeof", {
  240. beforeExpr,
  241. prefix,
  242. startsExpr
  243. }),
  244. _void: createKeyword("void", {
  245. beforeExpr,
  246. prefix,
  247. startsExpr
  248. }),
  249. _delete: createKeyword("delete", {
  250. beforeExpr,
  251. prefix,
  252. startsExpr
  253. })
  254. };
  255. const SCOPE_OTHER = 0b0000000000,
  256. SCOPE_PROGRAM = 0b0000000001,
  257. SCOPE_FUNCTION = 0b0000000010,
  258. SCOPE_ASYNC = 0b0000000100,
  259. SCOPE_GENERATOR = 0b0000001000,
  260. SCOPE_ARROW = 0b0000010000,
  261. SCOPE_SIMPLE_CATCH = 0b0000100000,
  262. SCOPE_SUPER = 0b0001000000,
  263. SCOPE_DIRECT_SUPER = 0b0010000000,
  264. SCOPE_CLASS = 0b0100000000,
  265. SCOPE_TS_MODULE = 0b1000000000,
  266. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  267. function functionFlags(isAsync, isGenerator) {
  268. return SCOPE_FUNCTION | (isAsync ? SCOPE_ASYNC : 0) | (isGenerator ? SCOPE_GENERATOR : 0);
  269. }
  270. const BIND_KIND_VALUE = 0b00000000001,
  271. BIND_KIND_TYPE = 0b00000000010,
  272. BIND_SCOPE_VAR = 0b00000000100,
  273. BIND_SCOPE_LEXICAL = 0b00000001000,
  274. BIND_SCOPE_FUNCTION = 0b00000010000,
  275. BIND_FLAGS_NONE = 0b00001000000,
  276. BIND_FLAGS_CLASS = 0b00010000000,
  277. BIND_FLAGS_TS_ENUM = 0b00100000000,
  278. BIND_FLAGS_TS_CONST_ENUM = 0b01000000000,
  279. BIND_FLAGS_TS_EXPORT_ONLY = 0b10000000000;
  280. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  281. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  282. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  283. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  284. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  285. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  286. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  287. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  288. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  289. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  290. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  291. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY;
  292. function isSimpleProperty(node) {
  293. return node != null && node.type === "Property" && node.kind === "init" && node.method === false;
  294. }
  295. var estree = (superClass => class extends superClass {
  296. estreeParseRegExpLiteral({
  297. pattern,
  298. flags
  299. }) {
  300. let regex = null;
  301. try {
  302. regex = new RegExp(pattern, flags);
  303. } catch (e) {}
  304. const node = this.estreeParseLiteral(regex);
  305. node.regex = {
  306. pattern,
  307. flags
  308. };
  309. return node;
  310. }
  311. estreeParseLiteral(value) {
  312. return this.parseLiteral(value, "Literal");
  313. }
  314. directiveToStmt(directive) {
  315. const directiveLiteral = directive.value;
  316. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  317. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  318. expression.value = directiveLiteral.value;
  319. expression.raw = directiveLiteral.extra.raw;
  320. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  321. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  322. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  323. }
  324. initFunction(node, isAsync) {
  325. super.initFunction(node, isAsync);
  326. node.expression = false;
  327. }
  328. checkDeclaration(node) {
  329. if (isSimpleProperty(node)) {
  330. this.checkDeclaration(node.value);
  331. } else {
  332. super.checkDeclaration(node);
  333. }
  334. }
  335. checkGetterSetterParams(method) {
  336. const prop = method;
  337. const paramCount = prop.kind === "get" ? 0 : 1;
  338. const start = prop.start;
  339. if (prop.value.params.length !== paramCount) {
  340. if (prop.kind === "get") {
  341. this.raise(start, "getter must not have any formal parameters");
  342. } else {
  343. this.raise(start, "setter must have exactly one formal parameter");
  344. }
  345. } else if (prop.kind === "set" && prop.value.params[0].type === "RestElement") {
  346. this.raise(start, "setter function argument must not be a rest parameter");
  347. }
  348. }
  349. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription, disallowLetBinding) {
  350. switch (expr.type) {
  351. case "ObjectPattern":
  352. expr.properties.forEach(prop => {
  353. this.checkLVal(prop.type === "Property" ? prop.value : prop, bindingType, checkClashes, "object destructuring pattern", disallowLetBinding);
  354. });
  355. break;
  356. default:
  357. super.checkLVal(expr, bindingType, checkClashes, contextDescription, disallowLetBinding);
  358. }
  359. }
  360. checkDuplicatedProto(prop, protoRef) {
  361. if (prop.type === "SpreadElement" || prop.computed || prop.method || prop.shorthand) {
  362. return;
  363. }
  364. const key = prop.key;
  365. const name = key.type === "Identifier" ? key.name : String(key.value);
  366. if (name === "__proto__" && prop.kind === "init") {
  367. if (protoRef.used && !protoRef.start) {
  368. protoRef.start = key.start;
  369. }
  370. protoRef.used = true;
  371. }
  372. }
  373. isStrictBody(node) {
  374. const isBlockStatement = node.body.type === "BlockStatement";
  375. if (isBlockStatement && node.body.body.length > 0) {
  376. for (let _i = 0, _node$body$body = node.body.body; _i < _node$body$body.length; _i++) {
  377. const directive = _node$body$body[_i];
  378. if (directive.type === "ExpressionStatement" && directive.expression.type === "Literal") {
  379. if (directive.expression.value === "use strict") return true;
  380. } else {
  381. break;
  382. }
  383. }
  384. }
  385. return false;
  386. }
  387. isValidDirective(stmt) {
  388. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && (!stmt.expression.extra || !stmt.expression.extra.parenthesized);
  389. }
  390. stmtToDirective(stmt) {
  391. const directive = super.stmtToDirective(stmt);
  392. const value = stmt.expression.value;
  393. directive.value.value = value;
  394. return directive;
  395. }
  396. parseBlockBody(node, allowDirectives, topLevel, end) {
  397. super.parseBlockBody(node, allowDirectives, topLevel, end);
  398. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  399. node.body = directiveStatements.concat(node.body);
  400. delete node.directives;
  401. }
  402. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  403. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  404. if (method.typeParameters) {
  405. method.value.typeParameters = method.typeParameters;
  406. delete method.typeParameters;
  407. }
  408. classBody.body.push(method);
  409. }
  410. parseExprAtom(refShorthandDefaultPos) {
  411. switch (this.state.type) {
  412. case types.regexp:
  413. return this.estreeParseRegExpLiteral(this.state.value);
  414. case types.num:
  415. case types.string:
  416. return this.estreeParseLiteral(this.state.value);
  417. case types._null:
  418. return this.estreeParseLiteral(null);
  419. case types._true:
  420. return this.estreeParseLiteral(true);
  421. case types._false:
  422. return this.estreeParseLiteral(false);
  423. default:
  424. return super.parseExprAtom(refShorthandDefaultPos);
  425. }
  426. }
  427. parseLiteral(value, type, startPos, startLoc) {
  428. const node = super.parseLiteral(value, type, startPos, startLoc);
  429. node.raw = node.extra.raw;
  430. delete node.extra;
  431. return node;
  432. }
  433. parseFunctionBody(node, allowExpression, isMethod = false) {
  434. super.parseFunctionBody(node, allowExpression, isMethod);
  435. node.expression = node.body.type !== "BlockStatement";
  436. }
  437. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  438. let funcNode = this.startNode();
  439. funcNode.kind = node.kind;
  440. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  441. funcNode.type = "FunctionExpression";
  442. delete funcNode.kind;
  443. node.value = funcNode;
  444. type = type === "ClassMethod" ? "MethodDefinition" : type;
  445. return this.finishNode(node, type);
  446. }
  447. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  448. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc);
  449. if (node) {
  450. node.type = "Property";
  451. if (node.kind === "method") node.kind = "init";
  452. node.shorthand = false;
  453. }
  454. return node;
  455. }
  456. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  457. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  458. if (node) {
  459. node.kind = "init";
  460. node.type = "Property";
  461. }
  462. return node;
  463. }
  464. toAssignable(node, isBinding, contextDescription) {
  465. if (isSimpleProperty(node)) {
  466. this.toAssignable(node.value, isBinding, contextDescription);
  467. return node;
  468. }
  469. return super.toAssignable(node, isBinding, contextDescription);
  470. }
  471. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  472. if (prop.kind === "get" || prop.kind === "set") {
  473. throw this.raise(prop.key.start, "Object pattern can't contain getter or setter");
  474. } else if (prop.method) {
  475. throw this.raise(prop.key.start, "Object pattern can't contain methods");
  476. } else {
  477. super.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  478. }
  479. }
  480. });
  481. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  482. const lineBreakG = new RegExp(lineBreak.source, "g");
  483. function isNewLine(code) {
  484. switch (code) {
  485. case 10:
  486. case 13:
  487. case 8232:
  488. case 8233:
  489. return true;
  490. default:
  491. return false;
  492. }
  493. }
  494. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  495. function isWhitespace(code) {
  496. switch (code) {
  497. case 0x0009:
  498. case 0x000b:
  499. case 0x000c:
  500. case 32:
  501. case 160:
  502. case 5760:
  503. case 0x2000:
  504. case 0x2001:
  505. case 0x2002:
  506. case 0x2003:
  507. case 0x2004:
  508. case 0x2005:
  509. case 0x2006:
  510. case 0x2007:
  511. case 0x2008:
  512. case 0x2009:
  513. case 0x200a:
  514. case 0x202f:
  515. case 0x205f:
  516. case 0x3000:
  517. case 0xfeff:
  518. return true;
  519. default:
  520. return false;
  521. }
  522. }
  523. class TokContext {
  524. constructor(token, isExpr, preserveSpace, override) {
  525. this.token = token;
  526. this.isExpr = !!isExpr;
  527. this.preserveSpace = !!preserveSpace;
  528. this.override = override;
  529. }
  530. }
  531. const types$1 = {
  532. braceStatement: new TokContext("{", false),
  533. braceExpression: new TokContext("{", true),
  534. templateQuasi: new TokContext("${", false),
  535. parenStatement: new TokContext("(", false),
  536. parenExpression: new TokContext("(", true),
  537. template: new TokContext("`", true, true, p => p.readTmplToken()),
  538. functionExpression: new TokContext("function", true),
  539. functionStatement: new TokContext("function", false)
  540. };
  541. types.parenR.updateContext = types.braceR.updateContext = function () {
  542. if (this.state.context.length === 1) {
  543. this.state.exprAllowed = true;
  544. return;
  545. }
  546. let out = this.state.context.pop();
  547. if (out === types$1.braceStatement && this.curContext().token === "function") {
  548. out = this.state.context.pop();
  549. }
  550. this.state.exprAllowed = !out.isExpr;
  551. };
  552. types.name.updateContext = function (prevType) {
  553. let allowed = false;
  554. if (prevType !== types.dot) {
  555. if (this.state.value === "of" && !this.state.exprAllowed || this.state.value === "yield" && this.scope.inGenerator) {
  556. allowed = true;
  557. }
  558. }
  559. this.state.exprAllowed = allowed;
  560. if (this.state.isIterator) {
  561. this.state.isIterator = false;
  562. }
  563. };
  564. types.braceL.updateContext = function (prevType) {
  565. this.state.context.push(this.braceIsBlock(prevType) ? types$1.braceStatement : types$1.braceExpression);
  566. this.state.exprAllowed = true;
  567. };
  568. types.dollarBraceL.updateContext = function () {
  569. this.state.context.push(types$1.templateQuasi);
  570. this.state.exprAllowed = true;
  571. };
  572. types.parenL.updateContext = function (prevType) {
  573. const statementParens = prevType === types._if || prevType === types._for || prevType === types._with || prevType === types._while;
  574. this.state.context.push(statementParens ? types$1.parenStatement : types$1.parenExpression);
  575. this.state.exprAllowed = true;
  576. };
  577. types.incDec.updateContext = function () {};
  578. types._function.updateContext = types._class.updateContext = function (prevType) {
  579. if (prevType.beforeExpr && prevType !== types.semi && prevType !== types._else && !(prevType === types._return && lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) && !((prevType === types.colon || prevType === types.braceL) && this.curContext() === types$1.b_stat)) {
  580. this.state.context.push(types$1.functionExpression);
  581. } else {
  582. this.state.context.push(types$1.functionStatement);
  583. }
  584. this.state.exprAllowed = false;
  585. };
  586. types.backQuote.updateContext = function () {
  587. if (this.curContext() === types$1.template) {
  588. this.state.context.pop();
  589. } else {
  590. this.state.context.push(types$1.template);
  591. }
  592. this.state.exprAllowed = false;
  593. };
  594. const reservedWords = {
  595. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  596. strictBind: ["eval", "arguments"]
  597. };
  598. const reservedWordsStrictSet = new Set(reservedWords.strict);
  599. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  600. const isReservedWord = (word, inModule) => {
  601. return inModule && word === "await" || word === "enum";
  602. };
  603. function isStrictReservedWord(word, inModule) {
  604. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  605. }
  606. function isStrictBindOnlyReservedWord(word) {
  607. return reservedWordsStrictBindSet.has(word);
  608. }
  609. function isStrictBindReservedWord(word, inModule) {
  610. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  611. }
  612. function isKeyword(word) {
  613. return keywords.has(word);
  614. }
  615. const keywordRelationalOperator = /^in(stanceof)?$/;
  616. 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-\u08bd\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\u0d05-\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-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fef\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7c6\ua7f7-\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-\uab67\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";
  617. 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\u0b56\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\u0d82\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\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\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";
  618. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  619. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  620. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  621. 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, 477, 28, 11, 0, 9, 21, 155, 22, 13, 52, 76, 44, 33, 24, 27, 35, 30, 0, 12, 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, 0, 33, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 230, 43, 117, 63, 32, 0, 161, 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, 270, 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, 754, 9486, 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, 42710, 42, 4148, 12, 221, 3, 5761, 15, 7472, 3104, 541];
  622. 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, 525, 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, 4, 9, 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, 232, 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, 19723, 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, 792487, 239];
  623. function isInAstralSet(code, set) {
  624. let pos = 0x10000;
  625. for (let i = 0, length = set.length; i < length; i += 2) {
  626. pos += set[i];
  627. if (pos > code) return false;
  628. pos += set[i + 1];
  629. if (pos >= code) return true;
  630. }
  631. return false;
  632. }
  633. function isIdentifierStart(code) {
  634. if (code < 65) return code === 36;
  635. if (code <= 90) return true;
  636. if (code < 97) return code === 95;
  637. if (code <= 122) return true;
  638. if (code <= 0xffff) {
  639. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  640. }
  641. return isInAstralSet(code, astralIdentifierStartCodes);
  642. }
  643. function isIteratorStart(current, next) {
  644. return current === 64 && next === 64;
  645. }
  646. function isIdentifierChar(code) {
  647. if (code < 48) return code === 36;
  648. if (code < 58) return true;
  649. if (code < 65) return false;
  650. if (code <= 90) return true;
  651. if (code < 97) return code === 95;
  652. if (code <= 122) return true;
  653. if (code <= 0xffff) {
  654. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  655. }
  656. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  657. }
  658. const reservedTypes = ["any", "bool", "boolean", "empty", "false", "mixed", "null", "number", "static", "string", "true", "typeof", "void", "interface", "extends", "_"];
  659. function isEsModuleType(bodyElement) {
  660. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  661. }
  662. function hasTypeImportKind(node) {
  663. return node.importKind === "type" || node.importKind === "typeof";
  664. }
  665. function isMaybeDefaultImport(state) {
  666. return (state.type === types.name || !!state.type.keyword) && state.value !== "from";
  667. }
  668. const exportSuggestions = {
  669. const: "declare export var",
  670. let: "declare export var",
  671. type: "export type",
  672. interface: "export interface"
  673. };
  674. function partition(list, test) {
  675. const list1 = [];
  676. const list2 = [];
  677. for (let i = 0; i < list.length; i++) {
  678. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  679. }
  680. return [list1, list2];
  681. }
  682. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  683. var flow = (superClass => class extends superClass {
  684. constructor(options, input) {
  685. super(options, input);
  686. this.flowPragma = undefined;
  687. }
  688. shouldParseTypes() {
  689. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  690. }
  691. shouldParseEnums() {
  692. return !!this.getPluginOption("flow", "enums");
  693. }
  694. finishToken(type, val) {
  695. if (type !== types.string && type !== types.semi && type !== types.interpreterDirective) {
  696. if (this.flowPragma === undefined) {
  697. this.flowPragma = null;
  698. }
  699. }
  700. return super.finishToken(type, val);
  701. }
  702. addComment(comment) {
  703. if (this.flowPragma === undefined) {
  704. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  705. if (!matches) ; else if (matches[1] === "flow") {
  706. this.flowPragma = "flow";
  707. } else if (matches[1] === "noflow") {
  708. this.flowPragma = "noflow";
  709. } else {
  710. throw new Error("Unexpected flow pragma");
  711. }
  712. }
  713. return super.addComment(comment);
  714. }
  715. flowParseTypeInitialiser(tok) {
  716. const oldInType = this.state.inType;
  717. this.state.inType = true;
  718. this.expect(tok || types.colon);
  719. const type = this.flowParseType();
  720. this.state.inType = oldInType;
  721. return type;
  722. }
  723. flowParsePredicate() {
  724. const node = this.startNode();
  725. const moduloLoc = this.state.startLoc;
  726. const moduloPos = this.state.start;
  727. this.expect(types.modulo);
  728. const checksLoc = this.state.startLoc;
  729. this.expectContextual("checks");
  730. if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) {
  731. this.raise(moduloPos, "Spaces between ´%´ and ´checks´ are not allowed here.");
  732. }
  733. if (this.eat(types.parenL)) {
  734. node.value = this.parseExpression();
  735. this.expect(types.parenR);
  736. return this.finishNode(node, "DeclaredPredicate");
  737. } else {
  738. return this.finishNode(node, "InferredPredicate");
  739. }
  740. }
  741. flowParseTypeAndPredicateInitialiser() {
  742. const oldInType = this.state.inType;
  743. this.state.inType = true;
  744. this.expect(types.colon);
  745. let type = null;
  746. let predicate = null;
  747. if (this.match(types.modulo)) {
  748. this.state.inType = oldInType;
  749. predicate = this.flowParsePredicate();
  750. } else {
  751. type = this.flowParseType();
  752. this.state.inType = oldInType;
  753. if (this.match(types.modulo)) {
  754. predicate = this.flowParsePredicate();
  755. }
  756. }
  757. return [type, predicate];
  758. }
  759. flowParseDeclareClass(node) {
  760. this.next();
  761. this.flowParseInterfaceish(node, true);
  762. return this.finishNode(node, "DeclareClass");
  763. }
  764. flowParseDeclareFunction(node) {
  765. this.next();
  766. const id = node.id = this.parseIdentifier();
  767. const typeNode = this.startNode();
  768. const typeContainer = this.startNode();
  769. if (this.isRelational("<")) {
  770. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  771. } else {
  772. typeNode.typeParameters = null;
  773. }
  774. this.expect(types.parenL);
  775. const tmp = this.flowParseFunctionTypeParams();
  776. typeNode.params = tmp.params;
  777. typeNode.rest = tmp.rest;
  778. this.expect(types.parenR);
  779. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  780. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  781. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  782. this.resetEndLocation(id);
  783. this.semicolon();
  784. return this.finishNode(node, "DeclareFunction");
  785. }
  786. flowParseDeclare(node, insideModule) {
  787. if (this.match(types._class)) {
  788. return this.flowParseDeclareClass(node);
  789. } else if (this.match(types._function)) {
  790. return this.flowParseDeclareFunction(node);
  791. } else if (this.match(types._var)) {
  792. return this.flowParseDeclareVariable(node);
  793. } else if (this.eatContextual("module")) {
  794. if (this.match(types.dot)) {
  795. return this.flowParseDeclareModuleExports(node);
  796. } else {
  797. if (insideModule) {
  798. this.raise(this.state.lastTokStart, "`declare module` cannot be used inside another `declare module`");
  799. }
  800. return this.flowParseDeclareModule(node);
  801. }
  802. } else if (this.isContextual("type")) {
  803. return this.flowParseDeclareTypeAlias(node);
  804. } else if (this.isContextual("opaque")) {
  805. return this.flowParseDeclareOpaqueType(node);
  806. } else if (this.isContextual("interface")) {
  807. return this.flowParseDeclareInterface(node);
  808. } else if (this.match(types._export)) {
  809. return this.flowParseDeclareExportDeclaration(node, insideModule);
  810. } else {
  811. throw this.unexpected();
  812. }
  813. }
  814. flowParseDeclareVariable(node) {
  815. this.next();
  816. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  817. this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
  818. this.semicolon();
  819. return this.finishNode(node, "DeclareVariable");
  820. }
  821. flowParseDeclareModule(node) {
  822. this.scope.enter(SCOPE_OTHER);
  823. if (this.match(types.string)) {
  824. node.id = this.parseExprAtom();
  825. } else {
  826. node.id = this.parseIdentifier();
  827. }
  828. const bodyNode = node.body = this.startNode();
  829. const body = bodyNode.body = [];
  830. this.expect(types.braceL);
  831. while (!this.match(types.braceR)) {
  832. let bodyNode = this.startNode();
  833. if (this.match(types._import)) {
  834. this.next();
  835. if (!this.isContextual("type") && !this.match(types._typeof)) {
  836. this.raise(this.state.lastTokStart, "Imports within a `declare module` body must always be `import type` or `import typeof`");
  837. }
  838. this.parseImport(bodyNode);
  839. } else {
  840. this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");
  841. bodyNode = this.flowParseDeclare(bodyNode, true);
  842. }
  843. body.push(bodyNode);
  844. }
  845. this.scope.exit();
  846. this.expect(types.braceR);
  847. this.finishNode(bodyNode, "BlockStatement");
  848. let kind = null;
  849. let hasModuleExport = false;
  850. const errorMessage = "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";
  851. body.forEach(bodyElement => {
  852. if (isEsModuleType(bodyElement)) {
  853. if (kind === "CommonJS") {
  854. this.raise(bodyElement.start, errorMessage);
  855. }
  856. kind = "ES";
  857. } else if (bodyElement.type === "DeclareModuleExports") {
  858. if (hasModuleExport) {
  859. this.raise(bodyElement.start, "Duplicate `declare module.exports` statement");
  860. }
  861. if (kind === "ES") this.raise(bodyElement.start, errorMessage);
  862. kind = "CommonJS";
  863. hasModuleExport = true;
  864. }
  865. });
  866. node.kind = kind || "CommonJS";
  867. return this.finishNode(node, "DeclareModule");
  868. }
  869. flowParseDeclareExportDeclaration(node, insideModule) {
  870. this.expect(types._export);
  871. if (this.eat(types._default)) {
  872. if (this.match(types._function) || this.match(types._class)) {
  873. node.declaration = this.flowParseDeclare(this.startNode());
  874. } else {
  875. node.declaration = this.flowParseType();
  876. this.semicolon();
  877. }
  878. node.default = true;
  879. return this.finishNode(node, "DeclareExportDeclaration");
  880. } else {
  881. if (this.match(types._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  882. const label = this.state.value;
  883. const suggestion = exportSuggestions[label];
  884. this.unexpected(this.state.start, `\`declare export ${label}\` is not supported. Use \`${suggestion}\` instead`);
  885. }
  886. if (this.match(types._var) || this.match(types._function) || this.match(types._class) || this.isContextual("opaque")) {
  887. node.declaration = this.flowParseDeclare(this.startNode());
  888. node.default = false;
  889. return this.finishNode(node, "DeclareExportDeclaration");
  890. } else if (this.match(types.star) || this.match(types.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  891. node = this.parseExport(node);
  892. if (node.type === "ExportNamedDeclaration") {
  893. node.type = "ExportDeclaration";
  894. node.default = false;
  895. delete node.exportKind;
  896. }
  897. node.type = "Declare" + node.type;
  898. return node;
  899. }
  900. }
  901. throw this.unexpected();
  902. }
  903. flowParseDeclareModuleExports(node) {
  904. this.next();
  905. this.expectContextual("exports");
  906. node.typeAnnotation = this.flowParseTypeAnnotation();
  907. this.semicolon();
  908. return this.finishNode(node, "DeclareModuleExports");
  909. }
  910. flowParseDeclareTypeAlias(node) {
  911. this.next();
  912. this.flowParseTypeAlias(node);
  913. node.type = "DeclareTypeAlias";
  914. return node;
  915. }
  916. flowParseDeclareOpaqueType(node) {
  917. this.next();
  918. this.flowParseOpaqueType(node, true);
  919. node.type = "DeclareOpaqueType";
  920. return node;
  921. }
  922. flowParseDeclareInterface(node) {
  923. this.next();
  924. this.flowParseInterfaceish(node);
  925. return this.finishNode(node, "DeclareInterface");
  926. }
  927. flowParseInterfaceish(node, isClass = false) {
  928. node.id = this.flowParseRestrictedIdentifier(!isClass);
  929. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.start);
  930. if (this.isRelational("<")) {
  931. node.typeParameters = this.flowParseTypeParameterDeclaration();
  932. } else {
  933. node.typeParameters = null;
  934. }
  935. node.extends = [];
  936. node.implements = [];
  937. node.mixins = [];
  938. if (this.eat(types._extends)) {
  939. do {
  940. node.extends.push(this.flowParseInterfaceExtends());
  941. } while (!isClass && this.eat(types.comma));
  942. }
  943. if (this.isContextual("mixins")) {
  944. this.next();
  945. do {
  946. node.mixins.push(this.flowParseInterfaceExtends());
  947. } while (this.eat(types.comma));
  948. }
  949. if (this.isContextual("implements")) {
  950. this.next();
  951. do {
  952. node.implements.push(this.flowParseInterfaceExtends());
  953. } while (this.eat(types.comma));
  954. }
  955. node.body = this.flowParseObjectType({
  956. allowStatic: isClass,
  957. allowExact: false,
  958. allowSpread: false,
  959. allowProto: isClass,
  960. allowInexact: false
  961. });
  962. }
  963. flowParseInterfaceExtends() {
  964. const node = this.startNode();
  965. node.id = this.flowParseQualifiedTypeIdentifier();
  966. if (this.isRelational("<")) {
  967. node.typeParameters = this.flowParseTypeParameterInstantiation();
  968. } else {
  969. node.typeParameters = null;
  970. }
  971. return this.finishNode(node, "InterfaceExtends");
  972. }
  973. flowParseInterface(node) {
  974. this.flowParseInterfaceish(node);
  975. return this.finishNode(node, "InterfaceDeclaration");
  976. }
  977. checkNotUnderscore(word) {
  978. if (word === "_") {
  979. this.raise(this.state.start, "`_` is only allowed as a type argument to call or new");
  980. }
  981. }
  982. checkReservedType(word, startLoc) {
  983. if (reservedTypes.indexOf(word) > -1) {
  984. this.raise(startLoc, `Cannot overwrite reserved type ${word}`);
  985. }
  986. }
  987. flowParseRestrictedIdentifier(liberal) {
  988. this.checkReservedType(this.state.value, this.state.start);
  989. return this.parseIdentifier(liberal);
  990. }
  991. flowParseTypeAlias(node) {
  992. node.id = this.flowParseRestrictedIdentifier();
  993. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  994. if (this.isRelational("<")) {
  995. node.typeParameters = this.flowParseTypeParameterDeclaration();
  996. } else {
  997. node.typeParameters = null;
  998. }
  999. node.right = this.flowParseTypeInitialiser(types.eq);
  1000. this.semicolon();
  1001. return this.finishNode(node, "TypeAlias");
  1002. }
  1003. flowParseOpaqueType(node, declare) {
  1004. this.expectContextual("type");
  1005. node.id = this.flowParseRestrictedIdentifier(true);
  1006. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  1007. if (this.isRelational("<")) {
  1008. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1009. } else {
  1010. node.typeParameters = null;
  1011. }
  1012. node.supertype = null;
  1013. if (this.match(types.colon)) {
  1014. node.supertype = this.flowParseTypeInitialiser(types.colon);
  1015. }
  1016. node.impltype = null;
  1017. if (!declare) {
  1018. node.impltype = this.flowParseTypeInitialiser(types.eq);
  1019. }
  1020. this.semicolon();
  1021. return this.finishNode(node, "OpaqueType");
  1022. }
  1023. flowParseTypeParameter(requireDefault = false) {
  1024. const nodeStart = this.state.start;
  1025. const node = this.startNode();
  1026. const variance = this.flowParseVariance();
  1027. const ident = this.flowParseTypeAnnotatableIdentifier();
  1028. node.name = ident.name;
  1029. node.variance = variance;
  1030. node.bound = ident.typeAnnotation;
  1031. if (this.match(types.eq)) {
  1032. this.eat(types.eq);
  1033. node.default = this.flowParseType();
  1034. } else {
  1035. if (requireDefault) {
  1036. this.raise(nodeStart, "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.");
  1037. }
  1038. }
  1039. return this.finishNode(node, "TypeParameter");
  1040. }
  1041. flowParseTypeParameterDeclaration() {
  1042. const oldInType = this.state.inType;
  1043. const node = this.startNode();
  1044. node.params = [];
  1045. this.state.inType = true;
  1046. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  1047. this.next();
  1048. } else {
  1049. this.unexpected();
  1050. }
  1051. let defaultRequired = false;
  1052. do {
  1053. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  1054. node.params.push(typeParameter);
  1055. if (typeParameter.default) {
  1056. defaultRequired = true;
  1057. }
  1058. if (!this.isRelational(">")) {
  1059. this.expect(types.comma);
  1060. }
  1061. } while (!this.isRelational(">"));
  1062. this.expectRelational(">");
  1063. this.state.inType = oldInType;
  1064. return this.finishNode(node, "TypeParameterDeclaration");
  1065. }
  1066. flowParseTypeParameterInstantiation() {
  1067. const node = this.startNode();
  1068. const oldInType = this.state.inType;
  1069. node.params = [];
  1070. this.state.inType = true;
  1071. this.expectRelational("<");
  1072. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1073. this.state.noAnonFunctionType = false;
  1074. while (!this.isRelational(">")) {
  1075. node.params.push(this.flowParseType());
  1076. if (!this.isRelational(">")) {
  1077. this.expect(types.comma);
  1078. }
  1079. }
  1080. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1081. this.expectRelational(">");
  1082. this.state.inType = oldInType;
  1083. return this.finishNode(node, "TypeParameterInstantiation");
  1084. }
  1085. flowParseTypeParameterInstantiationCallOrNew() {
  1086. const node = this.startNode();
  1087. const oldInType = this.state.inType;
  1088. node.params = [];
  1089. this.state.inType = true;
  1090. this.expectRelational("<");
  1091. while (!this.isRelational(">")) {
  1092. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1093. if (!this.isRelational(">")) {
  1094. this.expect(types.comma);
  1095. }
  1096. }
  1097. this.expectRelational(">");
  1098. this.state.inType = oldInType;
  1099. return this.finishNode(node, "TypeParameterInstantiation");
  1100. }
  1101. flowParseInterfaceType() {
  1102. const node = this.startNode();
  1103. this.expectContextual("interface");
  1104. node.extends = [];
  1105. if (this.eat(types._extends)) {
  1106. do {
  1107. node.extends.push(this.flowParseInterfaceExtends());
  1108. } while (this.eat(types.comma));
  1109. }
  1110. node.body = this.flowParseObjectType({
  1111. allowStatic: false,
  1112. allowExact: false,
  1113. allowSpread: false,
  1114. allowProto: false,
  1115. allowInexact: false
  1116. });
  1117. return this.finishNode(node, "InterfaceTypeAnnotation");
  1118. }
  1119. flowParseObjectPropertyKey() {
  1120. return this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1121. }
  1122. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1123. node.static = isStatic;
  1124. if (this.lookahead().type === types.colon) {
  1125. node.id = this.flowParseObjectPropertyKey();
  1126. node.key = this.flowParseTypeInitialiser();
  1127. } else {
  1128. node.id = null;
  1129. node.key = this.flowParseType();
  1130. }
  1131. this.expect(types.bracketR);
  1132. node.value = this.flowParseTypeInitialiser();
  1133. node.variance = variance;
  1134. return this.finishNode(node, "ObjectTypeIndexer");
  1135. }
  1136. flowParseObjectTypeInternalSlot(node, isStatic) {
  1137. node.static = isStatic;
  1138. node.id = this.flowParseObjectPropertyKey();
  1139. this.expect(types.bracketR);
  1140. this.expect(types.bracketR);
  1141. if (this.isRelational("<") || this.match(types.parenL)) {
  1142. node.method = true;
  1143. node.optional = false;
  1144. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1145. } else {
  1146. node.method = false;
  1147. if (this.eat(types.question)) {
  1148. node.optional = true;
  1149. }
  1150. node.value = this.flowParseTypeInitialiser();
  1151. }
  1152. return this.finishNode(node, "ObjectTypeInternalSlot");
  1153. }
  1154. flowParseObjectTypeMethodish(node) {
  1155. node.params = [];
  1156. node.rest = null;
  1157. node.typeParameters = null;
  1158. if (this.isRelational("<")) {
  1159. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1160. }
  1161. this.expect(types.parenL);
  1162. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1163. node.params.push(this.flowParseFunctionTypeParam());
  1164. if (!this.match(types.parenR)) {
  1165. this.expect(types.comma);
  1166. }
  1167. }
  1168. if (this.eat(types.ellipsis)) {
  1169. node.rest = this.flowParseFunctionTypeParam();
  1170. }
  1171. this.expect(types.parenR);
  1172. node.returnType = this.flowParseTypeInitialiser();
  1173. return this.finishNode(node, "FunctionTypeAnnotation");
  1174. }
  1175. flowParseObjectTypeCallProperty(node, isStatic) {
  1176. const valueNode = this.startNode();
  1177. node.static = isStatic;
  1178. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1179. return this.finishNode(node, "ObjectTypeCallProperty");
  1180. }
  1181. flowParseObjectType({
  1182. allowStatic,
  1183. allowExact,
  1184. allowSpread,
  1185. allowProto,
  1186. allowInexact
  1187. }) {
  1188. const oldInType = this.state.inType;
  1189. this.state.inType = true;
  1190. const nodeStart = this.startNode();
  1191. nodeStart.callProperties = [];
  1192. nodeStart.properties = [];
  1193. nodeStart.indexers = [];
  1194. nodeStart.internalSlots = [];
  1195. let endDelim;
  1196. let exact;
  1197. let inexact = false;
  1198. if (allowExact && this.match(types.braceBarL)) {
  1199. this.expect(types.braceBarL);
  1200. endDelim = types.braceBarR;
  1201. exact = true;
  1202. } else {
  1203. this.expect(types.braceL);
  1204. endDelim = types.braceR;
  1205. exact = false;
  1206. }
  1207. nodeStart.exact = exact;
  1208. while (!this.match(endDelim)) {
  1209. let isStatic = false;
  1210. let protoStart = null;
  1211. let inexactStart = null;
  1212. const node = this.startNode();
  1213. if (allowProto && this.isContextual("proto")) {
  1214. const lookahead = this.lookahead();
  1215. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1216. this.next();
  1217. protoStart = this.state.start;
  1218. allowStatic = false;
  1219. }
  1220. }
  1221. if (allowStatic && this.isContextual("static")) {
  1222. const lookahead = this.lookahead();
  1223. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1224. this.next();
  1225. isStatic = true;
  1226. }
  1227. }
  1228. const variance = this.flowParseVariance();
  1229. if (this.eat(types.bracketL)) {
  1230. if (protoStart != null) {
  1231. this.unexpected(protoStart);
  1232. }
  1233. if (this.eat(types.bracketL)) {
  1234. if (variance) {
  1235. this.unexpected(variance.start);
  1236. }
  1237. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  1238. } else {
  1239. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  1240. }
  1241. } else if (this.match(types.parenL) || this.isRelational("<")) {
  1242. if (protoStart != null) {
  1243. this.unexpected(protoStart);
  1244. }
  1245. if (variance) {
  1246. this.unexpected(variance.start);
  1247. }
  1248. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  1249. } else {
  1250. var _allowInexact;
  1251. let kind = "init";
  1252. if (this.isContextual("get") || this.isContextual("set")) {
  1253. const lookahead = this.lookahead();
  1254. if (lookahead.type === types.name || lookahead.type === types.string || lookahead.type === types.num) {
  1255. kind = this.state.value;
  1256. this.next();
  1257. }
  1258. }
  1259. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, (_allowInexact = allowInexact) !== null && _allowInexact !== void 0 ? _allowInexact : !exact);
  1260. if (propOrInexact === null) {
  1261. inexact = true;
  1262. inexactStart = this.state.lastTokStart;
  1263. } else {
  1264. nodeStart.properties.push(propOrInexact);
  1265. }
  1266. }
  1267. this.flowObjectTypeSemicolon();
  1268. if (inexactStart && !this.match(types.braceR) && !this.match(types.braceBarR)) {
  1269. this.raise(inexactStart, "Explicit inexact syntax must appear at the end of an inexact object");
  1270. }
  1271. }
  1272. this.expect(endDelim);
  1273. if (allowSpread) {
  1274. nodeStart.inexact = inexact;
  1275. }
  1276. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  1277. this.state.inType = oldInType;
  1278. return out;
  1279. }
  1280. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  1281. if (this.eat(types.ellipsis)) {
  1282. const isInexactToken = this.match(types.comma) || this.match(types.semi) || this.match(types.braceR) || this.match(types.braceBarR);
  1283. if (isInexactToken) {
  1284. if (!allowSpread) {
  1285. this.raise(this.state.lastTokStart, "Explicit inexact syntax cannot appear in class or interface definitions");
  1286. } else if (!allowInexact) {
  1287. this.raise(this.state.lastTokStart, "Explicit inexact syntax cannot appear inside an explicit exact object type");
  1288. }
  1289. if (variance) {
  1290. this.raise(variance.start, "Explicit inexact syntax cannot have variance");
  1291. }
  1292. return null;
  1293. }
  1294. if (!allowSpread) {
  1295. this.raise(this.state.lastTokStart, "Spread operator cannot appear in class or interface definitions");
  1296. }
  1297. if (protoStart != null) {
  1298. this.unexpected(protoStart);
  1299. }
  1300. if (variance) {
  1301. this.raise(variance.start, "Spread properties cannot have variance");
  1302. }
  1303. node.argument = this.flowParseType();
  1304. return this.finishNode(node, "ObjectTypeSpreadProperty");
  1305. } else {
  1306. node.key = this.flowParseObjectPropertyKey();
  1307. node.static = isStatic;
  1308. node.proto = protoStart != null;
  1309. node.kind = kind;
  1310. let optional = false;
  1311. if (this.isRelational("<") || this.match(types.parenL)) {
  1312. node.method = true;
  1313. if (protoStart != null) {
  1314. this.unexpected(protoStart);
  1315. }
  1316. if (variance) {
  1317. this.unexpected(variance.start);
  1318. }
  1319. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1320. if (kind === "get" || kind === "set") {
  1321. this.flowCheckGetterSetterParams(node);
  1322. }
  1323. } else {
  1324. if (kind !== "init") this.unexpected();
  1325. node.method = false;
  1326. if (this.eat(types.question)) {
  1327. optional = true;
  1328. }
  1329. node.value = this.flowParseTypeInitialiser();
  1330. node.variance = variance;
  1331. }
  1332. node.optional = optional;
  1333. return this.finishNode(node, "ObjectTypeProperty");
  1334. }
  1335. }
  1336. flowCheckGetterSetterParams(property) {
  1337. const paramCount = property.kind === "get" ? 0 : 1;
  1338. const start = property.start;
  1339. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  1340. if (length !== paramCount) {
  1341. if (property.kind === "get") {
  1342. this.raise(start, "getter must not have any formal parameters");
  1343. } else {
  1344. this.raise(start, "setter must have exactly one formal parameter");
  1345. }
  1346. }
  1347. if (property.kind === "set" && property.value.rest) {
  1348. this.raise(start, "setter function argument must not be a rest parameter");
  1349. }
  1350. }
  1351. flowObjectTypeSemicolon() {
  1352. if (!this.eat(types.semi) && !this.eat(types.comma) && !this.match(types.braceR) && !this.match(types.braceBarR)) {
  1353. this.unexpected();
  1354. }
  1355. }
  1356. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  1357. startPos = startPos || this.state.start;
  1358. startLoc = startLoc || this.state.startLoc;
  1359. let node = id || this.parseIdentifier();
  1360. while (this.eat(types.dot)) {
  1361. const node2 = this.startNodeAt(startPos, startLoc);
  1362. node2.qualification = node;
  1363. node2.id = this.parseIdentifier();
  1364. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  1365. }
  1366. return node;
  1367. }
  1368. flowParseGenericType(startPos, startLoc, id) {
  1369. const node = this.startNodeAt(startPos, startLoc);
  1370. node.typeParameters = null;
  1371. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  1372. if (this.isRelational("<")) {
  1373. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1374. }
  1375. return this.finishNode(node, "GenericTypeAnnotation");
  1376. }
  1377. flowParseTypeofType() {
  1378. const node = this.startNode();
  1379. this.expect(types._typeof);
  1380. node.argument = this.flowParsePrimaryType();
  1381. return this.finishNode(node, "TypeofTypeAnnotation");
  1382. }
  1383. flowParseTupleType() {
  1384. const node = this.startNode();
  1385. node.types = [];
  1386. this.expect(types.bracketL);
  1387. while (this.state.pos < this.length && !this.match(types.bracketR)) {
  1388. node.types.push(this.flowParseType());
  1389. if (this.match(types.bracketR)) break;
  1390. this.expect(types.comma);
  1391. }
  1392. this.expect(types.bracketR);
  1393. return this.finishNode(node, "TupleTypeAnnotation");
  1394. }
  1395. flowParseFunctionTypeParam() {
  1396. let name = null;
  1397. let optional = false;
  1398. let typeAnnotation = null;
  1399. const node = this.startNode();
  1400. const lh = this.lookahead();
  1401. if (lh.type === types.colon || lh.type === types.question) {
  1402. name = this.parseIdentifier();
  1403. if (this.eat(types.question)) {
  1404. optional = true;
  1405. }
  1406. typeAnnotation = this.flowParseTypeInitialiser();
  1407. } else {
  1408. typeAnnotation = this.flowParseType();
  1409. }
  1410. node.name = name;
  1411. node.optional = optional;
  1412. node.typeAnnotation = typeAnnotation;
  1413. return this.finishNode(node, "FunctionTypeParam");
  1414. }
  1415. reinterpretTypeAsFunctionTypeParam(type) {
  1416. const node = this.startNodeAt(type.start, type.loc.start);
  1417. node.name = null;
  1418. node.optional = false;
  1419. node.typeAnnotation = type;
  1420. return this.finishNode(node, "FunctionTypeParam");
  1421. }
  1422. flowParseFunctionTypeParams(params = []) {
  1423. let rest = null;
  1424. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1425. params.push(this.flowParseFunctionTypeParam());
  1426. if (!this.match(types.parenR)) {
  1427. this.expect(types.comma);
  1428. }
  1429. }
  1430. if (this.eat(types.ellipsis)) {
  1431. rest = this.flowParseFunctionTypeParam();
  1432. }
  1433. return {
  1434. params,
  1435. rest
  1436. };
  1437. }
  1438. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  1439. switch (id.name) {
  1440. case "any":
  1441. return this.finishNode(node, "AnyTypeAnnotation");
  1442. case "bool":
  1443. case "boolean":
  1444. return this.finishNode(node, "BooleanTypeAnnotation");
  1445. case "mixed":
  1446. return this.finishNode(node, "MixedTypeAnnotation");
  1447. case "empty":
  1448. return this.finishNode(node, "EmptyTypeAnnotation");
  1449. case "number":
  1450. return this.finishNode(node, "NumberTypeAnnotation");
  1451. case "string":
  1452. return this.finishNode(node, "StringTypeAnnotation");
  1453. default:
  1454. this.checkNotUnderscore(id.name);
  1455. return this.flowParseGenericType(startPos, startLoc, id);
  1456. }
  1457. }
  1458. flowParsePrimaryType() {
  1459. const startPos = this.state.start;
  1460. const startLoc = this.state.startLoc;
  1461. const node = this.startNode();
  1462. let tmp;
  1463. let type;
  1464. let isGroupedType = false;
  1465. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1466. switch (this.state.type) {
  1467. case types.name:
  1468. if (this.isContextual("interface")) {
  1469. return this.flowParseInterfaceType();
  1470. }
  1471. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  1472. case types.braceL:
  1473. return this.flowParseObjectType({
  1474. allowStatic: false,
  1475. allowExact: false,
  1476. allowSpread: true,
  1477. allowProto: false,
  1478. allowInexact: true
  1479. });
  1480. case types.braceBarL:
  1481. return this.flowParseObjectType({
  1482. allowStatic: false,
  1483. allowExact: true,
  1484. allowSpread: true,
  1485. allowProto: false,
  1486. allowInexact: false
  1487. });
  1488. case types.bracketL:
  1489. this.state.noAnonFunctionType = false;
  1490. type = this.flowParseTupleType();
  1491. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1492. return type;
  1493. case types.relational:
  1494. if (this.state.value === "<") {
  1495. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1496. this.expect(types.parenL);
  1497. tmp = this.flowParseFunctionTypeParams();
  1498. node.params = tmp.params;
  1499. node.rest = tmp.rest;
  1500. this.expect(types.parenR);
  1501. this.expect(types.arrow);
  1502. node.returnType = this.flowParseType();
  1503. return this.finishNode(node, "FunctionTypeAnnotation");
  1504. }
  1505. break;
  1506. case types.parenL:
  1507. this.next();
  1508. if (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1509. if (this.match(types.name)) {
  1510. const token = this.lookahead().type;
  1511. isGroupedType = token !== types.question && token !== types.colon;
  1512. } else {
  1513. isGroupedType = true;
  1514. }
  1515. }
  1516. if (isGroupedType) {
  1517. this.state.noAnonFunctionType = false;
  1518. type = this.flowParseType();
  1519. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1520. if (this.state.noAnonFunctionType || !(this.match(types.comma) || this.match(types.parenR) && this.lookahead().type === types.arrow)) {
  1521. this.expect(types.parenR);
  1522. return type;
  1523. } else {
  1524. this.eat(types.comma);
  1525. }
  1526. }
  1527. if (type) {
  1528. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  1529. } else {
  1530. tmp = this.flowParseFunctionTypeParams();
  1531. }
  1532. node.params = tmp.params;
  1533. node.rest = tmp.rest;
  1534. this.expect(types.parenR);
  1535. this.expect(types.arrow);
  1536. node.returnType = this.flowParseType();
  1537. node.typeParameters = null;
  1538. return this.finishNode(node, "FunctionTypeAnnotation");
  1539. case types.string:
  1540. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  1541. case types._true:
  1542. case types._false:
  1543. node.value = this.match(types._true);
  1544. this.next();
  1545. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  1546. case types.plusMin:
  1547. if (this.state.value === "-") {
  1548. this.next();
  1549. if (this.match(types.num)) {
  1550. return this.parseLiteral(-this.state.value, "NumberLiteralTypeAnnotation", node.start, node.loc.start);
  1551. }
  1552. if (this.match(types.bigint)) {
  1553. return this.parseLiteral(-this.state.value, "BigIntLiteralTypeAnnotation", node.start, node.loc.start);
  1554. }
  1555. throw this.raise(this.state.start, `Unexpected token, expected "number" or "bigint"`);
  1556. }
  1557. this.unexpected();
  1558. case types.num:
  1559. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  1560. case types.bigint:
  1561. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  1562. case types._void:
  1563. this.next();
  1564. return this.finishNode(node, "VoidTypeAnnotation");
  1565. case types._null:
  1566. this.next();
  1567. return this.finishNode(node, "NullLiteralTypeAnnotation");
  1568. case types._this:
  1569. this.next();
  1570. return this.finishNode(node, "ThisTypeAnnotation");
  1571. case types.star:
  1572. this.next();
  1573. return this.finishNode(node, "ExistsTypeAnnotation");
  1574. default:
  1575. if (this.state.type.keyword === "typeof") {
  1576. return this.flowParseTypeofType();
  1577. } else if (this.state.type.keyword) {
  1578. const label = this.state.type.label;
  1579. this.next();
  1580. return super.createIdentifier(node, label);
  1581. }
  1582. }
  1583. throw this.unexpected();
  1584. }
  1585. flowParsePostfixType() {
  1586. const startPos = this.state.start,
  1587. startLoc = this.state.startLoc;
  1588. let type = this.flowParsePrimaryType();
  1589. while (this.match(types.bracketL) && !this.canInsertSemicolon()) {
  1590. const node = this.startNodeAt(startPos, startLoc);
  1591. node.elementType = type;
  1592. this.expect(types.bracketL);
  1593. this.expect(types.bracketR);
  1594. type = this.finishNode(node, "ArrayTypeAnnotation");
  1595. }
  1596. return type;
  1597. }
  1598. flowParsePrefixType() {
  1599. const node = this.startNode();
  1600. if (this.eat(types.question)) {
  1601. node.typeAnnotation = this.flowParsePrefixType();
  1602. return this.finishNode(node, "NullableTypeAnnotation");
  1603. } else {
  1604. return this.flowParsePostfixType();
  1605. }
  1606. }
  1607. flowParseAnonFunctionWithoutParens() {
  1608. const param = this.flowParsePrefixType();
  1609. if (!this.state.noAnonFunctionType && this.eat(types.arrow)) {
  1610. const node = this.startNodeAt(param.start, param.loc.start);
  1611. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  1612. node.rest = null;
  1613. node.returnType = this.flowParseType();
  1614. node.typeParameters = null;
  1615. return this.finishNode(node, "FunctionTypeAnnotation");
  1616. }
  1617. return param;
  1618. }
  1619. flowParseIntersectionType() {
  1620. const node = this.startNode();
  1621. this.eat(types.bitwiseAND);
  1622. const type = this.flowParseAnonFunctionWithoutParens();
  1623. node.types = [type];
  1624. while (this.eat(types.bitwiseAND)) {
  1625. node.types.push(this.flowParseAnonFunctionWithoutParens());
  1626. }
  1627. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  1628. }
  1629. flowParseUnionType() {
  1630. const node = this.startNode();
  1631. this.eat(types.bitwiseOR);
  1632. const type = this.flowParseIntersectionType();
  1633. node.types = [type];
  1634. while (this.eat(types.bitwiseOR)) {
  1635. node.types.push(this.flowParseIntersectionType());
  1636. }
  1637. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  1638. }
  1639. flowParseType() {
  1640. const oldInType = this.state.inType;
  1641. this.state.inType = true;
  1642. const type = this.flowParseUnionType();
  1643. this.state.inType = oldInType;
  1644. this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
  1645. return type;
  1646. }
  1647. flowParseTypeOrImplicitInstantiation() {
  1648. if (this.state.type === types.name && this.state.value === "_") {
  1649. const startPos = this.state.start;
  1650. const startLoc = this.state.startLoc;
  1651. const node = this.parseIdentifier();
  1652. return this.flowParseGenericType(startPos, startLoc, node);
  1653. } else {
  1654. return this.flowParseType();
  1655. }
  1656. }
  1657. flowParseTypeAnnotation() {
  1658. const node = this.startNode();
  1659. node.typeAnnotation = this.flowParseTypeInitialiser();
  1660. return this.finishNode(node, "TypeAnnotation");
  1661. }
  1662. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  1663. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  1664. if (this.match(types.colon)) {
  1665. ident.typeAnnotation = this.flowParseTypeAnnotation();
  1666. this.resetEndLocation(ident);
  1667. }
  1668. return ident;
  1669. }
  1670. typeCastToParameter(node) {
  1671. node.expression.typeAnnotation = node.typeAnnotation;
  1672. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  1673. return node.expression;
  1674. }
  1675. flowParseVariance() {
  1676. let variance = null;
  1677. if (this.match(types.plusMin)) {
  1678. variance = this.startNode();
  1679. if (this.state.value === "+") {
  1680. variance.kind = "plus";
  1681. } else {
  1682. variance.kind = "minus";
  1683. }
  1684. this.next();
  1685. this.finishNode(variance, "Variance");
  1686. }
  1687. return variance;
  1688. }
  1689. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  1690. if (allowExpressionBody) {
  1691. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  1692. }
  1693. return super.parseFunctionBody(node, false, isMethod);
  1694. }
  1695. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  1696. if (this.match(types.colon)) {
  1697. const typeNode = this.startNode();
  1698. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1699. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  1700. }
  1701. super.parseFunctionBodyAndFinish(node, type, isMethod);
  1702. }
  1703. parseStatement(context, topLevel) {
  1704. if (this.state.strict && this.match(types.name) && this.state.value === "interface") {
  1705. const node = this.startNode();
  1706. this.next();
  1707. return this.flowParseInterface(node);
  1708. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  1709. const node = this.startNode();
  1710. this.next();
  1711. return this.flowParseEnumDeclaration(node);
  1712. } else {
  1713. const stmt = super.parseStatement(context, topLevel);
  1714. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  1715. this.flowPragma = null;
  1716. }
  1717. return stmt;
  1718. }
  1719. }
  1720. parseExpressionStatement(node, expr) {
  1721. if (expr.type === "Identifier") {
  1722. if (expr.name === "declare") {
  1723. if (this.match(types._class) || this.match(types.name) || this.match(types._function) || this.match(types._var) || this.match(types._export)) {
  1724. return this.flowParseDeclare(node);
  1725. }
  1726. } else if (this.match(types.name)) {
  1727. if (expr.name === "interface") {
  1728. return this.flowParseInterface(node);
  1729. } else if (expr.name === "type") {
  1730. return this.flowParseTypeAlias(node);
  1731. } else if (expr.name === "opaque") {
  1732. return this.flowParseOpaqueType(node, false);
  1733. }
  1734. }
  1735. }
  1736. return super.parseExpressionStatement(node, expr);
  1737. }
  1738. shouldParseExportDeclaration() {
  1739. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || this.shouldParseEnums() && this.isContextual("enum") || super.shouldParseExportDeclaration();
  1740. }
  1741. isExportDefaultSpecifier() {
  1742. if (this.match(types.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque" || this.shouldParseEnums() && this.state.value === "enum")) {
  1743. return false;
  1744. }
  1745. return super.isExportDefaultSpecifier();
  1746. }
  1747. parseExportDefaultExpression() {
  1748. if (this.shouldParseEnums() && this.isContextual("enum")) {
  1749. const node = this.startNode();
  1750. this.next();
  1751. return this.flowParseEnumDeclaration(node);
  1752. }
  1753. return super.parseExportDefaultExpression();
  1754. }
  1755. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  1756. if (!this.match(types.question)) return expr;
  1757. if (refNeedsArrowPos) {
  1758. const result = this.tryParse(() => super.parseConditional(expr, noIn, startPos, startLoc));
  1759. if (!result.node) {
  1760. refNeedsArrowPos.start = result.error.pos || this.state.start;
  1761. return expr;
  1762. }
  1763. if (result.error) this.state = result.failState;
  1764. return result.node;
  1765. }
  1766. this.expect(types.question);
  1767. const state = this.state.clone();
  1768. const originalNoArrowAt = this.state.noArrowAt;
  1769. const node = this.startNodeAt(startPos, startLoc);
  1770. let {
  1771. consequent,
  1772. failed
  1773. } = this.tryParseConditionalConsequent();
  1774. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1775. if (failed || invalid.length > 0) {
  1776. const noArrowAt = [...originalNoArrowAt];
  1777. if (invalid.length > 0) {
  1778. this.state = state;
  1779. this.state.noArrowAt = noArrowAt;
  1780. for (let i = 0; i < invalid.length; i++) {
  1781. noArrowAt.push(invalid[i].start);
  1782. }
  1783. ({
  1784. consequent,
  1785. failed
  1786. } = this.tryParseConditionalConsequent());
  1787. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1788. }
  1789. if (failed && valid.length > 1) {
  1790. this.raise(state.start, "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.");
  1791. }
  1792. if (failed && valid.length === 1) {
  1793. this.state = state;
  1794. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  1795. ({
  1796. consequent,
  1797. failed
  1798. } = this.tryParseConditionalConsequent());
  1799. }
  1800. }
  1801. this.getArrowLikeExpressions(consequent, true);
  1802. this.state.noArrowAt = originalNoArrowAt;
  1803. this.expect(types.colon);
  1804. node.test = expr;
  1805. node.consequent = consequent;
  1806. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(noIn, undefined, undefined, undefined));
  1807. return this.finishNode(node, "ConditionalExpression");
  1808. }
  1809. tryParseConditionalConsequent() {
  1810. this.state.noArrowParamsConversionAt.push(this.state.start);
  1811. const consequent = this.parseMaybeAssign();
  1812. const failed = !this.match(types.colon);
  1813. this.state.noArrowParamsConversionAt.pop();
  1814. return {
  1815. consequent,
  1816. failed
  1817. };
  1818. }
  1819. getArrowLikeExpressions(node, disallowInvalid) {
  1820. const stack = [node];
  1821. const arrows = [];
  1822. while (stack.length !== 0) {
  1823. const node = stack.pop();
  1824. if (node.type === "ArrowFunctionExpression") {
  1825. if (node.typeParameters || !node.returnType) {
  1826. this.finishArrowValidation(node);
  1827. } else {
  1828. arrows.push(node);
  1829. }
  1830. stack.push(node.body);
  1831. } else if (node.type === "ConditionalExpression") {
  1832. stack.push(node.consequent);
  1833. stack.push(node.alternate);
  1834. }
  1835. }
  1836. if (disallowInvalid) {
  1837. arrows.forEach(node => this.finishArrowValidation(node));
  1838. return [arrows, []];
  1839. }
  1840. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  1841. }
  1842. finishArrowValidation(node) {
  1843. var _node$extra;
  1844. this.toAssignableList(node.params, true, "arrow function parameters", (_node$extra = node.extra) === null || _node$extra === void 0 ? void 0 : _node$extra.trailingComma);
  1845. this.scope.enter(functionFlags(false, false) | SCOPE_ARROW);
  1846. super.checkParams(node, false, true);
  1847. this.scope.exit();
  1848. }
  1849. forwardNoArrowParamsConversionAt(node, parse) {
  1850. let result;
  1851. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  1852. this.state.noArrowParamsConversionAt.push(this.state.start);
  1853. result = parse();
  1854. this.state.noArrowParamsConversionAt.pop();
  1855. } else {
  1856. result = parse();
  1857. }
  1858. return result;
  1859. }
  1860. parseParenItem(node, startPos, startLoc) {
  1861. node = super.parseParenItem(node, startPos, startLoc);
  1862. if (this.eat(types.question)) {
  1863. node.optional = true;
  1864. this.resetEndLocation(node);
  1865. }
  1866. if (this.match(types.colon)) {
  1867. const typeCastNode = this.startNodeAt(startPos, startLoc);
  1868. typeCastNode.expression = node;
  1869. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  1870. return this.finishNode(typeCastNode, "TypeCastExpression");
  1871. }
  1872. return node;
  1873. }
  1874. assertModuleNodeAllowed(node) {
  1875. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  1876. return;
  1877. }
  1878. super.assertModuleNodeAllowed(node);
  1879. }
  1880. parseExport(node) {
  1881. const decl = super.parseExport(node);
  1882. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  1883. decl.exportKind = decl.exportKind || "value";
  1884. }
  1885. return decl;
  1886. }
  1887. parseExportDeclaration(node) {
  1888. if (this.isContextual("type")) {
  1889. node.exportKind = "type";
  1890. const declarationNode = this.startNode();
  1891. this.next();
  1892. if (this.match(types.braceL)) {
  1893. node.specifiers = this.parseExportSpecifiers();
  1894. this.parseExportFrom(node);
  1895. return null;
  1896. } else {
  1897. return this.flowParseTypeAlias(declarationNode);
  1898. }
  1899. } else if (this.isContextual("opaque")) {
  1900. node.exportKind = "type";
  1901. const declarationNode = this.startNode();
  1902. this.next();
  1903. return this.flowParseOpaqueType(declarationNode, false);
  1904. } else if (this.isContextual("interface")) {
  1905. node.exportKind = "type";
  1906. const declarationNode = this.startNode();
  1907. this.next();
  1908. return this.flowParseInterface(declarationNode);
  1909. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  1910. node.exportKind = "value";
  1911. const declarationNode = this.startNode();
  1912. this.next();
  1913. return this.flowParseEnumDeclaration(declarationNode);
  1914. } else {
  1915. return super.parseExportDeclaration(node);
  1916. }
  1917. }
  1918. eatExportStar(node) {
  1919. if (super.eatExportStar(...arguments)) return true;
  1920. if (this.isContextual("type") && this.lookahead().type === types.star) {
  1921. node.exportKind = "type";
  1922. this.next();
  1923. this.next();
  1924. return true;
  1925. }
  1926. return false;
  1927. }
  1928. maybeParseExportNamespaceSpecifier(node) {
  1929. const pos = this.state.start;
  1930. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  1931. if (hasNamespace && node.exportKind === "type") {
  1932. this.unexpected(pos);
  1933. }
  1934. return hasNamespace;
  1935. }
  1936. parseClassId(node, isStatement, optionalId) {
  1937. super.parseClassId(node, isStatement, optionalId);
  1938. if (this.isRelational("<")) {
  1939. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1940. }
  1941. }
  1942. getTokenFromCode(code) {
  1943. const next = this.input.charCodeAt(this.state.pos + 1);
  1944. if (code === 123 && next === 124) {
  1945. return this.finishOp(types.braceBarL, 2);
  1946. } else if (this.state.inType && (code === 62 || code === 60)) {
  1947. return this.finishOp(types.relational, 1);
  1948. } else if (isIteratorStart(code, next)) {
  1949. this.state.isIterator = true;
  1950. return super.readWord();
  1951. } else {
  1952. return super.getTokenFromCode(code);
  1953. }
  1954. }
  1955. isAssignable(node, isBinding) {
  1956. switch (node.type) {
  1957. case "Identifier":
  1958. case "ObjectPattern":
  1959. case "ArrayPattern":
  1960. case "AssignmentPattern":
  1961. return true;
  1962. case "ObjectExpression":
  1963. {
  1964. const last = node.properties.length - 1;
  1965. return node.properties.every((prop, i) => {
  1966. return prop.type !== "ObjectMethod" && (i === last || prop.type === "SpreadElement") && this.isAssignable(prop);
  1967. });
  1968. }
  1969. case "ObjectProperty":
  1970. return this.isAssignable(node.value);
  1971. case "SpreadElement":
  1972. return this.isAssignable(node.argument);
  1973. case "ArrayExpression":
  1974. return node.elements.every(element => this.isAssignable(element));
  1975. case "AssignmentExpression":
  1976. return node.operator === "=";
  1977. case "ParenthesizedExpression":
  1978. case "TypeCastExpression":
  1979. return this.isAssignable(node.expression);
  1980. case "MemberExpression":
  1981. case "OptionalMemberExpression":
  1982. return !isBinding;
  1983. default:
  1984. return false;
  1985. }
  1986. }
  1987. toAssignable(node, isBinding, contextDescription) {
  1988. if (node.type === "TypeCastExpression") {
  1989. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  1990. } else {
  1991. return super.toAssignable(node, isBinding, contextDescription);
  1992. }
  1993. }
  1994. toAssignableList(exprList, isBinding, contextDescription, trailingCommaPos) {
  1995. for (let i = 0; i < exprList.length; i++) {
  1996. const expr = exprList[i];
  1997. if (expr && expr.type === "TypeCastExpression") {
  1998. exprList[i] = this.typeCastToParameter(expr);
  1999. }
  2000. }
  2001. return super.toAssignableList(exprList, isBinding, contextDescription, trailingCommaPos);
  2002. }
  2003. toReferencedList(exprList, isParenthesizedExpr) {
  2004. for (let i = 0; i < exprList.length; i++) {
  2005. const expr = exprList[i];
  2006. if (expr && expr.type === "TypeCastExpression" && (!expr.extra || !expr.extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  2007. this.raise(expr.typeAnnotation.start, "The type cast expression is expected to be wrapped with parenthesis");
  2008. }
  2009. }
  2010. return exprList;
  2011. }
  2012. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  2013. if (expr.type !== "TypeCastExpression") {
  2014. return super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  2015. }
  2016. }
  2017. parseClassProperty(node) {
  2018. if (this.match(types.colon)) {
  2019. node.typeAnnotation = this.flowParseTypeAnnotation();
  2020. }
  2021. return super.parseClassProperty(node);
  2022. }
  2023. parseClassPrivateProperty(node) {
  2024. if (this.match(types.colon)) {
  2025. node.typeAnnotation = this.flowParseTypeAnnotation();
  2026. }
  2027. return super.parseClassPrivateProperty(node);
  2028. }
  2029. isClassMethod() {
  2030. return this.isRelational("<") || super.isClassMethod();
  2031. }
  2032. isClassProperty() {
  2033. return this.match(types.colon) || super.isClassProperty();
  2034. }
  2035. isNonstaticConstructor(method) {
  2036. return !this.match(types.colon) && super.isNonstaticConstructor(method);
  2037. }
  2038. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  2039. if (method.variance) {
  2040. this.unexpected(method.variance.start);
  2041. }
  2042. delete method.variance;
  2043. if (this.isRelational("<")) {
  2044. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2045. }
  2046. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  2047. }
  2048. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  2049. if (method.variance) {
  2050. this.unexpected(method.variance.start);
  2051. }
  2052. delete method.variance;
  2053. if (this.isRelational("<")) {
  2054. method.typeParameters = this.flowParseTypeParameterDeclaration();
  2055. }
  2056. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  2057. }
  2058. parseClassSuper(node) {
  2059. super.parseClassSuper(node);
  2060. if (node.superClass && this.isRelational("<")) {
  2061. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  2062. }
  2063. if (this.isContextual("implements")) {
  2064. this.next();
  2065. const implemented = node.implements = [];
  2066. do {
  2067. const node = this.startNode();
  2068. node.id = this.flowParseRestrictedIdentifier(true);
  2069. if (this.isRelational("<")) {
  2070. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2071. } else {
  2072. node.typeParameters = null;
  2073. }
  2074. implemented.push(this.finishNode(node, "ClassImplements"));
  2075. } while (this.eat(types.comma));
  2076. }
  2077. }
  2078. parsePropertyName(node) {
  2079. const variance = this.flowParseVariance();
  2080. const key = super.parsePropertyName(node);
  2081. node.variance = variance;
  2082. return key;
  2083. }
  2084. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  2085. if (prop.variance) {
  2086. this.unexpected(prop.variance.start);
  2087. }
  2088. delete prop.variance;
  2089. let typeParameters;
  2090. if (this.isRelational("<")) {
  2091. typeParameters = this.flowParseTypeParameterDeclaration();
  2092. if (!this.match(types.parenL)) this.unexpected();
  2093. }
  2094. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  2095. if (typeParameters) {
  2096. (prop.value || prop).typeParameters = typeParameters;
  2097. }
  2098. }
  2099. parseAssignableListItemTypes(param) {
  2100. if (this.eat(types.question)) {
  2101. if (param.type !== "Identifier") {
  2102. this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  2103. }
  2104. param.optional = true;
  2105. }
  2106. if (this.match(types.colon)) {
  2107. param.typeAnnotation = this.flowParseTypeAnnotation();
  2108. }
  2109. this.resetEndLocation(param);
  2110. return param;
  2111. }
  2112. parseMaybeDefault(startPos, startLoc, left) {
  2113. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2114. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2115. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  2116. }
  2117. return node;
  2118. }
  2119. shouldParseDefaultImport(node) {
  2120. if (!hasTypeImportKind(node)) {
  2121. return super.shouldParseDefaultImport(node);
  2122. }
  2123. return isMaybeDefaultImport(this.state);
  2124. }
  2125. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  2126. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true) : this.parseIdentifier();
  2127. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  2128. node.specifiers.push(this.finishNode(specifier, type));
  2129. }
  2130. maybeParseDefaultImportSpecifier(node) {
  2131. node.importKind = "value";
  2132. let kind = null;
  2133. if (this.match(types._typeof)) {
  2134. kind = "typeof";
  2135. } else if (this.isContextual("type")) {
  2136. kind = "type";
  2137. }
  2138. if (kind) {
  2139. const lh = this.lookahead();
  2140. if (kind === "type" && lh.type === types.star) {
  2141. this.unexpected(lh.start);
  2142. }
  2143. if (isMaybeDefaultImport(lh) || lh.type === types.braceL || lh.type === types.star) {
  2144. this.next();
  2145. node.importKind = kind;
  2146. }
  2147. }
  2148. return super.maybeParseDefaultImportSpecifier(node);
  2149. }
  2150. parseImportSpecifier(node) {
  2151. const specifier = this.startNode();
  2152. const firstIdentLoc = this.state.start;
  2153. const firstIdent = this.parseIdentifier(true);
  2154. let specifierTypeKind = null;
  2155. if (firstIdent.name === "type") {
  2156. specifierTypeKind = "type";
  2157. } else if (firstIdent.name === "typeof") {
  2158. specifierTypeKind = "typeof";
  2159. }
  2160. let isBinding = false;
  2161. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  2162. const as_ident = this.parseIdentifier(true);
  2163. if (specifierTypeKind !== null && !this.match(types.name) && !this.state.type.keyword) {
  2164. specifier.imported = as_ident;
  2165. specifier.importKind = specifierTypeKind;
  2166. specifier.local = as_ident.__clone();
  2167. } else {
  2168. specifier.imported = firstIdent;
  2169. specifier.importKind = null;
  2170. specifier.local = this.parseIdentifier();
  2171. }
  2172. } else if (specifierTypeKind !== null && (this.match(types.name) || this.state.type.keyword)) {
  2173. specifier.imported = this.parseIdentifier(true);
  2174. specifier.importKind = specifierTypeKind;
  2175. if (this.eatContextual("as")) {
  2176. specifier.local = this.parseIdentifier();
  2177. } else {
  2178. isBinding = true;
  2179. specifier.local = specifier.imported.__clone();
  2180. }
  2181. } else {
  2182. isBinding = true;
  2183. specifier.imported = firstIdent;
  2184. specifier.importKind = null;
  2185. specifier.local = specifier.imported.__clone();
  2186. }
  2187. const nodeIsTypeImport = hasTypeImportKind(node);
  2188. const specifierIsTypeImport = hasTypeImportKind(specifier);
  2189. if (nodeIsTypeImport && specifierIsTypeImport) {
  2190. this.raise(firstIdentLoc, "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");
  2191. }
  2192. if (nodeIsTypeImport || specifierIsTypeImport) {
  2193. this.checkReservedType(specifier.local.name, specifier.local.start);
  2194. }
  2195. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  2196. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  2197. }
  2198. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  2199. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  2200. }
  2201. parseFunctionParams(node, allowModifiers) {
  2202. const kind = node.kind;
  2203. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  2204. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2205. }
  2206. super.parseFunctionParams(node, allowModifiers);
  2207. }
  2208. parseVarId(decl, kind) {
  2209. super.parseVarId(decl, kind);
  2210. if (this.match(types.colon)) {
  2211. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  2212. this.resetEndLocation(decl.id);
  2213. }
  2214. }
  2215. parseAsyncArrowFromCallExpression(node, call) {
  2216. if (this.match(types.colon)) {
  2217. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2218. this.state.noAnonFunctionType = true;
  2219. node.returnType = this.flowParseTypeAnnotation();
  2220. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2221. }
  2222. return super.parseAsyncArrowFromCallExpression(node, call);
  2223. }
  2224. shouldParseAsyncArrow() {
  2225. return this.match(types.colon) || super.shouldParseAsyncArrow();
  2226. }
  2227. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  2228. let state = null;
  2229. let jsx;
  2230. if (this.hasPlugin("jsx") && (this.match(types.jsxTagStart) || this.isRelational("<"))) {
  2231. state = this.state.clone();
  2232. jsx = this.tryParse(() => super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos), state);
  2233. if (!jsx.error) return jsx.node;
  2234. const {
  2235. context
  2236. } = this.state;
  2237. if (context[context.length - 1] === types$1.j_oTag) {
  2238. context.length -= 2;
  2239. } else if (context[context.length - 1] === types$1.j_expr) {
  2240. context.length -= 1;
  2241. }
  2242. }
  2243. if (jsx && jsx.error || this.isRelational("<")) {
  2244. state = state || this.state.clone();
  2245. let typeParameters;
  2246. const arrow = this.tryParse(() => {
  2247. typeParameters = this.flowParseTypeParameterDeclaration();
  2248. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos));
  2249. arrowExpression.typeParameters = typeParameters;
  2250. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  2251. return arrowExpression;
  2252. }, state);
  2253. const arrowExpression = arrow.node && arrow.node.type === "ArrowFunctionExpression" ? arrow.node : null;
  2254. if (!arrow.error && arrowExpression) return arrowExpression;
  2255. if (jsx && jsx.node) {
  2256. this.state = jsx.failState;
  2257. return jsx.node;
  2258. }
  2259. if (arrowExpression) {
  2260. this.state = arrow.failState;
  2261. return arrowExpression;
  2262. }
  2263. if (jsx && jsx.thrown) throw jsx.error;
  2264. if (arrow.thrown) throw arrow.error;
  2265. throw this.raise(typeParameters.start, "Expected an arrow function after this type parameter declaration");
  2266. }
  2267. return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
  2268. }
  2269. parseArrow(node) {
  2270. if (this.match(types.colon)) {
  2271. const result = this.tryParse(() => {
  2272. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2273. this.state.noAnonFunctionType = true;
  2274. const typeNode = this.startNode();
  2275. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2276. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2277. if (this.canInsertSemicolon()) this.unexpected();
  2278. if (!this.match(types.arrow)) this.unexpected();
  2279. return typeNode;
  2280. });
  2281. if (result.thrown) return null;
  2282. if (result.error) this.state = result.failState;
  2283. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  2284. }
  2285. return super.parseArrow(node);
  2286. }
  2287. shouldParseArrow() {
  2288. return this.match(types.colon) || super.shouldParseArrow();
  2289. }
  2290. setArrowFunctionParameters(node, params) {
  2291. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2292. node.params = params;
  2293. } else {
  2294. super.setArrowFunctionParameters(node, params);
  2295. }
  2296. }
  2297. checkParams(node, allowDuplicates, isArrowFunction) {
  2298. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2299. return;
  2300. }
  2301. return super.checkParams(...arguments);
  2302. }
  2303. parseParenAndDistinguishExpression(canBeArrow) {
  2304. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  2305. }
  2306. parseSubscripts(base, startPos, startLoc, noCalls) {
  2307. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  2308. this.next();
  2309. const node = this.startNodeAt(startPos, startLoc);
  2310. node.callee = base;
  2311. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2312. base = this.finishNode(node, "CallExpression");
  2313. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  2314. const state = this.state.clone();
  2315. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  2316. if (!arrow.error && !arrow.aborted) return arrow.node;
  2317. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  2318. if (result.node && !result.error) return result.node;
  2319. if (arrow.node) {
  2320. this.state = arrow.failState;
  2321. return arrow.node;
  2322. }
  2323. if (result.node) {
  2324. this.state = result.failState;
  2325. return result.node;
  2326. }
  2327. throw arrow.error || result.error;
  2328. }
  2329. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  2330. }
  2331. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  2332. if (this.match(types.questionDot) && this.isLookaheadRelational("<")) {
  2333. this.expectPlugin("optionalChaining");
  2334. subscriptState.optionalChainMember = true;
  2335. if (noCalls) {
  2336. subscriptState.stop = true;
  2337. return base;
  2338. }
  2339. this.next();
  2340. const node = this.startNodeAt(startPos, startLoc);
  2341. node.callee = base;
  2342. node.typeArguments = this.flowParseTypeParameterInstantiation();
  2343. this.expect(types.parenL);
  2344. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2345. node.optional = true;
  2346. return this.finishCallExpression(node, true);
  2347. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  2348. const node = this.startNodeAt(startPos, startLoc);
  2349. node.callee = base;
  2350. const result = this.tryParse(() => {
  2351. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  2352. this.expect(types.parenL);
  2353. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2354. if (subscriptState.optionalChainMember) node.optional = false;
  2355. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  2356. });
  2357. if (result.node) {
  2358. if (result.error) this.state = result.failState;
  2359. return result.node;
  2360. }
  2361. }
  2362. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  2363. }
  2364. parseNewArguments(node) {
  2365. let targs = null;
  2366. if (this.shouldParseTypes() && this.isRelational("<")) {
  2367. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  2368. }
  2369. node.typeArguments = targs;
  2370. super.parseNewArguments(node);
  2371. }
  2372. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  2373. const node = this.startNodeAt(startPos, startLoc);
  2374. this.parseFunctionParams(node);
  2375. if (!this.parseArrow(node)) return;
  2376. return this.parseArrowExpression(node, undefined, true);
  2377. }
  2378. readToken_mult_modulo(code) {
  2379. const next = this.input.charCodeAt(this.state.pos + 1);
  2380. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  2381. this.state.hasFlowComment = false;
  2382. this.state.pos += 2;
  2383. this.nextToken();
  2384. return;
  2385. }
  2386. super.readToken_mult_modulo(code);
  2387. }
  2388. readToken_pipe_amp(code) {
  2389. const next = this.input.charCodeAt(this.state.pos + 1);
  2390. if (code === 124 && next === 125) {
  2391. this.finishOp(types.braceBarR, 2);
  2392. return;
  2393. }
  2394. super.readToken_pipe_amp(code);
  2395. }
  2396. parseTopLevel(file, program) {
  2397. const fileNode = super.parseTopLevel(file, program);
  2398. if (this.state.hasFlowComment) {
  2399. this.raise(this.state.pos, "Unterminated flow-comment");
  2400. }
  2401. return fileNode;
  2402. }
  2403. skipBlockComment() {
  2404. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  2405. if (this.state.hasFlowComment) {
  2406. this.unexpected(null, "Cannot have a flow comment inside another flow comment");
  2407. }
  2408. this.hasFlowCommentCompletion();
  2409. this.state.pos += this.skipFlowComment();
  2410. this.state.hasFlowComment = true;
  2411. return;
  2412. }
  2413. if (this.state.hasFlowComment) {
  2414. const end = this.input.indexOf("*-/", this.state.pos += 2);
  2415. if (end === -1) {
  2416. throw this.raise(this.state.pos - 2, "Unterminated comment");
  2417. }
  2418. this.state.pos = end + 3;
  2419. return;
  2420. }
  2421. super.skipBlockComment();
  2422. }
  2423. skipFlowComment() {
  2424. const {
  2425. pos
  2426. } = this.state;
  2427. let shiftToFirstNonWhiteSpace = 2;
  2428. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  2429. shiftToFirstNonWhiteSpace++;
  2430. }
  2431. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  2432. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  2433. if (ch2 === 58 && ch3 === 58) {
  2434. return shiftToFirstNonWhiteSpace + 2;
  2435. }
  2436. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  2437. return shiftToFirstNonWhiteSpace + 12;
  2438. }
  2439. if (ch2 === 58 && ch3 !== 58) {
  2440. return shiftToFirstNonWhiteSpace;
  2441. }
  2442. return false;
  2443. }
  2444. hasFlowCommentCompletion() {
  2445. const end = this.input.indexOf("*/", this.state.pos);
  2446. if (end === -1) {
  2447. throw this.raise(this.state.pos, "Unterminated comment");
  2448. }
  2449. }
  2450. flowEnumErrorBooleanMemberNotInitialized(pos, {
  2451. enumName,
  2452. memberName
  2453. }) {
  2454. this.raise(pos, `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` ` + `or \`${memberName} = false,\` in enum \`${enumName}\`.`);
  2455. }
  2456. flowEnumErrorInvalidMemberName(pos, {
  2457. enumName,
  2458. memberName
  2459. }) {
  2460. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  2461. this.raise(pos, `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using ` + `\`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`);
  2462. }
  2463. flowEnumErrorDuplicateMemberName(pos, {
  2464. enumName,
  2465. memberName
  2466. }) {
  2467. this.raise(pos, `Enum member names need to be unique, but the name \`${memberName}\` has already been used ` + `before in enum \`${enumName}\`.`);
  2468. }
  2469. flowEnumErrorInconsistentMemberValues(pos, {
  2470. enumName
  2471. }) {
  2472. this.raise(pos, `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or ` + `consistently use literals (either booleans, numbers, or strings) for all member initializers.`);
  2473. }
  2474. flowEnumErrorInvalidExplicitType(pos, {
  2475. enumName,
  2476. suppliedType
  2477. }) {
  2478. const suggestion = `Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in ` + `enum \`${enumName}\`.`;
  2479. const message = suppliedType === null ? `Supplied enum type is not valid. ${suggestion}` : `Enum type \`${suppliedType}\` is not valid. ${suggestion}`;
  2480. return this.raise(pos, message);
  2481. }
  2482. flowEnumErrorInvalidMemberInitializer(pos, {
  2483. enumName,
  2484. explicitType,
  2485. memberName
  2486. }) {
  2487. let message = null;
  2488. switch (explicitType) {
  2489. case "boolean":
  2490. case "number":
  2491. case "string":
  2492. message = `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of ` + `\`${memberName}\` needs to be a ${explicitType} literal.`;
  2493. break;
  2494. case "symbol":
  2495. message = `Symbol enum members cannot be initialized. Use \`${memberName},\` in ` + `enum \`${enumName}\`.`;
  2496. break;
  2497. default:
  2498. message = `The enum member initializer for \`${memberName}\` needs to be a literal (either ` + `a boolean, number, or string) in enum \`${enumName}\`.`;
  2499. }
  2500. return this.raise(pos, message);
  2501. }
  2502. flowEnumErrorNumberMemberNotInitialized(pos, {
  2503. enumName,
  2504. memberName
  2505. }) {
  2506. this.raise(pos, `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`);
  2507. }
  2508. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  2509. enumName
  2510. }) {
  2511. this.raise(pos, `String enum members need to consistently either all use initializers, or use no initializers, ` + `in enum \`${enumName}\`.`);
  2512. }
  2513. flowEnumMemberInit() {
  2514. const startPos = this.state.start;
  2515. const endOfInit = () => this.match(types.comma) || this.match(types.braceR);
  2516. switch (this.state.type) {
  2517. case types.num:
  2518. {
  2519. const literal = this.parseLiteral(this.state.value, "NumericLiteral");
  2520. if (endOfInit()) {
  2521. return {
  2522. type: "number",
  2523. pos: literal.start,
  2524. value: literal
  2525. };
  2526. }
  2527. return {
  2528. type: "invalid",
  2529. pos: startPos
  2530. };
  2531. }
  2532. case types.string:
  2533. {
  2534. const literal = this.parseLiteral(this.state.value, "StringLiteral");
  2535. if (endOfInit()) {
  2536. return {
  2537. type: "string",
  2538. pos: literal.start,
  2539. value: literal
  2540. };
  2541. }
  2542. return {
  2543. type: "invalid",
  2544. pos: startPos
  2545. };
  2546. }
  2547. case types._true:
  2548. case types._false:
  2549. {
  2550. const literal = this.parseBooleanLiteral();
  2551. if (endOfInit()) {
  2552. return {
  2553. type: "boolean",
  2554. pos: literal.start,
  2555. value: literal
  2556. };
  2557. }
  2558. return {
  2559. type: "invalid",
  2560. pos: startPos
  2561. };
  2562. }
  2563. default:
  2564. return {
  2565. type: "invalid",
  2566. pos: startPos
  2567. };
  2568. }
  2569. }
  2570. flowEnumMemberRaw() {
  2571. const pos = this.state.start;
  2572. const id = this.parseIdentifier(true);
  2573. const init = this.eat(types.eq) ? this.flowEnumMemberInit() : {
  2574. type: "none",
  2575. pos
  2576. };
  2577. return {
  2578. id,
  2579. init
  2580. };
  2581. }
  2582. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  2583. const {
  2584. explicitType
  2585. } = context;
  2586. if (explicitType === null) {
  2587. return;
  2588. }
  2589. if (explicitType !== expectedType) {
  2590. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  2591. }
  2592. }
  2593. flowEnumMembers({
  2594. enumName,
  2595. explicitType
  2596. }) {
  2597. const seenNames = new Set();
  2598. const members = {
  2599. booleanMembers: [],
  2600. numberMembers: [],
  2601. stringMembers: [],
  2602. defaultedMembers: []
  2603. };
  2604. while (!this.match(types.braceR)) {
  2605. const memberNode = this.startNode();
  2606. const {
  2607. id,
  2608. init
  2609. } = this.flowEnumMemberRaw();
  2610. const memberName = id.name;
  2611. if (memberName === "") {
  2612. continue;
  2613. }
  2614. if (/^[a-z]/.test(memberName)) {
  2615. this.flowEnumErrorInvalidMemberName(id.start, {
  2616. enumName,
  2617. memberName
  2618. });
  2619. }
  2620. if (seenNames.has(memberName)) {
  2621. this.flowEnumErrorDuplicateMemberName(id.start, {
  2622. enumName,
  2623. memberName
  2624. });
  2625. }
  2626. seenNames.add(memberName);
  2627. const context = {
  2628. enumName,
  2629. explicitType,
  2630. memberName
  2631. };
  2632. memberNode.id = id;
  2633. switch (init.type) {
  2634. case "boolean":
  2635. {
  2636. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  2637. memberNode.init = init.value;
  2638. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  2639. break;
  2640. }
  2641. case "number":
  2642. {
  2643. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  2644. memberNode.init = init.value;
  2645. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  2646. break;
  2647. }
  2648. case "string":
  2649. {
  2650. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  2651. memberNode.init = init.value;
  2652. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  2653. break;
  2654. }
  2655. case "invalid":
  2656. {
  2657. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  2658. }
  2659. case "none":
  2660. {
  2661. switch (explicitType) {
  2662. case "boolean":
  2663. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  2664. break;
  2665. case "number":
  2666. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  2667. break;
  2668. default:
  2669. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  2670. }
  2671. }
  2672. }
  2673. if (!this.match(types.braceR)) {
  2674. this.expect(types.comma);
  2675. }
  2676. }
  2677. return members;
  2678. }
  2679. flowEnumStringBody(bodyNode, initializedMembers, defaultedMembers, {
  2680. enumName
  2681. }) {
  2682. if (initializedMembers.length === 0) {
  2683. bodyNode.members = defaultedMembers;
  2684. } else if (defaultedMembers.length === 0) {
  2685. bodyNode.members = initializedMembers;
  2686. } else if (defaultedMembers.length > initializedMembers.length) {
  2687. bodyNode.members = defaultedMembers;
  2688. for (let _i = 0; _i < initializedMembers.length; _i++) {
  2689. const member = initializedMembers[_i];
  2690. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  2691. enumName
  2692. });
  2693. }
  2694. } else {
  2695. bodyNode.members = initializedMembers;
  2696. for (let _i2 = 0; _i2 < defaultedMembers.length; _i2++) {
  2697. const member = defaultedMembers[_i2];
  2698. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  2699. enumName
  2700. });
  2701. }
  2702. }
  2703. return this.finishNode(bodyNode, "EnumStringBody");
  2704. }
  2705. flowEnumParseExplicitType({
  2706. enumName
  2707. }) {
  2708. if (this.eatContextual("of")) {
  2709. if (!this.match(types.name)) {
  2710. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  2711. enumName,
  2712. suppliedType: null
  2713. });
  2714. }
  2715. const {
  2716. value
  2717. } = this.state;
  2718. this.next();
  2719. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  2720. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  2721. enumName,
  2722. suppliedType: value
  2723. });
  2724. }
  2725. return value;
  2726. }
  2727. return null;
  2728. }
  2729. flowParseEnumDeclaration(node) {
  2730. const id = this.parseIdentifier();
  2731. node.id = id;
  2732. const enumName = id.name;
  2733. const explicitType = this.flowEnumParseExplicitType({
  2734. enumName
  2735. });
  2736. this.expect(types.braceL);
  2737. const bodyNode = this.startNode();
  2738. const members = this.flowEnumMembers({
  2739. enumName,
  2740. explicitType
  2741. });
  2742. switch (explicitType) {
  2743. case "boolean":
  2744. bodyNode.explicitType = true;
  2745. bodyNode.members = members.booleanMembers;
  2746. node.body = this.finishNode(bodyNode, "EnumBooleanBody");
  2747. break;
  2748. case "number":
  2749. bodyNode.explicitType = true;
  2750. bodyNode.members = members.numberMembers;
  2751. node.body = this.finishNode(bodyNode, "EnumNumberBody");
  2752. break;
  2753. case "string":
  2754. bodyNode.explicitType = true;
  2755. node.body = this.flowEnumStringBody(bodyNode, members.stringMembers, members.defaultedMembers, {
  2756. enumName
  2757. });
  2758. break;
  2759. case "symbol":
  2760. bodyNode.members = members.defaultedMembers;
  2761. node.body = this.finishNode(bodyNode, "EnumSymbolBody");
  2762. break;
  2763. default:
  2764. {
  2765. const empty = () => {
  2766. bodyNode.members = [];
  2767. return this.finishNode(bodyNode, "EnumStringBody");
  2768. };
  2769. bodyNode.explicitType = false;
  2770. const boolsLen = members.booleanMembers.length;
  2771. const numsLen = members.numberMembers.length;
  2772. const strsLen = members.stringMembers.length;
  2773. const defaultedLen = members.defaultedMembers.length;
  2774. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  2775. node.body = empty();
  2776. } else if (!boolsLen && !numsLen) {
  2777. node.body = this.flowEnumStringBody(bodyNode, members.stringMembers, members.defaultedMembers, {
  2778. enumName
  2779. });
  2780. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  2781. bodyNode.members = members.booleanMembers;
  2782. node.body = this.finishNode(bodyNode, "EnumBooleanBody");
  2783. for (let _i3 = 0, _members$defaultedMem = members.defaultedMembers; _i3 < _members$defaultedMem.length; _i3++) {
  2784. const member = _members$defaultedMem[_i3];
  2785. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  2786. enumName,
  2787. memberName: member.id.name
  2788. });
  2789. }
  2790. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  2791. bodyNode.members = members.numberMembers;
  2792. node.body = this.finishNode(bodyNode, "EnumNumberBody");
  2793. for (let _i4 = 0, _members$defaultedMem2 = members.defaultedMembers; _i4 < _members$defaultedMem2.length; _i4++) {
  2794. const member = _members$defaultedMem2[_i4];
  2795. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  2796. enumName,
  2797. memberName: member.id.name
  2798. });
  2799. }
  2800. } else {
  2801. node.body = empty();
  2802. this.flowEnumErrorInconsistentMemberValues(id.start, {
  2803. enumName
  2804. });
  2805. }
  2806. }
  2807. }
  2808. this.expect(types.braceR);
  2809. return this.finishNode(node, "EnumDeclaration");
  2810. }
  2811. });
  2812. const entities = {
  2813. quot: "\u0022",
  2814. amp: "&",
  2815. apos: "\u0027",
  2816. lt: "<",
  2817. gt: ">",
  2818. nbsp: "\u00A0",
  2819. iexcl: "\u00A1",
  2820. cent: "\u00A2",
  2821. pound: "\u00A3",
  2822. curren: "\u00A4",
  2823. yen: "\u00A5",
  2824. brvbar: "\u00A6",
  2825. sect: "\u00A7",
  2826. uml: "\u00A8",
  2827. copy: "\u00A9",
  2828. ordf: "\u00AA",
  2829. laquo: "\u00AB",
  2830. not: "\u00AC",
  2831. shy: "\u00AD",
  2832. reg: "\u00AE",
  2833. macr: "\u00AF",
  2834. deg: "\u00B0",
  2835. plusmn: "\u00B1",
  2836. sup2: "\u00B2",
  2837. sup3: "\u00B3",
  2838. acute: "\u00B4",
  2839. micro: "\u00B5",
  2840. para: "\u00B6",
  2841. middot: "\u00B7",
  2842. cedil: "\u00B8",
  2843. sup1: "\u00B9",
  2844. ordm: "\u00BA",
  2845. raquo: "\u00BB",
  2846. frac14: "\u00BC",
  2847. frac12: "\u00BD",
  2848. frac34: "\u00BE",
  2849. iquest: "\u00BF",
  2850. Agrave: "\u00C0",
  2851. Aacute: "\u00C1",
  2852. Acirc: "\u00C2",
  2853. Atilde: "\u00C3",
  2854. Auml: "\u00C4",
  2855. Aring: "\u00C5",
  2856. AElig: "\u00C6",
  2857. Ccedil: "\u00C7",
  2858. Egrave: "\u00C8",
  2859. Eacute: "\u00C9",
  2860. Ecirc: "\u00CA",
  2861. Euml: "\u00CB",
  2862. Igrave: "\u00CC",
  2863. Iacute: "\u00CD",
  2864. Icirc: "\u00CE",
  2865. Iuml: "\u00CF",
  2866. ETH: "\u00D0",
  2867. Ntilde: "\u00D1",
  2868. Ograve: "\u00D2",
  2869. Oacute: "\u00D3",
  2870. Ocirc: "\u00D4",
  2871. Otilde: "\u00D5",
  2872. Ouml: "\u00D6",
  2873. times: "\u00D7",
  2874. Oslash: "\u00D8",
  2875. Ugrave: "\u00D9",
  2876. Uacute: "\u00DA",
  2877. Ucirc: "\u00DB",
  2878. Uuml: "\u00DC",
  2879. Yacute: "\u00DD",
  2880. THORN: "\u00DE",
  2881. szlig: "\u00DF",
  2882. agrave: "\u00E0",
  2883. aacute: "\u00E1",
  2884. acirc: "\u00E2",
  2885. atilde: "\u00E3",
  2886. auml: "\u00E4",
  2887. aring: "\u00E5",
  2888. aelig: "\u00E6",
  2889. ccedil: "\u00E7",
  2890. egrave: "\u00E8",
  2891. eacute: "\u00E9",
  2892. ecirc: "\u00EA",
  2893. euml: "\u00EB",
  2894. igrave: "\u00EC",
  2895. iacute: "\u00ED",
  2896. icirc: "\u00EE",
  2897. iuml: "\u00EF",
  2898. eth: "\u00F0",
  2899. ntilde: "\u00F1",
  2900. ograve: "\u00F2",
  2901. oacute: "\u00F3",
  2902. ocirc: "\u00F4",
  2903. otilde: "\u00F5",
  2904. ouml: "\u00F6",
  2905. divide: "\u00F7",
  2906. oslash: "\u00F8",
  2907. ugrave: "\u00F9",
  2908. uacute: "\u00FA",
  2909. ucirc: "\u00FB",
  2910. uuml: "\u00FC",
  2911. yacute: "\u00FD",
  2912. thorn: "\u00FE",
  2913. yuml: "\u00FF",
  2914. OElig: "\u0152",
  2915. oelig: "\u0153",
  2916. Scaron: "\u0160",
  2917. scaron: "\u0161",
  2918. Yuml: "\u0178",
  2919. fnof: "\u0192",
  2920. circ: "\u02C6",
  2921. tilde: "\u02DC",
  2922. Alpha: "\u0391",
  2923. Beta: "\u0392",
  2924. Gamma: "\u0393",
  2925. Delta: "\u0394",
  2926. Epsilon: "\u0395",
  2927. Zeta: "\u0396",
  2928. Eta: "\u0397",
  2929. Theta: "\u0398",
  2930. Iota: "\u0399",
  2931. Kappa: "\u039A",
  2932. Lambda: "\u039B",
  2933. Mu: "\u039C",
  2934. Nu: "\u039D",
  2935. Xi: "\u039E",
  2936. Omicron: "\u039F",
  2937. Pi: "\u03A0",
  2938. Rho: "\u03A1",
  2939. Sigma: "\u03A3",
  2940. Tau: "\u03A4",
  2941. Upsilon: "\u03A5",
  2942. Phi: "\u03A6",
  2943. Chi: "\u03A7",
  2944. Psi: "\u03A8",
  2945. Omega: "\u03A9",
  2946. alpha: "\u03B1",
  2947. beta: "\u03B2",
  2948. gamma: "\u03B3",
  2949. delta: "\u03B4",
  2950. epsilon: "\u03B5",
  2951. zeta: "\u03B6",
  2952. eta: "\u03B7",
  2953. theta: "\u03B8",
  2954. iota: "\u03B9",
  2955. kappa: "\u03BA",
  2956. lambda: "\u03BB",
  2957. mu: "\u03BC",
  2958. nu: "\u03BD",
  2959. xi: "\u03BE",
  2960. omicron: "\u03BF",
  2961. pi: "\u03C0",
  2962. rho: "\u03C1",
  2963. sigmaf: "\u03C2",
  2964. sigma: "\u03C3",
  2965. tau: "\u03C4",
  2966. upsilon: "\u03C5",
  2967. phi: "\u03C6",
  2968. chi: "\u03C7",
  2969. psi: "\u03C8",
  2970. omega: "\u03C9",
  2971. thetasym: "\u03D1",
  2972. upsih: "\u03D2",
  2973. piv: "\u03D6",
  2974. ensp: "\u2002",
  2975. emsp: "\u2003",
  2976. thinsp: "\u2009",
  2977. zwnj: "\u200C",
  2978. zwj: "\u200D",
  2979. lrm: "\u200E",
  2980. rlm: "\u200F",
  2981. ndash: "\u2013",
  2982. mdash: "\u2014",
  2983. lsquo: "\u2018",
  2984. rsquo: "\u2019",
  2985. sbquo: "\u201A",
  2986. ldquo: "\u201C",
  2987. rdquo: "\u201D",
  2988. bdquo: "\u201E",
  2989. dagger: "\u2020",
  2990. Dagger: "\u2021",
  2991. bull: "\u2022",
  2992. hellip: "\u2026",
  2993. permil: "\u2030",
  2994. prime: "\u2032",
  2995. Prime: "\u2033",
  2996. lsaquo: "\u2039",
  2997. rsaquo: "\u203A",
  2998. oline: "\u203E",
  2999. frasl: "\u2044",
  3000. euro: "\u20AC",
  3001. image: "\u2111",
  3002. weierp: "\u2118",
  3003. real: "\u211C",
  3004. trade: "\u2122",
  3005. alefsym: "\u2135",
  3006. larr: "\u2190",
  3007. uarr: "\u2191",
  3008. rarr: "\u2192",
  3009. darr: "\u2193",
  3010. harr: "\u2194",
  3011. crarr: "\u21B5",
  3012. lArr: "\u21D0",
  3013. uArr: "\u21D1",
  3014. rArr: "\u21D2",
  3015. dArr: "\u21D3",
  3016. hArr: "\u21D4",
  3017. forall: "\u2200",
  3018. part: "\u2202",
  3019. exist: "\u2203",
  3020. empty: "\u2205",
  3021. nabla: "\u2207",
  3022. isin: "\u2208",
  3023. notin: "\u2209",
  3024. ni: "\u220B",
  3025. prod: "\u220F",
  3026. sum: "\u2211",
  3027. minus: "\u2212",
  3028. lowast: "\u2217",
  3029. radic: "\u221A",
  3030. prop: "\u221D",
  3031. infin: "\u221E",
  3032. ang: "\u2220",
  3033. and: "\u2227",
  3034. or: "\u2228",
  3035. cap: "\u2229",
  3036. cup: "\u222A",
  3037. int: "\u222B",
  3038. there4: "\u2234",
  3039. sim: "\u223C",
  3040. cong: "\u2245",
  3041. asymp: "\u2248",
  3042. ne: "\u2260",
  3043. equiv: "\u2261",
  3044. le: "\u2264",
  3045. ge: "\u2265",
  3046. sub: "\u2282",
  3047. sup: "\u2283",
  3048. nsub: "\u2284",
  3049. sube: "\u2286",
  3050. supe: "\u2287",
  3051. oplus: "\u2295",
  3052. otimes: "\u2297",
  3053. perp: "\u22A5",
  3054. sdot: "\u22C5",
  3055. lceil: "\u2308",
  3056. rceil: "\u2309",
  3057. lfloor: "\u230A",
  3058. rfloor: "\u230B",
  3059. lang: "\u2329",
  3060. rang: "\u232A",
  3061. loz: "\u25CA",
  3062. spades: "\u2660",
  3063. clubs: "\u2663",
  3064. hearts: "\u2665",
  3065. diams: "\u2666"
  3066. };
  3067. const HEX_NUMBER = /^[\da-fA-F]+$/;
  3068. const DECIMAL_NUMBER = /^\d+$/;
  3069. types$1.j_oTag = new TokContext("<tag", false);
  3070. types$1.j_cTag = new TokContext("</tag", false);
  3071. types$1.j_expr = new TokContext("<tag>...</tag>", true, true);
  3072. types.jsxName = new TokenType("jsxName");
  3073. types.jsxText = new TokenType("jsxText", {
  3074. beforeExpr: true
  3075. });
  3076. types.jsxTagStart = new TokenType("jsxTagStart", {
  3077. startsExpr: true
  3078. });
  3079. types.jsxTagEnd = new TokenType("jsxTagEnd");
  3080. types.jsxTagStart.updateContext = function () {
  3081. this.state.context.push(types$1.j_expr);
  3082. this.state.context.push(types$1.j_oTag);
  3083. this.state.exprAllowed = false;
  3084. };
  3085. types.jsxTagEnd.updateContext = function (prevType) {
  3086. const out = this.state.context.pop();
  3087. if (out === types$1.j_oTag && prevType === types.slash || out === types$1.j_cTag) {
  3088. this.state.context.pop();
  3089. this.state.exprAllowed = this.curContext() === types$1.j_expr;
  3090. } else {
  3091. this.state.exprAllowed = true;
  3092. }
  3093. };
  3094. function isFragment(object) {
  3095. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  3096. }
  3097. function getQualifiedJSXName(object) {
  3098. if (object.type === "JSXIdentifier") {
  3099. return object.name;
  3100. }
  3101. if (object.type === "JSXNamespacedName") {
  3102. return object.namespace.name + ":" + object.name.name;
  3103. }
  3104. if (object.type === "JSXMemberExpression") {
  3105. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  3106. }
  3107. throw new Error("Node had unexpected type: " + object.type);
  3108. }
  3109. var jsx = (superClass => class extends superClass {
  3110. jsxReadToken() {
  3111. let out = "";
  3112. let chunkStart = this.state.pos;
  3113. for (;;) {
  3114. if (this.state.pos >= this.length) {
  3115. throw this.raise(this.state.start, "Unterminated JSX contents");
  3116. }
  3117. const ch = this.input.charCodeAt(this.state.pos);
  3118. switch (ch) {
  3119. case 60:
  3120. case 123:
  3121. if (this.state.pos === this.state.start) {
  3122. if (ch === 60 && this.state.exprAllowed) {
  3123. ++this.state.pos;
  3124. return this.finishToken(types.jsxTagStart);
  3125. }
  3126. return super.getTokenFromCode(ch);
  3127. }
  3128. out += this.input.slice(chunkStart, this.state.pos);
  3129. return this.finishToken(types.jsxText, out);
  3130. case 38:
  3131. out += this.input.slice(chunkStart, this.state.pos);
  3132. out += this.jsxReadEntity();
  3133. chunkStart = this.state.pos;
  3134. break;
  3135. default:
  3136. if (isNewLine(ch)) {
  3137. out += this.input.slice(chunkStart, this.state.pos);
  3138. out += this.jsxReadNewLine(true);
  3139. chunkStart = this.state.pos;
  3140. } else {
  3141. ++this.state.pos;
  3142. }
  3143. }
  3144. }
  3145. }
  3146. jsxReadNewLine(normalizeCRLF) {
  3147. const ch = this.input.charCodeAt(this.state.pos);
  3148. let out;
  3149. ++this.state.pos;
  3150. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  3151. ++this.state.pos;
  3152. out = normalizeCRLF ? "\n" : "\r\n";
  3153. } else {
  3154. out = String.fromCharCode(ch);
  3155. }
  3156. ++this.state.curLine;
  3157. this.state.lineStart = this.state.pos;
  3158. return out;
  3159. }
  3160. jsxReadString(quote) {
  3161. let out = "";
  3162. let chunkStart = ++this.state.pos;
  3163. for (;;) {
  3164. if (this.state.pos >= this.length) {
  3165. throw this.raise(this.state.start, "Unterminated string constant");
  3166. }
  3167. const ch = this.input.charCodeAt(this.state.pos);
  3168. if (ch === quote) break;
  3169. if (ch === 38) {
  3170. out += this.input.slice(chunkStart, this.state.pos);
  3171. out += this.jsxReadEntity();
  3172. chunkStart = this.state.pos;
  3173. } else if (isNewLine(ch)) {
  3174. out += this.input.slice(chunkStart, this.state.pos);
  3175. out += this.jsxReadNewLine(false);
  3176. chunkStart = this.state.pos;
  3177. } else {
  3178. ++this.state.pos;
  3179. }
  3180. }
  3181. out += this.input.slice(chunkStart, this.state.pos++);
  3182. return this.finishToken(types.string, out);
  3183. }
  3184. jsxReadEntity() {
  3185. let str = "";
  3186. let count = 0;
  3187. let entity;
  3188. let ch = this.input[this.state.pos];
  3189. const startPos = ++this.state.pos;
  3190. while (this.state.pos < this.length && count++ < 10) {
  3191. ch = this.input[this.state.pos++];
  3192. if (ch === ";") {
  3193. if (str[0] === "#") {
  3194. if (str[1] === "x") {
  3195. str = str.substr(2);
  3196. if (HEX_NUMBER.test(str)) {
  3197. entity = String.fromCodePoint(parseInt(str, 16));
  3198. }
  3199. } else {
  3200. str = str.substr(1);
  3201. if (DECIMAL_NUMBER.test(str)) {
  3202. entity = String.fromCodePoint(parseInt(str, 10));
  3203. }
  3204. }
  3205. } else {
  3206. entity = entities[str];
  3207. }
  3208. break;
  3209. }
  3210. str += ch;
  3211. }
  3212. if (!entity) {
  3213. this.state.pos = startPos;
  3214. return "&";
  3215. }
  3216. return entity;
  3217. }
  3218. jsxReadWord() {
  3219. let ch;
  3220. const start = this.state.pos;
  3221. do {
  3222. ch = this.input.charCodeAt(++this.state.pos);
  3223. } while (isIdentifierChar(ch) || ch === 45);
  3224. return this.finishToken(types.jsxName, this.input.slice(start, this.state.pos));
  3225. }
  3226. jsxParseIdentifier() {
  3227. const node = this.startNode();
  3228. if (this.match(types.jsxName)) {
  3229. node.name = this.state.value;
  3230. } else if (this.state.type.keyword) {
  3231. node.name = this.state.type.keyword;
  3232. } else {
  3233. this.unexpected();
  3234. }
  3235. this.next();
  3236. return this.finishNode(node, "JSXIdentifier");
  3237. }
  3238. jsxParseNamespacedName() {
  3239. const startPos = this.state.start;
  3240. const startLoc = this.state.startLoc;
  3241. const name = this.jsxParseIdentifier();
  3242. if (!this.eat(types.colon)) return name;
  3243. const node = this.startNodeAt(startPos, startLoc);
  3244. node.namespace = name;
  3245. node.name = this.jsxParseIdentifier();
  3246. return this.finishNode(node, "JSXNamespacedName");
  3247. }
  3248. jsxParseElementName() {
  3249. const startPos = this.state.start;
  3250. const startLoc = this.state.startLoc;
  3251. let node = this.jsxParseNamespacedName();
  3252. if (node.type === "JSXNamespacedName") {
  3253. return node;
  3254. }
  3255. while (this.eat(types.dot)) {
  3256. const newNode = this.startNodeAt(startPos, startLoc);
  3257. newNode.object = node;
  3258. newNode.property = this.jsxParseIdentifier();
  3259. node = this.finishNode(newNode, "JSXMemberExpression");
  3260. }
  3261. return node;
  3262. }
  3263. jsxParseAttributeValue() {
  3264. let node;
  3265. switch (this.state.type) {
  3266. case types.braceL:
  3267. node = this.startNode();
  3268. this.next();
  3269. node = this.jsxParseExpressionContainer(node);
  3270. if (node.expression.type === "JSXEmptyExpression") {
  3271. this.raise(node.start, "JSX attributes must only be assigned a non-empty expression");
  3272. }
  3273. return node;
  3274. case types.jsxTagStart:
  3275. case types.string:
  3276. return this.parseExprAtom();
  3277. default:
  3278. throw this.raise(this.state.start, "JSX value should be either an expression or a quoted JSX text");
  3279. }
  3280. }
  3281. jsxParseEmptyExpression() {
  3282. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  3283. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  3284. }
  3285. jsxParseSpreadChild(node) {
  3286. this.next();
  3287. node.expression = this.parseExpression();
  3288. this.expect(types.braceR);
  3289. return this.finishNode(node, "JSXSpreadChild");
  3290. }
  3291. jsxParseExpressionContainer(node) {
  3292. if (this.match(types.braceR)) {
  3293. node.expression = this.jsxParseEmptyExpression();
  3294. } else {
  3295. node.expression = this.parseExpression();
  3296. }
  3297. this.expect(types.braceR);
  3298. return this.finishNode(node, "JSXExpressionContainer");
  3299. }
  3300. jsxParseAttribute() {
  3301. const node = this.startNode();
  3302. if (this.eat(types.braceL)) {
  3303. this.expect(types.ellipsis);
  3304. node.argument = this.parseMaybeAssign();
  3305. this.expect(types.braceR);
  3306. return this.finishNode(node, "JSXSpreadAttribute");
  3307. }
  3308. node.name = this.jsxParseNamespacedName();
  3309. node.value = this.eat(types.eq) ? this.jsxParseAttributeValue() : null;
  3310. return this.finishNode(node, "JSXAttribute");
  3311. }
  3312. jsxParseOpeningElementAt(startPos, startLoc) {
  3313. const node = this.startNodeAt(startPos, startLoc);
  3314. if (this.match(types.jsxTagEnd)) {
  3315. this.expect(types.jsxTagEnd);
  3316. return this.finishNode(node, "JSXOpeningFragment");
  3317. }
  3318. node.name = this.jsxParseElementName();
  3319. return this.jsxParseOpeningElementAfterName(node);
  3320. }
  3321. jsxParseOpeningElementAfterName(node) {
  3322. const attributes = [];
  3323. while (!this.match(types.slash) && !this.match(types.jsxTagEnd)) {
  3324. attributes.push(this.jsxParseAttribute());
  3325. }
  3326. node.attributes = attributes;
  3327. node.selfClosing = this.eat(types.slash);
  3328. this.expect(types.jsxTagEnd);
  3329. return this.finishNode(node, "JSXOpeningElement");
  3330. }
  3331. jsxParseClosingElementAt(startPos, startLoc) {
  3332. const node = this.startNodeAt(startPos, startLoc);
  3333. if (this.match(types.jsxTagEnd)) {
  3334. this.expect(types.jsxTagEnd);
  3335. return this.finishNode(node, "JSXClosingFragment");
  3336. }
  3337. node.name = this.jsxParseElementName();
  3338. this.expect(types.jsxTagEnd);
  3339. return this.finishNode(node, "JSXClosingElement");
  3340. }
  3341. jsxParseElementAt(startPos, startLoc) {
  3342. const node = this.startNodeAt(startPos, startLoc);
  3343. const children = [];
  3344. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  3345. let closingElement = null;
  3346. if (!openingElement.selfClosing) {
  3347. contents: for (;;) {
  3348. switch (this.state.type) {
  3349. case types.jsxTagStart:
  3350. startPos = this.state.start;
  3351. startLoc = this.state.startLoc;
  3352. this.next();
  3353. if (this.eat(types.slash)) {
  3354. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  3355. break contents;
  3356. }
  3357. children.push(this.jsxParseElementAt(startPos, startLoc));
  3358. break;
  3359. case types.jsxText:
  3360. children.push(this.parseExprAtom());
  3361. break;
  3362. case types.braceL:
  3363. {
  3364. const node = this.startNode();
  3365. this.next();
  3366. if (this.match(types.ellipsis)) {
  3367. children.push(this.jsxParseSpreadChild(node));
  3368. } else {
  3369. children.push(this.jsxParseExpressionContainer(node));
  3370. }
  3371. break;
  3372. }
  3373. default:
  3374. throw this.unexpected();
  3375. }
  3376. }
  3377. if (isFragment(openingElement) && !isFragment(closingElement)) {
  3378. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <>");
  3379. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  3380. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  3381. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  3382. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  3383. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  3384. }
  3385. }
  3386. }
  3387. if (isFragment(openingElement)) {
  3388. node.openingFragment = openingElement;
  3389. node.closingFragment = closingElement;
  3390. } else {
  3391. node.openingElement = openingElement;
  3392. node.closingElement = closingElement;
  3393. }
  3394. node.children = children;
  3395. if (this.isRelational("<")) {
  3396. throw this.raise(this.state.start, "Adjacent JSX elements must be wrapped in an enclosing tag. " + "Did you want a JSX fragment <>...</>?");
  3397. }
  3398. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  3399. }
  3400. jsxParseElement() {
  3401. const startPos = this.state.start;
  3402. const startLoc = this.state.startLoc;
  3403. this.next();
  3404. return this.jsxParseElementAt(startPos, startLoc);
  3405. }
  3406. parseExprAtom(refShortHandDefaultPos) {
  3407. if (this.match(types.jsxText)) {
  3408. return this.parseLiteral(this.state.value, "JSXText");
  3409. } else if (this.match(types.jsxTagStart)) {
  3410. return this.jsxParseElement();
  3411. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  3412. this.finishToken(types.jsxTagStart);
  3413. return this.jsxParseElement();
  3414. } else {
  3415. return super.parseExprAtom(refShortHandDefaultPos);
  3416. }
  3417. }
  3418. getTokenFromCode(code) {
  3419. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  3420. const context = this.curContext();
  3421. if (context === types$1.j_expr) {
  3422. return this.jsxReadToken();
  3423. }
  3424. if (context === types$1.j_oTag || context === types$1.j_cTag) {
  3425. if (isIdentifierStart(code)) {
  3426. return this.jsxReadWord();
  3427. }
  3428. if (code === 62) {
  3429. ++this.state.pos;
  3430. return this.finishToken(types.jsxTagEnd);
  3431. }
  3432. if ((code === 34 || code === 39) && context === types$1.j_oTag) {
  3433. return this.jsxReadString(code);
  3434. }
  3435. }
  3436. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  3437. ++this.state.pos;
  3438. return this.finishToken(types.jsxTagStart);
  3439. }
  3440. return super.getTokenFromCode(code);
  3441. }
  3442. updateContext(prevType) {
  3443. if (this.match(types.braceL)) {
  3444. const curContext = this.curContext();
  3445. if (curContext === types$1.j_oTag) {
  3446. this.state.context.push(types$1.braceExpression);
  3447. } else if (curContext === types$1.j_expr) {
  3448. this.state.context.push(types$1.templateQuasi);
  3449. } else {
  3450. super.updateContext(prevType);
  3451. }
  3452. this.state.exprAllowed = true;
  3453. } else if (this.match(types.slash) && prevType === types.jsxTagStart) {
  3454. this.state.context.length -= 2;
  3455. this.state.context.push(types$1.j_cTag);
  3456. this.state.exprAllowed = false;
  3457. } else {
  3458. return super.updateContext(prevType);
  3459. }
  3460. }
  3461. });
  3462. class Scope {
  3463. constructor(flags) {
  3464. this.var = [];
  3465. this.lexical = [];
  3466. this.functions = [];
  3467. this.flags = flags;
  3468. }
  3469. }
  3470. class ScopeHandler {
  3471. constructor(raise, inModule) {
  3472. this.scopeStack = [];
  3473. this.undefinedExports = new Map();
  3474. this.raise = raise;
  3475. this.inModule = inModule;
  3476. }
  3477. get inFunction() {
  3478. return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0;
  3479. }
  3480. get inGenerator() {
  3481. return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0;
  3482. }
  3483. get inAsync() {
  3484. return (this.currentVarScope().flags & SCOPE_ASYNC) > 0;
  3485. }
  3486. get allowSuper() {
  3487. return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
  3488. }
  3489. get allowDirectSuper() {
  3490. return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0;
  3491. }
  3492. get inNonArrowFunction() {
  3493. return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0;
  3494. }
  3495. get treatFunctionsAsVar() {
  3496. return this.treatFunctionsAsVarInScope(this.currentScope());
  3497. }
  3498. createScope(flags) {
  3499. return new Scope(flags);
  3500. }
  3501. enter(flags) {
  3502. this.scopeStack.push(this.createScope(flags));
  3503. }
  3504. exit() {
  3505. this.scopeStack.pop();
  3506. }
  3507. treatFunctionsAsVarInScope(scope) {
  3508. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  3509. }
  3510. declareName(name, bindingType, pos) {
  3511. let scope = this.currentScope();
  3512. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  3513. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3514. if (bindingType & BIND_SCOPE_FUNCTION) {
  3515. scope.functions.push(name);
  3516. } else {
  3517. scope.lexical.push(name);
  3518. }
  3519. if (bindingType & BIND_SCOPE_LEXICAL) {
  3520. this.maybeExportDefined(scope, name);
  3521. }
  3522. } else if (bindingType & BIND_SCOPE_VAR) {
  3523. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  3524. scope = this.scopeStack[i];
  3525. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3526. scope.var.push(name);
  3527. this.maybeExportDefined(scope, name);
  3528. if (scope.flags & SCOPE_VAR) break;
  3529. }
  3530. }
  3531. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  3532. this.undefinedExports.delete(name);
  3533. }
  3534. }
  3535. maybeExportDefined(scope, name) {
  3536. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  3537. this.undefinedExports.delete(name);
  3538. }
  3539. }
  3540. checkRedeclarationInScope(scope, name, bindingType, pos) {
  3541. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  3542. this.raise(pos, `Identifier '${name}' has already been declared`);
  3543. }
  3544. }
  3545. isRedeclaredInScope(scope, name, bindingType) {
  3546. if (!(bindingType & BIND_KIND_VALUE)) return false;
  3547. if (bindingType & BIND_SCOPE_LEXICAL) {
  3548. return scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  3549. }
  3550. if (bindingType & BIND_SCOPE_FUNCTION) {
  3551. return scope.lexical.indexOf(name) > -1 || !this.treatFunctionsAsVarInScope(scope) && scope.var.indexOf(name) > -1;
  3552. }
  3553. return scope.lexical.indexOf(name) > -1 && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.indexOf(name) > -1;
  3554. }
  3555. checkLocalExport(id) {
  3556. if (this.scopeStack[0].lexical.indexOf(id.name) === -1 && this.scopeStack[0].var.indexOf(id.name) === -1 && this.scopeStack[0].functions.indexOf(id.name) === -1) {
  3557. this.undefinedExports.set(id.name, id.start);
  3558. }
  3559. }
  3560. currentScope() {
  3561. return this.scopeStack[this.scopeStack.length - 1];
  3562. }
  3563. currentVarScope() {
  3564. for (let i = this.scopeStack.length - 1;; i--) {
  3565. const scope = this.scopeStack[i];
  3566. if (scope.flags & SCOPE_VAR) {
  3567. return scope;
  3568. }
  3569. }
  3570. }
  3571. currentThisScope() {
  3572. for (let i = this.scopeStack.length - 1;; i--) {
  3573. const scope = this.scopeStack[i];
  3574. if ((scope.flags & SCOPE_VAR || scope.flags & SCOPE_CLASS) && !(scope.flags & SCOPE_ARROW)) {
  3575. return scope;
  3576. }
  3577. }
  3578. }
  3579. }
  3580. class TypeScriptScope extends Scope {
  3581. constructor(...args) {
  3582. super(...args);
  3583. this.types = [];
  3584. this.enums = [];
  3585. this.constEnums = [];
  3586. this.classes = [];
  3587. this.exportOnlyBindings = [];
  3588. }
  3589. }
  3590. class TypeScriptScopeHandler extends ScopeHandler {
  3591. createScope(flags) {
  3592. return new TypeScriptScope(flags);
  3593. }
  3594. declareName(name, bindingType, pos) {
  3595. const scope = this.currentScope();
  3596. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  3597. this.maybeExportDefined(scope, name);
  3598. scope.exportOnlyBindings.push(name);
  3599. return;
  3600. }
  3601. super.declareName(...arguments);
  3602. if (bindingType & BIND_KIND_TYPE) {
  3603. if (!(bindingType & BIND_KIND_VALUE)) {
  3604. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  3605. this.maybeExportDefined(scope, name);
  3606. }
  3607. scope.types.push(name);
  3608. }
  3609. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.push(name);
  3610. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.push(name);
  3611. if (bindingType & BIND_FLAGS_CLASS) scope.classes.push(name);
  3612. }
  3613. isRedeclaredInScope(scope, name, bindingType) {
  3614. if (scope.enums.indexOf(name) > -1) {
  3615. if (bindingType & BIND_FLAGS_TS_ENUM) {
  3616. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  3617. const wasConst = scope.constEnums.indexOf(name) > -1;
  3618. return isConst !== wasConst;
  3619. }
  3620. return true;
  3621. }
  3622. if (bindingType & BIND_FLAGS_CLASS && scope.classes.indexOf(name) > -1) {
  3623. if (scope.lexical.indexOf(name) > -1) {
  3624. return !!(bindingType & BIND_KIND_VALUE);
  3625. } else {
  3626. return false;
  3627. }
  3628. }
  3629. if (bindingType & BIND_KIND_TYPE && scope.types.indexOf(name) > -1) {
  3630. return true;
  3631. }
  3632. return super.isRedeclaredInScope(...arguments);
  3633. }
  3634. checkLocalExport(id) {
  3635. if (this.scopeStack[0].types.indexOf(id.name) === -1 && this.scopeStack[0].exportOnlyBindings.indexOf(id.name) === -1) {
  3636. super.checkLocalExport(id);
  3637. }
  3638. }
  3639. }
  3640. function nonNull(x) {
  3641. if (x == null) {
  3642. throw new Error(`Unexpected ${x} value.`);
  3643. }
  3644. return x;
  3645. }
  3646. function assert(x) {
  3647. if (!x) {
  3648. throw new Error("Assert fail");
  3649. }
  3650. }
  3651. function keywordTypeFromName(value) {
  3652. switch (value) {
  3653. case "any":
  3654. return "TSAnyKeyword";
  3655. case "boolean":
  3656. return "TSBooleanKeyword";
  3657. case "bigint":
  3658. return "TSBigIntKeyword";
  3659. case "never":
  3660. return "TSNeverKeyword";
  3661. case "number":
  3662. return "TSNumberKeyword";
  3663. case "object":
  3664. return "TSObjectKeyword";
  3665. case "string":
  3666. return "TSStringKeyword";
  3667. case "symbol":
  3668. return "TSSymbolKeyword";
  3669. case "undefined":
  3670. return "TSUndefinedKeyword";
  3671. case "unknown":
  3672. return "TSUnknownKeyword";
  3673. default:
  3674. return undefined;
  3675. }
  3676. }
  3677. var typescript = (superClass => class extends superClass {
  3678. getScopeHandler() {
  3679. return TypeScriptScopeHandler;
  3680. }
  3681. tsIsIdentifier() {
  3682. return this.match(types.name);
  3683. }
  3684. tsNextTokenCanFollowModifier() {
  3685. this.next();
  3686. return !this.hasPrecedingLineBreak() && !this.match(types.parenL) && !this.match(types.parenR) && !this.match(types.colon) && !this.match(types.eq) && !this.match(types.question) && !this.match(types.bang);
  3687. }
  3688. tsParseModifier(allowedModifiers) {
  3689. if (!this.match(types.name)) {
  3690. return undefined;
  3691. }
  3692. const modifier = this.state.value;
  3693. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  3694. return modifier;
  3695. }
  3696. return undefined;
  3697. }
  3698. tsParseModifiers(allowedModifiers) {
  3699. const modifiers = Object.create(null);
  3700. while (true) {
  3701. const startPos = this.state.start;
  3702. const modifier = this.tsParseModifier(allowedModifiers);
  3703. if (!modifier) break;
  3704. if (Object.hasOwnProperty.call(modifiers, modifier)) {
  3705. this.raise(startPos, `Duplicate modifier: '${modifier}'`);
  3706. }
  3707. modifiers[modifier] = true;
  3708. }
  3709. return modifiers;
  3710. }
  3711. tsIsListTerminator(kind) {
  3712. switch (kind) {
  3713. case "EnumMembers":
  3714. case "TypeMembers":
  3715. return this.match(types.braceR);
  3716. case "HeritageClauseElement":
  3717. return this.match(types.braceL);
  3718. case "TupleElementTypes":
  3719. return this.match(types.bracketR);
  3720. case "TypeParametersOrArguments":
  3721. return this.isRelational(">");
  3722. }
  3723. throw new Error("Unreachable");
  3724. }
  3725. tsParseList(kind, parseElement) {
  3726. const result = [];
  3727. while (!this.tsIsListTerminator(kind)) {
  3728. result.push(parseElement());
  3729. }
  3730. return result;
  3731. }
  3732. tsParseDelimitedList(kind, parseElement) {
  3733. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  3734. }
  3735. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  3736. const result = [];
  3737. while (true) {
  3738. if (this.tsIsListTerminator(kind)) {
  3739. break;
  3740. }
  3741. const element = parseElement();
  3742. if (element == null) {
  3743. return undefined;
  3744. }
  3745. result.push(element);
  3746. if (this.eat(types.comma)) {
  3747. continue;
  3748. }
  3749. if (this.tsIsListTerminator(kind)) {
  3750. break;
  3751. }
  3752. if (expectSuccess) {
  3753. this.expect(types.comma);
  3754. }
  3755. return undefined;
  3756. }
  3757. return result;
  3758. }
  3759. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  3760. if (!skipFirstToken) {
  3761. if (bracket) {
  3762. this.expect(types.bracketL);
  3763. } else {
  3764. this.expectRelational("<");
  3765. }
  3766. }
  3767. const result = this.tsParseDelimitedList(kind, parseElement);
  3768. if (bracket) {
  3769. this.expect(types.bracketR);
  3770. } else {
  3771. this.expectRelational(">");
  3772. }
  3773. return result;
  3774. }
  3775. tsParseImportType() {
  3776. const node = this.startNode();
  3777. this.expect(types._import);
  3778. this.expect(types.parenL);
  3779. if (!this.match(types.string)) {
  3780. this.raise(this.state.start, "Argument in a type import must be a string literal");
  3781. }
  3782. node.argument = this.parseExprAtom();
  3783. this.expect(types.parenR);
  3784. if (this.eat(types.dot)) {
  3785. node.qualifier = this.tsParseEntityName(true);
  3786. }
  3787. if (this.isRelational("<")) {
  3788. node.typeParameters = this.tsParseTypeArguments();
  3789. }
  3790. return this.finishNode(node, "TSImportType");
  3791. }
  3792. tsParseEntityName(allowReservedWords) {
  3793. let entity = this.parseIdentifier();
  3794. while (this.eat(types.dot)) {
  3795. const node = this.startNodeAtNode(entity);
  3796. node.left = entity;
  3797. node.right = this.parseIdentifier(allowReservedWords);
  3798. entity = this.finishNode(node, "TSQualifiedName");
  3799. }
  3800. return entity;
  3801. }
  3802. tsParseTypeReference() {
  3803. const node = this.startNode();
  3804. node.typeName = this.tsParseEntityName(false);
  3805. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  3806. node.typeParameters = this.tsParseTypeArguments();
  3807. }
  3808. return this.finishNode(node, "TSTypeReference");
  3809. }
  3810. tsParseThisTypePredicate(lhs) {
  3811. this.next();
  3812. const node = this.startNodeAtNode(lhs);
  3813. node.parameterName = lhs;
  3814. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  3815. return this.finishNode(node, "TSTypePredicate");
  3816. }
  3817. tsParseThisTypeNode() {
  3818. const node = this.startNode();
  3819. this.next();
  3820. return this.finishNode(node, "TSThisType");
  3821. }
  3822. tsParseTypeQuery() {
  3823. const node = this.startNode();
  3824. this.expect(types._typeof);
  3825. if (this.match(types._import)) {
  3826. node.exprName = this.tsParseImportType();
  3827. } else {
  3828. node.exprName = this.tsParseEntityName(true);
  3829. }
  3830. return this.finishNode(node, "TSTypeQuery");
  3831. }
  3832. tsParseTypeParameter() {
  3833. const node = this.startNode();
  3834. node.name = this.parseIdentifierName(node.start);
  3835. node.constraint = this.tsEatThenParseType(types._extends);
  3836. node.default = this.tsEatThenParseType(types.eq);
  3837. return this.finishNode(node, "TSTypeParameter");
  3838. }
  3839. tsTryParseTypeParameters() {
  3840. if (this.isRelational("<")) {
  3841. return this.tsParseTypeParameters();
  3842. }
  3843. }
  3844. tsParseTypeParameters() {
  3845. const node = this.startNode();
  3846. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  3847. this.next();
  3848. } else {
  3849. this.unexpected();
  3850. }
  3851. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  3852. return this.finishNode(node, "TSTypeParameterDeclaration");
  3853. }
  3854. tsTryNextParseConstantContext() {
  3855. if (this.lookahead().type === types._const) {
  3856. this.next();
  3857. return this.tsParseTypeReference();
  3858. }
  3859. return null;
  3860. }
  3861. tsFillSignature(returnToken, signature) {
  3862. const returnTokenRequired = returnToken === types.arrow;
  3863. signature.typeParameters = this.tsTryParseTypeParameters();
  3864. this.expect(types.parenL);
  3865. signature.parameters = this.tsParseBindingListForSignature();
  3866. if (returnTokenRequired) {
  3867. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  3868. } else if (this.match(returnToken)) {
  3869. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  3870. }
  3871. }
  3872. tsParseBindingListForSignature() {
  3873. return this.parseBindingList(types.parenR, 41).map(pattern => {
  3874. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  3875. this.raise(pattern.start, "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern," + `instead got ${pattern.type}`);
  3876. }
  3877. return pattern;
  3878. });
  3879. }
  3880. tsParseTypeMemberSemicolon() {
  3881. if (!this.eat(types.comma)) {
  3882. this.semicolon();
  3883. }
  3884. }
  3885. tsParseSignatureMember(kind, node) {
  3886. this.tsFillSignature(types.colon, node);
  3887. this.tsParseTypeMemberSemicolon();
  3888. return this.finishNode(node, kind);
  3889. }
  3890. tsIsUnambiguouslyIndexSignature() {
  3891. this.next();
  3892. return this.eat(types.name) && this.match(types.colon);
  3893. }
  3894. tsTryParseIndexSignature(node) {
  3895. if (!(this.match(types.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  3896. return undefined;
  3897. }
  3898. this.expect(types.bracketL);
  3899. const id = this.parseIdentifier();
  3900. id.typeAnnotation = this.tsParseTypeAnnotation();
  3901. this.resetEndLocation(id);
  3902. this.expect(types.bracketR);
  3903. node.parameters = [id];
  3904. const type = this.tsTryParseTypeAnnotation();
  3905. if (type) node.typeAnnotation = type;
  3906. this.tsParseTypeMemberSemicolon();
  3907. return this.finishNode(node, "TSIndexSignature");
  3908. }
  3909. tsParsePropertyOrMethodSignature(node, readonly) {
  3910. if (this.eat(types.question)) node.optional = true;
  3911. const nodeAny = node;
  3912. if (!readonly && (this.match(types.parenL) || this.isRelational("<"))) {
  3913. const method = nodeAny;
  3914. this.tsFillSignature(types.colon, method);
  3915. this.tsParseTypeMemberSemicolon();
  3916. return this.finishNode(method, "TSMethodSignature");
  3917. } else {
  3918. const property = nodeAny;
  3919. if (readonly) property.readonly = true;
  3920. const type = this.tsTryParseTypeAnnotation();
  3921. if (type) property.typeAnnotation = type;
  3922. this.tsParseTypeMemberSemicolon();
  3923. return this.finishNode(property, "TSPropertySignature");
  3924. }
  3925. }
  3926. tsParseTypeMember() {
  3927. const node = this.startNode();
  3928. if (this.match(types.parenL) || this.isRelational("<")) {
  3929. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  3930. }
  3931. if (this.match(types._new)) {
  3932. const id = this.startNode();
  3933. this.next();
  3934. if (this.match(types.parenL) || this.isRelational("<")) {
  3935. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  3936. } else {
  3937. node.key = this.createIdentifier(id, "new");
  3938. return this.tsParsePropertyOrMethodSignature(node, false);
  3939. }
  3940. }
  3941. const readonly = !!this.tsParseModifier(["readonly"]);
  3942. const idx = this.tsTryParseIndexSignature(node);
  3943. if (idx) {
  3944. if (readonly) node.readonly = true;
  3945. return idx;
  3946. }
  3947. this.parsePropertyName(node);
  3948. return this.tsParsePropertyOrMethodSignature(node, readonly);
  3949. }
  3950. tsParseTypeLiteral() {
  3951. const node = this.startNode();
  3952. node.members = this.tsParseObjectTypeMembers();
  3953. return this.finishNode(node, "TSTypeLiteral");
  3954. }
  3955. tsParseObjectTypeMembers() {
  3956. this.expect(types.braceL);
  3957. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  3958. this.expect(types.braceR);
  3959. return members;
  3960. }
  3961. tsIsStartOfMappedType() {
  3962. this.next();
  3963. if (this.eat(types.plusMin)) {
  3964. return this.isContextual("readonly");
  3965. }
  3966. if (this.isContextual("readonly")) {
  3967. this.next();
  3968. }
  3969. if (!this.match(types.bracketL)) {
  3970. return false;
  3971. }
  3972. this.next();
  3973. if (!this.tsIsIdentifier()) {
  3974. return false;
  3975. }
  3976. this.next();
  3977. return this.match(types._in);
  3978. }
  3979. tsParseMappedTypeParameter() {
  3980. const node = this.startNode();
  3981. node.name = this.parseIdentifierName(node.start);
  3982. node.constraint = this.tsExpectThenParseType(types._in);
  3983. return this.finishNode(node, "TSTypeParameter");
  3984. }
  3985. tsParseMappedType() {
  3986. const node = this.startNode();
  3987. this.expect(types.braceL);
  3988. if (this.match(types.plusMin)) {
  3989. node.readonly = this.state.value;
  3990. this.next();
  3991. this.expectContextual("readonly");
  3992. } else if (this.eatContextual("readonly")) {
  3993. node.readonly = true;
  3994. }
  3995. this.expect(types.bracketL);
  3996. node.typeParameter = this.tsParseMappedTypeParameter();
  3997. this.expect(types.bracketR);
  3998. if (this.match(types.plusMin)) {
  3999. node.optional = this.state.value;
  4000. this.next();
  4001. this.expect(types.question);
  4002. } else if (this.eat(types.question)) {
  4003. node.optional = true;
  4004. }
  4005. node.typeAnnotation = this.tsTryParseType();
  4006. this.semicolon();
  4007. this.expect(types.braceR);
  4008. return this.finishNode(node, "TSMappedType");
  4009. }
  4010. tsParseTupleType() {
  4011. const node = this.startNode();
  4012. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  4013. let seenOptionalElement = false;
  4014. node.elementTypes.forEach(elementNode => {
  4015. if (elementNode.type === "TSOptionalType") {
  4016. seenOptionalElement = true;
  4017. } else if (seenOptionalElement && elementNode.type !== "TSRestType") {
  4018. this.raise(elementNode.start, "A required element cannot follow an optional element.");
  4019. }
  4020. });
  4021. return this.finishNode(node, "TSTupleType");
  4022. }
  4023. tsParseTupleElementType() {
  4024. if (this.match(types.ellipsis)) {
  4025. const restNode = this.startNode();
  4026. this.next();
  4027. restNode.typeAnnotation = this.tsParseType();
  4028. this.checkCommaAfterRest(93);
  4029. return this.finishNode(restNode, "TSRestType");
  4030. }
  4031. const type = this.tsParseType();
  4032. if (this.eat(types.question)) {
  4033. const optionalTypeNode = this.startNodeAtNode(type);
  4034. optionalTypeNode.typeAnnotation = type;
  4035. return this.finishNode(optionalTypeNode, "TSOptionalType");
  4036. }
  4037. return type;
  4038. }
  4039. tsParseParenthesizedType() {
  4040. const node = this.startNode();
  4041. this.expect(types.parenL);
  4042. node.typeAnnotation = this.tsParseType();
  4043. this.expect(types.parenR);
  4044. return this.finishNode(node, "TSParenthesizedType");
  4045. }
  4046. tsParseFunctionOrConstructorType(type) {
  4047. const node = this.startNode();
  4048. if (type === "TSConstructorType") {
  4049. this.expect(types._new);
  4050. }
  4051. this.tsFillSignature(types.arrow, node);
  4052. return this.finishNode(node, type);
  4053. }
  4054. tsParseLiteralTypeNode() {
  4055. const node = this.startNode();
  4056. node.literal = (() => {
  4057. switch (this.state.type) {
  4058. case types.num:
  4059. case types.string:
  4060. case types._true:
  4061. case types._false:
  4062. return this.parseExprAtom();
  4063. default:
  4064. throw this.unexpected();
  4065. }
  4066. })();
  4067. return this.finishNode(node, "TSLiteralType");
  4068. }
  4069. tsParseTemplateLiteralType() {
  4070. const node = this.startNode();
  4071. const templateNode = this.parseTemplate(false);
  4072. if (templateNode.expressions.length > 0) {
  4073. this.raise(templateNode.expressions[0].start, "Template literal types cannot have any substitution");
  4074. }
  4075. node.literal = templateNode;
  4076. return this.finishNode(node, "TSLiteralType");
  4077. }
  4078. tsParseNonArrayType() {
  4079. switch (this.state.type) {
  4080. case types.name:
  4081. case types._void:
  4082. case types._null:
  4083. {
  4084. const type = this.match(types._void) ? "TSVoidKeyword" : this.match(types._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  4085. if (type !== undefined && this.lookaheadCharCode() !== 46) {
  4086. const node = this.startNode();
  4087. this.next();
  4088. return this.finishNode(node, type);
  4089. }
  4090. return this.tsParseTypeReference();
  4091. }
  4092. case types.string:
  4093. case types.num:
  4094. case types._true:
  4095. case types._false:
  4096. return this.tsParseLiteralTypeNode();
  4097. case types.plusMin:
  4098. if (this.state.value === "-") {
  4099. const node = this.startNode();
  4100. if (this.lookahead().type !== types.num) {
  4101. throw this.unexpected();
  4102. }
  4103. node.literal = this.parseMaybeUnary();
  4104. return this.finishNode(node, "TSLiteralType");
  4105. }
  4106. break;
  4107. case types._this:
  4108. {
  4109. const thisKeyword = this.tsParseThisTypeNode();
  4110. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  4111. return this.tsParseThisTypePredicate(thisKeyword);
  4112. } else {
  4113. return thisKeyword;
  4114. }
  4115. }
  4116. case types._typeof:
  4117. return this.tsParseTypeQuery();
  4118. case types._import:
  4119. return this.tsParseImportType();
  4120. case types.braceL:
  4121. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  4122. case types.bracketL:
  4123. return this.tsParseTupleType();
  4124. case types.parenL:
  4125. return this.tsParseParenthesizedType();
  4126. case types.backQuote:
  4127. return this.tsParseTemplateLiteralType();
  4128. }
  4129. throw this.unexpected();
  4130. }
  4131. tsParseArrayTypeOrHigher() {
  4132. let type = this.tsParseNonArrayType();
  4133. while (!this.hasPrecedingLineBreak() && this.eat(types.bracketL)) {
  4134. if (this.match(types.bracketR)) {
  4135. const node = this.startNodeAtNode(type);
  4136. node.elementType = type;
  4137. this.expect(types.bracketR);
  4138. type = this.finishNode(node, "TSArrayType");
  4139. } else {
  4140. const node = this.startNodeAtNode(type);
  4141. node.objectType = type;
  4142. node.indexType = this.tsParseType();
  4143. this.expect(types.bracketR);
  4144. type = this.finishNode(node, "TSIndexedAccessType");
  4145. }
  4146. }
  4147. return type;
  4148. }
  4149. tsParseTypeOperator(operator) {
  4150. const node = this.startNode();
  4151. this.expectContextual(operator);
  4152. node.operator = operator;
  4153. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  4154. if (operator === "readonly") {
  4155. this.tsCheckTypeAnnotationForReadOnly(node);
  4156. }
  4157. return this.finishNode(node, "TSTypeOperator");
  4158. }
  4159. tsCheckTypeAnnotationForReadOnly(node) {
  4160. switch (node.typeAnnotation.type) {
  4161. case "TSTupleType":
  4162. case "TSArrayType":
  4163. return;
  4164. default:
  4165. this.raise(node.start, "'readonly' type modifier is only permitted on array and tuple literal types.");
  4166. }
  4167. }
  4168. tsParseInferType() {
  4169. const node = this.startNode();
  4170. this.expectContextual("infer");
  4171. const typeParameter = this.startNode();
  4172. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  4173. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  4174. return this.finishNode(node, "TSInferType");
  4175. }
  4176. tsParseTypeOperatorOrHigher() {
  4177. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  4178. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  4179. }
  4180. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  4181. this.eat(operator);
  4182. let type = parseConstituentType();
  4183. if (this.match(operator)) {
  4184. const types = [type];
  4185. while (this.eat(operator)) {
  4186. types.push(parseConstituentType());
  4187. }
  4188. const node = this.startNodeAtNode(type);
  4189. node.types = types;
  4190. type = this.finishNode(node, kind);
  4191. }
  4192. return type;
  4193. }
  4194. tsParseIntersectionTypeOrHigher() {
  4195. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types.bitwiseAND);
  4196. }
  4197. tsParseUnionTypeOrHigher() {
  4198. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types.bitwiseOR);
  4199. }
  4200. tsIsStartOfFunctionType() {
  4201. if (this.isRelational("<")) {
  4202. return true;
  4203. }
  4204. return this.match(types.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  4205. }
  4206. tsSkipParameterStart() {
  4207. if (this.match(types.name) || this.match(types._this)) {
  4208. this.next();
  4209. return true;
  4210. }
  4211. if (this.match(types.braceL)) {
  4212. let braceStackCounter = 1;
  4213. this.next();
  4214. while (braceStackCounter > 0) {
  4215. if (this.match(types.braceL)) {
  4216. ++braceStackCounter;
  4217. } else if (this.match(types.braceR)) {
  4218. --braceStackCounter;
  4219. }
  4220. this.next();
  4221. }
  4222. return true;
  4223. }
  4224. if (this.match(types.bracketL)) {
  4225. let braceStackCounter = 1;
  4226. this.next();
  4227. while (braceStackCounter > 0) {
  4228. if (this.match(types.bracketL)) {
  4229. ++braceStackCounter;
  4230. } else if (this.match(types.bracketR)) {
  4231. --braceStackCounter;
  4232. }
  4233. this.next();
  4234. }
  4235. return true;
  4236. }
  4237. return false;
  4238. }
  4239. tsIsUnambiguouslyStartOfFunctionType() {
  4240. this.next();
  4241. if (this.match(types.parenR) || this.match(types.ellipsis)) {
  4242. return true;
  4243. }
  4244. if (this.tsSkipParameterStart()) {
  4245. if (this.match(types.colon) || this.match(types.comma) || this.match(types.question) || this.match(types.eq)) {
  4246. return true;
  4247. }
  4248. if (this.match(types.parenR)) {
  4249. this.next();
  4250. if (this.match(types.arrow)) {
  4251. return true;
  4252. }
  4253. }
  4254. }
  4255. return false;
  4256. }
  4257. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  4258. return this.tsInType(() => {
  4259. const t = this.startNode();
  4260. this.expect(returnToken);
  4261. const asserts = this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  4262. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  4263. if (!typePredicateVariable) {
  4264. if (!asserts) {
  4265. return this.tsParseTypeAnnotation(false, t);
  4266. }
  4267. const node = this.startNodeAtNode(t);
  4268. node.parameterName = this.parseIdentifier();
  4269. node.asserts = asserts;
  4270. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  4271. return this.finishNode(t, "TSTypeAnnotation");
  4272. }
  4273. const type = this.tsParseTypeAnnotation(false);
  4274. const node = this.startNodeAtNode(t);
  4275. node.parameterName = typePredicateVariable;
  4276. node.typeAnnotation = type;
  4277. node.asserts = asserts;
  4278. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  4279. return this.finishNode(t, "TSTypeAnnotation");
  4280. });
  4281. }
  4282. tsTryParseTypeOrTypePredicateAnnotation() {
  4283. return this.match(types.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types.colon) : undefined;
  4284. }
  4285. tsTryParseTypeAnnotation() {
  4286. return this.match(types.colon) ? this.tsParseTypeAnnotation() : undefined;
  4287. }
  4288. tsTryParseType() {
  4289. return this.tsEatThenParseType(types.colon);
  4290. }
  4291. tsParseTypePredicatePrefix() {
  4292. const id = this.parseIdentifier();
  4293. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  4294. this.next();
  4295. return id;
  4296. }
  4297. }
  4298. tsParseTypePredicateAsserts() {
  4299. if (!this.tsIsIdentifier()) {
  4300. return false;
  4301. }
  4302. const id = this.parseIdentifier();
  4303. if (id.name !== "asserts" || this.hasPrecedingLineBreak() || !this.tsIsIdentifier()) {
  4304. return false;
  4305. }
  4306. return true;
  4307. }
  4308. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  4309. this.tsInType(() => {
  4310. if (eatColon) this.expect(types.colon);
  4311. t.typeAnnotation = this.tsParseType();
  4312. });
  4313. return this.finishNode(t, "TSTypeAnnotation");
  4314. }
  4315. tsParseType() {
  4316. assert(this.state.inType);
  4317. const type = this.tsParseNonConditionalType();
  4318. if (this.hasPrecedingLineBreak() || !this.eat(types._extends)) {
  4319. return type;
  4320. }
  4321. const node = this.startNodeAtNode(type);
  4322. node.checkType = type;
  4323. node.extendsType = this.tsParseNonConditionalType();
  4324. this.expect(types.question);
  4325. node.trueType = this.tsParseType();
  4326. this.expect(types.colon);
  4327. node.falseType = this.tsParseType();
  4328. return this.finishNode(node, "TSConditionalType");
  4329. }
  4330. tsParseNonConditionalType() {
  4331. if (this.tsIsStartOfFunctionType()) {
  4332. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  4333. }
  4334. if (this.match(types._new)) {
  4335. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  4336. }
  4337. return this.tsParseUnionTypeOrHigher();
  4338. }
  4339. tsParseTypeAssertion() {
  4340. const node = this.startNode();
  4341. const _const = this.tsTryNextParseConstantContext();
  4342. node.typeAnnotation = _const || this.tsNextThenParseType();
  4343. this.expectRelational(">");
  4344. node.expression = this.parseMaybeUnary();
  4345. return this.finishNode(node, "TSTypeAssertion");
  4346. }
  4347. tsParseHeritageClause(descriptor) {
  4348. const originalStart = this.state.start;
  4349. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  4350. if (!delimitedList.length) {
  4351. this.raise(originalStart, `'${descriptor}' list cannot be empty.`);
  4352. }
  4353. return delimitedList;
  4354. }
  4355. tsParseExpressionWithTypeArguments() {
  4356. const node = this.startNode();
  4357. node.expression = this.tsParseEntityName(false);
  4358. if (this.isRelational("<")) {
  4359. node.typeParameters = this.tsParseTypeArguments();
  4360. }
  4361. return this.finishNode(node, "TSExpressionWithTypeArguments");
  4362. }
  4363. tsParseInterfaceDeclaration(node) {
  4364. node.id = this.parseIdentifier();
  4365. this.checkLVal(node.id, BIND_TS_INTERFACE, undefined, "typescript interface declaration");
  4366. node.typeParameters = this.tsTryParseTypeParameters();
  4367. if (this.eat(types._extends)) {
  4368. node.extends = this.tsParseHeritageClause("extends");
  4369. }
  4370. const body = this.startNode();
  4371. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  4372. node.body = this.finishNode(body, "TSInterfaceBody");
  4373. return this.finishNode(node, "TSInterfaceDeclaration");
  4374. }
  4375. tsParseTypeAliasDeclaration(node) {
  4376. node.id = this.parseIdentifier();
  4377. this.checkLVal(node.id, BIND_TS_TYPE, undefined, "typescript type alias");
  4378. node.typeParameters = this.tsTryParseTypeParameters();
  4379. node.typeAnnotation = this.tsExpectThenParseType(types.eq);
  4380. this.semicolon();
  4381. return this.finishNode(node, "TSTypeAliasDeclaration");
  4382. }
  4383. tsInNoContext(cb) {
  4384. const oldContext = this.state.context;
  4385. this.state.context = [oldContext[0]];
  4386. try {
  4387. return cb();
  4388. } finally {
  4389. this.state.context = oldContext;
  4390. }
  4391. }
  4392. tsInType(cb) {
  4393. const oldInType = this.state.inType;
  4394. this.state.inType = true;
  4395. try {
  4396. return cb();
  4397. } finally {
  4398. this.state.inType = oldInType;
  4399. }
  4400. }
  4401. tsEatThenParseType(token) {
  4402. return !this.match(token) ? undefined : this.tsNextThenParseType();
  4403. }
  4404. tsExpectThenParseType(token) {
  4405. return this.tsDoThenParseType(() => this.expect(token));
  4406. }
  4407. tsNextThenParseType() {
  4408. return this.tsDoThenParseType(() => this.next());
  4409. }
  4410. tsDoThenParseType(cb) {
  4411. return this.tsInType(() => {
  4412. cb();
  4413. return this.tsParseType();
  4414. });
  4415. }
  4416. tsParseEnumMember() {
  4417. const node = this.startNode();
  4418. node.id = this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  4419. if (this.eat(types.eq)) {
  4420. node.initializer = this.parseMaybeAssign();
  4421. }
  4422. return this.finishNode(node, "TSEnumMember");
  4423. }
  4424. tsParseEnumDeclaration(node, isConst) {
  4425. if (isConst) node.const = true;
  4426. node.id = this.parseIdentifier();
  4427. this.checkLVal(node.id, isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM, undefined, "typescript enum declaration");
  4428. this.expect(types.braceL);
  4429. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  4430. this.expect(types.braceR);
  4431. return this.finishNode(node, "TSEnumDeclaration");
  4432. }
  4433. tsParseModuleBlock() {
  4434. const node = this.startNode();
  4435. this.scope.enter(SCOPE_OTHER);
  4436. this.expect(types.braceL);
  4437. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types.braceR);
  4438. this.scope.exit();
  4439. return this.finishNode(node, "TSModuleBlock");
  4440. }
  4441. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  4442. node.id = this.parseIdentifier();
  4443. if (!nested) {
  4444. this.checkLVal(node.id, BIND_TS_NAMESPACE, null, "module or namespace declaration");
  4445. }
  4446. if (this.eat(types.dot)) {
  4447. const inner = this.startNode();
  4448. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  4449. node.body = inner;
  4450. } else {
  4451. this.scope.enter(SCOPE_TS_MODULE);
  4452. node.body = this.tsParseModuleBlock();
  4453. this.scope.exit();
  4454. }
  4455. return this.finishNode(node, "TSModuleDeclaration");
  4456. }
  4457. tsParseAmbientExternalModuleDeclaration(node) {
  4458. if (this.isContextual("global")) {
  4459. node.global = true;
  4460. node.id = this.parseIdentifier();
  4461. } else if (this.match(types.string)) {
  4462. node.id = this.parseExprAtom();
  4463. } else {
  4464. this.unexpected();
  4465. }
  4466. if (this.match(types.braceL)) {
  4467. this.scope.enter(SCOPE_TS_MODULE);
  4468. node.body = this.tsParseModuleBlock();
  4469. this.scope.exit();
  4470. } else {
  4471. this.semicolon();
  4472. }
  4473. return this.finishNode(node, "TSModuleDeclaration");
  4474. }
  4475. tsParseImportEqualsDeclaration(node, isExport) {
  4476. node.isExport = isExport || false;
  4477. node.id = this.parseIdentifier();
  4478. this.expect(types.eq);
  4479. node.moduleReference = this.tsParseModuleReference();
  4480. this.semicolon();
  4481. return this.finishNode(node, "TSImportEqualsDeclaration");
  4482. }
  4483. tsIsExternalModuleReference() {
  4484. return this.isContextual("require") && this.lookaheadCharCode() === 40;
  4485. }
  4486. tsParseModuleReference() {
  4487. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  4488. }
  4489. tsParseExternalModuleReference() {
  4490. const node = this.startNode();
  4491. this.expectContextual("require");
  4492. this.expect(types.parenL);
  4493. if (!this.match(types.string)) {
  4494. throw this.unexpected();
  4495. }
  4496. node.expression = this.parseExprAtom();
  4497. this.expect(types.parenR);
  4498. return this.finishNode(node, "TSExternalModuleReference");
  4499. }
  4500. tsLookAhead(f) {
  4501. const state = this.state.clone();
  4502. const res = f();
  4503. this.state = state;
  4504. return res;
  4505. }
  4506. tsTryParseAndCatch(f) {
  4507. const result = this.tryParse(abort => f() || abort());
  4508. if (result.aborted || !result.node) return undefined;
  4509. if (result.error) this.state = result.failState;
  4510. return result.node;
  4511. }
  4512. tsTryParse(f) {
  4513. const state = this.state.clone();
  4514. const result = f();
  4515. if (result !== undefined && result !== false) {
  4516. return result;
  4517. } else {
  4518. this.state = state;
  4519. return undefined;
  4520. }
  4521. }
  4522. tsTryParseDeclare(nany) {
  4523. if (this.isLineTerminator()) {
  4524. return;
  4525. }
  4526. let starttype = this.state.type;
  4527. let kind;
  4528. if (this.isContextual("let")) {
  4529. starttype = types._var;
  4530. kind = "let";
  4531. }
  4532. switch (starttype) {
  4533. case types._function:
  4534. return this.parseFunctionStatement(nany, false, true);
  4535. case types._class:
  4536. nany.declare = true;
  4537. return this.parseClass(nany, true, false);
  4538. case types._const:
  4539. if (this.match(types._const) && this.isLookaheadContextual("enum")) {
  4540. this.expect(types._const);
  4541. this.expectContextual("enum");
  4542. return this.tsParseEnumDeclaration(nany, true);
  4543. }
  4544. case types._var:
  4545. kind = kind || this.state.value;
  4546. return this.parseVarStatement(nany, kind);
  4547. case types.name:
  4548. {
  4549. const value = this.state.value;
  4550. if (value === "global") {
  4551. return this.tsParseAmbientExternalModuleDeclaration(nany);
  4552. } else {
  4553. return this.tsParseDeclaration(nany, value, true);
  4554. }
  4555. }
  4556. }
  4557. }
  4558. tsTryParseExportDeclaration() {
  4559. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  4560. }
  4561. tsParseExpressionStatement(node, expr) {
  4562. switch (expr.name) {
  4563. case "declare":
  4564. {
  4565. const declaration = this.tsTryParseDeclare(node);
  4566. if (declaration) {
  4567. declaration.declare = true;
  4568. return declaration;
  4569. }
  4570. break;
  4571. }
  4572. case "global":
  4573. if (this.match(types.braceL)) {
  4574. this.scope.enter(SCOPE_TS_MODULE);
  4575. const mod = node;
  4576. mod.global = true;
  4577. mod.id = expr;
  4578. mod.body = this.tsParseModuleBlock();
  4579. this.scope.exit();
  4580. return this.finishNode(mod, "TSModuleDeclaration");
  4581. }
  4582. break;
  4583. default:
  4584. return this.tsParseDeclaration(node, expr.name, false);
  4585. }
  4586. }
  4587. tsParseDeclaration(node, value, next) {
  4588. switch (value) {
  4589. case "abstract":
  4590. if (this.tsCheckLineTerminatorAndMatch(types._class, next)) {
  4591. const cls = node;
  4592. cls.abstract = true;
  4593. if (next) {
  4594. this.next();
  4595. if (!this.match(types._class)) {
  4596. this.unexpected(null, types._class);
  4597. }
  4598. }
  4599. return this.parseClass(cls, true, false);
  4600. }
  4601. break;
  4602. case "enum":
  4603. if (next || this.match(types.name)) {
  4604. if (next) this.next();
  4605. return this.tsParseEnumDeclaration(node, false);
  4606. }
  4607. break;
  4608. case "interface":
  4609. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4610. if (next) this.next();
  4611. return this.tsParseInterfaceDeclaration(node);
  4612. }
  4613. break;
  4614. case "module":
  4615. if (next) this.next();
  4616. if (this.match(types.string)) {
  4617. return this.tsParseAmbientExternalModuleDeclaration(node);
  4618. } else if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4619. return this.tsParseModuleOrNamespaceDeclaration(node);
  4620. }
  4621. break;
  4622. case "namespace":
  4623. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4624. if (next) this.next();
  4625. return this.tsParseModuleOrNamespaceDeclaration(node);
  4626. }
  4627. break;
  4628. case "type":
  4629. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  4630. if (next) this.next();
  4631. return this.tsParseTypeAliasDeclaration(node);
  4632. }
  4633. break;
  4634. }
  4635. }
  4636. tsCheckLineTerminatorAndMatch(tokenType, next) {
  4637. return (next || this.match(tokenType)) && !this.isLineTerminator();
  4638. }
  4639. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  4640. if (!this.isRelational("<")) {
  4641. return undefined;
  4642. }
  4643. const res = this.tsTryParseAndCatch(() => {
  4644. const node = this.startNodeAt(startPos, startLoc);
  4645. node.typeParameters = this.tsParseTypeParameters();
  4646. super.parseFunctionParams(node);
  4647. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  4648. this.expect(types.arrow);
  4649. return node;
  4650. });
  4651. if (!res) {
  4652. return undefined;
  4653. }
  4654. return this.parseArrowExpression(res, null, true);
  4655. }
  4656. tsParseTypeArguments() {
  4657. const node = this.startNode();
  4658. node.params = this.tsInType(() => this.tsInNoContext(() => {
  4659. this.expectRelational("<");
  4660. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  4661. }));
  4662. this.state.exprAllowed = false;
  4663. this.expectRelational(">");
  4664. return this.finishNode(node, "TSTypeParameterInstantiation");
  4665. }
  4666. tsIsDeclarationStart() {
  4667. if (this.match(types.name)) {
  4668. switch (this.state.value) {
  4669. case "abstract":
  4670. case "declare":
  4671. case "enum":
  4672. case "interface":
  4673. case "module":
  4674. case "namespace":
  4675. case "type":
  4676. return true;
  4677. }
  4678. }
  4679. return false;
  4680. }
  4681. isExportDefaultSpecifier() {
  4682. if (this.tsIsDeclarationStart()) return false;
  4683. return super.isExportDefaultSpecifier();
  4684. }
  4685. parseAssignableListItem(allowModifiers, decorators) {
  4686. const startPos = this.state.start;
  4687. const startLoc = this.state.startLoc;
  4688. let accessibility;
  4689. let readonly = false;
  4690. if (allowModifiers) {
  4691. accessibility = this.parseAccessModifier();
  4692. readonly = !!this.tsParseModifier(["readonly"]);
  4693. }
  4694. const left = this.parseMaybeDefault();
  4695. this.parseAssignableListItemTypes(left);
  4696. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  4697. if (accessibility || readonly) {
  4698. const pp = this.startNodeAt(startPos, startLoc);
  4699. if (decorators.length) {
  4700. pp.decorators = decorators;
  4701. }
  4702. if (accessibility) pp.accessibility = accessibility;
  4703. if (readonly) pp.readonly = readonly;
  4704. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  4705. this.raise(pp.start, "A parameter property may not be declared using a binding pattern.");
  4706. }
  4707. pp.parameter = elt;
  4708. return this.finishNode(pp, "TSParameterProperty");
  4709. }
  4710. if (decorators.length) {
  4711. left.decorators = decorators;
  4712. }
  4713. return elt;
  4714. }
  4715. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4716. if (this.match(types.colon)) {
  4717. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  4718. }
  4719. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  4720. if (bodilessType && !this.match(types.braceL) && this.isLineTerminator()) {
  4721. this.finishNode(node, bodilessType);
  4722. return;
  4723. }
  4724. super.parseFunctionBodyAndFinish(node, type, isMethod);
  4725. }
  4726. registerFunctionStatementId(node) {
  4727. if (!node.body && node.id) {
  4728. this.checkLVal(node.id, BIND_TS_AMBIENT, null, "function name");
  4729. } else {
  4730. super.registerFunctionStatementId(...arguments);
  4731. }
  4732. }
  4733. parseSubscript(base, startPos, startLoc, noCalls, state) {
  4734. if (!this.hasPrecedingLineBreak() && this.match(types.bang)) {
  4735. this.state.exprAllowed = false;
  4736. this.next();
  4737. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  4738. nonNullExpression.expression = base;
  4739. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  4740. }
  4741. if (this.isRelational("<")) {
  4742. const result = this.tsTryParseAndCatch(() => {
  4743. if (!noCalls && this.atPossibleAsync(base)) {
  4744. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  4745. if (asyncArrowFn) {
  4746. return asyncArrowFn;
  4747. }
  4748. }
  4749. const node = this.startNodeAt(startPos, startLoc);
  4750. node.callee = base;
  4751. const typeArguments = this.tsParseTypeArguments();
  4752. if (typeArguments) {
  4753. if (!noCalls && this.eat(types.parenL)) {
  4754. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  4755. node.typeParameters = typeArguments;
  4756. return this.finishCallExpression(node, state.optionalChainMember);
  4757. } else if (this.match(types.backQuote)) {
  4758. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments);
  4759. }
  4760. }
  4761. this.unexpected();
  4762. });
  4763. if (result) return result;
  4764. }
  4765. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  4766. }
  4767. parseNewArguments(node) {
  4768. if (this.isRelational("<")) {
  4769. const typeParameters = this.tsTryParseAndCatch(() => {
  4770. const args = this.tsParseTypeArguments();
  4771. if (!this.match(types.parenL)) this.unexpected();
  4772. return args;
  4773. });
  4774. if (typeParameters) {
  4775. node.typeParameters = typeParameters;
  4776. }
  4777. }
  4778. super.parseNewArguments(node);
  4779. }
  4780. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  4781. if (nonNull(types._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  4782. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  4783. node.expression = left;
  4784. const _const = this.tsTryNextParseConstantContext();
  4785. if (_const) {
  4786. node.typeAnnotation = _const;
  4787. } else {
  4788. node.typeAnnotation = this.tsNextThenParseType();
  4789. }
  4790. this.finishNode(node, "TSAsExpression");
  4791. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  4792. }
  4793. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn);
  4794. }
  4795. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  4796. checkDuplicateExports() {}
  4797. parseImport(node) {
  4798. if (this.match(types.name) && this.lookahead().type === types.eq) {
  4799. return this.tsParseImportEqualsDeclaration(node);
  4800. }
  4801. return super.parseImport(node);
  4802. }
  4803. parseExport(node) {
  4804. if (this.match(types._import)) {
  4805. this.expect(types._import);
  4806. return this.tsParseImportEqualsDeclaration(node, true);
  4807. } else if (this.eat(types.eq)) {
  4808. const assign = node;
  4809. assign.expression = this.parseExpression();
  4810. this.semicolon();
  4811. return this.finishNode(assign, "TSExportAssignment");
  4812. } else if (this.eatContextual("as")) {
  4813. const decl = node;
  4814. this.expectContextual("namespace");
  4815. decl.id = this.parseIdentifier();
  4816. this.semicolon();
  4817. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  4818. } else {
  4819. return super.parseExport(node);
  4820. }
  4821. }
  4822. isAbstractClass() {
  4823. return this.isContextual("abstract") && this.lookahead().type === types._class;
  4824. }
  4825. parseExportDefaultExpression() {
  4826. if (this.isAbstractClass()) {
  4827. const cls = this.startNode();
  4828. this.next();
  4829. this.parseClass(cls, true, true);
  4830. cls.abstract = true;
  4831. return cls;
  4832. }
  4833. if (this.state.value === "interface") {
  4834. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  4835. if (result) return result;
  4836. }
  4837. return super.parseExportDefaultExpression();
  4838. }
  4839. parseStatementContent(context, topLevel) {
  4840. if (this.state.type === types._const) {
  4841. const ahead = this.lookahead();
  4842. if (ahead.type === types.name && ahead.value === "enum") {
  4843. const node = this.startNode();
  4844. this.expect(types._const);
  4845. this.expectContextual("enum");
  4846. return this.tsParseEnumDeclaration(node, true);
  4847. }
  4848. }
  4849. return super.parseStatementContent(context, topLevel);
  4850. }
  4851. parseAccessModifier() {
  4852. return this.tsParseModifier(["public", "protected", "private"]);
  4853. }
  4854. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  4855. const accessibility = this.parseAccessModifier();
  4856. if (accessibility) member.accessibility = accessibility;
  4857. super.parseClassMember(classBody, member, state, constructorAllowsSuper);
  4858. }
  4859. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  4860. const modifiers = this.tsParseModifiers(["abstract", "readonly", "declare"]);
  4861. Object.assign(member, modifiers);
  4862. const idx = this.tsTryParseIndexSignature(member);
  4863. if (idx) {
  4864. classBody.body.push(idx);
  4865. if (modifiers.abstract) {
  4866. this.raise(member.start, "Index signatures cannot have the 'abstract' modifier");
  4867. }
  4868. if (isStatic) {
  4869. this.raise(member.start, "Index signatures cannot have the 'static' modifier");
  4870. }
  4871. if (member.accessibility) {
  4872. this.raise(member.start, `Index signatures cannot have an accessibility modifier ('${member.accessibility}')`);
  4873. }
  4874. return;
  4875. }
  4876. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  4877. }
  4878. parsePostMemberNameModifiers(methodOrProp) {
  4879. const optional = this.eat(types.question);
  4880. if (optional) methodOrProp.optional = true;
  4881. if (methodOrProp.readonly && this.match(types.parenL)) {
  4882. this.raise(methodOrProp.start, "Class methods cannot have the 'readonly' modifier");
  4883. }
  4884. if (methodOrProp.declare && this.match(types.parenL)) {
  4885. this.raise(methodOrProp.start, "Class methods cannot have the 'declare' modifier");
  4886. }
  4887. }
  4888. parseExpressionStatement(node, expr) {
  4889. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  4890. return decl || super.parseExpressionStatement(node, expr);
  4891. }
  4892. shouldParseExportDeclaration() {
  4893. if (this.tsIsDeclarationStart()) return true;
  4894. return super.shouldParseExportDeclaration();
  4895. }
  4896. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  4897. if (!refNeedsArrowPos || !this.match(types.question)) {
  4898. return super.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  4899. }
  4900. const result = this.tryParse(() => super.parseConditional(expr, noIn, startPos, startLoc));
  4901. if (!result.node) {
  4902. refNeedsArrowPos.start = result.error.pos || this.state.start;
  4903. return expr;
  4904. }
  4905. if (result.error) this.state = result.failState;
  4906. return result.node;
  4907. }
  4908. parseParenItem(node, startPos, startLoc) {
  4909. node = super.parseParenItem(node, startPos, startLoc);
  4910. if (this.eat(types.question)) {
  4911. node.optional = true;
  4912. this.resetEndLocation(node);
  4913. }
  4914. if (this.match(types.colon)) {
  4915. const typeCastNode = this.startNodeAt(startPos, startLoc);
  4916. typeCastNode.expression = node;
  4917. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  4918. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  4919. }
  4920. return node;
  4921. }
  4922. parseExportDeclaration(node) {
  4923. const startPos = this.state.start;
  4924. const startLoc = this.state.startLoc;
  4925. const isDeclare = this.eatContextual("declare");
  4926. let declaration;
  4927. if (this.match(types.name)) {
  4928. declaration = this.tsTryParseExportDeclaration();
  4929. }
  4930. if (!declaration) {
  4931. declaration = super.parseExportDeclaration(node);
  4932. }
  4933. if (declaration && isDeclare) {
  4934. this.resetStartLocation(declaration, startPos, startLoc);
  4935. declaration.declare = true;
  4936. }
  4937. return declaration;
  4938. }
  4939. parseClassId(node, isStatement, optionalId) {
  4940. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  4941. return;
  4942. }
  4943. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  4944. const typeParameters = this.tsTryParseTypeParameters();
  4945. if (typeParameters) node.typeParameters = typeParameters;
  4946. }
  4947. parseClassPropertyAnnotation(node) {
  4948. if (!node.optional && this.eat(types.bang)) {
  4949. node.definite = true;
  4950. }
  4951. const type = this.tsTryParseTypeAnnotation();
  4952. if (type) node.typeAnnotation = type;
  4953. }
  4954. parseClassProperty(node) {
  4955. this.parseClassPropertyAnnotation(node);
  4956. if (node.declare && this.match(types.equal)) {
  4957. this.raise(this.state.start, "'declare' class fields cannot have an initializer");
  4958. }
  4959. return super.parseClassProperty(node);
  4960. }
  4961. parseClassPrivateProperty(node) {
  4962. if (node.abstract) {
  4963. this.raise(node.start, "Private elements cannot have the 'abstract' modifier.");
  4964. }
  4965. if (node.accessibility) {
  4966. this.raise(node.start, `Private elements cannot have an accessibility modifier ('${node.accessibility}')`);
  4967. }
  4968. this.parseClassPropertyAnnotation(node);
  4969. return super.parseClassPrivateProperty(node);
  4970. }
  4971. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  4972. const typeParameters = this.tsTryParseTypeParameters();
  4973. if (typeParameters) method.typeParameters = typeParameters;
  4974. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  4975. }
  4976. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  4977. const typeParameters = this.tsTryParseTypeParameters();
  4978. if (typeParameters) method.typeParameters = typeParameters;
  4979. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  4980. }
  4981. parseClassSuper(node) {
  4982. super.parseClassSuper(node);
  4983. if (node.superClass && this.isRelational("<")) {
  4984. node.superTypeParameters = this.tsParseTypeArguments();
  4985. }
  4986. if (this.eatContextual("implements")) {
  4987. node.implements = this.tsParseHeritageClause("implements");
  4988. }
  4989. }
  4990. parseObjPropValue(prop, ...args) {
  4991. const typeParameters = this.tsTryParseTypeParameters();
  4992. if (typeParameters) prop.typeParameters = typeParameters;
  4993. super.parseObjPropValue(prop, ...args);
  4994. }
  4995. parseFunctionParams(node, allowModifiers) {
  4996. const typeParameters = this.tsTryParseTypeParameters();
  4997. if (typeParameters) node.typeParameters = typeParameters;
  4998. super.parseFunctionParams(node, allowModifiers);
  4999. }
  5000. parseVarId(decl, kind) {
  5001. super.parseVarId(decl, kind);
  5002. if (decl.id.type === "Identifier" && this.eat(types.bang)) {
  5003. decl.definite = true;
  5004. }
  5005. const type = this.tsTryParseTypeAnnotation();
  5006. if (type) {
  5007. decl.id.typeAnnotation = type;
  5008. this.resetEndLocation(decl.id);
  5009. }
  5010. }
  5011. parseAsyncArrowFromCallExpression(node, call) {
  5012. if (this.match(types.colon)) {
  5013. node.returnType = this.tsParseTypeAnnotation();
  5014. }
  5015. return super.parseAsyncArrowFromCallExpression(node, call);
  5016. }
  5017. parseMaybeAssign(...args) {
  5018. let state;
  5019. let jsx;
  5020. let typeCast;
  5021. if (this.match(types.jsxTagStart)) {
  5022. state = this.state.clone();
  5023. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  5024. if (!jsx.error) return jsx.node;
  5025. const {
  5026. context
  5027. } = this.state;
  5028. if (context[context.length - 1] === types$1.j_oTag) {
  5029. context.length -= 2;
  5030. } else if (context[context.length - 1] === types$1.j_expr) {
  5031. context.length -= 1;
  5032. }
  5033. }
  5034. if (!(jsx && jsx.error) && !this.isRelational("<")) {
  5035. return super.parseMaybeAssign(...args);
  5036. }
  5037. let typeParameters;
  5038. state = state || this.state.clone();
  5039. const arrow = this.tryParse(abort => {
  5040. typeParameters = this.tsParseTypeParameters();
  5041. const expr = super.parseMaybeAssign(...args);
  5042. if (expr.type !== "ArrowFunctionExpression" || expr.extra && expr.extra.parenthesized) {
  5043. abort();
  5044. }
  5045. if (typeParameters && typeParameters.params.length !== 0) {
  5046. this.resetStartLocationFromNode(expr, typeParameters);
  5047. }
  5048. expr.typeParameters = typeParameters;
  5049. return expr;
  5050. }, state);
  5051. if (!arrow.error && !arrow.aborted) return arrow.node;
  5052. if (!jsx) {
  5053. assert(!this.hasPlugin("jsx"));
  5054. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  5055. if (!typeCast.error) return typeCast.node;
  5056. }
  5057. if (jsx && jsx.node) {
  5058. this.state = jsx.failState;
  5059. return jsx.node;
  5060. }
  5061. if (arrow.node) {
  5062. this.state = arrow.failState;
  5063. return arrow.node;
  5064. }
  5065. if (typeCast && typeCast.node) {
  5066. this.state = typeCast.failState;
  5067. return typeCast.node;
  5068. }
  5069. if (jsx && jsx.thrown) throw jsx.error;
  5070. if (arrow.thrown) throw arrow.error;
  5071. if (typeCast && typeCast.thrown) throw typeCast.error;
  5072. throw jsx && jsx.error || arrow.error || typeCast && typeCast.error;
  5073. }
  5074. parseMaybeUnary(refShorthandDefaultPos) {
  5075. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  5076. return this.tsParseTypeAssertion();
  5077. } else {
  5078. return super.parseMaybeUnary(refShorthandDefaultPos);
  5079. }
  5080. }
  5081. parseArrow(node) {
  5082. if (this.match(types.colon)) {
  5083. const result = this.tryParse(abort => {
  5084. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  5085. if (this.canInsertSemicolon() || !this.match(types.arrow)) abort();
  5086. return returnType;
  5087. });
  5088. if (result.aborted) return;
  5089. if (!result.thrown) {
  5090. if (result.error) this.state = result.failState;
  5091. node.returnType = result.node;
  5092. }
  5093. }
  5094. return super.parseArrow(node);
  5095. }
  5096. parseAssignableListItemTypes(param) {
  5097. if (this.eat(types.question)) {
  5098. if (param.type !== "Identifier") {
  5099. this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  5100. }
  5101. param.optional = true;
  5102. }
  5103. const type = this.tsTryParseTypeAnnotation();
  5104. if (type) param.typeAnnotation = type;
  5105. this.resetEndLocation(param);
  5106. return param;
  5107. }
  5108. toAssignable(node, isBinding, contextDescription) {
  5109. switch (node.type) {
  5110. case "TSTypeCastExpression":
  5111. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  5112. case "TSParameterProperty":
  5113. return super.toAssignable(node, isBinding, contextDescription);
  5114. case "TSAsExpression":
  5115. case "TSNonNullExpression":
  5116. case "TSTypeAssertion":
  5117. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  5118. return node;
  5119. default:
  5120. return super.toAssignable(node, isBinding, contextDescription);
  5121. }
  5122. }
  5123. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  5124. switch (expr.type) {
  5125. case "TSTypeCastExpression":
  5126. return;
  5127. case "TSParameterProperty":
  5128. this.checkLVal(expr.parameter, bindingType, checkClashes, "parameter property");
  5129. return;
  5130. case "TSAsExpression":
  5131. case "TSNonNullExpression":
  5132. case "TSTypeAssertion":
  5133. this.checkLVal(expr.expression, bindingType, checkClashes, contextDescription);
  5134. return;
  5135. default:
  5136. super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  5137. return;
  5138. }
  5139. }
  5140. parseBindingAtom() {
  5141. switch (this.state.type) {
  5142. case types._this:
  5143. return this.parseIdentifier(true);
  5144. default:
  5145. return super.parseBindingAtom();
  5146. }
  5147. }
  5148. parseMaybeDecoratorArguments(expr) {
  5149. if (this.isRelational("<")) {
  5150. const typeArguments = this.tsParseTypeArguments();
  5151. if (this.match(types.parenL)) {
  5152. const call = super.parseMaybeDecoratorArguments(expr);
  5153. call.typeParameters = typeArguments;
  5154. return call;
  5155. }
  5156. this.unexpected(this.state.start, types.parenL);
  5157. }
  5158. return super.parseMaybeDecoratorArguments(expr);
  5159. }
  5160. isClassMethod() {
  5161. return this.isRelational("<") || super.isClassMethod();
  5162. }
  5163. isClassProperty() {
  5164. return this.match(types.bang) || this.match(types.colon) || super.isClassProperty();
  5165. }
  5166. parseMaybeDefault(...args) {
  5167. const node = super.parseMaybeDefault(...args);
  5168. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5169. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  5170. }
  5171. return node;
  5172. }
  5173. getTokenFromCode(code) {
  5174. if (this.state.inType && (code === 62 || code === 60)) {
  5175. return this.finishOp(types.relational, 1);
  5176. } else {
  5177. return super.getTokenFromCode(code);
  5178. }
  5179. }
  5180. toAssignableList(exprList) {
  5181. for (let i = 0; i < exprList.length; i++) {
  5182. const expr = exprList[i];
  5183. if (!expr) continue;
  5184. switch (expr.type) {
  5185. case "TSTypeCastExpression":
  5186. exprList[i] = this.typeCastToParameter(expr);
  5187. break;
  5188. case "TSAsExpression":
  5189. case "TSTypeAssertion":
  5190. this.raise(expr.start, "Unexpected type cast in parameter position.");
  5191. break;
  5192. }
  5193. }
  5194. return super.toAssignableList(...arguments);
  5195. }
  5196. typeCastToParameter(node) {
  5197. node.expression.typeAnnotation = node.typeAnnotation;
  5198. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  5199. return node.expression;
  5200. }
  5201. toReferencedList(exprList, isInParens) {
  5202. for (let i = 0; i < exprList.length; i++) {
  5203. const expr = exprList[i];
  5204. if (expr && expr._exprListItem && expr.type === "TsTypeCastExpression") {
  5205. this.raise(expr.start, "Did not expect a type annotation here.");
  5206. }
  5207. }
  5208. return exprList;
  5209. }
  5210. shouldParseArrow() {
  5211. return this.match(types.colon) || super.shouldParseArrow();
  5212. }
  5213. shouldParseAsyncArrow() {
  5214. return this.match(types.colon) || super.shouldParseAsyncArrow();
  5215. }
  5216. canHaveLeadingDecorator() {
  5217. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  5218. }
  5219. jsxParseOpeningElementAfterName(node) {
  5220. if (this.isRelational("<")) {
  5221. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  5222. if (typeArguments) node.typeParameters = typeArguments;
  5223. }
  5224. return super.jsxParseOpeningElementAfterName(node);
  5225. }
  5226. getGetterSetterExpectedParamCount(method) {
  5227. const baseCount = super.getGetterSetterExpectedParamCount(method);
  5228. const firstParam = method.params[0];
  5229. const hasContextParam = firstParam && firstParam.type === "Identifier" && firstParam.name === "this";
  5230. return hasContextParam ? baseCount + 1 : baseCount;
  5231. }
  5232. });
  5233. types.placeholder = new TokenType("%%", {
  5234. startsExpr: true
  5235. });
  5236. var placeholders = (superClass => class extends superClass {
  5237. parsePlaceholder(expectedNode) {
  5238. if (this.match(types.placeholder)) {
  5239. const node = this.startNode();
  5240. this.next();
  5241. this.assertNoSpace("Unexpected space in placeholder.");
  5242. node.name = super.parseIdentifier(true);
  5243. this.assertNoSpace("Unexpected space in placeholder.");
  5244. this.expect(types.placeholder);
  5245. return this.finishPlaceholder(node, expectedNode);
  5246. }
  5247. }
  5248. finishPlaceholder(node, expectedNode) {
  5249. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  5250. node.expectedNode = expectedNode;
  5251. return isFinished ? node : this.finishNode(node, "Placeholder");
  5252. }
  5253. getTokenFromCode(code) {
  5254. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  5255. return this.finishOp(types.placeholder, 2);
  5256. }
  5257. return super.getTokenFromCode(...arguments);
  5258. }
  5259. parseExprAtom() {
  5260. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  5261. }
  5262. parseIdentifier() {
  5263. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  5264. }
  5265. checkReservedWord(word) {
  5266. if (word !== undefined) super.checkReservedWord(...arguments);
  5267. }
  5268. parseBindingAtom() {
  5269. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  5270. }
  5271. checkLVal(expr) {
  5272. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  5273. }
  5274. toAssignable(node) {
  5275. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  5276. node.expectedNode = "Pattern";
  5277. return node;
  5278. }
  5279. return super.toAssignable(...arguments);
  5280. }
  5281. verifyBreakContinue(node) {
  5282. if (node.label && node.label.type === "Placeholder") return;
  5283. super.verifyBreakContinue(...arguments);
  5284. }
  5285. parseExpressionStatement(node, expr) {
  5286. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  5287. return super.parseExpressionStatement(...arguments);
  5288. }
  5289. if (this.match(types.colon)) {
  5290. const stmt = node;
  5291. stmt.label = this.finishPlaceholder(expr, "Identifier");
  5292. this.next();
  5293. stmt.body = this.parseStatement("label");
  5294. return this.finishNode(stmt, "LabeledStatement");
  5295. }
  5296. this.semicolon();
  5297. node.name = expr.name;
  5298. return this.finishPlaceholder(node, "Statement");
  5299. }
  5300. parseBlock() {
  5301. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  5302. }
  5303. parseFunctionId() {
  5304. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  5305. }
  5306. parseClass(node, isStatement, optionalId) {
  5307. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  5308. this.next();
  5309. this.takeDecorators(node);
  5310. const placeholder = this.parsePlaceholder("Identifier");
  5311. if (placeholder) {
  5312. if (this.match(types._extends) || this.match(types.placeholder) || this.match(types.braceL)) {
  5313. node.id = placeholder;
  5314. } else if (optionalId || !isStatement) {
  5315. node.id = null;
  5316. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  5317. return this.finishNode(node, type);
  5318. } else {
  5319. this.unexpected(null, "A class name is required");
  5320. }
  5321. } else {
  5322. this.parseClassId(node, isStatement, optionalId);
  5323. }
  5324. this.parseClassSuper(node);
  5325. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass);
  5326. return this.finishNode(node, type);
  5327. }
  5328. parseExport(node) {
  5329. const placeholder = this.parsePlaceholder("Identifier");
  5330. if (!placeholder) return super.parseExport(...arguments);
  5331. if (!this.isContextual("from") && !this.match(types.comma)) {
  5332. node.specifiers = [];
  5333. node.source = null;
  5334. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  5335. return this.finishNode(node, "ExportNamedDeclaration");
  5336. }
  5337. this.expectPlugin("exportDefaultFrom");
  5338. const specifier = this.startNode();
  5339. specifier.exported = placeholder;
  5340. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  5341. return super.parseExport(node);
  5342. }
  5343. maybeParseExportDefaultSpecifier(node) {
  5344. if (node.specifiers && node.specifiers.length > 0) {
  5345. return true;
  5346. }
  5347. return super.maybeParseExportDefaultSpecifier(...arguments);
  5348. }
  5349. checkExport(node) {
  5350. const {
  5351. specifiers
  5352. } = node;
  5353. if (specifiers && specifiers.length) {
  5354. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  5355. }
  5356. super.checkExport(node);
  5357. node.specifiers = specifiers;
  5358. }
  5359. parseImport(node) {
  5360. const placeholder = this.parsePlaceholder("Identifier");
  5361. if (!placeholder) return super.parseImport(...arguments);
  5362. node.specifiers = [];
  5363. if (!this.isContextual("from") && !this.match(types.comma)) {
  5364. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  5365. this.semicolon();
  5366. return this.finishNode(node, "ImportDeclaration");
  5367. }
  5368. const specifier = this.startNodeAtNode(placeholder);
  5369. specifier.local = placeholder;
  5370. this.finishNode(specifier, "ImportDefaultSpecifier");
  5371. node.specifiers.push(specifier);
  5372. if (this.eat(types.comma)) {
  5373. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  5374. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  5375. }
  5376. this.expectContextual("from");
  5377. node.source = this.parseImportSource();
  5378. this.semicolon();
  5379. return this.finishNode(node, "ImportDeclaration");
  5380. }
  5381. parseImportSource() {
  5382. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  5383. }
  5384. });
  5385. var v8intrinsic = (superClass => class extends superClass {
  5386. parseV8Intrinsic() {
  5387. if (this.match(types.modulo)) {
  5388. const v8IntrinsicStart = this.state.start;
  5389. const node = this.startNode();
  5390. this.eat(types.modulo);
  5391. if (this.match(types.name)) {
  5392. const name = this.parseIdentifierName(this.state.start);
  5393. const identifier = this.createIdentifier(node, name);
  5394. identifier.type = "V8IntrinsicIdentifier";
  5395. if (this.match(types.parenL)) {
  5396. return identifier;
  5397. }
  5398. }
  5399. this.unexpected(v8IntrinsicStart);
  5400. }
  5401. }
  5402. parseExprAtom() {
  5403. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  5404. }
  5405. });
  5406. function hasPlugin(plugins, name) {
  5407. return plugins.some(plugin => {
  5408. if (Array.isArray(plugin)) {
  5409. return plugin[0] === name;
  5410. } else {
  5411. return plugin === name;
  5412. }
  5413. });
  5414. }
  5415. function getPluginOption(plugins, name, option) {
  5416. const plugin = plugins.find(plugin => {
  5417. if (Array.isArray(plugin)) {
  5418. return plugin[0] === name;
  5419. } else {
  5420. return plugin === name;
  5421. }
  5422. });
  5423. if (plugin && Array.isArray(plugin)) {
  5424. return plugin[1][option];
  5425. }
  5426. return null;
  5427. }
  5428. const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
  5429. function validatePlugins(plugins) {
  5430. if (hasPlugin(plugins, "decorators")) {
  5431. if (hasPlugin(plugins, "decorators-legacy")) {
  5432. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  5433. }
  5434. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  5435. if (decoratorsBeforeExport == null) {
  5436. 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'.");
  5437. } else if (typeof decoratorsBeforeExport !== "boolean") {
  5438. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  5439. }
  5440. }
  5441. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  5442. throw new Error("Cannot combine flow and typescript plugins.");
  5443. }
  5444. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  5445. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  5446. }
  5447. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  5448. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  5449. }
  5450. }
  5451. const mixinPlugins = {
  5452. estree,
  5453. jsx,
  5454. flow,
  5455. typescript,
  5456. v8intrinsic,
  5457. placeholders
  5458. };
  5459. const mixinPluginNames = Object.keys(mixinPlugins);
  5460. const defaultOptions = {
  5461. sourceType: "script",
  5462. sourceFilename: undefined,
  5463. startLine: 1,
  5464. allowAwaitOutsideFunction: false,
  5465. allowReturnOutsideFunction: false,
  5466. allowImportExportEverywhere: false,
  5467. allowSuperOutsideMethod: false,
  5468. allowUndeclaredExports: false,
  5469. plugins: [],
  5470. strictMode: null,
  5471. ranges: false,
  5472. tokens: false,
  5473. createParenthesizedExpressions: false,
  5474. errorRecovery: false
  5475. };
  5476. function getOptions(opts) {
  5477. const options = {};
  5478. for (let _i = 0, _Object$keys = Object.keys(defaultOptions); _i < _Object$keys.length; _i++) {
  5479. const key = _Object$keys[_i];
  5480. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  5481. }
  5482. return options;
  5483. }
  5484. class Position {
  5485. constructor(line, col) {
  5486. this.line = line;
  5487. this.column = col;
  5488. }
  5489. }
  5490. class SourceLocation {
  5491. constructor(start, end) {
  5492. this.start = start;
  5493. this.end = end;
  5494. }
  5495. }
  5496. function getLineInfo(input, offset) {
  5497. let line = 1;
  5498. let lineStart = 0;
  5499. let match;
  5500. lineBreakG.lastIndex = 0;
  5501. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  5502. line++;
  5503. lineStart = lineBreakG.lastIndex;
  5504. }
  5505. return new Position(line, offset - lineStart);
  5506. }
  5507. class BaseParser {
  5508. constructor() {
  5509. this.sawUnambiguousESM = false;
  5510. this.ambiguousScriptDifferentAst = false;
  5511. }
  5512. hasPlugin(name) {
  5513. return this.plugins.has(name);
  5514. }
  5515. getPluginOption(plugin, name) {
  5516. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  5517. }
  5518. }
  5519. function last(stack) {
  5520. return stack[stack.length - 1];
  5521. }
  5522. class CommentsParser extends BaseParser {
  5523. addComment(comment) {
  5524. if (this.filename) comment.loc.filename = this.filename;
  5525. this.state.trailingComments.push(comment);
  5526. this.state.leadingComments.push(comment);
  5527. }
  5528. adjustCommentsAfterTrailingComma(node, elements, takeAllComments) {
  5529. if (this.state.leadingComments.length === 0) {
  5530. return;
  5531. }
  5532. let lastElement = null;
  5533. let i = elements.length;
  5534. while (lastElement === null && i > 0) {
  5535. lastElement = elements[--i];
  5536. }
  5537. if (lastElement === null) {
  5538. return;
  5539. }
  5540. for (let j = 0; j < this.state.leadingComments.length; j++) {
  5541. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5542. this.state.leadingComments.splice(j, 1);
  5543. j--;
  5544. }
  5545. }
  5546. const newTrailingComments = [];
  5547. for (let i = 0; i < this.state.leadingComments.length; i++) {
  5548. const leadingComment = this.state.leadingComments[i];
  5549. if (leadingComment.end < node.end) {
  5550. newTrailingComments.push(leadingComment);
  5551. if (!takeAllComments) {
  5552. this.state.leadingComments.splice(i, 1);
  5553. i--;
  5554. }
  5555. } else {
  5556. if (node.trailingComments === undefined) {
  5557. node.trailingComments = [];
  5558. }
  5559. node.trailingComments.push(leadingComment);
  5560. }
  5561. }
  5562. if (takeAllComments) this.state.leadingComments = [];
  5563. if (newTrailingComments.length > 0) {
  5564. lastElement.trailingComments = newTrailingComments;
  5565. } else if (lastElement.trailingComments !== undefined) {
  5566. lastElement.trailingComments = [];
  5567. }
  5568. }
  5569. processComment(node) {
  5570. if (node.type === "Program" && node.body.length > 0) return;
  5571. const stack = this.state.commentStack;
  5572. let firstChild, lastChild, trailingComments, i, j;
  5573. if (this.state.trailingComments.length > 0) {
  5574. if (this.state.trailingComments[0].start >= node.end) {
  5575. trailingComments = this.state.trailingComments;
  5576. this.state.trailingComments = [];
  5577. } else {
  5578. this.state.trailingComments.length = 0;
  5579. }
  5580. } else if (stack.length > 0) {
  5581. const lastInStack = last(stack);
  5582. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  5583. trailingComments = lastInStack.trailingComments;
  5584. delete lastInStack.trailingComments;
  5585. }
  5586. }
  5587. if (stack.length > 0 && last(stack).start >= node.start) {
  5588. firstChild = stack.pop();
  5589. }
  5590. while (stack.length > 0 && last(stack).start >= node.start) {
  5591. lastChild = stack.pop();
  5592. }
  5593. if (!lastChild && firstChild) lastChild = firstChild;
  5594. if (firstChild) {
  5595. switch (node.type) {
  5596. case "ObjectExpression":
  5597. this.adjustCommentsAfterTrailingComma(node, node.properties);
  5598. break;
  5599. case "ObjectPattern":
  5600. this.adjustCommentsAfterTrailingComma(node, node.properties, true);
  5601. break;
  5602. case "CallExpression":
  5603. this.adjustCommentsAfterTrailingComma(node, node.arguments);
  5604. break;
  5605. case "ArrayExpression":
  5606. this.adjustCommentsAfterTrailingComma(node, node.elements);
  5607. break;
  5608. case "ArrayPattern":
  5609. this.adjustCommentsAfterTrailingComma(node, node.elements, true);
  5610. break;
  5611. }
  5612. } else if (this.state.commentPreviousNode && (this.state.commentPreviousNode.type === "ImportSpecifier" && node.type !== "ImportSpecifier" || this.state.commentPreviousNode.type === "ExportSpecifier" && node.type !== "ExportSpecifier")) {
  5613. this.adjustCommentsAfterTrailingComma(node, [this.state.commentPreviousNode], true);
  5614. }
  5615. if (lastChild) {
  5616. if (lastChild.leadingComments) {
  5617. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  5618. node.leadingComments = lastChild.leadingComments;
  5619. delete lastChild.leadingComments;
  5620. } else {
  5621. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  5622. if (lastChild.leadingComments[i].end <= node.start) {
  5623. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  5624. break;
  5625. }
  5626. }
  5627. }
  5628. }
  5629. } else if (this.state.leadingComments.length > 0) {
  5630. if (last(this.state.leadingComments).end <= node.start) {
  5631. if (this.state.commentPreviousNode) {
  5632. for (j = 0; j < this.state.leadingComments.length; j++) {
  5633. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  5634. this.state.leadingComments.splice(j, 1);
  5635. j--;
  5636. }
  5637. }
  5638. }
  5639. if (this.state.leadingComments.length > 0) {
  5640. node.leadingComments = this.state.leadingComments;
  5641. this.state.leadingComments = [];
  5642. }
  5643. } else {
  5644. for (i = 0; i < this.state.leadingComments.length; i++) {
  5645. if (this.state.leadingComments[i].end > node.start) {
  5646. break;
  5647. }
  5648. }
  5649. const leadingComments = this.state.leadingComments.slice(0, i);
  5650. if (leadingComments.length) {
  5651. node.leadingComments = leadingComments;
  5652. }
  5653. trailingComments = this.state.leadingComments.slice(i);
  5654. if (trailingComments.length === 0) {
  5655. trailingComments = null;
  5656. }
  5657. }
  5658. }
  5659. this.state.commentPreviousNode = node;
  5660. if (trailingComments) {
  5661. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  5662. node.innerComments = trailingComments;
  5663. } else {
  5664. node.trailingComments = trailingComments;
  5665. }
  5666. }
  5667. stack.push(node);
  5668. }
  5669. }
  5670. class LocationParser extends CommentsParser {
  5671. getLocationForPosition(pos) {
  5672. let loc;
  5673. 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);
  5674. return loc;
  5675. }
  5676. raise(pos, message, {
  5677. missingPluginNames,
  5678. code
  5679. } = {}) {
  5680. const loc = this.getLocationForPosition(pos);
  5681. message += ` (${loc.line}:${loc.column})`;
  5682. const err = new SyntaxError(message);
  5683. err.pos = pos;
  5684. err.loc = loc;
  5685. if (missingPluginNames) {
  5686. err.missingPlugin = missingPluginNames;
  5687. }
  5688. if (code !== undefined) {
  5689. err.code = code;
  5690. }
  5691. if (this.options.errorRecovery) {
  5692. if (!this.isLookahead) this.state.errors.push(err);
  5693. return err;
  5694. } else {
  5695. throw err;
  5696. }
  5697. }
  5698. }
  5699. class State {
  5700. constructor() {
  5701. this.errors = [];
  5702. this.potentialArrowAt = -1;
  5703. this.noArrowAt = [];
  5704. this.noArrowParamsConversionAt = [];
  5705. this.inParameters = false;
  5706. this.maybeInArrowParameters = false;
  5707. this.inPipeline = false;
  5708. this.inType = false;
  5709. this.noAnonFunctionType = false;
  5710. this.inPropertyName = false;
  5711. this.inClassProperty = false;
  5712. this.hasFlowComment = false;
  5713. this.isIterator = false;
  5714. this.topicContext = {
  5715. maxNumOfResolvableTopics: 0,
  5716. maxTopicIndex: null
  5717. };
  5718. this.soloAwait = false;
  5719. this.inFSharpPipelineDirectBody = false;
  5720. this.classLevel = 0;
  5721. this.labels = [];
  5722. this.decoratorStack = [[]];
  5723. this.yieldPos = -1;
  5724. this.awaitPos = -1;
  5725. this.tokens = [];
  5726. this.comments = [];
  5727. this.trailingComments = [];
  5728. this.leadingComments = [];
  5729. this.commentStack = [];
  5730. this.commentPreviousNode = null;
  5731. this.pos = 0;
  5732. this.lineStart = 0;
  5733. this.type = types.eof;
  5734. this.value = null;
  5735. this.start = 0;
  5736. this.end = 0;
  5737. this.lastTokEndLoc = null;
  5738. this.lastTokStartLoc = null;
  5739. this.lastTokStart = 0;
  5740. this.lastTokEnd = 0;
  5741. this.context = [types$1.braceStatement];
  5742. this.exprAllowed = true;
  5743. this.containsEsc = false;
  5744. this.containsOctal = false;
  5745. this.octalPosition = null;
  5746. this.exportedIdentifiers = [];
  5747. this.invalidTemplateEscapePosition = null;
  5748. }
  5749. init(options) {
  5750. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  5751. this.curLine = options.startLine;
  5752. this.startLoc = this.endLoc = this.curPosition();
  5753. }
  5754. curPosition() {
  5755. return new Position(this.curLine, this.pos - this.lineStart);
  5756. }
  5757. clone(skipArrays) {
  5758. const state = new State();
  5759. const keys = Object.keys(this);
  5760. for (let i = 0, length = keys.length; i < length; i++) {
  5761. const key = keys[i];
  5762. let val = this[key];
  5763. if (!skipArrays && Array.isArray(val)) {
  5764. val = val.slice();
  5765. }
  5766. state[key] = val;
  5767. }
  5768. return state;
  5769. }
  5770. }
  5771. var _isDigit = function isDigit(code) {
  5772. return code >= 48 && code <= 57;
  5773. };
  5774. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
  5775. const forbiddenNumericSeparatorSiblings = {
  5776. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  5777. hex: [46, 88, 95, 120]
  5778. };
  5779. const allowedNumericSeparatorSiblings = {};
  5780. allowedNumericSeparatorSiblings.bin = [48, 49];
  5781. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  5782. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  5783. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  5784. class Token {
  5785. constructor(state) {
  5786. this.type = state.type;
  5787. this.value = state.value;
  5788. this.start = state.start;
  5789. this.end = state.end;
  5790. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  5791. }
  5792. }
  5793. class Tokenizer extends LocationParser {
  5794. constructor(options, input) {
  5795. super();
  5796. this.state = new State();
  5797. this.state.init(options);
  5798. this.input = input;
  5799. this.length = input.length;
  5800. this.isLookahead = false;
  5801. }
  5802. next() {
  5803. if (!this.isLookahead) {
  5804. this.checkKeywordEscapes();
  5805. if (this.options.tokens) {
  5806. this.state.tokens.push(new Token(this.state));
  5807. }
  5808. }
  5809. this.state.lastTokEnd = this.state.end;
  5810. this.state.lastTokStart = this.state.start;
  5811. this.state.lastTokEndLoc = this.state.endLoc;
  5812. this.state.lastTokStartLoc = this.state.startLoc;
  5813. this.nextToken();
  5814. }
  5815. eat(type) {
  5816. if (this.match(type)) {
  5817. this.next();
  5818. return true;
  5819. } else {
  5820. return false;
  5821. }
  5822. }
  5823. match(type) {
  5824. return this.state.type === type;
  5825. }
  5826. lookahead() {
  5827. const old = this.state;
  5828. this.state = old.clone(true);
  5829. this.isLookahead = true;
  5830. this.next();
  5831. this.isLookahead = false;
  5832. const curr = this.state;
  5833. this.state = old;
  5834. return curr;
  5835. }
  5836. nextTokenStart() {
  5837. const thisTokEnd = this.state.pos;
  5838. skipWhiteSpace.lastIndex = thisTokEnd;
  5839. const skip = skipWhiteSpace.exec(this.input);
  5840. return thisTokEnd + skip[0].length;
  5841. }
  5842. lookaheadCharCode() {
  5843. return this.input.charCodeAt(this.nextTokenStart());
  5844. }
  5845. setStrict(strict) {
  5846. this.state.strict = strict;
  5847. if (!this.match(types.num) && !this.match(types.string)) return;
  5848. this.state.pos = this.state.start;
  5849. while (this.state.pos < this.state.lineStart) {
  5850. this.state.lineStart = this.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
  5851. --this.state.curLine;
  5852. }
  5853. this.nextToken();
  5854. }
  5855. curContext() {
  5856. return this.state.context[this.state.context.length - 1];
  5857. }
  5858. nextToken() {
  5859. const curContext = this.curContext();
  5860. if (!curContext || !curContext.preserveSpace) this.skipSpace();
  5861. this.state.containsOctal = false;
  5862. this.state.octalPosition = null;
  5863. this.state.start = this.state.pos;
  5864. this.state.startLoc = this.state.curPosition();
  5865. if (this.state.pos >= this.length) {
  5866. this.finishToken(types.eof);
  5867. return;
  5868. }
  5869. if (curContext.override) {
  5870. curContext.override(this);
  5871. } else {
  5872. this.getTokenFromCode(this.input.codePointAt(this.state.pos));
  5873. }
  5874. }
  5875. pushComment(block, text, start, end, startLoc, endLoc) {
  5876. const comment = {
  5877. type: block ? "CommentBlock" : "CommentLine",
  5878. value: text,
  5879. start: start,
  5880. end: end,
  5881. loc: new SourceLocation(startLoc, endLoc)
  5882. };
  5883. if (this.options.tokens) this.state.tokens.push(comment);
  5884. this.state.comments.push(comment);
  5885. this.addComment(comment);
  5886. }
  5887. skipBlockComment() {
  5888. const startLoc = this.state.curPosition();
  5889. const start = this.state.pos;
  5890. const end = this.input.indexOf("*/", this.state.pos + 2);
  5891. if (end === -1) throw this.raise(start, "Unterminated comment");
  5892. this.state.pos = end + 2;
  5893. lineBreakG.lastIndex = start;
  5894. let match;
  5895. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  5896. ++this.state.curLine;
  5897. this.state.lineStart = match.index + match[0].length;
  5898. }
  5899. if (this.isLookahead) return;
  5900. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  5901. }
  5902. skipLineComment(startSkip) {
  5903. const start = this.state.pos;
  5904. const startLoc = this.state.curPosition();
  5905. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  5906. if (this.state.pos < this.length) {
  5907. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  5908. ch = this.input.charCodeAt(this.state.pos);
  5909. }
  5910. }
  5911. if (this.isLookahead) return;
  5912. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  5913. }
  5914. skipSpace() {
  5915. loop: while (this.state.pos < this.length) {
  5916. const ch = this.input.charCodeAt(this.state.pos);
  5917. switch (ch) {
  5918. case 32:
  5919. case 160:
  5920. case 9:
  5921. ++this.state.pos;
  5922. break;
  5923. case 13:
  5924. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  5925. ++this.state.pos;
  5926. }
  5927. case 10:
  5928. case 8232:
  5929. case 8233:
  5930. ++this.state.pos;
  5931. ++this.state.curLine;
  5932. this.state.lineStart = this.state.pos;
  5933. break;
  5934. case 47:
  5935. switch (this.input.charCodeAt(this.state.pos + 1)) {
  5936. case 42:
  5937. this.skipBlockComment();
  5938. break;
  5939. case 47:
  5940. this.skipLineComment(2);
  5941. break;
  5942. default:
  5943. break loop;
  5944. }
  5945. break;
  5946. default:
  5947. if (isWhitespace(ch)) {
  5948. ++this.state.pos;
  5949. } else {
  5950. break loop;
  5951. }
  5952. }
  5953. }
  5954. }
  5955. finishToken(type, val) {
  5956. this.state.end = this.state.pos;
  5957. this.state.endLoc = this.state.curPosition();
  5958. const prevType = this.state.type;
  5959. this.state.type = type;
  5960. this.state.value = val;
  5961. if (!this.isLookahead) this.updateContext(prevType);
  5962. }
  5963. readToken_numberSign() {
  5964. if (this.state.pos === 0 && this.readToken_interpreter()) {
  5965. return;
  5966. }
  5967. const nextPos = this.state.pos + 1;
  5968. const next = this.input.charCodeAt(nextPos);
  5969. if (next >= 48 && next <= 57) {
  5970. throw this.raise(this.state.pos, "Unexpected digit after hash token");
  5971. }
  5972. if ((this.hasPlugin("classPrivateProperties") || this.hasPlugin("classPrivateMethods")) && this.state.classLevel > 0) {
  5973. ++this.state.pos;
  5974. this.finishToken(types.hash);
  5975. return;
  5976. } else if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  5977. this.finishOp(types.hash, 1);
  5978. } else {
  5979. throw this.raise(this.state.pos, "Unexpected character '#'");
  5980. }
  5981. }
  5982. readToken_dot() {
  5983. const next = this.input.charCodeAt(this.state.pos + 1);
  5984. if (next >= 48 && next <= 57) {
  5985. this.readNumber(true);
  5986. return;
  5987. }
  5988. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  5989. this.state.pos += 3;
  5990. this.finishToken(types.ellipsis);
  5991. } else {
  5992. ++this.state.pos;
  5993. this.finishToken(types.dot);
  5994. }
  5995. }
  5996. readToken_slash() {
  5997. if (this.state.exprAllowed && !this.state.inType) {
  5998. ++this.state.pos;
  5999. this.readRegexp();
  6000. return;
  6001. }
  6002. const next = this.input.charCodeAt(this.state.pos + 1);
  6003. if (next === 61) {
  6004. this.finishOp(types.assign, 2);
  6005. } else {
  6006. this.finishOp(types.slash, 1);
  6007. }
  6008. }
  6009. readToken_interpreter() {
  6010. if (this.state.pos !== 0 || this.length < 2) return false;
  6011. const start = this.state.pos;
  6012. this.state.pos += 1;
  6013. let ch = this.input.charCodeAt(this.state.pos);
  6014. if (ch !== 33) return false;
  6015. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  6016. ch = this.input.charCodeAt(this.state.pos);
  6017. }
  6018. const value = this.input.slice(start + 2, this.state.pos);
  6019. this.finishToken(types.interpreterDirective, value);
  6020. return true;
  6021. }
  6022. readToken_mult_modulo(code) {
  6023. let type = code === 42 ? types.star : types.modulo;
  6024. let width = 1;
  6025. let next = this.input.charCodeAt(this.state.pos + 1);
  6026. const exprAllowed = this.state.exprAllowed;
  6027. if (code === 42 && next === 42) {
  6028. width++;
  6029. next = this.input.charCodeAt(this.state.pos + 2);
  6030. type = types.exponent;
  6031. }
  6032. if (next === 61 && !exprAllowed) {
  6033. width++;
  6034. type = types.assign;
  6035. }
  6036. this.finishOp(type, width);
  6037. }
  6038. readToken_pipe_amp(code) {
  6039. const next = this.input.charCodeAt(this.state.pos + 1);
  6040. if (next === code) {
  6041. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  6042. this.finishOp(types.assign, 3);
  6043. } else {
  6044. this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2);
  6045. }
  6046. return;
  6047. }
  6048. if (code === 124) {
  6049. if (next === 62) {
  6050. this.finishOp(types.pipeline, 2);
  6051. return;
  6052. }
  6053. }
  6054. if (next === 61) {
  6055. this.finishOp(types.assign, 2);
  6056. return;
  6057. }
  6058. this.finishOp(code === 124 ? types.bitwiseOR : types.bitwiseAND, 1);
  6059. }
  6060. readToken_caret() {
  6061. const next = this.input.charCodeAt(this.state.pos + 1);
  6062. if (next === 61) {
  6063. this.finishOp(types.assign, 2);
  6064. } else {
  6065. this.finishOp(types.bitwiseXOR, 1);
  6066. }
  6067. }
  6068. readToken_plus_min(code) {
  6069. const next = this.input.charCodeAt(this.state.pos + 1);
  6070. if (next === code) {
  6071. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && (this.state.lastTokEnd === 0 || lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.pos)))) {
  6072. this.skipLineComment(3);
  6073. this.skipSpace();
  6074. this.nextToken();
  6075. return;
  6076. }
  6077. this.finishOp(types.incDec, 2);
  6078. return;
  6079. }
  6080. if (next === 61) {
  6081. this.finishOp(types.assign, 2);
  6082. } else {
  6083. this.finishOp(types.plusMin, 1);
  6084. }
  6085. }
  6086. readToken_lt_gt(code) {
  6087. const next = this.input.charCodeAt(this.state.pos + 1);
  6088. let size = 1;
  6089. if (next === code) {
  6090. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  6091. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  6092. this.finishOp(types.assign, size + 1);
  6093. return;
  6094. }
  6095. this.finishOp(types.bitShift, size);
  6096. return;
  6097. }
  6098. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  6099. this.skipLineComment(4);
  6100. this.skipSpace();
  6101. this.nextToken();
  6102. return;
  6103. }
  6104. if (next === 61) {
  6105. size = 2;
  6106. }
  6107. this.finishOp(types.relational, size);
  6108. }
  6109. readToken_eq_excl(code) {
  6110. const next = this.input.charCodeAt(this.state.pos + 1);
  6111. if (next === 61) {
  6112. this.finishOp(types.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  6113. return;
  6114. }
  6115. if (code === 61 && next === 62) {
  6116. this.state.pos += 2;
  6117. this.finishToken(types.arrow);
  6118. return;
  6119. }
  6120. this.finishOp(code === 61 ? types.eq : types.bang, 1);
  6121. }
  6122. readToken_question() {
  6123. const next = this.input.charCodeAt(this.state.pos + 1);
  6124. const next2 = this.input.charCodeAt(this.state.pos + 2);
  6125. if (next === 63 && !this.state.inType) {
  6126. if (next2 === 61) {
  6127. this.finishOp(types.assign, 3);
  6128. } else {
  6129. this.finishOp(types.nullishCoalescing, 2);
  6130. }
  6131. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  6132. this.state.pos += 2;
  6133. this.finishToken(types.questionDot);
  6134. } else {
  6135. ++this.state.pos;
  6136. this.finishToken(types.question);
  6137. }
  6138. }
  6139. getTokenFromCode(code) {
  6140. switch (code) {
  6141. case 46:
  6142. this.readToken_dot();
  6143. return;
  6144. case 40:
  6145. ++this.state.pos;
  6146. this.finishToken(types.parenL);
  6147. return;
  6148. case 41:
  6149. ++this.state.pos;
  6150. this.finishToken(types.parenR);
  6151. return;
  6152. case 59:
  6153. ++this.state.pos;
  6154. this.finishToken(types.semi);
  6155. return;
  6156. case 44:
  6157. ++this.state.pos;
  6158. this.finishToken(types.comma);
  6159. return;
  6160. case 91:
  6161. ++this.state.pos;
  6162. this.finishToken(types.bracketL);
  6163. return;
  6164. case 93:
  6165. ++this.state.pos;
  6166. this.finishToken(types.bracketR);
  6167. return;
  6168. case 123:
  6169. ++this.state.pos;
  6170. this.finishToken(types.braceL);
  6171. return;
  6172. case 125:
  6173. ++this.state.pos;
  6174. this.finishToken(types.braceR);
  6175. return;
  6176. case 58:
  6177. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  6178. this.finishOp(types.doubleColon, 2);
  6179. } else {
  6180. ++this.state.pos;
  6181. this.finishToken(types.colon);
  6182. }
  6183. return;
  6184. case 63:
  6185. this.readToken_question();
  6186. return;
  6187. case 96:
  6188. ++this.state.pos;
  6189. this.finishToken(types.backQuote);
  6190. return;
  6191. case 48:
  6192. {
  6193. const next = this.input.charCodeAt(this.state.pos + 1);
  6194. if (next === 120 || next === 88) {
  6195. this.readRadixNumber(16);
  6196. return;
  6197. }
  6198. if (next === 111 || next === 79) {
  6199. this.readRadixNumber(8);
  6200. return;
  6201. }
  6202. if (next === 98 || next === 66) {
  6203. this.readRadixNumber(2);
  6204. return;
  6205. }
  6206. }
  6207. case 49:
  6208. case 50:
  6209. case 51:
  6210. case 52:
  6211. case 53:
  6212. case 54:
  6213. case 55:
  6214. case 56:
  6215. case 57:
  6216. this.readNumber(false);
  6217. return;
  6218. case 34:
  6219. case 39:
  6220. this.readString(code);
  6221. return;
  6222. case 47:
  6223. this.readToken_slash();
  6224. return;
  6225. case 37:
  6226. case 42:
  6227. this.readToken_mult_modulo(code);
  6228. return;
  6229. case 124:
  6230. case 38:
  6231. this.readToken_pipe_amp(code);
  6232. return;
  6233. case 94:
  6234. this.readToken_caret();
  6235. return;
  6236. case 43:
  6237. case 45:
  6238. this.readToken_plus_min(code);
  6239. return;
  6240. case 60:
  6241. case 62:
  6242. this.readToken_lt_gt(code);
  6243. return;
  6244. case 61:
  6245. case 33:
  6246. this.readToken_eq_excl(code);
  6247. return;
  6248. case 126:
  6249. this.finishOp(types.tilde, 1);
  6250. return;
  6251. case 64:
  6252. ++this.state.pos;
  6253. this.finishToken(types.at);
  6254. return;
  6255. case 35:
  6256. this.readToken_numberSign();
  6257. return;
  6258. case 92:
  6259. this.readWord();
  6260. return;
  6261. default:
  6262. if (isIdentifierStart(code)) {
  6263. this.readWord();
  6264. return;
  6265. }
  6266. }
  6267. throw this.raise(this.state.pos, `Unexpected character '${String.fromCodePoint(code)}'`);
  6268. }
  6269. finishOp(type, size) {
  6270. const str = this.input.slice(this.state.pos, this.state.pos + size);
  6271. this.state.pos += size;
  6272. this.finishToken(type, str);
  6273. }
  6274. readRegexp() {
  6275. const start = this.state.pos;
  6276. let escaped, inClass;
  6277. for (;;) {
  6278. if (this.state.pos >= this.length) {
  6279. throw this.raise(start, "Unterminated regular expression");
  6280. }
  6281. const ch = this.input.charAt(this.state.pos);
  6282. if (lineBreak.test(ch)) {
  6283. throw this.raise(start, "Unterminated regular expression");
  6284. }
  6285. if (escaped) {
  6286. escaped = false;
  6287. } else {
  6288. if (ch === "[") {
  6289. inClass = true;
  6290. } else if (ch === "]" && inClass) {
  6291. inClass = false;
  6292. } else if (ch === "/" && !inClass) {
  6293. break;
  6294. }
  6295. escaped = ch === "\\";
  6296. }
  6297. ++this.state.pos;
  6298. }
  6299. const content = this.input.slice(start, this.state.pos);
  6300. ++this.state.pos;
  6301. let mods = "";
  6302. while (this.state.pos < this.length) {
  6303. const char = this.input[this.state.pos];
  6304. const charCode = this.input.codePointAt(this.state.pos);
  6305. if (VALID_REGEX_FLAGS.has(char)) {
  6306. if (mods.indexOf(char) > -1) {
  6307. this.raise(this.state.pos + 1, "Duplicate regular expression flag");
  6308. }
  6309. } else if (isIdentifierChar(charCode) || charCode === 92) {
  6310. this.raise(this.state.pos + 1, "Invalid regular expression flag");
  6311. } else {
  6312. break;
  6313. }
  6314. ++this.state.pos;
  6315. mods += char;
  6316. }
  6317. this.finishToken(types.regexp, {
  6318. pattern: content,
  6319. flags: mods
  6320. });
  6321. }
  6322. readInt(radix, len, forceLen, allowNumSeparator = true) {
  6323. const start = this.state.pos;
  6324. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  6325. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  6326. let invalid = false;
  6327. let total = 0;
  6328. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  6329. const code = this.input.charCodeAt(this.state.pos);
  6330. let val;
  6331. if (this.hasPlugin("numericSeparator")) {
  6332. if (code === 95) {
  6333. const prev = this.input.charCodeAt(this.state.pos - 1);
  6334. const next = this.input.charCodeAt(this.state.pos + 1);
  6335. if (allowedSiblings.indexOf(next) === -1) {
  6336. this.raise(this.state.pos, "A numeric separator is only allowed between two digits");
  6337. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  6338. this.raise(this.state.pos, "A numeric separator is only allowed between two digits");
  6339. }
  6340. if (!allowNumSeparator) {
  6341. this.raise(this.state.pos, "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences");
  6342. }
  6343. ++this.state.pos;
  6344. continue;
  6345. }
  6346. }
  6347. if (code >= 97) {
  6348. val = code - 97 + 10;
  6349. } else if (code >= 65) {
  6350. val = code - 65 + 10;
  6351. } else if (_isDigit(code)) {
  6352. val = code - 48;
  6353. } else {
  6354. val = Infinity;
  6355. }
  6356. if (val >= radix) {
  6357. if (this.options.errorRecovery && val <= 9) {
  6358. val = 0;
  6359. this.raise(this.state.start + i + 2, "Expected number in radix " + radix);
  6360. } else if (forceLen) {
  6361. val = 0;
  6362. invalid = true;
  6363. } else {
  6364. break;
  6365. }
  6366. }
  6367. ++this.state.pos;
  6368. total = total * radix + val;
  6369. }
  6370. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  6371. return null;
  6372. }
  6373. return total;
  6374. }
  6375. readRadixNumber(radix) {
  6376. const start = this.state.pos;
  6377. let isBigInt = false;
  6378. this.state.pos += 2;
  6379. const val = this.readInt(radix);
  6380. if (val == null) {
  6381. this.raise(this.state.start + 2, "Expected number in radix " + radix);
  6382. }
  6383. if (this.hasPlugin("bigInt")) {
  6384. if (this.input.charCodeAt(this.state.pos) === 110) {
  6385. ++this.state.pos;
  6386. isBigInt = true;
  6387. }
  6388. }
  6389. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  6390. throw this.raise(this.state.pos, "Identifier directly after number");
  6391. }
  6392. if (isBigInt) {
  6393. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  6394. this.finishToken(types.bigint, str);
  6395. return;
  6396. }
  6397. this.finishToken(types.num, val);
  6398. }
  6399. readNumber(startsWithDot) {
  6400. const start = this.state.pos;
  6401. let isFloat = false;
  6402. let isBigInt = false;
  6403. let isNonOctalDecimalInt = false;
  6404. if (!startsWithDot && this.readInt(10) === null) {
  6405. this.raise(start, "Invalid number");
  6406. }
  6407. let octal = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  6408. if (octal) {
  6409. if (this.state.strict) {
  6410. this.raise(start, "Legacy octal literals are not allowed in strict mode");
  6411. }
  6412. if (/[89]/.test(this.input.slice(start, this.state.pos))) {
  6413. octal = false;
  6414. isNonOctalDecimalInt = true;
  6415. }
  6416. }
  6417. let next = this.input.charCodeAt(this.state.pos);
  6418. if (next === 46 && !octal) {
  6419. ++this.state.pos;
  6420. this.readInt(10);
  6421. isFloat = true;
  6422. next = this.input.charCodeAt(this.state.pos);
  6423. }
  6424. if ((next === 69 || next === 101) && !octal) {
  6425. next = this.input.charCodeAt(++this.state.pos);
  6426. if (next === 43 || next === 45) {
  6427. ++this.state.pos;
  6428. }
  6429. if (this.readInt(10) === null) this.raise(start, "Invalid number");
  6430. isFloat = true;
  6431. next = this.input.charCodeAt(this.state.pos);
  6432. }
  6433. if (this.hasPlugin("numericSeparator") && (octal || isNonOctalDecimalInt)) {
  6434. const underscorePos = this.input.slice(start, this.state.pos).indexOf("_");
  6435. if (underscorePos > 0) {
  6436. this.raise(underscorePos + start, "Numeric separator can not be used after leading 0");
  6437. }
  6438. }
  6439. if (this.hasPlugin("bigInt")) {
  6440. if (next === 110) {
  6441. if (isFloat || octal || isNonOctalDecimalInt) {
  6442. this.raise(start, "Invalid BigIntLiteral");
  6443. }
  6444. ++this.state.pos;
  6445. isBigInt = true;
  6446. }
  6447. }
  6448. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  6449. throw this.raise(this.state.pos, "Identifier directly after number");
  6450. }
  6451. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  6452. if (isBigInt) {
  6453. this.finishToken(types.bigint, str);
  6454. return;
  6455. }
  6456. const val = octal ? parseInt(str, 8) : parseFloat(str);
  6457. this.finishToken(types.num, val);
  6458. }
  6459. readCodePoint(throwOnInvalid) {
  6460. const ch = this.input.charCodeAt(this.state.pos);
  6461. let code;
  6462. if (ch === 123) {
  6463. const codePos = ++this.state.pos;
  6464. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  6465. ++this.state.pos;
  6466. if (code === null) {
  6467. --this.state.invalidTemplateEscapePosition;
  6468. } else if (code > 0x10ffff) {
  6469. if (throwOnInvalid) {
  6470. this.raise(codePos, "Code point out of bounds");
  6471. } else {
  6472. this.state.invalidTemplateEscapePosition = codePos - 2;
  6473. return null;
  6474. }
  6475. }
  6476. } else {
  6477. code = this.readHexChar(4, false, throwOnInvalid);
  6478. }
  6479. return code;
  6480. }
  6481. readString(quote) {
  6482. let out = "",
  6483. chunkStart = ++this.state.pos;
  6484. for (;;) {
  6485. if (this.state.pos >= this.length) {
  6486. throw this.raise(this.state.start, "Unterminated string constant");
  6487. }
  6488. const ch = this.input.charCodeAt(this.state.pos);
  6489. if (ch === quote) break;
  6490. if (ch === 92) {
  6491. out += this.input.slice(chunkStart, this.state.pos);
  6492. out += this.readEscapedChar(false);
  6493. chunkStart = this.state.pos;
  6494. } else if (ch === 8232 || ch === 8233) {
  6495. ++this.state.pos;
  6496. ++this.state.curLine;
  6497. } else if (isNewLine(ch)) {
  6498. throw this.raise(this.state.start, "Unterminated string constant");
  6499. } else {
  6500. ++this.state.pos;
  6501. }
  6502. }
  6503. out += this.input.slice(chunkStart, this.state.pos++);
  6504. this.finishToken(types.string, out);
  6505. }
  6506. readTmplToken() {
  6507. let out = "",
  6508. chunkStart = this.state.pos,
  6509. containsInvalid = false;
  6510. for (;;) {
  6511. if (this.state.pos >= this.length) {
  6512. throw this.raise(this.state.start, "Unterminated template");
  6513. }
  6514. const ch = this.input.charCodeAt(this.state.pos);
  6515. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  6516. if (this.state.pos === this.state.start && this.match(types.template)) {
  6517. if (ch === 36) {
  6518. this.state.pos += 2;
  6519. this.finishToken(types.dollarBraceL);
  6520. return;
  6521. } else {
  6522. ++this.state.pos;
  6523. this.finishToken(types.backQuote);
  6524. return;
  6525. }
  6526. }
  6527. out += this.input.slice(chunkStart, this.state.pos);
  6528. this.finishToken(types.template, containsInvalid ? null : out);
  6529. return;
  6530. }
  6531. if (ch === 92) {
  6532. out += this.input.slice(chunkStart, this.state.pos);
  6533. const escaped = this.readEscapedChar(true);
  6534. if (escaped === null) {
  6535. containsInvalid = true;
  6536. } else {
  6537. out += escaped;
  6538. }
  6539. chunkStart = this.state.pos;
  6540. } else if (isNewLine(ch)) {
  6541. out += this.input.slice(chunkStart, this.state.pos);
  6542. ++this.state.pos;
  6543. switch (ch) {
  6544. case 13:
  6545. if (this.input.charCodeAt(this.state.pos) === 10) {
  6546. ++this.state.pos;
  6547. }
  6548. case 10:
  6549. out += "\n";
  6550. break;
  6551. default:
  6552. out += String.fromCharCode(ch);
  6553. break;
  6554. }
  6555. ++this.state.curLine;
  6556. this.state.lineStart = this.state.pos;
  6557. chunkStart = this.state.pos;
  6558. } else {
  6559. ++this.state.pos;
  6560. }
  6561. }
  6562. }
  6563. readEscapedChar(inTemplate) {
  6564. const throwOnInvalid = !inTemplate;
  6565. const ch = this.input.charCodeAt(++this.state.pos);
  6566. ++this.state.pos;
  6567. switch (ch) {
  6568. case 110:
  6569. return "\n";
  6570. case 114:
  6571. return "\r";
  6572. case 120:
  6573. {
  6574. const code = this.readHexChar(2, false, throwOnInvalid);
  6575. return code === null ? null : String.fromCharCode(code);
  6576. }
  6577. case 117:
  6578. {
  6579. const code = this.readCodePoint(throwOnInvalid);
  6580. return code === null ? null : String.fromCodePoint(code);
  6581. }
  6582. case 116:
  6583. return "\t";
  6584. case 98:
  6585. return "\b";
  6586. case 118:
  6587. return "\u000b";
  6588. case 102:
  6589. return "\f";
  6590. case 13:
  6591. if (this.input.charCodeAt(this.state.pos) === 10) {
  6592. ++this.state.pos;
  6593. }
  6594. case 10:
  6595. this.state.lineStart = this.state.pos;
  6596. ++this.state.curLine;
  6597. case 8232:
  6598. case 8233:
  6599. return "";
  6600. case 56:
  6601. case 57:
  6602. if (inTemplate) {
  6603. const codePos = this.state.pos - 1;
  6604. this.state.invalidTemplateEscapePosition = codePos;
  6605. return null;
  6606. }
  6607. default:
  6608. if (ch >= 48 && ch <= 55) {
  6609. const codePos = this.state.pos - 1;
  6610. let octalStr = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/)[0];
  6611. let octal = parseInt(octalStr, 8);
  6612. if (octal > 255) {
  6613. octalStr = octalStr.slice(0, -1);
  6614. octal = parseInt(octalStr, 8);
  6615. }
  6616. this.state.pos += octalStr.length - 1;
  6617. const next = this.input.charCodeAt(this.state.pos);
  6618. if (octalStr !== "0" || next === 56 || next === 57) {
  6619. if (inTemplate) {
  6620. this.state.invalidTemplateEscapePosition = codePos;
  6621. return null;
  6622. } else if (this.state.strict) {
  6623. this.raise(codePos, "Octal literal in strict mode");
  6624. } else if (!this.state.containsOctal) {
  6625. this.state.containsOctal = true;
  6626. this.state.octalPosition = codePos;
  6627. }
  6628. }
  6629. return String.fromCharCode(octal);
  6630. }
  6631. return String.fromCharCode(ch);
  6632. }
  6633. }
  6634. readHexChar(len, forceLen, throwOnInvalid) {
  6635. const codePos = this.state.pos;
  6636. const n = this.readInt(16, len, forceLen, false);
  6637. if (n === null) {
  6638. if (throwOnInvalid) {
  6639. this.raise(codePos, "Bad character escape sequence");
  6640. } else {
  6641. this.state.pos = codePos - 1;
  6642. this.state.invalidTemplateEscapePosition = codePos - 1;
  6643. }
  6644. }
  6645. return n;
  6646. }
  6647. readWord1() {
  6648. let word = "";
  6649. this.state.containsEsc = false;
  6650. const start = this.state.pos;
  6651. let chunkStart = this.state.pos;
  6652. while (this.state.pos < this.length) {
  6653. const ch = this.input.codePointAt(this.state.pos);
  6654. if (isIdentifierChar(ch)) {
  6655. this.state.pos += ch <= 0xffff ? 1 : 2;
  6656. } else if (this.state.isIterator && ch === 64) {
  6657. ++this.state.pos;
  6658. } else if (ch === 92) {
  6659. this.state.containsEsc = true;
  6660. word += this.input.slice(chunkStart, this.state.pos);
  6661. const escStart = this.state.pos;
  6662. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  6663. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  6664. this.raise(this.state.pos, "Expecting Unicode escape sequence \\uXXXX");
  6665. continue;
  6666. }
  6667. ++this.state.pos;
  6668. const esc = this.readCodePoint(true);
  6669. if (esc !== null) {
  6670. if (!identifierCheck(esc)) {
  6671. this.raise(escStart, "Invalid Unicode escape");
  6672. }
  6673. word += String.fromCodePoint(esc);
  6674. }
  6675. chunkStart = this.state.pos;
  6676. } else {
  6677. break;
  6678. }
  6679. }
  6680. return word + this.input.slice(chunkStart, this.state.pos);
  6681. }
  6682. isIterator(word) {
  6683. return word === "@@iterator" || word === "@@asyncIterator";
  6684. }
  6685. readWord() {
  6686. const word = this.readWord1();
  6687. const type = keywords.get(word) || types.name;
  6688. if (this.state.isIterator && (!this.isIterator(word) || !this.state.inType)) {
  6689. this.raise(this.state.pos, `Invalid identifier ${word}`);
  6690. }
  6691. this.finishToken(type, word);
  6692. }
  6693. checkKeywordEscapes() {
  6694. const kw = this.state.type.keyword;
  6695. if (kw && this.state.containsEsc) {
  6696. this.raise(this.state.start, `Escape sequence in keyword ${kw}`);
  6697. }
  6698. }
  6699. braceIsBlock(prevType) {
  6700. const parent = this.curContext();
  6701. if (parent === types$1.functionExpression || parent === types$1.functionStatement) {
  6702. return true;
  6703. }
  6704. if (prevType === types.colon && (parent === types$1.braceStatement || parent === types$1.braceExpression)) {
  6705. return !parent.isExpr;
  6706. }
  6707. if (prevType === types._return || prevType === types.name && this.state.exprAllowed) {
  6708. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  6709. }
  6710. if (prevType === types._else || prevType === types.semi || prevType === types.eof || prevType === types.parenR || prevType === types.arrow) {
  6711. return true;
  6712. }
  6713. if (prevType === types.braceL) {
  6714. return parent === types$1.braceStatement;
  6715. }
  6716. if (prevType === types._var || prevType === types._const || prevType === types.name) {
  6717. return false;
  6718. }
  6719. if (prevType === types.relational) {
  6720. return true;
  6721. }
  6722. return !this.state.exprAllowed;
  6723. }
  6724. updateContext(prevType) {
  6725. const type = this.state.type;
  6726. let update;
  6727. if (type.keyword && (prevType === types.dot || prevType === types.questionDot)) {
  6728. this.state.exprAllowed = false;
  6729. } else if (update = type.updateContext) {
  6730. update.call(this, prevType);
  6731. } else {
  6732. this.state.exprAllowed = type.beforeExpr;
  6733. }
  6734. }
  6735. }
  6736. const literal = /^('|")((?:\\?.)*?)\1/;
  6737. class UtilParser extends Tokenizer {
  6738. addExtra(node, key, val) {
  6739. if (!node) return;
  6740. const extra = node.extra = node.extra || {};
  6741. extra[key] = val;
  6742. }
  6743. isRelational(op) {
  6744. return this.match(types.relational) && this.state.value === op;
  6745. }
  6746. isLookaheadRelational(op) {
  6747. const next = this.nextTokenStart();
  6748. if (this.input.charAt(next) === op) {
  6749. if (next + 1 === this.input.length) {
  6750. return true;
  6751. }
  6752. const afterNext = this.input.charCodeAt(next + 1);
  6753. return afterNext !== op.charCodeAt(0) && afterNext !== 61;
  6754. }
  6755. return false;
  6756. }
  6757. expectRelational(op) {
  6758. if (this.isRelational(op)) {
  6759. this.next();
  6760. } else {
  6761. this.unexpected(null, types.relational);
  6762. }
  6763. }
  6764. eatRelational(op) {
  6765. if (this.isRelational(op)) {
  6766. this.next();
  6767. return true;
  6768. }
  6769. return false;
  6770. }
  6771. isContextual(name) {
  6772. return this.match(types.name) && this.state.value === name && !this.state.containsEsc;
  6773. }
  6774. isUnparsedContextual(nameStart, name) {
  6775. const nameEnd = nameStart + name.length;
  6776. return this.input.slice(nameStart, nameEnd) === name && (nameEnd === this.input.length || !isIdentifierChar(this.input.charCodeAt(nameEnd)));
  6777. }
  6778. isLookaheadContextual(name) {
  6779. const next = this.nextTokenStart();
  6780. return this.isUnparsedContextual(next, name);
  6781. }
  6782. eatContextual(name) {
  6783. return this.isContextual(name) && this.eat(types.name);
  6784. }
  6785. expectContextual(name, message) {
  6786. if (!this.eatContextual(name)) this.unexpected(null, message);
  6787. }
  6788. canInsertSemicolon() {
  6789. return this.match(types.eof) || this.match(types.braceR) || this.hasPrecedingLineBreak();
  6790. }
  6791. hasPrecedingLineBreak() {
  6792. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  6793. }
  6794. isLineTerminator() {
  6795. return this.eat(types.semi) || this.canInsertSemicolon();
  6796. }
  6797. semicolon() {
  6798. if (!this.isLineTerminator()) this.unexpected(null, types.semi);
  6799. }
  6800. expect(type, pos) {
  6801. this.eat(type) || this.unexpected(pos, type);
  6802. }
  6803. assertNoSpace(message = "Unexpected space.") {
  6804. if (this.state.start > this.state.lastTokEnd) {
  6805. this.raise(this.state.lastTokEnd, message);
  6806. }
  6807. }
  6808. unexpected(pos, messageOrType = "Unexpected token") {
  6809. if (typeof messageOrType !== "string") {
  6810. messageOrType = `Unexpected token, expected "${messageOrType.label}"`;
  6811. }
  6812. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  6813. }
  6814. expectPlugin(name, pos) {
  6815. if (!this.hasPlugin(name)) {
  6816. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling the parser plugin: '${name}'`, {
  6817. missingPluginNames: [name]
  6818. });
  6819. }
  6820. return true;
  6821. }
  6822. expectOnePlugin(names, pos) {
  6823. if (!names.some(n => this.hasPlugin(n))) {
  6824. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`, {
  6825. missingPluginNames: names
  6826. });
  6827. }
  6828. }
  6829. checkYieldAwaitInDefaultParams() {
  6830. if (this.state.yieldPos !== -1 && (this.state.awaitPos === -1 || this.state.yieldPos < this.state.awaitPos)) {
  6831. this.raise(this.state.yieldPos, "Yield cannot be used as name inside a generator function");
  6832. }
  6833. if (this.state.awaitPos !== -1) {
  6834. this.raise(this.state.awaitPos, "Await cannot be used as name inside an async function");
  6835. }
  6836. }
  6837. strictDirective(start) {
  6838. for (;;) {
  6839. skipWhiteSpace.lastIndex = start;
  6840. start += skipWhiteSpace.exec(this.input)[0].length;
  6841. const match = literal.exec(this.input.slice(start));
  6842. if (!match) break;
  6843. if (match[2] === "use strict") return true;
  6844. start += match[0].length;
  6845. skipWhiteSpace.lastIndex = start;
  6846. start += skipWhiteSpace.exec(this.input)[0].length;
  6847. if (this.input[start] === ";") {
  6848. start++;
  6849. }
  6850. }
  6851. return false;
  6852. }
  6853. tryParse(fn, oldState = this.state.clone()) {
  6854. const abortSignal = {
  6855. node: null
  6856. };
  6857. try {
  6858. const node = fn((node = null) => {
  6859. abortSignal.node = node;
  6860. throw abortSignal;
  6861. });
  6862. if (this.state.errors.length > oldState.errors.length) {
  6863. const failState = this.state;
  6864. this.state = oldState;
  6865. return {
  6866. node,
  6867. error: failState.errors[oldState.errors.length],
  6868. thrown: false,
  6869. aborted: false,
  6870. failState
  6871. };
  6872. }
  6873. return {
  6874. node,
  6875. error: null,
  6876. thrown: false,
  6877. aborted: false,
  6878. failState: null
  6879. };
  6880. } catch (error) {
  6881. const failState = this.state;
  6882. this.state = oldState;
  6883. if (error instanceof SyntaxError) {
  6884. return {
  6885. node: null,
  6886. error,
  6887. thrown: true,
  6888. aborted: false,
  6889. failState
  6890. };
  6891. }
  6892. if (error === abortSignal) {
  6893. return {
  6894. node: abortSignal.node,
  6895. error: null,
  6896. thrown: false,
  6897. aborted: true,
  6898. failState
  6899. };
  6900. }
  6901. throw error;
  6902. }
  6903. }
  6904. }
  6905. class Node {
  6906. constructor(parser, pos, loc) {
  6907. this.type = "";
  6908. this.start = pos;
  6909. this.end = 0;
  6910. this.loc = new SourceLocation(loc);
  6911. if (parser && parser.options.ranges) this.range = [pos, 0];
  6912. if (parser && parser.filename) this.loc.filename = parser.filename;
  6913. }
  6914. __clone() {
  6915. const newNode = new Node();
  6916. const keys = Object.keys(this);
  6917. for (let i = 0, length = keys.length; i < length; i++) {
  6918. const key = keys[i];
  6919. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  6920. newNode[key] = this[key];
  6921. }
  6922. }
  6923. return newNode;
  6924. }
  6925. }
  6926. class NodeUtils extends UtilParser {
  6927. startNode() {
  6928. return new Node(this, this.state.start, this.state.startLoc);
  6929. }
  6930. startNodeAt(pos, loc) {
  6931. return new Node(this, pos, loc);
  6932. }
  6933. startNodeAtNode(type) {
  6934. return this.startNodeAt(type.start, type.loc.start);
  6935. }
  6936. finishNode(node, type) {
  6937. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  6938. }
  6939. finishNodeAt(node, type, pos, loc) {
  6940. node.type = type;
  6941. node.end = pos;
  6942. node.loc.end = loc;
  6943. if (this.options.ranges) node.range[1] = pos;
  6944. this.processComment(node);
  6945. return node;
  6946. }
  6947. resetStartLocation(node, start, startLoc) {
  6948. node.start = start;
  6949. node.loc.start = startLoc;
  6950. if (this.options.ranges) node.range[0] = start;
  6951. }
  6952. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  6953. node.end = end;
  6954. node.loc.end = endLoc;
  6955. if (this.options.ranges) node.range[1] = end;
  6956. }
  6957. resetStartLocationFromNode(node, locationNode) {
  6958. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  6959. }
  6960. }
  6961. class LValParser extends NodeUtils {
  6962. toAssignable(node, isBinding, contextDescription) {
  6963. var _node$extra2;
  6964. if (node) {
  6965. switch (node.type) {
  6966. case "Identifier":
  6967. case "ObjectPattern":
  6968. case "ArrayPattern":
  6969. case "AssignmentPattern":
  6970. break;
  6971. case "ObjectExpression":
  6972. node.type = "ObjectPattern";
  6973. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  6974. var _node$extra;
  6975. const prop = node.properties[i];
  6976. const isLast = i === last;
  6977. this.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  6978. if (isLast && prop.type === "RestElement" && ((_node$extra = node.extra) === null || _node$extra === void 0 ? void 0 : _node$extra.trailingComma)) {
  6979. this.raiseRestNotLast(node.extra.trailingComma);
  6980. }
  6981. }
  6982. break;
  6983. case "ObjectProperty":
  6984. this.toAssignable(node.value, isBinding, contextDescription);
  6985. break;
  6986. case "SpreadElement":
  6987. {
  6988. this.checkToRestConversion(node);
  6989. node.type = "RestElement";
  6990. const arg = node.argument;
  6991. this.toAssignable(arg, isBinding, contextDescription);
  6992. break;
  6993. }
  6994. case "ArrayExpression":
  6995. node.type = "ArrayPattern";
  6996. this.toAssignableList(node.elements, isBinding, contextDescription, (_node$extra2 = node.extra) === null || _node$extra2 === void 0 ? void 0 : _node$extra2.trailingComma);
  6997. break;
  6998. case "AssignmentExpression":
  6999. if (node.operator !== "=") {
  7000. this.raise(node.left.end, "Only '=' operator can be used for specifying default value.");
  7001. }
  7002. node.type = "AssignmentPattern";
  7003. delete node.operator;
  7004. this.toAssignable(node.left, isBinding, contextDescription);
  7005. break;
  7006. case "ParenthesizedExpression":
  7007. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  7008. break;
  7009. case "MemberExpression":
  7010. if (!isBinding) break;
  7011. default:
  7012. }
  7013. }
  7014. return node;
  7015. }
  7016. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  7017. if (prop.type === "ObjectMethod") {
  7018. const error = prop.kind === "get" || prop.kind === "set" ? "Object pattern can't contain getter or setter" : "Object pattern can't contain methods";
  7019. this.raise(prop.key.start, error);
  7020. } else if (prop.type === "SpreadElement" && !isLast) {
  7021. this.raiseRestNotLast(prop.start);
  7022. } else {
  7023. this.toAssignable(prop, isBinding, "object destructuring pattern");
  7024. }
  7025. }
  7026. toAssignableList(exprList, isBinding, contextDescription, trailingCommaPos) {
  7027. let end = exprList.length;
  7028. if (end) {
  7029. const last = exprList[end - 1];
  7030. if (last && last.type === "RestElement") {
  7031. --end;
  7032. } else if (last && last.type === "SpreadElement") {
  7033. last.type = "RestElement";
  7034. const arg = last.argument;
  7035. this.toAssignable(arg, isBinding, contextDescription);
  7036. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  7037. this.unexpected(arg.start);
  7038. }
  7039. if (trailingCommaPos) {
  7040. this.raiseTrailingCommaAfterRest(trailingCommaPos);
  7041. }
  7042. --end;
  7043. }
  7044. }
  7045. for (let i = 0; i < end; i++) {
  7046. const elt = exprList[i];
  7047. if (elt) {
  7048. this.toAssignable(elt, isBinding, contextDescription);
  7049. if (elt.type === "RestElement") {
  7050. this.raiseRestNotLast(elt.start);
  7051. }
  7052. }
  7053. }
  7054. return exprList;
  7055. }
  7056. toReferencedList(exprList, isParenthesizedExpr) {
  7057. return exprList;
  7058. }
  7059. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7060. this.toReferencedList(exprList, isParenthesizedExpr);
  7061. for (let _i = 0; _i < exprList.length; _i++) {
  7062. const expr = exprList[_i];
  7063. if (expr && expr.type === "ArrayExpression") {
  7064. this.toReferencedListDeep(expr.elements);
  7065. }
  7066. }
  7067. return exprList;
  7068. }
  7069. parseSpread(refShorthandDefaultPos, refNeedsArrowPos) {
  7070. const node = this.startNode();
  7071. this.next();
  7072. node.argument = this.parseMaybeAssign(false, refShorthandDefaultPos, undefined, refNeedsArrowPos);
  7073. return this.finishNode(node, "SpreadElement");
  7074. }
  7075. parseRestBinding() {
  7076. const node = this.startNode();
  7077. this.next();
  7078. node.argument = this.parseBindingAtom();
  7079. return this.finishNode(node, "RestElement");
  7080. }
  7081. parseBindingAtom() {
  7082. switch (this.state.type) {
  7083. case types.bracketL:
  7084. {
  7085. const node = this.startNode();
  7086. this.next();
  7087. node.elements = this.parseBindingList(types.bracketR, 93, true);
  7088. return this.finishNode(node, "ArrayPattern");
  7089. }
  7090. case types.braceL:
  7091. return this.parseObj(true);
  7092. }
  7093. return this.parseIdentifier();
  7094. }
  7095. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  7096. const elts = [];
  7097. let first = true;
  7098. while (!this.eat(close)) {
  7099. if (first) {
  7100. first = false;
  7101. } else {
  7102. this.expect(types.comma);
  7103. }
  7104. if (allowEmpty && this.match(types.comma)) {
  7105. elts.push(null);
  7106. } else if (this.eat(close)) {
  7107. break;
  7108. } else if (this.match(types.ellipsis)) {
  7109. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  7110. this.checkCommaAfterRest(closeCharCode);
  7111. this.expect(close);
  7112. break;
  7113. } else {
  7114. const decorators = [];
  7115. if (this.match(types.at) && this.hasPlugin("decorators")) {
  7116. this.raise(this.state.start, "Stage 2 decorators cannot be used to decorate parameters");
  7117. }
  7118. while (this.match(types.at)) {
  7119. decorators.push(this.parseDecorator());
  7120. }
  7121. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  7122. }
  7123. }
  7124. return elts;
  7125. }
  7126. parseAssignableListItem(allowModifiers, decorators) {
  7127. const left = this.parseMaybeDefault();
  7128. this.parseAssignableListItemTypes(left);
  7129. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  7130. if (decorators.length) {
  7131. left.decorators = decorators;
  7132. }
  7133. return elt;
  7134. }
  7135. parseAssignableListItemTypes(param) {
  7136. return param;
  7137. }
  7138. parseMaybeDefault(startPos, startLoc, left) {
  7139. startLoc = startLoc || this.state.startLoc;
  7140. startPos = startPos || this.state.start;
  7141. left = left || this.parseBindingAtom();
  7142. if (!this.eat(types.eq)) return left;
  7143. const node = this.startNodeAt(startPos, startLoc);
  7144. node.left = left;
  7145. node.right = this.parseMaybeAssign();
  7146. return this.finishNode(node, "AssignmentPattern");
  7147. }
  7148. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription, disallowLetBinding, strictModeChanged = false) {
  7149. switch (expr.type) {
  7150. case "Identifier":
  7151. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(expr.name, this.inModule) : isStrictBindOnlyReservedWord(expr.name))) {
  7152. this.raise(expr.start, `${bindingType === BIND_NONE ? "Assigning to" : "Binding"} '${expr.name}' in strict mode`);
  7153. }
  7154. if (checkClashes) {
  7155. const key = `_${expr.name}`;
  7156. if (checkClashes[key]) {
  7157. this.raise(expr.start, "Argument name clash");
  7158. } else {
  7159. checkClashes[key] = true;
  7160. }
  7161. }
  7162. if (disallowLetBinding && expr.name === "let") {
  7163. this.raise(expr.start, "'let' is not allowed to be used as a name in 'let' or 'const' declarations.");
  7164. }
  7165. if (!(bindingType & BIND_NONE)) {
  7166. this.scope.declareName(expr.name, bindingType, expr.start);
  7167. }
  7168. break;
  7169. case "MemberExpression":
  7170. if (bindingType !== BIND_NONE) {
  7171. this.raise(expr.start, "Binding member expression");
  7172. }
  7173. break;
  7174. case "ObjectPattern":
  7175. for (let _i2 = 0, _expr$properties = expr.properties; _i2 < _expr$properties.length; _i2++) {
  7176. let prop = _expr$properties[_i2];
  7177. if (prop.type === "ObjectProperty") prop = prop.value;else if (prop.type === "ObjectMethod") continue;
  7178. this.checkLVal(prop, bindingType, checkClashes, "object destructuring pattern", disallowLetBinding);
  7179. }
  7180. break;
  7181. case "ArrayPattern":
  7182. for (let _i3 = 0, _expr$elements = expr.elements; _i3 < _expr$elements.length; _i3++) {
  7183. const elem = _expr$elements[_i3];
  7184. if (elem) {
  7185. this.checkLVal(elem, bindingType, checkClashes, "array destructuring pattern", disallowLetBinding);
  7186. }
  7187. }
  7188. break;
  7189. case "AssignmentPattern":
  7190. this.checkLVal(expr.left, bindingType, checkClashes, "assignment pattern");
  7191. break;
  7192. case "RestElement":
  7193. this.checkLVal(expr.argument, bindingType, checkClashes, "rest element");
  7194. break;
  7195. case "ParenthesizedExpression":
  7196. this.checkLVal(expr.expression, bindingType, checkClashes, "parenthesized expression");
  7197. break;
  7198. default:
  7199. {
  7200. const message = (bindingType === BIND_NONE ? "Invalid" : "Binding invalid") + " left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
  7201. this.raise(expr.start, message);
  7202. }
  7203. }
  7204. }
  7205. checkToRestConversion(node) {
  7206. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  7207. this.raise(node.argument.start, "Invalid rest operator's argument");
  7208. }
  7209. }
  7210. checkCommaAfterRest(close) {
  7211. if (this.match(types.comma)) {
  7212. if (this.lookaheadCharCode() === close) {
  7213. this.raiseTrailingCommaAfterRest(this.state.start);
  7214. } else {
  7215. this.raiseRestNotLast(this.state.start);
  7216. }
  7217. }
  7218. }
  7219. raiseRestNotLast(pos) {
  7220. throw this.raise(pos, `Rest element must be last element`);
  7221. }
  7222. raiseTrailingCommaAfterRest(pos) {
  7223. this.raise(pos, `Unexpected trailing comma after rest element`);
  7224. }
  7225. }
  7226. const unwrapParenthesizedExpression = node => {
  7227. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  7228. };
  7229. class ExpressionParser extends LValParser {
  7230. checkDuplicatedProto(prop, protoRef) {
  7231. if (prop.type === "SpreadElement" || prop.computed || prop.kind || prop.shorthand) {
  7232. return;
  7233. }
  7234. const key = prop.key;
  7235. const name = key.type === "Identifier" ? key.name : String(key.value);
  7236. if (name === "__proto__") {
  7237. if (protoRef.used && !protoRef.start) {
  7238. protoRef.start = key.start;
  7239. }
  7240. protoRef.used = true;
  7241. }
  7242. }
  7243. getExpression() {
  7244. this.scope.enter(SCOPE_PROGRAM);
  7245. this.nextToken();
  7246. const expr = this.parseExpression();
  7247. if (!this.match(types.eof)) {
  7248. this.unexpected();
  7249. }
  7250. expr.comments = this.state.comments;
  7251. expr.errors = this.state.errors;
  7252. return expr;
  7253. }
  7254. parseExpression(noIn, refShorthandDefaultPos) {
  7255. const startPos = this.state.start;
  7256. const startLoc = this.state.startLoc;
  7257. const expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
  7258. if (this.match(types.comma)) {
  7259. const node = this.startNodeAt(startPos, startLoc);
  7260. node.expressions = [expr];
  7261. while (this.eat(types.comma)) {
  7262. node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos));
  7263. }
  7264. this.toReferencedList(node.expressions);
  7265. return this.finishNode(node, "SequenceExpression");
  7266. }
  7267. return expr;
  7268. }
  7269. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  7270. const startPos = this.state.start;
  7271. const startLoc = this.state.startLoc;
  7272. if (this.isContextual("yield")) {
  7273. if (this.scope.inGenerator) {
  7274. let left = this.parseYield(noIn);
  7275. if (afterLeftParse) {
  7276. left = afterLeftParse.call(this, left, startPos, startLoc);
  7277. }
  7278. return left;
  7279. } else {
  7280. this.state.exprAllowed = false;
  7281. }
  7282. }
  7283. let failOnShorthandAssign;
  7284. if (refShorthandDefaultPos) {
  7285. failOnShorthandAssign = false;
  7286. } else {
  7287. refShorthandDefaultPos = {
  7288. start: 0
  7289. };
  7290. failOnShorthandAssign = true;
  7291. }
  7292. if (this.match(types.parenL) || this.match(types.name)) {
  7293. this.state.potentialArrowAt = this.state.start;
  7294. }
  7295. let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos);
  7296. if (afterLeftParse) {
  7297. left = afterLeftParse.call(this, left, startPos, startLoc);
  7298. }
  7299. if (this.state.type.isAssign) {
  7300. const node = this.startNodeAt(startPos, startLoc);
  7301. const operator = this.state.value;
  7302. node.operator = operator;
  7303. if (operator === "??=") {
  7304. this.expectPlugin("nullishCoalescingOperator");
  7305. this.expectPlugin("logicalAssignment");
  7306. }
  7307. if (operator === "||=" || operator === "&&=") {
  7308. this.expectPlugin("logicalAssignment");
  7309. }
  7310. node.left = this.match(types.eq) ? this.toAssignable(left, undefined, "assignment expression") : left;
  7311. if (refShorthandDefaultPos.start >= node.left.start) {
  7312. refShorthandDefaultPos.start = 0;
  7313. }
  7314. this.checkLVal(left, undefined, undefined, "assignment expression");
  7315. const maybePattern = unwrapParenthesizedExpression(left);
  7316. let patternErrorMsg;
  7317. if (maybePattern.type === "ObjectPattern") {
  7318. patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
  7319. } else if (maybePattern.type === "ArrayPattern") {
  7320. patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
  7321. }
  7322. if (patternErrorMsg && (left.extra && left.extra.parenthesized || left.type === "ParenthesizedExpression")) {
  7323. this.raise(maybePattern.start, `You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`);
  7324. }
  7325. this.next();
  7326. node.right = this.parseMaybeAssign(noIn);
  7327. return this.finishNode(node, "AssignmentExpression");
  7328. } else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
  7329. this.unexpected(refShorthandDefaultPos.start);
  7330. }
  7331. return left;
  7332. }
  7333. parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos) {
  7334. const startPos = this.state.start;
  7335. const startLoc = this.state.startLoc;
  7336. const potentialArrowAt = this.state.potentialArrowAt;
  7337. const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
  7338. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  7339. return expr;
  7340. }
  7341. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  7342. return this.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  7343. }
  7344. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  7345. if (this.eat(types.question)) {
  7346. const node = this.startNodeAt(startPos, startLoc);
  7347. node.test = expr;
  7348. node.consequent = this.parseMaybeAssign();
  7349. this.expect(types.colon);
  7350. node.alternate = this.parseMaybeAssign(noIn);
  7351. return this.finishNode(node, "ConditionalExpression");
  7352. }
  7353. return expr;
  7354. }
  7355. parseExprOps(noIn, refShorthandDefaultPos) {
  7356. const startPos = this.state.start;
  7357. const startLoc = this.state.startLoc;
  7358. const potentialArrowAt = this.state.potentialArrowAt;
  7359. const expr = this.parseMaybeUnary(refShorthandDefaultPos);
  7360. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  7361. return expr;
  7362. }
  7363. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  7364. return expr;
  7365. }
  7366. return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
  7367. }
  7368. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  7369. const prec = this.state.type.binop;
  7370. if (prec != null && (!noIn || !this.match(types._in))) {
  7371. if (prec > minPrec) {
  7372. const operator = this.state.value;
  7373. if (operator === "|>" && this.state.inFSharpPipelineDirectBody) {
  7374. return left;
  7375. }
  7376. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  7377. node.left = left;
  7378. node.operator = operator;
  7379. if (operator === "**" && left.type === "UnaryExpression" && (this.options.createParenthesizedExpressions || !(left.extra && left.extra.parenthesized))) {
  7380. this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.");
  7381. }
  7382. const op = this.state.type;
  7383. if (op === types.pipeline) {
  7384. this.expectPlugin("pipelineOperator");
  7385. this.state.inPipeline = true;
  7386. this.checkPipelineAtInfixOperator(left, leftStartPos);
  7387. } else if (op === types.nullishCoalescing) {
  7388. this.expectPlugin("nullishCoalescingOperator");
  7389. }
  7390. this.next();
  7391. if (op === types.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  7392. if (this.match(types.name) && this.state.value === "await" && this.scope.inAsync) {
  7393. throw this.raise(this.state.start, `Unexpected "await" after pipeline body; await must have parentheses in minimal proposal`);
  7394. }
  7395. }
  7396. node.right = this.parseExprOpRightExpr(op, prec, noIn);
  7397. if (op === types.nullishCoalescing) {
  7398. if (left.type === "LogicalExpression" && left.operator !== "??" && !(left.extra && left.extra.parenthesized)) {
  7399. throw this.raise(left.start, `Nullish coalescing operator(??) requires parens when mixing with logical operators`);
  7400. } else if (node.right.type === "LogicalExpression" && node.right.operator !== "??" && !(node.right.extra && node.right.extra.parenthesized)) {
  7401. throw this.raise(node.right.start, `Nullish coalescing operator(??) requires parens when mixing with logical operators`);
  7402. }
  7403. }
  7404. this.finishNode(node, op === types.logicalOR || op === types.logicalAND || op === types.nullishCoalescing ? "LogicalExpression" : "BinaryExpression");
  7405. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  7406. }
  7407. }
  7408. return left;
  7409. }
  7410. parseExprOpRightExpr(op, prec, noIn) {
  7411. const startPos = this.state.start;
  7412. const startLoc = this.state.startLoc;
  7413. switch (op) {
  7414. case types.pipeline:
  7415. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  7416. case "smart":
  7417. return this.withTopicPermittingContext(() => {
  7418. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec, noIn), startPos, startLoc);
  7419. });
  7420. case "fsharp":
  7421. return this.withSoloAwaitPermittingContext(() => {
  7422. return this.parseFSharpPipelineBody(prec, noIn);
  7423. });
  7424. }
  7425. default:
  7426. return this.parseExprOpBaseRightExpr(op, prec, noIn);
  7427. }
  7428. }
  7429. parseExprOpBaseRightExpr(op, prec, noIn) {
  7430. const startPos = this.state.start;
  7431. const startLoc = this.state.startLoc;
  7432. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn);
  7433. }
  7434. parseMaybeUnary(refShorthandDefaultPos) {
  7435. if (this.isContextual("await") && this.isAwaitAllowed()) {
  7436. return this.parseAwait();
  7437. } else if (this.state.type.prefix) {
  7438. const node = this.startNode();
  7439. const update = this.match(types.incDec);
  7440. node.operator = this.state.value;
  7441. node.prefix = true;
  7442. if (node.operator === "throw") {
  7443. this.expectPlugin("throwExpressions");
  7444. }
  7445. this.next();
  7446. node.argument = this.parseMaybeUnary();
  7447. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  7448. this.unexpected(refShorthandDefaultPos.start);
  7449. }
  7450. if (update) {
  7451. this.checkLVal(node.argument, undefined, undefined, "prefix operation");
  7452. } else if (this.state.strict && node.operator === "delete") {
  7453. const arg = node.argument;
  7454. if (arg.type === "Identifier") {
  7455. this.raise(node.start, "Deleting local variable in strict mode");
  7456. } else if (arg.type === "MemberExpression" && arg.property.type === "PrivateName") {
  7457. this.raise(node.start, "Deleting a private field is not allowed");
  7458. }
  7459. }
  7460. return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  7461. }
  7462. const startPos = this.state.start;
  7463. const startLoc = this.state.startLoc;
  7464. let expr = this.parseExprSubscripts(refShorthandDefaultPos);
  7465. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  7466. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  7467. const node = this.startNodeAt(startPos, startLoc);
  7468. node.operator = this.state.value;
  7469. node.prefix = false;
  7470. node.argument = expr;
  7471. this.checkLVal(expr, undefined, undefined, "postfix operation");
  7472. this.next();
  7473. expr = this.finishNode(node, "UpdateExpression");
  7474. }
  7475. return expr;
  7476. }
  7477. parseExprSubscripts(refShorthandDefaultPos) {
  7478. const startPos = this.state.start;
  7479. const startLoc = this.state.startLoc;
  7480. const potentialArrowAt = this.state.potentialArrowAt;
  7481. const expr = this.parseExprAtom(refShorthandDefaultPos);
  7482. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  7483. return expr;
  7484. }
  7485. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  7486. return expr;
  7487. }
  7488. return this.parseSubscripts(expr, startPos, startLoc);
  7489. }
  7490. parseSubscripts(base, startPos, startLoc, noCalls) {
  7491. const state = {
  7492. optionalChainMember: false,
  7493. maybeAsyncArrow: this.atPossibleAsync(base),
  7494. stop: false
  7495. };
  7496. do {
  7497. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  7498. state.maybeAsyncArrow = false;
  7499. } while (!state.stop);
  7500. return base;
  7501. }
  7502. parseSubscript(base, startPos, startLoc, noCalls, state) {
  7503. if (!noCalls && this.eat(types.doubleColon)) {
  7504. const node = this.startNodeAt(startPos, startLoc);
  7505. node.object = base;
  7506. node.callee = this.parseNoCallExpr();
  7507. state.stop = true;
  7508. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  7509. } else if (this.match(types.questionDot)) {
  7510. this.expectPlugin("optionalChaining");
  7511. state.optionalChainMember = true;
  7512. if (noCalls && this.lookaheadCharCode() === 40) {
  7513. state.stop = true;
  7514. return base;
  7515. }
  7516. this.next();
  7517. const node = this.startNodeAt(startPos, startLoc);
  7518. if (this.eat(types.bracketL)) {
  7519. node.object = base;
  7520. node.property = this.parseExpression();
  7521. node.computed = true;
  7522. node.optional = true;
  7523. this.expect(types.bracketR);
  7524. return this.finishNode(node, "OptionalMemberExpression");
  7525. } else if (this.eat(types.parenL)) {
  7526. node.callee = base;
  7527. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  7528. node.optional = true;
  7529. return this.finishCallExpression(node, true);
  7530. } else {
  7531. node.object = base;
  7532. node.property = this.parseIdentifier(true);
  7533. node.computed = false;
  7534. node.optional = true;
  7535. return this.finishNode(node, "OptionalMemberExpression");
  7536. }
  7537. } else if (this.eat(types.dot)) {
  7538. const node = this.startNodeAt(startPos, startLoc);
  7539. node.object = base;
  7540. node.property = this.parseMaybePrivateName();
  7541. node.computed = false;
  7542. if (node.property.type === "PrivateName" && node.object.type === "Super") {
  7543. this.raise(startPos, "Private fields can't be accessed on super");
  7544. }
  7545. if (state.optionalChainMember) {
  7546. node.optional = false;
  7547. return this.finishNode(node, "OptionalMemberExpression");
  7548. }
  7549. return this.finishNode(node, "MemberExpression");
  7550. } else if (this.eat(types.bracketL)) {
  7551. const node = this.startNodeAt(startPos, startLoc);
  7552. node.object = base;
  7553. node.property = this.parseExpression();
  7554. node.computed = true;
  7555. this.expect(types.bracketR);
  7556. if (state.optionalChainMember) {
  7557. node.optional = false;
  7558. return this.finishNode(node, "OptionalMemberExpression");
  7559. }
  7560. return this.finishNode(node, "MemberExpression");
  7561. } else if (!noCalls && this.match(types.parenL)) {
  7562. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7563. const oldYieldPos = this.state.yieldPos;
  7564. const oldAwaitPos = this.state.awaitPos;
  7565. this.state.maybeInArrowParameters = true;
  7566. this.state.yieldPos = -1;
  7567. this.state.awaitPos = -1;
  7568. this.next();
  7569. let node = this.startNodeAt(startPos, startLoc);
  7570. node.callee = base;
  7571. node.arguments = this.parseCallExpressionArguments(types.parenR, state.maybeAsyncArrow, base.type === "Import", base.type !== "Super", node);
  7572. this.finishCallExpression(node, state.optionalChainMember);
  7573. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow()) {
  7574. state.stop = true;
  7575. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  7576. this.checkYieldAwaitInDefaultParams();
  7577. this.state.yieldPos = oldYieldPos;
  7578. this.state.awaitPos = oldAwaitPos;
  7579. } else {
  7580. this.toReferencedListDeep(node.arguments);
  7581. if (oldYieldPos !== -1) this.state.yieldPos = oldYieldPos;
  7582. if (!this.isAwaitAllowed() && !oldMaybeInArrowParameters || oldAwaitPos !== -1) {
  7583. this.state.awaitPos = oldAwaitPos;
  7584. }
  7585. }
  7586. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7587. return node;
  7588. } else if (this.match(types.backQuote)) {
  7589. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state);
  7590. } else {
  7591. state.stop = true;
  7592. return base;
  7593. }
  7594. }
  7595. parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments) {
  7596. const node = this.startNodeAt(startPos, startLoc);
  7597. node.tag = base;
  7598. node.quasi = this.parseTemplate(true);
  7599. if (typeArguments) node.typeParameters = typeArguments;
  7600. if (state.optionalChainMember) {
  7601. this.raise(startPos, "Tagged Template Literals are not allowed in optionalChain");
  7602. }
  7603. return this.finishNode(node, "TaggedTemplateExpression");
  7604. }
  7605. atPossibleAsync(base) {
  7606. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && this.input.slice(base.start, base.end) === "async";
  7607. }
  7608. finishCallExpression(node, optional) {
  7609. if (node.callee.type === "Import") {
  7610. if (node.arguments.length !== 1) {
  7611. this.raise(node.start, "import() requires exactly one argument");
  7612. } else {
  7613. const importArg = node.arguments[0];
  7614. if (importArg && importArg.type === "SpreadElement") {
  7615. this.raise(importArg.start, "... is not allowed in import()");
  7616. }
  7617. }
  7618. }
  7619. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  7620. }
  7621. parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport, allowPlaceholder, nodeForExtra) {
  7622. const elts = [];
  7623. let innerParenStart;
  7624. let first = true;
  7625. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7626. this.state.inFSharpPipelineDirectBody = false;
  7627. while (!this.eat(close)) {
  7628. if (first) {
  7629. first = false;
  7630. } else {
  7631. this.expect(types.comma);
  7632. if (this.match(close)) {
  7633. if (dynamicImport) {
  7634. this.raise(this.state.lastTokStart, "Trailing comma is disallowed inside import(...) arguments");
  7635. }
  7636. if (nodeForExtra) {
  7637. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  7638. }
  7639. this.next();
  7640. break;
  7641. }
  7642. }
  7643. if (this.match(types.parenL) && !innerParenStart) {
  7644. innerParenStart = this.state.start;
  7645. }
  7646. elts.push(this.parseExprListItem(false, possibleAsyncArrow ? {
  7647. start: 0
  7648. } : undefined, possibleAsyncArrow ? {
  7649. start: 0
  7650. } : undefined, allowPlaceholder));
  7651. }
  7652. if (possibleAsyncArrow && innerParenStart && this.shouldParseAsyncArrow()) {
  7653. this.unexpected();
  7654. }
  7655. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7656. return elts;
  7657. }
  7658. shouldParseAsyncArrow() {
  7659. return this.match(types.arrow) && !this.canInsertSemicolon();
  7660. }
  7661. parseAsyncArrowFromCallExpression(node, call) {
  7662. var _call$extra;
  7663. this.expect(types.arrow);
  7664. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) === null || _call$extra === void 0 ? void 0 : _call$extra.trailingComma);
  7665. return node;
  7666. }
  7667. parseNoCallExpr() {
  7668. const startPos = this.state.start;
  7669. const startLoc = this.state.startLoc;
  7670. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  7671. }
  7672. parseExprAtom(refShorthandDefaultPos) {
  7673. if (this.state.type === types.slash) this.readRegexp();
  7674. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  7675. let node;
  7676. switch (this.state.type) {
  7677. case types._super:
  7678. node = this.startNode();
  7679. this.next();
  7680. if (this.match(types.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  7681. this.raise(node.start, "super() is only valid inside a class constructor of a subclass. " + "Maybe a typo in the method name ('constructor') or not extending another class?");
  7682. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  7683. this.raise(node.start, "super is only allowed in object methods and classes");
  7684. }
  7685. if (!this.match(types.parenL) && !this.match(types.bracketL) && !this.match(types.dot)) {
  7686. this.raise(node.start, "super can only be used with function calls (i.e. super()) or " + "in property accesses (i.e. super.prop or super[prop])");
  7687. }
  7688. return this.finishNode(node, "Super");
  7689. case types._import:
  7690. node = this.startNode();
  7691. this.next();
  7692. if (this.match(types.dot)) {
  7693. return this.parseImportMetaProperty(node);
  7694. }
  7695. this.expectPlugin("dynamicImport", node.start);
  7696. if (!this.match(types.parenL)) {
  7697. this.unexpected(null, types.parenL);
  7698. }
  7699. return this.finishNode(node, "Import");
  7700. case types._this:
  7701. node = this.startNode();
  7702. this.next();
  7703. return this.finishNode(node, "ThisExpression");
  7704. case types.name:
  7705. {
  7706. node = this.startNode();
  7707. const containsEsc = this.state.containsEsc;
  7708. const id = this.parseIdentifier();
  7709. if (!containsEsc && id.name === "async" && this.match(types._function) && !this.canInsertSemicolon()) {
  7710. this.next();
  7711. return this.parseFunction(node, undefined, true);
  7712. } else if (canBeArrow && !containsEsc && id.name === "async" && this.match(types.name) && !this.canInsertSemicolon()) {
  7713. const params = [this.parseIdentifier()];
  7714. this.expect(types.arrow);
  7715. this.parseArrowExpression(node, params, true);
  7716. return node;
  7717. }
  7718. if (canBeArrow && this.match(types.arrow) && !this.canInsertSemicolon()) {
  7719. this.next();
  7720. this.parseArrowExpression(node, [id], false);
  7721. return node;
  7722. }
  7723. return id;
  7724. }
  7725. case types._do:
  7726. {
  7727. this.expectPlugin("doExpressions");
  7728. const node = this.startNode();
  7729. this.next();
  7730. const oldLabels = this.state.labels;
  7731. this.state.labels = [];
  7732. node.body = this.parseBlock();
  7733. this.state.labels = oldLabels;
  7734. return this.finishNode(node, "DoExpression");
  7735. }
  7736. case types.regexp:
  7737. {
  7738. const value = this.state.value;
  7739. node = this.parseLiteral(value.value, "RegExpLiteral");
  7740. node.pattern = value.pattern;
  7741. node.flags = value.flags;
  7742. return node;
  7743. }
  7744. case types.num:
  7745. return this.parseLiteral(this.state.value, "NumericLiteral");
  7746. case types.bigint:
  7747. return this.parseLiteral(this.state.value, "BigIntLiteral");
  7748. case types.string:
  7749. return this.parseLiteral(this.state.value, "StringLiteral");
  7750. case types._null:
  7751. node = this.startNode();
  7752. this.next();
  7753. return this.finishNode(node, "NullLiteral");
  7754. case types._true:
  7755. case types._false:
  7756. return this.parseBooleanLiteral();
  7757. case types.parenL:
  7758. return this.parseParenAndDistinguishExpression(canBeArrow);
  7759. case types.bracketL:
  7760. {
  7761. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7762. this.state.inFSharpPipelineDirectBody = false;
  7763. node = this.startNode();
  7764. this.next();
  7765. node.elements = this.parseExprList(types.bracketR, true, refShorthandDefaultPos, node);
  7766. if (!this.state.maybeInArrowParameters) {
  7767. this.toReferencedList(node.elements);
  7768. }
  7769. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7770. return this.finishNode(node, "ArrayExpression");
  7771. }
  7772. case types.braceL:
  7773. {
  7774. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7775. this.state.inFSharpPipelineDirectBody = false;
  7776. const ret = this.parseObj(false, refShorthandDefaultPos);
  7777. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7778. return ret;
  7779. }
  7780. case types._function:
  7781. return this.parseFunctionExpression();
  7782. case types.at:
  7783. this.parseDecorators();
  7784. case types._class:
  7785. node = this.startNode();
  7786. this.takeDecorators(node);
  7787. return this.parseClass(node, false);
  7788. case types._new:
  7789. return this.parseNew();
  7790. case types.backQuote:
  7791. return this.parseTemplate(false);
  7792. case types.doubleColon:
  7793. {
  7794. node = this.startNode();
  7795. this.next();
  7796. node.object = null;
  7797. const callee = node.callee = this.parseNoCallExpr();
  7798. if (callee.type === "MemberExpression") {
  7799. return this.finishNode(node, "BindExpression");
  7800. } else {
  7801. throw this.raise(callee.start, "Binding should be performed on object property.");
  7802. }
  7803. }
  7804. case types.hash:
  7805. {
  7806. if (this.state.inPipeline) {
  7807. node = this.startNode();
  7808. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  7809. this.raise(node.start, "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.");
  7810. }
  7811. this.next();
  7812. if (!this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  7813. this.raise(node.start, `Topic reference was used in a lexical context without topic binding`);
  7814. }
  7815. this.registerTopicReference();
  7816. return this.finishNode(node, "PipelinePrimaryTopicReference");
  7817. }
  7818. }
  7819. default:
  7820. throw this.unexpected();
  7821. }
  7822. }
  7823. parseBooleanLiteral() {
  7824. const node = this.startNode();
  7825. node.value = this.match(types._true);
  7826. this.next();
  7827. return this.finishNode(node, "BooleanLiteral");
  7828. }
  7829. parseMaybePrivateName() {
  7830. const isPrivate = this.match(types.hash);
  7831. if (isPrivate) {
  7832. this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
  7833. const node = this.startNode();
  7834. this.next();
  7835. this.assertNoSpace("Unexpected space between # and identifier");
  7836. node.id = this.parseIdentifier(true);
  7837. return this.finishNode(node, "PrivateName");
  7838. } else {
  7839. return this.parseIdentifier(true);
  7840. }
  7841. }
  7842. parseFunctionExpression() {
  7843. const node = this.startNode();
  7844. let meta = this.startNode();
  7845. this.next();
  7846. meta = this.createIdentifier(meta, "function");
  7847. if (this.scope.inGenerator && this.eat(types.dot)) {
  7848. return this.parseMetaProperty(node, meta, "sent");
  7849. }
  7850. return this.parseFunction(node);
  7851. }
  7852. parseMetaProperty(node, meta, propertyName) {
  7853. node.meta = meta;
  7854. if (meta.name === "function" && propertyName === "sent") {
  7855. if (this.isContextual(propertyName)) {
  7856. this.expectPlugin("functionSent");
  7857. } else if (!this.hasPlugin("functionSent")) {
  7858. this.unexpected();
  7859. }
  7860. }
  7861. const containsEsc = this.state.containsEsc;
  7862. node.property = this.parseIdentifier(true);
  7863. if (node.property.name !== propertyName || containsEsc) {
  7864. this.raise(node.property.start, `The only valid meta property for ${meta.name} is ${meta.name}.${propertyName}`);
  7865. }
  7866. return this.finishNode(node, "MetaProperty");
  7867. }
  7868. parseImportMetaProperty(node) {
  7869. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  7870. this.expect(types.dot);
  7871. if (this.isContextual("meta")) {
  7872. this.expectPlugin("importMeta");
  7873. if (!this.inModule) {
  7874. this.raise(id.start, `import.meta may appear only with 'sourceType: "module"'`, {
  7875. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  7876. });
  7877. }
  7878. this.sawUnambiguousESM = true;
  7879. } else if (!this.hasPlugin("importMeta")) {
  7880. this.raise(id.start, `Dynamic imports require a parameter: import('a.js')`);
  7881. }
  7882. return this.parseMetaProperty(node, id, "meta");
  7883. }
  7884. parseLiteral(value, type, startPos, startLoc) {
  7885. startPos = startPos || this.state.start;
  7886. startLoc = startLoc || this.state.startLoc;
  7887. const node = this.startNodeAt(startPos, startLoc);
  7888. this.addExtra(node, "rawValue", value);
  7889. this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
  7890. node.value = value;
  7891. this.next();
  7892. return this.finishNode(node, type);
  7893. }
  7894. parseParenAndDistinguishExpression(canBeArrow) {
  7895. const startPos = this.state.start;
  7896. const startLoc = this.state.startLoc;
  7897. let val;
  7898. this.expect(types.parenL);
  7899. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7900. const oldYieldPos = this.state.yieldPos;
  7901. const oldAwaitPos = this.state.awaitPos;
  7902. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  7903. this.state.maybeInArrowParameters = true;
  7904. this.state.yieldPos = -1;
  7905. this.state.awaitPos = -1;
  7906. this.state.inFSharpPipelineDirectBody = false;
  7907. const innerStartPos = this.state.start;
  7908. const innerStartLoc = this.state.startLoc;
  7909. const exprList = [];
  7910. const refShorthandDefaultPos = {
  7911. start: 0
  7912. };
  7913. const refNeedsArrowPos = {
  7914. start: 0
  7915. };
  7916. let first = true;
  7917. let spreadStart;
  7918. let optionalCommaStart;
  7919. while (!this.match(types.parenR)) {
  7920. if (first) {
  7921. first = false;
  7922. } else {
  7923. this.expect(types.comma, refNeedsArrowPos.start || null);
  7924. if (this.match(types.parenR)) {
  7925. optionalCommaStart = this.state.start;
  7926. break;
  7927. }
  7928. }
  7929. if (this.match(types.ellipsis)) {
  7930. const spreadNodeStartPos = this.state.start;
  7931. const spreadNodeStartLoc = this.state.startLoc;
  7932. spreadStart = this.state.start;
  7933. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  7934. this.checkCommaAfterRest(41);
  7935. break;
  7936. } else {
  7937. exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos));
  7938. }
  7939. }
  7940. const innerEndPos = this.state.start;
  7941. const innerEndLoc = this.state.startLoc;
  7942. this.expect(types.parenR);
  7943. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7944. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  7945. let arrowNode = this.startNodeAt(startPos, startLoc);
  7946. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  7947. this.checkYieldAwaitInDefaultParams();
  7948. this.state.yieldPos = oldYieldPos;
  7949. this.state.awaitPos = oldAwaitPos;
  7950. for (let _i = 0; _i < exprList.length; _i++) {
  7951. const param = exprList[_i];
  7952. if (param.extra && param.extra.parenthesized) {
  7953. this.unexpected(param.extra.parenStart);
  7954. }
  7955. }
  7956. this.parseArrowExpression(arrowNode, exprList, false);
  7957. return arrowNode;
  7958. }
  7959. if (oldYieldPos !== -1) this.state.yieldPos = oldYieldPos;
  7960. if (oldAwaitPos !== -1) this.state.awaitPos = oldAwaitPos;
  7961. if (!exprList.length) {
  7962. this.unexpected(this.state.lastTokStart);
  7963. }
  7964. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  7965. if (spreadStart) this.unexpected(spreadStart);
  7966. if (refShorthandDefaultPos.start) {
  7967. this.unexpected(refShorthandDefaultPos.start);
  7968. }
  7969. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  7970. this.toReferencedListDeep(exprList, true);
  7971. if (exprList.length > 1) {
  7972. val = this.startNodeAt(innerStartPos, innerStartLoc);
  7973. val.expressions = exprList;
  7974. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  7975. } else {
  7976. val = exprList[0];
  7977. }
  7978. if (!this.options.createParenthesizedExpressions) {
  7979. this.addExtra(val, "parenthesized", true);
  7980. this.addExtra(val, "parenStart", startPos);
  7981. return val;
  7982. }
  7983. const parenExpression = this.startNodeAt(startPos, startLoc);
  7984. parenExpression.expression = val;
  7985. this.finishNode(parenExpression, "ParenthesizedExpression");
  7986. return parenExpression;
  7987. }
  7988. shouldParseArrow() {
  7989. return !this.canInsertSemicolon();
  7990. }
  7991. parseArrow(node) {
  7992. if (this.eat(types.arrow)) {
  7993. return node;
  7994. }
  7995. }
  7996. parseParenItem(node, startPos, startLoc) {
  7997. return node;
  7998. }
  7999. parseNew() {
  8000. const node = this.startNode();
  8001. let meta = this.startNode();
  8002. this.next();
  8003. meta = this.createIdentifier(meta, "new");
  8004. if (this.eat(types.dot)) {
  8005. const metaProp = this.parseMetaProperty(node, meta, "target");
  8006. if (!this.scope.inNonArrowFunction && !this.state.inClassProperty) {
  8007. let error = "new.target can only be used in functions";
  8008. if (this.hasPlugin("classProperties")) {
  8009. error += " or class properties";
  8010. }
  8011. this.raise(metaProp.start, error);
  8012. }
  8013. return metaProp;
  8014. }
  8015. node.callee = this.parseNoCallExpr();
  8016. if (node.callee.type === "Import") {
  8017. this.raise(node.callee.start, "Cannot use new with import(...)");
  8018. } else if (node.callee.type === "OptionalMemberExpression" || node.callee.type === "OptionalCallExpression") {
  8019. this.raise(this.state.lastTokEnd, "constructors in/after an Optional Chain are not allowed");
  8020. } else if (this.eat(types.questionDot)) {
  8021. this.raise(this.state.start, "constructors in/after an Optional Chain are not allowed");
  8022. }
  8023. this.parseNewArguments(node);
  8024. return this.finishNode(node, "NewExpression");
  8025. }
  8026. parseNewArguments(node) {
  8027. if (this.eat(types.parenL)) {
  8028. const args = this.parseExprList(types.parenR);
  8029. this.toReferencedList(args);
  8030. node.arguments = args;
  8031. } else {
  8032. node.arguments = [];
  8033. }
  8034. }
  8035. parseTemplateElement(isTagged) {
  8036. const elem = this.startNode();
  8037. if (this.state.value === null) {
  8038. if (!isTagged) {
  8039. this.raise(this.state.invalidTemplateEscapePosition || 0, "Invalid escape sequence in template");
  8040. } else {
  8041. this.state.invalidTemplateEscapePosition = null;
  8042. }
  8043. }
  8044. elem.value = {
  8045. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  8046. cooked: this.state.value
  8047. };
  8048. this.next();
  8049. elem.tail = this.match(types.backQuote);
  8050. return this.finishNode(elem, "TemplateElement");
  8051. }
  8052. parseTemplate(isTagged) {
  8053. const node = this.startNode();
  8054. this.next();
  8055. node.expressions = [];
  8056. let curElt = this.parseTemplateElement(isTagged);
  8057. node.quasis = [curElt];
  8058. while (!curElt.tail) {
  8059. this.expect(types.dollarBraceL);
  8060. node.expressions.push(this.parseExpression());
  8061. this.expect(types.braceR);
  8062. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  8063. }
  8064. this.next();
  8065. return this.finishNode(node, "TemplateLiteral");
  8066. }
  8067. parseObj(isPattern, refShorthandDefaultPos) {
  8068. const propHash = Object.create(null);
  8069. let first = true;
  8070. const node = this.startNode();
  8071. node.properties = [];
  8072. this.next();
  8073. while (!this.eat(types.braceR)) {
  8074. if (first) {
  8075. first = false;
  8076. } else {
  8077. this.expect(types.comma);
  8078. if (this.match(types.braceR)) {
  8079. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  8080. this.next();
  8081. break;
  8082. }
  8083. }
  8084. const prop = this.parseObjectMember(isPattern, refShorthandDefaultPos);
  8085. if (!isPattern) this.checkDuplicatedProto(prop, propHash);
  8086. if (prop.shorthand) {
  8087. this.addExtra(prop, "shorthand", true);
  8088. }
  8089. node.properties.push(prop);
  8090. }
  8091. if (!this.match(types.eq) && propHash.start !== undefined) {
  8092. this.raise(propHash.start, "Redefinition of __proto__ property");
  8093. }
  8094. return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
  8095. }
  8096. isAsyncProp(prop) {
  8097. return !prop.computed && prop.key.type === "Identifier" && prop.key.name === "async" && (this.match(types.name) || this.match(types.num) || this.match(types.string) || this.match(types.bracketL) || this.state.type.keyword || this.match(types.star)) && !this.hasPrecedingLineBreak();
  8098. }
  8099. parseObjectMember(isPattern, refShorthandDefaultPos) {
  8100. let decorators = [];
  8101. if (this.match(types.at)) {
  8102. if (this.hasPlugin("decorators")) {
  8103. this.raise(this.state.start, "Stage 2 decorators disallow object literal property decorators");
  8104. }
  8105. while (this.match(types.at)) {
  8106. decorators.push(this.parseDecorator());
  8107. }
  8108. }
  8109. const prop = this.startNode();
  8110. let isGenerator = false;
  8111. let isAsync = false;
  8112. let startPos;
  8113. let startLoc;
  8114. if (this.match(types.ellipsis)) {
  8115. if (decorators.length) this.unexpected();
  8116. if (isPattern) {
  8117. this.next();
  8118. prop.argument = this.parseIdentifier();
  8119. this.checkCommaAfterRest(125);
  8120. return this.finishNode(prop, "RestElement");
  8121. }
  8122. return this.parseSpread();
  8123. }
  8124. if (decorators.length) {
  8125. prop.decorators = decorators;
  8126. decorators = [];
  8127. }
  8128. prop.method = false;
  8129. if (isPattern || refShorthandDefaultPos) {
  8130. startPos = this.state.start;
  8131. startLoc = this.state.startLoc;
  8132. }
  8133. if (!isPattern) {
  8134. isGenerator = this.eat(types.star);
  8135. }
  8136. const containsEsc = this.state.containsEsc;
  8137. this.parsePropertyName(prop);
  8138. if (!isPattern && !containsEsc && !isGenerator && this.isAsyncProp(prop)) {
  8139. isAsync = true;
  8140. isGenerator = this.eat(types.star);
  8141. this.parsePropertyName(prop);
  8142. } else {
  8143. isAsync = false;
  8144. }
  8145. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  8146. return prop;
  8147. }
  8148. isGetterOrSetterMethod(prop, isPattern) {
  8149. return !isPattern && !prop.computed && prop.key.type === "Identifier" && (prop.key.name === "get" || prop.key.name === "set") && (this.match(types.string) || this.match(types.num) || this.match(types.bracketL) || this.match(types.name) || !!this.state.type.keyword);
  8150. }
  8151. getGetterSetterExpectedParamCount(method) {
  8152. return method.kind === "get" ? 0 : 1;
  8153. }
  8154. checkGetterSetterParams(method) {
  8155. const paramCount = this.getGetterSetterExpectedParamCount(method);
  8156. const start = method.start;
  8157. if (method.params.length !== paramCount) {
  8158. if (method.kind === "get") {
  8159. this.raise(start, "getter must not have any formal parameters");
  8160. } else {
  8161. this.raise(start, "setter must have exactly one formal parameter");
  8162. }
  8163. }
  8164. if (method.kind === "set" && method.params[method.params.length - 1].type === "RestElement") {
  8165. this.raise(start, "setter function argument must not be a rest parameter");
  8166. }
  8167. }
  8168. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  8169. if (isAsync || isGenerator || this.match(types.parenL)) {
  8170. if (isPattern) this.unexpected();
  8171. prop.kind = "method";
  8172. prop.method = true;
  8173. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  8174. }
  8175. if (!containsEsc && this.isGetterOrSetterMethod(prop, isPattern)) {
  8176. if (isGenerator || isAsync) this.unexpected();
  8177. prop.kind = prop.key.name;
  8178. this.parsePropertyName(prop);
  8179. this.parseMethod(prop, false, false, false, false, "ObjectMethod");
  8180. this.checkGetterSetterParams(prop);
  8181. return prop;
  8182. }
  8183. }
  8184. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  8185. prop.shorthand = false;
  8186. if (this.eat(types.colon)) {
  8187. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
  8188. return this.finishNode(prop, "ObjectProperty");
  8189. }
  8190. if (!prop.computed && prop.key.type === "Identifier") {
  8191. this.checkReservedWord(prop.key.name, prop.key.start, true, true);
  8192. if (isPattern) {
  8193. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  8194. } else if (this.match(types.eq) && refShorthandDefaultPos) {
  8195. if (!refShorthandDefaultPos.start) {
  8196. refShorthandDefaultPos.start = this.state.start;
  8197. }
  8198. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  8199. } else {
  8200. prop.value = prop.key.__clone();
  8201. }
  8202. prop.shorthand = true;
  8203. return this.finishNode(prop, "ObjectProperty");
  8204. }
  8205. }
  8206. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  8207. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  8208. if (!node) this.unexpected();
  8209. return node;
  8210. }
  8211. parsePropertyName(prop) {
  8212. if (this.eat(types.bracketL)) {
  8213. prop.computed = true;
  8214. prop.key = this.parseMaybeAssign();
  8215. this.expect(types.bracketR);
  8216. } else {
  8217. const oldInPropertyName = this.state.inPropertyName;
  8218. this.state.inPropertyName = true;
  8219. prop.key = this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseMaybePrivateName();
  8220. if (prop.key.type !== "PrivateName") {
  8221. prop.computed = false;
  8222. }
  8223. this.state.inPropertyName = oldInPropertyName;
  8224. }
  8225. return prop.key;
  8226. }
  8227. initFunction(node, isAsync) {
  8228. node.id = null;
  8229. node.generator = false;
  8230. node.async = !!isAsync;
  8231. }
  8232. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  8233. const oldYieldPos = this.state.yieldPos;
  8234. const oldAwaitPos = this.state.awaitPos;
  8235. this.state.yieldPos = -1;
  8236. this.state.awaitPos = -1;
  8237. this.initFunction(node, isAsync);
  8238. node.generator = !!isGenerator;
  8239. const allowModifiers = isConstructor;
  8240. this.scope.enter(functionFlags(isAsync, node.generator) | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  8241. this.parseFunctionParams(node, allowModifiers);
  8242. this.checkYieldAwaitInDefaultParams();
  8243. this.parseFunctionBodyAndFinish(node, type, true);
  8244. this.scope.exit();
  8245. this.state.yieldPos = oldYieldPos;
  8246. this.state.awaitPos = oldAwaitPos;
  8247. return node;
  8248. }
  8249. parseArrowExpression(node, params, isAsync, trailingCommaPos) {
  8250. this.scope.enter(functionFlags(isAsync, false) | SCOPE_ARROW);
  8251. this.initFunction(node, isAsync);
  8252. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8253. const oldYieldPos = this.state.yieldPos;
  8254. const oldAwaitPos = this.state.awaitPos;
  8255. this.state.maybeInArrowParameters = false;
  8256. this.state.yieldPos = -1;
  8257. this.state.awaitPos = -1;
  8258. if (params) this.setArrowFunctionParameters(node, params, trailingCommaPos);
  8259. this.parseFunctionBody(node, true);
  8260. this.scope.exit();
  8261. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8262. this.state.yieldPos = oldYieldPos;
  8263. this.state.awaitPos = oldAwaitPos;
  8264. return this.finishNode(node, "ArrowFunctionExpression");
  8265. }
  8266. setArrowFunctionParameters(node, params, trailingCommaPos) {
  8267. node.params = this.toAssignableList(params, true, "arrow function parameters", trailingCommaPos);
  8268. }
  8269. isStrictBody(node) {
  8270. const isBlockStatement = node.body.type === "BlockStatement";
  8271. if (isBlockStatement && node.body.directives.length) {
  8272. for (let _i2 = 0, _node$body$directives = node.body.directives; _i2 < _node$body$directives.length; _i2++) {
  8273. const directive = _node$body$directives[_i2];
  8274. if (directive.value.value === "use strict") {
  8275. return true;
  8276. }
  8277. }
  8278. }
  8279. return false;
  8280. }
  8281. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8282. this.parseFunctionBody(node, false, isMethod);
  8283. this.finishNode(node, type);
  8284. }
  8285. parseFunctionBody(node, allowExpression, isMethod = false) {
  8286. const isExpression = allowExpression && !this.match(types.braceL);
  8287. const oldStrict = this.state.strict;
  8288. let useStrict = false;
  8289. const oldInParameters = this.state.inParameters;
  8290. this.state.inParameters = false;
  8291. if (isExpression) {
  8292. node.body = this.parseMaybeAssign();
  8293. this.checkParams(node, false, allowExpression, false);
  8294. } else {
  8295. const nonSimple = !this.isSimpleParamList(node.params);
  8296. if (!oldStrict || nonSimple) {
  8297. useStrict = this.strictDirective(this.state.end);
  8298. if (useStrict && nonSimple) {
  8299. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  8300. this.raise(errorPos, "Illegal 'use strict' directive in function with non-simple parameter list");
  8301. }
  8302. }
  8303. const oldLabels = this.state.labels;
  8304. this.state.labels = [];
  8305. if (useStrict) this.state.strict = true;
  8306. this.checkParams(node, !oldStrict && !useStrict && !allowExpression && !isMethod && !nonSimple, allowExpression, !oldStrict && useStrict);
  8307. node.body = this.parseBlock(true, false);
  8308. this.state.labels = oldLabels;
  8309. }
  8310. this.state.inParameters = oldInParameters;
  8311. if (this.state.strict && node.id) {
  8312. this.checkLVal(node.id, BIND_OUTSIDE, undefined, "function name", undefined, !oldStrict && useStrict);
  8313. }
  8314. this.state.strict = oldStrict;
  8315. }
  8316. isSimpleParamList(params) {
  8317. for (let i = 0, len = params.length; i < len; i++) {
  8318. if (params[i].type !== "Identifier") return false;
  8319. }
  8320. return true;
  8321. }
  8322. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  8323. const nameHash = Object.create(null);
  8324. for (let i = 0; i < node.params.length; i++) {
  8325. this.checkLVal(node.params[i], BIND_VAR, allowDuplicates ? null : nameHash, "function parameter list", undefined, strictModeChanged);
  8326. }
  8327. }
  8328. parseExprList(close, allowEmpty, refShorthandDefaultPos, nodeForExtra) {
  8329. const elts = [];
  8330. let first = true;
  8331. while (!this.eat(close)) {
  8332. if (first) {
  8333. first = false;
  8334. } else {
  8335. this.expect(types.comma);
  8336. if (this.match(close)) {
  8337. if (nodeForExtra) {
  8338. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  8339. }
  8340. this.next();
  8341. break;
  8342. }
  8343. }
  8344. elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
  8345. }
  8346. return elts;
  8347. }
  8348. parseExprListItem(allowEmpty, refShorthandDefaultPos, refNeedsArrowPos, allowPlaceholder) {
  8349. let elt;
  8350. if (allowEmpty && this.match(types.comma)) {
  8351. elt = null;
  8352. } else if (this.match(types.ellipsis)) {
  8353. const spreadNodeStartPos = this.state.start;
  8354. const spreadNodeStartLoc = this.state.startLoc;
  8355. elt = this.parseParenItem(this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
  8356. } else if (this.match(types.question)) {
  8357. this.expectPlugin("partialApplication");
  8358. if (!allowPlaceholder) {
  8359. this.raise(this.state.start, "Unexpected argument placeholder");
  8360. }
  8361. const node = this.startNode();
  8362. this.next();
  8363. elt = this.finishNode(node, "ArgumentPlaceholder");
  8364. } else {
  8365. elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos);
  8366. }
  8367. return elt;
  8368. }
  8369. parseIdentifier(liberal) {
  8370. const node = this.startNode();
  8371. const name = this.parseIdentifierName(node.start, liberal);
  8372. return this.createIdentifier(node, name);
  8373. }
  8374. createIdentifier(node, name) {
  8375. node.name = name;
  8376. node.loc.identifierName = name;
  8377. return this.finishNode(node, "Identifier");
  8378. }
  8379. parseIdentifierName(pos, liberal) {
  8380. let name;
  8381. if (this.match(types.name)) {
  8382. name = this.state.value;
  8383. } else if (this.state.type.keyword) {
  8384. name = this.state.type.keyword;
  8385. if ((name === "class" || name === "function") && (this.state.lastTokEnd !== this.state.lastTokStart + 1 || this.input.charCodeAt(this.state.lastTokStart) !== 46)) {
  8386. this.state.context.pop();
  8387. }
  8388. } else {
  8389. throw this.unexpected();
  8390. }
  8391. if (liberal) {
  8392. this.state.type = types.name;
  8393. } else {
  8394. this.checkReservedWord(name, this.state.start, !!this.state.type.keyword, false);
  8395. }
  8396. this.next();
  8397. return name;
  8398. }
  8399. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  8400. if (this.scope.inGenerator && word === "yield") {
  8401. this.raise(startLoc, "Can not use 'yield' as identifier inside a generator");
  8402. return;
  8403. }
  8404. if (word === "await") {
  8405. if (this.scope.inAsync) {
  8406. this.raise(startLoc, "Can not use 'await' as identifier inside an async function");
  8407. return;
  8408. }
  8409. if (this.state.awaitPos === -1 && (this.state.maybeInArrowParameters || this.isAwaitAllowed())) {
  8410. this.state.awaitPos = this.state.start;
  8411. }
  8412. }
  8413. if (this.state.inClassProperty && word === "arguments") {
  8414. this.raise(startLoc, "'arguments' is not allowed in class field initializer");
  8415. return;
  8416. }
  8417. if (checkKeywords && isKeyword(word)) {
  8418. this.raise(startLoc, `Unexpected keyword '${word}'`);
  8419. return;
  8420. }
  8421. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  8422. if (reservedTest(word, this.inModule)) {
  8423. if (!this.scope.inAsync && word === "await") {
  8424. this.raise(startLoc, "Can not use keyword 'await' outside an async function");
  8425. } else {
  8426. this.raise(startLoc, `Unexpected reserved word '${word}'`);
  8427. }
  8428. }
  8429. }
  8430. isAwaitAllowed() {
  8431. if (this.scope.inFunction) return this.scope.inAsync;
  8432. if (this.options.allowAwaitOutsideFunction) return true;
  8433. if (this.hasPlugin("topLevelAwait")) return this.inModule;
  8434. return false;
  8435. }
  8436. parseAwait() {
  8437. const node = this.startNode();
  8438. this.next();
  8439. if (this.state.inParameters) {
  8440. this.raise(node.start, "await is not allowed in async function parameters");
  8441. } else if (this.state.awaitPos === -1) {
  8442. this.state.awaitPos = node.start;
  8443. }
  8444. if (this.eat(types.star)) {
  8445. this.raise(node.start, "await* has been removed from the async functions proposal. Use Promise.all() instead.");
  8446. }
  8447. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  8448. if (this.hasPrecedingLineBreak() || this.match(types.plusMin) || this.match(types.parenL) || this.match(types.bracketL) || this.match(types.backQuote) || this.match(types.regexp) || this.match(types.slash) || this.hasPlugin("v8intrinsic") && this.match(types.modulo)) {
  8449. this.ambiguousScriptDifferentAst = true;
  8450. } else {
  8451. this.sawUnambiguousESM = true;
  8452. }
  8453. }
  8454. if (!this.state.soloAwait) {
  8455. node.argument = this.parseMaybeUnary();
  8456. }
  8457. return this.finishNode(node, "AwaitExpression");
  8458. }
  8459. parseYield(noIn) {
  8460. const node = this.startNode();
  8461. if (this.state.inParameters) {
  8462. this.raise(node.start, "yield is not allowed in generator parameters");
  8463. } else if (this.state.yieldPos === -1) {
  8464. this.state.yieldPos = node.start;
  8465. }
  8466. this.next();
  8467. if (this.match(types.semi) || !this.match(types.star) && !this.state.type.startsExpr || this.hasPrecedingLineBreak()) {
  8468. node.delegate = false;
  8469. node.argument = null;
  8470. } else {
  8471. node.delegate = this.eat(types.star);
  8472. node.argument = this.parseMaybeAssign(noIn);
  8473. }
  8474. return this.finishNode(node, "YieldExpression");
  8475. }
  8476. checkPipelineAtInfixOperator(left, leftStartPos) {
  8477. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  8478. if (left.type === "SequenceExpression") {
  8479. this.raise(leftStartPos, `Pipeline head should not be a comma-separated sequence expression`);
  8480. }
  8481. }
  8482. }
  8483. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  8484. const pipelineStyle = this.checkSmartPipelineBodyStyle(childExpression);
  8485. this.checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos);
  8486. return this.parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc);
  8487. }
  8488. checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos) {
  8489. if (this.match(types.arrow)) {
  8490. throw this.raise(this.state.start, `Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized`);
  8491. } else if (pipelineStyle === "PipelineTopicExpression" && childExpression.type === "SequenceExpression") {
  8492. this.raise(startPos, `Pipeline body may not be a comma-separated sequence expression`);
  8493. }
  8494. }
  8495. parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc) {
  8496. const bodyNode = this.startNodeAt(startPos, startLoc);
  8497. switch (pipelineStyle) {
  8498. case "PipelineBareFunction":
  8499. bodyNode.callee = childExpression;
  8500. break;
  8501. case "PipelineBareConstructor":
  8502. bodyNode.callee = childExpression.callee;
  8503. break;
  8504. case "PipelineBareAwaitedFunction":
  8505. bodyNode.callee = childExpression.argument;
  8506. break;
  8507. case "PipelineTopicExpression":
  8508. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  8509. this.raise(startPos, `Pipeline is in topic style but does not use topic reference`);
  8510. }
  8511. bodyNode.expression = childExpression;
  8512. break;
  8513. default:
  8514. throw new Error(`Internal @babel/parser error: Unknown pipeline style (${pipelineStyle})`);
  8515. }
  8516. return this.finishNode(bodyNode, pipelineStyle);
  8517. }
  8518. checkSmartPipelineBodyStyle(expression) {
  8519. switch (expression.type) {
  8520. default:
  8521. return this.isSimpleReference(expression) ? "PipelineBareFunction" : "PipelineTopicExpression";
  8522. }
  8523. }
  8524. isSimpleReference(expression) {
  8525. switch (expression.type) {
  8526. case "MemberExpression":
  8527. return !expression.computed && this.isSimpleReference(expression.object);
  8528. case "Identifier":
  8529. return true;
  8530. default:
  8531. return false;
  8532. }
  8533. }
  8534. withTopicPermittingContext(callback) {
  8535. const outerContextTopicState = this.state.topicContext;
  8536. this.state.topicContext = {
  8537. maxNumOfResolvableTopics: 1,
  8538. maxTopicIndex: null
  8539. };
  8540. try {
  8541. return callback();
  8542. } finally {
  8543. this.state.topicContext = outerContextTopicState;
  8544. }
  8545. }
  8546. withTopicForbiddingContext(callback) {
  8547. const outerContextTopicState = this.state.topicContext;
  8548. this.state.topicContext = {
  8549. maxNumOfResolvableTopics: 0,
  8550. maxTopicIndex: null
  8551. };
  8552. try {
  8553. return callback();
  8554. } finally {
  8555. this.state.topicContext = outerContextTopicState;
  8556. }
  8557. }
  8558. withSoloAwaitPermittingContext(callback) {
  8559. const outerContextSoloAwaitState = this.state.soloAwait;
  8560. this.state.soloAwait = true;
  8561. try {
  8562. return callback();
  8563. } finally {
  8564. this.state.soloAwait = outerContextSoloAwaitState;
  8565. }
  8566. }
  8567. registerTopicReference() {
  8568. this.state.topicContext.maxTopicIndex = 0;
  8569. }
  8570. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  8571. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  8572. }
  8573. topicReferenceWasUsedInCurrentTopicContext() {
  8574. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  8575. }
  8576. parseFSharpPipelineBody(prec, noIn) {
  8577. const startPos = this.state.start;
  8578. const startLoc = this.state.startLoc;
  8579. this.state.potentialArrowAt = this.state.start;
  8580. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  8581. this.state.inFSharpPipelineDirectBody = true;
  8582. const ret = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec, noIn);
  8583. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  8584. return ret;
  8585. }
  8586. }
  8587. const loopLabel = {
  8588. kind: "loop"
  8589. },
  8590. switchLabel = {
  8591. kind: "switch"
  8592. };
  8593. const FUNC_NO_FLAGS = 0b000,
  8594. FUNC_STATEMENT = 0b001,
  8595. FUNC_HANGING_STATEMENT = 0b010,
  8596. FUNC_NULLABLE_ID = 0b100;
  8597. class StatementParser extends ExpressionParser {
  8598. parseTopLevel(file, program) {
  8599. program.sourceType = this.options.sourceType;
  8600. program.interpreter = this.parseInterpreterDirective();
  8601. this.parseBlockBody(program, true, true, types.eof);
  8602. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  8603. for (let _i = 0, _Array$from = Array.from(this.scope.undefinedExports); _i < _Array$from.length; _i++) {
  8604. const [name] = _Array$from[_i];
  8605. const pos = this.scope.undefinedExports.get(name);
  8606. this.raise(pos, `Export '${name}' is not defined`);
  8607. }
  8608. }
  8609. file.program = this.finishNode(program, "Program");
  8610. file.comments = this.state.comments;
  8611. if (this.options.tokens) file.tokens = this.state.tokens;
  8612. return this.finishNode(file, "File");
  8613. }
  8614. stmtToDirective(stmt) {
  8615. const expr = stmt.expression;
  8616. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  8617. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  8618. const raw = this.input.slice(expr.start, expr.end);
  8619. const val = directiveLiteral.value = raw.slice(1, -1);
  8620. this.addExtra(directiveLiteral, "raw", raw);
  8621. this.addExtra(directiveLiteral, "rawValue", val);
  8622. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  8623. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  8624. }
  8625. parseInterpreterDirective() {
  8626. if (!this.match(types.interpreterDirective)) {
  8627. return null;
  8628. }
  8629. const node = this.startNode();
  8630. node.value = this.state.value;
  8631. this.next();
  8632. return this.finishNode(node, "InterpreterDirective");
  8633. }
  8634. isLet(context) {
  8635. if (!this.isContextual("let")) {
  8636. return false;
  8637. }
  8638. const next = this.nextTokenStart();
  8639. const nextCh = this.input.charCodeAt(next);
  8640. if (nextCh === 91) return true;
  8641. if (context) return false;
  8642. if (nextCh === 123) return true;
  8643. if (isIdentifierStart(nextCh)) {
  8644. let pos = next + 1;
  8645. while (isIdentifierChar(this.input.charCodeAt(pos))) {
  8646. ++pos;
  8647. }
  8648. const ident = this.input.slice(next, pos);
  8649. if (!keywordRelationalOperator.test(ident)) return true;
  8650. }
  8651. return false;
  8652. }
  8653. parseStatement(context, topLevel) {
  8654. if (this.match(types.at)) {
  8655. this.parseDecorators(true);
  8656. }
  8657. return this.parseStatementContent(context, topLevel);
  8658. }
  8659. parseStatementContent(context, topLevel) {
  8660. let starttype = this.state.type;
  8661. const node = this.startNode();
  8662. let kind;
  8663. if (this.isLet(context)) {
  8664. starttype = types._var;
  8665. kind = "let";
  8666. }
  8667. switch (starttype) {
  8668. case types._break:
  8669. case types._continue:
  8670. return this.parseBreakContinueStatement(node, starttype.keyword);
  8671. case types._debugger:
  8672. return this.parseDebuggerStatement(node);
  8673. case types._do:
  8674. return this.parseDoStatement(node);
  8675. case types._for:
  8676. return this.parseForStatement(node);
  8677. case types._function:
  8678. if (this.lookaheadCharCode() === 46) break;
  8679. if (context) {
  8680. if (this.state.strict) {
  8681. this.raise(this.state.start, "In strict mode code, functions can only be declared at top level or inside a block");
  8682. } else if (context !== "if" && context !== "label") {
  8683. this.raise(this.state.start, "In non-strict mode code, functions can only be declared at top level, " + "inside a block, or as the body of an if statement");
  8684. }
  8685. }
  8686. return this.parseFunctionStatement(node, false, !context);
  8687. case types._class:
  8688. if (context) this.unexpected();
  8689. return this.parseClass(node, true);
  8690. case types._if:
  8691. return this.parseIfStatement(node);
  8692. case types._return:
  8693. return this.parseReturnStatement(node);
  8694. case types._switch:
  8695. return this.parseSwitchStatement(node);
  8696. case types._throw:
  8697. return this.parseThrowStatement(node);
  8698. case types._try:
  8699. return this.parseTryStatement(node);
  8700. case types._const:
  8701. case types._var:
  8702. kind = kind || this.state.value;
  8703. if (context && kind !== "var") {
  8704. this.raise(this.state.start, "Lexical declaration cannot appear in a single-statement context");
  8705. }
  8706. return this.parseVarStatement(node, kind);
  8707. case types._while:
  8708. return this.parseWhileStatement(node);
  8709. case types._with:
  8710. return this.parseWithStatement(node);
  8711. case types.braceL:
  8712. return this.parseBlock();
  8713. case types.semi:
  8714. return this.parseEmptyStatement(node);
  8715. case types._export:
  8716. case types._import:
  8717. {
  8718. const nextTokenCharCode = this.lookaheadCharCode();
  8719. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  8720. break;
  8721. }
  8722. if (!this.options.allowImportExportEverywhere && !topLevel) {
  8723. this.raise(this.state.start, "'import' and 'export' may only appear at the top level");
  8724. }
  8725. this.next();
  8726. let result;
  8727. if (starttype === types._import) {
  8728. result = this.parseImport(node);
  8729. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  8730. this.sawUnambiguousESM = true;
  8731. }
  8732. } else {
  8733. result = this.parseExport(node);
  8734. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  8735. this.sawUnambiguousESM = true;
  8736. }
  8737. }
  8738. this.assertModuleNodeAllowed(node);
  8739. return result;
  8740. }
  8741. default:
  8742. {
  8743. if (this.isAsyncFunction()) {
  8744. if (context) {
  8745. this.raise(this.state.start, "Async functions can only be declared at the top level or inside a block");
  8746. }
  8747. this.next();
  8748. return this.parseFunctionStatement(node, true, !context);
  8749. }
  8750. }
  8751. }
  8752. const maybeName = this.state.value;
  8753. const expr = this.parseExpression();
  8754. if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon)) {
  8755. return this.parseLabeledStatement(node, maybeName, expr, context);
  8756. } else {
  8757. return this.parseExpressionStatement(node, expr);
  8758. }
  8759. }
  8760. assertModuleNodeAllowed(node) {
  8761. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  8762. this.raise(node.start, `'import' and 'export' may appear only with 'sourceType: "module"'`, {
  8763. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  8764. });
  8765. }
  8766. }
  8767. takeDecorators(node) {
  8768. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  8769. if (decorators.length) {
  8770. node.decorators = decorators;
  8771. this.resetStartLocationFromNode(node, decorators[0]);
  8772. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  8773. }
  8774. }
  8775. canHaveLeadingDecorator() {
  8776. return this.match(types._class);
  8777. }
  8778. parseDecorators(allowExport) {
  8779. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  8780. while (this.match(types.at)) {
  8781. const decorator = this.parseDecorator();
  8782. currentContextDecorators.push(decorator);
  8783. }
  8784. if (this.match(types._export)) {
  8785. if (!allowExport) {
  8786. this.unexpected();
  8787. }
  8788. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  8789. this.raise(this.state.start, "Using the export keyword between a decorator and a class is not allowed. " + "Please use `export @dec class` instead.");
  8790. }
  8791. } else if (!this.canHaveLeadingDecorator()) {
  8792. throw this.raise(this.state.start, "Leading decorators must be attached to a class declaration");
  8793. }
  8794. }
  8795. parseDecorator() {
  8796. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  8797. const node = this.startNode();
  8798. this.next();
  8799. if (this.hasPlugin("decorators")) {
  8800. this.state.decoratorStack.push([]);
  8801. const startPos = this.state.start;
  8802. const startLoc = this.state.startLoc;
  8803. let expr;
  8804. if (this.eat(types.parenL)) {
  8805. expr = this.parseExpression();
  8806. this.expect(types.parenR);
  8807. } else {
  8808. expr = this.parseIdentifier(false);
  8809. while (this.eat(types.dot)) {
  8810. const node = this.startNodeAt(startPos, startLoc);
  8811. node.object = expr;
  8812. node.property = this.parseIdentifier(true);
  8813. node.computed = false;
  8814. expr = this.finishNode(node, "MemberExpression");
  8815. }
  8816. }
  8817. node.expression = this.parseMaybeDecoratorArguments(expr);
  8818. this.state.decoratorStack.pop();
  8819. } else {
  8820. node.expression = this.parseExprSubscripts();
  8821. }
  8822. return this.finishNode(node, "Decorator");
  8823. }
  8824. parseMaybeDecoratorArguments(expr) {
  8825. if (this.eat(types.parenL)) {
  8826. const node = this.startNodeAtNode(expr);
  8827. node.callee = expr;
  8828. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  8829. this.toReferencedList(node.arguments);
  8830. return this.finishNode(node, "CallExpression");
  8831. }
  8832. return expr;
  8833. }
  8834. parseBreakContinueStatement(node, keyword) {
  8835. const isBreak = keyword === "break";
  8836. this.next();
  8837. if (this.isLineTerminator()) {
  8838. node.label = null;
  8839. } else {
  8840. node.label = this.parseIdentifier();
  8841. this.semicolon();
  8842. }
  8843. this.verifyBreakContinue(node, keyword);
  8844. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  8845. }
  8846. verifyBreakContinue(node, keyword) {
  8847. const isBreak = keyword === "break";
  8848. let i;
  8849. for (i = 0; i < this.state.labels.length; ++i) {
  8850. const lab = this.state.labels[i];
  8851. if (node.label == null || lab.name === node.label.name) {
  8852. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  8853. if (node.label && isBreak) break;
  8854. }
  8855. }
  8856. if (i === this.state.labels.length) {
  8857. this.raise(node.start, "Unsyntactic " + keyword);
  8858. }
  8859. }
  8860. parseDebuggerStatement(node) {
  8861. this.next();
  8862. this.semicolon();
  8863. return this.finishNode(node, "DebuggerStatement");
  8864. }
  8865. parseHeaderExpression() {
  8866. this.expect(types.parenL);
  8867. const val = this.parseExpression();
  8868. this.expect(types.parenR);
  8869. return val;
  8870. }
  8871. parseDoStatement(node) {
  8872. this.next();
  8873. this.state.labels.push(loopLabel);
  8874. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  8875. this.state.labels.pop();
  8876. this.expect(types._while);
  8877. node.test = this.parseHeaderExpression();
  8878. this.eat(types.semi);
  8879. return this.finishNode(node, "DoWhileStatement");
  8880. }
  8881. parseForStatement(node) {
  8882. this.next();
  8883. this.state.labels.push(loopLabel);
  8884. let awaitAt = -1;
  8885. if (this.isAwaitAllowed() && this.eatContextual("await")) {
  8886. awaitAt = this.state.lastTokStart;
  8887. }
  8888. this.scope.enter(SCOPE_OTHER);
  8889. this.expect(types.parenL);
  8890. if (this.match(types.semi)) {
  8891. if (awaitAt > -1) {
  8892. this.unexpected(awaitAt);
  8893. }
  8894. return this.parseFor(node, null);
  8895. }
  8896. const isLet = this.isLet();
  8897. if (this.match(types._var) || this.match(types._const) || isLet) {
  8898. const init = this.startNode();
  8899. const kind = isLet ? "let" : this.state.value;
  8900. this.next();
  8901. this.parseVar(init, true, kind);
  8902. this.finishNode(init, "VariableDeclaration");
  8903. if ((this.match(types._in) || this.isContextual("of")) && init.declarations.length === 1) {
  8904. return this.parseForIn(node, init, awaitAt);
  8905. }
  8906. if (awaitAt > -1) {
  8907. this.unexpected(awaitAt);
  8908. }
  8909. return this.parseFor(node, init);
  8910. }
  8911. const refShorthandDefaultPos = {
  8912. start: 0
  8913. };
  8914. const init = this.parseExpression(true, refShorthandDefaultPos);
  8915. if (this.match(types._in) || this.isContextual("of")) {
  8916. const description = this.isContextual("of") ? "for-of statement" : "for-in statement";
  8917. this.toAssignable(init, undefined, description);
  8918. this.checkLVal(init, undefined, undefined, description);
  8919. return this.parseForIn(node, init, awaitAt);
  8920. } else if (refShorthandDefaultPos.start) {
  8921. this.unexpected(refShorthandDefaultPos.start);
  8922. }
  8923. if (awaitAt > -1) {
  8924. this.unexpected(awaitAt);
  8925. }
  8926. return this.parseFor(node, init);
  8927. }
  8928. parseFunctionStatement(node, isAsync, declarationPosition) {
  8929. this.next();
  8930. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  8931. }
  8932. parseIfStatement(node) {
  8933. this.next();
  8934. node.test = this.parseHeaderExpression();
  8935. node.consequent = this.parseStatement("if");
  8936. node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
  8937. return this.finishNode(node, "IfStatement");
  8938. }
  8939. parseReturnStatement(node) {
  8940. if (!this.scope.inFunction && !this.options.allowReturnOutsideFunction) {
  8941. this.raise(this.state.start, "'return' outside of function");
  8942. }
  8943. this.next();
  8944. if (this.isLineTerminator()) {
  8945. node.argument = null;
  8946. } else {
  8947. node.argument = this.parseExpression();
  8948. this.semicolon();
  8949. }
  8950. return this.finishNode(node, "ReturnStatement");
  8951. }
  8952. parseSwitchStatement(node) {
  8953. this.next();
  8954. node.discriminant = this.parseHeaderExpression();
  8955. const cases = node.cases = [];
  8956. this.expect(types.braceL);
  8957. this.state.labels.push(switchLabel);
  8958. this.scope.enter(SCOPE_OTHER);
  8959. let cur;
  8960. for (let sawDefault; !this.match(types.braceR);) {
  8961. if (this.match(types._case) || this.match(types._default)) {
  8962. const isCase = this.match(types._case);
  8963. if (cur) this.finishNode(cur, "SwitchCase");
  8964. cases.push(cur = this.startNode());
  8965. cur.consequent = [];
  8966. this.next();
  8967. if (isCase) {
  8968. cur.test = this.parseExpression();
  8969. } else {
  8970. if (sawDefault) {
  8971. this.raise(this.state.lastTokStart, "Multiple default clauses");
  8972. }
  8973. sawDefault = true;
  8974. cur.test = null;
  8975. }
  8976. this.expect(types.colon);
  8977. } else {
  8978. if (cur) {
  8979. cur.consequent.push(this.parseStatement(null));
  8980. } else {
  8981. this.unexpected();
  8982. }
  8983. }
  8984. }
  8985. this.scope.exit();
  8986. if (cur) this.finishNode(cur, "SwitchCase");
  8987. this.next();
  8988. this.state.labels.pop();
  8989. return this.finishNode(node, "SwitchStatement");
  8990. }
  8991. parseThrowStatement(node) {
  8992. this.next();
  8993. if (lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) {
  8994. this.raise(this.state.lastTokEnd, "Illegal newline after throw");
  8995. }
  8996. node.argument = this.parseExpression();
  8997. this.semicolon();
  8998. return this.finishNode(node, "ThrowStatement");
  8999. }
  9000. parseTryStatement(node) {
  9001. this.next();
  9002. node.block = this.parseBlock();
  9003. node.handler = null;
  9004. if (this.match(types._catch)) {
  9005. const clause = this.startNode();
  9006. this.next();
  9007. if (this.match(types.parenL)) {
  9008. this.expect(types.parenL);
  9009. clause.param = this.parseBindingAtom();
  9010. const simple = clause.param.type === "Identifier";
  9011. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  9012. this.checkLVal(clause.param, BIND_LEXICAL, null, "catch clause");
  9013. this.expect(types.parenR);
  9014. } else {
  9015. clause.param = null;
  9016. this.scope.enter(SCOPE_OTHER);
  9017. }
  9018. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  9019. this.scope.exit();
  9020. node.handler = this.finishNode(clause, "CatchClause");
  9021. }
  9022. node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
  9023. if (!node.handler && !node.finalizer) {
  9024. this.raise(node.start, "Missing catch or finally clause");
  9025. }
  9026. return this.finishNode(node, "TryStatement");
  9027. }
  9028. parseVarStatement(node, kind) {
  9029. this.next();
  9030. this.parseVar(node, false, kind);
  9031. this.semicolon();
  9032. return this.finishNode(node, "VariableDeclaration");
  9033. }
  9034. parseWhileStatement(node) {
  9035. this.next();
  9036. node.test = this.parseHeaderExpression();
  9037. this.state.labels.push(loopLabel);
  9038. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  9039. this.state.labels.pop();
  9040. return this.finishNode(node, "WhileStatement");
  9041. }
  9042. parseWithStatement(node) {
  9043. if (this.state.strict) {
  9044. this.raise(this.state.start, "'with' in strict mode");
  9045. }
  9046. this.next();
  9047. node.object = this.parseHeaderExpression();
  9048. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  9049. return this.finishNode(node, "WithStatement");
  9050. }
  9051. parseEmptyStatement(node) {
  9052. this.next();
  9053. return this.finishNode(node, "EmptyStatement");
  9054. }
  9055. parseLabeledStatement(node, maybeName, expr, context) {
  9056. for (let _i2 = 0, _this$state$labels = this.state.labels; _i2 < _this$state$labels.length; _i2++) {
  9057. const label = _this$state$labels[_i2];
  9058. if (label.name === maybeName) {
  9059. this.raise(expr.start, `Label '${maybeName}' is already declared`);
  9060. }
  9061. }
  9062. const kind = this.state.type.isLoop ? "loop" : this.match(types._switch) ? "switch" : null;
  9063. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  9064. const label = this.state.labels[i];
  9065. if (label.statementStart === node.start) {
  9066. label.statementStart = this.state.start;
  9067. label.kind = kind;
  9068. } else {
  9069. break;
  9070. }
  9071. }
  9072. this.state.labels.push({
  9073. name: maybeName,
  9074. kind: kind,
  9075. statementStart: this.state.start
  9076. });
  9077. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  9078. this.state.labels.pop();
  9079. node.label = expr;
  9080. return this.finishNode(node, "LabeledStatement");
  9081. }
  9082. parseExpressionStatement(node, expr) {
  9083. node.expression = expr;
  9084. this.semicolon();
  9085. return this.finishNode(node, "ExpressionStatement");
  9086. }
  9087. parseBlock(allowDirectives = false, createNewLexicalScope = true) {
  9088. const node = this.startNode();
  9089. this.expect(types.braceL);
  9090. if (createNewLexicalScope) {
  9091. this.scope.enter(SCOPE_OTHER);
  9092. }
  9093. this.parseBlockBody(node, allowDirectives, false, types.braceR);
  9094. if (createNewLexicalScope) {
  9095. this.scope.exit();
  9096. }
  9097. return this.finishNode(node, "BlockStatement");
  9098. }
  9099. isValidDirective(stmt) {
  9100. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  9101. }
  9102. parseBlockBody(node, allowDirectives, topLevel, end) {
  9103. const body = node.body = [];
  9104. const directives = node.directives = [];
  9105. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end);
  9106. }
  9107. parseBlockOrModuleBlockBody(body, directives, topLevel, end) {
  9108. let parsedNonDirective = false;
  9109. let oldStrict;
  9110. let octalPosition;
  9111. while (!this.eat(end)) {
  9112. if (!parsedNonDirective && this.state.containsOctal && !octalPosition) {
  9113. octalPosition = this.state.octalPosition;
  9114. }
  9115. const stmt = this.parseStatement(null, topLevel);
  9116. if (directives && !parsedNonDirective && this.isValidDirective(stmt)) {
  9117. const directive = this.stmtToDirective(stmt);
  9118. directives.push(directive);
  9119. if (oldStrict === undefined && directive.value.value === "use strict") {
  9120. oldStrict = this.state.strict;
  9121. this.setStrict(true);
  9122. if (octalPosition) {
  9123. this.raise(octalPosition, "Octal literal in strict mode");
  9124. }
  9125. }
  9126. continue;
  9127. }
  9128. parsedNonDirective = true;
  9129. body.push(stmt);
  9130. }
  9131. if (oldStrict === false) {
  9132. this.setStrict(false);
  9133. }
  9134. }
  9135. parseFor(node, init) {
  9136. node.init = init;
  9137. this.expect(types.semi);
  9138. node.test = this.match(types.semi) ? null : this.parseExpression();
  9139. this.expect(types.semi);
  9140. node.update = this.match(types.parenR) ? null : this.parseExpression();
  9141. this.expect(types.parenR);
  9142. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  9143. this.scope.exit();
  9144. this.state.labels.pop();
  9145. return this.finishNode(node, "ForStatement");
  9146. }
  9147. parseForIn(node, init, awaitAt) {
  9148. const isForIn = this.match(types._in);
  9149. this.next();
  9150. if (isForIn) {
  9151. if (awaitAt > -1) this.unexpected(awaitAt);
  9152. } else {
  9153. node.await = awaitAt > -1;
  9154. }
  9155. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  9156. this.raise(init.start, `${isForIn ? "for-in" : "for-of"} loop variable declaration may not have an initializer`);
  9157. } else if (init.type === "AssignmentPattern") {
  9158. this.raise(init.start, "Invalid left-hand side in for-loop");
  9159. }
  9160. node.left = init;
  9161. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssign();
  9162. this.expect(types.parenR);
  9163. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  9164. this.scope.exit();
  9165. this.state.labels.pop();
  9166. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  9167. }
  9168. parseVar(node, isFor, kind) {
  9169. const declarations = node.declarations = [];
  9170. const isTypescript = this.hasPlugin("typescript");
  9171. node.kind = kind;
  9172. for (;;) {
  9173. const decl = this.startNode();
  9174. this.parseVarId(decl, kind);
  9175. if (this.eat(types.eq)) {
  9176. decl.init = this.parseMaybeAssign(isFor);
  9177. } else {
  9178. if (kind === "const" && !(this.match(types._in) || this.isContextual("of"))) {
  9179. if (!isTypescript) {
  9180. this.unexpected();
  9181. }
  9182. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types._in) || this.isContextual("of")))) {
  9183. this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value");
  9184. }
  9185. decl.init = null;
  9186. }
  9187. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  9188. if (!this.eat(types.comma)) break;
  9189. }
  9190. return node;
  9191. }
  9192. parseVarId(decl, kind) {
  9193. decl.id = this.parseBindingAtom();
  9194. this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, "variable declaration", kind !== "var");
  9195. }
  9196. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  9197. const isStatement = statement & FUNC_STATEMENT;
  9198. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  9199. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  9200. this.initFunction(node, isAsync);
  9201. if (this.match(types.star) && isHangingStatement) {
  9202. this.raise(this.state.start, "Generators can only be declared at the top level or inside a block");
  9203. }
  9204. node.generator = this.eat(types.star);
  9205. if (isStatement) {
  9206. node.id = this.parseFunctionId(requireId);
  9207. }
  9208. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9209. const oldInClassProperty = this.state.inClassProperty;
  9210. const oldYieldPos = this.state.yieldPos;
  9211. const oldAwaitPos = this.state.awaitPos;
  9212. this.state.maybeInArrowParameters = false;
  9213. this.state.inClassProperty = false;
  9214. this.state.yieldPos = -1;
  9215. this.state.awaitPos = -1;
  9216. this.scope.enter(functionFlags(node.async, node.generator));
  9217. if (!isStatement) {
  9218. node.id = this.parseFunctionId();
  9219. }
  9220. this.parseFunctionParams(node);
  9221. this.withTopicForbiddingContext(() => {
  9222. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  9223. });
  9224. this.scope.exit();
  9225. if (isStatement && !isHangingStatement) {
  9226. this.registerFunctionStatementId(node);
  9227. }
  9228. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9229. this.state.inClassProperty = oldInClassProperty;
  9230. this.state.yieldPos = oldYieldPos;
  9231. this.state.awaitPos = oldAwaitPos;
  9232. return node;
  9233. }
  9234. parseFunctionId(requireId) {
  9235. return requireId || this.match(types.name) ? this.parseIdentifier() : null;
  9236. }
  9237. parseFunctionParams(node, allowModifiers) {
  9238. const oldInParameters = this.state.inParameters;
  9239. this.state.inParameters = true;
  9240. this.expect(types.parenL);
  9241. node.params = this.parseBindingList(types.parenR, 41, false, allowModifiers);
  9242. this.state.inParameters = oldInParameters;
  9243. this.checkYieldAwaitInDefaultParams();
  9244. }
  9245. registerFunctionStatementId(node) {
  9246. if (!node.id) return;
  9247. 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);
  9248. }
  9249. parseClass(node, isStatement, optionalId) {
  9250. this.next();
  9251. this.takeDecorators(node);
  9252. const oldStrict = this.state.strict;
  9253. this.state.strict = true;
  9254. this.parseClassId(node, isStatement, optionalId);
  9255. this.parseClassSuper(node);
  9256. node.body = this.parseClassBody(!!node.superClass);
  9257. this.state.strict = oldStrict;
  9258. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  9259. }
  9260. isClassProperty() {
  9261. return this.match(types.eq) || this.match(types.semi) || this.match(types.braceR);
  9262. }
  9263. isClassMethod() {
  9264. return this.match(types.parenL);
  9265. }
  9266. isNonstaticConstructor(method) {
  9267. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  9268. }
  9269. parseClassBody(constructorAllowsSuper) {
  9270. this.state.classLevel++;
  9271. const state = {
  9272. hadConstructor: false
  9273. };
  9274. let decorators = [];
  9275. const classBody = this.startNode();
  9276. classBody.body = [];
  9277. this.expect(types.braceL);
  9278. this.withTopicForbiddingContext(() => {
  9279. while (!this.eat(types.braceR)) {
  9280. if (this.eat(types.semi)) {
  9281. if (decorators.length > 0) {
  9282. throw this.raise(this.state.lastTokEnd, "Decorators must not be followed by a semicolon");
  9283. }
  9284. continue;
  9285. }
  9286. if (this.match(types.at)) {
  9287. decorators.push(this.parseDecorator());
  9288. continue;
  9289. }
  9290. const member = this.startNode();
  9291. if (decorators.length) {
  9292. member.decorators = decorators;
  9293. this.resetStartLocationFromNode(member, decorators[0]);
  9294. decorators = [];
  9295. }
  9296. this.parseClassMember(classBody, member, state, constructorAllowsSuper);
  9297. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  9298. this.raise(member.start, "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?");
  9299. }
  9300. }
  9301. });
  9302. if (decorators.length) {
  9303. throw this.raise(this.state.start, "You have trailing decorators with no method");
  9304. }
  9305. this.state.classLevel--;
  9306. return this.finishNode(classBody, "ClassBody");
  9307. }
  9308. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  9309. let isStatic = false;
  9310. const containsEsc = this.state.containsEsc;
  9311. if (this.match(types.name) && this.state.value === "static") {
  9312. const key = this.parseIdentifier(true);
  9313. if (this.isClassMethod()) {
  9314. const method = member;
  9315. method.kind = "method";
  9316. method.computed = false;
  9317. method.key = key;
  9318. method.static = false;
  9319. this.pushClassMethod(classBody, method, false, false, false, false);
  9320. return;
  9321. } else if (this.isClassProperty()) {
  9322. const prop = member;
  9323. prop.computed = false;
  9324. prop.key = key;
  9325. prop.static = false;
  9326. classBody.body.push(this.parseClassProperty(prop));
  9327. return;
  9328. } else if (containsEsc) {
  9329. throw this.unexpected();
  9330. }
  9331. isStatic = true;
  9332. }
  9333. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  9334. }
  9335. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  9336. const publicMethod = member;
  9337. const privateMethod = member;
  9338. const publicProp = member;
  9339. const privateProp = member;
  9340. const method = publicMethod;
  9341. const publicMember = publicMethod;
  9342. member.static = isStatic;
  9343. if (this.eat(types.star)) {
  9344. method.kind = "method";
  9345. this.parseClassPropertyName(method);
  9346. if (method.key.type === "PrivateName") {
  9347. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  9348. return;
  9349. }
  9350. if (this.isNonstaticConstructor(publicMethod)) {
  9351. this.raise(publicMethod.key.start, "Constructor can't be a generator");
  9352. }
  9353. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  9354. return;
  9355. }
  9356. const containsEsc = this.state.containsEsc;
  9357. const key = this.parseClassPropertyName(member);
  9358. const isPrivate = key.type === "PrivateName";
  9359. const isSimple = key.type === "Identifier";
  9360. const maybeQuestionTokenStart = this.state.start;
  9361. this.parsePostMemberNameModifiers(publicMember);
  9362. if (this.isClassMethod()) {
  9363. method.kind = "method";
  9364. if (isPrivate) {
  9365. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  9366. return;
  9367. }
  9368. const isConstructor = this.isNonstaticConstructor(publicMethod);
  9369. let allowsDirectSuper = false;
  9370. if (isConstructor) {
  9371. publicMethod.kind = "constructor";
  9372. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  9373. this.raise(key.start, "Duplicate constructor in the same class");
  9374. }
  9375. state.hadConstructor = true;
  9376. allowsDirectSuper = constructorAllowsSuper;
  9377. }
  9378. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  9379. } else if (this.isClassProperty()) {
  9380. if (isPrivate) {
  9381. this.pushClassPrivateProperty(classBody, privateProp);
  9382. } else {
  9383. this.pushClassProperty(classBody, publicProp);
  9384. }
  9385. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  9386. const isGenerator = this.eat(types.star);
  9387. if (publicMember.optional) {
  9388. this.unexpected(maybeQuestionTokenStart);
  9389. }
  9390. method.kind = "method";
  9391. this.parseClassPropertyName(method);
  9392. if (method.key.type === "PrivateName") {
  9393. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  9394. } else {
  9395. if (this.isNonstaticConstructor(publicMethod)) {
  9396. this.raise(publicMethod.key.start, "Constructor can't be an async function");
  9397. }
  9398. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  9399. }
  9400. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types.star) && this.isLineTerminator())) {
  9401. method.kind = key.name;
  9402. this.parseClassPropertyName(publicMethod);
  9403. if (method.key.type === "PrivateName") {
  9404. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  9405. } else {
  9406. if (this.isNonstaticConstructor(publicMethod)) {
  9407. this.raise(publicMethod.key.start, "Constructor can't have get/set modifier");
  9408. }
  9409. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  9410. }
  9411. this.checkGetterSetterParams(publicMethod);
  9412. } else if (this.isLineTerminator()) {
  9413. if (isPrivate) {
  9414. this.pushClassPrivateProperty(classBody, privateProp);
  9415. } else {
  9416. this.pushClassProperty(classBody, publicProp);
  9417. }
  9418. } else {
  9419. this.unexpected();
  9420. }
  9421. }
  9422. parseClassPropertyName(member) {
  9423. const key = this.parsePropertyName(member);
  9424. if (!member.computed && member.static && (key.name === "prototype" || key.value === "prototype")) {
  9425. this.raise(key.start, "Classes may not have static property named prototype");
  9426. }
  9427. if (key.type === "PrivateName" && key.id.name === "constructor") {
  9428. this.raise(key.start, "Classes may not have a private field named '#constructor'");
  9429. }
  9430. return key;
  9431. }
  9432. pushClassProperty(classBody, prop) {
  9433. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  9434. this.raise(prop.key.start, "Classes may not have a field named 'constructor'");
  9435. }
  9436. classBody.body.push(this.parseClassProperty(prop));
  9437. }
  9438. pushClassPrivateProperty(classBody, prop) {
  9439. this.expectPlugin("classPrivateProperties", prop.key.start);
  9440. classBody.body.push(this.parseClassPrivateProperty(prop));
  9441. }
  9442. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9443. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  9444. }
  9445. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9446. this.expectPlugin("classPrivateMethods", method.key.start);
  9447. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true));
  9448. }
  9449. parsePostMemberNameModifiers(methodOrProp) {}
  9450. parseAccessModifier() {
  9451. return undefined;
  9452. }
  9453. parseClassPrivateProperty(node) {
  9454. this.state.inClassProperty = true;
  9455. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  9456. node.value = this.eat(types.eq) ? this.parseMaybeAssign() : null;
  9457. this.semicolon();
  9458. this.state.inClassProperty = false;
  9459. this.scope.exit();
  9460. return this.finishNode(node, "ClassPrivateProperty");
  9461. }
  9462. parseClassProperty(node) {
  9463. if (!node.typeAnnotation) {
  9464. this.expectPlugin("classProperties");
  9465. }
  9466. this.state.inClassProperty = true;
  9467. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  9468. if (this.match(types.eq)) {
  9469. this.expectPlugin("classProperties");
  9470. this.next();
  9471. node.value = this.parseMaybeAssign();
  9472. } else {
  9473. node.value = null;
  9474. }
  9475. this.semicolon();
  9476. this.state.inClassProperty = false;
  9477. this.scope.exit();
  9478. return this.finishNode(node, "ClassProperty");
  9479. }
  9480. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  9481. if (this.match(types.name)) {
  9482. node.id = this.parseIdentifier();
  9483. if (isStatement) {
  9484. this.checkLVal(node.id, bindingType, undefined, "class name");
  9485. }
  9486. } else {
  9487. if (optionalId || !isStatement) {
  9488. node.id = null;
  9489. } else {
  9490. this.unexpected(null, "A class name is required");
  9491. }
  9492. }
  9493. }
  9494. parseClassSuper(node) {
  9495. node.superClass = this.eat(types._extends) ? this.parseExprSubscripts() : null;
  9496. }
  9497. parseExport(node) {
  9498. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  9499. const parseAfterDefault = !hasDefault || this.eat(types.comma);
  9500. const hasStar = parseAfterDefault && this.eatExportStar(node);
  9501. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  9502. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types.comma));
  9503. const isFromRequired = hasDefault || hasStar;
  9504. if (hasStar && !hasNamespace) {
  9505. if (hasDefault) this.unexpected();
  9506. this.parseExportFrom(node, true);
  9507. return this.finishNode(node, "ExportAllDeclaration");
  9508. }
  9509. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  9510. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  9511. throw this.unexpected(null, types.braceL);
  9512. }
  9513. let hasDeclaration;
  9514. if (isFromRequired || hasSpecifiers) {
  9515. hasDeclaration = false;
  9516. this.parseExportFrom(node, isFromRequired);
  9517. } else {
  9518. hasDeclaration = this.maybeParseExportDeclaration(node);
  9519. }
  9520. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  9521. this.checkExport(node, true, false, !!node.source);
  9522. return this.finishNode(node, "ExportNamedDeclaration");
  9523. }
  9524. if (this.eat(types._default)) {
  9525. node.declaration = this.parseExportDefaultExpression();
  9526. this.checkExport(node, true, true);
  9527. return this.finishNode(node, "ExportDefaultDeclaration");
  9528. }
  9529. throw this.unexpected(null, types.braceL);
  9530. }
  9531. eatExportStar(node) {
  9532. return this.eat(types.star);
  9533. }
  9534. maybeParseExportDefaultSpecifier(node) {
  9535. if (this.isExportDefaultSpecifier()) {
  9536. this.expectPlugin("exportDefaultFrom");
  9537. const specifier = this.startNode();
  9538. specifier.exported = this.parseIdentifier(true);
  9539. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9540. return true;
  9541. }
  9542. return false;
  9543. }
  9544. maybeParseExportNamespaceSpecifier(node) {
  9545. if (this.isContextual("as")) {
  9546. if (!node.specifiers) node.specifiers = [];
  9547. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  9548. this.next();
  9549. specifier.exported = this.parseIdentifier(true);
  9550. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  9551. return true;
  9552. }
  9553. return false;
  9554. }
  9555. maybeParseExportNamedSpecifiers(node) {
  9556. if (this.match(types.braceL)) {
  9557. if (!node.specifiers) node.specifiers = [];
  9558. node.specifiers.push(...this.parseExportSpecifiers());
  9559. node.source = null;
  9560. node.declaration = null;
  9561. return true;
  9562. }
  9563. return false;
  9564. }
  9565. maybeParseExportDeclaration(node) {
  9566. if (this.shouldParseExportDeclaration()) {
  9567. if (this.isContextual("async")) {
  9568. const next = this.nextTokenStart();
  9569. if (!this.isUnparsedContextual(next, "function")) {
  9570. this.unexpected(next, `Unexpected token, expected "function"`);
  9571. }
  9572. }
  9573. node.specifiers = [];
  9574. node.source = null;
  9575. node.declaration = this.parseExportDeclaration(node);
  9576. return true;
  9577. }
  9578. return false;
  9579. }
  9580. isAsyncFunction() {
  9581. if (!this.isContextual("async")) return false;
  9582. const next = this.nextTokenStart();
  9583. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  9584. }
  9585. parseExportDefaultExpression() {
  9586. const expr = this.startNode();
  9587. const isAsync = this.isAsyncFunction();
  9588. if (this.match(types._function) || isAsync) {
  9589. this.next();
  9590. if (isAsync) {
  9591. this.next();
  9592. }
  9593. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  9594. } else if (this.match(types._class)) {
  9595. return this.parseClass(expr, true, true);
  9596. } else if (this.match(types.at)) {
  9597. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  9598. this.raise(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
  9599. }
  9600. this.parseDecorators(false);
  9601. return this.parseClass(expr, true, true);
  9602. } else if (this.match(types._const) || this.match(types._var) || this.isLet()) {
  9603. throw this.raise(this.state.start, "Only expressions, functions or classes are allowed as the `default` export.");
  9604. } else {
  9605. const res = this.parseMaybeAssign();
  9606. this.semicolon();
  9607. return res;
  9608. }
  9609. }
  9610. parseExportDeclaration(node) {
  9611. return this.parseStatement(null);
  9612. }
  9613. isExportDefaultSpecifier() {
  9614. if (this.match(types.name)) {
  9615. return this.state.value !== "async" && this.state.value !== "let";
  9616. }
  9617. if (!this.match(types._default)) {
  9618. return false;
  9619. }
  9620. const next = this.nextTokenStart();
  9621. return this.input.charCodeAt(next) === 44 || this.isUnparsedContextual(next, "from");
  9622. }
  9623. parseExportFrom(node, expect) {
  9624. if (this.eatContextual("from")) {
  9625. node.source = this.parseImportSource();
  9626. this.checkExport(node);
  9627. } else {
  9628. if (expect) {
  9629. this.unexpected();
  9630. } else {
  9631. node.source = null;
  9632. }
  9633. }
  9634. this.semicolon();
  9635. }
  9636. shouldParseExportDeclaration() {
  9637. if (this.match(types.at)) {
  9638. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  9639. if (this.hasPlugin("decorators")) {
  9640. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  9641. this.unexpected(this.state.start, "Decorators must be placed *before* the 'export' keyword." + " You can set the 'decoratorsBeforeExport' option to false to use" + " the 'export @decorator class {}' syntax");
  9642. } else {
  9643. return true;
  9644. }
  9645. }
  9646. }
  9647. 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();
  9648. }
  9649. checkExport(node, checkNames, isDefault, isFrom) {
  9650. if (checkNames) {
  9651. if (isDefault) {
  9652. this.checkDuplicateExports(node, "default");
  9653. } else if (node.specifiers && node.specifiers.length) {
  9654. for (let _i3 = 0, _node$specifiers = node.specifiers; _i3 < _node$specifiers.length; _i3++) {
  9655. const specifier = _node$specifiers[_i3];
  9656. this.checkDuplicateExports(specifier, specifier.exported.name);
  9657. if (!isFrom && specifier.local) {
  9658. this.checkReservedWord(specifier.local.name, specifier.local.start, true, false);
  9659. this.scope.checkLocalExport(specifier.local);
  9660. }
  9661. }
  9662. } else if (node.declaration) {
  9663. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  9664. const id = node.declaration.id;
  9665. if (!id) throw new Error("Assertion failure");
  9666. this.checkDuplicateExports(node, id.name);
  9667. } else if (node.declaration.type === "VariableDeclaration") {
  9668. for (let _i4 = 0, _node$declaration$dec = node.declaration.declarations; _i4 < _node$declaration$dec.length; _i4++) {
  9669. const declaration = _node$declaration$dec[_i4];
  9670. this.checkDeclaration(declaration.id);
  9671. }
  9672. }
  9673. }
  9674. }
  9675. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  9676. if (currentContextDecorators.length) {
  9677. const isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
  9678. if (!node.declaration || !isClass) {
  9679. throw this.raise(node.start, "You can only use decorators on an export when exporting a class");
  9680. }
  9681. this.takeDecorators(node.declaration);
  9682. }
  9683. }
  9684. checkDeclaration(node) {
  9685. if (node.type === "Identifier") {
  9686. this.checkDuplicateExports(node, node.name);
  9687. } else if (node.type === "ObjectPattern") {
  9688. for (let _i5 = 0, _node$properties = node.properties; _i5 < _node$properties.length; _i5++) {
  9689. const prop = _node$properties[_i5];
  9690. this.checkDeclaration(prop);
  9691. }
  9692. } else if (node.type === "ArrayPattern") {
  9693. for (let _i6 = 0, _node$elements = node.elements; _i6 < _node$elements.length; _i6++) {
  9694. const elem = _node$elements[_i6];
  9695. if (elem) {
  9696. this.checkDeclaration(elem);
  9697. }
  9698. }
  9699. } else if (node.type === "ObjectProperty") {
  9700. this.checkDeclaration(node.value);
  9701. } else if (node.type === "RestElement") {
  9702. this.checkDeclaration(node.argument);
  9703. } else if (node.type === "AssignmentPattern") {
  9704. this.checkDeclaration(node.left);
  9705. }
  9706. }
  9707. checkDuplicateExports(node, name) {
  9708. if (this.state.exportedIdentifiers.indexOf(name) > -1) {
  9709. this.raise(node.start, name === "default" ? "Only one default export allowed per module." : `\`${name}\` has already been exported. Exported identifiers must be unique.`);
  9710. }
  9711. this.state.exportedIdentifiers.push(name);
  9712. }
  9713. parseExportSpecifiers() {
  9714. const nodes = [];
  9715. let first = true;
  9716. this.expect(types.braceL);
  9717. while (!this.eat(types.braceR)) {
  9718. if (first) {
  9719. first = false;
  9720. } else {
  9721. this.expect(types.comma);
  9722. if (this.eat(types.braceR)) break;
  9723. }
  9724. const node = this.startNode();
  9725. node.local = this.parseIdentifier(true);
  9726. node.exported = this.eatContextual("as") ? this.parseIdentifier(true) : node.local.__clone();
  9727. nodes.push(this.finishNode(node, "ExportSpecifier"));
  9728. }
  9729. return nodes;
  9730. }
  9731. parseImport(node) {
  9732. node.specifiers = [];
  9733. if (!this.match(types.string)) {
  9734. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  9735. const parseNext = !hasDefault || this.eat(types.comma);
  9736. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  9737. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  9738. this.expectContextual("from");
  9739. }
  9740. node.source = this.parseImportSource();
  9741. this.semicolon();
  9742. return this.finishNode(node, "ImportDeclaration");
  9743. }
  9744. parseImportSource() {
  9745. if (!this.match(types.string)) this.unexpected();
  9746. return this.parseExprAtom();
  9747. }
  9748. shouldParseDefaultImport(node) {
  9749. return this.match(types.name);
  9750. }
  9751. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  9752. specifier.local = this.parseIdentifier();
  9753. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  9754. node.specifiers.push(this.finishNode(specifier, type));
  9755. }
  9756. maybeParseDefaultImportSpecifier(node) {
  9757. if (this.shouldParseDefaultImport(node)) {
  9758. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  9759. return true;
  9760. }
  9761. return false;
  9762. }
  9763. maybeParseStarImportSpecifier(node) {
  9764. if (this.match(types.star)) {
  9765. const specifier = this.startNode();
  9766. this.next();
  9767. this.expectContextual("as");
  9768. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  9769. return true;
  9770. }
  9771. return false;
  9772. }
  9773. parseNamedImportSpecifiers(node) {
  9774. let first = true;
  9775. this.expect(types.braceL);
  9776. while (!this.eat(types.braceR)) {
  9777. if (first) {
  9778. first = false;
  9779. } else {
  9780. if (this.eat(types.colon)) {
  9781. throw this.raise(this.state.start, "ES2015 named imports do not destructure. " + "Use another statement for destructuring after the import.");
  9782. }
  9783. this.expect(types.comma);
  9784. if (this.eat(types.braceR)) break;
  9785. }
  9786. this.parseImportSpecifier(node);
  9787. }
  9788. }
  9789. parseImportSpecifier(node) {
  9790. const specifier = this.startNode();
  9791. specifier.imported = this.parseIdentifier(true);
  9792. if (this.eatContextual("as")) {
  9793. specifier.local = this.parseIdentifier();
  9794. } else {
  9795. this.checkReservedWord(specifier.imported.name, specifier.start, true, true);
  9796. specifier.local = specifier.imported.__clone();
  9797. }
  9798. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  9799. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  9800. }
  9801. }
  9802. class Parser extends StatementParser {
  9803. constructor(options, input) {
  9804. options = getOptions(options);
  9805. super(options, input);
  9806. const ScopeHandler = this.getScopeHandler();
  9807. this.options = options;
  9808. this.inModule = this.options.sourceType === "module";
  9809. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  9810. this.plugins = pluginsMap(this.options.plugins);
  9811. this.filename = options.sourceFilename;
  9812. }
  9813. getScopeHandler() {
  9814. return ScopeHandler;
  9815. }
  9816. parse() {
  9817. this.scope.enter(SCOPE_PROGRAM);
  9818. const file = this.startNode();
  9819. const program = this.startNode();
  9820. this.nextToken();
  9821. file.errors = null;
  9822. this.parseTopLevel(file, program);
  9823. file.errors = this.state.errors;
  9824. return file;
  9825. }
  9826. }
  9827. function pluginsMap(plugins) {
  9828. const pluginMap = new Map();
  9829. for (let _i = 0; _i < plugins.length; _i++) {
  9830. const plugin = plugins[_i];
  9831. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  9832. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  9833. }
  9834. return pluginMap;
  9835. }
  9836. function parse(input, options) {
  9837. if (options && options.sourceType === "unambiguous") {
  9838. options = Object.assign({}, options);
  9839. try {
  9840. options.sourceType = "module";
  9841. const parser = getParser(options, input);
  9842. const ast = parser.parse();
  9843. if (parser.sawUnambiguousESM) {
  9844. return ast;
  9845. }
  9846. if (parser.ambiguousScriptDifferentAst) {
  9847. try {
  9848. options.sourceType = "script";
  9849. return getParser(options, input).parse();
  9850. } catch (_unused) {}
  9851. } else {
  9852. ast.program.sourceType = "script";
  9853. }
  9854. return ast;
  9855. } catch (moduleError) {
  9856. try {
  9857. options.sourceType = "script";
  9858. return getParser(options, input).parse();
  9859. } catch (_unused2) {}
  9860. throw moduleError;
  9861. }
  9862. } else {
  9863. return getParser(options, input).parse();
  9864. }
  9865. }
  9866. function parseExpression(input, options) {
  9867. const parser = getParser(options, input);
  9868. if (parser.options.strictMode) {
  9869. parser.state.strict = true;
  9870. }
  9871. return parser.getExpression();
  9872. }
  9873. function getParser(options, input) {
  9874. let cls = Parser;
  9875. if (options && options.plugins) {
  9876. validatePlugins(options.plugins);
  9877. cls = getParserClass(options.plugins);
  9878. }
  9879. return new cls(options, input);
  9880. }
  9881. const parserClassCache = {};
  9882. function getParserClass(pluginsFromOptions) {
  9883. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  9884. const key = pluginList.join("/");
  9885. let cls = parserClassCache[key];
  9886. if (!cls) {
  9887. cls = Parser;
  9888. for (let _i = 0; _i < pluginList.length; _i++) {
  9889. const plugin = pluginList[_i];
  9890. cls = mixinPlugins[plugin](cls);
  9891. }
  9892. parserClassCache[key] = cls;
  9893. }
  9894. return cls;
  9895. }
  9896. exports.parse = parse;
  9897. exports.parseExpression = parseExpression;
  9898. exports.tokTypes = types;