index.js 458 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512
  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. const ParseErrorCodes = Object.freeze({
  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: _(`import.meta may appear only with 'sourceType: "module"'`, {
  63. code: ParseErrorCodes.SourceTypeModuleError
  64. }),
  65. ImportOutsideModule: _(`'import' and 'export' may appear only with 'sourceType: "module"'`, {
  66. code: ParseErrorCodes.SourceTypeModuleError
  67. })
  68. }));
  69. const NodeDescriptions = {
  70. ArrayPattern: "array destructuring pattern",
  71. AssignmentExpression: "assignment expression",
  72. AssignmentPattern: "assignment expression",
  73. ArrowFunctionExpression: "arrow function expression",
  74. ConditionalExpression: "conditional expression",
  75. ForOfStatement: "for-of statement",
  76. ForInStatement: "for-in statement",
  77. ForStatement: "for-loop",
  78. FormalParameters: "function parameter list",
  79. Identifier: "identifier",
  80. ObjectPattern: "object destructuring pattern",
  81. ParenthesizedExpression: "parenthesized expression",
  82. RestElement: "rest element",
  83. UpdateExpression: {
  84. true: "prefix operation",
  85. false: "postfix operation"
  86. },
  87. VariableDeclarator: "variable declaration",
  88. YieldExpression: "yield expression"
  89. };
  90. const toNodeDescription = ({
  91. type,
  92. prefix
  93. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  94. var StandardErrors = (_ => ({
  95. AccessorIsGenerator: _(({
  96. kind
  97. }) => `A ${kind}ter cannot be a generator.`),
  98. ArgumentsInClass: _("'arguments' is only allowed in functions and class methods."),
  99. AsyncFunctionInSingleStatementContext: _("Async functions can only be declared at the top level or inside a block."),
  100. AwaitBindingIdentifier: _("Can not use 'await' as identifier inside an async function."),
  101. AwaitBindingIdentifierInStaticBlock: _("Can not use 'await' as identifier inside a static block."),
  102. AwaitExpressionFormalParameter: _("'await' is not allowed in async function parameters."),
  103. AwaitNotInAsyncContext: _("'await' is only allowed within async functions and at the top levels of modules."),
  104. AwaitNotInAsyncFunction: _("'await' is only allowed within async functions."),
  105. BadGetterArity: _("A 'get' accesor must not have any formal parameters."),
  106. BadSetterArity: _("A 'set' accesor must have exactly one formal parameter."),
  107. BadSetterRestParameter: _("A 'set' accesor function argument must not be a rest parameter."),
  108. ConstructorClassField: _("Classes may not have a field named 'constructor'."),
  109. ConstructorClassPrivateField: _("Classes may not have a private field named '#constructor'."),
  110. ConstructorIsAccessor: _("Class constructor may not be an accessor."),
  111. ConstructorIsAsync: _("Constructor can't be an async function."),
  112. ConstructorIsGenerator: _("Constructor can't be a generator."),
  113. DeclarationMissingInitializer: _(({
  114. kind
  115. }) => `Missing initializer in ${kind} declaration.`),
  116. DecoratorBeforeExport: _("Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax."),
  117. DecoratorConstructor: _("Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?"),
  118. DecoratorExportClass: _("Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead."),
  119. DecoratorSemicolon: _("Decorators must not be followed by a semicolon."),
  120. DecoratorStaticBlock: _("Decorators can't be used with a static block."),
  121. DeletePrivateField: _("Deleting a private field is not allowed."),
  122. DestructureNamedImport: _("ES2015 named imports do not destructure. Use another statement for destructuring after the import."),
  123. DuplicateConstructor: _("Duplicate constructor in the same class."),
  124. DuplicateDefaultExport: _("Only one default export allowed per module."),
  125. DuplicateExport: _(({
  126. exportName
  127. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`),
  128. DuplicateProto: _("Redefinition of __proto__ property."),
  129. DuplicateRegExpFlags: _("Duplicate regular expression flag."),
  130. ElementAfterRest: _("Rest element must be last element."),
  131. EscapedCharNotAnIdentifier: _("Invalid Unicode escape."),
  132. ExportBindingIsString: _(({
  133. localName,
  134. exportName
  135. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`),
  136. ExportDefaultFromAsIdentifier: _("'from' is not allowed as an identifier after 'export default'."),
  137. ForInOfLoopInitializer: _(({
  138. type
  139. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`),
  140. ForOfAsync: _("The left-hand side of a for-of loop may not be 'async'."),
  141. ForOfLet: _("The left-hand side of a for-of loop may not start with 'let'."),
  142. GeneratorInSingleStatementContext: _("Generators can only be declared at the top level or inside a block."),
  143. IllegalBreakContinue: _(({
  144. type
  145. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`),
  146. IllegalLanguageModeDirective: _("Illegal 'use strict' directive in function with non-simple parameter list."),
  147. IllegalReturn: _("'return' outside of function."),
  148. ImportBindingIsString: _(({
  149. importName
  150. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`),
  151. ImportCallArgumentTrailingComma: _("Trailing comma is disallowed inside import(...) arguments."),
  152. ImportCallArity: _(({
  153. maxArgumentCount
  154. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`),
  155. ImportCallNotNewExpression: _("Cannot use new with import(...)."),
  156. ImportCallSpreadArgument: _("`...` is not allowed in `import()`."),
  157. IncompatibleRegExpUVFlags: _("The 'u' and 'v' regular expression flags cannot be enabled at the same time."),
  158. InvalidBigIntLiteral: _("Invalid BigIntLiteral."),
  159. InvalidCodePoint: _("Code point out of bounds."),
  160. InvalidCoverInitializedName: _("Invalid shorthand property initializer."),
  161. InvalidDecimal: _("Invalid decimal."),
  162. InvalidDigit: _(({
  163. radix
  164. }) => `Expected number in radix ${radix}.`),
  165. InvalidEscapeSequence: _("Bad character escape sequence."),
  166. InvalidEscapeSequenceTemplate: _("Invalid escape sequence in template."),
  167. InvalidEscapedReservedWord: _(({
  168. reservedWord
  169. }) => `Escape sequence in keyword ${reservedWord}.`),
  170. InvalidIdentifier: _(({
  171. identifierName
  172. }) => `Invalid identifier ${identifierName}.`),
  173. InvalidLhs: _(({
  174. ancestor
  175. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`),
  176. InvalidLhsBinding: _(({
  177. ancestor
  178. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`),
  179. InvalidNumber: _("Invalid number."),
  180. InvalidOrMissingExponent: _("Floating-point numbers require a valid exponent after the 'e'."),
  181. InvalidOrUnexpectedToken: _(({
  182. unexpected
  183. }) => `Unexpected character '${unexpected}'.`),
  184. InvalidParenthesizedAssignment: _("Invalid parenthesized assignment pattern."),
  185. InvalidPrivateFieldResolution: _(({
  186. identifierName
  187. }) => `Private name #${identifierName} is not defined.`),
  188. InvalidPropertyBindingPattern: _("Binding member expression."),
  189. InvalidRecordProperty: _("Only properties and spread elements are allowed in record definitions."),
  190. InvalidRestAssignmentPattern: _("Invalid rest operator's argument."),
  191. LabelRedeclaration: _(({
  192. labelName
  193. }) => `Label '${labelName}' is already declared.`),
  194. LetInLexicalBinding: _("'let' is not allowed to be used as a name in 'let' or 'const' declarations."),
  195. LineTerminatorBeforeArrow: _("No line break is allowed before '=>'."),
  196. MalformedRegExpFlags: _("Invalid regular expression flag."),
  197. MissingClassName: _("A class name is required."),
  198. MissingEqInAssignment: _("Only '=' operator can be used for specifying default value."),
  199. MissingSemicolon: _("Missing semicolon."),
  200. MissingPlugin: _(({
  201. missingPlugin
  202. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`),
  203. MissingOneOfPlugins: _(({
  204. missingPlugin
  205. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`),
  206. MissingUnicodeEscape: _("Expecting Unicode escape sequence \\uXXXX."),
  207. MixingCoalesceWithLogical: _("Nullish coalescing operator(??) requires parens when mixing with logical operators."),
  208. ModuleAttributeDifferentFromType: _("The only accepted module attribute is `type`."),
  209. ModuleAttributeInvalidValue: _("Only string literals are allowed as module attribute values."),
  210. ModuleAttributesWithDuplicateKeys: _(({
  211. key
  212. }) => `Duplicate key "${key}" is not allowed in module attributes.`),
  213. ModuleExportNameHasLoneSurrogate: _(({
  214. surrogateCharCode
  215. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`),
  216. ModuleExportUndefined: _(({
  217. localName
  218. }) => `Export '${localName}' is not defined.`),
  219. MultipleDefaultsInSwitch: _("Multiple default clauses."),
  220. NewlineAfterThrow: _("Illegal newline after throw."),
  221. NoCatchOrFinally: _("Missing catch or finally clause."),
  222. NumberIdentifier: _("Identifier directly after number."),
  223. NumericSeparatorInEscapeSequence: _("Numeric separators are not allowed inside unicode escape sequences or hex escape sequences."),
  224. ObsoleteAwaitStar: _("'await*' has been removed from the async functions proposal. Use Promise.all() instead."),
  225. OptionalChainingNoNew: _("Constructors in/after an Optional Chain are not allowed."),
  226. OptionalChainingNoTemplate: _("Tagged Template Literals are not allowed in optionalChain."),
  227. OverrideOnConstructor: _("'override' modifier cannot appear on a constructor declaration."),
  228. ParamDupe: _("Argument name clash."),
  229. PatternHasAccessor: _("Object pattern can't contain getter or setter."),
  230. PatternHasMethod: _("Object pattern can't contain methods."),
  231. PrivateInExpectedIn: _(({
  232. identifierName
  233. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`),
  234. PrivateNameRedeclaration: _(({
  235. identifierName
  236. }) => `Duplicate private name #${identifierName}.`),
  237. RecordExpressionBarIncorrectEndSyntaxType: _("Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
  238. RecordExpressionBarIncorrectStartSyntaxType: _("Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
  239. RecordExpressionHashIncorrectStartSyntaxType: _("Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'."),
  240. RecordNoProto: _("'__proto__' is not allowed in Record expressions."),
  241. RestTrailingComma: _("Unexpected trailing comma after rest element."),
  242. 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."),
  243. StaticPrototype: _("Classes may not have static property named prototype."),
  244. 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?"),
  245. SuperPrivateField: _("Private fields can't be accessed on super."),
  246. TrailingDecorator: _("Decorators must be attached to a class element."),
  247. TupleExpressionBarIncorrectEndSyntaxType: _("Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
  248. TupleExpressionBarIncorrectStartSyntaxType: _("Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'."),
  249. TupleExpressionHashIncorrectStartSyntaxType: _("Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'."),
  250. UnexpectedArgumentPlaceholder: _("Unexpected argument placeholder."),
  251. UnexpectedAwaitAfterPipelineBody: _('Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.'),
  252. UnexpectedDigitAfterHash: _("Unexpected digit after hash token."),
  253. UnexpectedImportExport: _("'import' and 'export' may only appear at the top level."),
  254. UnexpectedKeyword: _(({
  255. keyword
  256. }) => `Unexpected keyword '${keyword}'.`),
  257. UnexpectedLeadingDecorator: _("Leading decorators must be attached to a class declaration."),
  258. UnexpectedLexicalDeclaration: _("Lexical declaration cannot appear in a single-statement context."),
  259. UnexpectedNewTarget: _("`new.target` can only be used in functions or class properties."),
  260. UnexpectedNumericSeparator: _("A numeric separator is only allowed between two digits."),
  261. UnexpectedPrivateField: _("Unexpected private name."),
  262. UnexpectedReservedWord: _(({
  263. reservedWord
  264. }) => `Unexpected reserved word '${reservedWord}'.`),
  265. UnexpectedSuper: _("'super' is only allowed in object methods and classes."),
  266. UnexpectedToken: _(({
  267. expected,
  268. unexpected
  269. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`),
  270. UnexpectedTokenUnaryExponentiation: _("Illegal expression. Wrap left hand side or entire exponentiation in parentheses."),
  271. UnsupportedBind: _("Binding should be performed on object property."),
  272. UnsupportedDecoratorExport: _("A decorated export must export a class declaration."),
  273. UnsupportedDefaultExport: _("Only expressions, functions or classes are allowed as the `default` export."),
  274. UnsupportedImport: _("`import` can only be used in `import()` or `import.meta`."),
  275. UnsupportedMetaProperty: _(({
  276. target,
  277. onlyValidPropertyName
  278. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`),
  279. UnsupportedParameterDecorator: _("Decorators cannot be used to decorate parameters."),
  280. UnsupportedPropertyDecorator: _("Decorators cannot be used to decorate object literal properties."),
  281. UnsupportedSuper: _("'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop])."),
  282. UnterminatedComment: _("Unterminated comment."),
  283. UnterminatedRegExp: _("Unterminated regular expression."),
  284. UnterminatedString: _("Unterminated string constant."),
  285. UnterminatedTemplate: _("Unterminated template."),
  286. VarRedeclaration: _(({
  287. identifierName
  288. }) => `Identifier '${identifierName}' has already been declared.`),
  289. YieldBindingIdentifier: _("Can not use 'yield' as identifier inside a generator."),
  290. YieldInParameter: _("Yield expression is not allowed in formal parameters."),
  291. ZeroDigitNumericSeparator: _("Numeric separator can not be used after leading 0.")
  292. }));
  293. var StrictModeErrors = (_ => ({
  294. StrictDelete: _("Deleting local variable in strict mode."),
  295. StrictEvalArguments: _(({
  296. referenceName
  297. }) => `Assigning to '${referenceName}' in strict mode.`),
  298. StrictEvalArgumentsBinding: _(({
  299. bindingName
  300. }) => `Binding '${bindingName}' in strict mode.`),
  301. StrictFunction: _("In strict mode code, functions can only be declared at top level or inside a block."),
  302. StrictNumericEscape: _("The only valid numeric escape in strict mode is '\\0'."),
  303. StrictOctalLiteral: _("Legacy octal literals are not allowed in strict mode."),
  304. StrictWith: _("'with' in strict mode.")
  305. }));
  306. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  307. var PipelineOperatorErrors = (_ => ({
  308. PipeBodyIsTighter: _("Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence."),
  309. PipeTopicRequiresHackPipes: _('Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'),
  310. PipeTopicUnbound: _("Topic reference is unbound; it must be inside a pipe body."),
  311. PipeTopicUnconfiguredToken: _(({
  312. token
  313. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`),
  314. PipeTopicUnused: _("Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once."),
  315. PipeUnparenthesizedBody: _(({
  316. type
  317. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  318. type
  319. })}; please wrap it in parentheses.`),
  320. PipelineBodyNoArrow: _('Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.'),
  321. PipelineBodySequenceExpression: _("Pipeline body may not be a comma-separated sequence expression."),
  322. PipelineHeadSequenceExpression: _("Pipeline head should not be a comma-separated sequence expression."),
  323. PipelineTopicUnused: _("Pipeline is in topic style but does not use topic reference."),
  324. PrimaryTopicNotAllowed: _("Topic reference was used in a lexical context without topic binding."),
  325. PrimaryTopicRequiresSmartPipeline: _('Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.')
  326. }));
  327. const _excluded$1 = ["toMessage"];
  328. function toParseErrorConstructor(_ref) {
  329. let {
  330. toMessage
  331. } = _ref,
  332. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  333. return function constructor({
  334. loc,
  335. details
  336. }) {
  337. return instantiate(SyntaxError, Object.assign({}, properties, {
  338. loc
  339. }), {
  340. clone(overrides = {}) {
  341. const loc = overrides.loc || {};
  342. return constructor({
  343. 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),
  344. details: Object.assign({}, this.details, overrides.details)
  345. });
  346. },
  347. details: {
  348. value: details,
  349. enumerable: false
  350. },
  351. message: {
  352. get() {
  353. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  354. },
  355. set(value) {
  356. Object.defineProperty(this, "message", {
  357. value
  358. });
  359. }
  360. },
  361. pos: {
  362. reflect: "loc.index",
  363. enumerable: true
  364. },
  365. missingPlugin: "missingPlugin" in details && {
  366. reflect: "details.missingPlugin",
  367. enumerable: true
  368. }
  369. });
  370. };
  371. }
  372. function toParseErrorCredentials(toMessageOrMessage, credentials) {
  373. return Object.assign({
  374. toMessage: typeof toMessageOrMessage === "string" ? () => toMessageOrMessage : toMessageOrMessage
  375. }, credentials);
  376. }
  377. function ParseErrorEnum(argument, syntaxPlugin) {
  378. if (Array.isArray(argument)) {
  379. return toParseErrorCredentialsMap => ParseErrorEnum(toParseErrorCredentialsMap, argument[0]);
  380. }
  381. const partialCredentials = argument(toParseErrorCredentials);
  382. const ParseErrorConstructors = {};
  383. for (const reasonCode of Object.keys(partialCredentials)) {
  384. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  385. code: ParseErrorCodes.SyntaxError,
  386. reasonCode
  387. }, syntaxPlugin ? {
  388. syntaxPlugin
  389. } : {}, partialCredentials[reasonCode]));
  390. }
  391. return ParseErrorConstructors;
  392. }
  393. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  394. const {
  395. defineProperty
  396. } = Object;
  397. const toUnenumerable = (object, key) => defineProperty(object, key, {
  398. enumerable: false,
  399. value: object[key]
  400. });
  401. function toESTreeLocation(node) {
  402. toUnenumerable(node.loc.start, "index");
  403. toUnenumerable(node.loc.end, "index");
  404. return node;
  405. }
  406. var estree = (superClass => class extends superClass {
  407. parse() {
  408. const file = toESTreeLocation(super.parse());
  409. if (this.options.tokens) {
  410. file.tokens = file.tokens.map(toESTreeLocation);
  411. }
  412. return file;
  413. }
  414. parseRegExpLiteral({
  415. pattern,
  416. flags
  417. }) {
  418. let regex = null;
  419. try {
  420. regex = new RegExp(pattern, flags);
  421. } catch (e) {}
  422. const node = this.estreeParseLiteral(regex);
  423. node.regex = {
  424. pattern,
  425. flags
  426. };
  427. return node;
  428. }
  429. parseBigIntLiteral(value) {
  430. let bigInt;
  431. try {
  432. bigInt = BigInt(value);
  433. } catch (_unused) {
  434. bigInt = null;
  435. }
  436. const node = this.estreeParseLiteral(bigInt);
  437. node.bigint = String(node.value || value);
  438. return node;
  439. }
  440. parseDecimalLiteral(value) {
  441. const decimal = null;
  442. const node = this.estreeParseLiteral(decimal);
  443. node.decimal = String(node.value || value);
  444. return node;
  445. }
  446. estreeParseLiteral(value) {
  447. return this.parseLiteral(value, "Literal");
  448. }
  449. parseStringLiteral(value) {
  450. return this.estreeParseLiteral(value);
  451. }
  452. parseNumericLiteral(value) {
  453. return this.estreeParseLiteral(value);
  454. }
  455. parseNullLiteral() {
  456. return this.estreeParseLiteral(null);
  457. }
  458. parseBooleanLiteral(value) {
  459. return this.estreeParseLiteral(value);
  460. }
  461. directiveToStmt(directive) {
  462. const directiveLiteral = directive.value;
  463. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  464. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  465. expression.value = directiveLiteral.extra.expressionValue;
  466. expression.raw = directiveLiteral.extra.raw;
  467. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.loc.end);
  468. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  469. return this.finishNodeAt(stmt, "ExpressionStatement", directive.loc.end);
  470. }
  471. initFunction(node, isAsync) {
  472. super.initFunction(node, isAsync);
  473. node.expression = false;
  474. }
  475. checkDeclaration(node) {
  476. if (node != null && this.isObjectProperty(node)) {
  477. this.checkDeclaration(node.value);
  478. } else {
  479. super.checkDeclaration(node);
  480. }
  481. }
  482. getObjectOrClassMethodParams(method) {
  483. return method.value.params;
  484. }
  485. isValidDirective(stmt) {
  486. var _stmt$expression$extr;
  487. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  488. }
  489. parseBlockBody(node, ...args) {
  490. super.parseBlockBody(node, ...args);
  491. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  492. node.body = directiveStatements.concat(node.body);
  493. delete node.directives;
  494. }
  495. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  496. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  497. if (method.typeParameters) {
  498. method.value.typeParameters = method.typeParameters;
  499. delete method.typeParameters;
  500. }
  501. classBody.body.push(method);
  502. }
  503. parsePrivateName() {
  504. const node = super.parsePrivateName();
  505. {
  506. if (!this.getPluginOption("estree", "classFeatures")) {
  507. return node;
  508. }
  509. }
  510. return this.convertPrivateNameToPrivateIdentifier(node);
  511. }
  512. convertPrivateNameToPrivateIdentifier(node) {
  513. const name = super.getPrivateNameSV(node);
  514. node = node;
  515. delete node.id;
  516. node.name = name;
  517. node.type = "PrivateIdentifier";
  518. return node;
  519. }
  520. isPrivateName(node) {
  521. {
  522. if (!this.getPluginOption("estree", "classFeatures")) {
  523. return super.isPrivateName(node);
  524. }
  525. }
  526. return node.type === "PrivateIdentifier";
  527. }
  528. getPrivateNameSV(node) {
  529. {
  530. if (!this.getPluginOption("estree", "classFeatures")) {
  531. return super.getPrivateNameSV(node);
  532. }
  533. }
  534. return node.name;
  535. }
  536. parseLiteral(value, type) {
  537. const node = super.parseLiteral(value, type);
  538. node.raw = node.extra.raw;
  539. delete node.extra;
  540. return node;
  541. }
  542. parseFunctionBody(node, allowExpression, isMethod = false) {
  543. super.parseFunctionBody(node, allowExpression, isMethod);
  544. node.expression = node.body.type !== "BlockStatement";
  545. }
  546. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  547. let funcNode = this.startNode();
  548. funcNode.kind = node.kind;
  549. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  550. funcNode.type = "FunctionExpression";
  551. delete funcNode.kind;
  552. node.value = funcNode;
  553. if (type === "ClassPrivateMethod") {
  554. node.computed = false;
  555. }
  556. type = "MethodDefinition";
  557. return this.finishNode(node, type);
  558. }
  559. parseClassProperty(...args) {
  560. const propertyNode = super.parseClassProperty(...args);
  561. {
  562. if (!this.getPluginOption("estree", "classFeatures")) {
  563. return propertyNode;
  564. }
  565. }
  566. propertyNode.type = "PropertyDefinition";
  567. return propertyNode;
  568. }
  569. parseClassPrivateProperty(...args) {
  570. const propertyNode = super.parseClassPrivateProperty(...args);
  571. {
  572. if (!this.getPluginOption("estree", "classFeatures")) {
  573. return propertyNode;
  574. }
  575. }
  576. propertyNode.type = "PropertyDefinition";
  577. propertyNode.computed = false;
  578. return propertyNode;
  579. }
  580. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  581. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  582. if (node) {
  583. node.type = "Property";
  584. if (node.kind === "method") node.kind = "init";
  585. node.shorthand = false;
  586. }
  587. return node;
  588. }
  589. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  590. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  591. if (node) {
  592. node.kind = "init";
  593. node.type = "Property";
  594. }
  595. return node;
  596. }
  597. isValidLVal(type, ...rest) {
  598. return type === "Property" ? "value" : super.isValidLVal(type, ...rest);
  599. }
  600. isAssignable(node, isBinding) {
  601. if (node != null && this.isObjectProperty(node)) {
  602. return this.isAssignable(node.value, isBinding);
  603. }
  604. return super.isAssignable(node, isBinding);
  605. }
  606. toAssignable(node, isLHS = false) {
  607. if (node != null && this.isObjectProperty(node)) {
  608. const {
  609. key,
  610. value
  611. } = node;
  612. if (this.isPrivateName(key)) {
  613. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  614. }
  615. this.toAssignable(value, isLHS);
  616. return node;
  617. }
  618. return super.toAssignable(node, isLHS);
  619. }
  620. toAssignableObjectExpressionProp(prop, ...args) {
  621. if (prop.kind === "get" || prop.kind === "set") {
  622. this.raise(Errors.PatternHasAccessor, {
  623. at: prop.key
  624. });
  625. } else if (prop.method) {
  626. this.raise(Errors.PatternHasMethod, {
  627. at: prop.key
  628. });
  629. } else {
  630. super.toAssignableObjectExpressionProp(prop, ...args);
  631. }
  632. }
  633. finishCallExpression(node, optional) {
  634. super.finishCallExpression(node, optional);
  635. if (node.callee.type === "Import") {
  636. node.type = "ImportExpression";
  637. node.source = node.arguments[0];
  638. if (this.hasPlugin("importAssertions")) {
  639. var _node$arguments$;
  640. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  641. }
  642. delete node.arguments;
  643. delete node.callee;
  644. }
  645. return node;
  646. }
  647. toReferencedArguments(node) {
  648. if (node.type === "ImportExpression") {
  649. return;
  650. }
  651. super.toReferencedArguments(node);
  652. }
  653. parseExport(node) {
  654. super.parseExport(node);
  655. switch (node.type) {
  656. case "ExportAllDeclaration":
  657. node.exported = null;
  658. break;
  659. case "ExportNamedDeclaration":
  660. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  661. node.type = "ExportAllDeclaration";
  662. node.exported = node.specifiers[0].exported;
  663. delete node.specifiers;
  664. }
  665. break;
  666. }
  667. return node;
  668. }
  669. parseSubscript(base, startPos, startLoc, noCalls, state) {
  670. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  671. if (state.optionalChainMember) {
  672. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  673. node.type = node.type.substring(8);
  674. }
  675. if (state.stop) {
  676. const chain = this.startNodeAtNode(node);
  677. chain.expression = node;
  678. return this.finishNode(chain, "ChainExpression");
  679. }
  680. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  681. node.optional = false;
  682. }
  683. return node;
  684. }
  685. hasPropertyAsPrivateName(node) {
  686. if (node.type === "ChainExpression") {
  687. node = node.expression;
  688. }
  689. return super.hasPropertyAsPrivateName(node);
  690. }
  691. isOptionalChain(node) {
  692. return node.type === "ChainExpression";
  693. }
  694. isObjectProperty(node) {
  695. return node.type === "Property" && node.kind === "init" && !node.method;
  696. }
  697. isObjectMethod(node) {
  698. return node.method || node.kind === "get" || node.kind === "set";
  699. }
  700. finishNodeAt(node, type, endLoc) {
  701. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  702. }
  703. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  704. super.resetEndLocation(node, endLoc);
  705. toESTreeLocation(node);
  706. }
  707. });
  708. class TokContext {
  709. constructor(token, preserveSpace) {
  710. this.token = void 0;
  711. this.preserveSpace = void 0;
  712. this.token = token;
  713. this.preserveSpace = !!preserveSpace;
  714. }
  715. }
  716. const types = {
  717. brace: new TokContext("{"),
  718. j_oTag: new TokContext("<tag"),
  719. j_cTag: new TokContext("</tag"),
  720. j_expr: new TokContext("<tag>...</tag>", true)
  721. };
  722. {
  723. types.template = new TokContext("`", true);
  724. }
  725. const beforeExpr = true;
  726. const startsExpr = true;
  727. const isLoop = true;
  728. const isAssign = true;
  729. const prefix = true;
  730. const postfix = true;
  731. class ExportedTokenType {
  732. constructor(label, conf = {}) {
  733. this.label = void 0;
  734. this.keyword = void 0;
  735. this.beforeExpr = void 0;
  736. this.startsExpr = void 0;
  737. this.rightAssociative = void 0;
  738. this.isLoop = void 0;
  739. this.isAssign = void 0;
  740. this.prefix = void 0;
  741. this.postfix = void 0;
  742. this.binop = void 0;
  743. this.label = label;
  744. this.keyword = conf.keyword;
  745. this.beforeExpr = !!conf.beforeExpr;
  746. this.startsExpr = !!conf.startsExpr;
  747. this.rightAssociative = !!conf.rightAssociative;
  748. this.isLoop = !!conf.isLoop;
  749. this.isAssign = !!conf.isAssign;
  750. this.prefix = !!conf.prefix;
  751. this.postfix = !!conf.postfix;
  752. this.binop = conf.binop != null ? conf.binop : null;
  753. {
  754. this.updateContext = null;
  755. }
  756. }
  757. }
  758. const keywords$1 = new Map();
  759. function createKeyword(name, options = {}) {
  760. options.keyword = name;
  761. const token = createToken(name, options);
  762. keywords$1.set(name, token);
  763. return token;
  764. }
  765. function createBinop(name, binop) {
  766. return createToken(name, {
  767. beforeExpr,
  768. binop
  769. });
  770. }
  771. let tokenTypeCounter = -1;
  772. const tokenTypes = [];
  773. const tokenLabels = [];
  774. const tokenBinops = [];
  775. const tokenBeforeExprs = [];
  776. const tokenStartsExprs = [];
  777. const tokenPrefixes = [];
  778. function createToken(name, options = {}) {
  779. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  780. ++tokenTypeCounter;
  781. tokenLabels.push(name);
  782. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  783. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  784. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  785. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  786. tokenTypes.push(new ExportedTokenType(name, options));
  787. return tokenTypeCounter;
  788. }
  789. function createKeywordLike(name, options = {}) {
  790. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  791. ++tokenTypeCounter;
  792. keywords$1.set(name, tokenTypeCounter);
  793. tokenLabels.push(name);
  794. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  795. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  796. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  797. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  798. tokenTypes.push(new ExportedTokenType("name", options));
  799. return tokenTypeCounter;
  800. }
  801. const tt = {
  802. bracketL: createToken("[", {
  803. beforeExpr,
  804. startsExpr
  805. }),
  806. bracketHashL: createToken("#[", {
  807. beforeExpr,
  808. startsExpr
  809. }),
  810. bracketBarL: createToken("[|", {
  811. beforeExpr,
  812. startsExpr
  813. }),
  814. bracketR: createToken("]"),
  815. bracketBarR: createToken("|]"),
  816. braceL: createToken("{", {
  817. beforeExpr,
  818. startsExpr
  819. }),
  820. braceBarL: createToken("{|", {
  821. beforeExpr,
  822. startsExpr
  823. }),
  824. braceHashL: createToken("#{", {
  825. beforeExpr,
  826. startsExpr
  827. }),
  828. braceR: createToken("}", {
  829. beforeExpr
  830. }),
  831. braceBarR: createToken("|}"),
  832. parenL: createToken("(", {
  833. beforeExpr,
  834. startsExpr
  835. }),
  836. parenR: createToken(")"),
  837. comma: createToken(",", {
  838. beforeExpr
  839. }),
  840. semi: createToken(";", {
  841. beforeExpr
  842. }),
  843. colon: createToken(":", {
  844. beforeExpr
  845. }),
  846. doubleColon: createToken("::", {
  847. beforeExpr
  848. }),
  849. dot: createToken("."),
  850. question: createToken("?", {
  851. beforeExpr
  852. }),
  853. questionDot: createToken("?."),
  854. arrow: createToken("=>", {
  855. beforeExpr
  856. }),
  857. template: createToken("template"),
  858. ellipsis: createToken("...", {
  859. beforeExpr
  860. }),
  861. backQuote: createToken("`", {
  862. startsExpr
  863. }),
  864. dollarBraceL: createToken("${", {
  865. beforeExpr,
  866. startsExpr
  867. }),
  868. templateTail: createToken("...`", {
  869. startsExpr
  870. }),
  871. templateNonTail: createToken("...${", {
  872. beforeExpr,
  873. startsExpr
  874. }),
  875. at: createToken("@"),
  876. hash: createToken("#", {
  877. startsExpr
  878. }),
  879. interpreterDirective: createToken("#!..."),
  880. eq: createToken("=", {
  881. beforeExpr,
  882. isAssign
  883. }),
  884. assign: createToken("_=", {
  885. beforeExpr,
  886. isAssign
  887. }),
  888. slashAssign: createToken("_=", {
  889. beforeExpr,
  890. isAssign
  891. }),
  892. xorAssign: createToken("_=", {
  893. beforeExpr,
  894. isAssign
  895. }),
  896. moduloAssign: createToken("_=", {
  897. beforeExpr,
  898. isAssign
  899. }),
  900. incDec: createToken("++/--", {
  901. prefix,
  902. postfix,
  903. startsExpr
  904. }),
  905. bang: createToken("!", {
  906. beforeExpr,
  907. prefix,
  908. startsExpr
  909. }),
  910. tilde: createToken("~", {
  911. beforeExpr,
  912. prefix,
  913. startsExpr
  914. }),
  915. doubleCaret: createToken("^^", {
  916. startsExpr
  917. }),
  918. doubleAt: createToken("@@", {
  919. startsExpr
  920. }),
  921. pipeline: createBinop("|>", 0),
  922. nullishCoalescing: createBinop("??", 1),
  923. logicalOR: createBinop("||", 1),
  924. logicalAND: createBinop("&&", 2),
  925. bitwiseOR: createBinop("|", 3),
  926. bitwiseXOR: createBinop("^", 4),
  927. bitwiseAND: createBinop("&", 5),
  928. equality: createBinop("==/!=/===/!==", 6),
  929. lt: createBinop("</>/<=/>=", 7),
  930. gt: createBinop("</>/<=/>=", 7),
  931. relational: createBinop("</>/<=/>=", 7),
  932. bitShift: createBinop("<</>>/>>>", 8),
  933. bitShiftL: createBinop("<</>>/>>>", 8),
  934. bitShiftR: createBinop("<</>>/>>>", 8),
  935. plusMin: createToken("+/-", {
  936. beforeExpr,
  937. binop: 9,
  938. prefix,
  939. startsExpr
  940. }),
  941. modulo: createToken("%", {
  942. binop: 10,
  943. startsExpr
  944. }),
  945. star: createToken("*", {
  946. binop: 10
  947. }),
  948. slash: createBinop("/", 10),
  949. exponent: createToken("**", {
  950. beforeExpr,
  951. binop: 11,
  952. rightAssociative: true
  953. }),
  954. _in: createKeyword("in", {
  955. beforeExpr,
  956. binop: 7
  957. }),
  958. _instanceof: createKeyword("instanceof", {
  959. beforeExpr,
  960. binop: 7
  961. }),
  962. _break: createKeyword("break"),
  963. _case: createKeyword("case", {
  964. beforeExpr
  965. }),
  966. _catch: createKeyword("catch"),
  967. _continue: createKeyword("continue"),
  968. _debugger: createKeyword("debugger"),
  969. _default: createKeyword("default", {
  970. beforeExpr
  971. }),
  972. _else: createKeyword("else", {
  973. beforeExpr
  974. }),
  975. _finally: createKeyword("finally"),
  976. _function: createKeyword("function", {
  977. startsExpr
  978. }),
  979. _if: createKeyword("if"),
  980. _return: createKeyword("return", {
  981. beforeExpr
  982. }),
  983. _switch: createKeyword("switch"),
  984. _throw: createKeyword("throw", {
  985. beforeExpr,
  986. prefix,
  987. startsExpr
  988. }),
  989. _try: createKeyword("try"),
  990. _var: createKeyword("var"),
  991. _const: createKeyword("const"),
  992. _with: createKeyword("with"),
  993. _new: createKeyword("new", {
  994. beforeExpr,
  995. startsExpr
  996. }),
  997. _this: createKeyword("this", {
  998. startsExpr
  999. }),
  1000. _super: createKeyword("super", {
  1001. startsExpr
  1002. }),
  1003. _class: createKeyword("class", {
  1004. startsExpr
  1005. }),
  1006. _extends: createKeyword("extends", {
  1007. beforeExpr
  1008. }),
  1009. _export: createKeyword("export"),
  1010. _import: createKeyword("import", {
  1011. startsExpr
  1012. }),
  1013. _null: createKeyword("null", {
  1014. startsExpr
  1015. }),
  1016. _true: createKeyword("true", {
  1017. startsExpr
  1018. }),
  1019. _false: createKeyword("false", {
  1020. startsExpr
  1021. }),
  1022. _typeof: createKeyword("typeof", {
  1023. beforeExpr,
  1024. prefix,
  1025. startsExpr
  1026. }),
  1027. _void: createKeyword("void", {
  1028. beforeExpr,
  1029. prefix,
  1030. startsExpr
  1031. }),
  1032. _delete: createKeyword("delete", {
  1033. beforeExpr,
  1034. prefix,
  1035. startsExpr
  1036. }),
  1037. _do: createKeyword("do", {
  1038. isLoop,
  1039. beforeExpr
  1040. }),
  1041. _for: createKeyword("for", {
  1042. isLoop
  1043. }),
  1044. _while: createKeyword("while", {
  1045. isLoop
  1046. }),
  1047. _as: createKeywordLike("as", {
  1048. startsExpr
  1049. }),
  1050. _assert: createKeywordLike("assert", {
  1051. startsExpr
  1052. }),
  1053. _async: createKeywordLike("async", {
  1054. startsExpr
  1055. }),
  1056. _await: createKeywordLike("await", {
  1057. startsExpr
  1058. }),
  1059. _from: createKeywordLike("from", {
  1060. startsExpr
  1061. }),
  1062. _get: createKeywordLike("get", {
  1063. startsExpr
  1064. }),
  1065. _let: createKeywordLike("let", {
  1066. startsExpr
  1067. }),
  1068. _meta: createKeywordLike("meta", {
  1069. startsExpr
  1070. }),
  1071. _of: createKeywordLike("of", {
  1072. startsExpr
  1073. }),
  1074. _sent: createKeywordLike("sent", {
  1075. startsExpr
  1076. }),
  1077. _set: createKeywordLike("set", {
  1078. startsExpr
  1079. }),
  1080. _static: createKeywordLike("static", {
  1081. startsExpr
  1082. }),
  1083. _yield: createKeywordLike("yield", {
  1084. startsExpr
  1085. }),
  1086. _asserts: createKeywordLike("asserts", {
  1087. startsExpr
  1088. }),
  1089. _checks: createKeywordLike("checks", {
  1090. startsExpr
  1091. }),
  1092. _exports: createKeywordLike("exports", {
  1093. startsExpr
  1094. }),
  1095. _global: createKeywordLike("global", {
  1096. startsExpr
  1097. }),
  1098. _implements: createKeywordLike("implements", {
  1099. startsExpr
  1100. }),
  1101. _intrinsic: createKeywordLike("intrinsic", {
  1102. startsExpr
  1103. }),
  1104. _infer: createKeywordLike("infer", {
  1105. startsExpr
  1106. }),
  1107. _is: createKeywordLike("is", {
  1108. startsExpr
  1109. }),
  1110. _mixins: createKeywordLike("mixins", {
  1111. startsExpr
  1112. }),
  1113. _proto: createKeywordLike("proto", {
  1114. startsExpr
  1115. }),
  1116. _require: createKeywordLike("require", {
  1117. startsExpr
  1118. }),
  1119. _keyof: createKeywordLike("keyof", {
  1120. startsExpr
  1121. }),
  1122. _readonly: createKeywordLike("readonly", {
  1123. startsExpr
  1124. }),
  1125. _unique: createKeywordLike("unique", {
  1126. startsExpr
  1127. }),
  1128. _abstract: createKeywordLike("abstract", {
  1129. startsExpr
  1130. }),
  1131. _declare: createKeywordLike("declare", {
  1132. startsExpr
  1133. }),
  1134. _enum: createKeywordLike("enum", {
  1135. startsExpr
  1136. }),
  1137. _module: createKeywordLike("module", {
  1138. startsExpr
  1139. }),
  1140. _namespace: createKeywordLike("namespace", {
  1141. startsExpr
  1142. }),
  1143. _interface: createKeywordLike("interface", {
  1144. startsExpr
  1145. }),
  1146. _type: createKeywordLike("type", {
  1147. startsExpr
  1148. }),
  1149. _opaque: createKeywordLike("opaque", {
  1150. startsExpr
  1151. }),
  1152. name: createToken("name", {
  1153. startsExpr
  1154. }),
  1155. string: createToken("string", {
  1156. startsExpr
  1157. }),
  1158. num: createToken("num", {
  1159. startsExpr
  1160. }),
  1161. bigint: createToken("bigint", {
  1162. startsExpr
  1163. }),
  1164. decimal: createToken("decimal", {
  1165. startsExpr
  1166. }),
  1167. regexp: createToken("regexp", {
  1168. startsExpr
  1169. }),
  1170. privateName: createToken("#name", {
  1171. startsExpr
  1172. }),
  1173. eof: createToken("eof"),
  1174. jsxName: createToken("jsxName"),
  1175. jsxText: createToken("jsxText", {
  1176. beforeExpr: true
  1177. }),
  1178. jsxTagStart: createToken("jsxTagStart", {
  1179. startsExpr: true
  1180. }),
  1181. jsxTagEnd: createToken("jsxTagEnd"),
  1182. placeholder: createToken("%%", {
  1183. startsExpr: true
  1184. })
  1185. };
  1186. function tokenIsIdentifier(token) {
  1187. return token >= 93 && token <= 128;
  1188. }
  1189. function tokenKeywordOrIdentifierIsKeyword(token) {
  1190. return token <= 92;
  1191. }
  1192. function tokenIsKeywordOrIdentifier(token) {
  1193. return token >= 58 && token <= 128;
  1194. }
  1195. function tokenIsLiteralPropertyName(token) {
  1196. return token >= 58 && token <= 132;
  1197. }
  1198. function tokenComesBeforeExpression(token) {
  1199. return tokenBeforeExprs[token];
  1200. }
  1201. function tokenCanStartExpression(token) {
  1202. return tokenStartsExprs[token];
  1203. }
  1204. function tokenIsAssignment(token) {
  1205. return token >= 29 && token <= 33;
  1206. }
  1207. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1208. return token >= 125 && token <= 127;
  1209. }
  1210. function tokenIsLoop(token) {
  1211. return token >= 90 && token <= 92;
  1212. }
  1213. function tokenIsKeyword(token) {
  1214. return token >= 58 && token <= 92;
  1215. }
  1216. function tokenIsOperator(token) {
  1217. return token >= 39 && token <= 59;
  1218. }
  1219. function tokenIsPostfix(token) {
  1220. return token === 34;
  1221. }
  1222. function tokenIsPrefix(token) {
  1223. return tokenPrefixes[token];
  1224. }
  1225. function tokenIsTSTypeOperator(token) {
  1226. return token >= 117 && token <= 119;
  1227. }
  1228. function tokenIsTSDeclarationStart(token) {
  1229. return token >= 120 && token <= 126;
  1230. }
  1231. function tokenLabelName(token) {
  1232. return tokenLabels[token];
  1233. }
  1234. function tokenOperatorPrecedence(token) {
  1235. return tokenBinops[token];
  1236. }
  1237. function tokenIsRightAssociative(token) {
  1238. return token === 57;
  1239. }
  1240. function tokenIsTemplate(token) {
  1241. return token >= 24 && token <= 25;
  1242. }
  1243. function getExportedToken(token) {
  1244. return tokenTypes[token];
  1245. }
  1246. {
  1247. tokenTypes[8].updateContext = context => {
  1248. context.pop();
  1249. };
  1250. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1251. context.push(types.brace);
  1252. };
  1253. tokenTypes[22].updateContext = context => {
  1254. if (context[context.length - 1] === types.template) {
  1255. context.pop();
  1256. } else {
  1257. context.push(types.template);
  1258. }
  1259. };
  1260. tokenTypes[138].updateContext = context => {
  1261. context.push(types.j_expr, types.j_oTag);
  1262. };
  1263. }
  1264. 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";
  1265. 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\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\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";
  1266. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1267. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1268. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1269. 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, 85, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 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, 190, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1070, 4050, 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, 46, 2, 18, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 482, 44, 11, 6, 17, 0, 322, 29, 19, 43, 1269, 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, 4152, 8, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938];
  1270. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 154, 10, 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, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 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, 262, 6, 10, 9, 357, 0, 62, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1271. function isInAstralSet(code, set) {
  1272. let pos = 0x10000;
  1273. for (let i = 0, length = set.length; i < length; i += 2) {
  1274. pos += set[i];
  1275. if (pos > code) return false;
  1276. pos += set[i + 1];
  1277. if (pos >= code) return true;
  1278. }
  1279. return false;
  1280. }
  1281. function isIdentifierStart(code) {
  1282. if (code < 65) return code === 36;
  1283. if (code <= 90) return true;
  1284. if (code < 97) return code === 95;
  1285. if (code <= 122) return true;
  1286. if (code <= 0xffff) {
  1287. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1288. }
  1289. return isInAstralSet(code, astralIdentifierStartCodes);
  1290. }
  1291. function isIdentifierChar(code) {
  1292. if (code < 48) return code === 36;
  1293. if (code < 58) return true;
  1294. if (code < 65) return false;
  1295. if (code <= 90) return true;
  1296. if (code < 97) return code === 95;
  1297. if (code <= 122) return true;
  1298. if (code <= 0xffff) {
  1299. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1300. }
  1301. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1302. }
  1303. const reservedWords = {
  1304. 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"],
  1305. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1306. strictBind: ["eval", "arguments"]
  1307. };
  1308. const keywords = new Set(reservedWords.keyword);
  1309. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1310. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1311. function isReservedWord(word, inModule) {
  1312. return inModule && word === "await" || word === "enum";
  1313. }
  1314. function isStrictReservedWord(word, inModule) {
  1315. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1316. }
  1317. function isStrictBindOnlyReservedWord(word) {
  1318. return reservedWordsStrictBindSet.has(word);
  1319. }
  1320. function isStrictBindReservedWord(word, inModule) {
  1321. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1322. }
  1323. function isKeyword(word) {
  1324. return keywords.has(word);
  1325. }
  1326. function isIteratorStart(current, next, next2) {
  1327. return current === 64 && next === 64 && isIdentifierStart(next2);
  1328. }
  1329. 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"]);
  1330. function canBeReservedWord(word) {
  1331. return reservedWordLikeSet.has(word);
  1332. }
  1333. const SCOPE_OTHER = 0b000000000,
  1334. SCOPE_PROGRAM = 0b000000001,
  1335. SCOPE_FUNCTION = 0b000000010,
  1336. SCOPE_ARROW = 0b000000100,
  1337. SCOPE_SIMPLE_CATCH = 0b000001000,
  1338. SCOPE_SUPER = 0b000010000,
  1339. SCOPE_DIRECT_SUPER = 0b000100000,
  1340. SCOPE_CLASS = 0b001000000,
  1341. SCOPE_STATIC_BLOCK = 0b010000000,
  1342. SCOPE_TS_MODULE = 0b100000000,
  1343. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1344. const BIND_KIND_VALUE = 0b000000000001,
  1345. BIND_KIND_TYPE = 0b000000000010,
  1346. BIND_SCOPE_VAR = 0b000000000100,
  1347. BIND_SCOPE_LEXICAL = 0b000000001000,
  1348. BIND_SCOPE_FUNCTION = 0b000000010000,
  1349. BIND_FLAGS_NONE = 0b000001000000,
  1350. BIND_FLAGS_CLASS = 0b000010000000,
  1351. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1352. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1353. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1354. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1355. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1356. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1357. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1358. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1359. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1360. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1361. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1362. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1363. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1364. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1365. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1366. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1367. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1368. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1369. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1370. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1371. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1372. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1373. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1374. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1375. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1376. CLASS_ELEMENT_OTHER = 0;
  1377. class BaseParser {
  1378. constructor() {
  1379. this.sawUnambiguousESM = false;
  1380. this.ambiguousScriptDifferentAst = false;
  1381. }
  1382. hasPlugin(pluginConfig) {
  1383. if (typeof pluginConfig === "string") {
  1384. return this.plugins.has(pluginConfig);
  1385. } else {
  1386. const [pluginName, pluginOptions] = pluginConfig;
  1387. if (!this.hasPlugin(pluginName)) {
  1388. return false;
  1389. }
  1390. const actualOptions = this.plugins.get(pluginName);
  1391. for (const key of Object.keys(pluginOptions)) {
  1392. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1393. return false;
  1394. }
  1395. }
  1396. return true;
  1397. }
  1398. }
  1399. getPluginOption(plugin, name) {
  1400. var _this$plugins$get;
  1401. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1402. }
  1403. }
  1404. function setTrailingComments(node, comments) {
  1405. if (node.trailingComments === undefined) {
  1406. node.trailingComments = comments;
  1407. } else {
  1408. node.trailingComments.unshift(...comments);
  1409. }
  1410. }
  1411. function setLeadingComments(node, comments) {
  1412. if (node.leadingComments === undefined) {
  1413. node.leadingComments = comments;
  1414. } else {
  1415. node.leadingComments.unshift(...comments);
  1416. }
  1417. }
  1418. function setInnerComments(node, comments) {
  1419. if (node.innerComments === undefined) {
  1420. node.innerComments = comments;
  1421. } else {
  1422. node.innerComments.unshift(...comments);
  1423. }
  1424. }
  1425. function adjustInnerComments(node, elements, commentWS) {
  1426. let lastElement = null;
  1427. let i = elements.length;
  1428. while (lastElement === null && i > 0) {
  1429. lastElement = elements[--i];
  1430. }
  1431. if (lastElement === null || lastElement.start > commentWS.start) {
  1432. setInnerComments(node, commentWS.comments);
  1433. } else {
  1434. setTrailingComments(lastElement, commentWS.comments);
  1435. }
  1436. }
  1437. class CommentsParser extends BaseParser {
  1438. addComment(comment) {
  1439. if (this.filename) comment.loc.filename = this.filename;
  1440. this.state.comments.push(comment);
  1441. }
  1442. processComment(node) {
  1443. const {
  1444. commentStack
  1445. } = this.state;
  1446. const commentStackLength = commentStack.length;
  1447. if (commentStackLength === 0) return;
  1448. let i = commentStackLength - 1;
  1449. const lastCommentWS = commentStack[i];
  1450. if (lastCommentWS.start === node.end) {
  1451. lastCommentWS.leadingNode = node;
  1452. i--;
  1453. }
  1454. const {
  1455. start: nodeStart
  1456. } = node;
  1457. for (; i >= 0; i--) {
  1458. const commentWS = commentStack[i];
  1459. const commentEnd = commentWS.end;
  1460. if (commentEnd > nodeStart) {
  1461. commentWS.containingNode = node;
  1462. this.finalizeComment(commentWS);
  1463. commentStack.splice(i, 1);
  1464. } else {
  1465. if (commentEnd === nodeStart) {
  1466. commentWS.trailingNode = node;
  1467. }
  1468. break;
  1469. }
  1470. }
  1471. }
  1472. finalizeComment(commentWS) {
  1473. const {
  1474. comments
  1475. } = commentWS;
  1476. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1477. if (commentWS.leadingNode !== null) {
  1478. setTrailingComments(commentWS.leadingNode, comments);
  1479. }
  1480. if (commentWS.trailingNode !== null) {
  1481. setLeadingComments(commentWS.trailingNode, comments);
  1482. }
  1483. } else {
  1484. const {
  1485. containingNode: node,
  1486. start: commentStart
  1487. } = commentWS;
  1488. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1489. switch (node.type) {
  1490. case "ObjectExpression":
  1491. case "ObjectPattern":
  1492. case "RecordExpression":
  1493. adjustInnerComments(node, node.properties, commentWS);
  1494. break;
  1495. case "CallExpression":
  1496. case "OptionalCallExpression":
  1497. adjustInnerComments(node, node.arguments, commentWS);
  1498. break;
  1499. case "FunctionDeclaration":
  1500. case "FunctionExpression":
  1501. case "ArrowFunctionExpression":
  1502. case "ObjectMethod":
  1503. case "ClassMethod":
  1504. case "ClassPrivateMethod":
  1505. adjustInnerComments(node, node.params, commentWS);
  1506. break;
  1507. case "ArrayExpression":
  1508. case "ArrayPattern":
  1509. case "TupleExpression":
  1510. adjustInnerComments(node, node.elements, commentWS);
  1511. break;
  1512. case "ExportNamedDeclaration":
  1513. case "ImportDeclaration":
  1514. adjustInnerComments(node, node.specifiers, commentWS);
  1515. break;
  1516. default:
  1517. {
  1518. setInnerComments(node, comments);
  1519. }
  1520. }
  1521. } else {
  1522. setInnerComments(node, comments);
  1523. }
  1524. }
  1525. }
  1526. finalizeRemainingComments() {
  1527. const {
  1528. commentStack
  1529. } = this.state;
  1530. for (let i = commentStack.length - 1; i >= 0; i--) {
  1531. this.finalizeComment(commentStack[i]);
  1532. }
  1533. this.state.commentStack = [];
  1534. }
  1535. resetPreviousNodeTrailingComments(node) {
  1536. const {
  1537. commentStack
  1538. } = this.state;
  1539. const {
  1540. length
  1541. } = commentStack;
  1542. if (length === 0) return;
  1543. const commentWS = commentStack[length - 1];
  1544. if (commentWS.leadingNode === node) {
  1545. commentWS.leadingNode = null;
  1546. }
  1547. }
  1548. takeSurroundingComments(node, start, end) {
  1549. const {
  1550. commentStack
  1551. } = this.state;
  1552. const commentStackLength = commentStack.length;
  1553. if (commentStackLength === 0) return;
  1554. let i = commentStackLength - 1;
  1555. for (; i >= 0; i--) {
  1556. const commentWS = commentStack[i];
  1557. const commentEnd = commentWS.end;
  1558. const commentStart = commentWS.start;
  1559. if (commentStart === end) {
  1560. commentWS.leadingNode = node;
  1561. } else if (commentEnd === start) {
  1562. commentWS.trailingNode = node;
  1563. } else if (commentEnd < start) {
  1564. break;
  1565. }
  1566. }
  1567. }
  1568. }
  1569. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1570. const lineBreakG = new RegExp(lineBreak.source, "g");
  1571. function isNewLine(code) {
  1572. switch (code) {
  1573. case 10:
  1574. case 13:
  1575. case 8232:
  1576. case 8233:
  1577. return true;
  1578. default:
  1579. return false;
  1580. }
  1581. }
  1582. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1583. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1584. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1585. function isWhitespace(code) {
  1586. switch (code) {
  1587. case 0x0009:
  1588. case 0x000b:
  1589. case 0x000c:
  1590. case 32:
  1591. case 160:
  1592. case 5760:
  1593. case 0x2000:
  1594. case 0x2001:
  1595. case 0x2002:
  1596. case 0x2003:
  1597. case 0x2004:
  1598. case 0x2005:
  1599. case 0x2006:
  1600. case 0x2007:
  1601. case 0x2008:
  1602. case 0x2009:
  1603. case 0x200a:
  1604. case 0x202f:
  1605. case 0x205f:
  1606. case 0x3000:
  1607. case 0xfeff:
  1608. return true;
  1609. default:
  1610. return false;
  1611. }
  1612. }
  1613. class State {
  1614. constructor() {
  1615. this.strict = void 0;
  1616. this.curLine = void 0;
  1617. this.lineStart = void 0;
  1618. this.startLoc = void 0;
  1619. this.endLoc = void 0;
  1620. this.errors = [];
  1621. this.potentialArrowAt = -1;
  1622. this.noArrowAt = [];
  1623. this.noArrowParamsConversionAt = [];
  1624. this.maybeInArrowParameters = false;
  1625. this.inType = false;
  1626. this.noAnonFunctionType = false;
  1627. this.hasFlowComment = false;
  1628. this.isAmbientContext = false;
  1629. this.inAbstractClass = false;
  1630. this.topicContext = {
  1631. maxNumOfResolvableTopics: 0,
  1632. maxTopicIndex: null
  1633. };
  1634. this.soloAwait = false;
  1635. this.inFSharpPipelineDirectBody = false;
  1636. this.labels = [];
  1637. this.decoratorStack = [[]];
  1638. this.comments = [];
  1639. this.commentStack = [];
  1640. this.pos = 0;
  1641. this.type = 135;
  1642. this.value = null;
  1643. this.start = 0;
  1644. this.end = 0;
  1645. this.lastTokEndLoc = null;
  1646. this.lastTokStartLoc = null;
  1647. this.lastTokStart = 0;
  1648. this.context = [types.brace];
  1649. this.canStartJSXElement = true;
  1650. this.containsEsc = false;
  1651. this.strictErrors = new Map();
  1652. this.tokensLength = 0;
  1653. }
  1654. init({
  1655. strictMode,
  1656. sourceType,
  1657. startLine,
  1658. startColumn
  1659. }) {
  1660. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1661. this.curLine = startLine;
  1662. this.lineStart = -startColumn;
  1663. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1664. }
  1665. curPosition() {
  1666. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1667. }
  1668. clone(skipArrays) {
  1669. const state = new State();
  1670. const keys = Object.keys(this);
  1671. for (let i = 0, length = keys.length; i < length; i++) {
  1672. const key = keys[i];
  1673. let val = this[key];
  1674. if (!skipArrays && Array.isArray(val)) {
  1675. val = val.slice();
  1676. }
  1677. state[key] = val;
  1678. }
  1679. return state;
  1680. }
  1681. }
  1682. const _excluded = ["at"],
  1683. _excluded2 = ["at"];
  1684. var _isDigit = function isDigit(code) {
  1685. return code >= 48 && code <= 57;
  1686. };
  1687. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  1688. const forbiddenNumericSeparatorSiblings = {
  1689. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1690. hex: new Set([46, 88, 95, 120])
  1691. };
  1692. const isAllowedNumericSeparatorSibling = {
  1693. bin: ch => ch === 48 || ch === 49,
  1694. oct: ch => ch >= 48 && ch <= 55,
  1695. dec: ch => ch >= 48 && ch <= 57,
  1696. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1697. };
  1698. class Token {
  1699. constructor(state) {
  1700. this.type = state.type;
  1701. this.value = state.value;
  1702. this.start = state.start;
  1703. this.end = state.end;
  1704. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  1705. }
  1706. }
  1707. class Tokenizer extends CommentsParser {
  1708. constructor(options, input) {
  1709. super();
  1710. this.isLookahead = void 0;
  1711. this.tokens = [];
  1712. this.state = new State();
  1713. this.state.init(options);
  1714. this.input = input;
  1715. this.length = input.length;
  1716. this.isLookahead = false;
  1717. }
  1718. pushToken(token) {
  1719. this.tokens.length = this.state.tokensLength;
  1720. this.tokens.push(token);
  1721. ++this.state.tokensLength;
  1722. }
  1723. next() {
  1724. this.checkKeywordEscapes();
  1725. if (this.options.tokens) {
  1726. this.pushToken(new Token(this.state));
  1727. }
  1728. this.state.lastTokStart = this.state.start;
  1729. this.state.lastTokEndLoc = this.state.endLoc;
  1730. this.state.lastTokStartLoc = this.state.startLoc;
  1731. this.nextToken();
  1732. }
  1733. eat(type) {
  1734. if (this.match(type)) {
  1735. this.next();
  1736. return true;
  1737. } else {
  1738. return false;
  1739. }
  1740. }
  1741. match(type) {
  1742. return this.state.type === type;
  1743. }
  1744. createLookaheadState(state) {
  1745. return {
  1746. pos: state.pos,
  1747. value: null,
  1748. type: state.type,
  1749. start: state.start,
  1750. end: state.end,
  1751. context: [this.curContext()],
  1752. inType: state.inType,
  1753. startLoc: state.startLoc,
  1754. lastTokEndLoc: state.lastTokEndLoc,
  1755. curLine: state.curLine,
  1756. lineStart: state.lineStart,
  1757. curPosition: state.curPosition
  1758. };
  1759. }
  1760. lookahead() {
  1761. const old = this.state;
  1762. this.state = this.createLookaheadState(old);
  1763. this.isLookahead = true;
  1764. this.nextToken();
  1765. this.isLookahead = false;
  1766. const curr = this.state;
  1767. this.state = old;
  1768. return curr;
  1769. }
  1770. nextTokenStart() {
  1771. return this.nextTokenStartSince(this.state.pos);
  1772. }
  1773. nextTokenStartSince(pos) {
  1774. skipWhiteSpace.lastIndex = pos;
  1775. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  1776. }
  1777. lookaheadCharCode() {
  1778. return this.input.charCodeAt(this.nextTokenStart());
  1779. }
  1780. codePointAtPos(pos) {
  1781. let cp = this.input.charCodeAt(pos);
  1782. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  1783. const trail = this.input.charCodeAt(pos);
  1784. if ((trail & 0xfc00) === 0xdc00) {
  1785. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  1786. }
  1787. }
  1788. return cp;
  1789. }
  1790. setStrict(strict) {
  1791. this.state.strict = strict;
  1792. if (strict) {
  1793. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  1794. at
  1795. }));
  1796. this.state.strictErrors.clear();
  1797. }
  1798. }
  1799. curContext() {
  1800. return this.state.context[this.state.context.length - 1];
  1801. }
  1802. nextToken() {
  1803. this.skipSpace();
  1804. this.state.start = this.state.pos;
  1805. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  1806. if (this.state.pos >= this.length) {
  1807. this.finishToken(135);
  1808. return;
  1809. }
  1810. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  1811. }
  1812. skipBlockComment() {
  1813. let startLoc;
  1814. if (!this.isLookahead) startLoc = this.state.curPosition();
  1815. const start = this.state.pos;
  1816. const end = this.input.indexOf("*/", start + 2);
  1817. if (end === -1) {
  1818. throw this.raise(Errors.UnterminatedComment, {
  1819. at: this.state.curPosition()
  1820. });
  1821. }
  1822. this.state.pos = end + 2;
  1823. lineBreakG.lastIndex = start + 2;
  1824. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  1825. ++this.state.curLine;
  1826. this.state.lineStart = lineBreakG.lastIndex;
  1827. }
  1828. if (this.isLookahead) return;
  1829. const comment = {
  1830. type: "CommentBlock",
  1831. value: this.input.slice(start + 2, end),
  1832. start,
  1833. end: end + 2,
  1834. loc: new SourceLocation(startLoc, this.state.curPosition())
  1835. };
  1836. if (this.options.tokens) this.pushToken(comment);
  1837. return comment;
  1838. }
  1839. skipLineComment(startSkip) {
  1840. const start = this.state.pos;
  1841. let startLoc;
  1842. if (!this.isLookahead) startLoc = this.state.curPosition();
  1843. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  1844. if (this.state.pos < this.length) {
  1845. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  1846. ch = this.input.charCodeAt(this.state.pos);
  1847. }
  1848. }
  1849. if (this.isLookahead) return;
  1850. const end = this.state.pos;
  1851. const value = this.input.slice(start + startSkip, end);
  1852. const comment = {
  1853. type: "CommentLine",
  1854. value,
  1855. start,
  1856. end,
  1857. loc: new SourceLocation(startLoc, this.state.curPosition())
  1858. };
  1859. if (this.options.tokens) this.pushToken(comment);
  1860. return comment;
  1861. }
  1862. skipSpace() {
  1863. const spaceStart = this.state.pos;
  1864. const comments = [];
  1865. loop: while (this.state.pos < this.length) {
  1866. const ch = this.input.charCodeAt(this.state.pos);
  1867. switch (ch) {
  1868. case 32:
  1869. case 160:
  1870. case 9:
  1871. ++this.state.pos;
  1872. break;
  1873. case 13:
  1874. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  1875. ++this.state.pos;
  1876. }
  1877. case 10:
  1878. case 8232:
  1879. case 8233:
  1880. ++this.state.pos;
  1881. ++this.state.curLine;
  1882. this.state.lineStart = this.state.pos;
  1883. break;
  1884. case 47:
  1885. switch (this.input.charCodeAt(this.state.pos + 1)) {
  1886. case 42:
  1887. {
  1888. const comment = this.skipBlockComment();
  1889. if (comment !== undefined) {
  1890. this.addComment(comment);
  1891. if (this.options.attachComment) comments.push(comment);
  1892. }
  1893. break;
  1894. }
  1895. case 47:
  1896. {
  1897. const comment = this.skipLineComment(2);
  1898. if (comment !== undefined) {
  1899. this.addComment(comment);
  1900. if (this.options.attachComment) comments.push(comment);
  1901. }
  1902. break;
  1903. }
  1904. default:
  1905. break loop;
  1906. }
  1907. break;
  1908. default:
  1909. if (isWhitespace(ch)) {
  1910. ++this.state.pos;
  1911. } else if (ch === 45 && !this.inModule) {
  1912. const pos = this.state.pos;
  1913. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  1914. const comment = this.skipLineComment(3);
  1915. if (comment !== undefined) {
  1916. this.addComment(comment);
  1917. if (this.options.attachComment) comments.push(comment);
  1918. }
  1919. } else {
  1920. break loop;
  1921. }
  1922. } else if (ch === 60 && !this.inModule) {
  1923. const pos = this.state.pos;
  1924. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  1925. const comment = this.skipLineComment(4);
  1926. if (comment !== undefined) {
  1927. this.addComment(comment);
  1928. if (this.options.attachComment) comments.push(comment);
  1929. }
  1930. } else {
  1931. break loop;
  1932. }
  1933. } else {
  1934. break loop;
  1935. }
  1936. }
  1937. }
  1938. if (comments.length > 0) {
  1939. const end = this.state.pos;
  1940. const CommentWhitespace = {
  1941. start: spaceStart,
  1942. end,
  1943. comments,
  1944. leadingNode: null,
  1945. trailingNode: null,
  1946. containingNode: null
  1947. };
  1948. this.state.commentStack.push(CommentWhitespace);
  1949. }
  1950. }
  1951. finishToken(type, val) {
  1952. this.state.end = this.state.pos;
  1953. this.state.endLoc = this.state.curPosition();
  1954. const prevType = this.state.type;
  1955. this.state.type = type;
  1956. this.state.value = val;
  1957. if (!this.isLookahead) {
  1958. this.updateContext(prevType);
  1959. }
  1960. }
  1961. replaceToken(type) {
  1962. this.state.type = type;
  1963. this.updateContext();
  1964. }
  1965. readToken_numberSign() {
  1966. if (this.state.pos === 0 && this.readToken_interpreter()) {
  1967. return;
  1968. }
  1969. const nextPos = this.state.pos + 1;
  1970. const next = this.codePointAtPos(nextPos);
  1971. if (next >= 48 && next <= 57) {
  1972. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  1973. at: this.state.curPosition()
  1974. });
  1975. }
  1976. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  1977. this.expectPlugin("recordAndTuple");
  1978. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  1979. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  1980. at: this.state.curPosition()
  1981. });
  1982. }
  1983. this.state.pos += 2;
  1984. if (next === 123) {
  1985. this.finishToken(7);
  1986. } else {
  1987. this.finishToken(1);
  1988. }
  1989. } else if (isIdentifierStart(next)) {
  1990. ++this.state.pos;
  1991. this.finishToken(134, this.readWord1(next));
  1992. } else if (next === 92) {
  1993. ++this.state.pos;
  1994. this.finishToken(134, this.readWord1());
  1995. } else {
  1996. this.finishOp(27, 1);
  1997. }
  1998. }
  1999. readToken_dot() {
  2000. const next = this.input.charCodeAt(this.state.pos + 1);
  2001. if (next >= 48 && next <= 57) {
  2002. this.readNumber(true);
  2003. return;
  2004. }
  2005. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2006. this.state.pos += 3;
  2007. this.finishToken(21);
  2008. } else {
  2009. ++this.state.pos;
  2010. this.finishToken(16);
  2011. }
  2012. }
  2013. readToken_slash() {
  2014. const next = this.input.charCodeAt(this.state.pos + 1);
  2015. if (next === 61) {
  2016. this.finishOp(31, 2);
  2017. } else {
  2018. this.finishOp(56, 1);
  2019. }
  2020. }
  2021. readToken_interpreter() {
  2022. if (this.state.pos !== 0 || this.length < 2) return false;
  2023. let ch = this.input.charCodeAt(this.state.pos + 1);
  2024. if (ch !== 33) return false;
  2025. const start = this.state.pos;
  2026. this.state.pos += 1;
  2027. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2028. ch = this.input.charCodeAt(this.state.pos);
  2029. }
  2030. const value = this.input.slice(start + 2, this.state.pos);
  2031. this.finishToken(28, value);
  2032. return true;
  2033. }
  2034. readToken_mult_modulo(code) {
  2035. let type = code === 42 ? 55 : 54;
  2036. let width = 1;
  2037. let next = this.input.charCodeAt(this.state.pos + 1);
  2038. if (code === 42 && next === 42) {
  2039. width++;
  2040. next = this.input.charCodeAt(this.state.pos + 2);
  2041. type = 57;
  2042. }
  2043. if (next === 61 && !this.state.inType) {
  2044. width++;
  2045. type = code === 37 ? 33 : 30;
  2046. }
  2047. this.finishOp(type, width);
  2048. }
  2049. readToken_pipe_amp(code) {
  2050. const next = this.input.charCodeAt(this.state.pos + 1);
  2051. if (next === code) {
  2052. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2053. this.finishOp(30, 3);
  2054. } else {
  2055. this.finishOp(code === 124 ? 41 : 42, 2);
  2056. }
  2057. return;
  2058. }
  2059. if (code === 124) {
  2060. if (next === 62) {
  2061. this.finishOp(39, 2);
  2062. return;
  2063. }
  2064. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2065. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2066. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2067. at: this.state.curPosition()
  2068. });
  2069. }
  2070. this.state.pos += 2;
  2071. this.finishToken(9);
  2072. return;
  2073. }
  2074. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2075. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2076. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2077. at: this.state.curPosition()
  2078. });
  2079. }
  2080. this.state.pos += 2;
  2081. this.finishToken(4);
  2082. return;
  2083. }
  2084. }
  2085. if (next === 61) {
  2086. this.finishOp(30, 2);
  2087. return;
  2088. }
  2089. this.finishOp(code === 124 ? 43 : 45, 1);
  2090. }
  2091. readToken_caret() {
  2092. const next = this.input.charCodeAt(this.state.pos + 1);
  2093. if (next === 61 && !this.state.inType) {
  2094. this.finishOp(32, 2);
  2095. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2096. proposal: "hack",
  2097. topicToken: "^^"
  2098. }])) {
  2099. this.finishOp(37, 2);
  2100. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2101. if (lookaheadCh === 94) {
  2102. throw this.unexpected();
  2103. }
  2104. } else {
  2105. this.finishOp(44, 1);
  2106. }
  2107. }
  2108. readToken_atSign() {
  2109. const next = this.input.charCodeAt(this.state.pos + 1);
  2110. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2111. proposal: "hack",
  2112. topicToken: "@@"
  2113. }])) {
  2114. this.finishOp(38, 2);
  2115. } else {
  2116. this.finishOp(26, 1);
  2117. }
  2118. }
  2119. readToken_plus_min(code) {
  2120. const next = this.input.charCodeAt(this.state.pos + 1);
  2121. if (next === code) {
  2122. this.finishOp(34, 2);
  2123. return;
  2124. }
  2125. if (next === 61) {
  2126. this.finishOp(30, 2);
  2127. } else {
  2128. this.finishOp(53, 1);
  2129. }
  2130. }
  2131. readToken_lt() {
  2132. const {
  2133. pos
  2134. } = this.state;
  2135. const next = this.input.charCodeAt(pos + 1);
  2136. if (next === 60) {
  2137. if (this.input.charCodeAt(pos + 2) === 61) {
  2138. this.finishOp(30, 3);
  2139. return;
  2140. }
  2141. this.finishOp(51, 2);
  2142. return;
  2143. }
  2144. if (next === 61) {
  2145. this.finishOp(49, 2);
  2146. return;
  2147. }
  2148. this.finishOp(47, 1);
  2149. }
  2150. readToken_gt() {
  2151. const {
  2152. pos
  2153. } = this.state;
  2154. const next = this.input.charCodeAt(pos + 1);
  2155. if (next === 62) {
  2156. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2157. if (this.input.charCodeAt(pos + size) === 61) {
  2158. this.finishOp(30, size + 1);
  2159. return;
  2160. }
  2161. this.finishOp(52, size);
  2162. return;
  2163. }
  2164. if (next === 61) {
  2165. this.finishOp(49, 2);
  2166. return;
  2167. }
  2168. this.finishOp(48, 1);
  2169. }
  2170. readToken_eq_excl(code) {
  2171. const next = this.input.charCodeAt(this.state.pos + 1);
  2172. if (next === 61) {
  2173. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2174. return;
  2175. }
  2176. if (code === 61 && next === 62) {
  2177. this.state.pos += 2;
  2178. this.finishToken(19);
  2179. return;
  2180. }
  2181. this.finishOp(code === 61 ? 29 : 35, 1);
  2182. }
  2183. readToken_question() {
  2184. const next = this.input.charCodeAt(this.state.pos + 1);
  2185. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2186. if (next === 63) {
  2187. if (next2 === 61) {
  2188. this.finishOp(30, 3);
  2189. } else {
  2190. this.finishOp(40, 2);
  2191. }
  2192. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2193. this.state.pos += 2;
  2194. this.finishToken(18);
  2195. } else {
  2196. ++this.state.pos;
  2197. this.finishToken(17);
  2198. }
  2199. }
  2200. getTokenFromCode(code) {
  2201. switch (code) {
  2202. case 46:
  2203. this.readToken_dot();
  2204. return;
  2205. case 40:
  2206. ++this.state.pos;
  2207. this.finishToken(10);
  2208. return;
  2209. case 41:
  2210. ++this.state.pos;
  2211. this.finishToken(11);
  2212. return;
  2213. case 59:
  2214. ++this.state.pos;
  2215. this.finishToken(13);
  2216. return;
  2217. case 44:
  2218. ++this.state.pos;
  2219. this.finishToken(12);
  2220. return;
  2221. case 91:
  2222. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2223. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2224. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2225. at: this.state.curPosition()
  2226. });
  2227. }
  2228. this.state.pos += 2;
  2229. this.finishToken(2);
  2230. } else {
  2231. ++this.state.pos;
  2232. this.finishToken(0);
  2233. }
  2234. return;
  2235. case 93:
  2236. ++this.state.pos;
  2237. this.finishToken(3);
  2238. return;
  2239. case 123:
  2240. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2241. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2242. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2243. at: this.state.curPosition()
  2244. });
  2245. }
  2246. this.state.pos += 2;
  2247. this.finishToken(6);
  2248. } else {
  2249. ++this.state.pos;
  2250. this.finishToken(5);
  2251. }
  2252. return;
  2253. case 125:
  2254. ++this.state.pos;
  2255. this.finishToken(8);
  2256. return;
  2257. case 58:
  2258. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2259. this.finishOp(15, 2);
  2260. } else {
  2261. ++this.state.pos;
  2262. this.finishToken(14);
  2263. }
  2264. return;
  2265. case 63:
  2266. this.readToken_question();
  2267. return;
  2268. case 96:
  2269. this.readTemplateToken();
  2270. return;
  2271. case 48:
  2272. {
  2273. const next = this.input.charCodeAt(this.state.pos + 1);
  2274. if (next === 120 || next === 88) {
  2275. this.readRadixNumber(16);
  2276. return;
  2277. }
  2278. if (next === 111 || next === 79) {
  2279. this.readRadixNumber(8);
  2280. return;
  2281. }
  2282. if (next === 98 || next === 66) {
  2283. this.readRadixNumber(2);
  2284. return;
  2285. }
  2286. }
  2287. case 49:
  2288. case 50:
  2289. case 51:
  2290. case 52:
  2291. case 53:
  2292. case 54:
  2293. case 55:
  2294. case 56:
  2295. case 57:
  2296. this.readNumber(false);
  2297. return;
  2298. case 34:
  2299. case 39:
  2300. this.readString(code);
  2301. return;
  2302. case 47:
  2303. this.readToken_slash();
  2304. return;
  2305. case 37:
  2306. case 42:
  2307. this.readToken_mult_modulo(code);
  2308. return;
  2309. case 124:
  2310. case 38:
  2311. this.readToken_pipe_amp(code);
  2312. return;
  2313. case 94:
  2314. this.readToken_caret();
  2315. return;
  2316. case 43:
  2317. case 45:
  2318. this.readToken_plus_min(code);
  2319. return;
  2320. case 60:
  2321. this.readToken_lt();
  2322. return;
  2323. case 62:
  2324. this.readToken_gt();
  2325. return;
  2326. case 61:
  2327. case 33:
  2328. this.readToken_eq_excl(code);
  2329. return;
  2330. case 126:
  2331. this.finishOp(36, 1);
  2332. return;
  2333. case 64:
  2334. this.readToken_atSign();
  2335. return;
  2336. case 35:
  2337. this.readToken_numberSign();
  2338. return;
  2339. case 92:
  2340. this.readWord();
  2341. return;
  2342. default:
  2343. if (isIdentifierStart(code)) {
  2344. this.readWord(code);
  2345. return;
  2346. }
  2347. }
  2348. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2349. at: this.state.curPosition(),
  2350. unexpected: String.fromCodePoint(code)
  2351. });
  2352. }
  2353. finishOp(type, size) {
  2354. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2355. this.state.pos += size;
  2356. this.finishToken(type, str);
  2357. }
  2358. readRegexp() {
  2359. const startLoc = this.state.startLoc;
  2360. const start = this.state.start + 1;
  2361. let escaped, inClass;
  2362. let {
  2363. pos
  2364. } = this.state;
  2365. for (;; ++pos) {
  2366. if (pos >= this.length) {
  2367. throw this.raise(Errors.UnterminatedRegExp, {
  2368. at: createPositionWithColumnOffset(startLoc, 1)
  2369. });
  2370. }
  2371. const ch = this.input.charCodeAt(pos);
  2372. if (isNewLine(ch)) {
  2373. throw this.raise(Errors.UnterminatedRegExp, {
  2374. at: createPositionWithColumnOffset(startLoc, 1)
  2375. });
  2376. }
  2377. if (escaped) {
  2378. escaped = false;
  2379. } else {
  2380. if (ch === 91) {
  2381. inClass = true;
  2382. } else if (ch === 93 && inClass) {
  2383. inClass = false;
  2384. } else if (ch === 47 && !inClass) {
  2385. break;
  2386. }
  2387. escaped = ch === 92;
  2388. }
  2389. }
  2390. const content = this.input.slice(start, pos);
  2391. ++pos;
  2392. let mods = "";
  2393. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2394. while (pos < this.length) {
  2395. const cp = this.codePointAtPos(pos);
  2396. const char = String.fromCharCode(cp);
  2397. if (VALID_REGEX_FLAGS.has(cp)) {
  2398. if (cp === 118) {
  2399. this.expectPlugin("regexpUnicodeSets", nextPos());
  2400. if (mods.includes("u")) {
  2401. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2402. at: nextPos()
  2403. });
  2404. }
  2405. } else if (cp === 117) {
  2406. if (mods.includes("v")) {
  2407. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2408. at: nextPos()
  2409. });
  2410. }
  2411. }
  2412. if (mods.includes(char)) {
  2413. this.raise(Errors.DuplicateRegExpFlags, {
  2414. at: nextPos()
  2415. });
  2416. }
  2417. } else if (isIdentifierChar(cp) || cp === 92) {
  2418. this.raise(Errors.MalformedRegExpFlags, {
  2419. at: nextPos()
  2420. });
  2421. } else {
  2422. break;
  2423. }
  2424. ++pos;
  2425. mods += char;
  2426. }
  2427. this.state.pos = pos;
  2428. this.finishToken(133, {
  2429. pattern: content,
  2430. flags: mods
  2431. });
  2432. }
  2433. readInt(radix, len, forceLen, allowNumSeparator = true) {
  2434. const start = this.state.pos;
  2435. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2436. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2437. let invalid = false;
  2438. let total = 0;
  2439. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2440. const code = this.input.charCodeAt(this.state.pos);
  2441. let val;
  2442. if (code === 95 && allowNumSeparator !== "bail") {
  2443. const prev = this.input.charCodeAt(this.state.pos - 1);
  2444. const next = this.input.charCodeAt(this.state.pos + 1);
  2445. if (!allowNumSeparator) {
  2446. this.raise(Errors.NumericSeparatorInEscapeSequence, {
  2447. at: this.state.curPosition()
  2448. });
  2449. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2450. this.raise(Errors.UnexpectedNumericSeparator, {
  2451. at: this.state.curPosition()
  2452. });
  2453. }
  2454. ++this.state.pos;
  2455. continue;
  2456. }
  2457. if (code >= 97) {
  2458. val = code - 97 + 10;
  2459. } else if (code >= 65) {
  2460. val = code - 65 + 10;
  2461. } else if (_isDigit(code)) {
  2462. val = code - 48;
  2463. } else {
  2464. val = Infinity;
  2465. }
  2466. if (val >= radix) {
  2467. if (this.options.errorRecovery && val <= 9) {
  2468. val = 0;
  2469. this.raise(Errors.InvalidDigit, {
  2470. at: this.state.curPosition(),
  2471. radix
  2472. });
  2473. } else if (forceLen) {
  2474. val = 0;
  2475. invalid = true;
  2476. } else {
  2477. break;
  2478. }
  2479. }
  2480. ++this.state.pos;
  2481. total = total * radix + val;
  2482. }
  2483. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  2484. return null;
  2485. }
  2486. return total;
  2487. }
  2488. readRadixNumber(radix) {
  2489. const startLoc = this.state.curPosition();
  2490. let isBigInt = false;
  2491. this.state.pos += 2;
  2492. const val = this.readInt(radix);
  2493. if (val == null) {
  2494. this.raise(Errors.InvalidDigit, {
  2495. at: createPositionWithColumnOffset(startLoc, 2),
  2496. radix
  2497. });
  2498. }
  2499. const next = this.input.charCodeAt(this.state.pos);
  2500. if (next === 110) {
  2501. ++this.state.pos;
  2502. isBigInt = true;
  2503. } else if (next === 109) {
  2504. throw this.raise(Errors.InvalidDecimal, {
  2505. at: startLoc
  2506. });
  2507. }
  2508. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2509. throw this.raise(Errors.NumberIdentifier, {
  2510. at: this.state.curPosition()
  2511. });
  2512. }
  2513. if (isBigInt) {
  2514. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  2515. this.finishToken(131, str);
  2516. return;
  2517. }
  2518. this.finishToken(130, val);
  2519. }
  2520. readNumber(startsWithDot) {
  2521. const start = this.state.pos;
  2522. const startLoc = this.state.curPosition();
  2523. let isFloat = false;
  2524. let isBigInt = false;
  2525. let isDecimal = false;
  2526. let hasExponent = false;
  2527. let isOctal = false;
  2528. if (!startsWithDot && this.readInt(10) === null) {
  2529. this.raise(Errors.InvalidNumber, {
  2530. at: this.state.curPosition()
  2531. });
  2532. }
  2533. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  2534. if (hasLeadingZero) {
  2535. const integer = this.input.slice(start, this.state.pos);
  2536. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  2537. at: startLoc
  2538. });
  2539. if (!this.state.strict) {
  2540. const underscorePos = integer.indexOf("_");
  2541. if (underscorePos > 0) {
  2542. this.raise(Errors.ZeroDigitNumericSeparator, {
  2543. at: createPositionWithColumnOffset(startLoc, underscorePos)
  2544. });
  2545. }
  2546. }
  2547. isOctal = hasLeadingZero && !/[89]/.test(integer);
  2548. }
  2549. let next = this.input.charCodeAt(this.state.pos);
  2550. if (next === 46 && !isOctal) {
  2551. ++this.state.pos;
  2552. this.readInt(10);
  2553. isFloat = true;
  2554. next = this.input.charCodeAt(this.state.pos);
  2555. }
  2556. if ((next === 69 || next === 101) && !isOctal) {
  2557. next = this.input.charCodeAt(++this.state.pos);
  2558. if (next === 43 || next === 45) {
  2559. ++this.state.pos;
  2560. }
  2561. if (this.readInt(10) === null) {
  2562. this.raise(Errors.InvalidOrMissingExponent, {
  2563. at: startLoc
  2564. });
  2565. }
  2566. isFloat = true;
  2567. hasExponent = true;
  2568. next = this.input.charCodeAt(this.state.pos);
  2569. }
  2570. if (next === 110) {
  2571. if (isFloat || hasLeadingZero) {
  2572. this.raise(Errors.InvalidBigIntLiteral, {
  2573. at: startLoc
  2574. });
  2575. }
  2576. ++this.state.pos;
  2577. isBigInt = true;
  2578. }
  2579. if (next === 109) {
  2580. this.expectPlugin("decimal", this.state.curPosition());
  2581. if (hasExponent || hasLeadingZero) {
  2582. this.raise(Errors.InvalidDecimal, {
  2583. at: startLoc
  2584. });
  2585. }
  2586. ++this.state.pos;
  2587. isDecimal = true;
  2588. }
  2589. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2590. throw this.raise(Errors.NumberIdentifier, {
  2591. at: this.state.curPosition()
  2592. });
  2593. }
  2594. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  2595. if (isBigInt) {
  2596. this.finishToken(131, str);
  2597. return;
  2598. }
  2599. if (isDecimal) {
  2600. this.finishToken(132, str);
  2601. return;
  2602. }
  2603. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  2604. this.finishToken(130, val);
  2605. }
  2606. readCodePoint(throwOnInvalid) {
  2607. const ch = this.input.charCodeAt(this.state.pos);
  2608. let code;
  2609. if (ch === 123) {
  2610. ++this.state.pos;
  2611. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  2612. ++this.state.pos;
  2613. if (code !== null && code > 0x10ffff) {
  2614. if (throwOnInvalid) {
  2615. this.raise(Errors.InvalidCodePoint, {
  2616. at: this.state.curPosition()
  2617. });
  2618. } else {
  2619. return null;
  2620. }
  2621. }
  2622. } else {
  2623. code = this.readHexChar(4, false, throwOnInvalid);
  2624. }
  2625. return code;
  2626. }
  2627. readString(quote) {
  2628. let out = "",
  2629. chunkStart = ++this.state.pos;
  2630. for (;;) {
  2631. if (this.state.pos >= this.length) {
  2632. throw this.raise(Errors.UnterminatedString, {
  2633. at: this.state.startLoc
  2634. });
  2635. }
  2636. const ch = this.input.charCodeAt(this.state.pos);
  2637. if (ch === quote) break;
  2638. if (ch === 92) {
  2639. out += this.input.slice(chunkStart, this.state.pos);
  2640. out += this.readEscapedChar(false);
  2641. chunkStart = this.state.pos;
  2642. } else if (ch === 8232 || ch === 8233) {
  2643. ++this.state.pos;
  2644. ++this.state.curLine;
  2645. this.state.lineStart = this.state.pos;
  2646. } else if (isNewLine(ch)) {
  2647. throw this.raise(Errors.UnterminatedString, {
  2648. at: this.state.startLoc
  2649. });
  2650. } else {
  2651. ++this.state.pos;
  2652. }
  2653. }
  2654. out += this.input.slice(chunkStart, this.state.pos++);
  2655. this.finishToken(129, out);
  2656. }
  2657. readTemplateContinuation() {
  2658. if (!this.match(8)) {
  2659. this.unexpected(null, 8);
  2660. }
  2661. this.state.pos--;
  2662. this.readTemplateToken();
  2663. }
  2664. readTemplateToken() {
  2665. let out = "",
  2666. chunkStart = this.state.pos,
  2667. containsInvalid = false;
  2668. ++this.state.pos;
  2669. for (;;) {
  2670. if (this.state.pos >= this.length) {
  2671. throw this.raise(Errors.UnterminatedTemplate, {
  2672. at: createPositionWithColumnOffset(this.state.startLoc, 1)
  2673. });
  2674. }
  2675. const ch = this.input.charCodeAt(this.state.pos);
  2676. if (ch === 96) {
  2677. ++this.state.pos;
  2678. out += this.input.slice(chunkStart, this.state.pos);
  2679. this.finishToken(24, containsInvalid ? null : out);
  2680. return;
  2681. }
  2682. if (ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  2683. this.state.pos += 2;
  2684. out += this.input.slice(chunkStart, this.state.pos);
  2685. this.finishToken(25, containsInvalid ? null : out);
  2686. return;
  2687. }
  2688. if (ch === 92) {
  2689. out += this.input.slice(chunkStart, this.state.pos);
  2690. const escaped = this.readEscapedChar(true);
  2691. if (escaped === null) {
  2692. containsInvalid = true;
  2693. } else {
  2694. out += escaped;
  2695. }
  2696. chunkStart = this.state.pos;
  2697. } else if (isNewLine(ch)) {
  2698. out += this.input.slice(chunkStart, this.state.pos);
  2699. ++this.state.pos;
  2700. switch (ch) {
  2701. case 13:
  2702. if (this.input.charCodeAt(this.state.pos) === 10) {
  2703. ++this.state.pos;
  2704. }
  2705. case 10:
  2706. out += "\n";
  2707. break;
  2708. default:
  2709. out += String.fromCharCode(ch);
  2710. break;
  2711. }
  2712. ++this.state.curLine;
  2713. this.state.lineStart = this.state.pos;
  2714. chunkStart = this.state.pos;
  2715. } else {
  2716. ++this.state.pos;
  2717. }
  2718. }
  2719. }
  2720. recordStrictModeErrors(toParseError, {
  2721. at
  2722. }) {
  2723. const index = at.index;
  2724. if (this.state.strict && !this.state.strictErrors.has(index)) {
  2725. this.raise(toParseError, {
  2726. at
  2727. });
  2728. } else {
  2729. this.state.strictErrors.set(index, [toParseError, at]);
  2730. }
  2731. }
  2732. readEscapedChar(inTemplate) {
  2733. const throwOnInvalid = !inTemplate;
  2734. const ch = this.input.charCodeAt(++this.state.pos);
  2735. ++this.state.pos;
  2736. switch (ch) {
  2737. case 110:
  2738. return "\n";
  2739. case 114:
  2740. return "\r";
  2741. case 120:
  2742. {
  2743. const code = this.readHexChar(2, false, throwOnInvalid);
  2744. return code === null ? null : String.fromCharCode(code);
  2745. }
  2746. case 117:
  2747. {
  2748. const code = this.readCodePoint(throwOnInvalid);
  2749. return code === null ? null : String.fromCodePoint(code);
  2750. }
  2751. case 116:
  2752. return "\t";
  2753. case 98:
  2754. return "\b";
  2755. case 118:
  2756. return "\u000b";
  2757. case 102:
  2758. return "\f";
  2759. case 13:
  2760. if (this.input.charCodeAt(this.state.pos) === 10) {
  2761. ++this.state.pos;
  2762. }
  2763. case 10:
  2764. this.state.lineStart = this.state.pos;
  2765. ++this.state.curLine;
  2766. case 8232:
  2767. case 8233:
  2768. return "";
  2769. case 56:
  2770. case 57:
  2771. if (inTemplate) {
  2772. return null;
  2773. } else {
  2774. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2775. at: createPositionWithColumnOffset(this.state.curPosition(), -1)
  2776. });
  2777. }
  2778. default:
  2779. if (ch >= 48 && ch <= 55) {
  2780. const codePos = createPositionWithColumnOffset(this.state.curPosition(), -1);
  2781. const match = this.input.slice(this.state.pos - 1, this.state.pos + 2).match(/^[0-7]+/);
  2782. let octalStr = match[0];
  2783. let octal = parseInt(octalStr, 8);
  2784. if (octal > 255) {
  2785. octalStr = octalStr.slice(0, -1);
  2786. octal = parseInt(octalStr, 8);
  2787. }
  2788. this.state.pos += octalStr.length - 1;
  2789. const next = this.input.charCodeAt(this.state.pos);
  2790. if (octalStr !== "0" || next === 56 || next === 57) {
  2791. if (inTemplate) {
  2792. return null;
  2793. } else {
  2794. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2795. at: codePos
  2796. });
  2797. }
  2798. }
  2799. return String.fromCharCode(octal);
  2800. }
  2801. return String.fromCharCode(ch);
  2802. }
  2803. }
  2804. readHexChar(len, forceLen, throwOnInvalid) {
  2805. const codeLoc = this.state.curPosition();
  2806. const n = this.readInt(16, len, forceLen, false);
  2807. if (n === null) {
  2808. if (throwOnInvalid) {
  2809. this.raise(Errors.InvalidEscapeSequence, {
  2810. at: codeLoc
  2811. });
  2812. } else {
  2813. this.state.pos = codeLoc.index - 1;
  2814. }
  2815. }
  2816. return n;
  2817. }
  2818. readWord1(firstCode) {
  2819. this.state.containsEsc = false;
  2820. let word = "";
  2821. const start = this.state.pos;
  2822. let chunkStart = this.state.pos;
  2823. if (firstCode !== undefined) {
  2824. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  2825. }
  2826. while (this.state.pos < this.length) {
  2827. const ch = this.codePointAtPos(this.state.pos);
  2828. if (isIdentifierChar(ch)) {
  2829. this.state.pos += ch <= 0xffff ? 1 : 2;
  2830. } else if (ch === 92) {
  2831. this.state.containsEsc = true;
  2832. word += this.input.slice(chunkStart, this.state.pos);
  2833. const escStart = this.state.curPosition();
  2834. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  2835. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  2836. this.raise(Errors.MissingUnicodeEscape, {
  2837. at: this.state.curPosition()
  2838. });
  2839. chunkStart = this.state.pos - 1;
  2840. continue;
  2841. }
  2842. ++this.state.pos;
  2843. const esc = this.readCodePoint(true);
  2844. if (esc !== null) {
  2845. if (!identifierCheck(esc)) {
  2846. this.raise(Errors.EscapedCharNotAnIdentifier, {
  2847. at: escStart
  2848. });
  2849. }
  2850. word += String.fromCodePoint(esc);
  2851. }
  2852. chunkStart = this.state.pos;
  2853. } else {
  2854. break;
  2855. }
  2856. }
  2857. return word + this.input.slice(chunkStart, this.state.pos);
  2858. }
  2859. readWord(firstCode) {
  2860. const word = this.readWord1(firstCode);
  2861. const type = keywords$1.get(word);
  2862. if (type !== undefined) {
  2863. this.finishToken(type, tokenLabelName(type));
  2864. } else {
  2865. this.finishToken(128, word);
  2866. }
  2867. }
  2868. checkKeywordEscapes() {
  2869. const {
  2870. type
  2871. } = this.state;
  2872. if (tokenIsKeyword(type) && this.state.containsEsc) {
  2873. this.raise(Errors.InvalidEscapedReservedWord, {
  2874. at: this.state.startLoc,
  2875. reservedWord: tokenLabelName(type)
  2876. });
  2877. }
  2878. }
  2879. raise(toParseError, raiseProperties) {
  2880. const {
  2881. at
  2882. } = raiseProperties,
  2883. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  2884. const loc = at instanceof Position ? at : at.loc.start;
  2885. const error = toParseError({
  2886. loc,
  2887. details
  2888. });
  2889. if (!this.options.errorRecovery) throw error;
  2890. if (!this.isLookahead) this.state.errors.push(error);
  2891. return error;
  2892. }
  2893. raiseOverwrite(toParseError, raiseProperties) {
  2894. const {
  2895. at
  2896. } = raiseProperties,
  2897. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  2898. const loc = at instanceof Position ? at : at.loc.start;
  2899. const pos = loc.index;
  2900. const errors = this.state.errors;
  2901. for (let i = errors.length - 1; i >= 0; i--) {
  2902. const error = errors[i];
  2903. if (error.loc.index === pos) {
  2904. return errors[i] = toParseError({
  2905. loc,
  2906. details
  2907. });
  2908. }
  2909. if (error.loc.index < pos) break;
  2910. }
  2911. return this.raise(toParseError, raiseProperties);
  2912. }
  2913. updateContext(prevType) {}
  2914. unexpected(loc, type) {
  2915. throw this.raise(Errors.UnexpectedToken, {
  2916. expected: type ? tokenLabelName(type) : null,
  2917. at: loc != null ? loc : this.state.startLoc
  2918. });
  2919. }
  2920. expectPlugin(pluginName, loc) {
  2921. if (this.hasPlugin(pluginName)) {
  2922. return true;
  2923. }
  2924. throw this.raise(Errors.MissingPlugin, {
  2925. at: loc != null ? loc : this.state.startLoc,
  2926. missingPlugin: [pluginName]
  2927. });
  2928. }
  2929. expectOnePlugin(pluginNames) {
  2930. if (!pluginNames.some(name => this.hasPlugin(name))) {
  2931. throw this.raise(Errors.MissingOneOfPlugins, {
  2932. at: this.state.startLoc,
  2933. missingPlugin: pluginNames
  2934. });
  2935. }
  2936. }
  2937. }
  2938. class Scope {
  2939. constructor(flags) {
  2940. this.var = new Set();
  2941. this.lexical = new Set();
  2942. this.functions = new Set();
  2943. this.flags = flags;
  2944. }
  2945. }
  2946. class ScopeHandler {
  2947. constructor(parser, inModule) {
  2948. this.parser = void 0;
  2949. this.scopeStack = [];
  2950. this.inModule = void 0;
  2951. this.undefinedExports = new Map();
  2952. this.parser = parser;
  2953. this.inModule = inModule;
  2954. }
  2955. get inFunction() {
  2956. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  2957. }
  2958. get allowSuper() {
  2959. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  2960. }
  2961. get allowDirectSuper() {
  2962. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  2963. }
  2964. get inClass() {
  2965. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  2966. }
  2967. get inClassAndNotInNonArrowFunction() {
  2968. const flags = this.currentThisScopeFlags();
  2969. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  2970. }
  2971. get inStaticBlock() {
  2972. for (let i = this.scopeStack.length - 1;; i--) {
  2973. const {
  2974. flags
  2975. } = this.scopeStack[i];
  2976. if (flags & SCOPE_STATIC_BLOCK) {
  2977. return true;
  2978. }
  2979. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  2980. return false;
  2981. }
  2982. }
  2983. }
  2984. get inNonArrowFunction() {
  2985. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  2986. }
  2987. get treatFunctionsAsVar() {
  2988. return this.treatFunctionsAsVarInScope(this.currentScope());
  2989. }
  2990. createScope(flags) {
  2991. return new Scope(flags);
  2992. }
  2993. enter(flags) {
  2994. this.scopeStack.push(this.createScope(flags));
  2995. }
  2996. exit() {
  2997. this.scopeStack.pop();
  2998. }
  2999. treatFunctionsAsVarInScope(scope) {
  3000. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  3001. }
  3002. declareName(name, bindingType, loc) {
  3003. let scope = this.currentScope();
  3004. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  3005. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  3006. if (bindingType & BIND_SCOPE_FUNCTION) {
  3007. scope.functions.add(name);
  3008. } else {
  3009. scope.lexical.add(name);
  3010. }
  3011. if (bindingType & BIND_SCOPE_LEXICAL) {
  3012. this.maybeExportDefined(scope, name);
  3013. }
  3014. } else if (bindingType & BIND_SCOPE_VAR) {
  3015. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  3016. scope = this.scopeStack[i];
  3017. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  3018. scope.var.add(name);
  3019. this.maybeExportDefined(scope, name);
  3020. if (scope.flags & SCOPE_VAR) break;
  3021. }
  3022. }
  3023. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  3024. this.undefinedExports.delete(name);
  3025. }
  3026. }
  3027. maybeExportDefined(scope, name) {
  3028. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  3029. this.undefinedExports.delete(name);
  3030. }
  3031. }
  3032. checkRedeclarationInScope(scope, name, bindingType, loc) {
  3033. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  3034. this.parser.raise(Errors.VarRedeclaration, {
  3035. at: loc,
  3036. identifierName: name
  3037. });
  3038. }
  3039. }
  3040. isRedeclaredInScope(scope, name, bindingType) {
  3041. if (!(bindingType & BIND_KIND_VALUE)) return false;
  3042. if (bindingType & BIND_SCOPE_LEXICAL) {
  3043. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  3044. }
  3045. if (bindingType & BIND_SCOPE_FUNCTION) {
  3046. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  3047. }
  3048. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  3049. }
  3050. checkLocalExport(id) {
  3051. const {
  3052. name
  3053. } = id;
  3054. const topLevelScope = this.scopeStack[0];
  3055. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  3056. this.undefinedExports.set(name, id.loc.start);
  3057. }
  3058. }
  3059. currentScope() {
  3060. return this.scopeStack[this.scopeStack.length - 1];
  3061. }
  3062. currentVarScopeFlags() {
  3063. for (let i = this.scopeStack.length - 1;; i--) {
  3064. const {
  3065. flags
  3066. } = this.scopeStack[i];
  3067. if (flags & SCOPE_VAR) {
  3068. return flags;
  3069. }
  3070. }
  3071. }
  3072. currentThisScopeFlags() {
  3073. for (let i = this.scopeStack.length - 1;; i--) {
  3074. const {
  3075. flags
  3076. } = this.scopeStack[i];
  3077. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  3078. return flags;
  3079. }
  3080. }
  3081. }
  3082. }
  3083. class FlowScope extends Scope {
  3084. constructor(...args) {
  3085. super(...args);
  3086. this.declareFunctions = new Set();
  3087. }
  3088. }
  3089. class FlowScopeHandler extends ScopeHandler {
  3090. createScope(flags) {
  3091. return new FlowScope(flags);
  3092. }
  3093. declareName(name, bindingType, loc) {
  3094. const scope = this.currentScope();
  3095. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  3096. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  3097. this.maybeExportDefined(scope, name);
  3098. scope.declareFunctions.add(name);
  3099. return;
  3100. }
  3101. super.declareName(...arguments);
  3102. }
  3103. isRedeclaredInScope(scope, name, bindingType) {
  3104. if (super.isRedeclaredInScope(...arguments)) return true;
  3105. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  3106. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  3107. }
  3108. return false;
  3109. }
  3110. checkLocalExport(id) {
  3111. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  3112. super.checkLocalExport(id);
  3113. }
  3114. }
  3115. }
  3116. class ClassScope {
  3117. constructor() {
  3118. this.privateNames = new Set();
  3119. this.loneAccessors = new Map();
  3120. this.undefinedPrivateNames = new Map();
  3121. }
  3122. }
  3123. class ClassScopeHandler {
  3124. constructor(parser) {
  3125. this.parser = void 0;
  3126. this.stack = [];
  3127. this.undefinedPrivateNames = new Map();
  3128. this.parser = parser;
  3129. }
  3130. current() {
  3131. return this.stack[this.stack.length - 1];
  3132. }
  3133. enter() {
  3134. this.stack.push(new ClassScope());
  3135. }
  3136. exit() {
  3137. const oldClassScope = this.stack.pop();
  3138. const current = this.current();
  3139. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3140. if (current) {
  3141. if (!current.undefinedPrivateNames.has(name)) {
  3142. current.undefinedPrivateNames.set(name, loc);
  3143. }
  3144. } else {
  3145. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3146. at: loc,
  3147. identifierName: name
  3148. });
  3149. }
  3150. }
  3151. }
  3152. declarePrivateName(name, elementType, loc) {
  3153. const {
  3154. privateNames,
  3155. loneAccessors,
  3156. undefinedPrivateNames
  3157. } = this.current();
  3158. let redefined = privateNames.has(name);
  3159. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3160. const accessor = redefined && loneAccessors.get(name);
  3161. if (accessor) {
  3162. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3163. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3164. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3165. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3166. redefined = oldKind === newKind || oldStatic !== newStatic;
  3167. if (!redefined) loneAccessors.delete(name);
  3168. } else if (!redefined) {
  3169. loneAccessors.set(name, elementType);
  3170. }
  3171. }
  3172. if (redefined) {
  3173. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3174. at: loc,
  3175. identifierName: name
  3176. });
  3177. }
  3178. privateNames.add(name);
  3179. undefinedPrivateNames.delete(name);
  3180. }
  3181. usePrivateName(name, loc) {
  3182. let classScope;
  3183. for (classScope of this.stack) {
  3184. if (classScope.privateNames.has(name)) return;
  3185. }
  3186. if (classScope) {
  3187. classScope.undefinedPrivateNames.set(name, loc);
  3188. } else {
  3189. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3190. at: loc,
  3191. identifierName: name
  3192. });
  3193. }
  3194. }
  3195. }
  3196. const kExpression = 0,
  3197. kMaybeArrowParameterDeclaration = 1,
  3198. kMaybeAsyncArrowParameterDeclaration = 2,
  3199. kParameterDeclaration = 3;
  3200. class ExpressionScope {
  3201. constructor(type = kExpression) {
  3202. this.type = void 0;
  3203. this.type = type;
  3204. }
  3205. canBeArrowParameterDeclaration() {
  3206. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3207. }
  3208. isCertainlyParameterDeclaration() {
  3209. return this.type === kParameterDeclaration;
  3210. }
  3211. }
  3212. class ArrowHeadParsingScope extends ExpressionScope {
  3213. constructor(type) {
  3214. super(type);
  3215. this.declarationErrors = new Map();
  3216. }
  3217. recordDeclarationError(ParsingErrorClass, {
  3218. at
  3219. }) {
  3220. const index = at.index;
  3221. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3222. }
  3223. clearDeclarationError(index) {
  3224. this.declarationErrors.delete(index);
  3225. }
  3226. iterateErrors(iterator) {
  3227. this.declarationErrors.forEach(iterator);
  3228. }
  3229. }
  3230. class ExpressionScopeHandler {
  3231. constructor(parser) {
  3232. this.parser = void 0;
  3233. this.stack = [new ExpressionScope()];
  3234. this.parser = parser;
  3235. }
  3236. enter(scope) {
  3237. this.stack.push(scope);
  3238. }
  3239. exit() {
  3240. this.stack.pop();
  3241. }
  3242. recordParameterInitializerError(toParseError, {
  3243. at: node
  3244. }) {
  3245. const origin = {
  3246. at: node.loc.start
  3247. };
  3248. const {
  3249. stack
  3250. } = this;
  3251. let i = stack.length - 1;
  3252. let scope = stack[i];
  3253. while (!scope.isCertainlyParameterDeclaration()) {
  3254. if (scope.canBeArrowParameterDeclaration()) {
  3255. scope.recordDeclarationError(toParseError, origin);
  3256. } else {
  3257. return;
  3258. }
  3259. scope = stack[--i];
  3260. }
  3261. this.parser.raise(toParseError, origin);
  3262. }
  3263. recordParenthesizedIdentifierError({
  3264. at: node
  3265. }) {
  3266. const {
  3267. stack
  3268. } = this;
  3269. const scope = stack[stack.length - 1];
  3270. const origin = {
  3271. at: node.loc.start
  3272. };
  3273. if (scope.isCertainlyParameterDeclaration()) {
  3274. this.parser.raise(Errors.InvalidParenthesizedAssignment, origin);
  3275. } else if (scope.canBeArrowParameterDeclaration()) {
  3276. scope.recordDeclarationError(Errors.InvalidParenthesizedAssignment, origin);
  3277. } else {
  3278. return;
  3279. }
  3280. }
  3281. recordAsyncArrowParametersError({
  3282. at
  3283. }) {
  3284. const {
  3285. stack
  3286. } = this;
  3287. let i = stack.length - 1;
  3288. let scope = stack[i];
  3289. while (scope.canBeArrowParameterDeclaration()) {
  3290. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3291. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3292. at
  3293. });
  3294. }
  3295. scope = stack[--i];
  3296. }
  3297. }
  3298. validateAsPattern() {
  3299. const {
  3300. stack
  3301. } = this;
  3302. const currentScope = stack[stack.length - 1];
  3303. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3304. currentScope.iterateErrors(([toParseError, loc]) => {
  3305. this.parser.raise(toParseError, {
  3306. at: loc
  3307. });
  3308. let i = stack.length - 2;
  3309. let scope = stack[i];
  3310. while (scope.canBeArrowParameterDeclaration()) {
  3311. scope.clearDeclarationError(loc.index);
  3312. scope = stack[--i];
  3313. }
  3314. });
  3315. }
  3316. }
  3317. function newParameterDeclarationScope() {
  3318. return new ExpressionScope(kParameterDeclaration);
  3319. }
  3320. function newArrowHeadScope() {
  3321. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3322. }
  3323. function newAsyncArrowScope() {
  3324. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3325. }
  3326. function newExpressionScope() {
  3327. return new ExpressionScope();
  3328. }
  3329. const PARAM = 0b0000,
  3330. PARAM_YIELD = 0b0001,
  3331. PARAM_AWAIT = 0b0010,
  3332. PARAM_RETURN = 0b0100,
  3333. PARAM_IN = 0b1000;
  3334. class ProductionParameterHandler {
  3335. constructor() {
  3336. this.stacks = [];
  3337. }
  3338. enter(flags) {
  3339. this.stacks.push(flags);
  3340. }
  3341. exit() {
  3342. this.stacks.pop();
  3343. }
  3344. currentFlags() {
  3345. return this.stacks[this.stacks.length - 1];
  3346. }
  3347. get hasAwait() {
  3348. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3349. }
  3350. get hasYield() {
  3351. return (this.currentFlags() & PARAM_YIELD) > 0;
  3352. }
  3353. get hasReturn() {
  3354. return (this.currentFlags() & PARAM_RETURN) > 0;
  3355. }
  3356. get hasIn() {
  3357. return (this.currentFlags() & PARAM_IN) > 0;
  3358. }
  3359. }
  3360. function functionFlags(isAsync, isGenerator) {
  3361. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3362. }
  3363. class UtilParser extends Tokenizer {
  3364. addExtra(node, key, value, enumerable = true) {
  3365. if (!node) return;
  3366. const extra = node.extra = node.extra || {};
  3367. if (enumerable) {
  3368. extra[key] = value;
  3369. } else {
  3370. Object.defineProperty(extra, key, {
  3371. enumerable,
  3372. value
  3373. });
  3374. }
  3375. }
  3376. isContextual(token) {
  3377. return this.state.type === token && !this.state.containsEsc;
  3378. }
  3379. isUnparsedContextual(nameStart, name) {
  3380. const nameEnd = nameStart + name.length;
  3381. if (this.input.slice(nameStart, nameEnd) === name) {
  3382. const nextCh = this.input.charCodeAt(nameEnd);
  3383. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3384. }
  3385. return false;
  3386. }
  3387. isLookaheadContextual(name) {
  3388. const next = this.nextTokenStart();
  3389. return this.isUnparsedContextual(next, name);
  3390. }
  3391. eatContextual(token) {
  3392. if (this.isContextual(token)) {
  3393. this.next();
  3394. return true;
  3395. }
  3396. return false;
  3397. }
  3398. expectContextual(token, toParseError) {
  3399. if (!this.eatContextual(token)) {
  3400. if (toParseError != null) {
  3401. throw this.raise(toParseError, {
  3402. at: this.state.startLoc
  3403. });
  3404. }
  3405. throw this.unexpected(null, token);
  3406. }
  3407. }
  3408. canInsertSemicolon() {
  3409. return this.match(135) || this.match(8) || this.hasPrecedingLineBreak();
  3410. }
  3411. hasPrecedingLineBreak() {
  3412. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3413. }
  3414. hasFollowingLineBreak() {
  3415. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3416. return skipWhiteSpaceToLineBreak.test(this.input);
  3417. }
  3418. isLineTerminator() {
  3419. return this.eat(13) || this.canInsertSemicolon();
  3420. }
  3421. semicolon(allowAsi = true) {
  3422. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3423. this.raise(Errors.MissingSemicolon, {
  3424. at: this.state.lastTokEndLoc
  3425. });
  3426. }
  3427. expect(type, loc) {
  3428. this.eat(type) || this.unexpected(loc, type);
  3429. }
  3430. tryParse(fn, oldState = this.state.clone()) {
  3431. const abortSignal = {
  3432. node: null
  3433. };
  3434. try {
  3435. const node = fn((node = null) => {
  3436. abortSignal.node = node;
  3437. throw abortSignal;
  3438. });
  3439. if (this.state.errors.length > oldState.errors.length) {
  3440. const failState = this.state;
  3441. this.state = oldState;
  3442. this.state.tokensLength = failState.tokensLength;
  3443. return {
  3444. node,
  3445. error: failState.errors[oldState.errors.length],
  3446. thrown: false,
  3447. aborted: false,
  3448. failState
  3449. };
  3450. }
  3451. return {
  3452. node,
  3453. error: null,
  3454. thrown: false,
  3455. aborted: false,
  3456. failState: null
  3457. };
  3458. } catch (error) {
  3459. const failState = this.state;
  3460. this.state = oldState;
  3461. if (error instanceof SyntaxError) {
  3462. return {
  3463. node: null,
  3464. error,
  3465. thrown: true,
  3466. aborted: false,
  3467. failState
  3468. };
  3469. }
  3470. if (error === abortSignal) {
  3471. return {
  3472. node: abortSignal.node,
  3473. error: null,
  3474. thrown: false,
  3475. aborted: true,
  3476. failState
  3477. };
  3478. }
  3479. throw error;
  3480. }
  3481. }
  3482. checkExpressionErrors(refExpressionErrors, andThrow) {
  3483. if (!refExpressionErrors) return false;
  3484. const {
  3485. shorthandAssignLoc,
  3486. doubleProtoLoc,
  3487. privateKeyLoc,
  3488. optionalParametersLoc
  3489. } = refExpressionErrors;
  3490. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3491. if (!andThrow) {
  3492. return hasErrors;
  3493. }
  3494. if (shorthandAssignLoc != null) {
  3495. this.raise(Errors.InvalidCoverInitializedName, {
  3496. at: shorthandAssignLoc
  3497. });
  3498. }
  3499. if (doubleProtoLoc != null) {
  3500. this.raise(Errors.DuplicateProto, {
  3501. at: doubleProtoLoc
  3502. });
  3503. }
  3504. if (privateKeyLoc != null) {
  3505. this.raise(Errors.UnexpectedPrivateField, {
  3506. at: privateKeyLoc
  3507. });
  3508. }
  3509. if (optionalParametersLoc != null) {
  3510. this.unexpected(optionalParametersLoc);
  3511. }
  3512. }
  3513. isLiteralPropertyName() {
  3514. return tokenIsLiteralPropertyName(this.state.type);
  3515. }
  3516. isPrivateName(node) {
  3517. return node.type === "PrivateName";
  3518. }
  3519. getPrivateNameSV(node) {
  3520. return node.id.name;
  3521. }
  3522. hasPropertyAsPrivateName(node) {
  3523. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3524. }
  3525. isOptionalChain(node) {
  3526. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  3527. }
  3528. isObjectProperty(node) {
  3529. return node.type === "ObjectProperty";
  3530. }
  3531. isObjectMethod(node) {
  3532. return node.type === "ObjectMethod";
  3533. }
  3534. initializeScopes(inModule = this.options.sourceType === "module") {
  3535. const oldLabels = this.state.labels;
  3536. this.state.labels = [];
  3537. const oldExportedIdentifiers = this.exportedIdentifiers;
  3538. this.exportedIdentifiers = new Set();
  3539. const oldInModule = this.inModule;
  3540. this.inModule = inModule;
  3541. const oldScope = this.scope;
  3542. const ScopeHandler = this.getScopeHandler();
  3543. this.scope = new ScopeHandler(this, inModule);
  3544. const oldProdParam = this.prodParam;
  3545. this.prodParam = new ProductionParameterHandler();
  3546. const oldClassScope = this.classScope;
  3547. this.classScope = new ClassScopeHandler(this);
  3548. const oldExpressionScope = this.expressionScope;
  3549. this.expressionScope = new ExpressionScopeHandler(this);
  3550. return () => {
  3551. this.state.labels = oldLabels;
  3552. this.exportedIdentifiers = oldExportedIdentifiers;
  3553. this.inModule = oldInModule;
  3554. this.scope = oldScope;
  3555. this.prodParam = oldProdParam;
  3556. this.classScope = oldClassScope;
  3557. this.expressionScope = oldExpressionScope;
  3558. };
  3559. }
  3560. enterInitialScopes() {
  3561. let paramFlags = PARAM;
  3562. if (this.inModule) {
  3563. paramFlags |= PARAM_AWAIT;
  3564. }
  3565. this.scope.enter(SCOPE_PROGRAM);
  3566. this.prodParam.enter(paramFlags);
  3567. }
  3568. checkDestructuringPrivate(refExpressionErrors) {
  3569. const {
  3570. privateKeyLoc
  3571. } = refExpressionErrors;
  3572. if (privateKeyLoc !== null) {
  3573. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3574. }
  3575. }
  3576. }
  3577. class ExpressionErrors {
  3578. constructor() {
  3579. this.shorthandAssignLoc = null;
  3580. this.doubleProtoLoc = null;
  3581. this.privateKeyLoc = null;
  3582. this.optionalParametersLoc = null;
  3583. }
  3584. }
  3585. class Node {
  3586. constructor(parser, pos, loc) {
  3587. this.type = "";
  3588. this.start = pos;
  3589. this.end = 0;
  3590. this.loc = new SourceLocation(loc);
  3591. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3592. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3593. }
  3594. }
  3595. const NodePrototype = Node.prototype;
  3596. {
  3597. NodePrototype.__clone = function () {
  3598. const newNode = new Node();
  3599. const keys = Object.keys(this);
  3600. for (let i = 0, length = keys.length; i < length; i++) {
  3601. const key = keys[i];
  3602. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3603. newNode[key] = this[key];
  3604. }
  3605. }
  3606. return newNode;
  3607. };
  3608. }
  3609. function clonePlaceholder(node) {
  3610. return cloneIdentifier(node);
  3611. }
  3612. function cloneIdentifier(node) {
  3613. const {
  3614. type,
  3615. start,
  3616. end,
  3617. loc,
  3618. range,
  3619. extra,
  3620. name
  3621. } = node;
  3622. const cloned = Object.create(NodePrototype);
  3623. cloned.type = type;
  3624. cloned.start = start;
  3625. cloned.end = end;
  3626. cloned.loc = loc;
  3627. cloned.range = range;
  3628. cloned.extra = extra;
  3629. cloned.name = name;
  3630. if (type === "Placeholder") {
  3631. cloned.expectedNode = node.expectedNode;
  3632. }
  3633. return cloned;
  3634. }
  3635. function cloneStringLiteral(node) {
  3636. const {
  3637. type,
  3638. start,
  3639. end,
  3640. loc,
  3641. range,
  3642. extra
  3643. } = node;
  3644. if (type === "Placeholder") {
  3645. return clonePlaceholder(node);
  3646. }
  3647. const cloned = Object.create(NodePrototype);
  3648. cloned.type = type;
  3649. cloned.start = start;
  3650. cloned.end = end;
  3651. cloned.loc = loc;
  3652. cloned.range = range;
  3653. if (node.raw !== undefined) {
  3654. cloned.raw = node.raw;
  3655. } else {
  3656. cloned.extra = extra;
  3657. }
  3658. cloned.value = node.value;
  3659. return cloned;
  3660. }
  3661. class NodeUtils extends UtilParser {
  3662. startNode() {
  3663. return new Node(this, this.state.start, this.state.startLoc);
  3664. }
  3665. startNodeAt(pos, loc) {
  3666. return new Node(this, pos, loc);
  3667. }
  3668. startNodeAtNode(type) {
  3669. return this.startNodeAt(type.start, type.loc.start);
  3670. }
  3671. finishNode(node, type) {
  3672. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3673. }
  3674. finishNodeAt(node, type, endLoc) {
  3675. node.type = type;
  3676. node.end = endLoc.index;
  3677. node.loc.end = endLoc;
  3678. if (this.options.ranges) node.range[1] = endLoc.index;
  3679. if (this.options.attachComment) this.processComment(node);
  3680. return node;
  3681. }
  3682. resetStartLocation(node, start, startLoc) {
  3683. node.start = start;
  3684. node.loc.start = startLoc;
  3685. if (this.options.ranges) node.range[0] = start;
  3686. }
  3687. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3688. node.end = endLoc.index;
  3689. node.loc.end = endLoc;
  3690. if (this.options.ranges) node.range[1] = endLoc.index;
  3691. }
  3692. resetStartLocationFromNode(node, locationNode) {
  3693. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  3694. }
  3695. }
  3696. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3697. const FlowErrors = ParseErrorEnum`flow`(_ => ({
  3698. AmbiguousConditionalArrow: _("Ambiguous expression: wrap the arrow functions in parentheses to disambiguate."),
  3699. 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."),
  3700. AssignReservedType: _(({
  3701. reservedType
  3702. }) => `Cannot overwrite reserved type ${reservedType}.`),
  3703. DeclareClassElement: _("The `declare` modifier can only appear on class fields."),
  3704. DeclareClassFieldInitializer: _("Initializers are not allowed in fields with the `declare` modifier."),
  3705. DuplicateDeclareModuleExports: _("Duplicate `declare module.exports` statement."),
  3706. EnumBooleanMemberNotInitialized: _(({
  3707. memberName,
  3708. enumName
  3709. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`),
  3710. EnumDuplicateMemberName: _(({
  3711. memberName,
  3712. enumName
  3713. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`),
  3714. EnumInconsistentMemberValues: _(({
  3715. enumName
  3716. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`),
  3717. EnumInvalidExplicitType: _(({
  3718. invalidEnumType,
  3719. enumName
  3720. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`),
  3721. EnumInvalidExplicitTypeUnknownSupplied: _(({
  3722. enumName
  3723. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`),
  3724. EnumInvalidMemberInitializerPrimaryType: _(({
  3725. enumName,
  3726. memberName,
  3727. explicitType
  3728. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`),
  3729. EnumInvalidMemberInitializerSymbolType: _(({
  3730. enumName,
  3731. memberName
  3732. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`),
  3733. EnumInvalidMemberInitializerUnknownType: _(({
  3734. enumName,
  3735. memberName
  3736. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`),
  3737. EnumInvalidMemberName: _(({
  3738. enumName,
  3739. memberName,
  3740. suggestion
  3741. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`),
  3742. EnumNumberMemberNotInitialized: _(({
  3743. enumName,
  3744. memberName
  3745. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`),
  3746. EnumStringMemberInconsistentlyInitailized: _(({
  3747. enumName
  3748. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`),
  3749. GetterMayNotHaveThisParam: _("A getter cannot have a `this` parameter."),
  3750. 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."),
  3751. InexactInsideExact: _("Explicit inexact syntax cannot appear inside an explicit exact object type."),
  3752. InexactInsideNonObject: _("Explicit inexact syntax cannot appear in class or interface definitions."),
  3753. InexactVariance: _("Explicit inexact syntax cannot have variance."),
  3754. InvalidNonTypeImportInDeclareModule: _("Imports within a `declare module` body must always be `import type` or `import typeof`."),
  3755. MissingTypeParamDefault: _("Type parameter declaration needs a default, since a preceding type parameter declaration has a default."),
  3756. NestedDeclareModule: _("`declare module` cannot be used inside another `declare module`."),
  3757. NestedFlowComment: _("Cannot have a flow comment inside another flow comment."),
  3758. PatternIsOptional: _("A binding pattern parameter cannot be optional in an implementation signature.", {
  3759. reasonCode: "OptionalBindingPattern"
  3760. }),
  3761. SetterMayNotHaveThisParam: _("A setter cannot have a `this` parameter."),
  3762. SpreadVariance: _("Spread properties cannot have variance."),
  3763. ThisParamAnnotationRequired: _("A type annotation is required for the `this` parameter."),
  3764. ThisParamBannedInConstructor: _("Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions."),
  3765. ThisParamMayNotBeOptional: _("The `this` parameter cannot be optional."),
  3766. ThisParamMustBeFirst: _("The `this` parameter must be the first function parameter."),
  3767. ThisParamNoDefault: _("The `this` parameter may not have a default value."),
  3768. TypeBeforeInitializer: _("Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`."),
  3769. TypeCastInPattern: _("The type cast expression is expected to be wrapped with parenthesis."),
  3770. UnexpectedExplicitInexactInObject: _("Explicit inexact syntax must appear at the end of an inexact object."),
  3771. UnexpectedReservedType: _(({
  3772. reservedType
  3773. }) => `Unexpected reserved type ${reservedType}.`),
  3774. UnexpectedReservedUnderscore: _("`_` is only allowed as a type argument to call or new."),
  3775. UnexpectedSpaceBetweenModuloChecks: _("Spaces between `%` and `checks` are not allowed here."),
  3776. UnexpectedSpreadType: _("Spread operator cannot appear in class or interface definitions."),
  3777. UnexpectedSubtractionOperand: _('Unexpected token, expected "number" or "bigint".'),
  3778. UnexpectedTokenAfterTypeParameter: _("Expected an arrow function after this type parameter declaration."),
  3779. UnexpectedTypeParameterBeforeAsyncArrowFunction: _("Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`."),
  3780. UnsupportedDeclareExportKind: _(({
  3781. unsupportedExportKind,
  3782. suggestion
  3783. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`),
  3784. UnsupportedStatementInDeclareModule: _("Only declares and type imports are allowed inside declare module."),
  3785. UnterminatedFlowComment: _("Unterminated flow-comment.")
  3786. }));
  3787. function isEsModuleType(bodyElement) {
  3788. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3789. }
  3790. function hasTypeImportKind(node) {
  3791. return node.importKind === "type" || node.importKind === "typeof";
  3792. }
  3793. function isMaybeDefaultImport(type) {
  3794. return tokenIsKeywordOrIdentifier(type) && type !== 97;
  3795. }
  3796. const exportSuggestions = {
  3797. const: "declare export var",
  3798. let: "declare export var",
  3799. type: "export type",
  3800. interface: "export interface"
  3801. };
  3802. function partition(list, test) {
  3803. const list1 = [];
  3804. const list2 = [];
  3805. for (let i = 0; i < list.length; i++) {
  3806. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3807. }
  3808. return [list1, list2];
  3809. }
  3810. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3811. var flow = (superClass => class extends superClass {
  3812. constructor(...args) {
  3813. super(...args);
  3814. this.flowPragma = undefined;
  3815. }
  3816. getScopeHandler() {
  3817. return FlowScopeHandler;
  3818. }
  3819. shouldParseTypes() {
  3820. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3821. }
  3822. shouldParseEnums() {
  3823. return !!this.getPluginOption("flow", "enums");
  3824. }
  3825. finishToken(type, val) {
  3826. if (type !== 129 && type !== 13 && type !== 28) {
  3827. if (this.flowPragma === undefined) {
  3828. this.flowPragma = null;
  3829. }
  3830. }
  3831. return super.finishToken(type, val);
  3832. }
  3833. addComment(comment) {
  3834. if (this.flowPragma === undefined) {
  3835. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3836. if (!matches) ; else if (matches[1] === "flow") {
  3837. this.flowPragma = "flow";
  3838. } else if (matches[1] === "noflow") {
  3839. this.flowPragma = "noflow";
  3840. } else {
  3841. throw new Error("Unexpected flow pragma");
  3842. }
  3843. }
  3844. return super.addComment(comment);
  3845. }
  3846. flowParseTypeInitialiser(tok) {
  3847. const oldInType = this.state.inType;
  3848. this.state.inType = true;
  3849. this.expect(tok || 14);
  3850. const type = this.flowParseType();
  3851. this.state.inType = oldInType;
  3852. return type;
  3853. }
  3854. flowParsePredicate() {
  3855. const node = this.startNode();
  3856. const moduloLoc = this.state.startLoc;
  3857. this.next();
  3858. this.expectContextual(107);
  3859. if (this.state.lastTokStart > moduloLoc.index + 1) {
  3860. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  3861. at: moduloLoc
  3862. });
  3863. }
  3864. if (this.eat(10)) {
  3865. node.value = this.parseExpression();
  3866. this.expect(11);
  3867. return this.finishNode(node, "DeclaredPredicate");
  3868. } else {
  3869. return this.finishNode(node, "InferredPredicate");
  3870. }
  3871. }
  3872. flowParseTypeAndPredicateInitialiser() {
  3873. const oldInType = this.state.inType;
  3874. this.state.inType = true;
  3875. this.expect(14);
  3876. let type = null;
  3877. let predicate = null;
  3878. if (this.match(54)) {
  3879. this.state.inType = oldInType;
  3880. predicate = this.flowParsePredicate();
  3881. } else {
  3882. type = this.flowParseType();
  3883. this.state.inType = oldInType;
  3884. if (this.match(54)) {
  3885. predicate = this.flowParsePredicate();
  3886. }
  3887. }
  3888. return [type, predicate];
  3889. }
  3890. flowParseDeclareClass(node) {
  3891. this.next();
  3892. this.flowParseInterfaceish(node, true);
  3893. return this.finishNode(node, "DeclareClass");
  3894. }
  3895. flowParseDeclareFunction(node) {
  3896. this.next();
  3897. const id = node.id = this.parseIdentifier();
  3898. const typeNode = this.startNode();
  3899. const typeContainer = this.startNode();
  3900. if (this.match(47)) {
  3901. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  3902. } else {
  3903. typeNode.typeParameters = null;
  3904. }
  3905. this.expect(10);
  3906. const tmp = this.flowParseFunctionTypeParams();
  3907. typeNode.params = tmp.params;
  3908. typeNode.rest = tmp.rest;
  3909. typeNode.this = tmp._this;
  3910. this.expect(11);
  3911. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3912. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  3913. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  3914. this.resetEndLocation(id);
  3915. this.semicolon();
  3916. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  3917. return this.finishNode(node, "DeclareFunction");
  3918. }
  3919. flowParseDeclare(node, insideModule) {
  3920. if (this.match(80)) {
  3921. return this.flowParseDeclareClass(node);
  3922. } else if (this.match(68)) {
  3923. return this.flowParseDeclareFunction(node);
  3924. } else if (this.match(74)) {
  3925. return this.flowParseDeclareVariable(node);
  3926. } else if (this.eatContextual(123)) {
  3927. if (this.match(16)) {
  3928. return this.flowParseDeclareModuleExports(node);
  3929. } else {
  3930. if (insideModule) {
  3931. this.raise(FlowErrors.NestedDeclareModule, {
  3932. at: this.state.lastTokStartLoc
  3933. });
  3934. }
  3935. return this.flowParseDeclareModule(node);
  3936. }
  3937. } else if (this.isContextual(126)) {
  3938. return this.flowParseDeclareTypeAlias(node);
  3939. } else if (this.isContextual(127)) {
  3940. return this.flowParseDeclareOpaqueType(node);
  3941. } else if (this.isContextual(125)) {
  3942. return this.flowParseDeclareInterface(node);
  3943. } else if (this.match(82)) {
  3944. return this.flowParseDeclareExportDeclaration(node, insideModule);
  3945. } else {
  3946. throw this.unexpected();
  3947. }
  3948. }
  3949. flowParseDeclareVariable(node) {
  3950. this.next();
  3951. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  3952. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  3953. this.semicolon();
  3954. return this.finishNode(node, "DeclareVariable");
  3955. }
  3956. flowParseDeclareModule(node) {
  3957. this.scope.enter(SCOPE_OTHER);
  3958. if (this.match(129)) {
  3959. node.id = this.parseExprAtom();
  3960. } else {
  3961. node.id = this.parseIdentifier();
  3962. }
  3963. const bodyNode = node.body = this.startNode();
  3964. const body = bodyNode.body = [];
  3965. this.expect(5);
  3966. while (!this.match(8)) {
  3967. let bodyNode = this.startNode();
  3968. if (this.match(83)) {
  3969. this.next();
  3970. if (!this.isContextual(126) && !this.match(87)) {
  3971. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  3972. at: this.state.lastTokStartLoc
  3973. });
  3974. }
  3975. this.parseImport(bodyNode);
  3976. } else {
  3977. this.expectContextual(121, FlowErrors.UnsupportedStatementInDeclareModule);
  3978. bodyNode = this.flowParseDeclare(bodyNode, true);
  3979. }
  3980. body.push(bodyNode);
  3981. }
  3982. this.scope.exit();
  3983. this.expect(8);
  3984. this.finishNode(bodyNode, "BlockStatement");
  3985. let kind = null;
  3986. let hasModuleExport = false;
  3987. body.forEach(bodyElement => {
  3988. if (isEsModuleType(bodyElement)) {
  3989. if (kind === "CommonJS") {
  3990. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  3991. at: bodyElement
  3992. });
  3993. }
  3994. kind = "ES";
  3995. } else if (bodyElement.type === "DeclareModuleExports") {
  3996. if (hasModuleExport) {
  3997. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  3998. at: bodyElement
  3999. });
  4000. }
  4001. if (kind === "ES") {
  4002. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4003. at: bodyElement
  4004. });
  4005. }
  4006. kind = "CommonJS";
  4007. hasModuleExport = true;
  4008. }
  4009. });
  4010. node.kind = kind || "CommonJS";
  4011. return this.finishNode(node, "DeclareModule");
  4012. }
  4013. flowParseDeclareExportDeclaration(node, insideModule) {
  4014. this.expect(82);
  4015. if (this.eat(65)) {
  4016. if (this.match(68) || this.match(80)) {
  4017. node.declaration = this.flowParseDeclare(this.startNode());
  4018. } else {
  4019. node.declaration = this.flowParseType();
  4020. this.semicolon();
  4021. }
  4022. node.default = true;
  4023. return this.finishNode(node, "DeclareExportDeclaration");
  4024. } else {
  4025. if (this.match(75) || this.isLet() || (this.isContextual(126) || this.isContextual(125)) && !insideModule) {
  4026. const label = this.state.value;
  4027. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4028. at: this.state.startLoc,
  4029. unsupportedExportKind: label,
  4030. suggestion: exportSuggestions[label]
  4031. });
  4032. }
  4033. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(127)) {
  4034. node.declaration = this.flowParseDeclare(this.startNode());
  4035. node.default = false;
  4036. return this.finishNode(node, "DeclareExportDeclaration");
  4037. } else if (this.match(55) || this.match(5) || this.isContextual(125) || this.isContextual(126) || this.isContextual(127)) {
  4038. node = this.parseExport(node);
  4039. if (node.type === "ExportNamedDeclaration") {
  4040. node.type = "ExportDeclaration";
  4041. node.default = false;
  4042. delete node.exportKind;
  4043. }
  4044. node.type = "Declare" + node.type;
  4045. return node;
  4046. }
  4047. }
  4048. throw this.unexpected();
  4049. }
  4050. flowParseDeclareModuleExports(node) {
  4051. this.next();
  4052. this.expectContextual(108);
  4053. node.typeAnnotation = this.flowParseTypeAnnotation();
  4054. this.semicolon();
  4055. return this.finishNode(node, "DeclareModuleExports");
  4056. }
  4057. flowParseDeclareTypeAlias(node) {
  4058. this.next();
  4059. this.flowParseTypeAlias(node);
  4060. node.type = "DeclareTypeAlias";
  4061. return node;
  4062. }
  4063. flowParseDeclareOpaqueType(node) {
  4064. this.next();
  4065. this.flowParseOpaqueType(node, true);
  4066. node.type = "DeclareOpaqueType";
  4067. return node;
  4068. }
  4069. flowParseDeclareInterface(node) {
  4070. this.next();
  4071. this.flowParseInterfaceish(node);
  4072. return this.finishNode(node, "DeclareInterface");
  4073. }
  4074. flowParseInterfaceish(node, isClass = false) {
  4075. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4076. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4077. if (this.match(47)) {
  4078. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4079. } else {
  4080. node.typeParameters = null;
  4081. }
  4082. node.extends = [];
  4083. node.implements = [];
  4084. node.mixins = [];
  4085. if (this.eat(81)) {
  4086. do {
  4087. node.extends.push(this.flowParseInterfaceExtends());
  4088. } while (!isClass && this.eat(12));
  4089. }
  4090. if (this.isContextual(114)) {
  4091. this.next();
  4092. do {
  4093. node.mixins.push(this.flowParseInterfaceExtends());
  4094. } while (this.eat(12));
  4095. }
  4096. if (this.isContextual(110)) {
  4097. this.next();
  4098. do {
  4099. node.implements.push(this.flowParseInterfaceExtends());
  4100. } while (this.eat(12));
  4101. }
  4102. node.body = this.flowParseObjectType({
  4103. allowStatic: isClass,
  4104. allowExact: false,
  4105. allowSpread: false,
  4106. allowProto: isClass,
  4107. allowInexact: false
  4108. });
  4109. }
  4110. flowParseInterfaceExtends() {
  4111. const node = this.startNode();
  4112. node.id = this.flowParseQualifiedTypeIdentifier();
  4113. if (this.match(47)) {
  4114. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4115. } else {
  4116. node.typeParameters = null;
  4117. }
  4118. return this.finishNode(node, "InterfaceExtends");
  4119. }
  4120. flowParseInterface(node) {
  4121. this.flowParseInterfaceish(node);
  4122. return this.finishNode(node, "InterfaceDeclaration");
  4123. }
  4124. checkNotUnderscore(word) {
  4125. if (word === "_") {
  4126. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4127. at: this.state.startLoc
  4128. });
  4129. }
  4130. }
  4131. checkReservedType(word, startLoc, declaration) {
  4132. if (!reservedTypes.has(word)) return;
  4133. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4134. at: startLoc,
  4135. reservedType: word
  4136. });
  4137. }
  4138. flowParseRestrictedIdentifier(liberal, declaration) {
  4139. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4140. return this.parseIdentifier(liberal);
  4141. }
  4142. flowParseTypeAlias(node) {
  4143. node.id = this.flowParseRestrictedIdentifier(false, true);
  4144. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4145. if (this.match(47)) {
  4146. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4147. } else {
  4148. node.typeParameters = null;
  4149. }
  4150. node.right = this.flowParseTypeInitialiser(29);
  4151. this.semicolon();
  4152. return this.finishNode(node, "TypeAlias");
  4153. }
  4154. flowParseOpaqueType(node, declare) {
  4155. this.expectContextual(126);
  4156. node.id = this.flowParseRestrictedIdentifier(true, true);
  4157. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4158. if (this.match(47)) {
  4159. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4160. } else {
  4161. node.typeParameters = null;
  4162. }
  4163. node.supertype = null;
  4164. if (this.match(14)) {
  4165. node.supertype = this.flowParseTypeInitialiser(14);
  4166. }
  4167. node.impltype = null;
  4168. if (!declare) {
  4169. node.impltype = this.flowParseTypeInitialiser(29);
  4170. }
  4171. this.semicolon();
  4172. return this.finishNode(node, "OpaqueType");
  4173. }
  4174. flowParseTypeParameter(requireDefault = false) {
  4175. const nodeStartLoc = this.state.startLoc;
  4176. const node = this.startNode();
  4177. const variance = this.flowParseVariance();
  4178. const ident = this.flowParseTypeAnnotatableIdentifier();
  4179. node.name = ident.name;
  4180. node.variance = variance;
  4181. node.bound = ident.typeAnnotation;
  4182. if (this.match(29)) {
  4183. this.eat(29);
  4184. node.default = this.flowParseType();
  4185. } else {
  4186. if (requireDefault) {
  4187. this.raise(FlowErrors.MissingTypeParamDefault, {
  4188. at: nodeStartLoc
  4189. });
  4190. }
  4191. }
  4192. return this.finishNode(node, "TypeParameter");
  4193. }
  4194. flowParseTypeParameterDeclaration() {
  4195. const oldInType = this.state.inType;
  4196. const node = this.startNode();
  4197. node.params = [];
  4198. this.state.inType = true;
  4199. if (this.match(47) || this.match(138)) {
  4200. this.next();
  4201. } else {
  4202. this.unexpected();
  4203. }
  4204. let defaultRequired = false;
  4205. do {
  4206. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4207. node.params.push(typeParameter);
  4208. if (typeParameter.default) {
  4209. defaultRequired = true;
  4210. }
  4211. if (!this.match(48)) {
  4212. this.expect(12);
  4213. }
  4214. } while (!this.match(48));
  4215. this.expect(48);
  4216. this.state.inType = oldInType;
  4217. return this.finishNode(node, "TypeParameterDeclaration");
  4218. }
  4219. flowParseTypeParameterInstantiation() {
  4220. const node = this.startNode();
  4221. const oldInType = this.state.inType;
  4222. node.params = [];
  4223. this.state.inType = true;
  4224. this.expect(47);
  4225. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4226. this.state.noAnonFunctionType = false;
  4227. while (!this.match(48)) {
  4228. node.params.push(this.flowParseType());
  4229. if (!this.match(48)) {
  4230. this.expect(12);
  4231. }
  4232. }
  4233. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4234. this.expect(48);
  4235. this.state.inType = oldInType;
  4236. return this.finishNode(node, "TypeParameterInstantiation");
  4237. }
  4238. flowParseTypeParameterInstantiationCallOrNew() {
  4239. const node = this.startNode();
  4240. const oldInType = this.state.inType;
  4241. node.params = [];
  4242. this.state.inType = true;
  4243. this.expect(47);
  4244. while (!this.match(48)) {
  4245. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4246. if (!this.match(48)) {
  4247. this.expect(12);
  4248. }
  4249. }
  4250. this.expect(48);
  4251. this.state.inType = oldInType;
  4252. return this.finishNode(node, "TypeParameterInstantiation");
  4253. }
  4254. flowParseInterfaceType() {
  4255. const node = this.startNode();
  4256. this.expectContextual(125);
  4257. node.extends = [];
  4258. if (this.eat(81)) {
  4259. do {
  4260. node.extends.push(this.flowParseInterfaceExtends());
  4261. } while (this.eat(12));
  4262. }
  4263. node.body = this.flowParseObjectType({
  4264. allowStatic: false,
  4265. allowExact: false,
  4266. allowSpread: false,
  4267. allowProto: false,
  4268. allowInexact: false
  4269. });
  4270. return this.finishNode(node, "InterfaceTypeAnnotation");
  4271. }
  4272. flowParseObjectPropertyKey() {
  4273. return this.match(130) || this.match(129) ? this.parseExprAtom() : this.parseIdentifier(true);
  4274. }
  4275. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4276. node.static = isStatic;
  4277. if (this.lookahead().type === 14) {
  4278. node.id = this.flowParseObjectPropertyKey();
  4279. node.key = this.flowParseTypeInitialiser();
  4280. } else {
  4281. node.id = null;
  4282. node.key = this.flowParseType();
  4283. }
  4284. this.expect(3);
  4285. node.value = this.flowParseTypeInitialiser();
  4286. node.variance = variance;
  4287. return this.finishNode(node, "ObjectTypeIndexer");
  4288. }
  4289. flowParseObjectTypeInternalSlot(node, isStatic) {
  4290. node.static = isStatic;
  4291. node.id = this.flowParseObjectPropertyKey();
  4292. this.expect(3);
  4293. this.expect(3);
  4294. if (this.match(47) || this.match(10)) {
  4295. node.method = true;
  4296. node.optional = false;
  4297. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4298. } else {
  4299. node.method = false;
  4300. if (this.eat(17)) {
  4301. node.optional = true;
  4302. }
  4303. node.value = this.flowParseTypeInitialiser();
  4304. }
  4305. return this.finishNode(node, "ObjectTypeInternalSlot");
  4306. }
  4307. flowParseObjectTypeMethodish(node) {
  4308. node.params = [];
  4309. node.rest = null;
  4310. node.typeParameters = null;
  4311. node.this = null;
  4312. if (this.match(47)) {
  4313. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4314. }
  4315. this.expect(10);
  4316. if (this.match(78)) {
  4317. node.this = this.flowParseFunctionTypeParam(true);
  4318. node.this.name = null;
  4319. if (!this.match(11)) {
  4320. this.expect(12);
  4321. }
  4322. }
  4323. while (!this.match(11) && !this.match(21)) {
  4324. node.params.push(this.flowParseFunctionTypeParam(false));
  4325. if (!this.match(11)) {
  4326. this.expect(12);
  4327. }
  4328. }
  4329. if (this.eat(21)) {
  4330. node.rest = this.flowParseFunctionTypeParam(false);
  4331. }
  4332. this.expect(11);
  4333. node.returnType = this.flowParseTypeInitialiser();
  4334. return this.finishNode(node, "FunctionTypeAnnotation");
  4335. }
  4336. flowParseObjectTypeCallProperty(node, isStatic) {
  4337. const valueNode = this.startNode();
  4338. node.static = isStatic;
  4339. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4340. return this.finishNode(node, "ObjectTypeCallProperty");
  4341. }
  4342. flowParseObjectType({
  4343. allowStatic,
  4344. allowExact,
  4345. allowSpread,
  4346. allowProto,
  4347. allowInexact
  4348. }) {
  4349. const oldInType = this.state.inType;
  4350. this.state.inType = true;
  4351. const nodeStart = this.startNode();
  4352. nodeStart.callProperties = [];
  4353. nodeStart.properties = [];
  4354. nodeStart.indexers = [];
  4355. nodeStart.internalSlots = [];
  4356. let endDelim;
  4357. let exact;
  4358. let inexact = false;
  4359. if (allowExact && this.match(6)) {
  4360. this.expect(6);
  4361. endDelim = 9;
  4362. exact = true;
  4363. } else {
  4364. this.expect(5);
  4365. endDelim = 8;
  4366. exact = false;
  4367. }
  4368. nodeStart.exact = exact;
  4369. while (!this.match(endDelim)) {
  4370. let isStatic = false;
  4371. let protoStartLoc = null;
  4372. let inexactStartLoc = null;
  4373. const node = this.startNode();
  4374. if (allowProto && this.isContextual(115)) {
  4375. const lookahead = this.lookahead();
  4376. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4377. this.next();
  4378. protoStartLoc = this.state.startLoc;
  4379. allowStatic = false;
  4380. }
  4381. }
  4382. if (allowStatic && this.isContextual(104)) {
  4383. const lookahead = this.lookahead();
  4384. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4385. this.next();
  4386. isStatic = true;
  4387. }
  4388. }
  4389. const variance = this.flowParseVariance();
  4390. if (this.eat(0)) {
  4391. if (protoStartLoc != null) {
  4392. this.unexpected(protoStartLoc);
  4393. }
  4394. if (this.eat(0)) {
  4395. if (variance) {
  4396. this.unexpected(variance.loc.start);
  4397. }
  4398. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4399. } else {
  4400. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4401. }
  4402. } else if (this.match(10) || this.match(47)) {
  4403. if (protoStartLoc != null) {
  4404. this.unexpected(protoStartLoc);
  4405. }
  4406. if (variance) {
  4407. this.unexpected(variance.loc.start);
  4408. }
  4409. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4410. } else {
  4411. let kind = "init";
  4412. if (this.isContextual(98) || this.isContextual(103)) {
  4413. const lookahead = this.lookahead();
  4414. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4415. kind = this.state.value;
  4416. this.next();
  4417. }
  4418. }
  4419. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4420. if (propOrInexact === null) {
  4421. inexact = true;
  4422. inexactStartLoc = this.state.lastTokStartLoc;
  4423. } else {
  4424. nodeStart.properties.push(propOrInexact);
  4425. }
  4426. }
  4427. this.flowObjectTypeSemicolon();
  4428. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4429. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4430. at: inexactStartLoc
  4431. });
  4432. }
  4433. }
  4434. this.expect(endDelim);
  4435. if (allowSpread) {
  4436. nodeStart.inexact = inexact;
  4437. }
  4438. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4439. this.state.inType = oldInType;
  4440. return out;
  4441. }
  4442. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4443. if (this.eat(21)) {
  4444. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4445. if (isInexactToken) {
  4446. if (!allowSpread) {
  4447. this.raise(FlowErrors.InexactInsideNonObject, {
  4448. at: this.state.lastTokStartLoc
  4449. });
  4450. } else if (!allowInexact) {
  4451. this.raise(FlowErrors.InexactInsideExact, {
  4452. at: this.state.lastTokStartLoc
  4453. });
  4454. }
  4455. if (variance) {
  4456. this.raise(FlowErrors.InexactVariance, {
  4457. at: variance
  4458. });
  4459. }
  4460. return null;
  4461. }
  4462. if (!allowSpread) {
  4463. this.raise(FlowErrors.UnexpectedSpreadType, {
  4464. at: this.state.lastTokStartLoc
  4465. });
  4466. }
  4467. if (protoStartLoc != null) {
  4468. this.unexpected(protoStartLoc);
  4469. }
  4470. if (variance) {
  4471. this.raise(FlowErrors.SpreadVariance, {
  4472. at: variance
  4473. });
  4474. }
  4475. node.argument = this.flowParseType();
  4476. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4477. } else {
  4478. node.key = this.flowParseObjectPropertyKey();
  4479. node.static = isStatic;
  4480. node.proto = protoStartLoc != null;
  4481. node.kind = kind;
  4482. let optional = false;
  4483. if (this.match(47) || this.match(10)) {
  4484. node.method = true;
  4485. if (protoStartLoc != null) {
  4486. this.unexpected(protoStartLoc);
  4487. }
  4488. if (variance) {
  4489. this.unexpected(variance.loc.start);
  4490. }
  4491. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4492. if (kind === "get" || kind === "set") {
  4493. this.flowCheckGetterSetterParams(node);
  4494. }
  4495. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4496. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4497. at: node.value.this
  4498. });
  4499. }
  4500. } else {
  4501. if (kind !== "init") this.unexpected();
  4502. node.method = false;
  4503. if (this.eat(17)) {
  4504. optional = true;
  4505. }
  4506. node.value = this.flowParseTypeInitialiser();
  4507. node.variance = variance;
  4508. }
  4509. node.optional = optional;
  4510. return this.finishNode(node, "ObjectTypeProperty");
  4511. }
  4512. }
  4513. flowCheckGetterSetterParams(property) {
  4514. const paramCount = property.kind === "get" ? 0 : 1;
  4515. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4516. if (property.value.this) {
  4517. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4518. at: property.value.this
  4519. });
  4520. }
  4521. if (length !== paramCount) {
  4522. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4523. at: property
  4524. });
  4525. }
  4526. if (property.kind === "set" && property.value.rest) {
  4527. this.raise(Errors.BadSetterRestParameter, {
  4528. at: property
  4529. });
  4530. }
  4531. }
  4532. flowObjectTypeSemicolon() {
  4533. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4534. this.unexpected();
  4535. }
  4536. }
  4537. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  4538. startPos = startPos || this.state.start;
  4539. startLoc = startLoc || this.state.startLoc;
  4540. let node = id || this.flowParseRestrictedIdentifier(true);
  4541. while (this.eat(16)) {
  4542. const node2 = this.startNodeAt(startPos, startLoc);
  4543. node2.qualification = node;
  4544. node2.id = this.flowParseRestrictedIdentifier(true);
  4545. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4546. }
  4547. return node;
  4548. }
  4549. flowParseGenericType(startPos, startLoc, id) {
  4550. const node = this.startNodeAt(startPos, startLoc);
  4551. node.typeParameters = null;
  4552. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  4553. if (this.match(47)) {
  4554. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4555. }
  4556. return this.finishNode(node, "GenericTypeAnnotation");
  4557. }
  4558. flowParseTypeofType() {
  4559. const node = this.startNode();
  4560. this.expect(87);
  4561. node.argument = this.flowParsePrimaryType();
  4562. return this.finishNode(node, "TypeofTypeAnnotation");
  4563. }
  4564. flowParseTupleType() {
  4565. const node = this.startNode();
  4566. node.types = [];
  4567. this.expect(0);
  4568. while (this.state.pos < this.length && !this.match(3)) {
  4569. node.types.push(this.flowParseType());
  4570. if (this.match(3)) break;
  4571. this.expect(12);
  4572. }
  4573. this.expect(3);
  4574. return this.finishNode(node, "TupleTypeAnnotation");
  4575. }
  4576. flowParseFunctionTypeParam(first) {
  4577. let name = null;
  4578. let optional = false;
  4579. let typeAnnotation = null;
  4580. const node = this.startNode();
  4581. const lh = this.lookahead();
  4582. const isThis = this.state.type === 78;
  4583. if (lh.type === 14 || lh.type === 17) {
  4584. if (isThis && !first) {
  4585. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4586. at: node
  4587. });
  4588. }
  4589. name = this.parseIdentifier(isThis);
  4590. if (this.eat(17)) {
  4591. optional = true;
  4592. if (isThis) {
  4593. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4594. at: node
  4595. });
  4596. }
  4597. }
  4598. typeAnnotation = this.flowParseTypeInitialiser();
  4599. } else {
  4600. typeAnnotation = this.flowParseType();
  4601. }
  4602. node.name = name;
  4603. node.optional = optional;
  4604. node.typeAnnotation = typeAnnotation;
  4605. return this.finishNode(node, "FunctionTypeParam");
  4606. }
  4607. reinterpretTypeAsFunctionTypeParam(type) {
  4608. const node = this.startNodeAt(type.start, type.loc.start);
  4609. node.name = null;
  4610. node.optional = false;
  4611. node.typeAnnotation = type;
  4612. return this.finishNode(node, "FunctionTypeParam");
  4613. }
  4614. flowParseFunctionTypeParams(params = []) {
  4615. let rest = null;
  4616. let _this = null;
  4617. if (this.match(78)) {
  4618. _this = this.flowParseFunctionTypeParam(true);
  4619. _this.name = null;
  4620. if (!this.match(11)) {
  4621. this.expect(12);
  4622. }
  4623. }
  4624. while (!this.match(11) && !this.match(21)) {
  4625. params.push(this.flowParseFunctionTypeParam(false));
  4626. if (!this.match(11)) {
  4627. this.expect(12);
  4628. }
  4629. }
  4630. if (this.eat(21)) {
  4631. rest = this.flowParseFunctionTypeParam(false);
  4632. }
  4633. return {
  4634. params,
  4635. rest,
  4636. _this
  4637. };
  4638. }
  4639. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  4640. switch (id.name) {
  4641. case "any":
  4642. return this.finishNode(node, "AnyTypeAnnotation");
  4643. case "bool":
  4644. case "boolean":
  4645. return this.finishNode(node, "BooleanTypeAnnotation");
  4646. case "mixed":
  4647. return this.finishNode(node, "MixedTypeAnnotation");
  4648. case "empty":
  4649. return this.finishNode(node, "EmptyTypeAnnotation");
  4650. case "number":
  4651. return this.finishNode(node, "NumberTypeAnnotation");
  4652. case "string":
  4653. return this.finishNode(node, "StringTypeAnnotation");
  4654. case "symbol":
  4655. return this.finishNode(node, "SymbolTypeAnnotation");
  4656. default:
  4657. this.checkNotUnderscore(id.name);
  4658. return this.flowParseGenericType(startPos, startLoc, id);
  4659. }
  4660. }
  4661. flowParsePrimaryType() {
  4662. const startPos = this.state.start;
  4663. const startLoc = this.state.startLoc;
  4664. const node = this.startNode();
  4665. let tmp;
  4666. let type;
  4667. let isGroupedType = false;
  4668. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4669. switch (this.state.type) {
  4670. case 5:
  4671. return this.flowParseObjectType({
  4672. allowStatic: false,
  4673. allowExact: false,
  4674. allowSpread: true,
  4675. allowProto: false,
  4676. allowInexact: true
  4677. });
  4678. case 6:
  4679. return this.flowParseObjectType({
  4680. allowStatic: false,
  4681. allowExact: true,
  4682. allowSpread: true,
  4683. allowProto: false,
  4684. allowInexact: false
  4685. });
  4686. case 0:
  4687. this.state.noAnonFunctionType = false;
  4688. type = this.flowParseTupleType();
  4689. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4690. return type;
  4691. case 47:
  4692. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4693. this.expect(10);
  4694. tmp = this.flowParseFunctionTypeParams();
  4695. node.params = tmp.params;
  4696. node.rest = tmp.rest;
  4697. node.this = tmp._this;
  4698. this.expect(11);
  4699. this.expect(19);
  4700. node.returnType = this.flowParseType();
  4701. return this.finishNode(node, "FunctionTypeAnnotation");
  4702. case 10:
  4703. this.next();
  4704. if (!this.match(11) && !this.match(21)) {
  4705. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4706. const token = this.lookahead().type;
  4707. isGroupedType = token !== 17 && token !== 14;
  4708. } else {
  4709. isGroupedType = true;
  4710. }
  4711. }
  4712. if (isGroupedType) {
  4713. this.state.noAnonFunctionType = false;
  4714. type = this.flowParseType();
  4715. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4716. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4717. this.expect(11);
  4718. return type;
  4719. } else {
  4720. this.eat(12);
  4721. }
  4722. }
  4723. if (type) {
  4724. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4725. } else {
  4726. tmp = this.flowParseFunctionTypeParams();
  4727. }
  4728. node.params = tmp.params;
  4729. node.rest = tmp.rest;
  4730. node.this = tmp._this;
  4731. this.expect(11);
  4732. this.expect(19);
  4733. node.returnType = this.flowParseType();
  4734. node.typeParameters = null;
  4735. return this.finishNode(node, "FunctionTypeAnnotation");
  4736. case 129:
  4737. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4738. case 85:
  4739. case 86:
  4740. node.value = this.match(85);
  4741. this.next();
  4742. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4743. case 53:
  4744. if (this.state.value === "-") {
  4745. this.next();
  4746. if (this.match(130)) {
  4747. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4748. }
  4749. if (this.match(131)) {
  4750. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4751. }
  4752. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4753. at: this.state.startLoc
  4754. });
  4755. }
  4756. throw this.unexpected();
  4757. case 130:
  4758. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4759. case 131:
  4760. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4761. case 88:
  4762. this.next();
  4763. return this.finishNode(node, "VoidTypeAnnotation");
  4764. case 84:
  4765. this.next();
  4766. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4767. case 78:
  4768. this.next();
  4769. return this.finishNode(node, "ThisTypeAnnotation");
  4770. case 55:
  4771. this.next();
  4772. return this.finishNode(node, "ExistsTypeAnnotation");
  4773. case 87:
  4774. return this.flowParseTypeofType();
  4775. default:
  4776. if (tokenIsKeyword(this.state.type)) {
  4777. const label = tokenLabelName(this.state.type);
  4778. this.next();
  4779. return super.createIdentifier(node, label);
  4780. } else if (tokenIsIdentifier(this.state.type)) {
  4781. if (this.isContextual(125)) {
  4782. return this.flowParseInterfaceType();
  4783. }
  4784. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  4785. }
  4786. }
  4787. throw this.unexpected();
  4788. }
  4789. flowParsePostfixType() {
  4790. const startPos = this.state.start;
  4791. const startLoc = this.state.startLoc;
  4792. let type = this.flowParsePrimaryType();
  4793. let seenOptionalIndexedAccess = false;
  4794. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4795. const node = this.startNodeAt(startPos, startLoc);
  4796. const optional = this.eat(18);
  4797. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4798. this.expect(0);
  4799. if (!optional && this.match(3)) {
  4800. node.elementType = type;
  4801. this.next();
  4802. type = this.finishNode(node, "ArrayTypeAnnotation");
  4803. } else {
  4804. node.objectType = type;
  4805. node.indexType = this.flowParseType();
  4806. this.expect(3);
  4807. if (seenOptionalIndexedAccess) {
  4808. node.optional = optional;
  4809. type = this.finishNode(node, "OptionalIndexedAccessType");
  4810. } else {
  4811. type = this.finishNode(node, "IndexedAccessType");
  4812. }
  4813. }
  4814. }
  4815. return type;
  4816. }
  4817. flowParsePrefixType() {
  4818. const node = this.startNode();
  4819. if (this.eat(17)) {
  4820. node.typeAnnotation = this.flowParsePrefixType();
  4821. return this.finishNode(node, "NullableTypeAnnotation");
  4822. } else {
  4823. return this.flowParsePostfixType();
  4824. }
  4825. }
  4826. flowParseAnonFunctionWithoutParens() {
  4827. const param = this.flowParsePrefixType();
  4828. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4829. const node = this.startNodeAt(param.start, param.loc.start);
  4830. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4831. node.rest = null;
  4832. node.this = null;
  4833. node.returnType = this.flowParseType();
  4834. node.typeParameters = null;
  4835. return this.finishNode(node, "FunctionTypeAnnotation");
  4836. }
  4837. return param;
  4838. }
  4839. flowParseIntersectionType() {
  4840. const node = this.startNode();
  4841. this.eat(45);
  4842. const type = this.flowParseAnonFunctionWithoutParens();
  4843. node.types = [type];
  4844. while (this.eat(45)) {
  4845. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4846. }
  4847. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4848. }
  4849. flowParseUnionType() {
  4850. const node = this.startNode();
  4851. this.eat(43);
  4852. const type = this.flowParseIntersectionType();
  4853. node.types = [type];
  4854. while (this.eat(43)) {
  4855. node.types.push(this.flowParseIntersectionType());
  4856. }
  4857. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  4858. }
  4859. flowParseType() {
  4860. const oldInType = this.state.inType;
  4861. this.state.inType = true;
  4862. const type = this.flowParseUnionType();
  4863. this.state.inType = oldInType;
  4864. return type;
  4865. }
  4866. flowParseTypeOrImplicitInstantiation() {
  4867. if (this.state.type === 128 && this.state.value === "_") {
  4868. const startPos = this.state.start;
  4869. const startLoc = this.state.startLoc;
  4870. const node = this.parseIdentifier();
  4871. return this.flowParseGenericType(startPos, startLoc, node);
  4872. } else {
  4873. return this.flowParseType();
  4874. }
  4875. }
  4876. flowParseTypeAnnotation() {
  4877. const node = this.startNode();
  4878. node.typeAnnotation = this.flowParseTypeInitialiser();
  4879. return this.finishNode(node, "TypeAnnotation");
  4880. }
  4881. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  4882. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  4883. if (this.match(14)) {
  4884. ident.typeAnnotation = this.flowParseTypeAnnotation();
  4885. this.resetEndLocation(ident);
  4886. }
  4887. return ident;
  4888. }
  4889. typeCastToParameter(node) {
  4890. node.expression.typeAnnotation = node.typeAnnotation;
  4891. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  4892. return node.expression;
  4893. }
  4894. flowParseVariance() {
  4895. let variance = null;
  4896. if (this.match(53)) {
  4897. variance = this.startNode();
  4898. if (this.state.value === "+") {
  4899. variance.kind = "plus";
  4900. } else {
  4901. variance.kind = "minus";
  4902. }
  4903. this.next();
  4904. this.finishNode(variance, "Variance");
  4905. }
  4906. return variance;
  4907. }
  4908. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  4909. if (allowExpressionBody) {
  4910. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  4911. }
  4912. return super.parseFunctionBody(node, false, isMethod);
  4913. }
  4914. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4915. if (this.match(14)) {
  4916. const typeNode = this.startNode();
  4917. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4918. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  4919. }
  4920. super.parseFunctionBodyAndFinish(node, type, isMethod);
  4921. }
  4922. parseStatement(context, topLevel) {
  4923. if (this.state.strict && this.isContextual(125)) {
  4924. const lookahead = this.lookahead();
  4925. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  4926. const node = this.startNode();
  4927. this.next();
  4928. return this.flowParseInterface(node);
  4929. }
  4930. } else if (this.shouldParseEnums() && this.isContextual(122)) {
  4931. const node = this.startNode();
  4932. this.next();
  4933. return this.flowParseEnumDeclaration(node);
  4934. }
  4935. const stmt = super.parseStatement(context, topLevel);
  4936. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  4937. this.flowPragma = null;
  4938. }
  4939. return stmt;
  4940. }
  4941. parseExpressionStatement(node, expr) {
  4942. if (expr.type === "Identifier") {
  4943. if (expr.name === "declare") {
  4944. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  4945. return this.flowParseDeclare(node);
  4946. }
  4947. } else if (tokenIsIdentifier(this.state.type)) {
  4948. if (expr.name === "interface") {
  4949. return this.flowParseInterface(node);
  4950. } else if (expr.name === "type") {
  4951. return this.flowParseTypeAlias(node);
  4952. } else if (expr.name === "opaque") {
  4953. return this.flowParseOpaqueType(node, false);
  4954. }
  4955. }
  4956. }
  4957. return super.parseExpressionStatement(node, expr);
  4958. }
  4959. shouldParseExportDeclaration() {
  4960. const {
  4961. type
  4962. } = this.state;
  4963. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
  4964. return !this.state.containsEsc;
  4965. }
  4966. return super.shouldParseExportDeclaration();
  4967. }
  4968. isExportDefaultSpecifier() {
  4969. const {
  4970. type
  4971. } = this.state;
  4972. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
  4973. return this.state.containsEsc;
  4974. }
  4975. return super.isExportDefaultSpecifier();
  4976. }
  4977. parseExportDefaultExpression() {
  4978. if (this.shouldParseEnums() && this.isContextual(122)) {
  4979. const node = this.startNode();
  4980. this.next();
  4981. return this.flowParseEnumDeclaration(node);
  4982. }
  4983. return super.parseExportDefaultExpression();
  4984. }
  4985. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  4986. if (!this.match(17)) return expr;
  4987. if (this.state.maybeInArrowParameters) {
  4988. const nextCh = this.lookaheadCharCode();
  4989. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  4990. this.setOptionalParametersError(refExpressionErrors);
  4991. return expr;
  4992. }
  4993. }
  4994. this.expect(17);
  4995. const state = this.state.clone();
  4996. const originalNoArrowAt = this.state.noArrowAt;
  4997. const node = this.startNodeAt(startPos, startLoc);
  4998. let {
  4999. consequent,
  5000. failed
  5001. } = this.tryParseConditionalConsequent();
  5002. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5003. if (failed || invalid.length > 0) {
  5004. const noArrowAt = [...originalNoArrowAt];
  5005. if (invalid.length > 0) {
  5006. this.state = state;
  5007. this.state.noArrowAt = noArrowAt;
  5008. for (let i = 0; i < invalid.length; i++) {
  5009. noArrowAt.push(invalid[i].start);
  5010. }
  5011. ({
  5012. consequent,
  5013. failed
  5014. } = this.tryParseConditionalConsequent());
  5015. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5016. }
  5017. if (failed && valid.length > 1) {
  5018. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5019. at: state.startLoc
  5020. });
  5021. }
  5022. if (failed && valid.length === 1) {
  5023. this.state = state;
  5024. noArrowAt.push(valid[0].start);
  5025. this.state.noArrowAt = noArrowAt;
  5026. ({
  5027. consequent,
  5028. failed
  5029. } = this.tryParseConditionalConsequent());
  5030. }
  5031. }
  5032. this.getArrowLikeExpressions(consequent, true);
  5033. this.state.noArrowAt = originalNoArrowAt;
  5034. this.expect(14);
  5035. node.test = expr;
  5036. node.consequent = consequent;
  5037. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5038. return this.finishNode(node, "ConditionalExpression");
  5039. }
  5040. tryParseConditionalConsequent() {
  5041. this.state.noArrowParamsConversionAt.push(this.state.start);
  5042. const consequent = this.parseMaybeAssignAllowIn();
  5043. const failed = !this.match(14);
  5044. this.state.noArrowParamsConversionAt.pop();
  5045. return {
  5046. consequent,
  5047. failed
  5048. };
  5049. }
  5050. getArrowLikeExpressions(node, disallowInvalid) {
  5051. const stack = [node];
  5052. const arrows = [];
  5053. while (stack.length !== 0) {
  5054. const node = stack.pop();
  5055. if (node.type === "ArrowFunctionExpression") {
  5056. if (node.typeParameters || !node.returnType) {
  5057. this.finishArrowValidation(node);
  5058. } else {
  5059. arrows.push(node);
  5060. }
  5061. stack.push(node.body);
  5062. } else if (node.type === "ConditionalExpression") {
  5063. stack.push(node.consequent);
  5064. stack.push(node.alternate);
  5065. }
  5066. }
  5067. if (disallowInvalid) {
  5068. arrows.forEach(node => this.finishArrowValidation(node));
  5069. return [arrows, []];
  5070. }
  5071. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5072. }
  5073. finishArrowValidation(node) {
  5074. var _node$extra;
  5075. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5076. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5077. super.checkParams(node, false, true);
  5078. this.scope.exit();
  5079. }
  5080. forwardNoArrowParamsConversionAt(node, parse) {
  5081. let result;
  5082. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5083. this.state.noArrowParamsConversionAt.push(this.state.start);
  5084. result = parse();
  5085. this.state.noArrowParamsConversionAt.pop();
  5086. } else {
  5087. result = parse();
  5088. }
  5089. return result;
  5090. }
  5091. parseParenItem(node, startPos, startLoc) {
  5092. node = super.parseParenItem(node, startPos, startLoc);
  5093. if (this.eat(17)) {
  5094. node.optional = true;
  5095. this.resetEndLocation(node);
  5096. }
  5097. if (this.match(14)) {
  5098. const typeCastNode = this.startNodeAt(startPos, startLoc);
  5099. typeCastNode.expression = node;
  5100. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5101. return this.finishNode(typeCastNode, "TypeCastExpression");
  5102. }
  5103. return node;
  5104. }
  5105. assertModuleNodeAllowed(node) {
  5106. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5107. return;
  5108. }
  5109. super.assertModuleNodeAllowed(node);
  5110. }
  5111. parseExport(node) {
  5112. const decl = super.parseExport(node);
  5113. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  5114. decl.exportKind = decl.exportKind || "value";
  5115. }
  5116. return decl;
  5117. }
  5118. parseExportDeclaration(node) {
  5119. if (this.isContextual(126)) {
  5120. node.exportKind = "type";
  5121. const declarationNode = this.startNode();
  5122. this.next();
  5123. if (this.match(5)) {
  5124. node.specifiers = this.parseExportSpecifiers(true);
  5125. this.parseExportFrom(node);
  5126. return null;
  5127. } else {
  5128. return this.flowParseTypeAlias(declarationNode);
  5129. }
  5130. } else if (this.isContextual(127)) {
  5131. node.exportKind = "type";
  5132. const declarationNode = this.startNode();
  5133. this.next();
  5134. return this.flowParseOpaqueType(declarationNode, false);
  5135. } else if (this.isContextual(125)) {
  5136. node.exportKind = "type";
  5137. const declarationNode = this.startNode();
  5138. this.next();
  5139. return this.flowParseInterface(declarationNode);
  5140. } else if (this.shouldParseEnums() && this.isContextual(122)) {
  5141. node.exportKind = "value";
  5142. const declarationNode = this.startNode();
  5143. this.next();
  5144. return this.flowParseEnumDeclaration(declarationNode);
  5145. } else {
  5146. return super.parseExportDeclaration(node);
  5147. }
  5148. }
  5149. eatExportStar(node) {
  5150. if (super.eatExportStar(...arguments)) return true;
  5151. if (this.isContextual(126) && this.lookahead().type === 55) {
  5152. node.exportKind = "type";
  5153. this.next();
  5154. this.next();
  5155. return true;
  5156. }
  5157. return false;
  5158. }
  5159. maybeParseExportNamespaceSpecifier(node) {
  5160. const {
  5161. startLoc
  5162. } = this.state;
  5163. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5164. if (hasNamespace && node.exportKind === "type") {
  5165. this.unexpected(startLoc);
  5166. }
  5167. return hasNamespace;
  5168. }
  5169. parseClassId(node, isStatement, optionalId) {
  5170. super.parseClassId(node, isStatement, optionalId);
  5171. if (this.match(47)) {
  5172. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5173. }
  5174. }
  5175. parseClassMember(classBody, member, state) {
  5176. const {
  5177. startLoc
  5178. } = this.state;
  5179. if (this.isContextual(121)) {
  5180. if (this.parseClassMemberFromModifier(classBody, member)) {
  5181. return;
  5182. }
  5183. member.declare = true;
  5184. }
  5185. super.parseClassMember(classBody, member, state);
  5186. if (member.declare) {
  5187. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5188. this.raise(FlowErrors.DeclareClassElement, {
  5189. at: startLoc
  5190. });
  5191. } else if (member.value) {
  5192. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5193. at: member.value
  5194. });
  5195. }
  5196. }
  5197. }
  5198. isIterator(word) {
  5199. return word === "iterator" || word === "asyncIterator";
  5200. }
  5201. readIterator() {
  5202. const word = super.readWord1();
  5203. const fullWord = "@@" + word;
  5204. if (!this.isIterator(word) || !this.state.inType) {
  5205. this.raise(Errors.InvalidIdentifier, {
  5206. at: this.state.curPosition(),
  5207. identifierName: fullWord
  5208. });
  5209. }
  5210. this.finishToken(128, fullWord);
  5211. }
  5212. getTokenFromCode(code) {
  5213. const next = this.input.charCodeAt(this.state.pos + 1);
  5214. if (code === 123 && next === 124) {
  5215. return this.finishOp(6, 2);
  5216. } else if (this.state.inType && (code === 62 || code === 60)) {
  5217. return this.finishOp(code === 62 ? 48 : 47, 1);
  5218. } else if (this.state.inType && code === 63) {
  5219. if (next === 46) {
  5220. return this.finishOp(18, 2);
  5221. }
  5222. return this.finishOp(17, 1);
  5223. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5224. this.state.pos += 2;
  5225. return this.readIterator();
  5226. } else {
  5227. return super.getTokenFromCode(code);
  5228. }
  5229. }
  5230. isAssignable(node, isBinding) {
  5231. if (node.type === "TypeCastExpression") {
  5232. return this.isAssignable(node.expression, isBinding);
  5233. } else {
  5234. return super.isAssignable(node, isBinding);
  5235. }
  5236. }
  5237. toAssignable(node, isLHS = false) {
  5238. if (node.type === "TypeCastExpression") {
  5239. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  5240. } else {
  5241. return super.toAssignable(node, isLHS);
  5242. }
  5243. }
  5244. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5245. for (let i = 0; i < exprList.length; i++) {
  5246. const expr = exprList[i];
  5247. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5248. exprList[i] = this.typeCastToParameter(expr);
  5249. }
  5250. }
  5251. return super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5252. }
  5253. toReferencedList(exprList, isParenthesizedExpr) {
  5254. for (let i = 0; i < exprList.length; i++) {
  5255. var _expr$extra;
  5256. const expr = exprList[i];
  5257. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5258. this.raise(FlowErrors.TypeCastInPattern, {
  5259. at: expr.typeAnnotation
  5260. });
  5261. }
  5262. }
  5263. return exprList;
  5264. }
  5265. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5266. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5267. if (canBePattern && !this.state.maybeInArrowParameters) {
  5268. this.toReferencedList(node.elements);
  5269. }
  5270. return node;
  5271. }
  5272. isValidLVal(type, ...rest) {
  5273. return type === "TypeCastExpression" || super.isValidLVal(type, ...rest);
  5274. }
  5275. parseClassProperty(node) {
  5276. if (this.match(14)) {
  5277. node.typeAnnotation = this.flowParseTypeAnnotation();
  5278. }
  5279. return super.parseClassProperty(node);
  5280. }
  5281. parseClassPrivateProperty(node) {
  5282. if (this.match(14)) {
  5283. node.typeAnnotation = this.flowParseTypeAnnotation();
  5284. }
  5285. return super.parseClassPrivateProperty(node);
  5286. }
  5287. isClassMethod() {
  5288. return this.match(47) || super.isClassMethod();
  5289. }
  5290. isClassProperty() {
  5291. return this.match(14) || super.isClassProperty();
  5292. }
  5293. isNonstaticConstructor(method) {
  5294. return !this.match(14) && super.isNonstaticConstructor(method);
  5295. }
  5296. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5297. if (method.variance) {
  5298. this.unexpected(method.variance.loc.start);
  5299. }
  5300. delete method.variance;
  5301. if (this.match(47)) {
  5302. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5303. }
  5304. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5305. if (method.params && isConstructor) {
  5306. const params = method.params;
  5307. if (params.length > 0 && this.isThisParam(params[0])) {
  5308. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5309. at: method
  5310. });
  5311. }
  5312. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5313. const params = method.value.params;
  5314. if (params.length > 0 && this.isThisParam(params[0])) {
  5315. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5316. at: method
  5317. });
  5318. }
  5319. }
  5320. }
  5321. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5322. if (method.variance) {
  5323. this.unexpected(method.variance.loc.start);
  5324. }
  5325. delete method.variance;
  5326. if (this.match(47)) {
  5327. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5328. }
  5329. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5330. }
  5331. parseClassSuper(node) {
  5332. super.parseClassSuper(node);
  5333. if (node.superClass && this.match(47)) {
  5334. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5335. }
  5336. if (this.isContextual(110)) {
  5337. this.next();
  5338. const implemented = node.implements = [];
  5339. do {
  5340. const node = this.startNode();
  5341. node.id = this.flowParseRestrictedIdentifier(true);
  5342. if (this.match(47)) {
  5343. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5344. } else {
  5345. node.typeParameters = null;
  5346. }
  5347. implemented.push(this.finishNode(node, "ClassImplements"));
  5348. } while (this.eat(12));
  5349. }
  5350. }
  5351. checkGetterSetterParams(method) {
  5352. super.checkGetterSetterParams(method);
  5353. const params = this.getObjectOrClassMethodParams(method);
  5354. if (params.length > 0) {
  5355. const param = params[0];
  5356. if (this.isThisParam(param) && method.kind === "get") {
  5357. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5358. at: param
  5359. });
  5360. } else if (this.isThisParam(param)) {
  5361. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5362. at: param
  5363. });
  5364. }
  5365. }
  5366. }
  5367. parsePropertyNamePrefixOperator(node) {
  5368. node.variance = this.flowParseVariance();
  5369. }
  5370. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5371. if (prop.variance) {
  5372. this.unexpected(prop.variance.loc.start);
  5373. }
  5374. delete prop.variance;
  5375. let typeParameters;
  5376. if (this.match(47) && !isAccessor) {
  5377. typeParameters = this.flowParseTypeParameterDeclaration();
  5378. if (!this.match(10)) this.unexpected();
  5379. }
  5380. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5381. if (typeParameters) {
  5382. (prop.value || prop).typeParameters = typeParameters;
  5383. }
  5384. }
  5385. parseAssignableListItemTypes(param) {
  5386. if (this.eat(17)) {
  5387. if (param.type !== "Identifier") {
  5388. this.raise(FlowErrors.PatternIsOptional, {
  5389. at: param
  5390. });
  5391. }
  5392. if (this.isThisParam(param)) {
  5393. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5394. at: param
  5395. });
  5396. }
  5397. param.optional = true;
  5398. }
  5399. if (this.match(14)) {
  5400. param.typeAnnotation = this.flowParseTypeAnnotation();
  5401. } else if (this.isThisParam(param)) {
  5402. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5403. at: param
  5404. });
  5405. }
  5406. if (this.match(29) && this.isThisParam(param)) {
  5407. this.raise(FlowErrors.ThisParamNoDefault, {
  5408. at: param
  5409. });
  5410. }
  5411. this.resetEndLocation(param);
  5412. return param;
  5413. }
  5414. parseMaybeDefault(startPos, startLoc, left) {
  5415. const node = super.parseMaybeDefault(startPos, startLoc, left);
  5416. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5417. this.raise(FlowErrors.TypeBeforeInitializer, {
  5418. at: node.typeAnnotation
  5419. });
  5420. }
  5421. return node;
  5422. }
  5423. shouldParseDefaultImport(node) {
  5424. if (!hasTypeImportKind(node)) {
  5425. return super.shouldParseDefaultImport(node);
  5426. }
  5427. return isMaybeDefaultImport(this.state.type);
  5428. }
  5429. parseImportSpecifierLocal(node, specifier, type) {
  5430. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5431. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5432. }
  5433. maybeParseDefaultImportSpecifier(node) {
  5434. node.importKind = "value";
  5435. let kind = null;
  5436. if (this.match(87)) {
  5437. kind = "typeof";
  5438. } else if (this.isContextual(126)) {
  5439. kind = "type";
  5440. }
  5441. if (kind) {
  5442. const lh = this.lookahead();
  5443. const {
  5444. type
  5445. } = lh;
  5446. if (kind === "type" && type === 55) {
  5447. this.unexpected(null, lh.type);
  5448. }
  5449. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5450. this.next();
  5451. node.importKind = kind;
  5452. }
  5453. }
  5454. return super.maybeParseDefaultImportSpecifier(node);
  5455. }
  5456. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  5457. const firstIdent = specifier.imported;
  5458. let specifierTypeKind = null;
  5459. if (firstIdent.type === "Identifier") {
  5460. if (firstIdent.name === "type") {
  5461. specifierTypeKind = "type";
  5462. } else if (firstIdent.name === "typeof") {
  5463. specifierTypeKind = "typeof";
  5464. }
  5465. }
  5466. let isBinding = false;
  5467. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5468. const as_ident = this.parseIdentifier(true);
  5469. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5470. specifier.imported = as_ident;
  5471. specifier.importKind = specifierTypeKind;
  5472. specifier.local = cloneIdentifier(as_ident);
  5473. } else {
  5474. specifier.imported = firstIdent;
  5475. specifier.importKind = null;
  5476. specifier.local = this.parseIdentifier();
  5477. }
  5478. } else {
  5479. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5480. specifier.imported = this.parseIdentifier(true);
  5481. specifier.importKind = specifierTypeKind;
  5482. } else {
  5483. if (importedIsString) {
  5484. throw this.raise(Errors.ImportBindingIsString, {
  5485. at: specifier,
  5486. importName: firstIdent.value
  5487. });
  5488. }
  5489. specifier.imported = firstIdent;
  5490. specifier.importKind = null;
  5491. }
  5492. if (this.eatContextual(93)) {
  5493. specifier.local = this.parseIdentifier();
  5494. } else {
  5495. isBinding = true;
  5496. specifier.local = cloneIdentifier(specifier.imported);
  5497. }
  5498. }
  5499. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5500. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5501. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5502. at: specifier
  5503. });
  5504. }
  5505. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5506. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5507. }
  5508. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5509. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5510. }
  5511. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5512. }
  5513. parseBindingAtom() {
  5514. switch (this.state.type) {
  5515. case 78:
  5516. return this.parseIdentifier(true);
  5517. default:
  5518. return super.parseBindingAtom();
  5519. }
  5520. }
  5521. parseFunctionParams(node, allowModifiers) {
  5522. const kind = node.kind;
  5523. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5524. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5525. }
  5526. super.parseFunctionParams(node, allowModifiers);
  5527. }
  5528. parseVarId(decl, kind) {
  5529. super.parseVarId(decl, kind);
  5530. if (this.match(14)) {
  5531. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5532. this.resetEndLocation(decl.id);
  5533. }
  5534. }
  5535. parseAsyncArrowFromCallExpression(node, call) {
  5536. if (this.match(14)) {
  5537. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5538. this.state.noAnonFunctionType = true;
  5539. node.returnType = this.flowParseTypeAnnotation();
  5540. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5541. }
  5542. return super.parseAsyncArrowFromCallExpression(node, call);
  5543. }
  5544. shouldParseAsyncArrow() {
  5545. return this.match(14) || super.shouldParseAsyncArrow();
  5546. }
  5547. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5548. var _jsx;
  5549. let state = null;
  5550. let jsx;
  5551. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  5552. state = this.state.clone();
  5553. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5554. if (!jsx.error) return jsx.node;
  5555. const {
  5556. context
  5557. } = this.state;
  5558. const currentContext = context[context.length - 1];
  5559. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5560. context.pop();
  5561. }
  5562. }
  5563. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5564. var _jsx2, _jsx3;
  5565. state = state || this.state.clone();
  5566. let typeParameters;
  5567. const arrow = this.tryParse(abort => {
  5568. var _arrowExpression$extr;
  5569. typeParameters = this.flowParseTypeParameterDeclaration();
  5570. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5571. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5572. this.resetStartLocationFromNode(result, typeParameters);
  5573. return result;
  5574. });
  5575. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5576. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5577. if (expr.type !== "ArrowFunctionExpression") abort();
  5578. expr.typeParameters = typeParameters;
  5579. this.resetStartLocationFromNode(expr, typeParameters);
  5580. return arrowExpression;
  5581. }, state);
  5582. let arrowExpression = null;
  5583. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5584. if (!arrow.error && !arrow.aborted) {
  5585. if (arrow.node.async) {
  5586. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5587. at: typeParameters
  5588. });
  5589. }
  5590. return arrow.node;
  5591. }
  5592. arrowExpression = arrow.node;
  5593. }
  5594. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5595. this.state = jsx.failState;
  5596. return jsx.node;
  5597. }
  5598. if (arrowExpression) {
  5599. this.state = arrow.failState;
  5600. return arrowExpression;
  5601. }
  5602. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5603. if (arrow.thrown) throw arrow.error;
  5604. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5605. at: typeParameters
  5606. });
  5607. }
  5608. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5609. }
  5610. parseArrow(node) {
  5611. if (this.match(14)) {
  5612. const result = this.tryParse(() => {
  5613. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5614. this.state.noAnonFunctionType = true;
  5615. const typeNode = this.startNode();
  5616. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5617. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5618. if (this.canInsertSemicolon()) this.unexpected();
  5619. if (!this.match(19)) this.unexpected();
  5620. return typeNode;
  5621. });
  5622. if (result.thrown) return null;
  5623. if (result.error) this.state = result.failState;
  5624. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5625. }
  5626. return super.parseArrow(node);
  5627. }
  5628. shouldParseArrow(params) {
  5629. return this.match(14) || super.shouldParseArrow(params);
  5630. }
  5631. setArrowFunctionParameters(node, params) {
  5632. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5633. node.params = params;
  5634. } else {
  5635. super.setArrowFunctionParameters(node, params);
  5636. }
  5637. }
  5638. checkParams(node, allowDuplicates, isArrowFunction) {
  5639. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5640. return;
  5641. }
  5642. for (let i = 0; i < node.params.length; i++) {
  5643. if (this.isThisParam(node.params[i]) && i > 0) {
  5644. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5645. at: node.params[i]
  5646. });
  5647. }
  5648. }
  5649. return super.checkParams(...arguments);
  5650. }
  5651. parseParenAndDistinguishExpression(canBeArrow) {
  5652. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5653. }
  5654. parseSubscripts(base, startPos, startLoc, noCalls) {
  5655. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  5656. this.next();
  5657. const node = this.startNodeAt(startPos, startLoc);
  5658. node.callee = base;
  5659. node.arguments = this.parseCallExpressionArguments(11, false);
  5660. base = this.finishNode(node, "CallExpression");
  5661. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5662. const state = this.state.clone();
  5663. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  5664. if (!arrow.error && !arrow.aborted) return arrow.node;
  5665. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  5666. if (result.node && !result.error) return result.node;
  5667. if (arrow.node) {
  5668. this.state = arrow.failState;
  5669. return arrow.node;
  5670. }
  5671. if (result.node) {
  5672. this.state = result.failState;
  5673. return result.node;
  5674. }
  5675. throw arrow.error || result.error;
  5676. }
  5677. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  5678. }
  5679. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  5680. if (this.match(18) && this.isLookaheadToken_lt()) {
  5681. subscriptState.optionalChainMember = true;
  5682. if (noCalls) {
  5683. subscriptState.stop = true;
  5684. return base;
  5685. }
  5686. this.next();
  5687. const node = this.startNodeAt(startPos, startLoc);
  5688. node.callee = base;
  5689. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5690. this.expect(10);
  5691. node.arguments = this.parseCallExpressionArguments(11, false);
  5692. node.optional = true;
  5693. return this.finishCallExpression(node, true);
  5694. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5695. const node = this.startNodeAt(startPos, startLoc);
  5696. node.callee = base;
  5697. const result = this.tryParse(() => {
  5698. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5699. this.expect(10);
  5700. node.arguments = this.parseCallExpressionArguments(11, false);
  5701. if (subscriptState.optionalChainMember) node.optional = false;
  5702. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5703. });
  5704. if (result.node) {
  5705. if (result.error) this.state = result.failState;
  5706. return result.node;
  5707. }
  5708. }
  5709. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  5710. }
  5711. parseNewArguments(node) {
  5712. let targs = null;
  5713. if (this.shouldParseTypes() && this.match(47)) {
  5714. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5715. }
  5716. node.typeArguments = targs;
  5717. super.parseNewArguments(node);
  5718. }
  5719. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  5720. const node = this.startNodeAt(startPos, startLoc);
  5721. this.parseFunctionParams(node);
  5722. if (!this.parseArrow(node)) return;
  5723. return this.parseArrowExpression(node, undefined, true);
  5724. }
  5725. readToken_mult_modulo(code) {
  5726. const next = this.input.charCodeAt(this.state.pos + 1);
  5727. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5728. this.state.hasFlowComment = false;
  5729. this.state.pos += 2;
  5730. this.nextToken();
  5731. return;
  5732. }
  5733. super.readToken_mult_modulo(code);
  5734. }
  5735. readToken_pipe_amp(code) {
  5736. const next = this.input.charCodeAt(this.state.pos + 1);
  5737. if (code === 124 && next === 125) {
  5738. this.finishOp(9, 2);
  5739. return;
  5740. }
  5741. super.readToken_pipe_amp(code);
  5742. }
  5743. parseTopLevel(file, program) {
  5744. const fileNode = super.parseTopLevel(file, program);
  5745. if (this.state.hasFlowComment) {
  5746. this.raise(FlowErrors.UnterminatedFlowComment, {
  5747. at: this.state.curPosition()
  5748. });
  5749. }
  5750. return fileNode;
  5751. }
  5752. skipBlockComment() {
  5753. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5754. if (this.state.hasFlowComment) {
  5755. throw this.raise(FlowErrors.NestedFlowComment, {
  5756. at: this.state.startLoc
  5757. });
  5758. }
  5759. this.hasFlowCommentCompletion();
  5760. this.state.pos += this.skipFlowComment();
  5761. this.state.hasFlowComment = true;
  5762. return;
  5763. }
  5764. if (this.state.hasFlowComment) {
  5765. const end = this.input.indexOf("*-/", this.state.pos + 2);
  5766. if (end === -1) {
  5767. throw this.raise(Errors.UnterminatedComment, {
  5768. at: this.state.curPosition()
  5769. });
  5770. }
  5771. this.state.pos = end + 2 + 3;
  5772. return;
  5773. }
  5774. return super.skipBlockComment();
  5775. }
  5776. skipFlowComment() {
  5777. const {
  5778. pos
  5779. } = this.state;
  5780. let shiftToFirstNonWhiteSpace = 2;
  5781. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5782. shiftToFirstNonWhiteSpace++;
  5783. }
  5784. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5785. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5786. if (ch2 === 58 && ch3 === 58) {
  5787. return shiftToFirstNonWhiteSpace + 2;
  5788. }
  5789. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5790. return shiftToFirstNonWhiteSpace + 12;
  5791. }
  5792. if (ch2 === 58 && ch3 !== 58) {
  5793. return shiftToFirstNonWhiteSpace;
  5794. }
  5795. return false;
  5796. }
  5797. hasFlowCommentCompletion() {
  5798. const end = this.input.indexOf("*/", this.state.pos);
  5799. if (end === -1) {
  5800. throw this.raise(Errors.UnterminatedComment, {
  5801. at: this.state.curPosition()
  5802. });
  5803. }
  5804. }
  5805. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5806. enumName,
  5807. memberName
  5808. }) {
  5809. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5810. at: loc,
  5811. memberName,
  5812. enumName
  5813. });
  5814. }
  5815. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5816. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  5817. at: loc
  5818. }, enumContext));
  5819. }
  5820. flowEnumErrorNumberMemberNotInitialized(loc, {
  5821. enumName,
  5822. memberName
  5823. }) {
  5824. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  5825. at: loc,
  5826. enumName,
  5827. memberName
  5828. });
  5829. }
  5830. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  5831. enumName
  5832. }) {
  5833. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  5834. at: node,
  5835. enumName
  5836. });
  5837. }
  5838. flowEnumMemberInit() {
  5839. const startLoc = this.state.startLoc;
  5840. const endOfInit = () => this.match(12) || this.match(8);
  5841. switch (this.state.type) {
  5842. case 130:
  5843. {
  5844. const literal = this.parseNumericLiteral(this.state.value);
  5845. if (endOfInit()) {
  5846. return {
  5847. type: "number",
  5848. loc: literal.loc.start,
  5849. value: literal
  5850. };
  5851. }
  5852. return {
  5853. type: "invalid",
  5854. loc: startLoc
  5855. };
  5856. }
  5857. case 129:
  5858. {
  5859. const literal = this.parseStringLiteral(this.state.value);
  5860. if (endOfInit()) {
  5861. return {
  5862. type: "string",
  5863. loc: literal.loc.start,
  5864. value: literal
  5865. };
  5866. }
  5867. return {
  5868. type: "invalid",
  5869. loc: startLoc
  5870. };
  5871. }
  5872. case 85:
  5873. case 86:
  5874. {
  5875. const literal = this.parseBooleanLiteral(this.match(85));
  5876. if (endOfInit()) {
  5877. return {
  5878. type: "boolean",
  5879. loc: literal.loc.start,
  5880. value: literal
  5881. };
  5882. }
  5883. return {
  5884. type: "invalid",
  5885. loc: startLoc
  5886. };
  5887. }
  5888. default:
  5889. return {
  5890. type: "invalid",
  5891. loc: startLoc
  5892. };
  5893. }
  5894. }
  5895. flowEnumMemberRaw() {
  5896. const loc = this.state.startLoc;
  5897. const id = this.parseIdentifier(true);
  5898. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  5899. type: "none",
  5900. loc
  5901. };
  5902. return {
  5903. id,
  5904. init
  5905. };
  5906. }
  5907. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  5908. const {
  5909. explicitType
  5910. } = context;
  5911. if (explicitType === null) {
  5912. return;
  5913. }
  5914. if (explicitType !== expectedType) {
  5915. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  5916. }
  5917. }
  5918. flowEnumMembers({
  5919. enumName,
  5920. explicitType
  5921. }) {
  5922. const seenNames = new Set();
  5923. const members = {
  5924. booleanMembers: [],
  5925. numberMembers: [],
  5926. stringMembers: [],
  5927. defaultedMembers: []
  5928. };
  5929. let hasUnknownMembers = false;
  5930. while (!this.match(8)) {
  5931. if (this.eat(21)) {
  5932. hasUnknownMembers = true;
  5933. break;
  5934. }
  5935. const memberNode = this.startNode();
  5936. const {
  5937. id,
  5938. init
  5939. } = this.flowEnumMemberRaw();
  5940. const memberName = id.name;
  5941. if (memberName === "") {
  5942. continue;
  5943. }
  5944. if (/^[a-z]/.test(memberName)) {
  5945. this.raise(FlowErrors.EnumInvalidMemberName, {
  5946. at: id,
  5947. memberName,
  5948. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  5949. enumName
  5950. });
  5951. }
  5952. if (seenNames.has(memberName)) {
  5953. this.raise(FlowErrors.EnumDuplicateMemberName, {
  5954. at: id,
  5955. memberName,
  5956. enumName
  5957. });
  5958. }
  5959. seenNames.add(memberName);
  5960. const context = {
  5961. enumName,
  5962. explicitType,
  5963. memberName
  5964. };
  5965. memberNode.id = id;
  5966. switch (init.type) {
  5967. case "boolean":
  5968. {
  5969. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  5970. memberNode.init = init.value;
  5971. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  5972. break;
  5973. }
  5974. case "number":
  5975. {
  5976. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  5977. memberNode.init = init.value;
  5978. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  5979. break;
  5980. }
  5981. case "string":
  5982. {
  5983. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  5984. memberNode.init = init.value;
  5985. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  5986. break;
  5987. }
  5988. case "invalid":
  5989. {
  5990. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  5991. }
  5992. case "none":
  5993. {
  5994. switch (explicitType) {
  5995. case "boolean":
  5996. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  5997. break;
  5998. case "number":
  5999. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6000. break;
  6001. default:
  6002. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6003. }
  6004. }
  6005. }
  6006. if (!this.match(8)) {
  6007. this.expect(12);
  6008. }
  6009. }
  6010. return {
  6011. members,
  6012. hasUnknownMembers
  6013. };
  6014. }
  6015. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6016. enumName
  6017. }) {
  6018. if (initializedMembers.length === 0) {
  6019. return defaultedMembers;
  6020. } else if (defaultedMembers.length === 0) {
  6021. return initializedMembers;
  6022. } else if (defaultedMembers.length > initializedMembers.length) {
  6023. for (const member of initializedMembers) {
  6024. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6025. enumName
  6026. });
  6027. }
  6028. return defaultedMembers;
  6029. } else {
  6030. for (const member of defaultedMembers) {
  6031. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6032. enumName
  6033. });
  6034. }
  6035. return initializedMembers;
  6036. }
  6037. }
  6038. flowEnumParseExplicitType({
  6039. enumName
  6040. }) {
  6041. if (!this.eatContextual(101)) return null;
  6042. if (!tokenIsIdentifier(this.state.type)) {
  6043. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6044. at: this.state.startLoc,
  6045. enumName
  6046. });
  6047. }
  6048. const {
  6049. value
  6050. } = this.state;
  6051. this.next();
  6052. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6053. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6054. at: this.state.startLoc,
  6055. enumName,
  6056. invalidEnumType: value
  6057. });
  6058. }
  6059. return value;
  6060. }
  6061. flowEnumBody(node, id) {
  6062. const enumName = id.name;
  6063. const nameLoc = id.loc.start;
  6064. const explicitType = this.flowEnumParseExplicitType({
  6065. enumName
  6066. });
  6067. this.expect(5);
  6068. const {
  6069. members,
  6070. hasUnknownMembers
  6071. } = this.flowEnumMembers({
  6072. enumName,
  6073. explicitType
  6074. });
  6075. node.hasUnknownMembers = hasUnknownMembers;
  6076. switch (explicitType) {
  6077. case "boolean":
  6078. node.explicitType = true;
  6079. node.members = members.booleanMembers;
  6080. this.expect(8);
  6081. return this.finishNode(node, "EnumBooleanBody");
  6082. case "number":
  6083. node.explicitType = true;
  6084. node.members = members.numberMembers;
  6085. this.expect(8);
  6086. return this.finishNode(node, "EnumNumberBody");
  6087. case "string":
  6088. node.explicitType = true;
  6089. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6090. enumName
  6091. });
  6092. this.expect(8);
  6093. return this.finishNode(node, "EnumStringBody");
  6094. case "symbol":
  6095. node.members = members.defaultedMembers;
  6096. this.expect(8);
  6097. return this.finishNode(node, "EnumSymbolBody");
  6098. default:
  6099. {
  6100. const empty = () => {
  6101. node.members = [];
  6102. this.expect(8);
  6103. return this.finishNode(node, "EnumStringBody");
  6104. };
  6105. node.explicitType = false;
  6106. const boolsLen = members.booleanMembers.length;
  6107. const numsLen = members.numberMembers.length;
  6108. const strsLen = members.stringMembers.length;
  6109. const defaultedLen = members.defaultedMembers.length;
  6110. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6111. return empty();
  6112. } else if (!boolsLen && !numsLen) {
  6113. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6114. enumName
  6115. });
  6116. this.expect(8);
  6117. return this.finishNode(node, "EnumStringBody");
  6118. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6119. for (const member of members.defaultedMembers) {
  6120. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6121. enumName,
  6122. memberName: member.id.name
  6123. });
  6124. }
  6125. node.members = members.booleanMembers;
  6126. this.expect(8);
  6127. return this.finishNode(node, "EnumBooleanBody");
  6128. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6129. for (const member of members.defaultedMembers) {
  6130. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6131. enumName,
  6132. memberName: member.id.name
  6133. });
  6134. }
  6135. node.members = members.numberMembers;
  6136. this.expect(8);
  6137. return this.finishNode(node, "EnumNumberBody");
  6138. } else {
  6139. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6140. at: nameLoc,
  6141. enumName
  6142. });
  6143. return empty();
  6144. }
  6145. }
  6146. }
  6147. }
  6148. flowParseEnumDeclaration(node) {
  6149. const id = this.parseIdentifier();
  6150. node.id = id;
  6151. node.body = this.flowEnumBody(this.startNode(), id);
  6152. return this.finishNode(node, "EnumDeclaration");
  6153. }
  6154. isLookaheadToken_lt() {
  6155. const next = this.nextTokenStart();
  6156. if (this.input.charCodeAt(next) === 60) {
  6157. const afterNext = this.input.charCodeAt(next + 1);
  6158. return afterNext !== 60 && afterNext !== 61;
  6159. }
  6160. return false;
  6161. }
  6162. maybeUnwrapTypeCastExpression(node) {
  6163. return node.type === "TypeCastExpression" ? node.expression : node;
  6164. }
  6165. });
  6166. const entities = {
  6167. __proto__: null,
  6168. quot: "\u0022",
  6169. amp: "&",
  6170. apos: "\u0027",
  6171. lt: "<",
  6172. gt: ">",
  6173. nbsp: "\u00A0",
  6174. iexcl: "\u00A1",
  6175. cent: "\u00A2",
  6176. pound: "\u00A3",
  6177. curren: "\u00A4",
  6178. yen: "\u00A5",
  6179. brvbar: "\u00A6",
  6180. sect: "\u00A7",
  6181. uml: "\u00A8",
  6182. copy: "\u00A9",
  6183. ordf: "\u00AA",
  6184. laquo: "\u00AB",
  6185. not: "\u00AC",
  6186. shy: "\u00AD",
  6187. reg: "\u00AE",
  6188. macr: "\u00AF",
  6189. deg: "\u00B0",
  6190. plusmn: "\u00B1",
  6191. sup2: "\u00B2",
  6192. sup3: "\u00B3",
  6193. acute: "\u00B4",
  6194. micro: "\u00B5",
  6195. para: "\u00B6",
  6196. middot: "\u00B7",
  6197. cedil: "\u00B8",
  6198. sup1: "\u00B9",
  6199. ordm: "\u00BA",
  6200. raquo: "\u00BB",
  6201. frac14: "\u00BC",
  6202. frac12: "\u00BD",
  6203. frac34: "\u00BE",
  6204. iquest: "\u00BF",
  6205. Agrave: "\u00C0",
  6206. Aacute: "\u00C1",
  6207. Acirc: "\u00C2",
  6208. Atilde: "\u00C3",
  6209. Auml: "\u00C4",
  6210. Aring: "\u00C5",
  6211. AElig: "\u00C6",
  6212. Ccedil: "\u00C7",
  6213. Egrave: "\u00C8",
  6214. Eacute: "\u00C9",
  6215. Ecirc: "\u00CA",
  6216. Euml: "\u00CB",
  6217. Igrave: "\u00CC",
  6218. Iacute: "\u00CD",
  6219. Icirc: "\u00CE",
  6220. Iuml: "\u00CF",
  6221. ETH: "\u00D0",
  6222. Ntilde: "\u00D1",
  6223. Ograve: "\u00D2",
  6224. Oacute: "\u00D3",
  6225. Ocirc: "\u00D4",
  6226. Otilde: "\u00D5",
  6227. Ouml: "\u00D6",
  6228. times: "\u00D7",
  6229. Oslash: "\u00D8",
  6230. Ugrave: "\u00D9",
  6231. Uacute: "\u00DA",
  6232. Ucirc: "\u00DB",
  6233. Uuml: "\u00DC",
  6234. Yacute: "\u00DD",
  6235. THORN: "\u00DE",
  6236. szlig: "\u00DF",
  6237. agrave: "\u00E0",
  6238. aacute: "\u00E1",
  6239. acirc: "\u00E2",
  6240. atilde: "\u00E3",
  6241. auml: "\u00E4",
  6242. aring: "\u00E5",
  6243. aelig: "\u00E6",
  6244. ccedil: "\u00E7",
  6245. egrave: "\u00E8",
  6246. eacute: "\u00E9",
  6247. ecirc: "\u00EA",
  6248. euml: "\u00EB",
  6249. igrave: "\u00EC",
  6250. iacute: "\u00ED",
  6251. icirc: "\u00EE",
  6252. iuml: "\u00EF",
  6253. eth: "\u00F0",
  6254. ntilde: "\u00F1",
  6255. ograve: "\u00F2",
  6256. oacute: "\u00F3",
  6257. ocirc: "\u00F4",
  6258. otilde: "\u00F5",
  6259. ouml: "\u00F6",
  6260. divide: "\u00F7",
  6261. oslash: "\u00F8",
  6262. ugrave: "\u00F9",
  6263. uacute: "\u00FA",
  6264. ucirc: "\u00FB",
  6265. uuml: "\u00FC",
  6266. yacute: "\u00FD",
  6267. thorn: "\u00FE",
  6268. yuml: "\u00FF",
  6269. OElig: "\u0152",
  6270. oelig: "\u0153",
  6271. Scaron: "\u0160",
  6272. scaron: "\u0161",
  6273. Yuml: "\u0178",
  6274. fnof: "\u0192",
  6275. circ: "\u02C6",
  6276. tilde: "\u02DC",
  6277. Alpha: "\u0391",
  6278. Beta: "\u0392",
  6279. Gamma: "\u0393",
  6280. Delta: "\u0394",
  6281. Epsilon: "\u0395",
  6282. Zeta: "\u0396",
  6283. Eta: "\u0397",
  6284. Theta: "\u0398",
  6285. Iota: "\u0399",
  6286. Kappa: "\u039A",
  6287. Lambda: "\u039B",
  6288. Mu: "\u039C",
  6289. Nu: "\u039D",
  6290. Xi: "\u039E",
  6291. Omicron: "\u039F",
  6292. Pi: "\u03A0",
  6293. Rho: "\u03A1",
  6294. Sigma: "\u03A3",
  6295. Tau: "\u03A4",
  6296. Upsilon: "\u03A5",
  6297. Phi: "\u03A6",
  6298. Chi: "\u03A7",
  6299. Psi: "\u03A8",
  6300. Omega: "\u03A9",
  6301. alpha: "\u03B1",
  6302. beta: "\u03B2",
  6303. gamma: "\u03B3",
  6304. delta: "\u03B4",
  6305. epsilon: "\u03B5",
  6306. zeta: "\u03B6",
  6307. eta: "\u03B7",
  6308. theta: "\u03B8",
  6309. iota: "\u03B9",
  6310. kappa: "\u03BA",
  6311. lambda: "\u03BB",
  6312. mu: "\u03BC",
  6313. nu: "\u03BD",
  6314. xi: "\u03BE",
  6315. omicron: "\u03BF",
  6316. pi: "\u03C0",
  6317. rho: "\u03C1",
  6318. sigmaf: "\u03C2",
  6319. sigma: "\u03C3",
  6320. tau: "\u03C4",
  6321. upsilon: "\u03C5",
  6322. phi: "\u03C6",
  6323. chi: "\u03C7",
  6324. psi: "\u03C8",
  6325. omega: "\u03C9",
  6326. thetasym: "\u03D1",
  6327. upsih: "\u03D2",
  6328. piv: "\u03D6",
  6329. ensp: "\u2002",
  6330. emsp: "\u2003",
  6331. thinsp: "\u2009",
  6332. zwnj: "\u200C",
  6333. zwj: "\u200D",
  6334. lrm: "\u200E",
  6335. rlm: "\u200F",
  6336. ndash: "\u2013",
  6337. mdash: "\u2014",
  6338. lsquo: "\u2018",
  6339. rsquo: "\u2019",
  6340. sbquo: "\u201A",
  6341. ldquo: "\u201C",
  6342. rdquo: "\u201D",
  6343. bdquo: "\u201E",
  6344. dagger: "\u2020",
  6345. Dagger: "\u2021",
  6346. bull: "\u2022",
  6347. hellip: "\u2026",
  6348. permil: "\u2030",
  6349. prime: "\u2032",
  6350. Prime: "\u2033",
  6351. lsaquo: "\u2039",
  6352. rsaquo: "\u203A",
  6353. oline: "\u203E",
  6354. frasl: "\u2044",
  6355. euro: "\u20AC",
  6356. image: "\u2111",
  6357. weierp: "\u2118",
  6358. real: "\u211C",
  6359. trade: "\u2122",
  6360. alefsym: "\u2135",
  6361. larr: "\u2190",
  6362. uarr: "\u2191",
  6363. rarr: "\u2192",
  6364. darr: "\u2193",
  6365. harr: "\u2194",
  6366. crarr: "\u21B5",
  6367. lArr: "\u21D0",
  6368. uArr: "\u21D1",
  6369. rArr: "\u21D2",
  6370. dArr: "\u21D3",
  6371. hArr: "\u21D4",
  6372. forall: "\u2200",
  6373. part: "\u2202",
  6374. exist: "\u2203",
  6375. empty: "\u2205",
  6376. nabla: "\u2207",
  6377. isin: "\u2208",
  6378. notin: "\u2209",
  6379. ni: "\u220B",
  6380. prod: "\u220F",
  6381. sum: "\u2211",
  6382. minus: "\u2212",
  6383. lowast: "\u2217",
  6384. radic: "\u221A",
  6385. prop: "\u221D",
  6386. infin: "\u221E",
  6387. ang: "\u2220",
  6388. and: "\u2227",
  6389. or: "\u2228",
  6390. cap: "\u2229",
  6391. cup: "\u222A",
  6392. int: "\u222B",
  6393. there4: "\u2234",
  6394. sim: "\u223C",
  6395. cong: "\u2245",
  6396. asymp: "\u2248",
  6397. ne: "\u2260",
  6398. equiv: "\u2261",
  6399. le: "\u2264",
  6400. ge: "\u2265",
  6401. sub: "\u2282",
  6402. sup: "\u2283",
  6403. nsub: "\u2284",
  6404. sube: "\u2286",
  6405. supe: "\u2287",
  6406. oplus: "\u2295",
  6407. otimes: "\u2297",
  6408. perp: "\u22A5",
  6409. sdot: "\u22C5",
  6410. lceil: "\u2308",
  6411. rceil: "\u2309",
  6412. lfloor: "\u230A",
  6413. rfloor: "\u230B",
  6414. lang: "\u2329",
  6415. rang: "\u232A",
  6416. loz: "\u25CA",
  6417. spades: "\u2660",
  6418. clubs: "\u2663",
  6419. hearts: "\u2665",
  6420. diams: "\u2666"
  6421. };
  6422. const JsxErrors = ParseErrorEnum`jsx`(_ => ({
  6423. AttributeIsEmpty: _("JSX attributes must only be assigned a non-empty expression."),
  6424. MissingClosingTagElement: _(({
  6425. openingTagName
  6426. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`),
  6427. MissingClosingTagFragment: _("Expected corresponding JSX closing tag for <>."),
  6428. UnexpectedSequenceExpression: _("Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?"),
  6429. UnexpectedToken: _(({
  6430. unexpected,
  6431. HTMLEntity
  6432. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`),
  6433. UnsupportedJsxValue: _("JSX value should be either an expression or a quoted JSX text."),
  6434. UnterminatedJsxContent: _("Unterminated JSX contents."),
  6435. UnwrappedAdjacentJSXElements: _("Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?")
  6436. }));
  6437. function isFragment(object) {
  6438. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6439. }
  6440. function getQualifiedJSXName(object) {
  6441. if (object.type === "JSXIdentifier") {
  6442. return object.name;
  6443. }
  6444. if (object.type === "JSXNamespacedName") {
  6445. return object.namespace.name + ":" + object.name.name;
  6446. }
  6447. if (object.type === "JSXMemberExpression") {
  6448. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6449. }
  6450. throw new Error("Node had unexpected type: " + object.type);
  6451. }
  6452. var jsx = (superClass => class extends superClass {
  6453. jsxReadToken() {
  6454. let out = "";
  6455. let chunkStart = this.state.pos;
  6456. for (;;) {
  6457. if (this.state.pos >= this.length) {
  6458. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6459. at: this.state.startLoc
  6460. });
  6461. }
  6462. const ch = this.input.charCodeAt(this.state.pos);
  6463. switch (ch) {
  6464. case 60:
  6465. case 123:
  6466. if (this.state.pos === this.state.start) {
  6467. if (ch === 60 && this.state.canStartJSXElement) {
  6468. ++this.state.pos;
  6469. return this.finishToken(138);
  6470. }
  6471. return super.getTokenFromCode(ch);
  6472. }
  6473. out += this.input.slice(chunkStart, this.state.pos);
  6474. return this.finishToken(137, out);
  6475. case 38:
  6476. out += this.input.slice(chunkStart, this.state.pos);
  6477. out += this.jsxReadEntity();
  6478. chunkStart = this.state.pos;
  6479. break;
  6480. case 62:
  6481. case 125:
  6482. default:
  6483. if (isNewLine(ch)) {
  6484. out += this.input.slice(chunkStart, this.state.pos);
  6485. out += this.jsxReadNewLine(true);
  6486. chunkStart = this.state.pos;
  6487. } else {
  6488. ++this.state.pos;
  6489. }
  6490. }
  6491. }
  6492. }
  6493. jsxReadNewLine(normalizeCRLF) {
  6494. const ch = this.input.charCodeAt(this.state.pos);
  6495. let out;
  6496. ++this.state.pos;
  6497. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6498. ++this.state.pos;
  6499. out = normalizeCRLF ? "\n" : "\r\n";
  6500. } else {
  6501. out = String.fromCharCode(ch);
  6502. }
  6503. ++this.state.curLine;
  6504. this.state.lineStart = this.state.pos;
  6505. return out;
  6506. }
  6507. jsxReadString(quote) {
  6508. let out = "";
  6509. let chunkStart = ++this.state.pos;
  6510. for (;;) {
  6511. if (this.state.pos >= this.length) {
  6512. throw this.raise(Errors.UnterminatedString, {
  6513. at: this.state.startLoc
  6514. });
  6515. }
  6516. const ch = this.input.charCodeAt(this.state.pos);
  6517. if (ch === quote) break;
  6518. if (ch === 38) {
  6519. out += this.input.slice(chunkStart, this.state.pos);
  6520. out += this.jsxReadEntity();
  6521. chunkStart = this.state.pos;
  6522. } else if (isNewLine(ch)) {
  6523. out += this.input.slice(chunkStart, this.state.pos);
  6524. out += this.jsxReadNewLine(false);
  6525. chunkStart = this.state.pos;
  6526. } else {
  6527. ++this.state.pos;
  6528. }
  6529. }
  6530. out += this.input.slice(chunkStart, this.state.pos++);
  6531. return this.finishToken(129, out);
  6532. }
  6533. jsxReadEntity() {
  6534. const startPos = ++this.state.pos;
  6535. if (this.codePointAtPos(this.state.pos) === 35) {
  6536. ++this.state.pos;
  6537. let radix = 10;
  6538. if (this.codePointAtPos(this.state.pos) === 120) {
  6539. radix = 16;
  6540. ++this.state.pos;
  6541. }
  6542. const codePoint = this.readInt(radix, undefined, false, "bail");
  6543. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6544. ++this.state.pos;
  6545. return String.fromCodePoint(codePoint);
  6546. }
  6547. } else {
  6548. let count = 0;
  6549. let semi = false;
  6550. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6551. ++this.state.pos;
  6552. }
  6553. if (semi) {
  6554. const desc = this.input.slice(startPos, this.state.pos);
  6555. const entity = entities[desc];
  6556. ++this.state.pos;
  6557. if (entity) {
  6558. return entity;
  6559. }
  6560. }
  6561. }
  6562. this.state.pos = startPos;
  6563. return "&";
  6564. }
  6565. jsxReadWord() {
  6566. let ch;
  6567. const start = this.state.pos;
  6568. do {
  6569. ch = this.input.charCodeAt(++this.state.pos);
  6570. } while (isIdentifierChar(ch) || ch === 45);
  6571. return this.finishToken(136, this.input.slice(start, this.state.pos));
  6572. }
  6573. jsxParseIdentifier() {
  6574. const node = this.startNode();
  6575. if (this.match(136)) {
  6576. node.name = this.state.value;
  6577. } else if (tokenIsKeyword(this.state.type)) {
  6578. node.name = tokenLabelName(this.state.type);
  6579. } else {
  6580. this.unexpected();
  6581. }
  6582. this.next();
  6583. return this.finishNode(node, "JSXIdentifier");
  6584. }
  6585. jsxParseNamespacedName() {
  6586. const startPos = this.state.start;
  6587. const startLoc = this.state.startLoc;
  6588. const name = this.jsxParseIdentifier();
  6589. if (!this.eat(14)) return name;
  6590. const node = this.startNodeAt(startPos, startLoc);
  6591. node.namespace = name;
  6592. node.name = this.jsxParseIdentifier();
  6593. return this.finishNode(node, "JSXNamespacedName");
  6594. }
  6595. jsxParseElementName() {
  6596. const startPos = this.state.start;
  6597. const startLoc = this.state.startLoc;
  6598. let node = this.jsxParseNamespacedName();
  6599. if (node.type === "JSXNamespacedName") {
  6600. return node;
  6601. }
  6602. while (this.eat(16)) {
  6603. const newNode = this.startNodeAt(startPos, startLoc);
  6604. newNode.object = node;
  6605. newNode.property = this.jsxParseIdentifier();
  6606. node = this.finishNode(newNode, "JSXMemberExpression");
  6607. }
  6608. return node;
  6609. }
  6610. jsxParseAttributeValue() {
  6611. let node;
  6612. switch (this.state.type) {
  6613. case 5:
  6614. node = this.startNode();
  6615. this.setContext(types.brace);
  6616. this.next();
  6617. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6618. if (node.expression.type === "JSXEmptyExpression") {
  6619. this.raise(JsxErrors.AttributeIsEmpty, {
  6620. at: node
  6621. });
  6622. }
  6623. return node;
  6624. case 138:
  6625. case 129:
  6626. return this.parseExprAtom();
  6627. default:
  6628. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6629. at: this.state.startLoc
  6630. });
  6631. }
  6632. }
  6633. jsxParseEmptyExpression() {
  6634. const node = this.startNodeAt(this.state.lastTokEndLoc.index, this.state.lastTokEndLoc);
  6635. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6636. }
  6637. jsxParseSpreadChild(node) {
  6638. this.next();
  6639. node.expression = this.parseExpression();
  6640. this.setContext(types.j_oTag);
  6641. this.expect(8);
  6642. return this.finishNode(node, "JSXSpreadChild");
  6643. }
  6644. jsxParseExpressionContainer(node, previousContext) {
  6645. if (this.match(8)) {
  6646. node.expression = this.jsxParseEmptyExpression();
  6647. } else {
  6648. const expression = this.parseExpression();
  6649. node.expression = expression;
  6650. }
  6651. this.setContext(previousContext);
  6652. this.expect(8);
  6653. return this.finishNode(node, "JSXExpressionContainer");
  6654. }
  6655. jsxParseAttribute() {
  6656. const node = this.startNode();
  6657. if (this.match(5)) {
  6658. this.setContext(types.brace);
  6659. this.next();
  6660. this.expect(21);
  6661. node.argument = this.parseMaybeAssignAllowIn();
  6662. this.setContext(types.j_oTag);
  6663. this.expect(8);
  6664. return this.finishNode(node, "JSXSpreadAttribute");
  6665. }
  6666. node.name = this.jsxParseNamespacedName();
  6667. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6668. return this.finishNode(node, "JSXAttribute");
  6669. }
  6670. jsxParseOpeningElementAt(startPos, startLoc) {
  6671. const node = this.startNodeAt(startPos, startLoc);
  6672. if (this.match(139)) {
  6673. this.expect(139);
  6674. return this.finishNode(node, "JSXOpeningFragment");
  6675. }
  6676. node.name = this.jsxParseElementName();
  6677. return this.jsxParseOpeningElementAfterName(node);
  6678. }
  6679. jsxParseOpeningElementAfterName(node) {
  6680. const attributes = [];
  6681. while (!this.match(56) && !this.match(139)) {
  6682. attributes.push(this.jsxParseAttribute());
  6683. }
  6684. node.attributes = attributes;
  6685. node.selfClosing = this.eat(56);
  6686. this.expect(139);
  6687. return this.finishNode(node, "JSXOpeningElement");
  6688. }
  6689. jsxParseClosingElementAt(startPos, startLoc) {
  6690. const node = this.startNodeAt(startPos, startLoc);
  6691. if (this.match(139)) {
  6692. this.expect(139);
  6693. return this.finishNode(node, "JSXClosingFragment");
  6694. }
  6695. node.name = this.jsxParseElementName();
  6696. this.expect(139);
  6697. return this.finishNode(node, "JSXClosingElement");
  6698. }
  6699. jsxParseElementAt(startPos, startLoc) {
  6700. const node = this.startNodeAt(startPos, startLoc);
  6701. const children = [];
  6702. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  6703. let closingElement = null;
  6704. if (!openingElement.selfClosing) {
  6705. contents: for (;;) {
  6706. switch (this.state.type) {
  6707. case 138:
  6708. startPos = this.state.start;
  6709. startLoc = this.state.startLoc;
  6710. this.next();
  6711. if (this.eat(56)) {
  6712. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  6713. break contents;
  6714. }
  6715. children.push(this.jsxParseElementAt(startPos, startLoc));
  6716. break;
  6717. case 137:
  6718. children.push(this.parseExprAtom());
  6719. break;
  6720. case 5:
  6721. {
  6722. const node = this.startNode();
  6723. this.setContext(types.brace);
  6724. this.next();
  6725. if (this.match(21)) {
  6726. children.push(this.jsxParseSpreadChild(node));
  6727. } else {
  6728. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6729. }
  6730. break;
  6731. }
  6732. default:
  6733. throw this.unexpected();
  6734. }
  6735. }
  6736. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6737. this.raise(JsxErrors.MissingClosingTagFragment, {
  6738. at: closingElement
  6739. });
  6740. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6741. this.raise(JsxErrors.MissingClosingTagElement, {
  6742. at: closingElement,
  6743. openingTagName: getQualifiedJSXName(openingElement.name)
  6744. });
  6745. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6746. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6747. this.raise(JsxErrors.MissingClosingTagElement, {
  6748. at: closingElement,
  6749. openingTagName: getQualifiedJSXName(openingElement.name)
  6750. });
  6751. }
  6752. }
  6753. }
  6754. if (isFragment(openingElement)) {
  6755. node.openingFragment = openingElement;
  6756. node.closingFragment = closingElement;
  6757. } else {
  6758. node.openingElement = openingElement;
  6759. node.closingElement = closingElement;
  6760. }
  6761. node.children = children;
  6762. if (this.match(47)) {
  6763. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6764. at: this.state.startLoc
  6765. });
  6766. }
  6767. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6768. }
  6769. jsxParseElement() {
  6770. const startPos = this.state.start;
  6771. const startLoc = this.state.startLoc;
  6772. this.next();
  6773. return this.jsxParseElementAt(startPos, startLoc);
  6774. }
  6775. setContext(newContext) {
  6776. const {
  6777. context
  6778. } = this.state;
  6779. context[context.length - 1] = newContext;
  6780. }
  6781. parseExprAtom(refExpressionErrors) {
  6782. if (this.match(137)) {
  6783. return this.parseLiteral(this.state.value, "JSXText");
  6784. } else if (this.match(138)) {
  6785. return this.jsxParseElement();
  6786. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6787. this.replaceToken(138);
  6788. return this.jsxParseElement();
  6789. } else {
  6790. return super.parseExprAtom(refExpressionErrors);
  6791. }
  6792. }
  6793. skipSpace() {
  6794. const curContext = this.curContext();
  6795. if (!curContext.preserveSpace) super.skipSpace();
  6796. }
  6797. getTokenFromCode(code) {
  6798. const context = this.curContext();
  6799. if (context === types.j_expr) {
  6800. return this.jsxReadToken();
  6801. }
  6802. if (context === types.j_oTag || context === types.j_cTag) {
  6803. if (isIdentifierStart(code)) {
  6804. return this.jsxReadWord();
  6805. }
  6806. if (code === 62) {
  6807. ++this.state.pos;
  6808. return this.finishToken(139);
  6809. }
  6810. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6811. return this.jsxReadString(code);
  6812. }
  6813. }
  6814. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6815. ++this.state.pos;
  6816. return this.finishToken(138);
  6817. }
  6818. return super.getTokenFromCode(code);
  6819. }
  6820. updateContext(prevType) {
  6821. const {
  6822. context,
  6823. type
  6824. } = this.state;
  6825. if (type === 56 && prevType === 138) {
  6826. context.splice(-2, 2, types.j_cTag);
  6827. this.state.canStartJSXElement = false;
  6828. } else if (type === 138) {
  6829. context.push(types.j_oTag);
  6830. } else if (type === 139) {
  6831. const out = context[context.length - 1];
  6832. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  6833. context.pop();
  6834. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6835. } else {
  6836. this.setContext(types.j_expr);
  6837. this.state.canStartJSXElement = true;
  6838. }
  6839. } else {
  6840. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6841. }
  6842. }
  6843. });
  6844. class TypeScriptScope extends Scope {
  6845. constructor(...args) {
  6846. super(...args);
  6847. this.types = new Set();
  6848. this.enums = new Set();
  6849. this.constEnums = new Set();
  6850. this.classes = new Set();
  6851. this.exportOnlyBindings = new Set();
  6852. }
  6853. }
  6854. class TypeScriptScopeHandler extends ScopeHandler {
  6855. createScope(flags) {
  6856. return new TypeScriptScope(flags);
  6857. }
  6858. declareName(name, bindingType, loc) {
  6859. const scope = this.currentScope();
  6860. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  6861. this.maybeExportDefined(scope, name);
  6862. scope.exportOnlyBindings.add(name);
  6863. return;
  6864. }
  6865. super.declareName(...arguments);
  6866. if (bindingType & BIND_KIND_TYPE) {
  6867. if (!(bindingType & BIND_KIND_VALUE)) {
  6868. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  6869. this.maybeExportDefined(scope, name);
  6870. }
  6871. scope.types.add(name);
  6872. }
  6873. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  6874. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  6875. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  6876. }
  6877. isRedeclaredInScope(scope, name, bindingType) {
  6878. if (scope.enums.has(name)) {
  6879. if (bindingType & BIND_FLAGS_TS_ENUM) {
  6880. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  6881. const wasConst = scope.constEnums.has(name);
  6882. return isConst !== wasConst;
  6883. }
  6884. return true;
  6885. }
  6886. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  6887. if (scope.lexical.has(name)) {
  6888. return !!(bindingType & BIND_KIND_VALUE);
  6889. } else {
  6890. return false;
  6891. }
  6892. }
  6893. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  6894. return true;
  6895. }
  6896. return super.isRedeclaredInScope(...arguments);
  6897. }
  6898. checkLocalExport(id) {
  6899. const topLevelScope = this.scopeStack[0];
  6900. const {
  6901. name
  6902. } = id;
  6903. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  6904. super.checkLocalExport(id);
  6905. }
  6906. }
  6907. }
  6908. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  6909. function nonNull(x) {
  6910. if (x == null) {
  6911. throw new Error(`Unexpected ${x} value.`);
  6912. }
  6913. return x;
  6914. }
  6915. function assert(x) {
  6916. if (!x) {
  6917. throw new Error("Assert fail");
  6918. }
  6919. }
  6920. const TSErrors = ParseErrorEnum`typescript`(_ => ({
  6921. AbstractMethodHasImplementation: _(({
  6922. methodName
  6923. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`),
  6924. AbstractPropertyHasInitializer: _(({
  6925. propertyName
  6926. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`),
  6927. AccesorCannotDeclareThisParameter: _("'get' and 'set' accessors cannot declare 'this' parameters."),
  6928. AccesorCannotHaveTypeParameters: _("An accessor cannot have type parameters."),
  6929. CannotFindName: _(({
  6930. name
  6931. }) => `Cannot find name '${name}'.`),
  6932. ClassMethodHasDeclare: _("Class methods cannot have the 'declare' modifier."),
  6933. ClassMethodHasReadonly: _("Class methods cannot have the 'readonly' modifier."),
  6934. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: _("A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference."),
  6935. ConstructorHasTypeParameters: _("Type parameters cannot appear on a constructor declaration."),
  6936. DeclareAccessor: _(({
  6937. kind
  6938. }) => `'declare' is not allowed in ${kind}ters.`),
  6939. DeclareClassFieldHasInitializer: _("Initializers are not allowed in ambient contexts."),
  6940. DeclareFunctionHasImplementation: _("An implementation cannot be declared in ambient contexts."),
  6941. DuplicateAccessibilityModifier: _(({
  6942. modifier
  6943. }) => `Accessibility modifier already seen.`),
  6944. DuplicateModifier: _(({
  6945. modifier
  6946. }) => `Duplicate modifier: '${modifier}'.`),
  6947. EmptyHeritageClauseType: _(({
  6948. token
  6949. }) => `'${token}' list cannot be empty.`),
  6950. EmptyTypeArguments: _("Type argument list cannot be empty."),
  6951. EmptyTypeParameters: _("Type parameter list cannot be empty."),
  6952. ExpectedAmbientAfterExportDeclare: _("'export declare' must be followed by an ambient declaration."),
  6953. ImportAliasHasImportType: _("An import alias can not use 'import type'."),
  6954. IncompatibleModifiers: _(({
  6955. modifiers
  6956. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`),
  6957. IndexSignatureHasAbstract: _("Index signatures cannot have the 'abstract' modifier."),
  6958. IndexSignatureHasAccessibility: _(({
  6959. modifier
  6960. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`),
  6961. IndexSignatureHasDeclare: _("Index signatures cannot have the 'declare' modifier."),
  6962. IndexSignatureHasOverride: _("'override' modifier cannot appear on an index signature."),
  6963. IndexSignatureHasStatic: _("Index signatures cannot have the 'static' modifier."),
  6964. InitializerNotAllowedInAmbientContext: _("Initializers are not allowed in ambient contexts."),
  6965. InvalidModifierOnTypeMember: _(({
  6966. modifier
  6967. }) => `'${modifier}' modifier cannot appear on a type member.`),
  6968. InvalidModifiersOrder: _(({
  6969. orderedModifiers
  6970. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`),
  6971. InvalidTupleMemberLabel: _("Tuple members must be labeled with a simple identifier."),
  6972. MissingInterfaceName: _("'interface' declarations must be followed by an identifier."),
  6973. MixedLabeledAndUnlabeledElements: _("Tuple members must all have names or all not have names."),
  6974. NonAbstractClassHasAbstractMethod: _("Abstract methods can only appear within an abstract class."),
  6975. NonClassMethodPropertyHasAbstractModifer: _("'abstract' modifier can only appear on a class, method, or property declaration."),
  6976. OptionalTypeBeforeRequired: _("A required element cannot follow an optional element."),
  6977. OverrideNotInSubClass: _("This member cannot have an 'override' modifier because its containing class does not extend another class."),
  6978. PatternIsOptional: _("A binding pattern parameter cannot be optional in an implementation signature."),
  6979. PrivateElementHasAbstract: _("Private elements cannot have the 'abstract' modifier."),
  6980. PrivateElementHasAccessibility: _(({
  6981. modifier
  6982. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`),
  6983. ReadonlyForMethodSignature: _("'readonly' modifier can only appear on a property declaration or index signature."),
  6984. ReservedArrowTypeParam: _("This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`."),
  6985. ReservedTypeAssertion: _("This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead."),
  6986. SetAccesorCannotHaveOptionalParameter: _("A 'set' accessor cannot have an optional parameter."),
  6987. SetAccesorCannotHaveRestParameter: _("A 'set' accessor cannot have rest parameter."),
  6988. SetAccesorCannotHaveReturnType: _("A 'set' accessor cannot have a return type annotation."),
  6989. SingleTypeParameterWithoutTrailingComma: _(({
  6990. typeParameterName
  6991. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`),
  6992. StaticBlockCannotHaveModifier: _("Static class blocks cannot have any modifier."),
  6993. TypeAnnotationAfterAssign: _("Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`."),
  6994. TypeImportCannotSpecifyDefaultAndNamed: _("A type-only import can specify a default import or named bindings, but not both."),
  6995. TypeModifierIsUsedInTypeExports: _("The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement."),
  6996. TypeModifierIsUsedInTypeImports: _("The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement."),
  6997. UnexpectedParameterModifier: _("A parameter property is only allowed in a constructor implementation."),
  6998. UnexpectedReadonly: _("'readonly' type modifier is only permitted on array and tuple literal types."),
  6999. UnexpectedTypeAnnotation: _("Did not expect a type annotation here."),
  7000. UnexpectedTypeCastInParameter: _("Unexpected type cast in parameter position."),
  7001. UnsupportedImportTypeArgument: _("Argument in a type import must be a string literal."),
  7002. UnsupportedParameterPropertyKind: _("A parameter property may not be declared using a binding pattern."),
  7003. UnsupportedSignatureParameterKind: _(({
  7004. type
  7005. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`)
  7006. }));
  7007. function keywordTypeFromName(value) {
  7008. switch (value) {
  7009. case "any":
  7010. return "TSAnyKeyword";
  7011. case "boolean":
  7012. return "TSBooleanKeyword";
  7013. case "bigint":
  7014. return "TSBigIntKeyword";
  7015. case "never":
  7016. return "TSNeverKeyword";
  7017. case "number":
  7018. return "TSNumberKeyword";
  7019. case "object":
  7020. return "TSObjectKeyword";
  7021. case "string":
  7022. return "TSStringKeyword";
  7023. case "symbol":
  7024. return "TSSymbolKeyword";
  7025. case "undefined":
  7026. return "TSUndefinedKeyword";
  7027. case "unknown":
  7028. return "TSUnknownKeyword";
  7029. default:
  7030. return undefined;
  7031. }
  7032. }
  7033. function tsIsAccessModifier(modifier) {
  7034. return modifier === "private" || modifier === "public" || modifier === "protected";
  7035. }
  7036. var typescript = (superClass => class extends superClass {
  7037. getScopeHandler() {
  7038. return TypeScriptScopeHandler;
  7039. }
  7040. tsIsIdentifier() {
  7041. return tokenIsIdentifier(this.state.type);
  7042. }
  7043. tsTokenCanFollowModifier() {
  7044. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(134) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7045. }
  7046. tsNextTokenCanFollowModifier() {
  7047. this.next();
  7048. return this.tsTokenCanFollowModifier();
  7049. }
  7050. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7051. if (!tokenIsIdentifier(this.state.type)) {
  7052. return undefined;
  7053. }
  7054. const modifier = this.state.value;
  7055. if (allowedModifiers.indexOf(modifier) !== -1) {
  7056. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7057. return undefined;
  7058. }
  7059. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7060. return modifier;
  7061. }
  7062. }
  7063. return undefined;
  7064. }
  7065. tsParseModifiers({
  7066. modified,
  7067. allowedModifiers,
  7068. disallowedModifiers,
  7069. stopOnStartOfClassStaticBlock
  7070. }) {
  7071. const enforceOrder = (loc, modifier, before, after) => {
  7072. if (modifier === before && modified[after]) {
  7073. this.raise(TSErrors.InvalidModifiersOrder, {
  7074. at: loc,
  7075. orderedModifiers: [before, after]
  7076. });
  7077. }
  7078. };
  7079. const incompatible = (loc, modifier, mod1, mod2) => {
  7080. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7081. this.raise(TSErrors.IncompatibleModifiers, {
  7082. at: loc,
  7083. modifiers: [mod1, mod2]
  7084. });
  7085. }
  7086. };
  7087. for (;;) {
  7088. const {
  7089. startLoc
  7090. } = this.state;
  7091. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7092. if (!modifier) break;
  7093. if (tsIsAccessModifier(modifier)) {
  7094. if (modified.accessibility) {
  7095. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7096. at: startLoc,
  7097. modifier
  7098. });
  7099. } else {
  7100. enforceOrder(startLoc, modifier, modifier, "override");
  7101. enforceOrder(startLoc, modifier, modifier, "static");
  7102. enforceOrder(startLoc, modifier, modifier, "readonly");
  7103. modified.accessibility = modifier;
  7104. }
  7105. } else {
  7106. if (Object.hasOwnProperty.call(modified, modifier)) {
  7107. this.raise(TSErrors.DuplicateModifier, {
  7108. at: startLoc,
  7109. modifier
  7110. });
  7111. } else {
  7112. enforceOrder(startLoc, modifier, "static", "readonly");
  7113. enforceOrder(startLoc, modifier, "static", "override");
  7114. enforceOrder(startLoc, modifier, "override", "readonly");
  7115. enforceOrder(startLoc, modifier, "abstract", "override");
  7116. incompatible(startLoc, modifier, "declare", "override");
  7117. incompatible(startLoc, modifier, "static", "abstract");
  7118. }
  7119. modified[modifier] = true;
  7120. }
  7121. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7122. this.raise(TSErrors.InvalidModifierOnTypeMember, {
  7123. at: startLoc,
  7124. modifier
  7125. });
  7126. }
  7127. }
  7128. }
  7129. tsIsListTerminator(kind) {
  7130. switch (kind) {
  7131. case "EnumMembers":
  7132. case "TypeMembers":
  7133. return this.match(8);
  7134. case "HeritageClauseElement":
  7135. return this.match(5);
  7136. case "TupleElementTypes":
  7137. return this.match(3);
  7138. case "TypeParametersOrArguments":
  7139. return this.match(48);
  7140. }
  7141. throw new Error("Unreachable");
  7142. }
  7143. tsParseList(kind, parseElement) {
  7144. const result = [];
  7145. while (!this.tsIsListTerminator(kind)) {
  7146. result.push(parseElement());
  7147. }
  7148. return result;
  7149. }
  7150. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7151. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7152. }
  7153. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7154. const result = [];
  7155. let trailingCommaPos = -1;
  7156. for (;;) {
  7157. if (this.tsIsListTerminator(kind)) {
  7158. break;
  7159. }
  7160. trailingCommaPos = -1;
  7161. const element = parseElement();
  7162. if (element == null) {
  7163. return undefined;
  7164. }
  7165. result.push(element);
  7166. if (this.eat(12)) {
  7167. trailingCommaPos = this.state.lastTokStart;
  7168. continue;
  7169. }
  7170. if (this.tsIsListTerminator(kind)) {
  7171. break;
  7172. }
  7173. if (expectSuccess) {
  7174. this.expect(12);
  7175. }
  7176. return undefined;
  7177. }
  7178. if (refTrailingCommaPos) {
  7179. refTrailingCommaPos.value = trailingCommaPos;
  7180. }
  7181. return result;
  7182. }
  7183. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7184. if (!skipFirstToken) {
  7185. if (bracket) {
  7186. this.expect(0);
  7187. } else {
  7188. this.expect(47);
  7189. }
  7190. }
  7191. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7192. if (bracket) {
  7193. this.expect(3);
  7194. } else {
  7195. this.expect(48);
  7196. }
  7197. return result;
  7198. }
  7199. tsParseImportType() {
  7200. const node = this.startNode();
  7201. this.expect(83);
  7202. this.expect(10);
  7203. if (!this.match(129)) {
  7204. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7205. at: this.state.startLoc
  7206. });
  7207. }
  7208. node.argument = this.parseExprAtom();
  7209. this.expect(11);
  7210. if (this.eat(16)) {
  7211. node.qualifier = this.tsParseEntityName();
  7212. }
  7213. if (this.match(47)) {
  7214. node.typeParameters = this.tsParseTypeArguments();
  7215. }
  7216. return this.finishNode(node, "TSImportType");
  7217. }
  7218. tsParseEntityName(allowReservedWords = true) {
  7219. let entity = this.parseIdentifier(allowReservedWords);
  7220. while (this.eat(16)) {
  7221. const node = this.startNodeAtNode(entity);
  7222. node.left = entity;
  7223. node.right = this.parseIdentifier(allowReservedWords);
  7224. entity = this.finishNode(node, "TSQualifiedName");
  7225. }
  7226. return entity;
  7227. }
  7228. tsParseTypeReference() {
  7229. const node = this.startNode();
  7230. node.typeName = this.tsParseEntityName();
  7231. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7232. node.typeParameters = this.tsParseTypeArguments();
  7233. }
  7234. return this.finishNode(node, "TSTypeReference");
  7235. }
  7236. tsParseThisTypePredicate(lhs) {
  7237. this.next();
  7238. const node = this.startNodeAtNode(lhs);
  7239. node.parameterName = lhs;
  7240. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7241. node.asserts = false;
  7242. return this.finishNode(node, "TSTypePredicate");
  7243. }
  7244. tsParseThisTypeNode() {
  7245. const node = this.startNode();
  7246. this.next();
  7247. return this.finishNode(node, "TSThisType");
  7248. }
  7249. tsParseTypeQuery() {
  7250. const node = this.startNode();
  7251. this.expect(87);
  7252. if (this.match(83)) {
  7253. node.exprName = this.tsParseImportType();
  7254. } else {
  7255. node.exprName = this.tsParseEntityName();
  7256. }
  7257. return this.finishNode(node, "TSTypeQuery");
  7258. }
  7259. tsParseTypeParameter() {
  7260. const node = this.startNode();
  7261. node.name = this.tsParseTypeParameterName();
  7262. node.constraint = this.tsEatThenParseType(81);
  7263. node.default = this.tsEatThenParseType(29);
  7264. return this.finishNode(node, "TSTypeParameter");
  7265. }
  7266. tsTryParseTypeParameters() {
  7267. if (this.match(47)) {
  7268. return this.tsParseTypeParameters();
  7269. }
  7270. }
  7271. tsParseTypeParameters() {
  7272. const node = this.startNode();
  7273. if (this.match(47) || this.match(138)) {
  7274. this.next();
  7275. } else {
  7276. this.unexpected();
  7277. }
  7278. const refTrailingCommaPos = {
  7279. value: -1
  7280. };
  7281. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true, refTrailingCommaPos);
  7282. if (node.params.length === 0) {
  7283. this.raise(TSErrors.EmptyTypeParameters, {
  7284. at: node
  7285. });
  7286. }
  7287. if (refTrailingCommaPos.value !== -1) {
  7288. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7289. }
  7290. return this.finishNode(node, "TSTypeParameterDeclaration");
  7291. }
  7292. tsTryNextParseConstantContext() {
  7293. if (this.lookahead().type !== 75) return null;
  7294. this.next();
  7295. const typeReference = this.tsParseTypeReference();
  7296. if (typeReference.typeParameters) {
  7297. this.raise(TSErrors.CannotFindName, {
  7298. at: typeReference.typeName,
  7299. name: "const"
  7300. });
  7301. }
  7302. return typeReference;
  7303. }
  7304. tsFillSignature(returnToken, signature) {
  7305. const returnTokenRequired = returnToken === 19;
  7306. const paramsKey = "parameters";
  7307. const returnTypeKey = "typeAnnotation";
  7308. signature.typeParameters = this.tsTryParseTypeParameters();
  7309. this.expect(10);
  7310. signature[paramsKey] = this.tsParseBindingListForSignature();
  7311. if (returnTokenRequired) {
  7312. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7313. } else if (this.match(returnToken)) {
  7314. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7315. }
  7316. }
  7317. tsParseBindingListForSignature() {
  7318. return this.parseBindingList(11, 41).map(pattern => {
  7319. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7320. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7321. at: pattern,
  7322. type: pattern.type
  7323. });
  7324. }
  7325. return pattern;
  7326. });
  7327. }
  7328. tsParseTypeMemberSemicolon() {
  7329. if (!this.eat(12) && !this.isLineTerminator()) {
  7330. this.expect(13);
  7331. }
  7332. }
  7333. tsParseSignatureMember(kind, node) {
  7334. this.tsFillSignature(14, node);
  7335. this.tsParseTypeMemberSemicolon();
  7336. return this.finishNode(node, kind);
  7337. }
  7338. tsIsUnambiguouslyIndexSignature() {
  7339. this.next();
  7340. if (tokenIsIdentifier(this.state.type)) {
  7341. this.next();
  7342. return this.match(14);
  7343. }
  7344. return false;
  7345. }
  7346. tsTryParseIndexSignature(node) {
  7347. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7348. return undefined;
  7349. }
  7350. this.expect(0);
  7351. const id = this.parseIdentifier();
  7352. id.typeAnnotation = this.tsParseTypeAnnotation();
  7353. this.resetEndLocation(id);
  7354. this.expect(3);
  7355. node.parameters = [id];
  7356. const type = this.tsTryParseTypeAnnotation();
  7357. if (type) node.typeAnnotation = type;
  7358. this.tsParseTypeMemberSemicolon();
  7359. return this.finishNode(node, "TSIndexSignature");
  7360. }
  7361. tsParsePropertyOrMethodSignature(node, readonly) {
  7362. if (this.eat(17)) node.optional = true;
  7363. const nodeAny = node;
  7364. if (this.match(10) || this.match(47)) {
  7365. if (readonly) {
  7366. this.raise(TSErrors.ReadonlyForMethodSignature, {
  7367. at: node
  7368. });
  7369. }
  7370. const method = nodeAny;
  7371. if (method.kind && this.match(47)) {
  7372. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  7373. at: this.state.curPosition()
  7374. });
  7375. }
  7376. this.tsFillSignature(14, method);
  7377. this.tsParseTypeMemberSemicolon();
  7378. const paramsKey = "parameters";
  7379. const returnTypeKey = "typeAnnotation";
  7380. if (method.kind === "get") {
  7381. if (method[paramsKey].length > 0) {
  7382. this.raise(Errors.BadGetterArity, {
  7383. at: this.state.curPosition()
  7384. });
  7385. if (this.isThisParam(method[paramsKey][0])) {
  7386. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7387. at: this.state.curPosition()
  7388. });
  7389. }
  7390. }
  7391. } else if (method.kind === "set") {
  7392. if (method[paramsKey].length !== 1) {
  7393. this.raise(Errors.BadSetterArity, {
  7394. at: this.state.curPosition()
  7395. });
  7396. } else {
  7397. const firstParameter = method[paramsKey][0];
  7398. if (this.isThisParam(firstParameter)) {
  7399. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7400. at: this.state.curPosition()
  7401. });
  7402. }
  7403. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7404. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  7405. at: this.state.curPosition()
  7406. });
  7407. }
  7408. if (firstParameter.type === "RestElement") {
  7409. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  7410. at: this.state.curPosition()
  7411. });
  7412. }
  7413. }
  7414. if (method[returnTypeKey]) {
  7415. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  7416. at: method[returnTypeKey]
  7417. });
  7418. }
  7419. } else {
  7420. method.kind = "method";
  7421. }
  7422. return this.finishNode(method, "TSMethodSignature");
  7423. } else {
  7424. const property = nodeAny;
  7425. if (readonly) property.readonly = true;
  7426. const type = this.tsTryParseTypeAnnotation();
  7427. if (type) property.typeAnnotation = type;
  7428. this.tsParseTypeMemberSemicolon();
  7429. return this.finishNode(property, "TSPropertySignature");
  7430. }
  7431. }
  7432. tsParseTypeMember() {
  7433. const node = this.startNode();
  7434. if (this.match(10) || this.match(47)) {
  7435. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7436. }
  7437. if (this.match(77)) {
  7438. const id = this.startNode();
  7439. this.next();
  7440. if (this.match(10) || this.match(47)) {
  7441. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7442. } else {
  7443. node.key = this.createIdentifier(id, "new");
  7444. return this.tsParsePropertyOrMethodSignature(node, false);
  7445. }
  7446. }
  7447. this.tsParseModifiers({
  7448. modified: node,
  7449. allowedModifiers: ["readonly"],
  7450. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  7451. });
  7452. const idx = this.tsTryParseIndexSignature(node);
  7453. if (idx) {
  7454. return idx;
  7455. }
  7456. this.parsePropertyName(node);
  7457. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7458. node.kind = node.key.name;
  7459. this.parsePropertyName(node);
  7460. }
  7461. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7462. }
  7463. tsParseTypeLiteral() {
  7464. const node = this.startNode();
  7465. node.members = this.tsParseObjectTypeMembers();
  7466. return this.finishNode(node, "TSTypeLiteral");
  7467. }
  7468. tsParseObjectTypeMembers() {
  7469. this.expect(5);
  7470. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7471. this.expect(8);
  7472. return members;
  7473. }
  7474. tsIsStartOfMappedType() {
  7475. this.next();
  7476. if (this.eat(53)) {
  7477. return this.isContextual(118);
  7478. }
  7479. if (this.isContextual(118)) {
  7480. this.next();
  7481. }
  7482. if (!this.match(0)) {
  7483. return false;
  7484. }
  7485. this.next();
  7486. if (!this.tsIsIdentifier()) {
  7487. return false;
  7488. }
  7489. this.next();
  7490. return this.match(58);
  7491. }
  7492. tsParseMappedTypeParameter() {
  7493. const node = this.startNode();
  7494. node.name = this.tsParseTypeParameterName();
  7495. node.constraint = this.tsExpectThenParseType(58);
  7496. return this.finishNode(node, "TSTypeParameter");
  7497. }
  7498. tsParseMappedType() {
  7499. const node = this.startNode();
  7500. this.expect(5);
  7501. if (this.match(53)) {
  7502. node.readonly = this.state.value;
  7503. this.next();
  7504. this.expectContextual(118);
  7505. } else if (this.eatContextual(118)) {
  7506. node.readonly = true;
  7507. }
  7508. this.expect(0);
  7509. node.typeParameter = this.tsParseMappedTypeParameter();
  7510. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  7511. this.expect(3);
  7512. if (this.match(53)) {
  7513. node.optional = this.state.value;
  7514. this.next();
  7515. this.expect(17);
  7516. } else if (this.eat(17)) {
  7517. node.optional = true;
  7518. }
  7519. node.typeAnnotation = this.tsTryParseType();
  7520. this.semicolon();
  7521. this.expect(8);
  7522. return this.finishNode(node, "TSMappedType");
  7523. }
  7524. tsParseTupleType() {
  7525. const node = this.startNode();
  7526. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7527. let seenOptionalElement = false;
  7528. let labeledElements = null;
  7529. node.elementTypes.forEach(elementNode => {
  7530. var _labeledElements;
  7531. let {
  7532. type
  7533. } = elementNode;
  7534. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  7535. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  7536. at: elementNode
  7537. });
  7538. }
  7539. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  7540. if (type === "TSRestType") {
  7541. elementNode = elementNode.typeAnnotation;
  7542. type = elementNode.type;
  7543. }
  7544. const isLabeled = type === "TSNamedTupleMember";
  7545. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  7546. if (labeledElements !== isLabeled) {
  7547. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  7548. at: elementNode
  7549. });
  7550. }
  7551. });
  7552. return this.finishNode(node, "TSTupleType");
  7553. }
  7554. tsParseTupleElementType() {
  7555. const {
  7556. start: startPos,
  7557. startLoc
  7558. } = this.state;
  7559. const rest = this.eat(21);
  7560. let type = this.tsParseType();
  7561. const optional = this.eat(17);
  7562. const labeled = this.eat(14);
  7563. if (labeled) {
  7564. const labeledNode = this.startNodeAtNode(type);
  7565. labeledNode.optional = optional;
  7566. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  7567. labeledNode.label = type.typeName;
  7568. } else {
  7569. this.raise(TSErrors.InvalidTupleMemberLabel, {
  7570. at: type
  7571. });
  7572. labeledNode.label = type;
  7573. }
  7574. labeledNode.elementType = this.tsParseType();
  7575. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  7576. } else if (optional) {
  7577. const optionalTypeNode = this.startNodeAtNode(type);
  7578. optionalTypeNode.typeAnnotation = type;
  7579. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  7580. }
  7581. if (rest) {
  7582. const restNode = this.startNodeAt(startPos, startLoc);
  7583. restNode.typeAnnotation = type;
  7584. type = this.finishNode(restNode, "TSRestType");
  7585. }
  7586. return type;
  7587. }
  7588. tsParseParenthesizedType() {
  7589. const node = this.startNode();
  7590. this.expect(10);
  7591. node.typeAnnotation = this.tsParseType();
  7592. this.expect(11);
  7593. return this.finishNode(node, "TSParenthesizedType");
  7594. }
  7595. tsParseFunctionOrConstructorType(type, abstract) {
  7596. const node = this.startNode();
  7597. if (type === "TSConstructorType") {
  7598. node.abstract = !!abstract;
  7599. if (abstract) this.next();
  7600. this.next();
  7601. }
  7602. this.tsFillSignature(19, node);
  7603. return this.finishNode(node, type);
  7604. }
  7605. tsParseLiteralTypeNode() {
  7606. const node = this.startNode();
  7607. node.literal = (() => {
  7608. switch (this.state.type) {
  7609. case 130:
  7610. case 131:
  7611. case 129:
  7612. case 85:
  7613. case 86:
  7614. return this.parseExprAtom();
  7615. default:
  7616. throw this.unexpected();
  7617. }
  7618. })();
  7619. return this.finishNode(node, "TSLiteralType");
  7620. }
  7621. tsParseTemplateLiteralType() {
  7622. const node = this.startNode();
  7623. node.literal = this.parseTemplate(false);
  7624. return this.finishNode(node, "TSLiteralType");
  7625. }
  7626. parseTemplateSubstitution() {
  7627. if (this.state.inType) return this.tsParseType();
  7628. return super.parseTemplateSubstitution();
  7629. }
  7630. tsParseThisTypeOrThisTypePredicate() {
  7631. const thisKeyword = this.tsParseThisTypeNode();
  7632. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  7633. return this.tsParseThisTypePredicate(thisKeyword);
  7634. } else {
  7635. return thisKeyword;
  7636. }
  7637. }
  7638. tsParseNonArrayType() {
  7639. switch (this.state.type) {
  7640. case 129:
  7641. case 130:
  7642. case 131:
  7643. case 85:
  7644. case 86:
  7645. return this.tsParseLiteralTypeNode();
  7646. case 53:
  7647. if (this.state.value === "-") {
  7648. const node = this.startNode();
  7649. const nextToken = this.lookahead();
  7650. if (nextToken.type !== 130 && nextToken.type !== 131) {
  7651. throw this.unexpected();
  7652. }
  7653. node.literal = this.parseMaybeUnary();
  7654. return this.finishNode(node, "TSLiteralType");
  7655. }
  7656. break;
  7657. case 78:
  7658. return this.tsParseThisTypeOrThisTypePredicate();
  7659. case 87:
  7660. return this.tsParseTypeQuery();
  7661. case 83:
  7662. return this.tsParseImportType();
  7663. case 5:
  7664. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7665. case 0:
  7666. return this.tsParseTupleType();
  7667. case 10:
  7668. return this.tsParseParenthesizedType();
  7669. case 25:
  7670. case 24:
  7671. return this.tsParseTemplateLiteralType();
  7672. default:
  7673. {
  7674. const {
  7675. type
  7676. } = this.state;
  7677. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  7678. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7679. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  7680. const node = this.startNode();
  7681. this.next();
  7682. return this.finishNode(node, nodeType);
  7683. }
  7684. return this.tsParseTypeReference();
  7685. }
  7686. }
  7687. }
  7688. throw this.unexpected();
  7689. }
  7690. tsParseArrayTypeOrHigher() {
  7691. let type = this.tsParseNonArrayType();
  7692. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  7693. if (this.match(3)) {
  7694. const node = this.startNodeAtNode(type);
  7695. node.elementType = type;
  7696. this.expect(3);
  7697. type = this.finishNode(node, "TSArrayType");
  7698. } else {
  7699. const node = this.startNodeAtNode(type);
  7700. node.objectType = type;
  7701. node.indexType = this.tsParseType();
  7702. this.expect(3);
  7703. type = this.finishNode(node, "TSIndexedAccessType");
  7704. }
  7705. }
  7706. return type;
  7707. }
  7708. tsParseTypeOperator() {
  7709. const node = this.startNode();
  7710. const operator = this.state.value;
  7711. this.next();
  7712. node.operator = operator;
  7713. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7714. if (operator === "readonly") {
  7715. this.tsCheckTypeAnnotationForReadOnly(node);
  7716. }
  7717. return this.finishNode(node, "TSTypeOperator");
  7718. }
  7719. tsCheckTypeAnnotationForReadOnly(node) {
  7720. switch (node.typeAnnotation.type) {
  7721. case "TSTupleType":
  7722. case "TSArrayType":
  7723. return;
  7724. default:
  7725. this.raise(TSErrors.UnexpectedReadonly, {
  7726. at: node
  7727. });
  7728. }
  7729. }
  7730. tsParseInferType() {
  7731. const node = this.startNode();
  7732. this.expectContextual(112);
  7733. const typeParameter = this.startNode();
  7734. typeParameter.name = this.tsParseTypeParameterName();
  7735. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7736. return this.finishNode(node, "TSInferType");
  7737. }
  7738. tsParseTypeOperatorOrHigher() {
  7739. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  7740. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(112) ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  7741. }
  7742. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7743. const node = this.startNode();
  7744. const hasLeadingOperator = this.eat(operator);
  7745. const types = [];
  7746. do {
  7747. types.push(parseConstituentType());
  7748. } while (this.eat(operator));
  7749. if (types.length === 1 && !hasLeadingOperator) {
  7750. return types[0];
  7751. }
  7752. node.types = types;
  7753. return this.finishNode(node, kind);
  7754. }
  7755. tsParseIntersectionTypeOrHigher() {
  7756. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  7757. }
  7758. tsParseUnionTypeOrHigher() {
  7759. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  7760. }
  7761. tsIsStartOfFunctionType() {
  7762. if (this.match(47)) {
  7763. return true;
  7764. }
  7765. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  7766. }
  7767. tsSkipParameterStart() {
  7768. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  7769. this.next();
  7770. return true;
  7771. }
  7772. if (this.match(5)) {
  7773. const {
  7774. errors
  7775. } = this.state;
  7776. const previousErrorCount = errors.length;
  7777. try {
  7778. this.parseObjectLike(8, true);
  7779. return errors.length === previousErrorCount;
  7780. } catch (_unused) {
  7781. return false;
  7782. }
  7783. }
  7784. if (this.match(0)) {
  7785. this.next();
  7786. const {
  7787. errors
  7788. } = this.state;
  7789. const previousErrorCount = errors.length;
  7790. try {
  7791. this.parseBindingList(3, 93, true);
  7792. return errors.length === previousErrorCount;
  7793. } catch (_unused2) {
  7794. return false;
  7795. }
  7796. }
  7797. return false;
  7798. }
  7799. tsIsUnambiguouslyStartOfFunctionType() {
  7800. this.next();
  7801. if (this.match(11) || this.match(21)) {
  7802. return true;
  7803. }
  7804. if (this.tsSkipParameterStart()) {
  7805. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  7806. return true;
  7807. }
  7808. if (this.match(11)) {
  7809. this.next();
  7810. if (this.match(19)) {
  7811. return true;
  7812. }
  7813. }
  7814. }
  7815. return false;
  7816. }
  7817. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  7818. return this.tsInType(() => {
  7819. const t = this.startNode();
  7820. this.expect(returnToken);
  7821. const node = this.startNode();
  7822. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  7823. if (asserts && this.match(78)) {
  7824. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  7825. if (thisTypePredicate.type === "TSThisType") {
  7826. node.parameterName = thisTypePredicate;
  7827. node.asserts = true;
  7828. node.typeAnnotation = null;
  7829. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  7830. } else {
  7831. this.resetStartLocationFromNode(thisTypePredicate, node);
  7832. thisTypePredicate.asserts = true;
  7833. }
  7834. t.typeAnnotation = thisTypePredicate;
  7835. return this.finishNode(t, "TSTypeAnnotation");
  7836. }
  7837. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  7838. if (!typePredicateVariable) {
  7839. if (!asserts) {
  7840. return this.tsParseTypeAnnotation(false, t);
  7841. }
  7842. node.parameterName = this.parseIdentifier();
  7843. node.asserts = asserts;
  7844. node.typeAnnotation = null;
  7845. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7846. return this.finishNode(t, "TSTypeAnnotation");
  7847. }
  7848. const type = this.tsParseTypeAnnotation(false);
  7849. node.parameterName = typePredicateVariable;
  7850. node.typeAnnotation = type;
  7851. node.asserts = asserts;
  7852. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7853. return this.finishNode(t, "TSTypeAnnotation");
  7854. });
  7855. }
  7856. tsTryParseTypeOrTypePredicateAnnotation() {
  7857. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  7858. }
  7859. tsTryParseTypeAnnotation() {
  7860. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  7861. }
  7862. tsTryParseType() {
  7863. return this.tsEatThenParseType(14);
  7864. }
  7865. tsParseTypePredicatePrefix() {
  7866. const id = this.parseIdentifier();
  7867. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  7868. this.next();
  7869. return id;
  7870. }
  7871. }
  7872. tsParseTypePredicateAsserts() {
  7873. if (this.state.type !== 106) {
  7874. return false;
  7875. }
  7876. const containsEsc = this.state.containsEsc;
  7877. this.next();
  7878. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  7879. return false;
  7880. }
  7881. if (containsEsc) {
  7882. this.raise(Errors.InvalidEscapedReservedWord, {
  7883. at: this.state.lastTokStartLoc,
  7884. reservedWord: "asserts"
  7885. });
  7886. }
  7887. return true;
  7888. }
  7889. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  7890. this.tsInType(() => {
  7891. if (eatColon) this.expect(14);
  7892. t.typeAnnotation = this.tsParseType();
  7893. });
  7894. return this.finishNode(t, "TSTypeAnnotation");
  7895. }
  7896. tsParseType() {
  7897. assert(this.state.inType);
  7898. const type = this.tsParseNonConditionalType();
  7899. if (this.hasPrecedingLineBreak() || !this.eat(81)) {
  7900. return type;
  7901. }
  7902. const node = this.startNodeAtNode(type);
  7903. node.checkType = type;
  7904. node.extendsType = this.tsParseNonConditionalType();
  7905. this.expect(17);
  7906. node.trueType = this.tsParseType();
  7907. this.expect(14);
  7908. node.falseType = this.tsParseType();
  7909. return this.finishNode(node, "TSConditionalType");
  7910. }
  7911. isAbstractConstructorSignature() {
  7912. return this.isContextual(120) && this.lookahead().type === 77;
  7913. }
  7914. tsParseNonConditionalType() {
  7915. if (this.tsIsStartOfFunctionType()) {
  7916. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  7917. }
  7918. if (this.match(77)) {
  7919. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  7920. } else if (this.isAbstractConstructorSignature()) {
  7921. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  7922. }
  7923. return this.tsParseUnionTypeOrHigher();
  7924. }
  7925. tsParseTypeAssertion() {
  7926. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  7927. this.raise(TSErrors.ReservedTypeAssertion, {
  7928. at: this.state.startLoc
  7929. });
  7930. }
  7931. const node = this.startNode();
  7932. const _const = this.tsTryNextParseConstantContext();
  7933. node.typeAnnotation = _const || this.tsNextThenParseType();
  7934. this.expect(48);
  7935. node.expression = this.parseMaybeUnary();
  7936. return this.finishNode(node, "TSTypeAssertion");
  7937. }
  7938. tsParseHeritageClause(token) {
  7939. const originalStartLoc = this.state.startLoc;
  7940. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  7941. if (!delimitedList.length) {
  7942. this.raise(TSErrors.EmptyHeritageClauseType, {
  7943. at: originalStartLoc,
  7944. token
  7945. });
  7946. }
  7947. return delimitedList;
  7948. }
  7949. tsParseExpressionWithTypeArguments() {
  7950. const node = this.startNode();
  7951. node.expression = this.tsParseEntityName();
  7952. if (this.match(47)) {
  7953. node.typeParameters = this.tsParseTypeArguments();
  7954. }
  7955. return this.finishNode(node, "TSExpressionWithTypeArguments");
  7956. }
  7957. tsParseInterfaceDeclaration(node, properties = {}) {
  7958. if (this.hasFollowingLineBreak()) return null;
  7959. this.expectContextual(125);
  7960. if (properties.declare) node.declare = true;
  7961. if (tokenIsIdentifier(this.state.type)) {
  7962. node.id = this.parseIdentifier();
  7963. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  7964. } else {
  7965. node.id = null;
  7966. this.raise(TSErrors.MissingInterfaceName, {
  7967. at: this.state.startLoc
  7968. });
  7969. }
  7970. node.typeParameters = this.tsTryParseTypeParameters();
  7971. if (this.eat(81)) {
  7972. node.extends = this.tsParseHeritageClause("extends");
  7973. }
  7974. const body = this.startNode();
  7975. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  7976. node.body = this.finishNode(body, "TSInterfaceBody");
  7977. return this.finishNode(node, "TSInterfaceDeclaration");
  7978. }
  7979. tsParseTypeAliasDeclaration(node) {
  7980. node.id = this.parseIdentifier();
  7981. this.checkIdentifier(node.id, BIND_TS_TYPE);
  7982. node.typeAnnotation = this.tsInType(() => {
  7983. node.typeParameters = this.tsTryParseTypeParameters();
  7984. this.expect(29);
  7985. if (this.isContextual(111) && this.lookahead().type !== 16) {
  7986. const node = this.startNode();
  7987. this.next();
  7988. return this.finishNode(node, "TSIntrinsicKeyword");
  7989. }
  7990. return this.tsParseType();
  7991. });
  7992. this.semicolon();
  7993. return this.finishNode(node, "TSTypeAliasDeclaration");
  7994. }
  7995. tsInNoContext(cb) {
  7996. const oldContext = this.state.context;
  7997. this.state.context = [oldContext[0]];
  7998. try {
  7999. return cb();
  8000. } finally {
  8001. this.state.context = oldContext;
  8002. }
  8003. }
  8004. tsInType(cb) {
  8005. const oldInType = this.state.inType;
  8006. this.state.inType = true;
  8007. try {
  8008. return cb();
  8009. } finally {
  8010. this.state.inType = oldInType;
  8011. }
  8012. }
  8013. tsEatThenParseType(token) {
  8014. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8015. }
  8016. tsExpectThenParseType(token) {
  8017. return this.tsDoThenParseType(() => this.expect(token));
  8018. }
  8019. tsNextThenParseType() {
  8020. return this.tsDoThenParseType(() => this.next());
  8021. }
  8022. tsDoThenParseType(cb) {
  8023. return this.tsInType(() => {
  8024. cb();
  8025. return this.tsParseType();
  8026. });
  8027. }
  8028. tsParseEnumMember() {
  8029. const node = this.startNode();
  8030. node.id = this.match(129) ? this.parseExprAtom() : this.parseIdentifier(true);
  8031. if (this.eat(29)) {
  8032. node.initializer = this.parseMaybeAssignAllowIn();
  8033. }
  8034. return this.finishNode(node, "TSEnumMember");
  8035. }
  8036. tsParseEnumDeclaration(node, properties = {}) {
  8037. if (properties.const) node.const = true;
  8038. if (properties.declare) node.declare = true;
  8039. this.expectContextual(122);
  8040. node.id = this.parseIdentifier();
  8041. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8042. this.expect(5);
  8043. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8044. this.expect(8);
  8045. return this.finishNode(node, "TSEnumDeclaration");
  8046. }
  8047. tsParseModuleBlock() {
  8048. const node = this.startNode();
  8049. this.scope.enter(SCOPE_OTHER);
  8050. this.expect(5);
  8051. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8052. this.scope.exit();
  8053. return this.finishNode(node, "TSModuleBlock");
  8054. }
  8055. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8056. node.id = this.parseIdentifier();
  8057. if (!nested) {
  8058. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8059. }
  8060. if (this.eat(16)) {
  8061. const inner = this.startNode();
  8062. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8063. node.body = inner;
  8064. } else {
  8065. this.scope.enter(SCOPE_TS_MODULE);
  8066. this.prodParam.enter(PARAM);
  8067. node.body = this.tsParseModuleBlock();
  8068. this.prodParam.exit();
  8069. this.scope.exit();
  8070. }
  8071. return this.finishNode(node, "TSModuleDeclaration");
  8072. }
  8073. tsParseAmbientExternalModuleDeclaration(node) {
  8074. if (this.isContextual(109)) {
  8075. node.global = true;
  8076. node.id = this.parseIdentifier();
  8077. } else if (this.match(129)) {
  8078. node.id = this.parseExprAtom();
  8079. } else {
  8080. this.unexpected();
  8081. }
  8082. if (this.match(5)) {
  8083. this.scope.enter(SCOPE_TS_MODULE);
  8084. this.prodParam.enter(PARAM);
  8085. node.body = this.tsParseModuleBlock();
  8086. this.prodParam.exit();
  8087. this.scope.exit();
  8088. } else {
  8089. this.semicolon();
  8090. }
  8091. return this.finishNode(node, "TSModuleDeclaration");
  8092. }
  8093. tsParseImportEqualsDeclaration(node, isExport) {
  8094. node.isExport = isExport || false;
  8095. node.id = this.parseIdentifier();
  8096. this.checkIdentifier(node.id, BIND_LEXICAL);
  8097. this.expect(29);
  8098. const moduleReference = this.tsParseModuleReference();
  8099. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8100. this.raise(TSErrors.ImportAliasHasImportType, {
  8101. at: moduleReference
  8102. });
  8103. }
  8104. node.moduleReference = moduleReference;
  8105. this.semicolon();
  8106. return this.finishNode(node, "TSImportEqualsDeclaration");
  8107. }
  8108. tsIsExternalModuleReference() {
  8109. return this.isContextual(116) && this.lookaheadCharCode() === 40;
  8110. }
  8111. tsParseModuleReference() {
  8112. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8113. }
  8114. tsParseExternalModuleReference() {
  8115. const node = this.startNode();
  8116. this.expectContextual(116);
  8117. this.expect(10);
  8118. if (!this.match(129)) {
  8119. throw this.unexpected();
  8120. }
  8121. node.expression = this.parseExprAtom();
  8122. this.expect(11);
  8123. return this.finishNode(node, "TSExternalModuleReference");
  8124. }
  8125. tsLookAhead(f) {
  8126. const state = this.state.clone();
  8127. const res = f();
  8128. this.state = state;
  8129. return res;
  8130. }
  8131. tsTryParseAndCatch(f) {
  8132. const result = this.tryParse(abort => f() || abort());
  8133. if (result.aborted || !result.node) return undefined;
  8134. if (result.error) this.state = result.failState;
  8135. return result.node;
  8136. }
  8137. tsTryParse(f) {
  8138. const state = this.state.clone();
  8139. const result = f();
  8140. if (result !== undefined && result !== false) {
  8141. return result;
  8142. } else {
  8143. this.state = state;
  8144. return undefined;
  8145. }
  8146. }
  8147. tsTryParseDeclare(nany) {
  8148. if (this.isLineTerminator()) {
  8149. return;
  8150. }
  8151. let starttype = this.state.type;
  8152. let kind;
  8153. if (this.isContextual(99)) {
  8154. starttype = 74;
  8155. kind = "let";
  8156. }
  8157. return this.tsInAmbientContext(() => {
  8158. if (starttype === 68) {
  8159. nany.declare = true;
  8160. return this.parseFunctionStatement(nany, false, true);
  8161. }
  8162. if (starttype === 80) {
  8163. nany.declare = true;
  8164. return this.parseClass(nany, true, false);
  8165. }
  8166. if (starttype === 122) {
  8167. return this.tsParseEnumDeclaration(nany, {
  8168. declare: true
  8169. });
  8170. }
  8171. if (starttype === 109) {
  8172. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8173. }
  8174. if (starttype === 75 || starttype === 74) {
  8175. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8176. nany.declare = true;
  8177. return this.parseVarStatement(nany, kind || this.state.value, true);
  8178. }
  8179. this.expect(75);
  8180. return this.tsParseEnumDeclaration(nany, {
  8181. const: true,
  8182. declare: true
  8183. });
  8184. }
  8185. if (starttype === 125) {
  8186. const result = this.tsParseInterfaceDeclaration(nany, {
  8187. declare: true
  8188. });
  8189. if (result) return result;
  8190. }
  8191. if (tokenIsIdentifier(starttype)) {
  8192. return this.tsParseDeclaration(nany, this.state.value, true);
  8193. }
  8194. });
  8195. }
  8196. tsTryParseExportDeclaration() {
  8197. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  8198. }
  8199. tsParseExpressionStatement(node, expr) {
  8200. switch (expr.name) {
  8201. case "declare":
  8202. {
  8203. const declaration = this.tsTryParseDeclare(node);
  8204. if (declaration) {
  8205. declaration.declare = true;
  8206. return declaration;
  8207. }
  8208. break;
  8209. }
  8210. case "global":
  8211. if (this.match(5)) {
  8212. this.scope.enter(SCOPE_TS_MODULE);
  8213. this.prodParam.enter(PARAM);
  8214. const mod = node;
  8215. mod.global = true;
  8216. mod.id = expr;
  8217. mod.body = this.tsParseModuleBlock();
  8218. this.scope.exit();
  8219. this.prodParam.exit();
  8220. return this.finishNode(mod, "TSModuleDeclaration");
  8221. }
  8222. break;
  8223. default:
  8224. return this.tsParseDeclaration(node, expr.name, false);
  8225. }
  8226. }
  8227. tsParseDeclaration(node, value, next) {
  8228. switch (value) {
  8229. case "abstract":
  8230. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8231. return this.tsParseAbstractDeclaration(node);
  8232. }
  8233. break;
  8234. case "module":
  8235. if (this.tsCheckLineTerminator(next)) {
  8236. if (this.match(129)) {
  8237. return this.tsParseAmbientExternalModuleDeclaration(node);
  8238. } else if (tokenIsIdentifier(this.state.type)) {
  8239. return this.tsParseModuleOrNamespaceDeclaration(node);
  8240. }
  8241. }
  8242. break;
  8243. case "namespace":
  8244. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8245. return this.tsParseModuleOrNamespaceDeclaration(node);
  8246. }
  8247. break;
  8248. case "type":
  8249. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8250. return this.tsParseTypeAliasDeclaration(node);
  8251. }
  8252. break;
  8253. }
  8254. }
  8255. tsCheckLineTerminator(next) {
  8256. if (next) {
  8257. if (this.hasFollowingLineBreak()) return false;
  8258. this.next();
  8259. return true;
  8260. }
  8261. return !this.isLineTerminator();
  8262. }
  8263. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  8264. if (!this.match(47)) {
  8265. return undefined;
  8266. }
  8267. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8268. this.state.maybeInArrowParameters = true;
  8269. const res = this.tsTryParseAndCatch(() => {
  8270. const node = this.startNodeAt(startPos, startLoc);
  8271. node.typeParameters = this.tsParseTypeParameters();
  8272. super.parseFunctionParams(node);
  8273. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8274. this.expect(19);
  8275. return node;
  8276. });
  8277. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8278. if (!res) {
  8279. return undefined;
  8280. }
  8281. return this.parseArrowExpression(res, null, true);
  8282. }
  8283. tsParseTypeArgumentsInExpression() {
  8284. if (this.reScan_lt() !== 47) {
  8285. return undefined;
  8286. }
  8287. return this.tsParseTypeArguments();
  8288. }
  8289. tsParseTypeArguments() {
  8290. const node = this.startNode();
  8291. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8292. this.expect(47);
  8293. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8294. }));
  8295. if (node.params.length === 0) {
  8296. this.raise(TSErrors.EmptyTypeArguments, {
  8297. at: node
  8298. });
  8299. }
  8300. this.expect(48);
  8301. return this.finishNode(node, "TSTypeParameterInstantiation");
  8302. }
  8303. tsIsDeclarationStart() {
  8304. return tokenIsTSDeclarationStart(this.state.type);
  8305. }
  8306. isExportDefaultSpecifier() {
  8307. if (this.tsIsDeclarationStart()) return false;
  8308. return super.isExportDefaultSpecifier();
  8309. }
  8310. parseAssignableListItem(allowModifiers, decorators) {
  8311. const startPos = this.state.start;
  8312. const startLoc = this.state.startLoc;
  8313. let accessibility;
  8314. let readonly = false;
  8315. let override = false;
  8316. if (allowModifiers !== undefined) {
  8317. const modified = {};
  8318. this.tsParseModifiers({
  8319. modified,
  8320. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8321. });
  8322. accessibility = modified.accessibility;
  8323. override = modified.override;
  8324. readonly = modified.readonly;
  8325. if (allowModifiers === false && (accessibility || readonly || override)) {
  8326. this.raise(TSErrors.UnexpectedParameterModifier, {
  8327. at: startLoc
  8328. });
  8329. }
  8330. }
  8331. const left = this.parseMaybeDefault();
  8332. this.parseAssignableListItemTypes(left);
  8333. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8334. if (accessibility || readonly || override) {
  8335. const pp = this.startNodeAt(startPos, startLoc);
  8336. if (decorators.length) {
  8337. pp.decorators = decorators;
  8338. }
  8339. if (accessibility) pp.accessibility = accessibility;
  8340. if (readonly) pp.readonly = readonly;
  8341. if (override) pp.override = override;
  8342. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8343. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  8344. at: pp
  8345. });
  8346. }
  8347. pp.parameter = elt;
  8348. return this.finishNode(pp, "TSParameterProperty");
  8349. }
  8350. if (decorators.length) {
  8351. left.decorators = decorators;
  8352. }
  8353. return elt;
  8354. }
  8355. isSimpleParameter(node) {
  8356. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  8357. }
  8358. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8359. if (this.match(14)) {
  8360. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8361. }
  8362. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  8363. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  8364. this.finishNode(node, bodilessType);
  8365. return;
  8366. }
  8367. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8368. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  8369. at: node
  8370. });
  8371. if (node.declare) {
  8372. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8373. return;
  8374. }
  8375. }
  8376. super.parseFunctionBodyAndFinish(node, type, isMethod);
  8377. }
  8378. registerFunctionStatementId(node) {
  8379. if (!node.body && node.id) {
  8380. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  8381. } else {
  8382. super.registerFunctionStatementId(...arguments);
  8383. }
  8384. }
  8385. tsCheckForInvalidTypeCasts(items) {
  8386. items.forEach(node => {
  8387. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8388. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  8389. at: node.typeAnnotation
  8390. });
  8391. }
  8392. });
  8393. }
  8394. toReferencedList(exprList, isInParens) {
  8395. this.tsCheckForInvalidTypeCasts(exprList);
  8396. return exprList;
  8397. }
  8398. parseArrayLike(...args) {
  8399. const node = super.parseArrayLike(...args);
  8400. if (node.type === "ArrayExpression") {
  8401. this.tsCheckForInvalidTypeCasts(node.elements);
  8402. }
  8403. return node;
  8404. }
  8405. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8406. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8407. this.state.canStartJSXElement = false;
  8408. this.next();
  8409. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  8410. nonNullExpression.expression = base;
  8411. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8412. }
  8413. let isOptionalCall = false;
  8414. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8415. if (noCalls) {
  8416. state.stop = true;
  8417. return base;
  8418. }
  8419. state.optionalChainMember = isOptionalCall = true;
  8420. this.next();
  8421. }
  8422. if (this.match(47) || this.match(51)) {
  8423. let missingParenErrorLoc;
  8424. const result = this.tsTryParseAndCatch(() => {
  8425. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8426. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  8427. if (asyncArrowFn) {
  8428. return asyncArrowFn;
  8429. }
  8430. }
  8431. const node = this.startNodeAt(startPos, startLoc);
  8432. node.callee = base;
  8433. const typeArguments = this.tsParseTypeArgumentsInExpression();
  8434. if (typeArguments) {
  8435. if (isOptionalCall && !this.match(10)) {
  8436. missingParenErrorLoc = this.state.curPosition();
  8437. this.unexpected();
  8438. }
  8439. if (!noCalls && this.eat(10)) {
  8440. node.arguments = this.parseCallExpressionArguments(11, false);
  8441. this.tsCheckForInvalidTypeCasts(node.arguments);
  8442. node.typeParameters = typeArguments;
  8443. if (state.optionalChainMember) {
  8444. node.optional = isOptionalCall;
  8445. }
  8446. return this.finishCallExpression(node, state.optionalChainMember);
  8447. } else if (tokenIsTemplate(this.state.type)) {
  8448. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8449. result.typeParameters = typeArguments;
  8450. return result;
  8451. }
  8452. }
  8453. this.unexpected();
  8454. });
  8455. if (missingParenErrorLoc) {
  8456. this.unexpected(missingParenErrorLoc, 10);
  8457. }
  8458. if (result) return result;
  8459. }
  8460. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  8461. }
  8462. parseNewArguments(node) {
  8463. if (this.match(47) || this.match(51)) {
  8464. const typeParameters = this.tsTryParseAndCatch(() => {
  8465. const args = this.tsParseTypeArgumentsInExpression();
  8466. if (!this.match(10)) this.unexpected();
  8467. return args;
  8468. });
  8469. if (typeParameters) {
  8470. node.typeParameters = typeParameters;
  8471. }
  8472. }
  8473. super.parseNewArguments(node);
  8474. }
  8475. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8476. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual(93)) {
  8477. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8478. node.expression = left;
  8479. const _const = this.tsTryNextParseConstantContext();
  8480. if (_const) {
  8481. node.typeAnnotation = _const;
  8482. } else {
  8483. node.typeAnnotation = this.tsNextThenParseType();
  8484. }
  8485. this.finishNode(node, "TSAsExpression");
  8486. this.reScan_lt_gt();
  8487. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8488. }
  8489. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  8490. }
  8491. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  8492. if (!this.state.isAmbientContext) {
  8493. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  8494. }
  8495. }
  8496. checkDuplicateExports() {}
  8497. parseImport(node) {
  8498. node.importKind = "value";
  8499. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  8500. let ahead = this.lookahead();
  8501. if (this.isContextual(126) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  8502. node.importKind = "type";
  8503. this.next();
  8504. ahead = this.lookahead();
  8505. }
  8506. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  8507. return this.tsParseImportEqualsDeclaration(node);
  8508. }
  8509. }
  8510. const importNode = super.parseImport(node);
  8511. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  8512. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  8513. at: importNode
  8514. });
  8515. }
  8516. return importNode;
  8517. }
  8518. parseExport(node) {
  8519. if (this.match(83)) {
  8520. this.next();
  8521. if (this.isContextual(126) && this.lookaheadCharCode() !== 61) {
  8522. node.importKind = "type";
  8523. this.next();
  8524. } else {
  8525. node.importKind = "value";
  8526. }
  8527. return this.tsParseImportEqualsDeclaration(node, true);
  8528. } else if (this.eat(29)) {
  8529. const assign = node;
  8530. assign.expression = this.parseExpression();
  8531. this.semicolon();
  8532. return this.finishNode(assign, "TSExportAssignment");
  8533. } else if (this.eatContextual(93)) {
  8534. const decl = node;
  8535. this.expectContextual(124);
  8536. decl.id = this.parseIdentifier();
  8537. this.semicolon();
  8538. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  8539. } else {
  8540. if (this.isContextual(126) && this.lookahead().type === 5) {
  8541. this.next();
  8542. node.exportKind = "type";
  8543. } else {
  8544. node.exportKind = "value";
  8545. }
  8546. return super.parseExport(node);
  8547. }
  8548. }
  8549. isAbstractClass() {
  8550. return this.isContextual(120) && this.lookahead().type === 80;
  8551. }
  8552. parseExportDefaultExpression() {
  8553. if (this.isAbstractClass()) {
  8554. const cls = this.startNode();
  8555. this.next();
  8556. cls.abstract = true;
  8557. this.parseClass(cls, true, true);
  8558. return cls;
  8559. }
  8560. if (this.match(125)) {
  8561. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8562. if (result) return result;
  8563. }
  8564. return super.parseExportDefaultExpression();
  8565. }
  8566. parseVarStatement(node, kind, allowMissingInitializer = false) {
  8567. const {
  8568. isAmbientContext
  8569. } = this.state;
  8570. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  8571. if (!isAmbientContext) return declaration;
  8572. for (const {
  8573. id,
  8574. init
  8575. } of declaration.declarations) {
  8576. if (!init) continue;
  8577. if (kind !== "const" || !!id.typeAnnotation) {
  8578. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  8579. at: init
  8580. });
  8581. } else if (init.type !== "StringLiteral" && init.type !== "BooleanLiteral" && init.type !== "NumericLiteral" && init.type !== "BigIntLiteral" && (init.type !== "TemplateLiteral" || init.expressions.length > 0) && !isPossiblyLiteralEnum(init)) {
  8582. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  8583. at: init
  8584. });
  8585. }
  8586. }
  8587. return declaration;
  8588. }
  8589. parseStatementContent(context, topLevel) {
  8590. if (this.match(75) && this.isLookaheadContextual("enum")) {
  8591. const node = this.startNode();
  8592. this.expect(75);
  8593. return this.tsParseEnumDeclaration(node, {
  8594. const: true
  8595. });
  8596. }
  8597. if (this.isContextual(122)) {
  8598. return this.tsParseEnumDeclaration(this.startNode());
  8599. }
  8600. if (this.isContextual(125)) {
  8601. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8602. if (result) return result;
  8603. }
  8604. return super.parseStatementContent(context, topLevel);
  8605. }
  8606. parseAccessModifier() {
  8607. return this.tsParseModifier(["public", "protected", "private"]);
  8608. }
  8609. tsHasSomeModifiers(member, modifiers) {
  8610. return modifiers.some(modifier => {
  8611. if (tsIsAccessModifier(modifier)) {
  8612. return member.accessibility === modifier;
  8613. }
  8614. return !!member[modifier];
  8615. });
  8616. }
  8617. tsIsStartOfStaticBlocks() {
  8618. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  8619. }
  8620. parseClassMember(classBody, member, state) {
  8621. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  8622. this.tsParseModifiers({
  8623. modified: member,
  8624. allowedModifiers: modifiers,
  8625. stopOnStartOfClassStaticBlock: true
  8626. });
  8627. const callParseClassMemberWithIsStatic = () => {
  8628. if (this.tsIsStartOfStaticBlocks()) {
  8629. this.next();
  8630. this.next();
  8631. if (this.tsHasSomeModifiers(member, modifiers)) {
  8632. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  8633. at: this.state.curPosition()
  8634. });
  8635. }
  8636. this.parseClassStaticBlock(classBody, member);
  8637. } else {
  8638. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  8639. }
  8640. };
  8641. if (member.declare) {
  8642. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  8643. } else {
  8644. callParseClassMemberWithIsStatic();
  8645. }
  8646. }
  8647. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  8648. const idx = this.tsTryParseIndexSignature(member);
  8649. if (idx) {
  8650. classBody.body.push(idx);
  8651. if (member.abstract) {
  8652. this.raise(TSErrors.IndexSignatureHasAbstract, {
  8653. at: member
  8654. });
  8655. }
  8656. if (member.accessibility) {
  8657. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  8658. at: member,
  8659. modifier: member.accessibility
  8660. });
  8661. }
  8662. if (member.declare) {
  8663. this.raise(TSErrors.IndexSignatureHasDeclare, {
  8664. at: member
  8665. });
  8666. }
  8667. if (member.override) {
  8668. this.raise(TSErrors.IndexSignatureHasOverride, {
  8669. at: member
  8670. });
  8671. }
  8672. return;
  8673. }
  8674. if (!this.state.inAbstractClass && member.abstract) {
  8675. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  8676. at: member
  8677. });
  8678. }
  8679. if (member.override) {
  8680. if (!state.hadSuperClass) {
  8681. this.raise(TSErrors.OverrideNotInSubClass, {
  8682. at: member
  8683. });
  8684. }
  8685. }
  8686. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  8687. }
  8688. parsePostMemberNameModifiers(methodOrProp) {
  8689. const optional = this.eat(17);
  8690. if (optional) methodOrProp.optional = true;
  8691. if (methodOrProp.readonly && this.match(10)) {
  8692. this.raise(TSErrors.ClassMethodHasReadonly, {
  8693. at: methodOrProp
  8694. });
  8695. }
  8696. if (methodOrProp.declare && this.match(10)) {
  8697. this.raise(TSErrors.ClassMethodHasDeclare, {
  8698. at: methodOrProp
  8699. });
  8700. }
  8701. }
  8702. parseExpressionStatement(node, expr) {
  8703. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  8704. return decl || super.parseExpressionStatement(node, expr);
  8705. }
  8706. shouldParseExportDeclaration() {
  8707. if (this.tsIsDeclarationStart()) return true;
  8708. return super.shouldParseExportDeclaration();
  8709. }
  8710. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8711. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  8712. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8713. }
  8714. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  8715. if (!result.node) {
  8716. if (result.error) {
  8717. super.setOptionalParametersError(refExpressionErrors, result.error);
  8718. }
  8719. return expr;
  8720. }
  8721. if (result.error) this.state = result.failState;
  8722. return result.node;
  8723. }
  8724. parseParenItem(node, startPos, startLoc) {
  8725. node = super.parseParenItem(node, startPos, startLoc);
  8726. if (this.eat(17)) {
  8727. node.optional = true;
  8728. this.resetEndLocation(node);
  8729. }
  8730. if (this.match(14)) {
  8731. const typeCastNode = this.startNodeAt(startPos, startLoc);
  8732. typeCastNode.expression = node;
  8733. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  8734. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  8735. }
  8736. return node;
  8737. }
  8738. parseExportDeclaration(node) {
  8739. if (!this.state.isAmbientContext && this.isContextual(121)) {
  8740. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  8741. }
  8742. const startPos = this.state.start;
  8743. const startLoc = this.state.startLoc;
  8744. const isDeclare = this.eatContextual(121);
  8745. if (isDeclare && (this.isContextual(121) || !this.shouldParseExportDeclaration())) {
  8746. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  8747. at: this.state.startLoc
  8748. });
  8749. }
  8750. const isIdentifier = tokenIsIdentifier(this.state.type);
  8751. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  8752. if (!declaration) return null;
  8753. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  8754. node.exportKind = "type";
  8755. }
  8756. if (isDeclare) {
  8757. this.resetStartLocation(declaration, startPos, startLoc);
  8758. declaration.declare = true;
  8759. }
  8760. return declaration;
  8761. }
  8762. parseClassId(node, isStatement, optionalId) {
  8763. if ((!isStatement || optionalId) && this.isContextual(110)) {
  8764. return;
  8765. }
  8766. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  8767. const typeParameters = this.tsTryParseTypeParameters();
  8768. if (typeParameters) node.typeParameters = typeParameters;
  8769. }
  8770. parseClassPropertyAnnotation(node) {
  8771. if (!node.optional && this.eat(35)) {
  8772. node.definite = true;
  8773. }
  8774. const type = this.tsTryParseTypeAnnotation();
  8775. if (type) node.typeAnnotation = type;
  8776. }
  8777. parseClassProperty(node) {
  8778. this.parseClassPropertyAnnotation(node);
  8779. if (this.state.isAmbientContext && this.match(29)) {
  8780. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  8781. at: this.state.startLoc
  8782. });
  8783. }
  8784. if (node.abstract && this.match(29)) {
  8785. const {
  8786. key
  8787. } = node;
  8788. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  8789. at: this.state.startLoc,
  8790. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  8791. });
  8792. }
  8793. return super.parseClassProperty(node);
  8794. }
  8795. parseClassPrivateProperty(node) {
  8796. if (node.abstract) {
  8797. this.raise(TSErrors.PrivateElementHasAbstract, {
  8798. at: node
  8799. });
  8800. }
  8801. if (node.accessibility) {
  8802. this.raise(TSErrors.PrivateElementHasAccessibility, {
  8803. at: node,
  8804. modifier: node.accessibility
  8805. });
  8806. }
  8807. this.parseClassPropertyAnnotation(node);
  8808. return super.parseClassPrivateProperty(node);
  8809. }
  8810. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  8811. const typeParameters = this.tsTryParseTypeParameters();
  8812. if (typeParameters && isConstructor) {
  8813. this.raise(TSErrors.ConstructorHasTypeParameters, {
  8814. at: typeParameters
  8815. });
  8816. }
  8817. const {
  8818. declare = false,
  8819. kind
  8820. } = method;
  8821. if (declare && (kind === "get" || kind === "set")) {
  8822. this.raise(TSErrors.DeclareAccessor, {
  8823. at: method,
  8824. kind
  8825. });
  8826. }
  8827. if (typeParameters) method.typeParameters = typeParameters;
  8828. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  8829. }
  8830. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  8831. const typeParameters = this.tsTryParseTypeParameters();
  8832. if (typeParameters) method.typeParameters = typeParameters;
  8833. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  8834. }
  8835. declareClassPrivateMethodInScope(node, kind) {
  8836. if (node.type === "TSDeclareMethod") return;
  8837. if (node.type === "MethodDefinition" && !node.value.body) return;
  8838. super.declareClassPrivateMethodInScope(node, kind);
  8839. }
  8840. parseClassSuper(node) {
  8841. super.parseClassSuper(node);
  8842. if (node.superClass && (this.match(47) || this.match(51))) {
  8843. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  8844. }
  8845. if (this.eatContextual(110)) {
  8846. node.implements = this.tsParseHeritageClause("implements");
  8847. }
  8848. }
  8849. parseObjPropValue(prop, ...args) {
  8850. const typeParameters = this.tsTryParseTypeParameters();
  8851. if (typeParameters) prop.typeParameters = typeParameters;
  8852. super.parseObjPropValue(prop, ...args);
  8853. }
  8854. parseFunctionParams(node, allowModifiers) {
  8855. const typeParameters = this.tsTryParseTypeParameters();
  8856. if (typeParameters) node.typeParameters = typeParameters;
  8857. super.parseFunctionParams(node, allowModifiers);
  8858. }
  8859. parseVarId(decl, kind) {
  8860. super.parseVarId(decl, kind);
  8861. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  8862. decl.definite = true;
  8863. }
  8864. const type = this.tsTryParseTypeAnnotation();
  8865. if (type) {
  8866. decl.id.typeAnnotation = type;
  8867. this.resetEndLocation(decl.id);
  8868. }
  8869. }
  8870. parseAsyncArrowFromCallExpression(node, call) {
  8871. if (this.match(14)) {
  8872. node.returnType = this.tsParseTypeAnnotation();
  8873. }
  8874. return super.parseAsyncArrowFromCallExpression(node, call);
  8875. }
  8876. parseMaybeAssign(...args) {
  8877. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  8878. let state;
  8879. let jsx;
  8880. let typeCast;
  8881. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  8882. state = this.state.clone();
  8883. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8884. if (!jsx.error) return jsx.node;
  8885. const {
  8886. context
  8887. } = this.state;
  8888. const currentContext = context[context.length - 1];
  8889. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  8890. context.pop();
  8891. }
  8892. }
  8893. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  8894. return super.parseMaybeAssign(...args);
  8895. }
  8896. let typeParameters;
  8897. state = state || this.state.clone();
  8898. const arrow = this.tryParse(abort => {
  8899. var _expr$extra, _typeParameters, _expr$typeParameters$;
  8900. typeParameters = this.tsParseTypeParameters();
  8901. const expr = super.parseMaybeAssign(...args);
  8902. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  8903. abort();
  8904. }
  8905. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  8906. this.resetStartLocationFromNode(expr, typeParameters);
  8907. }
  8908. expr.typeParameters = typeParameters;
  8909. if (this.hasPlugin("jsx") && expr.typeParameters.params.length === 1 && !((_expr$typeParameters$ = expr.typeParameters.extra) != null && _expr$typeParameters$.trailingComma)) {
  8910. const parameter = expr.typeParameters.params[0];
  8911. if (!parameter.constraint) ;
  8912. }
  8913. return expr;
  8914. }, state);
  8915. if (!arrow.error && !arrow.aborted) {
  8916. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8917. return arrow.node;
  8918. }
  8919. if (!jsx) {
  8920. assert(!this.hasPlugin("jsx"));
  8921. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8922. if (!typeCast.error) return typeCast.node;
  8923. }
  8924. if ((_jsx2 = jsx) != null && _jsx2.node) {
  8925. this.state = jsx.failState;
  8926. return jsx.node;
  8927. }
  8928. if (arrow.node) {
  8929. this.state = arrow.failState;
  8930. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8931. return arrow.node;
  8932. }
  8933. if ((_typeCast = typeCast) != null && _typeCast.node) {
  8934. this.state = typeCast.failState;
  8935. return typeCast.node;
  8936. }
  8937. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  8938. if (arrow.thrown) throw arrow.error;
  8939. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  8940. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  8941. }
  8942. reportReservedArrowTypeParam(node) {
  8943. var _node$extra;
  8944. if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8945. this.raise(TSErrors.ReservedArrowTypeParam, {
  8946. at: node
  8947. });
  8948. }
  8949. }
  8950. parseMaybeUnary(refExpressionErrors) {
  8951. if (!this.hasPlugin("jsx") && this.match(47)) {
  8952. return this.tsParseTypeAssertion();
  8953. } else {
  8954. return super.parseMaybeUnary(refExpressionErrors);
  8955. }
  8956. }
  8957. parseArrow(node) {
  8958. if (this.match(14)) {
  8959. const result = this.tryParse(abort => {
  8960. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8961. if (this.canInsertSemicolon() || !this.match(19)) abort();
  8962. return returnType;
  8963. });
  8964. if (result.aborted) return;
  8965. if (!result.thrown) {
  8966. if (result.error) this.state = result.failState;
  8967. node.returnType = result.node;
  8968. }
  8969. }
  8970. return super.parseArrow(node);
  8971. }
  8972. parseAssignableListItemTypes(param) {
  8973. if (this.eat(17)) {
  8974. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  8975. this.raise(TSErrors.PatternIsOptional, {
  8976. at: param
  8977. });
  8978. }
  8979. param.optional = true;
  8980. }
  8981. const type = this.tsTryParseTypeAnnotation();
  8982. if (type) param.typeAnnotation = type;
  8983. this.resetEndLocation(param);
  8984. return param;
  8985. }
  8986. isAssignable(node, isBinding) {
  8987. switch (node.type) {
  8988. case "TSTypeCastExpression":
  8989. return this.isAssignable(node.expression, isBinding);
  8990. case "TSParameterProperty":
  8991. return true;
  8992. default:
  8993. return super.isAssignable(node, isBinding);
  8994. }
  8995. }
  8996. toAssignable(node, isLHS = false) {
  8997. switch (node.type) {
  8998. case "TSTypeCastExpression":
  8999. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  9000. case "TSParameterProperty":
  9001. return super.toAssignable(node, isLHS);
  9002. case "ParenthesizedExpression":
  9003. return this.toAssignableParenthesizedExpression(node, isLHS);
  9004. case "TSAsExpression":
  9005. case "TSNonNullExpression":
  9006. case "TSTypeAssertion":
  9007. node.expression = this.toAssignable(node.expression, isLHS);
  9008. return node;
  9009. default:
  9010. return super.toAssignable(node, isLHS);
  9011. }
  9012. }
  9013. toAssignableParenthesizedExpression(node, isLHS) {
  9014. switch (node.expression.type) {
  9015. case "TSAsExpression":
  9016. case "TSNonNullExpression":
  9017. case "TSTypeAssertion":
  9018. case "ParenthesizedExpression":
  9019. node.expression = this.toAssignable(node.expression, isLHS);
  9020. return node;
  9021. default:
  9022. return super.toAssignable(node, isLHS);
  9023. }
  9024. }
  9025. isValidLVal(type, isParenthesized, binding) {
  9026. return getOwn$1({
  9027. TSTypeCastExpression: true,
  9028. TSParameterProperty: "parameter",
  9029. TSNonNullExpression: "expression",
  9030. TSAsExpression: (binding !== BIND_NONE || isParenthesized) && ["expression", true],
  9031. TSTypeAssertion: (binding !== BIND_NONE || isParenthesized) && ["expression", true]
  9032. }, type) || super.isValidLVal(type, isParenthesized, binding);
  9033. }
  9034. parseBindingAtom() {
  9035. switch (this.state.type) {
  9036. case 78:
  9037. return this.parseIdentifier(true);
  9038. default:
  9039. return super.parseBindingAtom();
  9040. }
  9041. }
  9042. parseMaybeDecoratorArguments(expr) {
  9043. if (this.match(47) || this.match(51)) {
  9044. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9045. if (this.match(10)) {
  9046. const call = super.parseMaybeDecoratorArguments(expr);
  9047. call.typeParameters = typeArguments;
  9048. return call;
  9049. }
  9050. this.unexpected(null, 10);
  9051. }
  9052. return super.parseMaybeDecoratorArguments(expr);
  9053. }
  9054. checkCommaAfterRest(close) {
  9055. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9056. this.next();
  9057. return false;
  9058. } else {
  9059. return super.checkCommaAfterRest(close);
  9060. }
  9061. }
  9062. isClassMethod() {
  9063. return this.match(47) || super.isClassMethod();
  9064. }
  9065. isClassProperty() {
  9066. return this.match(35) || this.match(14) || super.isClassProperty();
  9067. }
  9068. parseMaybeDefault(...args) {
  9069. const node = super.parseMaybeDefault(...args);
  9070. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9071. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9072. at: node.typeAnnotation
  9073. });
  9074. }
  9075. return node;
  9076. }
  9077. getTokenFromCode(code) {
  9078. if (this.state.inType) {
  9079. if (code === 62) {
  9080. return this.finishOp(48, 1);
  9081. }
  9082. if (code === 60) {
  9083. return this.finishOp(47, 1);
  9084. }
  9085. }
  9086. return super.getTokenFromCode(code);
  9087. }
  9088. reScan_lt_gt() {
  9089. const {
  9090. type
  9091. } = this.state;
  9092. if (type === 47) {
  9093. this.state.pos -= 1;
  9094. this.readToken_lt();
  9095. } else if (type === 48) {
  9096. this.state.pos -= 1;
  9097. this.readToken_gt();
  9098. }
  9099. }
  9100. reScan_lt() {
  9101. const {
  9102. type
  9103. } = this.state;
  9104. if (type === 51) {
  9105. this.state.pos -= 2;
  9106. this.finishOp(47, 1);
  9107. return 47;
  9108. }
  9109. return type;
  9110. }
  9111. toAssignableList(exprList) {
  9112. for (let i = 0; i < exprList.length; i++) {
  9113. const expr = exprList[i];
  9114. if (!expr) continue;
  9115. switch (expr.type) {
  9116. case "TSTypeCastExpression":
  9117. exprList[i] = this.typeCastToParameter(expr);
  9118. break;
  9119. case "TSAsExpression":
  9120. case "TSTypeAssertion":
  9121. if (!this.state.maybeInArrowParameters) {
  9122. exprList[i] = this.typeCastToParameter(expr);
  9123. } else {
  9124. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9125. at: expr
  9126. });
  9127. }
  9128. break;
  9129. }
  9130. }
  9131. return super.toAssignableList(...arguments);
  9132. }
  9133. typeCastToParameter(node) {
  9134. node.expression.typeAnnotation = node.typeAnnotation;
  9135. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9136. return node.expression;
  9137. }
  9138. shouldParseArrow(params) {
  9139. if (this.match(14)) {
  9140. return params.every(expr => this.isAssignable(expr, true));
  9141. }
  9142. return super.shouldParseArrow(params);
  9143. }
  9144. shouldParseAsyncArrow() {
  9145. return this.match(14) || super.shouldParseAsyncArrow();
  9146. }
  9147. canHaveLeadingDecorator() {
  9148. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9149. }
  9150. jsxParseOpeningElementAfterName(node) {
  9151. if (this.match(47) || this.match(51)) {
  9152. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9153. if (typeArguments) node.typeParameters = typeArguments;
  9154. }
  9155. return super.jsxParseOpeningElementAfterName(node);
  9156. }
  9157. getGetterSetterExpectedParamCount(method) {
  9158. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9159. const params = this.getObjectOrClassMethodParams(method);
  9160. const firstParam = params[0];
  9161. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9162. return hasContextParam ? baseCount + 1 : baseCount;
  9163. }
  9164. parseCatchClauseParam() {
  9165. const param = super.parseCatchClauseParam();
  9166. const type = this.tsTryParseTypeAnnotation();
  9167. if (type) {
  9168. param.typeAnnotation = type;
  9169. this.resetEndLocation(param);
  9170. }
  9171. return param;
  9172. }
  9173. tsInAmbientContext(cb) {
  9174. const oldIsAmbientContext = this.state.isAmbientContext;
  9175. this.state.isAmbientContext = true;
  9176. try {
  9177. return cb();
  9178. } finally {
  9179. this.state.isAmbientContext = oldIsAmbientContext;
  9180. }
  9181. }
  9182. parseClass(node, ...args) {
  9183. const oldInAbstractClass = this.state.inAbstractClass;
  9184. this.state.inAbstractClass = !!node.abstract;
  9185. try {
  9186. return super.parseClass(node, ...args);
  9187. } finally {
  9188. this.state.inAbstractClass = oldInAbstractClass;
  9189. }
  9190. }
  9191. tsParseAbstractDeclaration(node) {
  9192. if (this.match(80)) {
  9193. node.abstract = true;
  9194. return this.parseClass(node, true, false);
  9195. } else if (this.isContextual(125)) {
  9196. if (!this.hasFollowingLineBreak()) {
  9197. node.abstract = true;
  9198. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9199. at: node
  9200. });
  9201. return this.tsParseInterfaceDeclaration(node);
  9202. }
  9203. } else {
  9204. this.unexpected(null, 80);
  9205. }
  9206. }
  9207. parseMethod(...args) {
  9208. const method = super.parseMethod(...args);
  9209. if (method.abstract) {
  9210. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9211. if (hasBody) {
  9212. const {
  9213. key
  9214. } = method;
  9215. this.raise(TSErrors.AbstractMethodHasImplementation, {
  9216. at: method,
  9217. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9218. });
  9219. }
  9220. }
  9221. return method;
  9222. }
  9223. tsParseTypeParameterName() {
  9224. const typeName = this.parseIdentifier();
  9225. return typeName.name;
  9226. }
  9227. shouldParseAsAmbientContext() {
  9228. return !!this.getPluginOption("typescript", "dts");
  9229. }
  9230. parse() {
  9231. if (this.shouldParseAsAmbientContext()) {
  9232. this.state.isAmbientContext = true;
  9233. }
  9234. return super.parse();
  9235. }
  9236. getExpression() {
  9237. if (this.shouldParseAsAmbientContext()) {
  9238. this.state.isAmbientContext = true;
  9239. }
  9240. return super.getExpression();
  9241. }
  9242. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9243. if (!isString && isMaybeTypeOnly) {
  9244. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9245. return this.finishNode(node, "ExportSpecifier");
  9246. }
  9247. node.exportKind = "value";
  9248. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9249. }
  9250. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  9251. if (!importedIsString && isMaybeTypeOnly) {
  9252. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9253. return this.finishNode(specifier, "ImportSpecifier");
  9254. }
  9255. specifier.importKind = "value";
  9256. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly);
  9257. }
  9258. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9259. const leftOfAsKey = isImport ? "imported" : "local";
  9260. const rightOfAsKey = isImport ? "local" : "exported";
  9261. let leftOfAs = node[leftOfAsKey];
  9262. let rightOfAs;
  9263. let hasTypeSpecifier = false;
  9264. let canParseAsKeyword = true;
  9265. const loc = leftOfAs.loc.start;
  9266. if (this.isContextual(93)) {
  9267. const firstAs = this.parseIdentifier();
  9268. if (this.isContextual(93)) {
  9269. const secondAs = this.parseIdentifier();
  9270. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9271. hasTypeSpecifier = true;
  9272. leftOfAs = firstAs;
  9273. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9274. canParseAsKeyword = false;
  9275. } else {
  9276. rightOfAs = secondAs;
  9277. canParseAsKeyword = false;
  9278. }
  9279. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9280. canParseAsKeyword = false;
  9281. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9282. } else {
  9283. hasTypeSpecifier = true;
  9284. leftOfAs = firstAs;
  9285. }
  9286. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9287. hasTypeSpecifier = true;
  9288. leftOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9289. }
  9290. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  9291. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  9292. at: loc
  9293. });
  9294. }
  9295. node[leftOfAsKey] = leftOfAs;
  9296. node[rightOfAsKey] = rightOfAs;
  9297. const kindKey = isImport ? "importKind" : "exportKind";
  9298. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  9299. if (canParseAsKeyword && this.eatContextual(93)) {
  9300. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9301. }
  9302. if (!node[rightOfAsKey]) {
  9303. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  9304. }
  9305. if (isImport) {
  9306. this.checkIdentifier(node[rightOfAsKey], BIND_LEXICAL);
  9307. }
  9308. }
  9309. });
  9310. function isPossiblyLiteralEnum(expression) {
  9311. if (expression.type !== "MemberExpression") return false;
  9312. const {
  9313. computed,
  9314. property
  9315. } = expression;
  9316. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  9317. return false;
  9318. }
  9319. return isUncomputedMemberExpressionChain(expression.object);
  9320. }
  9321. function isUncomputedMemberExpressionChain(expression) {
  9322. if (expression.type === "Identifier") return true;
  9323. if (expression.type !== "MemberExpression") return false;
  9324. if (expression.computed) return false;
  9325. return isUncomputedMemberExpressionChain(expression.object);
  9326. }
  9327. const PlaceholderErrors = ParseErrorEnum`placeholders`(_ => ({
  9328. ClassNameIsRequired: _("A class name is required."),
  9329. UnexpectedSpace: _("Unexpected space in placeholder.")
  9330. }));
  9331. var placeholders = (superClass => class extends superClass {
  9332. parsePlaceholder(expectedNode) {
  9333. if (this.match(140)) {
  9334. const node = this.startNode();
  9335. this.next();
  9336. this.assertNoSpace();
  9337. node.name = super.parseIdentifier(true);
  9338. this.assertNoSpace();
  9339. this.expect(140);
  9340. return this.finishPlaceholder(node, expectedNode);
  9341. }
  9342. }
  9343. finishPlaceholder(node, expectedNode) {
  9344. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  9345. node.expectedNode = expectedNode;
  9346. return isFinished ? node : this.finishNode(node, "Placeholder");
  9347. }
  9348. getTokenFromCode(code) {
  9349. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  9350. return this.finishOp(140, 2);
  9351. }
  9352. return super.getTokenFromCode(...arguments);
  9353. }
  9354. parseExprAtom() {
  9355. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  9356. }
  9357. parseIdentifier() {
  9358. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  9359. }
  9360. checkReservedWord(word) {
  9361. if (word !== undefined) super.checkReservedWord(...arguments);
  9362. }
  9363. parseBindingAtom() {
  9364. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  9365. }
  9366. isValidLVal(type, ...rest) {
  9367. return type === "Placeholder" || super.isValidLVal(type, ...rest);
  9368. }
  9369. toAssignable(node) {
  9370. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  9371. node.expectedNode = "Pattern";
  9372. return node;
  9373. }
  9374. return super.toAssignable(...arguments);
  9375. }
  9376. isLet(context) {
  9377. if (super.isLet(context)) {
  9378. return true;
  9379. }
  9380. if (!this.isContextual(99)) {
  9381. return false;
  9382. }
  9383. if (context) return false;
  9384. const nextToken = this.lookahead();
  9385. if (nextToken.type === 140) {
  9386. return true;
  9387. }
  9388. return false;
  9389. }
  9390. verifyBreakContinue(node) {
  9391. if (node.label && node.label.type === "Placeholder") return;
  9392. super.verifyBreakContinue(...arguments);
  9393. }
  9394. parseExpressionStatement(node, expr) {
  9395. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  9396. return super.parseExpressionStatement(...arguments);
  9397. }
  9398. if (this.match(14)) {
  9399. const stmt = node;
  9400. stmt.label = this.finishPlaceholder(expr, "Identifier");
  9401. this.next();
  9402. stmt.body = this.parseStatement("label");
  9403. return this.finishNode(stmt, "LabeledStatement");
  9404. }
  9405. this.semicolon();
  9406. node.name = expr.name;
  9407. return this.finishPlaceholder(node, "Statement");
  9408. }
  9409. parseBlock() {
  9410. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  9411. }
  9412. parseFunctionId() {
  9413. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  9414. }
  9415. parseClass(node, isStatement, optionalId) {
  9416. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  9417. this.next();
  9418. this.takeDecorators(node);
  9419. const oldStrict = this.state.strict;
  9420. const placeholder = this.parsePlaceholder("Identifier");
  9421. if (placeholder) {
  9422. if (this.match(81) || this.match(140) || this.match(5)) {
  9423. node.id = placeholder;
  9424. } else if (optionalId || !isStatement) {
  9425. node.id = null;
  9426. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  9427. return this.finishNode(node, type);
  9428. } else {
  9429. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  9430. at: this.state.startLoc
  9431. });
  9432. }
  9433. } else {
  9434. this.parseClassId(node, isStatement, optionalId);
  9435. }
  9436. this.parseClassSuper(node);
  9437. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  9438. return this.finishNode(node, type);
  9439. }
  9440. parseExport(node) {
  9441. const placeholder = this.parsePlaceholder("Identifier");
  9442. if (!placeholder) return super.parseExport(...arguments);
  9443. if (!this.isContextual(97) && !this.match(12)) {
  9444. node.specifiers = [];
  9445. node.source = null;
  9446. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  9447. return this.finishNode(node, "ExportNamedDeclaration");
  9448. }
  9449. this.expectPlugin("exportDefaultFrom");
  9450. const specifier = this.startNode();
  9451. specifier.exported = placeholder;
  9452. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9453. return super.parseExport(node);
  9454. }
  9455. isExportDefaultSpecifier() {
  9456. if (this.match(65)) {
  9457. const next = this.nextTokenStart();
  9458. if (this.isUnparsedContextual(next, "from")) {
  9459. if (this.input.startsWith(tokenLabelName(140), this.nextTokenStartSince(next + 4))) {
  9460. return true;
  9461. }
  9462. }
  9463. }
  9464. return super.isExportDefaultSpecifier();
  9465. }
  9466. maybeParseExportDefaultSpecifier(node) {
  9467. if (node.specifiers && node.specifiers.length > 0) {
  9468. return true;
  9469. }
  9470. return super.maybeParseExportDefaultSpecifier(...arguments);
  9471. }
  9472. checkExport(node) {
  9473. const {
  9474. specifiers
  9475. } = node;
  9476. if (specifiers != null && specifiers.length) {
  9477. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  9478. }
  9479. super.checkExport(node);
  9480. node.specifiers = specifiers;
  9481. }
  9482. parseImport(node) {
  9483. const placeholder = this.parsePlaceholder("Identifier");
  9484. if (!placeholder) return super.parseImport(...arguments);
  9485. node.specifiers = [];
  9486. if (!this.isContextual(97) && !this.match(12)) {
  9487. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  9488. this.semicolon();
  9489. return this.finishNode(node, "ImportDeclaration");
  9490. }
  9491. const specifier = this.startNodeAtNode(placeholder);
  9492. specifier.local = placeholder;
  9493. this.finishNode(specifier, "ImportDefaultSpecifier");
  9494. node.specifiers.push(specifier);
  9495. if (this.eat(12)) {
  9496. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  9497. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  9498. }
  9499. this.expectContextual(97);
  9500. node.source = this.parseImportSource();
  9501. this.semicolon();
  9502. return this.finishNode(node, "ImportDeclaration");
  9503. }
  9504. parseImportSource() {
  9505. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  9506. }
  9507. assertNoSpace() {
  9508. if (this.state.start > this.state.lastTokEndLoc.index) {
  9509. this.raise(PlaceholderErrors.UnexpectedSpace, {
  9510. at: this.state.lastTokEndLoc
  9511. });
  9512. }
  9513. }
  9514. });
  9515. var v8intrinsic = (superClass => class extends superClass {
  9516. parseV8Intrinsic() {
  9517. if (this.match(54)) {
  9518. const v8IntrinsicStartLoc = this.state.startLoc;
  9519. const node = this.startNode();
  9520. this.next();
  9521. if (tokenIsIdentifier(this.state.type)) {
  9522. const name = this.parseIdentifierName(this.state.start);
  9523. const identifier = this.createIdentifier(node, name);
  9524. identifier.type = "V8IntrinsicIdentifier";
  9525. if (this.match(10)) {
  9526. return identifier;
  9527. }
  9528. }
  9529. this.unexpected(v8IntrinsicStartLoc);
  9530. }
  9531. }
  9532. parseExprAtom() {
  9533. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  9534. }
  9535. });
  9536. function hasPlugin(plugins, expectedConfig) {
  9537. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  9538. const expectedKeys = Object.keys(expectedOptions);
  9539. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  9540. return plugins.some(p => {
  9541. if (typeof p === "string") {
  9542. return expectedOptionsIsEmpty && p === expectedName;
  9543. } else {
  9544. const [pluginName, pluginOptions] = p;
  9545. if (pluginName !== expectedName) {
  9546. return false;
  9547. }
  9548. for (const key of expectedKeys) {
  9549. if (pluginOptions[key] !== expectedOptions[key]) {
  9550. return false;
  9551. }
  9552. }
  9553. return true;
  9554. }
  9555. });
  9556. }
  9557. function getPluginOption(plugins, name, option) {
  9558. const plugin = plugins.find(plugin => {
  9559. if (Array.isArray(plugin)) {
  9560. return plugin[0] === name;
  9561. } else {
  9562. return plugin === name;
  9563. }
  9564. });
  9565. if (plugin && Array.isArray(plugin)) {
  9566. return plugin[1][option];
  9567. }
  9568. return null;
  9569. }
  9570. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  9571. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  9572. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  9573. function validatePlugins(plugins) {
  9574. if (hasPlugin(plugins, "decorators")) {
  9575. if (hasPlugin(plugins, "decorators-legacy")) {
  9576. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  9577. }
  9578. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  9579. if (decoratorsBeforeExport == null) {
  9580. throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
  9581. } else if (typeof decoratorsBeforeExport !== "boolean") {
  9582. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  9583. }
  9584. }
  9585. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  9586. throw new Error("Cannot combine flow and typescript plugins.");
  9587. }
  9588. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  9589. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  9590. }
  9591. if (hasPlugin(plugins, "pipelineOperator")) {
  9592. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  9593. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  9594. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  9595. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  9596. }
  9597. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  9598. syntaxType: "hash"
  9599. }]);
  9600. if (proposal === "hack") {
  9601. if (hasPlugin(plugins, "placeholders")) {
  9602. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  9603. }
  9604. if (hasPlugin(plugins, "v8intrinsic")) {
  9605. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  9606. }
  9607. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  9608. if (!TOPIC_TOKENS.includes(topicToken)) {
  9609. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  9610. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  9611. }
  9612. if (topicToken === "#" && tupleSyntaxIsHash) {
  9613. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9614. }
  9615. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  9616. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9617. }
  9618. }
  9619. if (hasPlugin(plugins, "moduleAttributes")) {
  9620. {
  9621. if (hasPlugin(plugins, "importAssertions")) {
  9622. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  9623. }
  9624. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  9625. if (moduleAttributesVerionPluginOption !== "may-2020") {
  9626. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  9627. }
  9628. }
  9629. }
  9630. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  9631. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  9632. }
  9633. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  9634. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  9635. error.missingPlugins = "doExpressions";
  9636. throw error;
  9637. }
  9638. }
  9639. const mixinPlugins = {
  9640. estree,
  9641. jsx,
  9642. flow,
  9643. typescript,
  9644. v8intrinsic,
  9645. placeholders
  9646. };
  9647. const mixinPluginNames = Object.keys(mixinPlugins);
  9648. const defaultOptions = {
  9649. sourceType: "script",
  9650. sourceFilename: undefined,
  9651. startColumn: 0,
  9652. startLine: 1,
  9653. allowAwaitOutsideFunction: false,
  9654. allowReturnOutsideFunction: false,
  9655. allowImportExportEverywhere: false,
  9656. allowSuperOutsideMethod: false,
  9657. allowUndeclaredExports: false,
  9658. plugins: [],
  9659. strictMode: null,
  9660. ranges: false,
  9661. tokens: false,
  9662. createParenthesizedExpressions: false,
  9663. errorRecovery: false,
  9664. attachComment: true
  9665. };
  9666. function getOptions(opts) {
  9667. const options = {};
  9668. for (const key of Object.keys(defaultOptions)) {
  9669. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  9670. }
  9671. return options;
  9672. }
  9673. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  9674. const unwrapParenthesizedExpression = node => {
  9675. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  9676. };
  9677. class LValParser extends NodeUtils {
  9678. toAssignable(node, isLHS = false) {
  9679. var _node$extra, _node$extra3;
  9680. let parenthesized = undefined;
  9681. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  9682. parenthesized = unwrapParenthesizedExpression(node);
  9683. if (isLHS) {
  9684. if (parenthesized.type === "Identifier") {
  9685. this.expressionScope.recordParenthesizedIdentifierError({
  9686. at: node
  9687. });
  9688. } else if (parenthesized.type !== "MemberExpression") {
  9689. this.raise(Errors.InvalidParenthesizedAssignment, {
  9690. at: node
  9691. });
  9692. }
  9693. } else {
  9694. this.raise(Errors.InvalidParenthesizedAssignment, {
  9695. at: node
  9696. });
  9697. }
  9698. }
  9699. switch (node.type) {
  9700. case "Identifier":
  9701. case "ObjectPattern":
  9702. case "ArrayPattern":
  9703. case "AssignmentPattern":
  9704. case "RestElement":
  9705. break;
  9706. case "ObjectExpression":
  9707. node.type = "ObjectPattern";
  9708. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  9709. var _node$extra2;
  9710. const prop = node.properties[i];
  9711. const isLast = i === last;
  9712. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  9713. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  9714. this.raise(Errors.RestTrailingComma, {
  9715. at: node.extra.trailingCommaLoc
  9716. });
  9717. }
  9718. }
  9719. break;
  9720. case "ObjectProperty":
  9721. {
  9722. const {
  9723. key,
  9724. value
  9725. } = node;
  9726. if (this.isPrivateName(key)) {
  9727. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  9728. }
  9729. this.toAssignable(value, isLHS);
  9730. break;
  9731. }
  9732. case "SpreadElement":
  9733. {
  9734. this.checkToRestConversion(node);
  9735. node.type = "RestElement";
  9736. const arg = node.argument;
  9737. this.toAssignable(arg, isLHS);
  9738. break;
  9739. }
  9740. case "ArrayExpression":
  9741. node.type = "ArrayPattern";
  9742. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  9743. break;
  9744. case "AssignmentExpression":
  9745. if (node.operator !== "=") {
  9746. this.raise(Errors.MissingEqInAssignment, {
  9747. at: node.left.loc.end
  9748. });
  9749. }
  9750. node.type = "AssignmentPattern";
  9751. delete node.operator;
  9752. this.toAssignable(node.left, isLHS);
  9753. break;
  9754. case "ParenthesizedExpression":
  9755. this.toAssignable(parenthesized, isLHS);
  9756. break;
  9757. }
  9758. return node;
  9759. }
  9760. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  9761. if (prop.type === "ObjectMethod") {
  9762. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  9763. at: prop.key
  9764. });
  9765. } else if (prop.type === "SpreadElement" && !isLast) {
  9766. this.raise(Errors.RestTrailingComma, {
  9767. at: prop
  9768. });
  9769. } else {
  9770. this.toAssignable(prop, isLHS);
  9771. }
  9772. }
  9773. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9774. let end = exprList.length;
  9775. if (end) {
  9776. const last = exprList[end - 1];
  9777. if ((last == null ? void 0 : last.type) === "RestElement") {
  9778. --end;
  9779. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  9780. last.type = "RestElement";
  9781. let arg = last.argument;
  9782. this.toAssignable(arg, isLHS);
  9783. arg = unwrapParenthesizedExpression(arg);
  9784. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  9785. this.unexpected(arg.start);
  9786. }
  9787. if (trailingCommaLoc) {
  9788. this.raise(Errors.RestTrailingComma, {
  9789. at: trailingCommaLoc
  9790. });
  9791. }
  9792. --end;
  9793. }
  9794. }
  9795. for (let i = 0; i < end; i++) {
  9796. const elt = exprList[i];
  9797. if (elt) {
  9798. this.toAssignable(elt, isLHS);
  9799. if (elt.type === "RestElement") {
  9800. this.raise(Errors.RestTrailingComma, {
  9801. at: elt
  9802. });
  9803. }
  9804. }
  9805. }
  9806. return exprList;
  9807. }
  9808. isAssignable(node, isBinding) {
  9809. switch (node.type) {
  9810. case "Identifier":
  9811. case "ObjectPattern":
  9812. case "ArrayPattern":
  9813. case "AssignmentPattern":
  9814. case "RestElement":
  9815. return true;
  9816. case "ObjectExpression":
  9817. {
  9818. const last = node.properties.length - 1;
  9819. return node.properties.every((prop, i) => {
  9820. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  9821. });
  9822. }
  9823. case "ObjectProperty":
  9824. return this.isAssignable(node.value);
  9825. case "SpreadElement":
  9826. return this.isAssignable(node.argument);
  9827. case "ArrayExpression":
  9828. return node.elements.every(element => element === null || this.isAssignable(element));
  9829. case "AssignmentExpression":
  9830. return node.operator === "=";
  9831. case "ParenthesizedExpression":
  9832. return this.isAssignable(node.expression);
  9833. case "MemberExpression":
  9834. case "OptionalMemberExpression":
  9835. return !isBinding;
  9836. default:
  9837. return false;
  9838. }
  9839. }
  9840. toReferencedList(exprList, isParenthesizedExpr) {
  9841. return exprList;
  9842. }
  9843. toReferencedListDeep(exprList, isParenthesizedExpr) {
  9844. this.toReferencedList(exprList, isParenthesizedExpr);
  9845. for (const expr of exprList) {
  9846. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  9847. this.toReferencedListDeep(expr.elements);
  9848. }
  9849. }
  9850. }
  9851. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  9852. const node = this.startNode();
  9853. this.next();
  9854. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  9855. return this.finishNode(node, "SpreadElement");
  9856. }
  9857. parseRestBinding() {
  9858. const node = this.startNode();
  9859. this.next();
  9860. node.argument = this.parseBindingAtom();
  9861. return this.finishNode(node, "RestElement");
  9862. }
  9863. parseBindingAtom() {
  9864. switch (this.state.type) {
  9865. case 0:
  9866. {
  9867. const node = this.startNode();
  9868. this.next();
  9869. node.elements = this.parseBindingList(3, 93, true);
  9870. return this.finishNode(node, "ArrayPattern");
  9871. }
  9872. case 5:
  9873. return this.parseObjectLike(8, true);
  9874. }
  9875. return this.parseIdentifier();
  9876. }
  9877. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  9878. const elts = [];
  9879. let first = true;
  9880. while (!this.eat(close)) {
  9881. if (first) {
  9882. first = false;
  9883. } else {
  9884. this.expect(12);
  9885. }
  9886. if (allowEmpty && this.match(12)) {
  9887. elts.push(null);
  9888. } else if (this.eat(close)) {
  9889. break;
  9890. } else if (this.match(21)) {
  9891. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  9892. if (!this.checkCommaAfterRest(closeCharCode)) {
  9893. this.expect(close);
  9894. break;
  9895. }
  9896. } else {
  9897. const decorators = [];
  9898. if (this.match(26) && this.hasPlugin("decorators")) {
  9899. this.raise(Errors.UnsupportedParameterDecorator, {
  9900. at: this.state.startLoc
  9901. });
  9902. }
  9903. while (this.match(26)) {
  9904. decorators.push(this.parseDecorator());
  9905. }
  9906. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  9907. }
  9908. }
  9909. return elts;
  9910. }
  9911. parseBindingRestProperty(prop) {
  9912. this.next();
  9913. prop.argument = this.parseIdentifier();
  9914. this.checkCommaAfterRest(125);
  9915. return this.finishNode(prop, "RestElement");
  9916. }
  9917. parseBindingProperty() {
  9918. const prop = this.startNode();
  9919. const {
  9920. type,
  9921. start: startPos,
  9922. startLoc
  9923. } = this.state;
  9924. if (type === 21) {
  9925. return this.parseBindingRestProperty(prop);
  9926. } else if (type === 134) {
  9927. this.expectPlugin("destructuringPrivate", startLoc);
  9928. this.classScope.usePrivateName(this.state.value, startLoc);
  9929. prop.key = this.parsePrivateName();
  9930. } else {
  9931. this.parsePropertyName(prop);
  9932. }
  9933. prop.method = false;
  9934. this.parseObjPropValue(prop, startPos, startLoc, false, false, true, false);
  9935. return prop;
  9936. }
  9937. parseAssignableListItem(allowModifiers, decorators) {
  9938. const left = this.parseMaybeDefault();
  9939. this.parseAssignableListItemTypes(left);
  9940. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  9941. if (decorators.length) {
  9942. left.decorators = decorators;
  9943. }
  9944. return elt;
  9945. }
  9946. parseAssignableListItemTypes(param) {
  9947. return param;
  9948. }
  9949. parseMaybeDefault(startPos, startLoc, left) {
  9950. var _startLoc, _startPos, _left;
  9951. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  9952. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  9953. left = (_left = left) != null ? _left : this.parseBindingAtom();
  9954. if (!this.eat(29)) return left;
  9955. const node = this.startNodeAt(startPos, startLoc);
  9956. node.left = left;
  9957. node.right = this.parseMaybeAssignAllowIn();
  9958. return this.finishNode(node, "AssignmentPattern");
  9959. }
  9960. isValidLVal(type, isParenthesized, binding) {
  9961. return getOwn({
  9962. AssignmentPattern: "left",
  9963. RestElement: "argument",
  9964. ObjectProperty: "value",
  9965. ParenthesizedExpression: "expression",
  9966. ArrayPattern: "elements",
  9967. ObjectPattern: "properties"
  9968. }, type);
  9969. }
  9970. checkLVal(expression, {
  9971. in: ancestor,
  9972. binding = BIND_NONE,
  9973. checkClashes = false,
  9974. strictModeChanged = false,
  9975. allowingSloppyLetBinding = !(binding & BIND_SCOPE_LEXICAL),
  9976. hasParenthesizedAncestor = false
  9977. }) {
  9978. var _expression$extra;
  9979. const type = expression.type;
  9980. if (this.isObjectMethod(expression)) return;
  9981. if (type === "MemberExpression") {
  9982. if (binding !== BIND_NONE) {
  9983. this.raise(Errors.InvalidPropertyBindingPattern, {
  9984. at: expression
  9985. });
  9986. }
  9987. return;
  9988. }
  9989. if (expression.type === "Identifier") {
  9990. this.checkIdentifier(expression, binding, strictModeChanged, allowingSloppyLetBinding);
  9991. const {
  9992. name
  9993. } = expression;
  9994. if (checkClashes) {
  9995. if (checkClashes.has(name)) {
  9996. this.raise(Errors.ParamDupe, {
  9997. at: expression
  9998. });
  9999. } else {
  10000. checkClashes.add(name);
  10001. }
  10002. }
  10003. return;
  10004. }
  10005. const validity = this.isValidLVal(expression.type, hasParenthesizedAncestor || ((_expression$extra = expression.extra) == null ? void 0 : _expression$extra.parenthesized), binding);
  10006. if (validity === true) return;
  10007. if (validity === false) {
  10008. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  10009. this.raise(ParseErrorClass, {
  10010. at: expression,
  10011. ancestor: ancestor.type === "UpdateExpression" ? {
  10012. type: "UpdateExpression",
  10013. prefix: ancestor.prefix
  10014. } : {
  10015. type: ancestor.type
  10016. }
  10017. });
  10018. return;
  10019. }
  10020. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  10021. const nextAncestor = expression.type === "ArrayPattern" || expression.type === "ObjectPattern" || expression.type === "ParenthesizedExpression" ? expression : ancestor;
  10022. for (const child of [].concat(expression[key])) {
  10023. if (child) {
  10024. this.checkLVal(child, {
  10025. in: nextAncestor,
  10026. binding,
  10027. checkClashes,
  10028. allowingSloppyLetBinding,
  10029. strictModeChanged,
  10030. hasParenthesizedAncestor: isParenthesizedExpression
  10031. });
  10032. }
  10033. }
  10034. }
  10035. checkIdentifier(at, bindingType, strictModeChanged = false, allowLetBinding = !(bindingType & BIND_SCOPE_LEXICAL)) {
  10036. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  10037. if (bindingType === BIND_NONE) {
  10038. this.raise(Errors.StrictEvalArguments, {
  10039. at,
  10040. referenceName: at.name
  10041. });
  10042. } else {
  10043. this.raise(Errors.StrictEvalArgumentsBinding, {
  10044. at,
  10045. bindingName: at.name
  10046. });
  10047. }
  10048. }
  10049. if (!allowLetBinding && at.name === "let") {
  10050. this.raise(Errors.LetInLexicalBinding, {
  10051. at
  10052. });
  10053. }
  10054. if (!(bindingType & BIND_NONE)) {
  10055. this.declareNameFromIdentifier(at, bindingType);
  10056. }
  10057. }
  10058. declareNameFromIdentifier(identifier, binding) {
  10059. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  10060. }
  10061. checkToRestConversion(node) {
  10062. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  10063. this.raise(Errors.InvalidRestAssignmentPattern, {
  10064. at: node.argument
  10065. });
  10066. }
  10067. }
  10068. checkCommaAfterRest(close) {
  10069. if (!this.match(12)) {
  10070. return false;
  10071. }
  10072. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  10073. at: this.state.startLoc
  10074. });
  10075. return true;
  10076. }
  10077. }
  10078. class ExpressionParser extends LValParser {
  10079. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10080. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10081. return;
  10082. }
  10083. const key = prop.key;
  10084. const name = key.type === "Identifier" ? key.name : key.value;
  10085. if (name === "__proto__") {
  10086. if (isRecord) {
  10087. this.raise(Errors.RecordNoProto, {
  10088. at: key
  10089. });
  10090. return;
  10091. }
  10092. if (protoRef.used) {
  10093. if (refExpressionErrors) {
  10094. if (refExpressionErrors.doubleProtoLoc === null) {
  10095. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10096. }
  10097. } else {
  10098. this.raise(Errors.DuplicateProto, {
  10099. at: key
  10100. });
  10101. }
  10102. }
  10103. protoRef.used = true;
  10104. }
  10105. }
  10106. shouldExitDescending(expr, potentialArrowAt) {
  10107. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10108. }
  10109. getExpression() {
  10110. this.enterInitialScopes();
  10111. this.nextToken();
  10112. const expr = this.parseExpression();
  10113. if (!this.match(135)) {
  10114. this.unexpected();
  10115. }
  10116. this.finalizeRemainingComments();
  10117. expr.comments = this.state.comments;
  10118. expr.errors = this.state.errors;
  10119. if (this.options.tokens) {
  10120. expr.tokens = this.tokens;
  10121. }
  10122. return expr;
  10123. }
  10124. parseExpression(disallowIn, refExpressionErrors) {
  10125. if (disallowIn) {
  10126. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10127. }
  10128. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10129. }
  10130. parseExpressionBase(refExpressionErrors) {
  10131. const startPos = this.state.start;
  10132. const startLoc = this.state.startLoc;
  10133. const expr = this.parseMaybeAssign(refExpressionErrors);
  10134. if (this.match(12)) {
  10135. const node = this.startNodeAt(startPos, startLoc);
  10136. node.expressions = [expr];
  10137. while (this.eat(12)) {
  10138. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10139. }
  10140. this.toReferencedList(node.expressions);
  10141. return this.finishNode(node, "SequenceExpression");
  10142. }
  10143. return expr;
  10144. }
  10145. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10146. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10147. }
  10148. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10149. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10150. }
  10151. setOptionalParametersError(refExpressionErrors, resultError) {
  10152. var _resultError$loc;
  10153. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10154. }
  10155. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10156. const startPos = this.state.start;
  10157. const startLoc = this.state.startLoc;
  10158. if (this.isContextual(105)) {
  10159. if (this.prodParam.hasYield) {
  10160. let left = this.parseYield();
  10161. if (afterLeftParse) {
  10162. left = afterLeftParse.call(this, left, startPos, startLoc);
  10163. }
  10164. return left;
  10165. }
  10166. }
  10167. let ownExpressionErrors;
  10168. if (refExpressionErrors) {
  10169. ownExpressionErrors = false;
  10170. } else {
  10171. refExpressionErrors = new ExpressionErrors();
  10172. ownExpressionErrors = true;
  10173. }
  10174. const {
  10175. type
  10176. } = this.state;
  10177. if (type === 10 || tokenIsIdentifier(type)) {
  10178. this.state.potentialArrowAt = this.state.start;
  10179. }
  10180. let left = this.parseMaybeConditional(refExpressionErrors);
  10181. if (afterLeftParse) {
  10182. left = afterLeftParse.call(this, left, startPos, startLoc);
  10183. }
  10184. if (tokenIsAssignment(this.state.type)) {
  10185. const node = this.startNodeAt(startPos, startLoc);
  10186. const operator = this.state.value;
  10187. node.operator = operator;
  10188. if (this.match(29)) {
  10189. node.left = this.toAssignable(left, true);
  10190. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startPos) {
  10191. refExpressionErrors.doubleProtoLoc = null;
  10192. }
  10193. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startPos) {
  10194. refExpressionErrors.shorthandAssignLoc = null;
  10195. }
  10196. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startPos) {
  10197. this.checkDestructuringPrivate(refExpressionErrors);
  10198. refExpressionErrors.privateKeyLoc = null;
  10199. }
  10200. } else {
  10201. node.left = left;
  10202. }
  10203. this.next();
  10204. node.right = this.parseMaybeAssign();
  10205. this.checkLVal(left, {
  10206. in: this.finishNode(node, "AssignmentExpression")
  10207. });
  10208. return node;
  10209. } else if (ownExpressionErrors) {
  10210. this.checkExpressionErrors(refExpressionErrors, true);
  10211. }
  10212. return left;
  10213. }
  10214. parseMaybeConditional(refExpressionErrors) {
  10215. const startPos = this.state.start;
  10216. const startLoc = this.state.startLoc;
  10217. const potentialArrowAt = this.state.potentialArrowAt;
  10218. const expr = this.parseExprOps(refExpressionErrors);
  10219. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10220. return expr;
  10221. }
  10222. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  10223. }
  10224. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  10225. if (this.eat(17)) {
  10226. const node = this.startNodeAt(startPos, startLoc);
  10227. node.test = expr;
  10228. node.consequent = this.parseMaybeAssignAllowIn();
  10229. this.expect(14);
  10230. node.alternate = this.parseMaybeAssign();
  10231. return this.finishNode(node, "ConditionalExpression");
  10232. }
  10233. return expr;
  10234. }
  10235. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10236. return this.match(134) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10237. }
  10238. parseExprOps(refExpressionErrors) {
  10239. const startPos = this.state.start;
  10240. const startLoc = this.state.startLoc;
  10241. const potentialArrowAt = this.state.potentialArrowAt;
  10242. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10243. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10244. return expr;
  10245. }
  10246. return this.parseExprOp(expr, startPos, startLoc, -1);
  10247. }
  10248. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  10249. if (this.isPrivateName(left)) {
  10250. const value = this.getPrivateNameSV(left);
  10251. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10252. this.raise(Errors.PrivateInExpectedIn, {
  10253. at: left,
  10254. identifierName: value
  10255. });
  10256. }
  10257. this.classScope.usePrivateName(value, left.loc.start);
  10258. }
  10259. const op = this.state.type;
  10260. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10261. let prec = tokenOperatorPrecedence(op);
  10262. if (prec > minPrec) {
  10263. if (op === 39) {
  10264. this.expectPlugin("pipelineOperator");
  10265. if (this.state.inFSharpPipelineDirectBody) {
  10266. return left;
  10267. }
  10268. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10269. }
  10270. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  10271. node.left = left;
  10272. node.operator = this.state.value;
  10273. const logical = op === 41 || op === 42;
  10274. const coalesce = op === 40;
  10275. if (coalesce) {
  10276. prec = tokenOperatorPrecedence(42);
  10277. }
  10278. this.next();
  10279. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10280. proposal: "minimal"
  10281. }])) {
  10282. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10283. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10284. at: this.state.startLoc
  10285. });
  10286. }
  10287. }
  10288. node.right = this.parseExprOpRightExpr(op, prec);
  10289. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10290. const nextOp = this.state.type;
  10291. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10292. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10293. at: this.state.startLoc
  10294. });
  10295. }
  10296. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  10297. }
  10298. }
  10299. return left;
  10300. }
  10301. parseExprOpRightExpr(op, prec) {
  10302. const startPos = this.state.start;
  10303. const startLoc = this.state.startLoc;
  10304. switch (op) {
  10305. case 39:
  10306. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10307. case "hack":
  10308. return this.withTopicBindingContext(() => {
  10309. return this.parseHackPipeBody();
  10310. });
  10311. case "smart":
  10312. return this.withTopicBindingContext(() => {
  10313. if (this.prodParam.hasYield && this.isContextual(105)) {
  10314. throw this.raise(Errors.PipeBodyIsTighter, {
  10315. at: this.state.startLoc
  10316. });
  10317. }
  10318. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  10319. });
  10320. case "fsharp":
  10321. return this.withSoloAwaitPermittingContext(() => {
  10322. return this.parseFSharpPipelineBody(prec);
  10323. });
  10324. }
  10325. default:
  10326. return this.parseExprOpBaseRightExpr(op, prec);
  10327. }
  10328. }
  10329. parseExprOpBaseRightExpr(op, prec) {
  10330. const startPos = this.state.start;
  10331. const startLoc = this.state.startLoc;
  10332. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10333. }
  10334. parseHackPipeBody() {
  10335. var _body$extra;
  10336. const {
  10337. startLoc
  10338. } = this.state;
  10339. const body = this.parseMaybeAssign();
  10340. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10341. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10342. this.raise(Errors.PipeUnparenthesizedBody, {
  10343. at: startLoc,
  10344. type: body.type
  10345. });
  10346. }
  10347. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10348. this.raise(Errors.PipeTopicUnused, {
  10349. at: startLoc
  10350. });
  10351. }
  10352. return body;
  10353. }
  10354. checkExponentialAfterUnary(node) {
  10355. if (this.match(57)) {
  10356. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10357. at: node.argument
  10358. });
  10359. }
  10360. }
  10361. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10362. const startPos = this.state.start;
  10363. const startLoc = this.state.startLoc;
  10364. const isAwait = this.isContextual(96);
  10365. if (isAwait && this.isAwaitAllowed()) {
  10366. this.next();
  10367. const expr = this.parseAwait(startPos, startLoc);
  10368. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10369. return expr;
  10370. }
  10371. const update = this.match(34);
  10372. const node = this.startNode();
  10373. if (tokenIsPrefix(this.state.type)) {
  10374. node.operator = this.state.value;
  10375. node.prefix = true;
  10376. if (this.match(72)) {
  10377. this.expectPlugin("throwExpressions");
  10378. }
  10379. const isDelete = this.match(89);
  10380. this.next();
  10381. node.argument = this.parseMaybeUnary(null, true);
  10382. this.checkExpressionErrors(refExpressionErrors, true);
  10383. if (this.state.strict && isDelete) {
  10384. const arg = node.argument;
  10385. if (arg.type === "Identifier") {
  10386. this.raise(Errors.StrictDelete, {
  10387. at: node
  10388. });
  10389. } else if (this.hasPropertyAsPrivateName(arg)) {
  10390. this.raise(Errors.DeletePrivateField, {
  10391. at: node
  10392. });
  10393. }
  10394. }
  10395. if (!update) {
  10396. if (!sawUnary) this.checkExponentialAfterUnary(node);
  10397. return this.finishNode(node, "UnaryExpression");
  10398. }
  10399. }
  10400. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10401. if (isAwait) {
  10402. const {
  10403. type
  10404. } = this.state;
  10405. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10406. if (startsExpr && !this.isAmbiguousAwait()) {
  10407. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10408. at: startLoc
  10409. });
  10410. return this.parseAwait(startPos, startLoc);
  10411. }
  10412. }
  10413. return expr;
  10414. }
  10415. parseUpdate(node, update, refExpressionErrors) {
  10416. if (update) {
  10417. this.checkLVal(node.argument, {
  10418. in: this.finishNode(node, "UpdateExpression")
  10419. });
  10420. return node;
  10421. }
  10422. const startPos = this.state.start;
  10423. const startLoc = this.state.startLoc;
  10424. let expr = this.parseExprSubscripts(refExpressionErrors);
  10425. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10426. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10427. const node = this.startNodeAt(startPos, startLoc);
  10428. node.operator = this.state.value;
  10429. node.prefix = false;
  10430. node.argument = expr;
  10431. this.next();
  10432. this.checkLVal(expr, {
  10433. in: expr = this.finishNode(node, "UpdateExpression")
  10434. });
  10435. }
  10436. return expr;
  10437. }
  10438. parseExprSubscripts(refExpressionErrors) {
  10439. const startPos = this.state.start;
  10440. const startLoc = this.state.startLoc;
  10441. const potentialArrowAt = this.state.potentialArrowAt;
  10442. const expr = this.parseExprAtom(refExpressionErrors);
  10443. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10444. return expr;
  10445. }
  10446. return this.parseSubscripts(expr, startPos, startLoc);
  10447. }
  10448. parseSubscripts(base, startPos, startLoc, noCalls) {
  10449. const state = {
  10450. optionalChainMember: false,
  10451. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10452. stop: false
  10453. };
  10454. do {
  10455. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  10456. state.maybeAsyncArrow = false;
  10457. } while (!state.stop);
  10458. return base;
  10459. }
  10460. parseSubscript(base, startPos, startLoc, noCalls, state) {
  10461. const {
  10462. type
  10463. } = this.state;
  10464. if (!noCalls && type === 15) {
  10465. return this.parseBind(base, startPos, startLoc, noCalls, state);
  10466. } else if (tokenIsTemplate(type)) {
  10467. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  10468. }
  10469. let optional = false;
  10470. if (type === 18) {
  10471. if (noCalls && this.lookaheadCharCode() === 40) {
  10472. state.stop = true;
  10473. return base;
  10474. }
  10475. state.optionalChainMember = optional = true;
  10476. this.next();
  10477. }
  10478. if (!noCalls && this.match(10)) {
  10479. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  10480. } else {
  10481. const computed = this.eat(0);
  10482. if (computed || optional || this.eat(16)) {
  10483. return this.parseMember(base, startPos, startLoc, state, computed, optional);
  10484. } else {
  10485. state.stop = true;
  10486. return base;
  10487. }
  10488. }
  10489. }
  10490. parseMember(base, startPos, startLoc, state, computed, optional) {
  10491. const node = this.startNodeAt(startPos, startLoc);
  10492. node.object = base;
  10493. node.computed = computed;
  10494. if (computed) {
  10495. node.property = this.parseExpression();
  10496. this.expect(3);
  10497. } else if (this.match(134)) {
  10498. if (base.type === "Super") {
  10499. this.raise(Errors.SuperPrivateField, {
  10500. at: startLoc
  10501. });
  10502. }
  10503. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10504. node.property = this.parsePrivateName();
  10505. } else {
  10506. node.property = this.parseIdentifier(true);
  10507. }
  10508. if (state.optionalChainMember) {
  10509. node.optional = optional;
  10510. return this.finishNode(node, "OptionalMemberExpression");
  10511. } else {
  10512. return this.finishNode(node, "MemberExpression");
  10513. }
  10514. }
  10515. parseBind(base, startPos, startLoc, noCalls, state) {
  10516. const node = this.startNodeAt(startPos, startLoc);
  10517. node.object = base;
  10518. this.next();
  10519. node.callee = this.parseNoCallExpr();
  10520. state.stop = true;
  10521. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  10522. }
  10523. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  10524. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10525. let refExpressionErrors = null;
  10526. this.state.maybeInArrowParameters = true;
  10527. this.next();
  10528. let node = this.startNodeAt(startPos, startLoc);
  10529. node.callee = base;
  10530. const {
  10531. maybeAsyncArrow,
  10532. optionalChainMember
  10533. } = state;
  10534. if (maybeAsyncArrow) {
  10535. this.expressionScope.enter(newAsyncArrowScope());
  10536. refExpressionErrors = new ExpressionErrors();
  10537. }
  10538. if (optionalChainMember) {
  10539. node.optional = optional;
  10540. }
  10541. if (optional) {
  10542. node.arguments = this.parseCallExpressionArguments(11);
  10543. } else {
  10544. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10545. }
  10546. this.finishCallExpression(node, optionalChainMember);
  10547. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10548. state.stop = true;
  10549. this.checkDestructuringPrivate(refExpressionErrors);
  10550. this.expressionScope.validateAsPattern();
  10551. this.expressionScope.exit();
  10552. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  10553. } else {
  10554. if (maybeAsyncArrow) {
  10555. this.checkExpressionErrors(refExpressionErrors, true);
  10556. this.expressionScope.exit();
  10557. }
  10558. this.toReferencedArguments(node);
  10559. }
  10560. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10561. return node;
  10562. }
  10563. toReferencedArguments(node, isParenthesizedExpr) {
  10564. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10565. }
  10566. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  10567. const node = this.startNodeAt(startPos, startLoc);
  10568. node.tag = base;
  10569. node.quasi = this.parseTemplate(true);
  10570. if (state.optionalChainMember) {
  10571. this.raise(Errors.OptionalChainingNoTemplate, {
  10572. at: startLoc
  10573. });
  10574. }
  10575. return this.finishNode(node, "TaggedTemplateExpression");
  10576. }
  10577. atPossibleAsyncArrow(base) {
  10578. 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;
  10579. }
  10580. finishCallExpression(node, optional) {
  10581. if (node.callee.type === "Import") {
  10582. if (node.arguments.length === 2) {
  10583. {
  10584. if (!this.hasPlugin("moduleAttributes")) {
  10585. this.expectPlugin("importAssertions");
  10586. }
  10587. }
  10588. }
  10589. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10590. this.raise(Errors.ImportCallArity, {
  10591. at: node,
  10592. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10593. });
  10594. } else {
  10595. for (const arg of node.arguments) {
  10596. if (arg.type === "SpreadElement") {
  10597. this.raise(Errors.ImportCallSpreadArgument, {
  10598. at: arg
  10599. });
  10600. }
  10601. }
  10602. }
  10603. }
  10604. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10605. }
  10606. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10607. const elts = [];
  10608. let first = true;
  10609. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10610. this.state.inFSharpPipelineDirectBody = false;
  10611. while (!this.eat(close)) {
  10612. if (first) {
  10613. first = false;
  10614. } else {
  10615. this.expect(12);
  10616. if (this.match(close)) {
  10617. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10618. this.raise(Errors.ImportCallArgumentTrailingComma, {
  10619. at: this.state.lastTokStartLoc
  10620. });
  10621. }
  10622. if (nodeForExtra) {
  10623. this.addTrailingCommaExtraToNode(nodeForExtra);
  10624. }
  10625. this.next();
  10626. break;
  10627. }
  10628. }
  10629. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10630. }
  10631. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10632. return elts;
  10633. }
  10634. shouldParseAsyncArrow() {
  10635. return this.match(19) && !this.canInsertSemicolon();
  10636. }
  10637. parseAsyncArrowFromCallExpression(node, call) {
  10638. var _call$extra;
  10639. this.resetPreviousNodeTrailingComments(call);
  10640. this.expect(19);
  10641. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  10642. if (call.innerComments) {
  10643. setInnerComments(node, call.innerComments);
  10644. }
  10645. if (call.callee.trailingComments) {
  10646. setInnerComments(node, call.callee.trailingComments);
  10647. }
  10648. return node;
  10649. }
  10650. parseNoCallExpr() {
  10651. const startPos = this.state.start;
  10652. const startLoc = this.state.startLoc;
  10653. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  10654. }
  10655. parseExprAtom(refExpressionErrors) {
  10656. let node;
  10657. const {
  10658. type
  10659. } = this.state;
  10660. switch (type) {
  10661. case 79:
  10662. return this.parseSuper();
  10663. case 83:
  10664. node = this.startNode();
  10665. this.next();
  10666. if (this.match(16)) {
  10667. return this.parseImportMetaProperty(node);
  10668. }
  10669. if (!this.match(10)) {
  10670. this.raise(Errors.UnsupportedImport, {
  10671. at: this.state.lastTokStartLoc
  10672. });
  10673. }
  10674. return this.finishNode(node, "Import");
  10675. case 78:
  10676. node = this.startNode();
  10677. this.next();
  10678. return this.finishNode(node, "ThisExpression");
  10679. case 90:
  10680. {
  10681. return this.parseDo(this.startNode(), false);
  10682. }
  10683. case 56:
  10684. case 31:
  10685. {
  10686. this.readRegexp();
  10687. return this.parseRegExpLiteral(this.state.value);
  10688. }
  10689. case 130:
  10690. return this.parseNumericLiteral(this.state.value);
  10691. case 131:
  10692. return this.parseBigIntLiteral(this.state.value);
  10693. case 132:
  10694. return this.parseDecimalLiteral(this.state.value);
  10695. case 129:
  10696. return this.parseStringLiteral(this.state.value);
  10697. case 84:
  10698. return this.parseNullLiteral();
  10699. case 85:
  10700. return this.parseBooleanLiteral(true);
  10701. case 86:
  10702. return this.parseBooleanLiteral(false);
  10703. case 10:
  10704. {
  10705. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10706. return this.parseParenAndDistinguishExpression(canBeArrow);
  10707. }
  10708. case 2:
  10709. case 1:
  10710. {
  10711. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  10712. }
  10713. case 0:
  10714. {
  10715. return this.parseArrayLike(3, true, false, refExpressionErrors);
  10716. }
  10717. case 6:
  10718. case 7:
  10719. {
  10720. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  10721. }
  10722. case 5:
  10723. {
  10724. return this.parseObjectLike(8, false, false, refExpressionErrors);
  10725. }
  10726. case 68:
  10727. return this.parseFunctionOrFunctionSent();
  10728. case 26:
  10729. this.parseDecorators();
  10730. case 80:
  10731. node = this.startNode();
  10732. this.takeDecorators(node);
  10733. return this.parseClass(node, false);
  10734. case 77:
  10735. return this.parseNewOrNewTarget();
  10736. case 25:
  10737. case 24:
  10738. return this.parseTemplate(false);
  10739. case 15:
  10740. {
  10741. node = this.startNode();
  10742. this.next();
  10743. node.object = null;
  10744. const callee = node.callee = this.parseNoCallExpr();
  10745. if (callee.type === "MemberExpression") {
  10746. return this.finishNode(node, "BindExpression");
  10747. } else {
  10748. throw this.raise(Errors.UnsupportedBind, {
  10749. at: callee
  10750. });
  10751. }
  10752. }
  10753. case 134:
  10754. {
  10755. this.raise(Errors.PrivateInExpectedIn, {
  10756. at: this.state.startLoc,
  10757. identifierName: this.state.value
  10758. });
  10759. return this.parsePrivateName();
  10760. }
  10761. case 33:
  10762. {
  10763. return this.parseTopicReferenceThenEqualsSign(54, "%");
  10764. }
  10765. case 32:
  10766. {
  10767. return this.parseTopicReferenceThenEqualsSign(44, "^");
  10768. }
  10769. case 37:
  10770. case 38:
  10771. {
  10772. return this.parseTopicReference("hack");
  10773. }
  10774. case 44:
  10775. case 54:
  10776. case 27:
  10777. {
  10778. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10779. if (pipeProposal) {
  10780. return this.parseTopicReference(pipeProposal);
  10781. } else {
  10782. throw this.unexpected();
  10783. }
  10784. }
  10785. case 47:
  10786. {
  10787. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  10788. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  10789. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  10790. break;
  10791. } else {
  10792. throw this.unexpected();
  10793. }
  10794. }
  10795. default:
  10796. if (tokenIsIdentifier(type)) {
  10797. if (this.isContextual(123) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  10798. return this.parseModuleExpression();
  10799. }
  10800. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10801. const containsEsc = this.state.containsEsc;
  10802. const id = this.parseIdentifier();
  10803. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  10804. const {
  10805. type
  10806. } = this.state;
  10807. if (type === 68) {
  10808. this.resetPreviousNodeTrailingComments(id);
  10809. this.next();
  10810. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  10811. } else if (tokenIsIdentifier(type)) {
  10812. if (this.lookaheadCharCode() === 61) {
  10813. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  10814. } else {
  10815. return id;
  10816. }
  10817. } else if (type === 90) {
  10818. this.resetPreviousNodeTrailingComments(id);
  10819. return this.parseDo(this.startNodeAtNode(id), true);
  10820. }
  10821. }
  10822. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  10823. this.next();
  10824. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  10825. }
  10826. return id;
  10827. } else {
  10828. throw this.unexpected();
  10829. }
  10830. }
  10831. }
  10832. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  10833. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10834. if (pipeProposal) {
  10835. this.state.type = topicTokenType;
  10836. this.state.value = topicTokenValue;
  10837. this.state.pos--;
  10838. this.state.end--;
  10839. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  10840. return this.parseTopicReference(pipeProposal);
  10841. } else {
  10842. throw this.unexpected();
  10843. }
  10844. }
  10845. parseTopicReference(pipeProposal) {
  10846. const node = this.startNode();
  10847. const startLoc = this.state.startLoc;
  10848. const tokenType = this.state.type;
  10849. this.next();
  10850. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  10851. }
  10852. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  10853. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  10854. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  10855. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  10856. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  10857. at: startLoc
  10858. });
  10859. }
  10860. this.registerTopicReference();
  10861. return this.finishNode(node, nodeType);
  10862. } else {
  10863. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  10864. at: startLoc,
  10865. token: tokenLabelName(tokenType)
  10866. });
  10867. }
  10868. }
  10869. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  10870. switch (pipeProposal) {
  10871. case "hack":
  10872. {
  10873. return this.hasPlugin(["pipelineOperator", {
  10874. topicToken: tokenLabelName(tokenType)
  10875. }]);
  10876. }
  10877. case "smart":
  10878. return tokenType === 27;
  10879. default:
  10880. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  10881. at: startLoc
  10882. });
  10883. }
  10884. }
  10885. parseAsyncArrowUnaryFunction(node) {
  10886. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  10887. const params = [this.parseIdentifier()];
  10888. this.prodParam.exit();
  10889. if (this.hasPrecedingLineBreak()) {
  10890. this.raise(Errors.LineTerminatorBeforeArrow, {
  10891. at: this.state.curPosition()
  10892. });
  10893. }
  10894. this.expect(19);
  10895. this.parseArrowExpression(node, params, true);
  10896. return node;
  10897. }
  10898. parseDo(node, isAsync) {
  10899. this.expectPlugin("doExpressions");
  10900. if (isAsync) {
  10901. this.expectPlugin("asyncDoExpressions");
  10902. }
  10903. node.async = isAsync;
  10904. this.next();
  10905. const oldLabels = this.state.labels;
  10906. this.state.labels = [];
  10907. if (isAsync) {
  10908. this.prodParam.enter(PARAM_AWAIT);
  10909. node.body = this.parseBlock();
  10910. this.prodParam.exit();
  10911. } else {
  10912. node.body = this.parseBlock();
  10913. }
  10914. this.state.labels = oldLabels;
  10915. return this.finishNode(node, "DoExpression");
  10916. }
  10917. parseSuper() {
  10918. const node = this.startNode();
  10919. this.next();
  10920. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  10921. this.raise(Errors.SuperNotAllowed, {
  10922. at: node
  10923. });
  10924. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  10925. this.raise(Errors.UnexpectedSuper, {
  10926. at: node
  10927. });
  10928. }
  10929. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  10930. this.raise(Errors.UnsupportedSuper, {
  10931. at: node
  10932. });
  10933. }
  10934. return this.finishNode(node, "Super");
  10935. }
  10936. parsePrivateName() {
  10937. const node = this.startNode();
  10938. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart, this.state.start + 1));
  10939. const name = this.state.value;
  10940. this.next();
  10941. node.id = this.createIdentifier(id, name);
  10942. return this.finishNode(node, "PrivateName");
  10943. }
  10944. parseFunctionOrFunctionSent() {
  10945. const node = this.startNode();
  10946. this.next();
  10947. if (this.prodParam.hasYield && this.match(16)) {
  10948. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  10949. this.next();
  10950. if (this.match(102)) {
  10951. this.expectPlugin("functionSent");
  10952. } else if (!this.hasPlugin("functionSent")) {
  10953. this.unexpected();
  10954. }
  10955. return this.parseMetaProperty(node, meta, "sent");
  10956. }
  10957. return this.parseFunction(node);
  10958. }
  10959. parseMetaProperty(node, meta, propertyName) {
  10960. node.meta = meta;
  10961. const containsEsc = this.state.containsEsc;
  10962. node.property = this.parseIdentifier(true);
  10963. if (node.property.name !== propertyName || containsEsc) {
  10964. this.raise(Errors.UnsupportedMetaProperty, {
  10965. at: node.property,
  10966. target: meta.name,
  10967. onlyValidPropertyName: propertyName
  10968. });
  10969. }
  10970. return this.finishNode(node, "MetaProperty");
  10971. }
  10972. parseImportMetaProperty(node) {
  10973. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  10974. this.next();
  10975. if (this.isContextual(100)) {
  10976. if (!this.inModule) {
  10977. this.raise(Errors.ImportMetaOutsideModule, {
  10978. at: id
  10979. });
  10980. }
  10981. this.sawUnambiguousESM = true;
  10982. }
  10983. return this.parseMetaProperty(node, id, "meta");
  10984. }
  10985. parseLiteralAtNode(value, type, node) {
  10986. this.addExtra(node, "rawValue", value);
  10987. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  10988. node.value = value;
  10989. this.next();
  10990. return this.finishNode(node, type);
  10991. }
  10992. parseLiteral(value, type) {
  10993. const node = this.startNode();
  10994. return this.parseLiteralAtNode(value, type, node);
  10995. }
  10996. parseStringLiteral(value) {
  10997. return this.parseLiteral(value, "StringLiteral");
  10998. }
  10999. parseNumericLiteral(value) {
  11000. return this.parseLiteral(value, "NumericLiteral");
  11001. }
  11002. parseBigIntLiteral(value) {
  11003. return this.parseLiteral(value, "BigIntLiteral");
  11004. }
  11005. parseDecimalLiteral(value) {
  11006. return this.parseLiteral(value, "DecimalLiteral");
  11007. }
  11008. parseRegExpLiteral(value) {
  11009. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11010. node.pattern = value.pattern;
  11011. node.flags = value.flags;
  11012. return node;
  11013. }
  11014. parseBooleanLiteral(value) {
  11015. const node = this.startNode();
  11016. node.value = value;
  11017. this.next();
  11018. return this.finishNode(node, "BooleanLiteral");
  11019. }
  11020. parseNullLiteral() {
  11021. const node = this.startNode();
  11022. this.next();
  11023. return this.finishNode(node, "NullLiteral");
  11024. }
  11025. parseParenAndDistinguishExpression(canBeArrow) {
  11026. const startPos = this.state.start;
  11027. const startLoc = this.state.startLoc;
  11028. let val;
  11029. this.next();
  11030. this.expressionScope.enter(newArrowHeadScope());
  11031. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11032. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11033. this.state.maybeInArrowParameters = true;
  11034. this.state.inFSharpPipelineDirectBody = false;
  11035. const innerStartPos = this.state.start;
  11036. const innerStartLoc = this.state.startLoc;
  11037. const exprList = [];
  11038. const refExpressionErrors = new ExpressionErrors();
  11039. let first = true;
  11040. let spreadStartLoc;
  11041. let optionalCommaStartLoc;
  11042. while (!this.match(11)) {
  11043. if (first) {
  11044. first = false;
  11045. } else {
  11046. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11047. if (this.match(11)) {
  11048. optionalCommaStartLoc = this.state.startLoc;
  11049. break;
  11050. }
  11051. }
  11052. if (this.match(21)) {
  11053. const spreadNodeStartPos = this.state.start;
  11054. const spreadNodeStartLoc = this.state.startLoc;
  11055. spreadStartLoc = this.state.startLoc;
  11056. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  11057. if (!this.checkCommaAfterRest(41)) {
  11058. break;
  11059. }
  11060. } else {
  11061. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11062. }
  11063. }
  11064. const innerEndLoc = this.state.lastTokEndLoc;
  11065. this.expect(11);
  11066. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11067. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11068. let arrowNode = this.startNodeAt(startPos, startLoc);
  11069. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11070. this.checkDestructuringPrivate(refExpressionErrors);
  11071. this.expressionScope.validateAsPattern();
  11072. this.expressionScope.exit();
  11073. this.parseArrowExpression(arrowNode, exprList, false);
  11074. return arrowNode;
  11075. }
  11076. this.expressionScope.exit();
  11077. if (!exprList.length) {
  11078. this.unexpected(this.state.lastTokStartLoc);
  11079. }
  11080. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11081. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11082. this.checkExpressionErrors(refExpressionErrors, true);
  11083. this.toReferencedListDeep(exprList, true);
  11084. if (exprList.length > 1) {
  11085. val = this.startNodeAt(innerStartPos, innerStartLoc);
  11086. val.expressions = exprList;
  11087. this.finishNode(val, "SequenceExpression");
  11088. this.resetEndLocation(val, innerEndLoc);
  11089. } else {
  11090. val = exprList[0];
  11091. }
  11092. return this.wrapParenthesis(startPos, startLoc, val);
  11093. }
  11094. wrapParenthesis(startPos, startLoc, expression) {
  11095. if (!this.options.createParenthesizedExpressions) {
  11096. this.addExtra(expression, "parenthesized", true);
  11097. this.addExtra(expression, "parenStart", startPos);
  11098. this.takeSurroundingComments(expression, startPos, this.state.lastTokEndLoc.index);
  11099. return expression;
  11100. }
  11101. const parenExpression = this.startNodeAt(startPos, startLoc);
  11102. parenExpression.expression = expression;
  11103. this.finishNode(parenExpression, "ParenthesizedExpression");
  11104. return parenExpression;
  11105. }
  11106. shouldParseArrow(params) {
  11107. return !this.canInsertSemicolon();
  11108. }
  11109. parseArrow(node) {
  11110. if (this.eat(19)) {
  11111. return node;
  11112. }
  11113. }
  11114. parseParenItem(node, startPos, startLoc) {
  11115. return node;
  11116. }
  11117. parseNewOrNewTarget() {
  11118. const node = this.startNode();
  11119. this.next();
  11120. if (this.match(16)) {
  11121. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11122. this.next();
  11123. const metaProp = this.parseMetaProperty(node, meta, "target");
  11124. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  11125. this.raise(Errors.UnexpectedNewTarget, {
  11126. at: metaProp
  11127. });
  11128. }
  11129. return metaProp;
  11130. }
  11131. return this.parseNew(node);
  11132. }
  11133. parseNew(node) {
  11134. node.callee = this.parseNoCallExpr();
  11135. if (node.callee.type === "Import") {
  11136. this.raise(Errors.ImportCallNotNewExpression, {
  11137. at: node.callee
  11138. });
  11139. } else if (this.isOptionalChain(node.callee)) {
  11140. this.raise(Errors.OptionalChainingNoNew, {
  11141. at: this.state.lastTokEndLoc
  11142. });
  11143. } else if (this.eat(18)) {
  11144. this.raise(Errors.OptionalChainingNoNew, {
  11145. at: this.state.startLoc
  11146. });
  11147. }
  11148. this.parseNewArguments(node);
  11149. return this.finishNode(node, "NewExpression");
  11150. }
  11151. parseNewArguments(node) {
  11152. if (this.eat(10)) {
  11153. const args = this.parseExprList(11);
  11154. this.toReferencedList(args);
  11155. node.arguments = args;
  11156. } else {
  11157. node.arguments = [];
  11158. }
  11159. }
  11160. parseTemplateElement(isTagged) {
  11161. const {
  11162. start,
  11163. startLoc,
  11164. end,
  11165. value
  11166. } = this.state;
  11167. const elemStart = start + 1;
  11168. const elem = this.startNodeAt(elemStart, createPositionWithColumnOffset(startLoc, 1));
  11169. if (value === null) {
  11170. if (!isTagged) {
  11171. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11172. at: createPositionWithColumnOffset(startLoc, 2)
  11173. });
  11174. }
  11175. }
  11176. const isTail = this.match(24);
  11177. const endOffset = isTail ? -1 : -2;
  11178. const elemEnd = end + endOffset;
  11179. elem.value = {
  11180. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11181. cooked: value === null ? null : value.slice(1, endOffset)
  11182. };
  11183. elem.tail = isTail;
  11184. this.next();
  11185. this.finishNode(elem, "TemplateElement");
  11186. this.resetEndLocation(elem, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11187. return elem;
  11188. }
  11189. parseTemplate(isTagged) {
  11190. const node = this.startNode();
  11191. node.expressions = [];
  11192. let curElt = this.parseTemplateElement(isTagged);
  11193. node.quasis = [curElt];
  11194. while (!curElt.tail) {
  11195. node.expressions.push(this.parseTemplateSubstitution());
  11196. this.readTemplateContinuation();
  11197. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11198. }
  11199. return this.finishNode(node, "TemplateLiteral");
  11200. }
  11201. parseTemplateSubstitution() {
  11202. return this.parseExpression();
  11203. }
  11204. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11205. if (isRecord) {
  11206. this.expectPlugin("recordAndTuple");
  11207. }
  11208. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11209. this.state.inFSharpPipelineDirectBody = false;
  11210. const propHash = Object.create(null);
  11211. let first = true;
  11212. const node = this.startNode();
  11213. node.properties = [];
  11214. this.next();
  11215. while (!this.match(close)) {
  11216. if (first) {
  11217. first = false;
  11218. } else {
  11219. this.expect(12);
  11220. if (this.match(close)) {
  11221. this.addTrailingCommaExtraToNode(node);
  11222. break;
  11223. }
  11224. }
  11225. let prop;
  11226. if (isPattern) {
  11227. prop = this.parseBindingProperty();
  11228. } else {
  11229. prop = this.parsePropertyDefinition(refExpressionErrors);
  11230. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11231. }
  11232. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11233. this.raise(Errors.InvalidRecordProperty, {
  11234. at: prop
  11235. });
  11236. }
  11237. if (prop.shorthand) {
  11238. this.addExtra(prop, "shorthand", true);
  11239. }
  11240. node.properties.push(prop);
  11241. }
  11242. this.next();
  11243. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11244. let type = "ObjectExpression";
  11245. if (isPattern) {
  11246. type = "ObjectPattern";
  11247. } else if (isRecord) {
  11248. type = "RecordExpression";
  11249. }
  11250. return this.finishNode(node, type);
  11251. }
  11252. addTrailingCommaExtraToNode(node) {
  11253. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11254. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11255. }
  11256. maybeAsyncOrAccessorProp(prop) {
  11257. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11258. }
  11259. parsePropertyDefinition(refExpressionErrors) {
  11260. let decorators = [];
  11261. if (this.match(26)) {
  11262. if (this.hasPlugin("decorators")) {
  11263. this.raise(Errors.UnsupportedPropertyDecorator, {
  11264. at: this.state.startLoc
  11265. });
  11266. }
  11267. while (this.match(26)) {
  11268. decorators.push(this.parseDecorator());
  11269. }
  11270. }
  11271. const prop = this.startNode();
  11272. let isAsync = false;
  11273. let isAccessor = false;
  11274. let startPos;
  11275. let startLoc;
  11276. if (this.match(21)) {
  11277. if (decorators.length) this.unexpected();
  11278. return this.parseSpread();
  11279. }
  11280. if (decorators.length) {
  11281. prop.decorators = decorators;
  11282. decorators = [];
  11283. }
  11284. prop.method = false;
  11285. if (refExpressionErrors) {
  11286. startPos = this.state.start;
  11287. startLoc = this.state.startLoc;
  11288. }
  11289. let isGenerator = this.eat(55);
  11290. this.parsePropertyNamePrefixOperator(prop);
  11291. const containsEsc = this.state.containsEsc;
  11292. const key = this.parsePropertyName(prop, refExpressionErrors);
  11293. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11294. const keyName = key.name;
  11295. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11296. isAsync = true;
  11297. this.resetPreviousNodeTrailingComments(key);
  11298. isGenerator = this.eat(55);
  11299. this.parsePropertyName(prop);
  11300. }
  11301. if (keyName === "get" || keyName === "set") {
  11302. isAccessor = true;
  11303. this.resetPreviousNodeTrailingComments(key);
  11304. prop.kind = keyName;
  11305. if (this.match(55)) {
  11306. isGenerator = true;
  11307. this.raise(Errors.AccessorIsGenerator, {
  11308. at: this.state.curPosition(),
  11309. kind: keyName
  11310. });
  11311. this.next();
  11312. }
  11313. this.parsePropertyName(prop);
  11314. }
  11315. }
  11316. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11317. return prop;
  11318. }
  11319. getGetterSetterExpectedParamCount(method) {
  11320. return method.kind === "get" ? 0 : 1;
  11321. }
  11322. getObjectOrClassMethodParams(method) {
  11323. return method.params;
  11324. }
  11325. checkGetterSetterParams(method) {
  11326. var _params;
  11327. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11328. const params = this.getObjectOrClassMethodParams(method);
  11329. if (params.length !== paramCount) {
  11330. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11331. at: method
  11332. });
  11333. }
  11334. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11335. this.raise(Errors.BadSetterRestParameter, {
  11336. at: method
  11337. });
  11338. }
  11339. }
  11340. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11341. if (isAccessor) {
  11342. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11343. this.checkGetterSetterParams(prop);
  11344. return prop;
  11345. }
  11346. if (isAsync || isGenerator || this.match(10)) {
  11347. if (isPattern) this.unexpected();
  11348. prop.kind = "method";
  11349. prop.method = true;
  11350. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11351. }
  11352. }
  11353. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  11354. prop.shorthand = false;
  11355. if (this.eat(14)) {
  11356. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11357. return this.finishNode(prop, "ObjectProperty");
  11358. }
  11359. if (!prop.computed && prop.key.type === "Identifier") {
  11360. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11361. if (isPattern) {
  11362. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11363. } else if (this.match(29)) {
  11364. const shorthandAssignLoc = this.state.startLoc;
  11365. if (refExpressionErrors != null) {
  11366. if (refExpressionErrors.shorthandAssignLoc === null) {
  11367. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11368. }
  11369. } else {
  11370. this.raise(Errors.InvalidCoverInitializedName, {
  11371. at: shorthandAssignLoc
  11372. });
  11373. }
  11374. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11375. } else {
  11376. prop.value = cloneIdentifier(prop.key);
  11377. }
  11378. prop.shorthand = true;
  11379. return this.finishNode(prop, "ObjectProperty");
  11380. }
  11381. }
  11382. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11383. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  11384. if (!node) this.unexpected();
  11385. return node;
  11386. }
  11387. parsePropertyName(prop, refExpressionErrors) {
  11388. if (this.eat(0)) {
  11389. prop.computed = true;
  11390. prop.key = this.parseMaybeAssignAllowIn();
  11391. this.expect(3);
  11392. } else {
  11393. const {
  11394. type,
  11395. value
  11396. } = this.state;
  11397. let key;
  11398. if (tokenIsKeywordOrIdentifier(type)) {
  11399. key = this.parseIdentifier(true);
  11400. } else {
  11401. switch (type) {
  11402. case 130:
  11403. key = this.parseNumericLiteral(value);
  11404. break;
  11405. case 129:
  11406. key = this.parseStringLiteral(value);
  11407. break;
  11408. case 131:
  11409. key = this.parseBigIntLiteral(value);
  11410. break;
  11411. case 132:
  11412. key = this.parseDecimalLiteral(value);
  11413. break;
  11414. case 134:
  11415. {
  11416. const privateKeyLoc = this.state.startLoc;
  11417. if (refExpressionErrors != null) {
  11418. if (refExpressionErrors.privateKeyLoc === null) {
  11419. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11420. }
  11421. } else {
  11422. this.raise(Errors.UnexpectedPrivateField, {
  11423. at: privateKeyLoc
  11424. });
  11425. }
  11426. key = this.parsePrivateName();
  11427. break;
  11428. }
  11429. default:
  11430. throw this.unexpected();
  11431. }
  11432. }
  11433. prop.key = key;
  11434. if (type !== 134) {
  11435. prop.computed = false;
  11436. }
  11437. }
  11438. return prop.key;
  11439. }
  11440. initFunction(node, isAsync) {
  11441. node.id = null;
  11442. node.generator = false;
  11443. node.async = !!isAsync;
  11444. }
  11445. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11446. this.initFunction(node, isAsync);
  11447. node.generator = !!isGenerator;
  11448. const allowModifiers = isConstructor;
  11449. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11450. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11451. this.parseFunctionParams(node, allowModifiers);
  11452. this.parseFunctionBodyAndFinish(node, type, true);
  11453. this.prodParam.exit();
  11454. this.scope.exit();
  11455. return node;
  11456. }
  11457. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11458. if (isTuple) {
  11459. this.expectPlugin("recordAndTuple");
  11460. }
  11461. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11462. this.state.inFSharpPipelineDirectBody = false;
  11463. const node = this.startNode();
  11464. this.next();
  11465. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11466. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11467. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11468. }
  11469. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11470. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11471. let flags = functionFlags(isAsync, false);
  11472. if (!this.match(5) && this.prodParam.hasIn) {
  11473. flags |= PARAM_IN;
  11474. }
  11475. this.prodParam.enter(flags);
  11476. this.initFunction(node, isAsync);
  11477. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11478. if (params) {
  11479. this.state.maybeInArrowParameters = true;
  11480. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11481. }
  11482. this.state.maybeInArrowParameters = false;
  11483. this.parseFunctionBody(node, true);
  11484. this.prodParam.exit();
  11485. this.scope.exit();
  11486. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11487. return this.finishNode(node, "ArrowFunctionExpression");
  11488. }
  11489. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11490. node.params = this.toAssignableList(params, trailingCommaLoc, false);
  11491. }
  11492. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11493. this.parseFunctionBody(node, false, isMethod);
  11494. this.finishNode(node, type);
  11495. }
  11496. parseFunctionBody(node, allowExpression, isMethod = false) {
  11497. const isExpression = allowExpression && !this.match(5);
  11498. this.expressionScope.enter(newExpressionScope());
  11499. if (isExpression) {
  11500. node.body = this.parseMaybeAssign();
  11501. this.checkParams(node, false, allowExpression, false);
  11502. } else {
  11503. const oldStrict = this.state.strict;
  11504. const oldLabels = this.state.labels;
  11505. this.state.labels = [];
  11506. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11507. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11508. const nonSimple = !this.isSimpleParamList(node.params);
  11509. if (hasStrictModeDirective && nonSimple) {
  11510. this.raise(Errors.IllegalLanguageModeDirective, {
  11511. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11512. });
  11513. }
  11514. const strictModeChanged = !oldStrict && this.state.strict;
  11515. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11516. if (this.state.strict && node.id) {
  11517. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11518. }
  11519. });
  11520. this.prodParam.exit();
  11521. this.state.labels = oldLabels;
  11522. }
  11523. this.expressionScope.exit();
  11524. }
  11525. isSimpleParameter(node) {
  11526. return node.type === "Identifier";
  11527. }
  11528. isSimpleParamList(params) {
  11529. for (let i = 0, len = params.length; i < len; i++) {
  11530. if (!this.isSimpleParameter(params[i])) return false;
  11531. }
  11532. return true;
  11533. }
  11534. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11535. const checkClashes = !allowDuplicates && new Set();
  11536. const formalParameters = {
  11537. type: "FormalParameters"
  11538. };
  11539. for (const param of node.params) {
  11540. this.checkLVal(param, {
  11541. in: formalParameters,
  11542. binding: BIND_VAR,
  11543. checkClashes,
  11544. strictModeChanged
  11545. });
  11546. }
  11547. }
  11548. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11549. const elts = [];
  11550. let first = true;
  11551. while (!this.eat(close)) {
  11552. if (first) {
  11553. first = false;
  11554. } else {
  11555. this.expect(12);
  11556. if (this.match(close)) {
  11557. if (nodeForExtra) {
  11558. this.addTrailingCommaExtraToNode(nodeForExtra);
  11559. }
  11560. this.next();
  11561. break;
  11562. }
  11563. }
  11564. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11565. }
  11566. return elts;
  11567. }
  11568. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11569. let elt;
  11570. if (this.match(12)) {
  11571. if (!allowEmpty) {
  11572. this.raise(Errors.UnexpectedToken, {
  11573. at: this.state.curPosition(),
  11574. unexpected: ","
  11575. });
  11576. }
  11577. elt = null;
  11578. } else if (this.match(21)) {
  11579. const spreadNodeStartPos = this.state.start;
  11580. const spreadNodeStartLoc = this.state.startLoc;
  11581. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  11582. } else if (this.match(17)) {
  11583. this.expectPlugin("partialApplication");
  11584. if (!allowPlaceholder) {
  11585. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11586. at: this.state.startLoc
  11587. });
  11588. }
  11589. const node = this.startNode();
  11590. this.next();
  11591. elt = this.finishNode(node, "ArgumentPlaceholder");
  11592. } else {
  11593. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11594. }
  11595. return elt;
  11596. }
  11597. parseIdentifier(liberal) {
  11598. const node = this.startNode();
  11599. const name = this.parseIdentifierName(node.start, liberal);
  11600. return this.createIdentifier(node, name);
  11601. }
  11602. createIdentifier(node, name) {
  11603. node.name = name;
  11604. node.loc.identifierName = name;
  11605. return this.finishNode(node, "Identifier");
  11606. }
  11607. parseIdentifierName(pos, liberal) {
  11608. let name;
  11609. const {
  11610. startLoc,
  11611. type
  11612. } = this.state;
  11613. if (tokenIsKeywordOrIdentifier(type)) {
  11614. name = this.state.value;
  11615. } else {
  11616. throw this.unexpected();
  11617. }
  11618. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11619. if (liberal) {
  11620. if (tokenIsKeyword) {
  11621. this.replaceToken(128);
  11622. }
  11623. } else {
  11624. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11625. }
  11626. this.next();
  11627. return name;
  11628. }
  11629. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11630. if (word.length > 10) {
  11631. return;
  11632. }
  11633. if (!canBeReservedWord(word)) {
  11634. return;
  11635. }
  11636. if (word === "yield") {
  11637. if (this.prodParam.hasYield) {
  11638. this.raise(Errors.YieldBindingIdentifier, {
  11639. at: startLoc
  11640. });
  11641. return;
  11642. }
  11643. } else if (word === "await") {
  11644. if (this.prodParam.hasAwait) {
  11645. this.raise(Errors.AwaitBindingIdentifier, {
  11646. at: startLoc
  11647. });
  11648. return;
  11649. }
  11650. if (this.scope.inStaticBlock) {
  11651. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  11652. at: startLoc
  11653. });
  11654. return;
  11655. }
  11656. this.expressionScope.recordAsyncArrowParametersError({
  11657. at: startLoc
  11658. });
  11659. } else if (word === "arguments") {
  11660. if (this.scope.inClassAndNotInNonArrowFunction) {
  11661. this.raise(Errors.ArgumentsInClass, {
  11662. at: startLoc
  11663. });
  11664. return;
  11665. }
  11666. }
  11667. if (checkKeywords && isKeyword(word)) {
  11668. this.raise(Errors.UnexpectedKeyword, {
  11669. at: startLoc,
  11670. keyword: word
  11671. });
  11672. return;
  11673. }
  11674. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  11675. if (reservedTest(word, this.inModule)) {
  11676. this.raise(Errors.UnexpectedReservedWord, {
  11677. at: startLoc,
  11678. reservedWord: word
  11679. });
  11680. }
  11681. }
  11682. isAwaitAllowed() {
  11683. if (this.prodParam.hasAwait) return true;
  11684. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  11685. return true;
  11686. }
  11687. return false;
  11688. }
  11689. parseAwait(startPos, startLoc) {
  11690. const node = this.startNodeAt(startPos, startLoc);
  11691. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  11692. at: node
  11693. });
  11694. if (this.eat(55)) {
  11695. this.raise(Errors.ObsoleteAwaitStar, {
  11696. at: node
  11697. });
  11698. }
  11699. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  11700. if (this.isAmbiguousAwait()) {
  11701. this.ambiguousScriptDifferentAst = true;
  11702. } else {
  11703. this.sawUnambiguousESM = true;
  11704. }
  11705. }
  11706. if (!this.state.soloAwait) {
  11707. node.argument = this.parseMaybeUnary(null, true);
  11708. }
  11709. return this.finishNode(node, "AwaitExpression");
  11710. }
  11711. isAmbiguousAwait() {
  11712. if (this.hasPrecedingLineBreak()) return true;
  11713. const {
  11714. type
  11715. } = this.state;
  11716. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 133 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  11717. }
  11718. parseYield() {
  11719. const node = this.startNode();
  11720. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  11721. at: node
  11722. });
  11723. this.next();
  11724. let delegating = false;
  11725. let argument = null;
  11726. if (!this.hasPrecedingLineBreak()) {
  11727. delegating = this.eat(55);
  11728. switch (this.state.type) {
  11729. case 13:
  11730. case 135:
  11731. case 8:
  11732. case 11:
  11733. case 3:
  11734. case 9:
  11735. case 14:
  11736. case 12:
  11737. if (!delegating) break;
  11738. default:
  11739. argument = this.parseMaybeAssign();
  11740. }
  11741. }
  11742. node.delegate = delegating;
  11743. node.argument = argument;
  11744. return this.finishNode(node, "YieldExpression");
  11745. }
  11746. checkPipelineAtInfixOperator(left, leftStartLoc) {
  11747. if (this.hasPlugin(["pipelineOperator", {
  11748. proposal: "smart"
  11749. }])) {
  11750. if (left.type === "SequenceExpression") {
  11751. this.raise(Errors.PipelineHeadSequenceExpression, {
  11752. at: leftStartLoc
  11753. });
  11754. }
  11755. }
  11756. }
  11757. parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
  11758. const bodyNode = this.startNodeAt(startPos, startLoc);
  11759. if (this.isSimpleReference(childExpr)) {
  11760. bodyNode.callee = childExpr;
  11761. return this.finishNode(bodyNode, "PipelineBareFunction");
  11762. } else {
  11763. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  11764. bodyNode.expression = childExpr;
  11765. return this.finishNode(bodyNode, "PipelineTopicExpression");
  11766. }
  11767. }
  11768. isSimpleReference(expression) {
  11769. switch (expression.type) {
  11770. case "MemberExpression":
  11771. return !expression.computed && this.isSimpleReference(expression.object);
  11772. case "Identifier":
  11773. return true;
  11774. default:
  11775. return false;
  11776. }
  11777. }
  11778. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  11779. if (this.match(19)) {
  11780. throw this.raise(Errors.PipelineBodyNoArrow, {
  11781. at: this.state.startLoc
  11782. });
  11783. }
  11784. if (!this.topicReferenceWasUsedInCurrentContext()) {
  11785. this.raise(Errors.PipelineTopicUnused, {
  11786. at: startLoc
  11787. });
  11788. }
  11789. }
  11790. withTopicBindingContext(callback) {
  11791. const outerContextTopicState = this.state.topicContext;
  11792. this.state.topicContext = {
  11793. maxNumOfResolvableTopics: 1,
  11794. maxTopicIndex: null
  11795. };
  11796. try {
  11797. return callback();
  11798. } finally {
  11799. this.state.topicContext = outerContextTopicState;
  11800. }
  11801. }
  11802. withSmartMixTopicForbiddingContext(callback) {
  11803. if (this.hasPlugin(["pipelineOperator", {
  11804. proposal: "smart"
  11805. }])) {
  11806. const outerContextTopicState = this.state.topicContext;
  11807. this.state.topicContext = {
  11808. maxNumOfResolvableTopics: 0,
  11809. maxTopicIndex: null
  11810. };
  11811. try {
  11812. return callback();
  11813. } finally {
  11814. this.state.topicContext = outerContextTopicState;
  11815. }
  11816. } else {
  11817. return callback();
  11818. }
  11819. }
  11820. withSoloAwaitPermittingContext(callback) {
  11821. const outerContextSoloAwaitState = this.state.soloAwait;
  11822. this.state.soloAwait = true;
  11823. try {
  11824. return callback();
  11825. } finally {
  11826. this.state.soloAwait = outerContextSoloAwaitState;
  11827. }
  11828. }
  11829. allowInAnd(callback) {
  11830. const flags = this.prodParam.currentFlags();
  11831. const prodParamToSet = PARAM_IN & ~flags;
  11832. if (prodParamToSet) {
  11833. this.prodParam.enter(flags | PARAM_IN);
  11834. try {
  11835. return callback();
  11836. } finally {
  11837. this.prodParam.exit();
  11838. }
  11839. }
  11840. return callback();
  11841. }
  11842. disallowInAnd(callback) {
  11843. const flags = this.prodParam.currentFlags();
  11844. const prodParamToClear = PARAM_IN & flags;
  11845. if (prodParamToClear) {
  11846. this.prodParam.enter(flags & ~PARAM_IN);
  11847. try {
  11848. return callback();
  11849. } finally {
  11850. this.prodParam.exit();
  11851. }
  11852. }
  11853. return callback();
  11854. }
  11855. registerTopicReference() {
  11856. this.state.topicContext.maxTopicIndex = 0;
  11857. }
  11858. topicReferenceIsAllowedInCurrentContext() {
  11859. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  11860. }
  11861. topicReferenceWasUsedInCurrentContext() {
  11862. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  11863. }
  11864. parseFSharpPipelineBody(prec) {
  11865. const startPos = this.state.start;
  11866. const startLoc = this.state.startLoc;
  11867. this.state.potentialArrowAt = this.state.start;
  11868. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11869. this.state.inFSharpPipelineDirectBody = true;
  11870. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
  11871. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11872. return ret;
  11873. }
  11874. parseModuleExpression() {
  11875. this.expectPlugin("moduleBlocks");
  11876. const node = this.startNode();
  11877. this.next();
  11878. this.eat(5);
  11879. const revertScopes = this.initializeScopes(true);
  11880. this.enterInitialScopes();
  11881. const program = this.startNode();
  11882. try {
  11883. node.body = this.parseProgram(program, 8, "module");
  11884. } finally {
  11885. revertScopes();
  11886. }
  11887. this.eat(8);
  11888. return this.finishNode(node, "ModuleExpression");
  11889. }
  11890. parsePropertyNamePrefixOperator(prop) {}
  11891. }
  11892. const loopLabel = {
  11893. kind: "loop"
  11894. },
  11895. switchLabel = {
  11896. kind: "switch"
  11897. };
  11898. const FUNC_NO_FLAGS = 0b000,
  11899. FUNC_STATEMENT = 0b001,
  11900. FUNC_HANGING_STATEMENT = 0b010,
  11901. FUNC_NULLABLE_ID = 0b100;
  11902. const loneSurrogate = /[\uD800-\uDFFF]/u;
  11903. const keywordRelationalOperator = /in(?:stanceof)?/y;
  11904. function babel7CompatTokens(tokens, input) {
  11905. for (let i = 0; i < tokens.length; i++) {
  11906. const token = tokens[i];
  11907. const {
  11908. type
  11909. } = token;
  11910. if (typeof type === "number") {
  11911. {
  11912. if (type === 134) {
  11913. const {
  11914. loc,
  11915. start,
  11916. value,
  11917. end
  11918. } = token;
  11919. const hashEndPos = start + 1;
  11920. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  11921. tokens.splice(i, 1, new Token({
  11922. type: getExportedToken(27),
  11923. value: "#",
  11924. start: start,
  11925. end: hashEndPos,
  11926. startLoc: loc.start,
  11927. endLoc: hashEndLoc
  11928. }), new Token({
  11929. type: getExportedToken(128),
  11930. value: value,
  11931. start: hashEndPos,
  11932. end: end,
  11933. startLoc: hashEndLoc,
  11934. endLoc: loc.end
  11935. }));
  11936. i++;
  11937. continue;
  11938. }
  11939. if (tokenIsTemplate(type)) {
  11940. const {
  11941. loc,
  11942. start,
  11943. value,
  11944. end
  11945. } = token;
  11946. const backquoteEnd = start + 1;
  11947. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  11948. let startToken;
  11949. if (input.charCodeAt(start) === 96) {
  11950. startToken = new Token({
  11951. type: getExportedToken(22),
  11952. value: "`",
  11953. start: start,
  11954. end: backquoteEnd,
  11955. startLoc: loc.start,
  11956. endLoc: backquoteEndLoc
  11957. });
  11958. } else {
  11959. startToken = new Token({
  11960. type: getExportedToken(8),
  11961. value: "}",
  11962. start: start,
  11963. end: backquoteEnd,
  11964. startLoc: loc.start,
  11965. endLoc: backquoteEndLoc
  11966. });
  11967. }
  11968. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  11969. if (type === 24) {
  11970. templateElementEnd = end - 1;
  11971. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  11972. templateValue = value === null ? null : value.slice(1, -1);
  11973. endToken = new Token({
  11974. type: getExportedToken(22),
  11975. value: "`",
  11976. start: templateElementEnd,
  11977. end: end,
  11978. startLoc: templateElementEndLoc,
  11979. endLoc: loc.end
  11980. });
  11981. } else {
  11982. templateElementEnd = end - 2;
  11983. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  11984. templateValue = value === null ? null : value.slice(1, -2);
  11985. endToken = new Token({
  11986. type: getExportedToken(23),
  11987. value: "${",
  11988. start: templateElementEnd,
  11989. end: end,
  11990. startLoc: templateElementEndLoc,
  11991. endLoc: loc.end
  11992. });
  11993. }
  11994. tokens.splice(i, 1, startToken, new Token({
  11995. type: getExportedToken(20),
  11996. value: templateValue,
  11997. start: backquoteEnd,
  11998. end: templateElementEnd,
  11999. startLoc: backquoteEndLoc,
  12000. endLoc: templateElementEndLoc
  12001. }), endToken);
  12002. i += 2;
  12003. continue;
  12004. }
  12005. }
  12006. token.type = getExportedToken(type);
  12007. }
  12008. }
  12009. return tokens;
  12010. }
  12011. class StatementParser extends ExpressionParser {
  12012. parseTopLevel(file, program) {
  12013. file.program = this.parseProgram(program);
  12014. file.comments = this.state.comments;
  12015. if (this.options.tokens) {
  12016. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12017. }
  12018. return this.finishNode(file, "File");
  12019. }
  12020. parseProgram(program, end = 135, sourceType = this.options.sourceType) {
  12021. program.sourceType = sourceType;
  12022. program.interpreter = this.parseInterpreterDirective();
  12023. this.parseBlockBody(program, true, true, end);
  12024. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12025. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12026. this.raise(Errors.ModuleExportUndefined, {
  12027. at,
  12028. localName
  12029. });
  12030. }
  12031. }
  12032. return this.finishNode(program, "Program");
  12033. }
  12034. stmtToDirective(stmt) {
  12035. const directive = stmt;
  12036. directive.type = "Directive";
  12037. directive.value = directive.expression;
  12038. delete directive.expression;
  12039. const directiveLiteral = directive.value;
  12040. const expressionValue = directiveLiteral.value;
  12041. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12042. const val = directiveLiteral.value = raw.slice(1, -1);
  12043. this.addExtra(directiveLiteral, "raw", raw);
  12044. this.addExtra(directiveLiteral, "rawValue", val);
  12045. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12046. directiveLiteral.type = "DirectiveLiteral";
  12047. return directive;
  12048. }
  12049. parseInterpreterDirective() {
  12050. if (!this.match(28)) {
  12051. return null;
  12052. }
  12053. const node = this.startNode();
  12054. node.value = this.state.value;
  12055. this.next();
  12056. return this.finishNode(node, "InterpreterDirective");
  12057. }
  12058. isLet(context) {
  12059. if (!this.isContextual(99)) {
  12060. return false;
  12061. }
  12062. return this.isLetKeyword(context);
  12063. }
  12064. isLetKeyword(context) {
  12065. const next = this.nextTokenStart();
  12066. const nextCh = this.codePointAtPos(next);
  12067. if (nextCh === 92 || nextCh === 91) {
  12068. return true;
  12069. }
  12070. if (context) return false;
  12071. if (nextCh === 123) return true;
  12072. if (isIdentifierStart(nextCh)) {
  12073. keywordRelationalOperator.lastIndex = next;
  12074. if (keywordRelationalOperator.test(this.input)) {
  12075. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12076. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12077. return false;
  12078. }
  12079. }
  12080. return true;
  12081. }
  12082. return false;
  12083. }
  12084. parseStatement(context, topLevel) {
  12085. if (this.match(26)) {
  12086. this.parseDecorators(true);
  12087. }
  12088. return this.parseStatementContent(context, topLevel);
  12089. }
  12090. parseStatementContent(context, topLevel) {
  12091. let starttype = this.state.type;
  12092. const node = this.startNode();
  12093. let kind;
  12094. if (this.isLet(context)) {
  12095. starttype = 74;
  12096. kind = "let";
  12097. }
  12098. switch (starttype) {
  12099. case 60:
  12100. return this.parseBreakContinueStatement(node, true);
  12101. case 63:
  12102. return this.parseBreakContinueStatement(node, false);
  12103. case 64:
  12104. return this.parseDebuggerStatement(node);
  12105. case 90:
  12106. return this.parseDoStatement(node);
  12107. case 91:
  12108. return this.parseForStatement(node);
  12109. case 68:
  12110. if (this.lookaheadCharCode() === 46) break;
  12111. if (context) {
  12112. if (this.state.strict) {
  12113. this.raise(Errors.StrictFunction, {
  12114. at: this.state.startLoc
  12115. });
  12116. } else if (context !== "if" && context !== "label") {
  12117. this.raise(Errors.SloppyFunction, {
  12118. at: this.state.startLoc
  12119. });
  12120. }
  12121. }
  12122. return this.parseFunctionStatement(node, false, !context);
  12123. case 80:
  12124. if (context) this.unexpected();
  12125. return this.parseClass(node, true);
  12126. case 69:
  12127. return this.parseIfStatement(node);
  12128. case 70:
  12129. return this.parseReturnStatement(node);
  12130. case 71:
  12131. return this.parseSwitchStatement(node);
  12132. case 72:
  12133. return this.parseThrowStatement(node);
  12134. case 73:
  12135. return this.parseTryStatement(node);
  12136. case 75:
  12137. case 74:
  12138. kind = kind || this.state.value;
  12139. if (context && kind !== "var") {
  12140. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12141. at: this.state.startLoc
  12142. });
  12143. }
  12144. return this.parseVarStatement(node, kind);
  12145. case 92:
  12146. return this.parseWhileStatement(node);
  12147. case 76:
  12148. return this.parseWithStatement(node);
  12149. case 5:
  12150. return this.parseBlock();
  12151. case 13:
  12152. return this.parseEmptyStatement(node);
  12153. case 83:
  12154. {
  12155. const nextTokenCharCode = this.lookaheadCharCode();
  12156. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12157. break;
  12158. }
  12159. }
  12160. case 82:
  12161. {
  12162. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12163. this.raise(Errors.UnexpectedImportExport, {
  12164. at: this.state.startLoc
  12165. });
  12166. }
  12167. this.next();
  12168. let result;
  12169. if (starttype === 83) {
  12170. result = this.parseImport(node);
  12171. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12172. this.sawUnambiguousESM = true;
  12173. }
  12174. } else {
  12175. result = this.parseExport(node);
  12176. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12177. this.sawUnambiguousESM = true;
  12178. }
  12179. }
  12180. this.assertModuleNodeAllowed(node);
  12181. return result;
  12182. }
  12183. default:
  12184. {
  12185. if (this.isAsyncFunction()) {
  12186. if (context) {
  12187. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12188. at: this.state.startLoc
  12189. });
  12190. }
  12191. this.next();
  12192. return this.parseFunctionStatement(node, true, !context);
  12193. }
  12194. }
  12195. }
  12196. const maybeName = this.state.value;
  12197. const expr = this.parseExpression();
  12198. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12199. return this.parseLabeledStatement(node, maybeName, expr, context);
  12200. } else {
  12201. return this.parseExpressionStatement(node, expr);
  12202. }
  12203. }
  12204. assertModuleNodeAllowed(node) {
  12205. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12206. this.raise(Errors.ImportOutsideModule, {
  12207. at: node
  12208. });
  12209. }
  12210. }
  12211. takeDecorators(node) {
  12212. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12213. if (decorators.length) {
  12214. node.decorators = decorators;
  12215. this.resetStartLocationFromNode(node, decorators[0]);
  12216. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  12217. }
  12218. }
  12219. canHaveLeadingDecorator() {
  12220. return this.match(80);
  12221. }
  12222. parseDecorators(allowExport) {
  12223. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12224. while (this.match(26)) {
  12225. const decorator = this.parseDecorator();
  12226. currentContextDecorators.push(decorator);
  12227. }
  12228. if (this.match(82)) {
  12229. if (!allowExport) {
  12230. this.unexpected();
  12231. }
  12232. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12233. this.raise(Errors.DecoratorExportClass, {
  12234. at: this.state.startLoc
  12235. });
  12236. }
  12237. } else if (!this.canHaveLeadingDecorator()) {
  12238. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12239. at: this.state.startLoc
  12240. });
  12241. }
  12242. }
  12243. parseDecorator() {
  12244. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  12245. const node = this.startNode();
  12246. this.next();
  12247. if (this.hasPlugin("decorators")) {
  12248. this.state.decoratorStack.push([]);
  12249. const startPos = this.state.start;
  12250. const startLoc = this.state.startLoc;
  12251. let expr;
  12252. if (this.match(10)) {
  12253. const startPos = this.state.start;
  12254. const startLoc = this.state.startLoc;
  12255. this.next();
  12256. expr = this.parseExpression();
  12257. this.expect(11);
  12258. expr = this.wrapParenthesis(startPos, startLoc, expr);
  12259. } else {
  12260. expr = this.parseIdentifier(false);
  12261. while (this.eat(16)) {
  12262. const node = this.startNodeAt(startPos, startLoc);
  12263. node.object = expr;
  12264. node.property = this.parseIdentifier(true);
  12265. node.computed = false;
  12266. expr = this.finishNode(node, "MemberExpression");
  12267. }
  12268. }
  12269. node.expression = this.parseMaybeDecoratorArguments(expr);
  12270. this.state.decoratorStack.pop();
  12271. } else {
  12272. node.expression = this.parseExprSubscripts();
  12273. }
  12274. return this.finishNode(node, "Decorator");
  12275. }
  12276. parseMaybeDecoratorArguments(expr) {
  12277. if (this.eat(10)) {
  12278. const node = this.startNodeAtNode(expr);
  12279. node.callee = expr;
  12280. node.arguments = this.parseCallExpressionArguments(11, false);
  12281. this.toReferencedList(node.arguments);
  12282. return this.finishNode(node, "CallExpression");
  12283. }
  12284. return expr;
  12285. }
  12286. parseBreakContinueStatement(node, isBreak) {
  12287. this.next();
  12288. if (this.isLineTerminator()) {
  12289. node.label = null;
  12290. } else {
  12291. node.label = this.parseIdentifier();
  12292. this.semicolon();
  12293. }
  12294. this.verifyBreakContinue(node, isBreak);
  12295. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12296. }
  12297. verifyBreakContinue(node, isBreak) {
  12298. let i;
  12299. for (i = 0; i < this.state.labels.length; ++i) {
  12300. const lab = this.state.labels[i];
  12301. if (node.label == null || lab.name === node.label.name) {
  12302. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12303. if (node.label && isBreak) break;
  12304. }
  12305. }
  12306. if (i === this.state.labels.length) {
  12307. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12308. this.raise(Errors.IllegalBreakContinue, {
  12309. at: node,
  12310. type
  12311. });
  12312. }
  12313. }
  12314. parseDebuggerStatement(node) {
  12315. this.next();
  12316. this.semicolon();
  12317. return this.finishNode(node, "DebuggerStatement");
  12318. }
  12319. parseHeaderExpression() {
  12320. this.expect(10);
  12321. const val = this.parseExpression();
  12322. this.expect(11);
  12323. return val;
  12324. }
  12325. parseDoStatement(node) {
  12326. this.next();
  12327. this.state.labels.push(loopLabel);
  12328. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
  12329. this.state.labels.pop();
  12330. this.expect(92);
  12331. node.test = this.parseHeaderExpression();
  12332. this.eat(13);
  12333. return this.finishNode(node, "DoWhileStatement");
  12334. }
  12335. parseForStatement(node) {
  12336. this.next();
  12337. this.state.labels.push(loopLabel);
  12338. let awaitAt = null;
  12339. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12340. awaitAt = this.state.lastTokStartLoc;
  12341. }
  12342. this.scope.enter(SCOPE_OTHER);
  12343. this.expect(10);
  12344. if (this.match(13)) {
  12345. if (awaitAt !== null) {
  12346. this.unexpected(awaitAt);
  12347. }
  12348. return this.parseFor(node, null);
  12349. }
  12350. const startsWithLet = this.isContextual(99);
  12351. const isLet = startsWithLet && this.isLetKeyword();
  12352. if (this.match(74) || this.match(75) || isLet) {
  12353. const init = this.startNode();
  12354. const kind = isLet ? "let" : this.state.value;
  12355. this.next();
  12356. this.parseVar(init, true, kind);
  12357. this.finishNode(init, "VariableDeclaration");
  12358. if ((this.match(58) || this.isContextual(101)) && init.declarations.length === 1) {
  12359. return this.parseForIn(node, init, awaitAt);
  12360. }
  12361. if (awaitAt !== null) {
  12362. this.unexpected(awaitAt);
  12363. }
  12364. return this.parseFor(node, init);
  12365. }
  12366. const startsWithAsync = this.isContextual(95);
  12367. const refExpressionErrors = new ExpressionErrors();
  12368. const init = this.parseExpression(true, refExpressionErrors);
  12369. const isForOf = this.isContextual(101);
  12370. if (isForOf) {
  12371. if (startsWithLet) {
  12372. this.raise(Errors.ForOfLet, {
  12373. at: init
  12374. });
  12375. }
  12376. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12377. this.raise(Errors.ForOfAsync, {
  12378. at: init
  12379. });
  12380. }
  12381. }
  12382. if (isForOf || this.match(58)) {
  12383. this.checkDestructuringPrivate(refExpressionErrors);
  12384. this.toAssignable(init, true);
  12385. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12386. this.checkLVal(init, {
  12387. in: {
  12388. type
  12389. }
  12390. });
  12391. return this.parseForIn(node, init, awaitAt);
  12392. } else {
  12393. this.checkExpressionErrors(refExpressionErrors, true);
  12394. }
  12395. if (awaitAt !== null) {
  12396. this.unexpected(awaitAt);
  12397. }
  12398. return this.parseFor(node, init);
  12399. }
  12400. parseFunctionStatement(node, isAsync, declarationPosition) {
  12401. this.next();
  12402. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  12403. }
  12404. parseIfStatement(node) {
  12405. this.next();
  12406. node.test = this.parseHeaderExpression();
  12407. node.consequent = this.parseStatement("if");
  12408. node.alternate = this.eat(66) ? this.parseStatement("if") : null;
  12409. return this.finishNode(node, "IfStatement");
  12410. }
  12411. parseReturnStatement(node) {
  12412. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12413. this.raise(Errors.IllegalReturn, {
  12414. at: this.state.startLoc
  12415. });
  12416. }
  12417. this.next();
  12418. if (this.isLineTerminator()) {
  12419. node.argument = null;
  12420. } else {
  12421. node.argument = this.parseExpression();
  12422. this.semicolon();
  12423. }
  12424. return this.finishNode(node, "ReturnStatement");
  12425. }
  12426. parseSwitchStatement(node) {
  12427. this.next();
  12428. node.discriminant = this.parseHeaderExpression();
  12429. const cases = node.cases = [];
  12430. this.expect(5);
  12431. this.state.labels.push(switchLabel);
  12432. this.scope.enter(SCOPE_OTHER);
  12433. let cur;
  12434. for (let sawDefault; !this.match(8);) {
  12435. if (this.match(61) || this.match(65)) {
  12436. const isCase = this.match(61);
  12437. if (cur) this.finishNode(cur, "SwitchCase");
  12438. cases.push(cur = this.startNode());
  12439. cur.consequent = [];
  12440. this.next();
  12441. if (isCase) {
  12442. cur.test = this.parseExpression();
  12443. } else {
  12444. if (sawDefault) {
  12445. this.raise(Errors.MultipleDefaultsInSwitch, {
  12446. at: this.state.lastTokStartLoc
  12447. });
  12448. }
  12449. sawDefault = true;
  12450. cur.test = null;
  12451. }
  12452. this.expect(14);
  12453. } else {
  12454. if (cur) {
  12455. cur.consequent.push(this.parseStatement(null));
  12456. } else {
  12457. this.unexpected();
  12458. }
  12459. }
  12460. }
  12461. this.scope.exit();
  12462. if (cur) this.finishNode(cur, "SwitchCase");
  12463. this.next();
  12464. this.state.labels.pop();
  12465. return this.finishNode(node, "SwitchStatement");
  12466. }
  12467. parseThrowStatement(node) {
  12468. this.next();
  12469. if (this.hasPrecedingLineBreak()) {
  12470. this.raise(Errors.NewlineAfterThrow, {
  12471. at: this.state.lastTokEndLoc
  12472. });
  12473. }
  12474. node.argument = this.parseExpression();
  12475. this.semicolon();
  12476. return this.finishNode(node, "ThrowStatement");
  12477. }
  12478. parseCatchClauseParam() {
  12479. const param = this.parseBindingAtom();
  12480. const simple = param.type === "Identifier";
  12481. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  12482. this.checkLVal(param, {
  12483. in: {
  12484. type: "CatchClause"
  12485. },
  12486. binding: BIND_LEXICAL,
  12487. allowingSloppyLetBinding: true
  12488. });
  12489. return param;
  12490. }
  12491. parseTryStatement(node) {
  12492. this.next();
  12493. node.block = this.parseBlock();
  12494. node.handler = null;
  12495. if (this.match(62)) {
  12496. const clause = this.startNode();
  12497. this.next();
  12498. if (this.match(10)) {
  12499. this.expect(10);
  12500. clause.param = this.parseCatchClauseParam();
  12501. this.expect(11);
  12502. } else {
  12503. clause.param = null;
  12504. this.scope.enter(SCOPE_OTHER);
  12505. }
  12506. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12507. this.scope.exit();
  12508. node.handler = this.finishNode(clause, "CatchClause");
  12509. }
  12510. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12511. if (!node.handler && !node.finalizer) {
  12512. this.raise(Errors.NoCatchOrFinally, {
  12513. at: node
  12514. });
  12515. }
  12516. return this.finishNode(node, "TryStatement");
  12517. }
  12518. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12519. this.next();
  12520. this.parseVar(node, false, kind, allowMissingInitializer);
  12521. this.semicolon();
  12522. return this.finishNode(node, "VariableDeclaration");
  12523. }
  12524. parseWhileStatement(node) {
  12525. this.next();
  12526. node.test = this.parseHeaderExpression();
  12527. this.state.labels.push(loopLabel);
  12528. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
  12529. this.state.labels.pop();
  12530. return this.finishNode(node, "WhileStatement");
  12531. }
  12532. parseWithStatement(node) {
  12533. if (this.state.strict) {
  12534. this.raise(Errors.StrictWith, {
  12535. at: this.state.startLoc
  12536. });
  12537. }
  12538. this.next();
  12539. node.object = this.parseHeaderExpression();
  12540. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
  12541. return this.finishNode(node, "WithStatement");
  12542. }
  12543. parseEmptyStatement(node) {
  12544. this.next();
  12545. return this.finishNode(node, "EmptyStatement");
  12546. }
  12547. parseLabeledStatement(node, maybeName, expr, context) {
  12548. for (const label of this.state.labels) {
  12549. if (label.name === maybeName) {
  12550. this.raise(Errors.LabelRedeclaration, {
  12551. at: expr,
  12552. labelName: maybeName
  12553. });
  12554. }
  12555. }
  12556. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  12557. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  12558. const label = this.state.labels[i];
  12559. if (label.statementStart === node.start) {
  12560. label.statementStart = this.state.start;
  12561. label.kind = kind;
  12562. } else {
  12563. break;
  12564. }
  12565. }
  12566. this.state.labels.push({
  12567. name: maybeName,
  12568. kind: kind,
  12569. statementStart: this.state.start
  12570. });
  12571. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  12572. this.state.labels.pop();
  12573. node.label = expr;
  12574. return this.finishNode(node, "LabeledStatement");
  12575. }
  12576. parseExpressionStatement(node, expr) {
  12577. node.expression = expr;
  12578. this.semicolon();
  12579. return this.finishNode(node, "ExpressionStatement");
  12580. }
  12581. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  12582. const node = this.startNode();
  12583. if (allowDirectives) {
  12584. this.state.strictErrors.clear();
  12585. }
  12586. this.expect(5);
  12587. if (createNewLexicalScope) {
  12588. this.scope.enter(SCOPE_OTHER);
  12589. }
  12590. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  12591. if (createNewLexicalScope) {
  12592. this.scope.exit();
  12593. }
  12594. return this.finishNode(node, "BlockStatement");
  12595. }
  12596. isValidDirective(stmt) {
  12597. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  12598. }
  12599. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  12600. const body = node.body = [];
  12601. const directives = node.directives = [];
  12602. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  12603. }
  12604. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  12605. const oldStrict = this.state.strict;
  12606. let hasStrictModeDirective = false;
  12607. let parsedNonDirective = false;
  12608. while (!this.match(end)) {
  12609. const stmt = this.parseStatement(null, topLevel);
  12610. if (directives && !parsedNonDirective) {
  12611. if (this.isValidDirective(stmt)) {
  12612. const directive = this.stmtToDirective(stmt);
  12613. directives.push(directive);
  12614. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  12615. hasStrictModeDirective = true;
  12616. this.setStrict(true);
  12617. }
  12618. continue;
  12619. }
  12620. parsedNonDirective = true;
  12621. this.state.strictErrors.clear();
  12622. }
  12623. body.push(stmt);
  12624. }
  12625. if (afterBlockParse) {
  12626. afterBlockParse.call(this, hasStrictModeDirective);
  12627. }
  12628. if (!oldStrict) {
  12629. this.setStrict(false);
  12630. }
  12631. this.next();
  12632. }
  12633. parseFor(node, init) {
  12634. node.init = init;
  12635. this.semicolon(false);
  12636. node.test = this.match(13) ? null : this.parseExpression();
  12637. this.semicolon(false);
  12638. node.update = this.match(11) ? null : this.parseExpression();
  12639. this.expect(11);
  12640. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12641. this.scope.exit();
  12642. this.state.labels.pop();
  12643. return this.finishNode(node, "ForStatement");
  12644. }
  12645. parseForIn(node, init, awaitAt) {
  12646. const isForIn = this.match(58);
  12647. this.next();
  12648. if (isForIn) {
  12649. if (awaitAt !== null) this.unexpected(awaitAt);
  12650. } else {
  12651. node.await = awaitAt !== null;
  12652. }
  12653. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  12654. this.raise(Errors.ForInOfLoopInitializer, {
  12655. at: init,
  12656. type: isForIn ? "ForInStatement" : "ForOfStatement"
  12657. });
  12658. }
  12659. if (init.type === "AssignmentPattern") {
  12660. this.raise(Errors.InvalidLhs, {
  12661. at: init,
  12662. ancestor: {
  12663. type: "ForStatement"
  12664. }
  12665. });
  12666. }
  12667. node.left = init;
  12668. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  12669. this.expect(11);
  12670. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12671. this.scope.exit();
  12672. this.state.labels.pop();
  12673. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  12674. }
  12675. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  12676. const declarations = node.declarations = [];
  12677. node.kind = kind;
  12678. for (;;) {
  12679. const decl = this.startNode();
  12680. this.parseVarId(decl, kind);
  12681. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  12682. if (decl.init === null && !allowMissingInitializer) {
  12683. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  12684. this.raise(Errors.DeclarationMissingInitializer, {
  12685. at: this.state.lastTokEndLoc,
  12686. kind: "destructuring"
  12687. });
  12688. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  12689. this.raise(Errors.DeclarationMissingInitializer, {
  12690. at: this.state.lastTokEndLoc,
  12691. kind: "const"
  12692. });
  12693. }
  12694. }
  12695. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  12696. if (!this.eat(12)) break;
  12697. }
  12698. return node;
  12699. }
  12700. parseVarId(decl, kind) {
  12701. decl.id = this.parseBindingAtom();
  12702. this.checkLVal(decl.id, {
  12703. in: {
  12704. type: "VariableDeclarator"
  12705. },
  12706. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  12707. });
  12708. }
  12709. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  12710. const isStatement = statement & FUNC_STATEMENT;
  12711. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  12712. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  12713. this.initFunction(node, isAsync);
  12714. if (this.match(55) && isHangingStatement) {
  12715. this.raise(Errors.GeneratorInSingleStatementContext, {
  12716. at: this.state.startLoc
  12717. });
  12718. }
  12719. node.generator = this.eat(55);
  12720. if (isStatement) {
  12721. node.id = this.parseFunctionId(requireId);
  12722. }
  12723. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12724. this.state.maybeInArrowParameters = false;
  12725. this.scope.enter(SCOPE_FUNCTION);
  12726. this.prodParam.enter(functionFlags(isAsync, node.generator));
  12727. if (!isStatement) {
  12728. node.id = this.parseFunctionId();
  12729. }
  12730. this.parseFunctionParams(node, false);
  12731. this.withSmartMixTopicForbiddingContext(() => {
  12732. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  12733. });
  12734. this.prodParam.exit();
  12735. this.scope.exit();
  12736. if (isStatement && !isHangingStatement) {
  12737. this.registerFunctionStatementId(node);
  12738. }
  12739. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12740. return node;
  12741. }
  12742. parseFunctionId(requireId) {
  12743. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  12744. }
  12745. parseFunctionParams(node, allowModifiers) {
  12746. this.expect(10);
  12747. this.expressionScope.enter(newParameterDeclarationScope());
  12748. node.params = this.parseBindingList(11, 41, false, allowModifiers);
  12749. this.expressionScope.exit();
  12750. }
  12751. registerFunctionStatementId(node) {
  12752. if (!node.id) return;
  12753. 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);
  12754. }
  12755. parseClass(node, isStatement, optionalId) {
  12756. this.next();
  12757. this.takeDecorators(node);
  12758. const oldStrict = this.state.strict;
  12759. this.state.strict = true;
  12760. this.parseClassId(node, isStatement, optionalId);
  12761. this.parseClassSuper(node);
  12762. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  12763. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  12764. }
  12765. isClassProperty() {
  12766. return this.match(29) || this.match(13) || this.match(8);
  12767. }
  12768. isClassMethod() {
  12769. return this.match(10);
  12770. }
  12771. isNonstaticConstructor(method) {
  12772. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  12773. }
  12774. parseClassBody(hadSuperClass, oldStrict) {
  12775. this.classScope.enter();
  12776. const state = {
  12777. hadConstructor: false,
  12778. hadSuperClass
  12779. };
  12780. let decorators = [];
  12781. const classBody = this.startNode();
  12782. classBody.body = [];
  12783. this.expect(5);
  12784. this.withSmartMixTopicForbiddingContext(() => {
  12785. while (!this.match(8)) {
  12786. if (this.eat(13)) {
  12787. if (decorators.length > 0) {
  12788. throw this.raise(Errors.DecoratorSemicolon, {
  12789. at: this.state.lastTokEndLoc
  12790. });
  12791. }
  12792. continue;
  12793. }
  12794. if (this.match(26)) {
  12795. decorators.push(this.parseDecorator());
  12796. continue;
  12797. }
  12798. const member = this.startNode();
  12799. if (decorators.length) {
  12800. member.decorators = decorators;
  12801. this.resetStartLocationFromNode(member, decorators[0]);
  12802. decorators = [];
  12803. }
  12804. this.parseClassMember(classBody, member, state);
  12805. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  12806. this.raise(Errors.DecoratorConstructor, {
  12807. at: member
  12808. });
  12809. }
  12810. }
  12811. });
  12812. this.state.strict = oldStrict;
  12813. this.next();
  12814. if (decorators.length) {
  12815. throw this.raise(Errors.TrailingDecorator, {
  12816. at: this.state.startLoc
  12817. });
  12818. }
  12819. this.classScope.exit();
  12820. return this.finishNode(classBody, "ClassBody");
  12821. }
  12822. parseClassMemberFromModifier(classBody, member) {
  12823. const key = this.parseIdentifier(true);
  12824. if (this.isClassMethod()) {
  12825. const method = member;
  12826. method.kind = "method";
  12827. method.computed = false;
  12828. method.key = key;
  12829. method.static = false;
  12830. this.pushClassMethod(classBody, method, false, false, false, false);
  12831. return true;
  12832. } else if (this.isClassProperty()) {
  12833. const prop = member;
  12834. prop.computed = false;
  12835. prop.key = key;
  12836. prop.static = false;
  12837. classBody.body.push(this.parseClassProperty(prop));
  12838. return true;
  12839. }
  12840. this.resetPreviousNodeTrailingComments(key);
  12841. return false;
  12842. }
  12843. parseClassMember(classBody, member, state) {
  12844. const isStatic = this.isContextual(104);
  12845. if (isStatic) {
  12846. if (this.parseClassMemberFromModifier(classBody, member)) {
  12847. return;
  12848. }
  12849. if (this.eat(5)) {
  12850. this.parseClassStaticBlock(classBody, member);
  12851. return;
  12852. }
  12853. }
  12854. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  12855. }
  12856. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  12857. const publicMethod = member;
  12858. const privateMethod = member;
  12859. const publicProp = member;
  12860. const privateProp = member;
  12861. const accessorProp = member;
  12862. const method = publicMethod;
  12863. const publicMember = publicMethod;
  12864. member.static = isStatic;
  12865. this.parsePropertyNamePrefixOperator(member);
  12866. if (this.eat(55)) {
  12867. method.kind = "method";
  12868. const isPrivateName = this.match(134);
  12869. this.parseClassElementName(method);
  12870. if (isPrivateName) {
  12871. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  12872. return;
  12873. }
  12874. if (this.isNonstaticConstructor(publicMethod)) {
  12875. this.raise(Errors.ConstructorIsGenerator, {
  12876. at: publicMethod.key
  12877. });
  12878. }
  12879. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  12880. return;
  12881. }
  12882. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  12883. const isPrivate = this.match(134);
  12884. const key = this.parseClassElementName(member);
  12885. const maybeQuestionTokenStartLoc = this.state.startLoc;
  12886. this.parsePostMemberNameModifiers(publicMember);
  12887. if (this.isClassMethod()) {
  12888. method.kind = "method";
  12889. if (isPrivate) {
  12890. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  12891. return;
  12892. }
  12893. const isConstructor = this.isNonstaticConstructor(publicMethod);
  12894. let allowsDirectSuper = false;
  12895. if (isConstructor) {
  12896. publicMethod.kind = "constructor";
  12897. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  12898. this.raise(Errors.DuplicateConstructor, {
  12899. at: key
  12900. });
  12901. }
  12902. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  12903. this.raise(Errors.OverrideOnConstructor, {
  12904. at: key
  12905. });
  12906. }
  12907. state.hadConstructor = true;
  12908. allowsDirectSuper = state.hadSuperClass;
  12909. }
  12910. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  12911. } else if (this.isClassProperty()) {
  12912. if (isPrivate) {
  12913. this.pushClassPrivateProperty(classBody, privateProp);
  12914. } else {
  12915. this.pushClassProperty(classBody, publicProp);
  12916. }
  12917. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  12918. this.resetPreviousNodeTrailingComments(key);
  12919. const isGenerator = this.eat(55);
  12920. if (publicMember.optional) {
  12921. this.unexpected(maybeQuestionTokenStartLoc);
  12922. }
  12923. method.kind = "method";
  12924. const isPrivate = this.match(134);
  12925. this.parseClassElementName(method);
  12926. this.parsePostMemberNameModifiers(publicMember);
  12927. if (isPrivate) {
  12928. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  12929. } else {
  12930. if (this.isNonstaticConstructor(publicMethod)) {
  12931. this.raise(Errors.ConstructorIsAsync, {
  12932. at: publicMethod.key
  12933. });
  12934. }
  12935. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  12936. }
  12937. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  12938. this.resetPreviousNodeTrailingComments(key);
  12939. method.kind = key.name;
  12940. const isPrivate = this.match(134);
  12941. this.parseClassElementName(publicMethod);
  12942. if (isPrivate) {
  12943. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  12944. } else {
  12945. if (this.isNonstaticConstructor(publicMethod)) {
  12946. this.raise(Errors.ConstructorIsAccessor, {
  12947. at: publicMethod.key
  12948. });
  12949. }
  12950. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  12951. }
  12952. this.checkGetterSetterParams(publicMethod);
  12953. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  12954. this.expectPlugin("decoratorAutoAccessors");
  12955. this.resetPreviousNodeTrailingComments(key);
  12956. const isPrivate = this.match(134);
  12957. this.parseClassElementName(publicProp);
  12958. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  12959. } else if (this.isLineTerminator()) {
  12960. if (isPrivate) {
  12961. this.pushClassPrivateProperty(classBody, privateProp);
  12962. } else {
  12963. this.pushClassProperty(classBody, publicProp);
  12964. }
  12965. } else {
  12966. this.unexpected();
  12967. }
  12968. }
  12969. parseClassElementName(member) {
  12970. const {
  12971. type,
  12972. value
  12973. } = this.state;
  12974. if ((type === 128 || type === 129) && member.static && value === "prototype") {
  12975. this.raise(Errors.StaticPrototype, {
  12976. at: this.state.startLoc
  12977. });
  12978. }
  12979. if (type === 134) {
  12980. if (value === "constructor") {
  12981. this.raise(Errors.ConstructorClassPrivateField, {
  12982. at: this.state.startLoc
  12983. });
  12984. }
  12985. const key = this.parsePrivateName();
  12986. member.key = key;
  12987. return key;
  12988. }
  12989. return this.parsePropertyName(member);
  12990. }
  12991. parseClassStaticBlock(classBody, member) {
  12992. var _member$decorators;
  12993. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  12994. const oldLabels = this.state.labels;
  12995. this.state.labels = [];
  12996. this.prodParam.enter(PARAM);
  12997. const body = member.body = [];
  12998. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  12999. this.prodParam.exit();
  13000. this.scope.exit();
  13001. this.state.labels = oldLabels;
  13002. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13003. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13004. this.raise(Errors.DecoratorStaticBlock, {
  13005. at: member
  13006. });
  13007. }
  13008. }
  13009. pushClassProperty(classBody, prop) {
  13010. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13011. this.raise(Errors.ConstructorClassField, {
  13012. at: prop.key
  13013. });
  13014. }
  13015. classBody.body.push(this.parseClassProperty(prop));
  13016. }
  13017. pushClassPrivateProperty(classBody, prop) {
  13018. const node = this.parseClassPrivateProperty(prop);
  13019. classBody.body.push(node);
  13020. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13021. }
  13022. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13023. if (!isPrivate && !prop.computed) {
  13024. const key = prop.key;
  13025. if (key.name === "constructor" || key.value === "constructor") {
  13026. this.raise(Errors.ConstructorClassField, {
  13027. at: key
  13028. });
  13029. }
  13030. }
  13031. const node = this.parseClassAccessorProperty(prop);
  13032. classBody.body.push(node);
  13033. if (isPrivate) {
  13034. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13035. }
  13036. }
  13037. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13038. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13039. }
  13040. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13041. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13042. classBody.body.push(node);
  13043. 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;
  13044. this.declareClassPrivateMethodInScope(node, kind);
  13045. }
  13046. declareClassPrivateMethodInScope(node, kind) {
  13047. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13048. }
  13049. parsePostMemberNameModifiers(methodOrProp) {}
  13050. parseClassPrivateProperty(node) {
  13051. this.parseInitializer(node);
  13052. this.semicolon();
  13053. return this.finishNode(node, "ClassPrivateProperty");
  13054. }
  13055. parseClassProperty(node) {
  13056. this.parseInitializer(node);
  13057. this.semicolon();
  13058. return this.finishNode(node, "ClassProperty");
  13059. }
  13060. parseClassAccessorProperty(node) {
  13061. this.parseInitializer(node);
  13062. this.semicolon();
  13063. return this.finishNode(node, "ClassAccessorProperty");
  13064. }
  13065. parseInitializer(node) {
  13066. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13067. this.expressionScope.enter(newExpressionScope());
  13068. this.prodParam.enter(PARAM);
  13069. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13070. this.expressionScope.exit();
  13071. this.prodParam.exit();
  13072. this.scope.exit();
  13073. }
  13074. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13075. if (tokenIsIdentifier(this.state.type)) {
  13076. node.id = this.parseIdentifier();
  13077. if (isStatement) {
  13078. this.declareNameFromIdentifier(node.id, bindingType);
  13079. }
  13080. } else {
  13081. if (optionalId || !isStatement) {
  13082. node.id = null;
  13083. } else {
  13084. throw this.raise(Errors.MissingClassName, {
  13085. at: this.state.startLoc
  13086. });
  13087. }
  13088. }
  13089. }
  13090. parseClassSuper(node) {
  13091. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13092. }
  13093. parseExport(node) {
  13094. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13095. const parseAfterDefault = !hasDefault || this.eat(12);
  13096. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13097. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13098. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13099. const isFromRequired = hasDefault || hasStar;
  13100. if (hasStar && !hasNamespace) {
  13101. if (hasDefault) this.unexpected();
  13102. this.parseExportFrom(node, true);
  13103. return this.finishNode(node, "ExportAllDeclaration");
  13104. }
  13105. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13106. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  13107. throw this.unexpected(null, 5);
  13108. }
  13109. let hasDeclaration;
  13110. if (isFromRequired || hasSpecifiers) {
  13111. hasDeclaration = false;
  13112. this.parseExportFrom(node, isFromRequired);
  13113. } else {
  13114. hasDeclaration = this.maybeParseExportDeclaration(node);
  13115. }
  13116. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13117. this.checkExport(node, true, false, !!node.source);
  13118. return this.finishNode(node, "ExportNamedDeclaration");
  13119. }
  13120. if (this.eat(65)) {
  13121. node.declaration = this.parseExportDefaultExpression();
  13122. this.checkExport(node, true, true);
  13123. return this.finishNode(node, "ExportDefaultDeclaration");
  13124. }
  13125. throw this.unexpected(null, 5);
  13126. }
  13127. eatExportStar(node) {
  13128. return this.eat(55);
  13129. }
  13130. maybeParseExportDefaultSpecifier(node) {
  13131. if (this.isExportDefaultSpecifier()) {
  13132. this.expectPlugin("exportDefaultFrom");
  13133. const specifier = this.startNode();
  13134. specifier.exported = this.parseIdentifier(true);
  13135. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13136. return true;
  13137. }
  13138. return false;
  13139. }
  13140. maybeParseExportNamespaceSpecifier(node) {
  13141. if (this.isContextual(93)) {
  13142. if (!node.specifiers) node.specifiers = [];
  13143. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  13144. this.next();
  13145. specifier.exported = this.parseModuleExportName();
  13146. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13147. return true;
  13148. }
  13149. return false;
  13150. }
  13151. maybeParseExportNamedSpecifiers(node) {
  13152. if (this.match(5)) {
  13153. if (!node.specifiers) node.specifiers = [];
  13154. const isTypeExport = node.exportKind === "type";
  13155. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13156. node.source = null;
  13157. node.declaration = null;
  13158. if (this.hasPlugin("importAssertions")) {
  13159. node.assertions = [];
  13160. }
  13161. return true;
  13162. }
  13163. return false;
  13164. }
  13165. maybeParseExportDeclaration(node) {
  13166. if (this.shouldParseExportDeclaration()) {
  13167. node.specifiers = [];
  13168. node.source = null;
  13169. if (this.hasPlugin("importAssertions")) {
  13170. node.assertions = [];
  13171. }
  13172. node.declaration = this.parseExportDeclaration(node);
  13173. return true;
  13174. }
  13175. return false;
  13176. }
  13177. isAsyncFunction() {
  13178. if (!this.isContextual(95)) return false;
  13179. const next = this.nextTokenStart();
  13180. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13181. }
  13182. parseExportDefaultExpression() {
  13183. const expr = this.startNode();
  13184. const isAsync = this.isAsyncFunction();
  13185. if (this.match(68) || isAsync) {
  13186. this.next();
  13187. if (isAsync) {
  13188. this.next();
  13189. }
  13190. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  13191. }
  13192. if (this.match(80)) {
  13193. return this.parseClass(expr, true, true);
  13194. }
  13195. if (this.match(26)) {
  13196. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13197. this.raise(Errors.DecoratorBeforeExport, {
  13198. at: this.state.startLoc
  13199. });
  13200. }
  13201. this.parseDecorators(false);
  13202. return this.parseClass(expr, true, true);
  13203. }
  13204. if (this.match(75) || this.match(74) || this.isLet()) {
  13205. throw this.raise(Errors.UnsupportedDefaultExport, {
  13206. at: this.state.startLoc
  13207. });
  13208. }
  13209. const res = this.parseMaybeAssignAllowIn();
  13210. this.semicolon();
  13211. return res;
  13212. }
  13213. parseExportDeclaration(node) {
  13214. return this.parseStatement(null);
  13215. }
  13216. isExportDefaultSpecifier() {
  13217. const {
  13218. type
  13219. } = this.state;
  13220. if (tokenIsIdentifier(type)) {
  13221. if (type === 95 && !this.state.containsEsc || type === 99) {
  13222. return false;
  13223. }
  13224. if ((type === 126 || type === 125) && !this.state.containsEsc) {
  13225. const {
  13226. type: nextType
  13227. } = this.lookahead();
  13228. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13229. this.expectOnePlugin(["flow", "typescript"]);
  13230. return false;
  13231. }
  13232. }
  13233. } else if (!this.match(65)) {
  13234. return false;
  13235. }
  13236. const next = this.nextTokenStart();
  13237. const hasFrom = this.isUnparsedContextual(next, "from");
  13238. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13239. return true;
  13240. }
  13241. if (this.match(65) && hasFrom) {
  13242. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13243. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13244. }
  13245. return false;
  13246. }
  13247. parseExportFrom(node, expect) {
  13248. if (this.eatContextual(97)) {
  13249. node.source = this.parseImportSource();
  13250. this.checkExport(node);
  13251. const assertions = this.maybeParseImportAssertions();
  13252. if (assertions) {
  13253. node.assertions = assertions;
  13254. }
  13255. } else if (expect) {
  13256. this.unexpected();
  13257. }
  13258. this.semicolon();
  13259. }
  13260. shouldParseExportDeclaration() {
  13261. const {
  13262. type
  13263. } = this.state;
  13264. if (type === 26) {
  13265. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13266. if (this.hasPlugin("decorators")) {
  13267. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13268. throw this.raise(Errors.DecoratorBeforeExport, {
  13269. at: this.state.startLoc
  13270. });
  13271. }
  13272. return true;
  13273. }
  13274. }
  13275. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13276. }
  13277. checkExport(node, checkNames, isDefault, isFrom) {
  13278. if (checkNames) {
  13279. if (isDefault) {
  13280. this.checkDuplicateExports(node, "default");
  13281. if (this.hasPlugin("exportDefaultFrom")) {
  13282. var _declaration$extra;
  13283. const declaration = node.declaration;
  13284. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13285. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13286. at: declaration
  13287. });
  13288. }
  13289. }
  13290. } else if (node.specifiers && node.specifiers.length) {
  13291. for (const specifier of node.specifiers) {
  13292. const {
  13293. exported
  13294. } = specifier;
  13295. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13296. this.checkDuplicateExports(specifier, exportName);
  13297. if (!isFrom && specifier.local) {
  13298. const {
  13299. local
  13300. } = specifier;
  13301. if (local.type !== "Identifier") {
  13302. this.raise(Errors.ExportBindingIsString, {
  13303. at: specifier,
  13304. localName: local.value,
  13305. exportName
  13306. });
  13307. } else {
  13308. this.checkReservedWord(local.name, local.loc.start, true, false);
  13309. this.scope.checkLocalExport(local);
  13310. }
  13311. }
  13312. }
  13313. } else if (node.declaration) {
  13314. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13315. const id = node.declaration.id;
  13316. if (!id) throw new Error("Assertion failure");
  13317. this.checkDuplicateExports(node, id.name);
  13318. } else if (node.declaration.type === "VariableDeclaration") {
  13319. for (const declaration of node.declaration.declarations) {
  13320. this.checkDeclaration(declaration.id);
  13321. }
  13322. }
  13323. }
  13324. }
  13325. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  13326. if (currentContextDecorators.length) {
  13327. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13328. at: node
  13329. });
  13330. }
  13331. }
  13332. checkDeclaration(node) {
  13333. if (node.type === "Identifier") {
  13334. this.checkDuplicateExports(node, node.name);
  13335. } else if (node.type === "ObjectPattern") {
  13336. for (const prop of node.properties) {
  13337. this.checkDeclaration(prop);
  13338. }
  13339. } else if (node.type === "ArrayPattern") {
  13340. for (const elem of node.elements) {
  13341. if (elem) {
  13342. this.checkDeclaration(elem);
  13343. }
  13344. }
  13345. } else if (node.type === "ObjectProperty") {
  13346. this.checkDeclaration(node.value);
  13347. } else if (node.type === "RestElement") {
  13348. this.checkDeclaration(node.argument);
  13349. } else if (node.type === "AssignmentPattern") {
  13350. this.checkDeclaration(node.left);
  13351. }
  13352. }
  13353. checkDuplicateExports(node, exportName) {
  13354. if (this.exportedIdentifiers.has(exportName)) {
  13355. if (exportName === "default") {
  13356. this.raise(Errors.DuplicateDefaultExport, {
  13357. at: node
  13358. });
  13359. } else {
  13360. this.raise(Errors.DuplicateExport, {
  13361. at: node,
  13362. exportName
  13363. });
  13364. }
  13365. }
  13366. this.exportedIdentifiers.add(exportName);
  13367. }
  13368. parseExportSpecifiers(isInTypeExport) {
  13369. const nodes = [];
  13370. let first = true;
  13371. this.expect(5);
  13372. while (!this.eat(8)) {
  13373. if (first) {
  13374. first = false;
  13375. } else {
  13376. this.expect(12);
  13377. if (this.eat(8)) break;
  13378. }
  13379. const isMaybeTypeOnly = this.isContextual(126);
  13380. const isString = this.match(129);
  13381. const node = this.startNode();
  13382. node.local = this.parseModuleExportName();
  13383. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13384. }
  13385. return nodes;
  13386. }
  13387. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13388. if (this.eatContextual(93)) {
  13389. node.exported = this.parseModuleExportName();
  13390. } else if (isString) {
  13391. node.exported = cloneStringLiteral(node.local);
  13392. } else if (!node.exported) {
  13393. node.exported = cloneIdentifier(node.local);
  13394. }
  13395. return this.finishNode(node, "ExportSpecifier");
  13396. }
  13397. parseModuleExportName() {
  13398. if (this.match(129)) {
  13399. const result = this.parseStringLiteral(this.state.value);
  13400. const surrogate = result.value.match(loneSurrogate);
  13401. if (surrogate) {
  13402. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13403. at: result,
  13404. surrogateCharCode: surrogate[0].charCodeAt(0)
  13405. });
  13406. }
  13407. return result;
  13408. }
  13409. return this.parseIdentifier(true);
  13410. }
  13411. parseImport(node) {
  13412. node.specifiers = [];
  13413. if (!this.match(129)) {
  13414. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  13415. const parseNext = !hasDefault || this.eat(12);
  13416. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  13417. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  13418. this.expectContextual(97);
  13419. }
  13420. node.source = this.parseImportSource();
  13421. const assertions = this.maybeParseImportAssertions();
  13422. if (assertions) {
  13423. node.assertions = assertions;
  13424. } else {
  13425. const attributes = this.maybeParseModuleAttributes();
  13426. if (attributes) {
  13427. node.attributes = attributes;
  13428. }
  13429. }
  13430. this.semicolon();
  13431. return this.finishNode(node, "ImportDeclaration");
  13432. }
  13433. parseImportSource() {
  13434. if (!this.match(129)) this.unexpected();
  13435. return this.parseExprAtom();
  13436. }
  13437. shouldParseDefaultImport(node) {
  13438. return tokenIsIdentifier(this.state.type);
  13439. }
  13440. parseImportSpecifierLocal(node, specifier, type) {
  13441. specifier.local = this.parseIdentifier();
  13442. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  13443. }
  13444. finishImportSpecifier(specifier, type) {
  13445. this.checkLVal(specifier.local, {
  13446. in: specifier,
  13447. binding: BIND_LEXICAL
  13448. });
  13449. return this.finishNode(specifier, type);
  13450. }
  13451. parseAssertEntries() {
  13452. const attrs = [];
  13453. const attrNames = new Set();
  13454. do {
  13455. if (this.match(8)) {
  13456. break;
  13457. }
  13458. const node = this.startNode();
  13459. const keyName = this.state.value;
  13460. if (attrNames.has(keyName)) {
  13461. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13462. at: this.state.startLoc,
  13463. key: keyName
  13464. });
  13465. }
  13466. attrNames.add(keyName);
  13467. if (this.match(129)) {
  13468. node.key = this.parseStringLiteral(keyName);
  13469. } else {
  13470. node.key = this.parseIdentifier(true);
  13471. }
  13472. this.expect(14);
  13473. if (!this.match(129)) {
  13474. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13475. at: this.state.startLoc
  13476. });
  13477. }
  13478. node.value = this.parseStringLiteral(this.state.value);
  13479. this.finishNode(node, "ImportAttribute");
  13480. attrs.push(node);
  13481. } while (this.eat(12));
  13482. return attrs;
  13483. }
  13484. maybeParseModuleAttributes() {
  13485. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  13486. this.expectPlugin("moduleAttributes");
  13487. this.next();
  13488. } else {
  13489. if (this.hasPlugin("moduleAttributes")) return [];
  13490. return null;
  13491. }
  13492. const attrs = [];
  13493. const attributes = new Set();
  13494. do {
  13495. const node = this.startNode();
  13496. node.key = this.parseIdentifier(true);
  13497. if (node.key.name !== "type") {
  13498. this.raise(Errors.ModuleAttributeDifferentFromType, {
  13499. at: node.key
  13500. });
  13501. }
  13502. if (attributes.has(node.key.name)) {
  13503. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13504. at: node.key,
  13505. key: node.key.name
  13506. });
  13507. }
  13508. attributes.add(node.key.name);
  13509. this.expect(14);
  13510. if (!this.match(129)) {
  13511. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13512. at: this.state.startLoc
  13513. });
  13514. }
  13515. node.value = this.parseStringLiteral(this.state.value);
  13516. this.finishNode(node, "ImportAttribute");
  13517. attrs.push(node);
  13518. } while (this.eat(12));
  13519. return attrs;
  13520. }
  13521. maybeParseImportAssertions() {
  13522. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  13523. this.expectPlugin("importAssertions");
  13524. this.next();
  13525. } else {
  13526. if (this.hasPlugin("importAssertions")) return [];
  13527. return null;
  13528. }
  13529. this.eat(5);
  13530. const attrs = this.parseAssertEntries();
  13531. this.eat(8);
  13532. return attrs;
  13533. }
  13534. maybeParseDefaultImportSpecifier(node) {
  13535. if (this.shouldParseDefaultImport(node)) {
  13536. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  13537. return true;
  13538. }
  13539. return false;
  13540. }
  13541. maybeParseStarImportSpecifier(node) {
  13542. if (this.match(55)) {
  13543. const specifier = this.startNode();
  13544. this.next();
  13545. this.expectContextual(93);
  13546. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  13547. return true;
  13548. }
  13549. return false;
  13550. }
  13551. parseNamedImportSpecifiers(node) {
  13552. let first = true;
  13553. this.expect(5);
  13554. while (!this.eat(8)) {
  13555. if (first) {
  13556. first = false;
  13557. } else {
  13558. if (this.eat(14)) {
  13559. throw this.raise(Errors.DestructureNamedImport, {
  13560. at: this.state.startLoc
  13561. });
  13562. }
  13563. this.expect(12);
  13564. if (this.eat(8)) break;
  13565. }
  13566. const specifier = this.startNode();
  13567. const importedIsString = this.match(129);
  13568. const isMaybeTypeOnly = this.isContextual(126);
  13569. specifier.imported = this.parseModuleExportName();
  13570. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly);
  13571. node.specifiers.push(importSpecifier);
  13572. }
  13573. }
  13574. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  13575. if (this.eatContextual(93)) {
  13576. specifier.local = this.parseIdentifier();
  13577. } else {
  13578. const {
  13579. imported
  13580. } = specifier;
  13581. if (importedIsString) {
  13582. throw this.raise(Errors.ImportBindingIsString, {
  13583. at: specifier,
  13584. importName: imported.value
  13585. });
  13586. }
  13587. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  13588. if (!specifier.local) {
  13589. specifier.local = cloneIdentifier(imported);
  13590. }
  13591. }
  13592. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  13593. }
  13594. isThisParam(param) {
  13595. return param.type === "Identifier" && param.name === "this";
  13596. }
  13597. }
  13598. class Parser extends StatementParser {
  13599. constructor(options, input) {
  13600. options = getOptions(options);
  13601. super(options, input);
  13602. this.options = options;
  13603. this.initializeScopes();
  13604. this.plugins = pluginsMap(this.options.plugins);
  13605. this.filename = options.sourceFilename;
  13606. }
  13607. getScopeHandler() {
  13608. return ScopeHandler;
  13609. }
  13610. parse() {
  13611. this.enterInitialScopes();
  13612. const file = this.startNode();
  13613. const program = this.startNode();
  13614. this.nextToken();
  13615. file.errors = null;
  13616. this.parseTopLevel(file, program);
  13617. file.errors = this.state.errors;
  13618. return file;
  13619. }
  13620. }
  13621. function pluginsMap(plugins) {
  13622. const pluginMap = new Map();
  13623. for (const plugin of plugins) {
  13624. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  13625. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  13626. }
  13627. return pluginMap;
  13628. }
  13629. function parse(input, options) {
  13630. var _options;
  13631. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  13632. options = Object.assign({}, options);
  13633. try {
  13634. options.sourceType = "module";
  13635. const parser = getParser(options, input);
  13636. const ast = parser.parse();
  13637. if (parser.sawUnambiguousESM) {
  13638. return ast;
  13639. }
  13640. if (parser.ambiguousScriptDifferentAst) {
  13641. try {
  13642. options.sourceType = "script";
  13643. return getParser(options, input).parse();
  13644. } catch (_unused) {}
  13645. } else {
  13646. ast.program.sourceType = "script";
  13647. }
  13648. return ast;
  13649. } catch (moduleError) {
  13650. try {
  13651. options.sourceType = "script";
  13652. return getParser(options, input).parse();
  13653. } catch (_unused2) {}
  13654. throw moduleError;
  13655. }
  13656. } else {
  13657. return getParser(options, input).parse();
  13658. }
  13659. }
  13660. function parseExpression(input, options) {
  13661. const parser = getParser(options, input);
  13662. if (parser.options.strictMode) {
  13663. parser.state.strict = true;
  13664. }
  13665. return parser.getExpression();
  13666. }
  13667. function generateExportedTokenTypes(internalTokenTypes) {
  13668. const tokenTypes = {};
  13669. for (const typeName of Object.keys(internalTokenTypes)) {
  13670. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  13671. }
  13672. return tokenTypes;
  13673. }
  13674. const tokTypes = generateExportedTokenTypes(tt);
  13675. function getParser(options, input) {
  13676. let cls = Parser;
  13677. if (options != null && options.plugins) {
  13678. validatePlugins(options.plugins);
  13679. cls = getParserClass(options.plugins);
  13680. }
  13681. return new cls(options, input);
  13682. }
  13683. const parserClassCache = {};
  13684. function getParserClass(pluginsFromOptions) {
  13685. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  13686. const key = pluginList.join("/");
  13687. let cls = parserClassCache[key];
  13688. if (!cls) {
  13689. cls = Parser;
  13690. for (const plugin of pluginList) {
  13691. cls = mixinPlugins[plugin](cls);
  13692. }
  13693. parserClassCache[key] = cls;
  13694. }
  13695. return cls;
  13696. }
  13697. exports.parse = parse;
  13698. exports.parseExpression = parseExpression;
  13699. exports.tokTypes = tokTypes;
  13700. //# sourceMappingURL=index.js.map