index.js 480 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. function _objectWithoutPropertiesLoose(source, excluded) {
  6. if (source == null) return {};
  7. var target = {};
  8. var sourceKeys = Object.keys(source);
  9. var key, i;
  10. for (i = 0; i < sourceKeys.length; i++) {
  11. key = sourceKeys[i];
  12. if (excluded.indexOf(key) >= 0) continue;
  13. target[key] = source[key];
  14. }
  15. return target;
  16. }
  17. class Position {
  18. constructor(line, col, index) {
  19. this.line = void 0;
  20. this.column = void 0;
  21. this.index = void 0;
  22. this.line = line;
  23. this.column = col;
  24. this.index = index;
  25. }
  26. }
  27. class SourceLocation {
  28. constructor(start, end) {
  29. this.start = void 0;
  30. this.end = void 0;
  31. this.filename = void 0;
  32. this.identifierName = void 0;
  33. this.start = start;
  34. this.end = end;
  35. }
  36. }
  37. function createPositionWithColumnOffset(position, columnOffset) {
  38. const {
  39. line,
  40. column,
  41. index
  42. } = position;
  43. return new Position(line, column + columnOffset, index + columnOffset);
  44. }
  45. var ParseErrorCode = {
  46. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  47. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  48. };
  49. const reflect = (keys, last = keys.length - 1) => ({
  50. get() {
  51. return keys.reduce((object, key) => object[key], this);
  52. },
  53. set(value) {
  54. keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
  55. }
  56. });
  57. const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
  58. value: descriptor,
  59. enumerable: false
  60. } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
  61. configurable: true
  62. }, descriptor)), Object.assign(new constructor(), properties));
  63. var ModuleErrors = {
  64. ImportMetaOutsideModule: {
  65. message: `import.meta may appear only with 'sourceType: "module"'`,
  66. code: ParseErrorCode.SourceTypeModuleError
  67. },
  68. ImportOutsideModule: {
  69. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  70. code: ParseErrorCode.SourceTypeModuleError
  71. }
  72. };
  73. const NodeDescriptions = {
  74. ArrayPattern: "array destructuring pattern",
  75. AssignmentExpression: "assignment expression",
  76. AssignmentPattern: "assignment expression",
  77. ArrowFunctionExpression: "arrow function expression",
  78. ConditionalExpression: "conditional expression",
  79. CatchClause: "catch clause",
  80. ForOfStatement: "for-of statement",
  81. ForInStatement: "for-in statement",
  82. ForStatement: "for-loop",
  83. FormalParameters: "function parameter list",
  84. Identifier: "identifier",
  85. ImportSpecifier: "import specifier",
  86. ImportDefaultSpecifier: "import default specifier",
  87. ImportNamespaceSpecifier: "import namespace specifier",
  88. ObjectPattern: "object destructuring pattern",
  89. ParenthesizedExpression: "parenthesized expression",
  90. RestElement: "rest element",
  91. UpdateExpression: {
  92. true: "prefix operation",
  93. false: "postfix operation"
  94. },
  95. VariableDeclarator: "variable declaration",
  96. YieldExpression: "yield expression"
  97. };
  98. const toNodeDescription = ({
  99. type,
  100. prefix
  101. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  102. var StandardErrors = {
  103. AccessorIsGenerator: ({
  104. kind
  105. }) => `A ${kind}ter cannot be a generator.`,
  106. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  107. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  108. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  109. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  110. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  111. AwaitUsingNotInAsyncContext: "'await using' is only allowed within async functions and at the top levels of modules.",
  112. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  113. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  114. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  115. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  116. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  117. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  118. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  119. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  120. ConstructorIsAsync: "Constructor can't be an async function.",
  121. ConstructorIsGenerator: "Constructor can't be a generator.",
  122. DeclarationMissingInitializer: ({
  123. kind
  124. }) => `Missing initializer in ${kind} declaration.`,
  125. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  126. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  127. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  128. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  129. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  130. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  131. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  132. DeletePrivateField: "Deleting a private field is not allowed.",
  133. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  134. DuplicateConstructor: "Duplicate constructor in the same class.",
  135. DuplicateDefaultExport: "Only one default export allowed per module.",
  136. DuplicateExport: ({
  137. exportName
  138. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  139. DuplicateProto: "Redefinition of __proto__ property.",
  140. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  141. ElementAfterRest: "Rest element must be last element.",
  142. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  143. ExportBindingIsString: ({
  144. localName,
  145. exportName
  146. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  147. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  148. ForInOfLoopInitializer: ({
  149. type
  150. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  151. ForInUsing: "For-in loop may not start with 'using' declaration.",
  152. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  153. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  154. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  155. IllegalBreakContinue: ({
  156. type
  157. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  158. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  159. IllegalReturn: "'return' outside of function.",
  160. ImportAttributesUseAssert: "The `assert` keyword in import attributes is deprecated and it has been replaced by the `with` keyword. You can enable the `deprecatedAssertSyntax: true` option in the import attributes plugin to suppress this error.",
  161. ImportBindingIsString: ({
  162. importName
  163. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  164. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  165. ImportCallArity: ({
  166. maxArgumentCount
  167. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  168. ImportCallNotNewExpression: "Cannot use new with import(...).",
  169. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  170. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  171. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  172. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  173. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  174. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  175. InvalidCodePoint: "Code point out of bounds.",
  176. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  177. InvalidDecimal: "Invalid decimal.",
  178. InvalidDigit: ({
  179. radix
  180. }) => `Expected number in radix ${radix}.`,
  181. InvalidEscapeSequence: "Bad character escape sequence.",
  182. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  183. InvalidEscapedReservedWord: ({
  184. reservedWord
  185. }) => `Escape sequence in keyword ${reservedWord}.`,
  186. InvalidIdentifier: ({
  187. identifierName
  188. }) => `Invalid identifier ${identifierName}.`,
  189. InvalidLhs: ({
  190. ancestor
  191. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  192. InvalidLhsBinding: ({
  193. ancestor
  194. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  195. InvalidNumber: "Invalid number.",
  196. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  197. InvalidOrUnexpectedToken: ({
  198. unexpected
  199. }) => `Unexpected character '${unexpected}'.`,
  200. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  201. InvalidPrivateFieldResolution: ({
  202. identifierName
  203. }) => `Private name #${identifierName} is not defined.`,
  204. InvalidPropertyBindingPattern: "Binding member expression.",
  205. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  206. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  207. LabelRedeclaration: ({
  208. labelName
  209. }) => `Label '${labelName}' is already declared.`,
  210. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  211. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  212. MalformedRegExpFlags: "Invalid regular expression flag.",
  213. MissingClassName: "A class name is required.",
  214. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  215. MissingSemicolon: "Missing semicolon.",
  216. MissingPlugin: ({
  217. missingPlugin
  218. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  219. MissingOneOfPlugins: ({
  220. missingPlugin
  221. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  222. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  223. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  224. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  225. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  226. ModuleAttributesWithDuplicateKeys: ({
  227. key
  228. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  229. ModuleExportNameHasLoneSurrogate: ({
  230. surrogateCharCode
  231. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  232. ModuleExportUndefined: ({
  233. localName
  234. }) => `Export '${localName}' is not defined.`,
  235. MultipleDefaultsInSwitch: "Multiple default clauses.",
  236. NewlineAfterThrow: "Illegal newline after throw.",
  237. NoCatchOrFinally: "Missing catch or finally clause.",
  238. NumberIdentifier: "Identifier directly after number.",
  239. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  240. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  241. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  242. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  243. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  244. ParamDupe: "Argument name clash.",
  245. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  246. PatternHasMethod: "Object pattern can't contain methods.",
  247. PrivateInExpectedIn: ({
  248. identifierName
  249. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  250. PrivateNameRedeclaration: ({
  251. identifierName
  252. }) => `Duplicate private name #${identifierName}.`,
  253. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  254. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  255. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  256. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  257. RestTrailingComma: "Unexpected trailing comma after rest element.",
  258. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  259. SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  260. StaticPrototype: "Classes may not have static property named prototype.",
  261. SuperNotAllowed: "`super()` is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class?",
  262. SuperPrivateField: "Private fields can't be accessed on super.",
  263. TrailingDecorator: "Decorators must be attached to a class element.",
  264. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  265. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  266. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  267. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  268. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  269. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  270. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  271. UnexpectedKeyword: ({
  272. keyword
  273. }) => `Unexpected keyword '${keyword}'.`,
  274. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  275. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  276. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  277. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  278. UnexpectedPrivateField: "Unexpected private name.",
  279. UnexpectedReservedWord: ({
  280. reservedWord
  281. }) => `Unexpected reserved word '${reservedWord}'.`,
  282. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  283. UnexpectedToken: ({
  284. expected,
  285. unexpected
  286. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  287. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  288. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
  289. UnsupportedBind: "Binding should be performed on object property.",
  290. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  291. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  292. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  293. UnsupportedMetaProperty: ({
  294. target,
  295. onlyValidPropertyName
  296. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  297. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  298. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  299. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  300. UnterminatedComment: "Unterminated comment.",
  301. UnterminatedRegExp: "Unterminated regular expression.",
  302. UnterminatedString: "Unterminated string constant.",
  303. UnterminatedTemplate: "Unterminated template.",
  304. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  305. VarRedeclaration: ({
  306. identifierName
  307. }) => `Identifier '${identifierName}' has already been declared.`,
  308. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  309. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  310. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  311. };
  312. var StrictModeErrors = {
  313. StrictDelete: "Deleting local variable in strict mode.",
  314. StrictEvalArguments: ({
  315. referenceName
  316. }) => `Assigning to '${referenceName}' in strict mode.`,
  317. StrictEvalArgumentsBinding: ({
  318. bindingName
  319. }) => `Binding '${bindingName}' in strict mode.`,
  320. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  321. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  322. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  323. StrictWith: "'with' in strict mode."
  324. };
  325. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  326. var PipelineOperatorErrors = {
  327. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  328. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  329. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  330. PipeTopicUnconfiguredToken: ({
  331. token
  332. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  333. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  334. PipeUnparenthesizedBody: ({
  335. type
  336. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  337. type
  338. })}; please wrap it in parentheses.`,
  339. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  340. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  341. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  342. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  343. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  344. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  345. };
  346. const _excluded$1 = ["toMessage"],
  347. _excluded2$1 = ["message"];
  348. function toParseErrorConstructor(_ref) {
  349. let {
  350. toMessage
  351. } = _ref,
  352. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  353. return function constructor({
  354. loc,
  355. details
  356. }) {
  357. return instantiate(SyntaxError, Object.assign({}, properties, {
  358. loc
  359. }), {
  360. clone(overrides = {}) {
  361. const loc = overrides.loc || {};
  362. return constructor({
  363. loc: new Position("line" in loc ? loc.line : this.loc.line, "column" in loc ? loc.column : this.loc.column, "index" in loc ? loc.index : this.loc.index),
  364. details: Object.assign({}, this.details, overrides.details)
  365. });
  366. },
  367. details: {
  368. value: details,
  369. enumerable: false
  370. },
  371. message: {
  372. get() {
  373. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  374. },
  375. set(value) {
  376. Object.defineProperty(this, "message", {
  377. value
  378. });
  379. }
  380. },
  381. pos: {
  382. reflect: "loc.index",
  383. enumerable: true
  384. },
  385. missingPlugin: "missingPlugin" in details && {
  386. reflect: "details.missingPlugin",
  387. enumerable: true
  388. }
  389. });
  390. };
  391. }
  392. function ParseErrorEnum(argument, syntaxPlugin) {
  393. if (Array.isArray(argument)) {
  394. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  395. }
  396. const ParseErrorConstructors = {};
  397. for (const reasonCode of Object.keys(argument)) {
  398. const template = argument[reasonCode];
  399. const _ref2 = typeof template === "string" ? {
  400. message: () => template
  401. } : typeof template === "function" ? {
  402. message: template
  403. } : template,
  404. {
  405. message
  406. } = _ref2,
  407. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
  408. const toMessage = typeof message === "string" ? () => message : message;
  409. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  410. code: ParseErrorCode.SyntaxError,
  411. reasonCode,
  412. toMessage
  413. }, syntaxPlugin ? {
  414. syntaxPlugin
  415. } : {}, rest));
  416. }
  417. return ParseErrorConstructors;
  418. }
  419. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  420. const {
  421. defineProperty
  422. } = Object;
  423. const toUnenumerable = (object, key) => defineProperty(object, key, {
  424. enumerable: false,
  425. value: object[key]
  426. });
  427. function toESTreeLocation(node) {
  428. node.loc.start && toUnenumerable(node.loc.start, "index");
  429. node.loc.end && toUnenumerable(node.loc.end, "index");
  430. return node;
  431. }
  432. var estree = superClass => class ESTreeParserMixin extends superClass {
  433. parse() {
  434. const file = toESTreeLocation(super.parse());
  435. if (this.options.tokens) {
  436. file.tokens = file.tokens.map(toESTreeLocation);
  437. }
  438. return file;
  439. }
  440. parseRegExpLiteral({
  441. pattern,
  442. flags
  443. }) {
  444. let regex = null;
  445. try {
  446. regex = new RegExp(pattern, flags);
  447. } catch (e) {}
  448. const node = this.estreeParseLiteral(regex);
  449. node.regex = {
  450. pattern,
  451. flags
  452. };
  453. return node;
  454. }
  455. parseBigIntLiteral(value) {
  456. let bigInt;
  457. try {
  458. bigInt = BigInt(value);
  459. } catch (_unused) {
  460. bigInt = null;
  461. }
  462. const node = this.estreeParseLiteral(bigInt);
  463. node.bigint = String(node.value || value);
  464. return node;
  465. }
  466. parseDecimalLiteral(value) {
  467. const decimal = null;
  468. const node = this.estreeParseLiteral(decimal);
  469. node.decimal = String(node.value || value);
  470. return node;
  471. }
  472. estreeParseLiteral(value) {
  473. return this.parseLiteral(value, "Literal");
  474. }
  475. parseStringLiteral(value) {
  476. return this.estreeParseLiteral(value);
  477. }
  478. parseNumericLiteral(value) {
  479. return this.estreeParseLiteral(value);
  480. }
  481. parseNullLiteral() {
  482. return this.estreeParseLiteral(null);
  483. }
  484. parseBooleanLiteral(value) {
  485. return this.estreeParseLiteral(value);
  486. }
  487. directiveToStmt(directive) {
  488. const expression = directive.value;
  489. delete directive.value;
  490. expression.type = "Literal";
  491. expression.raw = expression.extra.raw;
  492. expression.value = expression.extra.expressionValue;
  493. const stmt = directive;
  494. stmt.type = "ExpressionStatement";
  495. stmt.expression = expression;
  496. stmt.directive = expression.extra.rawValue;
  497. delete expression.extra;
  498. return stmt;
  499. }
  500. initFunction(node, isAsync) {
  501. super.initFunction(node, isAsync);
  502. node.expression = false;
  503. }
  504. checkDeclaration(node) {
  505. if (node != null && this.isObjectProperty(node)) {
  506. this.checkDeclaration(node.value);
  507. } else {
  508. super.checkDeclaration(node);
  509. }
  510. }
  511. getObjectOrClassMethodParams(method) {
  512. return method.value.params;
  513. }
  514. isValidDirective(stmt) {
  515. var _stmt$expression$extr;
  516. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  517. }
  518. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  519. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  520. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  521. node.body = directiveStatements.concat(node.body);
  522. delete node.directives;
  523. }
  524. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  525. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  526. if (method.typeParameters) {
  527. method.value.typeParameters = method.typeParameters;
  528. delete method.typeParameters;
  529. }
  530. classBody.body.push(method);
  531. }
  532. parsePrivateName() {
  533. const node = super.parsePrivateName();
  534. {
  535. if (!this.getPluginOption("estree", "classFeatures")) {
  536. return node;
  537. }
  538. }
  539. return this.convertPrivateNameToPrivateIdentifier(node);
  540. }
  541. convertPrivateNameToPrivateIdentifier(node) {
  542. const name = super.getPrivateNameSV(node);
  543. node = node;
  544. delete node.id;
  545. node.name = name;
  546. node.type = "PrivateIdentifier";
  547. return node;
  548. }
  549. isPrivateName(node) {
  550. {
  551. if (!this.getPluginOption("estree", "classFeatures")) {
  552. return super.isPrivateName(node);
  553. }
  554. }
  555. return node.type === "PrivateIdentifier";
  556. }
  557. getPrivateNameSV(node) {
  558. {
  559. if (!this.getPluginOption("estree", "classFeatures")) {
  560. return super.getPrivateNameSV(node);
  561. }
  562. }
  563. return node.name;
  564. }
  565. parseLiteral(value, type) {
  566. const node = super.parseLiteral(value, type);
  567. node.raw = node.extra.raw;
  568. delete node.extra;
  569. return node;
  570. }
  571. parseFunctionBody(node, allowExpression, isMethod = false) {
  572. super.parseFunctionBody(node, allowExpression, isMethod);
  573. node.expression = node.body.type !== "BlockStatement";
  574. }
  575. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  576. let funcNode = this.startNode();
  577. funcNode.kind = node.kind;
  578. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  579. funcNode.type = "FunctionExpression";
  580. delete funcNode.kind;
  581. node.value = funcNode;
  582. if (type === "ClassPrivateMethod") {
  583. node.computed = false;
  584. }
  585. return this.finishNode(node, "MethodDefinition");
  586. }
  587. parseClassProperty(...args) {
  588. const propertyNode = super.parseClassProperty(...args);
  589. {
  590. if (!this.getPluginOption("estree", "classFeatures")) {
  591. return propertyNode;
  592. }
  593. }
  594. propertyNode.type = "PropertyDefinition";
  595. return propertyNode;
  596. }
  597. parseClassPrivateProperty(...args) {
  598. const propertyNode = super.parseClassPrivateProperty(...args);
  599. {
  600. if (!this.getPluginOption("estree", "classFeatures")) {
  601. return propertyNode;
  602. }
  603. }
  604. propertyNode.type = "PropertyDefinition";
  605. propertyNode.computed = false;
  606. return propertyNode;
  607. }
  608. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  609. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  610. if (node) {
  611. node.type = "Property";
  612. if (node.kind === "method") {
  613. node.kind = "init";
  614. }
  615. node.shorthand = false;
  616. }
  617. return node;
  618. }
  619. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  620. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  621. if (node) {
  622. node.kind = "init";
  623. node.type = "Property";
  624. }
  625. return node;
  626. }
  627. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  628. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  629. }
  630. isAssignable(node, isBinding) {
  631. if (node != null && this.isObjectProperty(node)) {
  632. return this.isAssignable(node.value, isBinding);
  633. }
  634. return super.isAssignable(node, isBinding);
  635. }
  636. toAssignable(node, isLHS = false) {
  637. if (node != null && this.isObjectProperty(node)) {
  638. const {
  639. key,
  640. value
  641. } = node;
  642. if (this.isPrivateName(key)) {
  643. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  644. }
  645. this.toAssignable(value, isLHS);
  646. } else {
  647. super.toAssignable(node, isLHS);
  648. }
  649. }
  650. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  651. if (prop.kind === "get" || prop.kind === "set") {
  652. this.raise(Errors.PatternHasAccessor, {
  653. at: prop.key
  654. });
  655. } else if (prop.method) {
  656. this.raise(Errors.PatternHasMethod, {
  657. at: prop.key
  658. });
  659. } else {
  660. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  661. }
  662. }
  663. finishCallExpression(unfinished, optional) {
  664. const node = super.finishCallExpression(unfinished, optional);
  665. if (node.callee.type === "Import") {
  666. node.type = "ImportExpression";
  667. node.source = node.arguments[0];
  668. if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  669. var _node$arguments$;
  670. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  671. }
  672. delete node.arguments;
  673. delete node.callee;
  674. }
  675. return node;
  676. }
  677. toReferencedArguments(node) {
  678. if (node.type === "ImportExpression") {
  679. return;
  680. }
  681. super.toReferencedArguments(node);
  682. }
  683. parseExport(unfinished, decorators) {
  684. const exportStartLoc = this.state.lastTokStartLoc;
  685. const node = super.parseExport(unfinished, decorators);
  686. switch (node.type) {
  687. case "ExportAllDeclaration":
  688. node.exported = null;
  689. break;
  690. case "ExportNamedDeclaration":
  691. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  692. node.type = "ExportAllDeclaration";
  693. node.exported = node.specifiers[0].exported;
  694. delete node.specifiers;
  695. }
  696. case "ExportDefaultDeclaration":
  697. {
  698. var _declaration$decorato;
  699. const {
  700. declaration
  701. } = node;
  702. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  703. this.resetStartLocation(node, exportStartLoc);
  704. }
  705. }
  706. break;
  707. }
  708. return node;
  709. }
  710. parseSubscript(base, startLoc, noCalls, state) {
  711. const node = super.parseSubscript(base, startLoc, noCalls, state);
  712. if (state.optionalChainMember) {
  713. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  714. node.type = node.type.substring(8);
  715. }
  716. if (state.stop) {
  717. const chain = this.startNodeAtNode(node);
  718. chain.expression = node;
  719. return this.finishNode(chain, "ChainExpression");
  720. }
  721. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  722. node.optional = false;
  723. }
  724. return node;
  725. }
  726. hasPropertyAsPrivateName(node) {
  727. if (node.type === "ChainExpression") {
  728. node = node.expression;
  729. }
  730. return super.hasPropertyAsPrivateName(node);
  731. }
  732. isObjectProperty(node) {
  733. return node.type === "Property" && node.kind === "init" && !node.method;
  734. }
  735. isObjectMethod(node) {
  736. return node.method || node.kind === "get" || node.kind === "set";
  737. }
  738. finishNodeAt(node, type, endLoc) {
  739. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  740. }
  741. resetStartLocation(node, startLoc) {
  742. super.resetStartLocation(node, startLoc);
  743. toESTreeLocation(node);
  744. }
  745. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  746. super.resetEndLocation(node, endLoc);
  747. toESTreeLocation(node);
  748. }
  749. };
  750. class TokContext {
  751. constructor(token, preserveSpace) {
  752. this.token = void 0;
  753. this.preserveSpace = void 0;
  754. this.token = token;
  755. this.preserveSpace = !!preserveSpace;
  756. }
  757. }
  758. const types = {
  759. brace: new TokContext("{"),
  760. j_oTag: new TokContext("<tag"),
  761. j_cTag: new TokContext("</tag"),
  762. j_expr: new TokContext("<tag>...</tag>", true)
  763. };
  764. {
  765. types.template = new TokContext("`", true);
  766. }
  767. const beforeExpr = true;
  768. const startsExpr = true;
  769. const isLoop = true;
  770. const isAssign = true;
  771. const prefix = true;
  772. const postfix = true;
  773. class ExportedTokenType {
  774. constructor(label, conf = {}) {
  775. this.label = void 0;
  776. this.keyword = void 0;
  777. this.beforeExpr = void 0;
  778. this.startsExpr = void 0;
  779. this.rightAssociative = void 0;
  780. this.isLoop = void 0;
  781. this.isAssign = void 0;
  782. this.prefix = void 0;
  783. this.postfix = void 0;
  784. this.binop = void 0;
  785. this.label = label;
  786. this.keyword = conf.keyword;
  787. this.beforeExpr = !!conf.beforeExpr;
  788. this.startsExpr = !!conf.startsExpr;
  789. this.rightAssociative = !!conf.rightAssociative;
  790. this.isLoop = !!conf.isLoop;
  791. this.isAssign = !!conf.isAssign;
  792. this.prefix = !!conf.prefix;
  793. this.postfix = !!conf.postfix;
  794. this.binop = conf.binop != null ? conf.binop : null;
  795. {
  796. this.updateContext = null;
  797. }
  798. }
  799. }
  800. const keywords$1 = new Map();
  801. function createKeyword(name, options = {}) {
  802. options.keyword = name;
  803. const token = createToken(name, options);
  804. keywords$1.set(name, token);
  805. return token;
  806. }
  807. function createBinop(name, binop) {
  808. return createToken(name, {
  809. beforeExpr,
  810. binop
  811. });
  812. }
  813. let tokenTypeCounter = -1;
  814. const tokenTypes = [];
  815. const tokenLabels = [];
  816. const tokenBinops = [];
  817. const tokenBeforeExprs = [];
  818. const tokenStartsExprs = [];
  819. const tokenPrefixes = [];
  820. function createToken(name, options = {}) {
  821. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  822. ++tokenTypeCounter;
  823. tokenLabels.push(name);
  824. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  825. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  826. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  827. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  828. tokenTypes.push(new ExportedTokenType(name, options));
  829. return tokenTypeCounter;
  830. }
  831. function createKeywordLike(name, options = {}) {
  832. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  833. ++tokenTypeCounter;
  834. keywords$1.set(name, tokenTypeCounter);
  835. tokenLabels.push(name);
  836. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  837. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  838. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  839. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  840. tokenTypes.push(new ExportedTokenType("name", options));
  841. return tokenTypeCounter;
  842. }
  843. const tt = {
  844. bracketL: createToken("[", {
  845. beforeExpr,
  846. startsExpr
  847. }),
  848. bracketHashL: createToken("#[", {
  849. beforeExpr,
  850. startsExpr
  851. }),
  852. bracketBarL: createToken("[|", {
  853. beforeExpr,
  854. startsExpr
  855. }),
  856. bracketR: createToken("]"),
  857. bracketBarR: createToken("|]"),
  858. braceL: createToken("{", {
  859. beforeExpr,
  860. startsExpr
  861. }),
  862. braceBarL: createToken("{|", {
  863. beforeExpr,
  864. startsExpr
  865. }),
  866. braceHashL: createToken("#{", {
  867. beforeExpr,
  868. startsExpr
  869. }),
  870. braceR: createToken("}"),
  871. braceBarR: createToken("|}"),
  872. parenL: createToken("(", {
  873. beforeExpr,
  874. startsExpr
  875. }),
  876. parenR: createToken(")"),
  877. comma: createToken(",", {
  878. beforeExpr
  879. }),
  880. semi: createToken(";", {
  881. beforeExpr
  882. }),
  883. colon: createToken(":", {
  884. beforeExpr
  885. }),
  886. doubleColon: createToken("::", {
  887. beforeExpr
  888. }),
  889. dot: createToken("."),
  890. question: createToken("?", {
  891. beforeExpr
  892. }),
  893. questionDot: createToken("?."),
  894. arrow: createToken("=>", {
  895. beforeExpr
  896. }),
  897. template: createToken("template"),
  898. ellipsis: createToken("...", {
  899. beforeExpr
  900. }),
  901. backQuote: createToken("`", {
  902. startsExpr
  903. }),
  904. dollarBraceL: createToken("${", {
  905. beforeExpr,
  906. startsExpr
  907. }),
  908. templateTail: createToken("...`", {
  909. startsExpr
  910. }),
  911. templateNonTail: createToken("...${", {
  912. beforeExpr,
  913. startsExpr
  914. }),
  915. at: createToken("@"),
  916. hash: createToken("#", {
  917. startsExpr
  918. }),
  919. interpreterDirective: createToken("#!..."),
  920. eq: createToken("=", {
  921. beforeExpr,
  922. isAssign
  923. }),
  924. assign: createToken("_=", {
  925. beforeExpr,
  926. isAssign
  927. }),
  928. slashAssign: createToken("_=", {
  929. beforeExpr,
  930. isAssign
  931. }),
  932. xorAssign: createToken("_=", {
  933. beforeExpr,
  934. isAssign
  935. }),
  936. moduloAssign: createToken("_=", {
  937. beforeExpr,
  938. isAssign
  939. }),
  940. incDec: createToken("++/--", {
  941. prefix,
  942. postfix,
  943. startsExpr
  944. }),
  945. bang: createToken("!", {
  946. beforeExpr,
  947. prefix,
  948. startsExpr
  949. }),
  950. tilde: createToken("~", {
  951. beforeExpr,
  952. prefix,
  953. startsExpr
  954. }),
  955. doubleCaret: createToken("^^", {
  956. startsExpr
  957. }),
  958. doubleAt: createToken("@@", {
  959. startsExpr
  960. }),
  961. pipeline: createBinop("|>", 0),
  962. nullishCoalescing: createBinop("??", 1),
  963. logicalOR: createBinop("||", 1),
  964. logicalAND: createBinop("&&", 2),
  965. bitwiseOR: createBinop("|", 3),
  966. bitwiseXOR: createBinop("^", 4),
  967. bitwiseAND: createBinop("&", 5),
  968. equality: createBinop("==/!=/===/!==", 6),
  969. lt: createBinop("</>/<=/>=", 7),
  970. gt: createBinop("</>/<=/>=", 7),
  971. relational: createBinop("</>/<=/>=", 7),
  972. bitShift: createBinop("<</>>/>>>", 8),
  973. bitShiftL: createBinop("<</>>/>>>", 8),
  974. bitShiftR: createBinop("<</>>/>>>", 8),
  975. plusMin: createToken("+/-", {
  976. beforeExpr,
  977. binop: 9,
  978. prefix,
  979. startsExpr
  980. }),
  981. modulo: createToken("%", {
  982. binop: 10,
  983. startsExpr
  984. }),
  985. star: createToken("*", {
  986. binop: 10
  987. }),
  988. slash: createBinop("/", 10),
  989. exponent: createToken("**", {
  990. beforeExpr,
  991. binop: 11,
  992. rightAssociative: true
  993. }),
  994. _in: createKeyword("in", {
  995. beforeExpr,
  996. binop: 7
  997. }),
  998. _instanceof: createKeyword("instanceof", {
  999. beforeExpr,
  1000. binop: 7
  1001. }),
  1002. _break: createKeyword("break"),
  1003. _case: createKeyword("case", {
  1004. beforeExpr
  1005. }),
  1006. _catch: createKeyword("catch"),
  1007. _continue: createKeyword("continue"),
  1008. _debugger: createKeyword("debugger"),
  1009. _default: createKeyword("default", {
  1010. beforeExpr
  1011. }),
  1012. _else: createKeyword("else", {
  1013. beforeExpr
  1014. }),
  1015. _finally: createKeyword("finally"),
  1016. _function: createKeyword("function", {
  1017. startsExpr
  1018. }),
  1019. _if: createKeyword("if"),
  1020. _return: createKeyword("return", {
  1021. beforeExpr
  1022. }),
  1023. _switch: createKeyword("switch"),
  1024. _throw: createKeyword("throw", {
  1025. beforeExpr,
  1026. prefix,
  1027. startsExpr
  1028. }),
  1029. _try: createKeyword("try"),
  1030. _var: createKeyword("var"),
  1031. _const: createKeyword("const"),
  1032. _with: createKeyword("with"),
  1033. _new: createKeyword("new", {
  1034. beforeExpr,
  1035. startsExpr
  1036. }),
  1037. _this: createKeyword("this", {
  1038. startsExpr
  1039. }),
  1040. _super: createKeyword("super", {
  1041. startsExpr
  1042. }),
  1043. _class: createKeyword("class", {
  1044. startsExpr
  1045. }),
  1046. _extends: createKeyword("extends", {
  1047. beforeExpr
  1048. }),
  1049. _export: createKeyword("export"),
  1050. _import: createKeyword("import", {
  1051. startsExpr
  1052. }),
  1053. _null: createKeyword("null", {
  1054. startsExpr
  1055. }),
  1056. _true: createKeyword("true", {
  1057. startsExpr
  1058. }),
  1059. _false: createKeyword("false", {
  1060. startsExpr
  1061. }),
  1062. _typeof: createKeyword("typeof", {
  1063. beforeExpr,
  1064. prefix,
  1065. startsExpr
  1066. }),
  1067. _void: createKeyword("void", {
  1068. beforeExpr,
  1069. prefix,
  1070. startsExpr
  1071. }),
  1072. _delete: createKeyword("delete", {
  1073. beforeExpr,
  1074. prefix,
  1075. startsExpr
  1076. }),
  1077. _do: createKeyword("do", {
  1078. isLoop,
  1079. beforeExpr
  1080. }),
  1081. _for: createKeyword("for", {
  1082. isLoop
  1083. }),
  1084. _while: createKeyword("while", {
  1085. isLoop
  1086. }),
  1087. _as: createKeywordLike("as", {
  1088. startsExpr
  1089. }),
  1090. _assert: createKeywordLike("assert", {
  1091. startsExpr
  1092. }),
  1093. _async: createKeywordLike("async", {
  1094. startsExpr
  1095. }),
  1096. _await: createKeywordLike("await", {
  1097. startsExpr
  1098. }),
  1099. _from: createKeywordLike("from", {
  1100. startsExpr
  1101. }),
  1102. _get: createKeywordLike("get", {
  1103. startsExpr
  1104. }),
  1105. _let: createKeywordLike("let", {
  1106. startsExpr
  1107. }),
  1108. _meta: createKeywordLike("meta", {
  1109. startsExpr
  1110. }),
  1111. _of: createKeywordLike("of", {
  1112. startsExpr
  1113. }),
  1114. _sent: createKeywordLike("sent", {
  1115. startsExpr
  1116. }),
  1117. _set: createKeywordLike("set", {
  1118. startsExpr
  1119. }),
  1120. _static: createKeywordLike("static", {
  1121. startsExpr
  1122. }),
  1123. _using: createKeywordLike("using", {
  1124. startsExpr
  1125. }),
  1126. _yield: createKeywordLike("yield", {
  1127. startsExpr
  1128. }),
  1129. _asserts: createKeywordLike("asserts", {
  1130. startsExpr
  1131. }),
  1132. _checks: createKeywordLike("checks", {
  1133. startsExpr
  1134. }),
  1135. _exports: createKeywordLike("exports", {
  1136. startsExpr
  1137. }),
  1138. _global: createKeywordLike("global", {
  1139. startsExpr
  1140. }),
  1141. _implements: createKeywordLike("implements", {
  1142. startsExpr
  1143. }),
  1144. _intrinsic: createKeywordLike("intrinsic", {
  1145. startsExpr
  1146. }),
  1147. _infer: createKeywordLike("infer", {
  1148. startsExpr
  1149. }),
  1150. _is: createKeywordLike("is", {
  1151. startsExpr
  1152. }),
  1153. _mixins: createKeywordLike("mixins", {
  1154. startsExpr
  1155. }),
  1156. _proto: createKeywordLike("proto", {
  1157. startsExpr
  1158. }),
  1159. _require: createKeywordLike("require", {
  1160. startsExpr
  1161. }),
  1162. _satisfies: createKeywordLike("satisfies", {
  1163. startsExpr
  1164. }),
  1165. _keyof: createKeywordLike("keyof", {
  1166. startsExpr
  1167. }),
  1168. _readonly: createKeywordLike("readonly", {
  1169. startsExpr
  1170. }),
  1171. _unique: createKeywordLike("unique", {
  1172. startsExpr
  1173. }),
  1174. _abstract: createKeywordLike("abstract", {
  1175. startsExpr
  1176. }),
  1177. _declare: createKeywordLike("declare", {
  1178. startsExpr
  1179. }),
  1180. _enum: createKeywordLike("enum", {
  1181. startsExpr
  1182. }),
  1183. _module: createKeywordLike("module", {
  1184. startsExpr
  1185. }),
  1186. _namespace: createKeywordLike("namespace", {
  1187. startsExpr
  1188. }),
  1189. _interface: createKeywordLike("interface", {
  1190. startsExpr
  1191. }),
  1192. _type: createKeywordLike("type", {
  1193. startsExpr
  1194. }),
  1195. _opaque: createKeywordLike("opaque", {
  1196. startsExpr
  1197. }),
  1198. name: createToken("name", {
  1199. startsExpr
  1200. }),
  1201. string: createToken("string", {
  1202. startsExpr
  1203. }),
  1204. num: createToken("num", {
  1205. startsExpr
  1206. }),
  1207. bigint: createToken("bigint", {
  1208. startsExpr
  1209. }),
  1210. decimal: createToken("decimal", {
  1211. startsExpr
  1212. }),
  1213. regexp: createToken("regexp", {
  1214. startsExpr
  1215. }),
  1216. privateName: createToken("#name", {
  1217. startsExpr
  1218. }),
  1219. eof: createToken("eof"),
  1220. jsxName: createToken("jsxName"),
  1221. jsxText: createToken("jsxText", {
  1222. beforeExpr: true
  1223. }),
  1224. jsxTagStart: createToken("jsxTagStart", {
  1225. startsExpr: true
  1226. }),
  1227. jsxTagEnd: createToken("jsxTagEnd"),
  1228. placeholder: createToken("%%", {
  1229. startsExpr: true
  1230. })
  1231. };
  1232. function tokenIsIdentifier(token) {
  1233. return token >= 93 && token <= 130;
  1234. }
  1235. function tokenKeywordOrIdentifierIsKeyword(token) {
  1236. return token <= 92;
  1237. }
  1238. function tokenIsKeywordOrIdentifier(token) {
  1239. return token >= 58 && token <= 130;
  1240. }
  1241. function tokenIsLiteralPropertyName(token) {
  1242. return token >= 58 && token <= 134;
  1243. }
  1244. function tokenComesBeforeExpression(token) {
  1245. return tokenBeforeExprs[token];
  1246. }
  1247. function tokenCanStartExpression(token) {
  1248. return tokenStartsExprs[token];
  1249. }
  1250. function tokenIsAssignment(token) {
  1251. return token >= 29 && token <= 33;
  1252. }
  1253. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1254. return token >= 127 && token <= 129;
  1255. }
  1256. function tokenIsLoop(token) {
  1257. return token >= 90 && token <= 92;
  1258. }
  1259. function tokenIsKeyword(token) {
  1260. return token >= 58 && token <= 92;
  1261. }
  1262. function tokenIsOperator(token) {
  1263. return token >= 39 && token <= 59;
  1264. }
  1265. function tokenIsPostfix(token) {
  1266. return token === 34;
  1267. }
  1268. function tokenIsPrefix(token) {
  1269. return tokenPrefixes[token];
  1270. }
  1271. function tokenIsTSTypeOperator(token) {
  1272. return token >= 119 && token <= 121;
  1273. }
  1274. function tokenIsTSDeclarationStart(token) {
  1275. return token >= 122 && token <= 128;
  1276. }
  1277. function tokenLabelName(token) {
  1278. return tokenLabels[token];
  1279. }
  1280. function tokenOperatorPrecedence(token) {
  1281. return tokenBinops[token];
  1282. }
  1283. function tokenIsRightAssociative(token) {
  1284. return token === 57;
  1285. }
  1286. function tokenIsTemplate(token) {
  1287. return token >= 24 && token <= 25;
  1288. }
  1289. function getExportedToken(token) {
  1290. return tokenTypes[token];
  1291. }
  1292. {
  1293. tokenTypes[8].updateContext = context => {
  1294. context.pop();
  1295. };
  1296. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1297. context.push(types.brace);
  1298. };
  1299. tokenTypes[22].updateContext = context => {
  1300. if (context[context.length - 1] === types.template) {
  1301. context.pop();
  1302. } else {
  1303. context.push(types.template);
  1304. }
  1305. };
  1306. tokenTypes[140].updateContext = context => {
  1307. context.push(types.j_expr, types.j_oTag);
  1308. };
  1309. }
  1310. let nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u037f\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u052f\u0531-\u0556\u0559\u0560-\u0588\u05d0-\u05ea\u05ef-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u0860-\u086a\u0870-\u0887\u0889-\u088e\u08a0-\u08c9\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u09fc\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0af9\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c39\u0c3d\u0c58-\u0c5a\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d04-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d54-\u0d56\u0d5f-\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e86-\u0e8a\u0e8c-\u0ea3\u0ea5\u0ea7-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f5\u13f8-\u13fd\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f8\u1700-\u1711\u171f-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1878\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191e\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19b0-\u19c9\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4c\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309b-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312f\u3131-\u318e\u31a0-\u31bf\u31f0-\u31ff\u3400-\u4dbf\u4e00-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7ca\ua7d0\ua7d1\ua7d3\ua7d5-\ua7d9\ua7f2-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua8fd\ua8fe\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\ua9e0-\ua9e4\ua9e6-\ua9ef\ua9fa-\ua9fe\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa7e-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uab30-\uab5a\uab5c-\uab69\uab70-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  1311. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  1312. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1313. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1314. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1315. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938, 6, 4191];
  1316. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1317. function isInAstralSet(code, set) {
  1318. let pos = 0x10000;
  1319. for (let i = 0, length = set.length; i < length; i += 2) {
  1320. pos += set[i];
  1321. if (pos > code) return false;
  1322. pos += set[i + 1];
  1323. if (pos >= code) return true;
  1324. }
  1325. return false;
  1326. }
  1327. function isIdentifierStart(code) {
  1328. if (code < 65) return code === 36;
  1329. if (code <= 90) return true;
  1330. if (code < 97) return code === 95;
  1331. if (code <= 122) return true;
  1332. if (code <= 0xffff) {
  1333. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1334. }
  1335. return isInAstralSet(code, astralIdentifierStartCodes);
  1336. }
  1337. function isIdentifierChar(code) {
  1338. if (code < 48) return code === 36;
  1339. if (code < 58) return true;
  1340. if (code < 65) return false;
  1341. if (code <= 90) return true;
  1342. if (code < 97) return code === 95;
  1343. if (code <= 122) return true;
  1344. if (code <= 0xffff) {
  1345. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1346. }
  1347. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1348. }
  1349. const reservedWords = {
  1350. keyword: ["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete"],
  1351. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1352. strictBind: ["eval", "arguments"]
  1353. };
  1354. const keywords = new Set(reservedWords.keyword);
  1355. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1356. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1357. function isReservedWord(word, inModule) {
  1358. return inModule && word === "await" || word === "enum";
  1359. }
  1360. function isStrictReservedWord(word, inModule) {
  1361. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1362. }
  1363. function isStrictBindOnlyReservedWord(word) {
  1364. return reservedWordsStrictBindSet.has(word);
  1365. }
  1366. function isStrictBindReservedWord(word, inModule) {
  1367. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1368. }
  1369. function isKeyword(word) {
  1370. return keywords.has(word);
  1371. }
  1372. function isIteratorStart(current, next, next2) {
  1373. return current === 64 && next === 64 && isIdentifierStart(next2);
  1374. }
  1375. const reservedWordLikeSet = new Set(["break", "case", "catch", "continue", "debugger", "default", "do", "else", "finally", "for", "function", "if", "return", "switch", "throw", "try", "var", "const", "while", "with", "new", "this", "super", "class", "extends", "export", "import", "null", "true", "false", "in", "instanceof", "typeof", "void", "delete", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "eval", "arguments", "enum", "await"]);
  1376. function canBeReservedWord(word) {
  1377. return reservedWordLikeSet.has(word);
  1378. }
  1379. const SCOPE_OTHER = 0b000000000,
  1380. SCOPE_PROGRAM = 0b000000001,
  1381. SCOPE_FUNCTION = 0b000000010,
  1382. SCOPE_ARROW = 0b000000100,
  1383. SCOPE_SIMPLE_CATCH = 0b000001000,
  1384. SCOPE_SUPER = 0b000010000,
  1385. SCOPE_DIRECT_SUPER = 0b000100000,
  1386. SCOPE_CLASS = 0b001000000,
  1387. SCOPE_STATIC_BLOCK = 0b010000000,
  1388. SCOPE_TS_MODULE = 0b100000000,
  1389. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_STATIC_BLOCK | SCOPE_TS_MODULE;
  1390. const BIND_KIND_VALUE = 0b0000000000001,
  1391. BIND_KIND_TYPE = 0b0000000000010,
  1392. BIND_SCOPE_VAR = 0b0000000000100,
  1393. BIND_SCOPE_LEXICAL = 0b0000000001000,
  1394. BIND_SCOPE_FUNCTION = 0b0000000010000,
  1395. BIND_FLAGS_NONE = 0b00000001000000,
  1396. BIND_FLAGS_CLASS = 0b00000010000000,
  1397. BIND_FLAGS_TS_ENUM = 0b00000100000000,
  1398. BIND_FLAGS_TS_CONST_ENUM = 0b00001000000000,
  1399. BIND_FLAGS_TS_EXPORT_ONLY = 0b00010000000000,
  1400. BIND_FLAGS_FLOW_DECLARE_FN = 0b00100000000000,
  1401. BIND_FLAGS_TS_IMPORT = 0b01000000000000,
  1402. BIND_FLAGS_NO_LET_IN_LEXICAL = 0b10000000000000;
  1403. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1404. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1405. BIND_CATCH_PARAM = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1406. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1407. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1408. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1409. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1410. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1411. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1412. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1413. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1414. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1415. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1416. BIND_TS_TYPE_IMPORT = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
  1417. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1418. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1419. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1420. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1421. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1422. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1423. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1424. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1425. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1426. CLASS_ELEMENT_OTHER = 0;
  1427. class Scope {
  1428. constructor(flags) {
  1429. this.var = new Set();
  1430. this.lexical = new Set();
  1431. this.functions = new Set();
  1432. this.flags = flags;
  1433. }
  1434. }
  1435. class ScopeHandler {
  1436. constructor(parser, inModule) {
  1437. this.parser = void 0;
  1438. this.scopeStack = [];
  1439. this.inModule = void 0;
  1440. this.undefinedExports = new Map();
  1441. this.parser = parser;
  1442. this.inModule = inModule;
  1443. }
  1444. get inTopLevel() {
  1445. return (this.currentScope().flags & SCOPE_PROGRAM) > 0;
  1446. }
  1447. get inFunction() {
  1448. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1449. }
  1450. get allowSuper() {
  1451. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1452. }
  1453. get allowDirectSuper() {
  1454. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1455. }
  1456. get inClass() {
  1457. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1458. }
  1459. get inClassAndNotInNonArrowFunction() {
  1460. const flags = this.currentThisScopeFlags();
  1461. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1462. }
  1463. get inStaticBlock() {
  1464. for (let i = this.scopeStack.length - 1;; i--) {
  1465. const {
  1466. flags
  1467. } = this.scopeStack[i];
  1468. if (flags & SCOPE_STATIC_BLOCK) {
  1469. return true;
  1470. }
  1471. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1472. return false;
  1473. }
  1474. }
  1475. }
  1476. get inNonArrowFunction() {
  1477. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1478. }
  1479. get treatFunctionsAsVar() {
  1480. return this.treatFunctionsAsVarInScope(this.currentScope());
  1481. }
  1482. createScope(flags) {
  1483. return new Scope(flags);
  1484. }
  1485. enter(flags) {
  1486. this.scopeStack.push(this.createScope(flags));
  1487. }
  1488. exit() {
  1489. const scope = this.scopeStack.pop();
  1490. return scope.flags;
  1491. }
  1492. treatFunctionsAsVarInScope(scope) {
  1493. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  1494. }
  1495. declareName(name, bindingType, loc) {
  1496. let scope = this.currentScope();
  1497. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1498. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1499. if (bindingType & BIND_SCOPE_FUNCTION) {
  1500. scope.functions.add(name);
  1501. } else {
  1502. scope.lexical.add(name);
  1503. }
  1504. if (bindingType & BIND_SCOPE_LEXICAL) {
  1505. this.maybeExportDefined(scope, name);
  1506. }
  1507. } else if (bindingType & BIND_SCOPE_VAR) {
  1508. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1509. scope = this.scopeStack[i];
  1510. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1511. scope.var.add(name);
  1512. this.maybeExportDefined(scope, name);
  1513. if (scope.flags & SCOPE_VAR) break;
  1514. }
  1515. }
  1516. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1517. this.undefinedExports.delete(name);
  1518. }
  1519. }
  1520. maybeExportDefined(scope, name) {
  1521. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1522. this.undefinedExports.delete(name);
  1523. }
  1524. }
  1525. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1526. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1527. this.parser.raise(Errors.VarRedeclaration, {
  1528. at: loc,
  1529. identifierName: name
  1530. });
  1531. }
  1532. }
  1533. isRedeclaredInScope(scope, name, bindingType) {
  1534. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1535. if (bindingType & BIND_SCOPE_LEXICAL) {
  1536. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1537. }
  1538. if (bindingType & BIND_SCOPE_FUNCTION) {
  1539. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1540. }
  1541. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1542. }
  1543. checkLocalExport(id) {
  1544. const {
  1545. name
  1546. } = id;
  1547. const topLevelScope = this.scopeStack[0];
  1548. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1549. this.undefinedExports.set(name, id.loc.start);
  1550. }
  1551. }
  1552. currentScope() {
  1553. return this.scopeStack[this.scopeStack.length - 1];
  1554. }
  1555. currentVarScopeFlags() {
  1556. for (let i = this.scopeStack.length - 1;; i--) {
  1557. const {
  1558. flags
  1559. } = this.scopeStack[i];
  1560. if (flags & SCOPE_VAR) {
  1561. return flags;
  1562. }
  1563. }
  1564. }
  1565. currentThisScopeFlags() {
  1566. for (let i = this.scopeStack.length - 1;; i--) {
  1567. const {
  1568. flags
  1569. } = this.scopeStack[i];
  1570. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1571. return flags;
  1572. }
  1573. }
  1574. }
  1575. }
  1576. class FlowScope extends Scope {
  1577. constructor(...args) {
  1578. super(...args);
  1579. this.declareFunctions = new Set();
  1580. }
  1581. }
  1582. class FlowScopeHandler extends ScopeHandler {
  1583. createScope(flags) {
  1584. return new FlowScope(flags);
  1585. }
  1586. declareName(name, bindingType, loc) {
  1587. const scope = this.currentScope();
  1588. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1589. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1590. this.maybeExportDefined(scope, name);
  1591. scope.declareFunctions.add(name);
  1592. return;
  1593. }
  1594. super.declareName(name, bindingType, loc);
  1595. }
  1596. isRedeclaredInScope(scope, name, bindingType) {
  1597. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1598. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1599. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1600. }
  1601. return false;
  1602. }
  1603. checkLocalExport(id) {
  1604. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1605. super.checkLocalExport(id);
  1606. }
  1607. }
  1608. }
  1609. class BaseParser {
  1610. constructor() {
  1611. this.sawUnambiguousESM = false;
  1612. this.ambiguousScriptDifferentAst = false;
  1613. }
  1614. hasPlugin(pluginConfig) {
  1615. if (typeof pluginConfig === "string") {
  1616. return this.plugins.has(pluginConfig);
  1617. } else {
  1618. const [pluginName, pluginOptions] = pluginConfig;
  1619. if (!this.hasPlugin(pluginName)) {
  1620. return false;
  1621. }
  1622. const actualOptions = this.plugins.get(pluginName);
  1623. for (const key of Object.keys(pluginOptions)) {
  1624. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1625. return false;
  1626. }
  1627. }
  1628. return true;
  1629. }
  1630. }
  1631. getPluginOption(plugin, name) {
  1632. var _this$plugins$get;
  1633. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1634. }
  1635. }
  1636. function setTrailingComments(node, comments) {
  1637. if (node.trailingComments === undefined) {
  1638. node.trailingComments = comments;
  1639. } else {
  1640. node.trailingComments.unshift(...comments);
  1641. }
  1642. }
  1643. function setLeadingComments(node, comments) {
  1644. if (node.leadingComments === undefined) {
  1645. node.leadingComments = comments;
  1646. } else {
  1647. node.leadingComments.unshift(...comments);
  1648. }
  1649. }
  1650. function setInnerComments(node, comments) {
  1651. if (node.innerComments === undefined) {
  1652. node.innerComments = comments;
  1653. } else {
  1654. node.innerComments.unshift(...comments);
  1655. }
  1656. }
  1657. function adjustInnerComments(node, elements, commentWS) {
  1658. let lastElement = null;
  1659. let i = elements.length;
  1660. while (lastElement === null && i > 0) {
  1661. lastElement = elements[--i];
  1662. }
  1663. if (lastElement === null || lastElement.start > commentWS.start) {
  1664. setInnerComments(node, commentWS.comments);
  1665. } else {
  1666. setTrailingComments(lastElement, commentWS.comments);
  1667. }
  1668. }
  1669. class CommentsParser extends BaseParser {
  1670. addComment(comment) {
  1671. if (this.filename) comment.loc.filename = this.filename;
  1672. this.state.comments.push(comment);
  1673. }
  1674. processComment(node) {
  1675. const {
  1676. commentStack
  1677. } = this.state;
  1678. const commentStackLength = commentStack.length;
  1679. if (commentStackLength === 0) return;
  1680. let i = commentStackLength - 1;
  1681. const lastCommentWS = commentStack[i];
  1682. if (lastCommentWS.start === node.end) {
  1683. lastCommentWS.leadingNode = node;
  1684. i--;
  1685. }
  1686. const {
  1687. start: nodeStart
  1688. } = node;
  1689. for (; i >= 0; i--) {
  1690. const commentWS = commentStack[i];
  1691. const commentEnd = commentWS.end;
  1692. if (commentEnd > nodeStart) {
  1693. commentWS.containingNode = node;
  1694. this.finalizeComment(commentWS);
  1695. commentStack.splice(i, 1);
  1696. } else {
  1697. if (commentEnd === nodeStart) {
  1698. commentWS.trailingNode = node;
  1699. }
  1700. break;
  1701. }
  1702. }
  1703. }
  1704. finalizeComment(commentWS) {
  1705. const {
  1706. comments
  1707. } = commentWS;
  1708. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1709. if (commentWS.leadingNode !== null) {
  1710. setTrailingComments(commentWS.leadingNode, comments);
  1711. }
  1712. if (commentWS.trailingNode !== null) {
  1713. setLeadingComments(commentWS.trailingNode, comments);
  1714. }
  1715. } else {
  1716. const {
  1717. containingNode: node,
  1718. start: commentStart
  1719. } = commentWS;
  1720. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1721. switch (node.type) {
  1722. case "ObjectExpression":
  1723. case "ObjectPattern":
  1724. case "RecordExpression":
  1725. adjustInnerComments(node, node.properties, commentWS);
  1726. break;
  1727. case "CallExpression":
  1728. case "OptionalCallExpression":
  1729. adjustInnerComments(node, node.arguments, commentWS);
  1730. break;
  1731. case "FunctionDeclaration":
  1732. case "FunctionExpression":
  1733. case "ArrowFunctionExpression":
  1734. case "ObjectMethod":
  1735. case "ClassMethod":
  1736. case "ClassPrivateMethod":
  1737. adjustInnerComments(node, node.params, commentWS);
  1738. break;
  1739. case "ArrayExpression":
  1740. case "ArrayPattern":
  1741. case "TupleExpression":
  1742. adjustInnerComments(node, node.elements, commentWS);
  1743. break;
  1744. case "ExportNamedDeclaration":
  1745. case "ImportDeclaration":
  1746. adjustInnerComments(node, node.specifiers, commentWS);
  1747. break;
  1748. default:
  1749. {
  1750. setInnerComments(node, comments);
  1751. }
  1752. }
  1753. } else {
  1754. setInnerComments(node, comments);
  1755. }
  1756. }
  1757. }
  1758. finalizeRemainingComments() {
  1759. const {
  1760. commentStack
  1761. } = this.state;
  1762. for (let i = commentStack.length - 1; i >= 0; i--) {
  1763. this.finalizeComment(commentStack[i]);
  1764. }
  1765. this.state.commentStack = [];
  1766. }
  1767. resetPreviousNodeTrailingComments(node) {
  1768. const {
  1769. commentStack
  1770. } = this.state;
  1771. const {
  1772. length
  1773. } = commentStack;
  1774. if (length === 0) return;
  1775. const commentWS = commentStack[length - 1];
  1776. if (commentWS.leadingNode === node) {
  1777. commentWS.leadingNode = null;
  1778. }
  1779. }
  1780. resetPreviousIdentifierLeadingComments(node) {
  1781. const {
  1782. commentStack
  1783. } = this.state;
  1784. const {
  1785. length
  1786. } = commentStack;
  1787. if (length === 0) return;
  1788. if (commentStack[length - 1].trailingNode === node) {
  1789. commentStack[length - 1].trailingNode = null;
  1790. } else if (length >= 2 && commentStack[length - 2].trailingNode === node) {
  1791. commentStack[length - 2].trailingNode = null;
  1792. }
  1793. }
  1794. takeSurroundingComments(node, start, end) {
  1795. const {
  1796. commentStack
  1797. } = this.state;
  1798. const commentStackLength = commentStack.length;
  1799. if (commentStackLength === 0) return;
  1800. let i = commentStackLength - 1;
  1801. for (; i >= 0; i--) {
  1802. const commentWS = commentStack[i];
  1803. const commentEnd = commentWS.end;
  1804. const commentStart = commentWS.start;
  1805. if (commentStart === end) {
  1806. commentWS.leadingNode = node;
  1807. } else if (commentEnd === start) {
  1808. commentWS.trailingNode = node;
  1809. } else if (commentEnd < start) {
  1810. break;
  1811. }
  1812. }
  1813. }
  1814. }
  1815. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1816. const lineBreakG = new RegExp(lineBreak.source, "g");
  1817. function isNewLine(code) {
  1818. switch (code) {
  1819. case 10:
  1820. case 13:
  1821. case 8232:
  1822. case 8233:
  1823. return true;
  1824. default:
  1825. return false;
  1826. }
  1827. }
  1828. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1829. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/g;
  1830. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1831. function isWhitespace(code) {
  1832. switch (code) {
  1833. case 0x0009:
  1834. case 0x000b:
  1835. case 0x000c:
  1836. case 32:
  1837. case 160:
  1838. case 5760:
  1839. case 0x2000:
  1840. case 0x2001:
  1841. case 0x2002:
  1842. case 0x2003:
  1843. case 0x2004:
  1844. case 0x2005:
  1845. case 0x2006:
  1846. case 0x2007:
  1847. case 0x2008:
  1848. case 0x2009:
  1849. case 0x200a:
  1850. case 0x202f:
  1851. case 0x205f:
  1852. case 0x3000:
  1853. case 0xfeff:
  1854. return true;
  1855. default:
  1856. return false;
  1857. }
  1858. }
  1859. class State {
  1860. constructor() {
  1861. this.strict = void 0;
  1862. this.curLine = void 0;
  1863. this.lineStart = void 0;
  1864. this.startLoc = void 0;
  1865. this.endLoc = void 0;
  1866. this.errors = [];
  1867. this.potentialArrowAt = -1;
  1868. this.noArrowAt = [];
  1869. this.noArrowParamsConversionAt = [];
  1870. this.maybeInArrowParameters = false;
  1871. this.inType = false;
  1872. this.noAnonFunctionType = false;
  1873. this.hasFlowComment = false;
  1874. this.isAmbientContext = false;
  1875. this.inAbstractClass = false;
  1876. this.inDisallowConditionalTypesContext = false;
  1877. this.topicContext = {
  1878. maxNumOfResolvableTopics: 0,
  1879. maxTopicIndex: null
  1880. };
  1881. this.soloAwait = false;
  1882. this.inFSharpPipelineDirectBody = false;
  1883. this.labels = [];
  1884. this.comments = [];
  1885. this.commentStack = [];
  1886. this.pos = 0;
  1887. this.type = 137;
  1888. this.value = null;
  1889. this.start = 0;
  1890. this.end = 0;
  1891. this.lastTokEndLoc = null;
  1892. this.lastTokStartLoc = null;
  1893. this.lastTokStart = 0;
  1894. this.context = [types.brace];
  1895. this.canStartJSXElement = true;
  1896. this.containsEsc = false;
  1897. this.firstInvalidTemplateEscapePos = null;
  1898. this.strictErrors = new Map();
  1899. this.tokensLength = 0;
  1900. }
  1901. init({
  1902. strictMode,
  1903. sourceType,
  1904. startLine,
  1905. startColumn
  1906. }) {
  1907. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1908. this.curLine = startLine;
  1909. this.lineStart = -startColumn;
  1910. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1911. }
  1912. curPosition() {
  1913. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1914. }
  1915. clone(skipArrays) {
  1916. const state = new State();
  1917. const keys = Object.keys(this);
  1918. for (let i = 0, length = keys.length; i < length; i++) {
  1919. const key = keys[i];
  1920. let val = this[key];
  1921. if (!skipArrays && Array.isArray(val)) {
  1922. val = val.slice();
  1923. }
  1924. state[key] = val;
  1925. }
  1926. return state;
  1927. }
  1928. }
  1929. var _isDigit = function isDigit(code) {
  1930. return code >= 48 && code <= 57;
  1931. };
  1932. const forbiddenNumericSeparatorSiblings = {
  1933. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1934. hex: new Set([46, 88, 95, 120])
  1935. };
  1936. const isAllowedNumericSeparatorSibling = {
  1937. bin: ch => ch === 48 || ch === 49,
  1938. oct: ch => ch >= 48 && ch <= 55,
  1939. dec: ch => ch >= 48 && ch <= 57,
  1940. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1941. };
  1942. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1943. const initialPos = pos;
  1944. const initialLineStart = lineStart;
  1945. const initialCurLine = curLine;
  1946. let out = "";
  1947. let firstInvalidLoc = null;
  1948. let chunkStart = pos;
  1949. const {
  1950. length
  1951. } = input;
  1952. for (;;) {
  1953. if (pos >= length) {
  1954. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1955. out += input.slice(chunkStart, pos);
  1956. break;
  1957. }
  1958. const ch = input.charCodeAt(pos);
  1959. if (isStringEnd(type, ch, input, pos)) {
  1960. out += input.slice(chunkStart, pos);
  1961. break;
  1962. }
  1963. if (ch === 92) {
  1964. out += input.slice(chunkStart, pos);
  1965. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  1966. if (res.ch === null && !firstInvalidLoc) {
  1967. firstInvalidLoc = {
  1968. pos,
  1969. lineStart,
  1970. curLine
  1971. };
  1972. } else {
  1973. out += res.ch;
  1974. }
  1975. ({
  1976. pos,
  1977. lineStart,
  1978. curLine
  1979. } = res);
  1980. chunkStart = pos;
  1981. } else if (ch === 8232 || ch === 8233) {
  1982. ++pos;
  1983. ++curLine;
  1984. lineStart = pos;
  1985. } else if (ch === 10 || ch === 13) {
  1986. if (type === "template") {
  1987. out += input.slice(chunkStart, pos) + "\n";
  1988. ++pos;
  1989. if (ch === 13 && input.charCodeAt(pos) === 10) {
  1990. ++pos;
  1991. }
  1992. ++curLine;
  1993. chunkStart = lineStart = pos;
  1994. } else {
  1995. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1996. }
  1997. } else {
  1998. ++pos;
  1999. }
  2000. }
  2001. return {
  2002. pos,
  2003. str: out,
  2004. firstInvalidLoc,
  2005. lineStart,
  2006. curLine,
  2007. containsInvalid: !!firstInvalidLoc
  2008. };
  2009. }
  2010. function isStringEnd(type, ch, input, pos) {
  2011. if (type === "template") {
  2012. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  2013. }
  2014. return ch === (type === "double" ? 34 : 39);
  2015. }
  2016. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  2017. const throwOnInvalid = !inTemplate;
  2018. pos++;
  2019. const res = ch => ({
  2020. pos,
  2021. ch,
  2022. lineStart,
  2023. curLine
  2024. });
  2025. const ch = input.charCodeAt(pos++);
  2026. switch (ch) {
  2027. case 110:
  2028. return res("\n");
  2029. case 114:
  2030. return res("\r");
  2031. case 120:
  2032. {
  2033. let code;
  2034. ({
  2035. code,
  2036. pos
  2037. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  2038. return res(code === null ? null : String.fromCharCode(code));
  2039. }
  2040. case 117:
  2041. {
  2042. let code;
  2043. ({
  2044. code,
  2045. pos
  2046. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2047. return res(code === null ? null : String.fromCodePoint(code));
  2048. }
  2049. case 116:
  2050. return res("\t");
  2051. case 98:
  2052. return res("\b");
  2053. case 118:
  2054. return res("\u000b");
  2055. case 102:
  2056. return res("\f");
  2057. case 13:
  2058. if (input.charCodeAt(pos) === 10) {
  2059. ++pos;
  2060. }
  2061. case 10:
  2062. lineStart = pos;
  2063. ++curLine;
  2064. case 8232:
  2065. case 8233:
  2066. return res("");
  2067. case 56:
  2068. case 57:
  2069. if (inTemplate) {
  2070. return res(null);
  2071. } else {
  2072. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2073. }
  2074. default:
  2075. if (ch >= 48 && ch <= 55) {
  2076. const startPos = pos - 1;
  2077. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  2078. let octalStr = match[0];
  2079. let octal = parseInt(octalStr, 8);
  2080. if (octal > 255) {
  2081. octalStr = octalStr.slice(0, -1);
  2082. octal = parseInt(octalStr, 8);
  2083. }
  2084. pos += octalStr.length - 1;
  2085. const next = input.charCodeAt(pos);
  2086. if (octalStr !== "0" || next === 56 || next === 57) {
  2087. if (inTemplate) {
  2088. return res(null);
  2089. } else {
  2090. errors.strictNumericEscape(startPos, lineStart, curLine);
  2091. }
  2092. }
  2093. return res(String.fromCharCode(octal));
  2094. }
  2095. return res(String.fromCharCode(ch));
  2096. }
  2097. }
  2098. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2099. const initialPos = pos;
  2100. let n;
  2101. ({
  2102. n,
  2103. pos
  2104. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2105. if (n === null) {
  2106. if (throwOnInvalid) {
  2107. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2108. } else {
  2109. pos = initialPos - 1;
  2110. }
  2111. }
  2112. return {
  2113. code: n,
  2114. pos
  2115. };
  2116. }
  2117. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2118. const start = pos;
  2119. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2120. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2121. let invalid = false;
  2122. let total = 0;
  2123. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2124. const code = input.charCodeAt(pos);
  2125. let val;
  2126. if (code === 95 && allowNumSeparator !== "bail") {
  2127. const prev = input.charCodeAt(pos - 1);
  2128. const next = input.charCodeAt(pos + 1);
  2129. if (!allowNumSeparator) {
  2130. if (bailOnError) return {
  2131. n: null,
  2132. pos
  2133. };
  2134. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2135. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2136. if (bailOnError) return {
  2137. n: null,
  2138. pos
  2139. };
  2140. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2141. }
  2142. ++pos;
  2143. continue;
  2144. }
  2145. if (code >= 97) {
  2146. val = code - 97 + 10;
  2147. } else if (code >= 65) {
  2148. val = code - 65 + 10;
  2149. } else if (_isDigit(code)) {
  2150. val = code - 48;
  2151. } else {
  2152. val = Infinity;
  2153. }
  2154. if (val >= radix) {
  2155. if (val <= 9 && bailOnError) {
  2156. return {
  2157. n: null,
  2158. pos
  2159. };
  2160. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2161. val = 0;
  2162. } else if (forceLen) {
  2163. val = 0;
  2164. invalid = true;
  2165. } else {
  2166. break;
  2167. }
  2168. }
  2169. ++pos;
  2170. total = total * radix + val;
  2171. }
  2172. if (pos === start || len != null && pos - start !== len || invalid) {
  2173. return {
  2174. n: null,
  2175. pos
  2176. };
  2177. }
  2178. return {
  2179. n: total,
  2180. pos
  2181. };
  2182. }
  2183. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2184. const ch = input.charCodeAt(pos);
  2185. let code;
  2186. if (ch === 123) {
  2187. ++pos;
  2188. ({
  2189. code,
  2190. pos
  2191. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2192. ++pos;
  2193. if (code !== null && code > 0x10ffff) {
  2194. if (throwOnInvalid) {
  2195. errors.invalidCodePoint(pos, lineStart, curLine);
  2196. } else {
  2197. return {
  2198. code: null,
  2199. pos
  2200. };
  2201. }
  2202. }
  2203. } else {
  2204. ({
  2205. code,
  2206. pos
  2207. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2208. }
  2209. return {
  2210. code,
  2211. pos
  2212. };
  2213. }
  2214. const _excluded = ["at"],
  2215. _excluded2 = ["at"];
  2216. function buildPosition(pos, lineStart, curLine) {
  2217. return new Position(curLine, pos - lineStart, pos);
  2218. }
  2219. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2220. class Token {
  2221. constructor(state) {
  2222. this.type = state.type;
  2223. this.value = state.value;
  2224. this.start = state.start;
  2225. this.end = state.end;
  2226. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2227. }
  2228. }
  2229. class Tokenizer extends CommentsParser {
  2230. constructor(options, input) {
  2231. super();
  2232. this.isLookahead = void 0;
  2233. this.tokens = [];
  2234. this.errorHandlers_readInt = {
  2235. invalidDigit: (pos, lineStart, curLine, radix) => {
  2236. if (!this.options.errorRecovery) return false;
  2237. this.raise(Errors.InvalidDigit, {
  2238. at: buildPosition(pos, lineStart, curLine),
  2239. radix
  2240. });
  2241. return true;
  2242. },
  2243. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2244. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2245. };
  2246. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2247. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2248. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2249. });
  2250. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2251. strictNumericEscape: (pos, lineStart, curLine) => {
  2252. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2253. at: buildPosition(pos, lineStart, curLine)
  2254. });
  2255. },
  2256. unterminated: (pos, lineStart, curLine) => {
  2257. throw this.raise(Errors.UnterminatedString, {
  2258. at: buildPosition(pos - 1, lineStart, curLine)
  2259. });
  2260. }
  2261. });
  2262. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2263. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2264. unterminated: (pos, lineStart, curLine) => {
  2265. throw this.raise(Errors.UnterminatedTemplate, {
  2266. at: buildPosition(pos, lineStart, curLine)
  2267. });
  2268. }
  2269. });
  2270. this.state = new State();
  2271. this.state.init(options);
  2272. this.input = input;
  2273. this.length = input.length;
  2274. this.isLookahead = false;
  2275. }
  2276. pushToken(token) {
  2277. this.tokens.length = this.state.tokensLength;
  2278. this.tokens.push(token);
  2279. ++this.state.tokensLength;
  2280. }
  2281. next() {
  2282. this.checkKeywordEscapes();
  2283. if (this.options.tokens) {
  2284. this.pushToken(new Token(this.state));
  2285. }
  2286. this.state.lastTokStart = this.state.start;
  2287. this.state.lastTokEndLoc = this.state.endLoc;
  2288. this.state.lastTokStartLoc = this.state.startLoc;
  2289. this.nextToken();
  2290. }
  2291. eat(type) {
  2292. if (this.match(type)) {
  2293. this.next();
  2294. return true;
  2295. } else {
  2296. return false;
  2297. }
  2298. }
  2299. match(type) {
  2300. return this.state.type === type;
  2301. }
  2302. createLookaheadState(state) {
  2303. return {
  2304. pos: state.pos,
  2305. value: null,
  2306. type: state.type,
  2307. start: state.start,
  2308. end: state.end,
  2309. context: [this.curContext()],
  2310. inType: state.inType,
  2311. startLoc: state.startLoc,
  2312. lastTokEndLoc: state.lastTokEndLoc,
  2313. curLine: state.curLine,
  2314. lineStart: state.lineStart,
  2315. curPosition: state.curPosition
  2316. };
  2317. }
  2318. lookahead() {
  2319. const old = this.state;
  2320. this.state = this.createLookaheadState(old);
  2321. this.isLookahead = true;
  2322. this.nextToken();
  2323. this.isLookahead = false;
  2324. const curr = this.state;
  2325. this.state = old;
  2326. return curr;
  2327. }
  2328. nextTokenStart() {
  2329. return this.nextTokenStartSince(this.state.pos);
  2330. }
  2331. nextTokenStartSince(pos) {
  2332. skipWhiteSpace.lastIndex = pos;
  2333. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2334. }
  2335. lookaheadCharCode() {
  2336. return this.input.charCodeAt(this.nextTokenStart());
  2337. }
  2338. nextTokenInLineStart() {
  2339. return this.nextTokenInLineStartSince(this.state.pos);
  2340. }
  2341. nextTokenInLineStartSince(pos) {
  2342. skipWhiteSpaceInLine.lastIndex = pos;
  2343. return skipWhiteSpaceInLine.test(this.input) ? skipWhiteSpaceInLine.lastIndex : pos;
  2344. }
  2345. lookaheadInLineCharCode() {
  2346. return this.input.charCodeAt(this.nextTokenInLineStart());
  2347. }
  2348. codePointAtPos(pos) {
  2349. let cp = this.input.charCodeAt(pos);
  2350. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2351. const trail = this.input.charCodeAt(pos);
  2352. if ((trail & 0xfc00) === 0xdc00) {
  2353. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2354. }
  2355. }
  2356. return cp;
  2357. }
  2358. setStrict(strict) {
  2359. this.state.strict = strict;
  2360. if (strict) {
  2361. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  2362. at
  2363. }));
  2364. this.state.strictErrors.clear();
  2365. }
  2366. }
  2367. curContext() {
  2368. return this.state.context[this.state.context.length - 1];
  2369. }
  2370. nextToken() {
  2371. this.skipSpace();
  2372. this.state.start = this.state.pos;
  2373. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2374. if (this.state.pos >= this.length) {
  2375. this.finishToken(137);
  2376. return;
  2377. }
  2378. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2379. }
  2380. skipBlockComment(commentEnd) {
  2381. let startLoc;
  2382. if (!this.isLookahead) startLoc = this.state.curPosition();
  2383. const start = this.state.pos;
  2384. const end = this.input.indexOf(commentEnd, start + 2);
  2385. if (end === -1) {
  2386. throw this.raise(Errors.UnterminatedComment, {
  2387. at: this.state.curPosition()
  2388. });
  2389. }
  2390. this.state.pos = end + commentEnd.length;
  2391. lineBreakG.lastIndex = start + 2;
  2392. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2393. ++this.state.curLine;
  2394. this.state.lineStart = lineBreakG.lastIndex;
  2395. }
  2396. if (this.isLookahead) return;
  2397. const comment = {
  2398. type: "CommentBlock",
  2399. value: this.input.slice(start + 2, end),
  2400. start,
  2401. end: end + commentEnd.length,
  2402. loc: new SourceLocation(startLoc, this.state.curPosition())
  2403. };
  2404. if (this.options.tokens) this.pushToken(comment);
  2405. return comment;
  2406. }
  2407. skipLineComment(startSkip) {
  2408. const start = this.state.pos;
  2409. let startLoc;
  2410. if (!this.isLookahead) startLoc = this.state.curPosition();
  2411. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2412. if (this.state.pos < this.length) {
  2413. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2414. ch = this.input.charCodeAt(this.state.pos);
  2415. }
  2416. }
  2417. if (this.isLookahead) return;
  2418. const end = this.state.pos;
  2419. const value = this.input.slice(start + startSkip, end);
  2420. const comment = {
  2421. type: "CommentLine",
  2422. value,
  2423. start,
  2424. end,
  2425. loc: new SourceLocation(startLoc, this.state.curPosition())
  2426. };
  2427. if (this.options.tokens) this.pushToken(comment);
  2428. return comment;
  2429. }
  2430. skipSpace() {
  2431. const spaceStart = this.state.pos;
  2432. const comments = [];
  2433. loop: while (this.state.pos < this.length) {
  2434. const ch = this.input.charCodeAt(this.state.pos);
  2435. switch (ch) {
  2436. case 32:
  2437. case 160:
  2438. case 9:
  2439. ++this.state.pos;
  2440. break;
  2441. case 13:
  2442. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2443. ++this.state.pos;
  2444. }
  2445. case 10:
  2446. case 8232:
  2447. case 8233:
  2448. ++this.state.pos;
  2449. ++this.state.curLine;
  2450. this.state.lineStart = this.state.pos;
  2451. break;
  2452. case 47:
  2453. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2454. case 42:
  2455. {
  2456. const comment = this.skipBlockComment("*/");
  2457. if (comment !== undefined) {
  2458. this.addComment(comment);
  2459. if (this.options.attachComment) comments.push(comment);
  2460. }
  2461. break;
  2462. }
  2463. case 47:
  2464. {
  2465. const comment = this.skipLineComment(2);
  2466. if (comment !== undefined) {
  2467. this.addComment(comment);
  2468. if (this.options.attachComment) comments.push(comment);
  2469. }
  2470. break;
  2471. }
  2472. default:
  2473. break loop;
  2474. }
  2475. break;
  2476. default:
  2477. if (isWhitespace(ch)) {
  2478. ++this.state.pos;
  2479. } else if (ch === 45 && !this.inModule && this.options.annexB) {
  2480. const pos = this.state.pos;
  2481. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2482. const comment = this.skipLineComment(3);
  2483. if (comment !== undefined) {
  2484. this.addComment(comment);
  2485. if (this.options.attachComment) comments.push(comment);
  2486. }
  2487. } else {
  2488. break loop;
  2489. }
  2490. } else if (ch === 60 && !this.inModule && this.options.annexB) {
  2491. const pos = this.state.pos;
  2492. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2493. const comment = this.skipLineComment(4);
  2494. if (comment !== undefined) {
  2495. this.addComment(comment);
  2496. if (this.options.attachComment) comments.push(comment);
  2497. }
  2498. } else {
  2499. break loop;
  2500. }
  2501. } else {
  2502. break loop;
  2503. }
  2504. }
  2505. }
  2506. if (comments.length > 0) {
  2507. const end = this.state.pos;
  2508. const commentWhitespace = {
  2509. start: spaceStart,
  2510. end,
  2511. comments,
  2512. leadingNode: null,
  2513. trailingNode: null,
  2514. containingNode: null
  2515. };
  2516. this.state.commentStack.push(commentWhitespace);
  2517. }
  2518. }
  2519. finishToken(type, val) {
  2520. this.state.end = this.state.pos;
  2521. this.state.endLoc = this.state.curPosition();
  2522. const prevType = this.state.type;
  2523. this.state.type = type;
  2524. this.state.value = val;
  2525. if (!this.isLookahead) {
  2526. this.updateContext(prevType);
  2527. }
  2528. }
  2529. replaceToken(type) {
  2530. this.state.type = type;
  2531. this.updateContext();
  2532. }
  2533. readToken_numberSign() {
  2534. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2535. return;
  2536. }
  2537. const nextPos = this.state.pos + 1;
  2538. const next = this.codePointAtPos(nextPos);
  2539. if (next >= 48 && next <= 57) {
  2540. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  2541. at: this.state.curPosition()
  2542. });
  2543. }
  2544. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2545. this.expectPlugin("recordAndTuple");
  2546. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2547. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  2548. at: this.state.curPosition()
  2549. });
  2550. }
  2551. this.state.pos += 2;
  2552. if (next === 123) {
  2553. this.finishToken(7);
  2554. } else {
  2555. this.finishToken(1);
  2556. }
  2557. } else if (isIdentifierStart(next)) {
  2558. ++this.state.pos;
  2559. this.finishToken(136, this.readWord1(next));
  2560. } else if (next === 92) {
  2561. ++this.state.pos;
  2562. this.finishToken(136, this.readWord1());
  2563. } else {
  2564. this.finishOp(27, 1);
  2565. }
  2566. }
  2567. readToken_dot() {
  2568. const next = this.input.charCodeAt(this.state.pos + 1);
  2569. if (next >= 48 && next <= 57) {
  2570. this.readNumber(true);
  2571. return;
  2572. }
  2573. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2574. this.state.pos += 3;
  2575. this.finishToken(21);
  2576. } else {
  2577. ++this.state.pos;
  2578. this.finishToken(16);
  2579. }
  2580. }
  2581. readToken_slash() {
  2582. const next = this.input.charCodeAt(this.state.pos + 1);
  2583. if (next === 61) {
  2584. this.finishOp(31, 2);
  2585. } else {
  2586. this.finishOp(56, 1);
  2587. }
  2588. }
  2589. readToken_interpreter() {
  2590. if (this.state.pos !== 0 || this.length < 2) return false;
  2591. let ch = this.input.charCodeAt(this.state.pos + 1);
  2592. if (ch !== 33) return false;
  2593. const start = this.state.pos;
  2594. this.state.pos += 1;
  2595. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2596. ch = this.input.charCodeAt(this.state.pos);
  2597. }
  2598. const value = this.input.slice(start + 2, this.state.pos);
  2599. this.finishToken(28, value);
  2600. return true;
  2601. }
  2602. readToken_mult_modulo(code) {
  2603. let type = code === 42 ? 55 : 54;
  2604. let width = 1;
  2605. let next = this.input.charCodeAt(this.state.pos + 1);
  2606. if (code === 42 && next === 42) {
  2607. width++;
  2608. next = this.input.charCodeAt(this.state.pos + 2);
  2609. type = 57;
  2610. }
  2611. if (next === 61 && !this.state.inType) {
  2612. width++;
  2613. type = code === 37 ? 33 : 30;
  2614. }
  2615. this.finishOp(type, width);
  2616. }
  2617. readToken_pipe_amp(code) {
  2618. const next = this.input.charCodeAt(this.state.pos + 1);
  2619. if (next === code) {
  2620. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2621. this.finishOp(30, 3);
  2622. } else {
  2623. this.finishOp(code === 124 ? 41 : 42, 2);
  2624. }
  2625. return;
  2626. }
  2627. if (code === 124) {
  2628. if (next === 62) {
  2629. this.finishOp(39, 2);
  2630. return;
  2631. }
  2632. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2633. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2634. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2635. at: this.state.curPosition()
  2636. });
  2637. }
  2638. this.state.pos += 2;
  2639. this.finishToken(9);
  2640. return;
  2641. }
  2642. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2643. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2644. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2645. at: this.state.curPosition()
  2646. });
  2647. }
  2648. this.state.pos += 2;
  2649. this.finishToken(4);
  2650. return;
  2651. }
  2652. }
  2653. if (next === 61) {
  2654. this.finishOp(30, 2);
  2655. return;
  2656. }
  2657. this.finishOp(code === 124 ? 43 : 45, 1);
  2658. }
  2659. readToken_caret() {
  2660. const next = this.input.charCodeAt(this.state.pos + 1);
  2661. if (next === 61 && !this.state.inType) {
  2662. this.finishOp(32, 2);
  2663. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2664. proposal: "hack",
  2665. topicToken: "^^"
  2666. }])) {
  2667. this.finishOp(37, 2);
  2668. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2669. if (lookaheadCh === 94) {
  2670. this.unexpected();
  2671. }
  2672. } else {
  2673. this.finishOp(44, 1);
  2674. }
  2675. }
  2676. readToken_atSign() {
  2677. const next = this.input.charCodeAt(this.state.pos + 1);
  2678. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2679. proposal: "hack",
  2680. topicToken: "@@"
  2681. }])) {
  2682. this.finishOp(38, 2);
  2683. } else {
  2684. this.finishOp(26, 1);
  2685. }
  2686. }
  2687. readToken_plus_min(code) {
  2688. const next = this.input.charCodeAt(this.state.pos + 1);
  2689. if (next === code) {
  2690. this.finishOp(34, 2);
  2691. return;
  2692. }
  2693. if (next === 61) {
  2694. this.finishOp(30, 2);
  2695. } else {
  2696. this.finishOp(53, 1);
  2697. }
  2698. }
  2699. readToken_lt() {
  2700. const {
  2701. pos
  2702. } = this.state;
  2703. const next = this.input.charCodeAt(pos + 1);
  2704. if (next === 60) {
  2705. if (this.input.charCodeAt(pos + 2) === 61) {
  2706. this.finishOp(30, 3);
  2707. return;
  2708. }
  2709. this.finishOp(51, 2);
  2710. return;
  2711. }
  2712. if (next === 61) {
  2713. this.finishOp(49, 2);
  2714. return;
  2715. }
  2716. this.finishOp(47, 1);
  2717. }
  2718. readToken_gt() {
  2719. const {
  2720. pos
  2721. } = this.state;
  2722. const next = this.input.charCodeAt(pos + 1);
  2723. if (next === 62) {
  2724. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2725. if (this.input.charCodeAt(pos + size) === 61) {
  2726. this.finishOp(30, size + 1);
  2727. return;
  2728. }
  2729. this.finishOp(52, size);
  2730. return;
  2731. }
  2732. if (next === 61) {
  2733. this.finishOp(49, 2);
  2734. return;
  2735. }
  2736. this.finishOp(48, 1);
  2737. }
  2738. readToken_eq_excl(code) {
  2739. const next = this.input.charCodeAt(this.state.pos + 1);
  2740. if (next === 61) {
  2741. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2742. return;
  2743. }
  2744. if (code === 61 && next === 62) {
  2745. this.state.pos += 2;
  2746. this.finishToken(19);
  2747. return;
  2748. }
  2749. this.finishOp(code === 61 ? 29 : 35, 1);
  2750. }
  2751. readToken_question() {
  2752. const next = this.input.charCodeAt(this.state.pos + 1);
  2753. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2754. if (next === 63) {
  2755. if (next2 === 61) {
  2756. this.finishOp(30, 3);
  2757. } else {
  2758. this.finishOp(40, 2);
  2759. }
  2760. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2761. this.state.pos += 2;
  2762. this.finishToken(18);
  2763. } else {
  2764. ++this.state.pos;
  2765. this.finishToken(17);
  2766. }
  2767. }
  2768. getTokenFromCode(code) {
  2769. switch (code) {
  2770. case 46:
  2771. this.readToken_dot();
  2772. return;
  2773. case 40:
  2774. ++this.state.pos;
  2775. this.finishToken(10);
  2776. return;
  2777. case 41:
  2778. ++this.state.pos;
  2779. this.finishToken(11);
  2780. return;
  2781. case 59:
  2782. ++this.state.pos;
  2783. this.finishToken(13);
  2784. return;
  2785. case 44:
  2786. ++this.state.pos;
  2787. this.finishToken(12);
  2788. return;
  2789. case 91:
  2790. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2791. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2792. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2793. at: this.state.curPosition()
  2794. });
  2795. }
  2796. this.state.pos += 2;
  2797. this.finishToken(2);
  2798. } else {
  2799. ++this.state.pos;
  2800. this.finishToken(0);
  2801. }
  2802. return;
  2803. case 93:
  2804. ++this.state.pos;
  2805. this.finishToken(3);
  2806. return;
  2807. case 123:
  2808. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2809. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2810. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2811. at: this.state.curPosition()
  2812. });
  2813. }
  2814. this.state.pos += 2;
  2815. this.finishToken(6);
  2816. } else {
  2817. ++this.state.pos;
  2818. this.finishToken(5);
  2819. }
  2820. return;
  2821. case 125:
  2822. ++this.state.pos;
  2823. this.finishToken(8);
  2824. return;
  2825. case 58:
  2826. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2827. this.finishOp(15, 2);
  2828. } else {
  2829. ++this.state.pos;
  2830. this.finishToken(14);
  2831. }
  2832. return;
  2833. case 63:
  2834. this.readToken_question();
  2835. return;
  2836. case 96:
  2837. this.readTemplateToken();
  2838. return;
  2839. case 48:
  2840. {
  2841. const next = this.input.charCodeAt(this.state.pos + 1);
  2842. if (next === 120 || next === 88) {
  2843. this.readRadixNumber(16);
  2844. return;
  2845. }
  2846. if (next === 111 || next === 79) {
  2847. this.readRadixNumber(8);
  2848. return;
  2849. }
  2850. if (next === 98 || next === 66) {
  2851. this.readRadixNumber(2);
  2852. return;
  2853. }
  2854. }
  2855. case 49:
  2856. case 50:
  2857. case 51:
  2858. case 52:
  2859. case 53:
  2860. case 54:
  2861. case 55:
  2862. case 56:
  2863. case 57:
  2864. this.readNumber(false);
  2865. return;
  2866. case 34:
  2867. case 39:
  2868. this.readString(code);
  2869. return;
  2870. case 47:
  2871. this.readToken_slash();
  2872. return;
  2873. case 37:
  2874. case 42:
  2875. this.readToken_mult_modulo(code);
  2876. return;
  2877. case 124:
  2878. case 38:
  2879. this.readToken_pipe_amp(code);
  2880. return;
  2881. case 94:
  2882. this.readToken_caret();
  2883. return;
  2884. case 43:
  2885. case 45:
  2886. this.readToken_plus_min(code);
  2887. return;
  2888. case 60:
  2889. this.readToken_lt();
  2890. return;
  2891. case 62:
  2892. this.readToken_gt();
  2893. return;
  2894. case 61:
  2895. case 33:
  2896. this.readToken_eq_excl(code);
  2897. return;
  2898. case 126:
  2899. this.finishOp(36, 1);
  2900. return;
  2901. case 64:
  2902. this.readToken_atSign();
  2903. return;
  2904. case 35:
  2905. this.readToken_numberSign();
  2906. return;
  2907. case 92:
  2908. this.readWord();
  2909. return;
  2910. default:
  2911. if (isIdentifierStart(code)) {
  2912. this.readWord(code);
  2913. return;
  2914. }
  2915. }
  2916. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2917. at: this.state.curPosition(),
  2918. unexpected: String.fromCodePoint(code)
  2919. });
  2920. }
  2921. finishOp(type, size) {
  2922. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2923. this.state.pos += size;
  2924. this.finishToken(type, str);
  2925. }
  2926. readRegexp() {
  2927. const startLoc = this.state.startLoc;
  2928. const start = this.state.start + 1;
  2929. let escaped, inClass;
  2930. let {
  2931. pos
  2932. } = this.state;
  2933. for (;; ++pos) {
  2934. if (pos >= this.length) {
  2935. throw this.raise(Errors.UnterminatedRegExp, {
  2936. at: createPositionWithColumnOffset(startLoc, 1)
  2937. });
  2938. }
  2939. const ch = this.input.charCodeAt(pos);
  2940. if (isNewLine(ch)) {
  2941. throw this.raise(Errors.UnterminatedRegExp, {
  2942. at: createPositionWithColumnOffset(startLoc, 1)
  2943. });
  2944. }
  2945. if (escaped) {
  2946. escaped = false;
  2947. } else {
  2948. if (ch === 91) {
  2949. inClass = true;
  2950. } else if (ch === 93 && inClass) {
  2951. inClass = false;
  2952. } else if (ch === 47 && !inClass) {
  2953. break;
  2954. }
  2955. escaped = ch === 92;
  2956. }
  2957. }
  2958. const content = this.input.slice(start, pos);
  2959. ++pos;
  2960. let mods = "";
  2961. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2962. while (pos < this.length) {
  2963. const cp = this.codePointAtPos(pos);
  2964. const char = String.fromCharCode(cp);
  2965. if (VALID_REGEX_FLAGS.has(cp)) {
  2966. if (cp === 118) {
  2967. if (mods.includes("u")) {
  2968. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2969. at: nextPos()
  2970. });
  2971. }
  2972. } else if (cp === 117) {
  2973. if (mods.includes("v")) {
  2974. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2975. at: nextPos()
  2976. });
  2977. }
  2978. }
  2979. if (mods.includes(char)) {
  2980. this.raise(Errors.DuplicateRegExpFlags, {
  2981. at: nextPos()
  2982. });
  2983. }
  2984. } else if (isIdentifierChar(cp) || cp === 92) {
  2985. this.raise(Errors.MalformedRegExpFlags, {
  2986. at: nextPos()
  2987. });
  2988. } else {
  2989. break;
  2990. }
  2991. ++pos;
  2992. mods += char;
  2993. }
  2994. this.state.pos = pos;
  2995. this.finishToken(135, {
  2996. pattern: content,
  2997. flags: mods
  2998. });
  2999. }
  3000. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  3001. const {
  3002. n,
  3003. pos
  3004. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  3005. this.state.pos = pos;
  3006. return n;
  3007. }
  3008. readRadixNumber(radix) {
  3009. const startLoc = this.state.curPosition();
  3010. let isBigInt = false;
  3011. this.state.pos += 2;
  3012. const val = this.readInt(radix);
  3013. if (val == null) {
  3014. this.raise(Errors.InvalidDigit, {
  3015. at: createPositionWithColumnOffset(startLoc, 2),
  3016. radix
  3017. });
  3018. }
  3019. const next = this.input.charCodeAt(this.state.pos);
  3020. if (next === 110) {
  3021. ++this.state.pos;
  3022. isBigInt = true;
  3023. } else if (next === 109) {
  3024. throw this.raise(Errors.InvalidDecimal, {
  3025. at: startLoc
  3026. });
  3027. }
  3028. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3029. throw this.raise(Errors.NumberIdentifier, {
  3030. at: this.state.curPosition()
  3031. });
  3032. }
  3033. if (isBigInt) {
  3034. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  3035. this.finishToken(133, str);
  3036. return;
  3037. }
  3038. this.finishToken(132, val);
  3039. }
  3040. readNumber(startsWithDot) {
  3041. const start = this.state.pos;
  3042. const startLoc = this.state.curPosition();
  3043. let isFloat = false;
  3044. let isBigInt = false;
  3045. let isDecimal = false;
  3046. let hasExponent = false;
  3047. let isOctal = false;
  3048. if (!startsWithDot && this.readInt(10) === null) {
  3049. this.raise(Errors.InvalidNumber, {
  3050. at: this.state.curPosition()
  3051. });
  3052. }
  3053. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3054. if (hasLeadingZero) {
  3055. const integer = this.input.slice(start, this.state.pos);
  3056. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  3057. at: startLoc
  3058. });
  3059. if (!this.state.strict) {
  3060. const underscorePos = integer.indexOf("_");
  3061. if (underscorePos > 0) {
  3062. this.raise(Errors.ZeroDigitNumericSeparator, {
  3063. at: createPositionWithColumnOffset(startLoc, underscorePos)
  3064. });
  3065. }
  3066. }
  3067. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3068. }
  3069. let next = this.input.charCodeAt(this.state.pos);
  3070. if (next === 46 && !isOctal) {
  3071. ++this.state.pos;
  3072. this.readInt(10);
  3073. isFloat = true;
  3074. next = this.input.charCodeAt(this.state.pos);
  3075. }
  3076. if ((next === 69 || next === 101) && !isOctal) {
  3077. next = this.input.charCodeAt(++this.state.pos);
  3078. if (next === 43 || next === 45) {
  3079. ++this.state.pos;
  3080. }
  3081. if (this.readInt(10) === null) {
  3082. this.raise(Errors.InvalidOrMissingExponent, {
  3083. at: startLoc
  3084. });
  3085. }
  3086. isFloat = true;
  3087. hasExponent = true;
  3088. next = this.input.charCodeAt(this.state.pos);
  3089. }
  3090. if (next === 110) {
  3091. if (isFloat || hasLeadingZero) {
  3092. this.raise(Errors.InvalidBigIntLiteral, {
  3093. at: startLoc
  3094. });
  3095. }
  3096. ++this.state.pos;
  3097. isBigInt = true;
  3098. }
  3099. if (next === 109) {
  3100. this.expectPlugin("decimal", this.state.curPosition());
  3101. if (hasExponent || hasLeadingZero) {
  3102. this.raise(Errors.InvalidDecimal, {
  3103. at: startLoc
  3104. });
  3105. }
  3106. ++this.state.pos;
  3107. isDecimal = true;
  3108. }
  3109. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3110. throw this.raise(Errors.NumberIdentifier, {
  3111. at: this.state.curPosition()
  3112. });
  3113. }
  3114. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3115. if (isBigInt) {
  3116. this.finishToken(133, str);
  3117. return;
  3118. }
  3119. if (isDecimal) {
  3120. this.finishToken(134, str);
  3121. return;
  3122. }
  3123. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3124. this.finishToken(132, val);
  3125. }
  3126. readCodePoint(throwOnInvalid) {
  3127. const {
  3128. code,
  3129. pos
  3130. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3131. this.state.pos = pos;
  3132. return code;
  3133. }
  3134. readString(quote) {
  3135. const {
  3136. str,
  3137. pos,
  3138. curLine,
  3139. lineStart
  3140. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3141. this.state.pos = pos + 1;
  3142. this.state.lineStart = lineStart;
  3143. this.state.curLine = curLine;
  3144. this.finishToken(131, str);
  3145. }
  3146. readTemplateContinuation() {
  3147. if (!this.match(8)) {
  3148. this.unexpected(null, 8);
  3149. }
  3150. this.state.pos--;
  3151. this.readTemplateToken();
  3152. }
  3153. readTemplateToken() {
  3154. const opening = this.input[this.state.pos];
  3155. const {
  3156. str,
  3157. firstInvalidLoc,
  3158. pos,
  3159. curLine,
  3160. lineStart
  3161. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3162. this.state.pos = pos + 1;
  3163. this.state.lineStart = lineStart;
  3164. this.state.curLine = curLine;
  3165. if (firstInvalidLoc) {
  3166. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3167. }
  3168. if (this.input.codePointAt(pos) === 96) {
  3169. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3170. } else {
  3171. this.state.pos++;
  3172. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3173. }
  3174. }
  3175. recordStrictModeErrors(toParseError, {
  3176. at
  3177. }) {
  3178. const index = at.index;
  3179. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3180. this.raise(toParseError, {
  3181. at
  3182. });
  3183. } else {
  3184. this.state.strictErrors.set(index, [toParseError, at]);
  3185. }
  3186. }
  3187. readWord1(firstCode) {
  3188. this.state.containsEsc = false;
  3189. let word = "";
  3190. const start = this.state.pos;
  3191. let chunkStart = this.state.pos;
  3192. if (firstCode !== undefined) {
  3193. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3194. }
  3195. while (this.state.pos < this.length) {
  3196. const ch = this.codePointAtPos(this.state.pos);
  3197. if (isIdentifierChar(ch)) {
  3198. this.state.pos += ch <= 0xffff ? 1 : 2;
  3199. } else if (ch === 92) {
  3200. this.state.containsEsc = true;
  3201. word += this.input.slice(chunkStart, this.state.pos);
  3202. const escStart = this.state.curPosition();
  3203. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3204. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3205. this.raise(Errors.MissingUnicodeEscape, {
  3206. at: this.state.curPosition()
  3207. });
  3208. chunkStart = this.state.pos - 1;
  3209. continue;
  3210. }
  3211. ++this.state.pos;
  3212. const esc = this.readCodePoint(true);
  3213. if (esc !== null) {
  3214. if (!identifierCheck(esc)) {
  3215. this.raise(Errors.EscapedCharNotAnIdentifier, {
  3216. at: escStart
  3217. });
  3218. }
  3219. word += String.fromCodePoint(esc);
  3220. }
  3221. chunkStart = this.state.pos;
  3222. } else {
  3223. break;
  3224. }
  3225. }
  3226. return word + this.input.slice(chunkStart, this.state.pos);
  3227. }
  3228. readWord(firstCode) {
  3229. const word = this.readWord1(firstCode);
  3230. const type = keywords$1.get(word);
  3231. if (type !== undefined) {
  3232. this.finishToken(type, tokenLabelName(type));
  3233. } else {
  3234. this.finishToken(130, word);
  3235. }
  3236. }
  3237. checkKeywordEscapes() {
  3238. const {
  3239. type
  3240. } = this.state;
  3241. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3242. this.raise(Errors.InvalidEscapedReservedWord, {
  3243. at: this.state.startLoc,
  3244. reservedWord: tokenLabelName(type)
  3245. });
  3246. }
  3247. }
  3248. raise(toParseError, raiseProperties) {
  3249. const {
  3250. at
  3251. } = raiseProperties,
  3252. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  3253. const loc = at instanceof Position ? at : at.loc.start;
  3254. const error = toParseError({
  3255. loc,
  3256. details
  3257. });
  3258. if (!this.options.errorRecovery) throw error;
  3259. if (!this.isLookahead) this.state.errors.push(error);
  3260. return error;
  3261. }
  3262. raiseOverwrite(toParseError, raiseProperties) {
  3263. const {
  3264. at
  3265. } = raiseProperties,
  3266. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  3267. const loc = at instanceof Position ? at : at.loc.start;
  3268. const pos = loc.index;
  3269. const errors = this.state.errors;
  3270. for (let i = errors.length - 1; i >= 0; i--) {
  3271. const error = errors[i];
  3272. if (error.loc.index === pos) {
  3273. return errors[i] = toParseError({
  3274. loc,
  3275. details
  3276. });
  3277. }
  3278. if (error.loc.index < pos) break;
  3279. }
  3280. return this.raise(toParseError, raiseProperties);
  3281. }
  3282. updateContext(prevType) {}
  3283. unexpected(loc, type) {
  3284. throw this.raise(Errors.UnexpectedToken, {
  3285. expected: type ? tokenLabelName(type) : null,
  3286. at: loc != null ? loc : this.state.startLoc
  3287. });
  3288. }
  3289. expectPlugin(pluginName, loc) {
  3290. if (this.hasPlugin(pluginName)) {
  3291. return true;
  3292. }
  3293. throw this.raise(Errors.MissingPlugin, {
  3294. at: loc != null ? loc : this.state.startLoc,
  3295. missingPlugin: [pluginName]
  3296. });
  3297. }
  3298. expectOnePlugin(pluginNames) {
  3299. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3300. throw this.raise(Errors.MissingOneOfPlugins, {
  3301. at: this.state.startLoc,
  3302. missingPlugin: pluginNames
  3303. });
  3304. }
  3305. }
  3306. errorBuilder(error) {
  3307. return (pos, lineStart, curLine) => {
  3308. this.raise(error, {
  3309. at: buildPosition(pos, lineStart, curLine)
  3310. });
  3311. };
  3312. }
  3313. }
  3314. class ClassScope {
  3315. constructor() {
  3316. this.privateNames = new Set();
  3317. this.loneAccessors = new Map();
  3318. this.undefinedPrivateNames = new Map();
  3319. }
  3320. }
  3321. class ClassScopeHandler {
  3322. constructor(parser) {
  3323. this.parser = void 0;
  3324. this.stack = [];
  3325. this.undefinedPrivateNames = new Map();
  3326. this.parser = parser;
  3327. }
  3328. current() {
  3329. return this.stack[this.stack.length - 1];
  3330. }
  3331. enter() {
  3332. this.stack.push(new ClassScope());
  3333. }
  3334. exit() {
  3335. const oldClassScope = this.stack.pop();
  3336. const current = this.current();
  3337. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3338. if (current) {
  3339. if (!current.undefinedPrivateNames.has(name)) {
  3340. current.undefinedPrivateNames.set(name, loc);
  3341. }
  3342. } else {
  3343. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3344. at: loc,
  3345. identifierName: name
  3346. });
  3347. }
  3348. }
  3349. }
  3350. declarePrivateName(name, elementType, loc) {
  3351. const {
  3352. privateNames,
  3353. loneAccessors,
  3354. undefinedPrivateNames
  3355. } = this.current();
  3356. let redefined = privateNames.has(name);
  3357. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3358. const accessor = redefined && loneAccessors.get(name);
  3359. if (accessor) {
  3360. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3361. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3362. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3363. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3364. redefined = oldKind === newKind || oldStatic !== newStatic;
  3365. if (!redefined) loneAccessors.delete(name);
  3366. } else if (!redefined) {
  3367. loneAccessors.set(name, elementType);
  3368. }
  3369. }
  3370. if (redefined) {
  3371. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3372. at: loc,
  3373. identifierName: name
  3374. });
  3375. }
  3376. privateNames.add(name);
  3377. undefinedPrivateNames.delete(name);
  3378. }
  3379. usePrivateName(name, loc) {
  3380. let classScope;
  3381. for (classScope of this.stack) {
  3382. if (classScope.privateNames.has(name)) return;
  3383. }
  3384. if (classScope) {
  3385. classScope.undefinedPrivateNames.set(name, loc);
  3386. } else {
  3387. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3388. at: loc,
  3389. identifierName: name
  3390. });
  3391. }
  3392. }
  3393. }
  3394. const kExpression = 0,
  3395. kMaybeArrowParameterDeclaration = 1,
  3396. kMaybeAsyncArrowParameterDeclaration = 2,
  3397. kParameterDeclaration = 3;
  3398. class ExpressionScope {
  3399. constructor(type = kExpression) {
  3400. this.type = void 0;
  3401. this.type = type;
  3402. }
  3403. canBeArrowParameterDeclaration() {
  3404. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3405. }
  3406. isCertainlyParameterDeclaration() {
  3407. return this.type === kParameterDeclaration;
  3408. }
  3409. }
  3410. class ArrowHeadParsingScope extends ExpressionScope {
  3411. constructor(type) {
  3412. super(type);
  3413. this.declarationErrors = new Map();
  3414. }
  3415. recordDeclarationError(ParsingErrorClass, {
  3416. at
  3417. }) {
  3418. const index = at.index;
  3419. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3420. }
  3421. clearDeclarationError(index) {
  3422. this.declarationErrors.delete(index);
  3423. }
  3424. iterateErrors(iterator) {
  3425. this.declarationErrors.forEach(iterator);
  3426. }
  3427. }
  3428. class ExpressionScopeHandler {
  3429. constructor(parser) {
  3430. this.parser = void 0;
  3431. this.stack = [new ExpressionScope()];
  3432. this.parser = parser;
  3433. }
  3434. enter(scope) {
  3435. this.stack.push(scope);
  3436. }
  3437. exit() {
  3438. this.stack.pop();
  3439. }
  3440. recordParameterInitializerError(toParseError, {
  3441. at: node
  3442. }) {
  3443. const origin = {
  3444. at: node.loc.start
  3445. };
  3446. const {
  3447. stack
  3448. } = this;
  3449. let i = stack.length - 1;
  3450. let scope = stack[i];
  3451. while (!scope.isCertainlyParameterDeclaration()) {
  3452. if (scope.canBeArrowParameterDeclaration()) {
  3453. scope.recordDeclarationError(toParseError, origin);
  3454. } else {
  3455. return;
  3456. }
  3457. scope = stack[--i];
  3458. }
  3459. this.parser.raise(toParseError, origin);
  3460. }
  3461. recordArrowParameterBindingError(error, {
  3462. at: node
  3463. }) {
  3464. const {
  3465. stack
  3466. } = this;
  3467. const scope = stack[stack.length - 1];
  3468. const origin = {
  3469. at: node.loc.start
  3470. };
  3471. if (scope.isCertainlyParameterDeclaration()) {
  3472. this.parser.raise(error, origin);
  3473. } else if (scope.canBeArrowParameterDeclaration()) {
  3474. scope.recordDeclarationError(error, origin);
  3475. } else {
  3476. return;
  3477. }
  3478. }
  3479. recordAsyncArrowParametersError({
  3480. at
  3481. }) {
  3482. const {
  3483. stack
  3484. } = this;
  3485. let i = stack.length - 1;
  3486. let scope = stack[i];
  3487. while (scope.canBeArrowParameterDeclaration()) {
  3488. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3489. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3490. at
  3491. });
  3492. }
  3493. scope = stack[--i];
  3494. }
  3495. }
  3496. validateAsPattern() {
  3497. const {
  3498. stack
  3499. } = this;
  3500. const currentScope = stack[stack.length - 1];
  3501. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3502. currentScope.iterateErrors(([toParseError, loc]) => {
  3503. this.parser.raise(toParseError, {
  3504. at: loc
  3505. });
  3506. let i = stack.length - 2;
  3507. let scope = stack[i];
  3508. while (scope.canBeArrowParameterDeclaration()) {
  3509. scope.clearDeclarationError(loc.index);
  3510. scope = stack[--i];
  3511. }
  3512. });
  3513. }
  3514. }
  3515. function newParameterDeclarationScope() {
  3516. return new ExpressionScope(kParameterDeclaration);
  3517. }
  3518. function newArrowHeadScope() {
  3519. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3520. }
  3521. function newAsyncArrowScope() {
  3522. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3523. }
  3524. function newExpressionScope() {
  3525. return new ExpressionScope();
  3526. }
  3527. const PARAM = 0b0000,
  3528. PARAM_YIELD = 0b0001,
  3529. PARAM_AWAIT = 0b0010,
  3530. PARAM_RETURN = 0b0100,
  3531. PARAM_IN = 0b1000;
  3532. class ProductionParameterHandler {
  3533. constructor() {
  3534. this.stacks = [];
  3535. }
  3536. enter(flags) {
  3537. this.stacks.push(flags);
  3538. }
  3539. exit() {
  3540. this.stacks.pop();
  3541. }
  3542. currentFlags() {
  3543. return this.stacks[this.stacks.length - 1];
  3544. }
  3545. get hasAwait() {
  3546. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3547. }
  3548. get hasYield() {
  3549. return (this.currentFlags() & PARAM_YIELD) > 0;
  3550. }
  3551. get hasReturn() {
  3552. return (this.currentFlags() & PARAM_RETURN) > 0;
  3553. }
  3554. get hasIn() {
  3555. return (this.currentFlags() & PARAM_IN) > 0;
  3556. }
  3557. }
  3558. function functionFlags(isAsync, isGenerator) {
  3559. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3560. }
  3561. class UtilParser extends Tokenizer {
  3562. addExtra(node, key, value, enumerable = true) {
  3563. if (!node) return;
  3564. const extra = node.extra = node.extra || {};
  3565. if (enumerable) {
  3566. extra[key] = value;
  3567. } else {
  3568. Object.defineProperty(extra, key, {
  3569. enumerable,
  3570. value
  3571. });
  3572. }
  3573. }
  3574. isContextual(token) {
  3575. return this.state.type === token && !this.state.containsEsc;
  3576. }
  3577. isUnparsedContextual(nameStart, name) {
  3578. const nameEnd = nameStart + name.length;
  3579. if (this.input.slice(nameStart, nameEnd) === name) {
  3580. const nextCh = this.input.charCodeAt(nameEnd);
  3581. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3582. }
  3583. return false;
  3584. }
  3585. isLookaheadContextual(name) {
  3586. const next = this.nextTokenStart();
  3587. return this.isUnparsedContextual(next, name);
  3588. }
  3589. eatContextual(token) {
  3590. if (this.isContextual(token)) {
  3591. this.next();
  3592. return true;
  3593. }
  3594. return false;
  3595. }
  3596. expectContextual(token, toParseError) {
  3597. if (!this.eatContextual(token)) {
  3598. if (toParseError != null) {
  3599. throw this.raise(toParseError, {
  3600. at: this.state.startLoc
  3601. });
  3602. }
  3603. this.unexpected(null, token);
  3604. }
  3605. }
  3606. canInsertSemicolon() {
  3607. return this.match(137) || this.match(8) || this.hasPrecedingLineBreak();
  3608. }
  3609. hasPrecedingLineBreak() {
  3610. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3611. }
  3612. hasFollowingLineBreak() {
  3613. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3614. return skipWhiteSpaceToLineBreak.test(this.input);
  3615. }
  3616. isLineTerminator() {
  3617. return this.eat(13) || this.canInsertSemicolon();
  3618. }
  3619. semicolon(allowAsi = true) {
  3620. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3621. this.raise(Errors.MissingSemicolon, {
  3622. at: this.state.lastTokEndLoc
  3623. });
  3624. }
  3625. expect(type, loc) {
  3626. this.eat(type) || this.unexpected(loc, type);
  3627. }
  3628. tryParse(fn, oldState = this.state.clone()) {
  3629. const abortSignal = {
  3630. node: null
  3631. };
  3632. try {
  3633. const node = fn((node = null) => {
  3634. abortSignal.node = node;
  3635. throw abortSignal;
  3636. });
  3637. if (this.state.errors.length > oldState.errors.length) {
  3638. const failState = this.state;
  3639. this.state = oldState;
  3640. this.state.tokensLength = failState.tokensLength;
  3641. return {
  3642. node,
  3643. error: failState.errors[oldState.errors.length],
  3644. thrown: false,
  3645. aborted: false,
  3646. failState
  3647. };
  3648. }
  3649. return {
  3650. node,
  3651. error: null,
  3652. thrown: false,
  3653. aborted: false,
  3654. failState: null
  3655. };
  3656. } catch (error) {
  3657. const failState = this.state;
  3658. this.state = oldState;
  3659. if (error instanceof SyntaxError) {
  3660. return {
  3661. node: null,
  3662. error,
  3663. thrown: true,
  3664. aborted: false,
  3665. failState
  3666. };
  3667. }
  3668. if (error === abortSignal) {
  3669. return {
  3670. node: abortSignal.node,
  3671. error: null,
  3672. thrown: false,
  3673. aborted: true,
  3674. failState
  3675. };
  3676. }
  3677. throw error;
  3678. }
  3679. }
  3680. checkExpressionErrors(refExpressionErrors, andThrow) {
  3681. if (!refExpressionErrors) return false;
  3682. const {
  3683. shorthandAssignLoc,
  3684. doubleProtoLoc,
  3685. privateKeyLoc,
  3686. optionalParametersLoc
  3687. } = refExpressionErrors;
  3688. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3689. if (!andThrow) {
  3690. return hasErrors;
  3691. }
  3692. if (shorthandAssignLoc != null) {
  3693. this.raise(Errors.InvalidCoverInitializedName, {
  3694. at: shorthandAssignLoc
  3695. });
  3696. }
  3697. if (doubleProtoLoc != null) {
  3698. this.raise(Errors.DuplicateProto, {
  3699. at: doubleProtoLoc
  3700. });
  3701. }
  3702. if (privateKeyLoc != null) {
  3703. this.raise(Errors.UnexpectedPrivateField, {
  3704. at: privateKeyLoc
  3705. });
  3706. }
  3707. if (optionalParametersLoc != null) {
  3708. this.unexpected(optionalParametersLoc);
  3709. }
  3710. }
  3711. isLiteralPropertyName() {
  3712. return tokenIsLiteralPropertyName(this.state.type);
  3713. }
  3714. isPrivateName(node) {
  3715. return node.type === "PrivateName";
  3716. }
  3717. getPrivateNameSV(node) {
  3718. return node.id.name;
  3719. }
  3720. hasPropertyAsPrivateName(node) {
  3721. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3722. }
  3723. isObjectProperty(node) {
  3724. return node.type === "ObjectProperty";
  3725. }
  3726. isObjectMethod(node) {
  3727. return node.type === "ObjectMethod";
  3728. }
  3729. initializeScopes(inModule = this.options.sourceType === "module") {
  3730. const oldLabels = this.state.labels;
  3731. this.state.labels = [];
  3732. const oldExportedIdentifiers = this.exportedIdentifiers;
  3733. this.exportedIdentifiers = new Set();
  3734. const oldInModule = this.inModule;
  3735. this.inModule = inModule;
  3736. const oldScope = this.scope;
  3737. const ScopeHandler = this.getScopeHandler();
  3738. this.scope = new ScopeHandler(this, inModule);
  3739. const oldProdParam = this.prodParam;
  3740. this.prodParam = new ProductionParameterHandler();
  3741. const oldClassScope = this.classScope;
  3742. this.classScope = new ClassScopeHandler(this);
  3743. const oldExpressionScope = this.expressionScope;
  3744. this.expressionScope = new ExpressionScopeHandler(this);
  3745. return () => {
  3746. this.state.labels = oldLabels;
  3747. this.exportedIdentifiers = oldExportedIdentifiers;
  3748. this.inModule = oldInModule;
  3749. this.scope = oldScope;
  3750. this.prodParam = oldProdParam;
  3751. this.classScope = oldClassScope;
  3752. this.expressionScope = oldExpressionScope;
  3753. };
  3754. }
  3755. enterInitialScopes() {
  3756. let paramFlags = PARAM;
  3757. if (this.inModule) {
  3758. paramFlags |= PARAM_AWAIT;
  3759. }
  3760. this.scope.enter(SCOPE_PROGRAM);
  3761. this.prodParam.enter(paramFlags);
  3762. }
  3763. checkDestructuringPrivate(refExpressionErrors) {
  3764. const {
  3765. privateKeyLoc
  3766. } = refExpressionErrors;
  3767. if (privateKeyLoc !== null) {
  3768. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3769. }
  3770. }
  3771. }
  3772. class ExpressionErrors {
  3773. constructor() {
  3774. this.shorthandAssignLoc = null;
  3775. this.doubleProtoLoc = null;
  3776. this.privateKeyLoc = null;
  3777. this.optionalParametersLoc = null;
  3778. }
  3779. }
  3780. class Node {
  3781. constructor(parser, pos, loc) {
  3782. this.type = "";
  3783. this.start = pos;
  3784. this.end = 0;
  3785. this.loc = new SourceLocation(loc);
  3786. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3787. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3788. }
  3789. }
  3790. const NodePrototype = Node.prototype;
  3791. {
  3792. NodePrototype.__clone = function () {
  3793. const newNode = new Node(undefined, this.start, this.loc.start);
  3794. const keys = Object.keys(this);
  3795. for (let i = 0, length = keys.length; i < length; i++) {
  3796. const key = keys[i];
  3797. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3798. newNode[key] = this[key];
  3799. }
  3800. }
  3801. return newNode;
  3802. };
  3803. }
  3804. function clonePlaceholder(node) {
  3805. return cloneIdentifier(node);
  3806. }
  3807. function cloneIdentifier(node) {
  3808. const {
  3809. type,
  3810. start,
  3811. end,
  3812. loc,
  3813. range,
  3814. extra,
  3815. name
  3816. } = node;
  3817. const cloned = Object.create(NodePrototype);
  3818. cloned.type = type;
  3819. cloned.start = start;
  3820. cloned.end = end;
  3821. cloned.loc = loc;
  3822. cloned.range = range;
  3823. cloned.extra = extra;
  3824. cloned.name = name;
  3825. if (type === "Placeholder") {
  3826. cloned.expectedNode = node.expectedNode;
  3827. }
  3828. return cloned;
  3829. }
  3830. function cloneStringLiteral(node) {
  3831. const {
  3832. type,
  3833. start,
  3834. end,
  3835. loc,
  3836. range,
  3837. extra
  3838. } = node;
  3839. if (type === "Placeholder") {
  3840. return clonePlaceholder(node);
  3841. }
  3842. const cloned = Object.create(NodePrototype);
  3843. cloned.type = type;
  3844. cloned.start = start;
  3845. cloned.end = end;
  3846. cloned.loc = loc;
  3847. cloned.range = range;
  3848. if (node.raw !== undefined) {
  3849. cloned.raw = node.raw;
  3850. } else {
  3851. cloned.extra = extra;
  3852. }
  3853. cloned.value = node.value;
  3854. return cloned;
  3855. }
  3856. class NodeUtils extends UtilParser {
  3857. startNode() {
  3858. return new Node(this, this.state.start, this.state.startLoc);
  3859. }
  3860. startNodeAt(loc) {
  3861. return new Node(this, loc.index, loc);
  3862. }
  3863. startNodeAtNode(type) {
  3864. return this.startNodeAt(type.loc.start);
  3865. }
  3866. finishNode(node, type) {
  3867. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3868. }
  3869. finishNodeAt(node, type, endLoc) {
  3870. node.type = type;
  3871. node.end = endLoc.index;
  3872. node.loc.end = endLoc;
  3873. if (this.options.ranges) node.range[1] = endLoc.index;
  3874. if (this.options.attachComment) this.processComment(node);
  3875. return node;
  3876. }
  3877. resetStartLocation(node, startLoc) {
  3878. node.start = startLoc.index;
  3879. node.loc.start = startLoc;
  3880. if (this.options.ranges) node.range[0] = startLoc.index;
  3881. }
  3882. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3883. node.end = endLoc.index;
  3884. node.loc.end = endLoc;
  3885. if (this.options.ranges) node.range[1] = endLoc.index;
  3886. }
  3887. resetStartLocationFromNode(node, locationNode) {
  3888. this.resetStartLocation(node, locationNode.loc.start);
  3889. }
  3890. }
  3891. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3892. const FlowErrors = ParseErrorEnum`flow`({
  3893. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3894. AmbiguousDeclareModuleKind: "Found both `declare module.exports` and `declare export` in the same module. Modules can only have 1 since they are either an ES module or they are a CommonJS module.",
  3895. AssignReservedType: ({
  3896. reservedType
  3897. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3898. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3899. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3900. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3901. EnumBooleanMemberNotInitialized: ({
  3902. memberName,
  3903. enumName
  3904. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3905. EnumDuplicateMemberName: ({
  3906. memberName,
  3907. enumName
  3908. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3909. EnumInconsistentMemberValues: ({
  3910. enumName
  3911. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3912. EnumInvalidExplicitType: ({
  3913. invalidEnumType,
  3914. enumName
  3915. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3916. EnumInvalidExplicitTypeUnknownSupplied: ({
  3917. enumName
  3918. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3919. EnumInvalidMemberInitializerPrimaryType: ({
  3920. enumName,
  3921. memberName,
  3922. explicitType
  3923. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3924. EnumInvalidMemberInitializerSymbolType: ({
  3925. enumName,
  3926. memberName
  3927. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3928. EnumInvalidMemberInitializerUnknownType: ({
  3929. enumName,
  3930. memberName
  3931. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3932. EnumInvalidMemberName: ({
  3933. enumName,
  3934. memberName,
  3935. suggestion
  3936. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3937. EnumNumberMemberNotInitialized: ({
  3938. enumName,
  3939. memberName
  3940. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3941. EnumStringMemberInconsistentlyInitailized: ({
  3942. enumName
  3943. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3944. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3945. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  3946. ImportTypeShorthandOnlyInPureImport: "The `type` and `typeof` keywords on named imports can only be used on regular `import` statements. It cannot be used with `import type` or `import typeof` statements.",
  3947. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3948. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3949. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3950. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3951. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3952. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3953. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3954. PatternIsOptional: Object.assign({
  3955. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3956. }, {
  3957. reasonCode: "OptionalBindingPattern"
  3958. }),
  3959. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3960. SpreadVariance: "Spread properties cannot have variance.",
  3961. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3962. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3963. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3964. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3965. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3966. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3967. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3968. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3969. UnexpectedReservedType: ({
  3970. reservedType
  3971. }) => `Unexpected reserved type ${reservedType}.`,
  3972. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3973. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3974. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3975. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3976. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3977. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3978. UnsupportedDeclareExportKind: ({
  3979. unsupportedExportKind,
  3980. suggestion
  3981. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3982. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3983. UnterminatedFlowComment: "Unterminated flow-comment."
  3984. });
  3985. function isEsModuleType(bodyElement) {
  3986. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3987. }
  3988. function hasTypeImportKind(node) {
  3989. return node.importKind === "type" || node.importKind === "typeof";
  3990. }
  3991. const exportSuggestions = {
  3992. const: "declare export var",
  3993. let: "declare export var",
  3994. type: "export type",
  3995. interface: "export interface"
  3996. };
  3997. function partition(list, test) {
  3998. const list1 = [];
  3999. const list2 = [];
  4000. for (let i = 0; i < list.length; i++) {
  4001. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  4002. }
  4003. return [list1, list2];
  4004. }
  4005. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  4006. var flow = superClass => class FlowParserMixin extends superClass {
  4007. constructor(...args) {
  4008. super(...args);
  4009. this.flowPragma = undefined;
  4010. }
  4011. getScopeHandler() {
  4012. return FlowScopeHandler;
  4013. }
  4014. shouldParseTypes() {
  4015. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  4016. }
  4017. shouldParseEnums() {
  4018. return !!this.getPluginOption("flow", "enums");
  4019. }
  4020. finishToken(type, val) {
  4021. if (type !== 131 && type !== 13 && type !== 28) {
  4022. if (this.flowPragma === undefined) {
  4023. this.flowPragma = null;
  4024. }
  4025. }
  4026. super.finishToken(type, val);
  4027. }
  4028. addComment(comment) {
  4029. if (this.flowPragma === undefined) {
  4030. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  4031. if (!matches) ;else if (matches[1] === "flow") {
  4032. this.flowPragma = "flow";
  4033. } else if (matches[1] === "noflow") {
  4034. this.flowPragma = "noflow";
  4035. } else {
  4036. throw new Error("Unexpected flow pragma");
  4037. }
  4038. }
  4039. super.addComment(comment);
  4040. }
  4041. flowParseTypeInitialiser(tok) {
  4042. const oldInType = this.state.inType;
  4043. this.state.inType = true;
  4044. this.expect(tok || 14);
  4045. const type = this.flowParseType();
  4046. this.state.inType = oldInType;
  4047. return type;
  4048. }
  4049. flowParsePredicate() {
  4050. const node = this.startNode();
  4051. const moduloLoc = this.state.startLoc;
  4052. this.next();
  4053. this.expectContextual(108);
  4054. if (this.state.lastTokStart > moduloLoc.index + 1) {
  4055. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  4056. at: moduloLoc
  4057. });
  4058. }
  4059. if (this.eat(10)) {
  4060. node.value = super.parseExpression();
  4061. this.expect(11);
  4062. return this.finishNode(node, "DeclaredPredicate");
  4063. } else {
  4064. return this.finishNode(node, "InferredPredicate");
  4065. }
  4066. }
  4067. flowParseTypeAndPredicateInitialiser() {
  4068. const oldInType = this.state.inType;
  4069. this.state.inType = true;
  4070. this.expect(14);
  4071. let type = null;
  4072. let predicate = null;
  4073. if (this.match(54)) {
  4074. this.state.inType = oldInType;
  4075. predicate = this.flowParsePredicate();
  4076. } else {
  4077. type = this.flowParseType();
  4078. this.state.inType = oldInType;
  4079. if (this.match(54)) {
  4080. predicate = this.flowParsePredicate();
  4081. }
  4082. }
  4083. return [type, predicate];
  4084. }
  4085. flowParseDeclareClass(node) {
  4086. this.next();
  4087. this.flowParseInterfaceish(node, true);
  4088. return this.finishNode(node, "DeclareClass");
  4089. }
  4090. flowParseDeclareFunction(node) {
  4091. this.next();
  4092. const id = node.id = this.parseIdentifier();
  4093. const typeNode = this.startNode();
  4094. const typeContainer = this.startNode();
  4095. if (this.match(47)) {
  4096. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4097. } else {
  4098. typeNode.typeParameters = null;
  4099. }
  4100. this.expect(10);
  4101. const tmp = this.flowParseFunctionTypeParams();
  4102. typeNode.params = tmp.params;
  4103. typeNode.rest = tmp.rest;
  4104. typeNode.this = tmp._this;
  4105. this.expect(11);
  4106. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4107. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4108. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4109. this.resetEndLocation(id);
  4110. this.semicolon();
  4111. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  4112. return this.finishNode(node, "DeclareFunction");
  4113. }
  4114. flowParseDeclare(node, insideModule) {
  4115. if (this.match(80)) {
  4116. return this.flowParseDeclareClass(node);
  4117. } else if (this.match(68)) {
  4118. return this.flowParseDeclareFunction(node);
  4119. } else if (this.match(74)) {
  4120. return this.flowParseDeclareVariable(node);
  4121. } else if (this.eatContextual(125)) {
  4122. if (this.match(16)) {
  4123. return this.flowParseDeclareModuleExports(node);
  4124. } else {
  4125. if (insideModule) {
  4126. this.raise(FlowErrors.NestedDeclareModule, {
  4127. at: this.state.lastTokStartLoc
  4128. });
  4129. }
  4130. return this.flowParseDeclareModule(node);
  4131. }
  4132. } else if (this.isContextual(128)) {
  4133. return this.flowParseDeclareTypeAlias(node);
  4134. } else if (this.isContextual(129)) {
  4135. return this.flowParseDeclareOpaqueType(node);
  4136. } else if (this.isContextual(127)) {
  4137. return this.flowParseDeclareInterface(node);
  4138. } else if (this.match(82)) {
  4139. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4140. } else {
  4141. this.unexpected();
  4142. }
  4143. }
  4144. flowParseDeclareVariable(node) {
  4145. this.next();
  4146. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4147. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  4148. this.semicolon();
  4149. return this.finishNode(node, "DeclareVariable");
  4150. }
  4151. flowParseDeclareModule(node) {
  4152. this.scope.enter(SCOPE_OTHER);
  4153. if (this.match(131)) {
  4154. node.id = super.parseExprAtom();
  4155. } else {
  4156. node.id = this.parseIdentifier();
  4157. }
  4158. const bodyNode = node.body = this.startNode();
  4159. const body = bodyNode.body = [];
  4160. this.expect(5);
  4161. while (!this.match(8)) {
  4162. let bodyNode = this.startNode();
  4163. if (this.match(83)) {
  4164. this.next();
  4165. if (!this.isContextual(128) && !this.match(87)) {
  4166. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  4167. at: this.state.lastTokStartLoc
  4168. });
  4169. }
  4170. super.parseImport(bodyNode);
  4171. } else {
  4172. this.expectContextual(123, FlowErrors.UnsupportedStatementInDeclareModule);
  4173. bodyNode = this.flowParseDeclare(bodyNode, true);
  4174. }
  4175. body.push(bodyNode);
  4176. }
  4177. this.scope.exit();
  4178. this.expect(8);
  4179. this.finishNode(bodyNode, "BlockStatement");
  4180. let kind = null;
  4181. let hasModuleExport = false;
  4182. body.forEach(bodyElement => {
  4183. if (isEsModuleType(bodyElement)) {
  4184. if (kind === "CommonJS") {
  4185. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4186. at: bodyElement
  4187. });
  4188. }
  4189. kind = "ES";
  4190. } else if (bodyElement.type === "DeclareModuleExports") {
  4191. if (hasModuleExport) {
  4192. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  4193. at: bodyElement
  4194. });
  4195. }
  4196. if (kind === "ES") {
  4197. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4198. at: bodyElement
  4199. });
  4200. }
  4201. kind = "CommonJS";
  4202. hasModuleExport = true;
  4203. }
  4204. });
  4205. node.kind = kind || "CommonJS";
  4206. return this.finishNode(node, "DeclareModule");
  4207. }
  4208. flowParseDeclareExportDeclaration(node, insideModule) {
  4209. this.expect(82);
  4210. if (this.eat(65)) {
  4211. if (this.match(68) || this.match(80)) {
  4212. node.declaration = this.flowParseDeclare(this.startNode());
  4213. } else {
  4214. node.declaration = this.flowParseType();
  4215. this.semicolon();
  4216. }
  4217. node.default = true;
  4218. return this.finishNode(node, "DeclareExportDeclaration");
  4219. } else {
  4220. if (this.match(75) || this.isLet() || (this.isContextual(128) || this.isContextual(127)) && !insideModule) {
  4221. const label = this.state.value;
  4222. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4223. at: this.state.startLoc,
  4224. unsupportedExportKind: label,
  4225. suggestion: exportSuggestions[label]
  4226. });
  4227. }
  4228. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(129)) {
  4229. node.declaration = this.flowParseDeclare(this.startNode());
  4230. node.default = false;
  4231. return this.finishNode(node, "DeclareExportDeclaration");
  4232. } else if (this.match(55) || this.match(5) || this.isContextual(127) || this.isContextual(128) || this.isContextual(129)) {
  4233. node = this.parseExport(node, null);
  4234. if (node.type === "ExportNamedDeclaration") {
  4235. node.type = "ExportDeclaration";
  4236. node.default = false;
  4237. delete node.exportKind;
  4238. }
  4239. node.type = "Declare" + node.type;
  4240. return node;
  4241. }
  4242. }
  4243. this.unexpected();
  4244. }
  4245. flowParseDeclareModuleExports(node) {
  4246. this.next();
  4247. this.expectContextual(109);
  4248. node.typeAnnotation = this.flowParseTypeAnnotation();
  4249. this.semicolon();
  4250. return this.finishNode(node, "DeclareModuleExports");
  4251. }
  4252. flowParseDeclareTypeAlias(node) {
  4253. this.next();
  4254. const finished = this.flowParseTypeAlias(node);
  4255. finished.type = "DeclareTypeAlias";
  4256. return finished;
  4257. }
  4258. flowParseDeclareOpaqueType(node) {
  4259. this.next();
  4260. const finished = this.flowParseOpaqueType(node, true);
  4261. finished.type = "DeclareOpaqueType";
  4262. return finished;
  4263. }
  4264. flowParseDeclareInterface(node) {
  4265. this.next();
  4266. this.flowParseInterfaceish(node, false);
  4267. return this.finishNode(node, "DeclareInterface");
  4268. }
  4269. flowParseInterfaceish(node, isClass) {
  4270. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4271. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4272. if (this.match(47)) {
  4273. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4274. } else {
  4275. node.typeParameters = null;
  4276. }
  4277. node.extends = [];
  4278. if (this.eat(81)) {
  4279. do {
  4280. node.extends.push(this.flowParseInterfaceExtends());
  4281. } while (!isClass && this.eat(12));
  4282. }
  4283. if (isClass) {
  4284. node.implements = [];
  4285. node.mixins = [];
  4286. if (this.eatContextual(115)) {
  4287. do {
  4288. node.mixins.push(this.flowParseInterfaceExtends());
  4289. } while (this.eat(12));
  4290. }
  4291. if (this.eatContextual(111)) {
  4292. do {
  4293. node.implements.push(this.flowParseInterfaceExtends());
  4294. } while (this.eat(12));
  4295. }
  4296. }
  4297. node.body = this.flowParseObjectType({
  4298. allowStatic: isClass,
  4299. allowExact: false,
  4300. allowSpread: false,
  4301. allowProto: isClass,
  4302. allowInexact: false
  4303. });
  4304. }
  4305. flowParseInterfaceExtends() {
  4306. const node = this.startNode();
  4307. node.id = this.flowParseQualifiedTypeIdentifier();
  4308. if (this.match(47)) {
  4309. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4310. } else {
  4311. node.typeParameters = null;
  4312. }
  4313. return this.finishNode(node, "InterfaceExtends");
  4314. }
  4315. flowParseInterface(node) {
  4316. this.flowParseInterfaceish(node, false);
  4317. return this.finishNode(node, "InterfaceDeclaration");
  4318. }
  4319. checkNotUnderscore(word) {
  4320. if (word === "_") {
  4321. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4322. at: this.state.startLoc
  4323. });
  4324. }
  4325. }
  4326. checkReservedType(word, startLoc, declaration) {
  4327. if (!reservedTypes.has(word)) return;
  4328. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4329. at: startLoc,
  4330. reservedType: word
  4331. });
  4332. }
  4333. flowParseRestrictedIdentifier(liberal, declaration) {
  4334. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4335. return this.parseIdentifier(liberal);
  4336. }
  4337. flowParseTypeAlias(node) {
  4338. node.id = this.flowParseRestrictedIdentifier(false, true);
  4339. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4340. if (this.match(47)) {
  4341. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4342. } else {
  4343. node.typeParameters = null;
  4344. }
  4345. node.right = this.flowParseTypeInitialiser(29);
  4346. this.semicolon();
  4347. return this.finishNode(node, "TypeAlias");
  4348. }
  4349. flowParseOpaqueType(node, declare) {
  4350. this.expectContextual(128);
  4351. node.id = this.flowParseRestrictedIdentifier(true, true);
  4352. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4353. if (this.match(47)) {
  4354. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4355. } else {
  4356. node.typeParameters = null;
  4357. }
  4358. node.supertype = null;
  4359. if (this.match(14)) {
  4360. node.supertype = this.flowParseTypeInitialiser(14);
  4361. }
  4362. node.impltype = null;
  4363. if (!declare) {
  4364. node.impltype = this.flowParseTypeInitialiser(29);
  4365. }
  4366. this.semicolon();
  4367. return this.finishNode(node, "OpaqueType");
  4368. }
  4369. flowParseTypeParameter(requireDefault = false) {
  4370. const nodeStartLoc = this.state.startLoc;
  4371. const node = this.startNode();
  4372. const variance = this.flowParseVariance();
  4373. const ident = this.flowParseTypeAnnotatableIdentifier();
  4374. node.name = ident.name;
  4375. node.variance = variance;
  4376. node.bound = ident.typeAnnotation;
  4377. if (this.match(29)) {
  4378. this.eat(29);
  4379. node.default = this.flowParseType();
  4380. } else {
  4381. if (requireDefault) {
  4382. this.raise(FlowErrors.MissingTypeParamDefault, {
  4383. at: nodeStartLoc
  4384. });
  4385. }
  4386. }
  4387. return this.finishNode(node, "TypeParameter");
  4388. }
  4389. flowParseTypeParameterDeclaration() {
  4390. const oldInType = this.state.inType;
  4391. const node = this.startNode();
  4392. node.params = [];
  4393. this.state.inType = true;
  4394. if (this.match(47) || this.match(140)) {
  4395. this.next();
  4396. } else {
  4397. this.unexpected();
  4398. }
  4399. let defaultRequired = false;
  4400. do {
  4401. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4402. node.params.push(typeParameter);
  4403. if (typeParameter.default) {
  4404. defaultRequired = true;
  4405. }
  4406. if (!this.match(48)) {
  4407. this.expect(12);
  4408. }
  4409. } while (!this.match(48));
  4410. this.expect(48);
  4411. this.state.inType = oldInType;
  4412. return this.finishNode(node, "TypeParameterDeclaration");
  4413. }
  4414. flowParseTypeParameterInstantiation() {
  4415. const node = this.startNode();
  4416. const oldInType = this.state.inType;
  4417. node.params = [];
  4418. this.state.inType = true;
  4419. this.expect(47);
  4420. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4421. this.state.noAnonFunctionType = false;
  4422. while (!this.match(48)) {
  4423. node.params.push(this.flowParseType());
  4424. if (!this.match(48)) {
  4425. this.expect(12);
  4426. }
  4427. }
  4428. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4429. this.expect(48);
  4430. this.state.inType = oldInType;
  4431. return this.finishNode(node, "TypeParameterInstantiation");
  4432. }
  4433. flowParseTypeParameterInstantiationCallOrNew() {
  4434. const node = this.startNode();
  4435. const oldInType = this.state.inType;
  4436. node.params = [];
  4437. this.state.inType = true;
  4438. this.expect(47);
  4439. while (!this.match(48)) {
  4440. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4441. if (!this.match(48)) {
  4442. this.expect(12);
  4443. }
  4444. }
  4445. this.expect(48);
  4446. this.state.inType = oldInType;
  4447. return this.finishNode(node, "TypeParameterInstantiation");
  4448. }
  4449. flowParseInterfaceType() {
  4450. const node = this.startNode();
  4451. this.expectContextual(127);
  4452. node.extends = [];
  4453. if (this.eat(81)) {
  4454. do {
  4455. node.extends.push(this.flowParseInterfaceExtends());
  4456. } while (this.eat(12));
  4457. }
  4458. node.body = this.flowParseObjectType({
  4459. allowStatic: false,
  4460. allowExact: false,
  4461. allowSpread: false,
  4462. allowProto: false,
  4463. allowInexact: false
  4464. });
  4465. return this.finishNode(node, "InterfaceTypeAnnotation");
  4466. }
  4467. flowParseObjectPropertyKey() {
  4468. return this.match(132) || this.match(131) ? super.parseExprAtom() : this.parseIdentifier(true);
  4469. }
  4470. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4471. node.static = isStatic;
  4472. if (this.lookahead().type === 14) {
  4473. node.id = this.flowParseObjectPropertyKey();
  4474. node.key = this.flowParseTypeInitialiser();
  4475. } else {
  4476. node.id = null;
  4477. node.key = this.flowParseType();
  4478. }
  4479. this.expect(3);
  4480. node.value = this.flowParseTypeInitialiser();
  4481. node.variance = variance;
  4482. return this.finishNode(node, "ObjectTypeIndexer");
  4483. }
  4484. flowParseObjectTypeInternalSlot(node, isStatic) {
  4485. node.static = isStatic;
  4486. node.id = this.flowParseObjectPropertyKey();
  4487. this.expect(3);
  4488. this.expect(3);
  4489. if (this.match(47) || this.match(10)) {
  4490. node.method = true;
  4491. node.optional = false;
  4492. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4493. } else {
  4494. node.method = false;
  4495. if (this.eat(17)) {
  4496. node.optional = true;
  4497. }
  4498. node.value = this.flowParseTypeInitialiser();
  4499. }
  4500. return this.finishNode(node, "ObjectTypeInternalSlot");
  4501. }
  4502. flowParseObjectTypeMethodish(node) {
  4503. node.params = [];
  4504. node.rest = null;
  4505. node.typeParameters = null;
  4506. node.this = null;
  4507. if (this.match(47)) {
  4508. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4509. }
  4510. this.expect(10);
  4511. if (this.match(78)) {
  4512. node.this = this.flowParseFunctionTypeParam(true);
  4513. node.this.name = null;
  4514. if (!this.match(11)) {
  4515. this.expect(12);
  4516. }
  4517. }
  4518. while (!this.match(11) && !this.match(21)) {
  4519. node.params.push(this.flowParseFunctionTypeParam(false));
  4520. if (!this.match(11)) {
  4521. this.expect(12);
  4522. }
  4523. }
  4524. if (this.eat(21)) {
  4525. node.rest = this.flowParseFunctionTypeParam(false);
  4526. }
  4527. this.expect(11);
  4528. node.returnType = this.flowParseTypeInitialiser();
  4529. return this.finishNode(node, "FunctionTypeAnnotation");
  4530. }
  4531. flowParseObjectTypeCallProperty(node, isStatic) {
  4532. const valueNode = this.startNode();
  4533. node.static = isStatic;
  4534. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4535. return this.finishNode(node, "ObjectTypeCallProperty");
  4536. }
  4537. flowParseObjectType({
  4538. allowStatic,
  4539. allowExact,
  4540. allowSpread,
  4541. allowProto,
  4542. allowInexact
  4543. }) {
  4544. const oldInType = this.state.inType;
  4545. this.state.inType = true;
  4546. const nodeStart = this.startNode();
  4547. nodeStart.callProperties = [];
  4548. nodeStart.properties = [];
  4549. nodeStart.indexers = [];
  4550. nodeStart.internalSlots = [];
  4551. let endDelim;
  4552. let exact;
  4553. let inexact = false;
  4554. if (allowExact && this.match(6)) {
  4555. this.expect(6);
  4556. endDelim = 9;
  4557. exact = true;
  4558. } else {
  4559. this.expect(5);
  4560. endDelim = 8;
  4561. exact = false;
  4562. }
  4563. nodeStart.exact = exact;
  4564. while (!this.match(endDelim)) {
  4565. let isStatic = false;
  4566. let protoStartLoc = null;
  4567. let inexactStartLoc = null;
  4568. const node = this.startNode();
  4569. if (allowProto && this.isContextual(116)) {
  4570. const lookahead = this.lookahead();
  4571. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4572. this.next();
  4573. protoStartLoc = this.state.startLoc;
  4574. allowStatic = false;
  4575. }
  4576. }
  4577. if (allowStatic && this.isContextual(104)) {
  4578. const lookahead = this.lookahead();
  4579. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4580. this.next();
  4581. isStatic = true;
  4582. }
  4583. }
  4584. const variance = this.flowParseVariance();
  4585. if (this.eat(0)) {
  4586. if (protoStartLoc != null) {
  4587. this.unexpected(protoStartLoc);
  4588. }
  4589. if (this.eat(0)) {
  4590. if (variance) {
  4591. this.unexpected(variance.loc.start);
  4592. }
  4593. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4594. } else {
  4595. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4596. }
  4597. } else if (this.match(10) || this.match(47)) {
  4598. if (protoStartLoc != null) {
  4599. this.unexpected(protoStartLoc);
  4600. }
  4601. if (variance) {
  4602. this.unexpected(variance.loc.start);
  4603. }
  4604. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4605. } else {
  4606. let kind = "init";
  4607. if (this.isContextual(98) || this.isContextual(103)) {
  4608. const lookahead = this.lookahead();
  4609. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4610. kind = this.state.value;
  4611. this.next();
  4612. }
  4613. }
  4614. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4615. if (propOrInexact === null) {
  4616. inexact = true;
  4617. inexactStartLoc = this.state.lastTokStartLoc;
  4618. } else {
  4619. nodeStart.properties.push(propOrInexact);
  4620. }
  4621. }
  4622. this.flowObjectTypeSemicolon();
  4623. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4624. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4625. at: inexactStartLoc
  4626. });
  4627. }
  4628. }
  4629. this.expect(endDelim);
  4630. if (allowSpread) {
  4631. nodeStart.inexact = inexact;
  4632. }
  4633. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4634. this.state.inType = oldInType;
  4635. return out;
  4636. }
  4637. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4638. if (this.eat(21)) {
  4639. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4640. if (isInexactToken) {
  4641. if (!allowSpread) {
  4642. this.raise(FlowErrors.InexactInsideNonObject, {
  4643. at: this.state.lastTokStartLoc
  4644. });
  4645. } else if (!allowInexact) {
  4646. this.raise(FlowErrors.InexactInsideExact, {
  4647. at: this.state.lastTokStartLoc
  4648. });
  4649. }
  4650. if (variance) {
  4651. this.raise(FlowErrors.InexactVariance, {
  4652. at: variance
  4653. });
  4654. }
  4655. return null;
  4656. }
  4657. if (!allowSpread) {
  4658. this.raise(FlowErrors.UnexpectedSpreadType, {
  4659. at: this.state.lastTokStartLoc
  4660. });
  4661. }
  4662. if (protoStartLoc != null) {
  4663. this.unexpected(protoStartLoc);
  4664. }
  4665. if (variance) {
  4666. this.raise(FlowErrors.SpreadVariance, {
  4667. at: variance
  4668. });
  4669. }
  4670. node.argument = this.flowParseType();
  4671. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4672. } else {
  4673. node.key = this.flowParseObjectPropertyKey();
  4674. node.static = isStatic;
  4675. node.proto = protoStartLoc != null;
  4676. node.kind = kind;
  4677. let optional = false;
  4678. if (this.match(47) || this.match(10)) {
  4679. node.method = true;
  4680. if (protoStartLoc != null) {
  4681. this.unexpected(protoStartLoc);
  4682. }
  4683. if (variance) {
  4684. this.unexpected(variance.loc.start);
  4685. }
  4686. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4687. if (kind === "get" || kind === "set") {
  4688. this.flowCheckGetterSetterParams(node);
  4689. }
  4690. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4691. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4692. at: node.value.this
  4693. });
  4694. }
  4695. } else {
  4696. if (kind !== "init") this.unexpected();
  4697. node.method = false;
  4698. if (this.eat(17)) {
  4699. optional = true;
  4700. }
  4701. node.value = this.flowParseTypeInitialiser();
  4702. node.variance = variance;
  4703. }
  4704. node.optional = optional;
  4705. return this.finishNode(node, "ObjectTypeProperty");
  4706. }
  4707. }
  4708. flowCheckGetterSetterParams(property) {
  4709. const paramCount = property.kind === "get" ? 0 : 1;
  4710. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4711. if (property.value.this) {
  4712. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4713. at: property.value.this
  4714. });
  4715. }
  4716. if (length !== paramCount) {
  4717. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4718. at: property
  4719. });
  4720. }
  4721. if (property.kind === "set" && property.value.rest) {
  4722. this.raise(Errors.BadSetterRestParameter, {
  4723. at: property
  4724. });
  4725. }
  4726. }
  4727. flowObjectTypeSemicolon() {
  4728. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4729. this.unexpected();
  4730. }
  4731. }
  4732. flowParseQualifiedTypeIdentifier(startLoc, id) {
  4733. var _startLoc;
  4734. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  4735. let node = id || this.flowParseRestrictedIdentifier(true);
  4736. while (this.eat(16)) {
  4737. const node2 = this.startNodeAt(startLoc);
  4738. node2.qualification = node;
  4739. node2.id = this.flowParseRestrictedIdentifier(true);
  4740. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4741. }
  4742. return node;
  4743. }
  4744. flowParseGenericType(startLoc, id) {
  4745. const node = this.startNodeAt(startLoc);
  4746. node.typeParameters = null;
  4747. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  4748. if (this.match(47)) {
  4749. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4750. }
  4751. return this.finishNode(node, "GenericTypeAnnotation");
  4752. }
  4753. flowParseTypeofType() {
  4754. const node = this.startNode();
  4755. this.expect(87);
  4756. node.argument = this.flowParsePrimaryType();
  4757. return this.finishNode(node, "TypeofTypeAnnotation");
  4758. }
  4759. flowParseTupleType() {
  4760. const node = this.startNode();
  4761. node.types = [];
  4762. this.expect(0);
  4763. while (this.state.pos < this.length && !this.match(3)) {
  4764. node.types.push(this.flowParseType());
  4765. if (this.match(3)) break;
  4766. this.expect(12);
  4767. }
  4768. this.expect(3);
  4769. return this.finishNode(node, "TupleTypeAnnotation");
  4770. }
  4771. flowParseFunctionTypeParam(first) {
  4772. let name = null;
  4773. let optional = false;
  4774. let typeAnnotation = null;
  4775. const node = this.startNode();
  4776. const lh = this.lookahead();
  4777. const isThis = this.state.type === 78;
  4778. if (lh.type === 14 || lh.type === 17) {
  4779. if (isThis && !first) {
  4780. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4781. at: node
  4782. });
  4783. }
  4784. name = this.parseIdentifier(isThis);
  4785. if (this.eat(17)) {
  4786. optional = true;
  4787. if (isThis) {
  4788. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4789. at: node
  4790. });
  4791. }
  4792. }
  4793. typeAnnotation = this.flowParseTypeInitialiser();
  4794. } else {
  4795. typeAnnotation = this.flowParseType();
  4796. }
  4797. node.name = name;
  4798. node.optional = optional;
  4799. node.typeAnnotation = typeAnnotation;
  4800. return this.finishNode(node, "FunctionTypeParam");
  4801. }
  4802. reinterpretTypeAsFunctionTypeParam(type) {
  4803. const node = this.startNodeAt(type.loc.start);
  4804. node.name = null;
  4805. node.optional = false;
  4806. node.typeAnnotation = type;
  4807. return this.finishNode(node, "FunctionTypeParam");
  4808. }
  4809. flowParseFunctionTypeParams(params = []) {
  4810. let rest = null;
  4811. let _this = null;
  4812. if (this.match(78)) {
  4813. _this = this.flowParseFunctionTypeParam(true);
  4814. _this.name = null;
  4815. if (!this.match(11)) {
  4816. this.expect(12);
  4817. }
  4818. }
  4819. while (!this.match(11) && !this.match(21)) {
  4820. params.push(this.flowParseFunctionTypeParam(false));
  4821. if (!this.match(11)) {
  4822. this.expect(12);
  4823. }
  4824. }
  4825. if (this.eat(21)) {
  4826. rest = this.flowParseFunctionTypeParam(false);
  4827. }
  4828. return {
  4829. params,
  4830. rest,
  4831. _this
  4832. };
  4833. }
  4834. flowIdentToTypeAnnotation(startLoc, node, id) {
  4835. switch (id.name) {
  4836. case "any":
  4837. return this.finishNode(node, "AnyTypeAnnotation");
  4838. case "bool":
  4839. case "boolean":
  4840. return this.finishNode(node, "BooleanTypeAnnotation");
  4841. case "mixed":
  4842. return this.finishNode(node, "MixedTypeAnnotation");
  4843. case "empty":
  4844. return this.finishNode(node, "EmptyTypeAnnotation");
  4845. case "number":
  4846. return this.finishNode(node, "NumberTypeAnnotation");
  4847. case "string":
  4848. return this.finishNode(node, "StringTypeAnnotation");
  4849. case "symbol":
  4850. return this.finishNode(node, "SymbolTypeAnnotation");
  4851. default:
  4852. this.checkNotUnderscore(id.name);
  4853. return this.flowParseGenericType(startLoc, id);
  4854. }
  4855. }
  4856. flowParsePrimaryType() {
  4857. const startLoc = this.state.startLoc;
  4858. const node = this.startNode();
  4859. let tmp;
  4860. let type;
  4861. let isGroupedType = false;
  4862. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4863. switch (this.state.type) {
  4864. case 5:
  4865. return this.flowParseObjectType({
  4866. allowStatic: false,
  4867. allowExact: false,
  4868. allowSpread: true,
  4869. allowProto: false,
  4870. allowInexact: true
  4871. });
  4872. case 6:
  4873. return this.flowParseObjectType({
  4874. allowStatic: false,
  4875. allowExact: true,
  4876. allowSpread: true,
  4877. allowProto: false,
  4878. allowInexact: false
  4879. });
  4880. case 0:
  4881. this.state.noAnonFunctionType = false;
  4882. type = this.flowParseTupleType();
  4883. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4884. return type;
  4885. case 47:
  4886. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4887. this.expect(10);
  4888. tmp = this.flowParseFunctionTypeParams();
  4889. node.params = tmp.params;
  4890. node.rest = tmp.rest;
  4891. node.this = tmp._this;
  4892. this.expect(11);
  4893. this.expect(19);
  4894. node.returnType = this.flowParseType();
  4895. return this.finishNode(node, "FunctionTypeAnnotation");
  4896. case 10:
  4897. this.next();
  4898. if (!this.match(11) && !this.match(21)) {
  4899. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4900. const token = this.lookahead().type;
  4901. isGroupedType = token !== 17 && token !== 14;
  4902. } else {
  4903. isGroupedType = true;
  4904. }
  4905. }
  4906. if (isGroupedType) {
  4907. this.state.noAnonFunctionType = false;
  4908. type = this.flowParseType();
  4909. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4910. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4911. this.expect(11);
  4912. return type;
  4913. } else {
  4914. this.eat(12);
  4915. }
  4916. }
  4917. if (type) {
  4918. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4919. } else {
  4920. tmp = this.flowParseFunctionTypeParams();
  4921. }
  4922. node.params = tmp.params;
  4923. node.rest = tmp.rest;
  4924. node.this = tmp._this;
  4925. this.expect(11);
  4926. this.expect(19);
  4927. node.returnType = this.flowParseType();
  4928. node.typeParameters = null;
  4929. return this.finishNode(node, "FunctionTypeAnnotation");
  4930. case 131:
  4931. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4932. case 85:
  4933. case 86:
  4934. node.value = this.match(85);
  4935. this.next();
  4936. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4937. case 53:
  4938. if (this.state.value === "-") {
  4939. this.next();
  4940. if (this.match(132)) {
  4941. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4942. }
  4943. if (this.match(133)) {
  4944. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4945. }
  4946. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4947. at: this.state.startLoc
  4948. });
  4949. }
  4950. this.unexpected();
  4951. return;
  4952. case 132:
  4953. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4954. case 133:
  4955. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4956. case 88:
  4957. this.next();
  4958. return this.finishNode(node, "VoidTypeAnnotation");
  4959. case 84:
  4960. this.next();
  4961. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4962. case 78:
  4963. this.next();
  4964. return this.finishNode(node, "ThisTypeAnnotation");
  4965. case 55:
  4966. this.next();
  4967. return this.finishNode(node, "ExistsTypeAnnotation");
  4968. case 87:
  4969. return this.flowParseTypeofType();
  4970. default:
  4971. if (tokenIsKeyword(this.state.type)) {
  4972. const label = tokenLabelName(this.state.type);
  4973. this.next();
  4974. return super.createIdentifier(node, label);
  4975. } else if (tokenIsIdentifier(this.state.type)) {
  4976. if (this.isContextual(127)) {
  4977. return this.flowParseInterfaceType();
  4978. }
  4979. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  4980. }
  4981. }
  4982. this.unexpected();
  4983. }
  4984. flowParsePostfixType() {
  4985. const startLoc = this.state.startLoc;
  4986. let type = this.flowParsePrimaryType();
  4987. let seenOptionalIndexedAccess = false;
  4988. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4989. const node = this.startNodeAt(startLoc);
  4990. const optional = this.eat(18);
  4991. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4992. this.expect(0);
  4993. if (!optional && this.match(3)) {
  4994. node.elementType = type;
  4995. this.next();
  4996. type = this.finishNode(node, "ArrayTypeAnnotation");
  4997. } else {
  4998. node.objectType = type;
  4999. node.indexType = this.flowParseType();
  5000. this.expect(3);
  5001. if (seenOptionalIndexedAccess) {
  5002. node.optional = optional;
  5003. type = this.finishNode(node, "OptionalIndexedAccessType");
  5004. } else {
  5005. type = this.finishNode(node, "IndexedAccessType");
  5006. }
  5007. }
  5008. }
  5009. return type;
  5010. }
  5011. flowParsePrefixType() {
  5012. const node = this.startNode();
  5013. if (this.eat(17)) {
  5014. node.typeAnnotation = this.flowParsePrefixType();
  5015. return this.finishNode(node, "NullableTypeAnnotation");
  5016. } else {
  5017. return this.flowParsePostfixType();
  5018. }
  5019. }
  5020. flowParseAnonFunctionWithoutParens() {
  5021. const param = this.flowParsePrefixType();
  5022. if (!this.state.noAnonFunctionType && this.eat(19)) {
  5023. const node = this.startNodeAt(param.loc.start);
  5024. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  5025. node.rest = null;
  5026. node.this = null;
  5027. node.returnType = this.flowParseType();
  5028. node.typeParameters = null;
  5029. return this.finishNode(node, "FunctionTypeAnnotation");
  5030. }
  5031. return param;
  5032. }
  5033. flowParseIntersectionType() {
  5034. const node = this.startNode();
  5035. this.eat(45);
  5036. const type = this.flowParseAnonFunctionWithoutParens();
  5037. node.types = [type];
  5038. while (this.eat(45)) {
  5039. node.types.push(this.flowParseAnonFunctionWithoutParens());
  5040. }
  5041. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  5042. }
  5043. flowParseUnionType() {
  5044. const node = this.startNode();
  5045. this.eat(43);
  5046. const type = this.flowParseIntersectionType();
  5047. node.types = [type];
  5048. while (this.eat(43)) {
  5049. node.types.push(this.flowParseIntersectionType());
  5050. }
  5051. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  5052. }
  5053. flowParseType() {
  5054. const oldInType = this.state.inType;
  5055. this.state.inType = true;
  5056. const type = this.flowParseUnionType();
  5057. this.state.inType = oldInType;
  5058. return type;
  5059. }
  5060. flowParseTypeOrImplicitInstantiation() {
  5061. if (this.state.type === 130 && this.state.value === "_") {
  5062. const startLoc = this.state.startLoc;
  5063. const node = this.parseIdentifier();
  5064. return this.flowParseGenericType(startLoc, node);
  5065. } else {
  5066. return this.flowParseType();
  5067. }
  5068. }
  5069. flowParseTypeAnnotation() {
  5070. const node = this.startNode();
  5071. node.typeAnnotation = this.flowParseTypeInitialiser();
  5072. return this.finishNode(node, "TypeAnnotation");
  5073. }
  5074. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  5075. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  5076. if (this.match(14)) {
  5077. ident.typeAnnotation = this.flowParseTypeAnnotation();
  5078. this.resetEndLocation(ident);
  5079. }
  5080. return ident;
  5081. }
  5082. typeCastToParameter(node) {
  5083. node.expression.typeAnnotation = node.typeAnnotation;
  5084. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5085. return node.expression;
  5086. }
  5087. flowParseVariance() {
  5088. let variance = null;
  5089. if (this.match(53)) {
  5090. variance = this.startNode();
  5091. if (this.state.value === "+") {
  5092. variance.kind = "plus";
  5093. } else {
  5094. variance.kind = "minus";
  5095. }
  5096. this.next();
  5097. return this.finishNode(variance, "Variance");
  5098. }
  5099. return variance;
  5100. }
  5101. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5102. if (allowExpressionBody) {
  5103. this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5104. return;
  5105. }
  5106. super.parseFunctionBody(node, false, isMethod);
  5107. }
  5108. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5109. if (this.match(14)) {
  5110. const typeNode = this.startNode();
  5111. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5112. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5113. }
  5114. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5115. }
  5116. parseStatementLike(flags) {
  5117. if (this.state.strict && this.isContextual(127)) {
  5118. const lookahead = this.lookahead();
  5119. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5120. const node = this.startNode();
  5121. this.next();
  5122. return this.flowParseInterface(node);
  5123. }
  5124. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5125. const node = this.startNode();
  5126. this.next();
  5127. return this.flowParseEnumDeclaration(node);
  5128. }
  5129. const stmt = super.parseStatementLike(flags);
  5130. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5131. this.flowPragma = null;
  5132. }
  5133. return stmt;
  5134. }
  5135. parseExpressionStatement(node, expr, decorators) {
  5136. if (expr.type === "Identifier") {
  5137. if (expr.name === "declare") {
  5138. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5139. return this.flowParseDeclare(node);
  5140. }
  5141. } else if (tokenIsIdentifier(this.state.type)) {
  5142. if (expr.name === "interface") {
  5143. return this.flowParseInterface(node);
  5144. } else if (expr.name === "type") {
  5145. return this.flowParseTypeAlias(node);
  5146. } else if (expr.name === "opaque") {
  5147. return this.flowParseOpaqueType(node, false);
  5148. }
  5149. }
  5150. }
  5151. return super.parseExpressionStatement(node, expr, decorators);
  5152. }
  5153. shouldParseExportDeclaration() {
  5154. const {
  5155. type
  5156. } = this.state;
  5157. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5158. return !this.state.containsEsc;
  5159. }
  5160. return super.shouldParseExportDeclaration();
  5161. }
  5162. isExportDefaultSpecifier() {
  5163. const {
  5164. type
  5165. } = this.state;
  5166. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5167. return this.state.containsEsc;
  5168. }
  5169. return super.isExportDefaultSpecifier();
  5170. }
  5171. parseExportDefaultExpression() {
  5172. if (this.shouldParseEnums() && this.isContextual(124)) {
  5173. const node = this.startNode();
  5174. this.next();
  5175. return this.flowParseEnumDeclaration(node);
  5176. }
  5177. return super.parseExportDefaultExpression();
  5178. }
  5179. parseConditional(expr, startLoc, refExpressionErrors) {
  5180. if (!this.match(17)) return expr;
  5181. if (this.state.maybeInArrowParameters) {
  5182. const nextCh = this.lookaheadCharCode();
  5183. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5184. this.setOptionalParametersError(refExpressionErrors);
  5185. return expr;
  5186. }
  5187. }
  5188. this.expect(17);
  5189. const state = this.state.clone();
  5190. const originalNoArrowAt = this.state.noArrowAt;
  5191. const node = this.startNodeAt(startLoc);
  5192. let {
  5193. consequent,
  5194. failed
  5195. } = this.tryParseConditionalConsequent();
  5196. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5197. if (failed || invalid.length > 0) {
  5198. const noArrowAt = [...originalNoArrowAt];
  5199. if (invalid.length > 0) {
  5200. this.state = state;
  5201. this.state.noArrowAt = noArrowAt;
  5202. for (let i = 0; i < invalid.length; i++) {
  5203. noArrowAt.push(invalid[i].start);
  5204. }
  5205. ({
  5206. consequent,
  5207. failed
  5208. } = this.tryParseConditionalConsequent());
  5209. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5210. }
  5211. if (failed && valid.length > 1) {
  5212. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5213. at: state.startLoc
  5214. });
  5215. }
  5216. if (failed && valid.length === 1) {
  5217. this.state = state;
  5218. noArrowAt.push(valid[0].start);
  5219. this.state.noArrowAt = noArrowAt;
  5220. ({
  5221. consequent,
  5222. failed
  5223. } = this.tryParseConditionalConsequent());
  5224. }
  5225. }
  5226. this.getArrowLikeExpressions(consequent, true);
  5227. this.state.noArrowAt = originalNoArrowAt;
  5228. this.expect(14);
  5229. node.test = expr;
  5230. node.consequent = consequent;
  5231. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5232. return this.finishNode(node, "ConditionalExpression");
  5233. }
  5234. tryParseConditionalConsequent() {
  5235. this.state.noArrowParamsConversionAt.push(this.state.start);
  5236. const consequent = this.parseMaybeAssignAllowIn();
  5237. const failed = !this.match(14);
  5238. this.state.noArrowParamsConversionAt.pop();
  5239. return {
  5240. consequent,
  5241. failed
  5242. };
  5243. }
  5244. getArrowLikeExpressions(node, disallowInvalid) {
  5245. const stack = [node];
  5246. const arrows = [];
  5247. while (stack.length !== 0) {
  5248. const node = stack.pop();
  5249. if (node.type === "ArrowFunctionExpression") {
  5250. if (node.typeParameters || !node.returnType) {
  5251. this.finishArrowValidation(node);
  5252. } else {
  5253. arrows.push(node);
  5254. }
  5255. stack.push(node.body);
  5256. } else if (node.type === "ConditionalExpression") {
  5257. stack.push(node.consequent);
  5258. stack.push(node.alternate);
  5259. }
  5260. }
  5261. if (disallowInvalid) {
  5262. arrows.forEach(node => this.finishArrowValidation(node));
  5263. return [arrows, []];
  5264. }
  5265. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5266. }
  5267. finishArrowValidation(node) {
  5268. var _node$extra;
  5269. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5270. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5271. super.checkParams(node, false, true);
  5272. this.scope.exit();
  5273. }
  5274. forwardNoArrowParamsConversionAt(node, parse) {
  5275. let result;
  5276. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5277. this.state.noArrowParamsConversionAt.push(this.state.start);
  5278. result = parse();
  5279. this.state.noArrowParamsConversionAt.pop();
  5280. } else {
  5281. result = parse();
  5282. }
  5283. return result;
  5284. }
  5285. parseParenItem(node, startLoc) {
  5286. node = super.parseParenItem(node, startLoc);
  5287. if (this.eat(17)) {
  5288. node.optional = true;
  5289. this.resetEndLocation(node);
  5290. }
  5291. if (this.match(14)) {
  5292. const typeCastNode = this.startNodeAt(startLoc);
  5293. typeCastNode.expression = node;
  5294. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5295. return this.finishNode(typeCastNode, "TypeCastExpression");
  5296. }
  5297. return node;
  5298. }
  5299. assertModuleNodeAllowed(node) {
  5300. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5301. return;
  5302. }
  5303. super.assertModuleNodeAllowed(node);
  5304. }
  5305. parseExportDeclaration(node) {
  5306. if (this.isContextual(128)) {
  5307. node.exportKind = "type";
  5308. const declarationNode = this.startNode();
  5309. this.next();
  5310. if (this.match(5)) {
  5311. node.specifiers = this.parseExportSpecifiers(true);
  5312. super.parseExportFrom(node);
  5313. return null;
  5314. } else {
  5315. return this.flowParseTypeAlias(declarationNode);
  5316. }
  5317. } else if (this.isContextual(129)) {
  5318. node.exportKind = "type";
  5319. const declarationNode = this.startNode();
  5320. this.next();
  5321. return this.flowParseOpaqueType(declarationNode, false);
  5322. } else if (this.isContextual(127)) {
  5323. node.exportKind = "type";
  5324. const declarationNode = this.startNode();
  5325. this.next();
  5326. return this.flowParseInterface(declarationNode);
  5327. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5328. node.exportKind = "value";
  5329. const declarationNode = this.startNode();
  5330. this.next();
  5331. return this.flowParseEnumDeclaration(declarationNode);
  5332. } else {
  5333. return super.parseExportDeclaration(node);
  5334. }
  5335. }
  5336. eatExportStar(node) {
  5337. if (super.eatExportStar(node)) return true;
  5338. if (this.isContextual(128) && this.lookahead().type === 55) {
  5339. node.exportKind = "type";
  5340. this.next();
  5341. this.next();
  5342. return true;
  5343. }
  5344. return false;
  5345. }
  5346. maybeParseExportNamespaceSpecifier(node) {
  5347. const {
  5348. startLoc
  5349. } = this.state;
  5350. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5351. if (hasNamespace && node.exportKind === "type") {
  5352. this.unexpected(startLoc);
  5353. }
  5354. return hasNamespace;
  5355. }
  5356. parseClassId(node, isStatement, optionalId) {
  5357. super.parseClassId(node, isStatement, optionalId);
  5358. if (this.match(47)) {
  5359. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5360. }
  5361. }
  5362. parseClassMember(classBody, member, state) {
  5363. const {
  5364. startLoc
  5365. } = this.state;
  5366. if (this.isContextual(123)) {
  5367. if (super.parseClassMemberFromModifier(classBody, member)) {
  5368. return;
  5369. }
  5370. member.declare = true;
  5371. }
  5372. super.parseClassMember(classBody, member, state);
  5373. if (member.declare) {
  5374. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5375. this.raise(FlowErrors.DeclareClassElement, {
  5376. at: startLoc
  5377. });
  5378. } else if (member.value) {
  5379. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5380. at: member.value
  5381. });
  5382. }
  5383. }
  5384. }
  5385. isIterator(word) {
  5386. return word === "iterator" || word === "asyncIterator";
  5387. }
  5388. readIterator() {
  5389. const word = super.readWord1();
  5390. const fullWord = "@@" + word;
  5391. if (!this.isIterator(word) || !this.state.inType) {
  5392. this.raise(Errors.InvalidIdentifier, {
  5393. at: this.state.curPosition(),
  5394. identifierName: fullWord
  5395. });
  5396. }
  5397. this.finishToken(130, fullWord);
  5398. }
  5399. getTokenFromCode(code) {
  5400. const next = this.input.charCodeAt(this.state.pos + 1);
  5401. if (code === 123 && next === 124) {
  5402. this.finishOp(6, 2);
  5403. } else if (this.state.inType && (code === 62 || code === 60)) {
  5404. this.finishOp(code === 62 ? 48 : 47, 1);
  5405. } else if (this.state.inType && code === 63) {
  5406. if (next === 46) {
  5407. this.finishOp(18, 2);
  5408. } else {
  5409. this.finishOp(17, 1);
  5410. }
  5411. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5412. this.state.pos += 2;
  5413. this.readIterator();
  5414. } else {
  5415. super.getTokenFromCode(code);
  5416. }
  5417. }
  5418. isAssignable(node, isBinding) {
  5419. if (node.type === "TypeCastExpression") {
  5420. return this.isAssignable(node.expression, isBinding);
  5421. } else {
  5422. return super.isAssignable(node, isBinding);
  5423. }
  5424. }
  5425. toAssignable(node, isLHS = false) {
  5426. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5427. node.left = this.typeCastToParameter(node.left);
  5428. }
  5429. super.toAssignable(node, isLHS);
  5430. }
  5431. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5432. for (let i = 0; i < exprList.length; i++) {
  5433. const expr = exprList[i];
  5434. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5435. exprList[i] = this.typeCastToParameter(expr);
  5436. }
  5437. }
  5438. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5439. }
  5440. toReferencedList(exprList, isParenthesizedExpr) {
  5441. for (let i = 0; i < exprList.length; i++) {
  5442. var _expr$extra;
  5443. const expr = exprList[i];
  5444. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5445. this.raise(FlowErrors.TypeCastInPattern, {
  5446. at: expr.typeAnnotation
  5447. });
  5448. }
  5449. }
  5450. return exprList;
  5451. }
  5452. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5453. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5454. if (canBePattern && !this.state.maybeInArrowParameters) {
  5455. this.toReferencedList(node.elements);
  5456. }
  5457. return node;
  5458. }
  5459. isValidLVal(type, isParenthesized, binding) {
  5460. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5461. }
  5462. parseClassProperty(node) {
  5463. if (this.match(14)) {
  5464. node.typeAnnotation = this.flowParseTypeAnnotation();
  5465. }
  5466. return super.parseClassProperty(node);
  5467. }
  5468. parseClassPrivateProperty(node) {
  5469. if (this.match(14)) {
  5470. node.typeAnnotation = this.flowParseTypeAnnotation();
  5471. }
  5472. return super.parseClassPrivateProperty(node);
  5473. }
  5474. isClassMethod() {
  5475. return this.match(47) || super.isClassMethod();
  5476. }
  5477. isClassProperty() {
  5478. return this.match(14) || super.isClassProperty();
  5479. }
  5480. isNonstaticConstructor(method) {
  5481. return !this.match(14) && super.isNonstaticConstructor(method);
  5482. }
  5483. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5484. if (method.variance) {
  5485. this.unexpected(method.variance.loc.start);
  5486. }
  5487. delete method.variance;
  5488. if (this.match(47)) {
  5489. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5490. }
  5491. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5492. if (method.params && isConstructor) {
  5493. const params = method.params;
  5494. if (params.length > 0 && this.isThisParam(params[0])) {
  5495. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5496. at: method
  5497. });
  5498. }
  5499. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5500. const params = method.value.params;
  5501. if (params.length > 0 && this.isThisParam(params[0])) {
  5502. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5503. at: method
  5504. });
  5505. }
  5506. }
  5507. }
  5508. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5509. if (method.variance) {
  5510. this.unexpected(method.variance.loc.start);
  5511. }
  5512. delete method.variance;
  5513. if (this.match(47)) {
  5514. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5515. }
  5516. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5517. }
  5518. parseClassSuper(node) {
  5519. super.parseClassSuper(node);
  5520. if (node.superClass && this.match(47)) {
  5521. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5522. }
  5523. if (this.isContextual(111)) {
  5524. this.next();
  5525. const implemented = node.implements = [];
  5526. do {
  5527. const node = this.startNode();
  5528. node.id = this.flowParseRestrictedIdentifier(true);
  5529. if (this.match(47)) {
  5530. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5531. } else {
  5532. node.typeParameters = null;
  5533. }
  5534. implemented.push(this.finishNode(node, "ClassImplements"));
  5535. } while (this.eat(12));
  5536. }
  5537. }
  5538. checkGetterSetterParams(method) {
  5539. super.checkGetterSetterParams(method);
  5540. const params = this.getObjectOrClassMethodParams(method);
  5541. if (params.length > 0) {
  5542. const param = params[0];
  5543. if (this.isThisParam(param) && method.kind === "get") {
  5544. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5545. at: param
  5546. });
  5547. } else if (this.isThisParam(param)) {
  5548. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5549. at: param
  5550. });
  5551. }
  5552. }
  5553. }
  5554. parsePropertyNamePrefixOperator(node) {
  5555. node.variance = this.flowParseVariance();
  5556. }
  5557. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5558. if (prop.variance) {
  5559. this.unexpected(prop.variance.loc.start);
  5560. }
  5561. delete prop.variance;
  5562. let typeParameters;
  5563. if (this.match(47) && !isAccessor) {
  5564. typeParameters = this.flowParseTypeParameterDeclaration();
  5565. if (!this.match(10)) this.unexpected();
  5566. }
  5567. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5568. if (typeParameters) {
  5569. (result.value || result).typeParameters = typeParameters;
  5570. }
  5571. return result;
  5572. }
  5573. parseAssignableListItemTypes(param) {
  5574. if (this.eat(17)) {
  5575. if (param.type !== "Identifier") {
  5576. this.raise(FlowErrors.PatternIsOptional, {
  5577. at: param
  5578. });
  5579. }
  5580. if (this.isThisParam(param)) {
  5581. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5582. at: param
  5583. });
  5584. }
  5585. param.optional = true;
  5586. }
  5587. if (this.match(14)) {
  5588. param.typeAnnotation = this.flowParseTypeAnnotation();
  5589. } else if (this.isThisParam(param)) {
  5590. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5591. at: param
  5592. });
  5593. }
  5594. if (this.match(29) && this.isThisParam(param)) {
  5595. this.raise(FlowErrors.ThisParamNoDefault, {
  5596. at: param
  5597. });
  5598. }
  5599. this.resetEndLocation(param);
  5600. return param;
  5601. }
  5602. parseMaybeDefault(startLoc, left) {
  5603. const node = super.parseMaybeDefault(startLoc, left);
  5604. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5605. this.raise(FlowErrors.TypeBeforeInitializer, {
  5606. at: node.typeAnnotation
  5607. });
  5608. }
  5609. return node;
  5610. }
  5611. checkImportReflection(node) {
  5612. super.checkImportReflection(node);
  5613. if (node.module && node.importKind !== "value") {
  5614. this.raise(FlowErrors.ImportReflectionHasImportType, {
  5615. at: node.specifiers[0].loc.start
  5616. });
  5617. }
  5618. }
  5619. parseImportSpecifierLocal(node, specifier, type) {
  5620. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5621. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5622. }
  5623. isPotentialImportPhase(isExport) {
  5624. if (super.isPotentialImportPhase(isExport)) return true;
  5625. if (this.isContextual(128)) {
  5626. if (!isExport) return true;
  5627. const ch = this.lookaheadCharCode();
  5628. return ch === 123 || ch === 42;
  5629. }
  5630. return !isExport && this.isContextual(87);
  5631. }
  5632. applyImportPhase(node, isExport, phase, loc) {
  5633. super.applyImportPhase(node, isExport, phase, loc);
  5634. if (isExport) {
  5635. if (!phase && this.match(65)) {
  5636. return;
  5637. }
  5638. node.exportKind = phase === "type" ? phase : "value";
  5639. } else {
  5640. if (phase === "type" && this.match(55)) this.unexpected();
  5641. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  5642. }
  5643. }
  5644. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5645. const firstIdent = specifier.imported;
  5646. let specifierTypeKind = null;
  5647. if (firstIdent.type === "Identifier") {
  5648. if (firstIdent.name === "type") {
  5649. specifierTypeKind = "type";
  5650. } else if (firstIdent.name === "typeof") {
  5651. specifierTypeKind = "typeof";
  5652. }
  5653. }
  5654. let isBinding = false;
  5655. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5656. const as_ident = this.parseIdentifier(true);
  5657. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5658. specifier.imported = as_ident;
  5659. specifier.importKind = specifierTypeKind;
  5660. specifier.local = cloneIdentifier(as_ident);
  5661. } else {
  5662. specifier.imported = firstIdent;
  5663. specifier.importKind = null;
  5664. specifier.local = this.parseIdentifier();
  5665. }
  5666. } else {
  5667. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5668. specifier.imported = this.parseIdentifier(true);
  5669. specifier.importKind = specifierTypeKind;
  5670. } else {
  5671. if (importedIsString) {
  5672. throw this.raise(Errors.ImportBindingIsString, {
  5673. at: specifier,
  5674. importName: firstIdent.value
  5675. });
  5676. }
  5677. specifier.imported = firstIdent;
  5678. specifier.importKind = null;
  5679. }
  5680. if (this.eatContextual(93)) {
  5681. specifier.local = this.parseIdentifier();
  5682. } else {
  5683. isBinding = true;
  5684. specifier.local = cloneIdentifier(specifier.imported);
  5685. }
  5686. }
  5687. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5688. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5689. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5690. at: specifier
  5691. });
  5692. }
  5693. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5694. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5695. }
  5696. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5697. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5698. }
  5699. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5700. }
  5701. parseBindingAtom() {
  5702. switch (this.state.type) {
  5703. case 78:
  5704. return this.parseIdentifier(true);
  5705. default:
  5706. return super.parseBindingAtom();
  5707. }
  5708. }
  5709. parseFunctionParams(node, isConstructor) {
  5710. const kind = node.kind;
  5711. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5712. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5713. }
  5714. super.parseFunctionParams(node, isConstructor);
  5715. }
  5716. parseVarId(decl, kind) {
  5717. super.parseVarId(decl, kind);
  5718. if (this.match(14)) {
  5719. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5720. this.resetEndLocation(decl.id);
  5721. }
  5722. }
  5723. parseAsyncArrowFromCallExpression(node, call) {
  5724. if (this.match(14)) {
  5725. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5726. this.state.noAnonFunctionType = true;
  5727. node.returnType = this.flowParseTypeAnnotation();
  5728. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5729. }
  5730. return super.parseAsyncArrowFromCallExpression(node, call);
  5731. }
  5732. shouldParseAsyncArrow() {
  5733. return this.match(14) || super.shouldParseAsyncArrow();
  5734. }
  5735. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5736. var _jsx;
  5737. let state = null;
  5738. let jsx;
  5739. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  5740. state = this.state.clone();
  5741. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5742. if (!jsx.error) return jsx.node;
  5743. const {
  5744. context
  5745. } = this.state;
  5746. const currentContext = context[context.length - 1];
  5747. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5748. context.pop();
  5749. }
  5750. }
  5751. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5752. var _jsx2, _jsx3;
  5753. state = state || this.state.clone();
  5754. let typeParameters;
  5755. const arrow = this.tryParse(abort => {
  5756. var _arrowExpression$extr;
  5757. typeParameters = this.flowParseTypeParameterDeclaration();
  5758. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5759. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5760. this.resetStartLocationFromNode(result, typeParameters);
  5761. return result;
  5762. });
  5763. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5764. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5765. if (expr.type !== "ArrowFunctionExpression") abort();
  5766. expr.typeParameters = typeParameters;
  5767. this.resetStartLocationFromNode(expr, typeParameters);
  5768. return arrowExpression;
  5769. }, state);
  5770. let arrowExpression = null;
  5771. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5772. if (!arrow.error && !arrow.aborted) {
  5773. if (arrow.node.async) {
  5774. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5775. at: typeParameters
  5776. });
  5777. }
  5778. return arrow.node;
  5779. }
  5780. arrowExpression = arrow.node;
  5781. }
  5782. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5783. this.state = jsx.failState;
  5784. return jsx.node;
  5785. }
  5786. if (arrowExpression) {
  5787. this.state = arrow.failState;
  5788. return arrowExpression;
  5789. }
  5790. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5791. if (arrow.thrown) throw arrow.error;
  5792. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5793. at: typeParameters
  5794. });
  5795. }
  5796. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5797. }
  5798. parseArrow(node) {
  5799. if (this.match(14)) {
  5800. const result = this.tryParse(() => {
  5801. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5802. this.state.noAnonFunctionType = true;
  5803. const typeNode = this.startNode();
  5804. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5805. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5806. if (this.canInsertSemicolon()) this.unexpected();
  5807. if (!this.match(19)) this.unexpected();
  5808. return typeNode;
  5809. });
  5810. if (result.thrown) return null;
  5811. if (result.error) this.state = result.failState;
  5812. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5813. }
  5814. return super.parseArrow(node);
  5815. }
  5816. shouldParseArrow(params) {
  5817. return this.match(14) || super.shouldParseArrow(params);
  5818. }
  5819. setArrowFunctionParameters(node, params) {
  5820. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5821. node.params = params;
  5822. } else {
  5823. super.setArrowFunctionParameters(node, params);
  5824. }
  5825. }
  5826. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5827. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5828. return;
  5829. }
  5830. for (let i = 0; i < node.params.length; i++) {
  5831. if (this.isThisParam(node.params[i]) && i > 0) {
  5832. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5833. at: node.params[i]
  5834. });
  5835. }
  5836. }
  5837. super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5838. }
  5839. parseParenAndDistinguishExpression(canBeArrow) {
  5840. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5841. }
  5842. parseSubscripts(base, startLoc, noCalls) {
  5843. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startLoc.index) !== -1) {
  5844. this.next();
  5845. const node = this.startNodeAt(startLoc);
  5846. node.callee = base;
  5847. node.arguments = super.parseCallExpressionArguments(11, false);
  5848. base = this.finishNode(node, "CallExpression");
  5849. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5850. const state = this.state.clone();
  5851. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  5852. if (!arrow.error && !arrow.aborted) return arrow.node;
  5853. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  5854. if (result.node && !result.error) return result.node;
  5855. if (arrow.node) {
  5856. this.state = arrow.failState;
  5857. return arrow.node;
  5858. }
  5859. if (result.node) {
  5860. this.state = result.failState;
  5861. return result.node;
  5862. }
  5863. throw arrow.error || result.error;
  5864. }
  5865. return super.parseSubscripts(base, startLoc, noCalls);
  5866. }
  5867. parseSubscript(base, startLoc, noCalls, subscriptState) {
  5868. if (this.match(18) && this.isLookaheadToken_lt()) {
  5869. subscriptState.optionalChainMember = true;
  5870. if (noCalls) {
  5871. subscriptState.stop = true;
  5872. return base;
  5873. }
  5874. this.next();
  5875. const node = this.startNodeAt(startLoc);
  5876. node.callee = base;
  5877. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5878. this.expect(10);
  5879. node.arguments = this.parseCallExpressionArguments(11, false);
  5880. node.optional = true;
  5881. return this.finishCallExpression(node, true);
  5882. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5883. const node = this.startNodeAt(startLoc);
  5884. node.callee = base;
  5885. const result = this.tryParse(() => {
  5886. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5887. this.expect(10);
  5888. node.arguments = super.parseCallExpressionArguments(11, false);
  5889. if (subscriptState.optionalChainMember) {
  5890. node.optional = false;
  5891. }
  5892. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5893. });
  5894. if (result.node) {
  5895. if (result.error) this.state = result.failState;
  5896. return result.node;
  5897. }
  5898. }
  5899. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  5900. }
  5901. parseNewCallee(node) {
  5902. super.parseNewCallee(node);
  5903. let targs = null;
  5904. if (this.shouldParseTypes() && this.match(47)) {
  5905. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5906. }
  5907. node.typeArguments = targs;
  5908. }
  5909. parseAsyncArrowWithTypeParameters(startLoc) {
  5910. const node = this.startNodeAt(startLoc);
  5911. this.parseFunctionParams(node, false);
  5912. if (!this.parseArrow(node)) return;
  5913. return super.parseArrowExpression(node, undefined, true);
  5914. }
  5915. readToken_mult_modulo(code) {
  5916. const next = this.input.charCodeAt(this.state.pos + 1);
  5917. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5918. this.state.hasFlowComment = false;
  5919. this.state.pos += 2;
  5920. this.nextToken();
  5921. return;
  5922. }
  5923. super.readToken_mult_modulo(code);
  5924. }
  5925. readToken_pipe_amp(code) {
  5926. const next = this.input.charCodeAt(this.state.pos + 1);
  5927. if (code === 124 && next === 125) {
  5928. this.finishOp(9, 2);
  5929. return;
  5930. }
  5931. super.readToken_pipe_amp(code);
  5932. }
  5933. parseTopLevel(file, program) {
  5934. const fileNode = super.parseTopLevel(file, program);
  5935. if (this.state.hasFlowComment) {
  5936. this.raise(FlowErrors.UnterminatedFlowComment, {
  5937. at: this.state.curPosition()
  5938. });
  5939. }
  5940. return fileNode;
  5941. }
  5942. skipBlockComment() {
  5943. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5944. if (this.state.hasFlowComment) {
  5945. throw this.raise(FlowErrors.NestedFlowComment, {
  5946. at: this.state.startLoc
  5947. });
  5948. }
  5949. this.hasFlowCommentCompletion();
  5950. const commentSkip = this.skipFlowComment();
  5951. if (commentSkip) {
  5952. this.state.pos += commentSkip;
  5953. this.state.hasFlowComment = true;
  5954. }
  5955. return;
  5956. }
  5957. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  5958. }
  5959. skipFlowComment() {
  5960. const {
  5961. pos
  5962. } = this.state;
  5963. let shiftToFirstNonWhiteSpace = 2;
  5964. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5965. shiftToFirstNonWhiteSpace++;
  5966. }
  5967. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5968. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5969. if (ch2 === 58 && ch3 === 58) {
  5970. return shiftToFirstNonWhiteSpace + 2;
  5971. }
  5972. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5973. return shiftToFirstNonWhiteSpace + 12;
  5974. }
  5975. if (ch2 === 58 && ch3 !== 58) {
  5976. return shiftToFirstNonWhiteSpace;
  5977. }
  5978. return false;
  5979. }
  5980. hasFlowCommentCompletion() {
  5981. const end = this.input.indexOf("*/", this.state.pos);
  5982. if (end === -1) {
  5983. throw this.raise(Errors.UnterminatedComment, {
  5984. at: this.state.curPosition()
  5985. });
  5986. }
  5987. }
  5988. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5989. enumName,
  5990. memberName
  5991. }) {
  5992. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5993. at: loc,
  5994. memberName,
  5995. enumName
  5996. });
  5997. }
  5998. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5999. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  6000. at: loc
  6001. }, enumContext));
  6002. }
  6003. flowEnumErrorNumberMemberNotInitialized(loc, {
  6004. enumName,
  6005. memberName
  6006. }) {
  6007. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  6008. at: loc,
  6009. enumName,
  6010. memberName
  6011. });
  6012. }
  6013. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  6014. enumName
  6015. }) {
  6016. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  6017. at: node,
  6018. enumName
  6019. });
  6020. }
  6021. flowEnumMemberInit() {
  6022. const startLoc = this.state.startLoc;
  6023. const endOfInit = () => this.match(12) || this.match(8);
  6024. switch (this.state.type) {
  6025. case 132:
  6026. {
  6027. const literal = this.parseNumericLiteral(this.state.value);
  6028. if (endOfInit()) {
  6029. return {
  6030. type: "number",
  6031. loc: literal.loc.start,
  6032. value: literal
  6033. };
  6034. }
  6035. return {
  6036. type: "invalid",
  6037. loc: startLoc
  6038. };
  6039. }
  6040. case 131:
  6041. {
  6042. const literal = this.parseStringLiteral(this.state.value);
  6043. if (endOfInit()) {
  6044. return {
  6045. type: "string",
  6046. loc: literal.loc.start,
  6047. value: literal
  6048. };
  6049. }
  6050. return {
  6051. type: "invalid",
  6052. loc: startLoc
  6053. };
  6054. }
  6055. case 85:
  6056. case 86:
  6057. {
  6058. const literal = this.parseBooleanLiteral(this.match(85));
  6059. if (endOfInit()) {
  6060. return {
  6061. type: "boolean",
  6062. loc: literal.loc.start,
  6063. value: literal
  6064. };
  6065. }
  6066. return {
  6067. type: "invalid",
  6068. loc: startLoc
  6069. };
  6070. }
  6071. default:
  6072. return {
  6073. type: "invalid",
  6074. loc: startLoc
  6075. };
  6076. }
  6077. }
  6078. flowEnumMemberRaw() {
  6079. const loc = this.state.startLoc;
  6080. const id = this.parseIdentifier(true);
  6081. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  6082. type: "none",
  6083. loc
  6084. };
  6085. return {
  6086. id,
  6087. init
  6088. };
  6089. }
  6090. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  6091. const {
  6092. explicitType
  6093. } = context;
  6094. if (explicitType === null) {
  6095. return;
  6096. }
  6097. if (explicitType !== expectedType) {
  6098. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  6099. }
  6100. }
  6101. flowEnumMembers({
  6102. enumName,
  6103. explicitType
  6104. }) {
  6105. const seenNames = new Set();
  6106. const members = {
  6107. booleanMembers: [],
  6108. numberMembers: [],
  6109. stringMembers: [],
  6110. defaultedMembers: []
  6111. };
  6112. let hasUnknownMembers = false;
  6113. while (!this.match(8)) {
  6114. if (this.eat(21)) {
  6115. hasUnknownMembers = true;
  6116. break;
  6117. }
  6118. const memberNode = this.startNode();
  6119. const {
  6120. id,
  6121. init
  6122. } = this.flowEnumMemberRaw();
  6123. const memberName = id.name;
  6124. if (memberName === "") {
  6125. continue;
  6126. }
  6127. if (/^[a-z]/.test(memberName)) {
  6128. this.raise(FlowErrors.EnumInvalidMemberName, {
  6129. at: id,
  6130. memberName,
  6131. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  6132. enumName
  6133. });
  6134. }
  6135. if (seenNames.has(memberName)) {
  6136. this.raise(FlowErrors.EnumDuplicateMemberName, {
  6137. at: id,
  6138. memberName,
  6139. enumName
  6140. });
  6141. }
  6142. seenNames.add(memberName);
  6143. const context = {
  6144. enumName,
  6145. explicitType,
  6146. memberName
  6147. };
  6148. memberNode.id = id;
  6149. switch (init.type) {
  6150. case "boolean":
  6151. {
  6152. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6153. memberNode.init = init.value;
  6154. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6155. break;
  6156. }
  6157. case "number":
  6158. {
  6159. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6160. memberNode.init = init.value;
  6161. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6162. break;
  6163. }
  6164. case "string":
  6165. {
  6166. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6167. memberNode.init = init.value;
  6168. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6169. break;
  6170. }
  6171. case "invalid":
  6172. {
  6173. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6174. }
  6175. case "none":
  6176. {
  6177. switch (explicitType) {
  6178. case "boolean":
  6179. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6180. break;
  6181. case "number":
  6182. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6183. break;
  6184. default:
  6185. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6186. }
  6187. }
  6188. }
  6189. if (!this.match(8)) {
  6190. this.expect(12);
  6191. }
  6192. }
  6193. return {
  6194. members,
  6195. hasUnknownMembers
  6196. };
  6197. }
  6198. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6199. enumName
  6200. }) {
  6201. if (initializedMembers.length === 0) {
  6202. return defaultedMembers;
  6203. } else if (defaultedMembers.length === 0) {
  6204. return initializedMembers;
  6205. } else if (defaultedMembers.length > initializedMembers.length) {
  6206. for (const member of initializedMembers) {
  6207. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6208. enumName
  6209. });
  6210. }
  6211. return defaultedMembers;
  6212. } else {
  6213. for (const member of defaultedMembers) {
  6214. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6215. enumName
  6216. });
  6217. }
  6218. return initializedMembers;
  6219. }
  6220. }
  6221. flowEnumParseExplicitType({
  6222. enumName
  6223. }) {
  6224. if (!this.eatContextual(101)) return null;
  6225. if (!tokenIsIdentifier(this.state.type)) {
  6226. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6227. at: this.state.startLoc,
  6228. enumName
  6229. });
  6230. }
  6231. const {
  6232. value
  6233. } = this.state;
  6234. this.next();
  6235. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6236. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6237. at: this.state.startLoc,
  6238. enumName,
  6239. invalidEnumType: value
  6240. });
  6241. }
  6242. return value;
  6243. }
  6244. flowEnumBody(node, id) {
  6245. const enumName = id.name;
  6246. const nameLoc = id.loc.start;
  6247. const explicitType = this.flowEnumParseExplicitType({
  6248. enumName
  6249. });
  6250. this.expect(5);
  6251. const {
  6252. members,
  6253. hasUnknownMembers
  6254. } = this.flowEnumMembers({
  6255. enumName,
  6256. explicitType
  6257. });
  6258. node.hasUnknownMembers = hasUnknownMembers;
  6259. switch (explicitType) {
  6260. case "boolean":
  6261. node.explicitType = true;
  6262. node.members = members.booleanMembers;
  6263. this.expect(8);
  6264. return this.finishNode(node, "EnumBooleanBody");
  6265. case "number":
  6266. node.explicitType = true;
  6267. node.members = members.numberMembers;
  6268. this.expect(8);
  6269. return this.finishNode(node, "EnumNumberBody");
  6270. case "string":
  6271. node.explicitType = true;
  6272. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6273. enumName
  6274. });
  6275. this.expect(8);
  6276. return this.finishNode(node, "EnumStringBody");
  6277. case "symbol":
  6278. node.members = members.defaultedMembers;
  6279. this.expect(8);
  6280. return this.finishNode(node, "EnumSymbolBody");
  6281. default:
  6282. {
  6283. const empty = () => {
  6284. node.members = [];
  6285. this.expect(8);
  6286. return this.finishNode(node, "EnumStringBody");
  6287. };
  6288. node.explicitType = false;
  6289. const boolsLen = members.booleanMembers.length;
  6290. const numsLen = members.numberMembers.length;
  6291. const strsLen = members.stringMembers.length;
  6292. const defaultedLen = members.defaultedMembers.length;
  6293. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6294. return empty();
  6295. } else if (!boolsLen && !numsLen) {
  6296. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6297. enumName
  6298. });
  6299. this.expect(8);
  6300. return this.finishNode(node, "EnumStringBody");
  6301. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6302. for (const member of members.defaultedMembers) {
  6303. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6304. enumName,
  6305. memberName: member.id.name
  6306. });
  6307. }
  6308. node.members = members.booleanMembers;
  6309. this.expect(8);
  6310. return this.finishNode(node, "EnumBooleanBody");
  6311. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6312. for (const member of members.defaultedMembers) {
  6313. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6314. enumName,
  6315. memberName: member.id.name
  6316. });
  6317. }
  6318. node.members = members.numberMembers;
  6319. this.expect(8);
  6320. return this.finishNode(node, "EnumNumberBody");
  6321. } else {
  6322. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6323. at: nameLoc,
  6324. enumName
  6325. });
  6326. return empty();
  6327. }
  6328. }
  6329. }
  6330. }
  6331. flowParseEnumDeclaration(node) {
  6332. const id = this.parseIdentifier();
  6333. node.id = id;
  6334. node.body = this.flowEnumBody(this.startNode(), id);
  6335. return this.finishNode(node, "EnumDeclaration");
  6336. }
  6337. isLookaheadToken_lt() {
  6338. const next = this.nextTokenStart();
  6339. if (this.input.charCodeAt(next) === 60) {
  6340. const afterNext = this.input.charCodeAt(next + 1);
  6341. return afterNext !== 60 && afterNext !== 61;
  6342. }
  6343. return false;
  6344. }
  6345. maybeUnwrapTypeCastExpression(node) {
  6346. return node.type === "TypeCastExpression" ? node.expression : node;
  6347. }
  6348. };
  6349. const entities = {
  6350. __proto__: null,
  6351. quot: "\u0022",
  6352. amp: "&",
  6353. apos: "\u0027",
  6354. lt: "<",
  6355. gt: ">",
  6356. nbsp: "\u00A0",
  6357. iexcl: "\u00A1",
  6358. cent: "\u00A2",
  6359. pound: "\u00A3",
  6360. curren: "\u00A4",
  6361. yen: "\u00A5",
  6362. brvbar: "\u00A6",
  6363. sect: "\u00A7",
  6364. uml: "\u00A8",
  6365. copy: "\u00A9",
  6366. ordf: "\u00AA",
  6367. laquo: "\u00AB",
  6368. not: "\u00AC",
  6369. shy: "\u00AD",
  6370. reg: "\u00AE",
  6371. macr: "\u00AF",
  6372. deg: "\u00B0",
  6373. plusmn: "\u00B1",
  6374. sup2: "\u00B2",
  6375. sup3: "\u00B3",
  6376. acute: "\u00B4",
  6377. micro: "\u00B5",
  6378. para: "\u00B6",
  6379. middot: "\u00B7",
  6380. cedil: "\u00B8",
  6381. sup1: "\u00B9",
  6382. ordm: "\u00BA",
  6383. raquo: "\u00BB",
  6384. frac14: "\u00BC",
  6385. frac12: "\u00BD",
  6386. frac34: "\u00BE",
  6387. iquest: "\u00BF",
  6388. Agrave: "\u00C0",
  6389. Aacute: "\u00C1",
  6390. Acirc: "\u00C2",
  6391. Atilde: "\u00C3",
  6392. Auml: "\u00C4",
  6393. Aring: "\u00C5",
  6394. AElig: "\u00C6",
  6395. Ccedil: "\u00C7",
  6396. Egrave: "\u00C8",
  6397. Eacute: "\u00C9",
  6398. Ecirc: "\u00CA",
  6399. Euml: "\u00CB",
  6400. Igrave: "\u00CC",
  6401. Iacute: "\u00CD",
  6402. Icirc: "\u00CE",
  6403. Iuml: "\u00CF",
  6404. ETH: "\u00D0",
  6405. Ntilde: "\u00D1",
  6406. Ograve: "\u00D2",
  6407. Oacute: "\u00D3",
  6408. Ocirc: "\u00D4",
  6409. Otilde: "\u00D5",
  6410. Ouml: "\u00D6",
  6411. times: "\u00D7",
  6412. Oslash: "\u00D8",
  6413. Ugrave: "\u00D9",
  6414. Uacute: "\u00DA",
  6415. Ucirc: "\u00DB",
  6416. Uuml: "\u00DC",
  6417. Yacute: "\u00DD",
  6418. THORN: "\u00DE",
  6419. szlig: "\u00DF",
  6420. agrave: "\u00E0",
  6421. aacute: "\u00E1",
  6422. acirc: "\u00E2",
  6423. atilde: "\u00E3",
  6424. auml: "\u00E4",
  6425. aring: "\u00E5",
  6426. aelig: "\u00E6",
  6427. ccedil: "\u00E7",
  6428. egrave: "\u00E8",
  6429. eacute: "\u00E9",
  6430. ecirc: "\u00EA",
  6431. euml: "\u00EB",
  6432. igrave: "\u00EC",
  6433. iacute: "\u00ED",
  6434. icirc: "\u00EE",
  6435. iuml: "\u00EF",
  6436. eth: "\u00F0",
  6437. ntilde: "\u00F1",
  6438. ograve: "\u00F2",
  6439. oacute: "\u00F3",
  6440. ocirc: "\u00F4",
  6441. otilde: "\u00F5",
  6442. ouml: "\u00F6",
  6443. divide: "\u00F7",
  6444. oslash: "\u00F8",
  6445. ugrave: "\u00F9",
  6446. uacute: "\u00FA",
  6447. ucirc: "\u00FB",
  6448. uuml: "\u00FC",
  6449. yacute: "\u00FD",
  6450. thorn: "\u00FE",
  6451. yuml: "\u00FF",
  6452. OElig: "\u0152",
  6453. oelig: "\u0153",
  6454. Scaron: "\u0160",
  6455. scaron: "\u0161",
  6456. Yuml: "\u0178",
  6457. fnof: "\u0192",
  6458. circ: "\u02C6",
  6459. tilde: "\u02DC",
  6460. Alpha: "\u0391",
  6461. Beta: "\u0392",
  6462. Gamma: "\u0393",
  6463. Delta: "\u0394",
  6464. Epsilon: "\u0395",
  6465. Zeta: "\u0396",
  6466. Eta: "\u0397",
  6467. Theta: "\u0398",
  6468. Iota: "\u0399",
  6469. Kappa: "\u039A",
  6470. Lambda: "\u039B",
  6471. Mu: "\u039C",
  6472. Nu: "\u039D",
  6473. Xi: "\u039E",
  6474. Omicron: "\u039F",
  6475. Pi: "\u03A0",
  6476. Rho: "\u03A1",
  6477. Sigma: "\u03A3",
  6478. Tau: "\u03A4",
  6479. Upsilon: "\u03A5",
  6480. Phi: "\u03A6",
  6481. Chi: "\u03A7",
  6482. Psi: "\u03A8",
  6483. Omega: "\u03A9",
  6484. alpha: "\u03B1",
  6485. beta: "\u03B2",
  6486. gamma: "\u03B3",
  6487. delta: "\u03B4",
  6488. epsilon: "\u03B5",
  6489. zeta: "\u03B6",
  6490. eta: "\u03B7",
  6491. theta: "\u03B8",
  6492. iota: "\u03B9",
  6493. kappa: "\u03BA",
  6494. lambda: "\u03BB",
  6495. mu: "\u03BC",
  6496. nu: "\u03BD",
  6497. xi: "\u03BE",
  6498. omicron: "\u03BF",
  6499. pi: "\u03C0",
  6500. rho: "\u03C1",
  6501. sigmaf: "\u03C2",
  6502. sigma: "\u03C3",
  6503. tau: "\u03C4",
  6504. upsilon: "\u03C5",
  6505. phi: "\u03C6",
  6506. chi: "\u03C7",
  6507. psi: "\u03C8",
  6508. omega: "\u03C9",
  6509. thetasym: "\u03D1",
  6510. upsih: "\u03D2",
  6511. piv: "\u03D6",
  6512. ensp: "\u2002",
  6513. emsp: "\u2003",
  6514. thinsp: "\u2009",
  6515. zwnj: "\u200C",
  6516. zwj: "\u200D",
  6517. lrm: "\u200E",
  6518. rlm: "\u200F",
  6519. ndash: "\u2013",
  6520. mdash: "\u2014",
  6521. lsquo: "\u2018",
  6522. rsquo: "\u2019",
  6523. sbquo: "\u201A",
  6524. ldquo: "\u201C",
  6525. rdquo: "\u201D",
  6526. bdquo: "\u201E",
  6527. dagger: "\u2020",
  6528. Dagger: "\u2021",
  6529. bull: "\u2022",
  6530. hellip: "\u2026",
  6531. permil: "\u2030",
  6532. prime: "\u2032",
  6533. Prime: "\u2033",
  6534. lsaquo: "\u2039",
  6535. rsaquo: "\u203A",
  6536. oline: "\u203E",
  6537. frasl: "\u2044",
  6538. euro: "\u20AC",
  6539. image: "\u2111",
  6540. weierp: "\u2118",
  6541. real: "\u211C",
  6542. trade: "\u2122",
  6543. alefsym: "\u2135",
  6544. larr: "\u2190",
  6545. uarr: "\u2191",
  6546. rarr: "\u2192",
  6547. darr: "\u2193",
  6548. harr: "\u2194",
  6549. crarr: "\u21B5",
  6550. lArr: "\u21D0",
  6551. uArr: "\u21D1",
  6552. rArr: "\u21D2",
  6553. dArr: "\u21D3",
  6554. hArr: "\u21D4",
  6555. forall: "\u2200",
  6556. part: "\u2202",
  6557. exist: "\u2203",
  6558. empty: "\u2205",
  6559. nabla: "\u2207",
  6560. isin: "\u2208",
  6561. notin: "\u2209",
  6562. ni: "\u220B",
  6563. prod: "\u220F",
  6564. sum: "\u2211",
  6565. minus: "\u2212",
  6566. lowast: "\u2217",
  6567. radic: "\u221A",
  6568. prop: "\u221D",
  6569. infin: "\u221E",
  6570. ang: "\u2220",
  6571. and: "\u2227",
  6572. or: "\u2228",
  6573. cap: "\u2229",
  6574. cup: "\u222A",
  6575. int: "\u222B",
  6576. there4: "\u2234",
  6577. sim: "\u223C",
  6578. cong: "\u2245",
  6579. asymp: "\u2248",
  6580. ne: "\u2260",
  6581. equiv: "\u2261",
  6582. le: "\u2264",
  6583. ge: "\u2265",
  6584. sub: "\u2282",
  6585. sup: "\u2283",
  6586. nsub: "\u2284",
  6587. sube: "\u2286",
  6588. supe: "\u2287",
  6589. oplus: "\u2295",
  6590. otimes: "\u2297",
  6591. perp: "\u22A5",
  6592. sdot: "\u22C5",
  6593. lceil: "\u2308",
  6594. rceil: "\u2309",
  6595. lfloor: "\u230A",
  6596. rfloor: "\u230B",
  6597. lang: "\u2329",
  6598. rang: "\u232A",
  6599. loz: "\u25CA",
  6600. spades: "\u2660",
  6601. clubs: "\u2663",
  6602. hearts: "\u2665",
  6603. diams: "\u2666"
  6604. };
  6605. const JsxErrors = ParseErrorEnum`jsx`({
  6606. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6607. MissingClosingTagElement: ({
  6608. openingTagName
  6609. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6610. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6611. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6612. UnexpectedToken: ({
  6613. unexpected,
  6614. HTMLEntity
  6615. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6616. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6617. UnterminatedJsxContent: "Unterminated JSX contents.",
  6618. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6619. });
  6620. function isFragment(object) {
  6621. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6622. }
  6623. function getQualifiedJSXName(object) {
  6624. if (object.type === "JSXIdentifier") {
  6625. return object.name;
  6626. }
  6627. if (object.type === "JSXNamespacedName") {
  6628. return object.namespace.name + ":" + object.name.name;
  6629. }
  6630. if (object.type === "JSXMemberExpression") {
  6631. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6632. }
  6633. throw new Error("Node had unexpected type: " + object.type);
  6634. }
  6635. var jsx = superClass => class JSXParserMixin extends superClass {
  6636. jsxReadToken() {
  6637. let out = "";
  6638. let chunkStart = this.state.pos;
  6639. for (;;) {
  6640. if (this.state.pos >= this.length) {
  6641. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6642. at: this.state.startLoc
  6643. });
  6644. }
  6645. const ch = this.input.charCodeAt(this.state.pos);
  6646. switch (ch) {
  6647. case 60:
  6648. case 123:
  6649. if (this.state.pos === this.state.start) {
  6650. if (ch === 60 && this.state.canStartJSXElement) {
  6651. ++this.state.pos;
  6652. this.finishToken(140);
  6653. } else {
  6654. super.getTokenFromCode(ch);
  6655. }
  6656. return;
  6657. }
  6658. out += this.input.slice(chunkStart, this.state.pos);
  6659. this.finishToken(139, out);
  6660. return;
  6661. case 38:
  6662. out += this.input.slice(chunkStart, this.state.pos);
  6663. out += this.jsxReadEntity();
  6664. chunkStart = this.state.pos;
  6665. break;
  6666. case 62:
  6667. case 125:
  6668. default:
  6669. if (isNewLine(ch)) {
  6670. out += this.input.slice(chunkStart, this.state.pos);
  6671. out += this.jsxReadNewLine(true);
  6672. chunkStart = this.state.pos;
  6673. } else {
  6674. ++this.state.pos;
  6675. }
  6676. }
  6677. }
  6678. }
  6679. jsxReadNewLine(normalizeCRLF) {
  6680. const ch = this.input.charCodeAt(this.state.pos);
  6681. let out;
  6682. ++this.state.pos;
  6683. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6684. ++this.state.pos;
  6685. out = normalizeCRLF ? "\n" : "\r\n";
  6686. } else {
  6687. out = String.fromCharCode(ch);
  6688. }
  6689. ++this.state.curLine;
  6690. this.state.lineStart = this.state.pos;
  6691. return out;
  6692. }
  6693. jsxReadString(quote) {
  6694. let out = "";
  6695. let chunkStart = ++this.state.pos;
  6696. for (;;) {
  6697. if (this.state.pos >= this.length) {
  6698. throw this.raise(Errors.UnterminatedString, {
  6699. at: this.state.startLoc
  6700. });
  6701. }
  6702. const ch = this.input.charCodeAt(this.state.pos);
  6703. if (ch === quote) break;
  6704. if (ch === 38) {
  6705. out += this.input.slice(chunkStart, this.state.pos);
  6706. out += this.jsxReadEntity();
  6707. chunkStart = this.state.pos;
  6708. } else if (isNewLine(ch)) {
  6709. out += this.input.slice(chunkStart, this.state.pos);
  6710. out += this.jsxReadNewLine(false);
  6711. chunkStart = this.state.pos;
  6712. } else {
  6713. ++this.state.pos;
  6714. }
  6715. }
  6716. out += this.input.slice(chunkStart, this.state.pos++);
  6717. this.finishToken(131, out);
  6718. }
  6719. jsxReadEntity() {
  6720. const startPos = ++this.state.pos;
  6721. if (this.codePointAtPos(this.state.pos) === 35) {
  6722. ++this.state.pos;
  6723. let radix = 10;
  6724. if (this.codePointAtPos(this.state.pos) === 120) {
  6725. radix = 16;
  6726. ++this.state.pos;
  6727. }
  6728. const codePoint = this.readInt(radix, undefined, false, "bail");
  6729. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6730. ++this.state.pos;
  6731. return String.fromCodePoint(codePoint);
  6732. }
  6733. } else {
  6734. let count = 0;
  6735. let semi = false;
  6736. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6737. ++this.state.pos;
  6738. }
  6739. if (semi) {
  6740. const desc = this.input.slice(startPos, this.state.pos);
  6741. const entity = entities[desc];
  6742. ++this.state.pos;
  6743. if (entity) {
  6744. return entity;
  6745. }
  6746. }
  6747. }
  6748. this.state.pos = startPos;
  6749. return "&";
  6750. }
  6751. jsxReadWord() {
  6752. let ch;
  6753. const start = this.state.pos;
  6754. do {
  6755. ch = this.input.charCodeAt(++this.state.pos);
  6756. } while (isIdentifierChar(ch) || ch === 45);
  6757. this.finishToken(138, this.input.slice(start, this.state.pos));
  6758. }
  6759. jsxParseIdentifier() {
  6760. const node = this.startNode();
  6761. if (this.match(138)) {
  6762. node.name = this.state.value;
  6763. } else if (tokenIsKeyword(this.state.type)) {
  6764. node.name = tokenLabelName(this.state.type);
  6765. } else {
  6766. this.unexpected();
  6767. }
  6768. this.next();
  6769. return this.finishNode(node, "JSXIdentifier");
  6770. }
  6771. jsxParseNamespacedName() {
  6772. const startLoc = this.state.startLoc;
  6773. const name = this.jsxParseIdentifier();
  6774. if (!this.eat(14)) return name;
  6775. const node = this.startNodeAt(startLoc);
  6776. node.namespace = name;
  6777. node.name = this.jsxParseIdentifier();
  6778. return this.finishNode(node, "JSXNamespacedName");
  6779. }
  6780. jsxParseElementName() {
  6781. const startLoc = this.state.startLoc;
  6782. let node = this.jsxParseNamespacedName();
  6783. if (node.type === "JSXNamespacedName") {
  6784. return node;
  6785. }
  6786. while (this.eat(16)) {
  6787. const newNode = this.startNodeAt(startLoc);
  6788. newNode.object = node;
  6789. newNode.property = this.jsxParseIdentifier();
  6790. node = this.finishNode(newNode, "JSXMemberExpression");
  6791. }
  6792. return node;
  6793. }
  6794. jsxParseAttributeValue() {
  6795. let node;
  6796. switch (this.state.type) {
  6797. case 5:
  6798. node = this.startNode();
  6799. this.setContext(types.brace);
  6800. this.next();
  6801. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6802. if (node.expression.type === "JSXEmptyExpression") {
  6803. this.raise(JsxErrors.AttributeIsEmpty, {
  6804. at: node
  6805. });
  6806. }
  6807. return node;
  6808. case 140:
  6809. case 131:
  6810. return this.parseExprAtom();
  6811. default:
  6812. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6813. at: this.state.startLoc
  6814. });
  6815. }
  6816. }
  6817. jsxParseEmptyExpression() {
  6818. const node = this.startNodeAt(this.state.lastTokEndLoc);
  6819. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6820. }
  6821. jsxParseSpreadChild(node) {
  6822. this.next();
  6823. node.expression = this.parseExpression();
  6824. this.setContext(types.j_expr);
  6825. this.state.canStartJSXElement = true;
  6826. this.expect(8);
  6827. return this.finishNode(node, "JSXSpreadChild");
  6828. }
  6829. jsxParseExpressionContainer(node, previousContext) {
  6830. if (this.match(8)) {
  6831. node.expression = this.jsxParseEmptyExpression();
  6832. } else {
  6833. const expression = this.parseExpression();
  6834. node.expression = expression;
  6835. }
  6836. this.setContext(previousContext);
  6837. this.state.canStartJSXElement = true;
  6838. this.expect(8);
  6839. return this.finishNode(node, "JSXExpressionContainer");
  6840. }
  6841. jsxParseAttribute() {
  6842. const node = this.startNode();
  6843. if (this.match(5)) {
  6844. this.setContext(types.brace);
  6845. this.next();
  6846. this.expect(21);
  6847. node.argument = this.parseMaybeAssignAllowIn();
  6848. this.setContext(types.j_oTag);
  6849. this.state.canStartJSXElement = true;
  6850. this.expect(8);
  6851. return this.finishNode(node, "JSXSpreadAttribute");
  6852. }
  6853. node.name = this.jsxParseNamespacedName();
  6854. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6855. return this.finishNode(node, "JSXAttribute");
  6856. }
  6857. jsxParseOpeningElementAt(startLoc) {
  6858. const node = this.startNodeAt(startLoc);
  6859. if (this.eat(141)) {
  6860. return this.finishNode(node, "JSXOpeningFragment");
  6861. }
  6862. node.name = this.jsxParseElementName();
  6863. return this.jsxParseOpeningElementAfterName(node);
  6864. }
  6865. jsxParseOpeningElementAfterName(node) {
  6866. const attributes = [];
  6867. while (!this.match(56) && !this.match(141)) {
  6868. attributes.push(this.jsxParseAttribute());
  6869. }
  6870. node.attributes = attributes;
  6871. node.selfClosing = this.eat(56);
  6872. this.expect(141);
  6873. return this.finishNode(node, "JSXOpeningElement");
  6874. }
  6875. jsxParseClosingElementAt(startLoc) {
  6876. const node = this.startNodeAt(startLoc);
  6877. if (this.eat(141)) {
  6878. return this.finishNode(node, "JSXClosingFragment");
  6879. }
  6880. node.name = this.jsxParseElementName();
  6881. this.expect(141);
  6882. return this.finishNode(node, "JSXClosingElement");
  6883. }
  6884. jsxParseElementAt(startLoc) {
  6885. const node = this.startNodeAt(startLoc);
  6886. const children = [];
  6887. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  6888. let closingElement = null;
  6889. if (!openingElement.selfClosing) {
  6890. contents: for (;;) {
  6891. switch (this.state.type) {
  6892. case 140:
  6893. startLoc = this.state.startLoc;
  6894. this.next();
  6895. if (this.eat(56)) {
  6896. closingElement = this.jsxParseClosingElementAt(startLoc);
  6897. break contents;
  6898. }
  6899. children.push(this.jsxParseElementAt(startLoc));
  6900. break;
  6901. case 139:
  6902. children.push(this.parseExprAtom());
  6903. break;
  6904. case 5:
  6905. {
  6906. const node = this.startNode();
  6907. this.setContext(types.brace);
  6908. this.next();
  6909. if (this.match(21)) {
  6910. children.push(this.jsxParseSpreadChild(node));
  6911. } else {
  6912. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6913. }
  6914. break;
  6915. }
  6916. default:
  6917. this.unexpected();
  6918. }
  6919. }
  6920. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6921. this.raise(JsxErrors.MissingClosingTagFragment, {
  6922. at: closingElement
  6923. });
  6924. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6925. this.raise(JsxErrors.MissingClosingTagElement, {
  6926. at: closingElement,
  6927. openingTagName: getQualifiedJSXName(openingElement.name)
  6928. });
  6929. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6930. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6931. this.raise(JsxErrors.MissingClosingTagElement, {
  6932. at: closingElement,
  6933. openingTagName: getQualifiedJSXName(openingElement.name)
  6934. });
  6935. }
  6936. }
  6937. }
  6938. if (isFragment(openingElement)) {
  6939. node.openingFragment = openingElement;
  6940. node.closingFragment = closingElement;
  6941. } else {
  6942. node.openingElement = openingElement;
  6943. node.closingElement = closingElement;
  6944. }
  6945. node.children = children;
  6946. if (this.match(47)) {
  6947. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6948. at: this.state.startLoc
  6949. });
  6950. }
  6951. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6952. }
  6953. jsxParseElement() {
  6954. const startLoc = this.state.startLoc;
  6955. this.next();
  6956. return this.jsxParseElementAt(startLoc);
  6957. }
  6958. setContext(newContext) {
  6959. const {
  6960. context
  6961. } = this.state;
  6962. context[context.length - 1] = newContext;
  6963. }
  6964. parseExprAtom(refExpressionErrors) {
  6965. if (this.match(139)) {
  6966. return this.parseLiteral(this.state.value, "JSXText");
  6967. } else if (this.match(140)) {
  6968. return this.jsxParseElement();
  6969. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6970. this.replaceToken(140);
  6971. return this.jsxParseElement();
  6972. } else {
  6973. return super.parseExprAtom(refExpressionErrors);
  6974. }
  6975. }
  6976. skipSpace() {
  6977. const curContext = this.curContext();
  6978. if (!curContext.preserveSpace) super.skipSpace();
  6979. }
  6980. getTokenFromCode(code) {
  6981. const context = this.curContext();
  6982. if (context === types.j_expr) {
  6983. this.jsxReadToken();
  6984. return;
  6985. }
  6986. if (context === types.j_oTag || context === types.j_cTag) {
  6987. if (isIdentifierStart(code)) {
  6988. this.jsxReadWord();
  6989. return;
  6990. }
  6991. if (code === 62) {
  6992. ++this.state.pos;
  6993. this.finishToken(141);
  6994. return;
  6995. }
  6996. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6997. this.jsxReadString(code);
  6998. return;
  6999. }
  7000. }
  7001. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  7002. ++this.state.pos;
  7003. this.finishToken(140);
  7004. return;
  7005. }
  7006. super.getTokenFromCode(code);
  7007. }
  7008. updateContext(prevType) {
  7009. const {
  7010. context,
  7011. type
  7012. } = this.state;
  7013. if (type === 56 && prevType === 140) {
  7014. context.splice(-2, 2, types.j_cTag);
  7015. this.state.canStartJSXElement = false;
  7016. } else if (type === 140) {
  7017. context.push(types.j_oTag);
  7018. } else if (type === 141) {
  7019. const out = context[context.length - 1];
  7020. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  7021. context.pop();
  7022. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  7023. } else {
  7024. this.setContext(types.j_expr);
  7025. this.state.canStartJSXElement = true;
  7026. }
  7027. } else {
  7028. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  7029. }
  7030. }
  7031. };
  7032. class TypeScriptScope extends Scope {
  7033. constructor(...args) {
  7034. super(...args);
  7035. this.types = new Set();
  7036. this.enums = new Set();
  7037. this.constEnums = new Set();
  7038. this.classes = new Set();
  7039. this.exportOnlyBindings = new Set();
  7040. }
  7041. }
  7042. class TypeScriptScopeHandler extends ScopeHandler {
  7043. constructor(...args) {
  7044. super(...args);
  7045. this.importsStack = [];
  7046. }
  7047. createScope(flags) {
  7048. this.importsStack.push(new Set());
  7049. return new TypeScriptScope(flags);
  7050. }
  7051. enter(flags) {
  7052. if (flags == SCOPE_TS_MODULE) {
  7053. this.importsStack.push(new Set());
  7054. }
  7055. super.enter(flags);
  7056. }
  7057. exit() {
  7058. const flags = super.exit();
  7059. if (flags == SCOPE_TS_MODULE) {
  7060. this.importsStack.pop();
  7061. }
  7062. return flags;
  7063. }
  7064. hasImport(name, allowShadow) {
  7065. const len = this.importsStack.length;
  7066. if (this.importsStack[len - 1].has(name)) {
  7067. return true;
  7068. }
  7069. if (!allowShadow && len > 1) {
  7070. for (let i = 0; i < len - 1; i++) {
  7071. if (this.importsStack[i].has(name)) return true;
  7072. }
  7073. }
  7074. return false;
  7075. }
  7076. declareName(name, bindingType, loc) {
  7077. if (bindingType & BIND_FLAGS_TS_IMPORT) {
  7078. if (this.hasImport(name, true)) {
  7079. this.parser.raise(Errors.VarRedeclaration, {
  7080. at: loc,
  7081. identifierName: name
  7082. });
  7083. }
  7084. this.importsStack[this.importsStack.length - 1].add(name);
  7085. return;
  7086. }
  7087. const scope = this.currentScope();
  7088. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  7089. this.maybeExportDefined(scope, name);
  7090. scope.exportOnlyBindings.add(name);
  7091. return;
  7092. }
  7093. super.declareName(name, bindingType, loc);
  7094. if (bindingType & BIND_KIND_TYPE) {
  7095. if (!(bindingType & BIND_KIND_VALUE)) {
  7096. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  7097. this.maybeExportDefined(scope, name);
  7098. }
  7099. scope.types.add(name);
  7100. }
  7101. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  7102. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  7103. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  7104. }
  7105. isRedeclaredInScope(scope, name, bindingType) {
  7106. if (scope.enums.has(name)) {
  7107. if (bindingType & BIND_FLAGS_TS_ENUM) {
  7108. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  7109. const wasConst = scope.constEnums.has(name);
  7110. return isConst !== wasConst;
  7111. }
  7112. return true;
  7113. }
  7114. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  7115. if (scope.lexical.has(name)) {
  7116. return !!(bindingType & BIND_KIND_VALUE);
  7117. } else {
  7118. return false;
  7119. }
  7120. }
  7121. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  7122. return true;
  7123. }
  7124. return super.isRedeclaredInScope(scope, name, bindingType);
  7125. }
  7126. checkLocalExport(id) {
  7127. const {
  7128. name
  7129. } = id;
  7130. if (this.hasImport(name)) return;
  7131. const len = this.scopeStack.length;
  7132. for (let i = len - 1; i >= 0; i--) {
  7133. const scope = this.scopeStack[i];
  7134. if (scope.types.has(name) || scope.exportOnlyBindings.has(name)) return;
  7135. }
  7136. super.checkLocalExport(id);
  7137. }
  7138. }
  7139. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7140. const unwrapParenthesizedExpression = node => {
  7141. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  7142. };
  7143. class LValParser extends NodeUtils {
  7144. toAssignable(node, isLHS = false) {
  7145. var _node$extra, _node$extra3;
  7146. let parenthesized = undefined;
  7147. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  7148. parenthesized = unwrapParenthesizedExpression(node);
  7149. if (isLHS) {
  7150. if (parenthesized.type === "Identifier") {
  7151. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, {
  7152. at: node
  7153. });
  7154. } else if (parenthesized.type !== "MemberExpression") {
  7155. this.raise(Errors.InvalidParenthesizedAssignment, {
  7156. at: node
  7157. });
  7158. }
  7159. } else {
  7160. this.raise(Errors.InvalidParenthesizedAssignment, {
  7161. at: node
  7162. });
  7163. }
  7164. }
  7165. switch (node.type) {
  7166. case "Identifier":
  7167. case "ObjectPattern":
  7168. case "ArrayPattern":
  7169. case "AssignmentPattern":
  7170. case "RestElement":
  7171. break;
  7172. case "ObjectExpression":
  7173. node.type = "ObjectPattern";
  7174. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  7175. var _node$extra2;
  7176. const prop = node.properties[i];
  7177. const isLast = i === last;
  7178. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  7179. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  7180. this.raise(Errors.RestTrailingComma, {
  7181. at: node.extra.trailingCommaLoc
  7182. });
  7183. }
  7184. }
  7185. break;
  7186. case "ObjectProperty":
  7187. {
  7188. const {
  7189. key,
  7190. value
  7191. } = node;
  7192. if (this.isPrivateName(key)) {
  7193. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7194. }
  7195. this.toAssignable(value, isLHS);
  7196. break;
  7197. }
  7198. case "SpreadElement":
  7199. {
  7200. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7201. }
  7202. case "ArrayExpression":
  7203. node.type = "ArrayPattern";
  7204. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7205. break;
  7206. case "AssignmentExpression":
  7207. if (node.operator !== "=") {
  7208. this.raise(Errors.MissingEqInAssignment, {
  7209. at: node.left.loc.end
  7210. });
  7211. }
  7212. node.type = "AssignmentPattern";
  7213. delete node.operator;
  7214. this.toAssignable(node.left, isLHS);
  7215. break;
  7216. case "ParenthesizedExpression":
  7217. this.toAssignable(parenthesized, isLHS);
  7218. break;
  7219. }
  7220. }
  7221. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7222. if (prop.type === "ObjectMethod") {
  7223. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  7224. at: prop.key
  7225. });
  7226. } else if (prop.type === "SpreadElement") {
  7227. prop.type = "RestElement";
  7228. const arg = prop.argument;
  7229. this.checkToRestConversion(arg, false);
  7230. this.toAssignable(arg, isLHS);
  7231. if (!isLast) {
  7232. this.raise(Errors.RestTrailingComma, {
  7233. at: prop
  7234. });
  7235. }
  7236. } else {
  7237. this.toAssignable(prop, isLHS);
  7238. }
  7239. }
  7240. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7241. const end = exprList.length - 1;
  7242. for (let i = 0; i <= end; i++) {
  7243. const elt = exprList[i];
  7244. if (!elt) continue;
  7245. if (elt.type === "SpreadElement") {
  7246. elt.type = "RestElement";
  7247. const arg = elt.argument;
  7248. this.checkToRestConversion(arg, true);
  7249. this.toAssignable(arg, isLHS);
  7250. } else {
  7251. this.toAssignable(elt, isLHS);
  7252. }
  7253. if (elt.type === "RestElement") {
  7254. if (i < end) {
  7255. this.raise(Errors.RestTrailingComma, {
  7256. at: elt
  7257. });
  7258. } else if (trailingCommaLoc) {
  7259. this.raise(Errors.RestTrailingComma, {
  7260. at: trailingCommaLoc
  7261. });
  7262. }
  7263. }
  7264. }
  7265. }
  7266. isAssignable(node, isBinding) {
  7267. switch (node.type) {
  7268. case "Identifier":
  7269. case "ObjectPattern":
  7270. case "ArrayPattern":
  7271. case "AssignmentPattern":
  7272. case "RestElement":
  7273. return true;
  7274. case "ObjectExpression":
  7275. {
  7276. const last = node.properties.length - 1;
  7277. return node.properties.every((prop, i) => {
  7278. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7279. });
  7280. }
  7281. case "ObjectProperty":
  7282. return this.isAssignable(node.value);
  7283. case "SpreadElement":
  7284. return this.isAssignable(node.argument);
  7285. case "ArrayExpression":
  7286. return node.elements.every(element => element === null || this.isAssignable(element));
  7287. case "AssignmentExpression":
  7288. return node.operator === "=";
  7289. case "ParenthesizedExpression":
  7290. return this.isAssignable(node.expression);
  7291. case "MemberExpression":
  7292. case "OptionalMemberExpression":
  7293. return !isBinding;
  7294. default:
  7295. return false;
  7296. }
  7297. }
  7298. toReferencedList(exprList, isParenthesizedExpr) {
  7299. return exprList;
  7300. }
  7301. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7302. this.toReferencedList(exprList, isParenthesizedExpr);
  7303. for (const expr of exprList) {
  7304. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7305. this.toReferencedListDeep(expr.elements);
  7306. }
  7307. }
  7308. }
  7309. parseSpread(refExpressionErrors) {
  7310. const node = this.startNode();
  7311. this.next();
  7312. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7313. return this.finishNode(node, "SpreadElement");
  7314. }
  7315. parseRestBinding() {
  7316. const node = this.startNode();
  7317. this.next();
  7318. node.argument = this.parseBindingAtom();
  7319. return this.finishNode(node, "RestElement");
  7320. }
  7321. parseBindingAtom() {
  7322. switch (this.state.type) {
  7323. case 0:
  7324. {
  7325. const node = this.startNode();
  7326. this.next();
  7327. node.elements = this.parseBindingList(3, 93, 1);
  7328. return this.finishNode(node, "ArrayPattern");
  7329. }
  7330. case 5:
  7331. return this.parseObjectLike(8, true);
  7332. }
  7333. return this.parseIdentifier();
  7334. }
  7335. parseBindingList(close, closeCharCode, flags) {
  7336. const allowEmpty = flags & 1;
  7337. const elts = [];
  7338. let first = true;
  7339. while (!this.eat(close)) {
  7340. if (first) {
  7341. first = false;
  7342. } else {
  7343. this.expect(12);
  7344. }
  7345. if (allowEmpty && this.match(12)) {
  7346. elts.push(null);
  7347. } else if (this.eat(close)) {
  7348. break;
  7349. } else if (this.match(21)) {
  7350. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding(), flags));
  7351. if (!this.checkCommaAfterRest(closeCharCode)) {
  7352. this.expect(close);
  7353. break;
  7354. }
  7355. } else {
  7356. const decorators = [];
  7357. if (this.match(26) && this.hasPlugin("decorators")) {
  7358. this.raise(Errors.UnsupportedParameterDecorator, {
  7359. at: this.state.startLoc
  7360. });
  7361. }
  7362. while (this.match(26)) {
  7363. decorators.push(this.parseDecorator());
  7364. }
  7365. elts.push(this.parseAssignableListItem(flags, decorators));
  7366. }
  7367. }
  7368. return elts;
  7369. }
  7370. parseBindingRestProperty(prop) {
  7371. this.next();
  7372. prop.argument = this.parseIdentifier();
  7373. this.checkCommaAfterRest(125);
  7374. return this.finishNode(prop, "RestElement");
  7375. }
  7376. parseBindingProperty() {
  7377. const prop = this.startNode();
  7378. const {
  7379. type,
  7380. startLoc
  7381. } = this.state;
  7382. if (type === 21) {
  7383. return this.parseBindingRestProperty(prop);
  7384. } else if (type === 136) {
  7385. this.expectPlugin("destructuringPrivate", startLoc);
  7386. this.classScope.usePrivateName(this.state.value, startLoc);
  7387. prop.key = this.parsePrivateName();
  7388. } else {
  7389. this.parsePropertyName(prop);
  7390. }
  7391. prop.method = false;
  7392. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7393. }
  7394. parseAssignableListItem(flags, decorators) {
  7395. const left = this.parseMaybeDefault();
  7396. this.parseAssignableListItemTypes(left, flags);
  7397. const elt = this.parseMaybeDefault(left.loc.start, left);
  7398. if (decorators.length) {
  7399. left.decorators = decorators;
  7400. }
  7401. return elt;
  7402. }
  7403. parseAssignableListItemTypes(param, flags) {
  7404. return param;
  7405. }
  7406. parseMaybeDefault(startLoc, left) {
  7407. var _startLoc, _left;
  7408. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  7409. left = (_left = left) != null ? _left : this.parseBindingAtom();
  7410. if (!this.eat(29)) return left;
  7411. const node = this.startNodeAt(startLoc);
  7412. node.left = left;
  7413. node.right = this.parseMaybeAssignAllowIn();
  7414. return this.finishNode(node, "AssignmentPattern");
  7415. }
  7416. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  7417. return getOwn$1({
  7418. AssignmentPattern: "left",
  7419. RestElement: "argument",
  7420. ObjectProperty: "value",
  7421. ParenthesizedExpression: "expression",
  7422. ArrayPattern: "elements",
  7423. ObjectPattern: "properties"
  7424. }, type);
  7425. }
  7426. checkLVal(expression, {
  7427. in: ancestor,
  7428. binding = BIND_NONE,
  7429. checkClashes = false,
  7430. strictModeChanged = false,
  7431. hasParenthesizedAncestor = false
  7432. }) {
  7433. var _expression$extra;
  7434. const type = expression.type;
  7435. if (this.isObjectMethod(expression)) return;
  7436. if (type === "MemberExpression") {
  7437. if (binding !== BIND_NONE) {
  7438. this.raise(Errors.InvalidPropertyBindingPattern, {
  7439. at: expression
  7440. });
  7441. }
  7442. return;
  7443. }
  7444. if (type === "Identifier") {
  7445. this.checkIdentifier(expression, binding, strictModeChanged);
  7446. const {
  7447. name
  7448. } = expression;
  7449. if (checkClashes) {
  7450. if (checkClashes.has(name)) {
  7451. this.raise(Errors.ParamDupe, {
  7452. at: expression
  7453. });
  7454. } else {
  7455. checkClashes.add(name);
  7456. }
  7457. }
  7458. return;
  7459. }
  7460. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7461. if (validity === true) return;
  7462. if (validity === false) {
  7463. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7464. this.raise(ParseErrorClass, {
  7465. at: expression,
  7466. ancestor
  7467. });
  7468. return;
  7469. }
  7470. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  7471. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" || type === "ParenthesizedExpression" ? {
  7472. type
  7473. } : ancestor;
  7474. for (const child of [].concat(expression[key])) {
  7475. if (child) {
  7476. this.checkLVal(child, {
  7477. in: nextAncestor,
  7478. binding,
  7479. checkClashes,
  7480. strictModeChanged,
  7481. hasParenthesizedAncestor: isParenthesizedExpression
  7482. });
  7483. }
  7484. }
  7485. }
  7486. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7487. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7488. if (bindingType === BIND_NONE) {
  7489. this.raise(Errors.StrictEvalArguments, {
  7490. at,
  7491. referenceName: at.name
  7492. });
  7493. } else {
  7494. this.raise(Errors.StrictEvalArgumentsBinding, {
  7495. at,
  7496. bindingName: at.name
  7497. });
  7498. }
  7499. }
  7500. if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
  7501. this.raise(Errors.LetInLexicalBinding, {
  7502. at
  7503. });
  7504. }
  7505. if (!(bindingType & BIND_NONE)) {
  7506. this.declareNameFromIdentifier(at, bindingType);
  7507. }
  7508. }
  7509. declareNameFromIdentifier(identifier, binding) {
  7510. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7511. }
  7512. checkToRestConversion(node, allowPattern) {
  7513. switch (node.type) {
  7514. case "ParenthesizedExpression":
  7515. this.checkToRestConversion(node.expression, allowPattern);
  7516. break;
  7517. case "Identifier":
  7518. case "MemberExpression":
  7519. break;
  7520. case "ArrayExpression":
  7521. case "ObjectExpression":
  7522. if (allowPattern) break;
  7523. default:
  7524. this.raise(Errors.InvalidRestAssignmentPattern, {
  7525. at: node
  7526. });
  7527. }
  7528. }
  7529. checkCommaAfterRest(close) {
  7530. if (!this.match(12)) {
  7531. return false;
  7532. }
  7533. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  7534. at: this.state.startLoc
  7535. });
  7536. return true;
  7537. }
  7538. }
  7539. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7540. function nonNull(x) {
  7541. if (x == null) {
  7542. throw new Error(`Unexpected ${x} value.`);
  7543. }
  7544. return x;
  7545. }
  7546. function assert(x) {
  7547. if (!x) {
  7548. throw new Error("Assert fail");
  7549. }
  7550. }
  7551. const TSErrors = ParseErrorEnum`typescript`({
  7552. AbstractMethodHasImplementation: ({
  7553. methodName
  7554. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7555. AbstractPropertyHasInitializer: ({
  7556. propertyName
  7557. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7558. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7559. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7560. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7561. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7562. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7563. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7564. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7565. DeclareAccessor: ({
  7566. kind
  7567. }) => `'declare' is not allowed in ${kind}ters.`,
  7568. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7569. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7570. DuplicateAccessibilityModifier: ({
  7571. modifier
  7572. }) => `Accessibility modifier already seen.`,
  7573. DuplicateModifier: ({
  7574. modifier
  7575. }) => `Duplicate modifier: '${modifier}'.`,
  7576. EmptyHeritageClauseType: ({
  7577. token
  7578. }) => `'${token}' list cannot be empty.`,
  7579. EmptyTypeArguments: "Type argument list cannot be empty.",
  7580. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7581. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7582. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7583. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7584. IncompatibleModifiers: ({
  7585. modifiers
  7586. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7587. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7588. IndexSignatureHasAccessibility: ({
  7589. modifier
  7590. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7591. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7592. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7593. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7594. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7595. InvalidModifierOnTypeMember: ({
  7596. modifier
  7597. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7598. InvalidModifierOnTypeParameter: ({
  7599. modifier
  7600. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7601. InvalidModifierOnTypeParameterPositions: ({
  7602. modifier
  7603. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7604. InvalidModifiersOrder: ({
  7605. orderedModifiers
  7606. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7607. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7608. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7609. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7610. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  7611. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7612. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7613. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7614. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7615. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7616. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7617. PrivateElementHasAccessibility: ({
  7618. modifier
  7619. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7620. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7621. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7622. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7623. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7624. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7625. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7626. SingleTypeParameterWithoutTrailingComma: ({
  7627. typeParameterName
  7628. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7629. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7630. TupleOptionalAfterType: "A labeled tuple optional element must be declared using a question mark after the name and before the colon (`name?: type`), rather than after the type (`name: type?`).",
  7631. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7632. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7633. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7634. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7635. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7636. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7637. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7638. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7639. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7640. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7641. UnsupportedSignatureParameterKind: ({
  7642. type
  7643. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7644. });
  7645. function keywordTypeFromName(value) {
  7646. switch (value) {
  7647. case "any":
  7648. return "TSAnyKeyword";
  7649. case "boolean":
  7650. return "TSBooleanKeyword";
  7651. case "bigint":
  7652. return "TSBigIntKeyword";
  7653. case "never":
  7654. return "TSNeverKeyword";
  7655. case "number":
  7656. return "TSNumberKeyword";
  7657. case "object":
  7658. return "TSObjectKeyword";
  7659. case "string":
  7660. return "TSStringKeyword";
  7661. case "symbol":
  7662. return "TSSymbolKeyword";
  7663. case "undefined":
  7664. return "TSUndefinedKeyword";
  7665. case "unknown":
  7666. return "TSUnknownKeyword";
  7667. default:
  7668. return undefined;
  7669. }
  7670. }
  7671. function tsIsAccessModifier(modifier) {
  7672. return modifier === "private" || modifier === "public" || modifier === "protected";
  7673. }
  7674. function tsIsVarianceAnnotations(modifier) {
  7675. return modifier === "in" || modifier === "out";
  7676. }
  7677. var typescript = superClass => class TypeScriptParserMixin extends superClass {
  7678. constructor(...args) {
  7679. super(...args);
  7680. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7681. allowedModifiers: ["in", "out"],
  7682. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7683. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7684. });
  7685. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7686. allowedModifiers: ["const"],
  7687. disallowedModifiers: ["in", "out"],
  7688. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7689. });
  7690. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7691. allowedModifiers: ["in", "out", "const"],
  7692. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7693. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7694. });
  7695. }
  7696. getScopeHandler() {
  7697. return TypeScriptScopeHandler;
  7698. }
  7699. tsIsIdentifier() {
  7700. return tokenIsIdentifier(this.state.type);
  7701. }
  7702. tsTokenCanFollowModifier() {
  7703. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(136) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7704. }
  7705. tsNextTokenCanFollowModifier() {
  7706. this.next();
  7707. return this.tsTokenCanFollowModifier();
  7708. }
  7709. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7710. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7711. return undefined;
  7712. }
  7713. const modifier = this.state.value;
  7714. if (allowedModifiers.indexOf(modifier) !== -1) {
  7715. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7716. return undefined;
  7717. }
  7718. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7719. return modifier;
  7720. }
  7721. }
  7722. return undefined;
  7723. }
  7724. tsParseModifiers({
  7725. allowedModifiers,
  7726. disallowedModifiers,
  7727. stopOnStartOfClassStaticBlock,
  7728. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7729. }, modified) {
  7730. const enforceOrder = (loc, modifier, before, after) => {
  7731. if (modifier === before && modified[after]) {
  7732. this.raise(TSErrors.InvalidModifiersOrder, {
  7733. at: loc,
  7734. orderedModifiers: [before, after]
  7735. });
  7736. }
  7737. };
  7738. const incompatible = (loc, modifier, mod1, mod2) => {
  7739. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7740. this.raise(TSErrors.IncompatibleModifiers, {
  7741. at: loc,
  7742. modifiers: [mod1, mod2]
  7743. });
  7744. }
  7745. };
  7746. for (;;) {
  7747. const {
  7748. startLoc
  7749. } = this.state;
  7750. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7751. if (!modifier) break;
  7752. if (tsIsAccessModifier(modifier)) {
  7753. if (modified.accessibility) {
  7754. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7755. at: startLoc,
  7756. modifier
  7757. });
  7758. } else {
  7759. enforceOrder(startLoc, modifier, modifier, "override");
  7760. enforceOrder(startLoc, modifier, modifier, "static");
  7761. enforceOrder(startLoc, modifier, modifier, "readonly");
  7762. modified.accessibility = modifier;
  7763. }
  7764. } else if (tsIsVarianceAnnotations(modifier)) {
  7765. if (modified[modifier]) {
  7766. this.raise(TSErrors.DuplicateModifier, {
  7767. at: startLoc,
  7768. modifier
  7769. });
  7770. }
  7771. modified[modifier] = true;
  7772. enforceOrder(startLoc, modifier, "in", "out");
  7773. } else {
  7774. if (Object.hasOwnProperty.call(modified, modifier)) {
  7775. this.raise(TSErrors.DuplicateModifier, {
  7776. at: startLoc,
  7777. modifier
  7778. });
  7779. } else {
  7780. enforceOrder(startLoc, modifier, "static", "readonly");
  7781. enforceOrder(startLoc, modifier, "static", "override");
  7782. enforceOrder(startLoc, modifier, "override", "readonly");
  7783. enforceOrder(startLoc, modifier, "abstract", "override");
  7784. incompatible(startLoc, modifier, "declare", "override");
  7785. incompatible(startLoc, modifier, "static", "abstract");
  7786. }
  7787. modified[modifier] = true;
  7788. }
  7789. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7790. this.raise(errorTemplate, {
  7791. at: startLoc,
  7792. modifier
  7793. });
  7794. }
  7795. }
  7796. }
  7797. tsIsListTerminator(kind) {
  7798. switch (kind) {
  7799. case "EnumMembers":
  7800. case "TypeMembers":
  7801. return this.match(8);
  7802. case "HeritageClauseElement":
  7803. return this.match(5);
  7804. case "TupleElementTypes":
  7805. return this.match(3);
  7806. case "TypeParametersOrArguments":
  7807. return this.match(48);
  7808. }
  7809. }
  7810. tsParseList(kind, parseElement) {
  7811. const result = [];
  7812. while (!this.tsIsListTerminator(kind)) {
  7813. result.push(parseElement());
  7814. }
  7815. return result;
  7816. }
  7817. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7818. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7819. }
  7820. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7821. const result = [];
  7822. let trailingCommaPos = -1;
  7823. for (;;) {
  7824. if (this.tsIsListTerminator(kind)) {
  7825. break;
  7826. }
  7827. trailingCommaPos = -1;
  7828. const element = parseElement();
  7829. if (element == null) {
  7830. return undefined;
  7831. }
  7832. result.push(element);
  7833. if (this.eat(12)) {
  7834. trailingCommaPos = this.state.lastTokStart;
  7835. continue;
  7836. }
  7837. if (this.tsIsListTerminator(kind)) {
  7838. break;
  7839. }
  7840. if (expectSuccess) {
  7841. this.expect(12);
  7842. }
  7843. return undefined;
  7844. }
  7845. if (refTrailingCommaPos) {
  7846. refTrailingCommaPos.value = trailingCommaPos;
  7847. }
  7848. return result;
  7849. }
  7850. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7851. if (!skipFirstToken) {
  7852. if (bracket) {
  7853. this.expect(0);
  7854. } else {
  7855. this.expect(47);
  7856. }
  7857. }
  7858. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7859. if (bracket) {
  7860. this.expect(3);
  7861. } else {
  7862. this.expect(48);
  7863. }
  7864. return result;
  7865. }
  7866. tsParseImportType() {
  7867. const node = this.startNode();
  7868. this.expect(83);
  7869. this.expect(10);
  7870. if (!this.match(131)) {
  7871. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7872. at: this.state.startLoc
  7873. });
  7874. }
  7875. node.argument = super.parseExprAtom();
  7876. this.expect(11);
  7877. if (this.eat(16)) {
  7878. node.qualifier = this.tsParseEntityName();
  7879. }
  7880. if (this.match(47)) {
  7881. node.typeParameters = this.tsParseTypeArguments();
  7882. }
  7883. return this.finishNode(node, "TSImportType");
  7884. }
  7885. tsParseEntityName(allowReservedWords = true) {
  7886. let entity = this.parseIdentifier(allowReservedWords);
  7887. while (this.eat(16)) {
  7888. const node = this.startNodeAtNode(entity);
  7889. node.left = entity;
  7890. node.right = this.parseIdentifier(allowReservedWords);
  7891. entity = this.finishNode(node, "TSQualifiedName");
  7892. }
  7893. return entity;
  7894. }
  7895. tsParseTypeReference() {
  7896. const node = this.startNode();
  7897. node.typeName = this.tsParseEntityName();
  7898. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7899. node.typeParameters = this.tsParseTypeArguments();
  7900. }
  7901. return this.finishNode(node, "TSTypeReference");
  7902. }
  7903. tsParseThisTypePredicate(lhs) {
  7904. this.next();
  7905. const node = this.startNodeAtNode(lhs);
  7906. node.parameterName = lhs;
  7907. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7908. node.asserts = false;
  7909. return this.finishNode(node, "TSTypePredicate");
  7910. }
  7911. tsParseThisTypeNode() {
  7912. const node = this.startNode();
  7913. this.next();
  7914. return this.finishNode(node, "TSThisType");
  7915. }
  7916. tsParseTypeQuery() {
  7917. const node = this.startNode();
  7918. this.expect(87);
  7919. if (this.match(83)) {
  7920. node.exprName = this.tsParseImportType();
  7921. } else {
  7922. node.exprName = this.tsParseEntityName();
  7923. }
  7924. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7925. node.typeParameters = this.tsParseTypeArguments();
  7926. }
  7927. return this.finishNode(node, "TSTypeQuery");
  7928. }
  7929. tsParseTypeParameter(parseModifiers) {
  7930. const node = this.startNode();
  7931. parseModifiers(node);
  7932. node.name = this.tsParseTypeParameterName();
  7933. node.constraint = this.tsEatThenParseType(81);
  7934. node.default = this.tsEatThenParseType(29);
  7935. return this.finishNode(node, "TSTypeParameter");
  7936. }
  7937. tsTryParseTypeParameters(parseModifiers) {
  7938. if (this.match(47)) {
  7939. return this.tsParseTypeParameters(parseModifiers);
  7940. }
  7941. }
  7942. tsParseTypeParameters(parseModifiers) {
  7943. const node = this.startNode();
  7944. if (this.match(47) || this.match(140)) {
  7945. this.next();
  7946. } else {
  7947. this.unexpected();
  7948. }
  7949. const refTrailingCommaPos = {
  7950. value: -1
  7951. };
  7952. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7953. if (node.params.length === 0) {
  7954. this.raise(TSErrors.EmptyTypeParameters, {
  7955. at: node
  7956. });
  7957. }
  7958. if (refTrailingCommaPos.value !== -1) {
  7959. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7960. }
  7961. return this.finishNode(node, "TSTypeParameterDeclaration");
  7962. }
  7963. tsFillSignature(returnToken, signature) {
  7964. const returnTokenRequired = returnToken === 19;
  7965. const paramsKey = "parameters";
  7966. const returnTypeKey = "typeAnnotation";
  7967. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  7968. this.expect(10);
  7969. signature[paramsKey] = this.tsParseBindingListForSignature();
  7970. if (returnTokenRequired) {
  7971. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7972. } else if (this.match(returnToken)) {
  7973. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7974. }
  7975. }
  7976. tsParseBindingListForSignature() {
  7977. const list = super.parseBindingList(11, 41, 2);
  7978. for (const pattern of list) {
  7979. const {
  7980. type
  7981. } = pattern;
  7982. if (type === "AssignmentPattern" || type === "TSParameterProperty") {
  7983. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7984. at: pattern,
  7985. type
  7986. });
  7987. }
  7988. }
  7989. return list;
  7990. }
  7991. tsParseTypeMemberSemicolon() {
  7992. if (!this.eat(12) && !this.isLineTerminator()) {
  7993. this.expect(13);
  7994. }
  7995. }
  7996. tsParseSignatureMember(kind, node) {
  7997. this.tsFillSignature(14, node);
  7998. this.tsParseTypeMemberSemicolon();
  7999. return this.finishNode(node, kind);
  8000. }
  8001. tsIsUnambiguouslyIndexSignature() {
  8002. this.next();
  8003. if (tokenIsIdentifier(this.state.type)) {
  8004. this.next();
  8005. return this.match(14);
  8006. }
  8007. return false;
  8008. }
  8009. tsTryParseIndexSignature(node) {
  8010. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  8011. return;
  8012. }
  8013. this.expect(0);
  8014. const id = this.parseIdentifier();
  8015. id.typeAnnotation = this.tsParseTypeAnnotation();
  8016. this.resetEndLocation(id);
  8017. this.expect(3);
  8018. node.parameters = [id];
  8019. const type = this.tsTryParseTypeAnnotation();
  8020. if (type) node.typeAnnotation = type;
  8021. this.tsParseTypeMemberSemicolon();
  8022. return this.finishNode(node, "TSIndexSignature");
  8023. }
  8024. tsParsePropertyOrMethodSignature(node, readonly) {
  8025. if (this.eat(17)) node.optional = true;
  8026. const nodeAny = node;
  8027. if (this.match(10) || this.match(47)) {
  8028. if (readonly) {
  8029. this.raise(TSErrors.ReadonlyForMethodSignature, {
  8030. at: node
  8031. });
  8032. }
  8033. const method = nodeAny;
  8034. if (method.kind && this.match(47)) {
  8035. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  8036. at: this.state.curPosition()
  8037. });
  8038. }
  8039. this.tsFillSignature(14, method);
  8040. this.tsParseTypeMemberSemicolon();
  8041. const paramsKey = "parameters";
  8042. const returnTypeKey = "typeAnnotation";
  8043. if (method.kind === "get") {
  8044. if (method[paramsKey].length > 0) {
  8045. this.raise(Errors.BadGetterArity, {
  8046. at: this.state.curPosition()
  8047. });
  8048. if (this.isThisParam(method[paramsKey][0])) {
  8049. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8050. at: this.state.curPosition()
  8051. });
  8052. }
  8053. }
  8054. } else if (method.kind === "set") {
  8055. if (method[paramsKey].length !== 1) {
  8056. this.raise(Errors.BadSetterArity, {
  8057. at: this.state.curPosition()
  8058. });
  8059. } else {
  8060. const firstParameter = method[paramsKey][0];
  8061. if (this.isThisParam(firstParameter)) {
  8062. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  8063. at: this.state.curPosition()
  8064. });
  8065. }
  8066. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  8067. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  8068. at: this.state.curPosition()
  8069. });
  8070. }
  8071. if (firstParameter.type === "RestElement") {
  8072. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  8073. at: this.state.curPosition()
  8074. });
  8075. }
  8076. }
  8077. if (method[returnTypeKey]) {
  8078. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  8079. at: method[returnTypeKey]
  8080. });
  8081. }
  8082. } else {
  8083. method.kind = "method";
  8084. }
  8085. return this.finishNode(method, "TSMethodSignature");
  8086. } else {
  8087. const property = nodeAny;
  8088. if (readonly) property.readonly = true;
  8089. const type = this.tsTryParseTypeAnnotation();
  8090. if (type) property.typeAnnotation = type;
  8091. this.tsParseTypeMemberSemicolon();
  8092. return this.finishNode(property, "TSPropertySignature");
  8093. }
  8094. }
  8095. tsParseTypeMember() {
  8096. const node = this.startNode();
  8097. if (this.match(10) || this.match(47)) {
  8098. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  8099. }
  8100. if (this.match(77)) {
  8101. const id = this.startNode();
  8102. this.next();
  8103. if (this.match(10) || this.match(47)) {
  8104. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  8105. } else {
  8106. node.key = this.createIdentifier(id, "new");
  8107. return this.tsParsePropertyOrMethodSignature(node, false);
  8108. }
  8109. }
  8110. this.tsParseModifiers({
  8111. allowedModifiers: ["readonly"],
  8112. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  8113. }, node);
  8114. const idx = this.tsTryParseIndexSignature(node);
  8115. if (idx) {
  8116. return idx;
  8117. }
  8118. super.parsePropertyName(node);
  8119. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  8120. node.kind = node.key.name;
  8121. super.parsePropertyName(node);
  8122. }
  8123. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  8124. }
  8125. tsParseTypeLiteral() {
  8126. const node = this.startNode();
  8127. node.members = this.tsParseObjectTypeMembers();
  8128. return this.finishNode(node, "TSTypeLiteral");
  8129. }
  8130. tsParseObjectTypeMembers() {
  8131. this.expect(5);
  8132. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  8133. this.expect(8);
  8134. return members;
  8135. }
  8136. tsIsStartOfMappedType() {
  8137. this.next();
  8138. if (this.eat(53)) {
  8139. return this.isContextual(120);
  8140. }
  8141. if (this.isContextual(120)) {
  8142. this.next();
  8143. }
  8144. if (!this.match(0)) {
  8145. return false;
  8146. }
  8147. this.next();
  8148. if (!this.tsIsIdentifier()) {
  8149. return false;
  8150. }
  8151. this.next();
  8152. return this.match(58);
  8153. }
  8154. tsParseMappedTypeParameter() {
  8155. const node = this.startNode();
  8156. node.name = this.tsParseTypeParameterName();
  8157. node.constraint = this.tsExpectThenParseType(58);
  8158. return this.finishNode(node, "TSTypeParameter");
  8159. }
  8160. tsParseMappedType() {
  8161. const node = this.startNode();
  8162. this.expect(5);
  8163. if (this.match(53)) {
  8164. node.readonly = this.state.value;
  8165. this.next();
  8166. this.expectContextual(120);
  8167. } else if (this.eatContextual(120)) {
  8168. node.readonly = true;
  8169. }
  8170. this.expect(0);
  8171. node.typeParameter = this.tsParseMappedTypeParameter();
  8172. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  8173. this.expect(3);
  8174. if (this.match(53)) {
  8175. node.optional = this.state.value;
  8176. this.next();
  8177. this.expect(17);
  8178. } else if (this.eat(17)) {
  8179. node.optional = true;
  8180. }
  8181. node.typeAnnotation = this.tsTryParseType();
  8182. this.semicolon();
  8183. this.expect(8);
  8184. return this.finishNode(node, "TSMappedType");
  8185. }
  8186. tsParseTupleType() {
  8187. const node = this.startNode();
  8188. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  8189. let seenOptionalElement = false;
  8190. let labeledElements = null;
  8191. node.elementTypes.forEach(elementNode => {
  8192. var _labeledElements;
  8193. const {
  8194. type
  8195. } = elementNode;
  8196. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  8197. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  8198. at: elementNode
  8199. });
  8200. }
  8201. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  8202. let checkType = type;
  8203. if (type === "TSRestType") {
  8204. elementNode = elementNode.typeAnnotation;
  8205. checkType = elementNode.type;
  8206. }
  8207. const isLabeled = checkType === "TSNamedTupleMember";
  8208. (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
  8209. if (labeledElements !== isLabeled) {
  8210. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  8211. at: elementNode
  8212. });
  8213. }
  8214. });
  8215. return this.finishNode(node, "TSTupleType");
  8216. }
  8217. tsParseTupleElementType() {
  8218. const {
  8219. startLoc
  8220. } = this.state;
  8221. const rest = this.eat(21);
  8222. let labeled;
  8223. let label;
  8224. let optional;
  8225. let type;
  8226. const isWord = tokenIsKeywordOrIdentifier(this.state.type);
  8227. const chAfterWord = isWord ? this.lookaheadCharCode() : null;
  8228. if (chAfterWord === 58) {
  8229. labeled = true;
  8230. optional = false;
  8231. label = this.parseIdentifier(true);
  8232. this.expect(14);
  8233. type = this.tsParseType();
  8234. } else if (chAfterWord === 63) {
  8235. optional = true;
  8236. const startLoc = this.state.startLoc;
  8237. const wordName = this.state.value;
  8238. const typeOrLabel = this.tsParseNonArrayType();
  8239. if (this.lookaheadCharCode() === 58) {
  8240. labeled = true;
  8241. label = this.createIdentifier(this.startNodeAt(startLoc), wordName);
  8242. this.expect(17);
  8243. this.expect(14);
  8244. type = this.tsParseType();
  8245. } else {
  8246. labeled = false;
  8247. type = typeOrLabel;
  8248. this.expect(17);
  8249. }
  8250. } else {
  8251. type = this.tsParseType();
  8252. optional = this.eat(17);
  8253. labeled = this.eat(14);
  8254. }
  8255. if (labeled) {
  8256. let labeledNode;
  8257. if (label) {
  8258. labeledNode = this.startNodeAtNode(label);
  8259. labeledNode.optional = optional;
  8260. labeledNode.label = label;
  8261. labeledNode.elementType = type;
  8262. if (this.eat(17)) {
  8263. labeledNode.optional = true;
  8264. this.raise(TSErrors.TupleOptionalAfterType, {
  8265. at: this.state.lastTokStartLoc
  8266. });
  8267. }
  8268. } else {
  8269. labeledNode = this.startNodeAtNode(type);
  8270. labeledNode.optional = optional;
  8271. this.raise(TSErrors.InvalidTupleMemberLabel, {
  8272. at: type
  8273. });
  8274. labeledNode.label = type;
  8275. labeledNode.elementType = this.tsParseType();
  8276. }
  8277. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8278. } else if (optional) {
  8279. const optionalTypeNode = this.startNodeAtNode(type);
  8280. optionalTypeNode.typeAnnotation = type;
  8281. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8282. }
  8283. if (rest) {
  8284. const restNode = this.startNodeAt(startLoc);
  8285. restNode.typeAnnotation = type;
  8286. type = this.finishNode(restNode, "TSRestType");
  8287. }
  8288. return type;
  8289. }
  8290. tsParseParenthesizedType() {
  8291. const node = this.startNode();
  8292. this.expect(10);
  8293. node.typeAnnotation = this.tsParseType();
  8294. this.expect(11);
  8295. return this.finishNode(node, "TSParenthesizedType");
  8296. }
  8297. tsParseFunctionOrConstructorType(type, abstract) {
  8298. const node = this.startNode();
  8299. if (type === "TSConstructorType") {
  8300. node.abstract = !!abstract;
  8301. if (abstract) this.next();
  8302. this.next();
  8303. }
  8304. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8305. return this.finishNode(node, type);
  8306. }
  8307. tsParseLiteralTypeNode() {
  8308. const node = this.startNode();
  8309. switch (this.state.type) {
  8310. case 132:
  8311. case 133:
  8312. case 131:
  8313. case 85:
  8314. case 86:
  8315. node.literal = super.parseExprAtom();
  8316. break;
  8317. default:
  8318. this.unexpected();
  8319. }
  8320. return this.finishNode(node, "TSLiteralType");
  8321. }
  8322. tsParseTemplateLiteralType() {
  8323. const node = this.startNode();
  8324. node.literal = super.parseTemplate(false);
  8325. return this.finishNode(node, "TSLiteralType");
  8326. }
  8327. parseTemplateSubstitution() {
  8328. if (this.state.inType) return this.tsParseType();
  8329. return super.parseTemplateSubstitution();
  8330. }
  8331. tsParseThisTypeOrThisTypePredicate() {
  8332. const thisKeyword = this.tsParseThisTypeNode();
  8333. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8334. return this.tsParseThisTypePredicate(thisKeyword);
  8335. } else {
  8336. return thisKeyword;
  8337. }
  8338. }
  8339. tsParseNonArrayType() {
  8340. switch (this.state.type) {
  8341. case 131:
  8342. case 132:
  8343. case 133:
  8344. case 85:
  8345. case 86:
  8346. return this.tsParseLiteralTypeNode();
  8347. case 53:
  8348. if (this.state.value === "-") {
  8349. const node = this.startNode();
  8350. const nextToken = this.lookahead();
  8351. if (nextToken.type !== 132 && nextToken.type !== 133) {
  8352. this.unexpected();
  8353. }
  8354. node.literal = this.parseMaybeUnary();
  8355. return this.finishNode(node, "TSLiteralType");
  8356. }
  8357. break;
  8358. case 78:
  8359. return this.tsParseThisTypeOrThisTypePredicate();
  8360. case 87:
  8361. return this.tsParseTypeQuery();
  8362. case 83:
  8363. return this.tsParseImportType();
  8364. case 5:
  8365. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8366. case 0:
  8367. return this.tsParseTupleType();
  8368. case 10:
  8369. return this.tsParseParenthesizedType();
  8370. case 25:
  8371. case 24:
  8372. return this.tsParseTemplateLiteralType();
  8373. default:
  8374. {
  8375. const {
  8376. type
  8377. } = this.state;
  8378. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8379. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8380. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8381. const node = this.startNode();
  8382. this.next();
  8383. return this.finishNode(node, nodeType);
  8384. }
  8385. return this.tsParseTypeReference();
  8386. }
  8387. }
  8388. }
  8389. this.unexpected();
  8390. }
  8391. tsParseArrayTypeOrHigher() {
  8392. let type = this.tsParseNonArrayType();
  8393. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8394. if (this.match(3)) {
  8395. const node = this.startNodeAtNode(type);
  8396. node.elementType = type;
  8397. this.expect(3);
  8398. type = this.finishNode(node, "TSArrayType");
  8399. } else {
  8400. const node = this.startNodeAtNode(type);
  8401. node.objectType = type;
  8402. node.indexType = this.tsParseType();
  8403. this.expect(3);
  8404. type = this.finishNode(node, "TSIndexedAccessType");
  8405. }
  8406. }
  8407. return type;
  8408. }
  8409. tsParseTypeOperator() {
  8410. const node = this.startNode();
  8411. const operator = this.state.value;
  8412. this.next();
  8413. node.operator = operator;
  8414. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8415. if (operator === "readonly") {
  8416. this.tsCheckTypeAnnotationForReadOnly(node);
  8417. }
  8418. return this.finishNode(node, "TSTypeOperator");
  8419. }
  8420. tsCheckTypeAnnotationForReadOnly(node) {
  8421. switch (node.typeAnnotation.type) {
  8422. case "TSTupleType":
  8423. case "TSArrayType":
  8424. return;
  8425. default:
  8426. this.raise(TSErrors.UnexpectedReadonly, {
  8427. at: node
  8428. });
  8429. }
  8430. }
  8431. tsParseInferType() {
  8432. const node = this.startNode();
  8433. this.expectContextual(113);
  8434. const typeParameter = this.startNode();
  8435. typeParameter.name = this.tsParseTypeParameterName();
  8436. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8437. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8438. return this.finishNode(node, "TSInferType");
  8439. }
  8440. tsParseConstraintForInferType() {
  8441. if (this.eat(81)) {
  8442. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8443. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8444. return constraint;
  8445. }
  8446. }
  8447. }
  8448. tsParseTypeOperatorOrHigher() {
  8449. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8450. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(113) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8451. }
  8452. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8453. const node = this.startNode();
  8454. const hasLeadingOperator = this.eat(operator);
  8455. const types = [];
  8456. do {
  8457. types.push(parseConstituentType());
  8458. } while (this.eat(operator));
  8459. if (types.length === 1 && !hasLeadingOperator) {
  8460. return types[0];
  8461. }
  8462. node.types = types;
  8463. return this.finishNode(node, kind);
  8464. }
  8465. tsParseIntersectionTypeOrHigher() {
  8466. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8467. }
  8468. tsParseUnionTypeOrHigher() {
  8469. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8470. }
  8471. tsIsStartOfFunctionType() {
  8472. if (this.match(47)) {
  8473. return true;
  8474. }
  8475. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8476. }
  8477. tsSkipParameterStart() {
  8478. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8479. this.next();
  8480. return true;
  8481. }
  8482. if (this.match(5)) {
  8483. const {
  8484. errors
  8485. } = this.state;
  8486. const previousErrorCount = errors.length;
  8487. try {
  8488. this.parseObjectLike(8, true);
  8489. return errors.length === previousErrorCount;
  8490. } catch (_unused) {
  8491. return false;
  8492. }
  8493. }
  8494. if (this.match(0)) {
  8495. this.next();
  8496. const {
  8497. errors
  8498. } = this.state;
  8499. const previousErrorCount = errors.length;
  8500. try {
  8501. super.parseBindingList(3, 93, 1);
  8502. return errors.length === previousErrorCount;
  8503. } catch (_unused2) {
  8504. return false;
  8505. }
  8506. }
  8507. return false;
  8508. }
  8509. tsIsUnambiguouslyStartOfFunctionType() {
  8510. this.next();
  8511. if (this.match(11) || this.match(21)) {
  8512. return true;
  8513. }
  8514. if (this.tsSkipParameterStart()) {
  8515. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8516. return true;
  8517. }
  8518. if (this.match(11)) {
  8519. this.next();
  8520. if (this.match(19)) {
  8521. return true;
  8522. }
  8523. }
  8524. }
  8525. return false;
  8526. }
  8527. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8528. return this.tsInType(() => {
  8529. const t = this.startNode();
  8530. this.expect(returnToken);
  8531. const node = this.startNode();
  8532. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8533. if (asserts && this.match(78)) {
  8534. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8535. if (thisTypePredicate.type === "TSThisType") {
  8536. node.parameterName = thisTypePredicate;
  8537. node.asserts = true;
  8538. node.typeAnnotation = null;
  8539. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8540. } else {
  8541. this.resetStartLocationFromNode(thisTypePredicate, node);
  8542. thisTypePredicate.asserts = true;
  8543. }
  8544. t.typeAnnotation = thisTypePredicate;
  8545. return this.finishNode(t, "TSTypeAnnotation");
  8546. }
  8547. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8548. if (!typePredicateVariable) {
  8549. if (!asserts) {
  8550. return this.tsParseTypeAnnotation(false, t);
  8551. }
  8552. node.parameterName = this.parseIdentifier();
  8553. node.asserts = asserts;
  8554. node.typeAnnotation = null;
  8555. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8556. return this.finishNode(t, "TSTypeAnnotation");
  8557. }
  8558. const type = this.tsParseTypeAnnotation(false);
  8559. node.parameterName = typePredicateVariable;
  8560. node.typeAnnotation = type;
  8561. node.asserts = asserts;
  8562. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8563. return this.finishNode(t, "TSTypeAnnotation");
  8564. });
  8565. }
  8566. tsTryParseTypeOrTypePredicateAnnotation() {
  8567. if (this.match(14)) {
  8568. return this.tsParseTypeOrTypePredicateAnnotation(14);
  8569. }
  8570. }
  8571. tsTryParseTypeAnnotation() {
  8572. if (this.match(14)) {
  8573. return this.tsParseTypeAnnotation();
  8574. }
  8575. }
  8576. tsTryParseType() {
  8577. return this.tsEatThenParseType(14);
  8578. }
  8579. tsParseTypePredicatePrefix() {
  8580. const id = this.parseIdentifier();
  8581. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8582. this.next();
  8583. return id;
  8584. }
  8585. }
  8586. tsParseTypePredicateAsserts() {
  8587. if (this.state.type !== 107) {
  8588. return false;
  8589. }
  8590. const containsEsc = this.state.containsEsc;
  8591. this.next();
  8592. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8593. return false;
  8594. }
  8595. if (containsEsc) {
  8596. this.raise(Errors.InvalidEscapedReservedWord, {
  8597. at: this.state.lastTokStartLoc,
  8598. reservedWord: "asserts"
  8599. });
  8600. }
  8601. return true;
  8602. }
  8603. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8604. this.tsInType(() => {
  8605. if (eatColon) this.expect(14);
  8606. t.typeAnnotation = this.tsParseType();
  8607. });
  8608. return this.finishNode(t, "TSTypeAnnotation");
  8609. }
  8610. tsParseType() {
  8611. assert(this.state.inType);
  8612. const type = this.tsParseNonConditionalType();
  8613. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8614. return type;
  8615. }
  8616. const node = this.startNodeAtNode(type);
  8617. node.checkType = type;
  8618. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8619. this.expect(17);
  8620. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8621. this.expect(14);
  8622. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8623. return this.finishNode(node, "TSConditionalType");
  8624. }
  8625. isAbstractConstructorSignature() {
  8626. return this.isContextual(122) && this.lookahead().type === 77;
  8627. }
  8628. tsParseNonConditionalType() {
  8629. if (this.tsIsStartOfFunctionType()) {
  8630. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8631. }
  8632. if (this.match(77)) {
  8633. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8634. } else if (this.isAbstractConstructorSignature()) {
  8635. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8636. }
  8637. return this.tsParseUnionTypeOrHigher();
  8638. }
  8639. tsParseTypeAssertion() {
  8640. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8641. this.raise(TSErrors.ReservedTypeAssertion, {
  8642. at: this.state.startLoc
  8643. });
  8644. }
  8645. const node = this.startNode();
  8646. node.typeAnnotation = this.tsInType(() => {
  8647. this.next();
  8648. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8649. });
  8650. this.expect(48);
  8651. node.expression = this.parseMaybeUnary();
  8652. return this.finishNode(node, "TSTypeAssertion");
  8653. }
  8654. tsParseHeritageClause(token) {
  8655. const originalStartLoc = this.state.startLoc;
  8656. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8657. const node = this.startNode();
  8658. node.expression = this.tsParseEntityName();
  8659. if (this.match(47)) {
  8660. node.typeParameters = this.tsParseTypeArguments();
  8661. }
  8662. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8663. });
  8664. if (!delimitedList.length) {
  8665. this.raise(TSErrors.EmptyHeritageClauseType, {
  8666. at: originalStartLoc,
  8667. token
  8668. });
  8669. }
  8670. return delimitedList;
  8671. }
  8672. tsParseInterfaceDeclaration(node, properties = {}) {
  8673. if (this.hasFollowingLineBreak()) return null;
  8674. this.expectContextual(127);
  8675. if (properties.declare) node.declare = true;
  8676. if (tokenIsIdentifier(this.state.type)) {
  8677. node.id = this.parseIdentifier();
  8678. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  8679. } else {
  8680. node.id = null;
  8681. this.raise(TSErrors.MissingInterfaceName, {
  8682. at: this.state.startLoc
  8683. });
  8684. }
  8685. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8686. if (this.eat(81)) {
  8687. node.extends = this.tsParseHeritageClause("extends");
  8688. }
  8689. const body = this.startNode();
  8690. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8691. node.body = this.finishNode(body, "TSInterfaceBody");
  8692. return this.finishNode(node, "TSInterfaceDeclaration");
  8693. }
  8694. tsParseTypeAliasDeclaration(node) {
  8695. node.id = this.parseIdentifier();
  8696. this.checkIdentifier(node.id, BIND_TS_TYPE);
  8697. node.typeAnnotation = this.tsInType(() => {
  8698. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8699. this.expect(29);
  8700. if (this.isContextual(112) && this.lookahead().type !== 16) {
  8701. const node = this.startNode();
  8702. this.next();
  8703. return this.finishNode(node, "TSIntrinsicKeyword");
  8704. }
  8705. return this.tsParseType();
  8706. });
  8707. this.semicolon();
  8708. return this.finishNode(node, "TSTypeAliasDeclaration");
  8709. }
  8710. tsInNoContext(cb) {
  8711. const oldContext = this.state.context;
  8712. this.state.context = [oldContext[0]];
  8713. try {
  8714. return cb();
  8715. } finally {
  8716. this.state.context = oldContext;
  8717. }
  8718. }
  8719. tsInType(cb) {
  8720. const oldInType = this.state.inType;
  8721. this.state.inType = true;
  8722. try {
  8723. return cb();
  8724. } finally {
  8725. this.state.inType = oldInType;
  8726. }
  8727. }
  8728. tsInDisallowConditionalTypesContext(cb) {
  8729. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8730. this.state.inDisallowConditionalTypesContext = true;
  8731. try {
  8732. return cb();
  8733. } finally {
  8734. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8735. }
  8736. }
  8737. tsInAllowConditionalTypesContext(cb) {
  8738. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8739. this.state.inDisallowConditionalTypesContext = false;
  8740. try {
  8741. return cb();
  8742. } finally {
  8743. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8744. }
  8745. }
  8746. tsEatThenParseType(token) {
  8747. if (this.match(token)) {
  8748. return this.tsNextThenParseType();
  8749. }
  8750. }
  8751. tsExpectThenParseType(token) {
  8752. return this.tsInType(() => {
  8753. this.expect(token);
  8754. return this.tsParseType();
  8755. });
  8756. }
  8757. tsNextThenParseType() {
  8758. return this.tsInType(() => {
  8759. this.next();
  8760. return this.tsParseType();
  8761. });
  8762. }
  8763. tsParseEnumMember() {
  8764. const node = this.startNode();
  8765. node.id = this.match(131) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8766. if (this.eat(29)) {
  8767. node.initializer = super.parseMaybeAssignAllowIn();
  8768. }
  8769. return this.finishNode(node, "TSEnumMember");
  8770. }
  8771. tsParseEnumDeclaration(node, properties = {}) {
  8772. if (properties.const) node.const = true;
  8773. if (properties.declare) node.declare = true;
  8774. this.expectContextual(124);
  8775. node.id = this.parseIdentifier();
  8776. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8777. this.expect(5);
  8778. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8779. this.expect(8);
  8780. return this.finishNode(node, "TSEnumDeclaration");
  8781. }
  8782. tsParseModuleBlock() {
  8783. const node = this.startNode();
  8784. this.scope.enter(SCOPE_OTHER);
  8785. this.expect(5);
  8786. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8787. this.scope.exit();
  8788. return this.finishNode(node, "TSModuleBlock");
  8789. }
  8790. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8791. node.id = this.parseIdentifier();
  8792. if (!nested) {
  8793. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8794. }
  8795. if (this.eat(16)) {
  8796. const inner = this.startNode();
  8797. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8798. node.body = inner;
  8799. } else {
  8800. this.scope.enter(SCOPE_TS_MODULE);
  8801. this.prodParam.enter(PARAM);
  8802. node.body = this.tsParseModuleBlock();
  8803. this.prodParam.exit();
  8804. this.scope.exit();
  8805. }
  8806. return this.finishNode(node, "TSModuleDeclaration");
  8807. }
  8808. tsParseAmbientExternalModuleDeclaration(node) {
  8809. if (this.isContextual(110)) {
  8810. node.global = true;
  8811. node.id = this.parseIdentifier();
  8812. } else if (this.match(131)) {
  8813. node.id = super.parseStringLiteral(this.state.value);
  8814. } else {
  8815. this.unexpected();
  8816. }
  8817. if (this.match(5)) {
  8818. this.scope.enter(SCOPE_TS_MODULE);
  8819. this.prodParam.enter(PARAM);
  8820. node.body = this.tsParseModuleBlock();
  8821. this.prodParam.exit();
  8822. this.scope.exit();
  8823. } else {
  8824. this.semicolon();
  8825. }
  8826. return this.finishNode(node, "TSModuleDeclaration");
  8827. }
  8828. tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, isExport) {
  8829. node.isExport = isExport || false;
  8830. node.id = maybeDefaultIdentifier || this.parseIdentifier();
  8831. this.checkIdentifier(node.id, BIND_FLAGS_TS_IMPORT);
  8832. this.expect(29);
  8833. const moduleReference = this.tsParseModuleReference();
  8834. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8835. this.raise(TSErrors.ImportAliasHasImportType, {
  8836. at: moduleReference
  8837. });
  8838. }
  8839. node.moduleReference = moduleReference;
  8840. this.semicolon();
  8841. return this.finishNode(node, "TSImportEqualsDeclaration");
  8842. }
  8843. tsIsExternalModuleReference() {
  8844. return this.isContextual(117) && this.lookaheadCharCode() === 40;
  8845. }
  8846. tsParseModuleReference() {
  8847. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8848. }
  8849. tsParseExternalModuleReference() {
  8850. const node = this.startNode();
  8851. this.expectContextual(117);
  8852. this.expect(10);
  8853. if (!this.match(131)) {
  8854. this.unexpected();
  8855. }
  8856. node.expression = super.parseExprAtom();
  8857. this.expect(11);
  8858. this.sawUnambiguousESM = true;
  8859. return this.finishNode(node, "TSExternalModuleReference");
  8860. }
  8861. tsLookAhead(f) {
  8862. const state = this.state.clone();
  8863. const res = f();
  8864. this.state = state;
  8865. return res;
  8866. }
  8867. tsTryParseAndCatch(f) {
  8868. const result = this.tryParse(abort => f() || abort());
  8869. if (result.aborted || !result.node) return;
  8870. if (result.error) this.state = result.failState;
  8871. return result.node;
  8872. }
  8873. tsTryParse(f) {
  8874. const state = this.state.clone();
  8875. const result = f();
  8876. if (result !== undefined && result !== false) {
  8877. return result;
  8878. }
  8879. this.state = state;
  8880. }
  8881. tsTryParseDeclare(nany) {
  8882. if (this.isLineTerminator()) {
  8883. return;
  8884. }
  8885. let startType = this.state.type;
  8886. let kind;
  8887. if (this.isContextual(99)) {
  8888. startType = 74;
  8889. kind = "let";
  8890. }
  8891. return this.tsInAmbientContext(() => {
  8892. switch (startType) {
  8893. case 68:
  8894. nany.declare = true;
  8895. return super.parseFunctionStatement(nany, false, false);
  8896. case 80:
  8897. nany.declare = true;
  8898. return this.parseClass(nany, true, false);
  8899. case 124:
  8900. return this.tsParseEnumDeclaration(nany, {
  8901. declare: true
  8902. });
  8903. case 110:
  8904. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8905. case 75:
  8906. case 74:
  8907. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8908. nany.declare = true;
  8909. return this.parseVarStatement(nany, kind || this.state.value, true);
  8910. }
  8911. this.expect(75);
  8912. return this.tsParseEnumDeclaration(nany, {
  8913. const: true,
  8914. declare: true
  8915. });
  8916. case 127:
  8917. {
  8918. const result = this.tsParseInterfaceDeclaration(nany, {
  8919. declare: true
  8920. });
  8921. if (result) return result;
  8922. }
  8923. default:
  8924. if (tokenIsIdentifier(startType)) {
  8925. return this.tsParseDeclaration(nany, this.state.value, true, null);
  8926. }
  8927. }
  8928. });
  8929. }
  8930. tsTryParseExportDeclaration() {
  8931. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  8932. }
  8933. tsParseExpressionStatement(node, expr, decorators) {
  8934. switch (expr.name) {
  8935. case "declare":
  8936. {
  8937. const declaration = this.tsTryParseDeclare(node);
  8938. if (declaration) {
  8939. declaration.declare = true;
  8940. }
  8941. return declaration;
  8942. }
  8943. case "global":
  8944. if (this.match(5)) {
  8945. this.scope.enter(SCOPE_TS_MODULE);
  8946. this.prodParam.enter(PARAM);
  8947. const mod = node;
  8948. mod.global = true;
  8949. mod.id = expr;
  8950. mod.body = this.tsParseModuleBlock();
  8951. this.scope.exit();
  8952. this.prodParam.exit();
  8953. return this.finishNode(mod, "TSModuleDeclaration");
  8954. }
  8955. break;
  8956. default:
  8957. return this.tsParseDeclaration(node, expr.name, false, decorators);
  8958. }
  8959. }
  8960. tsParseDeclaration(node, value, next, decorators) {
  8961. switch (value) {
  8962. case "abstract":
  8963. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8964. return this.tsParseAbstractDeclaration(node, decorators);
  8965. }
  8966. break;
  8967. case "module":
  8968. if (this.tsCheckLineTerminator(next)) {
  8969. if (this.match(131)) {
  8970. return this.tsParseAmbientExternalModuleDeclaration(node);
  8971. } else if (tokenIsIdentifier(this.state.type)) {
  8972. return this.tsParseModuleOrNamespaceDeclaration(node);
  8973. }
  8974. }
  8975. break;
  8976. case "namespace":
  8977. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8978. return this.tsParseModuleOrNamespaceDeclaration(node);
  8979. }
  8980. break;
  8981. case "type":
  8982. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8983. return this.tsParseTypeAliasDeclaration(node);
  8984. }
  8985. break;
  8986. }
  8987. }
  8988. tsCheckLineTerminator(next) {
  8989. if (next) {
  8990. if (this.hasFollowingLineBreak()) return false;
  8991. this.next();
  8992. return true;
  8993. }
  8994. return !this.isLineTerminator();
  8995. }
  8996. tsTryParseGenericAsyncArrowFunction(startLoc) {
  8997. if (!this.match(47)) return;
  8998. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8999. this.state.maybeInArrowParameters = true;
  9000. const res = this.tsTryParseAndCatch(() => {
  9001. const node = this.startNodeAt(startLoc);
  9002. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9003. super.parseFunctionParams(node);
  9004. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  9005. this.expect(19);
  9006. return node;
  9007. });
  9008. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9009. if (!res) return;
  9010. return super.parseArrowExpression(res, null, true);
  9011. }
  9012. tsParseTypeArgumentsInExpression() {
  9013. if (this.reScan_lt() !== 47) return;
  9014. return this.tsParseTypeArguments();
  9015. }
  9016. tsParseTypeArguments() {
  9017. const node = this.startNode();
  9018. node.params = this.tsInType(() => this.tsInNoContext(() => {
  9019. this.expect(47);
  9020. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  9021. }));
  9022. if (node.params.length === 0) {
  9023. this.raise(TSErrors.EmptyTypeArguments, {
  9024. at: node
  9025. });
  9026. } else if (!this.state.inType && this.curContext() === types.brace) {
  9027. this.reScan_lt_gt();
  9028. }
  9029. this.expect(48);
  9030. return this.finishNode(node, "TSTypeParameterInstantiation");
  9031. }
  9032. tsIsDeclarationStart() {
  9033. return tokenIsTSDeclarationStart(this.state.type);
  9034. }
  9035. isExportDefaultSpecifier() {
  9036. if (this.tsIsDeclarationStart()) return false;
  9037. return super.isExportDefaultSpecifier();
  9038. }
  9039. parseAssignableListItem(flags, decorators) {
  9040. const startLoc = this.state.startLoc;
  9041. const modified = {};
  9042. this.tsParseModifiers({
  9043. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  9044. }, modified);
  9045. const accessibility = modified.accessibility;
  9046. const override = modified.override;
  9047. const readonly = modified.readonly;
  9048. if (!(flags & 4) && (accessibility || readonly || override)) {
  9049. this.raise(TSErrors.UnexpectedParameterModifier, {
  9050. at: startLoc
  9051. });
  9052. }
  9053. const left = this.parseMaybeDefault();
  9054. this.parseAssignableListItemTypes(left, flags);
  9055. const elt = this.parseMaybeDefault(left.loc.start, left);
  9056. if (accessibility || readonly || override) {
  9057. const pp = this.startNodeAt(startLoc);
  9058. if (decorators.length) {
  9059. pp.decorators = decorators;
  9060. }
  9061. if (accessibility) pp.accessibility = accessibility;
  9062. if (readonly) pp.readonly = readonly;
  9063. if (override) pp.override = override;
  9064. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  9065. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  9066. at: pp
  9067. });
  9068. }
  9069. pp.parameter = elt;
  9070. return this.finishNode(pp, "TSParameterProperty");
  9071. }
  9072. if (decorators.length) {
  9073. left.decorators = decorators;
  9074. }
  9075. return elt;
  9076. }
  9077. isSimpleParameter(node) {
  9078. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  9079. }
  9080. tsDisallowOptionalPattern(node) {
  9081. for (const param of node.params) {
  9082. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  9083. this.raise(TSErrors.PatternIsOptional, {
  9084. at: param
  9085. });
  9086. }
  9087. }
  9088. }
  9089. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  9090. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  9091. this.tsDisallowOptionalPattern(node);
  9092. }
  9093. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9094. if (this.match(14)) {
  9095. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9096. }
  9097. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  9098. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  9099. return this.finishNode(node, bodilessType);
  9100. }
  9101. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  9102. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  9103. at: node
  9104. });
  9105. if (node.declare) {
  9106. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  9107. }
  9108. }
  9109. this.tsDisallowOptionalPattern(node);
  9110. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  9111. }
  9112. registerFunctionStatementId(node) {
  9113. if (!node.body && node.id) {
  9114. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  9115. } else {
  9116. super.registerFunctionStatementId(node);
  9117. }
  9118. }
  9119. tsCheckForInvalidTypeCasts(items) {
  9120. items.forEach(node => {
  9121. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  9122. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  9123. at: node.typeAnnotation
  9124. });
  9125. }
  9126. });
  9127. }
  9128. toReferencedList(exprList, isInParens) {
  9129. this.tsCheckForInvalidTypeCasts(exprList);
  9130. return exprList;
  9131. }
  9132. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  9133. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  9134. if (node.type === "ArrayExpression") {
  9135. this.tsCheckForInvalidTypeCasts(node.elements);
  9136. }
  9137. return node;
  9138. }
  9139. parseSubscript(base, startLoc, noCalls, state) {
  9140. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  9141. this.state.canStartJSXElement = false;
  9142. this.next();
  9143. const nonNullExpression = this.startNodeAt(startLoc);
  9144. nonNullExpression.expression = base;
  9145. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  9146. }
  9147. let isOptionalCall = false;
  9148. if (this.match(18) && this.lookaheadCharCode() === 60) {
  9149. if (noCalls) {
  9150. state.stop = true;
  9151. return base;
  9152. }
  9153. state.optionalChainMember = isOptionalCall = true;
  9154. this.next();
  9155. }
  9156. if (this.match(47) || this.match(51)) {
  9157. let missingParenErrorLoc;
  9158. const result = this.tsTryParseAndCatch(() => {
  9159. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  9160. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  9161. if (asyncArrowFn) {
  9162. return asyncArrowFn;
  9163. }
  9164. }
  9165. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9166. if (!typeArguments) return;
  9167. if (isOptionalCall && !this.match(10)) {
  9168. missingParenErrorLoc = this.state.curPosition();
  9169. return;
  9170. }
  9171. if (tokenIsTemplate(this.state.type)) {
  9172. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  9173. result.typeParameters = typeArguments;
  9174. return result;
  9175. }
  9176. if (!noCalls && this.eat(10)) {
  9177. const node = this.startNodeAt(startLoc);
  9178. node.callee = base;
  9179. node.arguments = this.parseCallExpressionArguments(11, false);
  9180. this.tsCheckForInvalidTypeCasts(node.arguments);
  9181. node.typeParameters = typeArguments;
  9182. if (state.optionalChainMember) {
  9183. node.optional = isOptionalCall;
  9184. }
  9185. return this.finishCallExpression(node, state.optionalChainMember);
  9186. }
  9187. const tokenType = this.state.type;
  9188. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  9189. return;
  9190. }
  9191. const node = this.startNodeAt(startLoc);
  9192. node.expression = base;
  9193. node.typeParameters = typeArguments;
  9194. return this.finishNode(node, "TSInstantiationExpression");
  9195. });
  9196. if (missingParenErrorLoc) {
  9197. this.unexpected(missingParenErrorLoc, 10);
  9198. }
  9199. if (result) {
  9200. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  9201. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
  9202. at: this.state.startLoc
  9203. });
  9204. }
  9205. return result;
  9206. }
  9207. }
  9208. return super.parseSubscript(base, startLoc, noCalls, state);
  9209. }
  9210. parseNewCallee(node) {
  9211. var _callee$extra;
  9212. super.parseNewCallee(node);
  9213. const {
  9214. callee
  9215. } = node;
  9216. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  9217. node.typeParameters = callee.typeParameters;
  9218. node.callee = callee.expression;
  9219. }
  9220. }
  9221. parseExprOp(left, leftStartLoc, minPrec) {
  9222. let isSatisfies;
  9223. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(118)))) {
  9224. const node = this.startNodeAt(leftStartLoc);
  9225. node.expression = left;
  9226. node.typeAnnotation = this.tsInType(() => {
  9227. this.next();
  9228. if (this.match(75)) {
  9229. if (isSatisfies) {
  9230. this.raise(Errors.UnexpectedKeyword, {
  9231. at: this.state.startLoc,
  9232. keyword: "const"
  9233. });
  9234. }
  9235. return this.tsParseTypeReference();
  9236. }
  9237. return this.tsParseType();
  9238. });
  9239. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  9240. this.reScan_lt_gt();
  9241. return this.parseExprOp(node, leftStartLoc, minPrec);
  9242. }
  9243. return super.parseExprOp(left, leftStartLoc, minPrec);
  9244. }
  9245. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9246. if (!this.state.isAmbientContext) {
  9247. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9248. }
  9249. }
  9250. checkImportReflection(node) {
  9251. super.checkImportReflection(node);
  9252. if (node.module && node.importKind !== "value") {
  9253. this.raise(TSErrors.ImportReflectionHasImportType, {
  9254. at: node.specifiers[0].loc.start
  9255. });
  9256. }
  9257. }
  9258. checkDuplicateExports() {}
  9259. isPotentialImportPhase(isExport) {
  9260. if (super.isPotentialImportPhase(isExport)) return true;
  9261. if (this.isContextual(128)) {
  9262. const ch = this.lookaheadCharCode();
  9263. return isExport ? ch === 123 || ch === 42 : ch !== 61;
  9264. }
  9265. return !isExport && this.isContextual(87);
  9266. }
  9267. applyImportPhase(node, isExport, phase, loc) {
  9268. super.applyImportPhase(node, isExport, phase, loc);
  9269. if (isExport) {
  9270. node.exportKind = phase === "type" ? "type" : "value";
  9271. } else {
  9272. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  9273. }
  9274. }
  9275. parseImport(node) {
  9276. if (this.match(131)) {
  9277. node.importKind = "value";
  9278. return super.parseImport(node);
  9279. }
  9280. let importNode;
  9281. if (tokenIsIdentifier(this.state.type) && this.lookaheadCharCode() === 61) {
  9282. node.importKind = "value";
  9283. return this.tsParseImportEqualsDeclaration(node);
  9284. } else if (this.isContextual(128)) {
  9285. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
  9286. if (this.lookaheadCharCode() === 61) {
  9287. return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier);
  9288. } else {
  9289. importNode = super.parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier);
  9290. }
  9291. } else {
  9292. importNode = super.parseImport(node);
  9293. }
  9294. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9295. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  9296. at: importNode
  9297. });
  9298. }
  9299. return importNode;
  9300. }
  9301. parseExport(node, decorators) {
  9302. if (this.match(83)) {
  9303. this.next();
  9304. let maybeDefaultIdentifier = null;
  9305. if (this.isContextual(128) && this.isPotentialImportPhase(false)) {
  9306. maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
  9307. } else {
  9308. node.importKind = "value";
  9309. }
  9310. return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, true);
  9311. } else if (this.eat(29)) {
  9312. const assign = node;
  9313. assign.expression = super.parseExpression();
  9314. this.semicolon();
  9315. this.sawUnambiguousESM = true;
  9316. return this.finishNode(assign, "TSExportAssignment");
  9317. } else if (this.eatContextual(93)) {
  9318. const decl = node;
  9319. this.expectContextual(126);
  9320. decl.id = this.parseIdentifier();
  9321. this.semicolon();
  9322. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9323. } else {
  9324. return super.parseExport(node, decorators);
  9325. }
  9326. }
  9327. isAbstractClass() {
  9328. return this.isContextual(122) && this.lookahead().type === 80;
  9329. }
  9330. parseExportDefaultExpression() {
  9331. if (this.isAbstractClass()) {
  9332. const cls = this.startNode();
  9333. this.next();
  9334. cls.abstract = true;
  9335. return this.parseClass(cls, true, true);
  9336. }
  9337. if (this.match(127)) {
  9338. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9339. if (result) return result;
  9340. }
  9341. return super.parseExportDefaultExpression();
  9342. }
  9343. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9344. const {
  9345. isAmbientContext
  9346. } = this.state;
  9347. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9348. if (!isAmbientContext) return declaration;
  9349. for (const {
  9350. id,
  9351. init
  9352. } of declaration.declarations) {
  9353. if (!init) continue;
  9354. if (kind !== "const" || !!id.typeAnnotation) {
  9355. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  9356. at: init
  9357. });
  9358. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9359. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  9360. at: init
  9361. });
  9362. }
  9363. }
  9364. return declaration;
  9365. }
  9366. parseStatementContent(flags, decorators) {
  9367. if (this.match(75) && this.isLookaheadContextual("enum")) {
  9368. const node = this.startNode();
  9369. this.expect(75);
  9370. return this.tsParseEnumDeclaration(node, {
  9371. const: true
  9372. });
  9373. }
  9374. if (this.isContextual(124)) {
  9375. return this.tsParseEnumDeclaration(this.startNode());
  9376. }
  9377. if (this.isContextual(127)) {
  9378. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9379. if (result) return result;
  9380. }
  9381. return super.parseStatementContent(flags, decorators);
  9382. }
  9383. parseAccessModifier() {
  9384. return this.tsParseModifier(["public", "protected", "private"]);
  9385. }
  9386. tsHasSomeModifiers(member, modifiers) {
  9387. return modifiers.some(modifier => {
  9388. if (tsIsAccessModifier(modifier)) {
  9389. return member.accessibility === modifier;
  9390. }
  9391. return !!member[modifier];
  9392. });
  9393. }
  9394. tsIsStartOfStaticBlocks() {
  9395. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  9396. }
  9397. parseClassMember(classBody, member, state) {
  9398. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9399. this.tsParseModifiers({
  9400. allowedModifiers: modifiers,
  9401. disallowedModifiers: ["in", "out"],
  9402. stopOnStartOfClassStaticBlock: true,
  9403. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9404. }, member);
  9405. const callParseClassMemberWithIsStatic = () => {
  9406. if (this.tsIsStartOfStaticBlocks()) {
  9407. this.next();
  9408. this.next();
  9409. if (this.tsHasSomeModifiers(member, modifiers)) {
  9410. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  9411. at: this.state.curPosition()
  9412. });
  9413. }
  9414. super.parseClassStaticBlock(classBody, member);
  9415. } else {
  9416. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9417. }
  9418. };
  9419. if (member.declare) {
  9420. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9421. } else {
  9422. callParseClassMemberWithIsStatic();
  9423. }
  9424. }
  9425. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9426. const idx = this.tsTryParseIndexSignature(member);
  9427. if (idx) {
  9428. classBody.body.push(idx);
  9429. if (member.abstract) {
  9430. this.raise(TSErrors.IndexSignatureHasAbstract, {
  9431. at: member
  9432. });
  9433. }
  9434. if (member.accessibility) {
  9435. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  9436. at: member,
  9437. modifier: member.accessibility
  9438. });
  9439. }
  9440. if (member.declare) {
  9441. this.raise(TSErrors.IndexSignatureHasDeclare, {
  9442. at: member
  9443. });
  9444. }
  9445. if (member.override) {
  9446. this.raise(TSErrors.IndexSignatureHasOverride, {
  9447. at: member
  9448. });
  9449. }
  9450. return;
  9451. }
  9452. if (!this.state.inAbstractClass && member.abstract) {
  9453. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  9454. at: member
  9455. });
  9456. }
  9457. if (member.override) {
  9458. if (!state.hadSuperClass) {
  9459. this.raise(TSErrors.OverrideNotInSubClass, {
  9460. at: member
  9461. });
  9462. }
  9463. }
  9464. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9465. }
  9466. parsePostMemberNameModifiers(methodOrProp) {
  9467. const optional = this.eat(17);
  9468. if (optional) methodOrProp.optional = true;
  9469. if (methodOrProp.readonly && this.match(10)) {
  9470. this.raise(TSErrors.ClassMethodHasReadonly, {
  9471. at: methodOrProp
  9472. });
  9473. }
  9474. if (methodOrProp.declare && this.match(10)) {
  9475. this.raise(TSErrors.ClassMethodHasDeclare, {
  9476. at: methodOrProp
  9477. });
  9478. }
  9479. }
  9480. parseExpressionStatement(node, expr, decorators) {
  9481. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9482. return decl || super.parseExpressionStatement(node, expr, decorators);
  9483. }
  9484. shouldParseExportDeclaration() {
  9485. if (this.tsIsDeclarationStart()) return true;
  9486. return super.shouldParseExportDeclaration();
  9487. }
  9488. parseConditional(expr, startLoc, refExpressionErrors) {
  9489. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  9490. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9491. }
  9492. const result = this.tryParse(() => super.parseConditional(expr, startLoc));
  9493. if (!result.node) {
  9494. if (result.error) {
  9495. super.setOptionalParametersError(refExpressionErrors, result.error);
  9496. }
  9497. return expr;
  9498. }
  9499. if (result.error) this.state = result.failState;
  9500. return result.node;
  9501. }
  9502. parseParenItem(node, startLoc) {
  9503. node = super.parseParenItem(node, startLoc);
  9504. if (this.eat(17)) {
  9505. node.optional = true;
  9506. this.resetEndLocation(node);
  9507. }
  9508. if (this.match(14)) {
  9509. const typeCastNode = this.startNodeAt(startLoc);
  9510. typeCastNode.expression = node;
  9511. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9512. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9513. }
  9514. return node;
  9515. }
  9516. parseExportDeclaration(node) {
  9517. if (!this.state.isAmbientContext && this.isContextual(123)) {
  9518. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9519. }
  9520. const startLoc = this.state.startLoc;
  9521. const isDeclare = this.eatContextual(123);
  9522. if (isDeclare && (this.isContextual(123) || !this.shouldParseExportDeclaration())) {
  9523. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  9524. at: this.state.startLoc
  9525. });
  9526. }
  9527. const isIdentifier = tokenIsIdentifier(this.state.type);
  9528. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9529. if (!declaration) return null;
  9530. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9531. node.exportKind = "type";
  9532. }
  9533. if (isDeclare) {
  9534. this.resetStartLocation(declaration, startLoc);
  9535. declaration.declare = true;
  9536. }
  9537. return declaration;
  9538. }
  9539. parseClassId(node, isStatement, optionalId, bindingType) {
  9540. if ((!isStatement || optionalId) && this.isContextual(111)) {
  9541. return;
  9542. }
  9543. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  9544. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9545. if (typeParameters) node.typeParameters = typeParameters;
  9546. }
  9547. parseClassPropertyAnnotation(node) {
  9548. if (!node.optional) {
  9549. if (this.eat(35)) {
  9550. node.definite = true;
  9551. } else if (this.eat(17)) {
  9552. node.optional = true;
  9553. }
  9554. }
  9555. const type = this.tsTryParseTypeAnnotation();
  9556. if (type) node.typeAnnotation = type;
  9557. }
  9558. parseClassProperty(node) {
  9559. this.parseClassPropertyAnnotation(node);
  9560. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9561. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  9562. at: this.state.startLoc
  9563. });
  9564. }
  9565. if (node.abstract && this.match(29)) {
  9566. const {
  9567. key
  9568. } = node;
  9569. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  9570. at: this.state.startLoc,
  9571. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9572. });
  9573. }
  9574. return super.parseClassProperty(node);
  9575. }
  9576. parseClassPrivateProperty(node) {
  9577. if (node.abstract) {
  9578. this.raise(TSErrors.PrivateElementHasAbstract, {
  9579. at: node
  9580. });
  9581. }
  9582. if (node.accessibility) {
  9583. this.raise(TSErrors.PrivateElementHasAccessibility, {
  9584. at: node,
  9585. modifier: node.accessibility
  9586. });
  9587. }
  9588. this.parseClassPropertyAnnotation(node);
  9589. return super.parseClassPrivateProperty(node);
  9590. }
  9591. parseClassAccessorProperty(node) {
  9592. this.parseClassPropertyAnnotation(node);
  9593. if (node.optional) {
  9594. this.raise(TSErrors.AccessorCannotBeOptional, {
  9595. at: node
  9596. });
  9597. }
  9598. return super.parseClassAccessorProperty(node);
  9599. }
  9600. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9601. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9602. if (typeParameters && isConstructor) {
  9603. this.raise(TSErrors.ConstructorHasTypeParameters, {
  9604. at: typeParameters
  9605. });
  9606. }
  9607. const {
  9608. declare = false,
  9609. kind
  9610. } = method;
  9611. if (declare && (kind === "get" || kind === "set")) {
  9612. this.raise(TSErrors.DeclareAccessor, {
  9613. at: method,
  9614. kind
  9615. });
  9616. }
  9617. if (typeParameters) method.typeParameters = typeParameters;
  9618. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9619. }
  9620. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9621. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9622. if (typeParameters) method.typeParameters = typeParameters;
  9623. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9624. }
  9625. declareClassPrivateMethodInScope(node, kind) {
  9626. if (node.type === "TSDeclareMethod") return;
  9627. if (node.type === "MethodDefinition" && !node.value.body) return;
  9628. super.declareClassPrivateMethodInScope(node, kind);
  9629. }
  9630. parseClassSuper(node) {
  9631. super.parseClassSuper(node);
  9632. if (node.superClass && (this.match(47) || this.match(51))) {
  9633. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9634. }
  9635. if (this.eatContextual(111)) {
  9636. node.implements = this.tsParseHeritageClause("implements");
  9637. }
  9638. }
  9639. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9640. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9641. if (typeParameters) prop.typeParameters = typeParameters;
  9642. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9643. }
  9644. parseFunctionParams(node, isConstructor) {
  9645. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9646. if (typeParameters) node.typeParameters = typeParameters;
  9647. super.parseFunctionParams(node, isConstructor);
  9648. }
  9649. parseVarId(decl, kind) {
  9650. super.parseVarId(decl, kind);
  9651. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9652. decl.definite = true;
  9653. }
  9654. const type = this.tsTryParseTypeAnnotation();
  9655. if (type) {
  9656. decl.id.typeAnnotation = type;
  9657. this.resetEndLocation(decl.id);
  9658. }
  9659. }
  9660. parseAsyncArrowFromCallExpression(node, call) {
  9661. if (this.match(14)) {
  9662. node.returnType = this.tsParseTypeAnnotation();
  9663. }
  9664. return super.parseAsyncArrowFromCallExpression(node, call);
  9665. }
  9666. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9667. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2;
  9668. let state;
  9669. let jsx;
  9670. let typeCast;
  9671. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  9672. state = this.state.clone();
  9673. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9674. if (!jsx.error) return jsx.node;
  9675. const {
  9676. context
  9677. } = this.state;
  9678. const currentContext = context[context.length - 1];
  9679. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9680. context.pop();
  9681. }
  9682. }
  9683. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9684. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9685. }
  9686. if (!state || state === this.state) state = this.state.clone();
  9687. let typeParameters;
  9688. const arrow = this.tryParse(abort => {
  9689. var _expr$extra, _typeParameters;
  9690. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9691. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9692. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9693. abort();
  9694. }
  9695. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9696. this.resetStartLocationFromNode(expr, typeParameters);
  9697. }
  9698. expr.typeParameters = typeParameters;
  9699. return expr;
  9700. }, state);
  9701. if (!arrow.error && !arrow.aborted) {
  9702. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9703. return arrow.node;
  9704. }
  9705. if (!jsx) {
  9706. assert(!this.hasPlugin("jsx"));
  9707. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9708. if (!typeCast.error) return typeCast.node;
  9709. }
  9710. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9711. this.state = jsx.failState;
  9712. return jsx.node;
  9713. }
  9714. if (arrow.node) {
  9715. this.state = arrow.failState;
  9716. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9717. return arrow.node;
  9718. }
  9719. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9720. this.state = typeCast.failState;
  9721. return typeCast.node;
  9722. }
  9723. throw ((_jsx3 = jsx) == null ? void 0 : _jsx3.error) || arrow.error || ((_typeCast2 = typeCast) == null ? void 0 : _typeCast2.error);
  9724. }
  9725. reportReservedArrowTypeParam(node) {
  9726. var _node$extra;
  9727. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9728. this.raise(TSErrors.ReservedArrowTypeParam, {
  9729. at: node
  9730. });
  9731. }
  9732. }
  9733. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9734. if (!this.hasPlugin("jsx") && this.match(47)) {
  9735. return this.tsParseTypeAssertion();
  9736. }
  9737. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9738. }
  9739. parseArrow(node) {
  9740. if (this.match(14)) {
  9741. const result = this.tryParse(abort => {
  9742. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9743. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9744. return returnType;
  9745. });
  9746. if (result.aborted) return;
  9747. if (!result.thrown) {
  9748. if (result.error) this.state = result.failState;
  9749. node.returnType = result.node;
  9750. }
  9751. }
  9752. return super.parseArrow(node);
  9753. }
  9754. parseAssignableListItemTypes(param, flags) {
  9755. if (!(flags & 2)) return param;
  9756. if (this.eat(17)) {
  9757. param.optional = true;
  9758. }
  9759. const type = this.tsTryParseTypeAnnotation();
  9760. if (type) param.typeAnnotation = type;
  9761. this.resetEndLocation(param);
  9762. return param;
  9763. }
  9764. isAssignable(node, isBinding) {
  9765. switch (node.type) {
  9766. case "TSTypeCastExpression":
  9767. return this.isAssignable(node.expression, isBinding);
  9768. case "TSParameterProperty":
  9769. return true;
  9770. default:
  9771. return super.isAssignable(node, isBinding);
  9772. }
  9773. }
  9774. toAssignable(node, isLHS = false) {
  9775. switch (node.type) {
  9776. case "ParenthesizedExpression":
  9777. this.toAssignableParenthesizedExpression(node, isLHS);
  9778. break;
  9779. case "TSAsExpression":
  9780. case "TSSatisfiesExpression":
  9781. case "TSNonNullExpression":
  9782. case "TSTypeAssertion":
  9783. if (isLHS) {
  9784. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9785. at: node
  9786. });
  9787. } else {
  9788. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9789. at: node
  9790. });
  9791. }
  9792. this.toAssignable(node.expression, isLHS);
  9793. break;
  9794. case "AssignmentExpression":
  9795. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9796. node.left = this.typeCastToParameter(node.left);
  9797. }
  9798. default:
  9799. super.toAssignable(node, isLHS);
  9800. }
  9801. }
  9802. toAssignableParenthesizedExpression(node, isLHS) {
  9803. switch (node.expression.type) {
  9804. case "TSAsExpression":
  9805. case "TSSatisfiesExpression":
  9806. case "TSNonNullExpression":
  9807. case "TSTypeAssertion":
  9808. case "ParenthesizedExpression":
  9809. this.toAssignable(node.expression, isLHS);
  9810. break;
  9811. default:
  9812. super.toAssignable(node, isLHS);
  9813. }
  9814. }
  9815. checkToRestConversion(node, allowPattern) {
  9816. switch (node.type) {
  9817. case "TSAsExpression":
  9818. case "TSSatisfiesExpression":
  9819. case "TSTypeAssertion":
  9820. case "TSNonNullExpression":
  9821. this.checkToRestConversion(node.expression, false);
  9822. break;
  9823. default:
  9824. super.checkToRestConversion(node, allowPattern);
  9825. }
  9826. }
  9827. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9828. return getOwn({
  9829. TSTypeCastExpression: true,
  9830. TSParameterProperty: "parameter",
  9831. TSNonNullExpression: "expression",
  9832. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9833. TSSatisfiesExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9834. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9835. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9836. }
  9837. parseBindingAtom() {
  9838. if (this.state.type === 78) {
  9839. return this.parseIdentifier(true);
  9840. }
  9841. return super.parseBindingAtom();
  9842. }
  9843. parseMaybeDecoratorArguments(expr) {
  9844. if (this.match(47) || this.match(51)) {
  9845. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9846. if (this.match(10)) {
  9847. const call = super.parseMaybeDecoratorArguments(expr);
  9848. call.typeParameters = typeArguments;
  9849. return call;
  9850. }
  9851. this.unexpected(null, 10);
  9852. }
  9853. return super.parseMaybeDecoratorArguments(expr);
  9854. }
  9855. checkCommaAfterRest(close) {
  9856. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9857. this.next();
  9858. return false;
  9859. }
  9860. return super.checkCommaAfterRest(close);
  9861. }
  9862. isClassMethod() {
  9863. return this.match(47) || super.isClassMethod();
  9864. }
  9865. isClassProperty() {
  9866. return this.match(35) || this.match(14) || super.isClassProperty();
  9867. }
  9868. parseMaybeDefault(startLoc, left) {
  9869. const node = super.parseMaybeDefault(startLoc, left);
  9870. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9871. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9872. at: node.typeAnnotation
  9873. });
  9874. }
  9875. return node;
  9876. }
  9877. getTokenFromCode(code) {
  9878. if (this.state.inType) {
  9879. if (code === 62) {
  9880. this.finishOp(48, 1);
  9881. return;
  9882. }
  9883. if (code === 60) {
  9884. this.finishOp(47, 1);
  9885. return;
  9886. }
  9887. }
  9888. super.getTokenFromCode(code);
  9889. }
  9890. reScan_lt_gt() {
  9891. const {
  9892. type
  9893. } = this.state;
  9894. if (type === 47) {
  9895. this.state.pos -= 1;
  9896. this.readToken_lt();
  9897. } else if (type === 48) {
  9898. this.state.pos -= 1;
  9899. this.readToken_gt();
  9900. }
  9901. }
  9902. reScan_lt() {
  9903. const {
  9904. type
  9905. } = this.state;
  9906. if (type === 51) {
  9907. this.state.pos -= 2;
  9908. this.finishOp(47, 1);
  9909. return 47;
  9910. }
  9911. return type;
  9912. }
  9913. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9914. for (let i = 0; i < exprList.length; i++) {
  9915. const expr = exprList[i];
  9916. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9917. exprList[i] = this.typeCastToParameter(expr);
  9918. }
  9919. }
  9920. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9921. }
  9922. typeCastToParameter(node) {
  9923. node.expression.typeAnnotation = node.typeAnnotation;
  9924. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9925. return node.expression;
  9926. }
  9927. shouldParseArrow(params) {
  9928. if (this.match(14)) {
  9929. return params.every(expr => this.isAssignable(expr, true));
  9930. }
  9931. return super.shouldParseArrow(params);
  9932. }
  9933. shouldParseAsyncArrow() {
  9934. return this.match(14) || super.shouldParseAsyncArrow();
  9935. }
  9936. canHaveLeadingDecorator() {
  9937. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9938. }
  9939. jsxParseOpeningElementAfterName(node) {
  9940. if (this.match(47) || this.match(51)) {
  9941. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9942. if (typeArguments) node.typeParameters = typeArguments;
  9943. }
  9944. return super.jsxParseOpeningElementAfterName(node);
  9945. }
  9946. getGetterSetterExpectedParamCount(method) {
  9947. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9948. const params = this.getObjectOrClassMethodParams(method);
  9949. const firstParam = params[0];
  9950. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9951. return hasContextParam ? baseCount + 1 : baseCount;
  9952. }
  9953. parseCatchClauseParam() {
  9954. const param = super.parseCatchClauseParam();
  9955. const type = this.tsTryParseTypeAnnotation();
  9956. if (type) {
  9957. param.typeAnnotation = type;
  9958. this.resetEndLocation(param);
  9959. }
  9960. return param;
  9961. }
  9962. tsInAmbientContext(cb) {
  9963. const oldIsAmbientContext = this.state.isAmbientContext;
  9964. this.state.isAmbientContext = true;
  9965. try {
  9966. return cb();
  9967. } finally {
  9968. this.state.isAmbientContext = oldIsAmbientContext;
  9969. }
  9970. }
  9971. parseClass(node, isStatement, optionalId) {
  9972. const oldInAbstractClass = this.state.inAbstractClass;
  9973. this.state.inAbstractClass = !!node.abstract;
  9974. try {
  9975. return super.parseClass(node, isStatement, optionalId);
  9976. } finally {
  9977. this.state.inAbstractClass = oldInAbstractClass;
  9978. }
  9979. }
  9980. tsParseAbstractDeclaration(node, decorators) {
  9981. if (this.match(80)) {
  9982. node.abstract = true;
  9983. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  9984. } else if (this.isContextual(127)) {
  9985. if (!this.hasFollowingLineBreak()) {
  9986. node.abstract = true;
  9987. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9988. at: node
  9989. });
  9990. return this.tsParseInterfaceDeclaration(node);
  9991. }
  9992. } else {
  9993. this.unexpected(null, 80);
  9994. }
  9995. }
  9996. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9997. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9998. if (method.abstract) {
  9999. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  10000. if (hasBody) {
  10001. const {
  10002. key
  10003. } = method;
  10004. this.raise(TSErrors.AbstractMethodHasImplementation, {
  10005. at: method,
  10006. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  10007. });
  10008. }
  10009. }
  10010. return method;
  10011. }
  10012. tsParseTypeParameterName() {
  10013. const typeName = this.parseIdentifier();
  10014. return typeName.name;
  10015. }
  10016. shouldParseAsAmbientContext() {
  10017. return !!this.getPluginOption("typescript", "dts");
  10018. }
  10019. parse() {
  10020. if (this.shouldParseAsAmbientContext()) {
  10021. this.state.isAmbientContext = true;
  10022. }
  10023. return super.parse();
  10024. }
  10025. getExpression() {
  10026. if (this.shouldParseAsAmbientContext()) {
  10027. this.state.isAmbientContext = true;
  10028. }
  10029. return super.getExpression();
  10030. }
  10031. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  10032. if (!isString && isMaybeTypeOnly) {
  10033. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  10034. return this.finishNode(node, "ExportSpecifier");
  10035. }
  10036. node.exportKind = "value";
  10037. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  10038. }
  10039. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  10040. if (!importedIsString && isMaybeTypeOnly) {
  10041. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  10042. return this.finishNode(specifier, "ImportSpecifier");
  10043. }
  10044. specifier.importKind = "value";
  10045. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  10046. }
  10047. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  10048. const leftOfAsKey = isImport ? "imported" : "local";
  10049. const rightOfAsKey = isImport ? "local" : "exported";
  10050. let leftOfAs = node[leftOfAsKey];
  10051. let rightOfAs;
  10052. let hasTypeSpecifier = false;
  10053. let canParseAsKeyword = true;
  10054. const loc = leftOfAs.loc.start;
  10055. if (this.isContextual(93)) {
  10056. const firstAs = this.parseIdentifier();
  10057. if (this.isContextual(93)) {
  10058. const secondAs = this.parseIdentifier();
  10059. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10060. hasTypeSpecifier = true;
  10061. leftOfAs = firstAs;
  10062. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10063. canParseAsKeyword = false;
  10064. } else {
  10065. rightOfAs = secondAs;
  10066. canParseAsKeyword = false;
  10067. }
  10068. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10069. canParseAsKeyword = false;
  10070. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10071. } else {
  10072. hasTypeSpecifier = true;
  10073. leftOfAs = firstAs;
  10074. }
  10075. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10076. hasTypeSpecifier = true;
  10077. if (isImport) {
  10078. leftOfAs = this.parseIdentifier(true);
  10079. if (!this.isContextual(93)) {
  10080. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  10081. }
  10082. } else {
  10083. leftOfAs = this.parseModuleExportName();
  10084. }
  10085. }
  10086. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  10087. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  10088. at: loc
  10089. });
  10090. }
  10091. node[leftOfAsKey] = leftOfAs;
  10092. node[rightOfAsKey] = rightOfAs;
  10093. const kindKey = isImport ? "importKind" : "exportKind";
  10094. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  10095. if (canParseAsKeyword && this.eatContextual(93)) {
  10096. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10097. }
  10098. if (!node[rightOfAsKey]) {
  10099. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  10100. }
  10101. if (isImport) {
  10102. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  10103. }
  10104. }
  10105. };
  10106. function isPossiblyLiteralEnum(expression) {
  10107. if (expression.type !== "MemberExpression") return false;
  10108. const {
  10109. computed,
  10110. property
  10111. } = expression;
  10112. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  10113. return false;
  10114. }
  10115. return isUncomputedMemberExpressionChain(expression.object);
  10116. }
  10117. function isValidAmbientConstInitializer(expression, estree) {
  10118. var _expression$extra;
  10119. const {
  10120. type
  10121. } = expression;
  10122. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  10123. return false;
  10124. }
  10125. if (estree) {
  10126. if (type === "Literal") {
  10127. const {
  10128. value
  10129. } = expression;
  10130. if (typeof value === "string" || typeof value === "boolean") {
  10131. return true;
  10132. }
  10133. }
  10134. } else {
  10135. if (type === "StringLiteral" || type === "BooleanLiteral") {
  10136. return true;
  10137. }
  10138. }
  10139. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  10140. return true;
  10141. }
  10142. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  10143. return true;
  10144. }
  10145. if (isPossiblyLiteralEnum(expression)) {
  10146. return true;
  10147. }
  10148. return false;
  10149. }
  10150. function isNumber(expression, estree) {
  10151. if (estree) {
  10152. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  10153. }
  10154. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  10155. }
  10156. function isNegativeNumber(expression, estree) {
  10157. if (expression.type === "UnaryExpression") {
  10158. const {
  10159. operator,
  10160. argument
  10161. } = expression;
  10162. if (operator === "-" && isNumber(argument, estree)) {
  10163. return true;
  10164. }
  10165. }
  10166. return false;
  10167. }
  10168. function isUncomputedMemberExpressionChain(expression) {
  10169. if (expression.type === "Identifier") return true;
  10170. if (expression.type !== "MemberExpression" || expression.computed) {
  10171. return false;
  10172. }
  10173. return isUncomputedMemberExpressionChain(expression.object);
  10174. }
  10175. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  10176. ClassNameIsRequired: "A class name is required.",
  10177. UnexpectedSpace: "Unexpected space in placeholder."
  10178. });
  10179. var placeholders = superClass => class PlaceholdersParserMixin extends superClass {
  10180. parsePlaceholder(expectedNode) {
  10181. if (this.match(142)) {
  10182. const node = this.startNode();
  10183. this.next();
  10184. this.assertNoSpace();
  10185. node.name = super.parseIdentifier(true);
  10186. this.assertNoSpace();
  10187. this.expect(142);
  10188. return this.finishPlaceholder(node, expectedNode);
  10189. }
  10190. }
  10191. finishPlaceholder(node, expectedNode) {
  10192. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  10193. node.expectedNode = expectedNode;
  10194. return isFinished ? node : this.finishNode(node, "Placeholder");
  10195. }
  10196. getTokenFromCode(code) {
  10197. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  10198. this.finishOp(142, 2);
  10199. } else {
  10200. super.getTokenFromCode(code);
  10201. }
  10202. }
  10203. parseExprAtom(refExpressionErrors) {
  10204. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  10205. }
  10206. parseIdentifier(liberal) {
  10207. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  10208. }
  10209. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10210. if (word !== undefined) {
  10211. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  10212. }
  10213. }
  10214. parseBindingAtom() {
  10215. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  10216. }
  10217. isValidLVal(type, isParenthesized, binding) {
  10218. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  10219. }
  10220. toAssignable(node, isLHS) {
  10221. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  10222. node.expectedNode = "Pattern";
  10223. } else {
  10224. super.toAssignable(node, isLHS);
  10225. }
  10226. }
  10227. chStartsBindingIdentifier(ch, pos) {
  10228. if (super.chStartsBindingIdentifier(ch, pos)) {
  10229. return true;
  10230. }
  10231. const nextToken = this.lookahead();
  10232. if (nextToken.type === 142) {
  10233. return true;
  10234. }
  10235. return false;
  10236. }
  10237. verifyBreakContinue(node, isBreak) {
  10238. if (node.label && node.label.type === "Placeholder") return;
  10239. super.verifyBreakContinue(node, isBreak);
  10240. }
  10241. parseExpressionStatement(node, expr) {
  10242. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  10243. return super.parseExpressionStatement(node, expr);
  10244. }
  10245. if (this.match(14)) {
  10246. const stmt = node;
  10247. stmt.label = this.finishPlaceholder(expr, "Identifier");
  10248. this.next();
  10249. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  10250. return this.finishNode(stmt, "LabeledStatement");
  10251. }
  10252. this.semicolon();
  10253. node.name = expr.name;
  10254. return this.finishPlaceholder(node, "Statement");
  10255. }
  10256. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  10257. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  10258. }
  10259. parseFunctionId(requireId) {
  10260. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  10261. }
  10262. parseClass(node, isStatement, optionalId) {
  10263. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  10264. this.next();
  10265. const oldStrict = this.state.strict;
  10266. const placeholder = this.parsePlaceholder("Identifier");
  10267. if (placeholder) {
  10268. if (this.match(81) || this.match(142) || this.match(5)) {
  10269. node.id = placeholder;
  10270. } else if (optionalId || !isStatement) {
  10271. node.id = null;
  10272. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  10273. return this.finishNode(node, type);
  10274. } else {
  10275. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  10276. at: this.state.startLoc
  10277. });
  10278. }
  10279. } else {
  10280. this.parseClassId(node, isStatement, optionalId);
  10281. }
  10282. super.parseClassSuper(node);
  10283. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  10284. return this.finishNode(node, type);
  10285. }
  10286. parseExport(node, decorators) {
  10287. const placeholder = this.parsePlaceholder("Identifier");
  10288. if (!placeholder) return super.parseExport(node, decorators);
  10289. if (!this.isContextual(97) && !this.match(12)) {
  10290. node.specifiers = [];
  10291. node.source = null;
  10292. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  10293. return this.finishNode(node, "ExportNamedDeclaration");
  10294. }
  10295. this.expectPlugin("exportDefaultFrom");
  10296. const specifier = this.startNode();
  10297. specifier.exported = placeholder;
  10298. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  10299. return super.parseExport(node, decorators);
  10300. }
  10301. isExportDefaultSpecifier() {
  10302. if (this.match(65)) {
  10303. const next = this.nextTokenStart();
  10304. if (this.isUnparsedContextual(next, "from")) {
  10305. if (this.input.startsWith(tokenLabelName(142), this.nextTokenStartSince(next + 4))) {
  10306. return true;
  10307. }
  10308. }
  10309. }
  10310. return super.isExportDefaultSpecifier();
  10311. }
  10312. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  10313. var _specifiers;
  10314. if ((_specifiers = node.specifiers) != null && _specifiers.length) {
  10315. return true;
  10316. }
  10317. return super.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  10318. }
  10319. checkExport(node) {
  10320. const {
  10321. specifiers
  10322. } = node;
  10323. if (specifiers != null && specifiers.length) {
  10324. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10325. }
  10326. super.checkExport(node);
  10327. node.specifiers = specifiers;
  10328. }
  10329. parseImport(node) {
  10330. const placeholder = this.parsePlaceholder("Identifier");
  10331. if (!placeholder) return super.parseImport(node);
  10332. node.specifiers = [];
  10333. if (!this.isContextual(97) && !this.match(12)) {
  10334. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10335. this.semicolon();
  10336. return this.finishNode(node, "ImportDeclaration");
  10337. }
  10338. const specifier = this.startNodeAtNode(placeholder);
  10339. specifier.local = placeholder;
  10340. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10341. if (this.eat(12)) {
  10342. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10343. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10344. }
  10345. this.expectContextual(97);
  10346. node.source = this.parseImportSource();
  10347. this.semicolon();
  10348. return this.finishNode(node, "ImportDeclaration");
  10349. }
  10350. parseImportSource() {
  10351. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10352. }
  10353. assertNoSpace() {
  10354. if (this.state.start > this.state.lastTokEndLoc.index) {
  10355. this.raise(PlaceholderErrors.UnexpectedSpace, {
  10356. at: this.state.lastTokEndLoc
  10357. });
  10358. }
  10359. }
  10360. };
  10361. var v8intrinsic = superClass => class V8IntrinsicMixin extends superClass {
  10362. parseV8Intrinsic() {
  10363. if (this.match(54)) {
  10364. const v8IntrinsicStartLoc = this.state.startLoc;
  10365. const node = this.startNode();
  10366. this.next();
  10367. if (tokenIsIdentifier(this.state.type)) {
  10368. const name = this.parseIdentifierName();
  10369. const identifier = this.createIdentifier(node, name);
  10370. identifier.type = "V8IntrinsicIdentifier";
  10371. if (this.match(10)) {
  10372. return identifier;
  10373. }
  10374. }
  10375. this.unexpected(v8IntrinsicStartLoc);
  10376. }
  10377. }
  10378. parseExprAtom(refExpressionErrors) {
  10379. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10380. }
  10381. };
  10382. function hasPlugin(plugins, expectedConfig) {
  10383. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  10384. const expectedKeys = Object.keys(expectedOptions);
  10385. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  10386. return plugins.some(p => {
  10387. if (typeof p === "string") {
  10388. return expectedOptionsIsEmpty && p === expectedName;
  10389. } else {
  10390. const [pluginName, pluginOptions] = p;
  10391. if (pluginName !== expectedName) {
  10392. return false;
  10393. }
  10394. for (const key of expectedKeys) {
  10395. if (pluginOptions[key] !== expectedOptions[key]) {
  10396. return false;
  10397. }
  10398. }
  10399. return true;
  10400. }
  10401. });
  10402. }
  10403. function getPluginOption(plugins, name, option) {
  10404. const plugin = plugins.find(plugin => {
  10405. if (Array.isArray(plugin)) {
  10406. return plugin[0] === name;
  10407. } else {
  10408. return plugin === name;
  10409. }
  10410. });
  10411. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  10412. return plugin[1][option];
  10413. }
  10414. return null;
  10415. }
  10416. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10417. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10418. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10419. function validatePlugins(plugins) {
  10420. if (hasPlugin(plugins, "decorators")) {
  10421. if (hasPlugin(plugins, "decorators-legacy")) {
  10422. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10423. }
  10424. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  10425. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10426. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10427. }
  10428. const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
  10429. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10430. throw new Error("'allowCallParenthesized' must be a boolean.");
  10431. }
  10432. }
  10433. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  10434. throw new Error("Cannot combine flow and typescript plugins.");
  10435. }
  10436. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  10437. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10438. }
  10439. if (hasPlugin(plugins, "pipelineOperator")) {
  10440. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  10441. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10442. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10443. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10444. }
  10445. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  10446. syntaxType: "hash"
  10447. }]);
  10448. if (proposal === "hack") {
  10449. if (hasPlugin(plugins, "placeholders")) {
  10450. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10451. }
  10452. if (hasPlugin(plugins, "v8intrinsic")) {
  10453. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10454. }
  10455. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  10456. if (!TOPIC_TOKENS.includes(topicToken)) {
  10457. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10458. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10459. }
  10460. if (topicToken === "#" && tupleSyntaxIsHash) {
  10461. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10462. }
  10463. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  10464. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  10465. }
  10466. }
  10467. if (hasPlugin(plugins, "moduleAttributes")) {
  10468. {
  10469. if (hasPlugin(plugins, "importAssertions") || hasPlugin(plugins, "importAttributes")) {
  10470. throw new Error("Cannot combine importAssertions, importAttributes and moduleAttributes plugins.");
  10471. }
  10472. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  10473. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10474. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10475. }
  10476. }
  10477. }
  10478. if (hasPlugin(plugins, "importAssertions") && hasPlugin(plugins, "importAttributes")) {
  10479. throw new Error("Cannot combine importAssertions and importAttributes plugins.");
  10480. }
  10481. if (hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") != null && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  10482. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10483. }
  10484. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  10485. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10486. error.missingPlugins = "doExpressions";
  10487. throw error;
  10488. }
  10489. }
  10490. const mixinPlugins = {
  10491. estree,
  10492. jsx,
  10493. flow,
  10494. typescript,
  10495. v8intrinsic,
  10496. placeholders
  10497. };
  10498. const mixinPluginNames = Object.keys(mixinPlugins);
  10499. const defaultOptions = {
  10500. sourceType: "script",
  10501. sourceFilename: undefined,
  10502. startColumn: 0,
  10503. startLine: 1,
  10504. allowAwaitOutsideFunction: false,
  10505. allowReturnOutsideFunction: false,
  10506. allowNewTargetOutsideFunction: false,
  10507. allowImportExportEverywhere: false,
  10508. allowSuperOutsideMethod: false,
  10509. allowUndeclaredExports: false,
  10510. plugins: [],
  10511. strictMode: null,
  10512. ranges: false,
  10513. tokens: false,
  10514. createParenthesizedExpressions: false,
  10515. errorRecovery: false,
  10516. attachComment: true,
  10517. annexB: true
  10518. };
  10519. function getOptions(opts) {
  10520. if (opts && opts.annexB != null && opts.annexB !== false) {
  10521. throw new Error("The `annexB` option can only be set to `false`.");
  10522. }
  10523. const options = {};
  10524. for (const key of Object.keys(defaultOptions)) {
  10525. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  10526. }
  10527. return options;
  10528. }
  10529. class ExpressionParser extends LValParser {
  10530. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10531. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10532. return;
  10533. }
  10534. const key = prop.key;
  10535. const name = key.type === "Identifier" ? key.name : key.value;
  10536. if (name === "__proto__") {
  10537. if (isRecord) {
  10538. this.raise(Errors.RecordNoProto, {
  10539. at: key
  10540. });
  10541. return;
  10542. }
  10543. if (protoRef.used) {
  10544. if (refExpressionErrors) {
  10545. if (refExpressionErrors.doubleProtoLoc === null) {
  10546. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10547. }
  10548. } else {
  10549. this.raise(Errors.DuplicateProto, {
  10550. at: key
  10551. });
  10552. }
  10553. }
  10554. protoRef.used = true;
  10555. }
  10556. }
  10557. shouldExitDescending(expr, potentialArrowAt) {
  10558. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10559. }
  10560. getExpression() {
  10561. this.enterInitialScopes();
  10562. this.nextToken();
  10563. const expr = this.parseExpression();
  10564. if (!this.match(137)) {
  10565. this.unexpected();
  10566. }
  10567. this.finalizeRemainingComments();
  10568. expr.comments = this.state.comments;
  10569. expr.errors = this.state.errors;
  10570. if (this.options.tokens) {
  10571. expr.tokens = this.tokens;
  10572. }
  10573. return expr;
  10574. }
  10575. parseExpression(disallowIn, refExpressionErrors) {
  10576. if (disallowIn) {
  10577. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10578. }
  10579. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10580. }
  10581. parseExpressionBase(refExpressionErrors) {
  10582. const startLoc = this.state.startLoc;
  10583. const expr = this.parseMaybeAssign(refExpressionErrors);
  10584. if (this.match(12)) {
  10585. const node = this.startNodeAt(startLoc);
  10586. node.expressions = [expr];
  10587. while (this.eat(12)) {
  10588. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10589. }
  10590. this.toReferencedList(node.expressions);
  10591. return this.finishNode(node, "SequenceExpression");
  10592. }
  10593. return expr;
  10594. }
  10595. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10596. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10597. }
  10598. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10599. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10600. }
  10601. setOptionalParametersError(refExpressionErrors, resultError) {
  10602. var _resultError$loc;
  10603. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10604. }
  10605. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10606. const startLoc = this.state.startLoc;
  10607. if (this.isContextual(106)) {
  10608. if (this.prodParam.hasYield) {
  10609. let left = this.parseYield();
  10610. if (afterLeftParse) {
  10611. left = afterLeftParse.call(this, left, startLoc);
  10612. }
  10613. return left;
  10614. }
  10615. }
  10616. let ownExpressionErrors;
  10617. if (refExpressionErrors) {
  10618. ownExpressionErrors = false;
  10619. } else {
  10620. refExpressionErrors = new ExpressionErrors();
  10621. ownExpressionErrors = true;
  10622. }
  10623. const {
  10624. type
  10625. } = this.state;
  10626. if (type === 10 || tokenIsIdentifier(type)) {
  10627. this.state.potentialArrowAt = this.state.start;
  10628. }
  10629. let left = this.parseMaybeConditional(refExpressionErrors);
  10630. if (afterLeftParse) {
  10631. left = afterLeftParse.call(this, left, startLoc);
  10632. }
  10633. if (tokenIsAssignment(this.state.type)) {
  10634. const node = this.startNodeAt(startLoc);
  10635. const operator = this.state.value;
  10636. node.operator = operator;
  10637. if (this.match(29)) {
  10638. this.toAssignable(left, true);
  10639. node.left = left;
  10640. const startIndex = startLoc.index;
  10641. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10642. refExpressionErrors.doubleProtoLoc = null;
  10643. }
  10644. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10645. refExpressionErrors.shorthandAssignLoc = null;
  10646. }
  10647. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10648. this.checkDestructuringPrivate(refExpressionErrors);
  10649. refExpressionErrors.privateKeyLoc = null;
  10650. }
  10651. } else {
  10652. node.left = left;
  10653. }
  10654. this.next();
  10655. node.right = this.parseMaybeAssign();
  10656. this.checkLVal(left, {
  10657. in: this.finishNode(node, "AssignmentExpression")
  10658. });
  10659. return node;
  10660. } else if (ownExpressionErrors) {
  10661. this.checkExpressionErrors(refExpressionErrors, true);
  10662. }
  10663. return left;
  10664. }
  10665. parseMaybeConditional(refExpressionErrors) {
  10666. const startLoc = this.state.startLoc;
  10667. const potentialArrowAt = this.state.potentialArrowAt;
  10668. const expr = this.parseExprOps(refExpressionErrors);
  10669. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10670. return expr;
  10671. }
  10672. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10673. }
  10674. parseConditional(expr, startLoc, refExpressionErrors) {
  10675. if (this.eat(17)) {
  10676. const node = this.startNodeAt(startLoc);
  10677. node.test = expr;
  10678. node.consequent = this.parseMaybeAssignAllowIn();
  10679. this.expect(14);
  10680. node.alternate = this.parseMaybeAssign();
  10681. return this.finishNode(node, "ConditionalExpression");
  10682. }
  10683. return expr;
  10684. }
  10685. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10686. return this.match(136) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10687. }
  10688. parseExprOps(refExpressionErrors) {
  10689. const startLoc = this.state.startLoc;
  10690. const potentialArrowAt = this.state.potentialArrowAt;
  10691. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10692. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10693. return expr;
  10694. }
  10695. return this.parseExprOp(expr, startLoc, -1);
  10696. }
  10697. parseExprOp(left, leftStartLoc, minPrec) {
  10698. if (this.isPrivateName(left)) {
  10699. const value = this.getPrivateNameSV(left);
  10700. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10701. this.raise(Errors.PrivateInExpectedIn, {
  10702. at: left,
  10703. identifierName: value
  10704. });
  10705. }
  10706. this.classScope.usePrivateName(value, left.loc.start);
  10707. }
  10708. const op = this.state.type;
  10709. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10710. let prec = tokenOperatorPrecedence(op);
  10711. if (prec > minPrec) {
  10712. if (op === 39) {
  10713. this.expectPlugin("pipelineOperator");
  10714. if (this.state.inFSharpPipelineDirectBody) {
  10715. return left;
  10716. }
  10717. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10718. }
  10719. const node = this.startNodeAt(leftStartLoc);
  10720. node.left = left;
  10721. node.operator = this.state.value;
  10722. const logical = op === 41 || op === 42;
  10723. const coalesce = op === 40;
  10724. if (coalesce) {
  10725. prec = tokenOperatorPrecedence(42);
  10726. }
  10727. this.next();
  10728. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10729. proposal: "minimal"
  10730. }])) {
  10731. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10732. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10733. at: this.state.startLoc
  10734. });
  10735. }
  10736. }
  10737. node.right = this.parseExprOpRightExpr(op, prec);
  10738. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10739. const nextOp = this.state.type;
  10740. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10741. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10742. at: this.state.startLoc
  10743. });
  10744. }
  10745. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10746. }
  10747. }
  10748. return left;
  10749. }
  10750. parseExprOpRightExpr(op, prec) {
  10751. const startLoc = this.state.startLoc;
  10752. switch (op) {
  10753. case 39:
  10754. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10755. case "hack":
  10756. return this.withTopicBindingContext(() => {
  10757. return this.parseHackPipeBody();
  10758. });
  10759. case "smart":
  10760. return this.withTopicBindingContext(() => {
  10761. if (this.prodParam.hasYield && this.isContextual(106)) {
  10762. throw this.raise(Errors.PipeBodyIsTighter, {
  10763. at: this.state.startLoc
  10764. });
  10765. }
  10766. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10767. });
  10768. case "fsharp":
  10769. return this.withSoloAwaitPermittingContext(() => {
  10770. return this.parseFSharpPipelineBody(prec);
  10771. });
  10772. }
  10773. default:
  10774. return this.parseExprOpBaseRightExpr(op, prec);
  10775. }
  10776. }
  10777. parseExprOpBaseRightExpr(op, prec) {
  10778. const startLoc = this.state.startLoc;
  10779. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10780. }
  10781. parseHackPipeBody() {
  10782. var _body$extra;
  10783. const {
  10784. startLoc
  10785. } = this.state;
  10786. const body = this.parseMaybeAssign();
  10787. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10788. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10789. this.raise(Errors.PipeUnparenthesizedBody, {
  10790. at: startLoc,
  10791. type: body.type
  10792. });
  10793. }
  10794. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10795. this.raise(Errors.PipeTopicUnused, {
  10796. at: startLoc
  10797. });
  10798. }
  10799. return body;
  10800. }
  10801. checkExponentialAfterUnary(node) {
  10802. if (this.match(57)) {
  10803. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10804. at: node.argument
  10805. });
  10806. }
  10807. }
  10808. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10809. const startLoc = this.state.startLoc;
  10810. const isAwait = this.isContextual(96);
  10811. if (isAwait && this.isAwaitAllowed()) {
  10812. this.next();
  10813. const expr = this.parseAwait(startLoc);
  10814. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10815. return expr;
  10816. }
  10817. const update = this.match(34);
  10818. const node = this.startNode();
  10819. if (tokenIsPrefix(this.state.type)) {
  10820. node.operator = this.state.value;
  10821. node.prefix = true;
  10822. if (this.match(72)) {
  10823. this.expectPlugin("throwExpressions");
  10824. }
  10825. const isDelete = this.match(89);
  10826. this.next();
  10827. node.argument = this.parseMaybeUnary(null, true);
  10828. this.checkExpressionErrors(refExpressionErrors, true);
  10829. if (this.state.strict && isDelete) {
  10830. const arg = node.argument;
  10831. if (arg.type === "Identifier") {
  10832. this.raise(Errors.StrictDelete, {
  10833. at: node
  10834. });
  10835. } else if (this.hasPropertyAsPrivateName(arg)) {
  10836. this.raise(Errors.DeletePrivateField, {
  10837. at: node
  10838. });
  10839. }
  10840. }
  10841. if (!update) {
  10842. if (!sawUnary) {
  10843. this.checkExponentialAfterUnary(node);
  10844. }
  10845. return this.finishNode(node, "UnaryExpression");
  10846. }
  10847. }
  10848. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10849. if (isAwait) {
  10850. const {
  10851. type
  10852. } = this.state;
  10853. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10854. if (startsExpr && !this.isAmbiguousAwait()) {
  10855. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10856. at: startLoc
  10857. });
  10858. return this.parseAwait(startLoc);
  10859. }
  10860. }
  10861. return expr;
  10862. }
  10863. parseUpdate(node, update, refExpressionErrors) {
  10864. if (update) {
  10865. const updateExpressionNode = node;
  10866. this.checkLVal(updateExpressionNode.argument, {
  10867. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10868. });
  10869. return node;
  10870. }
  10871. const startLoc = this.state.startLoc;
  10872. let expr = this.parseExprSubscripts(refExpressionErrors);
  10873. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10874. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10875. const node = this.startNodeAt(startLoc);
  10876. node.operator = this.state.value;
  10877. node.prefix = false;
  10878. node.argument = expr;
  10879. this.next();
  10880. this.checkLVal(expr, {
  10881. in: expr = this.finishNode(node, "UpdateExpression")
  10882. });
  10883. }
  10884. return expr;
  10885. }
  10886. parseExprSubscripts(refExpressionErrors) {
  10887. const startLoc = this.state.startLoc;
  10888. const potentialArrowAt = this.state.potentialArrowAt;
  10889. const expr = this.parseExprAtom(refExpressionErrors);
  10890. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10891. return expr;
  10892. }
  10893. return this.parseSubscripts(expr, startLoc);
  10894. }
  10895. parseSubscripts(base, startLoc, noCalls) {
  10896. const state = {
  10897. optionalChainMember: false,
  10898. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10899. stop: false
  10900. };
  10901. do {
  10902. base = this.parseSubscript(base, startLoc, noCalls, state);
  10903. state.maybeAsyncArrow = false;
  10904. } while (!state.stop);
  10905. return base;
  10906. }
  10907. parseSubscript(base, startLoc, noCalls, state) {
  10908. const {
  10909. type
  10910. } = this.state;
  10911. if (!noCalls && type === 15) {
  10912. return this.parseBind(base, startLoc, noCalls, state);
  10913. } else if (tokenIsTemplate(type)) {
  10914. return this.parseTaggedTemplateExpression(base, startLoc, state);
  10915. }
  10916. let optional = false;
  10917. if (type === 18) {
  10918. if (noCalls) {
  10919. this.raise(Errors.OptionalChainingNoNew, {
  10920. at: this.state.startLoc
  10921. });
  10922. if (this.lookaheadCharCode() === 40) {
  10923. state.stop = true;
  10924. return base;
  10925. }
  10926. }
  10927. state.optionalChainMember = optional = true;
  10928. this.next();
  10929. }
  10930. if (!noCalls && this.match(10)) {
  10931. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  10932. } else {
  10933. const computed = this.eat(0);
  10934. if (computed || optional || this.eat(16)) {
  10935. return this.parseMember(base, startLoc, state, computed, optional);
  10936. } else {
  10937. state.stop = true;
  10938. return base;
  10939. }
  10940. }
  10941. }
  10942. parseMember(base, startLoc, state, computed, optional) {
  10943. const node = this.startNodeAt(startLoc);
  10944. node.object = base;
  10945. node.computed = computed;
  10946. if (computed) {
  10947. node.property = this.parseExpression();
  10948. this.expect(3);
  10949. } else if (this.match(136)) {
  10950. if (base.type === "Super") {
  10951. this.raise(Errors.SuperPrivateField, {
  10952. at: startLoc
  10953. });
  10954. }
  10955. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10956. node.property = this.parsePrivateName();
  10957. } else {
  10958. node.property = this.parseIdentifier(true);
  10959. }
  10960. if (state.optionalChainMember) {
  10961. node.optional = optional;
  10962. return this.finishNode(node, "OptionalMemberExpression");
  10963. } else {
  10964. return this.finishNode(node, "MemberExpression");
  10965. }
  10966. }
  10967. parseBind(base, startLoc, noCalls, state) {
  10968. const node = this.startNodeAt(startLoc);
  10969. node.object = base;
  10970. this.next();
  10971. node.callee = this.parseNoCallExpr();
  10972. state.stop = true;
  10973. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  10974. }
  10975. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  10976. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10977. let refExpressionErrors = null;
  10978. this.state.maybeInArrowParameters = true;
  10979. this.next();
  10980. const node = this.startNodeAt(startLoc);
  10981. node.callee = base;
  10982. const {
  10983. maybeAsyncArrow,
  10984. optionalChainMember
  10985. } = state;
  10986. if (maybeAsyncArrow) {
  10987. this.expressionScope.enter(newAsyncArrowScope());
  10988. refExpressionErrors = new ExpressionErrors();
  10989. }
  10990. if (optionalChainMember) {
  10991. node.optional = optional;
  10992. }
  10993. if (optional) {
  10994. node.arguments = this.parseCallExpressionArguments(11);
  10995. } else {
  10996. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10997. }
  10998. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10999. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  11000. state.stop = true;
  11001. this.checkDestructuringPrivate(refExpressionErrors);
  11002. this.expressionScope.validateAsPattern();
  11003. this.expressionScope.exit();
  11004. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  11005. } else {
  11006. if (maybeAsyncArrow) {
  11007. this.checkExpressionErrors(refExpressionErrors, true);
  11008. this.expressionScope.exit();
  11009. }
  11010. this.toReferencedArguments(finishedNode);
  11011. }
  11012. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11013. return finishedNode;
  11014. }
  11015. toReferencedArguments(node, isParenthesizedExpr) {
  11016. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  11017. }
  11018. parseTaggedTemplateExpression(base, startLoc, state) {
  11019. const node = this.startNodeAt(startLoc);
  11020. node.tag = base;
  11021. node.quasi = this.parseTemplate(true);
  11022. if (state.optionalChainMember) {
  11023. this.raise(Errors.OptionalChainingNoTemplate, {
  11024. at: startLoc
  11025. });
  11026. }
  11027. return this.finishNode(node, "TaggedTemplateExpression");
  11028. }
  11029. atPossibleAsyncArrow(base) {
  11030. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  11031. }
  11032. expectImportAttributesPlugin() {
  11033. if (!this.hasPlugin("importAssertions")) {
  11034. this.expectPlugin("importAttributes");
  11035. }
  11036. }
  11037. finishCallExpression(node, optional) {
  11038. if (node.callee.type === "Import") {
  11039. if (node.arguments.length === 2) {
  11040. {
  11041. if (!this.hasPlugin("moduleAttributes")) {
  11042. this.expectImportAttributesPlugin();
  11043. }
  11044. }
  11045. }
  11046. if (node.arguments.length === 0 || node.arguments.length > 2) {
  11047. this.raise(Errors.ImportCallArity, {
  11048. at: node,
  11049. maxArgumentCount: this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  11050. });
  11051. } else {
  11052. for (const arg of node.arguments) {
  11053. if (arg.type === "SpreadElement") {
  11054. this.raise(Errors.ImportCallSpreadArgument, {
  11055. at: arg
  11056. });
  11057. }
  11058. }
  11059. }
  11060. }
  11061. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  11062. }
  11063. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  11064. const elts = [];
  11065. let first = true;
  11066. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11067. this.state.inFSharpPipelineDirectBody = false;
  11068. while (!this.eat(close)) {
  11069. if (first) {
  11070. first = false;
  11071. } else {
  11072. this.expect(12);
  11073. if (this.match(close)) {
  11074. if (dynamicImport && !this.hasPlugin("importAttributes") && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  11075. this.raise(Errors.ImportCallArgumentTrailingComma, {
  11076. at: this.state.lastTokStartLoc
  11077. });
  11078. }
  11079. if (nodeForExtra) {
  11080. this.addTrailingCommaExtraToNode(nodeForExtra);
  11081. }
  11082. this.next();
  11083. break;
  11084. }
  11085. }
  11086. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  11087. }
  11088. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11089. return elts;
  11090. }
  11091. shouldParseAsyncArrow() {
  11092. return this.match(19) && !this.canInsertSemicolon();
  11093. }
  11094. parseAsyncArrowFromCallExpression(node, call) {
  11095. var _call$extra;
  11096. this.resetPreviousNodeTrailingComments(call);
  11097. this.expect(19);
  11098. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  11099. if (call.innerComments) {
  11100. setInnerComments(node, call.innerComments);
  11101. }
  11102. if (call.callee.trailingComments) {
  11103. setInnerComments(node, call.callee.trailingComments);
  11104. }
  11105. return node;
  11106. }
  11107. parseNoCallExpr() {
  11108. const startLoc = this.state.startLoc;
  11109. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  11110. }
  11111. parseExprAtom(refExpressionErrors) {
  11112. let node;
  11113. let decorators = null;
  11114. const {
  11115. type
  11116. } = this.state;
  11117. switch (type) {
  11118. case 79:
  11119. return this.parseSuper();
  11120. case 83:
  11121. node = this.startNode();
  11122. this.next();
  11123. if (this.match(16)) {
  11124. return this.parseImportMetaProperty(node);
  11125. }
  11126. if (!this.match(10)) {
  11127. this.raise(Errors.UnsupportedImport, {
  11128. at: this.state.lastTokStartLoc
  11129. });
  11130. }
  11131. return this.finishNode(node, "Import");
  11132. case 78:
  11133. node = this.startNode();
  11134. this.next();
  11135. return this.finishNode(node, "ThisExpression");
  11136. case 90:
  11137. {
  11138. return this.parseDo(this.startNode(), false);
  11139. }
  11140. case 56:
  11141. case 31:
  11142. {
  11143. this.readRegexp();
  11144. return this.parseRegExpLiteral(this.state.value);
  11145. }
  11146. case 132:
  11147. return this.parseNumericLiteral(this.state.value);
  11148. case 133:
  11149. return this.parseBigIntLiteral(this.state.value);
  11150. case 134:
  11151. return this.parseDecimalLiteral(this.state.value);
  11152. case 131:
  11153. return this.parseStringLiteral(this.state.value);
  11154. case 84:
  11155. return this.parseNullLiteral();
  11156. case 85:
  11157. return this.parseBooleanLiteral(true);
  11158. case 86:
  11159. return this.parseBooleanLiteral(false);
  11160. case 10:
  11161. {
  11162. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11163. return this.parseParenAndDistinguishExpression(canBeArrow);
  11164. }
  11165. case 2:
  11166. case 1:
  11167. {
  11168. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  11169. }
  11170. case 0:
  11171. {
  11172. return this.parseArrayLike(3, true, false, refExpressionErrors);
  11173. }
  11174. case 6:
  11175. case 7:
  11176. {
  11177. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11178. }
  11179. case 5:
  11180. {
  11181. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11182. }
  11183. case 68:
  11184. return this.parseFunctionOrFunctionSent();
  11185. case 26:
  11186. decorators = this.parseDecorators();
  11187. case 80:
  11188. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  11189. case 77:
  11190. return this.parseNewOrNewTarget();
  11191. case 25:
  11192. case 24:
  11193. return this.parseTemplate(false);
  11194. case 15:
  11195. {
  11196. node = this.startNode();
  11197. this.next();
  11198. node.object = null;
  11199. const callee = node.callee = this.parseNoCallExpr();
  11200. if (callee.type === "MemberExpression") {
  11201. return this.finishNode(node, "BindExpression");
  11202. } else {
  11203. throw this.raise(Errors.UnsupportedBind, {
  11204. at: callee
  11205. });
  11206. }
  11207. }
  11208. case 136:
  11209. {
  11210. this.raise(Errors.PrivateInExpectedIn, {
  11211. at: this.state.startLoc,
  11212. identifierName: this.state.value
  11213. });
  11214. return this.parsePrivateName();
  11215. }
  11216. case 33:
  11217. {
  11218. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11219. }
  11220. case 32:
  11221. {
  11222. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11223. }
  11224. case 37:
  11225. case 38:
  11226. {
  11227. return this.parseTopicReference("hack");
  11228. }
  11229. case 44:
  11230. case 54:
  11231. case 27:
  11232. {
  11233. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11234. if (pipeProposal) {
  11235. return this.parseTopicReference(pipeProposal);
  11236. }
  11237. this.unexpected();
  11238. break;
  11239. }
  11240. case 47:
  11241. {
  11242. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11243. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11244. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11245. } else {
  11246. this.unexpected();
  11247. }
  11248. break;
  11249. }
  11250. default:
  11251. if (tokenIsIdentifier(type)) {
  11252. if (this.isContextual(125) && this.lookaheadInLineCharCode() === 123) {
  11253. return this.parseModuleExpression();
  11254. }
  11255. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11256. const containsEsc = this.state.containsEsc;
  11257. const id = this.parseIdentifier();
  11258. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11259. const {
  11260. type
  11261. } = this.state;
  11262. if (type === 68) {
  11263. this.resetPreviousNodeTrailingComments(id);
  11264. this.next();
  11265. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  11266. } else if (tokenIsIdentifier(type)) {
  11267. if (this.lookaheadCharCode() === 61) {
  11268. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11269. } else {
  11270. return id;
  11271. }
  11272. } else if (type === 90) {
  11273. this.resetPreviousNodeTrailingComments(id);
  11274. return this.parseDo(this.startNodeAtNode(id), true);
  11275. }
  11276. }
  11277. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11278. this.next();
  11279. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11280. }
  11281. return id;
  11282. } else {
  11283. this.unexpected();
  11284. }
  11285. }
  11286. }
  11287. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11288. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11289. if (pipeProposal) {
  11290. this.state.type = topicTokenType;
  11291. this.state.value = topicTokenValue;
  11292. this.state.pos--;
  11293. this.state.end--;
  11294. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11295. return this.parseTopicReference(pipeProposal);
  11296. } else {
  11297. this.unexpected();
  11298. }
  11299. }
  11300. parseTopicReference(pipeProposal) {
  11301. const node = this.startNode();
  11302. const startLoc = this.state.startLoc;
  11303. const tokenType = this.state.type;
  11304. this.next();
  11305. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11306. }
  11307. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11308. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11309. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  11310. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11311. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  11312. at: startLoc
  11313. });
  11314. }
  11315. this.registerTopicReference();
  11316. return this.finishNode(node, nodeType);
  11317. } else {
  11318. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  11319. at: startLoc,
  11320. token: tokenLabelName(tokenType)
  11321. });
  11322. }
  11323. }
  11324. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11325. switch (pipeProposal) {
  11326. case "hack":
  11327. {
  11328. return this.hasPlugin(["pipelineOperator", {
  11329. topicToken: tokenLabelName(tokenType)
  11330. }]);
  11331. }
  11332. case "smart":
  11333. return tokenType === 27;
  11334. default:
  11335. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  11336. at: startLoc
  11337. });
  11338. }
  11339. }
  11340. parseAsyncArrowUnaryFunction(node) {
  11341. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11342. const params = [this.parseIdentifier()];
  11343. this.prodParam.exit();
  11344. if (this.hasPrecedingLineBreak()) {
  11345. this.raise(Errors.LineTerminatorBeforeArrow, {
  11346. at: this.state.curPosition()
  11347. });
  11348. }
  11349. this.expect(19);
  11350. return this.parseArrowExpression(node, params, true);
  11351. }
  11352. parseDo(node, isAsync) {
  11353. this.expectPlugin("doExpressions");
  11354. if (isAsync) {
  11355. this.expectPlugin("asyncDoExpressions");
  11356. }
  11357. node.async = isAsync;
  11358. this.next();
  11359. const oldLabels = this.state.labels;
  11360. this.state.labels = [];
  11361. if (isAsync) {
  11362. this.prodParam.enter(PARAM_AWAIT);
  11363. node.body = this.parseBlock();
  11364. this.prodParam.exit();
  11365. } else {
  11366. node.body = this.parseBlock();
  11367. }
  11368. this.state.labels = oldLabels;
  11369. return this.finishNode(node, "DoExpression");
  11370. }
  11371. parseSuper() {
  11372. const node = this.startNode();
  11373. this.next();
  11374. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11375. this.raise(Errors.SuperNotAllowed, {
  11376. at: node
  11377. });
  11378. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11379. this.raise(Errors.UnexpectedSuper, {
  11380. at: node
  11381. });
  11382. }
  11383. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11384. this.raise(Errors.UnsupportedSuper, {
  11385. at: node
  11386. });
  11387. }
  11388. return this.finishNode(node, "Super");
  11389. }
  11390. parsePrivateName() {
  11391. const node = this.startNode();
  11392. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11393. const name = this.state.value;
  11394. this.next();
  11395. node.id = this.createIdentifier(id, name);
  11396. return this.finishNode(node, "PrivateName");
  11397. }
  11398. parseFunctionOrFunctionSent() {
  11399. const node = this.startNode();
  11400. this.next();
  11401. if (this.prodParam.hasYield && this.match(16)) {
  11402. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11403. this.next();
  11404. if (this.match(102)) {
  11405. this.expectPlugin("functionSent");
  11406. } else if (!this.hasPlugin("functionSent")) {
  11407. this.unexpected();
  11408. }
  11409. return this.parseMetaProperty(node, meta, "sent");
  11410. }
  11411. return this.parseFunction(node);
  11412. }
  11413. parseMetaProperty(node, meta, propertyName) {
  11414. node.meta = meta;
  11415. const containsEsc = this.state.containsEsc;
  11416. node.property = this.parseIdentifier(true);
  11417. if (node.property.name !== propertyName || containsEsc) {
  11418. this.raise(Errors.UnsupportedMetaProperty, {
  11419. at: node.property,
  11420. target: meta.name,
  11421. onlyValidPropertyName: propertyName
  11422. });
  11423. }
  11424. return this.finishNode(node, "MetaProperty");
  11425. }
  11426. parseImportMetaProperty(node) {
  11427. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11428. this.next();
  11429. if (this.isContextual(100)) {
  11430. if (!this.inModule) {
  11431. this.raise(Errors.ImportMetaOutsideModule, {
  11432. at: id
  11433. });
  11434. }
  11435. this.sawUnambiguousESM = true;
  11436. }
  11437. return this.parseMetaProperty(node, id, "meta");
  11438. }
  11439. parseLiteralAtNode(value, type, node) {
  11440. this.addExtra(node, "rawValue", value);
  11441. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11442. node.value = value;
  11443. this.next();
  11444. return this.finishNode(node, type);
  11445. }
  11446. parseLiteral(value, type) {
  11447. const node = this.startNode();
  11448. return this.parseLiteralAtNode(value, type, node);
  11449. }
  11450. parseStringLiteral(value) {
  11451. return this.parseLiteral(value, "StringLiteral");
  11452. }
  11453. parseNumericLiteral(value) {
  11454. return this.parseLiteral(value, "NumericLiteral");
  11455. }
  11456. parseBigIntLiteral(value) {
  11457. return this.parseLiteral(value, "BigIntLiteral");
  11458. }
  11459. parseDecimalLiteral(value) {
  11460. return this.parseLiteral(value, "DecimalLiteral");
  11461. }
  11462. parseRegExpLiteral(value) {
  11463. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11464. node.pattern = value.pattern;
  11465. node.flags = value.flags;
  11466. return node;
  11467. }
  11468. parseBooleanLiteral(value) {
  11469. const node = this.startNode();
  11470. node.value = value;
  11471. this.next();
  11472. return this.finishNode(node, "BooleanLiteral");
  11473. }
  11474. parseNullLiteral() {
  11475. const node = this.startNode();
  11476. this.next();
  11477. return this.finishNode(node, "NullLiteral");
  11478. }
  11479. parseParenAndDistinguishExpression(canBeArrow) {
  11480. const startLoc = this.state.startLoc;
  11481. let val;
  11482. this.next();
  11483. this.expressionScope.enter(newArrowHeadScope());
  11484. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11485. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11486. this.state.maybeInArrowParameters = true;
  11487. this.state.inFSharpPipelineDirectBody = false;
  11488. const innerStartLoc = this.state.startLoc;
  11489. const exprList = [];
  11490. const refExpressionErrors = new ExpressionErrors();
  11491. let first = true;
  11492. let spreadStartLoc;
  11493. let optionalCommaStartLoc;
  11494. while (!this.match(11)) {
  11495. if (first) {
  11496. first = false;
  11497. } else {
  11498. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11499. if (this.match(11)) {
  11500. optionalCommaStartLoc = this.state.startLoc;
  11501. break;
  11502. }
  11503. }
  11504. if (this.match(21)) {
  11505. const spreadNodeStartLoc = this.state.startLoc;
  11506. spreadStartLoc = this.state.startLoc;
  11507. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11508. if (!this.checkCommaAfterRest(41)) {
  11509. break;
  11510. }
  11511. } else {
  11512. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11513. }
  11514. }
  11515. const innerEndLoc = this.state.lastTokEndLoc;
  11516. this.expect(11);
  11517. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11518. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11519. let arrowNode = this.startNodeAt(startLoc);
  11520. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11521. this.checkDestructuringPrivate(refExpressionErrors);
  11522. this.expressionScope.validateAsPattern();
  11523. this.expressionScope.exit();
  11524. this.parseArrowExpression(arrowNode, exprList, false);
  11525. return arrowNode;
  11526. }
  11527. this.expressionScope.exit();
  11528. if (!exprList.length) {
  11529. this.unexpected(this.state.lastTokStartLoc);
  11530. }
  11531. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11532. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11533. this.checkExpressionErrors(refExpressionErrors, true);
  11534. this.toReferencedListDeep(exprList, true);
  11535. if (exprList.length > 1) {
  11536. val = this.startNodeAt(innerStartLoc);
  11537. val.expressions = exprList;
  11538. this.finishNode(val, "SequenceExpression");
  11539. this.resetEndLocation(val, innerEndLoc);
  11540. } else {
  11541. val = exprList[0];
  11542. }
  11543. return this.wrapParenthesis(startLoc, val);
  11544. }
  11545. wrapParenthesis(startLoc, expression) {
  11546. if (!this.options.createParenthesizedExpressions) {
  11547. this.addExtra(expression, "parenthesized", true);
  11548. this.addExtra(expression, "parenStart", startLoc.index);
  11549. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11550. return expression;
  11551. }
  11552. const parenExpression = this.startNodeAt(startLoc);
  11553. parenExpression.expression = expression;
  11554. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11555. }
  11556. shouldParseArrow(params) {
  11557. return !this.canInsertSemicolon();
  11558. }
  11559. parseArrow(node) {
  11560. if (this.eat(19)) {
  11561. return node;
  11562. }
  11563. }
  11564. parseParenItem(node, startLoc) {
  11565. return node;
  11566. }
  11567. parseNewOrNewTarget() {
  11568. const node = this.startNode();
  11569. this.next();
  11570. if (this.match(16)) {
  11571. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11572. this.next();
  11573. const metaProp = this.parseMetaProperty(node, meta, "target");
  11574. if (!this.scope.inNonArrowFunction && !this.scope.inClass && !this.options.allowNewTargetOutsideFunction) {
  11575. this.raise(Errors.UnexpectedNewTarget, {
  11576. at: metaProp
  11577. });
  11578. }
  11579. return metaProp;
  11580. }
  11581. return this.parseNew(node);
  11582. }
  11583. parseNew(node) {
  11584. this.parseNewCallee(node);
  11585. if (this.eat(10)) {
  11586. const args = this.parseExprList(11);
  11587. this.toReferencedList(args);
  11588. node.arguments = args;
  11589. } else {
  11590. node.arguments = [];
  11591. }
  11592. return this.finishNode(node, "NewExpression");
  11593. }
  11594. parseNewCallee(node) {
  11595. node.callee = this.parseNoCallExpr();
  11596. if (node.callee.type === "Import") {
  11597. this.raise(Errors.ImportCallNotNewExpression, {
  11598. at: node.callee
  11599. });
  11600. }
  11601. }
  11602. parseTemplateElement(isTagged) {
  11603. const {
  11604. start,
  11605. startLoc,
  11606. end,
  11607. value
  11608. } = this.state;
  11609. const elemStart = start + 1;
  11610. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11611. if (value === null) {
  11612. if (!isTagged) {
  11613. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11614. at: createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1)
  11615. });
  11616. }
  11617. }
  11618. const isTail = this.match(24);
  11619. const endOffset = isTail ? -1 : -2;
  11620. const elemEnd = end + endOffset;
  11621. elem.value = {
  11622. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11623. cooked: value === null ? null : value.slice(1, endOffset)
  11624. };
  11625. elem.tail = isTail;
  11626. this.next();
  11627. const finishedNode = this.finishNode(elem, "TemplateElement");
  11628. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11629. return finishedNode;
  11630. }
  11631. parseTemplate(isTagged) {
  11632. const node = this.startNode();
  11633. node.expressions = [];
  11634. let curElt = this.parseTemplateElement(isTagged);
  11635. node.quasis = [curElt];
  11636. while (!curElt.tail) {
  11637. node.expressions.push(this.parseTemplateSubstitution());
  11638. this.readTemplateContinuation();
  11639. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11640. }
  11641. return this.finishNode(node, "TemplateLiteral");
  11642. }
  11643. parseTemplateSubstitution() {
  11644. return this.parseExpression();
  11645. }
  11646. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11647. if (isRecord) {
  11648. this.expectPlugin("recordAndTuple");
  11649. }
  11650. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11651. this.state.inFSharpPipelineDirectBody = false;
  11652. const propHash = Object.create(null);
  11653. let first = true;
  11654. const node = this.startNode();
  11655. node.properties = [];
  11656. this.next();
  11657. while (!this.match(close)) {
  11658. if (first) {
  11659. first = false;
  11660. } else {
  11661. this.expect(12);
  11662. if (this.match(close)) {
  11663. this.addTrailingCommaExtraToNode(node);
  11664. break;
  11665. }
  11666. }
  11667. let prop;
  11668. if (isPattern) {
  11669. prop = this.parseBindingProperty();
  11670. } else {
  11671. prop = this.parsePropertyDefinition(refExpressionErrors);
  11672. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11673. }
  11674. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11675. this.raise(Errors.InvalidRecordProperty, {
  11676. at: prop
  11677. });
  11678. }
  11679. if (prop.shorthand) {
  11680. this.addExtra(prop, "shorthand", true);
  11681. }
  11682. node.properties.push(prop);
  11683. }
  11684. this.next();
  11685. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11686. let type = "ObjectExpression";
  11687. if (isPattern) {
  11688. type = "ObjectPattern";
  11689. } else if (isRecord) {
  11690. type = "RecordExpression";
  11691. }
  11692. return this.finishNode(node, type);
  11693. }
  11694. addTrailingCommaExtraToNode(node) {
  11695. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11696. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11697. }
  11698. maybeAsyncOrAccessorProp(prop) {
  11699. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11700. }
  11701. parsePropertyDefinition(refExpressionErrors) {
  11702. let decorators = [];
  11703. if (this.match(26)) {
  11704. if (this.hasPlugin("decorators")) {
  11705. this.raise(Errors.UnsupportedPropertyDecorator, {
  11706. at: this.state.startLoc
  11707. });
  11708. }
  11709. while (this.match(26)) {
  11710. decorators.push(this.parseDecorator());
  11711. }
  11712. }
  11713. const prop = this.startNode();
  11714. let isAsync = false;
  11715. let isAccessor = false;
  11716. let startLoc;
  11717. if (this.match(21)) {
  11718. if (decorators.length) this.unexpected();
  11719. return this.parseSpread();
  11720. }
  11721. if (decorators.length) {
  11722. prop.decorators = decorators;
  11723. decorators = [];
  11724. }
  11725. prop.method = false;
  11726. if (refExpressionErrors) {
  11727. startLoc = this.state.startLoc;
  11728. }
  11729. let isGenerator = this.eat(55);
  11730. this.parsePropertyNamePrefixOperator(prop);
  11731. const containsEsc = this.state.containsEsc;
  11732. const key = this.parsePropertyName(prop, refExpressionErrors);
  11733. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11734. const keyName = key.name;
  11735. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11736. isAsync = true;
  11737. this.resetPreviousNodeTrailingComments(key);
  11738. isGenerator = this.eat(55);
  11739. this.parsePropertyName(prop);
  11740. }
  11741. if (keyName === "get" || keyName === "set") {
  11742. isAccessor = true;
  11743. this.resetPreviousNodeTrailingComments(key);
  11744. prop.kind = keyName;
  11745. if (this.match(55)) {
  11746. isGenerator = true;
  11747. this.raise(Errors.AccessorIsGenerator, {
  11748. at: this.state.curPosition(),
  11749. kind: keyName
  11750. });
  11751. this.next();
  11752. }
  11753. this.parsePropertyName(prop);
  11754. }
  11755. }
  11756. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11757. }
  11758. getGetterSetterExpectedParamCount(method) {
  11759. return method.kind === "get" ? 0 : 1;
  11760. }
  11761. getObjectOrClassMethodParams(method) {
  11762. return method.params;
  11763. }
  11764. checkGetterSetterParams(method) {
  11765. var _params;
  11766. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11767. const params = this.getObjectOrClassMethodParams(method);
  11768. if (params.length !== paramCount) {
  11769. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11770. at: method
  11771. });
  11772. }
  11773. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11774. this.raise(Errors.BadSetterRestParameter, {
  11775. at: method
  11776. });
  11777. }
  11778. }
  11779. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11780. if (isAccessor) {
  11781. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11782. this.checkGetterSetterParams(finishedProp);
  11783. return finishedProp;
  11784. }
  11785. if (isAsync || isGenerator || this.match(10)) {
  11786. if (isPattern) this.unexpected();
  11787. prop.kind = "method";
  11788. prop.method = true;
  11789. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11790. }
  11791. }
  11792. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11793. prop.shorthand = false;
  11794. if (this.eat(14)) {
  11795. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11796. return this.finishNode(prop, "ObjectProperty");
  11797. }
  11798. if (!prop.computed && prop.key.type === "Identifier") {
  11799. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11800. if (isPattern) {
  11801. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11802. } else if (this.match(29)) {
  11803. const shorthandAssignLoc = this.state.startLoc;
  11804. if (refExpressionErrors != null) {
  11805. if (refExpressionErrors.shorthandAssignLoc === null) {
  11806. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11807. }
  11808. } else {
  11809. this.raise(Errors.InvalidCoverInitializedName, {
  11810. at: shorthandAssignLoc
  11811. });
  11812. }
  11813. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11814. } else {
  11815. prop.value = cloneIdentifier(prop.key);
  11816. }
  11817. prop.shorthand = true;
  11818. return this.finishNode(prop, "ObjectProperty");
  11819. }
  11820. }
  11821. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11822. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11823. if (!node) this.unexpected();
  11824. return node;
  11825. }
  11826. parsePropertyName(prop, refExpressionErrors) {
  11827. if (this.eat(0)) {
  11828. prop.computed = true;
  11829. prop.key = this.parseMaybeAssignAllowIn();
  11830. this.expect(3);
  11831. } else {
  11832. const {
  11833. type,
  11834. value
  11835. } = this.state;
  11836. let key;
  11837. if (tokenIsKeywordOrIdentifier(type)) {
  11838. key = this.parseIdentifier(true);
  11839. } else {
  11840. switch (type) {
  11841. case 132:
  11842. key = this.parseNumericLiteral(value);
  11843. break;
  11844. case 131:
  11845. key = this.parseStringLiteral(value);
  11846. break;
  11847. case 133:
  11848. key = this.parseBigIntLiteral(value);
  11849. break;
  11850. case 134:
  11851. key = this.parseDecimalLiteral(value);
  11852. break;
  11853. case 136:
  11854. {
  11855. const privateKeyLoc = this.state.startLoc;
  11856. if (refExpressionErrors != null) {
  11857. if (refExpressionErrors.privateKeyLoc === null) {
  11858. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11859. }
  11860. } else {
  11861. this.raise(Errors.UnexpectedPrivateField, {
  11862. at: privateKeyLoc
  11863. });
  11864. }
  11865. key = this.parsePrivateName();
  11866. break;
  11867. }
  11868. default:
  11869. this.unexpected();
  11870. }
  11871. }
  11872. prop.key = key;
  11873. if (type !== 136) {
  11874. prop.computed = false;
  11875. }
  11876. }
  11877. return prop.key;
  11878. }
  11879. initFunction(node, isAsync) {
  11880. node.id = null;
  11881. node.generator = false;
  11882. node.async = isAsync;
  11883. }
  11884. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11885. this.initFunction(node, isAsync);
  11886. node.generator = isGenerator;
  11887. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11888. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11889. this.parseFunctionParams(node, isConstructor);
  11890. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11891. this.prodParam.exit();
  11892. this.scope.exit();
  11893. return finishedNode;
  11894. }
  11895. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11896. if (isTuple) {
  11897. this.expectPlugin("recordAndTuple");
  11898. }
  11899. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11900. this.state.inFSharpPipelineDirectBody = false;
  11901. const node = this.startNode();
  11902. this.next();
  11903. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11904. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11905. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11906. }
  11907. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11908. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11909. let flags = functionFlags(isAsync, false);
  11910. if (!this.match(5) && this.prodParam.hasIn) {
  11911. flags |= PARAM_IN;
  11912. }
  11913. this.prodParam.enter(flags);
  11914. this.initFunction(node, isAsync);
  11915. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11916. if (params) {
  11917. this.state.maybeInArrowParameters = true;
  11918. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11919. }
  11920. this.state.maybeInArrowParameters = false;
  11921. this.parseFunctionBody(node, true);
  11922. this.prodParam.exit();
  11923. this.scope.exit();
  11924. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11925. return this.finishNode(node, "ArrowFunctionExpression");
  11926. }
  11927. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11928. this.toAssignableList(params, trailingCommaLoc, false);
  11929. node.params = params;
  11930. }
  11931. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11932. this.parseFunctionBody(node, false, isMethod);
  11933. return this.finishNode(node, type);
  11934. }
  11935. parseFunctionBody(node, allowExpression, isMethod = false) {
  11936. const isExpression = allowExpression && !this.match(5);
  11937. this.expressionScope.enter(newExpressionScope());
  11938. if (isExpression) {
  11939. node.body = this.parseMaybeAssign();
  11940. this.checkParams(node, false, allowExpression, false);
  11941. } else {
  11942. const oldStrict = this.state.strict;
  11943. const oldLabels = this.state.labels;
  11944. this.state.labels = [];
  11945. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11946. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11947. const nonSimple = !this.isSimpleParamList(node.params);
  11948. if (hasStrictModeDirective && nonSimple) {
  11949. this.raise(Errors.IllegalLanguageModeDirective, {
  11950. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11951. });
  11952. }
  11953. const strictModeChanged = !oldStrict && this.state.strict;
  11954. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11955. if (this.state.strict && node.id) {
  11956. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11957. }
  11958. });
  11959. this.prodParam.exit();
  11960. this.state.labels = oldLabels;
  11961. }
  11962. this.expressionScope.exit();
  11963. }
  11964. isSimpleParameter(node) {
  11965. return node.type === "Identifier";
  11966. }
  11967. isSimpleParamList(params) {
  11968. for (let i = 0, len = params.length; i < len; i++) {
  11969. if (!this.isSimpleParameter(params[i])) return false;
  11970. }
  11971. return true;
  11972. }
  11973. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11974. const checkClashes = !allowDuplicates && new Set();
  11975. const formalParameters = {
  11976. type: "FormalParameters"
  11977. };
  11978. for (const param of node.params) {
  11979. this.checkLVal(param, {
  11980. in: formalParameters,
  11981. binding: BIND_VAR,
  11982. checkClashes,
  11983. strictModeChanged
  11984. });
  11985. }
  11986. }
  11987. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11988. const elts = [];
  11989. let first = true;
  11990. while (!this.eat(close)) {
  11991. if (first) {
  11992. first = false;
  11993. } else {
  11994. this.expect(12);
  11995. if (this.match(close)) {
  11996. if (nodeForExtra) {
  11997. this.addTrailingCommaExtraToNode(nodeForExtra);
  11998. }
  11999. this.next();
  12000. break;
  12001. }
  12002. }
  12003. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  12004. }
  12005. return elts;
  12006. }
  12007. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  12008. let elt;
  12009. if (this.match(12)) {
  12010. if (!allowEmpty) {
  12011. this.raise(Errors.UnexpectedToken, {
  12012. at: this.state.curPosition(),
  12013. unexpected: ","
  12014. });
  12015. }
  12016. elt = null;
  12017. } else if (this.match(21)) {
  12018. const spreadNodeStartLoc = this.state.startLoc;
  12019. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  12020. } else if (this.match(17)) {
  12021. this.expectPlugin("partialApplication");
  12022. if (!allowPlaceholder) {
  12023. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  12024. at: this.state.startLoc
  12025. });
  12026. }
  12027. const node = this.startNode();
  12028. this.next();
  12029. elt = this.finishNode(node, "ArgumentPlaceholder");
  12030. } else {
  12031. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  12032. }
  12033. return elt;
  12034. }
  12035. parseIdentifier(liberal) {
  12036. const node = this.startNode();
  12037. const name = this.parseIdentifierName(liberal);
  12038. return this.createIdentifier(node, name);
  12039. }
  12040. createIdentifier(node, name) {
  12041. node.name = name;
  12042. node.loc.identifierName = name;
  12043. return this.finishNode(node, "Identifier");
  12044. }
  12045. parseIdentifierName(liberal) {
  12046. let name;
  12047. const {
  12048. startLoc,
  12049. type
  12050. } = this.state;
  12051. if (tokenIsKeywordOrIdentifier(type)) {
  12052. name = this.state.value;
  12053. } else {
  12054. this.unexpected();
  12055. }
  12056. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  12057. if (liberal) {
  12058. if (tokenIsKeyword) {
  12059. this.replaceToken(130);
  12060. }
  12061. } else {
  12062. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  12063. }
  12064. this.next();
  12065. return name;
  12066. }
  12067. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  12068. if (word.length > 10) {
  12069. return;
  12070. }
  12071. if (!canBeReservedWord(word)) {
  12072. return;
  12073. }
  12074. if (checkKeywords && isKeyword(word)) {
  12075. this.raise(Errors.UnexpectedKeyword, {
  12076. at: startLoc,
  12077. keyword: word
  12078. });
  12079. return;
  12080. }
  12081. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  12082. if (reservedTest(word, this.inModule)) {
  12083. this.raise(Errors.UnexpectedReservedWord, {
  12084. at: startLoc,
  12085. reservedWord: word
  12086. });
  12087. return;
  12088. } else if (word === "yield") {
  12089. if (this.prodParam.hasYield) {
  12090. this.raise(Errors.YieldBindingIdentifier, {
  12091. at: startLoc
  12092. });
  12093. return;
  12094. }
  12095. } else if (word === "await") {
  12096. if (this.prodParam.hasAwait) {
  12097. this.raise(Errors.AwaitBindingIdentifier, {
  12098. at: startLoc
  12099. });
  12100. return;
  12101. }
  12102. if (this.scope.inStaticBlock) {
  12103. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  12104. at: startLoc
  12105. });
  12106. return;
  12107. }
  12108. this.expressionScope.recordAsyncArrowParametersError({
  12109. at: startLoc
  12110. });
  12111. } else if (word === "arguments") {
  12112. if (this.scope.inClassAndNotInNonArrowFunction) {
  12113. this.raise(Errors.ArgumentsInClass, {
  12114. at: startLoc
  12115. });
  12116. return;
  12117. }
  12118. }
  12119. }
  12120. isAwaitAllowed() {
  12121. if (this.prodParam.hasAwait) return true;
  12122. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  12123. return true;
  12124. }
  12125. return false;
  12126. }
  12127. parseAwait(startLoc) {
  12128. const node = this.startNodeAt(startLoc);
  12129. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  12130. at: node
  12131. });
  12132. if (this.eat(55)) {
  12133. this.raise(Errors.ObsoleteAwaitStar, {
  12134. at: node
  12135. });
  12136. }
  12137. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  12138. if (this.isAmbiguousAwait()) {
  12139. this.ambiguousScriptDifferentAst = true;
  12140. } else {
  12141. this.sawUnambiguousESM = true;
  12142. }
  12143. }
  12144. if (!this.state.soloAwait) {
  12145. node.argument = this.parseMaybeUnary(null, true);
  12146. }
  12147. return this.finishNode(node, "AwaitExpression");
  12148. }
  12149. isAmbiguousAwait() {
  12150. if (this.hasPrecedingLineBreak()) return true;
  12151. const {
  12152. type
  12153. } = this.state;
  12154. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 101 && !this.state.containsEsc || type === 135 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12155. }
  12156. parseYield() {
  12157. const node = this.startNode();
  12158. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  12159. at: node
  12160. });
  12161. this.next();
  12162. let delegating = false;
  12163. let argument = null;
  12164. if (!this.hasPrecedingLineBreak()) {
  12165. delegating = this.eat(55);
  12166. switch (this.state.type) {
  12167. case 13:
  12168. case 137:
  12169. case 8:
  12170. case 11:
  12171. case 3:
  12172. case 9:
  12173. case 14:
  12174. case 12:
  12175. if (!delegating) break;
  12176. default:
  12177. argument = this.parseMaybeAssign();
  12178. }
  12179. }
  12180. node.delegate = delegating;
  12181. node.argument = argument;
  12182. return this.finishNode(node, "YieldExpression");
  12183. }
  12184. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12185. if (this.hasPlugin(["pipelineOperator", {
  12186. proposal: "smart"
  12187. }])) {
  12188. if (left.type === "SequenceExpression") {
  12189. this.raise(Errors.PipelineHeadSequenceExpression, {
  12190. at: leftStartLoc
  12191. });
  12192. }
  12193. }
  12194. }
  12195. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  12196. if (this.isSimpleReference(childExpr)) {
  12197. const bodyNode = this.startNodeAt(startLoc);
  12198. bodyNode.callee = childExpr;
  12199. return this.finishNode(bodyNode, "PipelineBareFunction");
  12200. } else {
  12201. const bodyNode = this.startNodeAt(startLoc);
  12202. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12203. bodyNode.expression = childExpr;
  12204. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12205. }
  12206. }
  12207. isSimpleReference(expression) {
  12208. switch (expression.type) {
  12209. case "MemberExpression":
  12210. return !expression.computed && this.isSimpleReference(expression.object);
  12211. case "Identifier":
  12212. return true;
  12213. default:
  12214. return false;
  12215. }
  12216. }
  12217. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12218. if (this.match(19)) {
  12219. throw this.raise(Errors.PipelineBodyNoArrow, {
  12220. at: this.state.startLoc
  12221. });
  12222. }
  12223. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12224. this.raise(Errors.PipelineTopicUnused, {
  12225. at: startLoc
  12226. });
  12227. }
  12228. }
  12229. withTopicBindingContext(callback) {
  12230. const outerContextTopicState = this.state.topicContext;
  12231. this.state.topicContext = {
  12232. maxNumOfResolvableTopics: 1,
  12233. maxTopicIndex: null
  12234. };
  12235. try {
  12236. return callback();
  12237. } finally {
  12238. this.state.topicContext = outerContextTopicState;
  12239. }
  12240. }
  12241. withSmartMixTopicForbiddingContext(callback) {
  12242. if (this.hasPlugin(["pipelineOperator", {
  12243. proposal: "smart"
  12244. }])) {
  12245. const outerContextTopicState = this.state.topicContext;
  12246. this.state.topicContext = {
  12247. maxNumOfResolvableTopics: 0,
  12248. maxTopicIndex: null
  12249. };
  12250. try {
  12251. return callback();
  12252. } finally {
  12253. this.state.topicContext = outerContextTopicState;
  12254. }
  12255. } else {
  12256. return callback();
  12257. }
  12258. }
  12259. withSoloAwaitPermittingContext(callback) {
  12260. const outerContextSoloAwaitState = this.state.soloAwait;
  12261. this.state.soloAwait = true;
  12262. try {
  12263. return callback();
  12264. } finally {
  12265. this.state.soloAwait = outerContextSoloAwaitState;
  12266. }
  12267. }
  12268. allowInAnd(callback) {
  12269. const flags = this.prodParam.currentFlags();
  12270. const prodParamToSet = PARAM_IN & ~flags;
  12271. if (prodParamToSet) {
  12272. this.prodParam.enter(flags | PARAM_IN);
  12273. try {
  12274. return callback();
  12275. } finally {
  12276. this.prodParam.exit();
  12277. }
  12278. }
  12279. return callback();
  12280. }
  12281. disallowInAnd(callback) {
  12282. const flags = this.prodParam.currentFlags();
  12283. const prodParamToClear = PARAM_IN & flags;
  12284. if (prodParamToClear) {
  12285. this.prodParam.enter(flags & ~PARAM_IN);
  12286. try {
  12287. return callback();
  12288. } finally {
  12289. this.prodParam.exit();
  12290. }
  12291. }
  12292. return callback();
  12293. }
  12294. registerTopicReference() {
  12295. this.state.topicContext.maxTopicIndex = 0;
  12296. }
  12297. topicReferenceIsAllowedInCurrentContext() {
  12298. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12299. }
  12300. topicReferenceWasUsedInCurrentContext() {
  12301. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12302. }
  12303. parseFSharpPipelineBody(prec) {
  12304. const startLoc = this.state.startLoc;
  12305. this.state.potentialArrowAt = this.state.start;
  12306. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12307. this.state.inFSharpPipelineDirectBody = true;
  12308. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  12309. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12310. return ret;
  12311. }
  12312. parseModuleExpression() {
  12313. this.expectPlugin("moduleBlocks");
  12314. const node = this.startNode();
  12315. this.next();
  12316. if (!this.match(5)) {
  12317. this.unexpected(null, 5);
  12318. }
  12319. const program = this.startNodeAt(this.state.endLoc);
  12320. this.next();
  12321. const revertScopes = this.initializeScopes(true);
  12322. this.enterInitialScopes();
  12323. try {
  12324. node.body = this.parseProgram(program, 8, "module");
  12325. } finally {
  12326. revertScopes();
  12327. }
  12328. return this.finishNode(node, "ModuleExpression");
  12329. }
  12330. parsePropertyNamePrefixOperator(prop) {}
  12331. }
  12332. const loopLabel = {
  12333. kind: "loop"
  12334. },
  12335. switchLabel = {
  12336. kind: "switch"
  12337. };
  12338. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12339. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12340. function babel7CompatTokens(tokens, input) {
  12341. for (let i = 0; i < tokens.length; i++) {
  12342. const token = tokens[i];
  12343. const {
  12344. type
  12345. } = token;
  12346. if (typeof type === "number") {
  12347. {
  12348. if (type === 136) {
  12349. const {
  12350. loc,
  12351. start,
  12352. value,
  12353. end
  12354. } = token;
  12355. const hashEndPos = start + 1;
  12356. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12357. tokens.splice(i, 1, new Token({
  12358. type: getExportedToken(27),
  12359. value: "#",
  12360. start: start,
  12361. end: hashEndPos,
  12362. startLoc: loc.start,
  12363. endLoc: hashEndLoc
  12364. }), new Token({
  12365. type: getExportedToken(130),
  12366. value: value,
  12367. start: hashEndPos,
  12368. end: end,
  12369. startLoc: hashEndLoc,
  12370. endLoc: loc.end
  12371. }));
  12372. i++;
  12373. continue;
  12374. }
  12375. if (tokenIsTemplate(type)) {
  12376. const {
  12377. loc,
  12378. start,
  12379. value,
  12380. end
  12381. } = token;
  12382. const backquoteEnd = start + 1;
  12383. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12384. let startToken;
  12385. if (input.charCodeAt(start) === 96) {
  12386. startToken = new Token({
  12387. type: getExportedToken(22),
  12388. value: "`",
  12389. start: start,
  12390. end: backquoteEnd,
  12391. startLoc: loc.start,
  12392. endLoc: backquoteEndLoc
  12393. });
  12394. } else {
  12395. startToken = new Token({
  12396. type: getExportedToken(8),
  12397. value: "}",
  12398. start: start,
  12399. end: backquoteEnd,
  12400. startLoc: loc.start,
  12401. endLoc: backquoteEndLoc
  12402. });
  12403. }
  12404. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12405. if (type === 24) {
  12406. templateElementEnd = end - 1;
  12407. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12408. templateValue = value === null ? null : value.slice(1, -1);
  12409. endToken = new Token({
  12410. type: getExportedToken(22),
  12411. value: "`",
  12412. start: templateElementEnd,
  12413. end: end,
  12414. startLoc: templateElementEndLoc,
  12415. endLoc: loc.end
  12416. });
  12417. } else {
  12418. templateElementEnd = end - 2;
  12419. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12420. templateValue = value === null ? null : value.slice(1, -2);
  12421. endToken = new Token({
  12422. type: getExportedToken(23),
  12423. value: "${",
  12424. start: templateElementEnd,
  12425. end: end,
  12426. startLoc: templateElementEndLoc,
  12427. endLoc: loc.end
  12428. });
  12429. }
  12430. tokens.splice(i, 1, startToken, new Token({
  12431. type: getExportedToken(20),
  12432. value: templateValue,
  12433. start: backquoteEnd,
  12434. end: templateElementEnd,
  12435. startLoc: backquoteEndLoc,
  12436. endLoc: templateElementEndLoc
  12437. }), endToken);
  12438. i += 2;
  12439. continue;
  12440. }
  12441. }
  12442. token.type = getExportedToken(type);
  12443. }
  12444. }
  12445. return tokens;
  12446. }
  12447. class StatementParser extends ExpressionParser {
  12448. parseTopLevel(file, program) {
  12449. file.program = this.parseProgram(program);
  12450. file.comments = this.state.comments;
  12451. if (this.options.tokens) {
  12452. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12453. }
  12454. return this.finishNode(file, "File");
  12455. }
  12456. parseProgram(program, end = 137, sourceType = this.options.sourceType) {
  12457. program.sourceType = sourceType;
  12458. program.interpreter = this.parseInterpreterDirective();
  12459. this.parseBlockBody(program, true, true, end);
  12460. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12461. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12462. this.raise(Errors.ModuleExportUndefined, {
  12463. at,
  12464. localName
  12465. });
  12466. }
  12467. }
  12468. let finishedProgram;
  12469. if (end === 137) {
  12470. finishedProgram = this.finishNode(program, "Program");
  12471. } else {
  12472. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12473. }
  12474. return finishedProgram;
  12475. }
  12476. stmtToDirective(stmt) {
  12477. const directive = stmt;
  12478. directive.type = "Directive";
  12479. directive.value = directive.expression;
  12480. delete directive.expression;
  12481. const directiveLiteral = directive.value;
  12482. const expressionValue = directiveLiteral.value;
  12483. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12484. const val = directiveLiteral.value = raw.slice(1, -1);
  12485. this.addExtra(directiveLiteral, "raw", raw);
  12486. this.addExtra(directiveLiteral, "rawValue", val);
  12487. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12488. directiveLiteral.type = "DirectiveLiteral";
  12489. return directive;
  12490. }
  12491. parseInterpreterDirective() {
  12492. if (!this.match(28)) {
  12493. return null;
  12494. }
  12495. const node = this.startNode();
  12496. node.value = this.state.value;
  12497. this.next();
  12498. return this.finishNode(node, "InterpreterDirective");
  12499. }
  12500. isLet() {
  12501. if (!this.isContextual(99)) {
  12502. return false;
  12503. }
  12504. return this.hasFollowingBindingAtom();
  12505. }
  12506. chStartsBindingIdentifier(ch, pos) {
  12507. if (isIdentifierStart(ch)) {
  12508. keywordRelationalOperator.lastIndex = pos;
  12509. if (keywordRelationalOperator.test(this.input)) {
  12510. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12511. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12512. return false;
  12513. }
  12514. }
  12515. return true;
  12516. } else if (ch === 92) {
  12517. return true;
  12518. } else {
  12519. return false;
  12520. }
  12521. }
  12522. chStartsBindingPattern(ch) {
  12523. return ch === 91 || ch === 123;
  12524. }
  12525. hasFollowingBindingAtom() {
  12526. const next = this.nextTokenStart();
  12527. const nextCh = this.codePointAtPos(next);
  12528. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12529. }
  12530. hasInLineFollowingBindingIdentifier() {
  12531. const next = this.nextTokenInLineStart();
  12532. const nextCh = this.codePointAtPos(next);
  12533. return this.chStartsBindingIdentifier(nextCh, next);
  12534. }
  12535. startsUsingForOf() {
  12536. const {
  12537. type,
  12538. containsEsc
  12539. } = this.lookahead();
  12540. if (type === 101 && !containsEsc) {
  12541. return false;
  12542. } else if (tokenIsIdentifier(type) && !this.hasFollowingLineBreak()) {
  12543. this.expectPlugin("explicitResourceManagement");
  12544. return true;
  12545. }
  12546. }
  12547. startsAwaitUsing() {
  12548. let next = this.nextTokenInLineStart();
  12549. if (this.isUnparsedContextual(next, "using")) {
  12550. next = this.nextTokenInLineStartSince(next + 5);
  12551. const nextCh = this.codePointAtPos(next);
  12552. if (this.chStartsBindingIdentifier(nextCh, next)) {
  12553. this.expectPlugin("explicitResourceManagement");
  12554. return true;
  12555. }
  12556. }
  12557. return false;
  12558. }
  12559. parseModuleItem() {
  12560. return this.parseStatementLike(1 | 2 | 4 | 8);
  12561. }
  12562. parseStatementListItem() {
  12563. return this.parseStatementLike(2 | 4 | (!this.options.annexB || this.state.strict ? 0 : 8));
  12564. }
  12565. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12566. let flags = 0;
  12567. if (this.options.annexB && !this.state.strict) {
  12568. flags |= 4;
  12569. if (allowLabeledFunction) {
  12570. flags |= 8;
  12571. }
  12572. }
  12573. return this.parseStatementLike(flags);
  12574. }
  12575. parseStatement() {
  12576. return this.parseStatementLike(0);
  12577. }
  12578. parseStatementLike(flags) {
  12579. let decorators = null;
  12580. if (this.match(26)) {
  12581. decorators = this.parseDecorators(true);
  12582. }
  12583. return this.parseStatementContent(flags, decorators);
  12584. }
  12585. parseStatementContent(flags, decorators) {
  12586. const starttype = this.state.type;
  12587. const node = this.startNode();
  12588. const allowDeclaration = !!(flags & 2);
  12589. const allowFunctionDeclaration = !!(flags & 4);
  12590. const topLevel = flags & 1;
  12591. switch (starttype) {
  12592. case 60:
  12593. return this.parseBreakContinueStatement(node, true);
  12594. case 63:
  12595. return this.parseBreakContinueStatement(node, false);
  12596. case 64:
  12597. return this.parseDebuggerStatement(node);
  12598. case 90:
  12599. return this.parseDoWhileStatement(node);
  12600. case 91:
  12601. return this.parseForStatement(node);
  12602. case 68:
  12603. if (this.lookaheadCharCode() === 46) break;
  12604. if (!allowFunctionDeclaration) {
  12605. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, {
  12606. at: this.state.startLoc
  12607. });
  12608. }
  12609. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12610. case 80:
  12611. if (!allowDeclaration) this.unexpected();
  12612. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12613. case 69:
  12614. return this.parseIfStatement(node);
  12615. case 70:
  12616. return this.parseReturnStatement(node);
  12617. case 71:
  12618. return this.parseSwitchStatement(node);
  12619. case 72:
  12620. return this.parseThrowStatement(node);
  12621. case 73:
  12622. return this.parseTryStatement(node);
  12623. case 96:
  12624. if (!this.state.containsEsc && this.startsAwaitUsing()) {
  12625. if (!this.isAwaitAllowed()) {
  12626. this.raise(Errors.AwaitUsingNotInAsyncContext, {
  12627. at: node
  12628. });
  12629. } else if (!allowDeclaration) {
  12630. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12631. at: node
  12632. });
  12633. }
  12634. this.next();
  12635. return this.parseVarStatement(node, "await using");
  12636. }
  12637. break;
  12638. case 105:
  12639. if (this.state.containsEsc || !this.hasInLineFollowingBindingIdentifier()) {
  12640. break;
  12641. }
  12642. this.expectPlugin("explicitResourceManagement");
  12643. if (!this.scope.inModule && this.scope.inTopLevel) {
  12644. this.raise(Errors.UnexpectedUsingDeclaration, {
  12645. at: this.state.startLoc
  12646. });
  12647. } else if (!allowDeclaration) {
  12648. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12649. at: this.state.startLoc
  12650. });
  12651. }
  12652. return this.parseVarStatement(node, "using");
  12653. case 99:
  12654. {
  12655. if (this.state.containsEsc) {
  12656. break;
  12657. }
  12658. const next = this.nextTokenStart();
  12659. const nextCh = this.codePointAtPos(next);
  12660. if (nextCh !== 91) {
  12661. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12662. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12663. break;
  12664. }
  12665. }
  12666. }
  12667. case 75:
  12668. {
  12669. if (!allowDeclaration) {
  12670. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12671. at: this.state.startLoc
  12672. });
  12673. }
  12674. }
  12675. case 74:
  12676. {
  12677. const kind = this.state.value;
  12678. return this.parseVarStatement(node, kind);
  12679. }
  12680. case 92:
  12681. return this.parseWhileStatement(node);
  12682. case 76:
  12683. return this.parseWithStatement(node);
  12684. case 5:
  12685. return this.parseBlock();
  12686. case 13:
  12687. return this.parseEmptyStatement(node);
  12688. case 83:
  12689. {
  12690. const nextTokenCharCode = this.lookaheadCharCode();
  12691. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12692. break;
  12693. }
  12694. }
  12695. case 82:
  12696. {
  12697. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12698. this.raise(Errors.UnexpectedImportExport, {
  12699. at: this.state.startLoc
  12700. });
  12701. }
  12702. this.next();
  12703. let result;
  12704. if (starttype === 83) {
  12705. result = this.parseImport(node);
  12706. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12707. this.sawUnambiguousESM = true;
  12708. }
  12709. } else {
  12710. result = this.parseExport(node, decorators);
  12711. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12712. this.sawUnambiguousESM = true;
  12713. }
  12714. }
  12715. this.assertModuleNodeAllowed(result);
  12716. return result;
  12717. }
  12718. default:
  12719. {
  12720. if (this.isAsyncFunction()) {
  12721. if (!allowDeclaration) {
  12722. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12723. at: this.state.startLoc
  12724. });
  12725. }
  12726. this.next();
  12727. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12728. }
  12729. }
  12730. }
  12731. const maybeName = this.state.value;
  12732. const expr = this.parseExpression();
  12733. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12734. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12735. } else {
  12736. return this.parseExpressionStatement(node, expr, decorators);
  12737. }
  12738. }
  12739. assertModuleNodeAllowed(node) {
  12740. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12741. this.raise(Errors.ImportOutsideModule, {
  12742. at: node
  12743. });
  12744. }
  12745. }
  12746. decoratorsEnabledBeforeExport() {
  12747. if (this.hasPlugin("decorators-legacy")) return true;
  12748. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12749. }
  12750. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12751. if (maybeDecorators) {
  12752. if (classNode.decorators && classNode.decorators.length > 0) {
  12753. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12754. this.raise(Errors.DecoratorsBeforeAfterExport, {
  12755. at: classNode.decorators[0]
  12756. });
  12757. }
  12758. classNode.decorators.unshift(...maybeDecorators);
  12759. } else {
  12760. classNode.decorators = maybeDecorators;
  12761. }
  12762. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12763. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12764. }
  12765. return classNode;
  12766. }
  12767. canHaveLeadingDecorator() {
  12768. return this.match(80);
  12769. }
  12770. parseDecorators(allowExport) {
  12771. const decorators = [];
  12772. do {
  12773. decorators.push(this.parseDecorator());
  12774. } while (this.match(26));
  12775. if (this.match(82)) {
  12776. if (!allowExport) {
  12777. this.unexpected();
  12778. }
  12779. if (!this.decoratorsEnabledBeforeExport()) {
  12780. this.raise(Errors.DecoratorExportClass, {
  12781. at: this.state.startLoc
  12782. });
  12783. }
  12784. } else if (!this.canHaveLeadingDecorator()) {
  12785. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12786. at: this.state.startLoc
  12787. });
  12788. }
  12789. return decorators;
  12790. }
  12791. parseDecorator() {
  12792. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12793. const node = this.startNode();
  12794. this.next();
  12795. if (this.hasPlugin("decorators")) {
  12796. const startLoc = this.state.startLoc;
  12797. let expr;
  12798. if (this.match(10)) {
  12799. const startLoc = this.state.startLoc;
  12800. this.next();
  12801. expr = this.parseExpression();
  12802. this.expect(11);
  12803. expr = this.wrapParenthesis(startLoc, expr);
  12804. const paramsStartLoc = this.state.startLoc;
  12805. node.expression = this.parseMaybeDecoratorArguments(expr);
  12806. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12807. this.raise(Errors.DecoratorArgumentsOutsideParentheses, {
  12808. at: paramsStartLoc
  12809. });
  12810. }
  12811. } else {
  12812. expr = this.parseIdentifier(false);
  12813. while (this.eat(16)) {
  12814. const node = this.startNodeAt(startLoc);
  12815. node.object = expr;
  12816. if (this.match(136)) {
  12817. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12818. node.property = this.parsePrivateName();
  12819. } else {
  12820. node.property = this.parseIdentifier(true);
  12821. }
  12822. node.computed = false;
  12823. expr = this.finishNode(node, "MemberExpression");
  12824. }
  12825. node.expression = this.parseMaybeDecoratorArguments(expr);
  12826. }
  12827. } else {
  12828. node.expression = this.parseExprSubscripts();
  12829. }
  12830. return this.finishNode(node, "Decorator");
  12831. }
  12832. parseMaybeDecoratorArguments(expr) {
  12833. if (this.eat(10)) {
  12834. const node = this.startNodeAtNode(expr);
  12835. node.callee = expr;
  12836. node.arguments = this.parseCallExpressionArguments(11, false);
  12837. this.toReferencedList(node.arguments);
  12838. return this.finishNode(node, "CallExpression");
  12839. }
  12840. return expr;
  12841. }
  12842. parseBreakContinueStatement(node, isBreak) {
  12843. this.next();
  12844. if (this.isLineTerminator()) {
  12845. node.label = null;
  12846. } else {
  12847. node.label = this.parseIdentifier();
  12848. this.semicolon();
  12849. }
  12850. this.verifyBreakContinue(node, isBreak);
  12851. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12852. }
  12853. verifyBreakContinue(node, isBreak) {
  12854. let i;
  12855. for (i = 0; i < this.state.labels.length; ++i) {
  12856. const lab = this.state.labels[i];
  12857. if (node.label == null || lab.name === node.label.name) {
  12858. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12859. if (node.label && isBreak) break;
  12860. }
  12861. }
  12862. if (i === this.state.labels.length) {
  12863. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12864. this.raise(Errors.IllegalBreakContinue, {
  12865. at: node,
  12866. type
  12867. });
  12868. }
  12869. }
  12870. parseDebuggerStatement(node) {
  12871. this.next();
  12872. this.semicolon();
  12873. return this.finishNode(node, "DebuggerStatement");
  12874. }
  12875. parseHeaderExpression() {
  12876. this.expect(10);
  12877. const val = this.parseExpression();
  12878. this.expect(11);
  12879. return val;
  12880. }
  12881. parseDoWhileStatement(node) {
  12882. this.next();
  12883. this.state.labels.push(loopLabel);
  12884. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12885. this.state.labels.pop();
  12886. this.expect(92);
  12887. node.test = this.parseHeaderExpression();
  12888. this.eat(13);
  12889. return this.finishNode(node, "DoWhileStatement");
  12890. }
  12891. parseForStatement(node) {
  12892. this.next();
  12893. this.state.labels.push(loopLabel);
  12894. let awaitAt = null;
  12895. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12896. awaitAt = this.state.lastTokStartLoc;
  12897. }
  12898. this.scope.enter(SCOPE_OTHER);
  12899. this.expect(10);
  12900. if (this.match(13)) {
  12901. if (awaitAt !== null) {
  12902. this.unexpected(awaitAt);
  12903. }
  12904. return this.parseFor(node, null);
  12905. }
  12906. const startsWithLet = this.isContextual(99);
  12907. {
  12908. const startsWithAwaitUsing = this.isContextual(96) && this.startsAwaitUsing();
  12909. const starsWithUsingDeclaration = startsWithAwaitUsing || this.isContextual(105) && this.startsUsingForOf();
  12910. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || starsWithUsingDeclaration;
  12911. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12912. const initNode = this.startNode();
  12913. let kind;
  12914. if (startsWithAwaitUsing) {
  12915. kind = "await using";
  12916. if (!this.isAwaitAllowed()) {
  12917. this.raise(Errors.AwaitUsingNotInAsyncContext, {
  12918. at: this.state.startLoc
  12919. });
  12920. }
  12921. this.next();
  12922. } else {
  12923. kind = this.state.value;
  12924. }
  12925. this.next();
  12926. this.parseVar(initNode, true, kind);
  12927. const init = this.finishNode(initNode, "VariableDeclaration");
  12928. const isForIn = this.match(58);
  12929. if (isForIn && starsWithUsingDeclaration) {
  12930. this.raise(Errors.ForInUsing, {
  12931. at: init
  12932. });
  12933. }
  12934. if ((isForIn || this.isContextual(101)) && init.declarations.length === 1) {
  12935. return this.parseForIn(node, init, awaitAt);
  12936. }
  12937. if (awaitAt !== null) {
  12938. this.unexpected(awaitAt);
  12939. }
  12940. return this.parseFor(node, init);
  12941. }
  12942. }
  12943. const startsWithAsync = this.isContextual(95);
  12944. const refExpressionErrors = new ExpressionErrors();
  12945. const init = this.parseExpression(true, refExpressionErrors);
  12946. const isForOf = this.isContextual(101);
  12947. if (isForOf) {
  12948. if (startsWithLet) {
  12949. this.raise(Errors.ForOfLet, {
  12950. at: init
  12951. });
  12952. }
  12953. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12954. this.raise(Errors.ForOfAsync, {
  12955. at: init
  12956. });
  12957. }
  12958. }
  12959. if (isForOf || this.match(58)) {
  12960. this.checkDestructuringPrivate(refExpressionErrors);
  12961. this.toAssignable(init, true);
  12962. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12963. this.checkLVal(init, {
  12964. in: {
  12965. type
  12966. }
  12967. });
  12968. return this.parseForIn(node, init, awaitAt);
  12969. } else {
  12970. this.checkExpressionErrors(refExpressionErrors, true);
  12971. }
  12972. if (awaitAt !== null) {
  12973. this.unexpected(awaitAt);
  12974. }
  12975. return this.parseFor(node, init);
  12976. }
  12977. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  12978. this.next();
  12979. return this.parseFunction(node, 1 | (isHangingDeclaration ? 2 : 0) | (isAsync ? 8 : 0));
  12980. }
  12981. parseIfStatement(node) {
  12982. this.next();
  12983. node.test = this.parseHeaderExpression();
  12984. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  12985. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  12986. return this.finishNode(node, "IfStatement");
  12987. }
  12988. parseReturnStatement(node) {
  12989. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12990. this.raise(Errors.IllegalReturn, {
  12991. at: this.state.startLoc
  12992. });
  12993. }
  12994. this.next();
  12995. if (this.isLineTerminator()) {
  12996. node.argument = null;
  12997. } else {
  12998. node.argument = this.parseExpression();
  12999. this.semicolon();
  13000. }
  13001. return this.finishNode(node, "ReturnStatement");
  13002. }
  13003. parseSwitchStatement(node) {
  13004. this.next();
  13005. node.discriminant = this.parseHeaderExpression();
  13006. const cases = node.cases = [];
  13007. this.expect(5);
  13008. this.state.labels.push(switchLabel);
  13009. this.scope.enter(SCOPE_OTHER);
  13010. let cur;
  13011. for (let sawDefault; !this.match(8);) {
  13012. if (this.match(61) || this.match(65)) {
  13013. const isCase = this.match(61);
  13014. if (cur) this.finishNode(cur, "SwitchCase");
  13015. cases.push(cur = this.startNode());
  13016. cur.consequent = [];
  13017. this.next();
  13018. if (isCase) {
  13019. cur.test = this.parseExpression();
  13020. } else {
  13021. if (sawDefault) {
  13022. this.raise(Errors.MultipleDefaultsInSwitch, {
  13023. at: this.state.lastTokStartLoc
  13024. });
  13025. }
  13026. sawDefault = true;
  13027. cur.test = null;
  13028. }
  13029. this.expect(14);
  13030. } else {
  13031. if (cur) {
  13032. cur.consequent.push(this.parseStatementListItem());
  13033. } else {
  13034. this.unexpected();
  13035. }
  13036. }
  13037. }
  13038. this.scope.exit();
  13039. if (cur) this.finishNode(cur, "SwitchCase");
  13040. this.next();
  13041. this.state.labels.pop();
  13042. return this.finishNode(node, "SwitchStatement");
  13043. }
  13044. parseThrowStatement(node) {
  13045. this.next();
  13046. if (this.hasPrecedingLineBreak()) {
  13047. this.raise(Errors.NewlineAfterThrow, {
  13048. at: this.state.lastTokEndLoc
  13049. });
  13050. }
  13051. node.argument = this.parseExpression();
  13052. this.semicolon();
  13053. return this.finishNode(node, "ThrowStatement");
  13054. }
  13055. parseCatchClauseParam() {
  13056. const param = this.parseBindingAtom();
  13057. this.scope.enter(this.options.annexB && param.type === "Identifier" ? SCOPE_SIMPLE_CATCH : 0);
  13058. this.checkLVal(param, {
  13059. in: {
  13060. type: "CatchClause"
  13061. },
  13062. binding: BIND_CATCH_PARAM
  13063. });
  13064. return param;
  13065. }
  13066. parseTryStatement(node) {
  13067. this.next();
  13068. node.block = this.parseBlock();
  13069. node.handler = null;
  13070. if (this.match(62)) {
  13071. const clause = this.startNode();
  13072. this.next();
  13073. if (this.match(10)) {
  13074. this.expect(10);
  13075. clause.param = this.parseCatchClauseParam();
  13076. this.expect(11);
  13077. } else {
  13078. clause.param = null;
  13079. this.scope.enter(SCOPE_OTHER);
  13080. }
  13081. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  13082. this.scope.exit();
  13083. node.handler = this.finishNode(clause, "CatchClause");
  13084. }
  13085. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  13086. if (!node.handler && !node.finalizer) {
  13087. this.raise(Errors.NoCatchOrFinally, {
  13088. at: node
  13089. });
  13090. }
  13091. return this.finishNode(node, "TryStatement");
  13092. }
  13093. parseVarStatement(node, kind, allowMissingInitializer = false) {
  13094. this.next();
  13095. this.parseVar(node, false, kind, allowMissingInitializer);
  13096. this.semicolon();
  13097. return this.finishNode(node, "VariableDeclaration");
  13098. }
  13099. parseWhileStatement(node) {
  13100. this.next();
  13101. node.test = this.parseHeaderExpression();
  13102. this.state.labels.push(loopLabel);
  13103. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13104. this.state.labels.pop();
  13105. return this.finishNode(node, "WhileStatement");
  13106. }
  13107. parseWithStatement(node) {
  13108. if (this.state.strict) {
  13109. this.raise(Errors.StrictWith, {
  13110. at: this.state.startLoc
  13111. });
  13112. }
  13113. this.next();
  13114. node.object = this.parseHeaderExpression();
  13115. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13116. return this.finishNode(node, "WithStatement");
  13117. }
  13118. parseEmptyStatement(node) {
  13119. this.next();
  13120. return this.finishNode(node, "EmptyStatement");
  13121. }
  13122. parseLabeledStatement(node, maybeName, expr, flags) {
  13123. for (const label of this.state.labels) {
  13124. if (label.name === maybeName) {
  13125. this.raise(Errors.LabelRedeclaration, {
  13126. at: expr,
  13127. labelName: maybeName
  13128. });
  13129. }
  13130. }
  13131. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  13132. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  13133. const label = this.state.labels[i];
  13134. if (label.statementStart === node.start) {
  13135. label.statementStart = this.state.start;
  13136. label.kind = kind;
  13137. } else {
  13138. break;
  13139. }
  13140. }
  13141. this.state.labels.push({
  13142. name: maybeName,
  13143. kind: kind,
  13144. statementStart: this.state.start
  13145. });
  13146. node.body = flags & 8 ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  13147. this.state.labels.pop();
  13148. node.label = expr;
  13149. return this.finishNode(node, "LabeledStatement");
  13150. }
  13151. parseExpressionStatement(node, expr, decorators) {
  13152. node.expression = expr;
  13153. this.semicolon();
  13154. return this.finishNode(node, "ExpressionStatement");
  13155. }
  13156. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  13157. const node = this.startNode();
  13158. if (allowDirectives) {
  13159. this.state.strictErrors.clear();
  13160. }
  13161. this.expect(5);
  13162. if (createNewLexicalScope) {
  13163. this.scope.enter(SCOPE_OTHER);
  13164. }
  13165. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  13166. if (createNewLexicalScope) {
  13167. this.scope.exit();
  13168. }
  13169. return this.finishNode(node, "BlockStatement");
  13170. }
  13171. isValidDirective(stmt) {
  13172. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  13173. }
  13174. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  13175. const body = node.body = [];
  13176. const directives = node.directives = [];
  13177. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  13178. }
  13179. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  13180. const oldStrict = this.state.strict;
  13181. let hasStrictModeDirective = false;
  13182. let parsedNonDirective = false;
  13183. while (!this.match(end)) {
  13184. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  13185. if (directives && !parsedNonDirective) {
  13186. if (this.isValidDirective(stmt)) {
  13187. const directive = this.stmtToDirective(stmt);
  13188. directives.push(directive);
  13189. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  13190. hasStrictModeDirective = true;
  13191. this.setStrict(true);
  13192. }
  13193. continue;
  13194. }
  13195. parsedNonDirective = true;
  13196. this.state.strictErrors.clear();
  13197. }
  13198. body.push(stmt);
  13199. }
  13200. if (afterBlockParse) {
  13201. afterBlockParse.call(this, hasStrictModeDirective);
  13202. }
  13203. if (!oldStrict) {
  13204. this.setStrict(false);
  13205. }
  13206. this.next();
  13207. }
  13208. parseFor(node, init) {
  13209. node.init = init;
  13210. this.semicolon(false);
  13211. node.test = this.match(13) ? null : this.parseExpression();
  13212. this.semicolon(false);
  13213. node.update = this.match(11) ? null : this.parseExpression();
  13214. this.expect(11);
  13215. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13216. this.scope.exit();
  13217. this.state.labels.pop();
  13218. return this.finishNode(node, "ForStatement");
  13219. }
  13220. parseForIn(node, init, awaitAt) {
  13221. const isForIn = this.match(58);
  13222. this.next();
  13223. if (isForIn) {
  13224. if (awaitAt !== null) this.unexpected(awaitAt);
  13225. } else {
  13226. node.await = awaitAt !== null;
  13227. }
  13228. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  13229. this.raise(Errors.ForInOfLoopInitializer, {
  13230. at: init,
  13231. type: isForIn ? "ForInStatement" : "ForOfStatement"
  13232. });
  13233. }
  13234. if (init.type === "AssignmentPattern") {
  13235. this.raise(Errors.InvalidLhs, {
  13236. at: init,
  13237. ancestor: {
  13238. type: "ForStatement"
  13239. }
  13240. });
  13241. }
  13242. node.left = init;
  13243. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  13244. this.expect(11);
  13245. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13246. this.scope.exit();
  13247. this.state.labels.pop();
  13248. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  13249. }
  13250. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  13251. const declarations = node.declarations = [];
  13252. node.kind = kind;
  13253. for (;;) {
  13254. const decl = this.startNode();
  13255. this.parseVarId(decl, kind);
  13256. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  13257. if (decl.init === null && !allowMissingInitializer) {
  13258. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  13259. this.raise(Errors.DeclarationMissingInitializer, {
  13260. at: this.state.lastTokEndLoc,
  13261. kind: "destructuring"
  13262. });
  13263. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  13264. this.raise(Errors.DeclarationMissingInitializer, {
  13265. at: this.state.lastTokEndLoc,
  13266. kind: "const"
  13267. });
  13268. }
  13269. }
  13270. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13271. if (!this.eat(12)) break;
  13272. }
  13273. return node;
  13274. }
  13275. parseVarId(decl, kind) {
  13276. const id = this.parseBindingAtom();
  13277. this.checkLVal(id, {
  13278. in: {
  13279. type: "VariableDeclarator"
  13280. },
  13281. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  13282. });
  13283. decl.id = id;
  13284. }
  13285. parseAsyncFunctionExpression(node) {
  13286. return this.parseFunction(node, 8);
  13287. }
  13288. parseFunction(node, flags = 0) {
  13289. const hangingDeclaration = flags & 2;
  13290. const isDeclaration = !!(flags & 1);
  13291. const requireId = isDeclaration && !(flags & 4);
  13292. const isAsync = !!(flags & 8);
  13293. this.initFunction(node, isAsync);
  13294. if (this.match(55)) {
  13295. if (hangingDeclaration) {
  13296. this.raise(Errors.GeneratorInSingleStatementContext, {
  13297. at: this.state.startLoc
  13298. });
  13299. }
  13300. this.next();
  13301. node.generator = true;
  13302. }
  13303. if (isDeclaration) {
  13304. node.id = this.parseFunctionId(requireId);
  13305. }
  13306. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13307. this.state.maybeInArrowParameters = false;
  13308. this.scope.enter(SCOPE_FUNCTION);
  13309. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13310. if (!isDeclaration) {
  13311. node.id = this.parseFunctionId();
  13312. }
  13313. this.parseFunctionParams(node, false);
  13314. this.withSmartMixTopicForbiddingContext(() => {
  13315. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  13316. });
  13317. this.prodParam.exit();
  13318. this.scope.exit();
  13319. if (isDeclaration && !hangingDeclaration) {
  13320. this.registerFunctionStatementId(node);
  13321. }
  13322. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13323. return node;
  13324. }
  13325. parseFunctionId(requireId) {
  13326. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13327. }
  13328. parseFunctionParams(node, isConstructor) {
  13329. this.expect(10);
  13330. this.expressionScope.enter(newParameterDeclarationScope());
  13331. node.params = this.parseBindingList(11, 41, 2 | (isConstructor ? 4 : 0));
  13332. this.expressionScope.exit();
  13333. }
  13334. registerFunctionStatementId(node) {
  13335. if (!node.id) return;
  13336. this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
  13337. }
  13338. parseClass(node, isStatement, optionalId) {
  13339. this.next();
  13340. const oldStrict = this.state.strict;
  13341. this.state.strict = true;
  13342. this.parseClassId(node, isStatement, optionalId);
  13343. this.parseClassSuper(node);
  13344. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13345. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13346. }
  13347. isClassProperty() {
  13348. return this.match(29) || this.match(13) || this.match(8);
  13349. }
  13350. isClassMethod() {
  13351. return this.match(10);
  13352. }
  13353. isNonstaticConstructor(method) {
  13354. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  13355. }
  13356. parseClassBody(hadSuperClass, oldStrict) {
  13357. this.classScope.enter();
  13358. const state = {
  13359. hadConstructor: false,
  13360. hadSuperClass
  13361. };
  13362. let decorators = [];
  13363. const classBody = this.startNode();
  13364. classBody.body = [];
  13365. this.expect(5);
  13366. this.withSmartMixTopicForbiddingContext(() => {
  13367. while (!this.match(8)) {
  13368. if (this.eat(13)) {
  13369. if (decorators.length > 0) {
  13370. throw this.raise(Errors.DecoratorSemicolon, {
  13371. at: this.state.lastTokEndLoc
  13372. });
  13373. }
  13374. continue;
  13375. }
  13376. if (this.match(26)) {
  13377. decorators.push(this.parseDecorator());
  13378. continue;
  13379. }
  13380. const member = this.startNode();
  13381. if (decorators.length) {
  13382. member.decorators = decorators;
  13383. this.resetStartLocationFromNode(member, decorators[0]);
  13384. decorators = [];
  13385. }
  13386. this.parseClassMember(classBody, member, state);
  13387. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13388. this.raise(Errors.DecoratorConstructor, {
  13389. at: member
  13390. });
  13391. }
  13392. }
  13393. });
  13394. this.state.strict = oldStrict;
  13395. this.next();
  13396. if (decorators.length) {
  13397. throw this.raise(Errors.TrailingDecorator, {
  13398. at: this.state.startLoc
  13399. });
  13400. }
  13401. this.classScope.exit();
  13402. return this.finishNode(classBody, "ClassBody");
  13403. }
  13404. parseClassMemberFromModifier(classBody, member) {
  13405. const key = this.parseIdentifier(true);
  13406. if (this.isClassMethod()) {
  13407. const method = member;
  13408. method.kind = "method";
  13409. method.computed = false;
  13410. method.key = key;
  13411. method.static = false;
  13412. this.pushClassMethod(classBody, method, false, false, false, false);
  13413. return true;
  13414. } else if (this.isClassProperty()) {
  13415. const prop = member;
  13416. prop.computed = false;
  13417. prop.key = key;
  13418. prop.static = false;
  13419. classBody.body.push(this.parseClassProperty(prop));
  13420. return true;
  13421. }
  13422. this.resetPreviousNodeTrailingComments(key);
  13423. return false;
  13424. }
  13425. parseClassMember(classBody, member, state) {
  13426. const isStatic = this.isContextual(104);
  13427. if (isStatic) {
  13428. if (this.parseClassMemberFromModifier(classBody, member)) {
  13429. return;
  13430. }
  13431. if (this.eat(5)) {
  13432. this.parseClassStaticBlock(classBody, member);
  13433. return;
  13434. }
  13435. }
  13436. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13437. }
  13438. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13439. const publicMethod = member;
  13440. const privateMethod = member;
  13441. const publicProp = member;
  13442. const privateProp = member;
  13443. const accessorProp = member;
  13444. const method = publicMethod;
  13445. const publicMember = publicMethod;
  13446. member.static = isStatic;
  13447. this.parsePropertyNamePrefixOperator(member);
  13448. if (this.eat(55)) {
  13449. method.kind = "method";
  13450. const isPrivateName = this.match(136);
  13451. this.parseClassElementName(method);
  13452. if (isPrivateName) {
  13453. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13454. return;
  13455. }
  13456. if (this.isNonstaticConstructor(publicMethod)) {
  13457. this.raise(Errors.ConstructorIsGenerator, {
  13458. at: publicMethod.key
  13459. });
  13460. }
  13461. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13462. return;
  13463. }
  13464. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  13465. const isPrivate = this.match(136);
  13466. const key = this.parseClassElementName(member);
  13467. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13468. this.parsePostMemberNameModifiers(publicMember);
  13469. if (this.isClassMethod()) {
  13470. method.kind = "method";
  13471. if (isPrivate) {
  13472. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13473. return;
  13474. }
  13475. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13476. let allowsDirectSuper = false;
  13477. if (isConstructor) {
  13478. publicMethod.kind = "constructor";
  13479. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13480. this.raise(Errors.DuplicateConstructor, {
  13481. at: key
  13482. });
  13483. }
  13484. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13485. this.raise(Errors.OverrideOnConstructor, {
  13486. at: key
  13487. });
  13488. }
  13489. state.hadConstructor = true;
  13490. allowsDirectSuper = state.hadSuperClass;
  13491. }
  13492. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13493. } else if (this.isClassProperty()) {
  13494. if (isPrivate) {
  13495. this.pushClassPrivateProperty(classBody, privateProp);
  13496. } else {
  13497. this.pushClassProperty(classBody, publicProp);
  13498. }
  13499. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  13500. this.resetPreviousNodeTrailingComments(key);
  13501. const isGenerator = this.eat(55);
  13502. if (publicMember.optional) {
  13503. this.unexpected(maybeQuestionTokenStartLoc);
  13504. }
  13505. method.kind = "method";
  13506. const isPrivate = this.match(136);
  13507. this.parseClassElementName(method);
  13508. this.parsePostMemberNameModifiers(publicMember);
  13509. if (isPrivate) {
  13510. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13511. } else {
  13512. if (this.isNonstaticConstructor(publicMethod)) {
  13513. this.raise(Errors.ConstructorIsAsync, {
  13514. at: publicMethod.key
  13515. });
  13516. }
  13517. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13518. }
  13519. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  13520. this.resetPreviousNodeTrailingComments(key);
  13521. method.kind = key.name;
  13522. const isPrivate = this.match(136);
  13523. this.parseClassElementName(publicMethod);
  13524. if (isPrivate) {
  13525. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13526. } else {
  13527. if (this.isNonstaticConstructor(publicMethod)) {
  13528. this.raise(Errors.ConstructorIsAccessor, {
  13529. at: publicMethod.key
  13530. });
  13531. }
  13532. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13533. }
  13534. this.checkGetterSetterParams(publicMethod);
  13535. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  13536. this.expectPlugin("decoratorAutoAccessors");
  13537. this.resetPreviousNodeTrailingComments(key);
  13538. const isPrivate = this.match(136);
  13539. this.parseClassElementName(publicProp);
  13540. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13541. } else if (this.isLineTerminator()) {
  13542. if (isPrivate) {
  13543. this.pushClassPrivateProperty(classBody, privateProp);
  13544. } else {
  13545. this.pushClassProperty(classBody, publicProp);
  13546. }
  13547. } else {
  13548. this.unexpected();
  13549. }
  13550. }
  13551. parseClassElementName(member) {
  13552. const {
  13553. type,
  13554. value
  13555. } = this.state;
  13556. if ((type === 130 || type === 131) && member.static && value === "prototype") {
  13557. this.raise(Errors.StaticPrototype, {
  13558. at: this.state.startLoc
  13559. });
  13560. }
  13561. if (type === 136) {
  13562. if (value === "constructor") {
  13563. this.raise(Errors.ConstructorClassPrivateField, {
  13564. at: this.state.startLoc
  13565. });
  13566. }
  13567. const key = this.parsePrivateName();
  13568. member.key = key;
  13569. return key;
  13570. }
  13571. return this.parsePropertyName(member);
  13572. }
  13573. parseClassStaticBlock(classBody, member) {
  13574. var _member$decorators;
  13575. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13576. const oldLabels = this.state.labels;
  13577. this.state.labels = [];
  13578. this.prodParam.enter(PARAM);
  13579. const body = member.body = [];
  13580. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13581. this.prodParam.exit();
  13582. this.scope.exit();
  13583. this.state.labels = oldLabels;
  13584. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13585. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13586. this.raise(Errors.DecoratorStaticBlock, {
  13587. at: member
  13588. });
  13589. }
  13590. }
  13591. pushClassProperty(classBody, prop) {
  13592. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13593. this.raise(Errors.ConstructorClassField, {
  13594. at: prop.key
  13595. });
  13596. }
  13597. classBody.body.push(this.parseClassProperty(prop));
  13598. }
  13599. pushClassPrivateProperty(classBody, prop) {
  13600. const node = this.parseClassPrivateProperty(prop);
  13601. classBody.body.push(node);
  13602. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13603. }
  13604. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13605. if (!isPrivate && !prop.computed) {
  13606. const key = prop.key;
  13607. if (key.name === "constructor" || key.value === "constructor") {
  13608. this.raise(Errors.ConstructorClassField, {
  13609. at: key
  13610. });
  13611. }
  13612. }
  13613. const node = this.parseClassAccessorProperty(prop);
  13614. classBody.body.push(node);
  13615. if (isPrivate) {
  13616. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13617. }
  13618. }
  13619. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13620. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13621. }
  13622. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13623. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13624. classBody.body.push(node);
  13625. const kind = node.kind === "get" ? node.static ? CLASS_ELEMENT_STATIC_GETTER : CLASS_ELEMENT_INSTANCE_GETTER : node.kind === "set" ? node.static ? CLASS_ELEMENT_STATIC_SETTER : CLASS_ELEMENT_INSTANCE_SETTER : CLASS_ELEMENT_OTHER;
  13626. this.declareClassPrivateMethodInScope(node, kind);
  13627. }
  13628. declareClassPrivateMethodInScope(node, kind) {
  13629. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13630. }
  13631. parsePostMemberNameModifiers(methodOrProp) {}
  13632. parseClassPrivateProperty(node) {
  13633. this.parseInitializer(node);
  13634. this.semicolon();
  13635. return this.finishNode(node, "ClassPrivateProperty");
  13636. }
  13637. parseClassProperty(node) {
  13638. this.parseInitializer(node);
  13639. this.semicolon();
  13640. return this.finishNode(node, "ClassProperty");
  13641. }
  13642. parseClassAccessorProperty(node) {
  13643. this.parseInitializer(node);
  13644. this.semicolon();
  13645. return this.finishNode(node, "ClassAccessorProperty");
  13646. }
  13647. parseInitializer(node) {
  13648. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13649. this.expressionScope.enter(newExpressionScope());
  13650. this.prodParam.enter(PARAM);
  13651. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13652. this.expressionScope.exit();
  13653. this.prodParam.exit();
  13654. this.scope.exit();
  13655. }
  13656. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13657. if (tokenIsIdentifier(this.state.type)) {
  13658. node.id = this.parseIdentifier();
  13659. if (isStatement) {
  13660. this.declareNameFromIdentifier(node.id, bindingType);
  13661. }
  13662. } else {
  13663. if (optionalId || !isStatement) {
  13664. node.id = null;
  13665. } else {
  13666. throw this.raise(Errors.MissingClassName, {
  13667. at: this.state.startLoc
  13668. });
  13669. }
  13670. }
  13671. }
  13672. parseClassSuper(node) {
  13673. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13674. }
  13675. parseExport(node, decorators) {
  13676. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, true);
  13677. const hasDefault = this.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  13678. const parseAfterDefault = !hasDefault || this.eat(12);
  13679. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13680. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13681. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13682. const isFromRequired = hasDefault || hasStar;
  13683. if (hasStar && !hasNamespace) {
  13684. if (hasDefault) this.unexpected();
  13685. if (decorators) {
  13686. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13687. at: node
  13688. });
  13689. }
  13690. this.parseExportFrom(node, true);
  13691. return this.finishNode(node, "ExportAllDeclaration");
  13692. }
  13693. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13694. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13695. this.unexpected(null, 5);
  13696. }
  13697. if (hasNamespace && parseAfterNamespace) {
  13698. this.unexpected(null, 97);
  13699. }
  13700. let hasDeclaration;
  13701. if (isFromRequired || hasSpecifiers) {
  13702. hasDeclaration = false;
  13703. if (decorators) {
  13704. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13705. at: node
  13706. });
  13707. }
  13708. this.parseExportFrom(node, isFromRequired);
  13709. } else {
  13710. hasDeclaration = this.maybeParseExportDeclaration(node);
  13711. }
  13712. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13713. var _node2$declaration;
  13714. const node2 = node;
  13715. this.checkExport(node2, true, false, !!node2.source);
  13716. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13717. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13718. } else if (decorators) {
  13719. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13720. at: node
  13721. });
  13722. }
  13723. return this.finishNode(node2, "ExportNamedDeclaration");
  13724. }
  13725. if (this.eat(65)) {
  13726. const node2 = node;
  13727. const decl = this.parseExportDefaultExpression();
  13728. node2.declaration = decl;
  13729. if (decl.type === "ClassDeclaration") {
  13730. this.maybeTakeDecorators(decorators, decl, node2);
  13731. } else if (decorators) {
  13732. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13733. at: node
  13734. });
  13735. }
  13736. this.checkExport(node2, true, true);
  13737. return this.finishNode(node2, "ExportDefaultDeclaration");
  13738. }
  13739. this.unexpected(null, 5);
  13740. }
  13741. eatExportStar(node) {
  13742. return this.eat(55);
  13743. }
  13744. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  13745. if (maybeDefaultIdentifier || this.isExportDefaultSpecifier()) {
  13746. this.expectPlugin("exportDefaultFrom", maybeDefaultIdentifier == null ? void 0 : maybeDefaultIdentifier.loc.start);
  13747. const id = maybeDefaultIdentifier || this.parseIdentifier(true);
  13748. const specifier = this.startNodeAtNode(id);
  13749. specifier.exported = id;
  13750. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13751. return true;
  13752. }
  13753. return false;
  13754. }
  13755. maybeParseExportNamespaceSpecifier(node) {
  13756. if (this.isContextual(93)) {
  13757. if (!node.specifiers) node.specifiers = [];
  13758. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13759. this.next();
  13760. specifier.exported = this.parseModuleExportName();
  13761. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13762. return true;
  13763. }
  13764. return false;
  13765. }
  13766. maybeParseExportNamedSpecifiers(node) {
  13767. if (this.match(5)) {
  13768. if (!node.specifiers) node.specifiers = [];
  13769. const isTypeExport = node.exportKind === "type";
  13770. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13771. node.source = null;
  13772. node.declaration = null;
  13773. if (this.hasPlugin("importAssertions")) {
  13774. node.assertions = [];
  13775. }
  13776. return true;
  13777. }
  13778. return false;
  13779. }
  13780. maybeParseExportDeclaration(node) {
  13781. if (this.shouldParseExportDeclaration()) {
  13782. node.specifiers = [];
  13783. node.source = null;
  13784. if (this.hasPlugin("importAssertions")) {
  13785. node.assertions = [];
  13786. }
  13787. node.declaration = this.parseExportDeclaration(node);
  13788. return true;
  13789. }
  13790. return false;
  13791. }
  13792. isAsyncFunction() {
  13793. if (!this.isContextual(95)) return false;
  13794. const next = this.nextTokenInLineStart();
  13795. return this.isUnparsedContextual(next, "function");
  13796. }
  13797. parseExportDefaultExpression() {
  13798. const expr = this.startNode();
  13799. if (this.match(68)) {
  13800. this.next();
  13801. return this.parseFunction(expr, 1 | 4);
  13802. } else if (this.isAsyncFunction()) {
  13803. this.next();
  13804. this.next();
  13805. return this.parseFunction(expr, 1 | 4 | 8);
  13806. }
  13807. if (this.match(80)) {
  13808. return this.parseClass(expr, true, true);
  13809. }
  13810. if (this.match(26)) {
  13811. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13812. this.raise(Errors.DecoratorBeforeExport, {
  13813. at: this.state.startLoc
  13814. });
  13815. }
  13816. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13817. }
  13818. if (this.match(75) || this.match(74) || this.isLet()) {
  13819. throw this.raise(Errors.UnsupportedDefaultExport, {
  13820. at: this.state.startLoc
  13821. });
  13822. }
  13823. const res = this.parseMaybeAssignAllowIn();
  13824. this.semicolon();
  13825. return res;
  13826. }
  13827. parseExportDeclaration(node) {
  13828. if (this.match(80)) {
  13829. const node = this.parseClass(this.startNode(), true, false);
  13830. return node;
  13831. }
  13832. return this.parseStatementListItem();
  13833. }
  13834. isExportDefaultSpecifier() {
  13835. const {
  13836. type
  13837. } = this.state;
  13838. if (tokenIsIdentifier(type)) {
  13839. if (type === 95 && !this.state.containsEsc || type === 99) {
  13840. return false;
  13841. }
  13842. if ((type === 128 || type === 127) && !this.state.containsEsc) {
  13843. const {
  13844. type: nextType
  13845. } = this.lookahead();
  13846. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13847. this.expectOnePlugin(["flow", "typescript"]);
  13848. return false;
  13849. }
  13850. }
  13851. } else if (!this.match(65)) {
  13852. return false;
  13853. }
  13854. const next = this.nextTokenStart();
  13855. const hasFrom = this.isUnparsedContextual(next, "from");
  13856. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13857. return true;
  13858. }
  13859. if (this.match(65) && hasFrom) {
  13860. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13861. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13862. }
  13863. return false;
  13864. }
  13865. parseExportFrom(node, expect) {
  13866. if (this.eatContextual(97)) {
  13867. node.source = this.parseImportSource();
  13868. this.checkExport(node);
  13869. this.maybeParseImportAttributes(node);
  13870. this.checkJSONModuleImport(node);
  13871. } else if (expect) {
  13872. this.unexpected();
  13873. }
  13874. this.semicolon();
  13875. }
  13876. shouldParseExportDeclaration() {
  13877. const {
  13878. type
  13879. } = this.state;
  13880. if (type === 26) {
  13881. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13882. if (this.hasPlugin("decorators")) {
  13883. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13884. this.raise(Errors.DecoratorBeforeExport, {
  13885. at: this.state.startLoc
  13886. });
  13887. }
  13888. return true;
  13889. }
  13890. }
  13891. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13892. }
  13893. checkExport(node, checkNames, isDefault, isFrom) {
  13894. if (checkNames) {
  13895. if (isDefault) {
  13896. this.checkDuplicateExports(node, "default");
  13897. if (this.hasPlugin("exportDefaultFrom")) {
  13898. var _declaration$extra;
  13899. const declaration = node.declaration;
  13900. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13901. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13902. at: declaration
  13903. });
  13904. }
  13905. }
  13906. } else if (node.specifiers && node.specifiers.length) {
  13907. for (const specifier of node.specifiers) {
  13908. const {
  13909. exported
  13910. } = specifier;
  13911. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13912. this.checkDuplicateExports(specifier, exportName);
  13913. if (!isFrom && specifier.local) {
  13914. const {
  13915. local
  13916. } = specifier;
  13917. if (local.type !== "Identifier") {
  13918. this.raise(Errors.ExportBindingIsString, {
  13919. at: specifier,
  13920. localName: local.value,
  13921. exportName
  13922. });
  13923. } else {
  13924. this.checkReservedWord(local.name, local.loc.start, true, false);
  13925. this.scope.checkLocalExport(local);
  13926. }
  13927. }
  13928. }
  13929. } else if (node.declaration) {
  13930. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13931. const id = node.declaration.id;
  13932. if (!id) throw new Error("Assertion failure");
  13933. this.checkDuplicateExports(node, id.name);
  13934. } else if (node.declaration.type === "VariableDeclaration") {
  13935. for (const declaration of node.declaration.declarations) {
  13936. this.checkDeclaration(declaration.id);
  13937. }
  13938. }
  13939. }
  13940. }
  13941. }
  13942. checkDeclaration(node) {
  13943. if (node.type === "Identifier") {
  13944. this.checkDuplicateExports(node, node.name);
  13945. } else if (node.type === "ObjectPattern") {
  13946. for (const prop of node.properties) {
  13947. this.checkDeclaration(prop);
  13948. }
  13949. } else if (node.type === "ArrayPattern") {
  13950. for (const elem of node.elements) {
  13951. if (elem) {
  13952. this.checkDeclaration(elem);
  13953. }
  13954. }
  13955. } else if (node.type === "ObjectProperty") {
  13956. this.checkDeclaration(node.value);
  13957. } else if (node.type === "RestElement") {
  13958. this.checkDeclaration(node.argument);
  13959. } else if (node.type === "AssignmentPattern") {
  13960. this.checkDeclaration(node.left);
  13961. }
  13962. }
  13963. checkDuplicateExports(node, exportName) {
  13964. if (this.exportedIdentifiers.has(exportName)) {
  13965. if (exportName === "default") {
  13966. this.raise(Errors.DuplicateDefaultExport, {
  13967. at: node
  13968. });
  13969. } else {
  13970. this.raise(Errors.DuplicateExport, {
  13971. at: node,
  13972. exportName
  13973. });
  13974. }
  13975. }
  13976. this.exportedIdentifiers.add(exportName);
  13977. }
  13978. parseExportSpecifiers(isInTypeExport) {
  13979. const nodes = [];
  13980. let first = true;
  13981. this.expect(5);
  13982. while (!this.eat(8)) {
  13983. if (first) {
  13984. first = false;
  13985. } else {
  13986. this.expect(12);
  13987. if (this.eat(8)) break;
  13988. }
  13989. const isMaybeTypeOnly = this.isContextual(128);
  13990. const isString = this.match(131);
  13991. const node = this.startNode();
  13992. node.local = this.parseModuleExportName();
  13993. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13994. }
  13995. return nodes;
  13996. }
  13997. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13998. if (this.eatContextual(93)) {
  13999. node.exported = this.parseModuleExportName();
  14000. } else if (isString) {
  14001. node.exported = cloneStringLiteral(node.local);
  14002. } else if (!node.exported) {
  14003. node.exported = cloneIdentifier(node.local);
  14004. }
  14005. return this.finishNode(node, "ExportSpecifier");
  14006. }
  14007. parseModuleExportName() {
  14008. if (this.match(131)) {
  14009. const result = this.parseStringLiteral(this.state.value);
  14010. const surrogate = result.value.match(loneSurrogate);
  14011. if (surrogate) {
  14012. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  14013. at: result,
  14014. surrogateCharCode: surrogate[0].charCodeAt(0)
  14015. });
  14016. }
  14017. return result;
  14018. }
  14019. return this.parseIdentifier(true);
  14020. }
  14021. isJSONModuleImport(node) {
  14022. if (node.assertions != null) {
  14023. return node.assertions.some(({
  14024. key,
  14025. value
  14026. }) => {
  14027. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  14028. });
  14029. }
  14030. return false;
  14031. }
  14032. checkImportReflection(node) {
  14033. if (node.module) {
  14034. var _node$assertions;
  14035. if (node.specifiers.length !== 1 || node.specifiers[0].type !== "ImportDefaultSpecifier") {
  14036. this.raise(Errors.ImportReflectionNotBinding, {
  14037. at: node.specifiers[0].loc.start
  14038. });
  14039. }
  14040. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  14041. this.raise(Errors.ImportReflectionHasAssertion, {
  14042. at: node.specifiers[0].loc.start
  14043. });
  14044. }
  14045. }
  14046. }
  14047. checkJSONModuleImport(node) {
  14048. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  14049. const {
  14050. specifiers
  14051. } = node;
  14052. if (specifiers != null) {
  14053. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  14054. let imported;
  14055. if (specifier.type === "ExportSpecifier") {
  14056. imported = specifier.local;
  14057. } else if (specifier.type === "ImportSpecifier") {
  14058. imported = specifier.imported;
  14059. }
  14060. if (imported !== undefined) {
  14061. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  14062. }
  14063. });
  14064. if (nonDefaultNamedSpecifier !== undefined) {
  14065. this.raise(Errors.ImportJSONBindingNotDefault, {
  14066. at: nonDefaultNamedSpecifier.loc.start
  14067. });
  14068. }
  14069. }
  14070. }
  14071. }
  14072. isPotentialImportPhase(isExport) {
  14073. return !isExport && this.isContextual(125);
  14074. }
  14075. applyImportPhase(node, isExport, phase, loc) {
  14076. if (isExport) {
  14077. return;
  14078. }
  14079. if (phase === "module") {
  14080. this.expectPlugin("importReflection", loc);
  14081. node.module = true;
  14082. } else if (this.hasPlugin("importReflection")) {
  14083. node.module = false;
  14084. }
  14085. }
  14086. parseMaybeImportPhase(node, isExport) {
  14087. if (!this.isPotentialImportPhase(isExport)) {
  14088. this.applyImportPhase(node, isExport, null);
  14089. return null;
  14090. }
  14091. const phaseIdentifier = this.parseIdentifier(true);
  14092. const {
  14093. type
  14094. } = this.state;
  14095. const isImportPhase = tokenIsKeywordOrIdentifier(type) ? type !== 97 || this.lookaheadCharCode() === 102 : type !== 12;
  14096. if (isImportPhase) {
  14097. this.resetPreviousIdentifierLeadingComments(phaseIdentifier);
  14098. this.applyImportPhase(node, isExport, phaseIdentifier.name, phaseIdentifier.loc.start);
  14099. return null;
  14100. } else {
  14101. this.applyImportPhase(node, isExport, null);
  14102. return phaseIdentifier;
  14103. }
  14104. }
  14105. isPrecedingIdImportPhase(phase) {
  14106. const {
  14107. type
  14108. } = this.state;
  14109. return tokenIsIdentifier(type) ? type !== 97 || this.lookaheadCharCode() === 102 : type !== 12;
  14110. }
  14111. parseImport(node) {
  14112. if (this.match(131)) {
  14113. return this.parseImportSourceAndAttributes(node);
  14114. }
  14115. return this.parseImportSpecifiersAndAfter(node, this.parseMaybeImportPhase(node, false));
  14116. }
  14117. parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier) {
  14118. node.specifiers = [];
  14119. const hasDefault = this.maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier);
  14120. const parseNext = !hasDefault || this.eat(12);
  14121. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  14122. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  14123. this.expectContextual(97);
  14124. return this.parseImportSourceAndAttributes(node);
  14125. }
  14126. parseImportSourceAndAttributes(node) {
  14127. var _node$specifiers;
  14128. (_node$specifiers = node.specifiers) != null ? _node$specifiers : node.specifiers = [];
  14129. node.source = this.parseImportSource();
  14130. this.maybeParseImportAttributes(node);
  14131. this.checkImportReflection(node);
  14132. this.checkJSONModuleImport(node);
  14133. this.semicolon();
  14134. return this.finishNode(node, "ImportDeclaration");
  14135. }
  14136. parseImportSource() {
  14137. if (!this.match(131)) this.unexpected();
  14138. return this.parseExprAtom();
  14139. }
  14140. parseImportSpecifierLocal(node, specifier, type) {
  14141. specifier.local = this.parseIdentifier();
  14142. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  14143. }
  14144. finishImportSpecifier(specifier, type, bindingType = BIND_LEXICAL) {
  14145. this.checkLVal(specifier.local, {
  14146. in: {
  14147. type
  14148. },
  14149. binding: bindingType
  14150. });
  14151. return this.finishNode(specifier, type);
  14152. }
  14153. parseImportAttributes() {
  14154. this.expect(5);
  14155. const attrs = [];
  14156. const attrNames = new Set();
  14157. do {
  14158. if (this.match(8)) {
  14159. break;
  14160. }
  14161. const node = this.startNode();
  14162. const keyName = this.state.value;
  14163. if (attrNames.has(keyName)) {
  14164. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14165. at: this.state.startLoc,
  14166. key: keyName
  14167. });
  14168. }
  14169. attrNames.add(keyName);
  14170. if (this.match(131)) {
  14171. node.key = this.parseStringLiteral(keyName);
  14172. } else {
  14173. node.key = this.parseIdentifier(true);
  14174. }
  14175. this.expect(14);
  14176. if (!this.match(131)) {
  14177. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14178. at: this.state.startLoc
  14179. });
  14180. }
  14181. node.value = this.parseStringLiteral(this.state.value);
  14182. attrs.push(this.finishNode(node, "ImportAttribute"));
  14183. } while (this.eat(12));
  14184. this.expect(8);
  14185. return attrs;
  14186. }
  14187. parseModuleAttributes() {
  14188. const attrs = [];
  14189. const attributes = new Set();
  14190. do {
  14191. const node = this.startNode();
  14192. node.key = this.parseIdentifier(true);
  14193. if (node.key.name !== "type") {
  14194. this.raise(Errors.ModuleAttributeDifferentFromType, {
  14195. at: node.key
  14196. });
  14197. }
  14198. if (attributes.has(node.key.name)) {
  14199. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14200. at: node.key,
  14201. key: node.key.name
  14202. });
  14203. }
  14204. attributes.add(node.key.name);
  14205. this.expect(14);
  14206. if (!this.match(131)) {
  14207. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14208. at: this.state.startLoc
  14209. });
  14210. }
  14211. node.value = this.parseStringLiteral(this.state.value);
  14212. attrs.push(this.finishNode(node, "ImportAttribute"));
  14213. } while (this.eat(12));
  14214. return attrs;
  14215. }
  14216. maybeParseImportAttributes(node) {
  14217. let attributes;
  14218. let useWith = false;
  14219. if (this.match(76)) {
  14220. if (this.hasPrecedingLineBreak() && this.lookaheadCharCode() === 40) {
  14221. return;
  14222. }
  14223. this.next();
  14224. {
  14225. if (this.hasPlugin("moduleAttributes")) {
  14226. attributes = this.parseModuleAttributes();
  14227. } else {
  14228. this.expectImportAttributesPlugin();
  14229. attributes = this.parseImportAttributes();
  14230. }
  14231. }
  14232. useWith = true;
  14233. } else if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  14234. if (this.hasPlugin("importAttributes")) {
  14235. if (this.getPluginOption("importAttributes", "deprecatedAssertSyntax") !== true) {
  14236. this.raise(Errors.ImportAttributesUseAssert, {
  14237. at: this.state.startLoc
  14238. });
  14239. }
  14240. this.addExtra(node, "deprecatedAssertSyntax", true);
  14241. } else {
  14242. this.expectOnePlugin(["importAttributes", "importAssertions"]);
  14243. }
  14244. this.next();
  14245. attributes = this.parseImportAttributes();
  14246. } else if (this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions")) {
  14247. attributes = [];
  14248. } else {
  14249. if (this.hasPlugin("moduleAttributes")) {
  14250. attributes = [];
  14251. } else return;
  14252. }
  14253. if (!useWith && this.hasPlugin("importAssertions")) {
  14254. node.assertions = attributes;
  14255. } else {
  14256. node.attributes = attributes;
  14257. }
  14258. }
  14259. maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier) {
  14260. if (maybeDefaultIdentifier) {
  14261. const specifier = this.startNodeAtNode(maybeDefaultIdentifier);
  14262. specifier.local = maybeDefaultIdentifier;
  14263. node.specifiers.push(this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"));
  14264. return true;
  14265. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  14266. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  14267. return true;
  14268. }
  14269. return false;
  14270. }
  14271. maybeParseStarImportSpecifier(node) {
  14272. if (this.match(55)) {
  14273. const specifier = this.startNode();
  14274. this.next();
  14275. this.expectContextual(93);
  14276. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  14277. return true;
  14278. }
  14279. return false;
  14280. }
  14281. parseNamedImportSpecifiers(node) {
  14282. let first = true;
  14283. this.expect(5);
  14284. while (!this.eat(8)) {
  14285. if (first) {
  14286. first = false;
  14287. } else {
  14288. if (this.eat(14)) {
  14289. throw this.raise(Errors.DestructureNamedImport, {
  14290. at: this.state.startLoc
  14291. });
  14292. }
  14293. this.expect(12);
  14294. if (this.eat(8)) break;
  14295. }
  14296. const specifier = this.startNode();
  14297. const importedIsString = this.match(131);
  14298. const isMaybeTypeOnly = this.isContextual(128);
  14299. specifier.imported = this.parseModuleExportName();
  14300. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  14301. node.specifiers.push(importSpecifier);
  14302. }
  14303. }
  14304. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  14305. if (this.eatContextual(93)) {
  14306. specifier.local = this.parseIdentifier();
  14307. } else {
  14308. const {
  14309. imported
  14310. } = specifier;
  14311. if (importedIsString) {
  14312. throw this.raise(Errors.ImportBindingIsString, {
  14313. at: specifier,
  14314. importName: imported.value
  14315. });
  14316. }
  14317. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  14318. if (!specifier.local) {
  14319. specifier.local = cloneIdentifier(imported);
  14320. }
  14321. }
  14322. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  14323. }
  14324. isThisParam(param) {
  14325. return param.type === "Identifier" && param.name === "this";
  14326. }
  14327. }
  14328. class Parser extends StatementParser {
  14329. constructor(options, input) {
  14330. options = getOptions(options);
  14331. super(options, input);
  14332. this.options = options;
  14333. this.initializeScopes();
  14334. this.plugins = pluginsMap(this.options.plugins);
  14335. this.filename = options.sourceFilename;
  14336. }
  14337. getScopeHandler() {
  14338. return ScopeHandler;
  14339. }
  14340. parse() {
  14341. this.enterInitialScopes();
  14342. const file = this.startNode();
  14343. const program = this.startNode();
  14344. this.nextToken();
  14345. file.errors = null;
  14346. this.parseTopLevel(file, program);
  14347. file.errors = this.state.errors;
  14348. return file;
  14349. }
  14350. }
  14351. function pluginsMap(plugins) {
  14352. const pluginMap = new Map();
  14353. for (const plugin of plugins) {
  14354. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  14355. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  14356. }
  14357. return pluginMap;
  14358. }
  14359. function parse(input, options) {
  14360. var _options;
  14361. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  14362. options = Object.assign({}, options);
  14363. try {
  14364. options.sourceType = "module";
  14365. const parser = getParser(options, input);
  14366. const ast = parser.parse();
  14367. if (parser.sawUnambiguousESM) {
  14368. return ast;
  14369. }
  14370. if (parser.ambiguousScriptDifferentAst) {
  14371. try {
  14372. options.sourceType = "script";
  14373. return getParser(options, input).parse();
  14374. } catch (_unused) {}
  14375. } else {
  14376. ast.program.sourceType = "script";
  14377. }
  14378. return ast;
  14379. } catch (moduleError) {
  14380. try {
  14381. options.sourceType = "script";
  14382. return getParser(options, input).parse();
  14383. } catch (_unused2) {}
  14384. throw moduleError;
  14385. }
  14386. } else {
  14387. return getParser(options, input).parse();
  14388. }
  14389. }
  14390. function parseExpression(input, options) {
  14391. const parser = getParser(options, input);
  14392. if (parser.options.strictMode) {
  14393. parser.state.strict = true;
  14394. }
  14395. return parser.getExpression();
  14396. }
  14397. function generateExportedTokenTypes(internalTokenTypes) {
  14398. const tokenTypes = {};
  14399. for (const typeName of Object.keys(internalTokenTypes)) {
  14400. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14401. }
  14402. return tokenTypes;
  14403. }
  14404. const tokTypes = generateExportedTokenTypes(tt);
  14405. function getParser(options, input) {
  14406. let cls = Parser;
  14407. if (options != null && options.plugins) {
  14408. validatePlugins(options.plugins);
  14409. cls = getParserClass(options.plugins);
  14410. }
  14411. return new cls(options, input);
  14412. }
  14413. const parserClassCache = {};
  14414. function getParserClass(pluginsFromOptions) {
  14415. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  14416. const key = pluginList.join("/");
  14417. let cls = parserClassCache[key];
  14418. if (!cls) {
  14419. cls = Parser;
  14420. for (const plugin of pluginList) {
  14421. cls = mixinPlugins[plugin](cls);
  14422. }
  14423. parserClassCache[key] = cls;
  14424. }
  14425. return cls;
  14426. }
  14427. exports.parse = parse;
  14428. exports.parseExpression = parseExpression;
  14429. exports.tokTypes = tokTypes;
  14430. //# sourceMappingURL=index.js.map