index.js 474 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _objectWithoutPropertiesLoose(source, excluded) {
  4. if (source == null) return {};
  5. var target = {};
  6. var sourceKeys = Object.keys(source);
  7. var key, i;
  8. for (i = 0; i < sourceKeys.length; i++) {
  9. key = sourceKeys[i];
  10. if (excluded.indexOf(key) >= 0) continue;
  11. target[key] = source[key];
  12. }
  13. return target;
  14. }
  15. class Position {
  16. constructor(line, col, index) {
  17. this.line = void 0;
  18. this.column = void 0;
  19. this.index = void 0;
  20. this.line = line;
  21. this.column = col;
  22. this.index = index;
  23. }
  24. }
  25. class SourceLocation {
  26. constructor(start, end) {
  27. this.start = void 0;
  28. this.end = void 0;
  29. this.filename = void 0;
  30. this.identifierName = void 0;
  31. this.start = start;
  32. this.end = end;
  33. }
  34. }
  35. function createPositionWithColumnOffset(position, columnOffset) {
  36. const {
  37. line,
  38. column,
  39. index
  40. } = position;
  41. return new Position(line, column + columnOffset, index + columnOffset);
  42. }
  43. var ParseErrorCode = {
  44. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  45. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  46. };
  47. const reflect = (keys, last = keys.length - 1) => ({
  48. get() {
  49. return keys.reduce((object, key) => object[key], this);
  50. },
  51. set(value) {
  52. keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
  53. }
  54. });
  55. const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
  56. value: descriptor,
  57. enumerable: false
  58. } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
  59. configurable: true
  60. }, descriptor)), Object.assign(new constructor(), properties));
  61. var ModuleErrors = {
  62. ImportMetaOutsideModule: {
  63. message: `import.meta may appear only with 'sourceType: "module"'`,
  64. code: ParseErrorCode.SourceTypeModuleError
  65. },
  66. ImportOutsideModule: {
  67. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  68. code: ParseErrorCode.SourceTypeModuleError
  69. }
  70. };
  71. const NodeDescriptions = {
  72. ArrayPattern: "array destructuring pattern",
  73. AssignmentExpression: "assignment expression",
  74. AssignmentPattern: "assignment expression",
  75. ArrowFunctionExpression: "arrow function expression",
  76. ConditionalExpression: "conditional expression",
  77. CatchClause: "catch clause",
  78. ForOfStatement: "for-of statement",
  79. ForInStatement: "for-in statement",
  80. ForStatement: "for-loop",
  81. FormalParameters: "function parameter list",
  82. Identifier: "identifier",
  83. ImportSpecifier: "import specifier",
  84. ImportDefaultSpecifier: "import default specifier",
  85. ImportNamespaceSpecifier: "import namespace specifier",
  86. ObjectPattern: "object destructuring pattern",
  87. ParenthesizedExpression: "parenthesized expression",
  88. RestElement: "rest element",
  89. UpdateExpression: {
  90. true: "prefix operation",
  91. false: "postfix operation"
  92. },
  93. VariableDeclarator: "variable declaration",
  94. YieldExpression: "yield expression"
  95. };
  96. const toNodeDescription = ({
  97. type,
  98. prefix
  99. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  100. var StandardErrors = {
  101. AccessorIsGenerator: ({
  102. kind
  103. }) => `A ${kind}ter cannot be a generator.`,
  104. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  105. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  106. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  107. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  108. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  109. AwaitInUsingBinding: "'await' is not allowed to be used as a name in 'using' declarations.",
  110. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  111. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  112. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  113. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  114. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  115. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  116. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  117. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  118. ConstructorIsAsync: "Constructor can't be an async function.",
  119. ConstructorIsGenerator: "Constructor can't be a generator.",
  120. DeclarationMissingInitializer: ({
  121. kind
  122. }) => `Missing initializer in ${kind} declaration.`,
  123. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  124. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  125. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  126. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  127. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  128. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  129. DeletePrivateField: "Deleting a private field is not allowed.",
  130. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  131. DuplicateConstructor: "Duplicate constructor in the same class.",
  132. DuplicateDefaultExport: "Only one default export allowed per module.",
  133. DuplicateExport: ({
  134. exportName
  135. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  136. DuplicateProto: "Redefinition of __proto__ property.",
  137. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  138. ElementAfterRest: "Rest element must be last element.",
  139. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  140. ExportBindingIsString: ({
  141. localName,
  142. exportName
  143. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  144. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  145. ForInOfLoopInitializer: ({
  146. type
  147. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  148. ForInUsing: "For-in loop may not start with 'using' declaration.",
  149. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  150. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  151. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  152. IllegalBreakContinue: ({
  153. type
  154. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  155. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  156. IllegalReturn: "'return' outside of function.",
  157. ImportBindingIsString: ({
  158. importName
  159. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  160. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  161. ImportCallArity: ({
  162. maxArgumentCount
  163. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  164. ImportCallNotNewExpression: "Cannot use new with import(...).",
  165. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  166. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  167. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  168. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  169. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  170. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  171. InvalidCodePoint: "Code point out of bounds.",
  172. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  173. InvalidDecimal: "Invalid decimal.",
  174. InvalidDigit: ({
  175. radix
  176. }) => `Expected number in radix ${radix}.`,
  177. InvalidEscapeSequence: "Bad character escape sequence.",
  178. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  179. InvalidEscapedReservedWord: ({
  180. reservedWord
  181. }) => `Escape sequence in keyword ${reservedWord}.`,
  182. InvalidIdentifier: ({
  183. identifierName
  184. }) => `Invalid identifier ${identifierName}.`,
  185. InvalidLhs: ({
  186. ancestor
  187. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  188. InvalidLhsBinding: ({
  189. ancestor
  190. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  191. InvalidNumber: "Invalid number.",
  192. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  193. InvalidOrUnexpectedToken: ({
  194. unexpected
  195. }) => `Unexpected character '${unexpected}'.`,
  196. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  197. InvalidPrivateFieldResolution: ({
  198. identifierName
  199. }) => `Private name #${identifierName} is not defined.`,
  200. InvalidPropertyBindingPattern: "Binding member expression.",
  201. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  202. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  203. LabelRedeclaration: ({
  204. labelName
  205. }) => `Label '${labelName}' is already declared.`,
  206. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  207. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  208. MalformedRegExpFlags: "Invalid regular expression flag.",
  209. MissingClassName: "A class name is required.",
  210. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  211. MissingSemicolon: "Missing semicolon.",
  212. MissingPlugin: ({
  213. missingPlugin
  214. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  215. MissingOneOfPlugins: ({
  216. missingPlugin
  217. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  218. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  219. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  220. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  221. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  222. ModuleAttributesWithDuplicateKeys: ({
  223. key
  224. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  225. ModuleExportNameHasLoneSurrogate: ({
  226. surrogateCharCode
  227. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  228. ModuleExportUndefined: ({
  229. localName
  230. }) => `Export '${localName}' is not defined.`,
  231. MultipleDefaultsInSwitch: "Multiple default clauses.",
  232. NewlineAfterThrow: "Illegal newline after throw.",
  233. NoCatchOrFinally: "Missing catch or finally clause.",
  234. NumberIdentifier: "Identifier directly after number.",
  235. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  236. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  237. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  238. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  239. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  240. ParamDupe: "Argument name clash.",
  241. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  242. PatternHasMethod: "Object pattern can't contain methods.",
  243. PrivateInExpectedIn: ({
  244. identifierName
  245. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  246. PrivateNameRedeclaration: ({
  247. identifierName
  248. }) => `Duplicate private name #${identifierName}.`,
  249. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  250. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  251. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  252. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  253. RestTrailingComma: "Unexpected trailing comma after rest element.",
  254. SloppyFunction: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  255. StaticPrototype: "Classes may not have static property named prototype.",
  256. 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?",
  257. SuperPrivateField: "Private fields can't be accessed on super.",
  258. TrailingDecorator: "Decorators must be attached to a class element.",
  259. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  260. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  261. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  262. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  263. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  264. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  265. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  266. UnexpectedKeyword: ({
  267. keyword
  268. }) => `Unexpected keyword '${keyword}'.`,
  269. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  270. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  271. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  272. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  273. UnexpectedPrivateField: "Unexpected private name.",
  274. UnexpectedReservedWord: ({
  275. reservedWord
  276. }) => `Unexpected reserved word '${reservedWord}'.`,
  277. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  278. UnexpectedToken: ({
  279. expected,
  280. unexpected
  281. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  282. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  283. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script`.",
  284. UnsupportedBind: "Binding should be performed on object property.",
  285. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  286. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  287. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  288. UnsupportedMetaProperty: ({
  289. target,
  290. onlyValidPropertyName
  291. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  292. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  293. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  294. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  295. UnterminatedComment: "Unterminated comment.",
  296. UnterminatedRegExp: "Unterminated regular expression.",
  297. UnterminatedString: "Unterminated string constant.",
  298. UnterminatedTemplate: "Unterminated template.",
  299. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  300. VarRedeclaration: ({
  301. identifierName
  302. }) => `Identifier '${identifierName}' has already been declared.`,
  303. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  304. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  305. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  306. };
  307. var StrictModeErrors = {
  308. StrictDelete: "Deleting local variable in strict mode.",
  309. StrictEvalArguments: ({
  310. referenceName
  311. }) => `Assigning to '${referenceName}' in strict mode.`,
  312. StrictEvalArgumentsBinding: ({
  313. bindingName
  314. }) => `Binding '${bindingName}' in strict mode.`,
  315. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  316. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  317. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  318. StrictWith: "'with' in strict mode."
  319. };
  320. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  321. var PipelineOperatorErrors = {
  322. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  323. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  324. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  325. PipeTopicUnconfiguredToken: ({
  326. token
  327. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  328. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  329. PipeUnparenthesizedBody: ({
  330. type
  331. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  332. type
  333. })}; please wrap it in parentheses.`,
  334. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  335. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  336. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  337. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  338. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  339. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  340. };
  341. const _excluded$1 = ["toMessage"],
  342. _excluded2$1 = ["message"];
  343. function toParseErrorConstructor(_ref) {
  344. let {
  345. toMessage
  346. } = _ref,
  347. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  348. return function constructor({
  349. loc,
  350. details
  351. }) {
  352. return instantiate(SyntaxError, Object.assign({}, properties, {
  353. loc
  354. }), {
  355. clone(overrides = {}) {
  356. const loc = overrides.loc || {};
  357. return constructor({
  358. 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),
  359. details: Object.assign({}, this.details, overrides.details)
  360. });
  361. },
  362. details: {
  363. value: details,
  364. enumerable: false
  365. },
  366. message: {
  367. get() {
  368. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  369. },
  370. set(value) {
  371. Object.defineProperty(this, "message", {
  372. value
  373. });
  374. }
  375. },
  376. pos: {
  377. reflect: "loc.index",
  378. enumerable: true
  379. },
  380. missingPlugin: "missingPlugin" in details && {
  381. reflect: "details.missingPlugin",
  382. enumerable: true
  383. }
  384. });
  385. };
  386. }
  387. function ParseErrorEnum(argument, syntaxPlugin) {
  388. if (Array.isArray(argument)) {
  389. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  390. }
  391. const ParseErrorConstructors = {};
  392. for (const reasonCode of Object.keys(argument)) {
  393. const template = argument[reasonCode];
  394. const _ref2 = typeof template === "string" ? {
  395. message: () => template
  396. } : typeof template === "function" ? {
  397. message: template
  398. } : template,
  399. {
  400. message
  401. } = _ref2,
  402. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
  403. const toMessage = typeof message === "string" ? () => message : message;
  404. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  405. code: ParseErrorCode.SyntaxError,
  406. reasonCode,
  407. toMessage
  408. }, syntaxPlugin ? {
  409. syntaxPlugin
  410. } : {}, rest));
  411. }
  412. return ParseErrorConstructors;
  413. }
  414. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  415. const {
  416. defineProperty
  417. } = Object;
  418. const toUnenumerable = (object, key) => defineProperty(object, key, {
  419. enumerable: false,
  420. value: object[key]
  421. });
  422. function toESTreeLocation(node) {
  423. node.loc.start && toUnenumerable(node.loc.start, "index");
  424. node.loc.end && toUnenumerable(node.loc.end, "index");
  425. return node;
  426. }
  427. var estree = (superClass => class ESTreeParserMixin extends superClass {
  428. parse() {
  429. const file = toESTreeLocation(super.parse());
  430. if (this.options.tokens) {
  431. file.tokens = file.tokens.map(toESTreeLocation);
  432. }
  433. return file;
  434. }
  435. parseRegExpLiteral({
  436. pattern,
  437. flags
  438. }) {
  439. let regex = null;
  440. try {
  441. regex = new RegExp(pattern, flags);
  442. } catch (e) {}
  443. const node = this.estreeParseLiteral(regex);
  444. node.regex = {
  445. pattern,
  446. flags
  447. };
  448. return node;
  449. }
  450. parseBigIntLiteral(value) {
  451. let bigInt;
  452. try {
  453. bigInt = BigInt(value);
  454. } catch (_unused) {
  455. bigInt = null;
  456. }
  457. const node = this.estreeParseLiteral(bigInt);
  458. node.bigint = String(node.value || value);
  459. return node;
  460. }
  461. parseDecimalLiteral(value) {
  462. const decimal = null;
  463. const node = this.estreeParseLiteral(decimal);
  464. node.decimal = String(node.value || value);
  465. return node;
  466. }
  467. estreeParseLiteral(value) {
  468. return this.parseLiteral(value, "Literal");
  469. }
  470. parseStringLiteral(value) {
  471. return this.estreeParseLiteral(value);
  472. }
  473. parseNumericLiteral(value) {
  474. return this.estreeParseLiteral(value);
  475. }
  476. parseNullLiteral() {
  477. return this.estreeParseLiteral(null);
  478. }
  479. parseBooleanLiteral(value) {
  480. return this.estreeParseLiteral(value);
  481. }
  482. directiveToStmt(directive) {
  483. const expression = directive.value;
  484. delete directive.value;
  485. expression.type = "Literal";
  486. expression.raw = expression.extra.raw;
  487. expression.value = expression.extra.expressionValue;
  488. const stmt = directive;
  489. stmt.type = "ExpressionStatement";
  490. stmt.expression = expression;
  491. stmt.directive = expression.extra.rawValue;
  492. delete expression.extra;
  493. return stmt;
  494. }
  495. initFunction(node, isAsync) {
  496. super.initFunction(node, isAsync);
  497. node.expression = false;
  498. }
  499. checkDeclaration(node) {
  500. if (node != null && this.isObjectProperty(node)) {
  501. this.checkDeclaration(node.value);
  502. } else {
  503. super.checkDeclaration(node);
  504. }
  505. }
  506. getObjectOrClassMethodParams(method) {
  507. return method.value.params;
  508. }
  509. isValidDirective(stmt) {
  510. var _stmt$expression$extr;
  511. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  512. }
  513. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  514. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  515. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  516. node.body = directiveStatements.concat(node.body);
  517. delete node.directives;
  518. }
  519. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  520. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  521. if (method.typeParameters) {
  522. method.value.typeParameters = method.typeParameters;
  523. delete method.typeParameters;
  524. }
  525. classBody.body.push(method);
  526. }
  527. parsePrivateName() {
  528. const node = super.parsePrivateName();
  529. {
  530. if (!this.getPluginOption("estree", "classFeatures")) {
  531. return node;
  532. }
  533. }
  534. return this.convertPrivateNameToPrivateIdentifier(node);
  535. }
  536. convertPrivateNameToPrivateIdentifier(node) {
  537. const name = super.getPrivateNameSV(node);
  538. node = node;
  539. delete node.id;
  540. node.name = name;
  541. node.type = "PrivateIdentifier";
  542. return node;
  543. }
  544. isPrivateName(node) {
  545. {
  546. if (!this.getPluginOption("estree", "classFeatures")) {
  547. return super.isPrivateName(node);
  548. }
  549. }
  550. return node.type === "PrivateIdentifier";
  551. }
  552. getPrivateNameSV(node) {
  553. {
  554. if (!this.getPluginOption("estree", "classFeatures")) {
  555. return super.getPrivateNameSV(node);
  556. }
  557. }
  558. return node.name;
  559. }
  560. parseLiteral(value, type) {
  561. const node = super.parseLiteral(value, type);
  562. node.raw = node.extra.raw;
  563. delete node.extra;
  564. return node;
  565. }
  566. parseFunctionBody(node, allowExpression, isMethod = false) {
  567. super.parseFunctionBody(node, allowExpression, isMethod);
  568. node.expression = node.body.type !== "BlockStatement";
  569. }
  570. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  571. let funcNode = this.startNode();
  572. funcNode.kind = node.kind;
  573. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  574. funcNode.type = "FunctionExpression";
  575. delete funcNode.kind;
  576. node.value = funcNode;
  577. if (type === "ClassPrivateMethod") {
  578. node.computed = false;
  579. }
  580. return this.finishNode(node, "MethodDefinition");
  581. }
  582. parseClassProperty(...args) {
  583. const propertyNode = super.parseClassProperty(...args);
  584. {
  585. if (!this.getPluginOption("estree", "classFeatures")) {
  586. return propertyNode;
  587. }
  588. }
  589. propertyNode.type = "PropertyDefinition";
  590. return propertyNode;
  591. }
  592. parseClassPrivateProperty(...args) {
  593. const propertyNode = super.parseClassPrivateProperty(...args);
  594. {
  595. if (!this.getPluginOption("estree", "classFeatures")) {
  596. return propertyNode;
  597. }
  598. }
  599. propertyNode.type = "PropertyDefinition";
  600. propertyNode.computed = false;
  601. return propertyNode;
  602. }
  603. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  604. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  605. if (node) {
  606. node.type = "Property";
  607. if (node.kind === "method") {
  608. node.kind = "init";
  609. }
  610. node.shorthand = false;
  611. }
  612. return node;
  613. }
  614. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  615. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  616. if (node) {
  617. node.kind = "init";
  618. node.type = "Property";
  619. }
  620. return node;
  621. }
  622. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  623. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  624. }
  625. isAssignable(node, isBinding) {
  626. if (node != null && this.isObjectProperty(node)) {
  627. return this.isAssignable(node.value, isBinding);
  628. }
  629. return super.isAssignable(node, isBinding);
  630. }
  631. toAssignable(node, isLHS = false) {
  632. if (node != null && this.isObjectProperty(node)) {
  633. const {
  634. key,
  635. value
  636. } = node;
  637. if (this.isPrivateName(key)) {
  638. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  639. }
  640. this.toAssignable(value, isLHS);
  641. } else {
  642. super.toAssignable(node, isLHS);
  643. }
  644. }
  645. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  646. if (prop.kind === "get" || prop.kind === "set") {
  647. this.raise(Errors.PatternHasAccessor, {
  648. at: prop.key
  649. });
  650. } else if (prop.method) {
  651. this.raise(Errors.PatternHasMethod, {
  652. at: prop.key
  653. });
  654. } else {
  655. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  656. }
  657. }
  658. finishCallExpression(unfinished, optional) {
  659. const node = super.finishCallExpression(unfinished, optional);
  660. if (node.callee.type === "Import") {
  661. node.type = "ImportExpression";
  662. node.source = node.arguments[0];
  663. if (this.hasPlugin("importAssertions")) {
  664. var _node$arguments$;
  665. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  666. }
  667. delete node.arguments;
  668. delete node.callee;
  669. }
  670. return node;
  671. }
  672. toReferencedArguments(node) {
  673. if (node.type === "ImportExpression") {
  674. return;
  675. }
  676. super.toReferencedArguments(node);
  677. }
  678. parseExport(unfinished, decorators) {
  679. const exportStartLoc = this.state.lastTokStartLoc;
  680. const node = super.parseExport(unfinished, decorators);
  681. switch (node.type) {
  682. case "ExportAllDeclaration":
  683. node.exported = null;
  684. break;
  685. case "ExportNamedDeclaration":
  686. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  687. node.type = "ExportAllDeclaration";
  688. node.exported = node.specifiers[0].exported;
  689. delete node.specifiers;
  690. }
  691. case "ExportDefaultDeclaration":
  692. {
  693. var _declaration$decorato;
  694. const {
  695. declaration
  696. } = node;
  697. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  698. this.resetStartLocation(node, exportStartLoc);
  699. }
  700. }
  701. break;
  702. }
  703. return node;
  704. }
  705. parseSubscript(base, startLoc, noCalls, state) {
  706. const node = super.parseSubscript(base, startLoc, noCalls, state);
  707. if (state.optionalChainMember) {
  708. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  709. node.type = node.type.substring(8);
  710. }
  711. if (state.stop) {
  712. const chain = this.startNodeAtNode(node);
  713. chain.expression = node;
  714. return this.finishNode(chain, "ChainExpression");
  715. }
  716. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  717. node.optional = false;
  718. }
  719. return node;
  720. }
  721. hasPropertyAsPrivateName(node) {
  722. if (node.type === "ChainExpression") {
  723. node = node.expression;
  724. }
  725. return super.hasPropertyAsPrivateName(node);
  726. }
  727. isOptionalChain(node) {
  728. return node.type === "ChainExpression";
  729. }
  730. isObjectProperty(node) {
  731. return node.type === "Property" && node.kind === "init" && !node.method;
  732. }
  733. isObjectMethod(node) {
  734. return node.method || node.kind === "get" || node.kind === "set";
  735. }
  736. finishNodeAt(node, type, endLoc) {
  737. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  738. }
  739. resetStartLocation(node, startLoc) {
  740. super.resetStartLocation(node, startLoc);
  741. toESTreeLocation(node);
  742. }
  743. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  744. super.resetEndLocation(node, endLoc);
  745. toESTreeLocation(node);
  746. }
  747. });
  748. class TokContext {
  749. constructor(token, preserveSpace) {
  750. this.token = void 0;
  751. this.preserveSpace = void 0;
  752. this.token = token;
  753. this.preserveSpace = !!preserveSpace;
  754. }
  755. }
  756. const types = {
  757. brace: new TokContext("{"),
  758. j_oTag: new TokContext("<tag"),
  759. j_cTag: new TokContext("</tag"),
  760. j_expr: new TokContext("<tag>...</tag>", true)
  761. };
  762. {
  763. types.template = new TokContext("`", true);
  764. }
  765. const beforeExpr = true;
  766. const startsExpr = true;
  767. const isLoop = true;
  768. const isAssign = true;
  769. const prefix = true;
  770. const postfix = true;
  771. class ExportedTokenType {
  772. constructor(label, conf = {}) {
  773. this.label = void 0;
  774. this.keyword = void 0;
  775. this.beforeExpr = void 0;
  776. this.startsExpr = void 0;
  777. this.rightAssociative = void 0;
  778. this.isLoop = void 0;
  779. this.isAssign = void 0;
  780. this.prefix = void 0;
  781. this.postfix = void 0;
  782. this.binop = void 0;
  783. this.label = label;
  784. this.keyword = conf.keyword;
  785. this.beforeExpr = !!conf.beforeExpr;
  786. this.startsExpr = !!conf.startsExpr;
  787. this.rightAssociative = !!conf.rightAssociative;
  788. this.isLoop = !!conf.isLoop;
  789. this.isAssign = !!conf.isAssign;
  790. this.prefix = !!conf.prefix;
  791. this.postfix = !!conf.postfix;
  792. this.binop = conf.binop != null ? conf.binop : null;
  793. {
  794. this.updateContext = null;
  795. }
  796. }
  797. }
  798. const keywords$1 = new Map();
  799. function createKeyword(name, options = {}) {
  800. options.keyword = name;
  801. const token = createToken(name, options);
  802. keywords$1.set(name, token);
  803. return token;
  804. }
  805. function createBinop(name, binop) {
  806. return createToken(name, {
  807. beforeExpr,
  808. binop
  809. });
  810. }
  811. let tokenTypeCounter = -1;
  812. const tokenTypes = [];
  813. const tokenLabels = [];
  814. const tokenBinops = [];
  815. const tokenBeforeExprs = [];
  816. const tokenStartsExprs = [];
  817. const tokenPrefixes = [];
  818. function createToken(name, options = {}) {
  819. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  820. ++tokenTypeCounter;
  821. tokenLabels.push(name);
  822. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  823. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  824. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  825. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  826. tokenTypes.push(new ExportedTokenType(name, options));
  827. return tokenTypeCounter;
  828. }
  829. function createKeywordLike(name, options = {}) {
  830. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  831. ++tokenTypeCounter;
  832. keywords$1.set(name, tokenTypeCounter);
  833. tokenLabels.push(name);
  834. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  835. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  836. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  837. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  838. tokenTypes.push(new ExportedTokenType("name", options));
  839. return tokenTypeCounter;
  840. }
  841. const tt = {
  842. bracketL: createToken("[", {
  843. beforeExpr,
  844. startsExpr
  845. }),
  846. bracketHashL: createToken("#[", {
  847. beforeExpr,
  848. startsExpr
  849. }),
  850. bracketBarL: createToken("[|", {
  851. beforeExpr,
  852. startsExpr
  853. }),
  854. bracketR: createToken("]"),
  855. bracketBarR: createToken("|]"),
  856. braceL: createToken("{", {
  857. beforeExpr,
  858. startsExpr
  859. }),
  860. braceBarL: createToken("{|", {
  861. beforeExpr,
  862. startsExpr
  863. }),
  864. braceHashL: createToken("#{", {
  865. beforeExpr,
  866. startsExpr
  867. }),
  868. braceR: createToken("}"),
  869. braceBarR: createToken("|}"),
  870. parenL: createToken("(", {
  871. beforeExpr,
  872. startsExpr
  873. }),
  874. parenR: createToken(")"),
  875. comma: createToken(",", {
  876. beforeExpr
  877. }),
  878. semi: createToken(";", {
  879. beforeExpr
  880. }),
  881. colon: createToken(":", {
  882. beforeExpr
  883. }),
  884. doubleColon: createToken("::", {
  885. beforeExpr
  886. }),
  887. dot: createToken("."),
  888. question: createToken("?", {
  889. beforeExpr
  890. }),
  891. questionDot: createToken("?."),
  892. arrow: createToken("=>", {
  893. beforeExpr
  894. }),
  895. template: createToken("template"),
  896. ellipsis: createToken("...", {
  897. beforeExpr
  898. }),
  899. backQuote: createToken("`", {
  900. startsExpr
  901. }),
  902. dollarBraceL: createToken("${", {
  903. beforeExpr,
  904. startsExpr
  905. }),
  906. templateTail: createToken("...`", {
  907. startsExpr
  908. }),
  909. templateNonTail: createToken("...${", {
  910. beforeExpr,
  911. startsExpr
  912. }),
  913. at: createToken("@"),
  914. hash: createToken("#", {
  915. startsExpr
  916. }),
  917. interpreterDirective: createToken("#!..."),
  918. eq: createToken("=", {
  919. beforeExpr,
  920. isAssign
  921. }),
  922. assign: createToken("_=", {
  923. beforeExpr,
  924. isAssign
  925. }),
  926. slashAssign: createToken("_=", {
  927. beforeExpr,
  928. isAssign
  929. }),
  930. xorAssign: createToken("_=", {
  931. beforeExpr,
  932. isAssign
  933. }),
  934. moduloAssign: createToken("_=", {
  935. beforeExpr,
  936. isAssign
  937. }),
  938. incDec: createToken("++/--", {
  939. prefix,
  940. postfix,
  941. startsExpr
  942. }),
  943. bang: createToken("!", {
  944. beforeExpr,
  945. prefix,
  946. startsExpr
  947. }),
  948. tilde: createToken("~", {
  949. beforeExpr,
  950. prefix,
  951. startsExpr
  952. }),
  953. doubleCaret: createToken("^^", {
  954. startsExpr
  955. }),
  956. doubleAt: createToken("@@", {
  957. startsExpr
  958. }),
  959. pipeline: createBinop("|>", 0),
  960. nullishCoalescing: createBinop("??", 1),
  961. logicalOR: createBinop("||", 1),
  962. logicalAND: createBinop("&&", 2),
  963. bitwiseOR: createBinop("|", 3),
  964. bitwiseXOR: createBinop("^", 4),
  965. bitwiseAND: createBinop("&", 5),
  966. equality: createBinop("==/!=/===/!==", 6),
  967. lt: createBinop("</>/<=/>=", 7),
  968. gt: createBinop("</>/<=/>=", 7),
  969. relational: createBinop("</>/<=/>=", 7),
  970. bitShift: createBinop("<</>>/>>>", 8),
  971. bitShiftL: createBinop("<</>>/>>>", 8),
  972. bitShiftR: createBinop("<</>>/>>>", 8),
  973. plusMin: createToken("+/-", {
  974. beforeExpr,
  975. binop: 9,
  976. prefix,
  977. startsExpr
  978. }),
  979. modulo: createToken("%", {
  980. binop: 10,
  981. startsExpr
  982. }),
  983. star: createToken("*", {
  984. binop: 10
  985. }),
  986. slash: createBinop("/", 10),
  987. exponent: createToken("**", {
  988. beforeExpr,
  989. binop: 11,
  990. rightAssociative: true
  991. }),
  992. _in: createKeyword("in", {
  993. beforeExpr,
  994. binop: 7
  995. }),
  996. _instanceof: createKeyword("instanceof", {
  997. beforeExpr,
  998. binop: 7
  999. }),
  1000. _break: createKeyword("break"),
  1001. _case: createKeyword("case", {
  1002. beforeExpr
  1003. }),
  1004. _catch: createKeyword("catch"),
  1005. _continue: createKeyword("continue"),
  1006. _debugger: createKeyword("debugger"),
  1007. _default: createKeyword("default", {
  1008. beforeExpr
  1009. }),
  1010. _else: createKeyword("else", {
  1011. beforeExpr
  1012. }),
  1013. _finally: createKeyword("finally"),
  1014. _function: createKeyword("function", {
  1015. startsExpr
  1016. }),
  1017. _if: createKeyword("if"),
  1018. _return: createKeyword("return", {
  1019. beforeExpr
  1020. }),
  1021. _switch: createKeyword("switch"),
  1022. _throw: createKeyword("throw", {
  1023. beforeExpr,
  1024. prefix,
  1025. startsExpr
  1026. }),
  1027. _try: createKeyword("try"),
  1028. _var: createKeyword("var"),
  1029. _const: createKeyword("const"),
  1030. _with: createKeyword("with"),
  1031. _new: createKeyword("new", {
  1032. beforeExpr,
  1033. startsExpr
  1034. }),
  1035. _this: createKeyword("this", {
  1036. startsExpr
  1037. }),
  1038. _super: createKeyword("super", {
  1039. startsExpr
  1040. }),
  1041. _class: createKeyword("class", {
  1042. startsExpr
  1043. }),
  1044. _extends: createKeyword("extends", {
  1045. beforeExpr
  1046. }),
  1047. _export: createKeyword("export"),
  1048. _import: createKeyword("import", {
  1049. startsExpr
  1050. }),
  1051. _null: createKeyword("null", {
  1052. startsExpr
  1053. }),
  1054. _true: createKeyword("true", {
  1055. startsExpr
  1056. }),
  1057. _false: createKeyword("false", {
  1058. startsExpr
  1059. }),
  1060. _typeof: createKeyword("typeof", {
  1061. beforeExpr,
  1062. prefix,
  1063. startsExpr
  1064. }),
  1065. _void: createKeyword("void", {
  1066. beforeExpr,
  1067. prefix,
  1068. startsExpr
  1069. }),
  1070. _delete: createKeyword("delete", {
  1071. beforeExpr,
  1072. prefix,
  1073. startsExpr
  1074. }),
  1075. _do: createKeyword("do", {
  1076. isLoop,
  1077. beforeExpr
  1078. }),
  1079. _for: createKeyword("for", {
  1080. isLoop
  1081. }),
  1082. _while: createKeyword("while", {
  1083. isLoop
  1084. }),
  1085. _as: createKeywordLike("as", {
  1086. startsExpr
  1087. }),
  1088. _assert: createKeywordLike("assert", {
  1089. startsExpr
  1090. }),
  1091. _async: createKeywordLike("async", {
  1092. startsExpr
  1093. }),
  1094. _await: createKeywordLike("await", {
  1095. startsExpr
  1096. }),
  1097. _from: createKeywordLike("from", {
  1098. startsExpr
  1099. }),
  1100. _get: createKeywordLike("get", {
  1101. startsExpr
  1102. }),
  1103. _let: createKeywordLike("let", {
  1104. startsExpr
  1105. }),
  1106. _meta: createKeywordLike("meta", {
  1107. startsExpr
  1108. }),
  1109. _of: createKeywordLike("of", {
  1110. startsExpr
  1111. }),
  1112. _sent: createKeywordLike("sent", {
  1113. startsExpr
  1114. }),
  1115. _set: createKeywordLike("set", {
  1116. startsExpr
  1117. }),
  1118. _static: createKeywordLike("static", {
  1119. startsExpr
  1120. }),
  1121. _using: createKeywordLike("using", {
  1122. startsExpr
  1123. }),
  1124. _yield: createKeywordLike("yield", {
  1125. startsExpr
  1126. }),
  1127. _asserts: createKeywordLike("asserts", {
  1128. startsExpr
  1129. }),
  1130. _checks: createKeywordLike("checks", {
  1131. startsExpr
  1132. }),
  1133. _exports: createKeywordLike("exports", {
  1134. startsExpr
  1135. }),
  1136. _global: createKeywordLike("global", {
  1137. startsExpr
  1138. }),
  1139. _implements: createKeywordLike("implements", {
  1140. startsExpr
  1141. }),
  1142. _intrinsic: createKeywordLike("intrinsic", {
  1143. startsExpr
  1144. }),
  1145. _infer: createKeywordLike("infer", {
  1146. startsExpr
  1147. }),
  1148. _is: createKeywordLike("is", {
  1149. startsExpr
  1150. }),
  1151. _mixins: createKeywordLike("mixins", {
  1152. startsExpr
  1153. }),
  1154. _proto: createKeywordLike("proto", {
  1155. startsExpr
  1156. }),
  1157. _require: createKeywordLike("require", {
  1158. startsExpr
  1159. }),
  1160. _satisfies: createKeywordLike("satisfies", {
  1161. startsExpr
  1162. }),
  1163. _keyof: createKeywordLike("keyof", {
  1164. startsExpr
  1165. }),
  1166. _readonly: createKeywordLike("readonly", {
  1167. startsExpr
  1168. }),
  1169. _unique: createKeywordLike("unique", {
  1170. startsExpr
  1171. }),
  1172. _abstract: createKeywordLike("abstract", {
  1173. startsExpr
  1174. }),
  1175. _declare: createKeywordLike("declare", {
  1176. startsExpr
  1177. }),
  1178. _enum: createKeywordLike("enum", {
  1179. startsExpr
  1180. }),
  1181. _module: createKeywordLike("module", {
  1182. startsExpr
  1183. }),
  1184. _namespace: createKeywordLike("namespace", {
  1185. startsExpr
  1186. }),
  1187. _interface: createKeywordLike("interface", {
  1188. startsExpr
  1189. }),
  1190. _type: createKeywordLike("type", {
  1191. startsExpr
  1192. }),
  1193. _opaque: createKeywordLike("opaque", {
  1194. startsExpr
  1195. }),
  1196. name: createToken("name", {
  1197. startsExpr
  1198. }),
  1199. string: createToken("string", {
  1200. startsExpr
  1201. }),
  1202. num: createToken("num", {
  1203. startsExpr
  1204. }),
  1205. bigint: createToken("bigint", {
  1206. startsExpr
  1207. }),
  1208. decimal: createToken("decimal", {
  1209. startsExpr
  1210. }),
  1211. regexp: createToken("regexp", {
  1212. startsExpr
  1213. }),
  1214. privateName: createToken("#name", {
  1215. startsExpr
  1216. }),
  1217. eof: createToken("eof"),
  1218. jsxName: createToken("jsxName"),
  1219. jsxText: createToken("jsxText", {
  1220. beforeExpr: true
  1221. }),
  1222. jsxTagStart: createToken("jsxTagStart", {
  1223. startsExpr: true
  1224. }),
  1225. jsxTagEnd: createToken("jsxTagEnd"),
  1226. placeholder: createToken("%%", {
  1227. startsExpr: true
  1228. })
  1229. };
  1230. function tokenIsIdentifier(token) {
  1231. return token >= 93 && token <= 130;
  1232. }
  1233. function tokenKeywordOrIdentifierIsKeyword(token) {
  1234. return token <= 92;
  1235. }
  1236. function tokenIsKeywordOrIdentifier(token) {
  1237. return token >= 58 && token <= 130;
  1238. }
  1239. function tokenIsLiteralPropertyName(token) {
  1240. return token >= 58 && token <= 134;
  1241. }
  1242. function tokenComesBeforeExpression(token) {
  1243. return tokenBeforeExprs[token];
  1244. }
  1245. function tokenCanStartExpression(token) {
  1246. return tokenStartsExprs[token];
  1247. }
  1248. function tokenIsAssignment(token) {
  1249. return token >= 29 && token <= 33;
  1250. }
  1251. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1252. return token >= 127 && token <= 129;
  1253. }
  1254. function tokenIsLoop(token) {
  1255. return token >= 90 && token <= 92;
  1256. }
  1257. function tokenIsKeyword(token) {
  1258. return token >= 58 && token <= 92;
  1259. }
  1260. function tokenIsOperator(token) {
  1261. return token >= 39 && token <= 59;
  1262. }
  1263. function tokenIsPostfix(token) {
  1264. return token === 34;
  1265. }
  1266. function tokenIsPrefix(token) {
  1267. return tokenPrefixes[token];
  1268. }
  1269. function tokenIsTSTypeOperator(token) {
  1270. return token >= 119 && token <= 121;
  1271. }
  1272. function tokenIsTSDeclarationStart(token) {
  1273. return token >= 122 && token <= 128;
  1274. }
  1275. function tokenLabelName(token) {
  1276. return tokenLabels[token];
  1277. }
  1278. function tokenOperatorPrecedence(token) {
  1279. return tokenBinops[token];
  1280. }
  1281. function tokenIsRightAssociative(token) {
  1282. return token === 57;
  1283. }
  1284. function tokenIsTemplate(token) {
  1285. return token >= 24 && token <= 25;
  1286. }
  1287. function getExportedToken(token) {
  1288. return tokenTypes[token];
  1289. }
  1290. {
  1291. tokenTypes[8].updateContext = context => {
  1292. context.pop();
  1293. };
  1294. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1295. context.push(types.brace);
  1296. };
  1297. tokenTypes[22].updateContext = context => {
  1298. if (context[context.length - 1] === types.template) {
  1299. context.pop();
  1300. } else {
  1301. context.push(types.template);
  1302. }
  1303. };
  1304. tokenTypes[140].updateContext = context => {
  1305. context.push(types.j_expr, types.j_oTag);
  1306. };
  1307. }
  1308. 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";
  1309. 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";
  1310. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1311. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1312. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1313. 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];
  1314. 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];
  1315. function isInAstralSet(code, set) {
  1316. let pos = 0x10000;
  1317. for (let i = 0, length = set.length; i < length; i += 2) {
  1318. pos += set[i];
  1319. if (pos > code) return false;
  1320. pos += set[i + 1];
  1321. if (pos >= code) return true;
  1322. }
  1323. return false;
  1324. }
  1325. function isIdentifierStart(code) {
  1326. if (code < 65) return code === 36;
  1327. if (code <= 90) return true;
  1328. if (code < 97) return code === 95;
  1329. if (code <= 122) return true;
  1330. if (code <= 0xffff) {
  1331. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1332. }
  1333. return isInAstralSet(code, astralIdentifierStartCodes);
  1334. }
  1335. function isIdentifierChar(code) {
  1336. if (code < 48) return code === 36;
  1337. if (code < 58) return true;
  1338. if (code < 65) return false;
  1339. if (code <= 90) return true;
  1340. if (code < 97) return code === 95;
  1341. if (code <= 122) return true;
  1342. if (code <= 0xffff) {
  1343. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1344. }
  1345. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1346. }
  1347. const reservedWords = {
  1348. 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"],
  1349. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1350. strictBind: ["eval", "arguments"]
  1351. };
  1352. const keywords = new Set(reservedWords.keyword);
  1353. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1354. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1355. function isReservedWord(word, inModule) {
  1356. return inModule && word === "await" || word === "enum";
  1357. }
  1358. function isStrictReservedWord(word, inModule) {
  1359. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1360. }
  1361. function isStrictBindOnlyReservedWord(word) {
  1362. return reservedWordsStrictBindSet.has(word);
  1363. }
  1364. function isStrictBindReservedWord(word, inModule) {
  1365. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1366. }
  1367. function isKeyword(word) {
  1368. return keywords.has(word);
  1369. }
  1370. function isIteratorStart(current, next, next2) {
  1371. return current === 64 && next === 64 && isIdentifierStart(next2);
  1372. }
  1373. 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"]);
  1374. function canBeReservedWord(word) {
  1375. return reservedWordLikeSet.has(word);
  1376. }
  1377. const SCOPE_OTHER = 0b000000000,
  1378. SCOPE_PROGRAM = 0b000000001,
  1379. SCOPE_FUNCTION = 0b000000010,
  1380. SCOPE_ARROW = 0b000000100,
  1381. SCOPE_SIMPLE_CATCH = 0b000001000,
  1382. SCOPE_SUPER = 0b000010000,
  1383. SCOPE_DIRECT_SUPER = 0b000100000,
  1384. SCOPE_CLASS = 0b001000000,
  1385. SCOPE_STATIC_BLOCK = 0b010000000,
  1386. SCOPE_TS_MODULE = 0b100000000,
  1387. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_STATIC_BLOCK | SCOPE_TS_MODULE;
  1388. const BIND_KIND_VALUE = 0b0000000000001,
  1389. BIND_KIND_TYPE = 0b0000000000010,
  1390. BIND_SCOPE_VAR = 0b0000000000100,
  1391. BIND_SCOPE_LEXICAL = 0b0000000001000,
  1392. BIND_SCOPE_FUNCTION = 0b0000000010000,
  1393. BIND_FLAGS_NONE = 0b00000001000000,
  1394. BIND_FLAGS_CLASS = 0b00000010000000,
  1395. BIND_FLAGS_TS_ENUM = 0b00000100000000,
  1396. BIND_FLAGS_TS_CONST_ENUM = 0b00001000000000,
  1397. BIND_FLAGS_TS_EXPORT_ONLY = 0b00010000000000,
  1398. BIND_FLAGS_FLOW_DECLARE_FN = 0b00100000000000,
  1399. BIND_FLAGS_TS_IMPORT = 0b01000000000000,
  1400. BIND_FLAGS_NO_LET_IN_LEXICAL = 0b10000000000000;
  1401. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1402. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1403. BIND_CATCH_PARAM = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1404. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1405. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1406. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1407. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1408. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM | BIND_FLAGS_NO_LET_IN_LEXICAL,
  1409. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1410. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1411. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1412. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1413. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1414. BIND_TS_TYPE_IMPORT = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
  1415. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1416. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1417. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1418. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1419. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1420. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1421. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1422. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1423. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1424. CLASS_ELEMENT_OTHER = 0;
  1425. class Scope {
  1426. constructor(flags) {
  1427. this.var = new Set();
  1428. this.lexical = new Set();
  1429. this.functions = new Set();
  1430. this.flags = flags;
  1431. }
  1432. }
  1433. class ScopeHandler {
  1434. constructor(parser, inModule) {
  1435. this.parser = void 0;
  1436. this.scopeStack = [];
  1437. this.inModule = void 0;
  1438. this.undefinedExports = new Map();
  1439. this.parser = parser;
  1440. this.inModule = inModule;
  1441. }
  1442. get inTopLevel() {
  1443. return (this.currentScope().flags & SCOPE_PROGRAM) > 0;
  1444. }
  1445. get inFunction() {
  1446. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1447. }
  1448. get allowSuper() {
  1449. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1450. }
  1451. get allowDirectSuper() {
  1452. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1453. }
  1454. get inClass() {
  1455. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1456. }
  1457. get inClassAndNotInNonArrowFunction() {
  1458. const flags = this.currentThisScopeFlags();
  1459. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1460. }
  1461. get inStaticBlock() {
  1462. for (let i = this.scopeStack.length - 1;; i--) {
  1463. const {
  1464. flags
  1465. } = this.scopeStack[i];
  1466. if (flags & SCOPE_STATIC_BLOCK) {
  1467. return true;
  1468. }
  1469. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1470. return false;
  1471. }
  1472. }
  1473. }
  1474. get inNonArrowFunction() {
  1475. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1476. }
  1477. get treatFunctionsAsVar() {
  1478. return this.treatFunctionsAsVarInScope(this.currentScope());
  1479. }
  1480. createScope(flags) {
  1481. return new Scope(flags);
  1482. }
  1483. enter(flags) {
  1484. this.scopeStack.push(this.createScope(flags));
  1485. }
  1486. exit() {
  1487. const scope = this.scopeStack.pop();
  1488. return scope.flags;
  1489. }
  1490. treatFunctionsAsVarInScope(scope) {
  1491. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  1492. }
  1493. declareName(name, bindingType, loc) {
  1494. let scope = this.currentScope();
  1495. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1496. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1497. if (bindingType & BIND_SCOPE_FUNCTION) {
  1498. scope.functions.add(name);
  1499. } else {
  1500. scope.lexical.add(name);
  1501. }
  1502. if (bindingType & BIND_SCOPE_LEXICAL) {
  1503. this.maybeExportDefined(scope, name);
  1504. }
  1505. } else if (bindingType & BIND_SCOPE_VAR) {
  1506. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1507. scope = this.scopeStack[i];
  1508. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1509. scope.var.add(name);
  1510. this.maybeExportDefined(scope, name);
  1511. if (scope.flags & SCOPE_VAR) break;
  1512. }
  1513. }
  1514. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1515. this.undefinedExports.delete(name);
  1516. }
  1517. }
  1518. maybeExportDefined(scope, name) {
  1519. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1520. this.undefinedExports.delete(name);
  1521. }
  1522. }
  1523. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1524. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1525. this.parser.raise(Errors.VarRedeclaration, {
  1526. at: loc,
  1527. identifierName: name
  1528. });
  1529. }
  1530. }
  1531. isRedeclaredInScope(scope, name, bindingType) {
  1532. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1533. if (bindingType & BIND_SCOPE_LEXICAL) {
  1534. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1535. }
  1536. if (bindingType & BIND_SCOPE_FUNCTION) {
  1537. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1538. }
  1539. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1540. }
  1541. checkLocalExport(id) {
  1542. const {
  1543. name
  1544. } = id;
  1545. const topLevelScope = this.scopeStack[0];
  1546. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1547. this.undefinedExports.set(name, id.loc.start);
  1548. }
  1549. }
  1550. currentScope() {
  1551. return this.scopeStack[this.scopeStack.length - 1];
  1552. }
  1553. currentVarScopeFlags() {
  1554. for (let i = this.scopeStack.length - 1;; i--) {
  1555. const {
  1556. flags
  1557. } = this.scopeStack[i];
  1558. if (flags & SCOPE_VAR) {
  1559. return flags;
  1560. }
  1561. }
  1562. }
  1563. currentThisScopeFlags() {
  1564. for (let i = this.scopeStack.length - 1;; i--) {
  1565. const {
  1566. flags
  1567. } = this.scopeStack[i];
  1568. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1569. return flags;
  1570. }
  1571. }
  1572. }
  1573. }
  1574. class FlowScope extends Scope {
  1575. constructor(...args) {
  1576. super(...args);
  1577. this.declareFunctions = new Set();
  1578. }
  1579. }
  1580. class FlowScopeHandler extends ScopeHandler {
  1581. createScope(flags) {
  1582. return new FlowScope(flags);
  1583. }
  1584. declareName(name, bindingType, loc) {
  1585. const scope = this.currentScope();
  1586. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1587. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1588. this.maybeExportDefined(scope, name);
  1589. scope.declareFunctions.add(name);
  1590. return;
  1591. }
  1592. super.declareName(name, bindingType, loc);
  1593. }
  1594. isRedeclaredInScope(scope, name, bindingType) {
  1595. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1596. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1597. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1598. }
  1599. return false;
  1600. }
  1601. checkLocalExport(id) {
  1602. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1603. super.checkLocalExport(id);
  1604. }
  1605. }
  1606. }
  1607. class BaseParser {
  1608. constructor() {
  1609. this.sawUnambiguousESM = false;
  1610. this.ambiguousScriptDifferentAst = false;
  1611. }
  1612. hasPlugin(pluginConfig) {
  1613. if (typeof pluginConfig === "string") {
  1614. return this.plugins.has(pluginConfig);
  1615. } else {
  1616. const [pluginName, pluginOptions] = pluginConfig;
  1617. if (!this.hasPlugin(pluginName)) {
  1618. return false;
  1619. }
  1620. const actualOptions = this.plugins.get(pluginName);
  1621. for (const key of Object.keys(pluginOptions)) {
  1622. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1623. return false;
  1624. }
  1625. }
  1626. return true;
  1627. }
  1628. }
  1629. getPluginOption(plugin, name) {
  1630. var _this$plugins$get;
  1631. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1632. }
  1633. }
  1634. function setTrailingComments(node, comments) {
  1635. if (node.trailingComments === undefined) {
  1636. node.trailingComments = comments;
  1637. } else {
  1638. node.trailingComments.unshift(...comments);
  1639. }
  1640. }
  1641. function setLeadingComments(node, comments) {
  1642. if (node.leadingComments === undefined) {
  1643. node.leadingComments = comments;
  1644. } else {
  1645. node.leadingComments.unshift(...comments);
  1646. }
  1647. }
  1648. function setInnerComments(node, comments) {
  1649. if (node.innerComments === undefined) {
  1650. node.innerComments = comments;
  1651. } else {
  1652. node.innerComments.unshift(...comments);
  1653. }
  1654. }
  1655. function adjustInnerComments(node, elements, commentWS) {
  1656. let lastElement = null;
  1657. let i = elements.length;
  1658. while (lastElement === null && i > 0) {
  1659. lastElement = elements[--i];
  1660. }
  1661. if (lastElement === null || lastElement.start > commentWS.start) {
  1662. setInnerComments(node, commentWS.comments);
  1663. } else {
  1664. setTrailingComments(lastElement, commentWS.comments);
  1665. }
  1666. }
  1667. class CommentsParser extends BaseParser {
  1668. addComment(comment) {
  1669. if (this.filename) comment.loc.filename = this.filename;
  1670. this.state.comments.push(comment);
  1671. }
  1672. processComment(node) {
  1673. const {
  1674. commentStack
  1675. } = this.state;
  1676. const commentStackLength = commentStack.length;
  1677. if (commentStackLength === 0) return;
  1678. let i = commentStackLength - 1;
  1679. const lastCommentWS = commentStack[i];
  1680. if (lastCommentWS.start === node.end) {
  1681. lastCommentWS.leadingNode = node;
  1682. i--;
  1683. }
  1684. const {
  1685. start: nodeStart
  1686. } = node;
  1687. for (; i >= 0; i--) {
  1688. const commentWS = commentStack[i];
  1689. const commentEnd = commentWS.end;
  1690. if (commentEnd > nodeStart) {
  1691. commentWS.containingNode = node;
  1692. this.finalizeComment(commentWS);
  1693. commentStack.splice(i, 1);
  1694. } else {
  1695. if (commentEnd === nodeStart) {
  1696. commentWS.trailingNode = node;
  1697. }
  1698. break;
  1699. }
  1700. }
  1701. }
  1702. finalizeComment(commentWS) {
  1703. const {
  1704. comments
  1705. } = commentWS;
  1706. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1707. if (commentWS.leadingNode !== null) {
  1708. setTrailingComments(commentWS.leadingNode, comments);
  1709. }
  1710. if (commentWS.trailingNode !== null) {
  1711. setLeadingComments(commentWS.trailingNode, comments);
  1712. }
  1713. } else {
  1714. const {
  1715. containingNode: node,
  1716. start: commentStart
  1717. } = commentWS;
  1718. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1719. switch (node.type) {
  1720. case "ObjectExpression":
  1721. case "ObjectPattern":
  1722. case "RecordExpression":
  1723. adjustInnerComments(node, node.properties, commentWS);
  1724. break;
  1725. case "CallExpression":
  1726. case "OptionalCallExpression":
  1727. adjustInnerComments(node, node.arguments, commentWS);
  1728. break;
  1729. case "FunctionDeclaration":
  1730. case "FunctionExpression":
  1731. case "ArrowFunctionExpression":
  1732. case "ObjectMethod":
  1733. case "ClassMethod":
  1734. case "ClassPrivateMethod":
  1735. adjustInnerComments(node, node.params, commentWS);
  1736. break;
  1737. case "ArrayExpression":
  1738. case "ArrayPattern":
  1739. case "TupleExpression":
  1740. adjustInnerComments(node, node.elements, commentWS);
  1741. break;
  1742. case "ExportNamedDeclaration":
  1743. case "ImportDeclaration":
  1744. adjustInnerComments(node, node.specifiers, commentWS);
  1745. break;
  1746. default:
  1747. {
  1748. setInnerComments(node, comments);
  1749. }
  1750. }
  1751. } else {
  1752. setInnerComments(node, comments);
  1753. }
  1754. }
  1755. }
  1756. finalizeRemainingComments() {
  1757. const {
  1758. commentStack
  1759. } = this.state;
  1760. for (let i = commentStack.length - 1; i >= 0; i--) {
  1761. this.finalizeComment(commentStack[i]);
  1762. }
  1763. this.state.commentStack = [];
  1764. }
  1765. resetPreviousNodeTrailingComments(node) {
  1766. const {
  1767. commentStack
  1768. } = this.state;
  1769. const {
  1770. length
  1771. } = commentStack;
  1772. if (length === 0) return;
  1773. const commentWS = commentStack[length - 1];
  1774. if (commentWS.leadingNode === node) {
  1775. commentWS.leadingNode = null;
  1776. }
  1777. }
  1778. takeSurroundingComments(node, start, end) {
  1779. const {
  1780. commentStack
  1781. } = this.state;
  1782. const commentStackLength = commentStack.length;
  1783. if (commentStackLength === 0) return;
  1784. let i = commentStackLength - 1;
  1785. for (; i >= 0; i--) {
  1786. const commentWS = commentStack[i];
  1787. const commentEnd = commentWS.end;
  1788. const commentStart = commentWS.start;
  1789. if (commentStart === end) {
  1790. commentWS.leadingNode = node;
  1791. } else if (commentEnd === start) {
  1792. commentWS.trailingNode = node;
  1793. } else if (commentEnd < start) {
  1794. break;
  1795. }
  1796. }
  1797. }
  1798. }
  1799. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1800. const lineBreakG = new RegExp(lineBreak.source, "g");
  1801. function isNewLine(code) {
  1802. switch (code) {
  1803. case 10:
  1804. case 13:
  1805. case 8232:
  1806. case 8233:
  1807. return true;
  1808. default:
  1809. return false;
  1810. }
  1811. }
  1812. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1813. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1814. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1815. function isWhitespace(code) {
  1816. switch (code) {
  1817. case 0x0009:
  1818. case 0x000b:
  1819. case 0x000c:
  1820. case 32:
  1821. case 160:
  1822. case 5760:
  1823. case 0x2000:
  1824. case 0x2001:
  1825. case 0x2002:
  1826. case 0x2003:
  1827. case 0x2004:
  1828. case 0x2005:
  1829. case 0x2006:
  1830. case 0x2007:
  1831. case 0x2008:
  1832. case 0x2009:
  1833. case 0x200a:
  1834. case 0x202f:
  1835. case 0x205f:
  1836. case 0x3000:
  1837. case 0xfeff:
  1838. return true;
  1839. default:
  1840. return false;
  1841. }
  1842. }
  1843. class State {
  1844. constructor() {
  1845. this.strict = void 0;
  1846. this.curLine = void 0;
  1847. this.lineStart = void 0;
  1848. this.startLoc = void 0;
  1849. this.endLoc = void 0;
  1850. this.errors = [];
  1851. this.potentialArrowAt = -1;
  1852. this.noArrowAt = [];
  1853. this.noArrowParamsConversionAt = [];
  1854. this.maybeInArrowParameters = false;
  1855. this.inType = false;
  1856. this.noAnonFunctionType = false;
  1857. this.hasFlowComment = false;
  1858. this.isAmbientContext = false;
  1859. this.inAbstractClass = false;
  1860. this.inDisallowConditionalTypesContext = false;
  1861. this.topicContext = {
  1862. maxNumOfResolvableTopics: 0,
  1863. maxTopicIndex: null
  1864. };
  1865. this.soloAwait = false;
  1866. this.inFSharpPipelineDirectBody = false;
  1867. this.labels = [];
  1868. this.comments = [];
  1869. this.commentStack = [];
  1870. this.pos = 0;
  1871. this.type = 137;
  1872. this.value = null;
  1873. this.start = 0;
  1874. this.end = 0;
  1875. this.lastTokEndLoc = null;
  1876. this.lastTokStartLoc = null;
  1877. this.lastTokStart = 0;
  1878. this.context = [types.brace];
  1879. this.canStartJSXElement = true;
  1880. this.containsEsc = false;
  1881. this.firstInvalidTemplateEscapePos = null;
  1882. this.strictErrors = new Map();
  1883. this.tokensLength = 0;
  1884. }
  1885. init({
  1886. strictMode,
  1887. sourceType,
  1888. startLine,
  1889. startColumn
  1890. }) {
  1891. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1892. this.curLine = startLine;
  1893. this.lineStart = -startColumn;
  1894. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1895. }
  1896. curPosition() {
  1897. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1898. }
  1899. clone(skipArrays) {
  1900. const state = new State();
  1901. const keys = Object.keys(this);
  1902. for (let i = 0, length = keys.length; i < length; i++) {
  1903. const key = keys[i];
  1904. let val = this[key];
  1905. if (!skipArrays && Array.isArray(val)) {
  1906. val = val.slice();
  1907. }
  1908. state[key] = val;
  1909. }
  1910. return state;
  1911. }
  1912. }
  1913. var _isDigit = function isDigit(code) {
  1914. return code >= 48 && code <= 57;
  1915. };
  1916. const forbiddenNumericSeparatorSiblings = {
  1917. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1918. hex: new Set([46, 88, 95, 120])
  1919. };
  1920. const isAllowedNumericSeparatorSibling = {
  1921. bin: ch => ch === 48 || ch === 49,
  1922. oct: ch => ch >= 48 && ch <= 55,
  1923. dec: ch => ch >= 48 && ch <= 57,
  1924. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1925. };
  1926. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1927. const initialPos = pos;
  1928. const initialLineStart = lineStart;
  1929. const initialCurLine = curLine;
  1930. let out = "";
  1931. let firstInvalidLoc = null;
  1932. let chunkStart = pos;
  1933. const {
  1934. length
  1935. } = input;
  1936. for (;;) {
  1937. if (pos >= length) {
  1938. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1939. out += input.slice(chunkStart, pos);
  1940. break;
  1941. }
  1942. const ch = input.charCodeAt(pos);
  1943. if (isStringEnd(type, ch, input, pos)) {
  1944. out += input.slice(chunkStart, pos);
  1945. break;
  1946. }
  1947. if (ch === 92) {
  1948. out += input.slice(chunkStart, pos);
  1949. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  1950. if (res.ch === null && !firstInvalidLoc) {
  1951. firstInvalidLoc = {
  1952. pos,
  1953. lineStart,
  1954. curLine
  1955. };
  1956. } else {
  1957. out += res.ch;
  1958. }
  1959. ({
  1960. pos,
  1961. lineStart,
  1962. curLine
  1963. } = res);
  1964. chunkStart = pos;
  1965. } else if (ch === 8232 || ch === 8233) {
  1966. ++pos;
  1967. ++curLine;
  1968. lineStart = pos;
  1969. } else if (ch === 10 || ch === 13) {
  1970. if (type === "template") {
  1971. out += input.slice(chunkStart, pos) + "\n";
  1972. ++pos;
  1973. if (ch === 13 && input.charCodeAt(pos) === 10) {
  1974. ++pos;
  1975. }
  1976. ++curLine;
  1977. chunkStart = lineStart = pos;
  1978. } else {
  1979. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1980. }
  1981. } else {
  1982. ++pos;
  1983. }
  1984. }
  1985. return {
  1986. pos,
  1987. str: out,
  1988. firstInvalidLoc,
  1989. lineStart,
  1990. curLine,
  1991. containsInvalid: !!firstInvalidLoc
  1992. };
  1993. }
  1994. function isStringEnd(type, ch, input, pos) {
  1995. if (type === "template") {
  1996. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  1997. }
  1998. return ch === (type === "double" ? 34 : 39);
  1999. }
  2000. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  2001. const throwOnInvalid = !inTemplate;
  2002. pos++;
  2003. const res = ch => ({
  2004. pos,
  2005. ch,
  2006. lineStart,
  2007. curLine
  2008. });
  2009. const ch = input.charCodeAt(pos++);
  2010. switch (ch) {
  2011. case 110:
  2012. return res("\n");
  2013. case 114:
  2014. return res("\r");
  2015. case 120:
  2016. {
  2017. let code;
  2018. ({
  2019. code,
  2020. pos
  2021. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  2022. return res(code === null ? null : String.fromCharCode(code));
  2023. }
  2024. case 117:
  2025. {
  2026. let code;
  2027. ({
  2028. code,
  2029. pos
  2030. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2031. return res(code === null ? null : String.fromCodePoint(code));
  2032. }
  2033. case 116:
  2034. return res("\t");
  2035. case 98:
  2036. return res("\b");
  2037. case 118:
  2038. return res("\u000b");
  2039. case 102:
  2040. return res("\f");
  2041. case 13:
  2042. if (input.charCodeAt(pos) === 10) {
  2043. ++pos;
  2044. }
  2045. case 10:
  2046. lineStart = pos;
  2047. ++curLine;
  2048. case 8232:
  2049. case 8233:
  2050. return res("");
  2051. case 56:
  2052. case 57:
  2053. if (inTemplate) {
  2054. return res(null);
  2055. } else {
  2056. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2057. }
  2058. default:
  2059. if (ch >= 48 && ch <= 55) {
  2060. const startPos = pos - 1;
  2061. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  2062. let octalStr = match[0];
  2063. let octal = parseInt(octalStr, 8);
  2064. if (octal > 255) {
  2065. octalStr = octalStr.slice(0, -1);
  2066. octal = parseInt(octalStr, 8);
  2067. }
  2068. pos += octalStr.length - 1;
  2069. const next = input.charCodeAt(pos);
  2070. if (octalStr !== "0" || next === 56 || next === 57) {
  2071. if (inTemplate) {
  2072. return res(null);
  2073. } else {
  2074. errors.strictNumericEscape(startPos, lineStart, curLine);
  2075. }
  2076. }
  2077. return res(String.fromCharCode(octal));
  2078. }
  2079. return res(String.fromCharCode(ch));
  2080. }
  2081. }
  2082. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2083. const initialPos = pos;
  2084. let n;
  2085. ({
  2086. n,
  2087. pos
  2088. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2089. if (n === null) {
  2090. if (throwOnInvalid) {
  2091. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2092. } else {
  2093. pos = initialPos - 1;
  2094. }
  2095. }
  2096. return {
  2097. code: n,
  2098. pos
  2099. };
  2100. }
  2101. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2102. const start = pos;
  2103. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2104. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2105. let invalid = false;
  2106. let total = 0;
  2107. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2108. const code = input.charCodeAt(pos);
  2109. let val;
  2110. if (code === 95 && allowNumSeparator !== "bail") {
  2111. const prev = input.charCodeAt(pos - 1);
  2112. const next = input.charCodeAt(pos + 1);
  2113. if (!allowNumSeparator) {
  2114. if (bailOnError) return {
  2115. n: null,
  2116. pos
  2117. };
  2118. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2119. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2120. if (bailOnError) return {
  2121. n: null,
  2122. pos
  2123. };
  2124. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2125. }
  2126. ++pos;
  2127. continue;
  2128. }
  2129. if (code >= 97) {
  2130. val = code - 97 + 10;
  2131. } else if (code >= 65) {
  2132. val = code - 65 + 10;
  2133. } else if (_isDigit(code)) {
  2134. val = code - 48;
  2135. } else {
  2136. val = Infinity;
  2137. }
  2138. if (val >= radix) {
  2139. if (val <= 9 && bailOnError) {
  2140. return {
  2141. n: null,
  2142. pos
  2143. };
  2144. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2145. val = 0;
  2146. } else if (forceLen) {
  2147. val = 0;
  2148. invalid = true;
  2149. } else {
  2150. break;
  2151. }
  2152. }
  2153. ++pos;
  2154. total = total * radix + val;
  2155. }
  2156. if (pos === start || len != null && pos - start !== len || invalid) {
  2157. return {
  2158. n: null,
  2159. pos
  2160. };
  2161. }
  2162. return {
  2163. n: total,
  2164. pos
  2165. };
  2166. }
  2167. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2168. const ch = input.charCodeAt(pos);
  2169. let code;
  2170. if (ch === 123) {
  2171. ++pos;
  2172. ({
  2173. code,
  2174. pos
  2175. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2176. ++pos;
  2177. if (code !== null && code > 0x10ffff) {
  2178. if (throwOnInvalid) {
  2179. errors.invalidCodePoint(pos, lineStart, curLine);
  2180. } else {
  2181. return {
  2182. code: null,
  2183. pos
  2184. };
  2185. }
  2186. }
  2187. } else {
  2188. ({
  2189. code,
  2190. pos
  2191. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2192. }
  2193. return {
  2194. code,
  2195. pos
  2196. };
  2197. }
  2198. const _excluded = ["at"],
  2199. _excluded2 = ["at"];
  2200. function buildPosition(pos, lineStart, curLine) {
  2201. return new Position(curLine, pos - lineStart, pos);
  2202. }
  2203. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2204. class Token {
  2205. constructor(state) {
  2206. this.type = state.type;
  2207. this.value = state.value;
  2208. this.start = state.start;
  2209. this.end = state.end;
  2210. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2211. }
  2212. }
  2213. class Tokenizer extends CommentsParser {
  2214. constructor(options, input) {
  2215. super();
  2216. this.isLookahead = void 0;
  2217. this.tokens = [];
  2218. this.errorHandlers_readInt = {
  2219. invalidDigit: (pos, lineStart, curLine, radix) => {
  2220. if (!this.options.errorRecovery) return false;
  2221. this.raise(Errors.InvalidDigit, {
  2222. at: buildPosition(pos, lineStart, curLine),
  2223. radix
  2224. });
  2225. return true;
  2226. },
  2227. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2228. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2229. };
  2230. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2231. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2232. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2233. });
  2234. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2235. strictNumericEscape: (pos, lineStart, curLine) => {
  2236. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2237. at: buildPosition(pos, lineStart, curLine)
  2238. });
  2239. },
  2240. unterminated: (pos, lineStart, curLine) => {
  2241. throw this.raise(Errors.UnterminatedString, {
  2242. at: buildPosition(pos - 1, lineStart, curLine)
  2243. });
  2244. }
  2245. });
  2246. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2247. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2248. unterminated: (pos, lineStart, curLine) => {
  2249. throw this.raise(Errors.UnterminatedTemplate, {
  2250. at: buildPosition(pos, lineStart, curLine)
  2251. });
  2252. }
  2253. });
  2254. this.state = new State();
  2255. this.state.init(options);
  2256. this.input = input;
  2257. this.length = input.length;
  2258. this.isLookahead = false;
  2259. }
  2260. pushToken(token) {
  2261. this.tokens.length = this.state.tokensLength;
  2262. this.tokens.push(token);
  2263. ++this.state.tokensLength;
  2264. }
  2265. next() {
  2266. this.checkKeywordEscapes();
  2267. if (this.options.tokens) {
  2268. this.pushToken(new Token(this.state));
  2269. }
  2270. this.state.lastTokStart = this.state.start;
  2271. this.state.lastTokEndLoc = this.state.endLoc;
  2272. this.state.lastTokStartLoc = this.state.startLoc;
  2273. this.nextToken();
  2274. }
  2275. eat(type) {
  2276. if (this.match(type)) {
  2277. this.next();
  2278. return true;
  2279. } else {
  2280. return false;
  2281. }
  2282. }
  2283. match(type) {
  2284. return this.state.type === type;
  2285. }
  2286. createLookaheadState(state) {
  2287. return {
  2288. pos: state.pos,
  2289. value: null,
  2290. type: state.type,
  2291. start: state.start,
  2292. end: state.end,
  2293. context: [this.curContext()],
  2294. inType: state.inType,
  2295. startLoc: state.startLoc,
  2296. lastTokEndLoc: state.lastTokEndLoc,
  2297. curLine: state.curLine,
  2298. lineStart: state.lineStart,
  2299. curPosition: state.curPosition
  2300. };
  2301. }
  2302. lookahead() {
  2303. const old = this.state;
  2304. this.state = this.createLookaheadState(old);
  2305. this.isLookahead = true;
  2306. this.nextToken();
  2307. this.isLookahead = false;
  2308. const curr = this.state;
  2309. this.state = old;
  2310. return curr;
  2311. }
  2312. nextTokenStart() {
  2313. return this.nextTokenStartSince(this.state.pos);
  2314. }
  2315. nextTokenStartSince(pos) {
  2316. skipWhiteSpace.lastIndex = pos;
  2317. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2318. }
  2319. lookaheadCharCode() {
  2320. return this.input.charCodeAt(this.nextTokenStart());
  2321. }
  2322. codePointAtPos(pos) {
  2323. let cp = this.input.charCodeAt(pos);
  2324. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2325. const trail = this.input.charCodeAt(pos);
  2326. if ((trail & 0xfc00) === 0xdc00) {
  2327. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2328. }
  2329. }
  2330. return cp;
  2331. }
  2332. setStrict(strict) {
  2333. this.state.strict = strict;
  2334. if (strict) {
  2335. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  2336. at
  2337. }));
  2338. this.state.strictErrors.clear();
  2339. }
  2340. }
  2341. curContext() {
  2342. return this.state.context[this.state.context.length - 1];
  2343. }
  2344. nextToken() {
  2345. this.skipSpace();
  2346. this.state.start = this.state.pos;
  2347. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2348. if (this.state.pos >= this.length) {
  2349. this.finishToken(137);
  2350. return;
  2351. }
  2352. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2353. }
  2354. skipBlockComment(commentEnd) {
  2355. let startLoc;
  2356. if (!this.isLookahead) startLoc = this.state.curPosition();
  2357. const start = this.state.pos;
  2358. const end = this.input.indexOf(commentEnd, start + 2);
  2359. if (end === -1) {
  2360. throw this.raise(Errors.UnterminatedComment, {
  2361. at: this.state.curPosition()
  2362. });
  2363. }
  2364. this.state.pos = end + commentEnd.length;
  2365. lineBreakG.lastIndex = start + 2;
  2366. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2367. ++this.state.curLine;
  2368. this.state.lineStart = lineBreakG.lastIndex;
  2369. }
  2370. if (this.isLookahead) return;
  2371. const comment = {
  2372. type: "CommentBlock",
  2373. value: this.input.slice(start + 2, end),
  2374. start,
  2375. end: end + commentEnd.length,
  2376. loc: new SourceLocation(startLoc, this.state.curPosition())
  2377. };
  2378. if (this.options.tokens) this.pushToken(comment);
  2379. return comment;
  2380. }
  2381. skipLineComment(startSkip) {
  2382. const start = this.state.pos;
  2383. let startLoc;
  2384. if (!this.isLookahead) startLoc = this.state.curPosition();
  2385. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2386. if (this.state.pos < this.length) {
  2387. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2388. ch = this.input.charCodeAt(this.state.pos);
  2389. }
  2390. }
  2391. if (this.isLookahead) return;
  2392. const end = this.state.pos;
  2393. const value = this.input.slice(start + startSkip, end);
  2394. const comment = {
  2395. type: "CommentLine",
  2396. value,
  2397. start,
  2398. end,
  2399. loc: new SourceLocation(startLoc, this.state.curPosition())
  2400. };
  2401. if (this.options.tokens) this.pushToken(comment);
  2402. return comment;
  2403. }
  2404. skipSpace() {
  2405. const spaceStart = this.state.pos;
  2406. const comments = [];
  2407. loop: while (this.state.pos < this.length) {
  2408. const ch = this.input.charCodeAt(this.state.pos);
  2409. switch (ch) {
  2410. case 32:
  2411. case 160:
  2412. case 9:
  2413. ++this.state.pos;
  2414. break;
  2415. case 13:
  2416. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2417. ++this.state.pos;
  2418. }
  2419. case 10:
  2420. case 8232:
  2421. case 8233:
  2422. ++this.state.pos;
  2423. ++this.state.curLine;
  2424. this.state.lineStart = this.state.pos;
  2425. break;
  2426. case 47:
  2427. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2428. case 42:
  2429. {
  2430. const comment = this.skipBlockComment("*/");
  2431. if (comment !== undefined) {
  2432. this.addComment(comment);
  2433. if (this.options.attachComment) comments.push(comment);
  2434. }
  2435. break;
  2436. }
  2437. case 47:
  2438. {
  2439. const comment = this.skipLineComment(2);
  2440. if (comment !== undefined) {
  2441. this.addComment(comment);
  2442. if (this.options.attachComment) comments.push(comment);
  2443. }
  2444. break;
  2445. }
  2446. default:
  2447. break loop;
  2448. }
  2449. break;
  2450. default:
  2451. if (isWhitespace(ch)) {
  2452. ++this.state.pos;
  2453. } else if (ch === 45 && !this.inModule) {
  2454. const pos = this.state.pos;
  2455. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2456. const comment = this.skipLineComment(3);
  2457. if (comment !== undefined) {
  2458. this.addComment(comment);
  2459. if (this.options.attachComment) comments.push(comment);
  2460. }
  2461. } else {
  2462. break loop;
  2463. }
  2464. } else if (ch === 60 && !this.inModule) {
  2465. const pos = this.state.pos;
  2466. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2467. const comment = this.skipLineComment(4);
  2468. if (comment !== undefined) {
  2469. this.addComment(comment);
  2470. if (this.options.attachComment) comments.push(comment);
  2471. }
  2472. } else {
  2473. break loop;
  2474. }
  2475. } else {
  2476. break loop;
  2477. }
  2478. }
  2479. }
  2480. if (comments.length > 0) {
  2481. const end = this.state.pos;
  2482. const commentWhitespace = {
  2483. start: spaceStart,
  2484. end,
  2485. comments,
  2486. leadingNode: null,
  2487. trailingNode: null,
  2488. containingNode: null
  2489. };
  2490. this.state.commentStack.push(commentWhitespace);
  2491. }
  2492. }
  2493. finishToken(type, val) {
  2494. this.state.end = this.state.pos;
  2495. this.state.endLoc = this.state.curPosition();
  2496. const prevType = this.state.type;
  2497. this.state.type = type;
  2498. this.state.value = val;
  2499. if (!this.isLookahead) {
  2500. this.updateContext(prevType);
  2501. }
  2502. }
  2503. replaceToken(type) {
  2504. this.state.type = type;
  2505. this.updateContext();
  2506. }
  2507. readToken_numberSign() {
  2508. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2509. return;
  2510. }
  2511. const nextPos = this.state.pos + 1;
  2512. const next = this.codePointAtPos(nextPos);
  2513. if (next >= 48 && next <= 57) {
  2514. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  2515. at: this.state.curPosition()
  2516. });
  2517. }
  2518. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2519. this.expectPlugin("recordAndTuple");
  2520. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2521. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  2522. at: this.state.curPosition()
  2523. });
  2524. }
  2525. this.state.pos += 2;
  2526. if (next === 123) {
  2527. this.finishToken(7);
  2528. } else {
  2529. this.finishToken(1);
  2530. }
  2531. } else if (isIdentifierStart(next)) {
  2532. ++this.state.pos;
  2533. this.finishToken(136, this.readWord1(next));
  2534. } else if (next === 92) {
  2535. ++this.state.pos;
  2536. this.finishToken(136, this.readWord1());
  2537. } else {
  2538. this.finishOp(27, 1);
  2539. }
  2540. }
  2541. readToken_dot() {
  2542. const next = this.input.charCodeAt(this.state.pos + 1);
  2543. if (next >= 48 && next <= 57) {
  2544. this.readNumber(true);
  2545. return;
  2546. }
  2547. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2548. this.state.pos += 3;
  2549. this.finishToken(21);
  2550. } else {
  2551. ++this.state.pos;
  2552. this.finishToken(16);
  2553. }
  2554. }
  2555. readToken_slash() {
  2556. const next = this.input.charCodeAt(this.state.pos + 1);
  2557. if (next === 61) {
  2558. this.finishOp(31, 2);
  2559. } else {
  2560. this.finishOp(56, 1);
  2561. }
  2562. }
  2563. readToken_interpreter() {
  2564. if (this.state.pos !== 0 || this.length < 2) return false;
  2565. let ch = this.input.charCodeAt(this.state.pos + 1);
  2566. if (ch !== 33) return false;
  2567. const start = this.state.pos;
  2568. this.state.pos += 1;
  2569. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2570. ch = this.input.charCodeAt(this.state.pos);
  2571. }
  2572. const value = this.input.slice(start + 2, this.state.pos);
  2573. this.finishToken(28, value);
  2574. return true;
  2575. }
  2576. readToken_mult_modulo(code) {
  2577. let type = code === 42 ? 55 : 54;
  2578. let width = 1;
  2579. let next = this.input.charCodeAt(this.state.pos + 1);
  2580. if (code === 42 && next === 42) {
  2581. width++;
  2582. next = this.input.charCodeAt(this.state.pos + 2);
  2583. type = 57;
  2584. }
  2585. if (next === 61 && !this.state.inType) {
  2586. width++;
  2587. type = code === 37 ? 33 : 30;
  2588. }
  2589. this.finishOp(type, width);
  2590. }
  2591. readToken_pipe_amp(code) {
  2592. const next = this.input.charCodeAt(this.state.pos + 1);
  2593. if (next === code) {
  2594. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2595. this.finishOp(30, 3);
  2596. } else {
  2597. this.finishOp(code === 124 ? 41 : 42, 2);
  2598. }
  2599. return;
  2600. }
  2601. if (code === 124) {
  2602. if (next === 62) {
  2603. this.finishOp(39, 2);
  2604. return;
  2605. }
  2606. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2607. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2608. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2609. at: this.state.curPosition()
  2610. });
  2611. }
  2612. this.state.pos += 2;
  2613. this.finishToken(9);
  2614. return;
  2615. }
  2616. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2617. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2618. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2619. at: this.state.curPosition()
  2620. });
  2621. }
  2622. this.state.pos += 2;
  2623. this.finishToken(4);
  2624. return;
  2625. }
  2626. }
  2627. if (next === 61) {
  2628. this.finishOp(30, 2);
  2629. return;
  2630. }
  2631. this.finishOp(code === 124 ? 43 : 45, 1);
  2632. }
  2633. readToken_caret() {
  2634. const next = this.input.charCodeAt(this.state.pos + 1);
  2635. if (next === 61 && !this.state.inType) {
  2636. this.finishOp(32, 2);
  2637. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2638. proposal: "hack",
  2639. topicToken: "^^"
  2640. }])) {
  2641. this.finishOp(37, 2);
  2642. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2643. if (lookaheadCh === 94) {
  2644. throw this.unexpected();
  2645. }
  2646. } else {
  2647. this.finishOp(44, 1);
  2648. }
  2649. }
  2650. readToken_atSign() {
  2651. const next = this.input.charCodeAt(this.state.pos + 1);
  2652. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2653. proposal: "hack",
  2654. topicToken: "@@"
  2655. }])) {
  2656. this.finishOp(38, 2);
  2657. } else {
  2658. this.finishOp(26, 1);
  2659. }
  2660. }
  2661. readToken_plus_min(code) {
  2662. const next = this.input.charCodeAt(this.state.pos + 1);
  2663. if (next === code) {
  2664. this.finishOp(34, 2);
  2665. return;
  2666. }
  2667. if (next === 61) {
  2668. this.finishOp(30, 2);
  2669. } else {
  2670. this.finishOp(53, 1);
  2671. }
  2672. }
  2673. readToken_lt() {
  2674. const {
  2675. pos
  2676. } = this.state;
  2677. const next = this.input.charCodeAt(pos + 1);
  2678. if (next === 60) {
  2679. if (this.input.charCodeAt(pos + 2) === 61) {
  2680. this.finishOp(30, 3);
  2681. return;
  2682. }
  2683. this.finishOp(51, 2);
  2684. return;
  2685. }
  2686. if (next === 61) {
  2687. this.finishOp(49, 2);
  2688. return;
  2689. }
  2690. this.finishOp(47, 1);
  2691. }
  2692. readToken_gt() {
  2693. const {
  2694. pos
  2695. } = this.state;
  2696. const next = this.input.charCodeAt(pos + 1);
  2697. if (next === 62) {
  2698. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2699. if (this.input.charCodeAt(pos + size) === 61) {
  2700. this.finishOp(30, size + 1);
  2701. return;
  2702. }
  2703. this.finishOp(52, size);
  2704. return;
  2705. }
  2706. if (next === 61) {
  2707. this.finishOp(49, 2);
  2708. return;
  2709. }
  2710. this.finishOp(48, 1);
  2711. }
  2712. readToken_eq_excl(code) {
  2713. const next = this.input.charCodeAt(this.state.pos + 1);
  2714. if (next === 61) {
  2715. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2716. return;
  2717. }
  2718. if (code === 61 && next === 62) {
  2719. this.state.pos += 2;
  2720. this.finishToken(19);
  2721. return;
  2722. }
  2723. this.finishOp(code === 61 ? 29 : 35, 1);
  2724. }
  2725. readToken_question() {
  2726. const next = this.input.charCodeAt(this.state.pos + 1);
  2727. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2728. if (next === 63) {
  2729. if (next2 === 61) {
  2730. this.finishOp(30, 3);
  2731. } else {
  2732. this.finishOp(40, 2);
  2733. }
  2734. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2735. this.state.pos += 2;
  2736. this.finishToken(18);
  2737. } else {
  2738. ++this.state.pos;
  2739. this.finishToken(17);
  2740. }
  2741. }
  2742. getTokenFromCode(code) {
  2743. switch (code) {
  2744. case 46:
  2745. this.readToken_dot();
  2746. return;
  2747. case 40:
  2748. ++this.state.pos;
  2749. this.finishToken(10);
  2750. return;
  2751. case 41:
  2752. ++this.state.pos;
  2753. this.finishToken(11);
  2754. return;
  2755. case 59:
  2756. ++this.state.pos;
  2757. this.finishToken(13);
  2758. return;
  2759. case 44:
  2760. ++this.state.pos;
  2761. this.finishToken(12);
  2762. return;
  2763. case 91:
  2764. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2765. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2766. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2767. at: this.state.curPosition()
  2768. });
  2769. }
  2770. this.state.pos += 2;
  2771. this.finishToken(2);
  2772. } else {
  2773. ++this.state.pos;
  2774. this.finishToken(0);
  2775. }
  2776. return;
  2777. case 93:
  2778. ++this.state.pos;
  2779. this.finishToken(3);
  2780. return;
  2781. case 123:
  2782. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2783. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2784. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2785. at: this.state.curPosition()
  2786. });
  2787. }
  2788. this.state.pos += 2;
  2789. this.finishToken(6);
  2790. } else {
  2791. ++this.state.pos;
  2792. this.finishToken(5);
  2793. }
  2794. return;
  2795. case 125:
  2796. ++this.state.pos;
  2797. this.finishToken(8);
  2798. return;
  2799. case 58:
  2800. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2801. this.finishOp(15, 2);
  2802. } else {
  2803. ++this.state.pos;
  2804. this.finishToken(14);
  2805. }
  2806. return;
  2807. case 63:
  2808. this.readToken_question();
  2809. return;
  2810. case 96:
  2811. this.readTemplateToken();
  2812. return;
  2813. case 48:
  2814. {
  2815. const next = this.input.charCodeAt(this.state.pos + 1);
  2816. if (next === 120 || next === 88) {
  2817. this.readRadixNumber(16);
  2818. return;
  2819. }
  2820. if (next === 111 || next === 79) {
  2821. this.readRadixNumber(8);
  2822. return;
  2823. }
  2824. if (next === 98 || next === 66) {
  2825. this.readRadixNumber(2);
  2826. return;
  2827. }
  2828. }
  2829. case 49:
  2830. case 50:
  2831. case 51:
  2832. case 52:
  2833. case 53:
  2834. case 54:
  2835. case 55:
  2836. case 56:
  2837. case 57:
  2838. this.readNumber(false);
  2839. return;
  2840. case 34:
  2841. case 39:
  2842. this.readString(code);
  2843. return;
  2844. case 47:
  2845. this.readToken_slash();
  2846. return;
  2847. case 37:
  2848. case 42:
  2849. this.readToken_mult_modulo(code);
  2850. return;
  2851. case 124:
  2852. case 38:
  2853. this.readToken_pipe_amp(code);
  2854. return;
  2855. case 94:
  2856. this.readToken_caret();
  2857. return;
  2858. case 43:
  2859. case 45:
  2860. this.readToken_plus_min(code);
  2861. return;
  2862. case 60:
  2863. this.readToken_lt();
  2864. return;
  2865. case 62:
  2866. this.readToken_gt();
  2867. return;
  2868. case 61:
  2869. case 33:
  2870. this.readToken_eq_excl(code);
  2871. return;
  2872. case 126:
  2873. this.finishOp(36, 1);
  2874. return;
  2875. case 64:
  2876. this.readToken_atSign();
  2877. return;
  2878. case 35:
  2879. this.readToken_numberSign();
  2880. return;
  2881. case 92:
  2882. this.readWord();
  2883. return;
  2884. default:
  2885. if (isIdentifierStart(code)) {
  2886. this.readWord(code);
  2887. return;
  2888. }
  2889. }
  2890. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2891. at: this.state.curPosition(),
  2892. unexpected: String.fromCodePoint(code)
  2893. });
  2894. }
  2895. finishOp(type, size) {
  2896. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2897. this.state.pos += size;
  2898. this.finishToken(type, str);
  2899. }
  2900. readRegexp() {
  2901. const startLoc = this.state.startLoc;
  2902. const start = this.state.start + 1;
  2903. let escaped, inClass;
  2904. let {
  2905. pos
  2906. } = this.state;
  2907. for (;; ++pos) {
  2908. if (pos >= this.length) {
  2909. throw this.raise(Errors.UnterminatedRegExp, {
  2910. at: createPositionWithColumnOffset(startLoc, 1)
  2911. });
  2912. }
  2913. const ch = this.input.charCodeAt(pos);
  2914. if (isNewLine(ch)) {
  2915. throw this.raise(Errors.UnterminatedRegExp, {
  2916. at: createPositionWithColumnOffset(startLoc, 1)
  2917. });
  2918. }
  2919. if (escaped) {
  2920. escaped = false;
  2921. } else {
  2922. if (ch === 91) {
  2923. inClass = true;
  2924. } else if (ch === 93 && inClass) {
  2925. inClass = false;
  2926. } else if (ch === 47 && !inClass) {
  2927. break;
  2928. }
  2929. escaped = ch === 92;
  2930. }
  2931. }
  2932. const content = this.input.slice(start, pos);
  2933. ++pos;
  2934. let mods = "";
  2935. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2936. while (pos < this.length) {
  2937. const cp = this.codePointAtPos(pos);
  2938. const char = String.fromCharCode(cp);
  2939. if (VALID_REGEX_FLAGS.has(cp)) {
  2940. if (cp === 118) {
  2941. this.expectPlugin("regexpUnicodeSets", nextPos());
  2942. if (mods.includes("u")) {
  2943. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2944. at: nextPos()
  2945. });
  2946. }
  2947. } else if (cp === 117) {
  2948. if (mods.includes("v")) {
  2949. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2950. at: nextPos()
  2951. });
  2952. }
  2953. }
  2954. if (mods.includes(char)) {
  2955. this.raise(Errors.DuplicateRegExpFlags, {
  2956. at: nextPos()
  2957. });
  2958. }
  2959. } else if (isIdentifierChar(cp) || cp === 92) {
  2960. this.raise(Errors.MalformedRegExpFlags, {
  2961. at: nextPos()
  2962. });
  2963. } else {
  2964. break;
  2965. }
  2966. ++pos;
  2967. mods += char;
  2968. }
  2969. this.state.pos = pos;
  2970. this.finishToken(135, {
  2971. pattern: content,
  2972. flags: mods
  2973. });
  2974. }
  2975. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  2976. const {
  2977. n,
  2978. pos
  2979. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  2980. this.state.pos = pos;
  2981. return n;
  2982. }
  2983. readRadixNumber(radix) {
  2984. const startLoc = this.state.curPosition();
  2985. let isBigInt = false;
  2986. this.state.pos += 2;
  2987. const val = this.readInt(radix);
  2988. if (val == null) {
  2989. this.raise(Errors.InvalidDigit, {
  2990. at: createPositionWithColumnOffset(startLoc, 2),
  2991. radix
  2992. });
  2993. }
  2994. const next = this.input.charCodeAt(this.state.pos);
  2995. if (next === 110) {
  2996. ++this.state.pos;
  2997. isBigInt = true;
  2998. } else if (next === 109) {
  2999. throw this.raise(Errors.InvalidDecimal, {
  3000. at: startLoc
  3001. });
  3002. }
  3003. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3004. throw this.raise(Errors.NumberIdentifier, {
  3005. at: this.state.curPosition()
  3006. });
  3007. }
  3008. if (isBigInt) {
  3009. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  3010. this.finishToken(133, str);
  3011. return;
  3012. }
  3013. this.finishToken(132, val);
  3014. }
  3015. readNumber(startsWithDot) {
  3016. const start = this.state.pos;
  3017. const startLoc = this.state.curPosition();
  3018. let isFloat = false;
  3019. let isBigInt = false;
  3020. let isDecimal = false;
  3021. let hasExponent = false;
  3022. let isOctal = false;
  3023. if (!startsWithDot && this.readInt(10) === null) {
  3024. this.raise(Errors.InvalidNumber, {
  3025. at: this.state.curPosition()
  3026. });
  3027. }
  3028. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3029. if (hasLeadingZero) {
  3030. const integer = this.input.slice(start, this.state.pos);
  3031. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  3032. at: startLoc
  3033. });
  3034. if (!this.state.strict) {
  3035. const underscorePos = integer.indexOf("_");
  3036. if (underscorePos > 0) {
  3037. this.raise(Errors.ZeroDigitNumericSeparator, {
  3038. at: createPositionWithColumnOffset(startLoc, underscorePos)
  3039. });
  3040. }
  3041. }
  3042. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3043. }
  3044. let next = this.input.charCodeAt(this.state.pos);
  3045. if (next === 46 && !isOctal) {
  3046. ++this.state.pos;
  3047. this.readInt(10);
  3048. isFloat = true;
  3049. next = this.input.charCodeAt(this.state.pos);
  3050. }
  3051. if ((next === 69 || next === 101) && !isOctal) {
  3052. next = this.input.charCodeAt(++this.state.pos);
  3053. if (next === 43 || next === 45) {
  3054. ++this.state.pos;
  3055. }
  3056. if (this.readInt(10) === null) {
  3057. this.raise(Errors.InvalidOrMissingExponent, {
  3058. at: startLoc
  3059. });
  3060. }
  3061. isFloat = true;
  3062. hasExponent = true;
  3063. next = this.input.charCodeAt(this.state.pos);
  3064. }
  3065. if (next === 110) {
  3066. if (isFloat || hasLeadingZero) {
  3067. this.raise(Errors.InvalidBigIntLiteral, {
  3068. at: startLoc
  3069. });
  3070. }
  3071. ++this.state.pos;
  3072. isBigInt = true;
  3073. }
  3074. if (next === 109) {
  3075. this.expectPlugin("decimal", this.state.curPosition());
  3076. if (hasExponent || hasLeadingZero) {
  3077. this.raise(Errors.InvalidDecimal, {
  3078. at: startLoc
  3079. });
  3080. }
  3081. ++this.state.pos;
  3082. isDecimal = true;
  3083. }
  3084. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3085. throw this.raise(Errors.NumberIdentifier, {
  3086. at: this.state.curPosition()
  3087. });
  3088. }
  3089. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3090. if (isBigInt) {
  3091. this.finishToken(133, str);
  3092. return;
  3093. }
  3094. if (isDecimal) {
  3095. this.finishToken(134, str);
  3096. return;
  3097. }
  3098. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3099. this.finishToken(132, val);
  3100. }
  3101. readCodePoint(throwOnInvalid) {
  3102. const {
  3103. code,
  3104. pos
  3105. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3106. this.state.pos = pos;
  3107. return code;
  3108. }
  3109. readString(quote) {
  3110. const {
  3111. str,
  3112. pos,
  3113. curLine,
  3114. lineStart
  3115. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3116. this.state.pos = pos + 1;
  3117. this.state.lineStart = lineStart;
  3118. this.state.curLine = curLine;
  3119. this.finishToken(131, str);
  3120. }
  3121. readTemplateContinuation() {
  3122. if (!this.match(8)) {
  3123. this.unexpected(null, 8);
  3124. }
  3125. this.state.pos--;
  3126. this.readTemplateToken();
  3127. }
  3128. readTemplateToken() {
  3129. const opening = this.input[this.state.pos];
  3130. const {
  3131. str,
  3132. firstInvalidLoc,
  3133. pos,
  3134. curLine,
  3135. lineStart
  3136. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3137. this.state.pos = pos + 1;
  3138. this.state.lineStart = lineStart;
  3139. this.state.curLine = curLine;
  3140. if (firstInvalidLoc) {
  3141. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3142. }
  3143. if (this.input.codePointAt(pos) === 96) {
  3144. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3145. } else {
  3146. this.state.pos++;
  3147. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3148. }
  3149. }
  3150. recordStrictModeErrors(toParseError, {
  3151. at
  3152. }) {
  3153. const index = at.index;
  3154. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3155. this.raise(toParseError, {
  3156. at
  3157. });
  3158. } else {
  3159. this.state.strictErrors.set(index, [toParseError, at]);
  3160. }
  3161. }
  3162. readWord1(firstCode) {
  3163. this.state.containsEsc = false;
  3164. let word = "";
  3165. const start = this.state.pos;
  3166. let chunkStart = this.state.pos;
  3167. if (firstCode !== undefined) {
  3168. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3169. }
  3170. while (this.state.pos < this.length) {
  3171. const ch = this.codePointAtPos(this.state.pos);
  3172. if (isIdentifierChar(ch)) {
  3173. this.state.pos += ch <= 0xffff ? 1 : 2;
  3174. } else if (ch === 92) {
  3175. this.state.containsEsc = true;
  3176. word += this.input.slice(chunkStart, this.state.pos);
  3177. const escStart = this.state.curPosition();
  3178. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3179. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3180. this.raise(Errors.MissingUnicodeEscape, {
  3181. at: this.state.curPosition()
  3182. });
  3183. chunkStart = this.state.pos - 1;
  3184. continue;
  3185. }
  3186. ++this.state.pos;
  3187. const esc = this.readCodePoint(true);
  3188. if (esc !== null) {
  3189. if (!identifierCheck(esc)) {
  3190. this.raise(Errors.EscapedCharNotAnIdentifier, {
  3191. at: escStart
  3192. });
  3193. }
  3194. word += String.fromCodePoint(esc);
  3195. }
  3196. chunkStart = this.state.pos;
  3197. } else {
  3198. break;
  3199. }
  3200. }
  3201. return word + this.input.slice(chunkStart, this.state.pos);
  3202. }
  3203. readWord(firstCode) {
  3204. const word = this.readWord1(firstCode);
  3205. const type = keywords$1.get(word);
  3206. if (type !== undefined) {
  3207. this.finishToken(type, tokenLabelName(type));
  3208. } else {
  3209. this.finishToken(130, word);
  3210. }
  3211. }
  3212. checkKeywordEscapes() {
  3213. const {
  3214. type
  3215. } = this.state;
  3216. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3217. this.raise(Errors.InvalidEscapedReservedWord, {
  3218. at: this.state.startLoc,
  3219. reservedWord: tokenLabelName(type)
  3220. });
  3221. }
  3222. }
  3223. raise(toParseError, raiseProperties) {
  3224. const {
  3225. at
  3226. } = raiseProperties,
  3227. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  3228. const loc = at instanceof Position ? at : at.loc.start;
  3229. const error = toParseError({
  3230. loc,
  3231. details
  3232. });
  3233. if (!this.options.errorRecovery) throw error;
  3234. if (!this.isLookahead) this.state.errors.push(error);
  3235. return error;
  3236. }
  3237. raiseOverwrite(toParseError, raiseProperties) {
  3238. const {
  3239. at
  3240. } = raiseProperties,
  3241. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  3242. const loc = at instanceof Position ? at : at.loc.start;
  3243. const pos = loc.index;
  3244. const errors = this.state.errors;
  3245. for (let i = errors.length - 1; i >= 0; i--) {
  3246. const error = errors[i];
  3247. if (error.loc.index === pos) {
  3248. return errors[i] = toParseError({
  3249. loc,
  3250. details
  3251. });
  3252. }
  3253. if (error.loc.index < pos) break;
  3254. }
  3255. return this.raise(toParseError, raiseProperties);
  3256. }
  3257. updateContext(prevType) {}
  3258. unexpected(loc, type) {
  3259. throw this.raise(Errors.UnexpectedToken, {
  3260. expected: type ? tokenLabelName(type) : null,
  3261. at: loc != null ? loc : this.state.startLoc
  3262. });
  3263. }
  3264. expectPlugin(pluginName, loc) {
  3265. if (this.hasPlugin(pluginName)) {
  3266. return true;
  3267. }
  3268. throw this.raise(Errors.MissingPlugin, {
  3269. at: loc != null ? loc : this.state.startLoc,
  3270. missingPlugin: [pluginName]
  3271. });
  3272. }
  3273. expectOnePlugin(pluginNames) {
  3274. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3275. throw this.raise(Errors.MissingOneOfPlugins, {
  3276. at: this.state.startLoc,
  3277. missingPlugin: pluginNames
  3278. });
  3279. }
  3280. }
  3281. errorBuilder(error) {
  3282. return (pos, lineStart, curLine) => {
  3283. this.raise(error, {
  3284. at: buildPosition(pos, lineStart, curLine)
  3285. });
  3286. };
  3287. }
  3288. }
  3289. class ClassScope {
  3290. constructor() {
  3291. this.privateNames = new Set();
  3292. this.loneAccessors = new Map();
  3293. this.undefinedPrivateNames = new Map();
  3294. }
  3295. }
  3296. class ClassScopeHandler {
  3297. constructor(parser) {
  3298. this.parser = void 0;
  3299. this.stack = [];
  3300. this.undefinedPrivateNames = new Map();
  3301. this.parser = parser;
  3302. }
  3303. current() {
  3304. return this.stack[this.stack.length - 1];
  3305. }
  3306. enter() {
  3307. this.stack.push(new ClassScope());
  3308. }
  3309. exit() {
  3310. const oldClassScope = this.stack.pop();
  3311. const current = this.current();
  3312. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3313. if (current) {
  3314. if (!current.undefinedPrivateNames.has(name)) {
  3315. current.undefinedPrivateNames.set(name, loc);
  3316. }
  3317. } else {
  3318. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3319. at: loc,
  3320. identifierName: name
  3321. });
  3322. }
  3323. }
  3324. }
  3325. declarePrivateName(name, elementType, loc) {
  3326. const {
  3327. privateNames,
  3328. loneAccessors,
  3329. undefinedPrivateNames
  3330. } = this.current();
  3331. let redefined = privateNames.has(name);
  3332. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3333. const accessor = redefined && loneAccessors.get(name);
  3334. if (accessor) {
  3335. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3336. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3337. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3338. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3339. redefined = oldKind === newKind || oldStatic !== newStatic;
  3340. if (!redefined) loneAccessors.delete(name);
  3341. } else if (!redefined) {
  3342. loneAccessors.set(name, elementType);
  3343. }
  3344. }
  3345. if (redefined) {
  3346. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3347. at: loc,
  3348. identifierName: name
  3349. });
  3350. }
  3351. privateNames.add(name);
  3352. undefinedPrivateNames.delete(name);
  3353. }
  3354. usePrivateName(name, loc) {
  3355. let classScope;
  3356. for (classScope of this.stack) {
  3357. if (classScope.privateNames.has(name)) return;
  3358. }
  3359. if (classScope) {
  3360. classScope.undefinedPrivateNames.set(name, loc);
  3361. } else {
  3362. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3363. at: loc,
  3364. identifierName: name
  3365. });
  3366. }
  3367. }
  3368. }
  3369. const kExpression = 0,
  3370. kMaybeArrowParameterDeclaration = 1,
  3371. kMaybeAsyncArrowParameterDeclaration = 2,
  3372. kParameterDeclaration = 3;
  3373. class ExpressionScope {
  3374. constructor(type = kExpression) {
  3375. this.type = void 0;
  3376. this.type = type;
  3377. }
  3378. canBeArrowParameterDeclaration() {
  3379. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3380. }
  3381. isCertainlyParameterDeclaration() {
  3382. return this.type === kParameterDeclaration;
  3383. }
  3384. }
  3385. class ArrowHeadParsingScope extends ExpressionScope {
  3386. constructor(type) {
  3387. super(type);
  3388. this.declarationErrors = new Map();
  3389. }
  3390. recordDeclarationError(ParsingErrorClass, {
  3391. at
  3392. }) {
  3393. const index = at.index;
  3394. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3395. }
  3396. clearDeclarationError(index) {
  3397. this.declarationErrors.delete(index);
  3398. }
  3399. iterateErrors(iterator) {
  3400. this.declarationErrors.forEach(iterator);
  3401. }
  3402. }
  3403. class ExpressionScopeHandler {
  3404. constructor(parser) {
  3405. this.parser = void 0;
  3406. this.stack = [new ExpressionScope()];
  3407. this.parser = parser;
  3408. }
  3409. enter(scope) {
  3410. this.stack.push(scope);
  3411. }
  3412. exit() {
  3413. this.stack.pop();
  3414. }
  3415. recordParameterInitializerError(toParseError, {
  3416. at: node
  3417. }) {
  3418. const origin = {
  3419. at: node.loc.start
  3420. };
  3421. const {
  3422. stack
  3423. } = this;
  3424. let i = stack.length - 1;
  3425. let scope = stack[i];
  3426. while (!scope.isCertainlyParameterDeclaration()) {
  3427. if (scope.canBeArrowParameterDeclaration()) {
  3428. scope.recordDeclarationError(toParseError, origin);
  3429. } else {
  3430. return;
  3431. }
  3432. scope = stack[--i];
  3433. }
  3434. this.parser.raise(toParseError, origin);
  3435. }
  3436. recordArrowParameterBindingError(error, {
  3437. at: node
  3438. }) {
  3439. const {
  3440. stack
  3441. } = this;
  3442. const scope = stack[stack.length - 1];
  3443. const origin = {
  3444. at: node.loc.start
  3445. };
  3446. if (scope.isCertainlyParameterDeclaration()) {
  3447. this.parser.raise(error, origin);
  3448. } else if (scope.canBeArrowParameterDeclaration()) {
  3449. scope.recordDeclarationError(error, origin);
  3450. } else {
  3451. return;
  3452. }
  3453. }
  3454. recordAsyncArrowParametersError({
  3455. at
  3456. }) {
  3457. const {
  3458. stack
  3459. } = this;
  3460. let i = stack.length - 1;
  3461. let scope = stack[i];
  3462. while (scope.canBeArrowParameterDeclaration()) {
  3463. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3464. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3465. at
  3466. });
  3467. }
  3468. scope = stack[--i];
  3469. }
  3470. }
  3471. validateAsPattern() {
  3472. const {
  3473. stack
  3474. } = this;
  3475. const currentScope = stack[stack.length - 1];
  3476. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3477. currentScope.iterateErrors(([toParseError, loc]) => {
  3478. this.parser.raise(toParseError, {
  3479. at: loc
  3480. });
  3481. let i = stack.length - 2;
  3482. let scope = stack[i];
  3483. while (scope.canBeArrowParameterDeclaration()) {
  3484. scope.clearDeclarationError(loc.index);
  3485. scope = stack[--i];
  3486. }
  3487. });
  3488. }
  3489. }
  3490. function newParameterDeclarationScope() {
  3491. return new ExpressionScope(kParameterDeclaration);
  3492. }
  3493. function newArrowHeadScope() {
  3494. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3495. }
  3496. function newAsyncArrowScope() {
  3497. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3498. }
  3499. function newExpressionScope() {
  3500. return new ExpressionScope();
  3501. }
  3502. const PARAM = 0b0000,
  3503. PARAM_YIELD = 0b0001,
  3504. PARAM_AWAIT = 0b0010,
  3505. PARAM_RETURN = 0b0100,
  3506. PARAM_IN = 0b1000;
  3507. class ProductionParameterHandler {
  3508. constructor() {
  3509. this.stacks = [];
  3510. }
  3511. enter(flags) {
  3512. this.stacks.push(flags);
  3513. }
  3514. exit() {
  3515. this.stacks.pop();
  3516. }
  3517. currentFlags() {
  3518. return this.stacks[this.stacks.length - 1];
  3519. }
  3520. get hasAwait() {
  3521. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3522. }
  3523. get hasYield() {
  3524. return (this.currentFlags() & PARAM_YIELD) > 0;
  3525. }
  3526. get hasReturn() {
  3527. return (this.currentFlags() & PARAM_RETURN) > 0;
  3528. }
  3529. get hasIn() {
  3530. return (this.currentFlags() & PARAM_IN) > 0;
  3531. }
  3532. }
  3533. function functionFlags(isAsync, isGenerator) {
  3534. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3535. }
  3536. class UtilParser extends Tokenizer {
  3537. addExtra(node, key, value, enumerable = true) {
  3538. if (!node) return;
  3539. const extra = node.extra = node.extra || {};
  3540. if (enumerable) {
  3541. extra[key] = value;
  3542. } else {
  3543. Object.defineProperty(extra, key, {
  3544. enumerable,
  3545. value
  3546. });
  3547. }
  3548. }
  3549. isContextual(token) {
  3550. return this.state.type === token && !this.state.containsEsc;
  3551. }
  3552. isUnparsedContextual(nameStart, name) {
  3553. const nameEnd = nameStart + name.length;
  3554. if (this.input.slice(nameStart, nameEnd) === name) {
  3555. const nextCh = this.input.charCodeAt(nameEnd);
  3556. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3557. }
  3558. return false;
  3559. }
  3560. isLookaheadContextual(name) {
  3561. const next = this.nextTokenStart();
  3562. return this.isUnparsedContextual(next, name);
  3563. }
  3564. eatContextual(token) {
  3565. if (this.isContextual(token)) {
  3566. this.next();
  3567. return true;
  3568. }
  3569. return false;
  3570. }
  3571. expectContextual(token, toParseError) {
  3572. if (!this.eatContextual(token)) {
  3573. if (toParseError != null) {
  3574. throw this.raise(toParseError, {
  3575. at: this.state.startLoc
  3576. });
  3577. }
  3578. throw this.unexpected(null, token);
  3579. }
  3580. }
  3581. canInsertSemicolon() {
  3582. return this.match(137) || this.match(8) || this.hasPrecedingLineBreak();
  3583. }
  3584. hasPrecedingLineBreak() {
  3585. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3586. }
  3587. hasFollowingLineBreak() {
  3588. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3589. return skipWhiteSpaceToLineBreak.test(this.input);
  3590. }
  3591. isLineTerminator() {
  3592. return this.eat(13) || this.canInsertSemicolon();
  3593. }
  3594. semicolon(allowAsi = true) {
  3595. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3596. this.raise(Errors.MissingSemicolon, {
  3597. at: this.state.lastTokEndLoc
  3598. });
  3599. }
  3600. expect(type, loc) {
  3601. this.eat(type) || this.unexpected(loc, type);
  3602. }
  3603. tryParse(fn, oldState = this.state.clone()) {
  3604. const abortSignal = {
  3605. node: null
  3606. };
  3607. try {
  3608. const node = fn((node = null) => {
  3609. abortSignal.node = node;
  3610. throw abortSignal;
  3611. });
  3612. if (this.state.errors.length > oldState.errors.length) {
  3613. const failState = this.state;
  3614. this.state = oldState;
  3615. this.state.tokensLength = failState.tokensLength;
  3616. return {
  3617. node,
  3618. error: failState.errors[oldState.errors.length],
  3619. thrown: false,
  3620. aborted: false,
  3621. failState
  3622. };
  3623. }
  3624. return {
  3625. node,
  3626. error: null,
  3627. thrown: false,
  3628. aborted: false,
  3629. failState: null
  3630. };
  3631. } catch (error) {
  3632. const failState = this.state;
  3633. this.state = oldState;
  3634. if (error instanceof SyntaxError) {
  3635. return {
  3636. node: null,
  3637. error,
  3638. thrown: true,
  3639. aborted: false,
  3640. failState
  3641. };
  3642. }
  3643. if (error === abortSignal) {
  3644. return {
  3645. node: abortSignal.node,
  3646. error: null,
  3647. thrown: false,
  3648. aborted: true,
  3649. failState
  3650. };
  3651. }
  3652. throw error;
  3653. }
  3654. }
  3655. checkExpressionErrors(refExpressionErrors, andThrow) {
  3656. if (!refExpressionErrors) return false;
  3657. const {
  3658. shorthandAssignLoc,
  3659. doubleProtoLoc,
  3660. privateKeyLoc,
  3661. optionalParametersLoc
  3662. } = refExpressionErrors;
  3663. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3664. if (!andThrow) {
  3665. return hasErrors;
  3666. }
  3667. if (shorthandAssignLoc != null) {
  3668. this.raise(Errors.InvalidCoverInitializedName, {
  3669. at: shorthandAssignLoc
  3670. });
  3671. }
  3672. if (doubleProtoLoc != null) {
  3673. this.raise(Errors.DuplicateProto, {
  3674. at: doubleProtoLoc
  3675. });
  3676. }
  3677. if (privateKeyLoc != null) {
  3678. this.raise(Errors.UnexpectedPrivateField, {
  3679. at: privateKeyLoc
  3680. });
  3681. }
  3682. if (optionalParametersLoc != null) {
  3683. this.unexpected(optionalParametersLoc);
  3684. }
  3685. }
  3686. isLiteralPropertyName() {
  3687. return tokenIsLiteralPropertyName(this.state.type);
  3688. }
  3689. isPrivateName(node) {
  3690. return node.type === "PrivateName";
  3691. }
  3692. getPrivateNameSV(node) {
  3693. return node.id.name;
  3694. }
  3695. hasPropertyAsPrivateName(node) {
  3696. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3697. }
  3698. isOptionalChain(node) {
  3699. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  3700. }
  3701. isObjectProperty(node) {
  3702. return node.type === "ObjectProperty";
  3703. }
  3704. isObjectMethod(node) {
  3705. return node.type === "ObjectMethod";
  3706. }
  3707. initializeScopes(inModule = this.options.sourceType === "module") {
  3708. const oldLabels = this.state.labels;
  3709. this.state.labels = [];
  3710. const oldExportedIdentifiers = this.exportedIdentifiers;
  3711. this.exportedIdentifiers = new Set();
  3712. const oldInModule = this.inModule;
  3713. this.inModule = inModule;
  3714. const oldScope = this.scope;
  3715. const ScopeHandler = this.getScopeHandler();
  3716. this.scope = new ScopeHandler(this, inModule);
  3717. const oldProdParam = this.prodParam;
  3718. this.prodParam = new ProductionParameterHandler();
  3719. const oldClassScope = this.classScope;
  3720. this.classScope = new ClassScopeHandler(this);
  3721. const oldExpressionScope = this.expressionScope;
  3722. this.expressionScope = new ExpressionScopeHandler(this);
  3723. return () => {
  3724. this.state.labels = oldLabels;
  3725. this.exportedIdentifiers = oldExportedIdentifiers;
  3726. this.inModule = oldInModule;
  3727. this.scope = oldScope;
  3728. this.prodParam = oldProdParam;
  3729. this.classScope = oldClassScope;
  3730. this.expressionScope = oldExpressionScope;
  3731. };
  3732. }
  3733. enterInitialScopes() {
  3734. let paramFlags = PARAM;
  3735. if (this.inModule) {
  3736. paramFlags |= PARAM_AWAIT;
  3737. }
  3738. this.scope.enter(SCOPE_PROGRAM);
  3739. this.prodParam.enter(paramFlags);
  3740. }
  3741. checkDestructuringPrivate(refExpressionErrors) {
  3742. const {
  3743. privateKeyLoc
  3744. } = refExpressionErrors;
  3745. if (privateKeyLoc !== null) {
  3746. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3747. }
  3748. }
  3749. }
  3750. class ExpressionErrors {
  3751. constructor() {
  3752. this.shorthandAssignLoc = null;
  3753. this.doubleProtoLoc = null;
  3754. this.privateKeyLoc = null;
  3755. this.optionalParametersLoc = null;
  3756. }
  3757. }
  3758. class Node {
  3759. constructor(parser, pos, loc) {
  3760. this.type = "";
  3761. this.start = pos;
  3762. this.end = 0;
  3763. this.loc = new SourceLocation(loc);
  3764. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3765. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3766. }
  3767. }
  3768. const NodePrototype = Node.prototype;
  3769. {
  3770. NodePrototype.__clone = function () {
  3771. const newNode = new Node(undefined, this.start, this.loc.start);
  3772. const keys = Object.keys(this);
  3773. for (let i = 0, length = keys.length; i < length; i++) {
  3774. const key = keys[i];
  3775. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3776. newNode[key] = this[key];
  3777. }
  3778. }
  3779. return newNode;
  3780. };
  3781. }
  3782. function clonePlaceholder(node) {
  3783. return cloneIdentifier(node);
  3784. }
  3785. function cloneIdentifier(node) {
  3786. const {
  3787. type,
  3788. start,
  3789. end,
  3790. loc,
  3791. range,
  3792. extra,
  3793. name
  3794. } = node;
  3795. const cloned = Object.create(NodePrototype);
  3796. cloned.type = type;
  3797. cloned.start = start;
  3798. cloned.end = end;
  3799. cloned.loc = loc;
  3800. cloned.range = range;
  3801. cloned.extra = extra;
  3802. cloned.name = name;
  3803. if (type === "Placeholder") {
  3804. cloned.expectedNode = node.expectedNode;
  3805. }
  3806. return cloned;
  3807. }
  3808. function cloneStringLiteral(node) {
  3809. const {
  3810. type,
  3811. start,
  3812. end,
  3813. loc,
  3814. range,
  3815. extra
  3816. } = node;
  3817. if (type === "Placeholder") {
  3818. return clonePlaceholder(node);
  3819. }
  3820. const cloned = Object.create(NodePrototype);
  3821. cloned.type = type;
  3822. cloned.start = start;
  3823. cloned.end = end;
  3824. cloned.loc = loc;
  3825. cloned.range = range;
  3826. if (node.raw !== undefined) {
  3827. cloned.raw = node.raw;
  3828. } else {
  3829. cloned.extra = extra;
  3830. }
  3831. cloned.value = node.value;
  3832. return cloned;
  3833. }
  3834. class NodeUtils extends UtilParser {
  3835. startNode() {
  3836. return new Node(this, this.state.start, this.state.startLoc);
  3837. }
  3838. startNodeAt(loc) {
  3839. return new Node(this, loc.index, loc);
  3840. }
  3841. startNodeAtNode(type) {
  3842. return this.startNodeAt(type.loc.start);
  3843. }
  3844. finishNode(node, type) {
  3845. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3846. }
  3847. finishNodeAt(node, type, endLoc) {
  3848. node.type = type;
  3849. node.end = endLoc.index;
  3850. node.loc.end = endLoc;
  3851. if (this.options.ranges) node.range[1] = endLoc.index;
  3852. if (this.options.attachComment) this.processComment(node);
  3853. return node;
  3854. }
  3855. resetStartLocation(node, startLoc) {
  3856. node.start = startLoc.index;
  3857. node.loc.start = startLoc;
  3858. if (this.options.ranges) node.range[0] = startLoc.index;
  3859. }
  3860. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3861. node.end = endLoc.index;
  3862. node.loc.end = endLoc;
  3863. if (this.options.ranges) node.range[1] = endLoc.index;
  3864. }
  3865. resetStartLocationFromNode(node, locationNode) {
  3866. this.resetStartLocation(node, locationNode.loc.start);
  3867. }
  3868. }
  3869. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3870. const FlowErrors = ParseErrorEnum`flow`({
  3871. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3872. 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.",
  3873. AssignReservedType: ({
  3874. reservedType
  3875. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3876. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3877. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3878. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3879. EnumBooleanMemberNotInitialized: ({
  3880. memberName,
  3881. enumName
  3882. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3883. EnumDuplicateMemberName: ({
  3884. memberName,
  3885. enumName
  3886. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3887. EnumInconsistentMemberValues: ({
  3888. enumName
  3889. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3890. EnumInvalidExplicitType: ({
  3891. invalidEnumType,
  3892. enumName
  3893. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3894. EnumInvalidExplicitTypeUnknownSupplied: ({
  3895. enumName
  3896. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3897. EnumInvalidMemberInitializerPrimaryType: ({
  3898. enumName,
  3899. memberName,
  3900. explicitType
  3901. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3902. EnumInvalidMemberInitializerSymbolType: ({
  3903. enumName,
  3904. memberName
  3905. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3906. EnumInvalidMemberInitializerUnknownType: ({
  3907. enumName,
  3908. memberName
  3909. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3910. EnumInvalidMemberName: ({
  3911. enumName,
  3912. memberName,
  3913. suggestion
  3914. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3915. EnumNumberMemberNotInitialized: ({
  3916. enumName,
  3917. memberName
  3918. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3919. EnumStringMemberInconsistentlyInitailized: ({
  3920. enumName
  3921. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3922. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3923. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  3924. 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.",
  3925. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3926. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3927. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3928. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3929. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3930. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3931. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3932. PatternIsOptional: Object.assign({
  3933. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3934. }, {
  3935. reasonCode: "OptionalBindingPattern"
  3936. }),
  3937. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3938. SpreadVariance: "Spread properties cannot have variance.",
  3939. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3940. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3941. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3942. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3943. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3944. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3945. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3946. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3947. UnexpectedReservedType: ({
  3948. reservedType
  3949. }) => `Unexpected reserved type ${reservedType}.`,
  3950. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3951. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3952. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3953. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3954. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3955. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3956. UnsupportedDeclareExportKind: ({
  3957. unsupportedExportKind,
  3958. suggestion
  3959. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3960. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3961. UnterminatedFlowComment: "Unterminated flow-comment."
  3962. });
  3963. function isEsModuleType(bodyElement) {
  3964. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3965. }
  3966. function hasTypeImportKind(node) {
  3967. return node.importKind === "type" || node.importKind === "typeof";
  3968. }
  3969. function isMaybeDefaultImport(type) {
  3970. return tokenIsKeywordOrIdentifier(type) && type !== 97;
  3971. }
  3972. const exportSuggestions = {
  3973. const: "declare export var",
  3974. let: "declare export var",
  3975. type: "export type",
  3976. interface: "export interface"
  3977. };
  3978. function partition(list, test) {
  3979. const list1 = [];
  3980. const list2 = [];
  3981. for (let i = 0; i < list.length; i++) {
  3982. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3983. }
  3984. return [list1, list2];
  3985. }
  3986. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3987. var flow = (superClass => class FlowParserMixin extends superClass {
  3988. constructor(...args) {
  3989. super(...args);
  3990. this.flowPragma = undefined;
  3991. }
  3992. getScopeHandler() {
  3993. return FlowScopeHandler;
  3994. }
  3995. shouldParseTypes() {
  3996. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3997. }
  3998. shouldParseEnums() {
  3999. return !!this.getPluginOption("flow", "enums");
  4000. }
  4001. finishToken(type, val) {
  4002. if (type !== 131 && type !== 13 && type !== 28) {
  4003. if (this.flowPragma === undefined) {
  4004. this.flowPragma = null;
  4005. }
  4006. }
  4007. return super.finishToken(type, val);
  4008. }
  4009. addComment(comment) {
  4010. if (this.flowPragma === undefined) {
  4011. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  4012. if (!matches) ; else if (matches[1] === "flow") {
  4013. this.flowPragma = "flow";
  4014. } else if (matches[1] === "noflow") {
  4015. this.flowPragma = "noflow";
  4016. } else {
  4017. throw new Error("Unexpected flow pragma");
  4018. }
  4019. }
  4020. return super.addComment(comment);
  4021. }
  4022. flowParseTypeInitialiser(tok) {
  4023. const oldInType = this.state.inType;
  4024. this.state.inType = true;
  4025. this.expect(tok || 14);
  4026. const type = this.flowParseType();
  4027. this.state.inType = oldInType;
  4028. return type;
  4029. }
  4030. flowParsePredicate() {
  4031. const node = this.startNode();
  4032. const moduloLoc = this.state.startLoc;
  4033. this.next();
  4034. this.expectContextual(108);
  4035. if (this.state.lastTokStart > moduloLoc.index + 1) {
  4036. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  4037. at: moduloLoc
  4038. });
  4039. }
  4040. if (this.eat(10)) {
  4041. node.value = super.parseExpression();
  4042. this.expect(11);
  4043. return this.finishNode(node, "DeclaredPredicate");
  4044. } else {
  4045. return this.finishNode(node, "InferredPredicate");
  4046. }
  4047. }
  4048. flowParseTypeAndPredicateInitialiser() {
  4049. const oldInType = this.state.inType;
  4050. this.state.inType = true;
  4051. this.expect(14);
  4052. let type = null;
  4053. let predicate = null;
  4054. if (this.match(54)) {
  4055. this.state.inType = oldInType;
  4056. predicate = this.flowParsePredicate();
  4057. } else {
  4058. type = this.flowParseType();
  4059. this.state.inType = oldInType;
  4060. if (this.match(54)) {
  4061. predicate = this.flowParsePredicate();
  4062. }
  4063. }
  4064. return [type, predicate];
  4065. }
  4066. flowParseDeclareClass(node) {
  4067. this.next();
  4068. this.flowParseInterfaceish(node, true);
  4069. return this.finishNode(node, "DeclareClass");
  4070. }
  4071. flowParseDeclareFunction(node) {
  4072. this.next();
  4073. const id = node.id = this.parseIdentifier();
  4074. const typeNode = this.startNode();
  4075. const typeContainer = this.startNode();
  4076. if (this.match(47)) {
  4077. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4078. } else {
  4079. typeNode.typeParameters = null;
  4080. }
  4081. this.expect(10);
  4082. const tmp = this.flowParseFunctionTypeParams();
  4083. typeNode.params = tmp.params;
  4084. typeNode.rest = tmp.rest;
  4085. typeNode.this = tmp._this;
  4086. this.expect(11);
  4087. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4088. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4089. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4090. this.resetEndLocation(id);
  4091. this.semicolon();
  4092. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  4093. return this.finishNode(node, "DeclareFunction");
  4094. }
  4095. flowParseDeclare(node, insideModule) {
  4096. if (this.match(80)) {
  4097. return this.flowParseDeclareClass(node);
  4098. } else if (this.match(68)) {
  4099. return this.flowParseDeclareFunction(node);
  4100. } else if (this.match(74)) {
  4101. return this.flowParseDeclareVariable(node);
  4102. } else if (this.eatContextual(125)) {
  4103. if (this.match(16)) {
  4104. return this.flowParseDeclareModuleExports(node);
  4105. } else {
  4106. if (insideModule) {
  4107. this.raise(FlowErrors.NestedDeclareModule, {
  4108. at: this.state.lastTokStartLoc
  4109. });
  4110. }
  4111. return this.flowParseDeclareModule(node);
  4112. }
  4113. } else if (this.isContextual(128)) {
  4114. return this.flowParseDeclareTypeAlias(node);
  4115. } else if (this.isContextual(129)) {
  4116. return this.flowParseDeclareOpaqueType(node);
  4117. } else if (this.isContextual(127)) {
  4118. return this.flowParseDeclareInterface(node);
  4119. } else if (this.match(82)) {
  4120. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4121. } else {
  4122. throw this.unexpected();
  4123. }
  4124. }
  4125. flowParseDeclareVariable(node) {
  4126. this.next();
  4127. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4128. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  4129. this.semicolon();
  4130. return this.finishNode(node, "DeclareVariable");
  4131. }
  4132. flowParseDeclareModule(node) {
  4133. this.scope.enter(SCOPE_OTHER);
  4134. if (this.match(131)) {
  4135. node.id = super.parseExprAtom();
  4136. } else {
  4137. node.id = this.parseIdentifier();
  4138. }
  4139. const bodyNode = node.body = this.startNode();
  4140. const body = bodyNode.body = [];
  4141. this.expect(5);
  4142. while (!this.match(8)) {
  4143. let bodyNode = this.startNode();
  4144. if (this.match(83)) {
  4145. this.next();
  4146. if (!this.isContextual(128) && !this.match(87)) {
  4147. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  4148. at: this.state.lastTokStartLoc
  4149. });
  4150. }
  4151. super.parseImport(bodyNode);
  4152. } else {
  4153. this.expectContextual(123, FlowErrors.UnsupportedStatementInDeclareModule);
  4154. bodyNode = this.flowParseDeclare(bodyNode, true);
  4155. }
  4156. body.push(bodyNode);
  4157. }
  4158. this.scope.exit();
  4159. this.expect(8);
  4160. this.finishNode(bodyNode, "BlockStatement");
  4161. let kind = null;
  4162. let hasModuleExport = false;
  4163. body.forEach(bodyElement => {
  4164. if (isEsModuleType(bodyElement)) {
  4165. if (kind === "CommonJS") {
  4166. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4167. at: bodyElement
  4168. });
  4169. }
  4170. kind = "ES";
  4171. } else if (bodyElement.type === "DeclareModuleExports") {
  4172. if (hasModuleExport) {
  4173. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  4174. at: bodyElement
  4175. });
  4176. }
  4177. if (kind === "ES") {
  4178. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4179. at: bodyElement
  4180. });
  4181. }
  4182. kind = "CommonJS";
  4183. hasModuleExport = true;
  4184. }
  4185. });
  4186. node.kind = kind || "CommonJS";
  4187. return this.finishNode(node, "DeclareModule");
  4188. }
  4189. flowParseDeclareExportDeclaration(node, insideModule) {
  4190. this.expect(82);
  4191. if (this.eat(65)) {
  4192. if (this.match(68) || this.match(80)) {
  4193. node.declaration = this.flowParseDeclare(this.startNode());
  4194. } else {
  4195. node.declaration = this.flowParseType();
  4196. this.semicolon();
  4197. }
  4198. node.default = true;
  4199. return this.finishNode(node, "DeclareExportDeclaration");
  4200. } else {
  4201. if (this.match(75) || this.isLet() || (this.isContextual(128) || this.isContextual(127)) && !insideModule) {
  4202. const label = this.state.value;
  4203. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4204. at: this.state.startLoc,
  4205. unsupportedExportKind: label,
  4206. suggestion: exportSuggestions[label]
  4207. });
  4208. }
  4209. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(129)) {
  4210. node.declaration = this.flowParseDeclare(this.startNode());
  4211. node.default = false;
  4212. return this.finishNode(node, "DeclareExportDeclaration");
  4213. } else if (this.match(55) || this.match(5) || this.isContextual(127) || this.isContextual(128) || this.isContextual(129)) {
  4214. node = this.parseExport(node, null);
  4215. if (node.type === "ExportNamedDeclaration") {
  4216. node.type = "ExportDeclaration";
  4217. node.default = false;
  4218. delete node.exportKind;
  4219. }
  4220. node.type = "Declare" + node.type;
  4221. return node;
  4222. }
  4223. }
  4224. throw this.unexpected();
  4225. }
  4226. flowParseDeclareModuleExports(node) {
  4227. this.next();
  4228. this.expectContextual(109);
  4229. node.typeAnnotation = this.flowParseTypeAnnotation();
  4230. this.semicolon();
  4231. return this.finishNode(node, "DeclareModuleExports");
  4232. }
  4233. flowParseDeclareTypeAlias(node) {
  4234. this.next();
  4235. const finished = this.flowParseTypeAlias(node);
  4236. finished.type = "DeclareTypeAlias";
  4237. return finished;
  4238. }
  4239. flowParseDeclareOpaqueType(node) {
  4240. this.next();
  4241. const finished = this.flowParseOpaqueType(node, true);
  4242. finished.type = "DeclareOpaqueType";
  4243. return finished;
  4244. }
  4245. flowParseDeclareInterface(node) {
  4246. this.next();
  4247. this.flowParseInterfaceish(node);
  4248. return this.finishNode(node, "DeclareInterface");
  4249. }
  4250. flowParseInterfaceish(node, isClass = false) {
  4251. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4252. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4253. if (this.match(47)) {
  4254. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4255. } else {
  4256. node.typeParameters = null;
  4257. }
  4258. node.extends = [];
  4259. node.implements = [];
  4260. node.mixins = [];
  4261. if (this.eat(81)) {
  4262. do {
  4263. node.extends.push(this.flowParseInterfaceExtends());
  4264. } while (!isClass && this.eat(12));
  4265. }
  4266. if (this.isContextual(115)) {
  4267. this.next();
  4268. do {
  4269. node.mixins.push(this.flowParseInterfaceExtends());
  4270. } while (this.eat(12));
  4271. }
  4272. if (this.isContextual(111)) {
  4273. this.next();
  4274. do {
  4275. node.implements.push(this.flowParseInterfaceExtends());
  4276. } while (this.eat(12));
  4277. }
  4278. node.body = this.flowParseObjectType({
  4279. allowStatic: isClass,
  4280. allowExact: false,
  4281. allowSpread: false,
  4282. allowProto: isClass,
  4283. allowInexact: false
  4284. });
  4285. }
  4286. flowParseInterfaceExtends() {
  4287. const node = this.startNode();
  4288. node.id = this.flowParseQualifiedTypeIdentifier();
  4289. if (this.match(47)) {
  4290. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4291. } else {
  4292. node.typeParameters = null;
  4293. }
  4294. return this.finishNode(node, "InterfaceExtends");
  4295. }
  4296. flowParseInterface(node) {
  4297. this.flowParseInterfaceish(node);
  4298. return this.finishNode(node, "InterfaceDeclaration");
  4299. }
  4300. checkNotUnderscore(word) {
  4301. if (word === "_") {
  4302. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4303. at: this.state.startLoc
  4304. });
  4305. }
  4306. }
  4307. checkReservedType(word, startLoc, declaration) {
  4308. if (!reservedTypes.has(word)) return;
  4309. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4310. at: startLoc,
  4311. reservedType: word
  4312. });
  4313. }
  4314. flowParseRestrictedIdentifier(liberal, declaration) {
  4315. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4316. return this.parseIdentifier(liberal);
  4317. }
  4318. flowParseTypeAlias(node) {
  4319. node.id = this.flowParseRestrictedIdentifier(false, true);
  4320. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4321. if (this.match(47)) {
  4322. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4323. } else {
  4324. node.typeParameters = null;
  4325. }
  4326. node.right = this.flowParseTypeInitialiser(29);
  4327. this.semicolon();
  4328. return this.finishNode(node, "TypeAlias");
  4329. }
  4330. flowParseOpaqueType(node, declare) {
  4331. this.expectContextual(128);
  4332. node.id = this.flowParseRestrictedIdentifier(true, true);
  4333. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4334. if (this.match(47)) {
  4335. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4336. } else {
  4337. node.typeParameters = null;
  4338. }
  4339. node.supertype = null;
  4340. if (this.match(14)) {
  4341. node.supertype = this.flowParseTypeInitialiser(14);
  4342. }
  4343. node.impltype = null;
  4344. if (!declare) {
  4345. node.impltype = this.flowParseTypeInitialiser(29);
  4346. }
  4347. this.semicolon();
  4348. return this.finishNode(node, "OpaqueType");
  4349. }
  4350. flowParseTypeParameter(requireDefault = false) {
  4351. const nodeStartLoc = this.state.startLoc;
  4352. const node = this.startNode();
  4353. const variance = this.flowParseVariance();
  4354. const ident = this.flowParseTypeAnnotatableIdentifier();
  4355. node.name = ident.name;
  4356. node.variance = variance;
  4357. node.bound = ident.typeAnnotation;
  4358. if (this.match(29)) {
  4359. this.eat(29);
  4360. node.default = this.flowParseType();
  4361. } else {
  4362. if (requireDefault) {
  4363. this.raise(FlowErrors.MissingTypeParamDefault, {
  4364. at: nodeStartLoc
  4365. });
  4366. }
  4367. }
  4368. return this.finishNode(node, "TypeParameter");
  4369. }
  4370. flowParseTypeParameterDeclaration() {
  4371. const oldInType = this.state.inType;
  4372. const node = this.startNode();
  4373. node.params = [];
  4374. this.state.inType = true;
  4375. if (this.match(47) || this.match(140)) {
  4376. this.next();
  4377. } else {
  4378. this.unexpected();
  4379. }
  4380. let defaultRequired = false;
  4381. do {
  4382. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4383. node.params.push(typeParameter);
  4384. if (typeParameter.default) {
  4385. defaultRequired = true;
  4386. }
  4387. if (!this.match(48)) {
  4388. this.expect(12);
  4389. }
  4390. } while (!this.match(48));
  4391. this.expect(48);
  4392. this.state.inType = oldInType;
  4393. return this.finishNode(node, "TypeParameterDeclaration");
  4394. }
  4395. flowParseTypeParameterInstantiation() {
  4396. const node = this.startNode();
  4397. const oldInType = this.state.inType;
  4398. node.params = [];
  4399. this.state.inType = true;
  4400. this.expect(47);
  4401. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4402. this.state.noAnonFunctionType = false;
  4403. while (!this.match(48)) {
  4404. node.params.push(this.flowParseType());
  4405. if (!this.match(48)) {
  4406. this.expect(12);
  4407. }
  4408. }
  4409. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4410. this.expect(48);
  4411. this.state.inType = oldInType;
  4412. return this.finishNode(node, "TypeParameterInstantiation");
  4413. }
  4414. flowParseTypeParameterInstantiationCallOrNew() {
  4415. const node = this.startNode();
  4416. const oldInType = this.state.inType;
  4417. node.params = [];
  4418. this.state.inType = true;
  4419. this.expect(47);
  4420. while (!this.match(48)) {
  4421. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4422. if (!this.match(48)) {
  4423. this.expect(12);
  4424. }
  4425. }
  4426. this.expect(48);
  4427. this.state.inType = oldInType;
  4428. return this.finishNode(node, "TypeParameterInstantiation");
  4429. }
  4430. flowParseInterfaceType() {
  4431. const node = this.startNode();
  4432. this.expectContextual(127);
  4433. node.extends = [];
  4434. if (this.eat(81)) {
  4435. do {
  4436. node.extends.push(this.flowParseInterfaceExtends());
  4437. } while (this.eat(12));
  4438. }
  4439. node.body = this.flowParseObjectType({
  4440. allowStatic: false,
  4441. allowExact: false,
  4442. allowSpread: false,
  4443. allowProto: false,
  4444. allowInexact: false
  4445. });
  4446. return this.finishNode(node, "InterfaceTypeAnnotation");
  4447. }
  4448. flowParseObjectPropertyKey() {
  4449. return this.match(132) || this.match(131) ? super.parseExprAtom() : this.parseIdentifier(true);
  4450. }
  4451. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4452. node.static = isStatic;
  4453. if (this.lookahead().type === 14) {
  4454. node.id = this.flowParseObjectPropertyKey();
  4455. node.key = this.flowParseTypeInitialiser();
  4456. } else {
  4457. node.id = null;
  4458. node.key = this.flowParseType();
  4459. }
  4460. this.expect(3);
  4461. node.value = this.flowParseTypeInitialiser();
  4462. node.variance = variance;
  4463. return this.finishNode(node, "ObjectTypeIndexer");
  4464. }
  4465. flowParseObjectTypeInternalSlot(node, isStatic) {
  4466. node.static = isStatic;
  4467. node.id = this.flowParseObjectPropertyKey();
  4468. this.expect(3);
  4469. this.expect(3);
  4470. if (this.match(47) || this.match(10)) {
  4471. node.method = true;
  4472. node.optional = false;
  4473. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4474. } else {
  4475. node.method = false;
  4476. if (this.eat(17)) {
  4477. node.optional = true;
  4478. }
  4479. node.value = this.flowParseTypeInitialiser();
  4480. }
  4481. return this.finishNode(node, "ObjectTypeInternalSlot");
  4482. }
  4483. flowParseObjectTypeMethodish(node) {
  4484. node.params = [];
  4485. node.rest = null;
  4486. node.typeParameters = null;
  4487. node.this = null;
  4488. if (this.match(47)) {
  4489. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4490. }
  4491. this.expect(10);
  4492. if (this.match(78)) {
  4493. node.this = this.flowParseFunctionTypeParam(true);
  4494. node.this.name = null;
  4495. if (!this.match(11)) {
  4496. this.expect(12);
  4497. }
  4498. }
  4499. while (!this.match(11) && !this.match(21)) {
  4500. node.params.push(this.flowParseFunctionTypeParam(false));
  4501. if (!this.match(11)) {
  4502. this.expect(12);
  4503. }
  4504. }
  4505. if (this.eat(21)) {
  4506. node.rest = this.flowParseFunctionTypeParam(false);
  4507. }
  4508. this.expect(11);
  4509. node.returnType = this.flowParseTypeInitialiser();
  4510. return this.finishNode(node, "FunctionTypeAnnotation");
  4511. }
  4512. flowParseObjectTypeCallProperty(node, isStatic) {
  4513. const valueNode = this.startNode();
  4514. node.static = isStatic;
  4515. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4516. return this.finishNode(node, "ObjectTypeCallProperty");
  4517. }
  4518. flowParseObjectType({
  4519. allowStatic,
  4520. allowExact,
  4521. allowSpread,
  4522. allowProto,
  4523. allowInexact
  4524. }) {
  4525. const oldInType = this.state.inType;
  4526. this.state.inType = true;
  4527. const nodeStart = this.startNode();
  4528. nodeStart.callProperties = [];
  4529. nodeStart.properties = [];
  4530. nodeStart.indexers = [];
  4531. nodeStart.internalSlots = [];
  4532. let endDelim;
  4533. let exact;
  4534. let inexact = false;
  4535. if (allowExact && this.match(6)) {
  4536. this.expect(6);
  4537. endDelim = 9;
  4538. exact = true;
  4539. } else {
  4540. this.expect(5);
  4541. endDelim = 8;
  4542. exact = false;
  4543. }
  4544. nodeStart.exact = exact;
  4545. while (!this.match(endDelim)) {
  4546. let isStatic = false;
  4547. let protoStartLoc = null;
  4548. let inexactStartLoc = null;
  4549. const node = this.startNode();
  4550. if (allowProto && this.isContextual(116)) {
  4551. const lookahead = this.lookahead();
  4552. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4553. this.next();
  4554. protoStartLoc = this.state.startLoc;
  4555. allowStatic = false;
  4556. }
  4557. }
  4558. if (allowStatic && this.isContextual(104)) {
  4559. const lookahead = this.lookahead();
  4560. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4561. this.next();
  4562. isStatic = true;
  4563. }
  4564. }
  4565. const variance = this.flowParseVariance();
  4566. if (this.eat(0)) {
  4567. if (protoStartLoc != null) {
  4568. this.unexpected(protoStartLoc);
  4569. }
  4570. if (this.eat(0)) {
  4571. if (variance) {
  4572. this.unexpected(variance.loc.start);
  4573. }
  4574. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4575. } else {
  4576. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4577. }
  4578. } else if (this.match(10) || this.match(47)) {
  4579. if (protoStartLoc != null) {
  4580. this.unexpected(protoStartLoc);
  4581. }
  4582. if (variance) {
  4583. this.unexpected(variance.loc.start);
  4584. }
  4585. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4586. } else {
  4587. let kind = "init";
  4588. if (this.isContextual(98) || this.isContextual(103)) {
  4589. const lookahead = this.lookahead();
  4590. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4591. kind = this.state.value;
  4592. this.next();
  4593. }
  4594. }
  4595. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4596. if (propOrInexact === null) {
  4597. inexact = true;
  4598. inexactStartLoc = this.state.lastTokStartLoc;
  4599. } else {
  4600. nodeStart.properties.push(propOrInexact);
  4601. }
  4602. }
  4603. this.flowObjectTypeSemicolon();
  4604. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4605. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4606. at: inexactStartLoc
  4607. });
  4608. }
  4609. }
  4610. this.expect(endDelim);
  4611. if (allowSpread) {
  4612. nodeStart.inexact = inexact;
  4613. }
  4614. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4615. this.state.inType = oldInType;
  4616. return out;
  4617. }
  4618. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4619. if (this.eat(21)) {
  4620. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4621. if (isInexactToken) {
  4622. if (!allowSpread) {
  4623. this.raise(FlowErrors.InexactInsideNonObject, {
  4624. at: this.state.lastTokStartLoc
  4625. });
  4626. } else if (!allowInexact) {
  4627. this.raise(FlowErrors.InexactInsideExact, {
  4628. at: this.state.lastTokStartLoc
  4629. });
  4630. }
  4631. if (variance) {
  4632. this.raise(FlowErrors.InexactVariance, {
  4633. at: variance
  4634. });
  4635. }
  4636. return null;
  4637. }
  4638. if (!allowSpread) {
  4639. this.raise(FlowErrors.UnexpectedSpreadType, {
  4640. at: this.state.lastTokStartLoc
  4641. });
  4642. }
  4643. if (protoStartLoc != null) {
  4644. this.unexpected(protoStartLoc);
  4645. }
  4646. if (variance) {
  4647. this.raise(FlowErrors.SpreadVariance, {
  4648. at: variance
  4649. });
  4650. }
  4651. node.argument = this.flowParseType();
  4652. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4653. } else {
  4654. node.key = this.flowParseObjectPropertyKey();
  4655. node.static = isStatic;
  4656. node.proto = protoStartLoc != null;
  4657. node.kind = kind;
  4658. let optional = false;
  4659. if (this.match(47) || this.match(10)) {
  4660. node.method = true;
  4661. if (protoStartLoc != null) {
  4662. this.unexpected(protoStartLoc);
  4663. }
  4664. if (variance) {
  4665. this.unexpected(variance.loc.start);
  4666. }
  4667. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  4668. if (kind === "get" || kind === "set") {
  4669. this.flowCheckGetterSetterParams(node);
  4670. }
  4671. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4672. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4673. at: node.value.this
  4674. });
  4675. }
  4676. } else {
  4677. if (kind !== "init") this.unexpected();
  4678. node.method = false;
  4679. if (this.eat(17)) {
  4680. optional = true;
  4681. }
  4682. node.value = this.flowParseTypeInitialiser();
  4683. node.variance = variance;
  4684. }
  4685. node.optional = optional;
  4686. return this.finishNode(node, "ObjectTypeProperty");
  4687. }
  4688. }
  4689. flowCheckGetterSetterParams(property) {
  4690. const paramCount = property.kind === "get" ? 0 : 1;
  4691. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4692. if (property.value.this) {
  4693. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4694. at: property.value.this
  4695. });
  4696. }
  4697. if (length !== paramCount) {
  4698. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4699. at: property
  4700. });
  4701. }
  4702. if (property.kind === "set" && property.value.rest) {
  4703. this.raise(Errors.BadSetterRestParameter, {
  4704. at: property
  4705. });
  4706. }
  4707. }
  4708. flowObjectTypeSemicolon() {
  4709. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4710. this.unexpected();
  4711. }
  4712. }
  4713. flowParseQualifiedTypeIdentifier(startLoc, id) {
  4714. var _startLoc;
  4715. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  4716. let node = id || this.flowParseRestrictedIdentifier(true);
  4717. while (this.eat(16)) {
  4718. const node2 = this.startNodeAt(startLoc);
  4719. node2.qualification = node;
  4720. node2.id = this.flowParseRestrictedIdentifier(true);
  4721. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4722. }
  4723. return node;
  4724. }
  4725. flowParseGenericType(startLoc, id) {
  4726. const node = this.startNodeAt(startLoc);
  4727. node.typeParameters = null;
  4728. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  4729. if (this.match(47)) {
  4730. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4731. }
  4732. return this.finishNode(node, "GenericTypeAnnotation");
  4733. }
  4734. flowParseTypeofType() {
  4735. const node = this.startNode();
  4736. this.expect(87);
  4737. node.argument = this.flowParsePrimaryType();
  4738. return this.finishNode(node, "TypeofTypeAnnotation");
  4739. }
  4740. flowParseTupleType() {
  4741. const node = this.startNode();
  4742. node.types = [];
  4743. this.expect(0);
  4744. while (this.state.pos < this.length && !this.match(3)) {
  4745. node.types.push(this.flowParseType());
  4746. if (this.match(3)) break;
  4747. this.expect(12);
  4748. }
  4749. this.expect(3);
  4750. return this.finishNode(node, "TupleTypeAnnotation");
  4751. }
  4752. flowParseFunctionTypeParam(first) {
  4753. let name = null;
  4754. let optional = false;
  4755. let typeAnnotation = null;
  4756. const node = this.startNode();
  4757. const lh = this.lookahead();
  4758. const isThis = this.state.type === 78;
  4759. if (lh.type === 14 || lh.type === 17) {
  4760. if (isThis && !first) {
  4761. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4762. at: node
  4763. });
  4764. }
  4765. name = this.parseIdentifier(isThis);
  4766. if (this.eat(17)) {
  4767. optional = true;
  4768. if (isThis) {
  4769. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4770. at: node
  4771. });
  4772. }
  4773. }
  4774. typeAnnotation = this.flowParseTypeInitialiser();
  4775. } else {
  4776. typeAnnotation = this.flowParseType();
  4777. }
  4778. node.name = name;
  4779. node.optional = optional;
  4780. node.typeAnnotation = typeAnnotation;
  4781. return this.finishNode(node, "FunctionTypeParam");
  4782. }
  4783. reinterpretTypeAsFunctionTypeParam(type) {
  4784. const node = this.startNodeAt(type.loc.start);
  4785. node.name = null;
  4786. node.optional = false;
  4787. node.typeAnnotation = type;
  4788. return this.finishNode(node, "FunctionTypeParam");
  4789. }
  4790. flowParseFunctionTypeParams(params = []) {
  4791. let rest = null;
  4792. let _this = null;
  4793. if (this.match(78)) {
  4794. _this = this.flowParseFunctionTypeParam(true);
  4795. _this.name = null;
  4796. if (!this.match(11)) {
  4797. this.expect(12);
  4798. }
  4799. }
  4800. while (!this.match(11) && !this.match(21)) {
  4801. params.push(this.flowParseFunctionTypeParam(false));
  4802. if (!this.match(11)) {
  4803. this.expect(12);
  4804. }
  4805. }
  4806. if (this.eat(21)) {
  4807. rest = this.flowParseFunctionTypeParam(false);
  4808. }
  4809. return {
  4810. params,
  4811. rest,
  4812. _this
  4813. };
  4814. }
  4815. flowIdentToTypeAnnotation(startLoc, node, id) {
  4816. switch (id.name) {
  4817. case "any":
  4818. return this.finishNode(node, "AnyTypeAnnotation");
  4819. case "bool":
  4820. case "boolean":
  4821. return this.finishNode(node, "BooleanTypeAnnotation");
  4822. case "mixed":
  4823. return this.finishNode(node, "MixedTypeAnnotation");
  4824. case "empty":
  4825. return this.finishNode(node, "EmptyTypeAnnotation");
  4826. case "number":
  4827. return this.finishNode(node, "NumberTypeAnnotation");
  4828. case "string":
  4829. return this.finishNode(node, "StringTypeAnnotation");
  4830. case "symbol":
  4831. return this.finishNode(node, "SymbolTypeAnnotation");
  4832. default:
  4833. this.checkNotUnderscore(id.name);
  4834. return this.flowParseGenericType(startLoc, id);
  4835. }
  4836. }
  4837. flowParsePrimaryType() {
  4838. const startLoc = this.state.startLoc;
  4839. const node = this.startNode();
  4840. let tmp;
  4841. let type;
  4842. let isGroupedType = false;
  4843. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4844. switch (this.state.type) {
  4845. case 5:
  4846. return this.flowParseObjectType({
  4847. allowStatic: false,
  4848. allowExact: false,
  4849. allowSpread: true,
  4850. allowProto: false,
  4851. allowInexact: true
  4852. });
  4853. case 6:
  4854. return this.flowParseObjectType({
  4855. allowStatic: false,
  4856. allowExact: true,
  4857. allowSpread: true,
  4858. allowProto: false,
  4859. allowInexact: false
  4860. });
  4861. case 0:
  4862. this.state.noAnonFunctionType = false;
  4863. type = this.flowParseTupleType();
  4864. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4865. return type;
  4866. case 47:
  4867. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4868. this.expect(10);
  4869. tmp = this.flowParseFunctionTypeParams();
  4870. node.params = tmp.params;
  4871. node.rest = tmp.rest;
  4872. node.this = tmp._this;
  4873. this.expect(11);
  4874. this.expect(19);
  4875. node.returnType = this.flowParseType();
  4876. return this.finishNode(node, "FunctionTypeAnnotation");
  4877. case 10:
  4878. this.next();
  4879. if (!this.match(11) && !this.match(21)) {
  4880. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4881. const token = this.lookahead().type;
  4882. isGroupedType = token !== 17 && token !== 14;
  4883. } else {
  4884. isGroupedType = true;
  4885. }
  4886. }
  4887. if (isGroupedType) {
  4888. this.state.noAnonFunctionType = false;
  4889. type = this.flowParseType();
  4890. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4891. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4892. this.expect(11);
  4893. return type;
  4894. } else {
  4895. this.eat(12);
  4896. }
  4897. }
  4898. if (type) {
  4899. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4900. } else {
  4901. tmp = this.flowParseFunctionTypeParams();
  4902. }
  4903. node.params = tmp.params;
  4904. node.rest = tmp.rest;
  4905. node.this = tmp._this;
  4906. this.expect(11);
  4907. this.expect(19);
  4908. node.returnType = this.flowParseType();
  4909. node.typeParameters = null;
  4910. return this.finishNode(node, "FunctionTypeAnnotation");
  4911. case 131:
  4912. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4913. case 85:
  4914. case 86:
  4915. node.value = this.match(85);
  4916. this.next();
  4917. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4918. case 53:
  4919. if (this.state.value === "-") {
  4920. this.next();
  4921. if (this.match(132)) {
  4922. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4923. }
  4924. if (this.match(133)) {
  4925. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4926. }
  4927. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4928. at: this.state.startLoc
  4929. });
  4930. }
  4931. throw this.unexpected();
  4932. case 132:
  4933. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4934. case 133:
  4935. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4936. case 88:
  4937. this.next();
  4938. return this.finishNode(node, "VoidTypeAnnotation");
  4939. case 84:
  4940. this.next();
  4941. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4942. case 78:
  4943. this.next();
  4944. return this.finishNode(node, "ThisTypeAnnotation");
  4945. case 55:
  4946. this.next();
  4947. return this.finishNode(node, "ExistsTypeAnnotation");
  4948. case 87:
  4949. return this.flowParseTypeofType();
  4950. default:
  4951. if (tokenIsKeyword(this.state.type)) {
  4952. const label = tokenLabelName(this.state.type);
  4953. this.next();
  4954. return super.createIdentifier(node, label);
  4955. } else if (tokenIsIdentifier(this.state.type)) {
  4956. if (this.isContextual(127)) {
  4957. return this.flowParseInterfaceType();
  4958. }
  4959. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  4960. }
  4961. }
  4962. throw this.unexpected();
  4963. }
  4964. flowParsePostfixType() {
  4965. const startLoc = this.state.startLoc;
  4966. let type = this.flowParsePrimaryType();
  4967. let seenOptionalIndexedAccess = false;
  4968. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4969. const node = this.startNodeAt(startLoc);
  4970. const optional = this.eat(18);
  4971. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4972. this.expect(0);
  4973. if (!optional && this.match(3)) {
  4974. node.elementType = type;
  4975. this.next();
  4976. type = this.finishNode(node, "ArrayTypeAnnotation");
  4977. } else {
  4978. node.objectType = type;
  4979. node.indexType = this.flowParseType();
  4980. this.expect(3);
  4981. if (seenOptionalIndexedAccess) {
  4982. node.optional = optional;
  4983. type = this.finishNode(node, "OptionalIndexedAccessType");
  4984. } else {
  4985. type = this.finishNode(node, "IndexedAccessType");
  4986. }
  4987. }
  4988. }
  4989. return type;
  4990. }
  4991. flowParsePrefixType() {
  4992. const node = this.startNode();
  4993. if (this.eat(17)) {
  4994. node.typeAnnotation = this.flowParsePrefixType();
  4995. return this.finishNode(node, "NullableTypeAnnotation");
  4996. } else {
  4997. return this.flowParsePostfixType();
  4998. }
  4999. }
  5000. flowParseAnonFunctionWithoutParens() {
  5001. const param = this.flowParsePrefixType();
  5002. if (!this.state.noAnonFunctionType && this.eat(19)) {
  5003. const node = this.startNodeAt(param.loc.start);
  5004. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  5005. node.rest = null;
  5006. node.this = null;
  5007. node.returnType = this.flowParseType();
  5008. node.typeParameters = null;
  5009. return this.finishNode(node, "FunctionTypeAnnotation");
  5010. }
  5011. return param;
  5012. }
  5013. flowParseIntersectionType() {
  5014. const node = this.startNode();
  5015. this.eat(45);
  5016. const type = this.flowParseAnonFunctionWithoutParens();
  5017. node.types = [type];
  5018. while (this.eat(45)) {
  5019. node.types.push(this.flowParseAnonFunctionWithoutParens());
  5020. }
  5021. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  5022. }
  5023. flowParseUnionType() {
  5024. const node = this.startNode();
  5025. this.eat(43);
  5026. const type = this.flowParseIntersectionType();
  5027. node.types = [type];
  5028. while (this.eat(43)) {
  5029. node.types.push(this.flowParseIntersectionType());
  5030. }
  5031. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  5032. }
  5033. flowParseType() {
  5034. const oldInType = this.state.inType;
  5035. this.state.inType = true;
  5036. const type = this.flowParseUnionType();
  5037. this.state.inType = oldInType;
  5038. return type;
  5039. }
  5040. flowParseTypeOrImplicitInstantiation() {
  5041. if (this.state.type === 130 && this.state.value === "_") {
  5042. const startLoc = this.state.startLoc;
  5043. const node = this.parseIdentifier();
  5044. return this.flowParseGenericType(startLoc, node);
  5045. } else {
  5046. return this.flowParseType();
  5047. }
  5048. }
  5049. flowParseTypeAnnotation() {
  5050. const node = this.startNode();
  5051. node.typeAnnotation = this.flowParseTypeInitialiser();
  5052. return this.finishNode(node, "TypeAnnotation");
  5053. }
  5054. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  5055. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  5056. if (this.match(14)) {
  5057. ident.typeAnnotation = this.flowParseTypeAnnotation();
  5058. this.resetEndLocation(ident);
  5059. }
  5060. return ident;
  5061. }
  5062. typeCastToParameter(node) {
  5063. node.expression.typeAnnotation = node.typeAnnotation;
  5064. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5065. return node.expression;
  5066. }
  5067. flowParseVariance() {
  5068. let variance = null;
  5069. if (this.match(53)) {
  5070. variance = this.startNode();
  5071. if (this.state.value === "+") {
  5072. variance.kind = "plus";
  5073. } else {
  5074. variance.kind = "minus";
  5075. }
  5076. this.next();
  5077. return this.finishNode(variance, "Variance");
  5078. }
  5079. return variance;
  5080. }
  5081. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5082. if (allowExpressionBody) {
  5083. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5084. }
  5085. return super.parseFunctionBody(node, false, isMethod);
  5086. }
  5087. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5088. if (this.match(14)) {
  5089. const typeNode = this.startNode();
  5090. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5091. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5092. }
  5093. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5094. }
  5095. parseStatementLike(flags) {
  5096. if (this.state.strict && this.isContextual(127)) {
  5097. const lookahead = this.lookahead();
  5098. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5099. const node = this.startNode();
  5100. this.next();
  5101. return this.flowParseInterface(node);
  5102. }
  5103. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5104. const node = this.startNode();
  5105. this.next();
  5106. return this.flowParseEnumDeclaration(node);
  5107. }
  5108. const stmt = super.parseStatementLike(flags);
  5109. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5110. this.flowPragma = null;
  5111. }
  5112. return stmt;
  5113. }
  5114. parseExpressionStatement(node, expr, decorators) {
  5115. if (expr.type === "Identifier") {
  5116. if (expr.name === "declare") {
  5117. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5118. return this.flowParseDeclare(node);
  5119. }
  5120. } else if (tokenIsIdentifier(this.state.type)) {
  5121. if (expr.name === "interface") {
  5122. return this.flowParseInterface(node);
  5123. } else if (expr.name === "type") {
  5124. return this.flowParseTypeAlias(node);
  5125. } else if (expr.name === "opaque") {
  5126. return this.flowParseOpaqueType(node, false);
  5127. }
  5128. }
  5129. }
  5130. return super.parseExpressionStatement(node, expr, decorators);
  5131. }
  5132. shouldParseExportDeclaration() {
  5133. const {
  5134. type
  5135. } = this.state;
  5136. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5137. return !this.state.containsEsc;
  5138. }
  5139. return super.shouldParseExportDeclaration();
  5140. }
  5141. isExportDefaultSpecifier() {
  5142. const {
  5143. type
  5144. } = this.state;
  5145. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 124) {
  5146. return this.state.containsEsc;
  5147. }
  5148. return super.isExportDefaultSpecifier();
  5149. }
  5150. parseExportDefaultExpression() {
  5151. if (this.shouldParseEnums() && this.isContextual(124)) {
  5152. const node = this.startNode();
  5153. this.next();
  5154. return this.flowParseEnumDeclaration(node);
  5155. }
  5156. return super.parseExportDefaultExpression();
  5157. }
  5158. parseConditional(expr, startLoc, refExpressionErrors) {
  5159. if (!this.match(17)) return expr;
  5160. if (this.state.maybeInArrowParameters) {
  5161. const nextCh = this.lookaheadCharCode();
  5162. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5163. this.setOptionalParametersError(refExpressionErrors);
  5164. return expr;
  5165. }
  5166. }
  5167. this.expect(17);
  5168. const state = this.state.clone();
  5169. const originalNoArrowAt = this.state.noArrowAt;
  5170. const node = this.startNodeAt(startLoc);
  5171. let {
  5172. consequent,
  5173. failed
  5174. } = this.tryParseConditionalConsequent();
  5175. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5176. if (failed || invalid.length > 0) {
  5177. const noArrowAt = [...originalNoArrowAt];
  5178. if (invalid.length > 0) {
  5179. this.state = state;
  5180. this.state.noArrowAt = noArrowAt;
  5181. for (let i = 0; i < invalid.length; i++) {
  5182. noArrowAt.push(invalid[i].start);
  5183. }
  5184. ({
  5185. consequent,
  5186. failed
  5187. } = this.tryParseConditionalConsequent());
  5188. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5189. }
  5190. if (failed && valid.length > 1) {
  5191. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5192. at: state.startLoc
  5193. });
  5194. }
  5195. if (failed && valid.length === 1) {
  5196. this.state = state;
  5197. noArrowAt.push(valid[0].start);
  5198. this.state.noArrowAt = noArrowAt;
  5199. ({
  5200. consequent,
  5201. failed
  5202. } = this.tryParseConditionalConsequent());
  5203. }
  5204. }
  5205. this.getArrowLikeExpressions(consequent, true);
  5206. this.state.noArrowAt = originalNoArrowAt;
  5207. this.expect(14);
  5208. node.test = expr;
  5209. node.consequent = consequent;
  5210. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5211. return this.finishNode(node, "ConditionalExpression");
  5212. }
  5213. tryParseConditionalConsequent() {
  5214. this.state.noArrowParamsConversionAt.push(this.state.start);
  5215. const consequent = this.parseMaybeAssignAllowIn();
  5216. const failed = !this.match(14);
  5217. this.state.noArrowParamsConversionAt.pop();
  5218. return {
  5219. consequent,
  5220. failed
  5221. };
  5222. }
  5223. getArrowLikeExpressions(node, disallowInvalid) {
  5224. const stack = [node];
  5225. const arrows = [];
  5226. while (stack.length !== 0) {
  5227. const node = stack.pop();
  5228. if (node.type === "ArrowFunctionExpression") {
  5229. if (node.typeParameters || !node.returnType) {
  5230. this.finishArrowValidation(node);
  5231. } else {
  5232. arrows.push(node);
  5233. }
  5234. stack.push(node.body);
  5235. } else if (node.type === "ConditionalExpression") {
  5236. stack.push(node.consequent);
  5237. stack.push(node.alternate);
  5238. }
  5239. }
  5240. if (disallowInvalid) {
  5241. arrows.forEach(node => this.finishArrowValidation(node));
  5242. return [arrows, []];
  5243. }
  5244. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5245. }
  5246. finishArrowValidation(node) {
  5247. var _node$extra;
  5248. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5249. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5250. super.checkParams(node, false, true);
  5251. this.scope.exit();
  5252. }
  5253. forwardNoArrowParamsConversionAt(node, parse) {
  5254. let result;
  5255. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5256. this.state.noArrowParamsConversionAt.push(this.state.start);
  5257. result = parse();
  5258. this.state.noArrowParamsConversionAt.pop();
  5259. } else {
  5260. result = parse();
  5261. }
  5262. return result;
  5263. }
  5264. parseParenItem(node, startLoc) {
  5265. node = super.parseParenItem(node, startLoc);
  5266. if (this.eat(17)) {
  5267. node.optional = true;
  5268. this.resetEndLocation(node);
  5269. }
  5270. if (this.match(14)) {
  5271. const typeCastNode = this.startNodeAt(startLoc);
  5272. typeCastNode.expression = node;
  5273. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5274. return this.finishNode(typeCastNode, "TypeCastExpression");
  5275. }
  5276. return node;
  5277. }
  5278. assertModuleNodeAllowed(node) {
  5279. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5280. return;
  5281. }
  5282. super.assertModuleNodeAllowed(node);
  5283. }
  5284. parseExport(node, decorators) {
  5285. const decl = super.parseExport(node, decorators);
  5286. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  5287. decl.exportKind = decl.exportKind || "value";
  5288. }
  5289. return decl;
  5290. }
  5291. parseExportDeclaration(node) {
  5292. if (this.isContextual(128)) {
  5293. node.exportKind = "type";
  5294. const declarationNode = this.startNode();
  5295. this.next();
  5296. if (this.match(5)) {
  5297. node.specifiers = this.parseExportSpecifiers(true);
  5298. super.parseExportFrom(node);
  5299. return null;
  5300. } else {
  5301. return this.flowParseTypeAlias(declarationNode);
  5302. }
  5303. } else if (this.isContextual(129)) {
  5304. node.exportKind = "type";
  5305. const declarationNode = this.startNode();
  5306. this.next();
  5307. return this.flowParseOpaqueType(declarationNode, false);
  5308. } else if (this.isContextual(127)) {
  5309. node.exportKind = "type";
  5310. const declarationNode = this.startNode();
  5311. this.next();
  5312. return this.flowParseInterface(declarationNode);
  5313. } else if (this.shouldParseEnums() && this.isContextual(124)) {
  5314. node.exportKind = "value";
  5315. const declarationNode = this.startNode();
  5316. this.next();
  5317. return this.flowParseEnumDeclaration(declarationNode);
  5318. } else {
  5319. return super.parseExportDeclaration(node);
  5320. }
  5321. }
  5322. eatExportStar(node) {
  5323. if (super.eatExportStar(node)) return true;
  5324. if (this.isContextual(128) && this.lookahead().type === 55) {
  5325. node.exportKind = "type";
  5326. this.next();
  5327. this.next();
  5328. return true;
  5329. }
  5330. return false;
  5331. }
  5332. maybeParseExportNamespaceSpecifier(node) {
  5333. const {
  5334. startLoc
  5335. } = this.state;
  5336. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5337. if (hasNamespace && node.exportKind === "type") {
  5338. this.unexpected(startLoc);
  5339. }
  5340. return hasNamespace;
  5341. }
  5342. parseClassId(node, isStatement, optionalId) {
  5343. super.parseClassId(node, isStatement, optionalId);
  5344. if (this.match(47)) {
  5345. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5346. }
  5347. }
  5348. parseClassMember(classBody, member, state) {
  5349. const {
  5350. startLoc
  5351. } = this.state;
  5352. if (this.isContextual(123)) {
  5353. if (super.parseClassMemberFromModifier(classBody, member)) {
  5354. return;
  5355. }
  5356. member.declare = true;
  5357. }
  5358. super.parseClassMember(classBody, member, state);
  5359. if (member.declare) {
  5360. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5361. this.raise(FlowErrors.DeclareClassElement, {
  5362. at: startLoc
  5363. });
  5364. } else if (member.value) {
  5365. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5366. at: member.value
  5367. });
  5368. }
  5369. }
  5370. }
  5371. isIterator(word) {
  5372. return word === "iterator" || word === "asyncIterator";
  5373. }
  5374. readIterator() {
  5375. const word = super.readWord1();
  5376. const fullWord = "@@" + word;
  5377. if (!this.isIterator(word) || !this.state.inType) {
  5378. this.raise(Errors.InvalidIdentifier, {
  5379. at: this.state.curPosition(),
  5380. identifierName: fullWord
  5381. });
  5382. }
  5383. this.finishToken(130, fullWord);
  5384. }
  5385. getTokenFromCode(code) {
  5386. const next = this.input.charCodeAt(this.state.pos + 1);
  5387. if (code === 123 && next === 124) {
  5388. return this.finishOp(6, 2);
  5389. } else if (this.state.inType && (code === 62 || code === 60)) {
  5390. return this.finishOp(code === 62 ? 48 : 47, 1);
  5391. } else if (this.state.inType && code === 63) {
  5392. if (next === 46) {
  5393. return this.finishOp(18, 2);
  5394. }
  5395. return this.finishOp(17, 1);
  5396. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5397. this.state.pos += 2;
  5398. return this.readIterator();
  5399. } else {
  5400. return super.getTokenFromCode(code);
  5401. }
  5402. }
  5403. isAssignable(node, isBinding) {
  5404. if (node.type === "TypeCastExpression") {
  5405. return this.isAssignable(node.expression, isBinding);
  5406. } else {
  5407. return super.isAssignable(node, isBinding);
  5408. }
  5409. }
  5410. toAssignable(node, isLHS = false) {
  5411. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5412. node.left = this.typeCastToParameter(node.left);
  5413. }
  5414. super.toAssignable(node, isLHS);
  5415. }
  5416. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5417. for (let i = 0; i < exprList.length; i++) {
  5418. const expr = exprList[i];
  5419. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5420. exprList[i] = this.typeCastToParameter(expr);
  5421. }
  5422. }
  5423. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5424. }
  5425. toReferencedList(exprList, isParenthesizedExpr) {
  5426. for (let i = 0; i < exprList.length; i++) {
  5427. var _expr$extra;
  5428. const expr = exprList[i];
  5429. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5430. this.raise(FlowErrors.TypeCastInPattern, {
  5431. at: expr.typeAnnotation
  5432. });
  5433. }
  5434. }
  5435. return exprList;
  5436. }
  5437. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5438. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5439. if (canBePattern && !this.state.maybeInArrowParameters) {
  5440. this.toReferencedList(node.elements);
  5441. }
  5442. return node;
  5443. }
  5444. isValidLVal(type, isParenthesized, binding) {
  5445. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5446. }
  5447. parseClassProperty(node) {
  5448. if (this.match(14)) {
  5449. node.typeAnnotation = this.flowParseTypeAnnotation();
  5450. }
  5451. return super.parseClassProperty(node);
  5452. }
  5453. parseClassPrivateProperty(node) {
  5454. if (this.match(14)) {
  5455. node.typeAnnotation = this.flowParseTypeAnnotation();
  5456. }
  5457. return super.parseClassPrivateProperty(node);
  5458. }
  5459. isClassMethod() {
  5460. return this.match(47) || super.isClassMethod();
  5461. }
  5462. isClassProperty() {
  5463. return this.match(14) || super.isClassProperty();
  5464. }
  5465. isNonstaticConstructor(method) {
  5466. return !this.match(14) && super.isNonstaticConstructor(method);
  5467. }
  5468. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5469. if (method.variance) {
  5470. this.unexpected(method.variance.loc.start);
  5471. }
  5472. delete method.variance;
  5473. if (this.match(47)) {
  5474. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5475. }
  5476. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5477. if (method.params && isConstructor) {
  5478. const params = method.params;
  5479. if (params.length > 0 && this.isThisParam(params[0])) {
  5480. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5481. at: method
  5482. });
  5483. }
  5484. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5485. const params = method.value.params;
  5486. if (params.length > 0 && this.isThisParam(params[0])) {
  5487. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5488. at: method
  5489. });
  5490. }
  5491. }
  5492. }
  5493. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5494. if (method.variance) {
  5495. this.unexpected(method.variance.loc.start);
  5496. }
  5497. delete method.variance;
  5498. if (this.match(47)) {
  5499. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5500. }
  5501. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5502. }
  5503. parseClassSuper(node) {
  5504. super.parseClassSuper(node);
  5505. if (node.superClass && this.match(47)) {
  5506. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5507. }
  5508. if (this.isContextual(111)) {
  5509. this.next();
  5510. const implemented = node.implements = [];
  5511. do {
  5512. const node = this.startNode();
  5513. node.id = this.flowParseRestrictedIdentifier(true);
  5514. if (this.match(47)) {
  5515. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5516. } else {
  5517. node.typeParameters = null;
  5518. }
  5519. implemented.push(this.finishNode(node, "ClassImplements"));
  5520. } while (this.eat(12));
  5521. }
  5522. }
  5523. checkGetterSetterParams(method) {
  5524. super.checkGetterSetterParams(method);
  5525. const params = this.getObjectOrClassMethodParams(method);
  5526. if (params.length > 0) {
  5527. const param = params[0];
  5528. if (this.isThisParam(param) && method.kind === "get") {
  5529. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5530. at: param
  5531. });
  5532. } else if (this.isThisParam(param)) {
  5533. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5534. at: param
  5535. });
  5536. }
  5537. }
  5538. }
  5539. parsePropertyNamePrefixOperator(node) {
  5540. node.variance = this.flowParseVariance();
  5541. }
  5542. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5543. if (prop.variance) {
  5544. this.unexpected(prop.variance.loc.start);
  5545. }
  5546. delete prop.variance;
  5547. let typeParameters;
  5548. if (this.match(47) && !isAccessor) {
  5549. typeParameters = this.flowParseTypeParameterDeclaration();
  5550. if (!this.match(10)) this.unexpected();
  5551. }
  5552. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5553. if (typeParameters) {
  5554. (result.value || result).typeParameters = typeParameters;
  5555. }
  5556. return result;
  5557. }
  5558. parseAssignableListItemTypes(param) {
  5559. if (this.eat(17)) {
  5560. if (param.type !== "Identifier") {
  5561. this.raise(FlowErrors.PatternIsOptional, {
  5562. at: param
  5563. });
  5564. }
  5565. if (this.isThisParam(param)) {
  5566. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5567. at: param
  5568. });
  5569. }
  5570. param.optional = true;
  5571. }
  5572. if (this.match(14)) {
  5573. param.typeAnnotation = this.flowParseTypeAnnotation();
  5574. } else if (this.isThisParam(param)) {
  5575. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5576. at: param
  5577. });
  5578. }
  5579. if (this.match(29) && this.isThisParam(param)) {
  5580. this.raise(FlowErrors.ThisParamNoDefault, {
  5581. at: param
  5582. });
  5583. }
  5584. this.resetEndLocation(param);
  5585. return param;
  5586. }
  5587. parseMaybeDefault(startLoc, left) {
  5588. const node = super.parseMaybeDefault(startLoc, left);
  5589. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5590. this.raise(FlowErrors.TypeBeforeInitializer, {
  5591. at: node.typeAnnotation
  5592. });
  5593. }
  5594. return node;
  5595. }
  5596. shouldParseDefaultImport(node) {
  5597. if (!hasTypeImportKind(node)) {
  5598. return super.shouldParseDefaultImport(node);
  5599. }
  5600. return isMaybeDefaultImport(this.state.type);
  5601. }
  5602. checkImportReflection(node) {
  5603. super.checkImportReflection(node);
  5604. if (node.module && node.importKind !== "value") {
  5605. this.raise(FlowErrors.ImportReflectionHasImportType, {
  5606. at: node.specifiers[0].loc.start
  5607. });
  5608. }
  5609. }
  5610. parseImportSpecifierLocal(node, specifier, type) {
  5611. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5612. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5613. }
  5614. maybeParseDefaultImportSpecifier(node) {
  5615. node.importKind = "value";
  5616. let kind = null;
  5617. if (this.match(87)) {
  5618. kind = "typeof";
  5619. } else if (this.isContextual(128)) {
  5620. kind = "type";
  5621. }
  5622. if (kind) {
  5623. const lh = this.lookahead();
  5624. const {
  5625. type
  5626. } = lh;
  5627. if (kind === "type" && type === 55) {
  5628. this.unexpected(null, lh.type);
  5629. }
  5630. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5631. this.next();
  5632. node.importKind = kind;
  5633. }
  5634. }
  5635. return super.maybeParseDefaultImportSpecifier(node);
  5636. }
  5637. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5638. const firstIdent = specifier.imported;
  5639. let specifierTypeKind = null;
  5640. if (firstIdent.type === "Identifier") {
  5641. if (firstIdent.name === "type") {
  5642. specifierTypeKind = "type";
  5643. } else if (firstIdent.name === "typeof") {
  5644. specifierTypeKind = "typeof";
  5645. }
  5646. }
  5647. let isBinding = false;
  5648. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5649. const as_ident = this.parseIdentifier(true);
  5650. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5651. specifier.imported = as_ident;
  5652. specifier.importKind = specifierTypeKind;
  5653. specifier.local = cloneIdentifier(as_ident);
  5654. } else {
  5655. specifier.imported = firstIdent;
  5656. specifier.importKind = null;
  5657. specifier.local = this.parseIdentifier();
  5658. }
  5659. } else {
  5660. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5661. specifier.imported = this.parseIdentifier(true);
  5662. specifier.importKind = specifierTypeKind;
  5663. } else {
  5664. if (importedIsString) {
  5665. throw this.raise(Errors.ImportBindingIsString, {
  5666. at: specifier,
  5667. importName: firstIdent.value
  5668. });
  5669. }
  5670. specifier.imported = firstIdent;
  5671. specifier.importKind = null;
  5672. }
  5673. if (this.eatContextual(93)) {
  5674. specifier.local = this.parseIdentifier();
  5675. } else {
  5676. isBinding = true;
  5677. specifier.local = cloneIdentifier(specifier.imported);
  5678. }
  5679. }
  5680. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5681. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5682. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5683. at: specifier
  5684. });
  5685. }
  5686. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5687. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5688. }
  5689. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5690. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5691. }
  5692. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5693. }
  5694. parseBindingAtom() {
  5695. switch (this.state.type) {
  5696. case 78:
  5697. return this.parseIdentifier(true);
  5698. default:
  5699. return super.parseBindingAtom();
  5700. }
  5701. }
  5702. parseFunctionParams(node, allowModifiers) {
  5703. const kind = node.kind;
  5704. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5705. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5706. }
  5707. super.parseFunctionParams(node, allowModifiers);
  5708. }
  5709. parseVarId(decl, kind) {
  5710. super.parseVarId(decl, kind);
  5711. if (this.match(14)) {
  5712. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5713. this.resetEndLocation(decl.id);
  5714. }
  5715. }
  5716. parseAsyncArrowFromCallExpression(node, call) {
  5717. if (this.match(14)) {
  5718. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5719. this.state.noAnonFunctionType = true;
  5720. node.returnType = this.flowParseTypeAnnotation();
  5721. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5722. }
  5723. return super.parseAsyncArrowFromCallExpression(node, call);
  5724. }
  5725. shouldParseAsyncArrow() {
  5726. return this.match(14) || super.shouldParseAsyncArrow();
  5727. }
  5728. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5729. var _jsx;
  5730. let state = null;
  5731. let jsx;
  5732. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  5733. state = this.state.clone();
  5734. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5735. if (!jsx.error) return jsx.node;
  5736. const {
  5737. context
  5738. } = this.state;
  5739. const currentContext = context[context.length - 1];
  5740. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5741. context.pop();
  5742. }
  5743. }
  5744. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5745. var _jsx2, _jsx3;
  5746. state = state || this.state.clone();
  5747. let typeParameters;
  5748. const arrow = this.tryParse(abort => {
  5749. var _arrowExpression$extr;
  5750. typeParameters = this.flowParseTypeParameterDeclaration();
  5751. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5752. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5753. this.resetStartLocationFromNode(result, typeParameters);
  5754. return result;
  5755. });
  5756. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5757. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5758. if (expr.type !== "ArrowFunctionExpression") abort();
  5759. expr.typeParameters = typeParameters;
  5760. this.resetStartLocationFromNode(expr, typeParameters);
  5761. return arrowExpression;
  5762. }, state);
  5763. let arrowExpression = null;
  5764. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5765. if (!arrow.error && !arrow.aborted) {
  5766. if (arrow.node.async) {
  5767. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5768. at: typeParameters
  5769. });
  5770. }
  5771. return arrow.node;
  5772. }
  5773. arrowExpression = arrow.node;
  5774. }
  5775. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5776. this.state = jsx.failState;
  5777. return jsx.node;
  5778. }
  5779. if (arrowExpression) {
  5780. this.state = arrow.failState;
  5781. return arrowExpression;
  5782. }
  5783. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5784. if (arrow.thrown) throw arrow.error;
  5785. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5786. at: typeParameters
  5787. });
  5788. }
  5789. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5790. }
  5791. parseArrow(node) {
  5792. if (this.match(14)) {
  5793. const result = this.tryParse(() => {
  5794. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5795. this.state.noAnonFunctionType = true;
  5796. const typeNode = this.startNode();
  5797. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5798. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5799. if (this.canInsertSemicolon()) this.unexpected();
  5800. if (!this.match(19)) this.unexpected();
  5801. return typeNode;
  5802. });
  5803. if (result.thrown) return null;
  5804. if (result.error) this.state = result.failState;
  5805. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5806. }
  5807. return super.parseArrow(node);
  5808. }
  5809. shouldParseArrow(params) {
  5810. return this.match(14) || super.shouldParseArrow(params);
  5811. }
  5812. setArrowFunctionParameters(node, params) {
  5813. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5814. node.params = params;
  5815. } else {
  5816. super.setArrowFunctionParameters(node, params);
  5817. }
  5818. }
  5819. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5820. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5821. return;
  5822. }
  5823. for (let i = 0; i < node.params.length; i++) {
  5824. if (this.isThisParam(node.params[i]) && i > 0) {
  5825. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5826. at: node.params[i]
  5827. });
  5828. }
  5829. }
  5830. return super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5831. }
  5832. parseParenAndDistinguishExpression(canBeArrow) {
  5833. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5834. }
  5835. parseSubscripts(base, startLoc, noCalls) {
  5836. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startLoc.index) !== -1) {
  5837. this.next();
  5838. const node = this.startNodeAt(startLoc);
  5839. node.callee = base;
  5840. node.arguments = super.parseCallExpressionArguments(11, false);
  5841. base = this.finishNode(node, "CallExpression");
  5842. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5843. const state = this.state.clone();
  5844. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  5845. if (!arrow.error && !arrow.aborted) return arrow.node;
  5846. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  5847. if (result.node && !result.error) return result.node;
  5848. if (arrow.node) {
  5849. this.state = arrow.failState;
  5850. return arrow.node;
  5851. }
  5852. if (result.node) {
  5853. this.state = result.failState;
  5854. return result.node;
  5855. }
  5856. throw arrow.error || result.error;
  5857. }
  5858. return super.parseSubscripts(base, startLoc, noCalls);
  5859. }
  5860. parseSubscript(base, startLoc, noCalls, subscriptState) {
  5861. if (this.match(18) && this.isLookaheadToken_lt()) {
  5862. subscriptState.optionalChainMember = true;
  5863. if (noCalls) {
  5864. subscriptState.stop = true;
  5865. return base;
  5866. }
  5867. this.next();
  5868. const node = this.startNodeAt(startLoc);
  5869. node.callee = base;
  5870. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5871. this.expect(10);
  5872. node.arguments = this.parseCallExpressionArguments(11, false);
  5873. node.optional = true;
  5874. return this.finishCallExpression(node, true);
  5875. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5876. const node = this.startNodeAt(startLoc);
  5877. node.callee = base;
  5878. const result = this.tryParse(() => {
  5879. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5880. this.expect(10);
  5881. node.arguments = super.parseCallExpressionArguments(11, false);
  5882. if (subscriptState.optionalChainMember) {
  5883. node.optional = false;
  5884. }
  5885. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5886. });
  5887. if (result.node) {
  5888. if (result.error) this.state = result.failState;
  5889. return result.node;
  5890. }
  5891. }
  5892. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  5893. }
  5894. parseNewCallee(node) {
  5895. super.parseNewCallee(node);
  5896. let targs = null;
  5897. if (this.shouldParseTypes() && this.match(47)) {
  5898. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5899. }
  5900. node.typeArguments = targs;
  5901. }
  5902. parseAsyncArrowWithTypeParameters(startLoc) {
  5903. const node = this.startNodeAt(startLoc);
  5904. this.parseFunctionParams(node);
  5905. if (!this.parseArrow(node)) return;
  5906. return super.parseArrowExpression(node, undefined, true);
  5907. }
  5908. readToken_mult_modulo(code) {
  5909. const next = this.input.charCodeAt(this.state.pos + 1);
  5910. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5911. this.state.hasFlowComment = false;
  5912. this.state.pos += 2;
  5913. this.nextToken();
  5914. return;
  5915. }
  5916. super.readToken_mult_modulo(code);
  5917. }
  5918. readToken_pipe_amp(code) {
  5919. const next = this.input.charCodeAt(this.state.pos + 1);
  5920. if (code === 124 && next === 125) {
  5921. this.finishOp(9, 2);
  5922. return;
  5923. }
  5924. super.readToken_pipe_amp(code);
  5925. }
  5926. parseTopLevel(file, program) {
  5927. const fileNode = super.parseTopLevel(file, program);
  5928. if (this.state.hasFlowComment) {
  5929. this.raise(FlowErrors.UnterminatedFlowComment, {
  5930. at: this.state.curPosition()
  5931. });
  5932. }
  5933. return fileNode;
  5934. }
  5935. skipBlockComment() {
  5936. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5937. if (this.state.hasFlowComment) {
  5938. throw this.raise(FlowErrors.NestedFlowComment, {
  5939. at: this.state.startLoc
  5940. });
  5941. }
  5942. this.hasFlowCommentCompletion();
  5943. const commentSkip = this.skipFlowComment();
  5944. if (commentSkip) {
  5945. this.state.pos += commentSkip;
  5946. this.state.hasFlowComment = true;
  5947. }
  5948. return;
  5949. }
  5950. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  5951. }
  5952. skipFlowComment() {
  5953. const {
  5954. pos
  5955. } = this.state;
  5956. let shiftToFirstNonWhiteSpace = 2;
  5957. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5958. shiftToFirstNonWhiteSpace++;
  5959. }
  5960. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5961. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5962. if (ch2 === 58 && ch3 === 58) {
  5963. return shiftToFirstNonWhiteSpace + 2;
  5964. }
  5965. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5966. return shiftToFirstNonWhiteSpace + 12;
  5967. }
  5968. if (ch2 === 58 && ch3 !== 58) {
  5969. return shiftToFirstNonWhiteSpace;
  5970. }
  5971. return false;
  5972. }
  5973. hasFlowCommentCompletion() {
  5974. const end = this.input.indexOf("*/", this.state.pos);
  5975. if (end === -1) {
  5976. throw this.raise(Errors.UnterminatedComment, {
  5977. at: this.state.curPosition()
  5978. });
  5979. }
  5980. }
  5981. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5982. enumName,
  5983. memberName
  5984. }) {
  5985. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5986. at: loc,
  5987. memberName,
  5988. enumName
  5989. });
  5990. }
  5991. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5992. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  5993. at: loc
  5994. }, enumContext));
  5995. }
  5996. flowEnumErrorNumberMemberNotInitialized(loc, {
  5997. enumName,
  5998. memberName
  5999. }) {
  6000. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  6001. at: loc,
  6002. enumName,
  6003. memberName
  6004. });
  6005. }
  6006. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  6007. enumName
  6008. }) {
  6009. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  6010. at: node,
  6011. enumName
  6012. });
  6013. }
  6014. flowEnumMemberInit() {
  6015. const startLoc = this.state.startLoc;
  6016. const endOfInit = () => this.match(12) || this.match(8);
  6017. switch (this.state.type) {
  6018. case 132:
  6019. {
  6020. const literal = this.parseNumericLiteral(this.state.value);
  6021. if (endOfInit()) {
  6022. return {
  6023. type: "number",
  6024. loc: literal.loc.start,
  6025. value: literal
  6026. };
  6027. }
  6028. return {
  6029. type: "invalid",
  6030. loc: startLoc
  6031. };
  6032. }
  6033. case 131:
  6034. {
  6035. const literal = this.parseStringLiteral(this.state.value);
  6036. if (endOfInit()) {
  6037. return {
  6038. type: "string",
  6039. loc: literal.loc.start,
  6040. value: literal
  6041. };
  6042. }
  6043. return {
  6044. type: "invalid",
  6045. loc: startLoc
  6046. };
  6047. }
  6048. case 85:
  6049. case 86:
  6050. {
  6051. const literal = this.parseBooleanLiteral(this.match(85));
  6052. if (endOfInit()) {
  6053. return {
  6054. type: "boolean",
  6055. loc: literal.loc.start,
  6056. value: literal
  6057. };
  6058. }
  6059. return {
  6060. type: "invalid",
  6061. loc: startLoc
  6062. };
  6063. }
  6064. default:
  6065. return {
  6066. type: "invalid",
  6067. loc: startLoc
  6068. };
  6069. }
  6070. }
  6071. flowEnumMemberRaw() {
  6072. const loc = this.state.startLoc;
  6073. const id = this.parseIdentifier(true);
  6074. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  6075. type: "none",
  6076. loc
  6077. };
  6078. return {
  6079. id,
  6080. init
  6081. };
  6082. }
  6083. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  6084. const {
  6085. explicitType
  6086. } = context;
  6087. if (explicitType === null) {
  6088. return;
  6089. }
  6090. if (explicitType !== expectedType) {
  6091. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  6092. }
  6093. }
  6094. flowEnumMembers({
  6095. enumName,
  6096. explicitType
  6097. }) {
  6098. const seenNames = new Set();
  6099. const members = {
  6100. booleanMembers: [],
  6101. numberMembers: [],
  6102. stringMembers: [],
  6103. defaultedMembers: []
  6104. };
  6105. let hasUnknownMembers = false;
  6106. while (!this.match(8)) {
  6107. if (this.eat(21)) {
  6108. hasUnknownMembers = true;
  6109. break;
  6110. }
  6111. const memberNode = this.startNode();
  6112. const {
  6113. id,
  6114. init
  6115. } = this.flowEnumMemberRaw();
  6116. const memberName = id.name;
  6117. if (memberName === "") {
  6118. continue;
  6119. }
  6120. if (/^[a-z]/.test(memberName)) {
  6121. this.raise(FlowErrors.EnumInvalidMemberName, {
  6122. at: id,
  6123. memberName,
  6124. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  6125. enumName
  6126. });
  6127. }
  6128. if (seenNames.has(memberName)) {
  6129. this.raise(FlowErrors.EnumDuplicateMemberName, {
  6130. at: id,
  6131. memberName,
  6132. enumName
  6133. });
  6134. }
  6135. seenNames.add(memberName);
  6136. const context = {
  6137. enumName,
  6138. explicitType,
  6139. memberName
  6140. };
  6141. memberNode.id = id;
  6142. switch (init.type) {
  6143. case "boolean":
  6144. {
  6145. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6146. memberNode.init = init.value;
  6147. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6148. break;
  6149. }
  6150. case "number":
  6151. {
  6152. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6153. memberNode.init = init.value;
  6154. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6155. break;
  6156. }
  6157. case "string":
  6158. {
  6159. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6160. memberNode.init = init.value;
  6161. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6162. break;
  6163. }
  6164. case "invalid":
  6165. {
  6166. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6167. }
  6168. case "none":
  6169. {
  6170. switch (explicitType) {
  6171. case "boolean":
  6172. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6173. break;
  6174. case "number":
  6175. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6176. break;
  6177. default:
  6178. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6179. }
  6180. }
  6181. }
  6182. if (!this.match(8)) {
  6183. this.expect(12);
  6184. }
  6185. }
  6186. return {
  6187. members,
  6188. hasUnknownMembers
  6189. };
  6190. }
  6191. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6192. enumName
  6193. }) {
  6194. if (initializedMembers.length === 0) {
  6195. return defaultedMembers;
  6196. } else if (defaultedMembers.length === 0) {
  6197. return initializedMembers;
  6198. } else if (defaultedMembers.length > initializedMembers.length) {
  6199. for (const member of initializedMembers) {
  6200. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6201. enumName
  6202. });
  6203. }
  6204. return defaultedMembers;
  6205. } else {
  6206. for (const member of defaultedMembers) {
  6207. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6208. enumName
  6209. });
  6210. }
  6211. return initializedMembers;
  6212. }
  6213. }
  6214. flowEnumParseExplicitType({
  6215. enumName
  6216. }) {
  6217. if (!this.eatContextual(101)) return null;
  6218. if (!tokenIsIdentifier(this.state.type)) {
  6219. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6220. at: this.state.startLoc,
  6221. enumName
  6222. });
  6223. }
  6224. const {
  6225. value
  6226. } = this.state;
  6227. this.next();
  6228. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6229. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6230. at: this.state.startLoc,
  6231. enumName,
  6232. invalidEnumType: value
  6233. });
  6234. }
  6235. return value;
  6236. }
  6237. flowEnumBody(node, id) {
  6238. const enumName = id.name;
  6239. const nameLoc = id.loc.start;
  6240. const explicitType = this.flowEnumParseExplicitType({
  6241. enumName
  6242. });
  6243. this.expect(5);
  6244. const {
  6245. members,
  6246. hasUnknownMembers
  6247. } = this.flowEnumMembers({
  6248. enumName,
  6249. explicitType
  6250. });
  6251. node.hasUnknownMembers = hasUnknownMembers;
  6252. switch (explicitType) {
  6253. case "boolean":
  6254. node.explicitType = true;
  6255. node.members = members.booleanMembers;
  6256. this.expect(8);
  6257. return this.finishNode(node, "EnumBooleanBody");
  6258. case "number":
  6259. node.explicitType = true;
  6260. node.members = members.numberMembers;
  6261. this.expect(8);
  6262. return this.finishNode(node, "EnumNumberBody");
  6263. case "string":
  6264. node.explicitType = true;
  6265. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6266. enumName
  6267. });
  6268. this.expect(8);
  6269. return this.finishNode(node, "EnumStringBody");
  6270. case "symbol":
  6271. node.members = members.defaultedMembers;
  6272. this.expect(8);
  6273. return this.finishNode(node, "EnumSymbolBody");
  6274. default:
  6275. {
  6276. const empty = () => {
  6277. node.members = [];
  6278. this.expect(8);
  6279. return this.finishNode(node, "EnumStringBody");
  6280. };
  6281. node.explicitType = false;
  6282. const boolsLen = members.booleanMembers.length;
  6283. const numsLen = members.numberMembers.length;
  6284. const strsLen = members.stringMembers.length;
  6285. const defaultedLen = members.defaultedMembers.length;
  6286. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6287. return empty();
  6288. } else if (!boolsLen && !numsLen) {
  6289. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6290. enumName
  6291. });
  6292. this.expect(8);
  6293. return this.finishNode(node, "EnumStringBody");
  6294. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6295. for (const member of members.defaultedMembers) {
  6296. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6297. enumName,
  6298. memberName: member.id.name
  6299. });
  6300. }
  6301. node.members = members.booleanMembers;
  6302. this.expect(8);
  6303. return this.finishNode(node, "EnumBooleanBody");
  6304. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6305. for (const member of members.defaultedMembers) {
  6306. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6307. enumName,
  6308. memberName: member.id.name
  6309. });
  6310. }
  6311. node.members = members.numberMembers;
  6312. this.expect(8);
  6313. return this.finishNode(node, "EnumNumberBody");
  6314. } else {
  6315. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6316. at: nameLoc,
  6317. enumName
  6318. });
  6319. return empty();
  6320. }
  6321. }
  6322. }
  6323. }
  6324. flowParseEnumDeclaration(node) {
  6325. const id = this.parseIdentifier();
  6326. node.id = id;
  6327. node.body = this.flowEnumBody(this.startNode(), id);
  6328. return this.finishNode(node, "EnumDeclaration");
  6329. }
  6330. isLookaheadToken_lt() {
  6331. const next = this.nextTokenStart();
  6332. if (this.input.charCodeAt(next) === 60) {
  6333. const afterNext = this.input.charCodeAt(next + 1);
  6334. return afterNext !== 60 && afterNext !== 61;
  6335. }
  6336. return false;
  6337. }
  6338. maybeUnwrapTypeCastExpression(node) {
  6339. return node.type === "TypeCastExpression" ? node.expression : node;
  6340. }
  6341. });
  6342. const entities = {
  6343. __proto__: null,
  6344. quot: "\u0022",
  6345. amp: "&",
  6346. apos: "\u0027",
  6347. lt: "<",
  6348. gt: ">",
  6349. nbsp: "\u00A0",
  6350. iexcl: "\u00A1",
  6351. cent: "\u00A2",
  6352. pound: "\u00A3",
  6353. curren: "\u00A4",
  6354. yen: "\u00A5",
  6355. brvbar: "\u00A6",
  6356. sect: "\u00A7",
  6357. uml: "\u00A8",
  6358. copy: "\u00A9",
  6359. ordf: "\u00AA",
  6360. laquo: "\u00AB",
  6361. not: "\u00AC",
  6362. shy: "\u00AD",
  6363. reg: "\u00AE",
  6364. macr: "\u00AF",
  6365. deg: "\u00B0",
  6366. plusmn: "\u00B1",
  6367. sup2: "\u00B2",
  6368. sup3: "\u00B3",
  6369. acute: "\u00B4",
  6370. micro: "\u00B5",
  6371. para: "\u00B6",
  6372. middot: "\u00B7",
  6373. cedil: "\u00B8",
  6374. sup1: "\u00B9",
  6375. ordm: "\u00BA",
  6376. raquo: "\u00BB",
  6377. frac14: "\u00BC",
  6378. frac12: "\u00BD",
  6379. frac34: "\u00BE",
  6380. iquest: "\u00BF",
  6381. Agrave: "\u00C0",
  6382. Aacute: "\u00C1",
  6383. Acirc: "\u00C2",
  6384. Atilde: "\u00C3",
  6385. Auml: "\u00C4",
  6386. Aring: "\u00C5",
  6387. AElig: "\u00C6",
  6388. Ccedil: "\u00C7",
  6389. Egrave: "\u00C8",
  6390. Eacute: "\u00C9",
  6391. Ecirc: "\u00CA",
  6392. Euml: "\u00CB",
  6393. Igrave: "\u00CC",
  6394. Iacute: "\u00CD",
  6395. Icirc: "\u00CE",
  6396. Iuml: "\u00CF",
  6397. ETH: "\u00D0",
  6398. Ntilde: "\u00D1",
  6399. Ograve: "\u00D2",
  6400. Oacute: "\u00D3",
  6401. Ocirc: "\u00D4",
  6402. Otilde: "\u00D5",
  6403. Ouml: "\u00D6",
  6404. times: "\u00D7",
  6405. Oslash: "\u00D8",
  6406. Ugrave: "\u00D9",
  6407. Uacute: "\u00DA",
  6408. Ucirc: "\u00DB",
  6409. Uuml: "\u00DC",
  6410. Yacute: "\u00DD",
  6411. THORN: "\u00DE",
  6412. szlig: "\u00DF",
  6413. agrave: "\u00E0",
  6414. aacute: "\u00E1",
  6415. acirc: "\u00E2",
  6416. atilde: "\u00E3",
  6417. auml: "\u00E4",
  6418. aring: "\u00E5",
  6419. aelig: "\u00E6",
  6420. ccedil: "\u00E7",
  6421. egrave: "\u00E8",
  6422. eacute: "\u00E9",
  6423. ecirc: "\u00EA",
  6424. euml: "\u00EB",
  6425. igrave: "\u00EC",
  6426. iacute: "\u00ED",
  6427. icirc: "\u00EE",
  6428. iuml: "\u00EF",
  6429. eth: "\u00F0",
  6430. ntilde: "\u00F1",
  6431. ograve: "\u00F2",
  6432. oacute: "\u00F3",
  6433. ocirc: "\u00F4",
  6434. otilde: "\u00F5",
  6435. ouml: "\u00F6",
  6436. divide: "\u00F7",
  6437. oslash: "\u00F8",
  6438. ugrave: "\u00F9",
  6439. uacute: "\u00FA",
  6440. ucirc: "\u00FB",
  6441. uuml: "\u00FC",
  6442. yacute: "\u00FD",
  6443. thorn: "\u00FE",
  6444. yuml: "\u00FF",
  6445. OElig: "\u0152",
  6446. oelig: "\u0153",
  6447. Scaron: "\u0160",
  6448. scaron: "\u0161",
  6449. Yuml: "\u0178",
  6450. fnof: "\u0192",
  6451. circ: "\u02C6",
  6452. tilde: "\u02DC",
  6453. Alpha: "\u0391",
  6454. Beta: "\u0392",
  6455. Gamma: "\u0393",
  6456. Delta: "\u0394",
  6457. Epsilon: "\u0395",
  6458. Zeta: "\u0396",
  6459. Eta: "\u0397",
  6460. Theta: "\u0398",
  6461. Iota: "\u0399",
  6462. Kappa: "\u039A",
  6463. Lambda: "\u039B",
  6464. Mu: "\u039C",
  6465. Nu: "\u039D",
  6466. Xi: "\u039E",
  6467. Omicron: "\u039F",
  6468. Pi: "\u03A0",
  6469. Rho: "\u03A1",
  6470. Sigma: "\u03A3",
  6471. Tau: "\u03A4",
  6472. Upsilon: "\u03A5",
  6473. Phi: "\u03A6",
  6474. Chi: "\u03A7",
  6475. Psi: "\u03A8",
  6476. Omega: "\u03A9",
  6477. alpha: "\u03B1",
  6478. beta: "\u03B2",
  6479. gamma: "\u03B3",
  6480. delta: "\u03B4",
  6481. epsilon: "\u03B5",
  6482. zeta: "\u03B6",
  6483. eta: "\u03B7",
  6484. theta: "\u03B8",
  6485. iota: "\u03B9",
  6486. kappa: "\u03BA",
  6487. lambda: "\u03BB",
  6488. mu: "\u03BC",
  6489. nu: "\u03BD",
  6490. xi: "\u03BE",
  6491. omicron: "\u03BF",
  6492. pi: "\u03C0",
  6493. rho: "\u03C1",
  6494. sigmaf: "\u03C2",
  6495. sigma: "\u03C3",
  6496. tau: "\u03C4",
  6497. upsilon: "\u03C5",
  6498. phi: "\u03C6",
  6499. chi: "\u03C7",
  6500. psi: "\u03C8",
  6501. omega: "\u03C9",
  6502. thetasym: "\u03D1",
  6503. upsih: "\u03D2",
  6504. piv: "\u03D6",
  6505. ensp: "\u2002",
  6506. emsp: "\u2003",
  6507. thinsp: "\u2009",
  6508. zwnj: "\u200C",
  6509. zwj: "\u200D",
  6510. lrm: "\u200E",
  6511. rlm: "\u200F",
  6512. ndash: "\u2013",
  6513. mdash: "\u2014",
  6514. lsquo: "\u2018",
  6515. rsquo: "\u2019",
  6516. sbquo: "\u201A",
  6517. ldquo: "\u201C",
  6518. rdquo: "\u201D",
  6519. bdquo: "\u201E",
  6520. dagger: "\u2020",
  6521. Dagger: "\u2021",
  6522. bull: "\u2022",
  6523. hellip: "\u2026",
  6524. permil: "\u2030",
  6525. prime: "\u2032",
  6526. Prime: "\u2033",
  6527. lsaquo: "\u2039",
  6528. rsaquo: "\u203A",
  6529. oline: "\u203E",
  6530. frasl: "\u2044",
  6531. euro: "\u20AC",
  6532. image: "\u2111",
  6533. weierp: "\u2118",
  6534. real: "\u211C",
  6535. trade: "\u2122",
  6536. alefsym: "\u2135",
  6537. larr: "\u2190",
  6538. uarr: "\u2191",
  6539. rarr: "\u2192",
  6540. darr: "\u2193",
  6541. harr: "\u2194",
  6542. crarr: "\u21B5",
  6543. lArr: "\u21D0",
  6544. uArr: "\u21D1",
  6545. rArr: "\u21D2",
  6546. dArr: "\u21D3",
  6547. hArr: "\u21D4",
  6548. forall: "\u2200",
  6549. part: "\u2202",
  6550. exist: "\u2203",
  6551. empty: "\u2205",
  6552. nabla: "\u2207",
  6553. isin: "\u2208",
  6554. notin: "\u2209",
  6555. ni: "\u220B",
  6556. prod: "\u220F",
  6557. sum: "\u2211",
  6558. minus: "\u2212",
  6559. lowast: "\u2217",
  6560. radic: "\u221A",
  6561. prop: "\u221D",
  6562. infin: "\u221E",
  6563. ang: "\u2220",
  6564. and: "\u2227",
  6565. or: "\u2228",
  6566. cap: "\u2229",
  6567. cup: "\u222A",
  6568. int: "\u222B",
  6569. there4: "\u2234",
  6570. sim: "\u223C",
  6571. cong: "\u2245",
  6572. asymp: "\u2248",
  6573. ne: "\u2260",
  6574. equiv: "\u2261",
  6575. le: "\u2264",
  6576. ge: "\u2265",
  6577. sub: "\u2282",
  6578. sup: "\u2283",
  6579. nsub: "\u2284",
  6580. sube: "\u2286",
  6581. supe: "\u2287",
  6582. oplus: "\u2295",
  6583. otimes: "\u2297",
  6584. perp: "\u22A5",
  6585. sdot: "\u22C5",
  6586. lceil: "\u2308",
  6587. rceil: "\u2309",
  6588. lfloor: "\u230A",
  6589. rfloor: "\u230B",
  6590. lang: "\u2329",
  6591. rang: "\u232A",
  6592. loz: "\u25CA",
  6593. spades: "\u2660",
  6594. clubs: "\u2663",
  6595. hearts: "\u2665",
  6596. diams: "\u2666"
  6597. };
  6598. const JsxErrors = ParseErrorEnum`jsx`({
  6599. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6600. MissingClosingTagElement: ({
  6601. openingTagName
  6602. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6603. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6604. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6605. UnexpectedToken: ({
  6606. unexpected,
  6607. HTMLEntity
  6608. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6609. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6610. UnterminatedJsxContent: "Unterminated JSX contents.",
  6611. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6612. });
  6613. function isFragment(object) {
  6614. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6615. }
  6616. function getQualifiedJSXName(object) {
  6617. if (object.type === "JSXIdentifier") {
  6618. return object.name;
  6619. }
  6620. if (object.type === "JSXNamespacedName") {
  6621. return object.namespace.name + ":" + object.name.name;
  6622. }
  6623. if (object.type === "JSXMemberExpression") {
  6624. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6625. }
  6626. throw new Error("Node had unexpected type: " + object.type);
  6627. }
  6628. var jsx = (superClass => class JSXParserMixin extends superClass {
  6629. jsxReadToken() {
  6630. let out = "";
  6631. let chunkStart = this.state.pos;
  6632. for (;;) {
  6633. if (this.state.pos >= this.length) {
  6634. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6635. at: this.state.startLoc
  6636. });
  6637. }
  6638. const ch = this.input.charCodeAt(this.state.pos);
  6639. switch (ch) {
  6640. case 60:
  6641. case 123:
  6642. if (this.state.pos === this.state.start) {
  6643. if (ch === 60 && this.state.canStartJSXElement) {
  6644. ++this.state.pos;
  6645. return this.finishToken(140);
  6646. }
  6647. return super.getTokenFromCode(ch);
  6648. }
  6649. out += this.input.slice(chunkStart, this.state.pos);
  6650. return this.finishToken(139, out);
  6651. case 38:
  6652. out += this.input.slice(chunkStart, this.state.pos);
  6653. out += this.jsxReadEntity();
  6654. chunkStart = this.state.pos;
  6655. break;
  6656. case 62:
  6657. case 125:
  6658. default:
  6659. if (isNewLine(ch)) {
  6660. out += this.input.slice(chunkStart, this.state.pos);
  6661. out += this.jsxReadNewLine(true);
  6662. chunkStart = this.state.pos;
  6663. } else {
  6664. ++this.state.pos;
  6665. }
  6666. }
  6667. }
  6668. }
  6669. jsxReadNewLine(normalizeCRLF) {
  6670. const ch = this.input.charCodeAt(this.state.pos);
  6671. let out;
  6672. ++this.state.pos;
  6673. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6674. ++this.state.pos;
  6675. out = normalizeCRLF ? "\n" : "\r\n";
  6676. } else {
  6677. out = String.fromCharCode(ch);
  6678. }
  6679. ++this.state.curLine;
  6680. this.state.lineStart = this.state.pos;
  6681. return out;
  6682. }
  6683. jsxReadString(quote) {
  6684. let out = "";
  6685. let chunkStart = ++this.state.pos;
  6686. for (;;) {
  6687. if (this.state.pos >= this.length) {
  6688. throw this.raise(Errors.UnterminatedString, {
  6689. at: this.state.startLoc
  6690. });
  6691. }
  6692. const ch = this.input.charCodeAt(this.state.pos);
  6693. if (ch === quote) break;
  6694. if (ch === 38) {
  6695. out += this.input.slice(chunkStart, this.state.pos);
  6696. out += this.jsxReadEntity();
  6697. chunkStart = this.state.pos;
  6698. } else if (isNewLine(ch)) {
  6699. out += this.input.slice(chunkStart, this.state.pos);
  6700. out += this.jsxReadNewLine(false);
  6701. chunkStart = this.state.pos;
  6702. } else {
  6703. ++this.state.pos;
  6704. }
  6705. }
  6706. out += this.input.slice(chunkStart, this.state.pos++);
  6707. return this.finishToken(131, out);
  6708. }
  6709. jsxReadEntity() {
  6710. const startPos = ++this.state.pos;
  6711. if (this.codePointAtPos(this.state.pos) === 35) {
  6712. ++this.state.pos;
  6713. let radix = 10;
  6714. if (this.codePointAtPos(this.state.pos) === 120) {
  6715. radix = 16;
  6716. ++this.state.pos;
  6717. }
  6718. const codePoint = this.readInt(radix, undefined, false, "bail");
  6719. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6720. ++this.state.pos;
  6721. return String.fromCodePoint(codePoint);
  6722. }
  6723. } else {
  6724. let count = 0;
  6725. let semi = false;
  6726. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6727. ++this.state.pos;
  6728. }
  6729. if (semi) {
  6730. const desc = this.input.slice(startPos, this.state.pos);
  6731. const entity = entities[desc];
  6732. ++this.state.pos;
  6733. if (entity) {
  6734. return entity;
  6735. }
  6736. }
  6737. }
  6738. this.state.pos = startPos;
  6739. return "&";
  6740. }
  6741. jsxReadWord() {
  6742. let ch;
  6743. const start = this.state.pos;
  6744. do {
  6745. ch = this.input.charCodeAt(++this.state.pos);
  6746. } while (isIdentifierChar(ch) || ch === 45);
  6747. return this.finishToken(138, this.input.slice(start, this.state.pos));
  6748. }
  6749. jsxParseIdentifier() {
  6750. const node = this.startNode();
  6751. if (this.match(138)) {
  6752. node.name = this.state.value;
  6753. } else if (tokenIsKeyword(this.state.type)) {
  6754. node.name = tokenLabelName(this.state.type);
  6755. } else {
  6756. this.unexpected();
  6757. }
  6758. this.next();
  6759. return this.finishNode(node, "JSXIdentifier");
  6760. }
  6761. jsxParseNamespacedName() {
  6762. const startLoc = this.state.startLoc;
  6763. const name = this.jsxParseIdentifier();
  6764. if (!this.eat(14)) return name;
  6765. const node = this.startNodeAt(startLoc);
  6766. node.namespace = name;
  6767. node.name = this.jsxParseIdentifier();
  6768. return this.finishNode(node, "JSXNamespacedName");
  6769. }
  6770. jsxParseElementName() {
  6771. const startLoc = this.state.startLoc;
  6772. let node = this.jsxParseNamespacedName();
  6773. if (node.type === "JSXNamespacedName") {
  6774. return node;
  6775. }
  6776. while (this.eat(16)) {
  6777. const newNode = this.startNodeAt(startLoc);
  6778. newNode.object = node;
  6779. newNode.property = this.jsxParseIdentifier();
  6780. node = this.finishNode(newNode, "JSXMemberExpression");
  6781. }
  6782. return node;
  6783. }
  6784. jsxParseAttributeValue() {
  6785. let node;
  6786. switch (this.state.type) {
  6787. case 5:
  6788. node = this.startNode();
  6789. this.setContext(types.brace);
  6790. this.next();
  6791. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6792. if (node.expression.type === "JSXEmptyExpression") {
  6793. this.raise(JsxErrors.AttributeIsEmpty, {
  6794. at: node
  6795. });
  6796. }
  6797. return node;
  6798. case 140:
  6799. case 131:
  6800. return this.parseExprAtom();
  6801. default:
  6802. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6803. at: this.state.startLoc
  6804. });
  6805. }
  6806. }
  6807. jsxParseEmptyExpression() {
  6808. const node = this.startNodeAt(this.state.lastTokEndLoc);
  6809. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6810. }
  6811. jsxParseSpreadChild(node) {
  6812. this.next();
  6813. node.expression = this.parseExpression();
  6814. this.setContext(types.j_expr);
  6815. this.state.canStartJSXElement = true;
  6816. this.expect(8);
  6817. return this.finishNode(node, "JSXSpreadChild");
  6818. }
  6819. jsxParseExpressionContainer(node, previousContext) {
  6820. if (this.match(8)) {
  6821. node.expression = this.jsxParseEmptyExpression();
  6822. } else {
  6823. const expression = this.parseExpression();
  6824. node.expression = expression;
  6825. }
  6826. this.setContext(previousContext);
  6827. this.state.canStartJSXElement = true;
  6828. this.expect(8);
  6829. return this.finishNode(node, "JSXExpressionContainer");
  6830. }
  6831. jsxParseAttribute() {
  6832. const node = this.startNode();
  6833. if (this.match(5)) {
  6834. this.setContext(types.brace);
  6835. this.next();
  6836. this.expect(21);
  6837. node.argument = this.parseMaybeAssignAllowIn();
  6838. this.setContext(types.j_oTag);
  6839. this.state.canStartJSXElement = true;
  6840. this.expect(8);
  6841. return this.finishNode(node, "JSXSpreadAttribute");
  6842. }
  6843. node.name = this.jsxParseNamespacedName();
  6844. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6845. return this.finishNode(node, "JSXAttribute");
  6846. }
  6847. jsxParseOpeningElementAt(startLoc) {
  6848. const node = this.startNodeAt(startLoc);
  6849. if (this.eat(141)) {
  6850. return this.finishNode(node, "JSXOpeningFragment");
  6851. }
  6852. node.name = this.jsxParseElementName();
  6853. return this.jsxParseOpeningElementAfterName(node);
  6854. }
  6855. jsxParseOpeningElementAfterName(node) {
  6856. const attributes = [];
  6857. while (!this.match(56) && !this.match(141)) {
  6858. attributes.push(this.jsxParseAttribute());
  6859. }
  6860. node.attributes = attributes;
  6861. node.selfClosing = this.eat(56);
  6862. this.expect(141);
  6863. return this.finishNode(node, "JSXOpeningElement");
  6864. }
  6865. jsxParseClosingElementAt(startLoc) {
  6866. const node = this.startNodeAt(startLoc);
  6867. if (this.eat(141)) {
  6868. return this.finishNode(node, "JSXClosingFragment");
  6869. }
  6870. node.name = this.jsxParseElementName();
  6871. this.expect(141);
  6872. return this.finishNode(node, "JSXClosingElement");
  6873. }
  6874. jsxParseElementAt(startLoc) {
  6875. const node = this.startNodeAt(startLoc);
  6876. const children = [];
  6877. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  6878. let closingElement = null;
  6879. if (!openingElement.selfClosing) {
  6880. contents: for (;;) {
  6881. switch (this.state.type) {
  6882. case 140:
  6883. startLoc = this.state.startLoc;
  6884. this.next();
  6885. if (this.eat(56)) {
  6886. closingElement = this.jsxParseClosingElementAt(startLoc);
  6887. break contents;
  6888. }
  6889. children.push(this.jsxParseElementAt(startLoc));
  6890. break;
  6891. case 139:
  6892. children.push(this.parseExprAtom());
  6893. break;
  6894. case 5:
  6895. {
  6896. const node = this.startNode();
  6897. this.setContext(types.brace);
  6898. this.next();
  6899. if (this.match(21)) {
  6900. children.push(this.jsxParseSpreadChild(node));
  6901. } else {
  6902. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6903. }
  6904. break;
  6905. }
  6906. default:
  6907. throw this.unexpected();
  6908. }
  6909. }
  6910. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6911. this.raise(JsxErrors.MissingClosingTagFragment, {
  6912. at: closingElement
  6913. });
  6914. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6915. this.raise(JsxErrors.MissingClosingTagElement, {
  6916. at: closingElement,
  6917. openingTagName: getQualifiedJSXName(openingElement.name)
  6918. });
  6919. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6920. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6921. this.raise(JsxErrors.MissingClosingTagElement, {
  6922. at: closingElement,
  6923. openingTagName: getQualifiedJSXName(openingElement.name)
  6924. });
  6925. }
  6926. }
  6927. }
  6928. if (isFragment(openingElement)) {
  6929. node.openingFragment = openingElement;
  6930. node.closingFragment = closingElement;
  6931. } else {
  6932. node.openingElement = openingElement;
  6933. node.closingElement = closingElement;
  6934. }
  6935. node.children = children;
  6936. if (this.match(47)) {
  6937. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6938. at: this.state.startLoc
  6939. });
  6940. }
  6941. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6942. }
  6943. jsxParseElement() {
  6944. const startLoc = this.state.startLoc;
  6945. this.next();
  6946. return this.jsxParseElementAt(startLoc);
  6947. }
  6948. setContext(newContext) {
  6949. const {
  6950. context
  6951. } = this.state;
  6952. context[context.length - 1] = newContext;
  6953. }
  6954. parseExprAtom(refExpressionErrors) {
  6955. if (this.match(139)) {
  6956. return this.parseLiteral(this.state.value, "JSXText");
  6957. } else if (this.match(140)) {
  6958. return this.jsxParseElement();
  6959. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6960. this.replaceToken(140);
  6961. return this.jsxParseElement();
  6962. } else {
  6963. return super.parseExprAtom(refExpressionErrors);
  6964. }
  6965. }
  6966. skipSpace() {
  6967. const curContext = this.curContext();
  6968. if (!curContext.preserveSpace) super.skipSpace();
  6969. }
  6970. getTokenFromCode(code) {
  6971. const context = this.curContext();
  6972. if (context === types.j_expr) {
  6973. return this.jsxReadToken();
  6974. }
  6975. if (context === types.j_oTag || context === types.j_cTag) {
  6976. if (isIdentifierStart(code)) {
  6977. return this.jsxReadWord();
  6978. }
  6979. if (code === 62) {
  6980. ++this.state.pos;
  6981. return this.finishToken(141);
  6982. }
  6983. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6984. return this.jsxReadString(code);
  6985. }
  6986. }
  6987. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6988. ++this.state.pos;
  6989. return this.finishToken(140);
  6990. }
  6991. return super.getTokenFromCode(code);
  6992. }
  6993. updateContext(prevType) {
  6994. const {
  6995. context,
  6996. type
  6997. } = this.state;
  6998. if (type === 56 && prevType === 140) {
  6999. context.splice(-2, 2, types.j_cTag);
  7000. this.state.canStartJSXElement = false;
  7001. } else if (type === 140) {
  7002. context.push(types.j_oTag);
  7003. } else if (type === 141) {
  7004. const out = context[context.length - 1];
  7005. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  7006. context.pop();
  7007. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  7008. } else {
  7009. this.setContext(types.j_expr);
  7010. this.state.canStartJSXElement = true;
  7011. }
  7012. } else {
  7013. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  7014. }
  7015. }
  7016. });
  7017. class TypeScriptScope extends Scope {
  7018. constructor(...args) {
  7019. super(...args);
  7020. this.types = new Set();
  7021. this.enums = new Set();
  7022. this.constEnums = new Set();
  7023. this.classes = new Set();
  7024. this.exportOnlyBindings = new Set();
  7025. }
  7026. }
  7027. class TypeScriptScopeHandler extends ScopeHandler {
  7028. constructor(...args) {
  7029. super(...args);
  7030. this.importsStack = [];
  7031. }
  7032. createScope(flags) {
  7033. this.importsStack.push(new Set());
  7034. return new TypeScriptScope(flags);
  7035. }
  7036. enter(flags) {
  7037. if (flags == SCOPE_TS_MODULE) {
  7038. this.importsStack.push(new Set());
  7039. }
  7040. super.enter(flags);
  7041. }
  7042. exit() {
  7043. const flags = super.exit();
  7044. if (flags == SCOPE_TS_MODULE) {
  7045. this.importsStack.pop();
  7046. }
  7047. return flags;
  7048. }
  7049. hasImport(name, allowShadow) {
  7050. const len = this.importsStack.length;
  7051. if (this.importsStack[len - 1].has(name)) {
  7052. return true;
  7053. }
  7054. if (!allowShadow && len > 1) {
  7055. for (let i = 0; i < len - 1; i++) {
  7056. if (this.importsStack[i].has(name)) return true;
  7057. }
  7058. }
  7059. return false;
  7060. }
  7061. declareName(name, bindingType, loc) {
  7062. if (bindingType & BIND_FLAGS_TS_IMPORT) {
  7063. if (this.hasImport(name, true)) {
  7064. this.parser.raise(Errors.VarRedeclaration, {
  7065. at: loc,
  7066. identifierName: name
  7067. });
  7068. }
  7069. this.importsStack[this.importsStack.length - 1].add(name);
  7070. return;
  7071. }
  7072. const scope = this.currentScope();
  7073. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  7074. this.maybeExportDefined(scope, name);
  7075. scope.exportOnlyBindings.add(name);
  7076. return;
  7077. }
  7078. super.declareName(name, bindingType, loc);
  7079. if (bindingType & BIND_KIND_TYPE) {
  7080. if (!(bindingType & BIND_KIND_VALUE)) {
  7081. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  7082. this.maybeExportDefined(scope, name);
  7083. }
  7084. scope.types.add(name);
  7085. }
  7086. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  7087. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  7088. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  7089. }
  7090. isRedeclaredInScope(scope, name, bindingType) {
  7091. if (scope.enums.has(name)) {
  7092. if (bindingType & BIND_FLAGS_TS_ENUM) {
  7093. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  7094. const wasConst = scope.constEnums.has(name);
  7095. return isConst !== wasConst;
  7096. }
  7097. return true;
  7098. }
  7099. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  7100. if (scope.lexical.has(name)) {
  7101. return !!(bindingType & BIND_KIND_VALUE);
  7102. } else {
  7103. return false;
  7104. }
  7105. }
  7106. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  7107. return true;
  7108. }
  7109. return super.isRedeclaredInScope(scope, name, bindingType);
  7110. }
  7111. checkLocalExport(id) {
  7112. const {
  7113. name
  7114. } = id;
  7115. if (this.hasImport(name)) return;
  7116. const len = this.scopeStack.length;
  7117. for (let i = len - 1; i >= 0; i--) {
  7118. const scope = this.scopeStack[i];
  7119. if (scope.types.has(name) || scope.exportOnlyBindings.has(name)) return;
  7120. }
  7121. super.checkLocalExport(id);
  7122. }
  7123. }
  7124. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7125. function nonNull(x) {
  7126. if (x == null) {
  7127. throw new Error(`Unexpected ${x} value.`);
  7128. }
  7129. return x;
  7130. }
  7131. function assert(x) {
  7132. if (!x) {
  7133. throw new Error("Assert fail");
  7134. }
  7135. }
  7136. const TSErrors = ParseErrorEnum`typescript`({
  7137. AbstractMethodHasImplementation: ({
  7138. methodName
  7139. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7140. AbstractPropertyHasInitializer: ({
  7141. propertyName
  7142. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7143. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7144. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7145. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7146. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7147. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7148. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7149. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7150. DeclareAccessor: ({
  7151. kind
  7152. }) => `'declare' is not allowed in ${kind}ters.`,
  7153. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7154. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7155. DuplicateAccessibilityModifier: ({
  7156. modifier
  7157. }) => `Accessibility modifier already seen.`,
  7158. DuplicateModifier: ({
  7159. modifier
  7160. }) => `Duplicate modifier: '${modifier}'.`,
  7161. EmptyHeritageClauseType: ({
  7162. token
  7163. }) => `'${token}' list cannot be empty.`,
  7164. EmptyTypeArguments: "Type argument list cannot be empty.",
  7165. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7166. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7167. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7168. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7169. IncompatibleModifiers: ({
  7170. modifiers
  7171. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7172. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7173. IndexSignatureHasAccessibility: ({
  7174. modifier
  7175. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7176. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7177. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7178. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7179. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7180. InvalidModifierOnTypeMember: ({
  7181. modifier
  7182. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7183. InvalidModifierOnTypeParameter: ({
  7184. modifier
  7185. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7186. InvalidModifierOnTypeParameterPositions: ({
  7187. modifier
  7188. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7189. InvalidModifiersOrder: ({
  7190. orderedModifiers
  7191. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7192. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7193. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7194. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7195. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  7196. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7197. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7198. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7199. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7200. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7201. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7202. PrivateElementHasAccessibility: ({
  7203. modifier
  7204. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7205. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7206. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7207. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7208. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7209. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7210. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7211. SingleTypeParameterWithoutTrailingComma: ({
  7212. typeParameterName
  7213. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7214. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7215. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7216. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7217. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7218. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7219. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7220. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7221. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7222. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7223. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7224. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7225. UnsupportedSignatureParameterKind: ({
  7226. type
  7227. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7228. });
  7229. function keywordTypeFromName(value) {
  7230. switch (value) {
  7231. case "any":
  7232. return "TSAnyKeyword";
  7233. case "boolean":
  7234. return "TSBooleanKeyword";
  7235. case "bigint":
  7236. return "TSBigIntKeyword";
  7237. case "never":
  7238. return "TSNeverKeyword";
  7239. case "number":
  7240. return "TSNumberKeyword";
  7241. case "object":
  7242. return "TSObjectKeyword";
  7243. case "string":
  7244. return "TSStringKeyword";
  7245. case "symbol":
  7246. return "TSSymbolKeyword";
  7247. case "undefined":
  7248. return "TSUndefinedKeyword";
  7249. case "unknown":
  7250. return "TSUnknownKeyword";
  7251. default:
  7252. return undefined;
  7253. }
  7254. }
  7255. function tsIsAccessModifier(modifier) {
  7256. return modifier === "private" || modifier === "public" || modifier === "protected";
  7257. }
  7258. function tsIsVarianceAnnotations(modifier) {
  7259. return modifier === "in" || modifier === "out";
  7260. }
  7261. var typescript = (superClass => class TypeScriptParserMixin extends superClass {
  7262. getScopeHandler() {
  7263. return TypeScriptScopeHandler;
  7264. }
  7265. tsIsIdentifier() {
  7266. return tokenIsIdentifier(this.state.type);
  7267. }
  7268. tsTokenCanFollowModifier() {
  7269. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(136) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7270. }
  7271. tsNextTokenCanFollowModifier() {
  7272. this.next();
  7273. return this.tsTokenCanFollowModifier();
  7274. }
  7275. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7276. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58) {
  7277. return undefined;
  7278. }
  7279. const modifier = this.state.value;
  7280. if (allowedModifiers.indexOf(modifier) !== -1) {
  7281. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7282. return undefined;
  7283. }
  7284. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7285. return modifier;
  7286. }
  7287. }
  7288. return undefined;
  7289. }
  7290. tsParseModifiers({
  7291. modified,
  7292. allowedModifiers,
  7293. disallowedModifiers,
  7294. stopOnStartOfClassStaticBlock,
  7295. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7296. }) {
  7297. const enforceOrder = (loc, modifier, before, after) => {
  7298. if (modifier === before && modified[after]) {
  7299. this.raise(TSErrors.InvalidModifiersOrder, {
  7300. at: loc,
  7301. orderedModifiers: [before, after]
  7302. });
  7303. }
  7304. };
  7305. const incompatible = (loc, modifier, mod1, mod2) => {
  7306. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7307. this.raise(TSErrors.IncompatibleModifiers, {
  7308. at: loc,
  7309. modifiers: [mod1, mod2]
  7310. });
  7311. }
  7312. };
  7313. for (;;) {
  7314. const {
  7315. startLoc
  7316. } = this.state;
  7317. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7318. if (!modifier) break;
  7319. if (tsIsAccessModifier(modifier)) {
  7320. if (modified.accessibility) {
  7321. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7322. at: startLoc,
  7323. modifier
  7324. });
  7325. } else {
  7326. enforceOrder(startLoc, modifier, modifier, "override");
  7327. enforceOrder(startLoc, modifier, modifier, "static");
  7328. enforceOrder(startLoc, modifier, modifier, "readonly");
  7329. modified.accessibility = modifier;
  7330. }
  7331. } else if (tsIsVarianceAnnotations(modifier)) {
  7332. if (modified[modifier]) {
  7333. this.raise(TSErrors.DuplicateModifier, {
  7334. at: startLoc,
  7335. modifier
  7336. });
  7337. }
  7338. modified[modifier] = true;
  7339. enforceOrder(startLoc, modifier, "in", "out");
  7340. } else {
  7341. if (Object.hasOwnProperty.call(modified, modifier)) {
  7342. this.raise(TSErrors.DuplicateModifier, {
  7343. at: startLoc,
  7344. modifier
  7345. });
  7346. } else {
  7347. enforceOrder(startLoc, modifier, "static", "readonly");
  7348. enforceOrder(startLoc, modifier, "static", "override");
  7349. enforceOrder(startLoc, modifier, "override", "readonly");
  7350. enforceOrder(startLoc, modifier, "abstract", "override");
  7351. incompatible(startLoc, modifier, "declare", "override");
  7352. incompatible(startLoc, modifier, "static", "abstract");
  7353. }
  7354. modified[modifier] = true;
  7355. }
  7356. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7357. this.raise(errorTemplate, {
  7358. at: startLoc,
  7359. modifier
  7360. });
  7361. }
  7362. }
  7363. }
  7364. tsIsListTerminator(kind) {
  7365. switch (kind) {
  7366. case "EnumMembers":
  7367. case "TypeMembers":
  7368. return this.match(8);
  7369. case "HeritageClauseElement":
  7370. return this.match(5);
  7371. case "TupleElementTypes":
  7372. return this.match(3);
  7373. case "TypeParametersOrArguments":
  7374. return this.match(48);
  7375. }
  7376. throw new Error("Unreachable");
  7377. }
  7378. tsParseList(kind, parseElement) {
  7379. const result = [];
  7380. while (!this.tsIsListTerminator(kind)) {
  7381. result.push(parseElement());
  7382. }
  7383. return result;
  7384. }
  7385. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7386. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7387. }
  7388. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7389. const result = [];
  7390. let trailingCommaPos = -1;
  7391. for (;;) {
  7392. if (this.tsIsListTerminator(kind)) {
  7393. break;
  7394. }
  7395. trailingCommaPos = -1;
  7396. const element = parseElement();
  7397. if (element == null) {
  7398. return undefined;
  7399. }
  7400. result.push(element);
  7401. if (this.eat(12)) {
  7402. trailingCommaPos = this.state.lastTokStart;
  7403. continue;
  7404. }
  7405. if (this.tsIsListTerminator(kind)) {
  7406. break;
  7407. }
  7408. if (expectSuccess) {
  7409. this.expect(12);
  7410. }
  7411. return undefined;
  7412. }
  7413. if (refTrailingCommaPos) {
  7414. refTrailingCommaPos.value = trailingCommaPos;
  7415. }
  7416. return result;
  7417. }
  7418. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7419. if (!skipFirstToken) {
  7420. if (bracket) {
  7421. this.expect(0);
  7422. } else {
  7423. this.expect(47);
  7424. }
  7425. }
  7426. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7427. if (bracket) {
  7428. this.expect(3);
  7429. } else {
  7430. this.expect(48);
  7431. }
  7432. return result;
  7433. }
  7434. tsParseImportType() {
  7435. const node = this.startNode();
  7436. this.expect(83);
  7437. this.expect(10);
  7438. if (!this.match(131)) {
  7439. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7440. at: this.state.startLoc
  7441. });
  7442. }
  7443. node.argument = super.parseExprAtom();
  7444. this.expect(11);
  7445. if (this.eat(16)) {
  7446. node.qualifier = this.tsParseEntityName();
  7447. }
  7448. if (this.match(47)) {
  7449. node.typeParameters = this.tsParseTypeArguments();
  7450. }
  7451. return this.finishNode(node, "TSImportType");
  7452. }
  7453. tsParseEntityName(allowReservedWords = true) {
  7454. let entity = this.parseIdentifier(allowReservedWords);
  7455. while (this.eat(16)) {
  7456. const node = this.startNodeAtNode(entity);
  7457. node.left = entity;
  7458. node.right = this.parseIdentifier(allowReservedWords);
  7459. entity = this.finishNode(node, "TSQualifiedName");
  7460. }
  7461. return entity;
  7462. }
  7463. tsParseTypeReference() {
  7464. const node = this.startNode();
  7465. node.typeName = this.tsParseEntityName();
  7466. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7467. node.typeParameters = this.tsParseTypeArguments();
  7468. }
  7469. return this.finishNode(node, "TSTypeReference");
  7470. }
  7471. tsParseThisTypePredicate(lhs) {
  7472. this.next();
  7473. const node = this.startNodeAtNode(lhs);
  7474. node.parameterName = lhs;
  7475. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7476. node.asserts = false;
  7477. return this.finishNode(node, "TSTypePredicate");
  7478. }
  7479. tsParseThisTypeNode() {
  7480. const node = this.startNode();
  7481. this.next();
  7482. return this.finishNode(node, "TSThisType");
  7483. }
  7484. tsParseTypeQuery() {
  7485. const node = this.startNode();
  7486. this.expect(87);
  7487. if (this.match(83)) {
  7488. node.exprName = this.tsParseImportType();
  7489. } else {
  7490. node.exprName = this.tsParseEntityName();
  7491. }
  7492. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7493. node.typeParameters = this.tsParseTypeArguments();
  7494. }
  7495. return this.finishNode(node, "TSTypeQuery");
  7496. }
  7497. tsParseInOutModifiers(node) {
  7498. this.tsParseModifiers({
  7499. modified: node,
  7500. allowedModifiers: ["in", "out"],
  7501. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7502. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7503. });
  7504. }
  7505. tsParseNoneModifiers(node) {
  7506. this.tsParseModifiers({
  7507. modified: node,
  7508. allowedModifiers: [],
  7509. disallowedModifiers: ["in", "out"],
  7510. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7511. });
  7512. }
  7513. tsParseTypeParameter(parseModifiers = this.tsParseNoneModifiers.bind(this)) {
  7514. const node = this.startNode();
  7515. parseModifiers(node);
  7516. node.name = this.tsParseTypeParameterName();
  7517. node.constraint = this.tsEatThenParseType(81);
  7518. node.default = this.tsEatThenParseType(29);
  7519. return this.finishNode(node, "TSTypeParameter");
  7520. }
  7521. tsTryParseTypeParameters(parseModifiers) {
  7522. if (this.match(47)) {
  7523. return this.tsParseTypeParameters(parseModifiers);
  7524. }
  7525. }
  7526. tsParseTypeParameters(parseModifiers) {
  7527. const node = this.startNode();
  7528. if (this.match(47) || this.match(140)) {
  7529. this.next();
  7530. } else {
  7531. this.unexpected();
  7532. }
  7533. const refTrailingCommaPos = {
  7534. value: -1
  7535. };
  7536. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7537. if (node.params.length === 0) {
  7538. this.raise(TSErrors.EmptyTypeParameters, {
  7539. at: node
  7540. });
  7541. }
  7542. if (refTrailingCommaPos.value !== -1) {
  7543. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7544. }
  7545. return this.finishNode(node, "TSTypeParameterDeclaration");
  7546. }
  7547. tsFillSignature(returnToken, signature) {
  7548. const returnTokenRequired = returnToken === 19;
  7549. const paramsKey = "parameters";
  7550. const returnTypeKey = "typeAnnotation";
  7551. signature.typeParameters = this.tsTryParseTypeParameters();
  7552. this.expect(10);
  7553. signature[paramsKey] = this.tsParseBindingListForSignature();
  7554. if (returnTokenRequired) {
  7555. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7556. } else if (this.match(returnToken)) {
  7557. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7558. }
  7559. }
  7560. tsParseBindingListForSignature() {
  7561. return super.parseBindingList(11, 41).map(pattern => {
  7562. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7563. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7564. at: pattern,
  7565. type: pattern.type
  7566. });
  7567. }
  7568. return pattern;
  7569. });
  7570. }
  7571. tsParseTypeMemberSemicolon() {
  7572. if (!this.eat(12) && !this.isLineTerminator()) {
  7573. this.expect(13);
  7574. }
  7575. }
  7576. tsParseSignatureMember(kind, node) {
  7577. this.tsFillSignature(14, node);
  7578. this.tsParseTypeMemberSemicolon();
  7579. return this.finishNode(node, kind);
  7580. }
  7581. tsIsUnambiguouslyIndexSignature() {
  7582. this.next();
  7583. if (tokenIsIdentifier(this.state.type)) {
  7584. this.next();
  7585. return this.match(14);
  7586. }
  7587. return false;
  7588. }
  7589. tsTryParseIndexSignature(node) {
  7590. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7591. return undefined;
  7592. }
  7593. this.expect(0);
  7594. const id = this.parseIdentifier();
  7595. id.typeAnnotation = this.tsParseTypeAnnotation();
  7596. this.resetEndLocation(id);
  7597. this.expect(3);
  7598. node.parameters = [id];
  7599. const type = this.tsTryParseTypeAnnotation();
  7600. if (type) node.typeAnnotation = type;
  7601. this.tsParseTypeMemberSemicolon();
  7602. return this.finishNode(node, "TSIndexSignature");
  7603. }
  7604. tsParsePropertyOrMethodSignature(node, readonly) {
  7605. if (this.eat(17)) node.optional = true;
  7606. const nodeAny = node;
  7607. if (this.match(10) || this.match(47)) {
  7608. if (readonly) {
  7609. this.raise(TSErrors.ReadonlyForMethodSignature, {
  7610. at: node
  7611. });
  7612. }
  7613. const method = nodeAny;
  7614. if (method.kind && this.match(47)) {
  7615. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  7616. at: this.state.curPosition()
  7617. });
  7618. }
  7619. this.tsFillSignature(14, method);
  7620. this.tsParseTypeMemberSemicolon();
  7621. const paramsKey = "parameters";
  7622. const returnTypeKey = "typeAnnotation";
  7623. if (method.kind === "get") {
  7624. if (method[paramsKey].length > 0) {
  7625. this.raise(Errors.BadGetterArity, {
  7626. at: this.state.curPosition()
  7627. });
  7628. if (this.isThisParam(method[paramsKey][0])) {
  7629. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7630. at: this.state.curPosition()
  7631. });
  7632. }
  7633. }
  7634. } else if (method.kind === "set") {
  7635. if (method[paramsKey].length !== 1) {
  7636. this.raise(Errors.BadSetterArity, {
  7637. at: this.state.curPosition()
  7638. });
  7639. } else {
  7640. const firstParameter = method[paramsKey][0];
  7641. if (this.isThisParam(firstParameter)) {
  7642. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7643. at: this.state.curPosition()
  7644. });
  7645. }
  7646. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7647. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  7648. at: this.state.curPosition()
  7649. });
  7650. }
  7651. if (firstParameter.type === "RestElement") {
  7652. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  7653. at: this.state.curPosition()
  7654. });
  7655. }
  7656. }
  7657. if (method[returnTypeKey]) {
  7658. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  7659. at: method[returnTypeKey]
  7660. });
  7661. }
  7662. } else {
  7663. method.kind = "method";
  7664. }
  7665. return this.finishNode(method, "TSMethodSignature");
  7666. } else {
  7667. const property = nodeAny;
  7668. if (readonly) property.readonly = true;
  7669. const type = this.tsTryParseTypeAnnotation();
  7670. if (type) property.typeAnnotation = type;
  7671. this.tsParseTypeMemberSemicolon();
  7672. return this.finishNode(property, "TSPropertySignature");
  7673. }
  7674. }
  7675. tsParseTypeMember() {
  7676. const node = this.startNode();
  7677. if (this.match(10) || this.match(47)) {
  7678. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7679. }
  7680. if (this.match(77)) {
  7681. const id = this.startNode();
  7682. this.next();
  7683. if (this.match(10) || this.match(47)) {
  7684. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7685. } else {
  7686. node.key = this.createIdentifier(id, "new");
  7687. return this.tsParsePropertyOrMethodSignature(node, false);
  7688. }
  7689. }
  7690. this.tsParseModifiers({
  7691. modified: node,
  7692. allowedModifiers: ["readonly"],
  7693. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  7694. });
  7695. const idx = this.tsTryParseIndexSignature(node);
  7696. if (idx) {
  7697. return idx;
  7698. }
  7699. super.parsePropertyName(node);
  7700. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7701. node.kind = node.key.name;
  7702. super.parsePropertyName(node);
  7703. }
  7704. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7705. }
  7706. tsParseTypeLiteral() {
  7707. const node = this.startNode();
  7708. node.members = this.tsParseObjectTypeMembers();
  7709. return this.finishNode(node, "TSTypeLiteral");
  7710. }
  7711. tsParseObjectTypeMembers() {
  7712. this.expect(5);
  7713. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7714. this.expect(8);
  7715. return members;
  7716. }
  7717. tsIsStartOfMappedType() {
  7718. this.next();
  7719. if (this.eat(53)) {
  7720. return this.isContextual(120);
  7721. }
  7722. if (this.isContextual(120)) {
  7723. this.next();
  7724. }
  7725. if (!this.match(0)) {
  7726. return false;
  7727. }
  7728. this.next();
  7729. if (!this.tsIsIdentifier()) {
  7730. return false;
  7731. }
  7732. this.next();
  7733. return this.match(58);
  7734. }
  7735. tsParseMappedTypeParameter() {
  7736. const node = this.startNode();
  7737. node.name = this.tsParseTypeParameterName();
  7738. node.constraint = this.tsExpectThenParseType(58);
  7739. return this.finishNode(node, "TSTypeParameter");
  7740. }
  7741. tsParseMappedType() {
  7742. const node = this.startNode();
  7743. this.expect(5);
  7744. if (this.match(53)) {
  7745. node.readonly = this.state.value;
  7746. this.next();
  7747. this.expectContextual(120);
  7748. } else if (this.eatContextual(120)) {
  7749. node.readonly = true;
  7750. }
  7751. this.expect(0);
  7752. node.typeParameter = this.tsParseMappedTypeParameter();
  7753. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  7754. this.expect(3);
  7755. if (this.match(53)) {
  7756. node.optional = this.state.value;
  7757. this.next();
  7758. this.expect(17);
  7759. } else if (this.eat(17)) {
  7760. node.optional = true;
  7761. }
  7762. node.typeAnnotation = this.tsTryParseType();
  7763. this.semicolon();
  7764. this.expect(8);
  7765. return this.finishNode(node, "TSMappedType");
  7766. }
  7767. tsParseTupleType() {
  7768. const node = this.startNode();
  7769. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7770. let seenOptionalElement = false;
  7771. let labeledElements = null;
  7772. node.elementTypes.forEach(elementNode => {
  7773. var _labeledElements;
  7774. const {
  7775. type
  7776. } = elementNode;
  7777. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  7778. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  7779. at: elementNode
  7780. });
  7781. }
  7782. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  7783. let checkType = type;
  7784. if (type === "TSRestType") {
  7785. elementNode = elementNode.typeAnnotation;
  7786. checkType = elementNode.type;
  7787. }
  7788. const isLabeled = checkType === "TSNamedTupleMember";
  7789. (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
  7790. if (labeledElements !== isLabeled) {
  7791. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  7792. at: elementNode
  7793. });
  7794. }
  7795. });
  7796. return this.finishNode(node, "TSTupleType");
  7797. }
  7798. tsParseTupleElementType() {
  7799. const {
  7800. startLoc
  7801. } = this.state;
  7802. const rest = this.eat(21);
  7803. let type = this.tsParseType();
  7804. const optional = this.eat(17);
  7805. const labeled = this.eat(14);
  7806. if (labeled) {
  7807. const labeledNode = this.startNodeAtNode(type);
  7808. labeledNode.optional = optional;
  7809. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  7810. labeledNode.label = type.typeName;
  7811. } else {
  7812. this.raise(TSErrors.InvalidTupleMemberLabel, {
  7813. at: type
  7814. });
  7815. labeledNode.label = type;
  7816. }
  7817. labeledNode.elementType = this.tsParseType();
  7818. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  7819. } else if (optional) {
  7820. const optionalTypeNode = this.startNodeAtNode(type);
  7821. optionalTypeNode.typeAnnotation = type;
  7822. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  7823. }
  7824. if (rest) {
  7825. const restNode = this.startNodeAt(startLoc);
  7826. restNode.typeAnnotation = type;
  7827. type = this.finishNode(restNode, "TSRestType");
  7828. }
  7829. return type;
  7830. }
  7831. tsParseParenthesizedType() {
  7832. const node = this.startNode();
  7833. this.expect(10);
  7834. node.typeAnnotation = this.tsParseType();
  7835. this.expect(11);
  7836. return this.finishNode(node, "TSParenthesizedType");
  7837. }
  7838. tsParseFunctionOrConstructorType(type, abstract) {
  7839. const node = this.startNode();
  7840. if (type === "TSConstructorType") {
  7841. node.abstract = !!abstract;
  7842. if (abstract) this.next();
  7843. this.next();
  7844. }
  7845. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  7846. return this.finishNode(node, type);
  7847. }
  7848. tsParseLiteralTypeNode() {
  7849. const node = this.startNode();
  7850. node.literal = (() => {
  7851. switch (this.state.type) {
  7852. case 132:
  7853. case 133:
  7854. case 131:
  7855. case 85:
  7856. case 86:
  7857. return super.parseExprAtom();
  7858. default:
  7859. throw this.unexpected();
  7860. }
  7861. })();
  7862. return this.finishNode(node, "TSLiteralType");
  7863. }
  7864. tsParseTemplateLiteralType() {
  7865. const node = this.startNode();
  7866. node.literal = super.parseTemplate(false);
  7867. return this.finishNode(node, "TSLiteralType");
  7868. }
  7869. parseTemplateSubstitution() {
  7870. if (this.state.inType) return this.tsParseType();
  7871. return super.parseTemplateSubstitution();
  7872. }
  7873. tsParseThisTypeOrThisTypePredicate() {
  7874. const thisKeyword = this.tsParseThisTypeNode();
  7875. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  7876. return this.tsParseThisTypePredicate(thisKeyword);
  7877. } else {
  7878. return thisKeyword;
  7879. }
  7880. }
  7881. tsParseNonArrayType() {
  7882. switch (this.state.type) {
  7883. case 131:
  7884. case 132:
  7885. case 133:
  7886. case 85:
  7887. case 86:
  7888. return this.tsParseLiteralTypeNode();
  7889. case 53:
  7890. if (this.state.value === "-") {
  7891. const node = this.startNode();
  7892. const nextToken = this.lookahead();
  7893. if (nextToken.type !== 132 && nextToken.type !== 133) {
  7894. throw this.unexpected();
  7895. }
  7896. node.literal = this.parseMaybeUnary();
  7897. return this.finishNode(node, "TSLiteralType");
  7898. }
  7899. break;
  7900. case 78:
  7901. return this.tsParseThisTypeOrThisTypePredicate();
  7902. case 87:
  7903. return this.tsParseTypeQuery();
  7904. case 83:
  7905. return this.tsParseImportType();
  7906. case 5:
  7907. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7908. case 0:
  7909. return this.tsParseTupleType();
  7910. case 10:
  7911. return this.tsParseParenthesizedType();
  7912. case 25:
  7913. case 24:
  7914. return this.tsParseTemplateLiteralType();
  7915. default:
  7916. {
  7917. const {
  7918. type
  7919. } = this.state;
  7920. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  7921. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7922. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  7923. const node = this.startNode();
  7924. this.next();
  7925. return this.finishNode(node, nodeType);
  7926. }
  7927. return this.tsParseTypeReference();
  7928. }
  7929. }
  7930. }
  7931. throw this.unexpected();
  7932. }
  7933. tsParseArrayTypeOrHigher() {
  7934. let type = this.tsParseNonArrayType();
  7935. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  7936. if (this.match(3)) {
  7937. const node = this.startNodeAtNode(type);
  7938. node.elementType = type;
  7939. this.expect(3);
  7940. type = this.finishNode(node, "TSArrayType");
  7941. } else {
  7942. const node = this.startNodeAtNode(type);
  7943. node.objectType = type;
  7944. node.indexType = this.tsParseType();
  7945. this.expect(3);
  7946. type = this.finishNode(node, "TSIndexedAccessType");
  7947. }
  7948. }
  7949. return type;
  7950. }
  7951. tsParseTypeOperator() {
  7952. const node = this.startNode();
  7953. const operator = this.state.value;
  7954. this.next();
  7955. node.operator = operator;
  7956. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7957. if (operator === "readonly") {
  7958. this.tsCheckTypeAnnotationForReadOnly(node);
  7959. }
  7960. return this.finishNode(node, "TSTypeOperator");
  7961. }
  7962. tsCheckTypeAnnotationForReadOnly(node) {
  7963. switch (node.typeAnnotation.type) {
  7964. case "TSTupleType":
  7965. case "TSArrayType":
  7966. return;
  7967. default:
  7968. this.raise(TSErrors.UnexpectedReadonly, {
  7969. at: node
  7970. });
  7971. }
  7972. }
  7973. tsParseInferType() {
  7974. const node = this.startNode();
  7975. this.expectContextual(113);
  7976. const typeParameter = this.startNode();
  7977. typeParameter.name = this.tsParseTypeParameterName();
  7978. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  7979. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7980. return this.finishNode(node, "TSInferType");
  7981. }
  7982. tsParseConstraintForInferType() {
  7983. if (this.eat(81)) {
  7984. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  7985. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  7986. return constraint;
  7987. }
  7988. }
  7989. }
  7990. tsParseTypeOperatorOrHigher() {
  7991. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  7992. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(113) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  7993. }
  7994. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7995. const node = this.startNode();
  7996. const hasLeadingOperator = this.eat(operator);
  7997. const types = [];
  7998. do {
  7999. types.push(parseConstituentType());
  8000. } while (this.eat(operator));
  8001. if (types.length === 1 && !hasLeadingOperator) {
  8002. return types[0];
  8003. }
  8004. node.types = types;
  8005. return this.finishNode(node, kind);
  8006. }
  8007. tsParseIntersectionTypeOrHigher() {
  8008. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8009. }
  8010. tsParseUnionTypeOrHigher() {
  8011. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8012. }
  8013. tsIsStartOfFunctionType() {
  8014. if (this.match(47)) {
  8015. return true;
  8016. }
  8017. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8018. }
  8019. tsSkipParameterStart() {
  8020. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8021. this.next();
  8022. return true;
  8023. }
  8024. if (this.match(5)) {
  8025. const {
  8026. errors
  8027. } = this.state;
  8028. const previousErrorCount = errors.length;
  8029. try {
  8030. this.parseObjectLike(8, true);
  8031. return errors.length === previousErrorCount;
  8032. } catch (_unused) {
  8033. return false;
  8034. }
  8035. }
  8036. if (this.match(0)) {
  8037. this.next();
  8038. const {
  8039. errors
  8040. } = this.state;
  8041. const previousErrorCount = errors.length;
  8042. try {
  8043. super.parseBindingList(3, 93, true);
  8044. return errors.length === previousErrorCount;
  8045. } catch (_unused2) {
  8046. return false;
  8047. }
  8048. }
  8049. return false;
  8050. }
  8051. tsIsUnambiguouslyStartOfFunctionType() {
  8052. this.next();
  8053. if (this.match(11) || this.match(21)) {
  8054. return true;
  8055. }
  8056. if (this.tsSkipParameterStart()) {
  8057. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8058. return true;
  8059. }
  8060. if (this.match(11)) {
  8061. this.next();
  8062. if (this.match(19)) {
  8063. return true;
  8064. }
  8065. }
  8066. }
  8067. return false;
  8068. }
  8069. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8070. return this.tsInType(() => {
  8071. const t = this.startNode();
  8072. this.expect(returnToken);
  8073. const node = this.startNode();
  8074. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8075. if (asserts && this.match(78)) {
  8076. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8077. if (thisTypePredicate.type === "TSThisType") {
  8078. node.parameterName = thisTypePredicate;
  8079. node.asserts = true;
  8080. node.typeAnnotation = null;
  8081. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8082. } else {
  8083. this.resetStartLocationFromNode(thisTypePredicate, node);
  8084. thisTypePredicate.asserts = true;
  8085. }
  8086. t.typeAnnotation = thisTypePredicate;
  8087. return this.finishNode(t, "TSTypeAnnotation");
  8088. }
  8089. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8090. if (!typePredicateVariable) {
  8091. if (!asserts) {
  8092. return this.tsParseTypeAnnotation(false, t);
  8093. }
  8094. node.parameterName = this.parseIdentifier();
  8095. node.asserts = asserts;
  8096. node.typeAnnotation = null;
  8097. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8098. return this.finishNode(t, "TSTypeAnnotation");
  8099. }
  8100. const type = this.tsParseTypeAnnotation(false);
  8101. node.parameterName = typePredicateVariable;
  8102. node.typeAnnotation = type;
  8103. node.asserts = asserts;
  8104. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8105. return this.finishNode(t, "TSTypeAnnotation");
  8106. });
  8107. }
  8108. tsTryParseTypeOrTypePredicateAnnotation() {
  8109. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  8110. }
  8111. tsTryParseTypeAnnotation() {
  8112. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  8113. }
  8114. tsTryParseType() {
  8115. return this.tsEatThenParseType(14);
  8116. }
  8117. tsParseTypePredicatePrefix() {
  8118. const id = this.parseIdentifier();
  8119. if (this.isContextual(114) && !this.hasPrecedingLineBreak()) {
  8120. this.next();
  8121. return id;
  8122. }
  8123. }
  8124. tsParseTypePredicateAsserts() {
  8125. if (this.state.type !== 107) {
  8126. return false;
  8127. }
  8128. const containsEsc = this.state.containsEsc;
  8129. this.next();
  8130. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8131. return false;
  8132. }
  8133. if (containsEsc) {
  8134. this.raise(Errors.InvalidEscapedReservedWord, {
  8135. at: this.state.lastTokStartLoc,
  8136. reservedWord: "asserts"
  8137. });
  8138. }
  8139. return true;
  8140. }
  8141. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8142. this.tsInType(() => {
  8143. if (eatColon) this.expect(14);
  8144. t.typeAnnotation = this.tsParseType();
  8145. });
  8146. return this.finishNode(t, "TSTypeAnnotation");
  8147. }
  8148. tsParseType() {
  8149. assert(this.state.inType);
  8150. const type = this.tsParseNonConditionalType();
  8151. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8152. return type;
  8153. }
  8154. const node = this.startNodeAtNode(type);
  8155. node.checkType = type;
  8156. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8157. this.expect(17);
  8158. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8159. this.expect(14);
  8160. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8161. return this.finishNode(node, "TSConditionalType");
  8162. }
  8163. isAbstractConstructorSignature() {
  8164. return this.isContextual(122) && this.lookahead().type === 77;
  8165. }
  8166. tsParseNonConditionalType() {
  8167. if (this.tsIsStartOfFunctionType()) {
  8168. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8169. }
  8170. if (this.match(77)) {
  8171. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8172. } else if (this.isAbstractConstructorSignature()) {
  8173. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8174. }
  8175. return this.tsParseUnionTypeOrHigher();
  8176. }
  8177. tsParseTypeAssertion() {
  8178. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8179. this.raise(TSErrors.ReservedTypeAssertion, {
  8180. at: this.state.startLoc
  8181. });
  8182. }
  8183. const node = this.startNode();
  8184. node.typeAnnotation = this.tsInType(() => {
  8185. this.next();
  8186. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8187. });
  8188. this.expect(48);
  8189. node.expression = this.parseMaybeUnary();
  8190. return this.finishNode(node, "TSTypeAssertion");
  8191. }
  8192. tsParseHeritageClause(token) {
  8193. const originalStartLoc = this.state.startLoc;
  8194. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8195. const node = this.startNode();
  8196. node.expression = this.tsParseEntityName();
  8197. if (this.match(47)) {
  8198. node.typeParameters = this.tsParseTypeArguments();
  8199. }
  8200. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8201. });
  8202. if (!delimitedList.length) {
  8203. this.raise(TSErrors.EmptyHeritageClauseType, {
  8204. at: originalStartLoc,
  8205. token
  8206. });
  8207. }
  8208. return delimitedList;
  8209. }
  8210. tsParseInterfaceDeclaration(node, properties = {}) {
  8211. if (this.hasFollowingLineBreak()) return null;
  8212. this.expectContextual(127);
  8213. if (properties.declare) node.declare = true;
  8214. if (tokenIsIdentifier(this.state.type)) {
  8215. node.id = this.parseIdentifier();
  8216. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  8217. } else {
  8218. node.id = null;
  8219. this.raise(TSErrors.MissingInterfaceName, {
  8220. at: this.state.startLoc
  8221. });
  8222. }
  8223. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  8224. if (this.eat(81)) {
  8225. node.extends = this.tsParseHeritageClause("extends");
  8226. }
  8227. const body = this.startNode();
  8228. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8229. node.body = this.finishNode(body, "TSInterfaceBody");
  8230. return this.finishNode(node, "TSInterfaceDeclaration");
  8231. }
  8232. tsParseTypeAliasDeclaration(node) {
  8233. node.id = this.parseIdentifier();
  8234. this.checkIdentifier(node.id, BIND_TS_TYPE);
  8235. node.typeAnnotation = this.tsInType(() => {
  8236. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  8237. this.expect(29);
  8238. if (this.isContextual(112) && this.lookahead().type !== 16) {
  8239. const node = this.startNode();
  8240. this.next();
  8241. return this.finishNode(node, "TSIntrinsicKeyword");
  8242. }
  8243. return this.tsParseType();
  8244. });
  8245. this.semicolon();
  8246. return this.finishNode(node, "TSTypeAliasDeclaration");
  8247. }
  8248. tsInNoContext(cb) {
  8249. const oldContext = this.state.context;
  8250. this.state.context = [oldContext[0]];
  8251. try {
  8252. return cb();
  8253. } finally {
  8254. this.state.context = oldContext;
  8255. }
  8256. }
  8257. tsInType(cb) {
  8258. const oldInType = this.state.inType;
  8259. this.state.inType = true;
  8260. try {
  8261. return cb();
  8262. } finally {
  8263. this.state.inType = oldInType;
  8264. }
  8265. }
  8266. tsInDisallowConditionalTypesContext(cb) {
  8267. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8268. this.state.inDisallowConditionalTypesContext = true;
  8269. try {
  8270. return cb();
  8271. } finally {
  8272. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8273. }
  8274. }
  8275. tsInAllowConditionalTypesContext(cb) {
  8276. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8277. this.state.inDisallowConditionalTypesContext = false;
  8278. try {
  8279. return cb();
  8280. } finally {
  8281. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8282. }
  8283. }
  8284. tsEatThenParseType(token) {
  8285. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8286. }
  8287. tsExpectThenParseType(token) {
  8288. return this.tsDoThenParseType(() => this.expect(token));
  8289. }
  8290. tsNextThenParseType() {
  8291. return this.tsDoThenParseType(() => this.next());
  8292. }
  8293. tsDoThenParseType(cb) {
  8294. return this.tsInType(() => {
  8295. cb();
  8296. return this.tsParseType();
  8297. });
  8298. }
  8299. tsParseEnumMember() {
  8300. const node = this.startNode();
  8301. node.id = this.match(131) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8302. if (this.eat(29)) {
  8303. node.initializer = super.parseMaybeAssignAllowIn();
  8304. }
  8305. return this.finishNode(node, "TSEnumMember");
  8306. }
  8307. tsParseEnumDeclaration(node, properties = {}) {
  8308. if (properties.const) node.const = true;
  8309. if (properties.declare) node.declare = true;
  8310. this.expectContextual(124);
  8311. node.id = this.parseIdentifier();
  8312. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8313. this.expect(5);
  8314. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8315. this.expect(8);
  8316. return this.finishNode(node, "TSEnumDeclaration");
  8317. }
  8318. tsParseModuleBlock() {
  8319. const node = this.startNode();
  8320. this.scope.enter(SCOPE_OTHER);
  8321. this.expect(5);
  8322. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8323. this.scope.exit();
  8324. return this.finishNode(node, "TSModuleBlock");
  8325. }
  8326. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8327. node.id = this.parseIdentifier();
  8328. if (!nested) {
  8329. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8330. }
  8331. if (this.eat(16)) {
  8332. const inner = this.startNode();
  8333. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8334. node.body = inner;
  8335. } else {
  8336. this.scope.enter(SCOPE_TS_MODULE);
  8337. this.prodParam.enter(PARAM);
  8338. node.body = this.tsParseModuleBlock();
  8339. this.prodParam.exit();
  8340. this.scope.exit();
  8341. }
  8342. return this.finishNode(node, "TSModuleDeclaration");
  8343. }
  8344. tsParseAmbientExternalModuleDeclaration(node) {
  8345. if (this.isContextual(110)) {
  8346. node.global = true;
  8347. node.id = this.parseIdentifier();
  8348. } else if (this.match(131)) {
  8349. node.id = super.parseStringLiteral(this.state.value);
  8350. } else {
  8351. this.unexpected();
  8352. }
  8353. if (this.match(5)) {
  8354. this.scope.enter(SCOPE_TS_MODULE);
  8355. this.prodParam.enter(PARAM);
  8356. node.body = this.tsParseModuleBlock();
  8357. this.prodParam.exit();
  8358. this.scope.exit();
  8359. } else {
  8360. this.semicolon();
  8361. }
  8362. return this.finishNode(node, "TSModuleDeclaration");
  8363. }
  8364. tsParseImportEqualsDeclaration(node, isExport) {
  8365. node.isExport = isExport || false;
  8366. node.id = this.parseIdentifier();
  8367. this.checkIdentifier(node.id, BIND_FLAGS_TS_IMPORT);
  8368. this.expect(29);
  8369. const moduleReference = this.tsParseModuleReference();
  8370. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8371. this.raise(TSErrors.ImportAliasHasImportType, {
  8372. at: moduleReference
  8373. });
  8374. }
  8375. node.moduleReference = moduleReference;
  8376. this.semicolon();
  8377. return this.finishNode(node, "TSImportEqualsDeclaration");
  8378. }
  8379. tsIsExternalModuleReference() {
  8380. return this.isContextual(117) && this.lookaheadCharCode() === 40;
  8381. }
  8382. tsParseModuleReference() {
  8383. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8384. }
  8385. tsParseExternalModuleReference() {
  8386. const node = this.startNode();
  8387. this.expectContextual(117);
  8388. this.expect(10);
  8389. if (!this.match(131)) {
  8390. throw this.unexpected();
  8391. }
  8392. node.expression = super.parseExprAtom();
  8393. this.expect(11);
  8394. return this.finishNode(node, "TSExternalModuleReference");
  8395. }
  8396. tsLookAhead(f) {
  8397. const state = this.state.clone();
  8398. const res = f();
  8399. this.state = state;
  8400. return res;
  8401. }
  8402. tsTryParseAndCatch(f) {
  8403. const result = this.tryParse(abort => f() || abort());
  8404. if (result.aborted || !result.node) return undefined;
  8405. if (result.error) this.state = result.failState;
  8406. return result.node;
  8407. }
  8408. tsTryParse(f) {
  8409. const state = this.state.clone();
  8410. const result = f();
  8411. if (result !== undefined && result !== false) {
  8412. return result;
  8413. } else {
  8414. this.state = state;
  8415. return undefined;
  8416. }
  8417. }
  8418. tsTryParseDeclare(nany) {
  8419. if (this.isLineTerminator()) {
  8420. return;
  8421. }
  8422. let starttype = this.state.type;
  8423. let kind;
  8424. if (this.isContextual(99)) {
  8425. starttype = 74;
  8426. kind = "let";
  8427. }
  8428. return this.tsInAmbientContext(() => {
  8429. if (starttype === 68) {
  8430. nany.declare = true;
  8431. return super.parseFunctionStatement(nany, false, false);
  8432. }
  8433. if (starttype === 80) {
  8434. nany.declare = true;
  8435. return this.parseClass(nany, true, false);
  8436. }
  8437. if (starttype === 124) {
  8438. return this.tsParseEnumDeclaration(nany, {
  8439. declare: true
  8440. });
  8441. }
  8442. if (starttype === 110) {
  8443. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8444. }
  8445. if (starttype === 75 || starttype === 74) {
  8446. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8447. nany.declare = true;
  8448. return this.parseVarStatement(nany, kind || this.state.value, true);
  8449. }
  8450. this.expect(75);
  8451. return this.tsParseEnumDeclaration(nany, {
  8452. const: true,
  8453. declare: true
  8454. });
  8455. }
  8456. if (starttype === 127) {
  8457. const result = this.tsParseInterfaceDeclaration(nany, {
  8458. declare: true
  8459. });
  8460. if (result) return result;
  8461. }
  8462. if (tokenIsIdentifier(starttype)) {
  8463. return this.tsParseDeclaration(nany, this.state.value, true, null);
  8464. }
  8465. });
  8466. }
  8467. tsTryParseExportDeclaration() {
  8468. return this.tsParseDeclaration(this.startNode(), this.state.value, true, null);
  8469. }
  8470. tsParseExpressionStatement(node, expr, decorators) {
  8471. switch (expr.name) {
  8472. case "declare":
  8473. {
  8474. const declaration = this.tsTryParseDeclare(node);
  8475. if (declaration) {
  8476. declaration.declare = true;
  8477. return declaration;
  8478. }
  8479. break;
  8480. }
  8481. case "global":
  8482. if (this.match(5)) {
  8483. this.scope.enter(SCOPE_TS_MODULE);
  8484. this.prodParam.enter(PARAM);
  8485. const mod = node;
  8486. mod.global = true;
  8487. mod.id = expr;
  8488. mod.body = this.tsParseModuleBlock();
  8489. this.scope.exit();
  8490. this.prodParam.exit();
  8491. return this.finishNode(mod, "TSModuleDeclaration");
  8492. }
  8493. break;
  8494. default:
  8495. return this.tsParseDeclaration(node, expr.name, false, decorators);
  8496. }
  8497. }
  8498. tsParseDeclaration(node, value, next, decorators) {
  8499. switch (value) {
  8500. case "abstract":
  8501. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8502. return this.tsParseAbstractDeclaration(node, decorators);
  8503. }
  8504. break;
  8505. case "module":
  8506. if (this.tsCheckLineTerminator(next)) {
  8507. if (this.match(131)) {
  8508. return this.tsParseAmbientExternalModuleDeclaration(node);
  8509. } else if (tokenIsIdentifier(this.state.type)) {
  8510. return this.tsParseModuleOrNamespaceDeclaration(node);
  8511. }
  8512. }
  8513. break;
  8514. case "namespace":
  8515. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8516. return this.tsParseModuleOrNamespaceDeclaration(node);
  8517. }
  8518. break;
  8519. case "type":
  8520. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8521. return this.tsParseTypeAliasDeclaration(node);
  8522. }
  8523. break;
  8524. }
  8525. }
  8526. tsCheckLineTerminator(next) {
  8527. if (next) {
  8528. if (this.hasFollowingLineBreak()) return false;
  8529. this.next();
  8530. return true;
  8531. }
  8532. return !this.isLineTerminator();
  8533. }
  8534. tsTryParseGenericAsyncArrowFunction(startLoc) {
  8535. if (!this.match(47)) {
  8536. return undefined;
  8537. }
  8538. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8539. this.state.maybeInArrowParameters = true;
  8540. const res = this.tsTryParseAndCatch(() => {
  8541. const node = this.startNodeAt(startLoc);
  8542. node.typeParameters = this.tsParseTypeParameters();
  8543. super.parseFunctionParams(node);
  8544. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8545. this.expect(19);
  8546. return node;
  8547. });
  8548. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8549. if (!res) {
  8550. return undefined;
  8551. }
  8552. return super.parseArrowExpression(res, null, true);
  8553. }
  8554. tsParseTypeArgumentsInExpression() {
  8555. if (this.reScan_lt() !== 47) {
  8556. return undefined;
  8557. }
  8558. return this.tsParseTypeArguments();
  8559. }
  8560. tsParseTypeArguments() {
  8561. const node = this.startNode();
  8562. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8563. this.expect(47);
  8564. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8565. }));
  8566. if (node.params.length === 0) {
  8567. this.raise(TSErrors.EmptyTypeArguments, {
  8568. at: node
  8569. });
  8570. }
  8571. this.expect(48);
  8572. return this.finishNode(node, "TSTypeParameterInstantiation");
  8573. }
  8574. tsIsDeclarationStart() {
  8575. return tokenIsTSDeclarationStart(this.state.type);
  8576. }
  8577. isExportDefaultSpecifier() {
  8578. if (this.tsIsDeclarationStart()) return false;
  8579. return super.isExportDefaultSpecifier();
  8580. }
  8581. parseAssignableListItem(allowModifiers, decorators) {
  8582. const startLoc = this.state.startLoc;
  8583. let accessibility;
  8584. let readonly = false;
  8585. let override = false;
  8586. if (allowModifiers !== undefined) {
  8587. const modified = {};
  8588. this.tsParseModifiers({
  8589. modified,
  8590. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8591. });
  8592. accessibility = modified.accessibility;
  8593. override = modified.override;
  8594. readonly = modified.readonly;
  8595. if (allowModifiers === false && (accessibility || readonly || override)) {
  8596. this.raise(TSErrors.UnexpectedParameterModifier, {
  8597. at: startLoc
  8598. });
  8599. }
  8600. }
  8601. const left = this.parseMaybeDefault();
  8602. this.parseAssignableListItemTypes(left);
  8603. const elt = this.parseMaybeDefault(left.loc.start, left);
  8604. if (accessibility || readonly || override) {
  8605. const pp = this.startNodeAt(startLoc);
  8606. if (decorators.length) {
  8607. pp.decorators = decorators;
  8608. }
  8609. if (accessibility) pp.accessibility = accessibility;
  8610. if (readonly) pp.readonly = readonly;
  8611. if (override) pp.override = override;
  8612. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8613. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  8614. at: pp
  8615. });
  8616. }
  8617. pp.parameter = elt;
  8618. return this.finishNode(pp, "TSParameterProperty");
  8619. }
  8620. if (decorators.length) {
  8621. left.decorators = decorators;
  8622. }
  8623. return elt;
  8624. }
  8625. isSimpleParameter(node) {
  8626. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  8627. }
  8628. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8629. if (this.match(14)) {
  8630. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8631. }
  8632. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  8633. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  8634. return this.finishNode(node, bodilessType);
  8635. }
  8636. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8637. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  8638. at: node
  8639. });
  8640. if (node.declare) {
  8641. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8642. }
  8643. }
  8644. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  8645. }
  8646. registerFunctionStatementId(node) {
  8647. if (!node.body && node.id) {
  8648. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  8649. } else {
  8650. super.registerFunctionStatementId(node);
  8651. }
  8652. }
  8653. tsCheckForInvalidTypeCasts(items) {
  8654. items.forEach(node => {
  8655. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8656. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  8657. at: node.typeAnnotation
  8658. });
  8659. }
  8660. });
  8661. }
  8662. toReferencedList(exprList, isInParens) {
  8663. this.tsCheckForInvalidTypeCasts(exprList);
  8664. return exprList;
  8665. }
  8666. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  8667. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  8668. if (node.type === "ArrayExpression") {
  8669. this.tsCheckForInvalidTypeCasts(node.elements);
  8670. }
  8671. return node;
  8672. }
  8673. parseSubscript(base, startLoc, noCalls, state) {
  8674. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8675. this.state.canStartJSXElement = false;
  8676. this.next();
  8677. const nonNullExpression = this.startNodeAt(startLoc);
  8678. nonNullExpression.expression = base;
  8679. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8680. }
  8681. let isOptionalCall = false;
  8682. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8683. if (noCalls) {
  8684. state.stop = true;
  8685. return base;
  8686. }
  8687. state.optionalChainMember = isOptionalCall = true;
  8688. this.next();
  8689. }
  8690. if (this.match(47) || this.match(51)) {
  8691. let missingParenErrorLoc;
  8692. const result = this.tsTryParseAndCatch(() => {
  8693. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8694. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  8695. if (asyncArrowFn) {
  8696. return asyncArrowFn;
  8697. }
  8698. }
  8699. const typeArguments = this.tsParseTypeArgumentsInExpression();
  8700. if (!typeArguments) return;
  8701. if (isOptionalCall && !this.match(10)) {
  8702. missingParenErrorLoc = this.state.curPosition();
  8703. return;
  8704. }
  8705. if (tokenIsTemplate(this.state.type)) {
  8706. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  8707. result.typeParameters = typeArguments;
  8708. return result;
  8709. }
  8710. if (!noCalls && this.eat(10)) {
  8711. const node = this.startNodeAt(startLoc);
  8712. node.callee = base;
  8713. node.arguments = this.parseCallExpressionArguments(11, false);
  8714. this.tsCheckForInvalidTypeCasts(node.arguments);
  8715. node.typeParameters = typeArguments;
  8716. if (state.optionalChainMember) {
  8717. node.optional = isOptionalCall;
  8718. }
  8719. return this.finishCallExpression(node, state.optionalChainMember);
  8720. }
  8721. const tokenType = this.state.type;
  8722. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  8723. return;
  8724. }
  8725. const node = this.startNodeAt(startLoc);
  8726. node.expression = base;
  8727. node.typeParameters = typeArguments;
  8728. return this.finishNode(node, "TSInstantiationExpression");
  8729. });
  8730. if (missingParenErrorLoc) {
  8731. this.unexpected(missingParenErrorLoc, 10);
  8732. }
  8733. if (result) {
  8734. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  8735. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
  8736. at: this.state.startLoc
  8737. });
  8738. }
  8739. return result;
  8740. }
  8741. }
  8742. return super.parseSubscript(base, startLoc, noCalls, state);
  8743. }
  8744. parseNewCallee(node) {
  8745. var _callee$extra;
  8746. super.parseNewCallee(node);
  8747. const {
  8748. callee
  8749. } = node;
  8750. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  8751. node.typeParameters = callee.typeParameters;
  8752. node.callee = callee.expression;
  8753. }
  8754. }
  8755. parseExprOp(left, leftStartLoc, minPrec) {
  8756. let isSatisfies;
  8757. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(118)))) {
  8758. const node = this.startNodeAt(leftStartLoc);
  8759. node.expression = left;
  8760. node.typeAnnotation = this.tsInType(() => {
  8761. this.next();
  8762. if (this.match(75)) {
  8763. if (isSatisfies) {
  8764. this.raise(Errors.UnexpectedKeyword, {
  8765. at: this.state.startLoc,
  8766. keyword: "const"
  8767. });
  8768. }
  8769. return this.tsParseTypeReference();
  8770. }
  8771. return this.tsParseType();
  8772. });
  8773. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  8774. this.reScan_lt_gt();
  8775. return this.parseExprOp(node, leftStartLoc, minPrec);
  8776. }
  8777. return super.parseExprOp(left, leftStartLoc, minPrec);
  8778. }
  8779. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  8780. if (!this.state.isAmbientContext) {
  8781. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  8782. }
  8783. }
  8784. checkImportReflection(node) {
  8785. super.checkImportReflection(node);
  8786. if (node.module && node.importKind !== "value") {
  8787. this.raise(TSErrors.ImportReflectionHasImportType, {
  8788. at: node.specifiers[0].loc.start
  8789. });
  8790. }
  8791. }
  8792. checkDuplicateExports() {}
  8793. parseImport(node) {
  8794. node.importKind = "value";
  8795. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  8796. let ahead = this.lookahead();
  8797. if (this.isContextual(128) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  8798. node.importKind = "type";
  8799. this.next();
  8800. ahead = this.lookahead();
  8801. }
  8802. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  8803. return this.tsParseImportEqualsDeclaration(node);
  8804. }
  8805. }
  8806. const importNode = super.parseImport(node);
  8807. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  8808. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  8809. at: importNode
  8810. });
  8811. }
  8812. return importNode;
  8813. }
  8814. parseExport(node, decorators) {
  8815. if (this.match(83)) {
  8816. this.next();
  8817. if (this.isContextual(128) && this.lookaheadCharCode() !== 61) {
  8818. node.importKind = "type";
  8819. this.next();
  8820. } else {
  8821. node.importKind = "value";
  8822. }
  8823. return this.tsParseImportEqualsDeclaration(node, true);
  8824. } else if (this.eat(29)) {
  8825. const assign = node;
  8826. assign.expression = super.parseExpression();
  8827. this.semicolon();
  8828. return this.finishNode(assign, "TSExportAssignment");
  8829. } else if (this.eatContextual(93)) {
  8830. const decl = node;
  8831. this.expectContextual(126);
  8832. decl.id = this.parseIdentifier();
  8833. this.semicolon();
  8834. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  8835. } else {
  8836. if (this.isContextual(128) && this.lookahead().type === 5) {
  8837. this.next();
  8838. node.exportKind = "type";
  8839. } else {
  8840. node.exportKind = "value";
  8841. }
  8842. return super.parseExport(node, decorators);
  8843. }
  8844. }
  8845. isAbstractClass() {
  8846. return this.isContextual(122) && this.lookahead().type === 80;
  8847. }
  8848. parseExportDefaultExpression() {
  8849. if (this.isAbstractClass()) {
  8850. const cls = this.startNode();
  8851. this.next();
  8852. cls.abstract = true;
  8853. return this.parseClass(cls, true, true);
  8854. }
  8855. if (this.match(127)) {
  8856. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8857. if (result) return result;
  8858. }
  8859. return super.parseExportDefaultExpression();
  8860. }
  8861. parseVarStatement(node, kind, allowMissingInitializer = false) {
  8862. const {
  8863. isAmbientContext
  8864. } = this.state;
  8865. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  8866. if (!isAmbientContext) return declaration;
  8867. for (const {
  8868. id,
  8869. init
  8870. } of declaration.declarations) {
  8871. if (!init) continue;
  8872. if (kind !== "const" || !!id.typeAnnotation) {
  8873. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  8874. at: init
  8875. });
  8876. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  8877. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  8878. at: init
  8879. });
  8880. }
  8881. }
  8882. return declaration;
  8883. }
  8884. parseStatementContent(flags, decorators) {
  8885. if (this.match(75) && this.isLookaheadContextual("enum")) {
  8886. const node = this.startNode();
  8887. this.expect(75);
  8888. return this.tsParseEnumDeclaration(node, {
  8889. const: true
  8890. });
  8891. }
  8892. if (this.isContextual(124)) {
  8893. return this.tsParseEnumDeclaration(this.startNode());
  8894. }
  8895. if (this.isContextual(127)) {
  8896. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8897. if (result) return result;
  8898. }
  8899. return super.parseStatementContent(flags, decorators);
  8900. }
  8901. parseAccessModifier() {
  8902. return this.tsParseModifier(["public", "protected", "private"]);
  8903. }
  8904. tsHasSomeModifiers(member, modifiers) {
  8905. return modifiers.some(modifier => {
  8906. if (tsIsAccessModifier(modifier)) {
  8907. return member.accessibility === modifier;
  8908. }
  8909. return !!member[modifier];
  8910. });
  8911. }
  8912. tsIsStartOfStaticBlocks() {
  8913. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  8914. }
  8915. parseClassMember(classBody, member, state) {
  8916. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  8917. this.tsParseModifiers({
  8918. modified: member,
  8919. allowedModifiers: modifiers,
  8920. disallowedModifiers: ["in", "out"],
  8921. stopOnStartOfClassStaticBlock: true,
  8922. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  8923. });
  8924. const callParseClassMemberWithIsStatic = () => {
  8925. if (this.tsIsStartOfStaticBlocks()) {
  8926. this.next();
  8927. this.next();
  8928. if (this.tsHasSomeModifiers(member, modifiers)) {
  8929. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  8930. at: this.state.curPosition()
  8931. });
  8932. }
  8933. super.parseClassStaticBlock(classBody, member);
  8934. } else {
  8935. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  8936. }
  8937. };
  8938. if (member.declare) {
  8939. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  8940. } else {
  8941. callParseClassMemberWithIsStatic();
  8942. }
  8943. }
  8944. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  8945. const idx = this.tsTryParseIndexSignature(member);
  8946. if (idx) {
  8947. classBody.body.push(idx);
  8948. if (member.abstract) {
  8949. this.raise(TSErrors.IndexSignatureHasAbstract, {
  8950. at: member
  8951. });
  8952. }
  8953. if (member.accessibility) {
  8954. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  8955. at: member,
  8956. modifier: member.accessibility
  8957. });
  8958. }
  8959. if (member.declare) {
  8960. this.raise(TSErrors.IndexSignatureHasDeclare, {
  8961. at: member
  8962. });
  8963. }
  8964. if (member.override) {
  8965. this.raise(TSErrors.IndexSignatureHasOverride, {
  8966. at: member
  8967. });
  8968. }
  8969. return;
  8970. }
  8971. if (!this.state.inAbstractClass && member.abstract) {
  8972. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  8973. at: member
  8974. });
  8975. }
  8976. if (member.override) {
  8977. if (!state.hadSuperClass) {
  8978. this.raise(TSErrors.OverrideNotInSubClass, {
  8979. at: member
  8980. });
  8981. }
  8982. }
  8983. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  8984. }
  8985. parsePostMemberNameModifiers(methodOrProp) {
  8986. const optional = this.eat(17);
  8987. if (optional) methodOrProp.optional = true;
  8988. if (methodOrProp.readonly && this.match(10)) {
  8989. this.raise(TSErrors.ClassMethodHasReadonly, {
  8990. at: methodOrProp
  8991. });
  8992. }
  8993. if (methodOrProp.declare && this.match(10)) {
  8994. this.raise(TSErrors.ClassMethodHasDeclare, {
  8995. at: methodOrProp
  8996. });
  8997. }
  8998. }
  8999. parseExpressionStatement(node, expr, decorators) {
  9000. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr, decorators) : undefined;
  9001. return decl || super.parseExpressionStatement(node, expr, decorators);
  9002. }
  9003. shouldParseExportDeclaration() {
  9004. if (this.tsIsDeclarationStart()) return true;
  9005. return super.shouldParseExportDeclaration();
  9006. }
  9007. parseConditional(expr, startLoc, refExpressionErrors) {
  9008. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  9009. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9010. }
  9011. const result = this.tryParse(() => super.parseConditional(expr, startLoc));
  9012. if (!result.node) {
  9013. if (result.error) {
  9014. super.setOptionalParametersError(refExpressionErrors, result.error);
  9015. }
  9016. return expr;
  9017. }
  9018. if (result.error) this.state = result.failState;
  9019. return result.node;
  9020. }
  9021. parseParenItem(node, startLoc) {
  9022. node = super.parseParenItem(node, startLoc);
  9023. if (this.eat(17)) {
  9024. node.optional = true;
  9025. this.resetEndLocation(node);
  9026. }
  9027. if (this.match(14)) {
  9028. const typeCastNode = this.startNodeAt(startLoc);
  9029. typeCastNode.expression = node;
  9030. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9031. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9032. }
  9033. return node;
  9034. }
  9035. parseExportDeclaration(node) {
  9036. if (!this.state.isAmbientContext && this.isContextual(123)) {
  9037. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9038. }
  9039. const startLoc = this.state.startLoc;
  9040. const isDeclare = this.eatContextual(123);
  9041. if (isDeclare && (this.isContextual(123) || !this.shouldParseExportDeclaration())) {
  9042. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  9043. at: this.state.startLoc
  9044. });
  9045. }
  9046. const isIdentifier = tokenIsIdentifier(this.state.type);
  9047. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9048. if (!declaration) return null;
  9049. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9050. node.exportKind = "type";
  9051. }
  9052. if (isDeclare) {
  9053. this.resetStartLocation(declaration, startLoc);
  9054. declaration.declare = true;
  9055. }
  9056. return declaration;
  9057. }
  9058. parseClassId(node, isStatement, optionalId, bindingType) {
  9059. if ((!isStatement || optionalId) && this.isContextual(111)) {
  9060. return;
  9061. }
  9062. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  9063. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  9064. if (typeParameters) node.typeParameters = typeParameters;
  9065. }
  9066. parseClassPropertyAnnotation(node) {
  9067. if (!node.optional) {
  9068. if (this.eat(35)) {
  9069. node.definite = true;
  9070. } else if (this.eat(17)) {
  9071. node.optional = true;
  9072. }
  9073. }
  9074. const type = this.tsTryParseTypeAnnotation();
  9075. if (type) node.typeAnnotation = type;
  9076. }
  9077. parseClassProperty(node) {
  9078. this.parseClassPropertyAnnotation(node);
  9079. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9080. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  9081. at: this.state.startLoc
  9082. });
  9083. }
  9084. if (node.abstract && this.match(29)) {
  9085. const {
  9086. key
  9087. } = node;
  9088. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  9089. at: this.state.startLoc,
  9090. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9091. });
  9092. }
  9093. return super.parseClassProperty(node);
  9094. }
  9095. parseClassPrivateProperty(node) {
  9096. if (node.abstract) {
  9097. this.raise(TSErrors.PrivateElementHasAbstract, {
  9098. at: node
  9099. });
  9100. }
  9101. if (node.accessibility) {
  9102. this.raise(TSErrors.PrivateElementHasAccessibility, {
  9103. at: node,
  9104. modifier: node.accessibility
  9105. });
  9106. }
  9107. this.parseClassPropertyAnnotation(node);
  9108. return super.parseClassPrivateProperty(node);
  9109. }
  9110. parseClassAccessorProperty(node) {
  9111. this.parseClassPropertyAnnotation(node);
  9112. if (node.optional) {
  9113. this.raise(TSErrors.AccessorCannotBeOptional, {
  9114. at: node
  9115. });
  9116. }
  9117. return super.parseClassAccessorProperty(node);
  9118. }
  9119. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9120. const typeParameters = this.tsTryParseTypeParameters();
  9121. if (typeParameters && isConstructor) {
  9122. this.raise(TSErrors.ConstructorHasTypeParameters, {
  9123. at: typeParameters
  9124. });
  9125. }
  9126. const {
  9127. declare = false,
  9128. kind
  9129. } = method;
  9130. if (declare && (kind === "get" || kind === "set")) {
  9131. this.raise(TSErrors.DeclareAccessor, {
  9132. at: method,
  9133. kind
  9134. });
  9135. }
  9136. if (typeParameters) method.typeParameters = typeParameters;
  9137. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9138. }
  9139. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9140. const typeParameters = this.tsTryParseTypeParameters();
  9141. if (typeParameters) method.typeParameters = typeParameters;
  9142. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9143. }
  9144. declareClassPrivateMethodInScope(node, kind) {
  9145. if (node.type === "TSDeclareMethod") return;
  9146. if (node.type === "MethodDefinition" && !node.value.body) return;
  9147. super.declareClassPrivateMethodInScope(node, kind);
  9148. }
  9149. parseClassSuper(node) {
  9150. super.parseClassSuper(node);
  9151. if (node.superClass && (this.match(47) || this.match(51))) {
  9152. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9153. }
  9154. if (this.eatContextual(111)) {
  9155. node.implements = this.tsParseHeritageClause("implements");
  9156. }
  9157. }
  9158. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9159. const typeParameters = this.tsTryParseTypeParameters();
  9160. if (typeParameters) prop.typeParameters = typeParameters;
  9161. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9162. }
  9163. parseFunctionParams(node, allowModifiers) {
  9164. const typeParameters = this.tsTryParseTypeParameters();
  9165. if (typeParameters) node.typeParameters = typeParameters;
  9166. super.parseFunctionParams(node, allowModifiers);
  9167. }
  9168. parseVarId(decl, kind) {
  9169. super.parseVarId(decl, kind);
  9170. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9171. decl.definite = true;
  9172. }
  9173. const type = this.tsTryParseTypeAnnotation();
  9174. if (type) {
  9175. decl.id.typeAnnotation = type;
  9176. this.resetEndLocation(decl.id);
  9177. }
  9178. }
  9179. parseAsyncArrowFromCallExpression(node, call) {
  9180. if (this.match(14)) {
  9181. node.returnType = this.tsParseTypeAnnotation();
  9182. }
  9183. return super.parseAsyncArrowFromCallExpression(node, call);
  9184. }
  9185. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9186. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  9187. let state;
  9188. let jsx;
  9189. let typeCast;
  9190. if (this.hasPlugin("jsx") && (this.match(140) || this.match(47))) {
  9191. state = this.state.clone();
  9192. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9193. if (!jsx.error) return jsx.node;
  9194. const {
  9195. context
  9196. } = this.state;
  9197. const currentContext = context[context.length - 1];
  9198. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9199. context.pop();
  9200. }
  9201. }
  9202. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9203. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9204. }
  9205. if (!state || state === this.state) state = this.state.clone();
  9206. let typeParameters;
  9207. const arrow = this.tryParse(abort => {
  9208. var _expr$extra, _typeParameters;
  9209. typeParameters = this.tsParseTypeParameters();
  9210. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9211. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9212. abort();
  9213. }
  9214. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9215. this.resetStartLocationFromNode(expr, typeParameters);
  9216. }
  9217. expr.typeParameters = typeParameters;
  9218. return expr;
  9219. }, state);
  9220. if (!arrow.error && !arrow.aborted) {
  9221. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9222. return arrow.node;
  9223. }
  9224. if (!jsx) {
  9225. assert(!this.hasPlugin("jsx"));
  9226. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9227. if (!typeCast.error) return typeCast.node;
  9228. }
  9229. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9230. this.state = jsx.failState;
  9231. return jsx.node;
  9232. }
  9233. if (arrow.node) {
  9234. this.state = arrow.failState;
  9235. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9236. return arrow.node;
  9237. }
  9238. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9239. this.state = typeCast.failState;
  9240. return typeCast.node;
  9241. }
  9242. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  9243. if (arrow.thrown) throw arrow.error;
  9244. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  9245. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  9246. }
  9247. reportReservedArrowTypeParam(node) {
  9248. var _node$extra;
  9249. if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9250. this.raise(TSErrors.ReservedArrowTypeParam, {
  9251. at: node
  9252. });
  9253. }
  9254. }
  9255. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9256. if (!this.hasPlugin("jsx") && this.match(47)) {
  9257. return this.tsParseTypeAssertion();
  9258. } else {
  9259. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9260. }
  9261. }
  9262. parseArrow(node) {
  9263. if (this.match(14)) {
  9264. const result = this.tryParse(abort => {
  9265. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9266. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9267. return returnType;
  9268. });
  9269. if (result.aborted) return;
  9270. if (!result.thrown) {
  9271. if (result.error) this.state = result.failState;
  9272. node.returnType = result.node;
  9273. }
  9274. }
  9275. return super.parseArrow(node);
  9276. }
  9277. parseAssignableListItemTypes(param) {
  9278. if (this.eat(17)) {
  9279. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  9280. this.raise(TSErrors.PatternIsOptional, {
  9281. at: param
  9282. });
  9283. }
  9284. param.optional = true;
  9285. }
  9286. const type = this.tsTryParseTypeAnnotation();
  9287. if (type) param.typeAnnotation = type;
  9288. this.resetEndLocation(param);
  9289. return param;
  9290. }
  9291. isAssignable(node, isBinding) {
  9292. switch (node.type) {
  9293. case "TSTypeCastExpression":
  9294. return this.isAssignable(node.expression, isBinding);
  9295. case "TSParameterProperty":
  9296. return true;
  9297. default:
  9298. return super.isAssignable(node, isBinding);
  9299. }
  9300. }
  9301. toAssignable(node, isLHS = false) {
  9302. switch (node.type) {
  9303. case "ParenthesizedExpression":
  9304. this.toAssignableParenthesizedExpression(node, isLHS);
  9305. break;
  9306. case "TSAsExpression":
  9307. case "TSSatisfiesExpression":
  9308. case "TSNonNullExpression":
  9309. case "TSTypeAssertion":
  9310. if (isLHS) {
  9311. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9312. at: node
  9313. });
  9314. } else {
  9315. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9316. at: node
  9317. });
  9318. }
  9319. this.toAssignable(node.expression, isLHS);
  9320. break;
  9321. case "AssignmentExpression":
  9322. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9323. node.left = this.typeCastToParameter(node.left);
  9324. }
  9325. default:
  9326. super.toAssignable(node, isLHS);
  9327. }
  9328. }
  9329. toAssignableParenthesizedExpression(node, isLHS) {
  9330. switch (node.expression.type) {
  9331. case "TSAsExpression":
  9332. case "TSSatisfiesExpression":
  9333. case "TSNonNullExpression":
  9334. case "TSTypeAssertion":
  9335. case "ParenthesizedExpression":
  9336. this.toAssignable(node.expression, isLHS);
  9337. break;
  9338. default:
  9339. super.toAssignable(node, isLHS);
  9340. }
  9341. }
  9342. checkToRestConversion(node, allowPattern) {
  9343. switch (node.type) {
  9344. case "TSAsExpression":
  9345. case "TSSatisfiesExpression":
  9346. case "TSTypeAssertion":
  9347. case "TSNonNullExpression":
  9348. this.checkToRestConversion(node.expression, false);
  9349. break;
  9350. default:
  9351. super.checkToRestConversion(node, allowPattern);
  9352. }
  9353. }
  9354. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9355. return getOwn$1({
  9356. TSTypeCastExpression: true,
  9357. TSParameterProperty: "parameter",
  9358. TSNonNullExpression: "expression",
  9359. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9360. TSSatisfiesExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9361. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9362. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9363. }
  9364. parseBindingAtom() {
  9365. switch (this.state.type) {
  9366. case 78:
  9367. return this.parseIdentifier(true);
  9368. default:
  9369. return super.parseBindingAtom();
  9370. }
  9371. }
  9372. parseMaybeDecoratorArguments(expr) {
  9373. if (this.match(47) || this.match(51)) {
  9374. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9375. if (this.match(10)) {
  9376. const call = super.parseMaybeDecoratorArguments(expr);
  9377. call.typeParameters = typeArguments;
  9378. return call;
  9379. }
  9380. this.unexpected(null, 10);
  9381. }
  9382. return super.parseMaybeDecoratorArguments(expr);
  9383. }
  9384. checkCommaAfterRest(close) {
  9385. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9386. this.next();
  9387. return false;
  9388. } else {
  9389. return super.checkCommaAfterRest(close);
  9390. }
  9391. }
  9392. isClassMethod() {
  9393. return this.match(47) || super.isClassMethod();
  9394. }
  9395. isClassProperty() {
  9396. return this.match(35) || this.match(14) || super.isClassProperty();
  9397. }
  9398. parseMaybeDefault(startLoc, left) {
  9399. const node = super.parseMaybeDefault(startLoc, left);
  9400. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9401. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9402. at: node.typeAnnotation
  9403. });
  9404. }
  9405. return node;
  9406. }
  9407. getTokenFromCode(code) {
  9408. if (this.state.inType) {
  9409. if (code === 62) {
  9410. return this.finishOp(48, 1);
  9411. }
  9412. if (code === 60) {
  9413. return this.finishOp(47, 1);
  9414. }
  9415. }
  9416. return super.getTokenFromCode(code);
  9417. }
  9418. reScan_lt_gt() {
  9419. const {
  9420. type
  9421. } = this.state;
  9422. if (type === 47) {
  9423. this.state.pos -= 1;
  9424. this.readToken_lt();
  9425. } else if (type === 48) {
  9426. this.state.pos -= 1;
  9427. this.readToken_gt();
  9428. }
  9429. }
  9430. reScan_lt() {
  9431. const {
  9432. type
  9433. } = this.state;
  9434. if (type === 51) {
  9435. this.state.pos -= 2;
  9436. this.finishOp(47, 1);
  9437. return 47;
  9438. }
  9439. return type;
  9440. }
  9441. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9442. for (let i = 0; i < exprList.length; i++) {
  9443. const expr = exprList[i];
  9444. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9445. exprList[i] = this.typeCastToParameter(expr);
  9446. }
  9447. }
  9448. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9449. }
  9450. typeCastToParameter(node) {
  9451. node.expression.typeAnnotation = node.typeAnnotation;
  9452. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9453. return node.expression;
  9454. }
  9455. shouldParseArrow(params) {
  9456. if (this.match(14)) {
  9457. return params.every(expr => this.isAssignable(expr, true));
  9458. }
  9459. return super.shouldParseArrow(params);
  9460. }
  9461. shouldParseAsyncArrow() {
  9462. return this.match(14) || super.shouldParseAsyncArrow();
  9463. }
  9464. canHaveLeadingDecorator() {
  9465. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9466. }
  9467. jsxParseOpeningElementAfterName(node) {
  9468. if (this.match(47) || this.match(51)) {
  9469. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9470. if (typeArguments) node.typeParameters = typeArguments;
  9471. }
  9472. return super.jsxParseOpeningElementAfterName(node);
  9473. }
  9474. getGetterSetterExpectedParamCount(method) {
  9475. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9476. const params = this.getObjectOrClassMethodParams(method);
  9477. const firstParam = params[0];
  9478. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9479. return hasContextParam ? baseCount + 1 : baseCount;
  9480. }
  9481. parseCatchClauseParam() {
  9482. const param = super.parseCatchClauseParam();
  9483. const type = this.tsTryParseTypeAnnotation();
  9484. if (type) {
  9485. param.typeAnnotation = type;
  9486. this.resetEndLocation(param);
  9487. }
  9488. return param;
  9489. }
  9490. tsInAmbientContext(cb) {
  9491. const oldIsAmbientContext = this.state.isAmbientContext;
  9492. this.state.isAmbientContext = true;
  9493. try {
  9494. return cb();
  9495. } finally {
  9496. this.state.isAmbientContext = oldIsAmbientContext;
  9497. }
  9498. }
  9499. parseClass(node, isStatement, optionalId) {
  9500. const oldInAbstractClass = this.state.inAbstractClass;
  9501. this.state.inAbstractClass = !!node.abstract;
  9502. try {
  9503. return super.parseClass(node, isStatement, optionalId);
  9504. } finally {
  9505. this.state.inAbstractClass = oldInAbstractClass;
  9506. }
  9507. }
  9508. tsParseAbstractDeclaration(node, decorators) {
  9509. if (this.match(80)) {
  9510. node.abstract = true;
  9511. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  9512. } else if (this.isContextual(127)) {
  9513. if (!this.hasFollowingLineBreak()) {
  9514. node.abstract = true;
  9515. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9516. at: node
  9517. });
  9518. return this.tsParseInterfaceDeclaration(node);
  9519. }
  9520. } else {
  9521. this.unexpected(null, 80);
  9522. }
  9523. }
  9524. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9525. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9526. if (method.abstract) {
  9527. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9528. if (hasBody) {
  9529. const {
  9530. key
  9531. } = method;
  9532. this.raise(TSErrors.AbstractMethodHasImplementation, {
  9533. at: method,
  9534. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9535. });
  9536. }
  9537. }
  9538. return method;
  9539. }
  9540. tsParseTypeParameterName() {
  9541. const typeName = this.parseIdentifier();
  9542. return typeName.name;
  9543. }
  9544. shouldParseAsAmbientContext() {
  9545. return !!this.getPluginOption("typescript", "dts");
  9546. }
  9547. parse() {
  9548. if (this.shouldParseAsAmbientContext()) {
  9549. this.state.isAmbientContext = true;
  9550. }
  9551. return super.parse();
  9552. }
  9553. getExpression() {
  9554. if (this.shouldParseAsAmbientContext()) {
  9555. this.state.isAmbientContext = true;
  9556. }
  9557. return super.getExpression();
  9558. }
  9559. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9560. if (!isString && isMaybeTypeOnly) {
  9561. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9562. return this.finishNode(node, "ExportSpecifier");
  9563. }
  9564. node.exportKind = "value";
  9565. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9566. }
  9567. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  9568. if (!importedIsString && isMaybeTypeOnly) {
  9569. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9570. return this.finishNode(specifier, "ImportSpecifier");
  9571. }
  9572. specifier.importKind = "value";
  9573. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  9574. }
  9575. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9576. const leftOfAsKey = isImport ? "imported" : "local";
  9577. const rightOfAsKey = isImport ? "local" : "exported";
  9578. let leftOfAs = node[leftOfAsKey];
  9579. let rightOfAs;
  9580. let hasTypeSpecifier = false;
  9581. let canParseAsKeyword = true;
  9582. const loc = leftOfAs.loc.start;
  9583. if (this.isContextual(93)) {
  9584. const firstAs = this.parseIdentifier();
  9585. if (this.isContextual(93)) {
  9586. const secondAs = this.parseIdentifier();
  9587. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9588. hasTypeSpecifier = true;
  9589. leftOfAs = firstAs;
  9590. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9591. canParseAsKeyword = false;
  9592. } else {
  9593. rightOfAs = secondAs;
  9594. canParseAsKeyword = false;
  9595. }
  9596. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9597. canParseAsKeyword = false;
  9598. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9599. } else {
  9600. hasTypeSpecifier = true;
  9601. leftOfAs = firstAs;
  9602. }
  9603. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9604. hasTypeSpecifier = true;
  9605. if (isImport) {
  9606. leftOfAs = this.parseIdentifier(true);
  9607. if (!this.isContextual(93)) {
  9608. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  9609. }
  9610. } else {
  9611. leftOfAs = this.parseModuleExportName();
  9612. }
  9613. }
  9614. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  9615. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  9616. at: loc
  9617. });
  9618. }
  9619. node[leftOfAsKey] = leftOfAs;
  9620. node[rightOfAsKey] = rightOfAs;
  9621. const kindKey = isImport ? "importKind" : "exportKind";
  9622. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  9623. if (canParseAsKeyword && this.eatContextual(93)) {
  9624. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9625. }
  9626. if (!node[rightOfAsKey]) {
  9627. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  9628. }
  9629. if (isImport) {
  9630. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  9631. }
  9632. }
  9633. });
  9634. function isPossiblyLiteralEnum(expression) {
  9635. if (expression.type !== "MemberExpression") return false;
  9636. const {
  9637. computed,
  9638. property
  9639. } = expression;
  9640. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  9641. return false;
  9642. }
  9643. return isUncomputedMemberExpressionChain(expression.object);
  9644. }
  9645. function isValidAmbientConstInitializer(expression, estree) {
  9646. var _expression$extra;
  9647. const {
  9648. type
  9649. } = expression;
  9650. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  9651. return false;
  9652. }
  9653. if (estree) {
  9654. if (type === "Literal") {
  9655. const {
  9656. value
  9657. } = expression;
  9658. if (typeof value === "string" || typeof value === "boolean") {
  9659. return true;
  9660. }
  9661. }
  9662. } else {
  9663. if (type === "StringLiteral" || type === "BooleanLiteral") {
  9664. return true;
  9665. }
  9666. }
  9667. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  9668. return true;
  9669. }
  9670. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  9671. return true;
  9672. }
  9673. if (isPossiblyLiteralEnum(expression)) {
  9674. return true;
  9675. }
  9676. return false;
  9677. }
  9678. function isNumber(expression, estree) {
  9679. if (estree) {
  9680. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  9681. } else {
  9682. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  9683. }
  9684. }
  9685. function isNegativeNumber(expression, estree) {
  9686. if (expression.type === "UnaryExpression") {
  9687. const {
  9688. operator,
  9689. argument
  9690. } = expression;
  9691. if (operator === "-" && isNumber(argument, estree)) {
  9692. return true;
  9693. }
  9694. }
  9695. return false;
  9696. }
  9697. function isUncomputedMemberExpressionChain(expression) {
  9698. if (expression.type === "Identifier") return true;
  9699. if (expression.type !== "MemberExpression") return false;
  9700. if (expression.computed) return false;
  9701. return isUncomputedMemberExpressionChain(expression.object);
  9702. }
  9703. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  9704. ClassNameIsRequired: "A class name is required.",
  9705. UnexpectedSpace: "Unexpected space in placeholder."
  9706. });
  9707. var placeholders = (superClass => class PlaceholdersParserMixin extends superClass {
  9708. parsePlaceholder(expectedNode) {
  9709. if (this.match(142)) {
  9710. const node = this.startNode();
  9711. this.next();
  9712. this.assertNoSpace();
  9713. node.name = super.parseIdentifier(true);
  9714. this.assertNoSpace();
  9715. this.expect(142);
  9716. return this.finishPlaceholder(node, expectedNode);
  9717. }
  9718. }
  9719. finishPlaceholder(node, expectedNode) {
  9720. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  9721. node.expectedNode = expectedNode;
  9722. return isFinished ? node : this.finishNode(node, "Placeholder");
  9723. }
  9724. getTokenFromCode(code) {
  9725. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  9726. return this.finishOp(142, 2);
  9727. }
  9728. return super.getTokenFromCode(code);
  9729. }
  9730. parseExprAtom(refExpressionErrors) {
  9731. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  9732. }
  9733. parseIdentifier(liberal) {
  9734. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  9735. }
  9736. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9737. if (word !== undefined) {
  9738. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9739. }
  9740. }
  9741. parseBindingAtom() {
  9742. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  9743. }
  9744. isValidLVal(type, isParenthesized, binding) {
  9745. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  9746. }
  9747. toAssignable(node, isLHS) {
  9748. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  9749. node.expectedNode = "Pattern";
  9750. } else {
  9751. super.toAssignable(node, isLHS);
  9752. }
  9753. }
  9754. chStartsBindingIdentifier(ch, pos) {
  9755. if (super.chStartsBindingIdentifier(ch, pos)) {
  9756. return true;
  9757. }
  9758. const nextToken = this.lookahead();
  9759. if (nextToken.type === 142) {
  9760. return true;
  9761. }
  9762. return false;
  9763. }
  9764. verifyBreakContinue(node, isBreak) {
  9765. if (node.label && node.label.type === "Placeholder") return;
  9766. super.verifyBreakContinue(node, isBreak);
  9767. }
  9768. parseExpressionStatement(node, expr) {
  9769. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  9770. return super.parseExpressionStatement(node, expr);
  9771. }
  9772. if (this.match(14)) {
  9773. const stmt = node;
  9774. stmt.label = this.finishPlaceholder(expr, "Identifier");
  9775. this.next();
  9776. stmt.body = super.parseStatementOrFunctionDeclaration(false);
  9777. return this.finishNode(stmt, "LabeledStatement");
  9778. }
  9779. this.semicolon();
  9780. node.name = expr.name;
  9781. return this.finishPlaceholder(node, "Statement");
  9782. }
  9783. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  9784. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  9785. }
  9786. parseFunctionId(requireId) {
  9787. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  9788. }
  9789. parseClass(node, isStatement, optionalId) {
  9790. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  9791. this.next();
  9792. const oldStrict = this.state.strict;
  9793. const placeholder = this.parsePlaceholder("Identifier");
  9794. if (placeholder) {
  9795. if (this.match(81) || this.match(142) || this.match(5)) {
  9796. node.id = placeholder;
  9797. } else if (optionalId || !isStatement) {
  9798. node.id = null;
  9799. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  9800. return this.finishNode(node, type);
  9801. } else {
  9802. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  9803. at: this.state.startLoc
  9804. });
  9805. }
  9806. } else {
  9807. this.parseClassId(node, isStatement, optionalId);
  9808. }
  9809. super.parseClassSuper(node);
  9810. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  9811. return this.finishNode(node, type);
  9812. }
  9813. parseExport(node, decorators) {
  9814. const placeholder = this.parsePlaceholder("Identifier");
  9815. if (!placeholder) return super.parseExport(node, decorators);
  9816. if (!this.isContextual(97) && !this.match(12)) {
  9817. node.specifiers = [];
  9818. node.source = null;
  9819. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  9820. return this.finishNode(node, "ExportNamedDeclaration");
  9821. }
  9822. this.expectPlugin("exportDefaultFrom");
  9823. const specifier = this.startNode();
  9824. specifier.exported = placeholder;
  9825. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9826. return super.parseExport(node, decorators);
  9827. }
  9828. isExportDefaultSpecifier() {
  9829. if (this.match(65)) {
  9830. const next = this.nextTokenStart();
  9831. if (this.isUnparsedContextual(next, "from")) {
  9832. if (this.input.startsWith(tokenLabelName(142), this.nextTokenStartSince(next + 4))) {
  9833. return true;
  9834. }
  9835. }
  9836. }
  9837. return super.isExportDefaultSpecifier();
  9838. }
  9839. maybeParseExportDefaultSpecifier(node) {
  9840. if (node.specifiers && node.specifiers.length > 0) {
  9841. return true;
  9842. }
  9843. return super.maybeParseExportDefaultSpecifier(node);
  9844. }
  9845. checkExport(node) {
  9846. const {
  9847. specifiers
  9848. } = node;
  9849. if (specifiers != null && specifiers.length) {
  9850. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  9851. }
  9852. super.checkExport(node);
  9853. node.specifiers = specifiers;
  9854. }
  9855. parseImport(node) {
  9856. const placeholder = this.parsePlaceholder("Identifier");
  9857. if (!placeholder) return super.parseImport(node);
  9858. node.specifiers = [];
  9859. if (!this.isContextual(97) && !this.match(12)) {
  9860. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  9861. this.semicolon();
  9862. return this.finishNode(node, "ImportDeclaration");
  9863. }
  9864. const specifier = this.startNodeAtNode(placeholder);
  9865. specifier.local = placeholder;
  9866. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  9867. if (this.eat(12)) {
  9868. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  9869. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  9870. }
  9871. this.expectContextual(97);
  9872. node.source = this.parseImportSource();
  9873. this.semicolon();
  9874. return this.finishNode(node, "ImportDeclaration");
  9875. }
  9876. parseImportSource() {
  9877. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  9878. }
  9879. assertNoSpace() {
  9880. if (this.state.start > this.state.lastTokEndLoc.index) {
  9881. this.raise(PlaceholderErrors.UnexpectedSpace, {
  9882. at: this.state.lastTokEndLoc
  9883. });
  9884. }
  9885. }
  9886. });
  9887. var v8intrinsic = (superClass => class V8IntrinsicMixin extends superClass {
  9888. parseV8Intrinsic() {
  9889. if (this.match(54)) {
  9890. const v8IntrinsicStartLoc = this.state.startLoc;
  9891. const node = this.startNode();
  9892. this.next();
  9893. if (tokenIsIdentifier(this.state.type)) {
  9894. const name = this.parseIdentifierName();
  9895. const identifier = this.createIdentifier(node, name);
  9896. identifier.type = "V8IntrinsicIdentifier";
  9897. if (this.match(10)) {
  9898. return identifier;
  9899. }
  9900. }
  9901. this.unexpected(v8IntrinsicStartLoc);
  9902. }
  9903. }
  9904. parseExprAtom(refExpressionErrors) {
  9905. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  9906. }
  9907. });
  9908. function hasPlugin(plugins, expectedConfig) {
  9909. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  9910. const expectedKeys = Object.keys(expectedOptions);
  9911. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  9912. return plugins.some(p => {
  9913. if (typeof p === "string") {
  9914. return expectedOptionsIsEmpty && p === expectedName;
  9915. } else {
  9916. const [pluginName, pluginOptions] = p;
  9917. if (pluginName !== expectedName) {
  9918. return false;
  9919. }
  9920. for (const key of expectedKeys) {
  9921. if (pluginOptions[key] !== expectedOptions[key]) {
  9922. return false;
  9923. }
  9924. }
  9925. return true;
  9926. }
  9927. });
  9928. }
  9929. function getPluginOption(plugins, name, option) {
  9930. const plugin = plugins.find(plugin => {
  9931. if (Array.isArray(plugin)) {
  9932. return plugin[0] === name;
  9933. } else {
  9934. return plugin === name;
  9935. }
  9936. });
  9937. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  9938. return plugin[1][option];
  9939. }
  9940. return null;
  9941. }
  9942. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  9943. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  9944. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  9945. function validatePlugins(plugins) {
  9946. if (hasPlugin(plugins, "decorators")) {
  9947. if (hasPlugin(plugins, "decorators-legacy")) {
  9948. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  9949. }
  9950. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  9951. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  9952. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  9953. }
  9954. const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
  9955. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  9956. throw new Error("'allowCallParenthesized' must be a boolean.");
  9957. }
  9958. }
  9959. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  9960. throw new Error("Cannot combine flow and typescript plugins.");
  9961. }
  9962. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  9963. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  9964. }
  9965. if (hasPlugin(plugins, "pipelineOperator")) {
  9966. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  9967. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  9968. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  9969. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  9970. }
  9971. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  9972. syntaxType: "hash"
  9973. }]);
  9974. if (proposal === "hack") {
  9975. if (hasPlugin(plugins, "placeholders")) {
  9976. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  9977. }
  9978. if (hasPlugin(plugins, "v8intrinsic")) {
  9979. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  9980. }
  9981. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  9982. if (!TOPIC_TOKENS.includes(topicToken)) {
  9983. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  9984. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  9985. }
  9986. if (topicToken === "#" && tupleSyntaxIsHash) {
  9987. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9988. }
  9989. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  9990. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9991. }
  9992. }
  9993. if (hasPlugin(plugins, "moduleAttributes")) {
  9994. {
  9995. if (hasPlugin(plugins, "importAssertions")) {
  9996. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  9997. }
  9998. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  9999. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10000. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10001. }
  10002. }
  10003. }
  10004. if (hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") != null && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  10005. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10006. }
  10007. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  10008. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10009. error.missingPlugins = "doExpressions";
  10010. throw error;
  10011. }
  10012. }
  10013. const mixinPlugins = {
  10014. estree,
  10015. jsx,
  10016. flow,
  10017. typescript,
  10018. v8intrinsic,
  10019. placeholders
  10020. };
  10021. const mixinPluginNames = Object.keys(mixinPlugins);
  10022. const defaultOptions = {
  10023. sourceType: "script",
  10024. sourceFilename: undefined,
  10025. startColumn: 0,
  10026. startLine: 1,
  10027. allowAwaitOutsideFunction: false,
  10028. allowReturnOutsideFunction: false,
  10029. allowImportExportEverywhere: false,
  10030. allowSuperOutsideMethod: false,
  10031. allowUndeclaredExports: false,
  10032. plugins: [],
  10033. strictMode: null,
  10034. ranges: false,
  10035. tokens: false,
  10036. createParenthesizedExpressions: false,
  10037. errorRecovery: false,
  10038. attachComment: true
  10039. };
  10040. function getOptions(opts) {
  10041. const options = {};
  10042. for (const key of Object.keys(defaultOptions)) {
  10043. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  10044. }
  10045. return options;
  10046. }
  10047. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  10048. const unwrapParenthesizedExpression = node => {
  10049. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  10050. };
  10051. class LValParser extends NodeUtils {
  10052. toAssignable(node, isLHS = false) {
  10053. var _node$extra, _node$extra3;
  10054. let parenthesized = undefined;
  10055. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  10056. parenthesized = unwrapParenthesizedExpression(node);
  10057. if (isLHS) {
  10058. if (parenthesized.type === "Identifier") {
  10059. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, {
  10060. at: node
  10061. });
  10062. } else if (parenthesized.type !== "MemberExpression") {
  10063. this.raise(Errors.InvalidParenthesizedAssignment, {
  10064. at: node
  10065. });
  10066. }
  10067. } else {
  10068. this.raise(Errors.InvalidParenthesizedAssignment, {
  10069. at: node
  10070. });
  10071. }
  10072. }
  10073. switch (node.type) {
  10074. case "Identifier":
  10075. case "ObjectPattern":
  10076. case "ArrayPattern":
  10077. case "AssignmentPattern":
  10078. case "RestElement":
  10079. break;
  10080. case "ObjectExpression":
  10081. node.type = "ObjectPattern";
  10082. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  10083. var _node$extra2;
  10084. const prop = node.properties[i];
  10085. const isLast = i === last;
  10086. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  10087. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  10088. this.raise(Errors.RestTrailingComma, {
  10089. at: node.extra.trailingCommaLoc
  10090. });
  10091. }
  10092. }
  10093. break;
  10094. case "ObjectProperty":
  10095. {
  10096. const {
  10097. key,
  10098. value
  10099. } = node;
  10100. if (this.isPrivateName(key)) {
  10101. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  10102. }
  10103. this.toAssignable(value, isLHS);
  10104. break;
  10105. }
  10106. case "SpreadElement":
  10107. {
  10108. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  10109. }
  10110. case "ArrayExpression":
  10111. node.type = "ArrayPattern";
  10112. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  10113. break;
  10114. case "AssignmentExpression":
  10115. if (node.operator !== "=") {
  10116. this.raise(Errors.MissingEqInAssignment, {
  10117. at: node.left.loc.end
  10118. });
  10119. }
  10120. node.type = "AssignmentPattern";
  10121. delete node.operator;
  10122. this.toAssignable(node.left, isLHS);
  10123. break;
  10124. case "ParenthesizedExpression":
  10125. this.toAssignable(parenthesized, isLHS);
  10126. break;
  10127. }
  10128. }
  10129. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  10130. if (prop.type === "ObjectMethod") {
  10131. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  10132. at: prop.key
  10133. });
  10134. } else if (prop.type === "SpreadElement") {
  10135. prop.type = "RestElement";
  10136. const arg = prop.argument;
  10137. this.checkToRestConversion(arg, false);
  10138. this.toAssignable(arg, isLHS);
  10139. if (!isLast) {
  10140. this.raise(Errors.RestTrailingComma, {
  10141. at: prop
  10142. });
  10143. }
  10144. } else {
  10145. this.toAssignable(prop, isLHS);
  10146. }
  10147. }
  10148. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  10149. const end = exprList.length - 1;
  10150. for (let i = 0; i <= end; i++) {
  10151. const elt = exprList[i];
  10152. if (!elt) continue;
  10153. if (elt.type === "SpreadElement") {
  10154. elt.type = "RestElement";
  10155. const arg = elt.argument;
  10156. this.checkToRestConversion(arg, true);
  10157. this.toAssignable(arg, isLHS);
  10158. } else {
  10159. this.toAssignable(elt, isLHS);
  10160. }
  10161. if (elt.type === "RestElement") {
  10162. if (i < end) {
  10163. this.raise(Errors.RestTrailingComma, {
  10164. at: elt
  10165. });
  10166. } else if (trailingCommaLoc) {
  10167. this.raise(Errors.RestTrailingComma, {
  10168. at: trailingCommaLoc
  10169. });
  10170. }
  10171. }
  10172. }
  10173. }
  10174. isAssignable(node, isBinding) {
  10175. switch (node.type) {
  10176. case "Identifier":
  10177. case "ObjectPattern":
  10178. case "ArrayPattern":
  10179. case "AssignmentPattern":
  10180. case "RestElement":
  10181. return true;
  10182. case "ObjectExpression":
  10183. {
  10184. const last = node.properties.length - 1;
  10185. return node.properties.every((prop, i) => {
  10186. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  10187. });
  10188. }
  10189. case "ObjectProperty":
  10190. return this.isAssignable(node.value);
  10191. case "SpreadElement":
  10192. return this.isAssignable(node.argument);
  10193. case "ArrayExpression":
  10194. return node.elements.every(element => element === null || this.isAssignable(element));
  10195. case "AssignmentExpression":
  10196. return node.operator === "=";
  10197. case "ParenthesizedExpression":
  10198. return this.isAssignable(node.expression);
  10199. case "MemberExpression":
  10200. case "OptionalMemberExpression":
  10201. return !isBinding;
  10202. default:
  10203. return false;
  10204. }
  10205. }
  10206. toReferencedList(exprList, isParenthesizedExpr) {
  10207. return exprList;
  10208. }
  10209. toReferencedListDeep(exprList, isParenthesizedExpr) {
  10210. this.toReferencedList(exprList, isParenthesizedExpr);
  10211. for (const expr of exprList) {
  10212. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  10213. this.toReferencedListDeep(expr.elements);
  10214. }
  10215. }
  10216. }
  10217. parseSpread(refExpressionErrors) {
  10218. const node = this.startNode();
  10219. this.next();
  10220. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  10221. return this.finishNode(node, "SpreadElement");
  10222. }
  10223. parseRestBinding() {
  10224. const node = this.startNode();
  10225. this.next();
  10226. node.argument = this.parseBindingAtom();
  10227. return this.finishNode(node, "RestElement");
  10228. }
  10229. parseBindingAtom() {
  10230. switch (this.state.type) {
  10231. case 0:
  10232. {
  10233. const node = this.startNode();
  10234. this.next();
  10235. node.elements = this.parseBindingList(3, 93, true);
  10236. return this.finishNode(node, "ArrayPattern");
  10237. }
  10238. case 5:
  10239. return this.parseObjectLike(8, true);
  10240. }
  10241. return this.parseIdentifier();
  10242. }
  10243. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  10244. const elts = [];
  10245. let first = true;
  10246. while (!this.eat(close)) {
  10247. if (first) {
  10248. first = false;
  10249. } else {
  10250. this.expect(12);
  10251. }
  10252. if (allowEmpty && this.match(12)) {
  10253. elts.push(null);
  10254. } else if (this.eat(close)) {
  10255. break;
  10256. } else if (this.match(21)) {
  10257. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  10258. if (!this.checkCommaAfterRest(closeCharCode)) {
  10259. this.expect(close);
  10260. break;
  10261. }
  10262. } else {
  10263. const decorators = [];
  10264. if (this.match(26) && this.hasPlugin("decorators")) {
  10265. this.raise(Errors.UnsupportedParameterDecorator, {
  10266. at: this.state.startLoc
  10267. });
  10268. }
  10269. while (this.match(26)) {
  10270. decorators.push(this.parseDecorator());
  10271. }
  10272. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  10273. }
  10274. }
  10275. return elts;
  10276. }
  10277. parseBindingRestProperty(prop) {
  10278. this.next();
  10279. prop.argument = this.parseIdentifier();
  10280. this.checkCommaAfterRest(125);
  10281. return this.finishNode(prop, "RestElement");
  10282. }
  10283. parseBindingProperty() {
  10284. const prop = this.startNode();
  10285. const {
  10286. type,
  10287. startLoc
  10288. } = this.state;
  10289. if (type === 21) {
  10290. return this.parseBindingRestProperty(prop);
  10291. } else if (type === 136) {
  10292. this.expectPlugin("destructuringPrivate", startLoc);
  10293. this.classScope.usePrivateName(this.state.value, startLoc);
  10294. prop.key = this.parsePrivateName();
  10295. } else {
  10296. this.parsePropertyName(prop);
  10297. }
  10298. prop.method = false;
  10299. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  10300. }
  10301. parseAssignableListItem(allowModifiers, decorators) {
  10302. const left = this.parseMaybeDefault();
  10303. this.parseAssignableListItemTypes(left);
  10304. const elt = this.parseMaybeDefault(left.loc.start, left);
  10305. if (decorators.length) {
  10306. left.decorators = decorators;
  10307. }
  10308. return elt;
  10309. }
  10310. parseAssignableListItemTypes(param) {
  10311. return param;
  10312. }
  10313. parseMaybeDefault(startLoc, left) {
  10314. var _startLoc, _left;
  10315. (_startLoc = startLoc) != null ? _startLoc : startLoc = this.state.startLoc;
  10316. left = (_left = left) != null ? _left : this.parseBindingAtom();
  10317. if (!this.eat(29)) return left;
  10318. const node = this.startNodeAt(startLoc);
  10319. node.left = left;
  10320. node.right = this.parseMaybeAssignAllowIn();
  10321. return this.finishNode(node, "AssignmentPattern");
  10322. }
  10323. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  10324. return getOwn({
  10325. AssignmentPattern: "left",
  10326. RestElement: "argument",
  10327. ObjectProperty: "value",
  10328. ParenthesizedExpression: "expression",
  10329. ArrayPattern: "elements",
  10330. ObjectPattern: "properties"
  10331. }, type);
  10332. }
  10333. checkLVal(expression, {
  10334. in: ancestor,
  10335. binding = BIND_NONE,
  10336. checkClashes = false,
  10337. strictModeChanged = false,
  10338. hasParenthesizedAncestor = false
  10339. }) {
  10340. var _expression$extra;
  10341. const type = expression.type;
  10342. if (this.isObjectMethod(expression)) return;
  10343. if (type === "MemberExpression") {
  10344. if (binding !== BIND_NONE) {
  10345. this.raise(Errors.InvalidPropertyBindingPattern, {
  10346. at: expression
  10347. });
  10348. }
  10349. return;
  10350. }
  10351. if (type === "Identifier") {
  10352. this.checkIdentifier(expression, binding, strictModeChanged);
  10353. const {
  10354. name
  10355. } = expression;
  10356. if (checkClashes) {
  10357. if (checkClashes.has(name)) {
  10358. this.raise(Errors.ParamDupe, {
  10359. at: expression
  10360. });
  10361. } else {
  10362. checkClashes.add(name);
  10363. }
  10364. }
  10365. return;
  10366. }
  10367. const validity = this.isValidLVal(type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  10368. if (validity === true) return;
  10369. if (validity === false) {
  10370. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  10371. this.raise(ParseErrorClass, {
  10372. at: expression,
  10373. ancestor
  10374. });
  10375. return;
  10376. }
  10377. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  10378. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" || type === "ParenthesizedExpression" ? {
  10379. type
  10380. } : ancestor;
  10381. for (const child of [].concat(expression[key])) {
  10382. if (child) {
  10383. this.checkLVal(child, {
  10384. in: nextAncestor,
  10385. binding,
  10386. checkClashes,
  10387. strictModeChanged,
  10388. hasParenthesizedAncestor: isParenthesizedExpression
  10389. });
  10390. }
  10391. }
  10392. }
  10393. checkIdentifier(at, bindingType, strictModeChanged = false) {
  10394. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  10395. if (bindingType === BIND_NONE) {
  10396. this.raise(Errors.StrictEvalArguments, {
  10397. at,
  10398. referenceName: at.name
  10399. });
  10400. } else {
  10401. this.raise(Errors.StrictEvalArgumentsBinding, {
  10402. at,
  10403. bindingName: at.name
  10404. });
  10405. }
  10406. }
  10407. if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
  10408. this.raise(Errors.LetInLexicalBinding, {
  10409. at
  10410. });
  10411. }
  10412. if (!(bindingType & BIND_NONE)) {
  10413. this.declareNameFromIdentifier(at, bindingType);
  10414. }
  10415. }
  10416. declareNameFromIdentifier(identifier, binding) {
  10417. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  10418. }
  10419. checkToRestConversion(node, allowPattern) {
  10420. switch (node.type) {
  10421. case "ParenthesizedExpression":
  10422. this.checkToRestConversion(node.expression, allowPattern);
  10423. break;
  10424. case "Identifier":
  10425. case "MemberExpression":
  10426. break;
  10427. case "ArrayExpression":
  10428. case "ObjectExpression":
  10429. if (allowPattern) break;
  10430. default:
  10431. this.raise(Errors.InvalidRestAssignmentPattern, {
  10432. at: node
  10433. });
  10434. }
  10435. }
  10436. checkCommaAfterRest(close) {
  10437. if (!this.match(12)) {
  10438. return false;
  10439. }
  10440. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  10441. at: this.state.startLoc
  10442. });
  10443. return true;
  10444. }
  10445. }
  10446. class ExpressionParser extends LValParser {
  10447. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10448. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10449. return;
  10450. }
  10451. const key = prop.key;
  10452. const name = key.type === "Identifier" ? key.name : key.value;
  10453. if (name === "__proto__") {
  10454. if (isRecord) {
  10455. this.raise(Errors.RecordNoProto, {
  10456. at: key
  10457. });
  10458. return;
  10459. }
  10460. if (protoRef.used) {
  10461. if (refExpressionErrors) {
  10462. if (refExpressionErrors.doubleProtoLoc === null) {
  10463. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10464. }
  10465. } else {
  10466. this.raise(Errors.DuplicateProto, {
  10467. at: key
  10468. });
  10469. }
  10470. }
  10471. protoRef.used = true;
  10472. }
  10473. }
  10474. shouldExitDescending(expr, potentialArrowAt) {
  10475. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10476. }
  10477. getExpression() {
  10478. this.enterInitialScopes();
  10479. this.nextToken();
  10480. const expr = this.parseExpression();
  10481. if (!this.match(137)) {
  10482. this.unexpected();
  10483. }
  10484. this.finalizeRemainingComments();
  10485. expr.comments = this.state.comments;
  10486. expr.errors = this.state.errors;
  10487. if (this.options.tokens) {
  10488. expr.tokens = this.tokens;
  10489. }
  10490. return expr;
  10491. }
  10492. parseExpression(disallowIn, refExpressionErrors) {
  10493. if (disallowIn) {
  10494. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10495. }
  10496. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10497. }
  10498. parseExpressionBase(refExpressionErrors) {
  10499. const startLoc = this.state.startLoc;
  10500. const expr = this.parseMaybeAssign(refExpressionErrors);
  10501. if (this.match(12)) {
  10502. const node = this.startNodeAt(startLoc);
  10503. node.expressions = [expr];
  10504. while (this.eat(12)) {
  10505. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10506. }
  10507. this.toReferencedList(node.expressions);
  10508. return this.finishNode(node, "SequenceExpression");
  10509. }
  10510. return expr;
  10511. }
  10512. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10513. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10514. }
  10515. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10516. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10517. }
  10518. setOptionalParametersError(refExpressionErrors, resultError) {
  10519. var _resultError$loc;
  10520. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10521. }
  10522. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10523. const startLoc = this.state.startLoc;
  10524. if (this.isContextual(106)) {
  10525. if (this.prodParam.hasYield) {
  10526. let left = this.parseYield();
  10527. if (afterLeftParse) {
  10528. left = afterLeftParse.call(this, left, startLoc);
  10529. }
  10530. return left;
  10531. }
  10532. }
  10533. let ownExpressionErrors;
  10534. if (refExpressionErrors) {
  10535. ownExpressionErrors = false;
  10536. } else {
  10537. refExpressionErrors = new ExpressionErrors();
  10538. ownExpressionErrors = true;
  10539. }
  10540. const {
  10541. type
  10542. } = this.state;
  10543. if (type === 10 || tokenIsIdentifier(type)) {
  10544. this.state.potentialArrowAt = this.state.start;
  10545. }
  10546. let left = this.parseMaybeConditional(refExpressionErrors);
  10547. if (afterLeftParse) {
  10548. left = afterLeftParse.call(this, left, startLoc);
  10549. }
  10550. if (tokenIsAssignment(this.state.type)) {
  10551. const node = this.startNodeAt(startLoc);
  10552. const operator = this.state.value;
  10553. node.operator = operator;
  10554. if (this.match(29)) {
  10555. this.toAssignable(left, true);
  10556. node.left = left;
  10557. const startIndex = startLoc.index;
  10558. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10559. refExpressionErrors.doubleProtoLoc = null;
  10560. }
  10561. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10562. refExpressionErrors.shorthandAssignLoc = null;
  10563. }
  10564. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10565. this.checkDestructuringPrivate(refExpressionErrors);
  10566. refExpressionErrors.privateKeyLoc = null;
  10567. }
  10568. } else {
  10569. node.left = left;
  10570. }
  10571. this.next();
  10572. node.right = this.parseMaybeAssign();
  10573. this.checkLVal(left, {
  10574. in: this.finishNode(node, "AssignmentExpression")
  10575. });
  10576. return node;
  10577. } else if (ownExpressionErrors) {
  10578. this.checkExpressionErrors(refExpressionErrors, true);
  10579. }
  10580. return left;
  10581. }
  10582. parseMaybeConditional(refExpressionErrors) {
  10583. const startLoc = this.state.startLoc;
  10584. const potentialArrowAt = this.state.potentialArrowAt;
  10585. const expr = this.parseExprOps(refExpressionErrors);
  10586. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10587. return expr;
  10588. }
  10589. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10590. }
  10591. parseConditional(expr, startLoc, refExpressionErrors) {
  10592. if (this.eat(17)) {
  10593. const node = this.startNodeAt(startLoc);
  10594. node.test = expr;
  10595. node.consequent = this.parseMaybeAssignAllowIn();
  10596. this.expect(14);
  10597. node.alternate = this.parseMaybeAssign();
  10598. return this.finishNode(node, "ConditionalExpression");
  10599. }
  10600. return expr;
  10601. }
  10602. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10603. return this.match(136) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10604. }
  10605. parseExprOps(refExpressionErrors) {
  10606. const startLoc = this.state.startLoc;
  10607. const potentialArrowAt = this.state.potentialArrowAt;
  10608. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10609. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10610. return expr;
  10611. }
  10612. return this.parseExprOp(expr, startLoc, -1);
  10613. }
  10614. parseExprOp(left, leftStartLoc, minPrec) {
  10615. if (this.isPrivateName(left)) {
  10616. const value = this.getPrivateNameSV(left);
  10617. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10618. this.raise(Errors.PrivateInExpectedIn, {
  10619. at: left,
  10620. identifierName: value
  10621. });
  10622. }
  10623. this.classScope.usePrivateName(value, left.loc.start);
  10624. }
  10625. const op = this.state.type;
  10626. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10627. let prec = tokenOperatorPrecedence(op);
  10628. if (prec > minPrec) {
  10629. if (op === 39) {
  10630. this.expectPlugin("pipelineOperator");
  10631. if (this.state.inFSharpPipelineDirectBody) {
  10632. return left;
  10633. }
  10634. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10635. }
  10636. const node = this.startNodeAt(leftStartLoc);
  10637. node.left = left;
  10638. node.operator = this.state.value;
  10639. const logical = op === 41 || op === 42;
  10640. const coalesce = op === 40;
  10641. if (coalesce) {
  10642. prec = tokenOperatorPrecedence(42);
  10643. }
  10644. this.next();
  10645. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10646. proposal: "minimal"
  10647. }])) {
  10648. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10649. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10650. at: this.state.startLoc
  10651. });
  10652. }
  10653. }
  10654. node.right = this.parseExprOpRightExpr(op, prec);
  10655. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10656. const nextOp = this.state.type;
  10657. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10658. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10659. at: this.state.startLoc
  10660. });
  10661. }
  10662. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10663. }
  10664. }
  10665. return left;
  10666. }
  10667. parseExprOpRightExpr(op, prec) {
  10668. const startLoc = this.state.startLoc;
  10669. switch (op) {
  10670. case 39:
  10671. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10672. case "hack":
  10673. return this.withTopicBindingContext(() => {
  10674. return this.parseHackPipeBody();
  10675. });
  10676. case "smart":
  10677. return this.withTopicBindingContext(() => {
  10678. if (this.prodParam.hasYield && this.isContextual(106)) {
  10679. throw this.raise(Errors.PipeBodyIsTighter, {
  10680. at: this.state.startLoc
  10681. });
  10682. }
  10683. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10684. });
  10685. case "fsharp":
  10686. return this.withSoloAwaitPermittingContext(() => {
  10687. return this.parseFSharpPipelineBody(prec);
  10688. });
  10689. }
  10690. default:
  10691. return this.parseExprOpBaseRightExpr(op, prec);
  10692. }
  10693. }
  10694. parseExprOpBaseRightExpr(op, prec) {
  10695. const startLoc = this.state.startLoc;
  10696. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10697. }
  10698. parseHackPipeBody() {
  10699. var _body$extra;
  10700. const {
  10701. startLoc
  10702. } = this.state;
  10703. const body = this.parseMaybeAssign();
  10704. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10705. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10706. this.raise(Errors.PipeUnparenthesizedBody, {
  10707. at: startLoc,
  10708. type: body.type
  10709. });
  10710. }
  10711. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10712. this.raise(Errors.PipeTopicUnused, {
  10713. at: startLoc
  10714. });
  10715. }
  10716. return body;
  10717. }
  10718. checkExponentialAfterUnary(node) {
  10719. if (this.match(57)) {
  10720. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10721. at: node.argument
  10722. });
  10723. }
  10724. }
  10725. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10726. const startLoc = this.state.startLoc;
  10727. const isAwait = this.isContextual(96);
  10728. if (isAwait && this.isAwaitAllowed()) {
  10729. this.next();
  10730. const expr = this.parseAwait(startLoc);
  10731. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10732. return expr;
  10733. }
  10734. const update = this.match(34);
  10735. const node = this.startNode();
  10736. if (tokenIsPrefix(this.state.type)) {
  10737. node.operator = this.state.value;
  10738. node.prefix = true;
  10739. if (this.match(72)) {
  10740. this.expectPlugin("throwExpressions");
  10741. }
  10742. const isDelete = this.match(89);
  10743. this.next();
  10744. node.argument = this.parseMaybeUnary(null, true);
  10745. this.checkExpressionErrors(refExpressionErrors, true);
  10746. if (this.state.strict && isDelete) {
  10747. const arg = node.argument;
  10748. if (arg.type === "Identifier") {
  10749. this.raise(Errors.StrictDelete, {
  10750. at: node
  10751. });
  10752. } else if (this.hasPropertyAsPrivateName(arg)) {
  10753. this.raise(Errors.DeletePrivateField, {
  10754. at: node
  10755. });
  10756. }
  10757. }
  10758. if (!update) {
  10759. if (!sawUnary) {
  10760. this.checkExponentialAfterUnary(node);
  10761. }
  10762. return this.finishNode(node, "UnaryExpression");
  10763. }
  10764. }
  10765. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10766. if (isAwait) {
  10767. const {
  10768. type
  10769. } = this.state;
  10770. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10771. if (startsExpr && !this.isAmbiguousAwait()) {
  10772. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10773. at: startLoc
  10774. });
  10775. return this.parseAwait(startLoc);
  10776. }
  10777. }
  10778. return expr;
  10779. }
  10780. parseUpdate(node, update, refExpressionErrors) {
  10781. if (update) {
  10782. const updateExpressionNode = node;
  10783. this.checkLVal(updateExpressionNode.argument, {
  10784. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10785. });
  10786. return node;
  10787. }
  10788. const startLoc = this.state.startLoc;
  10789. let expr = this.parseExprSubscripts(refExpressionErrors);
  10790. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10791. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10792. const node = this.startNodeAt(startLoc);
  10793. node.operator = this.state.value;
  10794. node.prefix = false;
  10795. node.argument = expr;
  10796. this.next();
  10797. this.checkLVal(expr, {
  10798. in: expr = this.finishNode(node, "UpdateExpression")
  10799. });
  10800. }
  10801. return expr;
  10802. }
  10803. parseExprSubscripts(refExpressionErrors) {
  10804. const startLoc = this.state.startLoc;
  10805. const potentialArrowAt = this.state.potentialArrowAt;
  10806. const expr = this.parseExprAtom(refExpressionErrors);
  10807. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10808. return expr;
  10809. }
  10810. return this.parseSubscripts(expr, startLoc);
  10811. }
  10812. parseSubscripts(base, startLoc, noCalls) {
  10813. const state = {
  10814. optionalChainMember: false,
  10815. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10816. stop: false
  10817. };
  10818. do {
  10819. base = this.parseSubscript(base, startLoc, noCalls, state);
  10820. state.maybeAsyncArrow = false;
  10821. } while (!state.stop);
  10822. return base;
  10823. }
  10824. parseSubscript(base, startLoc, noCalls, state) {
  10825. const {
  10826. type
  10827. } = this.state;
  10828. if (!noCalls && type === 15) {
  10829. return this.parseBind(base, startLoc, noCalls, state);
  10830. } else if (tokenIsTemplate(type)) {
  10831. return this.parseTaggedTemplateExpression(base, startLoc, state);
  10832. }
  10833. let optional = false;
  10834. if (type === 18) {
  10835. if (noCalls && this.lookaheadCharCode() === 40) {
  10836. state.stop = true;
  10837. return base;
  10838. }
  10839. state.optionalChainMember = optional = true;
  10840. this.next();
  10841. }
  10842. if (!noCalls && this.match(10)) {
  10843. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  10844. } else {
  10845. const computed = this.eat(0);
  10846. if (computed || optional || this.eat(16)) {
  10847. return this.parseMember(base, startLoc, state, computed, optional);
  10848. } else {
  10849. state.stop = true;
  10850. return base;
  10851. }
  10852. }
  10853. }
  10854. parseMember(base, startLoc, state, computed, optional) {
  10855. const node = this.startNodeAt(startLoc);
  10856. node.object = base;
  10857. node.computed = computed;
  10858. if (computed) {
  10859. node.property = this.parseExpression();
  10860. this.expect(3);
  10861. } else if (this.match(136)) {
  10862. if (base.type === "Super") {
  10863. this.raise(Errors.SuperPrivateField, {
  10864. at: startLoc
  10865. });
  10866. }
  10867. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10868. node.property = this.parsePrivateName();
  10869. } else {
  10870. node.property = this.parseIdentifier(true);
  10871. }
  10872. if (state.optionalChainMember) {
  10873. node.optional = optional;
  10874. return this.finishNode(node, "OptionalMemberExpression");
  10875. } else {
  10876. return this.finishNode(node, "MemberExpression");
  10877. }
  10878. }
  10879. parseBind(base, startLoc, noCalls, state) {
  10880. const node = this.startNodeAt(startLoc);
  10881. node.object = base;
  10882. this.next();
  10883. node.callee = this.parseNoCallExpr();
  10884. state.stop = true;
  10885. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, noCalls);
  10886. }
  10887. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  10888. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10889. let refExpressionErrors = null;
  10890. this.state.maybeInArrowParameters = true;
  10891. this.next();
  10892. const node = this.startNodeAt(startLoc);
  10893. node.callee = base;
  10894. const {
  10895. maybeAsyncArrow,
  10896. optionalChainMember
  10897. } = state;
  10898. if (maybeAsyncArrow) {
  10899. this.expressionScope.enter(newAsyncArrowScope());
  10900. refExpressionErrors = new ExpressionErrors();
  10901. }
  10902. if (optionalChainMember) {
  10903. node.optional = optional;
  10904. }
  10905. if (optional) {
  10906. node.arguments = this.parseCallExpressionArguments(11);
  10907. } else {
  10908. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10909. }
  10910. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10911. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10912. state.stop = true;
  10913. this.checkDestructuringPrivate(refExpressionErrors);
  10914. this.expressionScope.validateAsPattern();
  10915. this.expressionScope.exit();
  10916. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  10917. } else {
  10918. if (maybeAsyncArrow) {
  10919. this.checkExpressionErrors(refExpressionErrors, true);
  10920. this.expressionScope.exit();
  10921. }
  10922. this.toReferencedArguments(finishedNode);
  10923. }
  10924. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10925. return finishedNode;
  10926. }
  10927. toReferencedArguments(node, isParenthesizedExpr) {
  10928. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10929. }
  10930. parseTaggedTemplateExpression(base, startLoc, state) {
  10931. const node = this.startNodeAt(startLoc);
  10932. node.tag = base;
  10933. node.quasi = this.parseTemplate(true);
  10934. if (state.optionalChainMember) {
  10935. this.raise(Errors.OptionalChainingNoTemplate, {
  10936. at: startLoc
  10937. });
  10938. }
  10939. return this.finishNode(node, "TaggedTemplateExpression");
  10940. }
  10941. atPossibleAsyncArrow(base) {
  10942. 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;
  10943. }
  10944. finishCallExpression(node, optional) {
  10945. if (node.callee.type === "Import") {
  10946. if (node.arguments.length === 2) {
  10947. {
  10948. if (!this.hasPlugin("moduleAttributes")) {
  10949. this.expectPlugin("importAssertions");
  10950. }
  10951. }
  10952. }
  10953. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10954. this.raise(Errors.ImportCallArity, {
  10955. at: node,
  10956. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10957. });
  10958. } else {
  10959. for (const arg of node.arguments) {
  10960. if (arg.type === "SpreadElement") {
  10961. this.raise(Errors.ImportCallSpreadArgument, {
  10962. at: arg
  10963. });
  10964. }
  10965. }
  10966. }
  10967. }
  10968. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10969. }
  10970. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10971. const elts = [];
  10972. let first = true;
  10973. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10974. this.state.inFSharpPipelineDirectBody = false;
  10975. while (!this.eat(close)) {
  10976. if (first) {
  10977. first = false;
  10978. } else {
  10979. this.expect(12);
  10980. if (this.match(close)) {
  10981. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10982. this.raise(Errors.ImportCallArgumentTrailingComma, {
  10983. at: this.state.lastTokStartLoc
  10984. });
  10985. }
  10986. if (nodeForExtra) {
  10987. this.addTrailingCommaExtraToNode(nodeForExtra);
  10988. }
  10989. this.next();
  10990. break;
  10991. }
  10992. }
  10993. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10994. }
  10995. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10996. return elts;
  10997. }
  10998. shouldParseAsyncArrow() {
  10999. return this.match(19) && !this.canInsertSemicolon();
  11000. }
  11001. parseAsyncArrowFromCallExpression(node, call) {
  11002. var _call$extra;
  11003. this.resetPreviousNodeTrailingComments(call);
  11004. this.expect(19);
  11005. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  11006. if (call.innerComments) {
  11007. setInnerComments(node, call.innerComments);
  11008. }
  11009. if (call.callee.trailingComments) {
  11010. setInnerComments(node, call.callee.trailingComments);
  11011. }
  11012. return node;
  11013. }
  11014. parseNoCallExpr() {
  11015. const startLoc = this.state.startLoc;
  11016. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  11017. }
  11018. parseExprAtom(refExpressionErrors) {
  11019. let node;
  11020. let decorators = null;
  11021. const {
  11022. type
  11023. } = this.state;
  11024. switch (type) {
  11025. case 79:
  11026. return this.parseSuper();
  11027. case 83:
  11028. node = this.startNode();
  11029. this.next();
  11030. if (this.match(16)) {
  11031. return this.parseImportMetaProperty(node);
  11032. }
  11033. if (!this.match(10)) {
  11034. this.raise(Errors.UnsupportedImport, {
  11035. at: this.state.lastTokStartLoc
  11036. });
  11037. }
  11038. return this.finishNode(node, "Import");
  11039. case 78:
  11040. node = this.startNode();
  11041. this.next();
  11042. return this.finishNode(node, "ThisExpression");
  11043. case 90:
  11044. {
  11045. return this.parseDo(this.startNode(), false);
  11046. }
  11047. case 56:
  11048. case 31:
  11049. {
  11050. this.readRegexp();
  11051. return this.parseRegExpLiteral(this.state.value);
  11052. }
  11053. case 132:
  11054. return this.parseNumericLiteral(this.state.value);
  11055. case 133:
  11056. return this.parseBigIntLiteral(this.state.value);
  11057. case 134:
  11058. return this.parseDecimalLiteral(this.state.value);
  11059. case 131:
  11060. return this.parseStringLiteral(this.state.value);
  11061. case 84:
  11062. return this.parseNullLiteral();
  11063. case 85:
  11064. return this.parseBooleanLiteral(true);
  11065. case 86:
  11066. return this.parseBooleanLiteral(false);
  11067. case 10:
  11068. {
  11069. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11070. return this.parseParenAndDistinguishExpression(canBeArrow);
  11071. }
  11072. case 2:
  11073. case 1:
  11074. {
  11075. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  11076. }
  11077. case 0:
  11078. {
  11079. return this.parseArrayLike(3, true, false, refExpressionErrors);
  11080. }
  11081. case 6:
  11082. case 7:
  11083. {
  11084. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11085. }
  11086. case 5:
  11087. {
  11088. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11089. }
  11090. case 68:
  11091. return this.parseFunctionOrFunctionSent();
  11092. case 26:
  11093. decorators = this.parseDecorators();
  11094. case 80:
  11095. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  11096. case 77:
  11097. return this.parseNewOrNewTarget();
  11098. case 25:
  11099. case 24:
  11100. return this.parseTemplate(false);
  11101. case 15:
  11102. {
  11103. node = this.startNode();
  11104. this.next();
  11105. node.object = null;
  11106. const callee = node.callee = this.parseNoCallExpr();
  11107. if (callee.type === "MemberExpression") {
  11108. return this.finishNode(node, "BindExpression");
  11109. } else {
  11110. throw this.raise(Errors.UnsupportedBind, {
  11111. at: callee
  11112. });
  11113. }
  11114. }
  11115. case 136:
  11116. {
  11117. this.raise(Errors.PrivateInExpectedIn, {
  11118. at: this.state.startLoc,
  11119. identifierName: this.state.value
  11120. });
  11121. return this.parsePrivateName();
  11122. }
  11123. case 33:
  11124. {
  11125. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11126. }
  11127. case 32:
  11128. {
  11129. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11130. }
  11131. case 37:
  11132. case 38:
  11133. {
  11134. return this.parseTopicReference("hack");
  11135. }
  11136. case 44:
  11137. case 54:
  11138. case 27:
  11139. {
  11140. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11141. if (pipeProposal) {
  11142. return this.parseTopicReference(pipeProposal);
  11143. } else {
  11144. throw this.unexpected();
  11145. }
  11146. }
  11147. case 47:
  11148. {
  11149. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11150. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11151. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11152. break;
  11153. } else {
  11154. throw this.unexpected();
  11155. }
  11156. }
  11157. default:
  11158. if (tokenIsIdentifier(type)) {
  11159. if (this.isContextual(125) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  11160. return this.parseModuleExpression();
  11161. }
  11162. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11163. const containsEsc = this.state.containsEsc;
  11164. const id = this.parseIdentifier();
  11165. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11166. const {
  11167. type
  11168. } = this.state;
  11169. if (type === 68) {
  11170. this.resetPreviousNodeTrailingComments(id);
  11171. this.next();
  11172. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  11173. } else if (tokenIsIdentifier(type)) {
  11174. if (this.lookaheadCharCode() === 61) {
  11175. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11176. } else {
  11177. return id;
  11178. }
  11179. } else if (type === 90) {
  11180. this.resetPreviousNodeTrailingComments(id);
  11181. return this.parseDo(this.startNodeAtNode(id), true);
  11182. }
  11183. }
  11184. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11185. this.next();
  11186. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11187. }
  11188. return id;
  11189. } else {
  11190. throw this.unexpected();
  11191. }
  11192. }
  11193. }
  11194. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11195. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11196. if (pipeProposal) {
  11197. this.state.type = topicTokenType;
  11198. this.state.value = topicTokenValue;
  11199. this.state.pos--;
  11200. this.state.end--;
  11201. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11202. return this.parseTopicReference(pipeProposal);
  11203. } else {
  11204. throw this.unexpected();
  11205. }
  11206. }
  11207. parseTopicReference(pipeProposal) {
  11208. const node = this.startNode();
  11209. const startLoc = this.state.startLoc;
  11210. const tokenType = this.state.type;
  11211. this.next();
  11212. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11213. }
  11214. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11215. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11216. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  11217. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11218. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  11219. at: startLoc
  11220. });
  11221. }
  11222. this.registerTopicReference();
  11223. return this.finishNode(node, nodeType);
  11224. } else {
  11225. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  11226. at: startLoc,
  11227. token: tokenLabelName(tokenType)
  11228. });
  11229. }
  11230. }
  11231. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11232. switch (pipeProposal) {
  11233. case "hack":
  11234. {
  11235. return this.hasPlugin(["pipelineOperator", {
  11236. topicToken: tokenLabelName(tokenType)
  11237. }]);
  11238. }
  11239. case "smart":
  11240. return tokenType === 27;
  11241. default:
  11242. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  11243. at: startLoc
  11244. });
  11245. }
  11246. }
  11247. parseAsyncArrowUnaryFunction(node) {
  11248. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11249. const params = [this.parseIdentifier()];
  11250. this.prodParam.exit();
  11251. if (this.hasPrecedingLineBreak()) {
  11252. this.raise(Errors.LineTerminatorBeforeArrow, {
  11253. at: this.state.curPosition()
  11254. });
  11255. }
  11256. this.expect(19);
  11257. return this.parseArrowExpression(node, params, true);
  11258. }
  11259. parseDo(node, isAsync) {
  11260. this.expectPlugin("doExpressions");
  11261. if (isAsync) {
  11262. this.expectPlugin("asyncDoExpressions");
  11263. }
  11264. node.async = isAsync;
  11265. this.next();
  11266. const oldLabels = this.state.labels;
  11267. this.state.labels = [];
  11268. if (isAsync) {
  11269. this.prodParam.enter(PARAM_AWAIT);
  11270. node.body = this.parseBlock();
  11271. this.prodParam.exit();
  11272. } else {
  11273. node.body = this.parseBlock();
  11274. }
  11275. this.state.labels = oldLabels;
  11276. return this.finishNode(node, "DoExpression");
  11277. }
  11278. parseSuper() {
  11279. const node = this.startNode();
  11280. this.next();
  11281. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11282. this.raise(Errors.SuperNotAllowed, {
  11283. at: node
  11284. });
  11285. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11286. this.raise(Errors.UnexpectedSuper, {
  11287. at: node
  11288. });
  11289. }
  11290. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11291. this.raise(Errors.UnsupportedSuper, {
  11292. at: node
  11293. });
  11294. }
  11295. return this.finishNode(node, "Super");
  11296. }
  11297. parsePrivateName() {
  11298. const node = this.startNode();
  11299. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11300. const name = this.state.value;
  11301. this.next();
  11302. node.id = this.createIdentifier(id, name);
  11303. return this.finishNode(node, "PrivateName");
  11304. }
  11305. parseFunctionOrFunctionSent() {
  11306. const node = this.startNode();
  11307. this.next();
  11308. if (this.prodParam.hasYield && this.match(16)) {
  11309. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11310. this.next();
  11311. if (this.match(102)) {
  11312. this.expectPlugin("functionSent");
  11313. } else if (!this.hasPlugin("functionSent")) {
  11314. this.unexpected();
  11315. }
  11316. return this.parseMetaProperty(node, meta, "sent");
  11317. }
  11318. return this.parseFunction(node);
  11319. }
  11320. parseMetaProperty(node, meta, propertyName) {
  11321. node.meta = meta;
  11322. const containsEsc = this.state.containsEsc;
  11323. node.property = this.parseIdentifier(true);
  11324. if (node.property.name !== propertyName || containsEsc) {
  11325. this.raise(Errors.UnsupportedMetaProperty, {
  11326. at: node.property,
  11327. target: meta.name,
  11328. onlyValidPropertyName: propertyName
  11329. });
  11330. }
  11331. return this.finishNode(node, "MetaProperty");
  11332. }
  11333. parseImportMetaProperty(node) {
  11334. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11335. this.next();
  11336. if (this.isContextual(100)) {
  11337. if (!this.inModule) {
  11338. this.raise(Errors.ImportMetaOutsideModule, {
  11339. at: id
  11340. });
  11341. }
  11342. this.sawUnambiguousESM = true;
  11343. }
  11344. return this.parseMetaProperty(node, id, "meta");
  11345. }
  11346. parseLiteralAtNode(value, type, node) {
  11347. this.addExtra(node, "rawValue", value);
  11348. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11349. node.value = value;
  11350. this.next();
  11351. return this.finishNode(node, type);
  11352. }
  11353. parseLiteral(value, type) {
  11354. const node = this.startNode();
  11355. return this.parseLiteralAtNode(value, type, node);
  11356. }
  11357. parseStringLiteral(value) {
  11358. return this.parseLiteral(value, "StringLiteral");
  11359. }
  11360. parseNumericLiteral(value) {
  11361. return this.parseLiteral(value, "NumericLiteral");
  11362. }
  11363. parseBigIntLiteral(value) {
  11364. return this.parseLiteral(value, "BigIntLiteral");
  11365. }
  11366. parseDecimalLiteral(value) {
  11367. return this.parseLiteral(value, "DecimalLiteral");
  11368. }
  11369. parseRegExpLiteral(value) {
  11370. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11371. node.pattern = value.pattern;
  11372. node.flags = value.flags;
  11373. return node;
  11374. }
  11375. parseBooleanLiteral(value) {
  11376. const node = this.startNode();
  11377. node.value = value;
  11378. this.next();
  11379. return this.finishNode(node, "BooleanLiteral");
  11380. }
  11381. parseNullLiteral() {
  11382. const node = this.startNode();
  11383. this.next();
  11384. return this.finishNode(node, "NullLiteral");
  11385. }
  11386. parseParenAndDistinguishExpression(canBeArrow) {
  11387. const startLoc = this.state.startLoc;
  11388. let val;
  11389. this.next();
  11390. this.expressionScope.enter(newArrowHeadScope());
  11391. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11392. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11393. this.state.maybeInArrowParameters = true;
  11394. this.state.inFSharpPipelineDirectBody = false;
  11395. const innerStartLoc = this.state.startLoc;
  11396. const exprList = [];
  11397. const refExpressionErrors = new ExpressionErrors();
  11398. let first = true;
  11399. let spreadStartLoc;
  11400. let optionalCommaStartLoc;
  11401. while (!this.match(11)) {
  11402. if (first) {
  11403. first = false;
  11404. } else {
  11405. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11406. if (this.match(11)) {
  11407. optionalCommaStartLoc = this.state.startLoc;
  11408. break;
  11409. }
  11410. }
  11411. if (this.match(21)) {
  11412. const spreadNodeStartLoc = this.state.startLoc;
  11413. spreadStartLoc = this.state.startLoc;
  11414. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11415. if (!this.checkCommaAfterRest(41)) {
  11416. break;
  11417. }
  11418. } else {
  11419. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11420. }
  11421. }
  11422. const innerEndLoc = this.state.lastTokEndLoc;
  11423. this.expect(11);
  11424. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11425. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11426. let arrowNode = this.startNodeAt(startLoc);
  11427. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11428. this.checkDestructuringPrivate(refExpressionErrors);
  11429. this.expressionScope.validateAsPattern();
  11430. this.expressionScope.exit();
  11431. this.parseArrowExpression(arrowNode, exprList, false);
  11432. return arrowNode;
  11433. }
  11434. this.expressionScope.exit();
  11435. if (!exprList.length) {
  11436. this.unexpected(this.state.lastTokStartLoc);
  11437. }
  11438. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11439. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11440. this.checkExpressionErrors(refExpressionErrors, true);
  11441. this.toReferencedListDeep(exprList, true);
  11442. if (exprList.length > 1) {
  11443. val = this.startNodeAt(innerStartLoc);
  11444. val.expressions = exprList;
  11445. this.finishNode(val, "SequenceExpression");
  11446. this.resetEndLocation(val, innerEndLoc);
  11447. } else {
  11448. val = exprList[0];
  11449. }
  11450. return this.wrapParenthesis(startLoc, val);
  11451. }
  11452. wrapParenthesis(startLoc, expression) {
  11453. if (!this.options.createParenthesizedExpressions) {
  11454. this.addExtra(expression, "parenthesized", true);
  11455. this.addExtra(expression, "parenStart", startLoc.index);
  11456. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11457. return expression;
  11458. }
  11459. const parenExpression = this.startNodeAt(startLoc);
  11460. parenExpression.expression = expression;
  11461. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11462. }
  11463. shouldParseArrow(params) {
  11464. return !this.canInsertSemicolon();
  11465. }
  11466. parseArrow(node) {
  11467. if (this.eat(19)) {
  11468. return node;
  11469. }
  11470. }
  11471. parseParenItem(node, startLoc) {
  11472. return node;
  11473. }
  11474. parseNewOrNewTarget() {
  11475. const node = this.startNode();
  11476. this.next();
  11477. if (this.match(16)) {
  11478. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11479. this.next();
  11480. const metaProp = this.parseMetaProperty(node, meta, "target");
  11481. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  11482. this.raise(Errors.UnexpectedNewTarget, {
  11483. at: metaProp
  11484. });
  11485. }
  11486. return metaProp;
  11487. }
  11488. return this.parseNew(node);
  11489. }
  11490. parseNew(node) {
  11491. this.parseNewCallee(node);
  11492. if (this.eat(10)) {
  11493. const args = this.parseExprList(11);
  11494. this.toReferencedList(args);
  11495. node.arguments = args;
  11496. } else {
  11497. node.arguments = [];
  11498. }
  11499. return this.finishNode(node, "NewExpression");
  11500. }
  11501. parseNewCallee(node) {
  11502. var _node$callee$extra;
  11503. node.callee = this.parseNoCallExpr();
  11504. if (node.callee.type === "Import") {
  11505. this.raise(Errors.ImportCallNotNewExpression, {
  11506. at: node.callee
  11507. });
  11508. } else if (this.isOptionalChain(node.callee) && !((_node$callee$extra = node.callee.extra) != null && _node$callee$extra.parenthesized)) {
  11509. this.raise(Errors.OptionalChainingNoNew, {
  11510. at: this.state.lastTokEndLoc
  11511. });
  11512. } else if (this.eat(18)) {
  11513. this.raise(Errors.OptionalChainingNoNew, {
  11514. at: this.state.startLoc
  11515. });
  11516. }
  11517. }
  11518. parseTemplateElement(isTagged) {
  11519. const {
  11520. start,
  11521. startLoc,
  11522. end,
  11523. value
  11524. } = this.state;
  11525. const elemStart = start + 1;
  11526. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11527. if (value === null) {
  11528. if (!isTagged) {
  11529. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11530. at: createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1)
  11531. });
  11532. }
  11533. }
  11534. const isTail = this.match(24);
  11535. const endOffset = isTail ? -1 : -2;
  11536. const elemEnd = end + endOffset;
  11537. elem.value = {
  11538. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11539. cooked: value === null ? null : value.slice(1, endOffset)
  11540. };
  11541. elem.tail = isTail;
  11542. this.next();
  11543. const finishedNode = this.finishNode(elem, "TemplateElement");
  11544. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11545. return finishedNode;
  11546. }
  11547. parseTemplate(isTagged) {
  11548. const node = this.startNode();
  11549. node.expressions = [];
  11550. let curElt = this.parseTemplateElement(isTagged);
  11551. node.quasis = [curElt];
  11552. while (!curElt.tail) {
  11553. node.expressions.push(this.parseTemplateSubstitution());
  11554. this.readTemplateContinuation();
  11555. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11556. }
  11557. return this.finishNode(node, "TemplateLiteral");
  11558. }
  11559. parseTemplateSubstitution() {
  11560. return this.parseExpression();
  11561. }
  11562. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11563. if (isRecord) {
  11564. this.expectPlugin("recordAndTuple");
  11565. }
  11566. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11567. this.state.inFSharpPipelineDirectBody = false;
  11568. const propHash = Object.create(null);
  11569. let first = true;
  11570. const node = this.startNode();
  11571. node.properties = [];
  11572. this.next();
  11573. while (!this.match(close)) {
  11574. if (first) {
  11575. first = false;
  11576. } else {
  11577. this.expect(12);
  11578. if (this.match(close)) {
  11579. this.addTrailingCommaExtraToNode(node);
  11580. break;
  11581. }
  11582. }
  11583. let prop;
  11584. if (isPattern) {
  11585. prop = this.parseBindingProperty();
  11586. } else {
  11587. prop = this.parsePropertyDefinition(refExpressionErrors);
  11588. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11589. }
  11590. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11591. this.raise(Errors.InvalidRecordProperty, {
  11592. at: prop
  11593. });
  11594. }
  11595. if (prop.shorthand) {
  11596. this.addExtra(prop, "shorthand", true);
  11597. }
  11598. node.properties.push(prop);
  11599. }
  11600. this.next();
  11601. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11602. let type = "ObjectExpression";
  11603. if (isPattern) {
  11604. type = "ObjectPattern";
  11605. } else if (isRecord) {
  11606. type = "RecordExpression";
  11607. }
  11608. return this.finishNode(node, type);
  11609. }
  11610. addTrailingCommaExtraToNode(node) {
  11611. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11612. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11613. }
  11614. maybeAsyncOrAccessorProp(prop) {
  11615. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11616. }
  11617. parsePropertyDefinition(refExpressionErrors) {
  11618. let decorators = [];
  11619. if (this.match(26)) {
  11620. if (this.hasPlugin("decorators")) {
  11621. this.raise(Errors.UnsupportedPropertyDecorator, {
  11622. at: this.state.startLoc
  11623. });
  11624. }
  11625. while (this.match(26)) {
  11626. decorators.push(this.parseDecorator());
  11627. }
  11628. }
  11629. const prop = this.startNode();
  11630. let isAsync = false;
  11631. let isAccessor = false;
  11632. let startLoc;
  11633. if (this.match(21)) {
  11634. if (decorators.length) this.unexpected();
  11635. return this.parseSpread();
  11636. }
  11637. if (decorators.length) {
  11638. prop.decorators = decorators;
  11639. decorators = [];
  11640. }
  11641. prop.method = false;
  11642. if (refExpressionErrors) {
  11643. startLoc = this.state.startLoc;
  11644. }
  11645. let isGenerator = this.eat(55);
  11646. this.parsePropertyNamePrefixOperator(prop);
  11647. const containsEsc = this.state.containsEsc;
  11648. const key = this.parsePropertyName(prop, refExpressionErrors);
  11649. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11650. const keyName = key.name;
  11651. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11652. isAsync = true;
  11653. this.resetPreviousNodeTrailingComments(key);
  11654. isGenerator = this.eat(55);
  11655. this.parsePropertyName(prop);
  11656. }
  11657. if (keyName === "get" || keyName === "set") {
  11658. isAccessor = true;
  11659. this.resetPreviousNodeTrailingComments(key);
  11660. prop.kind = keyName;
  11661. if (this.match(55)) {
  11662. isGenerator = true;
  11663. this.raise(Errors.AccessorIsGenerator, {
  11664. at: this.state.curPosition(),
  11665. kind: keyName
  11666. });
  11667. this.next();
  11668. }
  11669. this.parsePropertyName(prop);
  11670. }
  11671. }
  11672. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11673. }
  11674. getGetterSetterExpectedParamCount(method) {
  11675. return method.kind === "get" ? 0 : 1;
  11676. }
  11677. getObjectOrClassMethodParams(method) {
  11678. return method.params;
  11679. }
  11680. checkGetterSetterParams(method) {
  11681. var _params;
  11682. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11683. const params = this.getObjectOrClassMethodParams(method);
  11684. if (params.length !== paramCount) {
  11685. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11686. at: method
  11687. });
  11688. }
  11689. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11690. this.raise(Errors.BadSetterRestParameter, {
  11691. at: method
  11692. });
  11693. }
  11694. }
  11695. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11696. if (isAccessor) {
  11697. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11698. this.checkGetterSetterParams(finishedProp);
  11699. return finishedProp;
  11700. }
  11701. if (isAsync || isGenerator || this.match(10)) {
  11702. if (isPattern) this.unexpected();
  11703. prop.kind = "method";
  11704. prop.method = true;
  11705. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11706. }
  11707. }
  11708. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11709. prop.shorthand = false;
  11710. if (this.eat(14)) {
  11711. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11712. return this.finishNode(prop, "ObjectProperty");
  11713. }
  11714. if (!prop.computed && prop.key.type === "Identifier") {
  11715. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11716. if (isPattern) {
  11717. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11718. } else if (this.match(29)) {
  11719. const shorthandAssignLoc = this.state.startLoc;
  11720. if (refExpressionErrors != null) {
  11721. if (refExpressionErrors.shorthandAssignLoc === null) {
  11722. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11723. }
  11724. } else {
  11725. this.raise(Errors.InvalidCoverInitializedName, {
  11726. at: shorthandAssignLoc
  11727. });
  11728. }
  11729. prop.value = this.parseMaybeDefault(startLoc, cloneIdentifier(prop.key));
  11730. } else {
  11731. prop.value = cloneIdentifier(prop.key);
  11732. }
  11733. prop.shorthand = true;
  11734. return this.finishNode(prop, "ObjectProperty");
  11735. }
  11736. }
  11737. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11738. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  11739. if (!node) this.unexpected();
  11740. return node;
  11741. }
  11742. parsePropertyName(prop, refExpressionErrors) {
  11743. if (this.eat(0)) {
  11744. prop.computed = true;
  11745. prop.key = this.parseMaybeAssignAllowIn();
  11746. this.expect(3);
  11747. } else {
  11748. const {
  11749. type,
  11750. value
  11751. } = this.state;
  11752. let key;
  11753. if (tokenIsKeywordOrIdentifier(type)) {
  11754. key = this.parseIdentifier(true);
  11755. } else {
  11756. switch (type) {
  11757. case 132:
  11758. key = this.parseNumericLiteral(value);
  11759. break;
  11760. case 131:
  11761. key = this.parseStringLiteral(value);
  11762. break;
  11763. case 133:
  11764. key = this.parseBigIntLiteral(value);
  11765. break;
  11766. case 134:
  11767. key = this.parseDecimalLiteral(value);
  11768. break;
  11769. case 136:
  11770. {
  11771. const privateKeyLoc = this.state.startLoc;
  11772. if (refExpressionErrors != null) {
  11773. if (refExpressionErrors.privateKeyLoc === null) {
  11774. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11775. }
  11776. } else {
  11777. this.raise(Errors.UnexpectedPrivateField, {
  11778. at: privateKeyLoc
  11779. });
  11780. }
  11781. key = this.parsePrivateName();
  11782. break;
  11783. }
  11784. default:
  11785. throw this.unexpected();
  11786. }
  11787. }
  11788. prop.key = key;
  11789. if (type !== 136) {
  11790. prop.computed = false;
  11791. }
  11792. }
  11793. return prop.key;
  11794. }
  11795. initFunction(node, isAsync) {
  11796. node.id = null;
  11797. node.generator = false;
  11798. node.async = isAsync;
  11799. }
  11800. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11801. this.initFunction(node, isAsync);
  11802. node.generator = isGenerator;
  11803. const allowModifiers = isConstructor;
  11804. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11805. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11806. this.parseFunctionParams(node, allowModifiers);
  11807. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11808. this.prodParam.exit();
  11809. this.scope.exit();
  11810. return finishedNode;
  11811. }
  11812. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11813. if (isTuple) {
  11814. this.expectPlugin("recordAndTuple");
  11815. }
  11816. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11817. this.state.inFSharpPipelineDirectBody = false;
  11818. const node = this.startNode();
  11819. this.next();
  11820. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11821. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11822. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11823. }
  11824. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11825. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11826. let flags = functionFlags(isAsync, false);
  11827. if (!this.match(5) && this.prodParam.hasIn) {
  11828. flags |= PARAM_IN;
  11829. }
  11830. this.prodParam.enter(flags);
  11831. this.initFunction(node, isAsync);
  11832. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11833. if (params) {
  11834. this.state.maybeInArrowParameters = true;
  11835. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11836. }
  11837. this.state.maybeInArrowParameters = false;
  11838. this.parseFunctionBody(node, true);
  11839. this.prodParam.exit();
  11840. this.scope.exit();
  11841. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11842. return this.finishNode(node, "ArrowFunctionExpression");
  11843. }
  11844. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11845. this.toAssignableList(params, trailingCommaLoc, false);
  11846. node.params = params;
  11847. }
  11848. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11849. this.parseFunctionBody(node, false, isMethod);
  11850. return this.finishNode(node, type);
  11851. }
  11852. parseFunctionBody(node, allowExpression, isMethod = false) {
  11853. const isExpression = allowExpression && !this.match(5);
  11854. this.expressionScope.enter(newExpressionScope());
  11855. if (isExpression) {
  11856. node.body = this.parseMaybeAssign();
  11857. this.checkParams(node, false, allowExpression, false);
  11858. } else {
  11859. const oldStrict = this.state.strict;
  11860. const oldLabels = this.state.labels;
  11861. this.state.labels = [];
  11862. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11863. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11864. const nonSimple = !this.isSimpleParamList(node.params);
  11865. if (hasStrictModeDirective && nonSimple) {
  11866. this.raise(Errors.IllegalLanguageModeDirective, {
  11867. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11868. });
  11869. }
  11870. const strictModeChanged = !oldStrict && this.state.strict;
  11871. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11872. if (this.state.strict && node.id) {
  11873. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11874. }
  11875. });
  11876. this.prodParam.exit();
  11877. this.state.labels = oldLabels;
  11878. }
  11879. this.expressionScope.exit();
  11880. }
  11881. isSimpleParameter(node) {
  11882. return node.type === "Identifier";
  11883. }
  11884. isSimpleParamList(params) {
  11885. for (let i = 0, len = params.length; i < len; i++) {
  11886. if (!this.isSimpleParameter(params[i])) return false;
  11887. }
  11888. return true;
  11889. }
  11890. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11891. const checkClashes = !allowDuplicates && new Set();
  11892. const formalParameters = {
  11893. type: "FormalParameters"
  11894. };
  11895. for (const param of node.params) {
  11896. this.checkLVal(param, {
  11897. in: formalParameters,
  11898. binding: BIND_VAR,
  11899. checkClashes,
  11900. strictModeChanged
  11901. });
  11902. }
  11903. }
  11904. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11905. const elts = [];
  11906. let first = true;
  11907. while (!this.eat(close)) {
  11908. if (first) {
  11909. first = false;
  11910. } else {
  11911. this.expect(12);
  11912. if (this.match(close)) {
  11913. if (nodeForExtra) {
  11914. this.addTrailingCommaExtraToNode(nodeForExtra);
  11915. }
  11916. this.next();
  11917. break;
  11918. }
  11919. }
  11920. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11921. }
  11922. return elts;
  11923. }
  11924. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11925. let elt;
  11926. if (this.match(12)) {
  11927. if (!allowEmpty) {
  11928. this.raise(Errors.UnexpectedToken, {
  11929. at: this.state.curPosition(),
  11930. unexpected: ","
  11931. });
  11932. }
  11933. elt = null;
  11934. } else if (this.match(21)) {
  11935. const spreadNodeStartLoc = this.state.startLoc;
  11936. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  11937. } else if (this.match(17)) {
  11938. this.expectPlugin("partialApplication");
  11939. if (!allowPlaceholder) {
  11940. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11941. at: this.state.startLoc
  11942. });
  11943. }
  11944. const node = this.startNode();
  11945. this.next();
  11946. elt = this.finishNode(node, "ArgumentPlaceholder");
  11947. } else {
  11948. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11949. }
  11950. return elt;
  11951. }
  11952. parseIdentifier(liberal) {
  11953. const node = this.startNode();
  11954. const name = this.parseIdentifierName(liberal);
  11955. return this.createIdentifier(node, name);
  11956. }
  11957. createIdentifier(node, name) {
  11958. node.name = name;
  11959. node.loc.identifierName = name;
  11960. return this.finishNode(node, "Identifier");
  11961. }
  11962. parseIdentifierName(liberal) {
  11963. let name;
  11964. const {
  11965. startLoc,
  11966. type
  11967. } = this.state;
  11968. if (tokenIsKeywordOrIdentifier(type)) {
  11969. name = this.state.value;
  11970. } else {
  11971. throw this.unexpected();
  11972. }
  11973. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11974. if (liberal) {
  11975. if (tokenIsKeyword) {
  11976. this.replaceToken(130);
  11977. }
  11978. } else {
  11979. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11980. }
  11981. this.next();
  11982. return name;
  11983. }
  11984. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11985. if (word.length > 10) {
  11986. return;
  11987. }
  11988. if (!canBeReservedWord(word)) {
  11989. return;
  11990. }
  11991. if (word === "yield") {
  11992. if (this.prodParam.hasYield) {
  11993. this.raise(Errors.YieldBindingIdentifier, {
  11994. at: startLoc
  11995. });
  11996. return;
  11997. }
  11998. } else if (word === "await") {
  11999. if (this.prodParam.hasAwait) {
  12000. this.raise(Errors.AwaitBindingIdentifier, {
  12001. at: startLoc
  12002. });
  12003. return;
  12004. }
  12005. if (this.scope.inStaticBlock) {
  12006. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  12007. at: startLoc
  12008. });
  12009. return;
  12010. }
  12011. this.expressionScope.recordAsyncArrowParametersError({
  12012. at: startLoc
  12013. });
  12014. } else if (word === "arguments") {
  12015. if (this.scope.inClassAndNotInNonArrowFunction) {
  12016. this.raise(Errors.ArgumentsInClass, {
  12017. at: startLoc
  12018. });
  12019. return;
  12020. }
  12021. }
  12022. if (checkKeywords && isKeyword(word)) {
  12023. this.raise(Errors.UnexpectedKeyword, {
  12024. at: startLoc,
  12025. keyword: word
  12026. });
  12027. return;
  12028. }
  12029. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  12030. if (reservedTest(word, this.inModule)) {
  12031. this.raise(Errors.UnexpectedReservedWord, {
  12032. at: startLoc,
  12033. reservedWord: word
  12034. });
  12035. }
  12036. }
  12037. isAwaitAllowed() {
  12038. if (this.prodParam.hasAwait) return true;
  12039. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  12040. return true;
  12041. }
  12042. return false;
  12043. }
  12044. parseAwait(startLoc) {
  12045. const node = this.startNodeAt(startLoc);
  12046. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  12047. at: node
  12048. });
  12049. if (this.eat(55)) {
  12050. this.raise(Errors.ObsoleteAwaitStar, {
  12051. at: node
  12052. });
  12053. }
  12054. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  12055. if (this.isAmbiguousAwait()) {
  12056. this.ambiguousScriptDifferentAst = true;
  12057. } else {
  12058. this.sawUnambiguousESM = true;
  12059. }
  12060. }
  12061. if (!this.state.soloAwait) {
  12062. node.argument = this.parseMaybeUnary(null, true);
  12063. }
  12064. return this.finishNode(node, "AwaitExpression");
  12065. }
  12066. isAmbiguousAwait() {
  12067. if (this.hasPrecedingLineBreak()) return true;
  12068. const {
  12069. type
  12070. } = this.state;
  12071. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 101 && !this.state.containsEsc || type === 135 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12072. }
  12073. parseYield() {
  12074. const node = this.startNode();
  12075. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  12076. at: node
  12077. });
  12078. this.next();
  12079. let delegating = false;
  12080. let argument = null;
  12081. if (!this.hasPrecedingLineBreak()) {
  12082. delegating = this.eat(55);
  12083. switch (this.state.type) {
  12084. case 13:
  12085. case 137:
  12086. case 8:
  12087. case 11:
  12088. case 3:
  12089. case 9:
  12090. case 14:
  12091. case 12:
  12092. if (!delegating) break;
  12093. default:
  12094. argument = this.parseMaybeAssign();
  12095. }
  12096. }
  12097. node.delegate = delegating;
  12098. node.argument = argument;
  12099. return this.finishNode(node, "YieldExpression");
  12100. }
  12101. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12102. if (this.hasPlugin(["pipelineOperator", {
  12103. proposal: "smart"
  12104. }])) {
  12105. if (left.type === "SequenceExpression") {
  12106. this.raise(Errors.PipelineHeadSequenceExpression, {
  12107. at: leftStartLoc
  12108. });
  12109. }
  12110. }
  12111. }
  12112. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  12113. if (this.isSimpleReference(childExpr)) {
  12114. const bodyNode = this.startNodeAt(startLoc);
  12115. bodyNode.callee = childExpr;
  12116. return this.finishNode(bodyNode, "PipelineBareFunction");
  12117. } else {
  12118. const bodyNode = this.startNodeAt(startLoc);
  12119. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12120. bodyNode.expression = childExpr;
  12121. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12122. }
  12123. }
  12124. isSimpleReference(expression) {
  12125. switch (expression.type) {
  12126. case "MemberExpression":
  12127. return !expression.computed && this.isSimpleReference(expression.object);
  12128. case "Identifier":
  12129. return true;
  12130. default:
  12131. return false;
  12132. }
  12133. }
  12134. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12135. if (this.match(19)) {
  12136. throw this.raise(Errors.PipelineBodyNoArrow, {
  12137. at: this.state.startLoc
  12138. });
  12139. }
  12140. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12141. this.raise(Errors.PipelineTopicUnused, {
  12142. at: startLoc
  12143. });
  12144. }
  12145. }
  12146. withTopicBindingContext(callback) {
  12147. const outerContextTopicState = this.state.topicContext;
  12148. this.state.topicContext = {
  12149. maxNumOfResolvableTopics: 1,
  12150. maxTopicIndex: null
  12151. };
  12152. try {
  12153. return callback();
  12154. } finally {
  12155. this.state.topicContext = outerContextTopicState;
  12156. }
  12157. }
  12158. withSmartMixTopicForbiddingContext(callback) {
  12159. if (this.hasPlugin(["pipelineOperator", {
  12160. proposal: "smart"
  12161. }])) {
  12162. const outerContextTopicState = this.state.topicContext;
  12163. this.state.topicContext = {
  12164. maxNumOfResolvableTopics: 0,
  12165. maxTopicIndex: null
  12166. };
  12167. try {
  12168. return callback();
  12169. } finally {
  12170. this.state.topicContext = outerContextTopicState;
  12171. }
  12172. } else {
  12173. return callback();
  12174. }
  12175. }
  12176. withSoloAwaitPermittingContext(callback) {
  12177. const outerContextSoloAwaitState = this.state.soloAwait;
  12178. this.state.soloAwait = true;
  12179. try {
  12180. return callback();
  12181. } finally {
  12182. this.state.soloAwait = outerContextSoloAwaitState;
  12183. }
  12184. }
  12185. allowInAnd(callback) {
  12186. const flags = this.prodParam.currentFlags();
  12187. const prodParamToSet = PARAM_IN & ~flags;
  12188. if (prodParamToSet) {
  12189. this.prodParam.enter(flags | PARAM_IN);
  12190. try {
  12191. return callback();
  12192. } finally {
  12193. this.prodParam.exit();
  12194. }
  12195. }
  12196. return callback();
  12197. }
  12198. disallowInAnd(callback) {
  12199. const flags = this.prodParam.currentFlags();
  12200. const prodParamToClear = PARAM_IN & flags;
  12201. if (prodParamToClear) {
  12202. this.prodParam.enter(flags & ~PARAM_IN);
  12203. try {
  12204. return callback();
  12205. } finally {
  12206. this.prodParam.exit();
  12207. }
  12208. }
  12209. return callback();
  12210. }
  12211. registerTopicReference() {
  12212. this.state.topicContext.maxTopicIndex = 0;
  12213. }
  12214. topicReferenceIsAllowedInCurrentContext() {
  12215. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12216. }
  12217. topicReferenceWasUsedInCurrentContext() {
  12218. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12219. }
  12220. parseFSharpPipelineBody(prec) {
  12221. const startLoc = this.state.startLoc;
  12222. this.state.potentialArrowAt = this.state.start;
  12223. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12224. this.state.inFSharpPipelineDirectBody = true;
  12225. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  12226. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12227. return ret;
  12228. }
  12229. parseModuleExpression() {
  12230. this.expectPlugin("moduleBlocks");
  12231. const node = this.startNode();
  12232. this.next();
  12233. if (!this.match(5)) {
  12234. this.unexpected(null, 5);
  12235. }
  12236. const program = this.startNodeAt(this.state.endLoc);
  12237. this.next();
  12238. const revertScopes = this.initializeScopes(true);
  12239. this.enterInitialScopes();
  12240. try {
  12241. node.body = this.parseProgram(program, 8, "module");
  12242. } finally {
  12243. revertScopes();
  12244. }
  12245. return this.finishNode(node, "ModuleExpression");
  12246. }
  12247. parsePropertyNamePrefixOperator(prop) {}
  12248. }
  12249. const loopLabel = {
  12250. kind: "loop"
  12251. },
  12252. switchLabel = {
  12253. kind: "switch"
  12254. };
  12255. var ParseFunctionFlag = {
  12256. Expression: 0,
  12257. Declaration: 1,
  12258. HangingDeclaration: 2,
  12259. NullableId: 4,
  12260. Async: 8
  12261. };
  12262. var ParseStatementFlag = {
  12263. StatementOnly: 0,
  12264. AllowImportExport: 1,
  12265. AllowDeclaration: 2,
  12266. AllowFunctionDeclaration: 4,
  12267. AllowLabeledFunction: 8
  12268. };
  12269. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12270. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12271. function babel7CompatTokens(tokens, input) {
  12272. for (let i = 0; i < tokens.length; i++) {
  12273. const token = tokens[i];
  12274. const {
  12275. type
  12276. } = token;
  12277. if (typeof type === "number") {
  12278. {
  12279. if (type === 136) {
  12280. const {
  12281. loc,
  12282. start,
  12283. value,
  12284. end
  12285. } = token;
  12286. const hashEndPos = start + 1;
  12287. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12288. tokens.splice(i, 1, new Token({
  12289. type: getExportedToken(27),
  12290. value: "#",
  12291. start: start,
  12292. end: hashEndPos,
  12293. startLoc: loc.start,
  12294. endLoc: hashEndLoc
  12295. }), new Token({
  12296. type: getExportedToken(130),
  12297. value: value,
  12298. start: hashEndPos,
  12299. end: end,
  12300. startLoc: hashEndLoc,
  12301. endLoc: loc.end
  12302. }));
  12303. i++;
  12304. continue;
  12305. }
  12306. if (tokenIsTemplate(type)) {
  12307. const {
  12308. loc,
  12309. start,
  12310. value,
  12311. end
  12312. } = token;
  12313. const backquoteEnd = start + 1;
  12314. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12315. let startToken;
  12316. if (input.charCodeAt(start) === 96) {
  12317. startToken = new Token({
  12318. type: getExportedToken(22),
  12319. value: "`",
  12320. start: start,
  12321. end: backquoteEnd,
  12322. startLoc: loc.start,
  12323. endLoc: backquoteEndLoc
  12324. });
  12325. } else {
  12326. startToken = new Token({
  12327. type: getExportedToken(8),
  12328. value: "}",
  12329. start: start,
  12330. end: backquoteEnd,
  12331. startLoc: loc.start,
  12332. endLoc: backquoteEndLoc
  12333. });
  12334. }
  12335. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12336. if (type === 24) {
  12337. templateElementEnd = end - 1;
  12338. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12339. templateValue = value === null ? null : value.slice(1, -1);
  12340. endToken = new Token({
  12341. type: getExportedToken(22),
  12342. value: "`",
  12343. start: templateElementEnd,
  12344. end: end,
  12345. startLoc: templateElementEndLoc,
  12346. endLoc: loc.end
  12347. });
  12348. } else {
  12349. templateElementEnd = end - 2;
  12350. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12351. templateValue = value === null ? null : value.slice(1, -2);
  12352. endToken = new Token({
  12353. type: getExportedToken(23),
  12354. value: "${",
  12355. start: templateElementEnd,
  12356. end: end,
  12357. startLoc: templateElementEndLoc,
  12358. endLoc: loc.end
  12359. });
  12360. }
  12361. tokens.splice(i, 1, startToken, new Token({
  12362. type: getExportedToken(20),
  12363. value: templateValue,
  12364. start: backquoteEnd,
  12365. end: templateElementEnd,
  12366. startLoc: backquoteEndLoc,
  12367. endLoc: templateElementEndLoc
  12368. }), endToken);
  12369. i += 2;
  12370. continue;
  12371. }
  12372. }
  12373. token.type = getExportedToken(type);
  12374. }
  12375. }
  12376. return tokens;
  12377. }
  12378. class StatementParser extends ExpressionParser {
  12379. parseTopLevel(file, program) {
  12380. file.program = this.parseProgram(program);
  12381. file.comments = this.state.comments;
  12382. if (this.options.tokens) {
  12383. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12384. }
  12385. return this.finishNode(file, "File");
  12386. }
  12387. parseProgram(program, end = 137, sourceType = this.options.sourceType) {
  12388. program.sourceType = sourceType;
  12389. program.interpreter = this.parseInterpreterDirective();
  12390. this.parseBlockBody(program, true, true, end);
  12391. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12392. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12393. this.raise(Errors.ModuleExportUndefined, {
  12394. at,
  12395. localName
  12396. });
  12397. }
  12398. }
  12399. let finishedProgram;
  12400. if (end === 137) {
  12401. finishedProgram = this.finishNode(program, "Program");
  12402. } else {
  12403. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12404. }
  12405. return finishedProgram;
  12406. }
  12407. stmtToDirective(stmt) {
  12408. const directive = stmt;
  12409. directive.type = "Directive";
  12410. directive.value = directive.expression;
  12411. delete directive.expression;
  12412. const directiveLiteral = directive.value;
  12413. const expressionValue = directiveLiteral.value;
  12414. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12415. const val = directiveLiteral.value = raw.slice(1, -1);
  12416. this.addExtra(directiveLiteral, "raw", raw);
  12417. this.addExtra(directiveLiteral, "rawValue", val);
  12418. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12419. directiveLiteral.type = "DirectiveLiteral";
  12420. return directive;
  12421. }
  12422. parseInterpreterDirective() {
  12423. if (!this.match(28)) {
  12424. return null;
  12425. }
  12426. const node = this.startNode();
  12427. node.value = this.state.value;
  12428. this.next();
  12429. return this.finishNode(node, "InterpreterDirective");
  12430. }
  12431. isLet() {
  12432. if (!this.isContextual(99)) {
  12433. return false;
  12434. }
  12435. return this.hasFollowingBindingAtom();
  12436. }
  12437. chStartsBindingIdentifier(ch, pos) {
  12438. if (isIdentifierStart(ch)) {
  12439. keywordRelationalOperator.lastIndex = pos;
  12440. if (keywordRelationalOperator.test(this.input)) {
  12441. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12442. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12443. return false;
  12444. }
  12445. }
  12446. return true;
  12447. } else if (ch === 92) {
  12448. return true;
  12449. } else {
  12450. return false;
  12451. }
  12452. }
  12453. chStartsBindingPattern(ch) {
  12454. return ch === 91 || ch === 123;
  12455. }
  12456. hasFollowingBindingAtom() {
  12457. const next = this.nextTokenStart();
  12458. const nextCh = this.codePointAtPos(next);
  12459. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12460. }
  12461. hasFollowingBindingIdentifier() {
  12462. const next = this.nextTokenStart();
  12463. const nextCh = this.codePointAtPos(next);
  12464. return this.chStartsBindingIdentifier(nextCh, next);
  12465. }
  12466. startsUsingForOf() {
  12467. const lookahead = this.lookahead();
  12468. if (lookahead.type === 101 && !lookahead.containsEsc) {
  12469. return false;
  12470. } else {
  12471. this.expectPlugin("explicitResourceManagement");
  12472. return true;
  12473. }
  12474. }
  12475. parseModuleItem() {
  12476. return this.parseStatementLike(ParseStatementFlag.AllowImportExport | ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | ParseStatementFlag.AllowLabeledFunction);
  12477. }
  12478. parseStatementListItem() {
  12479. return this.parseStatementLike(ParseStatementFlag.AllowDeclaration | ParseStatementFlag.AllowFunctionDeclaration | ParseStatementFlag.AllowLabeledFunction);
  12480. }
  12481. parseStatementOrFunctionDeclaration(disallowLabeledFunction) {
  12482. return this.parseStatementLike(ParseStatementFlag.AllowFunctionDeclaration | (disallowLabeledFunction ? 0 : ParseStatementFlag.AllowLabeledFunction));
  12483. }
  12484. parseStatement() {
  12485. return this.parseStatementLike(ParseStatementFlag.StatementOnly);
  12486. }
  12487. parseStatementLike(flags) {
  12488. let decorators = null;
  12489. if (this.match(26)) {
  12490. decorators = this.parseDecorators(true);
  12491. }
  12492. return this.parseStatementContent(flags, decorators);
  12493. }
  12494. parseStatementContent(flags, decorators) {
  12495. const starttype = this.state.type;
  12496. const node = this.startNode();
  12497. const allowDeclaration = !!(flags & ParseStatementFlag.AllowDeclaration);
  12498. const allowFunctionDeclaration = !!(flags & ParseStatementFlag.AllowFunctionDeclaration);
  12499. const topLevel = flags & ParseStatementFlag.AllowImportExport;
  12500. switch (starttype) {
  12501. case 60:
  12502. return this.parseBreakContinueStatement(node, true);
  12503. case 63:
  12504. return this.parseBreakContinueStatement(node, false);
  12505. case 64:
  12506. return this.parseDebuggerStatement(node);
  12507. case 90:
  12508. return this.parseDoWhileStatement(node);
  12509. case 91:
  12510. return this.parseForStatement(node);
  12511. case 68:
  12512. if (this.lookaheadCharCode() === 46) break;
  12513. if (!allowDeclaration) {
  12514. if (this.state.strict) {
  12515. this.raise(Errors.StrictFunction, {
  12516. at: this.state.startLoc
  12517. });
  12518. } else if (!allowFunctionDeclaration) {
  12519. this.raise(Errors.SloppyFunction, {
  12520. at: this.state.startLoc
  12521. });
  12522. }
  12523. }
  12524. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12525. case 80:
  12526. if (!allowDeclaration) this.unexpected();
  12527. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12528. case 69:
  12529. return this.parseIfStatement(node);
  12530. case 70:
  12531. return this.parseReturnStatement(node);
  12532. case 71:
  12533. return this.parseSwitchStatement(node);
  12534. case 72:
  12535. return this.parseThrowStatement(node);
  12536. case 73:
  12537. return this.parseTryStatement(node);
  12538. case 105:
  12539. if (this.hasFollowingLineBreak() || this.state.containsEsc || !this.hasFollowingBindingIdentifier()) {
  12540. break;
  12541. }
  12542. this.expectPlugin("explicitResourceManagement");
  12543. if (!this.scope.inModule && this.scope.inTopLevel) {
  12544. this.raise(Errors.UnexpectedUsingDeclaration, {
  12545. at: this.state.startLoc
  12546. });
  12547. } else if (!allowDeclaration) {
  12548. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12549. at: this.state.startLoc
  12550. });
  12551. }
  12552. return this.parseVarStatement(node, "using");
  12553. case 99:
  12554. {
  12555. if (this.state.containsEsc) {
  12556. break;
  12557. }
  12558. const next = this.nextTokenStart();
  12559. const nextCh = this.codePointAtPos(next);
  12560. if (nextCh !== 91) {
  12561. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12562. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12563. break;
  12564. }
  12565. }
  12566. }
  12567. case 75:
  12568. {
  12569. if (!allowDeclaration) {
  12570. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12571. at: this.state.startLoc
  12572. });
  12573. }
  12574. }
  12575. case 74:
  12576. {
  12577. const kind = this.state.value;
  12578. return this.parseVarStatement(node, kind);
  12579. }
  12580. case 92:
  12581. return this.parseWhileStatement(node);
  12582. case 76:
  12583. return this.parseWithStatement(node);
  12584. case 5:
  12585. return this.parseBlock();
  12586. case 13:
  12587. return this.parseEmptyStatement(node);
  12588. case 83:
  12589. {
  12590. const nextTokenCharCode = this.lookaheadCharCode();
  12591. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12592. break;
  12593. }
  12594. }
  12595. case 82:
  12596. {
  12597. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12598. this.raise(Errors.UnexpectedImportExport, {
  12599. at: this.state.startLoc
  12600. });
  12601. }
  12602. this.next();
  12603. let result;
  12604. if (starttype === 83) {
  12605. result = this.parseImport(node);
  12606. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12607. this.sawUnambiguousESM = true;
  12608. }
  12609. } else {
  12610. result = this.parseExport(node, decorators);
  12611. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12612. this.sawUnambiguousESM = true;
  12613. }
  12614. }
  12615. this.assertModuleNodeAllowed(result);
  12616. return result;
  12617. }
  12618. default:
  12619. {
  12620. if (this.isAsyncFunction()) {
  12621. if (!allowDeclaration) {
  12622. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12623. at: this.state.startLoc
  12624. });
  12625. }
  12626. this.next();
  12627. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12628. }
  12629. }
  12630. }
  12631. const maybeName = this.state.value;
  12632. const expr = this.parseExpression();
  12633. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12634. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12635. } else {
  12636. return this.parseExpressionStatement(node, expr, decorators);
  12637. }
  12638. }
  12639. assertModuleNodeAllowed(node) {
  12640. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12641. this.raise(Errors.ImportOutsideModule, {
  12642. at: node
  12643. });
  12644. }
  12645. }
  12646. decoratorsEnabledBeforeExport() {
  12647. if (this.hasPlugin("decorators-legacy")) return true;
  12648. return this.hasPlugin("decorators") && !!this.getPluginOption("decorators", "decoratorsBeforeExport");
  12649. }
  12650. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12651. if (maybeDecorators) {
  12652. classNode.decorators = maybeDecorators;
  12653. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12654. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12655. }
  12656. return classNode;
  12657. }
  12658. canHaveLeadingDecorator() {
  12659. return this.match(80);
  12660. }
  12661. parseDecorators(allowExport) {
  12662. const decorators = [];
  12663. do {
  12664. decorators.push(this.parseDecorator());
  12665. } while (this.match(26));
  12666. if (this.match(82)) {
  12667. if (!allowExport) {
  12668. this.unexpected();
  12669. }
  12670. if (!this.decoratorsEnabledBeforeExport()) {
  12671. this.raise(Errors.DecoratorExportClass, {
  12672. at: this.state.startLoc
  12673. });
  12674. }
  12675. } else if (!this.canHaveLeadingDecorator()) {
  12676. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12677. at: this.state.startLoc
  12678. });
  12679. }
  12680. return decorators;
  12681. }
  12682. parseDecorator() {
  12683. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12684. const node = this.startNode();
  12685. this.next();
  12686. if (this.hasPlugin("decorators")) {
  12687. const startLoc = this.state.startLoc;
  12688. let expr;
  12689. if (this.match(10)) {
  12690. const startLoc = this.state.startLoc;
  12691. this.next();
  12692. expr = this.parseExpression();
  12693. this.expect(11);
  12694. expr = this.wrapParenthesis(startLoc, expr);
  12695. const paramsStartLoc = this.state.startLoc;
  12696. node.expression = this.parseMaybeDecoratorArguments(expr);
  12697. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12698. this.raise(Errors.DecoratorArgumentsOutsideParentheses, {
  12699. at: paramsStartLoc
  12700. });
  12701. }
  12702. } else {
  12703. expr = this.parseIdentifier(false);
  12704. while (this.eat(16)) {
  12705. const node = this.startNodeAt(startLoc);
  12706. node.object = expr;
  12707. if (this.match(136)) {
  12708. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12709. node.property = this.parsePrivateName();
  12710. } else {
  12711. node.property = this.parseIdentifier(true);
  12712. }
  12713. node.computed = false;
  12714. expr = this.finishNode(node, "MemberExpression");
  12715. }
  12716. node.expression = this.parseMaybeDecoratorArguments(expr);
  12717. }
  12718. } else {
  12719. node.expression = this.parseExprSubscripts();
  12720. }
  12721. return this.finishNode(node, "Decorator");
  12722. }
  12723. parseMaybeDecoratorArguments(expr) {
  12724. if (this.eat(10)) {
  12725. const node = this.startNodeAtNode(expr);
  12726. node.callee = expr;
  12727. node.arguments = this.parseCallExpressionArguments(11, false);
  12728. this.toReferencedList(node.arguments);
  12729. return this.finishNode(node, "CallExpression");
  12730. }
  12731. return expr;
  12732. }
  12733. parseBreakContinueStatement(node, isBreak) {
  12734. this.next();
  12735. if (this.isLineTerminator()) {
  12736. node.label = null;
  12737. } else {
  12738. node.label = this.parseIdentifier();
  12739. this.semicolon();
  12740. }
  12741. this.verifyBreakContinue(node, isBreak);
  12742. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12743. }
  12744. verifyBreakContinue(node, isBreak) {
  12745. let i;
  12746. for (i = 0; i < this.state.labels.length; ++i) {
  12747. const lab = this.state.labels[i];
  12748. if (node.label == null || lab.name === node.label.name) {
  12749. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12750. if (node.label && isBreak) break;
  12751. }
  12752. }
  12753. if (i === this.state.labels.length) {
  12754. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12755. this.raise(Errors.IllegalBreakContinue, {
  12756. at: node,
  12757. type
  12758. });
  12759. }
  12760. }
  12761. parseDebuggerStatement(node) {
  12762. this.next();
  12763. this.semicolon();
  12764. return this.finishNode(node, "DebuggerStatement");
  12765. }
  12766. parseHeaderExpression() {
  12767. this.expect(10);
  12768. const val = this.parseExpression();
  12769. this.expect(11);
  12770. return val;
  12771. }
  12772. parseDoWhileStatement(node) {
  12773. this.next();
  12774. this.state.labels.push(loopLabel);
  12775. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12776. this.state.labels.pop();
  12777. this.expect(92);
  12778. node.test = this.parseHeaderExpression();
  12779. this.eat(13);
  12780. return this.finishNode(node, "DoWhileStatement");
  12781. }
  12782. parseForStatement(node) {
  12783. this.next();
  12784. this.state.labels.push(loopLabel);
  12785. let awaitAt = null;
  12786. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12787. awaitAt = this.state.lastTokStartLoc;
  12788. }
  12789. this.scope.enter(SCOPE_OTHER);
  12790. this.expect(10);
  12791. if (this.match(13)) {
  12792. if (awaitAt !== null) {
  12793. this.unexpected(awaitAt);
  12794. }
  12795. return this.parseFor(node, null);
  12796. }
  12797. const startsWithLet = this.isContextual(99);
  12798. const startsWithUsing = this.isContextual(105) && !this.hasFollowingLineBreak();
  12799. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || startsWithUsing && this.hasFollowingBindingIdentifier() && this.startsUsingForOf();
  12800. if (this.match(74) || this.match(75) || isLetOrUsing) {
  12801. const initNode = this.startNode();
  12802. const kind = this.state.value;
  12803. this.next();
  12804. this.parseVar(initNode, true, kind);
  12805. const init = this.finishNode(initNode, "VariableDeclaration");
  12806. const isForIn = this.match(58);
  12807. if (isForIn && startsWithUsing) {
  12808. this.raise(Errors.ForInUsing, {
  12809. at: init
  12810. });
  12811. }
  12812. if ((isForIn || this.isContextual(101)) && init.declarations.length === 1) {
  12813. return this.parseForIn(node, init, awaitAt);
  12814. }
  12815. if (awaitAt !== null) {
  12816. this.unexpected(awaitAt);
  12817. }
  12818. return this.parseFor(node, init);
  12819. }
  12820. const startsWithAsync = this.isContextual(95);
  12821. const refExpressionErrors = new ExpressionErrors();
  12822. const init = this.parseExpression(true, refExpressionErrors);
  12823. const isForOf = this.isContextual(101);
  12824. if (isForOf) {
  12825. if (startsWithLet) {
  12826. this.raise(Errors.ForOfLet, {
  12827. at: init
  12828. });
  12829. }
  12830. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12831. this.raise(Errors.ForOfAsync, {
  12832. at: init
  12833. });
  12834. }
  12835. }
  12836. if (isForOf || this.match(58)) {
  12837. this.checkDestructuringPrivate(refExpressionErrors);
  12838. this.toAssignable(init, true);
  12839. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12840. this.checkLVal(init, {
  12841. in: {
  12842. type
  12843. }
  12844. });
  12845. return this.parseForIn(node, init, awaitAt);
  12846. } else {
  12847. this.checkExpressionErrors(refExpressionErrors, true);
  12848. }
  12849. if (awaitAt !== null) {
  12850. this.unexpected(awaitAt);
  12851. }
  12852. return this.parseFor(node, init);
  12853. }
  12854. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  12855. this.next();
  12856. return this.parseFunction(node, ParseFunctionFlag.Declaration | (isHangingDeclaration ? ParseFunctionFlag.HangingDeclaration : 0) | (isAsync ? ParseFunctionFlag.Async : 0));
  12857. }
  12858. parseIfStatement(node) {
  12859. this.next();
  12860. node.test = this.parseHeaderExpression();
  12861. node.consequent = this.parseStatementOrFunctionDeclaration(true);
  12862. node.alternate = this.eat(66) ? this.parseStatementOrFunctionDeclaration(true) : null;
  12863. return this.finishNode(node, "IfStatement");
  12864. }
  12865. parseReturnStatement(node) {
  12866. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12867. this.raise(Errors.IllegalReturn, {
  12868. at: this.state.startLoc
  12869. });
  12870. }
  12871. this.next();
  12872. if (this.isLineTerminator()) {
  12873. node.argument = null;
  12874. } else {
  12875. node.argument = this.parseExpression();
  12876. this.semicolon();
  12877. }
  12878. return this.finishNode(node, "ReturnStatement");
  12879. }
  12880. parseSwitchStatement(node) {
  12881. this.next();
  12882. node.discriminant = this.parseHeaderExpression();
  12883. const cases = node.cases = [];
  12884. this.expect(5);
  12885. this.state.labels.push(switchLabel);
  12886. this.scope.enter(SCOPE_OTHER);
  12887. let cur;
  12888. for (let sawDefault; !this.match(8);) {
  12889. if (this.match(61) || this.match(65)) {
  12890. const isCase = this.match(61);
  12891. if (cur) this.finishNode(cur, "SwitchCase");
  12892. cases.push(cur = this.startNode());
  12893. cur.consequent = [];
  12894. this.next();
  12895. if (isCase) {
  12896. cur.test = this.parseExpression();
  12897. } else {
  12898. if (sawDefault) {
  12899. this.raise(Errors.MultipleDefaultsInSwitch, {
  12900. at: this.state.lastTokStartLoc
  12901. });
  12902. }
  12903. sawDefault = true;
  12904. cur.test = null;
  12905. }
  12906. this.expect(14);
  12907. } else {
  12908. if (cur) {
  12909. cur.consequent.push(this.parseStatementListItem());
  12910. } else {
  12911. this.unexpected();
  12912. }
  12913. }
  12914. }
  12915. this.scope.exit();
  12916. if (cur) this.finishNode(cur, "SwitchCase");
  12917. this.next();
  12918. this.state.labels.pop();
  12919. return this.finishNode(node, "SwitchStatement");
  12920. }
  12921. parseThrowStatement(node) {
  12922. this.next();
  12923. if (this.hasPrecedingLineBreak()) {
  12924. this.raise(Errors.NewlineAfterThrow, {
  12925. at: this.state.lastTokEndLoc
  12926. });
  12927. }
  12928. node.argument = this.parseExpression();
  12929. this.semicolon();
  12930. return this.finishNode(node, "ThrowStatement");
  12931. }
  12932. parseCatchClauseParam() {
  12933. const param = this.parseBindingAtom();
  12934. const simple = param.type === "Identifier";
  12935. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  12936. this.checkLVal(param, {
  12937. in: {
  12938. type: "CatchClause"
  12939. },
  12940. binding: BIND_CATCH_PARAM
  12941. });
  12942. return param;
  12943. }
  12944. parseTryStatement(node) {
  12945. this.next();
  12946. node.block = this.parseBlock();
  12947. node.handler = null;
  12948. if (this.match(62)) {
  12949. const clause = this.startNode();
  12950. this.next();
  12951. if (this.match(10)) {
  12952. this.expect(10);
  12953. clause.param = this.parseCatchClauseParam();
  12954. this.expect(11);
  12955. } else {
  12956. clause.param = null;
  12957. this.scope.enter(SCOPE_OTHER);
  12958. }
  12959. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12960. this.scope.exit();
  12961. node.handler = this.finishNode(clause, "CatchClause");
  12962. }
  12963. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12964. if (!node.handler && !node.finalizer) {
  12965. this.raise(Errors.NoCatchOrFinally, {
  12966. at: node
  12967. });
  12968. }
  12969. return this.finishNode(node, "TryStatement");
  12970. }
  12971. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12972. this.next();
  12973. this.parseVar(node, false, kind, allowMissingInitializer);
  12974. this.semicolon();
  12975. return this.finishNode(node, "VariableDeclaration");
  12976. }
  12977. parseWhileStatement(node) {
  12978. this.next();
  12979. node.test = this.parseHeaderExpression();
  12980. this.state.labels.push(loopLabel);
  12981. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12982. this.state.labels.pop();
  12983. return this.finishNode(node, "WhileStatement");
  12984. }
  12985. parseWithStatement(node) {
  12986. if (this.state.strict) {
  12987. this.raise(Errors.StrictWith, {
  12988. at: this.state.startLoc
  12989. });
  12990. }
  12991. this.next();
  12992. node.object = this.parseHeaderExpression();
  12993. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  12994. return this.finishNode(node, "WithStatement");
  12995. }
  12996. parseEmptyStatement(node) {
  12997. this.next();
  12998. return this.finishNode(node, "EmptyStatement");
  12999. }
  13000. parseLabeledStatement(node, maybeName, expr, flags) {
  13001. for (const label of this.state.labels) {
  13002. if (label.name === maybeName) {
  13003. this.raise(Errors.LabelRedeclaration, {
  13004. at: expr,
  13005. labelName: maybeName
  13006. });
  13007. }
  13008. }
  13009. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  13010. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  13011. const label = this.state.labels[i];
  13012. if (label.statementStart === node.start) {
  13013. label.statementStart = this.state.start;
  13014. label.kind = kind;
  13015. } else {
  13016. break;
  13017. }
  13018. }
  13019. this.state.labels.push({
  13020. name: maybeName,
  13021. kind: kind,
  13022. statementStart: this.state.start
  13023. });
  13024. node.body = flags & ParseStatementFlag.AllowLabeledFunction ? this.parseStatementOrFunctionDeclaration(false) : this.parseStatement();
  13025. this.state.labels.pop();
  13026. node.label = expr;
  13027. return this.finishNode(node, "LabeledStatement");
  13028. }
  13029. parseExpressionStatement(node, expr, decorators) {
  13030. node.expression = expr;
  13031. this.semicolon();
  13032. return this.finishNode(node, "ExpressionStatement");
  13033. }
  13034. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  13035. const node = this.startNode();
  13036. if (allowDirectives) {
  13037. this.state.strictErrors.clear();
  13038. }
  13039. this.expect(5);
  13040. if (createNewLexicalScope) {
  13041. this.scope.enter(SCOPE_OTHER);
  13042. }
  13043. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  13044. if (createNewLexicalScope) {
  13045. this.scope.exit();
  13046. }
  13047. return this.finishNode(node, "BlockStatement");
  13048. }
  13049. isValidDirective(stmt) {
  13050. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  13051. }
  13052. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  13053. const body = node.body = [];
  13054. const directives = node.directives = [];
  13055. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  13056. }
  13057. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  13058. const oldStrict = this.state.strict;
  13059. let hasStrictModeDirective = false;
  13060. let parsedNonDirective = false;
  13061. while (!this.match(end)) {
  13062. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  13063. if (directives && !parsedNonDirective) {
  13064. if (this.isValidDirective(stmt)) {
  13065. const directive = this.stmtToDirective(stmt);
  13066. directives.push(directive);
  13067. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  13068. hasStrictModeDirective = true;
  13069. this.setStrict(true);
  13070. }
  13071. continue;
  13072. }
  13073. parsedNonDirective = true;
  13074. this.state.strictErrors.clear();
  13075. }
  13076. body.push(stmt);
  13077. }
  13078. if (afterBlockParse) {
  13079. afterBlockParse.call(this, hasStrictModeDirective);
  13080. }
  13081. if (!oldStrict) {
  13082. this.setStrict(false);
  13083. }
  13084. this.next();
  13085. }
  13086. parseFor(node, init) {
  13087. node.init = init;
  13088. this.semicolon(false);
  13089. node.test = this.match(13) ? null : this.parseExpression();
  13090. this.semicolon(false);
  13091. node.update = this.match(11) ? null : this.parseExpression();
  13092. this.expect(11);
  13093. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13094. this.scope.exit();
  13095. this.state.labels.pop();
  13096. return this.finishNode(node, "ForStatement");
  13097. }
  13098. parseForIn(node, init, awaitAt) {
  13099. const isForIn = this.match(58);
  13100. this.next();
  13101. if (isForIn) {
  13102. if (awaitAt !== null) this.unexpected(awaitAt);
  13103. } else {
  13104. node.await = awaitAt !== null;
  13105. }
  13106. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  13107. this.raise(Errors.ForInOfLoopInitializer, {
  13108. at: init,
  13109. type: isForIn ? "ForInStatement" : "ForOfStatement"
  13110. });
  13111. }
  13112. if (init.type === "AssignmentPattern") {
  13113. this.raise(Errors.InvalidLhs, {
  13114. at: init,
  13115. ancestor: {
  13116. type: "ForStatement"
  13117. }
  13118. });
  13119. }
  13120. node.left = init;
  13121. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  13122. this.expect(11);
  13123. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13124. this.scope.exit();
  13125. this.state.labels.pop();
  13126. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  13127. }
  13128. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  13129. const declarations = node.declarations = [];
  13130. node.kind = kind;
  13131. for (;;) {
  13132. const decl = this.startNode();
  13133. this.parseVarId(decl, kind);
  13134. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  13135. if (decl.init === null && !allowMissingInitializer) {
  13136. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  13137. this.raise(Errors.DeclarationMissingInitializer, {
  13138. at: this.state.lastTokEndLoc,
  13139. kind: "destructuring"
  13140. });
  13141. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  13142. this.raise(Errors.DeclarationMissingInitializer, {
  13143. at: this.state.lastTokEndLoc,
  13144. kind: "const"
  13145. });
  13146. }
  13147. }
  13148. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13149. if (!this.eat(12)) break;
  13150. }
  13151. return node;
  13152. }
  13153. parseVarId(decl, kind) {
  13154. if (kind === "using" && !this.inModule && this.match(96)) {
  13155. this.raise(Errors.AwaitInUsingBinding, {
  13156. at: this.state.startLoc
  13157. });
  13158. }
  13159. const id = this.parseBindingAtom();
  13160. this.checkLVal(id, {
  13161. in: {
  13162. type: "VariableDeclarator"
  13163. },
  13164. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  13165. });
  13166. decl.id = id;
  13167. }
  13168. parseAsyncFunctionExpression(node) {
  13169. return this.parseFunction(node, ParseFunctionFlag.Async);
  13170. }
  13171. parseFunction(node, flags = ParseFunctionFlag.Expression) {
  13172. const hangingDeclaration = flags & ParseFunctionFlag.HangingDeclaration;
  13173. const isDeclaration = !!(flags & ParseFunctionFlag.Declaration);
  13174. const requireId = isDeclaration && !(flags & ParseFunctionFlag.NullableId);
  13175. const isAsync = !!(flags & ParseFunctionFlag.Async);
  13176. this.initFunction(node, isAsync);
  13177. if (this.match(55)) {
  13178. if (hangingDeclaration) {
  13179. this.raise(Errors.GeneratorInSingleStatementContext, {
  13180. at: this.state.startLoc
  13181. });
  13182. }
  13183. this.next();
  13184. node.generator = true;
  13185. }
  13186. if (isDeclaration) {
  13187. node.id = this.parseFunctionId(requireId);
  13188. }
  13189. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13190. this.state.maybeInArrowParameters = false;
  13191. this.scope.enter(SCOPE_FUNCTION);
  13192. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13193. if (!isDeclaration) {
  13194. node.id = this.parseFunctionId();
  13195. }
  13196. this.parseFunctionParams(node, false);
  13197. this.withSmartMixTopicForbiddingContext(() => {
  13198. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  13199. });
  13200. this.prodParam.exit();
  13201. this.scope.exit();
  13202. if (isDeclaration && !hangingDeclaration) {
  13203. this.registerFunctionStatementId(node);
  13204. }
  13205. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13206. return node;
  13207. }
  13208. parseFunctionId(requireId) {
  13209. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13210. }
  13211. parseFunctionParams(node, allowModifiers) {
  13212. this.expect(10);
  13213. this.expressionScope.enter(newParameterDeclarationScope());
  13214. node.params = this.parseBindingList(11, 41, false, allowModifiers);
  13215. this.expressionScope.exit();
  13216. }
  13217. registerFunctionStatementId(node) {
  13218. if (!node.id) return;
  13219. this.scope.declareName(node.id.name, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.loc.start);
  13220. }
  13221. parseClass(node, isStatement, optionalId) {
  13222. this.next();
  13223. const oldStrict = this.state.strict;
  13224. this.state.strict = true;
  13225. this.parseClassId(node, isStatement, optionalId);
  13226. this.parseClassSuper(node);
  13227. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13228. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13229. }
  13230. isClassProperty() {
  13231. return this.match(29) || this.match(13) || this.match(8);
  13232. }
  13233. isClassMethod() {
  13234. return this.match(10);
  13235. }
  13236. isNonstaticConstructor(method) {
  13237. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  13238. }
  13239. parseClassBody(hadSuperClass, oldStrict) {
  13240. this.classScope.enter();
  13241. const state = {
  13242. hadConstructor: false,
  13243. hadSuperClass
  13244. };
  13245. let decorators = [];
  13246. const classBody = this.startNode();
  13247. classBody.body = [];
  13248. this.expect(5);
  13249. this.withSmartMixTopicForbiddingContext(() => {
  13250. while (!this.match(8)) {
  13251. if (this.eat(13)) {
  13252. if (decorators.length > 0) {
  13253. throw this.raise(Errors.DecoratorSemicolon, {
  13254. at: this.state.lastTokEndLoc
  13255. });
  13256. }
  13257. continue;
  13258. }
  13259. if (this.match(26)) {
  13260. decorators.push(this.parseDecorator());
  13261. continue;
  13262. }
  13263. const member = this.startNode();
  13264. if (decorators.length) {
  13265. member.decorators = decorators;
  13266. this.resetStartLocationFromNode(member, decorators[0]);
  13267. decorators = [];
  13268. }
  13269. this.parseClassMember(classBody, member, state);
  13270. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13271. this.raise(Errors.DecoratorConstructor, {
  13272. at: member
  13273. });
  13274. }
  13275. }
  13276. });
  13277. this.state.strict = oldStrict;
  13278. this.next();
  13279. if (decorators.length) {
  13280. throw this.raise(Errors.TrailingDecorator, {
  13281. at: this.state.startLoc
  13282. });
  13283. }
  13284. this.classScope.exit();
  13285. return this.finishNode(classBody, "ClassBody");
  13286. }
  13287. parseClassMemberFromModifier(classBody, member) {
  13288. const key = this.parseIdentifier(true);
  13289. if (this.isClassMethod()) {
  13290. const method = member;
  13291. method.kind = "method";
  13292. method.computed = false;
  13293. method.key = key;
  13294. method.static = false;
  13295. this.pushClassMethod(classBody, method, false, false, false, false);
  13296. return true;
  13297. } else if (this.isClassProperty()) {
  13298. const prop = member;
  13299. prop.computed = false;
  13300. prop.key = key;
  13301. prop.static = false;
  13302. classBody.body.push(this.parseClassProperty(prop));
  13303. return true;
  13304. }
  13305. this.resetPreviousNodeTrailingComments(key);
  13306. return false;
  13307. }
  13308. parseClassMember(classBody, member, state) {
  13309. const isStatic = this.isContextual(104);
  13310. if (isStatic) {
  13311. if (this.parseClassMemberFromModifier(classBody, member)) {
  13312. return;
  13313. }
  13314. if (this.eat(5)) {
  13315. this.parseClassStaticBlock(classBody, member);
  13316. return;
  13317. }
  13318. }
  13319. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13320. }
  13321. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13322. const publicMethod = member;
  13323. const privateMethod = member;
  13324. const publicProp = member;
  13325. const privateProp = member;
  13326. const accessorProp = member;
  13327. const method = publicMethod;
  13328. const publicMember = publicMethod;
  13329. member.static = isStatic;
  13330. this.parsePropertyNamePrefixOperator(member);
  13331. if (this.eat(55)) {
  13332. method.kind = "method";
  13333. const isPrivateName = this.match(136);
  13334. this.parseClassElementName(method);
  13335. if (isPrivateName) {
  13336. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13337. return;
  13338. }
  13339. if (this.isNonstaticConstructor(publicMethod)) {
  13340. this.raise(Errors.ConstructorIsGenerator, {
  13341. at: publicMethod.key
  13342. });
  13343. }
  13344. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13345. return;
  13346. }
  13347. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  13348. const isPrivate = this.match(136);
  13349. const key = this.parseClassElementName(member);
  13350. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13351. this.parsePostMemberNameModifiers(publicMember);
  13352. if (this.isClassMethod()) {
  13353. method.kind = "method";
  13354. if (isPrivate) {
  13355. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13356. return;
  13357. }
  13358. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13359. let allowsDirectSuper = false;
  13360. if (isConstructor) {
  13361. publicMethod.kind = "constructor";
  13362. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13363. this.raise(Errors.DuplicateConstructor, {
  13364. at: key
  13365. });
  13366. }
  13367. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13368. this.raise(Errors.OverrideOnConstructor, {
  13369. at: key
  13370. });
  13371. }
  13372. state.hadConstructor = true;
  13373. allowsDirectSuper = state.hadSuperClass;
  13374. }
  13375. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13376. } else if (this.isClassProperty()) {
  13377. if (isPrivate) {
  13378. this.pushClassPrivateProperty(classBody, privateProp);
  13379. } else {
  13380. this.pushClassProperty(classBody, publicProp);
  13381. }
  13382. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  13383. this.resetPreviousNodeTrailingComments(key);
  13384. const isGenerator = this.eat(55);
  13385. if (publicMember.optional) {
  13386. this.unexpected(maybeQuestionTokenStartLoc);
  13387. }
  13388. method.kind = "method";
  13389. const isPrivate = this.match(136);
  13390. this.parseClassElementName(method);
  13391. this.parsePostMemberNameModifiers(publicMember);
  13392. if (isPrivate) {
  13393. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13394. } else {
  13395. if (this.isNonstaticConstructor(publicMethod)) {
  13396. this.raise(Errors.ConstructorIsAsync, {
  13397. at: publicMethod.key
  13398. });
  13399. }
  13400. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13401. }
  13402. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  13403. this.resetPreviousNodeTrailingComments(key);
  13404. method.kind = key.name;
  13405. const isPrivate = this.match(136);
  13406. this.parseClassElementName(publicMethod);
  13407. if (isPrivate) {
  13408. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13409. } else {
  13410. if (this.isNonstaticConstructor(publicMethod)) {
  13411. this.raise(Errors.ConstructorIsAccessor, {
  13412. at: publicMethod.key
  13413. });
  13414. }
  13415. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13416. }
  13417. this.checkGetterSetterParams(publicMethod);
  13418. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  13419. this.expectPlugin("decoratorAutoAccessors");
  13420. this.resetPreviousNodeTrailingComments(key);
  13421. const isPrivate = this.match(136);
  13422. this.parseClassElementName(publicProp);
  13423. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13424. } else if (this.isLineTerminator()) {
  13425. if (isPrivate) {
  13426. this.pushClassPrivateProperty(classBody, privateProp);
  13427. } else {
  13428. this.pushClassProperty(classBody, publicProp);
  13429. }
  13430. } else {
  13431. this.unexpected();
  13432. }
  13433. }
  13434. parseClassElementName(member) {
  13435. const {
  13436. type,
  13437. value
  13438. } = this.state;
  13439. if ((type === 130 || type === 131) && member.static && value === "prototype") {
  13440. this.raise(Errors.StaticPrototype, {
  13441. at: this.state.startLoc
  13442. });
  13443. }
  13444. if (type === 136) {
  13445. if (value === "constructor") {
  13446. this.raise(Errors.ConstructorClassPrivateField, {
  13447. at: this.state.startLoc
  13448. });
  13449. }
  13450. const key = this.parsePrivateName();
  13451. member.key = key;
  13452. return key;
  13453. }
  13454. return this.parsePropertyName(member);
  13455. }
  13456. parseClassStaticBlock(classBody, member) {
  13457. var _member$decorators;
  13458. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13459. const oldLabels = this.state.labels;
  13460. this.state.labels = [];
  13461. this.prodParam.enter(PARAM);
  13462. const body = member.body = [];
  13463. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13464. this.prodParam.exit();
  13465. this.scope.exit();
  13466. this.state.labels = oldLabels;
  13467. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13468. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13469. this.raise(Errors.DecoratorStaticBlock, {
  13470. at: member
  13471. });
  13472. }
  13473. }
  13474. pushClassProperty(classBody, prop) {
  13475. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13476. this.raise(Errors.ConstructorClassField, {
  13477. at: prop.key
  13478. });
  13479. }
  13480. classBody.body.push(this.parseClassProperty(prop));
  13481. }
  13482. pushClassPrivateProperty(classBody, prop) {
  13483. const node = this.parseClassPrivateProperty(prop);
  13484. classBody.body.push(node);
  13485. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13486. }
  13487. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13488. if (!isPrivate && !prop.computed) {
  13489. const key = prop.key;
  13490. if (key.name === "constructor" || key.value === "constructor") {
  13491. this.raise(Errors.ConstructorClassField, {
  13492. at: key
  13493. });
  13494. }
  13495. }
  13496. const node = this.parseClassAccessorProperty(prop);
  13497. classBody.body.push(node);
  13498. if (isPrivate) {
  13499. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13500. }
  13501. }
  13502. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13503. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13504. }
  13505. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13506. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13507. classBody.body.push(node);
  13508. 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;
  13509. this.declareClassPrivateMethodInScope(node, kind);
  13510. }
  13511. declareClassPrivateMethodInScope(node, kind) {
  13512. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13513. }
  13514. parsePostMemberNameModifiers(methodOrProp) {}
  13515. parseClassPrivateProperty(node) {
  13516. this.parseInitializer(node);
  13517. this.semicolon();
  13518. return this.finishNode(node, "ClassPrivateProperty");
  13519. }
  13520. parseClassProperty(node) {
  13521. this.parseInitializer(node);
  13522. this.semicolon();
  13523. return this.finishNode(node, "ClassProperty");
  13524. }
  13525. parseClassAccessorProperty(node) {
  13526. this.parseInitializer(node);
  13527. this.semicolon();
  13528. return this.finishNode(node, "ClassAccessorProperty");
  13529. }
  13530. parseInitializer(node) {
  13531. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13532. this.expressionScope.enter(newExpressionScope());
  13533. this.prodParam.enter(PARAM);
  13534. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13535. this.expressionScope.exit();
  13536. this.prodParam.exit();
  13537. this.scope.exit();
  13538. }
  13539. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13540. if (tokenIsIdentifier(this.state.type)) {
  13541. node.id = this.parseIdentifier();
  13542. if (isStatement) {
  13543. this.declareNameFromIdentifier(node.id, bindingType);
  13544. }
  13545. } else {
  13546. if (optionalId || !isStatement) {
  13547. node.id = null;
  13548. } else {
  13549. throw this.raise(Errors.MissingClassName, {
  13550. at: this.state.startLoc
  13551. });
  13552. }
  13553. }
  13554. }
  13555. parseClassSuper(node) {
  13556. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13557. }
  13558. parseExport(node, decorators) {
  13559. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13560. const parseAfterDefault = !hasDefault || this.eat(12);
  13561. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13562. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13563. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13564. const isFromRequired = hasDefault || hasStar;
  13565. if (hasStar && !hasNamespace) {
  13566. if (hasDefault) this.unexpected();
  13567. if (decorators) {
  13568. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13569. at: node
  13570. });
  13571. }
  13572. this.parseExportFrom(node, true);
  13573. return this.finishNode(node, "ExportAllDeclaration");
  13574. }
  13575. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13576. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13577. throw this.unexpected(null, 5);
  13578. }
  13579. if (hasNamespace && parseAfterNamespace) {
  13580. throw this.unexpected(null, 97);
  13581. }
  13582. let hasDeclaration;
  13583. if (isFromRequired || hasSpecifiers) {
  13584. hasDeclaration = false;
  13585. if (decorators) {
  13586. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13587. at: node
  13588. });
  13589. }
  13590. this.parseExportFrom(node, isFromRequired);
  13591. } else {
  13592. hasDeclaration = this.maybeParseExportDeclaration(node);
  13593. }
  13594. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13595. var _node2$declaration;
  13596. const node2 = node;
  13597. this.checkExport(node2, true, false, !!node2.source);
  13598. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13599. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13600. } else if (decorators) {
  13601. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13602. at: node
  13603. });
  13604. }
  13605. return this.finishNode(node2, "ExportNamedDeclaration");
  13606. }
  13607. if (this.eat(65)) {
  13608. const node2 = node;
  13609. const decl = this.parseExportDefaultExpression();
  13610. node2.declaration = decl;
  13611. if (decl.type === "ClassDeclaration") {
  13612. this.maybeTakeDecorators(decorators, decl, node2);
  13613. } else if (decorators) {
  13614. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13615. at: node
  13616. });
  13617. }
  13618. this.checkExport(node2, true, true);
  13619. return this.finishNode(node2, "ExportDefaultDeclaration");
  13620. }
  13621. throw this.unexpected(null, 5);
  13622. }
  13623. eatExportStar(node) {
  13624. return this.eat(55);
  13625. }
  13626. maybeParseExportDefaultSpecifier(node) {
  13627. if (this.isExportDefaultSpecifier()) {
  13628. this.expectPlugin("exportDefaultFrom");
  13629. const specifier = this.startNode();
  13630. specifier.exported = this.parseIdentifier(true);
  13631. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13632. return true;
  13633. }
  13634. return false;
  13635. }
  13636. maybeParseExportNamespaceSpecifier(node) {
  13637. if (this.isContextual(93)) {
  13638. if (!node.specifiers) node.specifiers = [];
  13639. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13640. this.next();
  13641. specifier.exported = this.parseModuleExportName();
  13642. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13643. return true;
  13644. }
  13645. return false;
  13646. }
  13647. maybeParseExportNamedSpecifiers(node) {
  13648. if (this.match(5)) {
  13649. if (!node.specifiers) node.specifiers = [];
  13650. const isTypeExport = node.exportKind === "type";
  13651. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13652. node.source = null;
  13653. node.declaration = null;
  13654. if (this.hasPlugin("importAssertions")) {
  13655. node.assertions = [];
  13656. }
  13657. return true;
  13658. }
  13659. return false;
  13660. }
  13661. maybeParseExportDeclaration(node) {
  13662. if (this.shouldParseExportDeclaration()) {
  13663. node.specifiers = [];
  13664. node.source = null;
  13665. if (this.hasPlugin("importAssertions")) {
  13666. node.assertions = [];
  13667. }
  13668. node.declaration = this.parseExportDeclaration(node);
  13669. return true;
  13670. }
  13671. return false;
  13672. }
  13673. isAsyncFunction() {
  13674. if (!this.isContextual(95)) return false;
  13675. const next = this.nextTokenStart();
  13676. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13677. }
  13678. parseExportDefaultExpression() {
  13679. const expr = this.startNode();
  13680. if (this.match(68)) {
  13681. this.next();
  13682. return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId);
  13683. } else if (this.isAsyncFunction()) {
  13684. this.next();
  13685. this.next();
  13686. return this.parseFunction(expr, ParseFunctionFlag.Declaration | ParseFunctionFlag.NullableId | ParseFunctionFlag.Async);
  13687. }
  13688. if (this.match(80)) {
  13689. return this.parseClass(expr, true, true);
  13690. }
  13691. if (this.match(26)) {
  13692. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13693. this.raise(Errors.DecoratorBeforeExport, {
  13694. at: this.state.startLoc
  13695. });
  13696. }
  13697. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13698. }
  13699. if (this.match(75) || this.match(74) || this.isLet()) {
  13700. throw this.raise(Errors.UnsupportedDefaultExport, {
  13701. at: this.state.startLoc
  13702. });
  13703. }
  13704. const res = this.parseMaybeAssignAllowIn();
  13705. this.semicolon();
  13706. return res;
  13707. }
  13708. parseExportDeclaration(node) {
  13709. if (this.match(80)) {
  13710. const node = this.parseClass(this.startNode(), true, false);
  13711. return node;
  13712. }
  13713. return this.parseStatementListItem();
  13714. }
  13715. isExportDefaultSpecifier() {
  13716. const {
  13717. type
  13718. } = this.state;
  13719. if (tokenIsIdentifier(type)) {
  13720. if (type === 95 && !this.state.containsEsc || type === 99) {
  13721. return false;
  13722. }
  13723. if ((type === 128 || type === 127) && !this.state.containsEsc) {
  13724. const {
  13725. type: nextType
  13726. } = this.lookahead();
  13727. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13728. this.expectOnePlugin(["flow", "typescript"]);
  13729. return false;
  13730. }
  13731. }
  13732. } else if (!this.match(65)) {
  13733. return false;
  13734. }
  13735. const next = this.nextTokenStart();
  13736. const hasFrom = this.isUnparsedContextual(next, "from");
  13737. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13738. return true;
  13739. }
  13740. if (this.match(65) && hasFrom) {
  13741. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13742. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13743. }
  13744. return false;
  13745. }
  13746. parseExportFrom(node, expect) {
  13747. if (this.eatContextual(97)) {
  13748. node.source = this.parseImportSource();
  13749. this.checkExport(node);
  13750. const assertions = this.maybeParseImportAssertions();
  13751. if (assertions) {
  13752. node.assertions = assertions;
  13753. this.checkJSONModuleImport(node);
  13754. }
  13755. } else if (expect) {
  13756. this.unexpected();
  13757. }
  13758. this.semicolon();
  13759. }
  13760. shouldParseExportDeclaration() {
  13761. const {
  13762. type
  13763. } = this.state;
  13764. if (type === 26) {
  13765. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13766. if (this.hasPlugin("decorators")) {
  13767. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13768. throw this.raise(Errors.DecoratorBeforeExport, {
  13769. at: this.state.startLoc
  13770. });
  13771. }
  13772. return true;
  13773. }
  13774. }
  13775. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13776. }
  13777. checkExport(node, checkNames, isDefault, isFrom) {
  13778. if (checkNames) {
  13779. if (isDefault) {
  13780. this.checkDuplicateExports(node, "default");
  13781. if (this.hasPlugin("exportDefaultFrom")) {
  13782. var _declaration$extra;
  13783. const declaration = node.declaration;
  13784. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13785. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13786. at: declaration
  13787. });
  13788. }
  13789. }
  13790. } else if (node.specifiers && node.specifiers.length) {
  13791. for (const specifier of node.specifiers) {
  13792. const {
  13793. exported
  13794. } = specifier;
  13795. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13796. this.checkDuplicateExports(specifier, exportName);
  13797. if (!isFrom && specifier.local) {
  13798. const {
  13799. local
  13800. } = specifier;
  13801. if (local.type !== "Identifier") {
  13802. this.raise(Errors.ExportBindingIsString, {
  13803. at: specifier,
  13804. localName: local.value,
  13805. exportName
  13806. });
  13807. } else {
  13808. this.checkReservedWord(local.name, local.loc.start, true, false);
  13809. this.scope.checkLocalExport(local);
  13810. }
  13811. }
  13812. }
  13813. } else if (node.declaration) {
  13814. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13815. const id = node.declaration.id;
  13816. if (!id) throw new Error("Assertion failure");
  13817. this.checkDuplicateExports(node, id.name);
  13818. } else if (node.declaration.type === "VariableDeclaration") {
  13819. for (const declaration of node.declaration.declarations) {
  13820. this.checkDeclaration(declaration.id);
  13821. }
  13822. }
  13823. }
  13824. }
  13825. }
  13826. checkDeclaration(node) {
  13827. if (node.type === "Identifier") {
  13828. this.checkDuplicateExports(node, node.name);
  13829. } else if (node.type === "ObjectPattern") {
  13830. for (const prop of node.properties) {
  13831. this.checkDeclaration(prop);
  13832. }
  13833. } else if (node.type === "ArrayPattern") {
  13834. for (const elem of node.elements) {
  13835. if (elem) {
  13836. this.checkDeclaration(elem);
  13837. }
  13838. }
  13839. } else if (node.type === "ObjectProperty") {
  13840. this.checkDeclaration(node.value);
  13841. } else if (node.type === "RestElement") {
  13842. this.checkDeclaration(node.argument);
  13843. } else if (node.type === "AssignmentPattern") {
  13844. this.checkDeclaration(node.left);
  13845. }
  13846. }
  13847. checkDuplicateExports(node, exportName) {
  13848. if (this.exportedIdentifiers.has(exportName)) {
  13849. if (exportName === "default") {
  13850. this.raise(Errors.DuplicateDefaultExport, {
  13851. at: node
  13852. });
  13853. } else {
  13854. this.raise(Errors.DuplicateExport, {
  13855. at: node,
  13856. exportName
  13857. });
  13858. }
  13859. }
  13860. this.exportedIdentifiers.add(exportName);
  13861. }
  13862. parseExportSpecifiers(isInTypeExport) {
  13863. const nodes = [];
  13864. let first = true;
  13865. this.expect(5);
  13866. while (!this.eat(8)) {
  13867. if (first) {
  13868. first = false;
  13869. } else {
  13870. this.expect(12);
  13871. if (this.eat(8)) break;
  13872. }
  13873. const isMaybeTypeOnly = this.isContextual(128);
  13874. const isString = this.match(131);
  13875. const node = this.startNode();
  13876. node.local = this.parseModuleExportName();
  13877. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13878. }
  13879. return nodes;
  13880. }
  13881. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13882. if (this.eatContextual(93)) {
  13883. node.exported = this.parseModuleExportName();
  13884. } else if (isString) {
  13885. node.exported = cloneStringLiteral(node.local);
  13886. } else if (!node.exported) {
  13887. node.exported = cloneIdentifier(node.local);
  13888. }
  13889. return this.finishNode(node, "ExportSpecifier");
  13890. }
  13891. parseModuleExportName() {
  13892. if (this.match(131)) {
  13893. const result = this.parseStringLiteral(this.state.value);
  13894. const surrogate = result.value.match(loneSurrogate);
  13895. if (surrogate) {
  13896. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13897. at: result,
  13898. surrogateCharCode: surrogate[0].charCodeAt(0)
  13899. });
  13900. }
  13901. return result;
  13902. }
  13903. return this.parseIdentifier(true);
  13904. }
  13905. isJSONModuleImport(node) {
  13906. if (node.assertions != null) {
  13907. return node.assertions.some(({
  13908. key,
  13909. value
  13910. }) => {
  13911. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13912. });
  13913. }
  13914. return false;
  13915. }
  13916. checkImportReflection(node) {
  13917. if (node.module) {
  13918. var _node$assertions;
  13919. if (node.specifiers.length !== 1 || node.specifiers[0].type !== "ImportDefaultSpecifier") {
  13920. this.raise(Errors.ImportReflectionNotBinding, {
  13921. at: node.specifiers[0].loc.start
  13922. });
  13923. }
  13924. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  13925. this.raise(Errors.ImportReflectionHasAssertion, {
  13926. at: node.specifiers[0].loc.start
  13927. });
  13928. }
  13929. }
  13930. }
  13931. checkJSONModuleImport(node) {
  13932. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13933. const {
  13934. specifiers
  13935. } = node;
  13936. if (specifiers != null) {
  13937. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13938. let imported;
  13939. if (specifier.type === "ExportSpecifier") {
  13940. imported = specifier.local;
  13941. } else if (specifier.type === "ImportSpecifier") {
  13942. imported = specifier.imported;
  13943. }
  13944. if (imported !== undefined) {
  13945. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13946. }
  13947. });
  13948. if (nonDefaultNamedSpecifier !== undefined) {
  13949. this.raise(Errors.ImportJSONBindingNotDefault, {
  13950. at: nonDefaultNamedSpecifier.loc.start
  13951. });
  13952. }
  13953. }
  13954. }
  13955. }
  13956. parseMaybeImportReflection(node) {
  13957. let isImportReflection = false;
  13958. if (this.isContextual(125)) {
  13959. const lookahead = this.lookahead();
  13960. const nextType = lookahead.type;
  13961. if (tokenIsIdentifier(nextType)) {
  13962. if (nextType !== 97) {
  13963. isImportReflection = true;
  13964. } else {
  13965. const nextNextTokenFirstChar = this.input.charCodeAt(this.nextTokenStartSince(lookahead.end));
  13966. if (nextNextTokenFirstChar === 102) {
  13967. isImportReflection = true;
  13968. }
  13969. }
  13970. } else if (nextType !== 12) {
  13971. isImportReflection = true;
  13972. }
  13973. }
  13974. if (isImportReflection) {
  13975. this.expectPlugin("importReflection");
  13976. this.next();
  13977. node.module = true;
  13978. } else if (this.hasPlugin("importReflection")) {
  13979. node.module = false;
  13980. }
  13981. }
  13982. parseImport(node) {
  13983. node.specifiers = [];
  13984. if (!this.match(131)) {
  13985. this.parseMaybeImportReflection(node);
  13986. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  13987. const parseNext = !hasDefault || this.eat(12);
  13988. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  13989. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  13990. this.expectContextual(97);
  13991. }
  13992. node.source = this.parseImportSource();
  13993. const assertions = this.maybeParseImportAssertions();
  13994. if (assertions) {
  13995. node.assertions = assertions;
  13996. } else {
  13997. const attributes = this.maybeParseModuleAttributes();
  13998. if (attributes) {
  13999. node.attributes = attributes;
  14000. }
  14001. }
  14002. this.checkImportReflection(node);
  14003. this.checkJSONModuleImport(node);
  14004. this.semicolon();
  14005. return this.finishNode(node, "ImportDeclaration");
  14006. }
  14007. parseImportSource() {
  14008. if (!this.match(131)) this.unexpected();
  14009. return this.parseExprAtom();
  14010. }
  14011. shouldParseDefaultImport(node) {
  14012. return tokenIsIdentifier(this.state.type);
  14013. }
  14014. parseImportSpecifierLocal(node, specifier, type) {
  14015. specifier.local = this.parseIdentifier();
  14016. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  14017. }
  14018. finishImportSpecifier(specifier, type, bindingType = BIND_LEXICAL) {
  14019. this.checkLVal(specifier.local, {
  14020. in: {
  14021. type
  14022. },
  14023. binding: bindingType
  14024. });
  14025. return this.finishNode(specifier, type);
  14026. }
  14027. parseAssertEntries() {
  14028. const attrs = [];
  14029. const attrNames = new Set();
  14030. do {
  14031. if (this.match(8)) {
  14032. break;
  14033. }
  14034. const node = this.startNode();
  14035. const keyName = this.state.value;
  14036. if (attrNames.has(keyName)) {
  14037. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14038. at: this.state.startLoc,
  14039. key: keyName
  14040. });
  14041. }
  14042. attrNames.add(keyName);
  14043. if (this.match(131)) {
  14044. node.key = this.parseStringLiteral(keyName);
  14045. } else {
  14046. node.key = this.parseIdentifier(true);
  14047. }
  14048. this.expect(14);
  14049. if (!this.match(131)) {
  14050. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14051. at: this.state.startLoc
  14052. });
  14053. }
  14054. node.value = this.parseStringLiteral(this.state.value);
  14055. attrs.push(this.finishNode(node, "ImportAttribute"));
  14056. } while (this.eat(12));
  14057. return attrs;
  14058. }
  14059. maybeParseModuleAttributes() {
  14060. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  14061. this.expectPlugin("moduleAttributes");
  14062. this.next();
  14063. } else {
  14064. if (this.hasPlugin("moduleAttributes")) return [];
  14065. return null;
  14066. }
  14067. const attrs = [];
  14068. const attributes = new Set();
  14069. do {
  14070. const node = this.startNode();
  14071. node.key = this.parseIdentifier(true);
  14072. if (node.key.name !== "type") {
  14073. this.raise(Errors.ModuleAttributeDifferentFromType, {
  14074. at: node.key
  14075. });
  14076. }
  14077. if (attributes.has(node.key.name)) {
  14078. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  14079. at: node.key,
  14080. key: node.key.name
  14081. });
  14082. }
  14083. attributes.add(node.key.name);
  14084. this.expect(14);
  14085. if (!this.match(131)) {
  14086. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  14087. at: this.state.startLoc
  14088. });
  14089. }
  14090. node.value = this.parseStringLiteral(this.state.value);
  14091. this.finishNode(node, "ImportAttribute");
  14092. attrs.push(node);
  14093. } while (this.eat(12));
  14094. return attrs;
  14095. }
  14096. maybeParseImportAssertions() {
  14097. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  14098. this.expectPlugin("importAssertions");
  14099. this.next();
  14100. } else {
  14101. if (this.hasPlugin("importAssertions")) return [];
  14102. return null;
  14103. }
  14104. this.eat(5);
  14105. const attrs = this.parseAssertEntries();
  14106. this.eat(8);
  14107. return attrs;
  14108. }
  14109. maybeParseDefaultImportSpecifier(node) {
  14110. if (this.shouldParseDefaultImport(node)) {
  14111. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  14112. return true;
  14113. }
  14114. return false;
  14115. }
  14116. maybeParseStarImportSpecifier(node) {
  14117. if (this.match(55)) {
  14118. const specifier = this.startNode();
  14119. this.next();
  14120. this.expectContextual(93);
  14121. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  14122. return true;
  14123. }
  14124. return false;
  14125. }
  14126. parseNamedImportSpecifiers(node) {
  14127. let first = true;
  14128. this.expect(5);
  14129. while (!this.eat(8)) {
  14130. if (first) {
  14131. first = false;
  14132. } else {
  14133. if (this.eat(14)) {
  14134. throw this.raise(Errors.DestructureNamedImport, {
  14135. at: this.state.startLoc
  14136. });
  14137. }
  14138. this.expect(12);
  14139. if (this.eat(8)) break;
  14140. }
  14141. const specifier = this.startNode();
  14142. const importedIsString = this.match(131);
  14143. const isMaybeTypeOnly = this.isContextual(128);
  14144. specifier.imported = this.parseModuleExportName();
  14145. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  14146. node.specifiers.push(importSpecifier);
  14147. }
  14148. }
  14149. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  14150. if (this.eatContextual(93)) {
  14151. specifier.local = this.parseIdentifier();
  14152. } else {
  14153. const {
  14154. imported
  14155. } = specifier;
  14156. if (importedIsString) {
  14157. throw this.raise(Errors.ImportBindingIsString, {
  14158. at: specifier,
  14159. importName: imported.value
  14160. });
  14161. }
  14162. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  14163. if (!specifier.local) {
  14164. specifier.local = cloneIdentifier(imported);
  14165. }
  14166. }
  14167. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  14168. }
  14169. isThisParam(param) {
  14170. return param.type === "Identifier" && param.name === "this";
  14171. }
  14172. }
  14173. class Parser extends StatementParser {
  14174. constructor(options, input) {
  14175. options = getOptions(options);
  14176. super(options, input);
  14177. this.options = options;
  14178. this.initializeScopes();
  14179. this.plugins = pluginsMap(this.options.plugins);
  14180. this.filename = options.sourceFilename;
  14181. }
  14182. getScopeHandler() {
  14183. return ScopeHandler;
  14184. }
  14185. parse() {
  14186. this.enterInitialScopes();
  14187. const file = this.startNode();
  14188. const program = this.startNode();
  14189. this.nextToken();
  14190. file.errors = null;
  14191. this.parseTopLevel(file, program);
  14192. file.errors = this.state.errors;
  14193. return file;
  14194. }
  14195. }
  14196. function pluginsMap(plugins) {
  14197. const pluginMap = new Map();
  14198. for (const plugin of plugins) {
  14199. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  14200. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  14201. }
  14202. return pluginMap;
  14203. }
  14204. function parse(input, options) {
  14205. var _options;
  14206. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  14207. options = Object.assign({}, options);
  14208. try {
  14209. options.sourceType = "module";
  14210. const parser = getParser(options, input);
  14211. const ast = parser.parse();
  14212. if (parser.sawUnambiguousESM) {
  14213. return ast;
  14214. }
  14215. if (parser.ambiguousScriptDifferentAst) {
  14216. try {
  14217. options.sourceType = "script";
  14218. return getParser(options, input).parse();
  14219. } catch (_unused) {}
  14220. } else {
  14221. ast.program.sourceType = "script";
  14222. }
  14223. return ast;
  14224. } catch (moduleError) {
  14225. try {
  14226. options.sourceType = "script";
  14227. return getParser(options, input).parse();
  14228. } catch (_unused2) {}
  14229. throw moduleError;
  14230. }
  14231. } else {
  14232. return getParser(options, input).parse();
  14233. }
  14234. }
  14235. function parseExpression(input, options) {
  14236. const parser = getParser(options, input);
  14237. if (parser.options.strictMode) {
  14238. parser.state.strict = true;
  14239. }
  14240. return parser.getExpression();
  14241. }
  14242. function generateExportedTokenTypes(internalTokenTypes) {
  14243. const tokenTypes = {};
  14244. for (const typeName of Object.keys(internalTokenTypes)) {
  14245. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14246. }
  14247. return tokenTypes;
  14248. }
  14249. const tokTypes = generateExportedTokenTypes(tt);
  14250. function getParser(options, input) {
  14251. let cls = Parser;
  14252. if (options != null && options.plugins) {
  14253. validatePlugins(options.plugins);
  14254. cls = getParserClass(options.plugins);
  14255. }
  14256. return new cls(options, input);
  14257. }
  14258. const parserClassCache = {};
  14259. function getParserClass(pluginsFromOptions) {
  14260. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  14261. const key = pluginList.join("/");
  14262. let cls = parserClassCache[key];
  14263. if (!cls) {
  14264. cls = Parser;
  14265. for (const plugin of pluginList) {
  14266. cls = mixinPlugins[plugin](cls);
  14267. }
  14268. parserClassCache[key] = cls;
  14269. }
  14270. return cls;
  14271. }
  14272. exports.parse = parse;
  14273. exports.parseExpression = parseExpression;
  14274. exports.tokTypes = tokTypes;
  14275. //# sourceMappingURL=index.js.map