index.js 323 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180
  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("||", 1),
  136. logicalAND: createBinop("&&", 2),
  137. bitwiseOR: createBinop("|", 3),
  138. bitwiseXOR: createBinop("^", 4),
  139. bitwiseAND: createBinop("&", 5),
  140. equality: createBinop("==/!=", 6),
  141. relational: createBinop("</>", 7),
  142. bitShift: createBinop("<</>>", 8),
  143. plusMin: new TokenType("+/-", {
  144. beforeExpr,
  145. binop: 9,
  146. prefix,
  147. startsExpr
  148. }),
  149. modulo: createBinop("%", 10),
  150. star: createBinop("*", 10),
  151. slash: createBinop("/", 10),
  152. exponent: new TokenType("**", {
  153. beforeExpr,
  154. binop: 11,
  155. rightAssociative: true
  156. }),
  157. _break: createKeyword("break"),
  158. _case: createKeyword("case", {
  159. beforeExpr
  160. }),
  161. _catch: createKeyword("catch"),
  162. _continue: createKeyword("continue"),
  163. _debugger: createKeyword("debugger"),
  164. _default: createKeyword("default", {
  165. beforeExpr
  166. }),
  167. _do: createKeyword("do", {
  168. isLoop,
  169. beforeExpr
  170. }),
  171. _else: createKeyword("else", {
  172. beforeExpr
  173. }),
  174. _finally: createKeyword("finally"),
  175. _for: createKeyword("for", {
  176. isLoop
  177. }),
  178. _function: createKeyword("function", {
  179. startsExpr
  180. }),
  181. _if: createKeyword("if"),
  182. _return: createKeyword("return", {
  183. beforeExpr
  184. }),
  185. _switch: createKeyword("switch"),
  186. _throw: createKeyword("throw", {
  187. beforeExpr,
  188. prefix,
  189. startsExpr
  190. }),
  191. _try: createKeyword("try"),
  192. _var: createKeyword("var"),
  193. _const: createKeyword("const"),
  194. _while: createKeyword("while", {
  195. isLoop
  196. }),
  197. _with: createKeyword("with"),
  198. _new: createKeyword("new", {
  199. beforeExpr,
  200. startsExpr
  201. }),
  202. _this: createKeyword("this", {
  203. startsExpr
  204. }),
  205. _super: createKeyword("super", {
  206. startsExpr
  207. }),
  208. _class: createKeyword("class", {
  209. startsExpr
  210. }),
  211. _extends: createKeyword("extends", {
  212. beforeExpr
  213. }),
  214. _export: createKeyword("export"),
  215. _import: createKeyword("import", {
  216. startsExpr
  217. }),
  218. _null: createKeyword("null", {
  219. startsExpr
  220. }),
  221. _true: createKeyword("true", {
  222. startsExpr
  223. }),
  224. _false: createKeyword("false", {
  225. startsExpr
  226. }),
  227. _in: createKeyword("in", {
  228. beforeExpr,
  229. binop: 7
  230. }),
  231. _instanceof: createKeyword("instanceof", {
  232. beforeExpr,
  233. binop: 7
  234. }),
  235. _typeof: createKeyword("typeof", {
  236. beforeExpr,
  237. prefix,
  238. startsExpr
  239. }),
  240. _void: createKeyword("void", {
  241. beforeExpr,
  242. prefix,
  243. startsExpr
  244. }),
  245. _delete: createKeyword("delete", {
  246. beforeExpr,
  247. prefix,
  248. startsExpr
  249. })
  250. };
  251. const SCOPE_OTHER = 0b000000000,
  252. SCOPE_PROGRAM = 0b000000001,
  253. SCOPE_FUNCTION = 0b000000010,
  254. SCOPE_ASYNC = 0b000000100,
  255. SCOPE_GENERATOR = 0b000001000,
  256. SCOPE_ARROW = 0b000010000,
  257. SCOPE_SIMPLE_CATCH = 0b000100000,
  258. SCOPE_SUPER = 0b001000000,
  259. SCOPE_DIRECT_SUPER = 0b010000000,
  260. SCOPE_CLASS = 0b100000000,
  261. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION;
  262. function functionFlags(isAsync, isGenerator) {
  263. return SCOPE_FUNCTION | (isAsync ? SCOPE_ASYNC : 0) | (isGenerator ? SCOPE_GENERATOR : 0);
  264. }
  265. const BIND_NONE = 0,
  266. BIND_VAR = 1,
  267. BIND_LEXICAL = 2,
  268. BIND_FUNCTION = 3,
  269. BIND_SIMPLE_CATCH = 4,
  270. BIND_OUTSIDE = 5;
  271. function isSimpleProperty(node) {
  272. return node != null && node.type === "Property" && node.kind === "init" && node.method === false;
  273. }
  274. var estree = (superClass => class extends superClass {
  275. estreeParseRegExpLiteral({
  276. pattern,
  277. flags
  278. }) {
  279. let regex = null;
  280. try {
  281. regex = new RegExp(pattern, flags);
  282. } catch (e) {}
  283. const node = this.estreeParseLiteral(regex);
  284. node.regex = {
  285. pattern,
  286. flags
  287. };
  288. return node;
  289. }
  290. estreeParseLiteral(value) {
  291. return this.parseLiteral(value, "Literal");
  292. }
  293. directiveToStmt(directive) {
  294. const directiveLiteral = directive.value;
  295. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  296. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  297. expression.value = directiveLiteral.value;
  298. expression.raw = directiveLiteral.extra.raw;
  299. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  300. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  301. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  302. }
  303. initFunction(node, isAsync) {
  304. super.initFunction(node, isAsync);
  305. node.expression = false;
  306. }
  307. checkDeclaration(node) {
  308. if (isSimpleProperty(node)) {
  309. this.checkDeclaration(node.value);
  310. } else {
  311. super.checkDeclaration(node);
  312. }
  313. }
  314. checkGetterSetterParams(method) {
  315. const prop = method;
  316. const paramCount = prop.kind === "get" ? 0 : 1;
  317. const start = prop.start;
  318. if (prop.value.params.length !== paramCount) {
  319. if (prop.kind === "get") {
  320. this.raise(start, "getter must not have any formal parameters");
  321. } else {
  322. this.raise(start, "setter must have exactly one formal parameter");
  323. }
  324. }
  325. if (prop.kind === "set" && prop.value.params[0].type === "RestElement") {
  326. this.raise(start, "setter function argument must not be a rest parameter");
  327. }
  328. }
  329. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  330. switch (expr.type) {
  331. case "ObjectPattern":
  332. expr.properties.forEach(prop => {
  333. this.checkLVal(prop.type === "Property" ? prop.value : prop, bindingType, checkClashes, "object destructuring pattern");
  334. });
  335. break;
  336. default:
  337. super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  338. }
  339. }
  340. checkPropClash(prop, propHash) {
  341. if (prop.type === "SpreadElement" || prop.computed || prop.method || prop.shorthand) {
  342. return;
  343. }
  344. const key = prop.key;
  345. const name = key.type === "Identifier" ? key.name : String(key.value);
  346. if (name === "__proto__" && prop.kind === "init") {
  347. if (propHash.proto) {
  348. this.raise(key.start, "Redefinition of __proto__ property");
  349. }
  350. propHash.proto = true;
  351. }
  352. }
  353. isStrictBody(node) {
  354. const isBlockStatement = node.body.type === "BlockStatement";
  355. if (isBlockStatement && node.body.body.length > 0) {
  356. for (let _i = 0, _node$body$body = node.body.body; _i < _node$body$body.length; _i++) {
  357. const directive = _node$body$body[_i];
  358. if (directive.type === "ExpressionStatement" && directive.expression.type === "Literal") {
  359. if (directive.expression.value === "use strict") return true;
  360. } else {
  361. break;
  362. }
  363. }
  364. }
  365. return false;
  366. }
  367. isValidDirective(stmt) {
  368. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && (!stmt.expression.extra || !stmt.expression.extra.parenthesized);
  369. }
  370. stmtToDirective(stmt) {
  371. const directive = super.stmtToDirective(stmt);
  372. const value = stmt.expression.value;
  373. directive.value.value = value;
  374. return directive;
  375. }
  376. parseBlockBody(node, allowDirectives, topLevel, end) {
  377. super.parseBlockBody(node, allowDirectives, topLevel, end);
  378. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  379. node.body = directiveStatements.concat(node.body);
  380. delete node.directives;
  381. }
  382. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  383. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "MethodDefinition", true);
  384. if (method.typeParameters) {
  385. method.value.typeParameters = method.typeParameters;
  386. delete method.typeParameters;
  387. }
  388. classBody.body.push(method);
  389. }
  390. parseExprAtom(refShorthandDefaultPos) {
  391. switch (this.state.type) {
  392. case types.regexp:
  393. return this.estreeParseRegExpLiteral(this.state.value);
  394. case types.num:
  395. case types.string:
  396. return this.estreeParseLiteral(this.state.value);
  397. case types._null:
  398. return this.estreeParseLiteral(null);
  399. case types._true:
  400. return this.estreeParseLiteral(true);
  401. case types._false:
  402. return this.estreeParseLiteral(false);
  403. default:
  404. return super.parseExprAtom(refShorthandDefaultPos);
  405. }
  406. }
  407. parseLiteral(value, type, startPos, startLoc) {
  408. const node = super.parseLiteral(value, type, startPos, startLoc);
  409. node.raw = node.extra.raw;
  410. delete node.extra;
  411. return node;
  412. }
  413. parseFunctionBody(node, allowExpression, isMethod = false) {
  414. super.parseFunctionBody(node, allowExpression, isMethod);
  415. node.expression = node.body.type !== "BlockStatement";
  416. }
  417. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  418. let funcNode = this.startNode();
  419. funcNode.kind = node.kind;
  420. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, "FunctionExpression", inClassScope);
  421. delete funcNode.kind;
  422. node.value = funcNode;
  423. return this.finishNode(node, type);
  424. }
  425. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  426. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc);
  427. if (node) {
  428. node.type = "Property";
  429. if (node.kind === "method") node.kind = "init";
  430. node.shorthand = false;
  431. }
  432. return node;
  433. }
  434. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  435. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  436. if (node) {
  437. node.kind = "init";
  438. node.type = "Property";
  439. }
  440. return node;
  441. }
  442. toAssignable(node, isBinding, contextDescription) {
  443. if (isSimpleProperty(node)) {
  444. this.toAssignable(node.value, isBinding, contextDescription);
  445. return node;
  446. }
  447. return super.toAssignable(node, isBinding, contextDescription);
  448. }
  449. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  450. if (prop.kind === "get" || prop.kind === "set") {
  451. this.raise(prop.key.start, "Object pattern can't contain getter or setter");
  452. } else if (prop.method) {
  453. this.raise(prop.key.start, "Object pattern can't contain methods");
  454. } else {
  455. super.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  456. }
  457. }
  458. });
  459. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  460. const lineBreakG = new RegExp(lineBreak.source, "g");
  461. function isNewLine(code) {
  462. switch (code) {
  463. case 10:
  464. case 13:
  465. case 8232:
  466. case 8233:
  467. return true;
  468. default:
  469. return false;
  470. }
  471. }
  472. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  473. function isWhitespace(code) {
  474. switch (code) {
  475. case 0x0009:
  476. case 0x000b:
  477. case 0x000c:
  478. case 32:
  479. case 160:
  480. case 5760:
  481. case 0x2000:
  482. case 0x2001:
  483. case 0x2002:
  484. case 0x2003:
  485. case 0x2004:
  486. case 0x2005:
  487. case 0x2006:
  488. case 0x2007:
  489. case 0x2008:
  490. case 0x2009:
  491. case 0x200a:
  492. case 0x202f:
  493. case 0x205f:
  494. case 0x3000:
  495. case 0xfeff:
  496. return true;
  497. default:
  498. return false;
  499. }
  500. }
  501. class TokContext {
  502. constructor(token, isExpr, preserveSpace, override) {
  503. this.token = token;
  504. this.isExpr = !!isExpr;
  505. this.preserveSpace = !!preserveSpace;
  506. this.override = override;
  507. }
  508. }
  509. const types$1 = {
  510. braceStatement: new TokContext("{", false),
  511. braceExpression: new TokContext("{", true),
  512. templateQuasi: new TokContext("${", false),
  513. parenStatement: new TokContext("(", false),
  514. parenExpression: new TokContext("(", true),
  515. template: new TokContext("`", true, true, p => p.readTmplToken()),
  516. functionExpression: new TokContext("function", true),
  517. functionStatement: new TokContext("function", false)
  518. };
  519. types.parenR.updateContext = types.braceR.updateContext = function () {
  520. if (this.state.context.length === 1) {
  521. this.state.exprAllowed = true;
  522. return;
  523. }
  524. let out = this.state.context.pop();
  525. if (out === types$1.braceStatement && this.curContext().token === "function") {
  526. out = this.state.context.pop();
  527. }
  528. this.state.exprAllowed = !out.isExpr;
  529. };
  530. types.name.updateContext = function (prevType) {
  531. let allowed = false;
  532. if (prevType !== types.dot) {
  533. if (this.state.value === "of" && !this.state.exprAllowed || this.state.value === "yield" && this.scope.inGenerator) {
  534. allowed = true;
  535. }
  536. }
  537. this.state.exprAllowed = allowed;
  538. if (this.state.isIterator) {
  539. this.state.isIterator = false;
  540. }
  541. };
  542. types.braceL.updateContext = function (prevType) {
  543. this.state.context.push(this.braceIsBlock(prevType) ? types$1.braceStatement : types$1.braceExpression);
  544. this.state.exprAllowed = true;
  545. };
  546. types.dollarBraceL.updateContext = function () {
  547. this.state.context.push(types$1.templateQuasi);
  548. this.state.exprAllowed = true;
  549. };
  550. types.parenL.updateContext = function (prevType) {
  551. const statementParens = prevType === types._if || prevType === types._for || prevType === types._with || prevType === types._while;
  552. this.state.context.push(statementParens ? types$1.parenStatement : types$1.parenExpression);
  553. this.state.exprAllowed = true;
  554. };
  555. types.incDec.updateContext = function () {};
  556. types._function.updateContext = types._class.updateContext = function (prevType) {
  557. 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)) {
  558. this.state.context.push(types$1.functionExpression);
  559. } else {
  560. this.state.context.push(types$1.functionStatement);
  561. }
  562. this.state.exprAllowed = false;
  563. };
  564. types.backQuote.updateContext = function () {
  565. if (this.curContext() === types$1.template) {
  566. this.state.context.pop();
  567. } else {
  568. this.state.context.push(types$1.template);
  569. }
  570. this.state.exprAllowed = false;
  571. };
  572. const reservedWords = {
  573. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  574. strictBind: ["eval", "arguments"]
  575. };
  576. const reservedWordsStrictSet = new Set(reservedWords.strict);
  577. const reservedWordsStrictBindSet = new Set(reservedWords.strict.concat(reservedWords.strictBind));
  578. const isReservedWord = (word, inModule) => {
  579. return inModule && word === "await" || word === "enum";
  580. };
  581. function isStrictReservedWord(word, inModule) {
  582. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  583. }
  584. function isStrictBindReservedWord(word, inModule) {
  585. return isReservedWord(word, inModule) || reservedWordsStrictBindSet.has(word);
  586. }
  587. function isKeyword(word) {
  588. return keywords.has(word);
  589. }
  590. const keywordRelationalOperator = /^in(stanceof)?$/;
  591. 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";
  592. 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";
  593. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  594. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  595. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  596. 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];
  597. 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];
  598. function isInAstralSet(code, set) {
  599. let pos = 0x10000;
  600. for (let i = 0, length = set.length; i < length; i += 2) {
  601. pos += set[i];
  602. if (pos > code) return false;
  603. pos += set[i + 1];
  604. if (pos >= code) return true;
  605. }
  606. return false;
  607. }
  608. function isIdentifierStart(code) {
  609. if (code < 65) return code === 36;
  610. if (code <= 90) return true;
  611. if (code < 97) return code === 95;
  612. if (code <= 122) return true;
  613. if (code <= 0xffff) {
  614. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  615. }
  616. return isInAstralSet(code, astralIdentifierStartCodes);
  617. }
  618. function isIteratorStart(current, next) {
  619. return current === 64 && next === 64;
  620. }
  621. function isIdentifierChar(code) {
  622. if (code < 48) return code === 36;
  623. if (code < 58) return true;
  624. if (code < 65) return false;
  625. if (code <= 90) return true;
  626. if (code < 97) return code === 95;
  627. if (code <= 122) return true;
  628. if (code <= 0xffff) {
  629. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  630. }
  631. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  632. }
  633. const reservedTypes = ["any", "bool", "boolean", "empty", "false", "mixed", "null", "number", "static", "string", "true", "typeof", "void", "interface", "extends", "_"];
  634. function isEsModuleType(bodyElement) {
  635. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  636. }
  637. function hasTypeImportKind(node) {
  638. return node.importKind === "type" || node.importKind === "typeof";
  639. }
  640. function isMaybeDefaultImport(state) {
  641. return (state.type === types.name || !!state.type.keyword) && state.value !== "from";
  642. }
  643. const exportSuggestions = {
  644. const: "declare export var",
  645. let: "declare export var",
  646. type: "export type",
  647. interface: "export interface"
  648. };
  649. function partition(list, test) {
  650. const list1 = [];
  651. const list2 = [];
  652. for (let i = 0; i < list.length; i++) {
  653. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  654. }
  655. return [list1, list2];
  656. }
  657. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  658. var flow = (superClass => class extends superClass {
  659. constructor(options, input) {
  660. super(options, input);
  661. this.flowPragma = undefined;
  662. }
  663. shouldParseTypes() {
  664. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  665. }
  666. addComment(comment) {
  667. if (this.flowPragma === undefined) {
  668. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  669. if (!matches) {
  670. this.flowPragma = null;
  671. } else if (matches[1] === "flow") {
  672. this.flowPragma = "flow";
  673. } else if (matches[1] === "noflow") {
  674. this.flowPragma = "noflow";
  675. } else {
  676. throw new Error("Unexpected flow pragma");
  677. }
  678. }
  679. return super.addComment(comment);
  680. }
  681. flowParseTypeInitialiser(tok) {
  682. const oldInType = this.state.inType;
  683. this.state.inType = true;
  684. this.expect(tok || types.colon);
  685. const type = this.flowParseType();
  686. this.state.inType = oldInType;
  687. return type;
  688. }
  689. flowParsePredicate() {
  690. const node = this.startNode();
  691. const moduloLoc = this.state.startLoc;
  692. const moduloPos = this.state.start;
  693. this.expect(types.modulo);
  694. const checksLoc = this.state.startLoc;
  695. this.expectContextual("checks");
  696. if (moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1) {
  697. this.raise(moduloPos, "Spaces between ´%´ and ´checks´ are not allowed here.");
  698. }
  699. if (this.eat(types.parenL)) {
  700. node.value = this.parseExpression();
  701. this.expect(types.parenR);
  702. return this.finishNode(node, "DeclaredPredicate");
  703. } else {
  704. return this.finishNode(node, "InferredPredicate");
  705. }
  706. }
  707. flowParseTypeAndPredicateInitialiser() {
  708. const oldInType = this.state.inType;
  709. this.state.inType = true;
  710. this.expect(types.colon);
  711. let type = null;
  712. let predicate = null;
  713. if (this.match(types.modulo)) {
  714. this.state.inType = oldInType;
  715. predicate = this.flowParsePredicate();
  716. } else {
  717. type = this.flowParseType();
  718. this.state.inType = oldInType;
  719. if (this.match(types.modulo)) {
  720. predicate = this.flowParsePredicate();
  721. }
  722. }
  723. return [type, predicate];
  724. }
  725. flowParseDeclareClass(node) {
  726. this.next();
  727. this.flowParseInterfaceish(node, true);
  728. return this.finishNode(node, "DeclareClass");
  729. }
  730. flowParseDeclareFunction(node) {
  731. this.next();
  732. const id = node.id = this.parseIdentifier();
  733. const typeNode = this.startNode();
  734. const typeContainer = this.startNode();
  735. if (this.isRelational("<")) {
  736. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  737. } else {
  738. typeNode.typeParameters = null;
  739. }
  740. this.expect(types.parenL);
  741. const tmp = this.flowParseFunctionTypeParams();
  742. typeNode.params = tmp.params;
  743. typeNode.rest = tmp.rest;
  744. this.expect(types.parenR);
  745. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  746. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  747. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  748. this.finishNode(id, id.type);
  749. this.semicolon();
  750. return this.finishNode(node, "DeclareFunction");
  751. }
  752. flowParseDeclare(node, insideModule) {
  753. if (this.match(types._class)) {
  754. return this.flowParseDeclareClass(node);
  755. } else if (this.match(types._function)) {
  756. return this.flowParseDeclareFunction(node);
  757. } else if (this.match(types._var)) {
  758. return this.flowParseDeclareVariable(node);
  759. } else if (this.isContextual("module")) {
  760. if (this.lookahead().type === types.dot) {
  761. return this.flowParseDeclareModuleExports(node);
  762. } else {
  763. if (insideModule) {
  764. this.unexpected(null, "`declare module` cannot be used inside another `declare module`");
  765. }
  766. return this.flowParseDeclareModule(node);
  767. }
  768. } else if (this.isContextual("type")) {
  769. return this.flowParseDeclareTypeAlias(node);
  770. } else if (this.isContextual("opaque")) {
  771. return this.flowParseDeclareOpaqueType(node);
  772. } else if (this.isContextual("interface")) {
  773. return this.flowParseDeclareInterface(node);
  774. } else if (this.match(types._export)) {
  775. return this.flowParseDeclareExportDeclaration(node, insideModule);
  776. } else {
  777. throw this.unexpected();
  778. }
  779. }
  780. flowParseDeclareVariable(node) {
  781. this.next();
  782. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  783. this.semicolon();
  784. return this.finishNode(node, "DeclareVariable");
  785. }
  786. flowParseDeclareModule(node) {
  787. this.next();
  788. this.scope.enter(SCOPE_OTHER);
  789. if (this.match(types.string)) {
  790. node.id = this.parseExprAtom();
  791. } else {
  792. node.id = this.parseIdentifier();
  793. }
  794. const bodyNode = node.body = this.startNode();
  795. const body = bodyNode.body = [];
  796. this.expect(types.braceL);
  797. while (!this.match(types.braceR)) {
  798. let bodyNode = this.startNode();
  799. if (this.match(types._import)) {
  800. const lookahead = this.lookahead();
  801. if (lookahead.value !== "type" && lookahead.value !== "typeof") {
  802. this.unexpected(null, "Imports within a `declare module` body must always be `import type` or `import typeof`");
  803. }
  804. this.next();
  805. this.parseImport(bodyNode);
  806. } else {
  807. this.expectContextual("declare", "Only declares and type imports are allowed inside declare module");
  808. bodyNode = this.flowParseDeclare(bodyNode, true);
  809. }
  810. body.push(bodyNode);
  811. }
  812. this.scope.exit();
  813. this.expect(types.braceR);
  814. this.finishNode(bodyNode, "BlockStatement");
  815. let kind = null;
  816. let hasModuleExport = false;
  817. 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";
  818. body.forEach(bodyElement => {
  819. if (isEsModuleType(bodyElement)) {
  820. if (kind === "CommonJS") {
  821. this.unexpected(bodyElement.start, errorMessage);
  822. }
  823. kind = "ES";
  824. } else if (bodyElement.type === "DeclareModuleExports") {
  825. if (hasModuleExport) {
  826. this.unexpected(bodyElement.start, "Duplicate `declare module.exports` statement");
  827. }
  828. if (kind === "ES") this.unexpected(bodyElement.start, errorMessage);
  829. kind = "CommonJS";
  830. hasModuleExport = true;
  831. }
  832. });
  833. node.kind = kind || "CommonJS";
  834. return this.finishNode(node, "DeclareModule");
  835. }
  836. flowParseDeclareExportDeclaration(node, insideModule) {
  837. this.expect(types._export);
  838. if (this.eat(types._default)) {
  839. if (this.match(types._function) || this.match(types._class)) {
  840. node.declaration = this.flowParseDeclare(this.startNode());
  841. } else {
  842. node.declaration = this.flowParseType();
  843. this.semicolon();
  844. }
  845. node.default = true;
  846. return this.finishNode(node, "DeclareExportDeclaration");
  847. } else {
  848. if (this.match(types._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  849. const label = this.state.value;
  850. const suggestion = exportSuggestions[label];
  851. this.unexpected(this.state.start, `\`declare export ${label}\` is not supported. Use \`${suggestion}\` instead`);
  852. }
  853. if (this.match(types._var) || this.match(types._function) || this.match(types._class) || this.isContextual("opaque")) {
  854. node.declaration = this.flowParseDeclare(this.startNode());
  855. node.default = false;
  856. return this.finishNode(node, "DeclareExportDeclaration");
  857. } else if (this.match(types.star) || this.match(types.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  858. node = this.parseExport(node);
  859. if (node.type === "ExportNamedDeclaration") {
  860. node.type = "ExportDeclaration";
  861. node.default = false;
  862. delete node.exportKind;
  863. }
  864. node.type = "Declare" + node.type;
  865. return node;
  866. }
  867. }
  868. throw this.unexpected();
  869. }
  870. flowParseDeclareModuleExports(node) {
  871. this.expectContextual("module");
  872. this.expect(types.dot);
  873. this.expectContextual("exports");
  874. node.typeAnnotation = this.flowParseTypeAnnotation();
  875. this.semicolon();
  876. return this.finishNode(node, "DeclareModuleExports");
  877. }
  878. flowParseDeclareTypeAlias(node) {
  879. this.next();
  880. this.flowParseTypeAlias(node);
  881. return this.finishNode(node, "DeclareTypeAlias");
  882. }
  883. flowParseDeclareOpaqueType(node) {
  884. this.next();
  885. this.flowParseOpaqueType(node, true);
  886. return this.finishNode(node, "DeclareOpaqueType");
  887. }
  888. flowParseDeclareInterface(node) {
  889. this.next();
  890. this.flowParseInterfaceish(node);
  891. return this.finishNode(node, "DeclareInterface");
  892. }
  893. flowParseInterfaceish(node, isClass = false) {
  894. node.id = this.flowParseRestrictedIdentifier(!isClass);
  895. if (this.isRelational("<")) {
  896. node.typeParameters = this.flowParseTypeParameterDeclaration();
  897. } else {
  898. node.typeParameters = null;
  899. }
  900. node.extends = [];
  901. node.implements = [];
  902. node.mixins = [];
  903. if (this.eat(types._extends)) {
  904. do {
  905. node.extends.push(this.flowParseInterfaceExtends());
  906. } while (!isClass && this.eat(types.comma));
  907. }
  908. if (this.isContextual("mixins")) {
  909. this.next();
  910. do {
  911. node.mixins.push(this.flowParseInterfaceExtends());
  912. } while (this.eat(types.comma));
  913. }
  914. if (this.isContextual("implements")) {
  915. this.next();
  916. do {
  917. node.implements.push(this.flowParseInterfaceExtends());
  918. } while (this.eat(types.comma));
  919. }
  920. node.body = this.flowParseObjectType({
  921. allowStatic: isClass,
  922. allowExact: false,
  923. allowSpread: false,
  924. allowProto: isClass,
  925. allowInexact: false
  926. });
  927. }
  928. flowParseInterfaceExtends() {
  929. const node = this.startNode();
  930. node.id = this.flowParseQualifiedTypeIdentifier();
  931. if (this.isRelational("<")) {
  932. node.typeParameters = this.flowParseTypeParameterInstantiation();
  933. } else {
  934. node.typeParameters = null;
  935. }
  936. return this.finishNode(node, "InterfaceExtends");
  937. }
  938. flowParseInterface(node) {
  939. this.flowParseInterfaceish(node);
  940. return this.finishNode(node, "InterfaceDeclaration");
  941. }
  942. checkNotUnderscore(word) {
  943. if (word === "_") {
  944. throw this.unexpected(null, "`_` is only allowed as a type argument to call or new");
  945. }
  946. }
  947. checkReservedType(word, startLoc) {
  948. if (reservedTypes.indexOf(word) > -1) {
  949. this.raise(startLoc, `Cannot overwrite reserved type ${word}`);
  950. }
  951. }
  952. flowParseRestrictedIdentifier(liberal) {
  953. this.checkReservedType(this.state.value, this.state.start);
  954. return this.parseIdentifier(liberal);
  955. }
  956. flowParseTypeAlias(node) {
  957. node.id = this.flowParseRestrictedIdentifier();
  958. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  959. if (this.isRelational("<")) {
  960. node.typeParameters = this.flowParseTypeParameterDeclaration();
  961. } else {
  962. node.typeParameters = null;
  963. }
  964. node.right = this.flowParseTypeInitialiser(types.eq);
  965. this.semicolon();
  966. return this.finishNode(node, "TypeAlias");
  967. }
  968. flowParseOpaqueType(node, declare) {
  969. this.expectContextual("type");
  970. node.id = this.flowParseRestrictedIdentifier(true);
  971. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  972. if (this.isRelational("<")) {
  973. node.typeParameters = this.flowParseTypeParameterDeclaration();
  974. } else {
  975. node.typeParameters = null;
  976. }
  977. node.supertype = null;
  978. if (this.match(types.colon)) {
  979. node.supertype = this.flowParseTypeInitialiser(types.colon);
  980. }
  981. node.impltype = null;
  982. if (!declare) {
  983. node.impltype = this.flowParseTypeInitialiser(types.eq);
  984. }
  985. this.semicolon();
  986. return this.finishNode(node, "OpaqueType");
  987. }
  988. flowParseTypeParameter(allowDefault = true, requireDefault = false) {
  989. if (!allowDefault && requireDefault) {
  990. throw new Error("Cannot disallow a default value (`allowDefault`) while also requiring it (`requireDefault`).");
  991. }
  992. const nodeStart = this.state.start;
  993. const node = this.startNode();
  994. const variance = this.flowParseVariance();
  995. const ident = this.flowParseTypeAnnotatableIdentifier();
  996. node.name = ident.name;
  997. node.variance = variance;
  998. node.bound = ident.typeAnnotation;
  999. if (this.match(types.eq)) {
  1000. if (allowDefault) {
  1001. this.eat(types.eq);
  1002. node.default = this.flowParseType();
  1003. } else {
  1004. this.unexpected();
  1005. }
  1006. } else {
  1007. if (requireDefault) {
  1008. this.unexpected(nodeStart, "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.");
  1009. }
  1010. }
  1011. return this.finishNode(node, "TypeParameter");
  1012. }
  1013. flowParseTypeParameterDeclaration(allowDefault = true) {
  1014. const oldInType = this.state.inType;
  1015. const node = this.startNode();
  1016. node.params = [];
  1017. this.state.inType = true;
  1018. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  1019. this.next();
  1020. } else {
  1021. this.unexpected();
  1022. }
  1023. let defaultRequired = false;
  1024. do {
  1025. const typeParameter = this.flowParseTypeParameter(allowDefault, defaultRequired);
  1026. node.params.push(typeParameter);
  1027. if (typeParameter.default) {
  1028. defaultRequired = true;
  1029. }
  1030. if (!this.isRelational(">")) {
  1031. this.expect(types.comma);
  1032. }
  1033. } while (!this.isRelational(">"));
  1034. this.expectRelational(">");
  1035. this.state.inType = oldInType;
  1036. return this.finishNode(node, "TypeParameterDeclaration");
  1037. }
  1038. flowParseTypeParameterInstantiation() {
  1039. const node = this.startNode();
  1040. const oldInType = this.state.inType;
  1041. node.params = [];
  1042. this.state.inType = true;
  1043. this.expectRelational("<");
  1044. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1045. this.state.noAnonFunctionType = false;
  1046. while (!this.isRelational(">")) {
  1047. node.params.push(this.flowParseType());
  1048. if (!this.isRelational(">")) {
  1049. this.expect(types.comma);
  1050. }
  1051. }
  1052. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1053. this.expectRelational(">");
  1054. this.state.inType = oldInType;
  1055. return this.finishNode(node, "TypeParameterInstantiation");
  1056. }
  1057. flowParseTypeParameterInstantiationCallOrNew() {
  1058. const node = this.startNode();
  1059. const oldInType = this.state.inType;
  1060. node.params = [];
  1061. this.state.inType = true;
  1062. this.expectRelational("<");
  1063. while (!this.isRelational(">")) {
  1064. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  1065. if (!this.isRelational(">")) {
  1066. this.expect(types.comma);
  1067. }
  1068. }
  1069. this.expectRelational(">");
  1070. this.state.inType = oldInType;
  1071. return this.finishNode(node, "TypeParameterInstantiation");
  1072. }
  1073. flowParseInterfaceType() {
  1074. const node = this.startNode();
  1075. this.expectContextual("interface");
  1076. node.extends = [];
  1077. if (this.eat(types._extends)) {
  1078. do {
  1079. node.extends.push(this.flowParseInterfaceExtends());
  1080. } while (this.eat(types.comma));
  1081. }
  1082. node.body = this.flowParseObjectType({
  1083. allowStatic: false,
  1084. allowExact: false,
  1085. allowSpread: false,
  1086. allowProto: false,
  1087. allowInexact: false
  1088. });
  1089. return this.finishNode(node, "InterfaceTypeAnnotation");
  1090. }
  1091. flowParseObjectPropertyKey() {
  1092. return this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  1093. }
  1094. flowParseObjectTypeIndexer(node, isStatic, variance) {
  1095. node.static = isStatic;
  1096. if (this.lookahead().type === types.colon) {
  1097. node.id = this.flowParseObjectPropertyKey();
  1098. node.key = this.flowParseTypeInitialiser();
  1099. } else {
  1100. node.id = null;
  1101. node.key = this.flowParseType();
  1102. }
  1103. this.expect(types.bracketR);
  1104. node.value = this.flowParseTypeInitialiser();
  1105. node.variance = variance;
  1106. return this.finishNode(node, "ObjectTypeIndexer");
  1107. }
  1108. flowParseObjectTypeInternalSlot(node, isStatic) {
  1109. node.static = isStatic;
  1110. node.id = this.flowParseObjectPropertyKey();
  1111. this.expect(types.bracketR);
  1112. this.expect(types.bracketR);
  1113. if (this.isRelational("<") || this.match(types.parenL)) {
  1114. node.method = true;
  1115. node.optional = false;
  1116. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1117. } else {
  1118. node.method = false;
  1119. if (this.eat(types.question)) {
  1120. node.optional = true;
  1121. }
  1122. node.value = this.flowParseTypeInitialiser();
  1123. }
  1124. return this.finishNode(node, "ObjectTypeInternalSlot");
  1125. }
  1126. flowParseObjectTypeMethodish(node) {
  1127. node.params = [];
  1128. node.rest = null;
  1129. node.typeParameters = null;
  1130. if (this.isRelational("<")) {
  1131. node.typeParameters = this.flowParseTypeParameterDeclaration(false);
  1132. }
  1133. this.expect(types.parenL);
  1134. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1135. node.params.push(this.flowParseFunctionTypeParam());
  1136. if (!this.match(types.parenR)) {
  1137. this.expect(types.comma);
  1138. }
  1139. }
  1140. if (this.eat(types.ellipsis)) {
  1141. node.rest = this.flowParseFunctionTypeParam();
  1142. }
  1143. this.expect(types.parenR);
  1144. node.returnType = this.flowParseTypeInitialiser();
  1145. return this.finishNode(node, "FunctionTypeAnnotation");
  1146. }
  1147. flowParseObjectTypeCallProperty(node, isStatic) {
  1148. const valueNode = this.startNode();
  1149. node.static = isStatic;
  1150. node.value = this.flowParseObjectTypeMethodish(valueNode);
  1151. return this.finishNode(node, "ObjectTypeCallProperty");
  1152. }
  1153. flowParseObjectType({
  1154. allowStatic,
  1155. allowExact,
  1156. allowSpread,
  1157. allowProto,
  1158. allowInexact
  1159. }) {
  1160. const oldInType = this.state.inType;
  1161. this.state.inType = true;
  1162. const nodeStart = this.startNode();
  1163. nodeStart.callProperties = [];
  1164. nodeStart.properties = [];
  1165. nodeStart.indexers = [];
  1166. nodeStart.internalSlots = [];
  1167. let endDelim;
  1168. let exact;
  1169. let inexact = false;
  1170. if (allowExact && this.match(types.braceBarL)) {
  1171. this.expect(types.braceBarL);
  1172. endDelim = types.braceBarR;
  1173. exact = true;
  1174. } else {
  1175. this.expect(types.braceL);
  1176. endDelim = types.braceR;
  1177. exact = false;
  1178. }
  1179. nodeStart.exact = exact;
  1180. while (!this.match(endDelim)) {
  1181. let isStatic = false;
  1182. let protoStart = null;
  1183. const node = this.startNode();
  1184. if (allowProto && this.isContextual("proto")) {
  1185. const lookahead = this.lookahead();
  1186. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1187. this.next();
  1188. protoStart = this.state.start;
  1189. allowStatic = false;
  1190. }
  1191. }
  1192. if (allowStatic && this.isContextual("static")) {
  1193. const lookahead = this.lookahead();
  1194. if (lookahead.type !== types.colon && lookahead.type !== types.question) {
  1195. this.next();
  1196. isStatic = true;
  1197. }
  1198. }
  1199. const variance = this.flowParseVariance();
  1200. if (this.eat(types.bracketL)) {
  1201. if (protoStart != null) {
  1202. this.unexpected(protoStart);
  1203. }
  1204. if (this.eat(types.bracketL)) {
  1205. if (variance) {
  1206. this.unexpected(variance.start);
  1207. }
  1208. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  1209. } else {
  1210. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  1211. }
  1212. } else if (this.match(types.parenL) || this.isRelational("<")) {
  1213. if (protoStart != null) {
  1214. this.unexpected(protoStart);
  1215. }
  1216. if (variance) {
  1217. this.unexpected(variance.start);
  1218. }
  1219. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  1220. } else {
  1221. let kind = "init";
  1222. if (this.isContextual("get") || this.isContextual("set")) {
  1223. const lookahead = this.lookahead();
  1224. if (lookahead.type === types.name || lookahead.type === types.string || lookahead.type === types.num) {
  1225. kind = this.state.value;
  1226. this.next();
  1227. }
  1228. }
  1229. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact);
  1230. if (propOrInexact === null) {
  1231. inexact = true;
  1232. } else {
  1233. nodeStart.properties.push(propOrInexact);
  1234. }
  1235. }
  1236. this.flowObjectTypeSemicolon();
  1237. }
  1238. this.expect(endDelim);
  1239. if (allowSpread) {
  1240. nodeStart.inexact = inexact;
  1241. }
  1242. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  1243. this.state.inType = oldInType;
  1244. return out;
  1245. }
  1246. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  1247. if (this.match(types.ellipsis)) {
  1248. if (!allowSpread) {
  1249. this.unexpected(null, "Spread operator cannot appear in class or interface definitions");
  1250. }
  1251. if (protoStart != null) {
  1252. this.unexpected(protoStart);
  1253. }
  1254. if (variance) {
  1255. this.unexpected(variance.start, "Spread properties cannot have variance");
  1256. }
  1257. this.expect(types.ellipsis);
  1258. const isInexactToken = this.eat(types.comma) || this.eat(types.semi);
  1259. if (this.match(types.braceR)) {
  1260. if (allowInexact) return null;
  1261. this.unexpected(null, "Explicit inexact syntax is only allowed inside inexact objects");
  1262. }
  1263. if (this.match(types.braceBarR)) {
  1264. this.unexpected(null, "Explicit inexact syntax cannot appear inside an explicit exact object type");
  1265. }
  1266. if (isInexactToken) {
  1267. this.unexpected(null, "Explicit inexact syntax must appear at the end of an inexact object");
  1268. }
  1269. node.argument = this.flowParseType();
  1270. return this.finishNode(node, "ObjectTypeSpreadProperty");
  1271. } else {
  1272. node.key = this.flowParseObjectPropertyKey();
  1273. node.static = isStatic;
  1274. node.proto = protoStart != null;
  1275. node.kind = kind;
  1276. let optional = false;
  1277. if (this.isRelational("<") || this.match(types.parenL)) {
  1278. node.method = true;
  1279. if (protoStart != null) {
  1280. this.unexpected(protoStart);
  1281. }
  1282. if (variance) {
  1283. this.unexpected(variance.start);
  1284. }
  1285. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  1286. if (kind === "get" || kind === "set") {
  1287. this.flowCheckGetterSetterParams(node);
  1288. }
  1289. } else {
  1290. if (kind !== "init") this.unexpected();
  1291. node.method = false;
  1292. if (this.eat(types.question)) {
  1293. optional = true;
  1294. }
  1295. node.value = this.flowParseTypeInitialiser();
  1296. node.variance = variance;
  1297. }
  1298. node.optional = optional;
  1299. return this.finishNode(node, "ObjectTypeProperty");
  1300. }
  1301. }
  1302. flowCheckGetterSetterParams(property) {
  1303. const paramCount = property.kind === "get" ? 0 : 1;
  1304. const start = property.start;
  1305. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  1306. if (length !== paramCount) {
  1307. if (property.kind === "get") {
  1308. this.raise(start, "getter must not have any formal parameters");
  1309. } else {
  1310. this.raise(start, "setter must have exactly one formal parameter");
  1311. }
  1312. }
  1313. if (property.kind === "set" && property.value.rest) {
  1314. this.raise(start, "setter function argument must not be a rest parameter");
  1315. }
  1316. }
  1317. flowObjectTypeSemicolon() {
  1318. if (!this.eat(types.semi) && !this.eat(types.comma) && !this.match(types.braceR) && !this.match(types.braceBarR)) {
  1319. this.unexpected();
  1320. }
  1321. }
  1322. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  1323. startPos = startPos || this.state.start;
  1324. startLoc = startLoc || this.state.startLoc;
  1325. let node = id || this.parseIdentifier();
  1326. while (this.eat(types.dot)) {
  1327. const node2 = this.startNodeAt(startPos, startLoc);
  1328. node2.qualification = node;
  1329. node2.id = this.parseIdentifier();
  1330. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  1331. }
  1332. return node;
  1333. }
  1334. flowParseGenericType(startPos, startLoc, id) {
  1335. const node = this.startNodeAt(startPos, startLoc);
  1336. node.typeParameters = null;
  1337. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  1338. if (this.isRelational("<")) {
  1339. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1340. }
  1341. return this.finishNode(node, "GenericTypeAnnotation");
  1342. }
  1343. flowParseTypeofType() {
  1344. const node = this.startNode();
  1345. this.expect(types._typeof);
  1346. node.argument = this.flowParsePrimaryType();
  1347. return this.finishNode(node, "TypeofTypeAnnotation");
  1348. }
  1349. flowParseTupleType() {
  1350. const node = this.startNode();
  1351. node.types = [];
  1352. this.expect(types.bracketL);
  1353. while (this.state.pos < this.length && !this.match(types.bracketR)) {
  1354. node.types.push(this.flowParseType());
  1355. if (this.match(types.bracketR)) break;
  1356. this.expect(types.comma);
  1357. }
  1358. this.expect(types.bracketR);
  1359. return this.finishNode(node, "TupleTypeAnnotation");
  1360. }
  1361. flowParseFunctionTypeParam() {
  1362. let name = null;
  1363. let optional = false;
  1364. let typeAnnotation = null;
  1365. const node = this.startNode();
  1366. const lh = this.lookahead();
  1367. if (lh.type === types.colon || lh.type === types.question) {
  1368. name = this.parseIdentifier();
  1369. if (this.eat(types.question)) {
  1370. optional = true;
  1371. }
  1372. typeAnnotation = this.flowParseTypeInitialiser();
  1373. } else {
  1374. typeAnnotation = this.flowParseType();
  1375. }
  1376. node.name = name;
  1377. node.optional = optional;
  1378. node.typeAnnotation = typeAnnotation;
  1379. return this.finishNode(node, "FunctionTypeParam");
  1380. }
  1381. reinterpretTypeAsFunctionTypeParam(type) {
  1382. const node = this.startNodeAt(type.start, type.loc.start);
  1383. node.name = null;
  1384. node.optional = false;
  1385. node.typeAnnotation = type;
  1386. return this.finishNode(node, "FunctionTypeParam");
  1387. }
  1388. flowParseFunctionTypeParams(params = []) {
  1389. let rest = null;
  1390. while (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1391. params.push(this.flowParseFunctionTypeParam());
  1392. if (!this.match(types.parenR)) {
  1393. this.expect(types.comma);
  1394. }
  1395. }
  1396. if (this.eat(types.ellipsis)) {
  1397. rest = this.flowParseFunctionTypeParam();
  1398. }
  1399. return {
  1400. params,
  1401. rest
  1402. };
  1403. }
  1404. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  1405. switch (id.name) {
  1406. case "any":
  1407. return this.finishNode(node, "AnyTypeAnnotation");
  1408. case "bool":
  1409. case "boolean":
  1410. return this.finishNode(node, "BooleanTypeAnnotation");
  1411. case "mixed":
  1412. return this.finishNode(node, "MixedTypeAnnotation");
  1413. case "empty":
  1414. return this.finishNode(node, "EmptyTypeAnnotation");
  1415. case "number":
  1416. return this.finishNode(node, "NumberTypeAnnotation");
  1417. case "string":
  1418. return this.finishNode(node, "StringTypeAnnotation");
  1419. default:
  1420. this.checkNotUnderscore(id.name);
  1421. return this.flowParseGenericType(startPos, startLoc, id);
  1422. }
  1423. }
  1424. flowParsePrimaryType() {
  1425. const startPos = this.state.start;
  1426. const startLoc = this.state.startLoc;
  1427. const node = this.startNode();
  1428. let tmp;
  1429. let type;
  1430. let isGroupedType = false;
  1431. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  1432. switch (this.state.type) {
  1433. case types.name:
  1434. if (this.isContextual("interface")) {
  1435. return this.flowParseInterfaceType();
  1436. }
  1437. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  1438. case types.braceL:
  1439. return this.flowParseObjectType({
  1440. allowStatic: false,
  1441. allowExact: false,
  1442. allowSpread: true,
  1443. allowProto: false,
  1444. allowInexact: true
  1445. });
  1446. case types.braceBarL:
  1447. return this.flowParseObjectType({
  1448. allowStatic: false,
  1449. allowExact: true,
  1450. allowSpread: true,
  1451. allowProto: false,
  1452. allowInexact: false
  1453. });
  1454. case types.bracketL:
  1455. return this.flowParseTupleType();
  1456. case types.relational:
  1457. if (this.state.value === "<") {
  1458. node.typeParameters = this.flowParseTypeParameterDeclaration(false);
  1459. this.expect(types.parenL);
  1460. tmp = this.flowParseFunctionTypeParams();
  1461. node.params = tmp.params;
  1462. node.rest = tmp.rest;
  1463. this.expect(types.parenR);
  1464. this.expect(types.arrow);
  1465. node.returnType = this.flowParseType();
  1466. return this.finishNode(node, "FunctionTypeAnnotation");
  1467. }
  1468. break;
  1469. case types.parenL:
  1470. this.next();
  1471. if (!this.match(types.parenR) && !this.match(types.ellipsis)) {
  1472. if (this.match(types.name)) {
  1473. const token = this.lookahead().type;
  1474. isGroupedType = token !== types.question && token !== types.colon;
  1475. } else {
  1476. isGroupedType = true;
  1477. }
  1478. }
  1479. if (isGroupedType) {
  1480. this.state.noAnonFunctionType = false;
  1481. type = this.flowParseType();
  1482. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  1483. if (this.state.noAnonFunctionType || !(this.match(types.comma) || this.match(types.parenR) && this.lookahead().type === types.arrow)) {
  1484. this.expect(types.parenR);
  1485. return type;
  1486. } else {
  1487. this.eat(types.comma);
  1488. }
  1489. }
  1490. if (type) {
  1491. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  1492. } else {
  1493. tmp = this.flowParseFunctionTypeParams();
  1494. }
  1495. node.params = tmp.params;
  1496. node.rest = tmp.rest;
  1497. this.expect(types.parenR);
  1498. this.expect(types.arrow);
  1499. node.returnType = this.flowParseType();
  1500. node.typeParameters = null;
  1501. return this.finishNode(node, "FunctionTypeAnnotation");
  1502. case types.string:
  1503. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  1504. case types._true:
  1505. case types._false:
  1506. node.value = this.match(types._true);
  1507. this.next();
  1508. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  1509. case types.plusMin:
  1510. if (this.state.value === "-") {
  1511. this.next();
  1512. if (!this.match(types.num)) {
  1513. this.unexpected(null, `Unexpected token, expected "number"`);
  1514. }
  1515. return this.parseLiteral(-this.state.value, "NumberLiteralTypeAnnotation", node.start, node.loc.start);
  1516. }
  1517. this.unexpected();
  1518. case types.num:
  1519. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  1520. case types._void:
  1521. this.next();
  1522. return this.finishNode(node, "VoidTypeAnnotation");
  1523. case types._null:
  1524. this.next();
  1525. return this.finishNode(node, "NullLiteralTypeAnnotation");
  1526. case types._this:
  1527. this.next();
  1528. return this.finishNode(node, "ThisTypeAnnotation");
  1529. case types.star:
  1530. this.next();
  1531. return this.finishNode(node, "ExistsTypeAnnotation");
  1532. default:
  1533. if (this.state.type.keyword === "typeof") {
  1534. return this.flowParseTypeofType();
  1535. } else if (this.state.type.keyword) {
  1536. const label = this.state.type.label;
  1537. this.next();
  1538. return super.createIdentifier(node, label);
  1539. }
  1540. }
  1541. throw this.unexpected();
  1542. }
  1543. flowParsePostfixType() {
  1544. const startPos = this.state.start,
  1545. startLoc = this.state.startLoc;
  1546. let type = this.flowParsePrimaryType();
  1547. while (this.match(types.bracketL) && !this.canInsertSemicolon()) {
  1548. const node = this.startNodeAt(startPos, startLoc);
  1549. node.elementType = type;
  1550. this.expect(types.bracketL);
  1551. this.expect(types.bracketR);
  1552. type = this.finishNode(node, "ArrayTypeAnnotation");
  1553. }
  1554. return type;
  1555. }
  1556. flowParsePrefixType() {
  1557. const node = this.startNode();
  1558. if (this.eat(types.question)) {
  1559. node.typeAnnotation = this.flowParsePrefixType();
  1560. return this.finishNode(node, "NullableTypeAnnotation");
  1561. } else {
  1562. return this.flowParsePostfixType();
  1563. }
  1564. }
  1565. flowParseAnonFunctionWithoutParens() {
  1566. const param = this.flowParsePrefixType();
  1567. if (!this.state.noAnonFunctionType && this.eat(types.arrow)) {
  1568. const node = this.startNodeAt(param.start, param.loc.start);
  1569. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  1570. node.rest = null;
  1571. node.returnType = this.flowParseType();
  1572. node.typeParameters = null;
  1573. return this.finishNode(node, "FunctionTypeAnnotation");
  1574. }
  1575. return param;
  1576. }
  1577. flowParseIntersectionType() {
  1578. const node = this.startNode();
  1579. this.eat(types.bitwiseAND);
  1580. const type = this.flowParseAnonFunctionWithoutParens();
  1581. node.types = [type];
  1582. while (this.eat(types.bitwiseAND)) {
  1583. node.types.push(this.flowParseAnonFunctionWithoutParens());
  1584. }
  1585. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  1586. }
  1587. flowParseUnionType() {
  1588. const node = this.startNode();
  1589. this.eat(types.bitwiseOR);
  1590. const type = this.flowParseIntersectionType();
  1591. node.types = [type];
  1592. while (this.eat(types.bitwiseOR)) {
  1593. node.types.push(this.flowParseIntersectionType());
  1594. }
  1595. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  1596. }
  1597. flowParseType() {
  1598. const oldInType = this.state.inType;
  1599. this.state.inType = true;
  1600. const type = this.flowParseUnionType();
  1601. this.state.inType = oldInType;
  1602. this.state.exprAllowed = this.state.exprAllowed || this.state.noAnonFunctionType;
  1603. return type;
  1604. }
  1605. flowParseTypeOrImplicitInstantiation() {
  1606. if (this.state.type === types.name && this.state.value === "_") {
  1607. const startPos = this.state.start;
  1608. const startLoc = this.state.startLoc;
  1609. const node = this.parseIdentifier();
  1610. return this.flowParseGenericType(startPos, startLoc, node);
  1611. } else {
  1612. return this.flowParseType();
  1613. }
  1614. }
  1615. flowParseTypeAnnotation() {
  1616. const node = this.startNode();
  1617. node.typeAnnotation = this.flowParseTypeInitialiser();
  1618. return this.finishNode(node, "TypeAnnotation");
  1619. }
  1620. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  1621. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  1622. if (this.match(types.colon)) {
  1623. ident.typeAnnotation = this.flowParseTypeAnnotation();
  1624. this.finishNode(ident, ident.type);
  1625. }
  1626. return ident;
  1627. }
  1628. typeCastToParameter(node) {
  1629. node.expression.typeAnnotation = node.typeAnnotation;
  1630. return this.finishNodeAt(node.expression, node.expression.type, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  1631. }
  1632. flowParseVariance() {
  1633. let variance = null;
  1634. if (this.match(types.plusMin)) {
  1635. variance = this.startNode();
  1636. if (this.state.value === "+") {
  1637. variance.kind = "plus";
  1638. } else {
  1639. variance.kind = "minus";
  1640. }
  1641. this.next();
  1642. this.finishNode(variance, "Variance");
  1643. }
  1644. return variance;
  1645. }
  1646. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  1647. if (allowExpressionBody) {
  1648. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  1649. }
  1650. return super.parseFunctionBody(node, false, isMethod);
  1651. }
  1652. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  1653. if (this.match(types.colon)) {
  1654. const typeNode = this.startNode();
  1655. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1656. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  1657. }
  1658. super.parseFunctionBodyAndFinish(node, type, isMethod);
  1659. }
  1660. parseStatement(context, topLevel) {
  1661. if (this.state.strict && this.match(types.name) && this.state.value === "interface") {
  1662. const node = this.startNode();
  1663. this.next();
  1664. return this.flowParseInterface(node);
  1665. } else {
  1666. const stmt = super.parseStatement(context, topLevel);
  1667. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  1668. this.flowPragma = null;
  1669. }
  1670. return stmt;
  1671. }
  1672. }
  1673. parseExpressionStatement(node, expr) {
  1674. if (expr.type === "Identifier") {
  1675. if (expr.name === "declare") {
  1676. if (this.match(types._class) || this.match(types.name) || this.match(types._function) || this.match(types._var) || this.match(types._export)) {
  1677. return this.flowParseDeclare(node);
  1678. }
  1679. } else if (this.match(types.name)) {
  1680. if (expr.name === "interface") {
  1681. return this.flowParseInterface(node);
  1682. } else if (expr.name === "type") {
  1683. return this.flowParseTypeAlias(node);
  1684. } else if (expr.name === "opaque") {
  1685. return this.flowParseOpaqueType(node, false);
  1686. }
  1687. }
  1688. }
  1689. return super.parseExpressionStatement(node, expr);
  1690. }
  1691. shouldParseExportDeclaration() {
  1692. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || super.shouldParseExportDeclaration();
  1693. }
  1694. isExportDefaultSpecifier() {
  1695. if (this.match(types.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque")) {
  1696. return false;
  1697. }
  1698. return super.isExportDefaultSpecifier();
  1699. }
  1700. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  1701. if (!this.match(types.question)) return expr;
  1702. if (refNeedsArrowPos) {
  1703. const state = this.state.clone();
  1704. try {
  1705. return super.parseConditional(expr, noIn, startPos, startLoc);
  1706. } catch (err) {
  1707. if (err instanceof SyntaxError) {
  1708. this.state = state;
  1709. refNeedsArrowPos.start = err.pos || this.state.start;
  1710. return expr;
  1711. } else {
  1712. throw err;
  1713. }
  1714. }
  1715. }
  1716. this.expect(types.question);
  1717. const state = this.state.clone();
  1718. const originalNoArrowAt = this.state.noArrowAt;
  1719. const node = this.startNodeAt(startPos, startLoc);
  1720. let {
  1721. consequent,
  1722. failed
  1723. } = this.tryParseConditionalConsequent();
  1724. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1725. if (failed || invalid.length > 0) {
  1726. const noArrowAt = [...originalNoArrowAt];
  1727. if (invalid.length > 0) {
  1728. this.state = state;
  1729. this.state.noArrowAt = noArrowAt;
  1730. for (let i = 0; i < invalid.length; i++) {
  1731. noArrowAt.push(invalid[i].start);
  1732. }
  1733. ({
  1734. consequent,
  1735. failed
  1736. } = this.tryParseConditionalConsequent());
  1737. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  1738. }
  1739. if (failed && valid.length > 1) {
  1740. this.raise(state.start, "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.");
  1741. }
  1742. if (failed && valid.length === 1) {
  1743. this.state = state;
  1744. this.state.noArrowAt = noArrowAt.concat(valid[0].start);
  1745. ({
  1746. consequent,
  1747. failed
  1748. } = this.tryParseConditionalConsequent());
  1749. }
  1750. this.getArrowLikeExpressions(consequent, true);
  1751. }
  1752. this.state.noArrowAt = originalNoArrowAt;
  1753. this.expect(types.colon);
  1754. node.test = expr;
  1755. node.consequent = consequent;
  1756. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(noIn, undefined, undefined, undefined));
  1757. return this.finishNode(node, "ConditionalExpression");
  1758. }
  1759. tryParseConditionalConsequent() {
  1760. this.state.noArrowParamsConversionAt.push(this.state.start);
  1761. const consequent = this.parseMaybeAssign();
  1762. const failed = !this.match(types.colon);
  1763. this.state.noArrowParamsConversionAt.pop();
  1764. return {
  1765. consequent,
  1766. failed
  1767. };
  1768. }
  1769. getArrowLikeExpressions(node, disallowInvalid) {
  1770. const stack = [node];
  1771. const arrows = [];
  1772. while (stack.length !== 0) {
  1773. const node = stack.pop();
  1774. if (node.type === "ArrowFunctionExpression") {
  1775. if (node.typeParameters || !node.returnType) {
  1776. this.toAssignableList(node.params, true, "arrow function parameters");
  1777. this.scope.enter(functionFlags(false, false) | SCOPE_ARROW);
  1778. super.checkParams(node, false, true);
  1779. this.scope.exit();
  1780. } else {
  1781. arrows.push(node);
  1782. }
  1783. stack.push(node.body);
  1784. } else if (node.type === "ConditionalExpression") {
  1785. stack.push(node.consequent);
  1786. stack.push(node.alternate);
  1787. }
  1788. }
  1789. if (disallowInvalid) {
  1790. for (let i = 0; i < arrows.length; i++) {
  1791. this.toAssignableList(node.params, true, "arrow function parameters");
  1792. }
  1793. return [arrows, []];
  1794. }
  1795. return partition(arrows, node => {
  1796. try {
  1797. this.toAssignableList(node.params, true, "arrow function parameters");
  1798. return true;
  1799. } catch (err) {
  1800. return false;
  1801. }
  1802. });
  1803. }
  1804. forwardNoArrowParamsConversionAt(node, parse) {
  1805. let result;
  1806. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  1807. this.state.noArrowParamsConversionAt.push(this.state.start);
  1808. result = parse();
  1809. this.state.noArrowParamsConversionAt.pop();
  1810. } else {
  1811. result = parse();
  1812. }
  1813. return result;
  1814. }
  1815. parseParenItem(node, startPos, startLoc) {
  1816. node = super.parseParenItem(node, startPos, startLoc);
  1817. if (this.eat(types.question)) {
  1818. node.optional = true;
  1819. }
  1820. if (this.match(types.colon)) {
  1821. const typeCastNode = this.startNodeAt(startPos, startLoc);
  1822. typeCastNode.expression = node;
  1823. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  1824. return this.finishNode(typeCastNode, "TypeCastExpression");
  1825. }
  1826. return node;
  1827. }
  1828. assertModuleNodeAllowed(node) {
  1829. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  1830. return;
  1831. }
  1832. super.assertModuleNodeAllowed(node);
  1833. }
  1834. parseExport(node) {
  1835. const decl = super.parseExport(node);
  1836. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  1837. decl.exportKind = decl.exportKind || "value";
  1838. }
  1839. return decl;
  1840. }
  1841. parseExportDeclaration(node) {
  1842. if (this.isContextual("type")) {
  1843. node.exportKind = "type";
  1844. const declarationNode = this.startNode();
  1845. this.next();
  1846. if (this.match(types.braceL)) {
  1847. node.specifiers = this.parseExportSpecifiers();
  1848. this.parseExportFrom(node);
  1849. return null;
  1850. } else {
  1851. return this.flowParseTypeAlias(declarationNode);
  1852. }
  1853. } else if (this.isContextual("opaque")) {
  1854. node.exportKind = "type";
  1855. const declarationNode = this.startNode();
  1856. this.next();
  1857. return this.flowParseOpaqueType(declarationNode, false);
  1858. } else if (this.isContextual("interface")) {
  1859. node.exportKind = "type";
  1860. const declarationNode = this.startNode();
  1861. this.next();
  1862. return this.flowParseInterface(declarationNode);
  1863. } else {
  1864. return super.parseExportDeclaration(node);
  1865. }
  1866. }
  1867. eatExportStar(node) {
  1868. if (super.eatExportStar(...arguments)) return true;
  1869. if (this.isContextual("type") && this.lookahead().type === types.star) {
  1870. node.exportKind = "type";
  1871. this.next();
  1872. this.next();
  1873. return true;
  1874. }
  1875. return false;
  1876. }
  1877. maybeParseExportNamespaceSpecifier(node) {
  1878. const pos = this.state.start;
  1879. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  1880. if (hasNamespace && node.exportKind === "type") {
  1881. this.unexpected(pos);
  1882. }
  1883. return hasNamespace;
  1884. }
  1885. parseClassId(node, isStatement, optionalId) {
  1886. super.parseClassId(node, isStatement, optionalId);
  1887. if (this.isRelational("<")) {
  1888. node.typeParameters = this.flowParseTypeParameterDeclaration();
  1889. }
  1890. }
  1891. getTokenFromCode(code) {
  1892. const next = this.input.charCodeAt(this.state.pos + 1);
  1893. if (code === 123 && next === 124) {
  1894. return this.finishOp(types.braceBarL, 2);
  1895. } else if (this.state.inType && (code === 62 || code === 60)) {
  1896. return this.finishOp(types.relational, 1);
  1897. } else if (isIteratorStart(code, next)) {
  1898. this.state.isIterator = true;
  1899. return super.readWord();
  1900. } else {
  1901. return super.getTokenFromCode(code);
  1902. }
  1903. }
  1904. toAssignable(node, isBinding, contextDescription) {
  1905. if (node.type === "TypeCastExpression") {
  1906. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  1907. } else {
  1908. return super.toAssignable(node, isBinding, contextDescription);
  1909. }
  1910. }
  1911. toAssignableList(exprList, isBinding, contextDescription) {
  1912. for (let i = 0; i < exprList.length; i++) {
  1913. const expr = exprList[i];
  1914. if (expr && expr.type === "TypeCastExpression") {
  1915. exprList[i] = this.typeCastToParameter(expr);
  1916. }
  1917. }
  1918. return super.toAssignableList(exprList, isBinding, contextDescription);
  1919. }
  1920. toReferencedList(exprList, isParenthesizedExpr) {
  1921. for (let i = 0; i < exprList.length; i++) {
  1922. const expr = exprList[i];
  1923. if (expr && expr.type === "TypeCastExpression" && (!expr.extra || !expr.extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  1924. this.raise(expr.typeAnnotation.start, "The type cast expression is expected to be wrapped with parenthesis");
  1925. }
  1926. }
  1927. return exprList;
  1928. }
  1929. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  1930. if (expr.type !== "TypeCastExpression") {
  1931. return super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  1932. }
  1933. }
  1934. parseClassProperty(node) {
  1935. if (this.match(types.colon)) {
  1936. node.typeAnnotation = this.flowParseTypeAnnotation();
  1937. }
  1938. return super.parseClassProperty(node);
  1939. }
  1940. parseClassPrivateProperty(node) {
  1941. if (this.match(types.colon)) {
  1942. node.typeAnnotation = this.flowParseTypeAnnotation();
  1943. }
  1944. return super.parseClassPrivateProperty(node);
  1945. }
  1946. isClassMethod() {
  1947. return this.isRelational("<") || super.isClassMethod();
  1948. }
  1949. isClassProperty() {
  1950. return this.match(types.colon) || super.isClassProperty();
  1951. }
  1952. isNonstaticConstructor(method) {
  1953. return !this.match(types.colon) && super.isNonstaticConstructor(method);
  1954. }
  1955. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  1956. if (method.variance) {
  1957. this.unexpected(method.variance.start);
  1958. }
  1959. delete method.variance;
  1960. if (this.isRelational("<")) {
  1961. method.typeParameters = this.flowParseTypeParameterDeclaration(false);
  1962. }
  1963. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  1964. }
  1965. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  1966. if (method.variance) {
  1967. this.unexpected(method.variance.start);
  1968. }
  1969. delete method.variance;
  1970. if (this.isRelational("<")) {
  1971. method.typeParameters = this.flowParseTypeParameterDeclaration();
  1972. }
  1973. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  1974. }
  1975. parseClassSuper(node) {
  1976. super.parseClassSuper(node);
  1977. if (node.superClass && this.isRelational("<")) {
  1978. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  1979. }
  1980. if (this.isContextual("implements")) {
  1981. this.next();
  1982. const implemented = node.implements = [];
  1983. do {
  1984. const node = this.startNode();
  1985. node.id = this.flowParseRestrictedIdentifier(true);
  1986. if (this.isRelational("<")) {
  1987. node.typeParameters = this.flowParseTypeParameterInstantiation();
  1988. } else {
  1989. node.typeParameters = null;
  1990. }
  1991. implemented.push(this.finishNode(node, "ClassImplements"));
  1992. } while (this.eat(types.comma));
  1993. }
  1994. }
  1995. parsePropertyName(node) {
  1996. const variance = this.flowParseVariance();
  1997. const key = super.parsePropertyName(node);
  1998. node.variance = variance;
  1999. return key;
  2000. }
  2001. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  2002. if (prop.variance) {
  2003. this.unexpected(prop.variance.start);
  2004. }
  2005. delete prop.variance;
  2006. let typeParameters;
  2007. if (this.isRelational("<")) {
  2008. typeParameters = this.flowParseTypeParameterDeclaration(false);
  2009. if (!this.match(types.parenL)) this.unexpected();
  2010. }
  2011. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  2012. if (typeParameters) {
  2013. (prop.value || prop).typeParameters = typeParameters;
  2014. }
  2015. }
  2016. parseAssignableListItemTypes(param) {
  2017. if (this.eat(types.question)) {
  2018. if (param.type !== "Identifier") {
  2019. throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  2020. }
  2021. param.optional = true;
  2022. }
  2023. if (this.match(types.colon)) {
  2024. param.typeAnnotation = this.flowParseTypeAnnotation();
  2025. }
  2026. this.finishNode(param, param.type);
  2027. return param;
  2028. }
  2029. parseMaybeDefault(startPos, startLoc, left) {
  2030. const node = super.parseMaybeDefault(startPos, startLoc, left);
  2031. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  2032. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  2033. }
  2034. return node;
  2035. }
  2036. shouldParseDefaultImport(node) {
  2037. if (!hasTypeImportKind(node)) {
  2038. return super.shouldParseDefaultImport(node);
  2039. }
  2040. return isMaybeDefaultImport(this.state);
  2041. }
  2042. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  2043. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true) : this.parseIdentifier();
  2044. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  2045. node.specifiers.push(this.finishNode(specifier, type));
  2046. }
  2047. maybeParseDefaultImportSpecifier(node) {
  2048. node.importKind = "value";
  2049. let kind = null;
  2050. if (this.match(types._typeof)) {
  2051. kind = "typeof";
  2052. } else if (this.isContextual("type")) {
  2053. kind = "type";
  2054. }
  2055. if (kind) {
  2056. const lh = this.lookahead();
  2057. if (kind === "type" && lh.type === types.star) {
  2058. this.unexpected(lh.start);
  2059. }
  2060. if (isMaybeDefaultImport(lh) || lh.type === types.braceL || lh.type === types.star) {
  2061. this.next();
  2062. node.importKind = kind;
  2063. }
  2064. }
  2065. return super.maybeParseDefaultImportSpecifier(node);
  2066. }
  2067. parseImportSpecifier(node) {
  2068. const specifier = this.startNode();
  2069. const firstIdentLoc = this.state.start;
  2070. const firstIdent = this.parseIdentifier(true);
  2071. let specifierTypeKind = null;
  2072. if (firstIdent.name === "type") {
  2073. specifierTypeKind = "type";
  2074. } else if (firstIdent.name === "typeof") {
  2075. specifierTypeKind = "typeof";
  2076. }
  2077. let isBinding = false;
  2078. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  2079. const as_ident = this.parseIdentifier(true);
  2080. if (specifierTypeKind !== null && !this.match(types.name) && !this.state.type.keyword) {
  2081. specifier.imported = as_ident;
  2082. specifier.importKind = specifierTypeKind;
  2083. specifier.local = as_ident.__clone();
  2084. } else {
  2085. specifier.imported = firstIdent;
  2086. specifier.importKind = null;
  2087. specifier.local = this.parseIdentifier();
  2088. }
  2089. } else if (specifierTypeKind !== null && (this.match(types.name) || this.state.type.keyword)) {
  2090. specifier.imported = this.parseIdentifier(true);
  2091. specifier.importKind = specifierTypeKind;
  2092. if (this.eatContextual("as")) {
  2093. specifier.local = this.parseIdentifier();
  2094. } else {
  2095. isBinding = true;
  2096. specifier.local = specifier.imported.__clone();
  2097. }
  2098. } else {
  2099. isBinding = true;
  2100. specifier.imported = firstIdent;
  2101. specifier.importKind = null;
  2102. specifier.local = specifier.imported.__clone();
  2103. }
  2104. const nodeIsTypeImport = hasTypeImportKind(node);
  2105. const specifierIsTypeImport = hasTypeImportKind(specifier);
  2106. if (nodeIsTypeImport && specifierIsTypeImport) {
  2107. 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");
  2108. }
  2109. if (nodeIsTypeImport || specifierIsTypeImport) {
  2110. this.checkReservedType(specifier.local.name, specifier.local.start);
  2111. }
  2112. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  2113. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  2114. }
  2115. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  2116. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  2117. }
  2118. parseFunctionParams(node, allowModifiers) {
  2119. const kind = node.kind;
  2120. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  2121. node.typeParameters = this.flowParseTypeParameterDeclaration(false);
  2122. }
  2123. super.parseFunctionParams(node, allowModifiers);
  2124. }
  2125. parseVarId(decl, kind) {
  2126. super.parseVarId(decl, kind);
  2127. if (this.match(types.colon)) {
  2128. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  2129. this.finishNode(decl.id, decl.id.type);
  2130. }
  2131. }
  2132. parseAsyncArrowFromCallExpression(node, call) {
  2133. if (this.match(types.colon)) {
  2134. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2135. this.state.noAnonFunctionType = true;
  2136. node.returnType = this.flowParseTypeAnnotation();
  2137. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2138. }
  2139. return super.parseAsyncArrowFromCallExpression(node, call);
  2140. }
  2141. shouldParseAsyncArrow() {
  2142. return this.match(types.colon) || super.shouldParseAsyncArrow();
  2143. }
  2144. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  2145. let jsxError = null;
  2146. if (this.hasPlugin("jsx") && (this.match(types.jsxTagStart) || this.isRelational("<"))) {
  2147. const state = this.state.clone();
  2148. try {
  2149. return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
  2150. } catch (err) {
  2151. if (err instanceof SyntaxError) {
  2152. this.state = state;
  2153. const cLength = this.state.context.length;
  2154. if (this.state.context[cLength - 1] === types$1.j_oTag) {
  2155. this.state.context.length -= 2;
  2156. }
  2157. jsxError = err;
  2158. } else {
  2159. throw err;
  2160. }
  2161. }
  2162. }
  2163. if (jsxError != null || this.isRelational("<")) {
  2164. let arrowExpression;
  2165. let typeParameters;
  2166. try {
  2167. typeParameters = this.flowParseTypeParameterDeclaration();
  2168. arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos));
  2169. arrowExpression.typeParameters = typeParameters;
  2170. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  2171. } catch (err) {
  2172. throw jsxError || err;
  2173. }
  2174. if (arrowExpression.type === "ArrowFunctionExpression") {
  2175. return arrowExpression;
  2176. } else if (jsxError != null) {
  2177. throw jsxError;
  2178. } else {
  2179. this.raise(typeParameters.start, "Expected an arrow function after this type parameter declaration");
  2180. }
  2181. }
  2182. return super.parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos);
  2183. }
  2184. parseArrow(node) {
  2185. if (this.match(types.colon)) {
  2186. const state = this.state.clone();
  2187. try {
  2188. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2189. this.state.noAnonFunctionType = true;
  2190. const typeNode = this.startNode();
  2191. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2192. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2193. if (this.canInsertSemicolon()) this.unexpected();
  2194. if (!this.match(types.arrow)) this.unexpected();
  2195. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2196. } catch (err) {
  2197. if (err instanceof SyntaxError) {
  2198. this.state = state;
  2199. } else {
  2200. throw err;
  2201. }
  2202. }
  2203. }
  2204. return super.parseArrow(node);
  2205. }
  2206. shouldParseArrow() {
  2207. return this.match(types.colon) || super.shouldParseArrow();
  2208. }
  2209. setArrowFunctionParameters(node, params) {
  2210. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2211. node.params = params;
  2212. } else {
  2213. super.setArrowFunctionParameters(node, params);
  2214. }
  2215. }
  2216. checkParams(node, allowDuplicates, isArrowFunction) {
  2217. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  2218. return;
  2219. }
  2220. return super.checkParams(node, allowDuplicates, isArrowFunction);
  2221. }
  2222. parseParenAndDistinguishExpression(canBeArrow) {
  2223. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  2224. }
  2225. parseSubscripts(base, startPos, startLoc, noCalls) {
  2226. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  2227. this.next();
  2228. const node = this.startNodeAt(startPos, startLoc);
  2229. node.callee = base;
  2230. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2231. base = this.finishNode(node, "CallExpression");
  2232. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  2233. const state = this.state.clone();
  2234. let error;
  2235. try {
  2236. const node = this.parseAsyncArrowWithTypeParameters(startPos, startLoc);
  2237. if (node) return node;
  2238. } catch (e) {
  2239. error = e;
  2240. }
  2241. this.state = state;
  2242. try {
  2243. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  2244. } catch (e) {
  2245. throw error || e;
  2246. }
  2247. }
  2248. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  2249. }
  2250. parseSubscript(base, startPos, startLoc, noCalls, subscriptState, maybeAsyncArrow) {
  2251. if (this.match(types.questionDot) && this.isLookaheadRelational("<")) {
  2252. this.expectPlugin("optionalChaining");
  2253. subscriptState.optionalChainMember = true;
  2254. if (noCalls) {
  2255. subscriptState.stop = true;
  2256. return base;
  2257. }
  2258. this.next();
  2259. const node = this.startNodeAt(startPos, startLoc);
  2260. node.callee = base;
  2261. node.typeArguments = this.flowParseTypeParameterInstantiation();
  2262. this.expect(types.parenL);
  2263. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2264. node.optional = true;
  2265. return this.finishNode(node, "OptionalCallExpression");
  2266. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  2267. const node = this.startNodeAt(startPos, startLoc);
  2268. node.callee = base;
  2269. const state = this.state.clone();
  2270. try {
  2271. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  2272. this.expect(types.parenL);
  2273. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  2274. if (subscriptState.optionalChainMember) {
  2275. node.optional = false;
  2276. return this.finishNode(node, "OptionalCallExpression");
  2277. }
  2278. return this.finishNode(node, "CallExpression");
  2279. } catch (e) {
  2280. if (e instanceof SyntaxError) {
  2281. this.state = state;
  2282. } else {
  2283. throw e;
  2284. }
  2285. }
  2286. }
  2287. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState, maybeAsyncArrow);
  2288. }
  2289. parseNewArguments(node) {
  2290. let targs = null;
  2291. if (this.shouldParseTypes() && this.isRelational("<")) {
  2292. const state = this.state.clone();
  2293. try {
  2294. targs = this.flowParseTypeParameterInstantiationCallOrNew();
  2295. } catch (e) {
  2296. if (e instanceof SyntaxError) {
  2297. this.state = state;
  2298. } else {
  2299. throw e;
  2300. }
  2301. }
  2302. }
  2303. node.typeArguments = targs;
  2304. super.parseNewArguments(node);
  2305. }
  2306. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  2307. const node = this.startNodeAt(startPos, startLoc);
  2308. this.parseFunctionParams(node);
  2309. if (!this.parseArrow(node)) return;
  2310. return this.parseArrowExpression(node, undefined, true);
  2311. }
  2312. readToken_mult_modulo(code) {
  2313. const next = this.input.charCodeAt(this.state.pos + 1);
  2314. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  2315. this.state.hasFlowComment = false;
  2316. this.state.pos += 2;
  2317. this.nextToken();
  2318. return;
  2319. }
  2320. super.readToken_mult_modulo(code);
  2321. }
  2322. readToken_pipe_amp(code) {
  2323. const next = this.input.charCodeAt(this.state.pos + 1);
  2324. if (code === 124 && next === 125) {
  2325. this.finishOp(types.braceBarR, 2);
  2326. return;
  2327. }
  2328. super.readToken_pipe_amp(code);
  2329. }
  2330. parseTopLevel(file, program) {
  2331. const fileNode = super.parseTopLevel(file, program);
  2332. if (this.state.hasFlowComment) {
  2333. this.unexpected(null, "Unterminated flow-comment");
  2334. }
  2335. return fileNode;
  2336. }
  2337. skipBlockComment() {
  2338. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  2339. if (this.state.hasFlowComment) {
  2340. this.unexpected(null, "Cannot have a flow comment inside another flow comment");
  2341. }
  2342. this.hasFlowCommentCompletion();
  2343. this.state.pos += this.skipFlowComment();
  2344. this.state.hasFlowComment = true;
  2345. return;
  2346. }
  2347. if (this.state.hasFlowComment) {
  2348. const end = this.input.indexOf("*-/", this.state.pos += 2);
  2349. if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
  2350. this.state.pos = end + 3;
  2351. return;
  2352. }
  2353. super.skipBlockComment();
  2354. }
  2355. skipFlowComment() {
  2356. const {
  2357. pos
  2358. } = this.state;
  2359. let shiftToFirstNonWhiteSpace = 2;
  2360. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  2361. shiftToFirstNonWhiteSpace++;
  2362. }
  2363. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  2364. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  2365. if (ch2 === 58 && ch3 === 58) {
  2366. return shiftToFirstNonWhiteSpace + 2;
  2367. }
  2368. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  2369. return shiftToFirstNonWhiteSpace + 12;
  2370. }
  2371. if (ch2 === 58 && ch3 !== 58) {
  2372. return shiftToFirstNonWhiteSpace;
  2373. }
  2374. return false;
  2375. }
  2376. hasFlowCommentCompletion() {
  2377. const end = this.input.indexOf("*/", this.state.pos);
  2378. if (end === -1) {
  2379. this.raise(this.state.pos, "Unterminated comment");
  2380. }
  2381. }
  2382. });
  2383. const entities = {
  2384. quot: "\u0022",
  2385. amp: "&",
  2386. apos: "\u0027",
  2387. lt: "<",
  2388. gt: ">",
  2389. nbsp: "\u00A0",
  2390. iexcl: "\u00A1",
  2391. cent: "\u00A2",
  2392. pound: "\u00A3",
  2393. curren: "\u00A4",
  2394. yen: "\u00A5",
  2395. brvbar: "\u00A6",
  2396. sect: "\u00A7",
  2397. uml: "\u00A8",
  2398. copy: "\u00A9",
  2399. ordf: "\u00AA",
  2400. laquo: "\u00AB",
  2401. not: "\u00AC",
  2402. shy: "\u00AD",
  2403. reg: "\u00AE",
  2404. macr: "\u00AF",
  2405. deg: "\u00B0",
  2406. plusmn: "\u00B1",
  2407. sup2: "\u00B2",
  2408. sup3: "\u00B3",
  2409. acute: "\u00B4",
  2410. micro: "\u00B5",
  2411. para: "\u00B6",
  2412. middot: "\u00B7",
  2413. cedil: "\u00B8",
  2414. sup1: "\u00B9",
  2415. ordm: "\u00BA",
  2416. raquo: "\u00BB",
  2417. frac14: "\u00BC",
  2418. frac12: "\u00BD",
  2419. frac34: "\u00BE",
  2420. iquest: "\u00BF",
  2421. Agrave: "\u00C0",
  2422. Aacute: "\u00C1",
  2423. Acirc: "\u00C2",
  2424. Atilde: "\u00C3",
  2425. Auml: "\u00C4",
  2426. Aring: "\u00C5",
  2427. AElig: "\u00C6",
  2428. Ccedil: "\u00C7",
  2429. Egrave: "\u00C8",
  2430. Eacute: "\u00C9",
  2431. Ecirc: "\u00CA",
  2432. Euml: "\u00CB",
  2433. Igrave: "\u00CC",
  2434. Iacute: "\u00CD",
  2435. Icirc: "\u00CE",
  2436. Iuml: "\u00CF",
  2437. ETH: "\u00D0",
  2438. Ntilde: "\u00D1",
  2439. Ograve: "\u00D2",
  2440. Oacute: "\u00D3",
  2441. Ocirc: "\u00D4",
  2442. Otilde: "\u00D5",
  2443. Ouml: "\u00D6",
  2444. times: "\u00D7",
  2445. Oslash: "\u00D8",
  2446. Ugrave: "\u00D9",
  2447. Uacute: "\u00DA",
  2448. Ucirc: "\u00DB",
  2449. Uuml: "\u00DC",
  2450. Yacute: "\u00DD",
  2451. THORN: "\u00DE",
  2452. szlig: "\u00DF",
  2453. agrave: "\u00E0",
  2454. aacute: "\u00E1",
  2455. acirc: "\u00E2",
  2456. atilde: "\u00E3",
  2457. auml: "\u00E4",
  2458. aring: "\u00E5",
  2459. aelig: "\u00E6",
  2460. ccedil: "\u00E7",
  2461. egrave: "\u00E8",
  2462. eacute: "\u00E9",
  2463. ecirc: "\u00EA",
  2464. euml: "\u00EB",
  2465. igrave: "\u00EC",
  2466. iacute: "\u00ED",
  2467. icirc: "\u00EE",
  2468. iuml: "\u00EF",
  2469. eth: "\u00F0",
  2470. ntilde: "\u00F1",
  2471. ograve: "\u00F2",
  2472. oacute: "\u00F3",
  2473. ocirc: "\u00F4",
  2474. otilde: "\u00F5",
  2475. ouml: "\u00F6",
  2476. divide: "\u00F7",
  2477. oslash: "\u00F8",
  2478. ugrave: "\u00F9",
  2479. uacute: "\u00FA",
  2480. ucirc: "\u00FB",
  2481. uuml: "\u00FC",
  2482. yacute: "\u00FD",
  2483. thorn: "\u00FE",
  2484. yuml: "\u00FF",
  2485. OElig: "\u0152",
  2486. oelig: "\u0153",
  2487. Scaron: "\u0160",
  2488. scaron: "\u0161",
  2489. Yuml: "\u0178",
  2490. fnof: "\u0192",
  2491. circ: "\u02C6",
  2492. tilde: "\u02DC",
  2493. Alpha: "\u0391",
  2494. Beta: "\u0392",
  2495. Gamma: "\u0393",
  2496. Delta: "\u0394",
  2497. Epsilon: "\u0395",
  2498. Zeta: "\u0396",
  2499. Eta: "\u0397",
  2500. Theta: "\u0398",
  2501. Iota: "\u0399",
  2502. Kappa: "\u039A",
  2503. Lambda: "\u039B",
  2504. Mu: "\u039C",
  2505. Nu: "\u039D",
  2506. Xi: "\u039E",
  2507. Omicron: "\u039F",
  2508. Pi: "\u03A0",
  2509. Rho: "\u03A1",
  2510. Sigma: "\u03A3",
  2511. Tau: "\u03A4",
  2512. Upsilon: "\u03A5",
  2513. Phi: "\u03A6",
  2514. Chi: "\u03A7",
  2515. Psi: "\u03A8",
  2516. Omega: "\u03A9",
  2517. alpha: "\u03B1",
  2518. beta: "\u03B2",
  2519. gamma: "\u03B3",
  2520. delta: "\u03B4",
  2521. epsilon: "\u03B5",
  2522. zeta: "\u03B6",
  2523. eta: "\u03B7",
  2524. theta: "\u03B8",
  2525. iota: "\u03B9",
  2526. kappa: "\u03BA",
  2527. lambda: "\u03BB",
  2528. mu: "\u03BC",
  2529. nu: "\u03BD",
  2530. xi: "\u03BE",
  2531. omicron: "\u03BF",
  2532. pi: "\u03C0",
  2533. rho: "\u03C1",
  2534. sigmaf: "\u03C2",
  2535. sigma: "\u03C3",
  2536. tau: "\u03C4",
  2537. upsilon: "\u03C5",
  2538. phi: "\u03C6",
  2539. chi: "\u03C7",
  2540. psi: "\u03C8",
  2541. omega: "\u03C9",
  2542. thetasym: "\u03D1",
  2543. upsih: "\u03D2",
  2544. piv: "\u03D6",
  2545. ensp: "\u2002",
  2546. emsp: "\u2003",
  2547. thinsp: "\u2009",
  2548. zwnj: "\u200C",
  2549. zwj: "\u200D",
  2550. lrm: "\u200E",
  2551. rlm: "\u200F",
  2552. ndash: "\u2013",
  2553. mdash: "\u2014",
  2554. lsquo: "\u2018",
  2555. rsquo: "\u2019",
  2556. sbquo: "\u201A",
  2557. ldquo: "\u201C",
  2558. rdquo: "\u201D",
  2559. bdquo: "\u201E",
  2560. dagger: "\u2020",
  2561. Dagger: "\u2021",
  2562. bull: "\u2022",
  2563. hellip: "\u2026",
  2564. permil: "\u2030",
  2565. prime: "\u2032",
  2566. Prime: "\u2033",
  2567. lsaquo: "\u2039",
  2568. rsaquo: "\u203A",
  2569. oline: "\u203E",
  2570. frasl: "\u2044",
  2571. euro: "\u20AC",
  2572. image: "\u2111",
  2573. weierp: "\u2118",
  2574. real: "\u211C",
  2575. trade: "\u2122",
  2576. alefsym: "\u2135",
  2577. larr: "\u2190",
  2578. uarr: "\u2191",
  2579. rarr: "\u2192",
  2580. darr: "\u2193",
  2581. harr: "\u2194",
  2582. crarr: "\u21B5",
  2583. lArr: "\u21D0",
  2584. uArr: "\u21D1",
  2585. rArr: "\u21D2",
  2586. dArr: "\u21D3",
  2587. hArr: "\u21D4",
  2588. forall: "\u2200",
  2589. part: "\u2202",
  2590. exist: "\u2203",
  2591. empty: "\u2205",
  2592. nabla: "\u2207",
  2593. isin: "\u2208",
  2594. notin: "\u2209",
  2595. ni: "\u220B",
  2596. prod: "\u220F",
  2597. sum: "\u2211",
  2598. minus: "\u2212",
  2599. lowast: "\u2217",
  2600. radic: "\u221A",
  2601. prop: "\u221D",
  2602. infin: "\u221E",
  2603. ang: "\u2220",
  2604. and: "\u2227",
  2605. or: "\u2228",
  2606. cap: "\u2229",
  2607. cup: "\u222A",
  2608. int: "\u222B",
  2609. there4: "\u2234",
  2610. sim: "\u223C",
  2611. cong: "\u2245",
  2612. asymp: "\u2248",
  2613. ne: "\u2260",
  2614. equiv: "\u2261",
  2615. le: "\u2264",
  2616. ge: "\u2265",
  2617. sub: "\u2282",
  2618. sup: "\u2283",
  2619. nsub: "\u2284",
  2620. sube: "\u2286",
  2621. supe: "\u2287",
  2622. oplus: "\u2295",
  2623. otimes: "\u2297",
  2624. perp: "\u22A5",
  2625. sdot: "\u22C5",
  2626. lceil: "\u2308",
  2627. rceil: "\u2309",
  2628. lfloor: "\u230A",
  2629. rfloor: "\u230B",
  2630. lang: "\u2329",
  2631. rang: "\u232A",
  2632. loz: "\u25CA",
  2633. spades: "\u2660",
  2634. clubs: "\u2663",
  2635. hearts: "\u2665",
  2636. diams: "\u2666"
  2637. };
  2638. const HEX_NUMBER = /^[\da-fA-F]+$/;
  2639. const DECIMAL_NUMBER = /^\d+$/;
  2640. types$1.j_oTag = new TokContext("<tag", false);
  2641. types$1.j_cTag = new TokContext("</tag", false);
  2642. types$1.j_expr = new TokContext("<tag>...</tag>", true, true);
  2643. types.jsxName = new TokenType("jsxName");
  2644. types.jsxText = new TokenType("jsxText", {
  2645. beforeExpr: true
  2646. });
  2647. types.jsxTagStart = new TokenType("jsxTagStart", {
  2648. startsExpr: true
  2649. });
  2650. types.jsxTagEnd = new TokenType("jsxTagEnd");
  2651. types.jsxTagStart.updateContext = function () {
  2652. this.state.context.push(types$1.j_expr);
  2653. this.state.context.push(types$1.j_oTag);
  2654. this.state.exprAllowed = false;
  2655. };
  2656. types.jsxTagEnd.updateContext = function (prevType) {
  2657. const out = this.state.context.pop();
  2658. if (out === types$1.j_oTag && prevType === types.slash || out === types$1.j_cTag) {
  2659. this.state.context.pop();
  2660. this.state.exprAllowed = this.curContext() === types$1.j_expr;
  2661. } else {
  2662. this.state.exprAllowed = true;
  2663. }
  2664. };
  2665. function isFragment(object) {
  2666. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  2667. }
  2668. function getQualifiedJSXName(object) {
  2669. if (object.type === "JSXIdentifier") {
  2670. return object.name;
  2671. }
  2672. if (object.type === "JSXNamespacedName") {
  2673. return object.namespace.name + ":" + object.name.name;
  2674. }
  2675. if (object.type === "JSXMemberExpression") {
  2676. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  2677. }
  2678. throw new Error("Node had unexpected type: " + object.type);
  2679. }
  2680. var jsx = (superClass => class extends superClass {
  2681. jsxReadToken() {
  2682. let out = "";
  2683. let chunkStart = this.state.pos;
  2684. for (;;) {
  2685. if (this.state.pos >= this.length) {
  2686. this.raise(this.state.start, "Unterminated JSX contents");
  2687. }
  2688. const ch = this.input.charCodeAt(this.state.pos);
  2689. switch (ch) {
  2690. case 60:
  2691. case 123:
  2692. if (this.state.pos === this.state.start) {
  2693. if (ch === 60 && this.state.exprAllowed) {
  2694. ++this.state.pos;
  2695. return this.finishToken(types.jsxTagStart);
  2696. }
  2697. return super.getTokenFromCode(ch);
  2698. }
  2699. out += this.input.slice(chunkStart, this.state.pos);
  2700. return this.finishToken(types.jsxText, out);
  2701. case 38:
  2702. out += this.input.slice(chunkStart, this.state.pos);
  2703. out += this.jsxReadEntity();
  2704. chunkStart = this.state.pos;
  2705. break;
  2706. default:
  2707. if (isNewLine(ch)) {
  2708. out += this.input.slice(chunkStart, this.state.pos);
  2709. out += this.jsxReadNewLine(true);
  2710. chunkStart = this.state.pos;
  2711. } else {
  2712. ++this.state.pos;
  2713. }
  2714. }
  2715. }
  2716. }
  2717. jsxReadNewLine(normalizeCRLF) {
  2718. const ch = this.input.charCodeAt(this.state.pos);
  2719. let out;
  2720. ++this.state.pos;
  2721. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  2722. ++this.state.pos;
  2723. out = normalizeCRLF ? "\n" : "\r\n";
  2724. } else {
  2725. out = String.fromCharCode(ch);
  2726. }
  2727. ++this.state.curLine;
  2728. this.state.lineStart = this.state.pos;
  2729. return out;
  2730. }
  2731. jsxReadString(quote) {
  2732. let out = "";
  2733. let chunkStart = ++this.state.pos;
  2734. for (;;) {
  2735. if (this.state.pos >= this.length) {
  2736. this.raise(this.state.start, "Unterminated string constant");
  2737. }
  2738. const ch = this.input.charCodeAt(this.state.pos);
  2739. if (ch === quote) break;
  2740. if (ch === 38) {
  2741. out += this.input.slice(chunkStart, this.state.pos);
  2742. out += this.jsxReadEntity();
  2743. chunkStart = this.state.pos;
  2744. } else if (isNewLine(ch)) {
  2745. out += this.input.slice(chunkStart, this.state.pos);
  2746. out += this.jsxReadNewLine(false);
  2747. chunkStart = this.state.pos;
  2748. } else {
  2749. ++this.state.pos;
  2750. }
  2751. }
  2752. out += this.input.slice(chunkStart, this.state.pos++);
  2753. return this.finishToken(types.string, out);
  2754. }
  2755. jsxReadEntity() {
  2756. let str = "";
  2757. let count = 0;
  2758. let entity;
  2759. let ch = this.input[this.state.pos];
  2760. const startPos = ++this.state.pos;
  2761. while (this.state.pos < this.length && count++ < 10) {
  2762. ch = this.input[this.state.pos++];
  2763. if (ch === ";") {
  2764. if (str[0] === "#") {
  2765. if (str[1] === "x") {
  2766. str = str.substr(2);
  2767. if (HEX_NUMBER.test(str)) {
  2768. entity = String.fromCodePoint(parseInt(str, 16));
  2769. }
  2770. } else {
  2771. str = str.substr(1);
  2772. if (DECIMAL_NUMBER.test(str)) {
  2773. entity = String.fromCodePoint(parseInt(str, 10));
  2774. }
  2775. }
  2776. } else {
  2777. entity = entities[str];
  2778. }
  2779. break;
  2780. }
  2781. str += ch;
  2782. }
  2783. if (!entity) {
  2784. this.state.pos = startPos;
  2785. return "&";
  2786. }
  2787. return entity;
  2788. }
  2789. jsxReadWord() {
  2790. let ch;
  2791. const start = this.state.pos;
  2792. do {
  2793. ch = this.input.charCodeAt(++this.state.pos);
  2794. } while (isIdentifierChar(ch) || ch === 45);
  2795. return this.finishToken(types.jsxName, this.input.slice(start, this.state.pos));
  2796. }
  2797. jsxParseIdentifier() {
  2798. const node = this.startNode();
  2799. if (this.match(types.jsxName)) {
  2800. node.name = this.state.value;
  2801. } else if (this.state.type.keyword) {
  2802. node.name = this.state.type.keyword;
  2803. } else {
  2804. this.unexpected();
  2805. }
  2806. this.next();
  2807. return this.finishNode(node, "JSXIdentifier");
  2808. }
  2809. jsxParseNamespacedName() {
  2810. const startPos = this.state.start;
  2811. const startLoc = this.state.startLoc;
  2812. const name = this.jsxParseIdentifier();
  2813. if (!this.eat(types.colon)) return name;
  2814. const node = this.startNodeAt(startPos, startLoc);
  2815. node.namespace = name;
  2816. node.name = this.jsxParseIdentifier();
  2817. return this.finishNode(node, "JSXNamespacedName");
  2818. }
  2819. jsxParseElementName() {
  2820. const startPos = this.state.start;
  2821. const startLoc = this.state.startLoc;
  2822. let node = this.jsxParseNamespacedName();
  2823. while (this.eat(types.dot)) {
  2824. const newNode = this.startNodeAt(startPos, startLoc);
  2825. newNode.object = node;
  2826. newNode.property = this.jsxParseIdentifier();
  2827. node = this.finishNode(newNode, "JSXMemberExpression");
  2828. }
  2829. return node;
  2830. }
  2831. jsxParseAttributeValue() {
  2832. let node;
  2833. switch (this.state.type) {
  2834. case types.braceL:
  2835. node = this.jsxParseExpressionContainer();
  2836. if (node.expression.type === "JSXEmptyExpression") {
  2837. throw this.raise(node.start, "JSX attributes must only be assigned a non-empty expression");
  2838. } else {
  2839. return node;
  2840. }
  2841. case types.jsxTagStart:
  2842. case types.string:
  2843. return this.parseExprAtom();
  2844. default:
  2845. throw this.raise(this.state.start, "JSX value should be either an expression or a quoted JSX text");
  2846. }
  2847. }
  2848. jsxParseEmptyExpression() {
  2849. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  2850. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  2851. }
  2852. jsxParseSpreadChild() {
  2853. const node = this.startNode();
  2854. this.expect(types.braceL);
  2855. this.expect(types.ellipsis);
  2856. node.expression = this.parseExpression();
  2857. this.expect(types.braceR);
  2858. return this.finishNode(node, "JSXSpreadChild");
  2859. }
  2860. jsxParseExpressionContainer() {
  2861. const node = this.startNode();
  2862. this.next();
  2863. if (this.match(types.braceR)) {
  2864. node.expression = this.jsxParseEmptyExpression();
  2865. } else {
  2866. node.expression = this.parseExpression();
  2867. }
  2868. this.expect(types.braceR);
  2869. return this.finishNode(node, "JSXExpressionContainer");
  2870. }
  2871. jsxParseAttribute() {
  2872. const node = this.startNode();
  2873. if (this.eat(types.braceL)) {
  2874. this.expect(types.ellipsis);
  2875. node.argument = this.parseMaybeAssign();
  2876. this.expect(types.braceR);
  2877. return this.finishNode(node, "JSXSpreadAttribute");
  2878. }
  2879. node.name = this.jsxParseNamespacedName();
  2880. node.value = this.eat(types.eq) ? this.jsxParseAttributeValue() : null;
  2881. return this.finishNode(node, "JSXAttribute");
  2882. }
  2883. jsxParseOpeningElementAt(startPos, startLoc) {
  2884. const node = this.startNodeAt(startPos, startLoc);
  2885. if (this.match(types.jsxTagEnd)) {
  2886. this.expect(types.jsxTagEnd);
  2887. return this.finishNode(node, "JSXOpeningFragment");
  2888. }
  2889. node.name = this.jsxParseElementName();
  2890. return this.jsxParseOpeningElementAfterName(node);
  2891. }
  2892. jsxParseOpeningElementAfterName(node) {
  2893. const attributes = [];
  2894. while (!this.match(types.slash) && !this.match(types.jsxTagEnd)) {
  2895. attributes.push(this.jsxParseAttribute());
  2896. }
  2897. node.attributes = attributes;
  2898. node.selfClosing = this.eat(types.slash);
  2899. this.expect(types.jsxTagEnd);
  2900. return this.finishNode(node, "JSXOpeningElement");
  2901. }
  2902. jsxParseClosingElementAt(startPos, startLoc) {
  2903. const node = this.startNodeAt(startPos, startLoc);
  2904. if (this.match(types.jsxTagEnd)) {
  2905. this.expect(types.jsxTagEnd);
  2906. return this.finishNode(node, "JSXClosingFragment");
  2907. }
  2908. node.name = this.jsxParseElementName();
  2909. this.expect(types.jsxTagEnd);
  2910. return this.finishNode(node, "JSXClosingElement");
  2911. }
  2912. jsxParseElementAt(startPos, startLoc) {
  2913. const node = this.startNodeAt(startPos, startLoc);
  2914. const children = [];
  2915. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  2916. let closingElement = null;
  2917. if (!openingElement.selfClosing) {
  2918. contents: for (;;) {
  2919. switch (this.state.type) {
  2920. case types.jsxTagStart:
  2921. startPos = this.state.start;
  2922. startLoc = this.state.startLoc;
  2923. this.next();
  2924. if (this.eat(types.slash)) {
  2925. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  2926. break contents;
  2927. }
  2928. children.push(this.jsxParseElementAt(startPos, startLoc));
  2929. break;
  2930. case types.jsxText:
  2931. children.push(this.parseExprAtom());
  2932. break;
  2933. case types.braceL:
  2934. if (this.lookahead().type === types.ellipsis) {
  2935. children.push(this.jsxParseSpreadChild());
  2936. } else {
  2937. children.push(this.jsxParseExpressionContainer());
  2938. }
  2939. break;
  2940. default:
  2941. throw this.unexpected();
  2942. }
  2943. }
  2944. if (isFragment(openingElement) && !isFragment(closingElement)) {
  2945. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <>");
  2946. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  2947. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  2948. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  2949. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  2950. this.raise(closingElement.start, "Expected corresponding JSX closing tag for <" + getQualifiedJSXName(openingElement.name) + ">");
  2951. }
  2952. }
  2953. }
  2954. if (isFragment(openingElement)) {
  2955. node.openingFragment = openingElement;
  2956. node.closingFragment = closingElement;
  2957. } else {
  2958. node.openingElement = openingElement;
  2959. node.closingElement = closingElement;
  2960. }
  2961. node.children = children;
  2962. if (this.match(types.relational) && this.state.value === "<") {
  2963. this.raise(this.state.start, "Adjacent JSX elements must be wrapped in an enclosing tag. " + "Did you want a JSX fragment <>...</>?");
  2964. }
  2965. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  2966. }
  2967. jsxParseElement() {
  2968. const startPos = this.state.start;
  2969. const startLoc = this.state.startLoc;
  2970. this.next();
  2971. return this.jsxParseElementAt(startPos, startLoc);
  2972. }
  2973. parseExprAtom(refShortHandDefaultPos) {
  2974. if (this.match(types.jsxText)) {
  2975. return this.parseLiteral(this.state.value, "JSXText");
  2976. } else if (this.match(types.jsxTagStart)) {
  2977. return this.jsxParseElement();
  2978. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  2979. this.finishToken(types.jsxTagStart);
  2980. return this.jsxParseElement();
  2981. } else {
  2982. return super.parseExprAtom(refShortHandDefaultPos);
  2983. }
  2984. }
  2985. getTokenFromCode(code) {
  2986. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  2987. const context = this.curContext();
  2988. if (context === types$1.j_expr) {
  2989. return this.jsxReadToken();
  2990. }
  2991. if (context === types$1.j_oTag || context === types$1.j_cTag) {
  2992. if (isIdentifierStart(code)) {
  2993. return this.jsxReadWord();
  2994. }
  2995. if (code === 62) {
  2996. ++this.state.pos;
  2997. return this.finishToken(types.jsxTagEnd);
  2998. }
  2999. if ((code === 34 || code === 39) && context === types$1.j_oTag) {
  3000. return this.jsxReadString(code);
  3001. }
  3002. }
  3003. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  3004. ++this.state.pos;
  3005. return this.finishToken(types.jsxTagStart);
  3006. }
  3007. return super.getTokenFromCode(code);
  3008. }
  3009. updateContext(prevType) {
  3010. if (this.match(types.braceL)) {
  3011. const curContext = this.curContext();
  3012. if (curContext === types$1.j_oTag) {
  3013. this.state.context.push(types$1.braceExpression);
  3014. } else if (curContext === types$1.j_expr) {
  3015. this.state.context.push(types$1.templateQuasi);
  3016. } else {
  3017. super.updateContext(prevType);
  3018. }
  3019. this.state.exprAllowed = true;
  3020. } else if (this.match(types.slash) && prevType === types.jsxTagStart) {
  3021. this.state.context.length -= 2;
  3022. this.state.context.push(types$1.j_cTag);
  3023. this.state.exprAllowed = false;
  3024. } else {
  3025. return super.updateContext(prevType);
  3026. }
  3027. }
  3028. });
  3029. const defaultOptions = {
  3030. sourceType: "script",
  3031. sourceFilename: undefined,
  3032. startLine: 1,
  3033. allowAwaitOutsideFunction: false,
  3034. allowReturnOutsideFunction: false,
  3035. allowImportExportEverywhere: false,
  3036. allowSuperOutsideMethod: false,
  3037. plugins: [],
  3038. strictMode: null,
  3039. ranges: false,
  3040. tokens: false,
  3041. createParenthesizedExpressions: false
  3042. };
  3043. function getOptions(opts) {
  3044. const options = {};
  3045. for (let _i = 0, _Object$keys = Object.keys(defaultOptions); _i < _Object$keys.length; _i++) {
  3046. const key = _Object$keys[_i];
  3047. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  3048. }
  3049. return options;
  3050. }
  3051. class Position {
  3052. constructor(line, col) {
  3053. this.line = line;
  3054. this.column = col;
  3055. }
  3056. }
  3057. class SourceLocation {
  3058. constructor(start, end) {
  3059. this.start = start;
  3060. this.end = end;
  3061. }
  3062. }
  3063. function getLineInfo(input, offset) {
  3064. let line = 1;
  3065. let lineStart = 0;
  3066. let match;
  3067. lineBreakG.lastIndex = 0;
  3068. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  3069. line++;
  3070. lineStart = lineBreakG.lastIndex;
  3071. }
  3072. return new Position(line, offset - lineStart);
  3073. }
  3074. class BaseParser {
  3075. constructor() {
  3076. this.sawUnambiguousESM = false;
  3077. }
  3078. hasPlugin(name) {
  3079. return this.plugins.has(name);
  3080. }
  3081. getPluginOption(plugin, name) {
  3082. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  3083. }
  3084. }
  3085. function last(stack) {
  3086. return stack[stack.length - 1];
  3087. }
  3088. class CommentsParser extends BaseParser {
  3089. addComment(comment) {
  3090. if (this.filename) comment.loc.filename = this.filename;
  3091. this.state.trailingComments.push(comment);
  3092. this.state.leadingComments.push(comment);
  3093. }
  3094. processComment(node) {
  3095. if (node.type === "Program" && node.body.length > 0) return;
  3096. const stack = this.state.commentStack;
  3097. let firstChild, lastChild, trailingComments, i, j;
  3098. if (this.state.trailingComments.length > 0) {
  3099. if (this.state.trailingComments[0].start >= node.end) {
  3100. trailingComments = this.state.trailingComments;
  3101. this.state.trailingComments = [];
  3102. } else {
  3103. this.state.trailingComments.length = 0;
  3104. }
  3105. } else if (stack.length > 0) {
  3106. const lastInStack = last(stack);
  3107. if (lastInStack.trailingComments && lastInStack.trailingComments[0].start >= node.end) {
  3108. trailingComments = lastInStack.trailingComments;
  3109. delete lastInStack.trailingComments;
  3110. }
  3111. }
  3112. if (stack.length > 0 && last(stack).start >= node.start) {
  3113. firstChild = stack.pop();
  3114. }
  3115. while (stack.length > 0 && last(stack).start >= node.start) {
  3116. lastChild = stack.pop();
  3117. }
  3118. if (!lastChild && firstChild) lastChild = firstChild;
  3119. if (firstChild && this.state.leadingComments.length > 0) {
  3120. const lastComment = last(this.state.leadingComments);
  3121. if (firstChild.type === "ObjectProperty") {
  3122. if (lastComment.start >= node.start) {
  3123. if (this.state.commentPreviousNode) {
  3124. for (j = 0; j < this.state.leadingComments.length; j++) {
  3125. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  3126. this.state.leadingComments.splice(j, 1);
  3127. j--;
  3128. }
  3129. }
  3130. if (this.state.leadingComments.length > 0) {
  3131. firstChild.trailingComments = this.state.leadingComments;
  3132. this.state.leadingComments = [];
  3133. }
  3134. }
  3135. }
  3136. } else if (node.type === "CallExpression" && node.arguments && node.arguments.length) {
  3137. const lastArg = last(node.arguments);
  3138. if (lastArg && lastComment.start >= lastArg.start && lastComment.end <= node.end) {
  3139. if (this.state.commentPreviousNode) {
  3140. for (j = 0; j < this.state.leadingComments.length; j++) {
  3141. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  3142. this.state.leadingComments.splice(j, 1);
  3143. j--;
  3144. }
  3145. }
  3146. if (this.state.leadingComments.length > 0) {
  3147. lastArg.trailingComments = this.state.leadingComments;
  3148. this.state.leadingComments = [];
  3149. }
  3150. }
  3151. }
  3152. }
  3153. }
  3154. if (lastChild) {
  3155. if (lastChild.leadingComments) {
  3156. if (lastChild !== node && lastChild.leadingComments.length > 0 && last(lastChild.leadingComments).end <= node.start) {
  3157. node.leadingComments = lastChild.leadingComments;
  3158. delete lastChild.leadingComments;
  3159. } else {
  3160. for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
  3161. if (lastChild.leadingComments[i].end <= node.start) {
  3162. node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
  3163. break;
  3164. }
  3165. }
  3166. }
  3167. }
  3168. } else if (this.state.leadingComments.length > 0) {
  3169. if (last(this.state.leadingComments).end <= node.start) {
  3170. if (this.state.commentPreviousNode) {
  3171. for (j = 0; j < this.state.leadingComments.length; j++) {
  3172. if (this.state.leadingComments[j].end < this.state.commentPreviousNode.end) {
  3173. this.state.leadingComments.splice(j, 1);
  3174. j--;
  3175. }
  3176. }
  3177. }
  3178. if (this.state.leadingComments.length > 0) {
  3179. node.leadingComments = this.state.leadingComments;
  3180. this.state.leadingComments = [];
  3181. }
  3182. } else {
  3183. for (i = 0; i < this.state.leadingComments.length; i++) {
  3184. if (this.state.leadingComments[i].end > node.start) {
  3185. break;
  3186. }
  3187. }
  3188. const leadingComments = this.state.leadingComments.slice(0, i);
  3189. if (leadingComments.length) {
  3190. node.leadingComments = leadingComments;
  3191. }
  3192. trailingComments = this.state.leadingComments.slice(i);
  3193. if (trailingComments.length === 0) {
  3194. trailingComments = null;
  3195. }
  3196. }
  3197. }
  3198. this.state.commentPreviousNode = node;
  3199. if (trailingComments) {
  3200. if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
  3201. node.innerComments = trailingComments;
  3202. } else {
  3203. node.trailingComments = trailingComments;
  3204. }
  3205. }
  3206. stack.push(node);
  3207. }
  3208. }
  3209. class LocationParser extends CommentsParser {
  3210. raise(pos, message, {
  3211. missingPluginNames,
  3212. code
  3213. } = {}) {
  3214. const loc = getLineInfo(this.input, pos);
  3215. message += ` (${loc.line}:${loc.column})`;
  3216. const err = new SyntaxError(message);
  3217. err.pos = pos;
  3218. err.loc = loc;
  3219. if (missingPluginNames) {
  3220. err.missingPlugin = missingPluginNames;
  3221. }
  3222. if (code !== undefined) {
  3223. err.code = code;
  3224. }
  3225. throw err;
  3226. }
  3227. }
  3228. class State {
  3229. constructor() {
  3230. this.potentialArrowAt = -1;
  3231. this.noArrowAt = [];
  3232. this.noArrowParamsConversionAt = [];
  3233. this.commaAfterSpreadAt = -1;
  3234. this.inParameters = false;
  3235. this.maybeInArrowParameters = false;
  3236. this.inPipeline = false;
  3237. this.inType = false;
  3238. this.noAnonFunctionType = false;
  3239. this.inPropertyName = false;
  3240. this.inClassProperty = false;
  3241. this.hasFlowComment = false;
  3242. this.isIterator = false;
  3243. this.topicContext = {
  3244. maxNumOfResolvableTopics: 0,
  3245. maxTopicIndex: null
  3246. };
  3247. this.classLevel = 0;
  3248. this.labels = [];
  3249. this.decoratorStack = [[]];
  3250. this.yieldPos = 0;
  3251. this.awaitPos = 0;
  3252. this.tokens = [];
  3253. this.comments = [];
  3254. this.trailingComments = [];
  3255. this.leadingComments = [];
  3256. this.commentStack = [];
  3257. this.commentPreviousNode = null;
  3258. this.pos = 0;
  3259. this.lineStart = 0;
  3260. this.type = types.eof;
  3261. this.value = null;
  3262. this.start = 0;
  3263. this.end = 0;
  3264. this.lastTokEndLoc = null;
  3265. this.lastTokStartLoc = null;
  3266. this.lastTokStart = 0;
  3267. this.lastTokEnd = 0;
  3268. this.context = [types$1.braceStatement];
  3269. this.exprAllowed = true;
  3270. this.containsEsc = false;
  3271. this.containsOctal = false;
  3272. this.octalPosition = null;
  3273. this.exportedIdentifiers = [];
  3274. this.invalidTemplateEscapePosition = null;
  3275. }
  3276. init(options) {
  3277. this.strict = options.strictMode === false ? false : options.sourceType === "module";
  3278. this.curLine = options.startLine;
  3279. this.startLoc = this.endLoc = this.curPosition();
  3280. }
  3281. curPosition() {
  3282. return new Position(this.curLine, this.pos - this.lineStart);
  3283. }
  3284. clone(skipArrays) {
  3285. const state = new State();
  3286. const keys = Object.keys(this);
  3287. for (let i = 0, length = keys.length; i < length; i++) {
  3288. const key = keys[i];
  3289. let val = this[key];
  3290. if ((!skipArrays || key === "context") && Array.isArray(val)) {
  3291. val = val.slice();
  3292. }
  3293. state[key] = val;
  3294. }
  3295. return state;
  3296. }
  3297. }
  3298. var _isDigit = function isDigit(code) {
  3299. return code >= 48 && code <= 57;
  3300. };
  3301. const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
  3302. const forbiddenNumericSeparatorSiblings = {
  3303. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  3304. hex: [46, 88, 95, 120]
  3305. };
  3306. const allowedNumericSeparatorSiblings = {};
  3307. allowedNumericSeparatorSiblings.bin = [48, 49];
  3308. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  3309. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  3310. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  3311. class Token {
  3312. constructor(state) {
  3313. this.type = state.type;
  3314. this.value = state.value;
  3315. this.start = state.start;
  3316. this.end = state.end;
  3317. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  3318. }
  3319. }
  3320. class Tokenizer extends LocationParser {
  3321. constructor(options, input) {
  3322. super();
  3323. this.state = new State();
  3324. this.state.init(options);
  3325. this.input = input;
  3326. this.length = input.length;
  3327. this.isLookahead = false;
  3328. }
  3329. next() {
  3330. if (this.options.tokens && !this.isLookahead) {
  3331. this.state.tokens.push(new Token(this.state));
  3332. }
  3333. this.state.lastTokEnd = this.state.end;
  3334. this.state.lastTokStart = this.state.start;
  3335. this.state.lastTokEndLoc = this.state.endLoc;
  3336. this.state.lastTokStartLoc = this.state.startLoc;
  3337. this.nextToken();
  3338. }
  3339. eat(type) {
  3340. if (this.match(type)) {
  3341. this.next();
  3342. return true;
  3343. } else {
  3344. return false;
  3345. }
  3346. }
  3347. match(type) {
  3348. return this.state.type === type;
  3349. }
  3350. lookahead() {
  3351. const old = this.state;
  3352. this.state = old.clone(true);
  3353. this.isLookahead = true;
  3354. this.next();
  3355. this.isLookahead = false;
  3356. const curr = this.state;
  3357. this.state = old;
  3358. return curr;
  3359. }
  3360. setStrict(strict) {
  3361. this.state.strict = strict;
  3362. if (!this.match(types.num) && !this.match(types.string)) return;
  3363. this.state.pos = this.state.start;
  3364. while (this.state.pos < this.state.lineStart) {
  3365. this.state.lineStart = this.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
  3366. --this.state.curLine;
  3367. }
  3368. this.nextToken();
  3369. }
  3370. curContext() {
  3371. return this.state.context[this.state.context.length - 1];
  3372. }
  3373. nextToken() {
  3374. const curContext = this.curContext();
  3375. if (!curContext || !curContext.preserveSpace) this.skipSpace();
  3376. this.state.containsOctal = false;
  3377. this.state.octalPosition = null;
  3378. this.state.start = this.state.pos;
  3379. this.state.startLoc = this.state.curPosition();
  3380. if (this.state.pos >= this.length) {
  3381. this.finishToken(types.eof);
  3382. return;
  3383. }
  3384. if (curContext.override) {
  3385. curContext.override(this);
  3386. } else {
  3387. this.getTokenFromCode(this.input.codePointAt(this.state.pos));
  3388. }
  3389. }
  3390. pushComment(block, text, start, end, startLoc, endLoc) {
  3391. const comment = {
  3392. type: block ? "CommentBlock" : "CommentLine",
  3393. value: text,
  3394. start: start,
  3395. end: end,
  3396. loc: new SourceLocation(startLoc, endLoc)
  3397. };
  3398. if (!this.isLookahead) {
  3399. if (this.options.tokens) this.state.tokens.push(comment);
  3400. this.state.comments.push(comment);
  3401. this.addComment(comment);
  3402. }
  3403. }
  3404. skipBlockComment() {
  3405. const startLoc = this.state.curPosition();
  3406. const start = this.state.pos;
  3407. const end = this.input.indexOf("*/", this.state.pos += 2);
  3408. if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
  3409. this.state.pos = end + 2;
  3410. lineBreakG.lastIndex = start;
  3411. let match;
  3412. while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
  3413. ++this.state.curLine;
  3414. this.state.lineStart = match.index + match[0].length;
  3415. }
  3416. this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
  3417. }
  3418. skipLineComment(startSkip) {
  3419. const start = this.state.pos;
  3420. const startLoc = this.state.curPosition();
  3421. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  3422. if (this.state.pos < this.length) {
  3423. while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.length) {
  3424. ch = this.input.charCodeAt(this.state.pos);
  3425. }
  3426. }
  3427. this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
  3428. }
  3429. skipSpace() {
  3430. loop: while (this.state.pos < this.length) {
  3431. const ch = this.input.charCodeAt(this.state.pos);
  3432. switch (ch) {
  3433. case 32:
  3434. case 160:
  3435. case 9:
  3436. ++this.state.pos;
  3437. break;
  3438. case 13:
  3439. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  3440. ++this.state.pos;
  3441. }
  3442. case 10:
  3443. case 8232:
  3444. case 8233:
  3445. ++this.state.pos;
  3446. ++this.state.curLine;
  3447. this.state.lineStart = this.state.pos;
  3448. break;
  3449. case 47:
  3450. switch (this.input.charCodeAt(this.state.pos + 1)) {
  3451. case 42:
  3452. this.skipBlockComment();
  3453. break;
  3454. case 47:
  3455. this.skipLineComment(2);
  3456. break;
  3457. default:
  3458. break loop;
  3459. }
  3460. break;
  3461. default:
  3462. if (isWhitespace(ch)) {
  3463. ++this.state.pos;
  3464. } else {
  3465. break loop;
  3466. }
  3467. }
  3468. }
  3469. }
  3470. finishToken(type, val) {
  3471. this.state.end = this.state.pos;
  3472. this.state.endLoc = this.state.curPosition();
  3473. const prevType = this.state.type;
  3474. this.state.type = type;
  3475. this.state.value = val;
  3476. this.updateContext(prevType);
  3477. }
  3478. readToken_numberSign() {
  3479. if (this.state.pos === 0 && this.readToken_interpreter()) {
  3480. return;
  3481. }
  3482. const nextPos = this.state.pos + 1;
  3483. const next = this.input.charCodeAt(nextPos);
  3484. if (next >= 48 && next <= 57) {
  3485. this.raise(this.state.pos, "Unexpected digit after hash token");
  3486. }
  3487. if ((this.hasPlugin("classPrivateProperties") || this.hasPlugin("classPrivateMethods")) && this.state.classLevel > 0) {
  3488. ++this.state.pos;
  3489. this.finishToken(types.hash);
  3490. return;
  3491. } else if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  3492. this.finishOp(types.hash, 1);
  3493. } else {
  3494. this.raise(this.state.pos, "Unexpected character '#'");
  3495. }
  3496. }
  3497. readToken_dot() {
  3498. const next = this.input.charCodeAt(this.state.pos + 1);
  3499. if (next >= 48 && next <= 57) {
  3500. this.readNumber(true);
  3501. return;
  3502. }
  3503. const next2 = this.input.charCodeAt(this.state.pos + 2);
  3504. if (next === 46 && next2 === 46) {
  3505. this.state.pos += 3;
  3506. this.finishToken(types.ellipsis);
  3507. } else {
  3508. ++this.state.pos;
  3509. this.finishToken(types.dot);
  3510. }
  3511. }
  3512. readToken_slash() {
  3513. if (this.state.exprAllowed && !this.state.inType) {
  3514. ++this.state.pos;
  3515. this.readRegexp();
  3516. return;
  3517. }
  3518. const next = this.input.charCodeAt(this.state.pos + 1);
  3519. if (next === 61) {
  3520. this.finishOp(types.assign, 2);
  3521. } else {
  3522. this.finishOp(types.slash, 1);
  3523. }
  3524. }
  3525. readToken_interpreter() {
  3526. if (this.state.pos !== 0 || this.length < 2) return false;
  3527. const start = this.state.pos;
  3528. this.state.pos += 1;
  3529. let ch = this.input.charCodeAt(this.state.pos);
  3530. if (ch !== 33) return false;
  3531. while (ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233 && ++this.state.pos < this.length) {
  3532. ch = this.input.charCodeAt(this.state.pos);
  3533. }
  3534. const value = this.input.slice(start + 2, this.state.pos);
  3535. this.finishToken(types.interpreterDirective, value);
  3536. return true;
  3537. }
  3538. readToken_mult_modulo(code) {
  3539. let type = code === 42 ? types.star : types.modulo;
  3540. let width = 1;
  3541. let next = this.input.charCodeAt(this.state.pos + 1);
  3542. const exprAllowed = this.state.exprAllowed;
  3543. if (code === 42 && next === 42) {
  3544. width++;
  3545. next = this.input.charCodeAt(this.state.pos + 2);
  3546. type = types.exponent;
  3547. }
  3548. if (next === 61 && !exprAllowed) {
  3549. width++;
  3550. type = types.assign;
  3551. }
  3552. this.finishOp(type, width);
  3553. }
  3554. readToken_pipe_amp(code) {
  3555. const next = this.input.charCodeAt(this.state.pos + 1);
  3556. if (next === code) {
  3557. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  3558. this.finishOp(types.assign, 3);
  3559. } else {
  3560. this.finishOp(code === 124 ? types.logicalOR : types.logicalAND, 2);
  3561. }
  3562. return;
  3563. }
  3564. if (code === 124) {
  3565. if (next === 62) {
  3566. this.finishOp(types.pipeline, 2);
  3567. return;
  3568. }
  3569. }
  3570. if (next === 61) {
  3571. this.finishOp(types.assign, 2);
  3572. return;
  3573. }
  3574. this.finishOp(code === 124 ? types.bitwiseOR : types.bitwiseAND, 1);
  3575. }
  3576. readToken_caret() {
  3577. const next = this.input.charCodeAt(this.state.pos + 1);
  3578. if (next === 61) {
  3579. this.finishOp(types.assign, 2);
  3580. } else {
  3581. this.finishOp(types.bitwiseXOR, 1);
  3582. }
  3583. }
  3584. readToken_plus_min(code) {
  3585. const next = this.input.charCodeAt(this.state.pos + 1);
  3586. if (next === code) {
  3587. if (next === 45 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 62 && lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.pos))) {
  3588. this.skipLineComment(3);
  3589. this.skipSpace();
  3590. this.nextToken();
  3591. return;
  3592. }
  3593. this.finishOp(types.incDec, 2);
  3594. return;
  3595. }
  3596. if (next === 61) {
  3597. this.finishOp(types.assign, 2);
  3598. } else {
  3599. this.finishOp(types.plusMin, 1);
  3600. }
  3601. }
  3602. readToken_lt_gt(code) {
  3603. const next = this.input.charCodeAt(this.state.pos + 1);
  3604. let size = 1;
  3605. if (next === code) {
  3606. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  3607. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  3608. this.finishOp(types.assign, size + 1);
  3609. return;
  3610. }
  3611. this.finishOp(types.bitShift, size);
  3612. return;
  3613. }
  3614. if (next === 33 && code === 60 && !this.inModule && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
  3615. this.skipLineComment(4);
  3616. this.skipSpace();
  3617. this.nextToken();
  3618. return;
  3619. }
  3620. if (next === 61) {
  3621. size = 2;
  3622. }
  3623. this.finishOp(types.relational, size);
  3624. }
  3625. readToken_eq_excl(code) {
  3626. const next = this.input.charCodeAt(this.state.pos + 1);
  3627. if (next === 61) {
  3628. this.finishOp(types.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  3629. return;
  3630. }
  3631. if (code === 61 && next === 62) {
  3632. this.state.pos += 2;
  3633. this.finishToken(types.arrow);
  3634. return;
  3635. }
  3636. this.finishOp(code === 61 ? types.eq : types.bang, 1);
  3637. }
  3638. readToken_question() {
  3639. const next = this.input.charCodeAt(this.state.pos + 1);
  3640. const next2 = this.input.charCodeAt(this.state.pos + 2);
  3641. if (next === 63 && !this.state.inType) {
  3642. if (next2 === 61) {
  3643. this.finishOp(types.assign, 3);
  3644. } else {
  3645. this.finishOp(types.nullishCoalescing, 2);
  3646. }
  3647. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  3648. this.state.pos += 2;
  3649. this.finishToken(types.questionDot);
  3650. } else {
  3651. ++this.state.pos;
  3652. this.finishToken(types.question);
  3653. }
  3654. }
  3655. getTokenFromCode(code) {
  3656. switch (code) {
  3657. case 46:
  3658. this.readToken_dot();
  3659. return;
  3660. case 40:
  3661. ++this.state.pos;
  3662. this.finishToken(types.parenL);
  3663. return;
  3664. case 41:
  3665. ++this.state.pos;
  3666. this.finishToken(types.parenR);
  3667. return;
  3668. case 59:
  3669. ++this.state.pos;
  3670. this.finishToken(types.semi);
  3671. return;
  3672. case 44:
  3673. ++this.state.pos;
  3674. this.finishToken(types.comma);
  3675. return;
  3676. case 91:
  3677. ++this.state.pos;
  3678. this.finishToken(types.bracketL);
  3679. return;
  3680. case 93:
  3681. ++this.state.pos;
  3682. this.finishToken(types.bracketR);
  3683. return;
  3684. case 123:
  3685. ++this.state.pos;
  3686. this.finishToken(types.braceL);
  3687. return;
  3688. case 125:
  3689. ++this.state.pos;
  3690. this.finishToken(types.braceR);
  3691. return;
  3692. case 58:
  3693. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  3694. this.finishOp(types.doubleColon, 2);
  3695. } else {
  3696. ++this.state.pos;
  3697. this.finishToken(types.colon);
  3698. }
  3699. return;
  3700. case 63:
  3701. this.readToken_question();
  3702. return;
  3703. case 96:
  3704. ++this.state.pos;
  3705. this.finishToken(types.backQuote);
  3706. return;
  3707. case 48:
  3708. {
  3709. const next = this.input.charCodeAt(this.state.pos + 1);
  3710. if (next === 120 || next === 88) {
  3711. this.readRadixNumber(16);
  3712. return;
  3713. }
  3714. if (next === 111 || next === 79) {
  3715. this.readRadixNumber(8);
  3716. return;
  3717. }
  3718. if (next === 98 || next === 66) {
  3719. this.readRadixNumber(2);
  3720. return;
  3721. }
  3722. }
  3723. case 49:
  3724. case 50:
  3725. case 51:
  3726. case 52:
  3727. case 53:
  3728. case 54:
  3729. case 55:
  3730. case 56:
  3731. case 57:
  3732. this.readNumber(false);
  3733. return;
  3734. case 34:
  3735. case 39:
  3736. this.readString(code);
  3737. return;
  3738. case 47:
  3739. this.readToken_slash();
  3740. return;
  3741. case 37:
  3742. case 42:
  3743. this.readToken_mult_modulo(code);
  3744. return;
  3745. case 124:
  3746. case 38:
  3747. this.readToken_pipe_amp(code);
  3748. return;
  3749. case 94:
  3750. this.readToken_caret();
  3751. return;
  3752. case 43:
  3753. case 45:
  3754. this.readToken_plus_min(code);
  3755. return;
  3756. case 60:
  3757. case 62:
  3758. this.readToken_lt_gt(code);
  3759. return;
  3760. case 61:
  3761. case 33:
  3762. this.readToken_eq_excl(code);
  3763. return;
  3764. case 126:
  3765. this.finishOp(types.tilde, 1);
  3766. return;
  3767. case 64:
  3768. ++this.state.pos;
  3769. this.finishToken(types.at);
  3770. return;
  3771. case 35:
  3772. this.readToken_numberSign();
  3773. return;
  3774. case 92:
  3775. this.readWord();
  3776. return;
  3777. default:
  3778. if (isIdentifierStart(code)) {
  3779. this.readWord();
  3780. return;
  3781. }
  3782. }
  3783. this.raise(this.state.pos, `Unexpected character '${String.fromCodePoint(code)}'`);
  3784. }
  3785. finishOp(type, size) {
  3786. const str = this.input.slice(this.state.pos, this.state.pos + size);
  3787. this.state.pos += size;
  3788. this.finishToken(type, str);
  3789. }
  3790. readRegexp() {
  3791. const start = this.state.pos;
  3792. let escaped, inClass;
  3793. for (;;) {
  3794. if (this.state.pos >= this.length) {
  3795. this.raise(start, "Unterminated regular expression");
  3796. }
  3797. const ch = this.input.charAt(this.state.pos);
  3798. if (lineBreak.test(ch)) {
  3799. this.raise(start, "Unterminated regular expression");
  3800. }
  3801. if (escaped) {
  3802. escaped = false;
  3803. } else {
  3804. if (ch === "[") {
  3805. inClass = true;
  3806. } else if (ch === "]" && inClass) {
  3807. inClass = false;
  3808. } else if (ch === "/" && !inClass) {
  3809. break;
  3810. }
  3811. escaped = ch === "\\";
  3812. }
  3813. ++this.state.pos;
  3814. }
  3815. const content = this.input.slice(start, this.state.pos);
  3816. ++this.state.pos;
  3817. let mods = "";
  3818. while (this.state.pos < this.length) {
  3819. const char = this.input[this.state.pos];
  3820. const charCode = this.input.codePointAt(this.state.pos);
  3821. if (VALID_REGEX_FLAGS.has(char)) {
  3822. if (mods.indexOf(char) > -1) {
  3823. this.raise(this.state.pos + 1, "Duplicate regular expression flag");
  3824. }
  3825. ++this.state.pos;
  3826. mods += char;
  3827. } else if (isIdentifierChar(charCode) || charCode === 92) {
  3828. this.raise(this.state.pos + 1, "Invalid regular expression flag");
  3829. } else {
  3830. break;
  3831. }
  3832. }
  3833. this.finishToken(types.regexp, {
  3834. pattern: content,
  3835. flags: mods
  3836. });
  3837. }
  3838. readInt(radix, len) {
  3839. const start = this.state.pos;
  3840. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  3841. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  3842. let total = 0;
  3843. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  3844. const code = this.input.charCodeAt(this.state.pos);
  3845. let val;
  3846. if (this.hasPlugin("numericSeparator")) {
  3847. const prev = this.input.charCodeAt(this.state.pos - 1);
  3848. const next = this.input.charCodeAt(this.state.pos + 1);
  3849. if (code === 95) {
  3850. if (allowedSiblings.indexOf(next) === -1) {
  3851. this.raise(this.state.pos, "Invalid or unexpected token");
  3852. }
  3853. if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  3854. this.raise(this.state.pos, "Invalid or unexpected token");
  3855. }
  3856. ++this.state.pos;
  3857. continue;
  3858. }
  3859. }
  3860. if (code >= 97) {
  3861. val = code - 97 + 10;
  3862. } else if (code >= 65) {
  3863. val = code - 65 + 10;
  3864. } else if (_isDigit(code)) {
  3865. val = code - 48;
  3866. } else {
  3867. val = Infinity;
  3868. }
  3869. if (val >= radix) break;
  3870. ++this.state.pos;
  3871. total = total * radix + val;
  3872. }
  3873. if (this.state.pos === start || len != null && this.state.pos - start !== len) {
  3874. return null;
  3875. }
  3876. return total;
  3877. }
  3878. readRadixNumber(radix) {
  3879. const start = this.state.pos;
  3880. let isBigInt = false;
  3881. this.state.pos += 2;
  3882. const val = this.readInt(radix);
  3883. if (val == null) {
  3884. this.raise(this.state.start + 2, "Expected number in radix " + radix);
  3885. }
  3886. if (this.hasPlugin("bigInt")) {
  3887. if (this.input.charCodeAt(this.state.pos) === 110) {
  3888. ++this.state.pos;
  3889. isBigInt = true;
  3890. }
  3891. }
  3892. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  3893. this.raise(this.state.pos, "Identifier directly after number");
  3894. }
  3895. if (isBigInt) {
  3896. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  3897. this.finishToken(types.bigint, str);
  3898. return;
  3899. }
  3900. this.finishToken(types.num, val);
  3901. }
  3902. readNumber(startsWithDot) {
  3903. const start = this.state.pos;
  3904. let isFloat = false;
  3905. let isBigInt = false;
  3906. if (!startsWithDot && this.readInt(10) === null) {
  3907. this.raise(start, "Invalid number");
  3908. }
  3909. let octal = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3910. if (octal) {
  3911. if (this.state.strict) {
  3912. this.raise(start, "Legacy octal literals are not allowed in strict mode");
  3913. }
  3914. if (/[89]/.test(this.input.slice(start, this.state.pos))) {
  3915. octal = false;
  3916. }
  3917. }
  3918. let next = this.input.charCodeAt(this.state.pos);
  3919. if (next === 46 && !octal) {
  3920. ++this.state.pos;
  3921. this.readInt(10);
  3922. isFloat = true;
  3923. next = this.input.charCodeAt(this.state.pos);
  3924. }
  3925. if ((next === 69 || next === 101) && !octal) {
  3926. next = this.input.charCodeAt(++this.state.pos);
  3927. if (next === 43 || next === 45) {
  3928. ++this.state.pos;
  3929. }
  3930. if (this.readInt(10) === null) this.raise(start, "Invalid number");
  3931. isFloat = true;
  3932. next = this.input.charCodeAt(this.state.pos);
  3933. }
  3934. if (this.hasPlugin("bigInt")) {
  3935. if (next === 110) {
  3936. if (isFloat || octal) this.raise(start, "Invalid BigIntLiteral");
  3937. ++this.state.pos;
  3938. isBigInt = true;
  3939. }
  3940. }
  3941. if (isIdentifierStart(this.input.codePointAt(this.state.pos))) {
  3942. this.raise(this.state.pos, "Identifier directly after number");
  3943. }
  3944. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  3945. if (isBigInt) {
  3946. this.finishToken(types.bigint, str);
  3947. return;
  3948. }
  3949. const val = octal ? parseInt(str, 8) : parseFloat(str);
  3950. this.finishToken(types.num, val);
  3951. }
  3952. readCodePoint(throwOnInvalid) {
  3953. const ch = this.input.charCodeAt(this.state.pos);
  3954. let code;
  3955. if (ch === 123) {
  3956. const codePos = ++this.state.pos;
  3957. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, throwOnInvalid);
  3958. ++this.state.pos;
  3959. if (code === null) {
  3960. --this.state.invalidTemplateEscapePosition;
  3961. } else if (code > 0x10ffff) {
  3962. if (throwOnInvalid) {
  3963. this.raise(codePos, "Code point out of bounds");
  3964. } else {
  3965. this.state.invalidTemplateEscapePosition = codePos - 2;
  3966. return null;
  3967. }
  3968. }
  3969. } else {
  3970. code = this.readHexChar(4, throwOnInvalid);
  3971. }
  3972. return code;
  3973. }
  3974. readString(quote) {
  3975. let out = "",
  3976. chunkStart = ++this.state.pos;
  3977. for (;;) {
  3978. if (this.state.pos >= this.length) {
  3979. this.raise(this.state.start, "Unterminated string constant");
  3980. }
  3981. const ch = this.input.charCodeAt(this.state.pos);
  3982. if (ch === quote) break;
  3983. if (ch === 92) {
  3984. out += this.input.slice(chunkStart, this.state.pos);
  3985. out += this.readEscapedChar(false);
  3986. chunkStart = this.state.pos;
  3987. } else if (ch === 8232 || ch === 8233) {
  3988. ++this.state.pos;
  3989. ++this.state.curLine;
  3990. } else if (isNewLine(ch)) {
  3991. this.raise(this.state.start, "Unterminated string constant");
  3992. } else {
  3993. ++this.state.pos;
  3994. }
  3995. }
  3996. out += this.input.slice(chunkStart, this.state.pos++);
  3997. this.finishToken(types.string, out);
  3998. }
  3999. readTmplToken() {
  4000. let out = "",
  4001. chunkStart = this.state.pos,
  4002. containsInvalid = false;
  4003. for (;;) {
  4004. if (this.state.pos >= this.length) {
  4005. this.raise(this.state.start, "Unterminated template");
  4006. }
  4007. const ch = this.input.charCodeAt(this.state.pos);
  4008. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  4009. if (this.state.pos === this.state.start && this.match(types.template)) {
  4010. if (ch === 36) {
  4011. this.state.pos += 2;
  4012. this.finishToken(types.dollarBraceL);
  4013. return;
  4014. } else {
  4015. ++this.state.pos;
  4016. this.finishToken(types.backQuote);
  4017. return;
  4018. }
  4019. }
  4020. out += this.input.slice(chunkStart, this.state.pos);
  4021. this.finishToken(types.template, containsInvalid ? null : out);
  4022. return;
  4023. }
  4024. if (ch === 92) {
  4025. out += this.input.slice(chunkStart, this.state.pos);
  4026. const escaped = this.readEscapedChar(true);
  4027. if (escaped === null) {
  4028. containsInvalid = true;
  4029. } else {
  4030. out += escaped;
  4031. }
  4032. chunkStart = this.state.pos;
  4033. } else if (isNewLine(ch)) {
  4034. out += this.input.slice(chunkStart, this.state.pos);
  4035. ++this.state.pos;
  4036. switch (ch) {
  4037. case 13:
  4038. if (this.input.charCodeAt(this.state.pos) === 10) {
  4039. ++this.state.pos;
  4040. }
  4041. case 10:
  4042. out += "\n";
  4043. break;
  4044. default:
  4045. out += String.fromCharCode(ch);
  4046. break;
  4047. }
  4048. ++this.state.curLine;
  4049. this.state.lineStart = this.state.pos;
  4050. chunkStart = this.state.pos;
  4051. } else {
  4052. ++this.state.pos;
  4053. }
  4054. }
  4055. }
  4056. readEscapedChar(inTemplate) {
  4057. const throwOnInvalid = !inTemplate;
  4058. const ch = this.input.charCodeAt(++this.state.pos);
  4059. ++this.state.pos;
  4060. switch (ch) {
  4061. case 110:
  4062. return "\n";
  4063. case 114:
  4064. return "\r";
  4065. case 120:
  4066. {
  4067. const code = this.readHexChar(2, throwOnInvalid);
  4068. return code === null ? null : String.fromCharCode(code);
  4069. }
  4070. case 117:
  4071. {
  4072. const code = this.readCodePoint(throwOnInvalid);
  4073. return code === null ? null : String.fromCodePoint(code);
  4074. }
  4075. case 116:
  4076. return "\t";
  4077. case 98:
  4078. return "\b";
  4079. case 118:
  4080. return "\u000b";
  4081. case 102:
  4082. return "\f";
  4083. case 13:
  4084. if (this.input.charCodeAt(this.state.pos) === 10) {
  4085. ++this.state.pos;
  4086. }
  4087. case 10:
  4088. this.state.lineStart = this.state.pos;
  4089. ++this.state.curLine;
  4090. case 8232:
  4091. case 8233:
  4092. return "";
  4093. default:
  4094. if (ch >= 48 && ch <= 55) {
  4095. const codePos = this.state.pos - 1;
  4096. let octalStr = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/)[0];
  4097. let octal = parseInt(octalStr, 8);
  4098. if (octal > 255) {
  4099. octalStr = octalStr.slice(0, -1);
  4100. octal = parseInt(octalStr, 8);
  4101. }
  4102. if (octal > 0) {
  4103. if (inTemplate) {
  4104. this.state.invalidTemplateEscapePosition = codePos;
  4105. return null;
  4106. } else if (this.state.strict) {
  4107. this.raise(codePos, "Octal literal in strict mode");
  4108. } else if (!this.state.containsOctal) {
  4109. this.state.containsOctal = true;
  4110. this.state.octalPosition = codePos;
  4111. }
  4112. }
  4113. this.state.pos += octalStr.length - 1;
  4114. return String.fromCharCode(octal);
  4115. }
  4116. return String.fromCharCode(ch);
  4117. }
  4118. }
  4119. readHexChar(len, throwOnInvalid) {
  4120. const codePos = this.state.pos;
  4121. const n = this.readInt(16, len);
  4122. if (n === null) {
  4123. if (throwOnInvalid) {
  4124. this.raise(codePos, "Bad character escape sequence");
  4125. } else {
  4126. this.state.pos = codePos - 1;
  4127. this.state.invalidTemplateEscapePosition = codePos - 1;
  4128. }
  4129. }
  4130. return n;
  4131. }
  4132. readWord1() {
  4133. let word = "";
  4134. this.state.containsEsc = false;
  4135. const start = this.state.pos;
  4136. let chunkStart = this.state.pos;
  4137. while (this.state.pos < this.length) {
  4138. const ch = this.input.codePointAt(this.state.pos);
  4139. if (isIdentifierChar(ch)) {
  4140. this.state.pos += ch <= 0xffff ? 1 : 2;
  4141. } else if (this.state.isIterator && ch === 64) {
  4142. ++this.state.pos;
  4143. } else if (ch === 92) {
  4144. this.state.containsEsc = true;
  4145. word += this.input.slice(chunkStart, this.state.pos);
  4146. const escStart = this.state.pos;
  4147. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  4148. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  4149. this.raise(this.state.pos, "Expecting Unicode escape sequence \\uXXXX");
  4150. }
  4151. ++this.state.pos;
  4152. const esc = this.readCodePoint(true);
  4153. if (!identifierCheck(esc, true)) {
  4154. this.raise(escStart, "Invalid Unicode escape");
  4155. }
  4156. word += String.fromCodePoint(esc);
  4157. chunkStart = this.state.pos;
  4158. } else {
  4159. break;
  4160. }
  4161. }
  4162. return word + this.input.slice(chunkStart, this.state.pos);
  4163. }
  4164. isIterator(word) {
  4165. return word === "@@iterator" || word === "@@asyncIterator";
  4166. }
  4167. readWord() {
  4168. const word = this.readWord1();
  4169. const type = keywords.get(word) || types.name;
  4170. if (type.keyword && this.state.containsEsc) {
  4171. this.raise(this.state.pos, `Escape sequence in keyword ${word}`);
  4172. }
  4173. if (this.state.isIterator && (!this.isIterator(word) || !this.state.inType)) {
  4174. this.raise(this.state.pos, `Invalid identifier ${word}`);
  4175. }
  4176. this.finishToken(type, word);
  4177. }
  4178. braceIsBlock(prevType) {
  4179. const parent = this.curContext();
  4180. if (parent === types$1.functionExpression || parent === types$1.functionStatement) {
  4181. return true;
  4182. }
  4183. if (prevType === types.colon && (parent === types$1.braceStatement || parent === types$1.braceExpression)) {
  4184. return !parent.isExpr;
  4185. }
  4186. if (prevType === types._return || prevType === types.name && this.state.exprAllowed) {
  4187. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  4188. }
  4189. if (prevType === types._else || prevType === types.semi || prevType === types.eof || prevType === types.parenR || prevType === types.arrow) {
  4190. return true;
  4191. }
  4192. if (prevType === types.braceL) {
  4193. return parent === types$1.braceStatement;
  4194. }
  4195. if (prevType === types._var || prevType === types._const || prevType === types.name) {
  4196. return false;
  4197. }
  4198. if (prevType === types.relational) {
  4199. return true;
  4200. }
  4201. return !this.state.exprAllowed;
  4202. }
  4203. updateContext(prevType) {
  4204. const type = this.state.type;
  4205. let update;
  4206. if (type.keyword && (prevType === types.dot || prevType === types.questionDot)) {
  4207. this.state.exprAllowed = false;
  4208. } else if (update = type.updateContext) {
  4209. update.call(this, prevType);
  4210. } else {
  4211. this.state.exprAllowed = type.beforeExpr;
  4212. }
  4213. }
  4214. }
  4215. const literal = /^('|")((?:\\?.)*?)\1/;
  4216. class UtilParser extends Tokenizer {
  4217. addExtra(node, key, val) {
  4218. if (!node) return;
  4219. const extra = node.extra = node.extra || {};
  4220. extra[key] = val;
  4221. }
  4222. isRelational(op) {
  4223. return this.match(types.relational) && this.state.value === op;
  4224. }
  4225. isLookaheadRelational(op) {
  4226. const l = this.lookahead();
  4227. return l.type === types.relational && l.value === op;
  4228. }
  4229. expectRelational(op) {
  4230. if (this.isRelational(op)) {
  4231. this.next();
  4232. } else {
  4233. this.unexpected(null, types.relational);
  4234. }
  4235. }
  4236. eatRelational(op) {
  4237. if (this.isRelational(op)) {
  4238. this.next();
  4239. return true;
  4240. }
  4241. return false;
  4242. }
  4243. isContextual(name) {
  4244. return this.match(types.name) && this.state.value === name && !this.state.containsEsc;
  4245. }
  4246. isLookaheadContextual(name) {
  4247. const l = this.lookahead();
  4248. return l.type === types.name && l.value === name;
  4249. }
  4250. eatContextual(name) {
  4251. return this.isContextual(name) && this.eat(types.name);
  4252. }
  4253. expectContextual(name, message) {
  4254. if (!this.eatContextual(name)) this.unexpected(null, message);
  4255. }
  4256. canInsertSemicolon() {
  4257. return this.match(types.eof) || this.match(types.braceR) || this.hasPrecedingLineBreak();
  4258. }
  4259. hasPrecedingLineBreak() {
  4260. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  4261. }
  4262. isLineTerminator() {
  4263. return this.eat(types.semi) || this.canInsertSemicolon();
  4264. }
  4265. semicolon() {
  4266. if (!this.isLineTerminator()) this.unexpected(null, types.semi);
  4267. }
  4268. expect(type, pos) {
  4269. this.eat(type) || this.unexpected(pos, type);
  4270. }
  4271. assertNoSpace(message = "Unexpected space.") {
  4272. if (this.state.start > this.state.lastTokEnd) {
  4273. this.raise(this.state.lastTokEnd, message);
  4274. }
  4275. }
  4276. unexpected(pos, messageOrType = "Unexpected token") {
  4277. if (typeof messageOrType !== "string") {
  4278. messageOrType = `Unexpected token, expected "${messageOrType.label}"`;
  4279. }
  4280. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  4281. }
  4282. expectPlugin(name, pos) {
  4283. if (!this.hasPlugin(name)) {
  4284. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling the parser plugin: '${name}'`, {
  4285. missingPluginNames: [name]
  4286. });
  4287. }
  4288. return true;
  4289. }
  4290. expectOnePlugin(names, pos) {
  4291. if (!names.some(n => this.hasPlugin(n))) {
  4292. throw this.raise(pos != null ? pos : this.state.start, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`, {
  4293. missingPluginNames: names
  4294. });
  4295. }
  4296. }
  4297. checkYieldAwaitInDefaultParams() {
  4298. if (this.state.yieldPos && (!this.state.awaitPos || this.state.yieldPos < this.state.awaitPos)) {
  4299. this.raise(this.state.yieldPos, "Yield cannot be used as name inside a generator function");
  4300. }
  4301. if (this.state.awaitPos) {
  4302. this.raise(this.state.awaitPos, "Await cannot be used as name inside an async function");
  4303. }
  4304. }
  4305. strictDirective(start) {
  4306. for (;;) {
  4307. skipWhiteSpace.lastIndex = start;
  4308. start += skipWhiteSpace.exec(this.input)[0].length;
  4309. const match = literal.exec(this.input.slice(start));
  4310. if (!match) break;
  4311. if (match[2] === "use strict") return true;
  4312. start += match[0].length;
  4313. skipWhiteSpace.lastIndex = start;
  4314. start += skipWhiteSpace.exec(this.input)[0].length;
  4315. if (this.input[start] === ";") {
  4316. start++;
  4317. }
  4318. }
  4319. return false;
  4320. }
  4321. }
  4322. class Node {
  4323. constructor(parser, pos, loc) {
  4324. this.type = "";
  4325. this.start = pos;
  4326. this.end = 0;
  4327. this.loc = new SourceLocation(loc);
  4328. if (parser && parser.options.ranges) this.range = [pos, 0];
  4329. if (parser && parser.filename) this.loc.filename = parser.filename;
  4330. }
  4331. __clone() {
  4332. const newNode = new Node();
  4333. const keys = Object.keys(this);
  4334. for (let i = 0, length = keys.length; i < length; i++) {
  4335. const key = keys[i];
  4336. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  4337. newNode[key] = this[key];
  4338. }
  4339. }
  4340. return newNode;
  4341. }
  4342. }
  4343. class NodeUtils extends UtilParser {
  4344. startNode() {
  4345. return new Node(this, this.state.start, this.state.startLoc);
  4346. }
  4347. startNodeAt(pos, loc) {
  4348. return new Node(this, pos, loc);
  4349. }
  4350. startNodeAtNode(type) {
  4351. return this.startNodeAt(type.start, type.loc.start);
  4352. }
  4353. finishNode(node, type) {
  4354. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  4355. }
  4356. finishNodeAt(node, type, pos, loc) {
  4357. node.type = type;
  4358. node.end = pos;
  4359. node.loc.end = loc;
  4360. if (this.options.ranges) node.range[1] = pos;
  4361. this.processComment(node);
  4362. return node;
  4363. }
  4364. resetStartLocation(node, start, startLoc) {
  4365. node.start = start;
  4366. node.loc.start = startLoc;
  4367. if (this.options.ranges) node.range[0] = start;
  4368. }
  4369. resetStartLocationFromNode(node, locationNode) {
  4370. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  4371. }
  4372. }
  4373. class LValParser extends NodeUtils {
  4374. toAssignable(node, isBinding, contextDescription) {
  4375. if (node) {
  4376. switch (node.type) {
  4377. case "Identifier":
  4378. case "ObjectPattern":
  4379. case "ArrayPattern":
  4380. case "AssignmentPattern":
  4381. break;
  4382. case "ObjectExpression":
  4383. node.type = "ObjectPattern";
  4384. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  4385. const prop = node.properties[i];
  4386. const isLast = i === last;
  4387. this.toAssignableObjectExpressionProp(prop, isBinding, isLast);
  4388. }
  4389. break;
  4390. case "ObjectProperty":
  4391. this.toAssignable(node.value, isBinding, contextDescription);
  4392. break;
  4393. case "SpreadElement":
  4394. {
  4395. this.checkToRestConversion(node);
  4396. node.type = "RestElement";
  4397. const arg = node.argument;
  4398. this.toAssignable(arg, isBinding, contextDescription);
  4399. break;
  4400. }
  4401. case "ArrayExpression":
  4402. node.type = "ArrayPattern";
  4403. this.toAssignableList(node.elements, isBinding, contextDescription);
  4404. break;
  4405. case "AssignmentExpression":
  4406. if (node.operator === "=") {
  4407. node.type = "AssignmentPattern";
  4408. delete node.operator;
  4409. } else {
  4410. this.raise(node.left.end, "Only '=' operator can be used for specifying default value.");
  4411. }
  4412. break;
  4413. case "ParenthesizedExpression":
  4414. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  4415. break;
  4416. case "MemberExpression":
  4417. if (!isBinding) break;
  4418. default:
  4419. {
  4420. const message = "Invalid left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
  4421. this.raise(node.start, message);
  4422. }
  4423. }
  4424. }
  4425. return node;
  4426. }
  4427. toAssignableObjectExpressionProp(prop, isBinding, isLast) {
  4428. if (prop.type === "ObjectMethod") {
  4429. const error = prop.kind === "get" || prop.kind === "set" ? "Object pattern can't contain getter or setter" : "Object pattern can't contain methods";
  4430. this.raise(prop.key.start, error);
  4431. } else if (prop.type === "SpreadElement" && !isLast) {
  4432. this.raiseRestNotLast(prop.start);
  4433. } else {
  4434. this.toAssignable(prop, isBinding, "object destructuring pattern");
  4435. }
  4436. }
  4437. toAssignableList(exprList, isBinding, contextDescription) {
  4438. let end = exprList.length;
  4439. if (end) {
  4440. const last = exprList[end - 1];
  4441. if (last && last.type === "RestElement") {
  4442. --end;
  4443. } else if (last && last.type === "SpreadElement") {
  4444. last.type = "RestElement";
  4445. const arg = last.argument;
  4446. this.toAssignable(arg, isBinding, contextDescription);
  4447. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  4448. this.unexpected(arg.start);
  4449. }
  4450. --end;
  4451. }
  4452. }
  4453. for (let i = 0; i < end; i++) {
  4454. const elt = exprList[i];
  4455. if (elt) {
  4456. this.toAssignable(elt, isBinding, contextDescription);
  4457. if (elt.type === "RestElement") {
  4458. this.raiseRestNotLast(elt.start);
  4459. }
  4460. }
  4461. }
  4462. return exprList;
  4463. }
  4464. toReferencedList(exprList, isParenthesizedExpr) {
  4465. return exprList;
  4466. }
  4467. toReferencedListDeep(exprList, isParenthesizedExpr) {
  4468. this.toReferencedList(exprList, isParenthesizedExpr);
  4469. for (let _i = 0; _i < exprList.length; _i++) {
  4470. const expr = exprList[_i];
  4471. if (expr && expr.type === "ArrayExpression") {
  4472. this.toReferencedListDeep(expr.elements);
  4473. }
  4474. }
  4475. return exprList;
  4476. }
  4477. parseSpread(refShorthandDefaultPos, refNeedsArrowPos) {
  4478. const node = this.startNode();
  4479. this.next();
  4480. node.argument = this.parseMaybeAssign(false, refShorthandDefaultPos, undefined, refNeedsArrowPos);
  4481. if (this.state.commaAfterSpreadAt === -1 && this.match(types.comma)) {
  4482. this.state.commaAfterSpreadAt = this.state.start;
  4483. }
  4484. return this.finishNode(node, "SpreadElement");
  4485. }
  4486. parseRestBinding() {
  4487. const node = this.startNode();
  4488. this.next();
  4489. node.argument = this.parseBindingAtom();
  4490. return this.finishNode(node, "RestElement");
  4491. }
  4492. parseBindingAtom() {
  4493. switch (this.state.type) {
  4494. case types.name:
  4495. return this.parseIdentifier();
  4496. case types.bracketL:
  4497. {
  4498. const node = this.startNode();
  4499. this.next();
  4500. node.elements = this.parseBindingList(types.bracketR, true);
  4501. return this.finishNode(node, "ArrayPattern");
  4502. }
  4503. case types.braceL:
  4504. return this.parseObj(true);
  4505. default:
  4506. throw this.unexpected();
  4507. }
  4508. }
  4509. parseBindingList(close, allowEmpty, allowModifiers) {
  4510. const elts = [];
  4511. let first = true;
  4512. while (!this.eat(close)) {
  4513. if (first) {
  4514. first = false;
  4515. } else {
  4516. this.expect(types.comma);
  4517. }
  4518. if (allowEmpty && this.match(types.comma)) {
  4519. elts.push(null);
  4520. } else if (this.eat(close)) {
  4521. break;
  4522. } else if (this.match(types.ellipsis)) {
  4523. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  4524. this.checkCommaAfterRest();
  4525. this.expect(close);
  4526. break;
  4527. } else {
  4528. const decorators = [];
  4529. if (this.match(types.at) && this.hasPlugin("decorators")) {
  4530. this.raise(this.state.start, "Stage 2 decorators cannot be used to decorate parameters");
  4531. }
  4532. while (this.match(types.at)) {
  4533. decorators.push(this.parseDecorator());
  4534. }
  4535. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  4536. }
  4537. }
  4538. return elts;
  4539. }
  4540. parseAssignableListItem(allowModifiers, decorators) {
  4541. const left = this.parseMaybeDefault();
  4542. this.parseAssignableListItemTypes(left);
  4543. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  4544. if (decorators.length) {
  4545. left.decorators = decorators;
  4546. }
  4547. return elt;
  4548. }
  4549. parseAssignableListItemTypes(param) {
  4550. return param;
  4551. }
  4552. parseMaybeDefault(startPos, startLoc, left) {
  4553. startLoc = startLoc || this.state.startLoc;
  4554. startPos = startPos || this.state.start;
  4555. left = left || this.parseBindingAtom();
  4556. if (!this.eat(types.eq)) return left;
  4557. const node = this.startNodeAt(startPos, startLoc);
  4558. node.left = left;
  4559. node.right = this.parseMaybeAssign();
  4560. return this.finishNode(node, "AssignmentPattern");
  4561. }
  4562. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  4563. switch (expr.type) {
  4564. case "Identifier":
  4565. if (this.state.strict && isStrictBindReservedWord(expr.name, this.inModule)) {
  4566. this.raise(expr.start, `${bindingType === BIND_NONE ? "Assigning to" : "Binding"} '${expr.name}' in strict mode`);
  4567. }
  4568. if (checkClashes) {
  4569. const key = `_${expr.name}`;
  4570. if (checkClashes[key]) {
  4571. this.raise(expr.start, "Argument name clash");
  4572. } else {
  4573. checkClashes[key] = true;
  4574. }
  4575. }
  4576. if (bindingType !== BIND_NONE && bindingType !== BIND_OUTSIDE) {
  4577. this.scope.declareName(expr.name, bindingType, expr.start);
  4578. }
  4579. break;
  4580. case "MemberExpression":
  4581. if (bindingType !== BIND_NONE) {
  4582. this.raise(expr.start, "Binding member expression");
  4583. }
  4584. break;
  4585. case "ObjectPattern":
  4586. for (let _i2 = 0, _expr$properties = expr.properties; _i2 < _expr$properties.length; _i2++) {
  4587. let prop = _expr$properties[_i2];
  4588. if (prop.type === "ObjectProperty") prop = prop.value;
  4589. this.checkLVal(prop, bindingType, checkClashes, "object destructuring pattern");
  4590. }
  4591. break;
  4592. case "ArrayPattern":
  4593. for (let _i3 = 0, _expr$elements = expr.elements; _i3 < _expr$elements.length; _i3++) {
  4594. const elem = _expr$elements[_i3];
  4595. if (elem) {
  4596. this.checkLVal(elem, bindingType, checkClashes, "array destructuring pattern");
  4597. }
  4598. }
  4599. break;
  4600. case "AssignmentPattern":
  4601. this.checkLVal(expr.left, bindingType, checkClashes, "assignment pattern");
  4602. break;
  4603. case "RestElement":
  4604. this.checkLVal(expr.argument, bindingType, checkClashes, "rest element");
  4605. break;
  4606. case "ParenthesizedExpression":
  4607. this.checkLVal(expr.expression, bindingType, checkClashes, "parenthesized expression");
  4608. break;
  4609. default:
  4610. {
  4611. const message = (bindingType === BIND_NONE ? "Invalid" : "Binding invalid") + " left-hand side" + (contextDescription ? " in " + contextDescription : "expression");
  4612. this.raise(expr.start, message);
  4613. }
  4614. }
  4615. }
  4616. checkToRestConversion(node) {
  4617. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  4618. this.raise(node.argument.start, "Invalid rest operator's argument");
  4619. }
  4620. }
  4621. checkCommaAfterRest() {
  4622. if (this.match(types.comma)) {
  4623. this.raiseRestNotLast(this.state.start);
  4624. }
  4625. }
  4626. checkCommaAfterRestFromSpread() {
  4627. if (this.state.commaAfterSpreadAt > -1) {
  4628. this.raiseRestNotLast(this.state.commaAfterSpreadAt);
  4629. }
  4630. }
  4631. raiseRestNotLast(pos) {
  4632. this.raise(pos, `Rest element must be last element`);
  4633. }
  4634. }
  4635. const unwrapParenthesizedExpression = node => {
  4636. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  4637. };
  4638. class ExpressionParser extends LValParser {
  4639. checkPropClash(prop, propHash) {
  4640. if (prop.type === "SpreadElement" || prop.computed || prop.kind || prop.shorthand) {
  4641. return;
  4642. }
  4643. const key = prop.key;
  4644. const name = key.type === "Identifier" ? key.name : String(key.value);
  4645. if (name === "__proto__") {
  4646. if (propHash.proto) {
  4647. this.raise(key.start, "Redefinition of __proto__ property");
  4648. }
  4649. propHash.proto = true;
  4650. }
  4651. }
  4652. getExpression() {
  4653. this.scope.enter(SCOPE_PROGRAM);
  4654. this.nextToken();
  4655. const expr = this.parseExpression();
  4656. if (!this.match(types.eof)) {
  4657. this.unexpected();
  4658. }
  4659. expr.comments = this.state.comments;
  4660. return expr;
  4661. }
  4662. parseExpression(noIn, refShorthandDefaultPos) {
  4663. const startPos = this.state.start;
  4664. const startLoc = this.state.startLoc;
  4665. const expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
  4666. if (this.match(types.comma)) {
  4667. const node = this.startNodeAt(startPos, startLoc);
  4668. node.expressions = [expr];
  4669. while (this.eat(types.comma)) {
  4670. node.expressions.push(this.parseMaybeAssign(noIn, refShorthandDefaultPos));
  4671. }
  4672. this.toReferencedList(node.expressions);
  4673. return this.finishNode(node, "SequenceExpression");
  4674. }
  4675. return expr;
  4676. }
  4677. parseMaybeAssign(noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) {
  4678. const startPos = this.state.start;
  4679. const startLoc = this.state.startLoc;
  4680. if (this.isContextual("yield")) {
  4681. if (this.scope.inGenerator) {
  4682. let left = this.parseYield(noIn);
  4683. if (afterLeftParse) {
  4684. left = afterLeftParse.call(this, left, startPos, startLoc);
  4685. }
  4686. return left;
  4687. } else {
  4688. this.state.exprAllowed = false;
  4689. }
  4690. }
  4691. const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
  4692. this.state.commaAfterSpreadAt = -1;
  4693. let failOnShorthandAssign;
  4694. if (refShorthandDefaultPos) {
  4695. failOnShorthandAssign = false;
  4696. } else {
  4697. refShorthandDefaultPos = {
  4698. start: 0
  4699. };
  4700. failOnShorthandAssign = true;
  4701. }
  4702. if (this.match(types.parenL) || this.match(types.name)) {
  4703. this.state.potentialArrowAt = this.state.start;
  4704. }
  4705. let left = this.parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos);
  4706. if (afterLeftParse) {
  4707. left = afterLeftParse.call(this, left, startPos, startLoc);
  4708. }
  4709. if (this.state.type.isAssign) {
  4710. const node = this.startNodeAt(startPos, startLoc);
  4711. const operator = this.state.value;
  4712. node.operator = operator;
  4713. if (operator === "??=") {
  4714. this.expectPlugin("nullishCoalescingOperator");
  4715. this.expectPlugin("logicalAssignment");
  4716. }
  4717. if (operator === "||=" || operator === "&&=") {
  4718. this.expectPlugin("logicalAssignment");
  4719. }
  4720. node.left = this.match(types.eq) ? this.toAssignable(left, undefined, "assignment expression") : left;
  4721. refShorthandDefaultPos.start = 0;
  4722. this.checkLVal(left, undefined, undefined, "assignment expression");
  4723. const maybePattern = unwrapParenthesizedExpression(left);
  4724. let patternErrorMsg;
  4725. if (maybePattern.type === "ObjectPattern") {
  4726. patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
  4727. } else if (maybePattern.type === "ArrayPattern") {
  4728. patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
  4729. }
  4730. if (patternErrorMsg && (left.extra && left.extra.parenthesized || left.type === "ParenthesizedExpression")) {
  4731. this.raise(maybePattern.start, `You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`);
  4732. }
  4733. if (patternErrorMsg) this.checkCommaAfterRestFromSpread();
  4734. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  4735. this.next();
  4736. node.right = this.parseMaybeAssign(noIn);
  4737. return this.finishNode(node, "AssignmentExpression");
  4738. } else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
  4739. this.unexpected(refShorthandDefaultPos.start);
  4740. }
  4741. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  4742. return left;
  4743. }
  4744. parseMaybeConditional(noIn, refShorthandDefaultPos, refNeedsArrowPos) {
  4745. const startPos = this.state.start;
  4746. const startLoc = this.state.startLoc;
  4747. const potentialArrowAt = this.state.potentialArrowAt;
  4748. const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
  4749. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  4750. return expr;
  4751. }
  4752. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  4753. return this.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  4754. }
  4755. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  4756. if (this.eat(types.question)) {
  4757. const node = this.startNodeAt(startPos, startLoc);
  4758. node.test = expr;
  4759. node.consequent = this.parseMaybeAssign();
  4760. this.expect(types.colon);
  4761. node.alternate = this.parseMaybeAssign(noIn);
  4762. return this.finishNode(node, "ConditionalExpression");
  4763. }
  4764. return expr;
  4765. }
  4766. parseExprOps(noIn, refShorthandDefaultPos) {
  4767. const startPos = this.state.start;
  4768. const startLoc = this.state.startLoc;
  4769. const potentialArrowAt = this.state.potentialArrowAt;
  4770. const expr = this.parseMaybeUnary(refShorthandDefaultPos);
  4771. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  4772. return expr;
  4773. }
  4774. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  4775. return expr;
  4776. }
  4777. return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
  4778. }
  4779. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  4780. const prec = this.state.type.binop;
  4781. if (prec != null && (!noIn || !this.match(types._in))) {
  4782. if (prec > minPrec) {
  4783. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  4784. const operator = this.state.value;
  4785. node.left = left;
  4786. node.operator = operator;
  4787. if (operator === "**" && left.type === "UnaryExpression" && (this.options.createParenthesizedExpressions || !(left.extra && left.extra.parenthesized))) {
  4788. this.raise(left.argument.start, "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.");
  4789. }
  4790. const op = this.state.type;
  4791. if (op === types.pipeline) {
  4792. this.expectPlugin("pipelineOperator");
  4793. this.state.inPipeline = true;
  4794. this.checkPipelineAtInfixOperator(left, leftStartPos);
  4795. } else if (op === types.nullishCoalescing) {
  4796. this.expectPlugin("nullishCoalescingOperator");
  4797. }
  4798. this.next();
  4799. if (op === types.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  4800. if (this.match(types.name) && this.state.value === "await" && this.scope.inAsync) {
  4801. throw this.raise(this.state.start, `Unexpected "await" after pipeline body; await must have parentheses in minimal proposal`);
  4802. }
  4803. }
  4804. node.right = this.parseExprOpRightExpr(op, prec, noIn);
  4805. this.finishNode(node, op === types.logicalOR || op === types.logicalAND || op === types.nullishCoalescing ? "LogicalExpression" : "BinaryExpression");
  4806. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  4807. }
  4808. }
  4809. return left;
  4810. }
  4811. parseExprOpRightExpr(op, prec, noIn) {
  4812. switch (op) {
  4813. case types.pipeline:
  4814. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  4815. const startPos = this.state.start;
  4816. const startLoc = this.state.startLoc;
  4817. return this.withTopicPermittingContext(() => {
  4818. return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec, noIn), startPos, startLoc);
  4819. });
  4820. }
  4821. default:
  4822. return this.parseExprOpBaseRightExpr(op, prec, noIn);
  4823. }
  4824. }
  4825. parseExprOpBaseRightExpr(op, prec, noIn) {
  4826. const startPos = this.state.start;
  4827. const startLoc = this.state.startLoc;
  4828. return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn);
  4829. }
  4830. parseMaybeUnary(refShorthandDefaultPos) {
  4831. if (this.isContextual("await") && (this.scope.inAsync || !this.scope.inFunction && this.options.allowAwaitOutsideFunction)) {
  4832. return this.parseAwait();
  4833. } else if (this.state.type.prefix) {
  4834. const node = this.startNode();
  4835. const update = this.match(types.incDec);
  4836. node.operator = this.state.value;
  4837. node.prefix = true;
  4838. if (node.operator === "throw") {
  4839. this.expectPlugin("throwExpressions");
  4840. }
  4841. this.next();
  4842. node.argument = this.parseMaybeUnary();
  4843. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  4844. this.unexpected(refShorthandDefaultPos.start);
  4845. }
  4846. if (update) {
  4847. this.checkLVal(node.argument, undefined, undefined, "prefix operation");
  4848. } else if (this.state.strict && node.operator === "delete") {
  4849. const arg = node.argument;
  4850. if (arg.type === "Identifier") {
  4851. this.raise(node.start, "Deleting local variable in strict mode");
  4852. } else if (arg.type === "MemberExpression" && arg.property.type === "PrivateName") {
  4853. this.raise(node.start, "Deleting a private field is not allowed");
  4854. }
  4855. }
  4856. return this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  4857. }
  4858. const startPos = this.state.start;
  4859. const startLoc = this.state.startLoc;
  4860. let expr = this.parseExprSubscripts(refShorthandDefaultPos);
  4861. if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
  4862. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  4863. const node = this.startNodeAt(startPos, startLoc);
  4864. node.operator = this.state.value;
  4865. node.prefix = false;
  4866. node.argument = expr;
  4867. this.checkLVal(expr, undefined, undefined, "postfix operation");
  4868. this.next();
  4869. expr = this.finishNode(node, "UpdateExpression");
  4870. }
  4871. return expr;
  4872. }
  4873. parseExprSubscripts(refShorthandDefaultPos) {
  4874. const startPos = this.state.start;
  4875. const startLoc = this.state.startLoc;
  4876. const potentialArrowAt = this.state.potentialArrowAt;
  4877. const expr = this.parseExprAtom(refShorthandDefaultPos);
  4878. if (expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt) {
  4879. return expr;
  4880. }
  4881. if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
  4882. return expr;
  4883. }
  4884. return this.parseSubscripts(expr, startPos, startLoc);
  4885. }
  4886. parseSubscripts(base, startPos, startLoc, noCalls) {
  4887. const maybeAsyncArrow = this.atPossibleAsync(base);
  4888. const state = {
  4889. optionalChainMember: false,
  4890. stop: false
  4891. };
  4892. do {
  4893. base = this.parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow);
  4894. } while (!state.stop);
  4895. return base;
  4896. }
  4897. parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow) {
  4898. if (!noCalls && this.eat(types.doubleColon)) {
  4899. const node = this.startNodeAt(startPos, startLoc);
  4900. node.object = base;
  4901. node.callee = this.parseNoCallExpr();
  4902. state.stop = true;
  4903. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  4904. } else if (this.match(types.questionDot)) {
  4905. this.expectPlugin("optionalChaining");
  4906. state.optionalChainMember = true;
  4907. if (noCalls && this.lookahead().type === types.parenL) {
  4908. state.stop = true;
  4909. return base;
  4910. }
  4911. this.next();
  4912. const node = this.startNodeAt(startPos, startLoc);
  4913. if (this.eat(types.bracketL)) {
  4914. node.object = base;
  4915. node.property = this.parseExpression();
  4916. node.computed = true;
  4917. node.optional = true;
  4918. this.expect(types.bracketR);
  4919. return this.finishNode(node, "OptionalMemberExpression");
  4920. } else if (this.eat(types.parenL)) {
  4921. node.callee = base;
  4922. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  4923. node.optional = true;
  4924. return this.finishNode(node, "OptionalCallExpression");
  4925. } else {
  4926. node.object = base;
  4927. node.property = this.parseIdentifier(true);
  4928. node.computed = false;
  4929. node.optional = true;
  4930. return this.finishNode(node, "OptionalMemberExpression");
  4931. }
  4932. } else if (this.eat(types.dot)) {
  4933. const node = this.startNodeAt(startPos, startLoc);
  4934. node.object = base;
  4935. node.property = this.parseMaybePrivateName();
  4936. node.computed = false;
  4937. if (state.optionalChainMember) {
  4938. node.optional = false;
  4939. return this.finishNode(node, "OptionalMemberExpression");
  4940. }
  4941. return this.finishNode(node, "MemberExpression");
  4942. } else if (this.eat(types.bracketL)) {
  4943. const node = this.startNodeAt(startPos, startLoc);
  4944. node.object = base;
  4945. node.property = this.parseExpression();
  4946. node.computed = true;
  4947. this.expect(types.bracketR);
  4948. if (state.optionalChainMember) {
  4949. node.optional = false;
  4950. return this.finishNode(node, "OptionalMemberExpression");
  4951. }
  4952. return this.finishNode(node, "MemberExpression");
  4953. } else if (!noCalls && this.match(types.parenL)) {
  4954. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  4955. const oldYieldPos = this.state.yieldPos;
  4956. const oldAwaitPos = this.state.awaitPos;
  4957. this.state.maybeInArrowParameters = true;
  4958. this.state.yieldPos = 0;
  4959. this.state.awaitPos = 0;
  4960. this.next();
  4961. let node = this.startNodeAt(startPos, startLoc);
  4962. node.callee = base;
  4963. const oldCommaAfterSpreadAt = this.state.commaAfterSpreadAt;
  4964. this.state.commaAfterSpreadAt = -1;
  4965. node.arguments = this.parseCallExpressionArguments(types.parenR, maybeAsyncArrow, base.type === "Import", base.type !== "Super");
  4966. if (!state.optionalChainMember) {
  4967. this.finishCallExpression(node);
  4968. } else {
  4969. this.finishOptionalCallExpression(node);
  4970. }
  4971. if (maybeAsyncArrow && this.shouldParseAsyncArrow()) {
  4972. state.stop = true;
  4973. this.checkCommaAfterRestFromSpread();
  4974. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  4975. this.checkYieldAwaitInDefaultParams();
  4976. this.state.yieldPos = oldYieldPos;
  4977. this.state.awaitPos = oldAwaitPos;
  4978. } else {
  4979. this.toReferencedListDeep(node.arguments);
  4980. this.state.yieldPos = oldYieldPos || this.state.yieldPos;
  4981. this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
  4982. }
  4983. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  4984. this.state.commaAfterSpreadAt = oldCommaAfterSpreadAt;
  4985. return node;
  4986. } else if (this.match(types.backQuote)) {
  4987. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state);
  4988. } else {
  4989. state.stop = true;
  4990. return base;
  4991. }
  4992. }
  4993. parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments) {
  4994. const node = this.startNodeAt(startPos, startLoc);
  4995. node.tag = base;
  4996. node.quasi = this.parseTemplate(true);
  4997. if (typeArguments) node.typeParameters = typeArguments;
  4998. if (state.optionalChainMember) {
  4999. this.raise(startPos, "Tagged Template Literals are not allowed in optionalChain");
  5000. }
  5001. return this.finishNode(node, "TaggedTemplateExpression");
  5002. }
  5003. atPossibleAsync(base) {
  5004. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && this.input.slice(base.start, base.end) === "async";
  5005. }
  5006. finishCallExpression(node) {
  5007. if (node.callee.type === "Import") {
  5008. if (node.arguments.length !== 1) {
  5009. this.raise(node.start, "import() requires exactly one argument");
  5010. }
  5011. const importArg = node.arguments[0];
  5012. if (importArg && importArg.type === "SpreadElement") {
  5013. this.raise(importArg.start, "... is not allowed in import()");
  5014. }
  5015. }
  5016. return this.finishNode(node, "CallExpression");
  5017. }
  5018. finishOptionalCallExpression(node) {
  5019. if (node.callee.type === "Import") {
  5020. if (node.arguments.length !== 1) {
  5021. this.raise(node.start, "import() requires exactly one argument");
  5022. }
  5023. const importArg = node.arguments[0];
  5024. if (importArg && importArg.type === "SpreadElement") {
  5025. this.raise(importArg.start, "... is not allowed in import()");
  5026. }
  5027. }
  5028. return this.finishNode(node, "OptionalCallExpression");
  5029. }
  5030. parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport, allowPlaceholder) {
  5031. const elts = [];
  5032. let innerParenStart;
  5033. let first = true;
  5034. while (!this.eat(close)) {
  5035. if (first) {
  5036. first = false;
  5037. } else {
  5038. this.expect(types.comma);
  5039. if (this.eat(close)) {
  5040. if (dynamicImport) {
  5041. this.raise(this.state.lastTokStart, "Trailing comma is disallowed inside import(...) arguments");
  5042. }
  5043. break;
  5044. }
  5045. }
  5046. if (this.match(types.parenL) && !innerParenStart) {
  5047. innerParenStart = this.state.start;
  5048. }
  5049. elts.push(this.parseExprListItem(false, possibleAsyncArrow ? {
  5050. start: 0
  5051. } : undefined, possibleAsyncArrow ? {
  5052. start: 0
  5053. } : undefined, allowPlaceholder));
  5054. }
  5055. if (possibleAsyncArrow && innerParenStart && this.shouldParseAsyncArrow()) {
  5056. this.unexpected();
  5057. }
  5058. return elts;
  5059. }
  5060. shouldParseAsyncArrow() {
  5061. return this.match(types.arrow) && !this.canInsertSemicolon();
  5062. }
  5063. parseAsyncArrowFromCallExpression(node, call) {
  5064. this.expect(types.arrow);
  5065. this.parseArrowExpression(node, call.arguments, true);
  5066. return node;
  5067. }
  5068. parseNoCallExpr() {
  5069. const startPos = this.state.start;
  5070. const startLoc = this.state.startLoc;
  5071. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  5072. }
  5073. parseExprAtom(refShorthandDefaultPos) {
  5074. if (this.state.type === types.slash) this.readRegexp();
  5075. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  5076. let node;
  5077. switch (this.state.type) {
  5078. case types._super:
  5079. if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  5080. this.raise(this.state.start, "super is only allowed in object methods and classes");
  5081. }
  5082. node = this.startNode();
  5083. this.next();
  5084. if (this.match(types.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  5085. 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?");
  5086. }
  5087. if (!this.match(types.parenL) && !this.match(types.bracketL) && !this.match(types.dot)) {
  5088. this.unexpected();
  5089. }
  5090. return this.finishNode(node, "Super");
  5091. case types._import:
  5092. if (this.lookahead().type === types.dot) {
  5093. return this.parseImportMetaProperty();
  5094. }
  5095. this.expectPlugin("dynamicImport");
  5096. node = this.startNode();
  5097. this.next();
  5098. if (!this.match(types.parenL)) {
  5099. this.unexpected(null, types.parenL);
  5100. }
  5101. return this.finishNode(node, "Import");
  5102. case types._this:
  5103. node = this.startNode();
  5104. this.next();
  5105. return this.finishNode(node, "ThisExpression");
  5106. case types.name:
  5107. {
  5108. node = this.startNode();
  5109. const containsEsc = this.state.containsEsc;
  5110. const id = this.parseIdentifier();
  5111. if (!containsEsc && id.name === "async" && this.match(types._function) && !this.canInsertSemicolon()) {
  5112. this.next();
  5113. return this.parseFunction(node, undefined, true);
  5114. } else if (canBeArrow && !containsEsc && id.name === "async" && this.match(types.name) && !this.canInsertSemicolon()) {
  5115. const params = [this.parseIdentifier()];
  5116. this.expect(types.arrow);
  5117. this.parseArrowExpression(node, params, true);
  5118. return node;
  5119. }
  5120. if (canBeArrow && this.match(types.arrow) && !this.canInsertSemicolon()) {
  5121. this.next();
  5122. this.parseArrowExpression(node, [id], false);
  5123. return node;
  5124. }
  5125. return id;
  5126. }
  5127. case types._do:
  5128. {
  5129. this.expectPlugin("doExpressions");
  5130. const node = this.startNode();
  5131. this.next();
  5132. const oldLabels = this.state.labels;
  5133. this.state.labels = [];
  5134. node.body = this.parseBlock();
  5135. this.state.labels = oldLabels;
  5136. return this.finishNode(node, "DoExpression");
  5137. }
  5138. case types.regexp:
  5139. {
  5140. const value = this.state.value;
  5141. node = this.parseLiteral(value.value, "RegExpLiteral");
  5142. node.pattern = value.pattern;
  5143. node.flags = value.flags;
  5144. return node;
  5145. }
  5146. case types.num:
  5147. return this.parseLiteral(this.state.value, "NumericLiteral");
  5148. case types.bigint:
  5149. return this.parseLiteral(this.state.value, "BigIntLiteral");
  5150. case types.string:
  5151. return this.parseLiteral(this.state.value, "StringLiteral");
  5152. case types._null:
  5153. node = this.startNode();
  5154. this.next();
  5155. return this.finishNode(node, "NullLiteral");
  5156. case types._true:
  5157. case types._false:
  5158. return this.parseBooleanLiteral();
  5159. case types.parenL:
  5160. return this.parseParenAndDistinguishExpression(canBeArrow);
  5161. case types.bracketL:
  5162. node = this.startNode();
  5163. this.next();
  5164. node.elements = this.parseExprList(types.bracketR, true, refShorthandDefaultPos);
  5165. if (!this.state.maybeInArrowParameters) {
  5166. this.toReferencedList(node.elements);
  5167. }
  5168. return this.finishNode(node, "ArrayExpression");
  5169. case types.braceL:
  5170. return this.parseObj(false, refShorthandDefaultPos);
  5171. case types._function:
  5172. return this.parseFunctionExpression();
  5173. case types.at:
  5174. this.parseDecorators();
  5175. case types._class:
  5176. node = this.startNode();
  5177. this.takeDecorators(node);
  5178. return this.parseClass(node, false);
  5179. case types._new:
  5180. return this.parseNew();
  5181. case types.backQuote:
  5182. return this.parseTemplate(false);
  5183. case types.doubleColon:
  5184. {
  5185. node = this.startNode();
  5186. this.next();
  5187. node.object = null;
  5188. const callee = node.callee = this.parseNoCallExpr();
  5189. if (callee.type === "MemberExpression") {
  5190. return this.finishNode(node, "BindExpression");
  5191. } else {
  5192. throw this.raise(callee.start, "Binding should be performed on object property.");
  5193. }
  5194. }
  5195. case types.hash:
  5196. {
  5197. if (this.state.inPipeline) {
  5198. node = this.startNode();
  5199. if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
  5200. this.raise(node.start, "Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.");
  5201. }
  5202. this.next();
  5203. if (this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
  5204. this.registerTopicReference();
  5205. return this.finishNode(node, "PipelinePrimaryTopicReference");
  5206. } else {
  5207. throw this.raise(node.start, `Topic reference was used in a lexical context without topic binding`);
  5208. }
  5209. }
  5210. }
  5211. default:
  5212. throw this.unexpected();
  5213. }
  5214. }
  5215. parseBooleanLiteral() {
  5216. const node = this.startNode();
  5217. node.value = this.match(types._true);
  5218. this.next();
  5219. return this.finishNode(node, "BooleanLiteral");
  5220. }
  5221. parseMaybePrivateName() {
  5222. const isPrivate = this.match(types.hash);
  5223. if (isPrivate) {
  5224. this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
  5225. const node = this.startNode();
  5226. this.next();
  5227. this.assertNoSpace("Unexpected space between # and identifier");
  5228. node.id = this.parseIdentifier(true);
  5229. return this.finishNode(node, "PrivateName");
  5230. } else {
  5231. return this.parseIdentifier(true);
  5232. }
  5233. }
  5234. parseFunctionExpression() {
  5235. const node = this.startNode();
  5236. let meta = this.startNode();
  5237. this.next();
  5238. meta = this.createIdentifier(meta, "function");
  5239. if (this.scope.inGenerator && this.eat(types.dot)) {
  5240. return this.parseMetaProperty(node, meta, "sent");
  5241. }
  5242. return this.parseFunction(node);
  5243. }
  5244. parseMetaProperty(node, meta, propertyName) {
  5245. node.meta = meta;
  5246. if (meta.name === "function" && propertyName === "sent") {
  5247. if (this.isContextual(propertyName)) {
  5248. this.expectPlugin("functionSent");
  5249. } else if (!this.hasPlugin("functionSent")) {
  5250. this.unexpected();
  5251. }
  5252. }
  5253. const containsEsc = this.state.containsEsc;
  5254. node.property = this.parseIdentifier(true);
  5255. if (node.property.name !== propertyName || containsEsc) {
  5256. this.raise(node.property.start, `The only valid meta property for ${meta.name} is ${meta.name}.${propertyName}`);
  5257. }
  5258. return this.finishNode(node, "MetaProperty");
  5259. }
  5260. parseImportMetaProperty() {
  5261. const node = this.startNode();
  5262. const id = this.parseIdentifier(true);
  5263. this.expect(types.dot);
  5264. if (id.name === "import") {
  5265. if (this.isContextual("meta")) {
  5266. this.expectPlugin("importMeta");
  5267. } else if (!this.hasPlugin("importMeta")) {
  5268. this.raise(id.start, `Dynamic imports require a parameter: import('a.js')`);
  5269. }
  5270. }
  5271. if (!this.inModule) {
  5272. this.raise(id.start, `import.meta may appear only with 'sourceType: "module"'`, {
  5273. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  5274. });
  5275. }
  5276. this.sawUnambiguousESM = true;
  5277. return this.parseMetaProperty(node, id, "meta");
  5278. }
  5279. parseLiteral(value, type, startPos, startLoc) {
  5280. startPos = startPos || this.state.start;
  5281. startLoc = startLoc || this.state.startLoc;
  5282. const node = this.startNodeAt(startPos, startLoc);
  5283. this.addExtra(node, "rawValue", value);
  5284. this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
  5285. node.value = value;
  5286. this.next();
  5287. return this.finishNode(node, type);
  5288. }
  5289. parseParenAndDistinguishExpression(canBeArrow) {
  5290. const startPos = this.state.start;
  5291. const startLoc = this.state.startLoc;
  5292. let val;
  5293. this.expect(types.parenL);
  5294. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  5295. const oldYieldPos = this.state.yieldPos;
  5296. const oldAwaitPos = this.state.awaitPos;
  5297. this.state.maybeInArrowParameters = true;
  5298. this.state.yieldPos = 0;
  5299. this.state.awaitPos = 0;
  5300. const innerStartPos = this.state.start;
  5301. const innerStartLoc = this.state.startLoc;
  5302. const exprList = [];
  5303. const refShorthandDefaultPos = {
  5304. start: 0
  5305. };
  5306. const refNeedsArrowPos = {
  5307. start: 0
  5308. };
  5309. let first = true;
  5310. let spreadStart;
  5311. let optionalCommaStart;
  5312. while (!this.match(types.parenR)) {
  5313. if (first) {
  5314. first = false;
  5315. } else {
  5316. this.expect(types.comma, refNeedsArrowPos.start || null);
  5317. if (this.match(types.parenR)) {
  5318. optionalCommaStart = this.state.start;
  5319. break;
  5320. }
  5321. }
  5322. if (this.match(types.ellipsis)) {
  5323. const spreadNodeStartPos = this.state.start;
  5324. const spreadNodeStartLoc = this.state.startLoc;
  5325. spreadStart = this.state.start;
  5326. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  5327. this.checkCommaAfterRest();
  5328. break;
  5329. } else {
  5330. exprList.push(this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos));
  5331. }
  5332. }
  5333. const innerEndPos = this.state.start;
  5334. const innerEndLoc = this.state.startLoc;
  5335. this.expect(types.parenR);
  5336. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  5337. let arrowNode = this.startNodeAt(startPos, startLoc);
  5338. if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
  5339. this.checkYieldAwaitInDefaultParams();
  5340. this.state.yieldPos = oldYieldPos;
  5341. this.state.awaitPos = oldAwaitPos;
  5342. for (let _i = 0; _i < exprList.length; _i++) {
  5343. const param = exprList[_i];
  5344. if (param.extra && param.extra.parenthesized) {
  5345. this.unexpected(param.extra.parenStart);
  5346. }
  5347. }
  5348. this.parseArrowExpression(arrowNode, exprList, false);
  5349. return arrowNode;
  5350. }
  5351. this.state.yieldPos = oldYieldPos || this.state.yieldPos;
  5352. this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
  5353. if (!exprList.length) {
  5354. this.unexpected(this.state.lastTokStart);
  5355. }
  5356. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  5357. if (spreadStart) this.unexpected(spreadStart);
  5358. if (refShorthandDefaultPos.start) {
  5359. this.unexpected(refShorthandDefaultPos.start);
  5360. }
  5361. if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
  5362. this.toReferencedListDeep(exprList, true);
  5363. if (exprList.length > 1) {
  5364. val = this.startNodeAt(innerStartPos, innerStartLoc);
  5365. val.expressions = exprList;
  5366. this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
  5367. } else {
  5368. val = exprList[0];
  5369. }
  5370. if (!this.options.createParenthesizedExpressions) {
  5371. this.addExtra(val, "parenthesized", true);
  5372. this.addExtra(val, "parenStart", startPos);
  5373. return val;
  5374. }
  5375. const parenExpression = this.startNodeAt(startPos, startLoc);
  5376. parenExpression.expression = val;
  5377. this.finishNode(parenExpression, "ParenthesizedExpression");
  5378. return parenExpression;
  5379. }
  5380. shouldParseArrow() {
  5381. return !this.canInsertSemicolon();
  5382. }
  5383. parseArrow(node) {
  5384. if (this.eat(types.arrow)) {
  5385. return node;
  5386. }
  5387. }
  5388. parseParenItem(node, startPos, startLoc) {
  5389. return node;
  5390. }
  5391. parseNew() {
  5392. const node = this.startNode();
  5393. const meta = this.parseIdentifier(true);
  5394. if (this.eat(types.dot)) {
  5395. const metaProp = this.parseMetaProperty(node, meta, "target");
  5396. if (!this.scope.inNonArrowFunction && !this.state.inClassProperty) {
  5397. let error = "new.target can only be used in functions";
  5398. if (this.hasPlugin("classProperties")) {
  5399. error += " or class properties";
  5400. }
  5401. this.raise(metaProp.start, error);
  5402. }
  5403. return metaProp;
  5404. }
  5405. node.callee = this.parseNoCallExpr();
  5406. if (node.callee.type === "Import") {
  5407. this.raise(node.callee.start, "Cannot use new with import(...)");
  5408. } else if (node.callee.type === "OptionalMemberExpression" || node.callee.type === "OptionalCallExpression") {
  5409. this.raise(this.state.lastTokEnd, "constructors in/after an Optional Chain are not allowed");
  5410. } else if (this.eat(types.questionDot)) {
  5411. this.raise(this.state.start, "constructors in/after an Optional Chain are not allowed");
  5412. }
  5413. this.parseNewArguments(node);
  5414. return this.finishNode(node, "NewExpression");
  5415. }
  5416. parseNewArguments(node) {
  5417. if (this.eat(types.parenL)) {
  5418. const args = this.parseExprList(types.parenR);
  5419. this.toReferencedList(args);
  5420. node.arguments = args;
  5421. } else {
  5422. node.arguments = [];
  5423. }
  5424. }
  5425. parseTemplateElement(isTagged) {
  5426. const elem = this.startNode();
  5427. if (this.state.value === null) {
  5428. if (!isTagged) {
  5429. this.raise(this.state.invalidTemplateEscapePosition || 0, "Invalid escape sequence in template");
  5430. } else {
  5431. this.state.invalidTemplateEscapePosition = null;
  5432. }
  5433. }
  5434. elem.value = {
  5435. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  5436. cooked: this.state.value
  5437. };
  5438. this.next();
  5439. elem.tail = this.match(types.backQuote);
  5440. return this.finishNode(elem, "TemplateElement");
  5441. }
  5442. parseTemplate(isTagged) {
  5443. const node = this.startNode();
  5444. this.next();
  5445. node.expressions = [];
  5446. let curElt = this.parseTemplateElement(isTagged);
  5447. node.quasis = [curElt];
  5448. while (!curElt.tail) {
  5449. this.expect(types.dollarBraceL);
  5450. node.expressions.push(this.parseExpression());
  5451. this.expect(types.braceR);
  5452. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  5453. }
  5454. this.next();
  5455. return this.finishNode(node, "TemplateLiteral");
  5456. }
  5457. parseObj(isPattern, refShorthandDefaultPos) {
  5458. const propHash = Object.create(null);
  5459. let first = true;
  5460. const node = this.startNode();
  5461. node.properties = [];
  5462. this.next();
  5463. while (!this.eat(types.braceR)) {
  5464. if (first) {
  5465. first = false;
  5466. } else {
  5467. this.expect(types.comma);
  5468. if (this.eat(types.braceR)) break;
  5469. }
  5470. const prop = this.parseObjectMember(isPattern, refShorthandDefaultPos);
  5471. if (!isPattern) this.checkPropClash(prop, propHash);
  5472. if (prop.shorthand) {
  5473. this.addExtra(prop, "shorthand", true);
  5474. }
  5475. node.properties.push(prop);
  5476. }
  5477. return this.finishNode(node, isPattern ? "ObjectPattern" : "ObjectExpression");
  5478. }
  5479. isAsyncProp(prop) {
  5480. 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();
  5481. }
  5482. parseObjectMember(isPattern, refShorthandDefaultPos) {
  5483. let decorators = [];
  5484. if (this.match(types.at)) {
  5485. if (this.hasPlugin("decorators")) {
  5486. this.raise(this.state.start, "Stage 2 decorators disallow object literal property decorators");
  5487. } else {
  5488. while (this.match(types.at)) {
  5489. decorators.push(this.parseDecorator());
  5490. }
  5491. }
  5492. }
  5493. const prop = this.startNode();
  5494. let isGenerator = false;
  5495. let isAsync = false;
  5496. let startPos;
  5497. let startLoc;
  5498. if (this.match(types.ellipsis)) {
  5499. if (decorators.length) this.unexpected();
  5500. if (isPattern) {
  5501. this.next();
  5502. prop.argument = this.parseIdentifier();
  5503. this.checkCommaAfterRest();
  5504. return this.finishNode(prop, "RestElement");
  5505. }
  5506. return this.parseSpread();
  5507. }
  5508. if (decorators.length) {
  5509. prop.decorators = decorators;
  5510. decorators = [];
  5511. }
  5512. prop.method = false;
  5513. if (isPattern || refShorthandDefaultPos) {
  5514. startPos = this.state.start;
  5515. startLoc = this.state.startLoc;
  5516. }
  5517. if (!isPattern) {
  5518. isGenerator = this.eat(types.star);
  5519. }
  5520. const containsEsc = this.state.containsEsc;
  5521. this.parsePropertyName(prop);
  5522. if (!isPattern && !containsEsc && !isGenerator && this.isAsyncProp(prop)) {
  5523. isAsync = true;
  5524. isGenerator = this.eat(types.star);
  5525. this.parsePropertyName(prop);
  5526. } else {
  5527. isAsync = false;
  5528. }
  5529. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc);
  5530. return prop;
  5531. }
  5532. isGetterOrSetterMethod(prop, isPattern) {
  5533. 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);
  5534. }
  5535. getGetterSetterExpectedParamCount(method) {
  5536. return method.kind === "get" ? 0 : 1;
  5537. }
  5538. checkGetterSetterParams(method) {
  5539. const paramCount = this.getGetterSetterExpectedParamCount(method);
  5540. const start = method.start;
  5541. if (method.params.length !== paramCount) {
  5542. if (method.kind === "get") {
  5543. this.raise(start, "getter must not have any formal parameters");
  5544. } else {
  5545. this.raise(start, "setter must have exactly one formal parameter");
  5546. }
  5547. }
  5548. if (method.kind === "set" && method.params[method.params.length - 1].type === "RestElement") {
  5549. this.raise(start, "setter function argument must not be a rest parameter");
  5550. }
  5551. }
  5552. parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) {
  5553. if (isAsync || isGenerator || this.match(types.parenL)) {
  5554. if (isPattern) this.unexpected();
  5555. prop.kind = "method";
  5556. prop.method = true;
  5557. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  5558. }
  5559. if (!containsEsc && this.isGetterOrSetterMethod(prop, isPattern)) {
  5560. if (isGenerator || isAsync) this.unexpected();
  5561. prop.kind = prop.key.name;
  5562. this.parsePropertyName(prop);
  5563. this.parseMethod(prop, false, false, false, false, "ObjectMethod");
  5564. this.checkGetterSetterParams(prop);
  5565. return prop;
  5566. }
  5567. }
  5568. parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos) {
  5569. prop.shorthand = false;
  5570. if (this.eat(types.colon)) {
  5571. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssign(false, refShorthandDefaultPos);
  5572. return this.finishNode(prop, "ObjectProperty");
  5573. }
  5574. if (!prop.computed && prop.key.type === "Identifier") {
  5575. this.checkReservedWord(prop.key.name, prop.key.start, true, true);
  5576. if (isPattern) {
  5577. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  5578. } else if (this.match(types.eq) && refShorthandDefaultPos) {
  5579. if (!refShorthandDefaultPos.start) {
  5580. refShorthandDefaultPos.start = this.state.start;
  5581. }
  5582. prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
  5583. } else {
  5584. prop.value = prop.key.__clone();
  5585. }
  5586. prop.shorthand = true;
  5587. return this.finishNode(prop, "ObjectProperty");
  5588. }
  5589. }
  5590. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, refShorthandDefaultPos, containsEsc) {
  5591. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, containsEsc) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos);
  5592. if (!node) this.unexpected();
  5593. return node;
  5594. }
  5595. parsePropertyName(prop) {
  5596. if (this.eat(types.bracketL)) {
  5597. prop.computed = true;
  5598. prop.key = this.parseMaybeAssign();
  5599. this.expect(types.bracketR);
  5600. } else {
  5601. const oldInPropertyName = this.state.inPropertyName;
  5602. this.state.inPropertyName = true;
  5603. prop.key = this.match(types.num) || this.match(types.string) ? this.parseExprAtom() : this.parseMaybePrivateName();
  5604. if (prop.key.type !== "PrivateName") {
  5605. prop.computed = false;
  5606. }
  5607. this.state.inPropertyName = oldInPropertyName;
  5608. }
  5609. return prop.key;
  5610. }
  5611. initFunction(node, isAsync) {
  5612. node.id = null;
  5613. node.generator = false;
  5614. node.async = !!isAsync;
  5615. }
  5616. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  5617. const oldYieldPos = this.state.yieldPos;
  5618. const oldAwaitPos = this.state.awaitPos;
  5619. this.state.yieldPos = 0;
  5620. this.state.awaitPos = 0;
  5621. this.initFunction(node, isAsync);
  5622. node.generator = !!isGenerator;
  5623. const allowModifiers = isConstructor;
  5624. this.scope.enter(functionFlags(isAsync, node.generator) | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  5625. this.parseFunctionParams(node, allowModifiers);
  5626. this.checkYieldAwaitInDefaultParams();
  5627. this.parseFunctionBodyAndFinish(node, type, true);
  5628. this.state.yieldPos = oldYieldPos;
  5629. this.state.awaitPos = oldAwaitPos;
  5630. return node;
  5631. }
  5632. parseArrowExpression(node, params, isAsync) {
  5633. this.scope.enter(functionFlags(isAsync, false) | SCOPE_ARROW);
  5634. this.initFunction(node, isAsync);
  5635. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  5636. const oldYieldPos = this.state.yieldPos;
  5637. const oldAwaitPos = this.state.awaitPos;
  5638. this.state.maybeInArrowParameters = false;
  5639. this.state.yieldPos = 0;
  5640. this.state.awaitPos = 0;
  5641. if (params) this.setArrowFunctionParameters(node, params);
  5642. this.parseFunctionBody(node, true);
  5643. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  5644. this.state.yieldPos = oldYieldPos;
  5645. this.state.awaitPos = oldAwaitPos;
  5646. return this.finishNode(node, "ArrowFunctionExpression");
  5647. }
  5648. setArrowFunctionParameters(node, params) {
  5649. node.params = this.toAssignableList(params, true, "arrow function parameters");
  5650. }
  5651. isStrictBody(node) {
  5652. const isBlockStatement = node.body.type === "BlockStatement";
  5653. if (isBlockStatement && node.body.directives.length) {
  5654. for (let _i2 = 0, _node$body$directives = node.body.directives; _i2 < _node$body$directives.length; _i2++) {
  5655. const directive = _node$body$directives[_i2];
  5656. if (directive.value.value === "use strict") {
  5657. return true;
  5658. }
  5659. }
  5660. }
  5661. return false;
  5662. }
  5663. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5664. this.parseFunctionBody(node, false, isMethod);
  5665. this.finishNode(node, type);
  5666. }
  5667. parseFunctionBody(node, allowExpression, isMethod = false) {
  5668. const isExpression = allowExpression && !this.match(types.braceL);
  5669. const oldStrict = this.state.strict;
  5670. let useStrict = false;
  5671. const oldInParameters = this.state.inParameters;
  5672. this.state.inParameters = false;
  5673. if (isExpression) {
  5674. node.body = this.parseMaybeAssign();
  5675. this.checkParams(node, false, allowExpression);
  5676. } else {
  5677. const nonSimple = !this.isSimpleParamList(node.params);
  5678. if (!oldStrict || nonSimple) {
  5679. useStrict = this.strictDirective(this.state.end);
  5680. if (useStrict && nonSimple) {
  5681. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  5682. this.raise(errorPos, "Illegal 'use strict' directive in function with non-simple parameter list");
  5683. }
  5684. }
  5685. const oldLabels = this.state.labels;
  5686. this.state.labels = [];
  5687. if (useStrict) this.state.strict = true;
  5688. this.checkParams(node, !oldStrict && !useStrict && !allowExpression && !isMethod && !nonSimple, allowExpression);
  5689. node.body = this.parseBlock(true, false);
  5690. this.state.labels = oldLabels;
  5691. }
  5692. this.scope.exit();
  5693. this.state.inParameters = oldInParameters;
  5694. if (this.state.strict && node.id) {
  5695. this.checkLVal(node.id, BIND_OUTSIDE, undefined, "function name");
  5696. }
  5697. this.state.strict = oldStrict;
  5698. }
  5699. isSimpleParamList(params) {
  5700. for (let i = 0, len = params.length; i < len; i++) {
  5701. if (params[i].type !== "Identifier") return false;
  5702. }
  5703. return true;
  5704. }
  5705. checkParams(node, allowDuplicates, isArrowFunction) {
  5706. const nameHash = Object.create(null);
  5707. for (let i = 0; i < node.params.length; i++) {
  5708. this.checkLVal(node.params[i], BIND_VAR, allowDuplicates ? null : nameHash, "function paramter list");
  5709. }
  5710. }
  5711. parseExprList(close, allowEmpty, refShorthandDefaultPos) {
  5712. const elts = [];
  5713. let first = true;
  5714. while (!this.eat(close)) {
  5715. if (first) {
  5716. first = false;
  5717. } else {
  5718. this.expect(types.comma);
  5719. if (this.eat(close)) break;
  5720. }
  5721. elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
  5722. }
  5723. return elts;
  5724. }
  5725. parseExprListItem(allowEmpty, refShorthandDefaultPos, refNeedsArrowPos, allowPlaceholder) {
  5726. let elt;
  5727. if (allowEmpty && this.match(types.comma)) {
  5728. elt = null;
  5729. } else if (this.match(types.ellipsis)) {
  5730. const spreadNodeStartPos = this.state.start;
  5731. const spreadNodeStartLoc = this.state.startLoc;
  5732. elt = this.parseParenItem(this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
  5733. } else if (this.match(types.question)) {
  5734. this.expectPlugin("partialApplication");
  5735. if (!allowPlaceholder) {
  5736. this.raise(this.state.start, "Unexpected argument placeholder");
  5737. }
  5738. const node = this.startNode();
  5739. this.next();
  5740. elt = this.finishNode(node, "ArgumentPlaceholder");
  5741. } else {
  5742. elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos);
  5743. }
  5744. return elt;
  5745. }
  5746. parseIdentifier(liberal) {
  5747. const node = this.startNode();
  5748. const name = this.parseIdentifierName(node.start, liberal);
  5749. return this.createIdentifier(node, name);
  5750. }
  5751. createIdentifier(node, name) {
  5752. node.name = name;
  5753. node.loc.identifierName = name;
  5754. return this.finishNode(node, "Identifier");
  5755. }
  5756. parseIdentifierName(pos, liberal) {
  5757. let name;
  5758. if (this.match(types.name)) {
  5759. name = this.state.value;
  5760. } else if (this.state.type.keyword) {
  5761. name = this.state.type.keyword;
  5762. if ((name === "class" || name === "function") && (this.state.lastTokEnd !== this.state.lastTokStart + 1 || this.input.charCodeAt(this.state.lastTokStart) !== 46)) {
  5763. this.state.context.pop();
  5764. }
  5765. } else {
  5766. throw this.unexpected();
  5767. }
  5768. if (!liberal) {
  5769. this.checkReservedWord(name, this.state.start, !!this.state.type.keyword, false);
  5770. }
  5771. this.next();
  5772. return name;
  5773. }
  5774. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  5775. if (this.scope.inGenerator && word === "yield") {
  5776. this.raise(startLoc, "Can not use 'yield' as identifier inside a generator");
  5777. }
  5778. if (this.scope.inAsync && word === "await") {
  5779. this.raise(startLoc, "Can not use 'await' as identifier inside an async function");
  5780. }
  5781. if (this.state.inClassProperty && word === "arguments") {
  5782. this.raise(startLoc, "'arguments' is not allowed in class field initializer");
  5783. }
  5784. if (checkKeywords && isKeyword(word)) {
  5785. this.raise(startLoc, `Unexpected keyword '${word}'`);
  5786. }
  5787. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  5788. if (reservedTest(word, this.inModule)) {
  5789. if (!this.scope.inAsync && word === "await") {
  5790. this.raise(startLoc, "Can not use keyword 'await' outside an async function");
  5791. }
  5792. this.raise(startLoc, `Unexpected reserved word '${word}'`);
  5793. }
  5794. }
  5795. parseAwait() {
  5796. if (!this.state.awaitPos) {
  5797. this.state.awaitPos = this.state.start;
  5798. }
  5799. const node = this.startNode();
  5800. this.next();
  5801. if (this.state.inParameters) {
  5802. this.raise(node.start, "await is not allowed in async function parameters");
  5803. }
  5804. if (this.match(types.star)) {
  5805. this.raise(node.start, "await* has been removed from the async functions proposal. Use Promise.all() instead.");
  5806. }
  5807. node.argument = this.parseMaybeUnary();
  5808. return this.finishNode(node, "AwaitExpression");
  5809. }
  5810. parseYield(noIn) {
  5811. if (!this.state.yieldPos) {
  5812. this.state.yieldPos = this.state.start;
  5813. }
  5814. const node = this.startNode();
  5815. if (this.state.inParameters) {
  5816. this.raise(node.start, "yield is not allowed in generator parameters");
  5817. }
  5818. this.next();
  5819. if (this.match(types.semi) || !this.match(types.star) && !this.state.type.startsExpr || this.canInsertSemicolon()) {
  5820. node.delegate = false;
  5821. node.argument = null;
  5822. } else {
  5823. node.delegate = this.eat(types.star);
  5824. node.argument = this.parseMaybeAssign(noIn);
  5825. }
  5826. return this.finishNode(node, "YieldExpression");
  5827. }
  5828. checkPipelineAtInfixOperator(left, leftStartPos) {
  5829. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  5830. if (left.type === "SequenceExpression") {
  5831. throw this.raise(leftStartPos, `Pipeline head should not be a comma-separated sequence expression`);
  5832. }
  5833. }
  5834. }
  5835. parseSmartPipelineBody(childExpression, startPos, startLoc) {
  5836. const pipelineStyle = this.checkSmartPipelineBodyStyle(childExpression);
  5837. this.checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos);
  5838. return this.parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc);
  5839. }
  5840. checkSmartPipelineBodyEarlyErrors(childExpression, pipelineStyle, startPos) {
  5841. if (this.match(types.arrow)) {
  5842. throw this.raise(this.state.start, `Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized`);
  5843. } else if (pipelineStyle === "PipelineTopicExpression" && childExpression.type === "SequenceExpression") {
  5844. throw this.raise(startPos, `Pipeline body may not be a comma-separated sequence expression`);
  5845. }
  5846. }
  5847. parseSmartPipelineBodyInStyle(childExpression, pipelineStyle, startPos, startLoc) {
  5848. const bodyNode = this.startNodeAt(startPos, startLoc);
  5849. switch (pipelineStyle) {
  5850. case "PipelineBareFunction":
  5851. bodyNode.callee = childExpression;
  5852. break;
  5853. case "PipelineBareConstructor":
  5854. bodyNode.callee = childExpression.callee;
  5855. break;
  5856. case "PipelineBareAwaitedFunction":
  5857. bodyNode.callee = childExpression.argument;
  5858. break;
  5859. case "PipelineTopicExpression":
  5860. if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
  5861. throw this.raise(startPos, `Pipeline is in topic style but does not use topic reference`);
  5862. }
  5863. bodyNode.expression = childExpression;
  5864. break;
  5865. default:
  5866. throw this.raise(startPos, `Unknown pipeline style ${pipelineStyle}`);
  5867. }
  5868. return this.finishNode(bodyNode, pipelineStyle);
  5869. }
  5870. checkSmartPipelineBodyStyle(expression) {
  5871. switch (expression.type) {
  5872. default:
  5873. return this.isSimpleReference(expression) ? "PipelineBareFunction" : "PipelineTopicExpression";
  5874. }
  5875. }
  5876. isSimpleReference(expression) {
  5877. switch (expression.type) {
  5878. case "MemberExpression":
  5879. return !expression.computed && this.isSimpleReference(expression.object);
  5880. case "Identifier":
  5881. return true;
  5882. default:
  5883. return false;
  5884. }
  5885. }
  5886. withTopicPermittingContext(callback) {
  5887. const outerContextTopicState = this.state.topicContext;
  5888. this.state.topicContext = {
  5889. maxNumOfResolvableTopics: 1,
  5890. maxTopicIndex: null
  5891. };
  5892. try {
  5893. return callback();
  5894. } finally {
  5895. this.state.topicContext = outerContextTopicState;
  5896. }
  5897. }
  5898. withTopicForbiddingContext(callback) {
  5899. const outerContextTopicState = this.state.topicContext;
  5900. this.state.topicContext = {
  5901. maxNumOfResolvableTopics: 0,
  5902. maxTopicIndex: null
  5903. };
  5904. try {
  5905. return callback();
  5906. } finally {
  5907. this.state.topicContext = outerContextTopicState;
  5908. }
  5909. }
  5910. registerTopicReference() {
  5911. this.state.topicContext.maxTopicIndex = 0;
  5912. }
  5913. primaryTopicReferenceIsAllowedInCurrentTopicContext() {
  5914. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  5915. }
  5916. topicReferenceWasUsedInCurrentTopicContext() {
  5917. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  5918. }
  5919. }
  5920. const empty = [];
  5921. const loopLabel = {
  5922. kind: "loop"
  5923. },
  5924. switchLabel = {
  5925. kind: "switch"
  5926. };
  5927. const FUNC_NO_FLAGS = 0b000,
  5928. FUNC_STATEMENT = 0b001,
  5929. FUNC_HANGING_STATEMENT = 0b010,
  5930. FUNC_NULLABLE_ID = 0b100;
  5931. class StatementParser extends ExpressionParser {
  5932. parseTopLevel(file, program) {
  5933. program.sourceType = this.options.sourceType;
  5934. program.interpreter = this.parseInterpreterDirective();
  5935. this.parseBlockBody(program, true, true, types.eof);
  5936. if (this.inModule && this.scope.undefinedExports.size > 0) {
  5937. for (let _i = 0, _Array$from = Array.from(this.scope.undefinedExports); _i < _Array$from.length; _i++) {
  5938. const [name] = _Array$from[_i];
  5939. const pos = this.scope.undefinedExports.get(name);
  5940. this.raise(pos, `Export '${name}' is not defined`);
  5941. }
  5942. }
  5943. file.program = this.finishNode(program, "Program");
  5944. file.comments = this.state.comments;
  5945. if (this.options.tokens) file.tokens = this.state.tokens;
  5946. return this.finishNode(file, "File");
  5947. }
  5948. stmtToDirective(stmt) {
  5949. const expr = stmt.expression;
  5950. const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
  5951. const directive = this.startNodeAt(stmt.start, stmt.loc.start);
  5952. const raw = this.input.slice(expr.start, expr.end);
  5953. const val = directiveLiteral.value = raw.slice(1, -1);
  5954. this.addExtra(directiveLiteral, "raw", raw);
  5955. this.addExtra(directiveLiteral, "rawValue", val);
  5956. directive.value = this.finishNodeAt(directiveLiteral, "DirectiveLiteral", expr.end, expr.loc.end);
  5957. return this.finishNodeAt(directive, "Directive", stmt.end, stmt.loc.end);
  5958. }
  5959. parseInterpreterDirective() {
  5960. if (!this.match(types.interpreterDirective)) {
  5961. return null;
  5962. }
  5963. const node = this.startNode();
  5964. node.value = this.state.value;
  5965. this.next();
  5966. return this.finishNode(node, "InterpreterDirective");
  5967. }
  5968. isLet(context) {
  5969. if (!this.isContextual("let")) {
  5970. return false;
  5971. }
  5972. skipWhiteSpace.lastIndex = this.state.pos;
  5973. const skip = skipWhiteSpace.exec(this.input);
  5974. const next = this.state.pos + skip[0].length;
  5975. const nextCh = this.input.charCodeAt(next);
  5976. if (nextCh === 91) return true;
  5977. if (context) return false;
  5978. if (nextCh === 123) return true;
  5979. if (isIdentifierStart(nextCh)) {
  5980. let pos = next + 1;
  5981. while (isIdentifierChar(this.input.charCodeAt(pos))) {
  5982. ++pos;
  5983. }
  5984. const ident = this.input.slice(next, pos);
  5985. if (!keywordRelationalOperator.test(ident)) return true;
  5986. }
  5987. return false;
  5988. }
  5989. parseStatement(context, topLevel) {
  5990. if (this.match(types.at)) {
  5991. this.parseDecorators(true);
  5992. }
  5993. return this.parseStatementContent(context, topLevel);
  5994. }
  5995. parseStatementContent(context, topLevel) {
  5996. let starttype = this.state.type;
  5997. const node = this.startNode();
  5998. let kind;
  5999. if (this.isLet(context)) {
  6000. starttype = types._var;
  6001. kind = "let";
  6002. }
  6003. switch (starttype) {
  6004. case types._break:
  6005. case types._continue:
  6006. return this.parseBreakContinueStatement(node, starttype.keyword);
  6007. case types._debugger:
  6008. return this.parseDebuggerStatement(node);
  6009. case types._do:
  6010. return this.parseDoStatement(node);
  6011. case types._for:
  6012. return this.parseForStatement(node);
  6013. case types._function:
  6014. if (this.lookahead().type === types.dot) break;
  6015. if (context) {
  6016. if (this.state.strict) {
  6017. this.raise(this.state.start, "In strict mode code, functions can only be declared at top level or inside a block");
  6018. } else if (context !== "if" && context !== "label") {
  6019. 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");
  6020. }
  6021. }
  6022. return this.parseFunctionStatement(node, false, !context);
  6023. case types._class:
  6024. if (context) this.unexpected();
  6025. return this.parseClass(node, true);
  6026. case types._if:
  6027. return this.parseIfStatement(node);
  6028. case types._return:
  6029. return this.parseReturnStatement(node);
  6030. case types._switch:
  6031. return this.parseSwitchStatement(node);
  6032. case types._throw:
  6033. return this.parseThrowStatement(node);
  6034. case types._try:
  6035. return this.parseTryStatement(node);
  6036. case types._const:
  6037. case types._var:
  6038. kind = kind || this.state.value;
  6039. if (context && kind !== "var") {
  6040. this.unexpected(this.state.start, "Lexical declaration cannot appear in a single-statement context");
  6041. }
  6042. return this.parseVarStatement(node, kind);
  6043. case types._while:
  6044. return this.parseWhileStatement(node);
  6045. case types._with:
  6046. return this.parseWithStatement(node);
  6047. case types.braceL:
  6048. return this.parseBlock();
  6049. case types.semi:
  6050. return this.parseEmptyStatement(node);
  6051. case types._export:
  6052. case types._import:
  6053. {
  6054. const nextToken = this.lookahead();
  6055. if (nextToken.type === types.parenL || nextToken.type === types.dot) {
  6056. break;
  6057. }
  6058. if (!this.options.allowImportExportEverywhere && !topLevel) {
  6059. this.raise(this.state.start, "'import' and 'export' may only appear at the top level");
  6060. }
  6061. this.next();
  6062. let result;
  6063. if (starttype === types._import) {
  6064. result = this.parseImport(node);
  6065. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  6066. this.sawUnambiguousESM = true;
  6067. }
  6068. } else {
  6069. result = this.parseExport(node);
  6070. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  6071. this.sawUnambiguousESM = true;
  6072. }
  6073. }
  6074. this.assertModuleNodeAllowed(node);
  6075. return result;
  6076. }
  6077. default:
  6078. {
  6079. if (this.isAsyncFunction()) {
  6080. if (context) {
  6081. this.unexpected(null, "Async functions can only be declared at the top level or inside a block");
  6082. }
  6083. this.next();
  6084. return this.parseFunctionStatement(node, true, !context);
  6085. }
  6086. }
  6087. }
  6088. const maybeName = this.state.value;
  6089. const expr = this.parseExpression();
  6090. if (starttype === types.name && expr.type === "Identifier" && this.eat(types.colon)) {
  6091. return this.parseLabeledStatement(node, maybeName, expr, context);
  6092. } else {
  6093. return this.parseExpressionStatement(node, expr);
  6094. }
  6095. }
  6096. assertModuleNodeAllowed(node) {
  6097. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  6098. this.raise(node.start, `'import' and 'export' may appear only with 'sourceType: "module"'`, {
  6099. code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  6100. });
  6101. }
  6102. }
  6103. takeDecorators(node) {
  6104. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  6105. if (decorators.length) {
  6106. node.decorators = decorators;
  6107. this.resetStartLocationFromNode(node, decorators[0]);
  6108. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  6109. }
  6110. }
  6111. canHaveLeadingDecorator() {
  6112. return this.match(types._class);
  6113. }
  6114. parseDecorators(allowExport) {
  6115. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  6116. while (this.match(types.at)) {
  6117. const decorator = this.parseDecorator();
  6118. currentContextDecorators.push(decorator);
  6119. }
  6120. if (this.match(types._export)) {
  6121. if (!allowExport) {
  6122. this.unexpected();
  6123. }
  6124. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  6125. this.raise(this.state.start, "Using the export keyword between a decorator and a class is not allowed. " + "Please use `export @dec class` instead.");
  6126. }
  6127. } else if (!this.canHaveLeadingDecorator()) {
  6128. this.raise(this.state.start, "Leading decorators must be attached to a class declaration");
  6129. }
  6130. }
  6131. parseDecorator() {
  6132. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  6133. const node = this.startNode();
  6134. this.next();
  6135. if (this.hasPlugin("decorators")) {
  6136. this.state.decoratorStack.push([]);
  6137. const startPos = this.state.start;
  6138. const startLoc = this.state.startLoc;
  6139. let expr;
  6140. if (this.eat(types.parenL)) {
  6141. expr = this.parseExpression();
  6142. this.expect(types.parenR);
  6143. } else {
  6144. expr = this.parseIdentifier(false);
  6145. while (this.eat(types.dot)) {
  6146. const node = this.startNodeAt(startPos, startLoc);
  6147. node.object = expr;
  6148. node.property = this.parseIdentifier(true);
  6149. node.computed = false;
  6150. expr = this.finishNode(node, "MemberExpression");
  6151. }
  6152. }
  6153. node.expression = this.parseMaybeDecoratorArguments(expr);
  6154. this.state.decoratorStack.pop();
  6155. } else {
  6156. node.expression = this.parseMaybeAssign();
  6157. }
  6158. return this.finishNode(node, "Decorator");
  6159. }
  6160. parseMaybeDecoratorArguments(expr) {
  6161. if (this.eat(types.parenL)) {
  6162. const node = this.startNodeAtNode(expr);
  6163. node.callee = expr;
  6164. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  6165. this.toReferencedList(node.arguments);
  6166. return this.finishNode(node, "CallExpression");
  6167. }
  6168. return expr;
  6169. }
  6170. parseBreakContinueStatement(node, keyword) {
  6171. const isBreak = keyword === "break";
  6172. this.next();
  6173. if (this.isLineTerminator()) {
  6174. node.label = null;
  6175. } else {
  6176. node.label = this.parseIdentifier();
  6177. this.semicolon();
  6178. }
  6179. this.verifyBreakContinue(node, keyword);
  6180. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  6181. }
  6182. verifyBreakContinue(node, keyword) {
  6183. const isBreak = keyword === "break";
  6184. let i;
  6185. for (i = 0; i < this.state.labels.length; ++i) {
  6186. const lab = this.state.labels[i];
  6187. if (node.label == null || lab.name === node.label.name) {
  6188. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  6189. if (node.label && isBreak) break;
  6190. }
  6191. }
  6192. if (i === this.state.labels.length) {
  6193. this.raise(node.start, "Unsyntactic " + keyword);
  6194. }
  6195. }
  6196. parseDebuggerStatement(node) {
  6197. this.next();
  6198. this.semicolon();
  6199. return this.finishNode(node, "DebuggerStatement");
  6200. }
  6201. parseHeaderExpression() {
  6202. this.expect(types.parenL);
  6203. const val = this.parseExpression();
  6204. this.expect(types.parenR);
  6205. return val;
  6206. }
  6207. parseDoStatement(node) {
  6208. this.next();
  6209. this.state.labels.push(loopLabel);
  6210. node.body = this.withTopicForbiddingContext(() => this.parseStatement("do"));
  6211. this.state.labels.pop();
  6212. this.expect(types._while);
  6213. node.test = this.parseHeaderExpression();
  6214. this.eat(types.semi);
  6215. return this.finishNode(node, "DoWhileStatement");
  6216. }
  6217. parseForStatement(node) {
  6218. this.next();
  6219. this.state.labels.push(loopLabel);
  6220. let awaitAt = -1;
  6221. if ((this.scope.inAsync || !this.scope.inFunction && this.options.allowAwaitOutsideFunction) && this.eatContextual("await")) {
  6222. awaitAt = this.state.lastTokStart;
  6223. }
  6224. this.scope.enter(SCOPE_OTHER);
  6225. this.expect(types.parenL);
  6226. if (this.match(types.semi)) {
  6227. if (awaitAt > -1) {
  6228. this.unexpected(awaitAt);
  6229. }
  6230. return this.parseFor(node, null);
  6231. }
  6232. const isLet = this.isLet();
  6233. if (this.match(types._var) || this.match(types._const) || isLet) {
  6234. const init = this.startNode();
  6235. const kind = isLet ? "let" : this.state.value;
  6236. this.next();
  6237. this.parseVar(init, true, kind);
  6238. this.finishNode(init, "VariableDeclaration");
  6239. if (this.match(types._in) || this.isContextual("of")) {
  6240. if (init.declarations.length === 1) {
  6241. const declaration = init.declarations[0];
  6242. const isForInInitializer = kind === "var" && declaration.init && declaration.id.type != "ObjectPattern" && declaration.id.type != "ArrayPattern" && !this.isContextual("of");
  6243. if (this.state.strict && isForInInitializer) {
  6244. this.raise(this.state.start, "for-in initializer in strict mode");
  6245. } else if (isForInInitializer || !declaration.init) {
  6246. return this.parseForIn(node, init, awaitAt);
  6247. }
  6248. }
  6249. }
  6250. if (awaitAt > -1) {
  6251. this.unexpected(awaitAt);
  6252. }
  6253. return this.parseFor(node, init);
  6254. }
  6255. const refShorthandDefaultPos = {
  6256. start: 0
  6257. };
  6258. const init = this.parseExpression(true, refShorthandDefaultPos);
  6259. if (this.match(types._in) || this.isContextual("of")) {
  6260. const description = this.isContextual("of") ? "for-of statement" : "for-in statement";
  6261. this.toAssignable(init, undefined, description);
  6262. this.checkLVal(init, undefined, undefined, description);
  6263. return this.parseForIn(node, init, awaitAt);
  6264. } else if (refShorthandDefaultPos.start) {
  6265. this.unexpected(refShorthandDefaultPos.start);
  6266. }
  6267. if (awaitAt > -1) {
  6268. this.unexpected(awaitAt);
  6269. }
  6270. return this.parseFor(node, init);
  6271. }
  6272. parseFunctionStatement(node, isAsync, declarationPosition) {
  6273. this.next();
  6274. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  6275. }
  6276. parseIfStatement(node) {
  6277. this.next();
  6278. node.test = this.parseHeaderExpression();
  6279. node.consequent = this.parseStatement("if");
  6280. node.alternate = this.eat(types._else) ? this.parseStatement("if") : null;
  6281. return this.finishNode(node, "IfStatement");
  6282. }
  6283. parseReturnStatement(node) {
  6284. if (!this.scope.inFunction && !this.options.allowReturnOutsideFunction) {
  6285. this.raise(this.state.start, "'return' outside of function");
  6286. }
  6287. this.next();
  6288. if (this.isLineTerminator()) {
  6289. node.argument = null;
  6290. } else {
  6291. node.argument = this.parseExpression();
  6292. this.semicolon();
  6293. }
  6294. return this.finishNode(node, "ReturnStatement");
  6295. }
  6296. parseSwitchStatement(node) {
  6297. this.next();
  6298. node.discriminant = this.parseHeaderExpression();
  6299. const cases = node.cases = [];
  6300. this.expect(types.braceL);
  6301. this.state.labels.push(switchLabel);
  6302. this.scope.enter(SCOPE_OTHER);
  6303. let cur;
  6304. for (let sawDefault; !this.match(types.braceR);) {
  6305. if (this.match(types._case) || this.match(types._default)) {
  6306. const isCase = this.match(types._case);
  6307. if (cur) this.finishNode(cur, "SwitchCase");
  6308. cases.push(cur = this.startNode());
  6309. cur.consequent = [];
  6310. this.next();
  6311. if (isCase) {
  6312. cur.test = this.parseExpression();
  6313. } else {
  6314. if (sawDefault) {
  6315. this.raise(this.state.lastTokStart, "Multiple default clauses");
  6316. }
  6317. sawDefault = true;
  6318. cur.test = null;
  6319. }
  6320. this.expect(types.colon);
  6321. } else {
  6322. if (cur) {
  6323. cur.consequent.push(this.parseStatement(null));
  6324. } else {
  6325. this.unexpected();
  6326. }
  6327. }
  6328. }
  6329. this.scope.exit();
  6330. if (cur) this.finishNode(cur, "SwitchCase");
  6331. this.next();
  6332. this.state.labels.pop();
  6333. return this.finishNode(node, "SwitchStatement");
  6334. }
  6335. parseThrowStatement(node) {
  6336. this.next();
  6337. if (lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))) {
  6338. this.raise(this.state.lastTokEnd, "Illegal newline after throw");
  6339. }
  6340. node.argument = this.parseExpression();
  6341. this.semicolon();
  6342. return this.finishNode(node, "ThrowStatement");
  6343. }
  6344. parseTryStatement(node) {
  6345. this.next();
  6346. node.block = this.parseBlock();
  6347. node.handler = null;
  6348. if (this.match(types._catch)) {
  6349. const clause = this.startNode();
  6350. this.next();
  6351. if (this.match(types.parenL)) {
  6352. this.expect(types.parenL);
  6353. clause.param = this.parseBindingAtom();
  6354. const simple = clause.param.type === "Identifier";
  6355. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  6356. this.checkLVal(clause.param, simple ? BIND_SIMPLE_CATCH : BIND_LEXICAL, null, "catch clause");
  6357. this.expect(types.parenR);
  6358. } else {
  6359. clause.param = null;
  6360. this.scope.enter(SCOPE_OTHER);
  6361. }
  6362. clause.body = this.withTopicForbiddingContext(() => this.parseBlock(false, false));
  6363. this.scope.exit();
  6364. node.handler = this.finishNode(clause, "CatchClause");
  6365. }
  6366. node.guardedHandlers = empty;
  6367. node.finalizer = this.eat(types._finally) ? this.parseBlock() : null;
  6368. if (!node.handler && !node.finalizer) {
  6369. this.raise(node.start, "Missing catch or finally clause");
  6370. }
  6371. return this.finishNode(node, "TryStatement");
  6372. }
  6373. parseVarStatement(node, kind) {
  6374. this.next();
  6375. this.parseVar(node, false, kind);
  6376. this.semicolon();
  6377. return this.finishNode(node, "VariableDeclaration");
  6378. }
  6379. parseWhileStatement(node) {
  6380. this.next();
  6381. node.test = this.parseHeaderExpression();
  6382. this.state.labels.push(loopLabel);
  6383. node.body = this.withTopicForbiddingContext(() => this.parseStatement("while"));
  6384. this.state.labels.pop();
  6385. return this.finishNode(node, "WhileStatement");
  6386. }
  6387. parseWithStatement(node) {
  6388. if (this.state.strict) {
  6389. this.raise(this.state.start, "'with' in strict mode");
  6390. }
  6391. this.next();
  6392. node.object = this.parseHeaderExpression();
  6393. node.body = this.withTopicForbiddingContext(() => this.parseStatement("with"));
  6394. return this.finishNode(node, "WithStatement");
  6395. }
  6396. parseEmptyStatement(node) {
  6397. this.next();
  6398. return this.finishNode(node, "EmptyStatement");
  6399. }
  6400. parseLabeledStatement(node, maybeName, expr, context) {
  6401. for (let _i2 = 0, _this$state$labels = this.state.labels; _i2 < _this$state$labels.length; _i2++) {
  6402. const label = _this$state$labels[_i2];
  6403. if (label.name === maybeName) {
  6404. this.raise(expr.start, `Label '${maybeName}' is already declared`);
  6405. }
  6406. }
  6407. const kind = this.state.type.isLoop ? "loop" : this.match(types._switch) ? "switch" : null;
  6408. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  6409. const label = this.state.labels[i];
  6410. if (label.statementStart === node.start) {
  6411. label.statementStart = this.state.start;
  6412. label.kind = kind;
  6413. } else {
  6414. break;
  6415. }
  6416. }
  6417. this.state.labels.push({
  6418. name: maybeName,
  6419. kind: kind,
  6420. statementStart: this.state.start
  6421. });
  6422. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  6423. this.state.labels.pop();
  6424. node.label = expr;
  6425. return this.finishNode(node, "LabeledStatement");
  6426. }
  6427. parseExpressionStatement(node, expr) {
  6428. node.expression = expr;
  6429. this.semicolon();
  6430. return this.finishNode(node, "ExpressionStatement");
  6431. }
  6432. parseBlock(allowDirectives = false, createNewLexicalScope = true) {
  6433. const node = this.startNode();
  6434. this.expect(types.braceL);
  6435. if (createNewLexicalScope) {
  6436. this.scope.enter(SCOPE_OTHER);
  6437. }
  6438. this.parseBlockBody(node, allowDirectives, false, types.braceR);
  6439. if (createNewLexicalScope) {
  6440. this.scope.exit();
  6441. }
  6442. return this.finishNode(node, "BlockStatement");
  6443. }
  6444. isValidDirective(stmt) {
  6445. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  6446. }
  6447. parseBlockBody(node, allowDirectives, topLevel, end) {
  6448. const body = node.body = [];
  6449. const directives = node.directives = [];
  6450. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end);
  6451. }
  6452. parseBlockOrModuleBlockBody(body, directives, topLevel, end) {
  6453. let parsedNonDirective = false;
  6454. let oldStrict;
  6455. let octalPosition;
  6456. while (!this.eat(end)) {
  6457. if (!parsedNonDirective && this.state.containsOctal && !octalPosition) {
  6458. octalPosition = this.state.octalPosition;
  6459. }
  6460. const stmt = this.parseStatement(null, topLevel);
  6461. if (directives && !parsedNonDirective && this.isValidDirective(stmt)) {
  6462. const directive = this.stmtToDirective(stmt);
  6463. directives.push(directive);
  6464. if (oldStrict === undefined && directive.value.value === "use strict") {
  6465. oldStrict = this.state.strict;
  6466. this.setStrict(true);
  6467. if (octalPosition) {
  6468. this.raise(octalPosition, "Octal literal in strict mode");
  6469. }
  6470. }
  6471. continue;
  6472. }
  6473. parsedNonDirective = true;
  6474. body.push(stmt);
  6475. }
  6476. if (oldStrict === false) {
  6477. this.setStrict(false);
  6478. }
  6479. }
  6480. parseFor(node, init) {
  6481. node.init = init;
  6482. this.expect(types.semi);
  6483. node.test = this.match(types.semi) ? null : this.parseExpression();
  6484. this.expect(types.semi);
  6485. node.update = this.match(types.parenR) ? null : this.parseExpression();
  6486. this.expect(types.parenR);
  6487. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  6488. this.scope.exit();
  6489. this.state.labels.pop();
  6490. return this.finishNode(node, "ForStatement");
  6491. }
  6492. parseForIn(node, init, awaitAt) {
  6493. const type = this.match(types._in) ? "ForInStatement" : "ForOfStatement";
  6494. if (awaitAt > -1) {
  6495. this.eatContextual("of");
  6496. } else {
  6497. this.next();
  6498. }
  6499. if (type === "ForOfStatement") {
  6500. node.await = awaitAt > -1;
  6501. }
  6502. node.left = init;
  6503. node.right = this.parseExpression();
  6504. this.expect(types.parenR);
  6505. node.body = this.withTopicForbiddingContext(() => this.parseStatement("for"));
  6506. this.scope.exit();
  6507. this.state.labels.pop();
  6508. return this.finishNode(node, type);
  6509. }
  6510. parseVar(node, isFor, kind) {
  6511. const declarations = node.declarations = [];
  6512. const isTypescript = this.hasPlugin("typescript");
  6513. node.kind = kind;
  6514. for (;;) {
  6515. const decl = this.startNode();
  6516. this.parseVarId(decl, kind);
  6517. if (this.eat(types.eq)) {
  6518. decl.init = this.parseMaybeAssign(isFor);
  6519. } else {
  6520. if (kind === "const" && !(this.match(types._in) || this.isContextual("of"))) {
  6521. if (!isTypescript) {
  6522. this.unexpected();
  6523. }
  6524. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types._in) || this.isContextual("of")))) {
  6525. this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value");
  6526. }
  6527. decl.init = null;
  6528. }
  6529. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  6530. if (!this.eat(types.comma)) break;
  6531. }
  6532. return node;
  6533. }
  6534. parseVarId(decl, kind) {
  6535. if ((kind === "const" || kind === "let") && this.isContextual("let")) {
  6536. this.unexpected(null, "let is disallowed as a lexically bound name");
  6537. }
  6538. decl.id = this.parseBindingAtom();
  6539. this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, "variable declaration");
  6540. }
  6541. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  6542. const isStatement = statement & FUNC_STATEMENT;
  6543. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  6544. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  6545. this.initFunction(node, isAsync);
  6546. if (this.match(types.star) && isHangingStatement) {
  6547. this.unexpected(this.state.start, "Generators can only be declared at the top level or inside a block");
  6548. }
  6549. node.generator = this.eat(types.star);
  6550. if (isStatement) {
  6551. node.id = this.parseFunctionId(requireId);
  6552. if (node.id && !isHangingStatement) {
  6553. this.checkLVal(node.id, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, null, "function name");
  6554. }
  6555. }
  6556. const oldInClassProperty = this.state.inClassProperty;
  6557. const oldYieldPos = this.state.yieldPos;
  6558. const oldAwaitPos = this.state.awaitPos;
  6559. this.state.inClassProperty = false;
  6560. this.state.yieldPos = 0;
  6561. this.state.awaitPos = 0;
  6562. this.scope.enter(functionFlags(node.async, node.generator));
  6563. if (!isStatement) {
  6564. node.id = this.parseFunctionId();
  6565. }
  6566. this.parseFunctionParams(node);
  6567. this.withTopicForbiddingContext(() => {
  6568. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  6569. });
  6570. this.state.inClassProperty = oldInClassProperty;
  6571. this.state.yieldPos = oldYieldPos;
  6572. this.state.awaitPos = oldAwaitPos;
  6573. return node;
  6574. }
  6575. parseFunctionId(requireId) {
  6576. return requireId || this.match(types.name) ? this.parseIdentifier() : null;
  6577. }
  6578. parseFunctionParams(node, allowModifiers) {
  6579. const oldInParameters = this.state.inParameters;
  6580. this.state.inParameters = true;
  6581. this.expect(types.parenL);
  6582. node.params = this.parseBindingList(types.parenR, false, allowModifiers);
  6583. this.state.inParameters = oldInParameters;
  6584. this.checkYieldAwaitInDefaultParams();
  6585. }
  6586. parseClass(node, isStatement, optionalId) {
  6587. this.next();
  6588. this.takeDecorators(node);
  6589. const oldStrict = this.state.strict;
  6590. this.state.strict = true;
  6591. this.parseClassId(node, isStatement, optionalId);
  6592. this.parseClassSuper(node);
  6593. node.body = this.parseClassBody(!!node.superClass);
  6594. this.state.strict = oldStrict;
  6595. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  6596. }
  6597. isClassProperty() {
  6598. return this.match(types.eq) || this.match(types.semi) || this.match(types.braceR);
  6599. }
  6600. isClassMethod() {
  6601. return this.match(types.parenL);
  6602. }
  6603. isNonstaticConstructor(method) {
  6604. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  6605. }
  6606. parseClassBody(constructorAllowsSuper) {
  6607. this.state.classLevel++;
  6608. const state = {
  6609. hadConstructor: false
  6610. };
  6611. let decorators = [];
  6612. const classBody = this.startNode();
  6613. classBody.body = [];
  6614. this.expect(types.braceL);
  6615. this.withTopicForbiddingContext(() => {
  6616. while (!this.eat(types.braceR)) {
  6617. if (this.eat(types.semi)) {
  6618. if (decorators.length > 0) {
  6619. this.raise(this.state.lastTokEnd, "Decorators must not be followed by a semicolon");
  6620. }
  6621. continue;
  6622. }
  6623. if (this.match(types.at)) {
  6624. decorators.push(this.parseDecorator());
  6625. continue;
  6626. }
  6627. const member = this.startNode();
  6628. if (decorators.length) {
  6629. member.decorators = decorators;
  6630. this.resetStartLocationFromNode(member, decorators[0]);
  6631. decorators = [];
  6632. }
  6633. this.parseClassMember(classBody, member, state, constructorAllowsSuper);
  6634. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  6635. this.raise(member.start, "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?");
  6636. }
  6637. }
  6638. });
  6639. if (decorators.length) {
  6640. this.raise(this.state.start, "You have trailing decorators with no method");
  6641. }
  6642. this.state.classLevel--;
  6643. return this.finishNode(classBody, "ClassBody");
  6644. }
  6645. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  6646. let isStatic = false;
  6647. const containsEsc = this.state.containsEsc;
  6648. if (this.match(types.name) && this.state.value === "static") {
  6649. const key = this.parseIdentifier(true);
  6650. if (this.isClassMethod()) {
  6651. const method = member;
  6652. method.kind = "method";
  6653. method.computed = false;
  6654. method.key = key;
  6655. method.static = false;
  6656. this.pushClassMethod(classBody, method, false, false, false, false);
  6657. return;
  6658. } else if (this.isClassProperty()) {
  6659. const prop = member;
  6660. prop.computed = false;
  6661. prop.key = key;
  6662. prop.static = false;
  6663. classBody.body.push(this.parseClassProperty(prop));
  6664. return;
  6665. } else if (containsEsc) {
  6666. throw this.unexpected();
  6667. }
  6668. isStatic = true;
  6669. }
  6670. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  6671. }
  6672. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  6673. const publicMethod = member;
  6674. const privateMethod = member;
  6675. const publicProp = member;
  6676. const privateProp = member;
  6677. const method = publicMethod;
  6678. const publicMember = publicMethod;
  6679. member.static = isStatic;
  6680. if (this.eat(types.star)) {
  6681. method.kind = "method";
  6682. this.parseClassPropertyName(method);
  6683. if (method.key.type === "PrivateName") {
  6684. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  6685. return;
  6686. }
  6687. if (this.isNonstaticConstructor(publicMethod)) {
  6688. this.raise(publicMethod.key.start, "Constructor can't be a generator");
  6689. }
  6690. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  6691. return;
  6692. }
  6693. const containsEsc = this.state.containsEsc;
  6694. const key = this.parseClassPropertyName(member);
  6695. const isPrivate = key.type === "PrivateName";
  6696. const isSimple = key.type === "Identifier";
  6697. this.parsePostMemberNameModifiers(publicMember);
  6698. if (this.isClassMethod()) {
  6699. method.kind = "method";
  6700. if (isPrivate) {
  6701. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  6702. return;
  6703. }
  6704. const isConstructor = this.isNonstaticConstructor(publicMethod);
  6705. let allowsDirectSuper = false;
  6706. if (isConstructor) {
  6707. publicMethod.kind = "constructor";
  6708. if (publicMethod.decorators) {
  6709. this.raise(publicMethod.start, "You can't attach decorators to a class constructor");
  6710. }
  6711. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  6712. this.raise(key.start, "Duplicate constructor in the same class");
  6713. }
  6714. state.hadConstructor = true;
  6715. allowsDirectSuper = constructorAllowsSuper;
  6716. }
  6717. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  6718. } else if (this.isClassProperty()) {
  6719. if (isPrivate) {
  6720. this.pushClassPrivateProperty(classBody, privateProp);
  6721. } else {
  6722. this.pushClassProperty(classBody, publicProp);
  6723. }
  6724. } else if (isSimple && key.name === "async" && !containsEsc && !this.isLineTerminator()) {
  6725. const isGenerator = this.eat(types.star);
  6726. method.kind = "method";
  6727. this.parseClassPropertyName(method);
  6728. if (method.key.type === "PrivateName") {
  6729. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  6730. } else {
  6731. if (this.isNonstaticConstructor(publicMethod)) {
  6732. this.raise(publicMethod.key.start, "Constructor can't be an async function");
  6733. }
  6734. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  6735. }
  6736. } else if (isSimple && (key.name === "get" || key.name === "set") && !containsEsc && !(this.match(types.star) && this.isLineTerminator())) {
  6737. method.kind = key.name;
  6738. this.parseClassPropertyName(publicMethod);
  6739. if (method.key.type === "PrivateName") {
  6740. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  6741. } else {
  6742. if (this.isNonstaticConstructor(publicMethod)) {
  6743. this.raise(publicMethod.key.start, "Constructor can't have get/set modifier");
  6744. }
  6745. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  6746. }
  6747. this.checkGetterSetterParams(publicMethod);
  6748. } else if (this.isLineTerminator()) {
  6749. if (isPrivate) {
  6750. this.pushClassPrivateProperty(classBody, privateProp);
  6751. } else {
  6752. this.pushClassProperty(classBody, publicProp);
  6753. }
  6754. } else {
  6755. this.unexpected();
  6756. }
  6757. }
  6758. parseClassPropertyName(member) {
  6759. const key = this.parsePropertyName(member);
  6760. if (!member.computed && member.static && (key.name === "prototype" || key.value === "prototype")) {
  6761. this.raise(key.start, "Classes may not have static property named prototype");
  6762. }
  6763. if (key.type === "PrivateName" && key.id.name === "constructor") {
  6764. this.raise(key.start, "Classes may not have a private field named '#constructor'");
  6765. }
  6766. return key;
  6767. }
  6768. pushClassProperty(classBody, prop) {
  6769. if (this.isNonstaticConstructor(prop)) {
  6770. this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'");
  6771. }
  6772. classBody.body.push(this.parseClassProperty(prop));
  6773. }
  6774. pushClassPrivateProperty(classBody, prop) {
  6775. this.expectPlugin("classPrivateProperties", prop.key.start);
  6776. classBody.body.push(this.parseClassPrivateProperty(prop));
  6777. }
  6778. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  6779. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  6780. }
  6781. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  6782. this.expectPlugin("classPrivateMethods", method.key.start);
  6783. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true));
  6784. }
  6785. parsePostMemberNameModifiers(methodOrProp) {}
  6786. parseAccessModifier() {
  6787. return undefined;
  6788. }
  6789. parseClassPrivateProperty(node) {
  6790. this.state.inClassProperty = true;
  6791. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  6792. node.value = this.eat(types.eq) ? this.parseMaybeAssign() : null;
  6793. this.semicolon();
  6794. this.state.inClassProperty = false;
  6795. this.scope.exit();
  6796. return this.finishNode(node, "ClassPrivateProperty");
  6797. }
  6798. parseClassProperty(node) {
  6799. if (!node.typeAnnotation) {
  6800. this.expectPlugin("classProperties");
  6801. }
  6802. this.state.inClassProperty = true;
  6803. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  6804. if (this.match(types.eq)) {
  6805. this.expectPlugin("classProperties");
  6806. this.next();
  6807. node.value = this.parseMaybeAssign();
  6808. } else {
  6809. node.value = null;
  6810. }
  6811. this.semicolon();
  6812. this.state.inClassProperty = false;
  6813. this.scope.exit();
  6814. return this.finishNode(node, "ClassProperty");
  6815. }
  6816. parseClassId(node, isStatement, optionalId) {
  6817. if (this.match(types.name)) {
  6818. node.id = this.parseIdentifier();
  6819. if (isStatement) {
  6820. this.checkLVal(node.id, BIND_LEXICAL, undefined, "class name");
  6821. }
  6822. } else {
  6823. if (optionalId || !isStatement) {
  6824. node.id = null;
  6825. } else {
  6826. this.unexpected(null, "A class name is required");
  6827. }
  6828. }
  6829. }
  6830. parseClassSuper(node) {
  6831. node.superClass = this.eat(types._extends) ? this.parseExprSubscripts() : null;
  6832. }
  6833. parseExport(node) {
  6834. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  6835. const parseAfterDefault = !hasDefault || this.eat(types.comma);
  6836. const hasStar = parseAfterDefault && this.eatExportStar(node);
  6837. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  6838. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types.comma));
  6839. const isFromRequired = hasDefault || hasStar;
  6840. if (hasStar && !hasNamespace) {
  6841. if (hasDefault) this.unexpected();
  6842. this.parseExportFrom(node, true);
  6843. return this.finishNode(node, "ExportAllDeclaration");
  6844. }
  6845. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  6846. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  6847. throw this.unexpected(null, types.braceL);
  6848. }
  6849. let hasDeclaration;
  6850. if (isFromRequired || hasSpecifiers) {
  6851. hasDeclaration = false;
  6852. this.parseExportFrom(node, isFromRequired);
  6853. } else {
  6854. hasDeclaration = this.maybeParseExportDeclaration(node);
  6855. }
  6856. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  6857. this.checkExport(node, true, false, !!node.source);
  6858. return this.finishNode(node, "ExportNamedDeclaration");
  6859. }
  6860. if (this.eat(types._default)) {
  6861. node.declaration = this.parseExportDefaultExpression();
  6862. this.checkExport(node, true, true);
  6863. return this.finishNode(node, "ExportDefaultDeclaration");
  6864. }
  6865. throw this.unexpected(null, types.braceL);
  6866. }
  6867. eatExportStar(node) {
  6868. return this.eat(types.star);
  6869. }
  6870. maybeParseExportDefaultSpecifier(node) {
  6871. if (this.isExportDefaultSpecifier()) {
  6872. this.expectPlugin("exportDefaultFrom");
  6873. const specifier = this.startNode();
  6874. specifier.exported = this.parseIdentifier(true);
  6875. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  6876. return true;
  6877. }
  6878. return false;
  6879. }
  6880. maybeParseExportNamespaceSpecifier(node) {
  6881. if (this.isContextual("as")) {
  6882. if (!node.specifiers) node.specifiers = [];
  6883. this.expectPlugin("exportNamespaceFrom");
  6884. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  6885. this.next();
  6886. specifier.exported = this.parseIdentifier(true);
  6887. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  6888. return true;
  6889. }
  6890. return false;
  6891. }
  6892. maybeParseExportNamedSpecifiers(node) {
  6893. if (this.match(types.braceL)) {
  6894. if (!node.specifiers) node.specifiers = [];
  6895. node.specifiers.push(...this.parseExportSpecifiers());
  6896. node.source = null;
  6897. node.declaration = null;
  6898. return true;
  6899. }
  6900. return false;
  6901. }
  6902. maybeParseExportDeclaration(node) {
  6903. if (this.shouldParseExportDeclaration()) {
  6904. if (this.isContextual("async")) {
  6905. const next = this.lookahead();
  6906. if (next.type !== types._function) {
  6907. this.unexpected(next.start, `Unexpected token, expected "function"`);
  6908. }
  6909. }
  6910. node.specifiers = [];
  6911. node.source = null;
  6912. node.declaration = this.parseExportDeclaration(node);
  6913. return true;
  6914. }
  6915. return false;
  6916. }
  6917. isAsyncFunction() {
  6918. if (!this.isContextual("async")) return false;
  6919. const {
  6920. pos
  6921. } = this.state;
  6922. skipWhiteSpace.lastIndex = pos;
  6923. const skip = skipWhiteSpace.exec(this.input);
  6924. if (!skip || !skip.length) return false;
  6925. const next = pos + skip[0].length;
  6926. return !lineBreak.test(this.input.slice(pos, next)) && this.input.slice(next, next + 8) === "function" && (next + 8 === this.length || !isIdentifierChar(this.input.charCodeAt(next + 8)));
  6927. }
  6928. parseExportDefaultExpression() {
  6929. const expr = this.startNode();
  6930. const isAsync = this.isAsyncFunction();
  6931. if (this.match(types._function) || isAsync) {
  6932. this.next();
  6933. if (isAsync) {
  6934. this.next();
  6935. }
  6936. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  6937. } else if (this.match(types._class)) {
  6938. return this.parseClass(expr, true, true);
  6939. } else if (this.match(types.at)) {
  6940. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  6941. 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");
  6942. }
  6943. this.parseDecorators(false);
  6944. return this.parseClass(expr, true, true);
  6945. } else if (this.match(types._const) || this.match(types._var) || this.isLet()) {
  6946. return this.raise(this.state.start, "Only expressions, functions or classes are allowed as the `default` export.");
  6947. } else {
  6948. const res = this.parseMaybeAssign();
  6949. this.semicolon();
  6950. return res;
  6951. }
  6952. }
  6953. parseExportDeclaration(node) {
  6954. return this.parseStatement(null);
  6955. }
  6956. isExportDefaultSpecifier() {
  6957. if (this.match(types.name)) {
  6958. return this.state.value !== "async" && this.state.value !== "let";
  6959. }
  6960. if (!this.match(types._default)) {
  6961. return false;
  6962. }
  6963. const lookahead = this.lookahead();
  6964. return lookahead.type === types.comma || lookahead.type === types.name && lookahead.value === "from";
  6965. }
  6966. parseExportFrom(node, expect) {
  6967. if (this.eatContextual("from")) {
  6968. node.source = this.parseImportSource();
  6969. this.checkExport(node);
  6970. } else {
  6971. if (expect) {
  6972. this.unexpected();
  6973. } else {
  6974. node.source = null;
  6975. }
  6976. }
  6977. this.semicolon();
  6978. }
  6979. shouldParseExportDeclaration() {
  6980. if (this.match(types.at)) {
  6981. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  6982. if (this.hasPlugin("decorators")) {
  6983. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  6984. 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");
  6985. } else {
  6986. return true;
  6987. }
  6988. }
  6989. }
  6990. 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();
  6991. }
  6992. checkExport(node, checkNames, isDefault, isFrom) {
  6993. if (checkNames) {
  6994. if (isDefault) {
  6995. this.checkDuplicateExports(node, "default");
  6996. } else if (node.specifiers && node.specifiers.length) {
  6997. for (let _i3 = 0, _node$specifiers = node.specifiers; _i3 < _node$specifiers.length; _i3++) {
  6998. const specifier = _node$specifiers[_i3];
  6999. this.checkDuplicateExports(specifier, specifier.exported.name);
  7000. if (!isFrom && specifier.local) {
  7001. this.checkReservedWord(specifier.local.name, specifier.local.start, true, false);
  7002. this.scope.checkLocalExport(specifier.local);
  7003. }
  7004. }
  7005. } else if (node.declaration) {
  7006. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  7007. const id = node.declaration.id;
  7008. if (!id) throw new Error("Assertion failure");
  7009. this.checkDuplicateExports(node, id.name);
  7010. } else if (node.declaration.type === "VariableDeclaration") {
  7011. for (let _i4 = 0, _node$declaration$dec = node.declaration.declarations; _i4 < _node$declaration$dec.length; _i4++) {
  7012. const declaration = _node$declaration$dec[_i4];
  7013. this.checkDeclaration(declaration.id);
  7014. }
  7015. }
  7016. }
  7017. }
  7018. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  7019. if (currentContextDecorators.length) {
  7020. const isClass = node.declaration && (node.declaration.type === "ClassDeclaration" || node.declaration.type === "ClassExpression");
  7021. if (!node.declaration || !isClass) {
  7022. throw this.raise(node.start, "You can only use decorators on an export when exporting a class");
  7023. }
  7024. this.takeDecorators(node.declaration);
  7025. }
  7026. }
  7027. checkDeclaration(node) {
  7028. if (node.type === "Identifier") {
  7029. this.checkDuplicateExports(node, node.name);
  7030. } else if (node.type === "ObjectPattern") {
  7031. for (let _i5 = 0, _node$properties = node.properties; _i5 < _node$properties.length; _i5++) {
  7032. const prop = _node$properties[_i5];
  7033. this.checkDeclaration(prop);
  7034. }
  7035. } else if (node.type === "ArrayPattern") {
  7036. for (let _i6 = 0, _node$elements = node.elements; _i6 < _node$elements.length; _i6++) {
  7037. const elem = _node$elements[_i6];
  7038. if (elem) {
  7039. this.checkDeclaration(elem);
  7040. }
  7041. }
  7042. } else if (node.type === "ObjectProperty") {
  7043. this.checkDeclaration(node.value);
  7044. } else if (node.type === "RestElement") {
  7045. this.checkDeclaration(node.argument);
  7046. } else if (node.type === "AssignmentPattern") {
  7047. this.checkDeclaration(node.left);
  7048. }
  7049. }
  7050. checkDuplicateExports(node, name) {
  7051. if (this.state.exportedIdentifiers.indexOf(name) > -1) {
  7052. throw this.raise(node.start, name === "default" ? "Only one default export allowed per module." : `\`${name}\` has already been exported. Exported identifiers must be unique.`);
  7053. }
  7054. this.state.exportedIdentifiers.push(name);
  7055. }
  7056. parseExportSpecifiers() {
  7057. const nodes = [];
  7058. let first = true;
  7059. this.expect(types.braceL);
  7060. while (!this.eat(types.braceR)) {
  7061. if (first) {
  7062. first = false;
  7063. } else {
  7064. this.expect(types.comma);
  7065. if (this.eat(types.braceR)) break;
  7066. }
  7067. const node = this.startNode();
  7068. node.local = this.parseIdentifier(true);
  7069. node.exported = this.eatContextual("as") ? this.parseIdentifier(true) : node.local.__clone();
  7070. nodes.push(this.finishNode(node, "ExportSpecifier"));
  7071. }
  7072. return nodes;
  7073. }
  7074. parseImport(node) {
  7075. node.specifiers = [];
  7076. if (!this.match(types.string)) {
  7077. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  7078. const parseNext = !hasDefault || this.eat(types.comma);
  7079. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  7080. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  7081. this.expectContextual("from");
  7082. }
  7083. node.source = this.parseImportSource();
  7084. this.semicolon();
  7085. return this.finishNode(node, "ImportDeclaration");
  7086. }
  7087. parseImportSource() {
  7088. if (!this.match(types.string)) this.unexpected();
  7089. return this.parseExprAtom();
  7090. }
  7091. shouldParseDefaultImport(node) {
  7092. return this.match(types.name);
  7093. }
  7094. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  7095. specifier.local = this.parseIdentifier();
  7096. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, contextDescription);
  7097. node.specifiers.push(this.finishNode(specifier, type));
  7098. }
  7099. maybeParseDefaultImportSpecifier(node) {
  7100. if (this.shouldParseDefaultImport(node)) {
  7101. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  7102. return true;
  7103. }
  7104. return false;
  7105. }
  7106. maybeParseStarImportSpecifier(node) {
  7107. if (this.match(types.star)) {
  7108. const specifier = this.startNode();
  7109. this.next();
  7110. this.expectContextual("as");
  7111. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  7112. return true;
  7113. }
  7114. return false;
  7115. }
  7116. parseNamedImportSpecifiers(node) {
  7117. let first = true;
  7118. this.expect(types.braceL);
  7119. while (!this.eat(types.braceR)) {
  7120. if (first) {
  7121. first = false;
  7122. } else {
  7123. if (this.eat(types.colon)) {
  7124. this.unexpected(null, "ES2015 named imports do not destructure. " + "Use another statement for destructuring after the import.");
  7125. }
  7126. this.expect(types.comma);
  7127. if (this.eat(types.braceR)) break;
  7128. }
  7129. this.parseImportSpecifier(node);
  7130. }
  7131. }
  7132. parseImportSpecifier(node) {
  7133. const specifier = this.startNode();
  7134. specifier.imported = this.parseIdentifier(true);
  7135. if (this.eatContextual("as")) {
  7136. specifier.local = this.parseIdentifier();
  7137. } else {
  7138. this.checkReservedWord(specifier.imported.name, specifier.start, true, true);
  7139. specifier.local = specifier.imported.__clone();
  7140. }
  7141. this.checkLVal(specifier.local, BIND_LEXICAL, undefined, "import specifier");
  7142. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  7143. }
  7144. }
  7145. class Scope {
  7146. constructor(flags) {
  7147. this.var = [];
  7148. this.lexical = [];
  7149. this.functions = [];
  7150. this.flags = flags;
  7151. }
  7152. }
  7153. class ScopeHandler {
  7154. constructor(raise, inModule) {
  7155. this.scopeStack = [];
  7156. this.undefinedExports = new Map();
  7157. this.raise = raise;
  7158. this.inModule = inModule;
  7159. }
  7160. get inFunction() {
  7161. return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0;
  7162. }
  7163. get inGenerator() {
  7164. return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0;
  7165. }
  7166. get inAsync() {
  7167. return (this.currentVarScope().flags & SCOPE_ASYNC) > 0;
  7168. }
  7169. get allowSuper() {
  7170. return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
  7171. }
  7172. get allowDirectSuper() {
  7173. return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0;
  7174. }
  7175. get inNonArrowFunction() {
  7176. return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0;
  7177. }
  7178. get treatFunctionsAsVar() {
  7179. return this.treatFunctionsAsVarInScope(this.currentScope());
  7180. }
  7181. enter(flags) {
  7182. this.scopeStack.push(new Scope(flags));
  7183. }
  7184. exit() {
  7185. this.scopeStack.pop();
  7186. }
  7187. treatFunctionsAsVarInScope(scope) {
  7188. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  7189. }
  7190. declareName(name, bindingType, pos) {
  7191. let redeclared = false;
  7192. if (bindingType === BIND_LEXICAL) {
  7193. const scope = this.currentScope();
  7194. redeclared = scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  7195. scope.lexical.push(name);
  7196. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  7197. this.undefinedExports.delete(name);
  7198. }
  7199. } else if (bindingType === BIND_SIMPLE_CATCH) {
  7200. const scope = this.currentScope();
  7201. scope.lexical.push(name);
  7202. } else if (bindingType === BIND_FUNCTION) {
  7203. const scope = this.currentScope();
  7204. if (this.treatFunctionsAsVar) {
  7205. redeclared = scope.lexical.indexOf(name) > -1;
  7206. } else {
  7207. redeclared = scope.lexical.indexOf(name) > -1 || scope.var.indexOf(name) > -1;
  7208. }
  7209. scope.functions.push(name);
  7210. } else {
  7211. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  7212. const scope = this.scopeStack[i];
  7213. if (scope.lexical.indexOf(name) > -1 && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.indexOf(name) > -1) {
  7214. redeclared = true;
  7215. break;
  7216. }
  7217. scope.var.push(name);
  7218. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  7219. this.undefinedExports.delete(name);
  7220. }
  7221. if (scope.flags & SCOPE_VAR) break;
  7222. }
  7223. }
  7224. if (redeclared) {
  7225. this.raise(pos, `Identifier '${name}' has already been declared`);
  7226. }
  7227. }
  7228. checkLocalExport(id) {
  7229. 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) {
  7230. this.undefinedExports.set(id.name, id.start);
  7231. }
  7232. }
  7233. currentScope() {
  7234. return this.scopeStack[this.scopeStack.length - 1];
  7235. }
  7236. currentVarScope() {
  7237. for (let i = this.scopeStack.length - 1;; i--) {
  7238. const scope = this.scopeStack[i];
  7239. if (scope.flags & SCOPE_VAR) {
  7240. return scope;
  7241. }
  7242. }
  7243. }
  7244. currentThisScope() {
  7245. for (let i = this.scopeStack.length - 1;; i--) {
  7246. const scope = this.scopeStack[i];
  7247. if ((scope.flags & SCOPE_VAR || scope.flags & SCOPE_CLASS) && !(scope.flags & SCOPE_ARROW)) {
  7248. return scope;
  7249. }
  7250. }
  7251. }
  7252. }
  7253. class Parser extends StatementParser {
  7254. constructor(options, input) {
  7255. options = getOptions(options);
  7256. super(options, input);
  7257. this.options = options;
  7258. this.inModule = this.options.sourceType === "module";
  7259. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  7260. this.plugins = pluginsMap(this.options.plugins);
  7261. this.filename = options.sourceFilename;
  7262. }
  7263. parse() {
  7264. this.scope.enter(SCOPE_PROGRAM);
  7265. const file = this.startNode();
  7266. const program = this.startNode();
  7267. this.nextToken();
  7268. return this.parseTopLevel(file, program);
  7269. }
  7270. }
  7271. function pluginsMap(plugins) {
  7272. const pluginMap = new Map();
  7273. for (let _i = 0; _i < plugins.length; _i++) {
  7274. const plugin = plugins[_i];
  7275. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  7276. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  7277. }
  7278. return pluginMap;
  7279. }
  7280. function nonNull(x) {
  7281. if (x == null) {
  7282. throw new Error(`Unexpected ${x} value.`);
  7283. }
  7284. return x;
  7285. }
  7286. function assert(x) {
  7287. if (!x) {
  7288. throw new Error("Assert fail");
  7289. }
  7290. }
  7291. function keywordTypeFromName(value) {
  7292. switch (value) {
  7293. case "any":
  7294. return "TSAnyKeyword";
  7295. case "boolean":
  7296. return "TSBooleanKeyword";
  7297. case "bigint":
  7298. return "TSBigIntKeyword";
  7299. case "never":
  7300. return "TSNeverKeyword";
  7301. case "number":
  7302. return "TSNumberKeyword";
  7303. case "object":
  7304. return "TSObjectKeyword";
  7305. case "string":
  7306. return "TSStringKeyword";
  7307. case "symbol":
  7308. return "TSSymbolKeyword";
  7309. case "undefined":
  7310. return "TSUndefinedKeyword";
  7311. case "unknown":
  7312. return "TSUnknownKeyword";
  7313. default:
  7314. return undefined;
  7315. }
  7316. }
  7317. var typescript = (superClass => class extends superClass {
  7318. tsIsIdentifier() {
  7319. return this.match(types.name);
  7320. }
  7321. tsNextTokenCanFollowModifier() {
  7322. this.next();
  7323. 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);
  7324. }
  7325. tsParseModifier(allowedModifiers) {
  7326. if (!this.match(types.name)) {
  7327. return undefined;
  7328. }
  7329. const modifier = this.state.value;
  7330. if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7331. return modifier;
  7332. }
  7333. return undefined;
  7334. }
  7335. tsIsListTerminator(kind) {
  7336. switch (kind) {
  7337. case "EnumMembers":
  7338. case "TypeMembers":
  7339. return this.match(types.braceR);
  7340. case "HeritageClauseElement":
  7341. return this.match(types.braceL);
  7342. case "TupleElementTypes":
  7343. return this.match(types.bracketR);
  7344. case "TypeParametersOrArguments":
  7345. return this.isRelational(">");
  7346. }
  7347. throw new Error("Unreachable");
  7348. }
  7349. tsParseList(kind, parseElement) {
  7350. const result = [];
  7351. while (!this.tsIsListTerminator(kind)) {
  7352. result.push(parseElement());
  7353. }
  7354. return result;
  7355. }
  7356. tsParseDelimitedList(kind, parseElement) {
  7357. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  7358. }
  7359. tsTryParseDelimitedList(kind, parseElement) {
  7360. return this.tsParseDelimitedListWorker(kind, parseElement, false);
  7361. }
  7362. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  7363. const result = [];
  7364. while (true) {
  7365. if (this.tsIsListTerminator(kind)) {
  7366. break;
  7367. }
  7368. const element = parseElement();
  7369. if (element == null) {
  7370. return undefined;
  7371. }
  7372. result.push(element);
  7373. if (this.eat(types.comma)) {
  7374. continue;
  7375. }
  7376. if (this.tsIsListTerminator(kind)) {
  7377. break;
  7378. }
  7379. if (expectSuccess) {
  7380. this.expect(types.comma);
  7381. }
  7382. return undefined;
  7383. }
  7384. return result;
  7385. }
  7386. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  7387. if (!skipFirstToken) {
  7388. if (bracket) {
  7389. this.expect(types.bracketL);
  7390. } else {
  7391. this.expectRelational("<");
  7392. }
  7393. }
  7394. const result = this.tsParseDelimitedList(kind, parseElement);
  7395. if (bracket) {
  7396. this.expect(types.bracketR);
  7397. } else {
  7398. this.expectRelational(">");
  7399. }
  7400. return result;
  7401. }
  7402. tsParseImportType() {
  7403. const node = this.startNode();
  7404. this.expect(types._import);
  7405. this.expect(types.parenL);
  7406. if (!this.match(types.string)) {
  7407. throw this.unexpected(null, "Argument in a type import must be a string literal");
  7408. }
  7409. node.argument = this.parseLiteral(this.state.value, "StringLiteral");
  7410. this.expect(types.parenR);
  7411. if (this.eat(types.dot)) {
  7412. node.qualifier = this.tsParseEntityName(true);
  7413. }
  7414. if (this.isRelational("<")) {
  7415. node.typeParameters = this.tsParseTypeArguments();
  7416. }
  7417. return this.finishNode(node, "TSImportType");
  7418. }
  7419. tsParseEntityName(allowReservedWords) {
  7420. let entity = this.parseIdentifier();
  7421. while (this.eat(types.dot)) {
  7422. const node = this.startNodeAtNode(entity);
  7423. node.left = entity;
  7424. node.right = this.parseIdentifier(allowReservedWords);
  7425. entity = this.finishNode(node, "TSQualifiedName");
  7426. }
  7427. return entity;
  7428. }
  7429. tsParseTypeReference() {
  7430. const node = this.startNode();
  7431. node.typeName = this.tsParseEntityName(false);
  7432. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  7433. node.typeParameters = this.tsParseTypeArguments();
  7434. }
  7435. return this.finishNode(node, "TSTypeReference");
  7436. }
  7437. tsParseThisTypePredicate(lhs) {
  7438. this.next();
  7439. const node = this.startNodeAtNode(lhs);
  7440. node.parameterName = lhs;
  7441. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7442. return this.finishNode(node, "TSTypePredicate");
  7443. }
  7444. tsParseThisTypeNode() {
  7445. const node = this.startNode();
  7446. this.next();
  7447. return this.finishNode(node, "TSThisType");
  7448. }
  7449. tsParseTypeQuery() {
  7450. const node = this.startNode();
  7451. this.expect(types._typeof);
  7452. if (this.match(types._import)) {
  7453. node.exprName = this.tsParseImportType();
  7454. } else {
  7455. node.exprName = this.tsParseEntityName(true);
  7456. }
  7457. return this.finishNode(node, "TSTypeQuery");
  7458. }
  7459. tsParseTypeParameter() {
  7460. const node = this.startNode();
  7461. node.name = this.parseIdentifierName(node.start);
  7462. node.constraint = this.tsEatThenParseType(types._extends);
  7463. node.default = this.tsEatThenParseType(types.eq);
  7464. return this.finishNode(node, "TSTypeParameter");
  7465. }
  7466. tsTryParseTypeParameters() {
  7467. if (this.isRelational("<")) {
  7468. return this.tsParseTypeParameters();
  7469. }
  7470. }
  7471. tsParseTypeParameters() {
  7472. const node = this.startNode();
  7473. if (this.isRelational("<") || this.match(types.jsxTagStart)) {
  7474. this.next();
  7475. } else {
  7476. this.unexpected();
  7477. }
  7478. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  7479. return this.finishNode(node, "TSTypeParameterDeclaration");
  7480. }
  7481. tsTryNextParseConstantContext() {
  7482. if (this.lookahead().type === types._const) {
  7483. this.next();
  7484. return this.tsParseTypeReference();
  7485. }
  7486. return null;
  7487. }
  7488. tsCheckLiteralForConstantContext(node) {
  7489. switch (node.type) {
  7490. case "StringLiteral":
  7491. case "TemplateLiteral":
  7492. case "NumericLiteral":
  7493. case "BooleanLiteral":
  7494. case "SpreadElement":
  7495. case "ObjectMethod":
  7496. case "ObjectExpression":
  7497. return;
  7498. case "ArrayExpression":
  7499. return node.elements.forEach(element => {
  7500. if (element) {
  7501. this.tsCheckLiteralForConstantContext(element);
  7502. }
  7503. });
  7504. case "ObjectProperty":
  7505. return this.tsCheckLiteralForConstantContext(node.value);
  7506. case "UnaryExpression":
  7507. return this.tsCheckLiteralForConstantContext(node.argument);
  7508. default:
  7509. this.raise(node.start, "Only literal values are allowed in constant contexts");
  7510. }
  7511. }
  7512. tsFillSignature(returnToken, signature) {
  7513. const returnTokenRequired = returnToken === types.arrow;
  7514. signature.typeParameters = this.tsTryParseTypeParameters();
  7515. this.expect(types.parenL);
  7516. signature.parameters = this.tsParseBindingListForSignature();
  7517. if (returnTokenRequired) {
  7518. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7519. } else if (this.match(returnToken)) {
  7520. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7521. }
  7522. }
  7523. tsParseBindingListForSignature() {
  7524. return this.parseBindingList(types.parenR).map(pattern => {
  7525. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7526. throw this.unexpected(pattern.start, `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${pattern.type}`);
  7527. }
  7528. return pattern;
  7529. });
  7530. }
  7531. tsParseTypeMemberSemicolon() {
  7532. if (!this.eat(types.comma)) {
  7533. this.semicolon();
  7534. }
  7535. }
  7536. tsParseSignatureMember(kind) {
  7537. const node = this.startNode();
  7538. if (kind === "TSConstructSignatureDeclaration") {
  7539. this.expect(types._new);
  7540. }
  7541. this.tsFillSignature(types.colon, node);
  7542. this.tsParseTypeMemberSemicolon();
  7543. return this.finishNode(node, kind);
  7544. }
  7545. tsIsUnambiguouslyIndexSignature() {
  7546. this.next();
  7547. return this.eat(types.name) && this.match(types.colon);
  7548. }
  7549. tsTryParseIndexSignature(node) {
  7550. if (!(this.match(types.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7551. return undefined;
  7552. }
  7553. this.expect(types.bracketL);
  7554. const id = this.parseIdentifier();
  7555. id.typeAnnotation = this.tsParseTypeAnnotation();
  7556. this.finishNode(id, "Identifier");
  7557. this.expect(types.bracketR);
  7558. node.parameters = [id];
  7559. const type = this.tsTryParseTypeAnnotation();
  7560. if (type) node.typeAnnotation = type;
  7561. this.tsParseTypeMemberSemicolon();
  7562. return this.finishNode(node, "TSIndexSignature");
  7563. }
  7564. tsParsePropertyOrMethodSignature(node, readonly) {
  7565. this.parsePropertyName(node);
  7566. if (this.eat(types.question)) node.optional = true;
  7567. const nodeAny = node;
  7568. if (!readonly && (this.match(types.parenL) || this.isRelational("<"))) {
  7569. const method = nodeAny;
  7570. this.tsFillSignature(types.colon, method);
  7571. this.tsParseTypeMemberSemicolon();
  7572. return this.finishNode(method, "TSMethodSignature");
  7573. } else {
  7574. const property = nodeAny;
  7575. if (readonly) property.readonly = true;
  7576. const type = this.tsTryParseTypeAnnotation();
  7577. if (type) property.typeAnnotation = type;
  7578. this.tsParseTypeMemberSemicolon();
  7579. return this.finishNode(property, "TSPropertySignature");
  7580. }
  7581. }
  7582. tsParseTypeMember() {
  7583. if (this.match(types.parenL) || this.isRelational("<")) {
  7584. return this.tsParseSignatureMember("TSCallSignatureDeclaration");
  7585. }
  7586. if (this.match(types._new) && this.tsLookAhead(this.tsIsStartOfConstructSignature.bind(this))) {
  7587. return this.tsParseSignatureMember("TSConstructSignatureDeclaration");
  7588. }
  7589. const node = this.startNode();
  7590. const readonly = !!this.tsParseModifier(["readonly"]);
  7591. const idx = this.tsTryParseIndexSignature(node);
  7592. if (idx) {
  7593. if (readonly) node.readonly = true;
  7594. return idx;
  7595. }
  7596. return this.tsParsePropertyOrMethodSignature(node, readonly);
  7597. }
  7598. tsIsStartOfConstructSignature() {
  7599. this.next();
  7600. return this.match(types.parenL) || this.isRelational("<");
  7601. }
  7602. tsParseTypeLiteral() {
  7603. const node = this.startNode();
  7604. node.members = this.tsParseObjectTypeMembers();
  7605. return this.finishNode(node, "TSTypeLiteral");
  7606. }
  7607. tsParseObjectTypeMembers() {
  7608. this.expect(types.braceL);
  7609. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7610. this.expect(types.braceR);
  7611. return members;
  7612. }
  7613. tsIsStartOfMappedType() {
  7614. this.next();
  7615. if (this.eat(types.plusMin)) {
  7616. return this.isContextual("readonly");
  7617. }
  7618. if (this.isContextual("readonly")) {
  7619. this.next();
  7620. }
  7621. if (!this.match(types.bracketL)) {
  7622. return false;
  7623. }
  7624. this.next();
  7625. if (!this.tsIsIdentifier()) {
  7626. return false;
  7627. }
  7628. this.next();
  7629. return this.match(types._in);
  7630. }
  7631. tsParseMappedTypeParameter() {
  7632. const node = this.startNode();
  7633. node.name = this.parseIdentifierName(node.start);
  7634. node.constraint = this.tsExpectThenParseType(types._in);
  7635. return this.finishNode(node, "TSTypeParameter");
  7636. }
  7637. tsParseMappedType() {
  7638. const node = this.startNode();
  7639. this.expect(types.braceL);
  7640. if (this.match(types.plusMin)) {
  7641. node.readonly = this.state.value;
  7642. this.next();
  7643. this.expectContextual("readonly");
  7644. } else if (this.eatContextual("readonly")) {
  7645. node.readonly = true;
  7646. }
  7647. this.expect(types.bracketL);
  7648. node.typeParameter = this.tsParseMappedTypeParameter();
  7649. this.expect(types.bracketR);
  7650. if (this.match(types.plusMin)) {
  7651. node.optional = this.state.value;
  7652. this.next();
  7653. this.expect(types.question);
  7654. } else if (this.eat(types.question)) {
  7655. node.optional = true;
  7656. }
  7657. node.typeAnnotation = this.tsTryParseType();
  7658. this.semicolon();
  7659. this.expect(types.braceR);
  7660. return this.finishNode(node, "TSMappedType");
  7661. }
  7662. tsParseTupleType() {
  7663. const node = this.startNode();
  7664. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7665. let seenOptionalElement = false;
  7666. node.elementTypes.forEach(elementNode => {
  7667. if (elementNode.type === "TSOptionalType") {
  7668. seenOptionalElement = true;
  7669. } else if (seenOptionalElement && elementNode.type !== "TSRestType") {
  7670. this.raise(elementNode.start, "A required element cannot follow an optional element.");
  7671. }
  7672. });
  7673. return this.finishNode(node, "TSTupleType");
  7674. }
  7675. tsParseTupleElementType() {
  7676. if (this.match(types.ellipsis)) {
  7677. const restNode = this.startNode();
  7678. this.next();
  7679. restNode.typeAnnotation = this.tsParseType();
  7680. this.checkCommaAfterRest();
  7681. return this.finishNode(restNode, "TSRestType");
  7682. }
  7683. const type = this.tsParseType();
  7684. if (this.eat(types.question)) {
  7685. const optionalTypeNode = this.startNodeAtNode(type);
  7686. optionalTypeNode.typeAnnotation = type;
  7687. return this.finishNode(optionalTypeNode, "TSOptionalType");
  7688. }
  7689. return type;
  7690. }
  7691. tsParseParenthesizedType() {
  7692. const node = this.startNode();
  7693. this.expect(types.parenL);
  7694. node.typeAnnotation = this.tsParseType();
  7695. this.expect(types.parenR);
  7696. return this.finishNode(node, "TSParenthesizedType");
  7697. }
  7698. tsParseFunctionOrConstructorType(type) {
  7699. const node = this.startNode();
  7700. if (type === "TSConstructorType") {
  7701. this.expect(types._new);
  7702. }
  7703. this.tsFillSignature(types.arrow, node);
  7704. return this.finishNode(node, type);
  7705. }
  7706. tsParseLiteralTypeNode() {
  7707. const node = this.startNode();
  7708. node.literal = (() => {
  7709. switch (this.state.type) {
  7710. case types.num:
  7711. return this.parseLiteral(this.state.value, "NumericLiteral");
  7712. case types.string:
  7713. return this.parseLiteral(this.state.value, "StringLiteral");
  7714. case types._true:
  7715. case types._false:
  7716. return this.parseBooleanLiteral();
  7717. default:
  7718. throw this.unexpected();
  7719. }
  7720. })();
  7721. return this.finishNode(node, "TSLiteralType");
  7722. }
  7723. tsParseNonArrayType() {
  7724. switch (this.state.type) {
  7725. case types.name:
  7726. case types._void:
  7727. case types._null:
  7728. {
  7729. const type = this.match(types._void) ? "TSVoidKeyword" : this.match(types._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7730. if (type !== undefined && this.lookahead().type !== types.dot) {
  7731. const node = this.startNode();
  7732. this.next();
  7733. return this.finishNode(node, type);
  7734. }
  7735. return this.tsParseTypeReference();
  7736. }
  7737. case types.string:
  7738. case types.num:
  7739. case types._true:
  7740. case types._false:
  7741. return this.tsParseLiteralTypeNode();
  7742. case types.plusMin:
  7743. if (this.state.value === "-") {
  7744. const node = this.startNode();
  7745. this.next();
  7746. if (!this.match(types.num)) {
  7747. throw this.unexpected();
  7748. }
  7749. node.literal = this.parseLiteral(-this.state.value, "NumericLiteral", node.start, node.loc.start);
  7750. return this.finishNode(node, "TSLiteralType");
  7751. }
  7752. break;
  7753. case types._this:
  7754. {
  7755. const thisKeyword = this.tsParseThisTypeNode();
  7756. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  7757. return this.tsParseThisTypePredicate(thisKeyword);
  7758. } else {
  7759. return thisKeyword;
  7760. }
  7761. }
  7762. case types._typeof:
  7763. return this.tsParseTypeQuery();
  7764. case types._import:
  7765. return this.tsParseImportType();
  7766. case types.braceL:
  7767. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7768. case types.bracketL:
  7769. return this.tsParseTupleType();
  7770. case types.parenL:
  7771. return this.tsParseParenthesizedType();
  7772. }
  7773. throw this.unexpected();
  7774. }
  7775. tsParseArrayTypeOrHigher() {
  7776. let type = this.tsParseNonArrayType();
  7777. while (!this.hasPrecedingLineBreak() && this.eat(types.bracketL)) {
  7778. if (this.match(types.bracketR)) {
  7779. const node = this.startNodeAtNode(type);
  7780. node.elementType = type;
  7781. this.expect(types.bracketR);
  7782. type = this.finishNode(node, "TSArrayType");
  7783. } else {
  7784. const node = this.startNodeAtNode(type);
  7785. node.objectType = type;
  7786. node.indexType = this.tsParseType();
  7787. this.expect(types.bracketR);
  7788. type = this.finishNode(node, "TSIndexedAccessType");
  7789. }
  7790. }
  7791. return type;
  7792. }
  7793. tsParseTypeOperator(operator) {
  7794. const node = this.startNode();
  7795. this.expectContextual(operator);
  7796. node.operator = operator;
  7797. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7798. if (operator === "readonly") {
  7799. this.tsCheckTypeAnnotationForReadOnly(node);
  7800. }
  7801. return this.finishNode(node, "TSTypeOperator");
  7802. }
  7803. tsCheckTypeAnnotationForReadOnly(node) {
  7804. switch (node.typeAnnotation.type) {
  7805. case "TSTupleType":
  7806. case "TSArrayType":
  7807. return;
  7808. default:
  7809. this.raise(node.operator, "'readonly' type modifier is only permitted on array and tuple literal types.");
  7810. }
  7811. }
  7812. tsParseInferType() {
  7813. const node = this.startNode();
  7814. this.expectContextual("infer");
  7815. const typeParameter = this.startNode();
  7816. typeParameter.name = this.parseIdentifierName(typeParameter.start);
  7817. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7818. return this.finishNode(node, "TSInferType");
  7819. }
  7820. tsParseTypeOperatorOrHigher() {
  7821. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  7822. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  7823. }
  7824. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7825. this.eat(operator);
  7826. let type = parseConstituentType();
  7827. if (this.match(operator)) {
  7828. const types = [type];
  7829. while (this.eat(operator)) {
  7830. types.push(parseConstituentType());
  7831. }
  7832. const node = this.startNodeAtNode(type);
  7833. node.types = types;
  7834. type = this.finishNode(node, kind);
  7835. }
  7836. return type;
  7837. }
  7838. tsParseIntersectionTypeOrHigher() {
  7839. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types.bitwiseAND);
  7840. }
  7841. tsParseUnionTypeOrHigher() {
  7842. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types.bitwiseOR);
  7843. }
  7844. tsIsStartOfFunctionType() {
  7845. if (this.isRelational("<")) {
  7846. return true;
  7847. }
  7848. return this.match(types.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  7849. }
  7850. tsSkipParameterStart() {
  7851. if (this.match(types.name) || this.match(types._this)) {
  7852. this.next();
  7853. return true;
  7854. }
  7855. if (this.match(types.braceL)) {
  7856. let braceStackCounter = 1;
  7857. this.next();
  7858. while (braceStackCounter > 0) {
  7859. if (this.match(types.braceL)) {
  7860. ++braceStackCounter;
  7861. } else if (this.match(types.braceR)) {
  7862. --braceStackCounter;
  7863. }
  7864. this.next();
  7865. }
  7866. return true;
  7867. }
  7868. if (this.match(types.bracketL)) {
  7869. let braceStackCounter = 1;
  7870. this.next();
  7871. while (braceStackCounter > 0) {
  7872. if (this.match(types.bracketL)) {
  7873. ++braceStackCounter;
  7874. } else if (this.match(types.bracketR)) {
  7875. --braceStackCounter;
  7876. }
  7877. this.next();
  7878. }
  7879. return true;
  7880. }
  7881. return false;
  7882. }
  7883. tsIsUnambiguouslyStartOfFunctionType() {
  7884. this.next();
  7885. if (this.match(types.parenR) || this.match(types.ellipsis)) {
  7886. return true;
  7887. }
  7888. if (this.tsSkipParameterStart()) {
  7889. if (this.match(types.colon) || this.match(types.comma) || this.match(types.question) || this.match(types.eq)) {
  7890. return true;
  7891. }
  7892. if (this.match(types.parenR)) {
  7893. this.next();
  7894. if (this.match(types.arrow)) {
  7895. return true;
  7896. }
  7897. }
  7898. }
  7899. return false;
  7900. }
  7901. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  7902. return this.tsInType(() => {
  7903. const t = this.startNode();
  7904. this.expect(returnToken);
  7905. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  7906. if (!typePredicateVariable) {
  7907. return this.tsParseTypeAnnotation(false, t);
  7908. }
  7909. const type = this.tsParseTypeAnnotation(false);
  7910. const node = this.startNodeAtNode(typePredicateVariable);
  7911. node.parameterName = typePredicateVariable;
  7912. node.typeAnnotation = type;
  7913. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7914. return this.finishNode(t, "TSTypeAnnotation");
  7915. });
  7916. }
  7917. tsTryParseTypeOrTypePredicateAnnotation() {
  7918. return this.match(types.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types.colon) : undefined;
  7919. }
  7920. tsTryParseTypeAnnotation() {
  7921. return this.match(types.colon) ? this.tsParseTypeAnnotation() : undefined;
  7922. }
  7923. tsTryParseType() {
  7924. return this.tsEatThenParseType(types.colon);
  7925. }
  7926. tsParseTypePredicatePrefix() {
  7927. const id = this.parseIdentifier();
  7928. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  7929. this.next();
  7930. return id;
  7931. }
  7932. }
  7933. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  7934. this.tsInType(() => {
  7935. if (eatColon) this.expect(types.colon);
  7936. t.typeAnnotation = this.tsParseType();
  7937. });
  7938. return this.finishNode(t, "TSTypeAnnotation");
  7939. }
  7940. tsParseType() {
  7941. assert(this.state.inType);
  7942. const type = this.tsParseNonConditionalType();
  7943. if (this.hasPrecedingLineBreak() || !this.eat(types._extends)) {
  7944. return type;
  7945. }
  7946. const node = this.startNodeAtNode(type);
  7947. node.checkType = type;
  7948. node.extendsType = this.tsParseNonConditionalType();
  7949. this.expect(types.question);
  7950. node.trueType = this.tsParseType();
  7951. this.expect(types.colon);
  7952. node.falseType = this.tsParseType();
  7953. return this.finishNode(node, "TSConditionalType");
  7954. }
  7955. tsParseNonConditionalType() {
  7956. if (this.tsIsStartOfFunctionType()) {
  7957. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  7958. }
  7959. if (this.match(types._new)) {
  7960. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  7961. }
  7962. return this.tsParseUnionTypeOrHigher();
  7963. }
  7964. tsParseTypeAssertion() {
  7965. const node = this.startNode();
  7966. const _const = this.tsTryNextParseConstantContext();
  7967. node.typeAnnotation = _const || this.tsNextThenParseType();
  7968. this.expectRelational(">");
  7969. node.expression = this.parseMaybeUnary();
  7970. if (_const) {
  7971. this.tsCheckLiteralForConstantContext(node.expression);
  7972. }
  7973. return this.finishNode(node, "TSTypeAssertion");
  7974. }
  7975. tsParseHeritageClause(descriptor) {
  7976. const originalStart = this.state.start;
  7977. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  7978. if (!delimitedList.length) {
  7979. this.raise(originalStart, `'${descriptor}' list cannot be empty.`);
  7980. }
  7981. return delimitedList;
  7982. }
  7983. tsParseExpressionWithTypeArguments() {
  7984. const node = this.startNode();
  7985. node.expression = this.tsParseEntityName(false);
  7986. if (this.isRelational("<")) {
  7987. node.typeParameters = this.tsParseTypeArguments();
  7988. }
  7989. return this.finishNode(node, "TSExpressionWithTypeArguments");
  7990. }
  7991. tsParseInterfaceDeclaration(node) {
  7992. node.id = this.parseIdentifier();
  7993. node.typeParameters = this.tsTryParseTypeParameters();
  7994. if (this.eat(types._extends)) {
  7995. node.extends = this.tsParseHeritageClause("extends");
  7996. }
  7997. const body = this.startNode();
  7998. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  7999. node.body = this.finishNode(body, "TSInterfaceBody");
  8000. return this.finishNode(node, "TSInterfaceDeclaration");
  8001. }
  8002. tsParseTypeAliasDeclaration(node) {
  8003. node.id = this.parseIdentifier();
  8004. node.typeParameters = this.tsTryParseTypeParameters();
  8005. node.typeAnnotation = this.tsExpectThenParseType(types.eq);
  8006. this.semicolon();
  8007. return this.finishNode(node, "TSTypeAliasDeclaration");
  8008. }
  8009. tsInNoContext(cb) {
  8010. const oldContext = this.state.context;
  8011. this.state.context = [oldContext[0]];
  8012. try {
  8013. return cb();
  8014. } finally {
  8015. this.state.context = oldContext;
  8016. }
  8017. }
  8018. tsInType(cb) {
  8019. const oldInType = this.state.inType;
  8020. this.state.inType = true;
  8021. try {
  8022. return cb();
  8023. } finally {
  8024. this.state.inType = oldInType;
  8025. }
  8026. }
  8027. tsEatThenParseType(token) {
  8028. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8029. }
  8030. tsExpectThenParseType(token) {
  8031. return this.tsDoThenParseType(() => this.expect(token));
  8032. }
  8033. tsNextThenParseType() {
  8034. return this.tsDoThenParseType(() => this.next());
  8035. }
  8036. tsDoThenParseType(cb) {
  8037. return this.tsInType(() => {
  8038. cb();
  8039. return this.tsParseType();
  8040. });
  8041. }
  8042. tsParseEnumMember() {
  8043. const node = this.startNode();
  8044. node.id = this.match(types.string) ? this.parseLiteral(this.state.value, "StringLiteral") : this.parseIdentifier(true);
  8045. if (this.eat(types.eq)) {
  8046. node.initializer = this.parseMaybeAssign();
  8047. }
  8048. return this.finishNode(node, "TSEnumMember");
  8049. }
  8050. tsParseEnumDeclaration(node, isConst) {
  8051. if (isConst) node.const = true;
  8052. node.id = this.parseIdentifier();
  8053. this.expect(types.braceL);
  8054. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8055. this.expect(types.braceR);
  8056. return this.finishNode(node, "TSEnumDeclaration");
  8057. }
  8058. tsParseModuleBlock() {
  8059. const node = this.startNode();
  8060. this.scope.enter(SCOPE_OTHER);
  8061. this.expect(types.braceL);
  8062. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types.braceR);
  8063. this.scope.exit();
  8064. return this.finishNode(node, "TSModuleBlock");
  8065. }
  8066. tsParseModuleOrNamespaceDeclaration(node) {
  8067. node.id = this.parseIdentifier();
  8068. if (this.eat(types.dot)) {
  8069. const inner = this.startNode();
  8070. this.tsParseModuleOrNamespaceDeclaration(inner);
  8071. node.body = inner;
  8072. } else {
  8073. node.body = this.tsParseModuleBlock();
  8074. }
  8075. return this.finishNode(node, "TSModuleDeclaration");
  8076. }
  8077. tsParseAmbientExternalModuleDeclaration(node) {
  8078. if (this.isContextual("global")) {
  8079. node.global = true;
  8080. node.id = this.parseIdentifier();
  8081. } else if (this.match(types.string)) {
  8082. node.id = this.parseExprAtom();
  8083. } else {
  8084. this.unexpected();
  8085. }
  8086. if (this.match(types.braceL)) {
  8087. node.body = this.tsParseModuleBlock();
  8088. } else {
  8089. this.semicolon();
  8090. }
  8091. return this.finishNode(node, "TSModuleDeclaration");
  8092. }
  8093. tsParseImportEqualsDeclaration(node, isExport) {
  8094. node.isExport = isExport || false;
  8095. node.id = this.parseIdentifier();
  8096. this.expect(types.eq);
  8097. node.moduleReference = this.tsParseModuleReference();
  8098. this.semicolon();
  8099. return this.finishNode(node, "TSImportEqualsDeclaration");
  8100. }
  8101. tsIsExternalModuleReference() {
  8102. return this.isContextual("require") && this.lookahead().type === types.parenL;
  8103. }
  8104. tsParseModuleReference() {
  8105. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8106. }
  8107. tsParseExternalModuleReference() {
  8108. const node = this.startNode();
  8109. this.expectContextual("require");
  8110. this.expect(types.parenL);
  8111. if (!this.match(types.string)) {
  8112. throw this.unexpected();
  8113. }
  8114. node.expression = this.parseLiteral(this.state.value, "StringLiteral");
  8115. this.expect(types.parenR);
  8116. return this.finishNode(node, "TSExternalModuleReference");
  8117. }
  8118. tsLookAhead(f) {
  8119. const state = this.state.clone();
  8120. const res = f();
  8121. this.state = state;
  8122. return res;
  8123. }
  8124. tsTryParseAndCatch(f) {
  8125. const state = this.state.clone();
  8126. try {
  8127. return f();
  8128. } catch (e) {
  8129. if (e instanceof SyntaxError) {
  8130. this.state = state;
  8131. return undefined;
  8132. }
  8133. throw e;
  8134. }
  8135. }
  8136. tsTryParse(f) {
  8137. const state = this.state.clone();
  8138. const result = f();
  8139. if (result !== undefined && result !== false) {
  8140. return result;
  8141. } else {
  8142. this.state = state;
  8143. return undefined;
  8144. }
  8145. }
  8146. nodeWithSamePosition(original, type) {
  8147. const node = this.startNodeAtNode(original);
  8148. node.type = type;
  8149. node.end = original.end;
  8150. node.loc.end = original.loc.end;
  8151. if (original.leadingComments) {
  8152. node.leadingComments = original.leadingComments;
  8153. }
  8154. if (original.trailingComments) {
  8155. node.trailingComments = original.trailingComments;
  8156. }
  8157. if (original.innerComments) node.innerComments = original.innerComments;
  8158. return node;
  8159. }
  8160. tsTryParseDeclare(nany) {
  8161. if (this.isLineTerminator()) {
  8162. return;
  8163. }
  8164. let starttype = this.state.type;
  8165. let kind;
  8166. if (this.isContextual("let")) {
  8167. starttype = types._var;
  8168. kind = "let";
  8169. }
  8170. switch (starttype) {
  8171. case types._function:
  8172. return this.parseFunctionStatement(nany);
  8173. case types._class:
  8174. return this.parseClass(nany, true, false);
  8175. case types._const:
  8176. if (this.match(types._const) && this.isLookaheadContextual("enum")) {
  8177. this.expect(types._const);
  8178. this.expectContextual("enum");
  8179. return this.tsParseEnumDeclaration(nany, true);
  8180. }
  8181. case types._var:
  8182. kind = kind || this.state.value;
  8183. return this.parseVarStatement(nany, kind);
  8184. case types.name:
  8185. {
  8186. const value = this.state.value;
  8187. if (value === "global") {
  8188. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8189. } else {
  8190. return this.tsParseDeclaration(nany, value, true);
  8191. }
  8192. }
  8193. }
  8194. }
  8195. tsTryParseExportDeclaration() {
  8196. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  8197. }
  8198. tsParseExpressionStatement(node, expr) {
  8199. switch (expr.name) {
  8200. case "declare":
  8201. {
  8202. const declaration = this.tsTryParseDeclare(node);
  8203. if (declaration) {
  8204. declaration.declare = true;
  8205. return declaration;
  8206. }
  8207. break;
  8208. }
  8209. case "global":
  8210. if (this.match(types.braceL)) {
  8211. const mod = node;
  8212. mod.global = true;
  8213. mod.id = expr;
  8214. mod.body = this.tsParseModuleBlock();
  8215. return this.finishNode(mod, "TSModuleDeclaration");
  8216. }
  8217. break;
  8218. default:
  8219. return this.tsParseDeclaration(node, expr.name, false);
  8220. }
  8221. }
  8222. tsParseDeclaration(node, value, next) {
  8223. switch (value) {
  8224. case "abstract":
  8225. if (this.tsCheckLineTerminatorAndMatch(types._class, next)) {
  8226. const cls = node;
  8227. cls.abstract = true;
  8228. if (next) {
  8229. this.next();
  8230. if (!this.match(types._class)) {
  8231. this.unexpected(null, types._class);
  8232. }
  8233. }
  8234. return this.parseClass(cls, true, false);
  8235. }
  8236. break;
  8237. case "enum":
  8238. if (next || this.match(types.name)) {
  8239. if (next) this.next();
  8240. return this.tsParseEnumDeclaration(node, false);
  8241. }
  8242. break;
  8243. case "interface":
  8244. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  8245. if (next) this.next();
  8246. return this.tsParseInterfaceDeclaration(node);
  8247. }
  8248. break;
  8249. case "module":
  8250. if (next) this.next();
  8251. if (this.match(types.string)) {
  8252. return this.tsParseAmbientExternalModuleDeclaration(node);
  8253. } else if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  8254. return this.tsParseModuleOrNamespaceDeclaration(node);
  8255. }
  8256. break;
  8257. case "namespace":
  8258. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  8259. if (next) this.next();
  8260. return this.tsParseModuleOrNamespaceDeclaration(node);
  8261. }
  8262. break;
  8263. case "type":
  8264. if (this.tsCheckLineTerminatorAndMatch(types.name, next)) {
  8265. if (next) this.next();
  8266. return this.tsParseTypeAliasDeclaration(node);
  8267. }
  8268. break;
  8269. }
  8270. }
  8271. tsCheckLineTerminatorAndMatch(tokenType, next) {
  8272. return (next || this.match(tokenType)) && !this.isLineTerminator();
  8273. }
  8274. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  8275. const res = this.tsTryParseAndCatch(() => {
  8276. const node = this.startNodeAt(startPos, startLoc);
  8277. node.typeParameters = this.tsParseTypeParameters();
  8278. super.parseFunctionParams(node);
  8279. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8280. this.expect(types.arrow);
  8281. return node;
  8282. });
  8283. if (!res) {
  8284. return undefined;
  8285. }
  8286. return this.parseArrowExpression(res, null, true);
  8287. }
  8288. tsParseTypeArguments() {
  8289. const node = this.startNode();
  8290. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8291. this.expectRelational("<");
  8292. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8293. }));
  8294. this.state.exprAllowed = false;
  8295. this.expectRelational(">");
  8296. return this.finishNode(node, "TSTypeParameterInstantiation");
  8297. }
  8298. tsIsDeclarationStart() {
  8299. if (this.match(types.name)) {
  8300. switch (this.state.value) {
  8301. case "abstract":
  8302. case "declare":
  8303. case "enum":
  8304. case "interface":
  8305. case "module":
  8306. case "namespace":
  8307. case "type":
  8308. return true;
  8309. }
  8310. }
  8311. return false;
  8312. }
  8313. isExportDefaultSpecifier() {
  8314. if (this.tsIsDeclarationStart()) return false;
  8315. return super.isExportDefaultSpecifier();
  8316. }
  8317. parseAssignableListItem(allowModifiers, decorators) {
  8318. const startPos = this.state.start;
  8319. const startLoc = this.state.startLoc;
  8320. let accessibility;
  8321. let readonly = false;
  8322. if (allowModifiers) {
  8323. accessibility = this.parseAccessModifier();
  8324. readonly = !!this.tsParseModifier(["readonly"]);
  8325. }
  8326. const left = this.parseMaybeDefault();
  8327. this.parseAssignableListItemTypes(left);
  8328. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8329. if (accessibility || readonly) {
  8330. const pp = this.startNodeAt(startPos, startLoc);
  8331. if (decorators.length) {
  8332. pp.decorators = decorators;
  8333. }
  8334. if (accessibility) pp.accessibility = accessibility;
  8335. if (readonly) pp.readonly = readonly;
  8336. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8337. throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern.");
  8338. }
  8339. pp.parameter = elt;
  8340. return this.finishNode(pp, "TSParameterProperty");
  8341. }
  8342. if (decorators.length) {
  8343. left.decorators = decorators;
  8344. }
  8345. return elt;
  8346. }
  8347. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8348. if (this.match(types.colon)) {
  8349. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  8350. }
  8351. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  8352. if (bodilessType && !this.match(types.braceL) && this.isLineTerminator()) {
  8353. this.finishNode(node, bodilessType);
  8354. return;
  8355. }
  8356. super.parseFunctionBodyAndFinish(node, type, isMethod);
  8357. }
  8358. parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow) {
  8359. if (!this.hasPrecedingLineBreak() && this.match(types.bang)) {
  8360. this.state.exprAllowed = false;
  8361. this.next();
  8362. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  8363. nonNullExpression.expression = base;
  8364. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8365. }
  8366. if (this.isRelational("<")) {
  8367. const result = this.tsTryParseAndCatch(() => {
  8368. if (!noCalls && this.atPossibleAsync(base)) {
  8369. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  8370. if (asyncArrowFn) {
  8371. return asyncArrowFn;
  8372. }
  8373. }
  8374. const node = this.startNodeAt(startPos, startLoc);
  8375. node.callee = base;
  8376. const typeArguments = this.tsParseTypeArguments();
  8377. if (typeArguments) {
  8378. if (!noCalls && this.eat(types.parenL)) {
  8379. node.arguments = this.parseCallExpressionArguments(types.parenR, false);
  8380. node.typeParameters = typeArguments;
  8381. return this.finishCallExpression(node);
  8382. } else if (this.match(types.backQuote)) {
  8383. return this.parseTaggedTemplateExpression(startPos, startLoc, base, state, typeArguments);
  8384. }
  8385. }
  8386. this.unexpected();
  8387. });
  8388. if (result) return result;
  8389. }
  8390. return super.parseSubscript(base, startPos, startLoc, noCalls, state, maybeAsyncArrow);
  8391. }
  8392. parseNewArguments(node) {
  8393. if (this.isRelational("<")) {
  8394. const typeParameters = this.tsTryParseAndCatch(() => {
  8395. const args = this.tsParseTypeArguments();
  8396. if (!this.match(types.parenL)) this.unexpected();
  8397. return args;
  8398. });
  8399. if (typeParameters) {
  8400. node.typeParameters = typeParameters;
  8401. }
  8402. }
  8403. super.parseNewArguments(node);
  8404. }
  8405. parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn) {
  8406. if (nonNull(types._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  8407. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8408. node.expression = left;
  8409. const _const = this.tsTryNextParseConstantContext();
  8410. if (_const) {
  8411. this.tsCheckLiteralForConstantContext(node.expression);
  8412. node.typeAnnotation = _const;
  8413. } else {
  8414. node.typeAnnotation = this.tsNextThenParseType();
  8415. }
  8416. this.finishNode(node, "TSAsExpression");
  8417. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn);
  8418. }
  8419. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn);
  8420. }
  8421. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  8422. checkDuplicateExports() {}
  8423. parseImport(node) {
  8424. if (this.match(types.name) && this.lookahead().type === types.eq) {
  8425. return this.tsParseImportEqualsDeclaration(node);
  8426. }
  8427. return super.parseImport(node);
  8428. }
  8429. parseExport(node) {
  8430. if (this.match(types._import)) {
  8431. this.expect(types._import);
  8432. return this.tsParseImportEqualsDeclaration(node, true);
  8433. } else if (this.eat(types.eq)) {
  8434. const assign = node;
  8435. assign.expression = this.parseExpression();
  8436. this.semicolon();
  8437. return this.finishNode(assign, "TSExportAssignment");
  8438. } else if (this.eatContextual("as")) {
  8439. const decl = node;
  8440. this.expectContextual("namespace");
  8441. decl.id = this.parseIdentifier();
  8442. this.semicolon();
  8443. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  8444. } else {
  8445. return super.parseExport(node);
  8446. }
  8447. }
  8448. isAbstractClass() {
  8449. return this.isContextual("abstract") && this.lookahead().type === types._class;
  8450. }
  8451. parseExportDefaultExpression() {
  8452. if (this.isAbstractClass()) {
  8453. const cls = this.startNode();
  8454. this.next();
  8455. this.parseClass(cls, true, true);
  8456. cls.abstract = true;
  8457. return cls;
  8458. }
  8459. if (this.state.value === "interface") {
  8460. const result = this.tsParseDeclaration(this.startNode(), this.state.value, true);
  8461. if (result) return result;
  8462. }
  8463. return super.parseExportDefaultExpression();
  8464. }
  8465. parseStatementContent(context, topLevel) {
  8466. if (this.state.type === types._const) {
  8467. const ahead = this.lookahead();
  8468. if (ahead.type === types.name && ahead.value === "enum") {
  8469. const node = this.startNode();
  8470. this.expect(types._const);
  8471. this.expectContextual("enum");
  8472. return this.tsParseEnumDeclaration(node, true);
  8473. }
  8474. }
  8475. return super.parseStatementContent(context, topLevel);
  8476. }
  8477. parseAccessModifier() {
  8478. return this.tsParseModifier(["public", "protected", "private"]);
  8479. }
  8480. parseClassMember(classBody, member, state, constructorAllowsSuper) {
  8481. const accessibility = this.parseAccessModifier();
  8482. if (accessibility) member.accessibility = accessibility;
  8483. super.parseClassMember(classBody, member, state, constructorAllowsSuper);
  8484. }
  8485. parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper) {
  8486. const methodOrProp = member;
  8487. const prop = member;
  8488. const propOrIdx = member;
  8489. let abstract = false,
  8490. readonly = false;
  8491. const mod = this.tsParseModifier(["abstract", "readonly"]);
  8492. switch (mod) {
  8493. case "readonly":
  8494. readonly = true;
  8495. abstract = !!this.tsParseModifier(["abstract"]);
  8496. break;
  8497. case "abstract":
  8498. abstract = true;
  8499. readonly = !!this.tsParseModifier(["readonly"]);
  8500. break;
  8501. }
  8502. if (abstract) methodOrProp.abstract = true;
  8503. if (readonly) propOrIdx.readonly = true;
  8504. if (!abstract && !isStatic && !methodOrProp.accessibility) {
  8505. const idx = this.tsTryParseIndexSignature(member);
  8506. if (idx) {
  8507. classBody.body.push(idx);
  8508. return;
  8509. }
  8510. }
  8511. if (readonly) {
  8512. methodOrProp.static = isStatic;
  8513. this.parseClassPropertyName(prop);
  8514. this.parsePostMemberNameModifiers(methodOrProp);
  8515. this.pushClassProperty(classBody, prop);
  8516. return;
  8517. }
  8518. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic, constructorAllowsSuper);
  8519. }
  8520. parsePostMemberNameModifiers(methodOrProp) {
  8521. const optional = this.eat(types.question);
  8522. if (optional) methodOrProp.optional = true;
  8523. }
  8524. parseExpressionStatement(node, expr) {
  8525. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  8526. return decl || super.parseExpressionStatement(node, expr);
  8527. }
  8528. shouldParseExportDeclaration() {
  8529. if (this.tsIsDeclarationStart()) return true;
  8530. return super.shouldParseExportDeclaration();
  8531. }
  8532. parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos) {
  8533. if (!refNeedsArrowPos || !this.match(types.question)) {
  8534. return super.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos);
  8535. }
  8536. const state = this.state.clone();
  8537. try {
  8538. return super.parseConditional(expr, noIn, startPos, startLoc);
  8539. } catch (err) {
  8540. if (!(err instanceof SyntaxError)) {
  8541. throw err;
  8542. }
  8543. this.state = state;
  8544. refNeedsArrowPos.start = err.pos || this.state.start;
  8545. return expr;
  8546. }
  8547. }
  8548. parseParenItem(node, startPos, startLoc) {
  8549. node = super.parseParenItem(node, startPos, startLoc);
  8550. if (this.eat(types.question)) {
  8551. node.optional = true;
  8552. }
  8553. if (this.match(types.colon)) {
  8554. const typeCastNode = this.startNodeAt(startPos, startLoc);
  8555. typeCastNode.expression = node;
  8556. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  8557. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  8558. }
  8559. return this.finishNode(node, node.type);
  8560. }
  8561. parseExportDeclaration(node) {
  8562. const startPos = this.state.start;
  8563. const startLoc = this.state.startLoc;
  8564. const isDeclare = this.eatContextual("declare");
  8565. let declaration;
  8566. if (this.match(types.name)) {
  8567. declaration = this.tsTryParseExportDeclaration();
  8568. }
  8569. if (!declaration) {
  8570. declaration = super.parseExportDeclaration(node);
  8571. }
  8572. if (declaration && isDeclare) {
  8573. this.resetStartLocation(declaration, startPos, startLoc);
  8574. declaration.declare = true;
  8575. }
  8576. return declaration;
  8577. }
  8578. parseClassId(node, isStatement, optionalId) {
  8579. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  8580. return;
  8581. }
  8582. super.parseClassId(...arguments);
  8583. const typeParameters = this.tsTryParseTypeParameters();
  8584. if (typeParameters) node.typeParameters = typeParameters;
  8585. }
  8586. parseClassProperty(node) {
  8587. if (!node.optional && this.eat(types.bang)) {
  8588. node.definite = true;
  8589. }
  8590. const type = this.tsTryParseTypeAnnotation();
  8591. if (type) node.typeAnnotation = type;
  8592. return super.parseClassProperty(node);
  8593. }
  8594. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  8595. const typeParameters = this.tsTryParseTypeParameters();
  8596. if (typeParameters) method.typeParameters = typeParameters;
  8597. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  8598. }
  8599. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  8600. const typeParameters = this.tsTryParseTypeParameters();
  8601. if (typeParameters) method.typeParameters = typeParameters;
  8602. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  8603. }
  8604. parseClassSuper(node) {
  8605. super.parseClassSuper(node);
  8606. if (node.superClass && this.isRelational("<")) {
  8607. node.superTypeParameters = this.tsParseTypeArguments();
  8608. }
  8609. if (this.eatContextual("implements")) {
  8610. node.implements = this.tsParseHeritageClause("implements");
  8611. }
  8612. }
  8613. parseObjPropValue(prop, ...args) {
  8614. const typeParameters = this.tsTryParseTypeParameters();
  8615. if (typeParameters) prop.typeParameters = typeParameters;
  8616. super.parseObjPropValue(prop, ...args);
  8617. }
  8618. parseFunctionParams(node, allowModifiers) {
  8619. const typeParameters = this.tsTryParseTypeParameters();
  8620. if (typeParameters) node.typeParameters = typeParameters;
  8621. super.parseFunctionParams(node, allowModifiers);
  8622. }
  8623. parseVarId(decl, kind) {
  8624. super.parseVarId(decl, kind);
  8625. if (decl.id.type === "Identifier" && this.eat(types.bang)) {
  8626. decl.definite = true;
  8627. }
  8628. const type = this.tsTryParseTypeAnnotation();
  8629. if (type) {
  8630. decl.id.typeAnnotation = type;
  8631. this.finishNode(decl.id, decl.id.type);
  8632. }
  8633. }
  8634. parseAsyncArrowFromCallExpression(node, call) {
  8635. if (this.match(types.colon)) {
  8636. node.returnType = this.tsParseTypeAnnotation();
  8637. }
  8638. return super.parseAsyncArrowFromCallExpression(node, call);
  8639. }
  8640. parseMaybeAssign(...args) {
  8641. let jsxError;
  8642. if (this.match(types.jsxTagStart)) {
  8643. const context = this.curContext();
  8644. assert(context === types$1.j_oTag);
  8645. assert(this.state.context[this.state.context.length - 2] === types$1.j_expr);
  8646. const state = this.state.clone();
  8647. try {
  8648. return super.parseMaybeAssign(...args);
  8649. } catch (err) {
  8650. if (!(err instanceof SyntaxError)) {
  8651. throw err;
  8652. }
  8653. this.state = state;
  8654. assert(this.curContext() === types$1.j_oTag);
  8655. this.state.context.pop();
  8656. assert(this.curContext() === types$1.j_expr);
  8657. this.state.context.pop();
  8658. jsxError = err;
  8659. }
  8660. }
  8661. if (jsxError === undefined && !this.isRelational("<")) {
  8662. return super.parseMaybeAssign(...args);
  8663. }
  8664. let arrowExpression;
  8665. let typeParameters;
  8666. const state = this.state.clone();
  8667. try {
  8668. typeParameters = this.tsParseTypeParameters();
  8669. arrowExpression = super.parseMaybeAssign(...args);
  8670. if (arrowExpression.type !== "ArrowFunctionExpression" || arrowExpression.extra && arrowExpression.extra.parenthesized) {
  8671. this.unexpected();
  8672. }
  8673. } catch (err) {
  8674. if (!(err instanceof SyntaxError)) {
  8675. throw err;
  8676. }
  8677. if (jsxError) {
  8678. throw jsxError;
  8679. }
  8680. assert(!this.hasPlugin("jsx"));
  8681. this.state = state;
  8682. return super.parseMaybeAssign(...args);
  8683. }
  8684. if (typeParameters && typeParameters.params.length !== 0) {
  8685. this.resetStartLocationFromNode(arrowExpression, typeParameters);
  8686. }
  8687. arrowExpression.typeParameters = typeParameters;
  8688. return arrowExpression;
  8689. }
  8690. parseMaybeUnary(refShorthandDefaultPos) {
  8691. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  8692. return this.tsParseTypeAssertion();
  8693. } else {
  8694. return super.parseMaybeUnary(refShorthandDefaultPos);
  8695. }
  8696. }
  8697. parseArrow(node) {
  8698. if (this.match(types.colon)) {
  8699. const state = this.state.clone();
  8700. try {
  8701. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types.colon);
  8702. if (this.canInsertSemicolon()) this.unexpected();
  8703. if (!this.match(types.arrow)) this.unexpected();
  8704. node.returnType = returnType;
  8705. } catch (err) {
  8706. if (err instanceof SyntaxError) {
  8707. this.state = state;
  8708. } else {
  8709. throw err;
  8710. }
  8711. }
  8712. }
  8713. return super.parseArrow(node);
  8714. }
  8715. parseAssignableListItemTypes(param) {
  8716. if (this.eat(types.question)) {
  8717. if (param.type !== "Identifier") {
  8718. throw this.raise(param.start, "A binding pattern parameter cannot be optional in an implementation signature.");
  8719. }
  8720. param.optional = true;
  8721. }
  8722. const type = this.tsTryParseTypeAnnotation();
  8723. if (type) param.typeAnnotation = type;
  8724. return this.finishNode(param, param.type);
  8725. }
  8726. toAssignable(node, isBinding, contextDescription) {
  8727. switch (node.type) {
  8728. case "TSTypeCastExpression":
  8729. return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription);
  8730. case "TSParameterProperty":
  8731. return super.toAssignable(node, isBinding, contextDescription);
  8732. case "TSAsExpression":
  8733. case "TSNonNullExpression":
  8734. case "TSTypeAssertion":
  8735. node.expression = this.toAssignable(node.expression, isBinding, contextDescription);
  8736. return node;
  8737. default:
  8738. return super.toAssignable(node, isBinding, contextDescription);
  8739. }
  8740. }
  8741. checkLVal(expr, bindingType = BIND_NONE, checkClashes, contextDescription) {
  8742. switch (expr.type) {
  8743. case "TSTypeCastExpression":
  8744. return;
  8745. case "TSParameterProperty":
  8746. this.checkLVal(expr.parameter, bindingType, checkClashes, "parameter property");
  8747. return;
  8748. case "TSAsExpression":
  8749. case "TSNonNullExpression":
  8750. case "TSTypeAssertion":
  8751. this.checkLVal(expr.expression, bindingType, checkClashes, contextDescription);
  8752. return;
  8753. default:
  8754. super.checkLVal(expr, bindingType, checkClashes, contextDescription);
  8755. return;
  8756. }
  8757. }
  8758. parseBindingAtom() {
  8759. switch (this.state.type) {
  8760. case types._this:
  8761. return this.parseIdentifier(true);
  8762. default:
  8763. return super.parseBindingAtom();
  8764. }
  8765. }
  8766. parseMaybeDecoratorArguments(expr) {
  8767. if (this.isRelational("<")) {
  8768. const typeArguments = this.tsParseTypeArguments();
  8769. if (this.match(types.parenL)) {
  8770. const call = super.parseMaybeDecoratorArguments(expr);
  8771. call.typeParameters = typeArguments;
  8772. return call;
  8773. }
  8774. this.unexpected(this.state.start, types.parenL);
  8775. }
  8776. return super.parseMaybeDecoratorArguments(expr);
  8777. }
  8778. isClassMethod() {
  8779. return this.isRelational("<") || super.isClassMethod();
  8780. }
  8781. isClassProperty() {
  8782. return this.match(types.bang) || this.match(types.colon) || super.isClassProperty();
  8783. }
  8784. parseMaybeDefault(...args) {
  8785. const node = super.parseMaybeDefault(...args);
  8786. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  8787. this.raise(node.typeAnnotation.start, "Type annotations must come before default assignments, " + "e.g. instead of `age = 25: number` use `age: number = 25`");
  8788. }
  8789. return node;
  8790. }
  8791. getTokenFromCode(code) {
  8792. if (this.state.inType && (code === 62 || code === 60)) {
  8793. return this.finishOp(types.relational, 1);
  8794. } else {
  8795. return super.getTokenFromCode(code);
  8796. }
  8797. }
  8798. toAssignableList(exprList, isBinding, contextDescription) {
  8799. for (let i = 0; i < exprList.length; i++) {
  8800. const expr = exprList[i];
  8801. if (!expr) continue;
  8802. switch (expr.type) {
  8803. case "TSTypeCastExpression":
  8804. exprList[i] = this.typeCastToParameter(expr);
  8805. break;
  8806. case "TSAsExpression":
  8807. case "TSTypeAssertion":
  8808. this.raise(expr.start, "Unexpected type cast in parameter position.");
  8809. break;
  8810. }
  8811. }
  8812. return super.toAssignableList(exprList, isBinding, contextDescription);
  8813. }
  8814. typeCastToParameter(node) {
  8815. node.expression.typeAnnotation = node.typeAnnotation;
  8816. return this.finishNodeAt(node.expression, node.expression.type, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  8817. }
  8818. toReferencedList(exprList, isInParens) {
  8819. for (let i = 0; i < exprList.length; i++) {
  8820. const expr = exprList[i];
  8821. if (expr && expr._exprListItem && expr.type === "TsTypeCastExpression") {
  8822. this.raise(expr.start, "Did not expect a type annotation here.");
  8823. }
  8824. }
  8825. return exprList;
  8826. }
  8827. shouldParseArrow() {
  8828. return this.match(types.colon) || super.shouldParseArrow();
  8829. }
  8830. shouldParseAsyncArrow() {
  8831. return this.match(types.colon) || super.shouldParseAsyncArrow();
  8832. }
  8833. canHaveLeadingDecorator() {
  8834. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  8835. }
  8836. jsxParseOpeningElementAfterName(node) {
  8837. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  8838. if (typeArguments) node.typeParameters = typeArguments;
  8839. return super.jsxParseOpeningElementAfterName(node);
  8840. }
  8841. getGetterSetterExpectedParamCount(method) {
  8842. const baseCount = super.getGetterSetterExpectedParamCount(method);
  8843. const firstParam = method.params[0];
  8844. const hasContextParam = firstParam && firstParam.type === "Identifier" && firstParam.name === "this";
  8845. return hasContextParam ? baseCount + 1 : baseCount;
  8846. }
  8847. });
  8848. types.placeholder = new TokenType("%%", {
  8849. startsExpr: true
  8850. });
  8851. var placeholders = (superClass => class extends superClass {
  8852. parsePlaceholder(expectedNode) {
  8853. if (this.match(types.placeholder)) {
  8854. const node = this.startNode();
  8855. this.next();
  8856. this.assertNoSpace("Unexpected space in placeholder.");
  8857. node.name = super.parseIdentifier(true);
  8858. this.assertNoSpace("Unexpected space in placeholder.");
  8859. this.expect(types.placeholder);
  8860. return this.finishPlaceholder(node, expectedNode);
  8861. }
  8862. }
  8863. finishPlaceholder(node, expectedNode) {
  8864. node.expectedNode = expectedNode;
  8865. return this.finishNode(node, "Placeholder");
  8866. }
  8867. getTokenFromCode(code) {
  8868. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  8869. return this.finishOp(types.placeholder, 2);
  8870. }
  8871. return super.getTokenFromCode(...arguments);
  8872. }
  8873. parseExprAtom() {
  8874. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  8875. }
  8876. parseIdentifier() {
  8877. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  8878. }
  8879. checkReservedWord(word) {
  8880. if (word !== undefined) super.checkReservedWord(...arguments);
  8881. }
  8882. parseBindingAtom() {
  8883. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  8884. }
  8885. checkLVal(expr) {
  8886. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  8887. }
  8888. toAssignable(node) {
  8889. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  8890. node.expectedNode = "Pattern";
  8891. return node;
  8892. }
  8893. return super.toAssignable(...arguments);
  8894. }
  8895. verifyBreakContinue(node) {
  8896. if (node.label && node.label.type === "Placeholder") return;
  8897. super.verifyBreakContinue(...arguments);
  8898. }
  8899. parseExpressionStatement(node, expr) {
  8900. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  8901. return super.parseExpressionStatement(...arguments);
  8902. }
  8903. if (this.match(types.colon)) {
  8904. const stmt = node;
  8905. stmt.label = this.finishPlaceholder(expr, "Identifier");
  8906. this.next();
  8907. stmt.body = this.parseStatement("label");
  8908. return this.finishNode(stmt, "LabeledStatement");
  8909. }
  8910. this.semicolon();
  8911. node.name = expr.name;
  8912. return this.finishPlaceholder(node, "Statement");
  8913. }
  8914. parseBlock() {
  8915. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  8916. }
  8917. parseFunctionId() {
  8918. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  8919. }
  8920. parseClass(node, isStatement, optionalId) {
  8921. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  8922. this.next();
  8923. this.takeDecorators(node);
  8924. const placeholder = this.parsePlaceholder("Identifier");
  8925. if (placeholder) {
  8926. if (this.match(types._extends) || this.match(types.placeholder) || this.match(types.braceL)) {
  8927. node.id = placeholder;
  8928. } else if (optionalId || !isStatement) {
  8929. node.id = null;
  8930. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  8931. return this.finishNode(node, type);
  8932. } else {
  8933. this.unexpected(null, "A class name is required");
  8934. }
  8935. } else {
  8936. this.parseClassId(node, isStatement, optionalId);
  8937. }
  8938. this.parseClassSuper(node);
  8939. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass);
  8940. return this.finishNode(node, type);
  8941. }
  8942. parseExport(node) {
  8943. const placeholder = this.parsePlaceholder("Identifier");
  8944. if (!placeholder) return super.parseExport(...arguments);
  8945. if (!this.isContextual("from") && !this.match(types.comma)) {
  8946. node.specifiers = [];
  8947. node.source = null;
  8948. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  8949. return this.finishNode(node, "ExportNamedDeclaration");
  8950. }
  8951. this.expectPlugin("exportDefaultFrom");
  8952. const specifier = this.startNode();
  8953. specifier.exported = placeholder;
  8954. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  8955. return super.parseExport(node);
  8956. }
  8957. maybeParseExportDefaultSpecifier(node) {
  8958. if (node.specifiers && node.specifiers.length > 0) {
  8959. return true;
  8960. }
  8961. return super.maybeParseExportDefaultSpecifier(...arguments);
  8962. }
  8963. checkExport(node) {
  8964. const {
  8965. specifiers
  8966. } = node;
  8967. if (specifiers && specifiers.length) {
  8968. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  8969. }
  8970. super.checkExport(node);
  8971. node.specifiers = specifiers;
  8972. }
  8973. parseImport(node) {
  8974. const placeholder = this.parsePlaceholder("Identifier");
  8975. if (!placeholder) return super.parseImport(...arguments);
  8976. node.specifiers = [];
  8977. if (!this.isContextual("from") && !this.match(types.comma)) {
  8978. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  8979. this.semicolon();
  8980. return this.finishNode(node, "ImportDeclaration");
  8981. }
  8982. const specifier = this.startNodeAtNode(placeholder);
  8983. specifier.local = placeholder;
  8984. this.finishNode(specifier, "ImportDefaultSpecifier");
  8985. node.specifiers.push(specifier);
  8986. if (this.eat(types.comma)) {
  8987. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  8988. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  8989. }
  8990. this.expectContextual("from");
  8991. node.source = this.parseImportSource();
  8992. this.semicolon();
  8993. return this.finishNode(node, "ImportDeclaration");
  8994. }
  8995. parseImportSource() {
  8996. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  8997. }
  8998. });
  8999. function hasPlugin(plugins, name) {
  9000. return plugins.some(plugin => {
  9001. if (Array.isArray(plugin)) {
  9002. return plugin[0] === name;
  9003. } else {
  9004. return plugin === name;
  9005. }
  9006. });
  9007. }
  9008. function getPluginOption(plugins, name, option) {
  9009. const plugin = plugins.find(plugin => {
  9010. if (Array.isArray(plugin)) {
  9011. return plugin[0] === name;
  9012. } else {
  9013. return plugin === name;
  9014. }
  9015. });
  9016. if (plugin && Array.isArray(plugin)) {
  9017. return plugin[1][option];
  9018. }
  9019. return null;
  9020. }
  9021. const PIPELINE_PROPOSALS = ["minimal", "smart"];
  9022. function validatePlugins(plugins) {
  9023. if (hasPlugin(plugins, "decorators")) {
  9024. if (hasPlugin(plugins, "decorators-legacy")) {
  9025. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  9026. }
  9027. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  9028. if (decoratorsBeforeExport == null) {
  9029. 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'.");
  9030. } else if (typeof decoratorsBeforeExport !== "boolean") {
  9031. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  9032. }
  9033. }
  9034. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  9035. throw new Error("Cannot combine flow and typescript plugins.");
  9036. }
  9037. if (hasPlugin(plugins, "pipelineOperator") && !PIPELINE_PROPOSALS.includes(getPluginOption(plugins, "pipelineOperator", "proposal"))) {
  9038. throw new Error("'pipelineOperator' requires 'proposal' option whose value should be one of: " + PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "));
  9039. }
  9040. }
  9041. const mixinPlugins = {
  9042. estree,
  9043. jsx,
  9044. flow,
  9045. typescript,
  9046. placeholders
  9047. };
  9048. const mixinPluginNames = Object.keys(mixinPlugins);
  9049. function parse(input, options) {
  9050. if (options && options.sourceType === "unambiguous") {
  9051. options = Object.assign({}, options);
  9052. try {
  9053. options.sourceType = "module";
  9054. const parser = getParser(options, input);
  9055. const ast = parser.parse();
  9056. if (!parser.sawUnambiguousESM) ast.program.sourceType = "script";
  9057. return ast;
  9058. } catch (moduleError) {
  9059. try {
  9060. options.sourceType = "script";
  9061. return getParser(options, input).parse();
  9062. } catch (scriptError) {}
  9063. throw moduleError;
  9064. }
  9065. } else {
  9066. return getParser(options, input).parse();
  9067. }
  9068. }
  9069. function parseExpression(input, options) {
  9070. const parser = getParser(options, input);
  9071. if (parser.options.strictMode) {
  9072. parser.state.strict = true;
  9073. }
  9074. return parser.getExpression();
  9075. }
  9076. function getParser(options, input) {
  9077. let cls = Parser;
  9078. if (options && options.plugins) {
  9079. validatePlugins(options.plugins);
  9080. cls = getParserClass(options.plugins);
  9081. }
  9082. return new cls(options, input);
  9083. }
  9084. const parserClassCache = {};
  9085. function getParserClass(pluginsFromOptions) {
  9086. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  9087. const key = pluginList.join("/");
  9088. let cls = parserClassCache[key];
  9089. if (!cls) {
  9090. cls = Parser;
  9091. for (let _i = 0; _i < pluginList.length; _i++) {
  9092. const plugin = pluginList[_i];
  9093. cls = mixinPlugins[plugin](cls);
  9094. }
  9095. parserClassCache[key] = cls;
  9096. }
  9097. return cls;
  9098. }
  9099. exports.parse = parse;
  9100. exports.parseExpression = parseExpression;
  9101. exports.tokTypes = types;