index.js 459 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528
  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. } else {
  617. super.toAssignable(node, isLHS);
  618. }
  619. }
  620. toAssignableObjectExpressionProp(prop) {
  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(...arguments);
  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. recordArrowParemeterBindingError(error, {
  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(error, origin);
  3275. } else if (scope.canBeArrowParameterDeclaration()) {
  3276. scope.recordDeclarationError(error, 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 (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5239. node.left = this.typeCastToParameter(node.left);
  5240. }
  5241. super.toAssignable(...arguments);
  5242. }
  5243. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5244. for (let i = 0; i < exprList.length; i++) {
  5245. const expr = exprList[i];
  5246. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5247. exprList[i] = this.typeCastToParameter(expr);
  5248. }
  5249. }
  5250. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5251. }
  5252. toReferencedList(exprList, isParenthesizedExpr) {
  5253. for (let i = 0; i < exprList.length; i++) {
  5254. var _expr$extra;
  5255. const expr = exprList[i];
  5256. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5257. this.raise(FlowErrors.TypeCastInPattern, {
  5258. at: expr.typeAnnotation
  5259. });
  5260. }
  5261. }
  5262. return exprList;
  5263. }
  5264. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5265. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5266. if (canBePattern && !this.state.maybeInArrowParameters) {
  5267. this.toReferencedList(node.elements);
  5268. }
  5269. return node;
  5270. }
  5271. isValidLVal(type, ...rest) {
  5272. return type === "TypeCastExpression" || super.isValidLVal(type, ...rest);
  5273. }
  5274. parseClassProperty(node) {
  5275. if (this.match(14)) {
  5276. node.typeAnnotation = this.flowParseTypeAnnotation();
  5277. }
  5278. return super.parseClassProperty(node);
  5279. }
  5280. parseClassPrivateProperty(node) {
  5281. if (this.match(14)) {
  5282. node.typeAnnotation = this.flowParseTypeAnnotation();
  5283. }
  5284. return super.parseClassPrivateProperty(node);
  5285. }
  5286. isClassMethod() {
  5287. return this.match(47) || super.isClassMethod();
  5288. }
  5289. isClassProperty() {
  5290. return this.match(14) || super.isClassProperty();
  5291. }
  5292. isNonstaticConstructor(method) {
  5293. return !this.match(14) && super.isNonstaticConstructor(method);
  5294. }
  5295. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5296. if (method.variance) {
  5297. this.unexpected(method.variance.loc.start);
  5298. }
  5299. delete method.variance;
  5300. if (this.match(47)) {
  5301. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5302. }
  5303. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5304. if (method.params && isConstructor) {
  5305. const params = method.params;
  5306. if (params.length > 0 && this.isThisParam(params[0])) {
  5307. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5308. at: method
  5309. });
  5310. }
  5311. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5312. const params = method.value.params;
  5313. if (params.length > 0 && this.isThisParam(params[0])) {
  5314. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5315. at: method
  5316. });
  5317. }
  5318. }
  5319. }
  5320. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5321. if (method.variance) {
  5322. this.unexpected(method.variance.loc.start);
  5323. }
  5324. delete method.variance;
  5325. if (this.match(47)) {
  5326. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5327. }
  5328. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5329. }
  5330. parseClassSuper(node) {
  5331. super.parseClassSuper(node);
  5332. if (node.superClass && this.match(47)) {
  5333. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5334. }
  5335. if (this.isContextual(110)) {
  5336. this.next();
  5337. const implemented = node.implements = [];
  5338. do {
  5339. const node = this.startNode();
  5340. node.id = this.flowParseRestrictedIdentifier(true);
  5341. if (this.match(47)) {
  5342. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5343. } else {
  5344. node.typeParameters = null;
  5345. }
  5346. implemented.push(this.finishNode(node, "ClassImplements"));
  5347. } while (this.eat(12));
  5348. }
  5349. }
  5350. checkGetterSetterParams(method) {
  5351. super.checkGetterSetterParams(method);
  5352. const params = this.getObjectOrClassMethodParams(method);
  5353. if (params.length > 0) {
  5354. const param = params[0];
  5355. if (this.isThisParam(param) && method.kind === "get") {
  5356. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5357. at: param
  5358. });
  5359. } else if (this.isThisParam(param)) {
  5360. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5361. at: param
  5362. });
  5363. }
  5364. }
  5365. }
  5366. parsePropertyNamePrefixOperator(node) {
  5367. node.variance = this.flowParseVariance();
  5368. }
  5369. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5370. if (prop.variance) {
  5371. this.unexpected(prop.variance.loc.start);
  5372. }
  5373. delete prop.variance;
  5374. let typeParameters;
  5375. if (this.match(47) && !isAccessor) {
  5376. typeParameters = this.flowParseTypeParameterDeclaration();
  5377. if (!this.match(10)) this.unexpected();
  5378. }
  5379. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5380. if (typeParameters) {
  5381. (prop.value || prop).typeParameters = typeParameters;
  5382. }
  5383. }
  5384. parseAssignableListItemTypes(param) {
  5385. if (this.eat(17)) {
  5386. if (param.type !== "Identifier") {
  5387. this.raise(FlowErrors.PatternIsOptional, {
  5388. at: param
  5389. });
  5390. }
  5391. if (this.isThisParam(param)) {
  5392. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5393. at: param
  5394. });
  5395. }
  5396. param.optional = true;
  5397. }
  5398. if (this.match(14)) {
  5399. param.typeAnnotation = this.flowParseTypeAnnotation();
  5400. } else if (this.isThisParam(param)) {
  5401. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5402. at: param
  5403. });
  5404. }
  5405. if (this.match(29) && this.isThisParam(param)) {
  5406. this.raise(FlowErrors.ThisParamNoDefault, {
  5407. at: param
  5408. });
  5409. }
  5410. this.resetEndLocation(param);
  5411. return param;
  5412. }
  5413. parseMaybeDefault(startPos, startLoc, left) {
  5414. const node = super.parseMaybeDefault(startPos, startLoc, left);
  5415. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5416. this.raise(FlowErrors.TypeBeforeInitializer, {
  5417. at: node.typeAnnotation
  5418. });
  5419. }
  5420. return node;
  5421. }
  5422. shouldParseDefaultImport(node) {
  5423. if (!hasTypeImportKind(node)) {
  5424. return super.shouldParseDefaultImport(node);
  5425. }
  5426. return isMaybeDefaultImport(this.state.type);
  5427. }
  5428. parseImportSpecifierLocal(node, specifier, type) {
  5429. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5430. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5431. }
  5432. maybeParseDefaultImportSpecifier(node) {
  5433. node.importKind = "value";
  5434. let kind = null;
  5435. if (this.match(87)) {
  5436. kind = "typeof";
  5437. } else if (this.isContextual(126)) {
  5438. kind = "type";
  5439. }
  5440. if (kind) {
  5441. const lh = this.lookahead();
  5442. const {
  5443. type
  5444. } = lh;
  5445. if (kind === "type" && type === 55) {
  5446. this.unexpected(null, lh.type);
  5447. }
  5448. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5449. this.next();
  5450. node.importKind = kind;
  5451. }
  5452. }
  5453. return super.maybeParseDefaultImportSpecifier(node);
  5454. }
  5455. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  5456. const firstIdent = specifier.imported;
  5457. let specifierTypeKind = null;
  5458. if (firstIdent.type === "Identifier") {
  5459. if (firstIdent.name === "type") {
  5460. specifierTypeKind = "type";
  5461. } else if (firstIdent.name === "typeof") {
  5462. specifierTypeKind = "typeof";
  5463. }
  5464. }
  5465. let isBinding = false;
  5466. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5467. const as_ident = this.parseIdentifier(true);
  5468. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5469. specifier.imported = as_ident;
  5470. specifier.importKind = specifierTypeKind;
  5471. specifier.local = cloneIdentifier(as_ident);
  5472. } else {
  5473. specifier.imported = firstIdent;
  5474. specifier.importKind = null;
  5475. specifier.local = this.parseIdentifier();
  5476. }
  5477. } else {
  5478. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5479. specifier.imported = this.parseIdentifier(true);
  5480. specifier.importKind = specifierTypeKind;
  5481. } else {
  5482. if (importedIsString) {
  5483. throw this.raise(Errors.ImportBindingIsString, {
  5484. at: specifier,
  5485. importName: firstIdent.value
  5486. });
  5487. }
  5488. specifier.imported = firstIdent;
  5489. specifier.importKind = null;
  5490. }
  5491. if (this.eatContextual(93)) {
  5492. specifier.local = this.parseIdentifier();
  5493. } else {
  5494. isBinding = true;
  5495. specifier.local = cloneIdentifier(specifier.imported);
  5496. }
  5497. }
  5498. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5499. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5500. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5501. at: specifier
  5502. });
  5503. }
  5504. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5505. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5506. }
  5507. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5508. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5509. }
  5510. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5511. }
  5512. parseBindingAtom() {
  5513. switch (this.state.type) {
  5514. case 78:
  5515. return this.parseIdentifier(true);
  5516. default:
  5517. return super.parseBindingAtom();
  5518. }
  5519. }
  5520. parseFunctionParams(node, allowModifiers) {
  5521. const kind = node.kind;
  5522. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5523. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5524. }
  5525. super.parseFunctionParams(node, allowModifiers);
  5526. }
  5527. parseVarId(decl, kind) {
  5528. super.parseVarId(decl, kind);
  5529. if (this.match(14)) {
  5530. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5531. this.resetEndLocation(decl.id);
  5532. }
  5533. }
  5534. parseAsyncArrowFromCallExpression(node, call) {
  5535. if (this.match(14)) {
  5536. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5537. this.state.noAnonFunctionType = true;
  5538. node.returnType = this.flowParseTypeAnnotation();
  5539. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5540. }
  5541. return super.parseAsyncArrowFromCallExpression(node, call);
  5542. }
  5543. shouldParseAsyncArrow() {
  5544. return this.match(14) || super.shouldParseAsyncArrow();
  5545. }
  5546. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5547. var _jsx;
  5548. let state = null;
  5549. let jsx;
  5550. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  5551. state = this.state.clone();
  5552. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5553. if (!jsx.error) return jsx.node;
  5554. const {
  5555. context
  5556. } = this.state;
  5557. const currentContext = context[context.length - 1];
  5558. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5559. context.pop();
  5560. }
  5561. }
  5562. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5563. var _jsx2, _jsx3;
  5564. state = state || this.state.clone();
  5565. let typeParameters;
  5566. const arrow = this.tryParse(abort => {
  5567. var _arrowExpression$extr;
  5568. typeParameters = this.flowParseTypeParameterDeclaration();
  5569. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5570. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5571. this.resetStartLocationFromNode(result, typeParameters);
  5572. return result;
  5573. });
  5574. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5575. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5576. if (expr.type !== "ArrowFunctionExpression") abort();
  5577. expr.typeParameters = typeParameters;
  5578. this.resetStartLocationFromNode(expr, typeParameters);
  5579. return arrowExpression;
  5580. }, state);
  5581. let arrowExpression = null;
  5582. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5583. if (!arrow.error && !arrow.aborted) {
  5584. if (arrow.node.async) {
  5585. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5586. at: typeParameters
  5587. });
  5588. }
  5589. return arrow.node;
  5590. }
  5591. arrowExpression = arrow.node;
  5592. }
  5593. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5594. this.state = jsx.failState;
  5595. return jsx.node;
  5596. }
  5597. if (arrowExpression) {
  5598. this.state = arrow.failState;
  5599. return arrowExpression;
  5600. }
  5601. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5602. if (arrow.thrown) throw arrow.error;
  5603. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5604. at: typeParameters
  5605. });
  5606. }
  5607. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5608. }
  5609. parseArrow(node) {
  5610. if (this.match(14)) {
  5611. const result = this.tryParse(() => {
  5612. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5613. this.state.noAnonFunctionType = true;
  5614. const typeNode = this.startNode();
  5615. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5616. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5617. if (this.canInsertSemicolon()) this.unexpected();
  5618. if (!this.match(19)) this.unexpected();
  5619. return typeNode;
  5620. });
  5621. if (result.thrown) return null;
  5622. if (result.error) this.state = result.failState;
  5623. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5624. }
  5625. return super.parseArrow(node);
  5626. }
  5627. shouldParseArrow(params) {
  5628. return this.match(14) || super.shouldParseArrow(params);
  5629. }
  5630. setArrowFunctionParameters(node, params) {
  5631. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5632. node.params = params;
  5633. } else {
  5634. super.setArrowFunctionParameters(node, params);
  5635. }
  5636. }
  5637. checkParams(node, allowDuplicates, isArrowFunction) {
  5638. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5639. return;
  5640. }
  5641. for (let i = 0; i < node.params.length; i++) {
  5642. if (this.isThisParam(node.params[i]) && i > 0) {
  5643. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5644. at: node.params[i]
  5645. });
  5646. }
  5647. }
  5648. return super.checkParams(...arguments);
  5649. }
  5650. parseParenAndDistinguishExpression(canBeArrow) {
  5651. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5652. }
  5653. parseSubscripts(base, startPos, startLoc, noCalls) {
  5654. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  5655. this.next();
  5656. const node = this.startNodeAt(startPos, startLoc);
  5657. node.callee = base;
  5658. node.arguments = this.parseCallExpressionArguments(11, false);
  5659. base = this.finishNode(node, "CallExpression");
  5660. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5661. const state = this.state.clone();
  5662. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  5663. if (!arrow.error && !arrow.aborted) return arrow.node;
  5664. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  5665. if (result.node && !result.error) return result.node;
  5666. if (arrow.node) {
  5667. this.state = arrow.failState;
  5668. return arrow.node;
  5669. }
  5670. if (result.node) {
  5671. this.state = result.failState;
  5672. return result.node;
  5673. }
  5674. throw arrow.error || result.error;
  5675. }
  5676. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  5677. }
  5678. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  5679. if (this.match(18) && this.isLookaheadToken_lt()) {
  5680. subscriptState.optionalChainMember = true;
  5681. if (noCalls) {
  5682. subscriptState.stop = true;
  5683. return base;
  5684. }
  5685. this.next();
  5686. const node = this.startNodeAt(startPos, startLoc);
  5687. node.callee = base;
  5688. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5689. this.expect(10);
  5690. node.arguments = this.parseCallExpressionArguments(11, false);
  5691. node.optional = true;
  5692. return this.finishCallExpression(node, true);
  5693. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5694. const node = this.startNodeAt(startPos, startLoc);
  5695. node.callee = base;
  5696. const result = this.tryParse(() => {
  5697. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5698. this.expect(10);
  5699. node.arguments = this.parseCallExpressionArguments(11, false);
  5700. if (subscriptState.optionalChainMember) node.optional = false;
  5701. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5702. });
  5703. if (result.node) {
  5704. if (result.error) this.state = result.failState;
  5705. return result.node;
  5706. }
  5707. }
  5708. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  5709. }
  5710. parseNewArguments(node) {
  5711. let targs = null;
  5712. if (this.shouldParseTypes() && this.match(47)) {
  5713. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5714. }
  5715. node.typeArguments = targs;
  5716. super.parseNewArguments(node);
  5717. }
  5718. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  5719. const node = this.startNodeAt(startPos, startLoc);
  5720. this.parseFunctionParams(node);
  5721. if (!this.parseArrow(node)) return;
  5722. return this.parseArrowExpression(node, undefined, true);
  5723. }
  5724. readToken_mult_modulo(code) {
  5725. const next = this.input.charCodeAt(this.state.pos + 1);
  5726. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5727. this.state.hasFlowComment = false;
  5728. this.state.pos += 2;
  5729. this.nextToken();
  5730. return;
  5731. }
  5732. super.readToken_mult_modulo(code);
  5733. }
  5734. readToken_pipe_amp(code) {
  5735. const next = this.input.charCodeAt(this.state.pos + 1);
  5736. if (code === 124 && next === 125) {
  5737. this.finishOp(9, 2);
  5738. return;
  5739. }
  5740. super.readToken_pipe_amp(code);
  5741. }
  5742. parseTopLevel(file, program) {
  5743. const fileNode = super.parseTopLevel(file, program);
  5744. if (this.state.hasFlowComment) {
  5745. this.raise(FlowErrors.UnterminatedFlowComment, {
  5746. at: this.state.curPosition()
  5747. });
  5748. }
  5749. return fileNode;
  5750. }
  5751. skipBlockComment() {
  5752. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5753. if (this.state.hasFlowComment) {
  5754. throw this.raise(FlowErrors.NestedFlowComment, {
  5755. at: this.state.startLoc
  5756. });
  5757. }
  5758. this.hasFlowCommentCompletion();
  5759. this.state.pos += this.skipFlowComment();
  5760. this.state.hasFlowComment = true;
  5761. return;
  5762. }
  5763. if (this.state.hasFlowComment) {
  5764. const end = this.input.indexOf("*-/", this.state.pos + 2);
  5765. if (end === -1) {
  5766. throw this.raise(Errors.UnterminatedComment, {
  5767. at: this.state.curPosition()
  5768. });
  5769. }
  5770. this.state.pos = end + 2 + 3;
  5771. return;
  5772. }
  5773. return super.skipBlockComment();
  5774. }
  5775. skipFlowComment() {
  5776. const {
  5777. pos
  5778. } = this.state;
  5779. let shiftToFirstNonWhiteSpace = 2;
  5780. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5781. shiftToFirstNonWhiteSpace++;
  5782. }
  5783. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5784. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5785. if (ch2 === 58 && ch3 === 58) {
  5786. return shiftToFirstNonWhiteSpace + 2;
  5787. }
  5788. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5789. return shiftToFirstNonWhiteSpace + 12;
  5790. }
  5791. if (ch2 === 58 && ch3 !== 58) {
  5792. return shiftToFirstNonWhiteSpace;
  5793. }
  5794. return false;
  5795. }
  5796. hasFlowCommentCompletion() {
  5797. const end = this.input.indexOf("*/", this.state.pos);
  5798. if (end === -1) {
  5799. throw this.raise(Errors.UnterminatedComment, {
  5800. at: this.state.curPosition()
  5801. });
  5802. }
  5803. }
  5804. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5805. enumName,
  5806. memberName
  5807. }) {
  5808. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5809. at: loc,
  5810. memberName,
  5811. enumName
  5812. });
  5813. }
  5814. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5815. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  5816. at: loc
  5817. }, enumContext));
  5818. }
  5819. flowEnumErrorNumberMemberNotInitialized(loc, {
  5820. enumName,
  5821. memberName
  5822. }) {
  5823. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  5824. at: loc,
  5825. enumName,
  5826. memberName
  5827. });
  5828. }
  5829. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  5830. enumName
  5831. }) {
  5832. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  5833. at: node,
  5834. enumName
  5835. });
  5836. }
  5837. flowEnumMemberInit() {
  5838. const startLoc = this.state.startLoc;
  5839. const endOfInit = () => this.match(12) || this.match(8);
  5840. switch (this.state.type) {
  5841. case 130:
  5842. {
  5843. const literal = this.parseNumericLiteral(this.state.value);
  5844. if (endOfInit()) {
  5845. return {
  5846. type: "number",
  5847. loc: literal.loc.start,
  5848. value: literal
  5849. };
  5850. }
  5851. return {
  5852. type: "invalid",
  5853. loc: startLoc
  5854. };
  5855. }
  5856. case 129:
  5857. {
  5858. const literal = this.parseStringLiteral(this.state.value);
  5859. if (endOfInit()) {
  5860. return {
  5861. type: "string",
  5862. loc: literal.loc.start,
  5863. value: literal
  5864. };
  5865. }
  5866. return {
  5867. type: "invalid",
  5868. loc: startLoc
  5869. };
  5870. }
  5871. case 85:
  5872. case 86:
  5873. {
  5874. const literal = this.parseBooleanLiteral(this.match(85));
  5875. if (endOfInit()) {
  5876. return {
  5877. type: "boolean",
  5878. loc: literal.loc.start,
  5879. value: literal
  5880. };
  5881. }
  5882. return {
  5883. type: "invalid",
  5884. loc: startLoc
  5885. };
  5886. }
  5887. default:
  5888. return {
  5889. type: "invalid",
  5890. loc: startLoc
  5891. };
  5892. }
  5893. }
  5894. flowEnumMemberRaw() {
  5895. const loc = this.state.startLoc;
  5896. const id = this.parseIdentifier(true);
  5897. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  5898. type: "none",
  5899. loc
  5900. };
  5901. return {
  5902. id,
  5903. init
  5904. };
  5905. }
  5906. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  5907. const {
  5908. explicitType
  5909. } = context;
  5910. if (explicitType === null) {
  5911. return;
  5912. }
  5913. if (explicitType !== expectedType) {
  5914. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  5915. }
  5916. }
  5917. flowEnumMembers({
  5918. enumName,
  5919. explicitType
  5920. }) {
  5921. const seenNames = new Set();
  5922. const members = {
  5923. booleanMembers: [],
  5924. numberMembers: [],
  5925. stringMembers: [],
  5926. defaultedMembers: []
  5927. };
  5928. let hasUnknownMembers = false;
  5929. while (!this.match(8)) {
  5930. if (this.eat(21)) {
  5931. hasUnknownMembers = true;
  5932. break;
  5933. }
  5934. const memberNode = this.startNode();
  5935. const {
  5936. id,
  5937. init
  5938. } = this.flowEnumMemberRaw();
  5939. const memberName = id.name;
  5940. if (memberName === "") {
  5941. continue;
  5942. }
  5943. if (/^[a-z]/.test(memberName)) {
  5944. this.raise(FlowErrors.EnumInvalidMemberName, {
  5945. at: id,
  5946. memberName,
  5947. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  5948. enumName
  5949. });
  5950. }
  5951. if (seenNames.has(memberName)) {
  5952. this.raise(FlowErrors.EnumDuplicateMemberName, {
  5953. at: id,
  5954. memberName,
  5955. enumName
  5956. });
  5957. }
  5958. seenNames.add(memberName);
  5959. const context = {
  5960. enumName,
  5961. explicitType,
  5962. memberName
  5963. };
  5964. memberNode.id = id;
  5965. switch (init.type) {
  5966. case "boolean":
  5967. {
  5968. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  5969. memberNode.init = init.value;
  5970. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  5971. break;
  5972. }
  5973. case "number":
  5974. {
  5975. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  5976. memberNode.init = init.value;
  5977. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  5978. break;
  5979. }
  5980. case "string":
  5981. {
  5982. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  5983. memberNode.init = init.value;
  5984. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  5985. break;
  5986. }
  5987. case "invalid":
  5988. {
  5989. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  5990. }
  5991. case "none":
  5992. {
  5993. switch (explicitType) {
  5994. case "boolean":
  5995. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  5996. break;
  5997. case "number":
  5998. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  5999. break;
  6000. default:
  6001. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6002. }
  6003. }
  6004. }
  6005. if (!this.match(8)) {
  6006. this.expect(12);
  6007. }
  6008. }
  6009. return {
  6010. members,
  6011. hasUnknownMembers
  6012. };
  6013. }
  6014. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6015. enumName
  6016. }) {
  6017. if (initializedMembers.length === 0) {
  6018. return defaultedMembers;
  6019. } else if (defaultedMembers.length === 0) {
  6020. return initializedMembers;
  6021. } else if (defaultedMembers.length > initializedMembers.length) {
  6022. for (const member of initializedMembers) {
  6023. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6024. enumName
  6025. });
  6026. }
  6027. return defaultedMembers;
  6028. } else {
  6029. for (const member of defaultedMembers) {
  6030. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6031. enumName
  6032. });
  6033. }
  6034. return initializedMembers;
  6035. }
  6036. }
  6037. flowEnumParseExplicitType({
  6038. enumName
  6039. }) {
  6040. if (!this.eatContextual(101)) return null;
  6041. if (!tokenIsIdentifier(this.state.type)) {
  6042. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6043. at: this.state.startLoc,
  6044. enumName
  6045. });
  6046. }
  6047. const {
  6048. value
  6049. } = this.state;
  6050. this.next();
  6051. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6052. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6053. at: this.state.startLoc,
  6054. enumName,
  6055. invalidEnumType: value
  6056. });
  6057. }
  6058. return value;
  6059. }
  6060. flowEnumBody(node, id) {
  6061. const enumName = id.name;
  6062. const nameLoc = id.loc.start;
  6063. const explicitType = this.flowEnumParseExplicitType({
  6064. enumName
  6065. });
  6066. this.expect(5);
  6067. const {
  6068. members,
  6069. hasUnknownMembers
  6070. } = this.flowEnumMembers({
  6071. enumName,
  6072. explicitType
  6073. });
  6074. node.hasUnknownMembers = hasUnknownMembers;
  6075. switch (explicitType) {
  6076. case "boolean":
  6077. node.explicitType = true;
  6078. node.members = members.booleanMembers;
  6079. this.expect(8);
  6080. return this.finishNode(node, "EnumBooleanBody");
  6081. case "number":
  6082. node.explicitType = true;
  6083. node.members = members.numberMembers;
  6084. this.expect(8);
  6085. return this.finishNode(node, "EnumNumberBody");
  6086. case "string":
  6087. node.explicitType = true;
  6088. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6089. enumName
  6090. });
  6091. this.expect(8);
  6092. return this.finishNode(node, "EnumStringBody");
  6093. case "symbol":
  6094. node.members = members.defaultedMembers;
  6095. this.expect(8);
  6096. return this.finishNode(node, "EnumSymbolBody");
  6097. default:
  6098. {
  6099. const empty = () => {
  6100. node.members = [];
  6101. this.expect(8);
  6102. return this.finishNode(node, "EnumStringBody");
  6103. };
  6104. node.explicitType = false;
  6105. const boolsLen = members.booleanMembers.length;
  6106. const numsLen = members.numberMembers.length;
  6107. const strsLen = members.stringMembers.length;
  6108. const defaultedLen = members.defaultedMembers.length;
  6109. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6110. return empty();
  6111. } else if (!boolsLen && !numsLen) {
  6112. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6113. enumName
  6114. });
  6115. this.expect(8);
  6116. return this.finishNode(node, "EnumStringBody");
  6117. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6118. for (const member of members.defaultedMembers) {
  6119. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6120. enumName,
  6121. memberName: member.id.name
  6122. });
  6123. }
  6124. node.members = members.booleanMembers;
  6125. this.expect(8);
  6126. return this.finishNode(node, "EnumBooleanBody");
  6127. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6128. for (const member of members.defaultedMembers) {
  6129. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6130. enumName,
  6131. memberName: member.id.name
  6132. });
  6133. }
  6134. node.members = members.numberMembers;
  6135. this.expect(8);
  6136. return this.finishNode(node, "EnumNumberBody");
  6137. } else {
  6138. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6139. at: nameLoc,
  6140. enumName
  6141. });
  6142. return empty();
  6143. }
  6144. }
  6145. }
  6146. }
  6147. flowParseEnumDeclaration(node) {
  6148. const id = this.parseIdentifier();
  6149. node.id = id;
  6150. node.body = this.flowEnumBody(this.startNode(), id);
  6151. return this.finishNode(node, "EnumDeclaration");
  6152. }
  6153. isLookaheadToken_lt() {
  6154. const next = this.nextTokenStart();
  6155. if (this.input.charCodeAt(next) === 60) {
  6156. const afterNext = this.input.charCodeAt(next + 1);
  6157. return afterNext !== 60 && afterNext !== 61;
  6158. }
  6159. return false;
  6160. }
  6161. maybeUnwrapTypeCastExpression(node) {
  6162. return node.type === "TypeCastExpression" ? node.expression : node;
  6163. }
  6164. });
  6165. const entities = {
  6166. __proto__: null,
  6167. quot: "\u0022",
  6168. amp: "&",
  6169. apos: "\u0027",
  6170. lt: "<",
  6171. gt: ">",
  6172. nbsp: "\u00A0",
  6173. iexcl: "\u00A1",
  6174. cent: "\u00A2",
  6175. pound: "\u00A3",
  6176. curren: "\u00A4",
  6177. yen: "\u00A5",
  6178. brvbar: "\u00A6",
  6179. sect: "\u00A7",
  6180. uml: "\u00A8",
  6181. copy: "\u00A9",
  6182. ordf: "\u00AA",
  6183. laquo: "\u00AB",
  6184. not: "\u00AC",
  6185. shy: "\u00AD",
  6186. reg: "\u00AE",
  6187. macr: "\u00AF",
  6188. deg: "\u00B0",
  6189. plusmn: "\u00B1",
  6190. sup2: "\u00B2",
  6191. sup3: "\u00B3",
  6192. acute: "\u00B4",
  6193. micro: "\u00B5",
  6194. para: "\u00B6",
  6195. middot: "\u00B7",
  6196. cedil: "\u00B8",
  6197. sup1: "\u00B9",
  6198. ordm: "\u00BA",
  6199. raquo: "\u00BB",
  6200. frac14: "\u00BC",
  6201. frac12: "\u00BD",
  6202. frac34: "\u00BE",
  6203. iquest: "\u00BF",
  6204. Agrave: "\u00C0",
  6205. Aacute: "\u00C1",
  6206. Acirc: "\u00C2",
  6207. Atilde: "\u00C3",
  6208. Auml: "\u00C4",
  6209. Aring: "\u00C5",
  6210. AElig: "\u00C6",
  6211. Ccedil: "\u00C7",
  6212. Egrave: "\u00C8",
  6213. Eacute: "\u00C9",
  6214. Ecirc: "\u00CA",
  6215. Euml: "\u00CB",
  6216. Igrave: "\u00CC",
  6217. Iacute: "\u00CD",
  6218. Icirc: "\u00CE",
  6219. Iuml: "\u00CF",
  6220. ETH: "\u00D0",
  6221. Ntilde: "\u00D1",
  6222. Ograve: "\u00D2",
  6223. Oacute: "\u00D3",
  6224. Ocirc: "\u00D4",
  6225. Otilde: "\u00D5",
  6226. Ouml: "\u00D6",
  6227. times: "\u00D7",
  6228. Oslash: "\u00D8",
  6229. Ugrave: "\u00D9",
  6230. Uacute: "\u00DA",
  6231. Ucirc: "\u00DB",
  6232. Uuml: "\u00DC",
  6233. Yacute: "\u00DD",
  6234. THORN: "\u00DE",
  6235. szlig: "\u00DF",
  6236. agrave: "\u00E0",
  6237. aacute: "\u00E1",
  6238. acirc: "\u00E2",
  6239. atilde: "\u00E3",
  6240. auml: "\u00E4",
  6241. aring: "\u00E5",
  6242. aelig: "\u00E6",
  6243. ccedil: "\u00E7",
  6244. egrave: "\u00E8",
  6245. eacute: "\u00E9",
  6246. ecirc: "\u00EA",
  6247. euml: "\u00EB",
  6248. igrave: "\u00EC",
  6249. iacute: "\u00ED",
  6250. icirc: "\u00EE",
  6251. iuml: "\u00EF",
  6252. eth: "\u00F0",
  6253. ntilde: "\u00F1",
  6254. ograve: "\u00F2",
  6255. oacute: "\u00F3",
  6256. ocirc: "\u00F4",
  6257. otilde: "\u00F5",
  6258. ouml: "\u00F6",
  6259. divide: "\u00F7",
  6260. oslash: "\u00F8",
  6261. ugrave: "\u00F9",
  6262. uacute: "\u00FA",
  6263. ucirc: "\u00FB",
  6264. uuml: "\u00FC",
  6265. yacute: "\u00FD",
  6266. thorn: "\u00FE",
  6267. yuml: "\u00FF",
  6268. OElig: "\u0152",
  6269. oelig: "\u0153",
  6270. Scaron: "\u0160",
  6271. scaron: "\u0161",
  6272. Yuml: "\u0178",
  6273. fnof: "\u0192",
  6274. circ: "\u02C6",
  6275. tilde: "\u02DC",
  6276. Alpha: "\u0391",
  6277. Beta: "\u0392",
  6278. Gamma: "\u0393",
  6279. Delta: "\u0394",
  6280. Epsilon: "\u0395",
  6281. Zeta: "\u0396",
  6282. Eta: "\u0397",
  6283. Theta: "\u0398",
  6284. Iota: "\u0399",
  6285. Kappa: "\u039A",
  6286. Lambda: "\u039B",
  6287. Mu: "\u039C",
  6288. Nu: "\u039D",
  6289. Xi: "\u039E",
  6290. Omicron: "\u039F",
  6291. Pi: "\u03A0",
  6292. Rho: "\u03A1",
  6293. Sigma: "\u03A3",
  6294. Tau: "\u03A4",
  6295. Upsilon: "\u03A5",
  6296. Phi: "\u03A6",
  6297. Chi: "\u03A7",
  6298. Psi: "\u03A8",
  6299. Omega: "\u03A9",
  6300. alpha: "\u03B1",
  6301. beta: "\u03B2",
  6302. gamma: "\u03B3",
  6303. delta: "\u03B4",
  6304. epsilon: "\u03B5",
  6305. zeta: "\u03B6",
  6306. eta: "\u03B7",
  6307. theta: "\u03B8",
  6308. iota: "\u03B9",
  6309. kappa: "\u03BA",
  6310. lambda: "\u03BB",
  6311. mu: "\u03BC",
  6312. nu: "\u03BD",
  6313. xi: "\u03BE",
  6314. omicron: "\u03BF",
  6315. pi: "\u03C0",
  6316. rho: "\u03C1",
  6317. sigmaf: "\u03C2",
  6318. sigma: "\u03C3",
  6319. tau: "\u03C4",
  6320. upsilon: "\u03C5",
  6321. phi: "\u03C6",
  6322. chi: "\u03C7",
  6323. psi: "\u03C8",
  6324. omega: "\u03C9",
  6325. thetasym: "\u03D1",
  6326. upsih: "\u03D2",
  6327. piv: "\u03D6",
  6328. ensp: "\u2002",
  6329. emsp: "\u2003",
  6330. thinsp: "\u2009",
  6331. zwnj: "\u200C",
  6332. zwj: "\u200D",
  6333. lrm: "\u200E",
  6334. rlm: "\u200F",
  6335. ndash: "\u2013",
  6336. mdash: "\u2014",
  6337. lsquo: "\u2018",
  6338. rsquo: "\u2019",
  6339. sbquo: "\u201A",
  6340. ldquo: "\u201C",
  6341. rdquo: "\u201D",
  6342. bdquo: "\u201E",
  6343. dagger: "\u2020",
  6344. Dagger: "\u2021",
  6345. bull: "\u2022",
  6346. hellip: "\u2026",
  6347. permil: "\u2030",
  6348. prime: "\u2032",
  6349. Prime: "\u2033",
  6350. lsaquo: "\u2039",
  6351. rsaquo: "\u203A",
  6352. oline: "\u203E",
  6353. frasl: "\u2044",
  6354. euro: "\u20AC",
  6355. image: "\u2111",
  6356. weierp: "\u2118",
  6357. real: "\u211C",
  6358. trade: "\u2122",
  6359. alefsym: "\u2135",
  6360. larr: "\u2190",
  6361. uarr: "\u2191",
  6362. rarr: "\u2192",
  6363. darr: "\u2193",
  6364. harr: "\u2194",
  6365. crarr: "\u21B5",
  6366. lArr: "\u21D0",
  6367. uArr: "\u21D1",
  6368. rArr: "\u21D2",
  6369. dArr: "\u21D3",
  6370. hArr: "\u21D4",
  6371. forall: "\u2200",
  6372. part: "\u2202",
  6373. exist: "\u2203",
  6374. empty: "\u2205",
  6375. nabla: "\u2207",
  6376. isin: "\u2208",
  6377. notin: "\u2209",
  6378. ni: "\u220B",
  6379. prod: "\u220F",
  6380. sum: "\u2211",
  6381. minus: "\u2212",
  6382. lowast: "\u2217",
  6383. radic: "\u221A",
  6384. prop: "\u221D",
  6385. infin: "\u221E",
  6386. ang: "\u2220",
  6387. and: "\u2227",
  6388. or: "\u2228",
  6389. cap: "\u2229",
  6390. cup: "\u222A",
  6391. int: "\u222B",
  6392. there4: "\u2234",
  6393. sim: "\u223C",
  6394. cong: "\u2245",
  6395. asymp: "\u2248",
  6396. ne: "\u2260",
  6397. equiv: "\u2261",
  6398. le: "\u2264",
  6399. ge: "\u2265",
  6400. sub: "\u2282",
  6401. sup: "\u2283",
  6402. nsub: "\u2284",
  6403. sube: "\u2286",
  6404. supe: "\u2287",
  6405. oplus: "\u2295",
  6406. otimes: "\u2297",
  6407. perp: "\u22A5",
  6408. sdot: "\u22C5",
  6409. lceil: "\u2308",
  6410. rceil: "\u2309",
  6411. lfloor: "\u230A",
  6412. rfloor: "\u230B",
  6413. lang: "\u2329",
  6414. rang: "\u232A",
  6415. loz: "\u25CA",
  6416. spades: "\u2660",
  6417. clubs: "\u2663",
  6418. hearts: "\u2665",
  6419. diams: "\u2666"
  6420. };
  6421. const JsxErrors = ParseErrorEnum`jsx`(_ => ({
  6422. AttributeIsEmpty: _("JSX attributes must only be assigned a non-empty expression."),
  6423. MissingClosingTagElement: _(({
  6424. openingTagName
  6425. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`),
  6426. MissingClosingTagFragment: _("Expected corresponding JSX closing tag for <>."),
  6427. UnexpectedSequenceExpression: _("Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?"),
  6428. UnexpectedToken: _(({
  6429. unexpected,
  6430. HTMLEntity
  6431. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`),
  6432. UnsupportedJsxValue: _("JSX value should be either an expression or a quoted JSX text."),
  6433. UnterminatedJsxContent: _("Unterminated JSX contents."),
  6434. UnwrappedAdjacentJSXElements: _("Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?")
  6435. }));
  6436. function isFragment(object) {
  6437. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6438. }
  6439. function getQualifiedJSXName(object) {
  6440. if (object.type === "JSXIdentifier") {
  6441. return object.name;
  6442. }
  6443. if (object.type === "JSXNamespacedName") {
  6444. return object.namespace.name + ":" + object.name.name;
  6445. }
  6446. if (object.type === "JSXMemberExpression") {
  6447. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6448. }
  6449. throw new Error("Node had unexpected type: " + object.type);
  6450. }
  6451. var jsx = (superClass => class extends superClass {
  6452. jsxReadToken() {
  6453. let out = "";
  6454. let chunkStart = this.state.pos;
  6455. for (;;) {
  6456. if (this.state.pos >= this.length) {
  6457. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6458. at: this.state.startLoc
  6459. });
  6460. }
  6461. const ch = this.input.charCodeAt(this.state.pos);
  6462. switch (ch) {
  6463. case 60:
  6464. case 123:
  6465. if (this.state.pos === this.state.start) {
  6466. if (ch === 60 && this.state.canStartJSXElement) {
  6467. ++this.state.pos;
  6468. return this.finishToken(138);
  6469. }
  6470. return super.getTokenFromCode(ch);
  6471. }
  6472. out += this.input.slice(chunkStart, this.state.pos);
  6473. return this.finishToken(137, out);
  6474. case 38:
  6475. out += this.input.slice(chunkStart, this.state.pos);
  6476. out += this.jsxReadEntity();
  6477. chunkStart = this.state.pos;
  6478. break;
  6479. case 62:
  6480. case 125:
  6481. default:
  6482. if (isNewLine(ch)) {
  6483. out += this.input.slice(chunkStart, this.state.pos);
  6484. out += this.jsxReadNewLine(true);
  6485. chunkStart = this.state.pos;
  6486. } else {
  6487. ++this.state.pos;
  6488. }
  6489. }
  6490. }
  6491. }
  6492. jsxReadNewLine(normalizeCRLF) {
  6493. const ch = this.input.charCodeAt(this.state.pos);
  6494. let out;
  6495. ++this.state.pos;
  6496. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6497. ++this.state.pos;
  6498. out = normalizeCRLF ? "\n" : "\r\n";
  6499. } else {
  6500. out = String.fromCharCode(ch);
  6501. }
  6502. ++this.state.curLine;
  6503. this.state.lineStart = this.state.pos;
  6504. return out;
  6505. }
  6506. jsxReadString(quote) {
  6507. let out = "";
  6508. let chunkStart = ++this.state.pos;
  6509. for (;;) {
  6510. if (this.state.pos >= this.length) {
  6511. throw this.raise(Errors.UnterminatedString, {
  6512. at: this.state.startLoc
  6513. });
  6514. }
  6515. const ch = this.input.charCodeAt(this.state.pos);
  6516. if (ch === quote) break;
  6517. if (ch === 38) {
  6518. out += this.input.slice(chunkStart, this.state.pos);
  6519. out += this.jsxReadEntity();
  6520. chunkStart = this.state.pos;
  6521. } else if (isNewLine(ch)) {
  6522. out += this.input.slice(chunkStart, this.state.pos);
  6523. out += this.jsxReadNewLine(false);
  6524. chunkStart = this.state.pos;
  6525. } else {
  6526. ++this.state.pos;
  6527. }
  6528. }
  6529. out += this.input.slice(chunkStart, this.state.pos++);
  6530. return this.finishToken(129, out);
  6531. }
  6532. jsxReadEntity() {
  6533. const startPos = ++this.state.pos;
  6534. if (this.codePointAtPos(this.state.pos) === 35) {
  6535. ++this.state.pos;
  6536. let radix = 10;
  6537. if (this.codePointAtPos(this.state.pos) === 120) {
  6538. radix = 16;
  6539. ++this.state.pos;
  6540. }
  6541. const codePoint = this.readInt(radix, undefined, false, "bail");
  6542. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6543. ++this.state.pos;
  6544. return String.fromCodePoint(codePoint);
  6545. }
  6546. } else {
  6547. let count = 0;
  6548. let semi = false;
  6549. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6550. ++this.state.pos;
  6551. }
  6552. if (semi) {
  6553. const desc = this.input.slice(startPos, this.state.pos);
  6554. const entity = entities[desc];
  6555. ++this.state.pos;
  6556. if (entity) {
  6557. return entity;
  6558. }
  6559. }
  6560. }
  6561. this.state.pos = startPos;
  6562. return "&";
  6563. }
  6564. jsxReadWord() {
  6565. let ch;
  6566. const start = this.state.pos;
  6567. do {
  6568. ch = this.input.charCodeAt(++this.state.pos);
  6569. } while (isIdentifierChar(ch) || ch === 45);
  6570. return this.finishToken(136, this.input.slice(start, this.state.pos));
  6571. }
  6572. jsxParseIdentifier() {
  6573. const node = this.startNode();
  6574. if (this.match(136)) {
  6575. node.name = this.state.value;
  6576. } else if (tokenIsKeyword(this.state.type)) {
  6577. node.name = tokenLabelName(this.state.type);
  6578. } else {
  6579. this.unexpected();
  6580. }
  6581. this.next();
  6582. return this.finishNode(node, "JSXIdentifier");
  6583. }
  6584. jsxParseNamespacedName() {
  6585. const startPos = this.state.start;
  6586. const startLoc = this.state.startLoc;
  6587. const name = this.jsxParseIdentifier();
  6588. if (!this.eat(14)) return name;
  6589. const node = this.startNodeAt(startPos, startLoc);
  6590. node.namespace = name;
  6591. node.name = this.jsxParseIdentifier();
  6592. return this.finishNode(node, "JSXNamespacedName");
  6593. }
  6594. jsxParseElementName() {
  6595. const startPos = this.state.start;
  6596. const startLoc = this.state.startLoc;
  6597. let node = this.jsxParseNamespacedName();
  6598. if (node.type === "JSXNamespacedName") {
  6599. return node;
  6600. }
  6601. while (this.eat(16)) {
  6602. const newNode = this.startNodeAt(startPos, startLoc);
  6603. newNode.object = node;
  6604. newNode.property = this.jsxParseIdentifier();
  6605. node = this.finishNode(newNode, "JSXMemberExpression");
  6606. }
  6607. return node;
  6608. }
  6609. jsxParseAttributeValue() {
  6610. let node;
  6611. switch (this.state.type) {
  6612. case 5:
  6613. node = this.startNode();
  6614. this.setContext(types.brace);
  6615. this.next();
  6616. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6617. if (node.expression.type === "JSXEmptyExpression") {
  6618. this.raise(JsxErrors.AttributeIsEmpty, {
  6619. at: node
  6620. });
  6621. }
  6622. return node;
  6623. case 138:
  6624. case 129:
  6625. return this.parseExprAtom();
  6626. default:
  6627. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6628. at: this.state.startLoc
  6629. });
  6630. }
  6631. }
  6632. jsxParseEmptyExpression() {
  6633. const node = this.startNodeAt(this.state.lastTokEndLoc.index, this.state.lastTokEndLoc);
  6634. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6635. }
  6636. jsxParseSpreadChild(node) {
  6637. this.next();
  6638. node.expression = this.parseExpression();
  6639. this.setContext(types.j_oTag);
  6640. this.expect(8);
  6641. return this.finishNode(node, "JSXSpreadChild");
  6642. }
  6643. jsxParseExpressionContainer(node, previousContext) {
  6644. if (this.match(8)) {
  6645. node.expression = this.jsxParseEmptyExpression();
  6646. } else {
  6647. const expression = this.parseExpression();
  6648. node.expression = expression;
  6649. }
  6650. this.setContext(previousContext);
  6651. this.expect(8);
  6652. return this.finishNode(node, "JSXExpressionContainer");
  6653. }
  6654. jsxParseAttribute() {
  6655. const node = this.startNode();
  6656. if (this.match(5)) {
  6657. this.setContext(types.brace);
  6658. this.next();
  6659. this.expect(21);
  6660. node.argument = this.parseMaybeAssignAllowIn();
  6661. this.setContext(types.j_oTag);
  6662. this.expect(8);
  6663. return this.finishNode(node, "JSXSpreadAttribute");
  6664. }
  6665. node.name = this.jsxParseNamespacedName();
  6666. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6667. return this.finishNode(node, "JSXAttribute");
  6668. }
  6669. jsxParseOpeningElementAt(startPos, startLoc) {
  6670. const node = this.startNodeAt(startPos, startLoc);
  6671. if (this.match(139)) {
  6672. this.expect(139);
  6673. return this.finishNode(node, "JSXOpeningFragment");
  6674. }
  6675. node.name = this.jsxParseElementName();
  6676. return this.jsxParseOpeningElementAfterName(node);
  6677. }
  6678. jsxParseOpeningElementAfterName(node) {
  6679. const attributes = [];
  6680. while (!this.match(56) && !this.match(139)) {
  6681. attributes.push(this.jsxParseAttribute());
  6682. }
  6683. node.attributes = attributes;
  6684. node.selfClosing = this.eat(56);
  6685. this.expect(139);
  6686. return this.finishNode(node, "JSXOpeningElement");
  6687. }
  6688. jsxParseClosingElementAt(startPos, startLoc) {
  6689. const node = this.startNodeAt(startPos, startLoc);
  6690. if (this.match(139)) {
  6691. this.expect(139);
  6692. return this.finishNode(node, "JSXClosingFragment");
  6693. }
  6694. node.name = this.jsxParseElementName();
  6695. this.expect(139);
  6696. return this.finishNode(node, "JSXClosingElement");
  6697. }
  6698. jsxParseElementAt(startPos, startLoc) {
  6699. const node = this.startNodeAt(startPos, startLoc);
  6700. const children = [];
  6701. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  6702. let closingElement = null;
  6703. if (!openingElement.selfClosing) {
  6704. contents: for (;;) {
  6705. switch (this.state.type) {
  6706. case 138:
  6707. startPos = this.state.start;
  6708. startLoc = this.state.startLoc;
  6709. this.next();
  6710. if (this.eat(56)) {
  6711. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  6712. break contents;
  6713. }
  6714. children.push(this.jsxParseElementAt(startPos, startLoc));
  6715. break;
  6716. case 137:
  6717. children.push(this.parseExprAtom());
  6718. break;
  6719. case 5:
  6720. {
  6721. const node = this.startNode();
  6722. this.setContext(types.brace);
  6723. this.next();
  6724. if (this.match(21)) {
  6725. children.push(this.jsxParseSpreadChild(node));
  6726. } else {
  6727. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6728. }
  6729. break;
  6730. }
  6731. default:
  6732. throw this.unexpected();
  6733. }
  6734. }
  6735. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6736. this.raise(JsxErrors.MissingClosingTagFragment, {
  6737. at: closingElement
  6738. });
  6739. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6740. this.raise(JsxErrors.MissingClosingTagElement, {
  6741. at: closingElement,
  6742. openingTagName: getQualifiedJSXName(openingElement.name)
  6743. });
  6744. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6745. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6746. this.raise(JsxErrors.MissingClosingTagElement, {
  6747. at: closingElement,
  6748. openingTagName: getQualifiedJSXName(openingElement.name)
  6749. });
  6750. }
  6751. }
  6752. }
  6753. if (isFragment(openingElement)) {
  6754. node.openingFragment = openingElement;
  6755. node.closingFragment = closingElement;
  6756. } else {
  6757. node.openingElement = openingElement;
  6758. node.closingElement = closingElement;
  6759. }
  6760. node.children = children;
  6761. if (this.match(47)) {
  6762. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6763. at: this.state.startLoc
  6764. });
  6765. }
  6766. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6767. }
  6768. jsxParseElement() {
  6769. const startPos = this.state.start;
  6770. const startLoc = this.state.startLoc;
  6771. this.next();
  6772. return this.jsxParseElementAt(startPos, startLoc);
  6773. }
  6774. setContext(newContext) {
  6775. const {
  6776. context
  6777. } = this.state;
  6778. context[context.length - 1] = newContext;
  6779. }
  6780. parseExprAtom(refExpressionErrors) {
  6781. if (this.match(137)) {
  6782. return this.parseLiteral(this.state.value, "JSXText");
  6783. } else if (this.match(138)) {
  6784. return this.jsxParseElement();
  6785. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6786. this.replaceToken(138);
  6787. return this.jsxParseElement();
  6788. } else {
  6789. return super.parseExprAtom(refExpressionErrors);
  6790. }
  6791. }
  6792. skipSpace() {
  6793. const curContext = this.curContext();
  6794. if (!curContext.preserveSpace) super.skipSpace();
  6795. }
  6796. getTokenFromCode(code) {
  6797. const context = this.curContext();
  6798. if (context === types.j_expr) {
  6799. return this.jsxReadToken();
  6800. }
  6801. if (context === types.j_oTag || context === types.j_cTag) {
  6802. if (isIdentifierStart(code)) {
  6803. return this.jsxReadWord();
  6804. }
  6805. if (code === 62) {
  6806. ++this.state.pos;
  6807. return this.finishToken(139);
  6808. }
  6809. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6810. return this.jsxReadString(code);
  6811. }
  6812. }
  6813. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6814. ++this.state.pos;
  6815. return this.finishToken(138);
  6816. }
  6817. return super.getTokenFromCode(code);
  6818. }
  6819. updateContext(prevType) {
  6820. const {
  6821. context,
  6822. type
  6823. } = this.state;
  6824. if (type === 56 && prevType === 138) {
  6825. context.splice(-2, 2, types.j_cTag);
  6826. this.state.canStartJSXElement = false;
  6827. } else if (type === 138) {
  6828. context.push(types.j_oTag);
  6829. } else if (type === 139) {
  6830. const out = context[context.length - 1];
  6831. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  6832. context.pop();
  6833. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6834. } else {
  6835. this.setContext(types.j_expr);
  6836. this.state.canStartJSXElement = true;
  6837. }
  6838. } else {
  6839. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6840. }
  6841. }
  6842. });
  6843. class TypeScriptScope extends Scope {
  6844. constructor(...args) {
  6845. super(...args);
  6846. this.types = new Set();
  6847. this.enums = new Set();
  6848. this.constEnums = new Set();
  6849. this.classes = new Set();
  6850. this.exportOnlyBindings = new Set();
  6851. }
  6852. }
  6853. class TypeScriptScopeHandler extends ScopeHandler {
  6854. createScope(flags) {
  6855. return new TypeScriptScope(flags);
  6856. }
  6857. declareName(name, bindingType, loc) {
  6858. const scope = this.currentScope();
  6859. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  6860. this.maybeExportDefined(scope, name);
  6861. scope.exportOnlyBindings.add(name);
  6862. return;
  6863. }
  6864. super.declareName(...arguments);
  6865. if (bindingType & BIND_KIND_TYPE) {
  6866. if (!(bindingType & BIND_KIND_VALUE)) {
  6867. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  6868. this.maybeExportDefined(scope, name);
  6869. }
  6870. scope.types.add(name);
  6871. }
  6872. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  6873. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  6874. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  6875. }
  6876. isRedeclaredInScope(scope, name, bindingType) {
  6877. if (scope.enums.has(name)) {
  6878. if (bindingType & BIND_FLAGS_TS_ENUM) {
  6879. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  6880. const wasConst = scope.constEnums.has(name);
  6881. return isConst !== wasConst;
  6882. }
  6883. return true;
  6884. }
  6885. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  6886. if (scope.lexical.has(name)) {
  6887. return !!(bindingType & BIND_KIND_VALUE);
  6888. } else {
  6889. return false;
  6890. }
  6891. }
  6892. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  6893. return true;
  6894. }
  6895. return super.isRedeclaredInScope(...arguments);
  6896. }
  6897. checkLocalExport(id) {
  6898. const topLevelScope = this.scopeStack[0];
  6899. const {
  6900. name
  6901. } = id;
  6902. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  6903. super.checkLocalExport(id);
  6904. }
  6905. }
  6906. }
  6907. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  6908. function nonNull(x) {
  6909. if (x == null) {
  6910. throw new Error(`Unexpected ${x} value.`);
  6911. }
  6912. return x;
  6913. }
  6914. function assert(x) {
  6915. if (!x) {
  6916. throw new Error("Assert fail");
  6917. }
  6918. }
  6919. const TSErrors = ParseErrorEnum`typescript`(_ => ({
  6920. AbstractMethodHasImplementation: _(({
  6921. methodName
  6922. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`),
  6923. AbstractPropertyHasInitializer: _(({
  6924. propertyName
  6925. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`),
  6926. AccesorCannotDeclareThisParameter: _("'get' and 'set' accessors cannot declare 'this' parameters."),
  6927. AccesorCannotHaveTypeParameters: _("An accessor cannot have type parameters."),
  6928. CannotFindName: _(({
  6929. name
  6930. }) => `Cannot find name '${name}'.`),
  6931. ClassMethodHasDeclare: _("Class methods cannot have the 'declare' modifier."),
  6932. ClassMethodHasReadonly: _("Class methods cannot have the 'readonly' modifier."),
  6933. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: _("A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference."),
  6934. ConstructorHasTypeParameters: _("Type parameters cannot appear on a constructor declaration."),
  6935. DeclareAccessor: _(({
  6936. kind
  6937. }) => `'declare' is not allowed in ${kind}ters.`),
  6938. DeclareClassFieldHasInitializer: _("Initializers are not allowed in ambient contexts."),
  6939. DeclareFunctionHasImplementation: _("An implementation cannot be declared in ambient contexts."),
  6940. DuplicateAccessibilityModifier: _(({
  6941. modifier
  6942. }) => `Accessibility modifier already seen.`),
  6943. DuplicateModifier: _(({
  6944. modifier
  6945. }) => `Duplicate modifier: '${modifier}'.`),
  6946. EmptyHeritageClauseType: _(({
  6947. token
  6948. }) => `'${token}' list cannot be empty.`),
  6949. EmptyTypeArguments: _("Type argument list cannot be empty."),
  6950. EmptyTypeParameters: _("Type parameter list cannot be empty."),
  6951. ExpectedAmbientAfterExportDeclare: _("'export declare' must be followed by an ambient declaration."),
  6952. ImportAliasHasImportType: _("An import alias can not use 'import type'."),
  6953. IncompatibleModifiers: _(({
  6954. modifiers
  6955. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`),
  6956. IndexSignatureHasAbstract: _("Index signatures cannot have the 'abstract' modifier."),
  6957. IndexSignatureHasAccessibility: _(({
  6958. modifier
  6959. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`),
  6960. IndexSignatureHasDeclare: _("Index signatures cannot have the 'declare' modifier."),
  6961. IndexSignatureHasOverride: _("'override' modifier cannot appear on an index signature."),
  6962. IndexSignatureHasStatic: _("Index signatures cannot have the 'static' modifier."),
  6963. InitializerNotAllowedInAmbientContext: _("Initializers are not allowed in ambient contexts."),
  6964. InvalidModifierOnTypeMember: _(({
  6965. modifier
  6966. }) => `'${modifier}' modifier cannot appear on a type member.`),
  6967. InvalidModifiersOrder: _(({
  6968. orderedModifiers
  6969. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`),
  6970. InvalidTupleMemberLabel: _("Tuple members must be labeled with a simple identifier."),
  6971. MissingInterfaceName: _("'interface' declarations must be followed by an identifier."),
  6972. MixedLabeledAndUnlabeledElements: _("Tuple members must all have names or all not have names."),
  6973. NonAbstractClassHasAbstractMethod: _("Abstract methods can only appear within an abstract class."),
  6974. NonClassMethodPropertyHasAbstractModifer: _("'abstract' modifier can only appear on a class, method, or property declaration."),
  6975. OptionalTypeBeforeRequired: _("A required element cannot follow an optional element."),
  6976. OverrideNotInSubClass: _("This member cannot have an 'override' modifier because its containing class does not extend another class."),
  6977. PatternIsOptional: _("A binding pattern parameter cannot be optional in an implementation signature."),
  6978. PrivateElementHasAbstract: _("Private elements cannot have the 'abstract' modifier."),
  6979. PrivateElementHasAccessibility: _(({
  6980. modifier
  6981. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`),
  6982. ReadonlyForMethodSignature: _("'readonly' modifier can only appear on a property declaration or index signature."),
  6983. ReservedArrowTypeParam: _("This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`."),
  6984. ReservedTypeAssertion: _("This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead."),
  6985. SetAccesorCannotHaveOptionalParameter: _("A 'set' accessor cannot have an optional parameter."),
  6986. SetAccesorCannotHaveRestParameter: _("A 'set' accessor cannot have rest parameter."),
  6987. SetAccesorCannotHaveReturnType: _("A 'set' accessor cannot have a return type annotation."),
  6988. SingleTypeParameterWithoutTrailingComma: _(({
  6989. typeParameterName
  6990. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`),
  6991. StaticBlockCannotHaveModifier: _("Static class blocks cannot have any modifier."),
  6992. TypeAnnotationAfterAssign: _("Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`."),
  6993. TypeImportCannotSpecifyDefaultAndNamed: _("A type-only import can specify a default import or named bindings, but not both."),
  6994. TypeModifierIsUsedInTypeExports: _("The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement."),
  6995. TypeModifierIsUsedInTypeImports: _("The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement."),
  6996. UnexpectedParameterModifier: _("A parameter property is only allowed in a constructor implementation."),
  6997. UnexpectedReadonly: _("'readonly' type modifier is only permitted on array and tuple literal types."),
  6998. UnexpectedTypeAnnotation: _("Did not expect a type annotation here."),
  6999. UnexpectedTypeCastInParameter: _("Unexpected type cast in parameter position."),
  7000. UnsupportedImportTypeArgument: _("Argument in a type import must be a string literal."),
  7001. UnsupportedParameterPropertyKind: _("A parameter property may not be declared using a binding pattern."),
  7002. UnsupportedSignatureParameterKind: _(({
  7003. type
  7004. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`)
  7005. }));
  7006. function keywordTypeFromName(value) {
  7007. switch (value) {
  7008. case "any":
  7009. return "TSAnyKeyword";
  7010. case "boolean":
  7011. return "TSBooleanKeyword";
  7012. case "bigint":
  7013. return "TSBigIntKeyword";
  7014. case "never":
  7015. return "TSNeverKeyword";
  7016. case "number":
  7017. return "TSNumberKeyword";
  7018. case "object":
  7019. return "TSObjectKeyword";
  7020. case "string":
  7021. return "TSStringKeyword";
  7022. case "symbol":
  7023. return "TSSymbolKeyword";
  7024. case "undefined":
  7025. return "TSUndefinedKeyword";
  7026. case "unknown":
  7027. return "TSUnknownKeyword";
  7028. default:
  7029. return undefined;
  7030. }
  7031. }
  7032. function tsIsAccessModifier(modifier) {
  7033. return modifier === "private" || modifier === "public" || modifier === "protected";
  7034. }
  7035. var typescript = (superClass => class extends superClass {
  7036. getScopeHandler() {
  7037. return TypeScriptScopeHandler;
  7038. }
  7039. tsIsIdentifier() {
  7040. return tokenIsIdentifier(this.state.type);
  7041. }
  7042. tsTokenCanFollowModifier() {
  7043. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(134) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7044. }
  7045. tsNextTokenCanFollowModifier() {
  7046. this.next();
  7047. return this.tsTokenCanFollowModifier();
  7048. }
  7049. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7050. if (!tokenIsIdentifier(this.state.type)) {
  7051. return undefined;
  7052. }
  7053. const modifier = this.state.value;
  7054. if (allowedModifiers.indexOf(modifier) !== -1) {
  7055. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7056. return undefined;
  7057. }
  7058. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7059. return modifier;
  7060. }
  7061. }
  7062. return undefined;
  7063. }
  7064. tsParseModifiers({
  7065. modified,
  7066. allowedModifiers,
  7067. disallowedModifiers,
  7068. stopOnStartOfClassStaticBlock
  7069. }) {
  7070. const enforceOrder = (loc, modifier, before, after) => {
  7071. if (modifier === before && modified[after]) {
  7072. this.raise(TSErrors.InvalidModifiersOrder, {
  7073. at: loc,
  7074. orderedModifiers: [before, after]
  7075. });
  7076. }
  7077. };
  7078. const incompatible = (loc, modifier, mod1, mod2) => {
  7079. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7080. this.raise(TSErrors.IncompatibleModifiers, {
  7081. at: loc,
  7082. modifiers: [mod1, mod2]
  7083. });
  7084. }
  7085. };
  7086. for (;;) {
  7087. const {
  7088. startLoc
  7089. } = this.state;
  7090. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7091. if (!modifier) break;
  7092. if (tsIsAccessModifier(modifier)) {
  7093. if (modified.accessibility) {
  7094. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7095. at: startLoc,
  7096. modifier
  7097. });
  7098. } else {
  7099. enforceOrder(startLoc, modifier, modifier, "override");
  7100. enforceOrder(startLoc, modifier, modifier, "static");
  7101. enforceOrder(startLoc, modifier, modifier, "readonly");
  7102. modified.accessibility = modifier;
  7103. }
  7104. } else {
  7105. if (Object.hasOwnProperty.call(modified, modifier)) {
  7106. this.raise(TSErrors.DuplicateModifier, {
  7107. at: startLoc,
  7108. modifier
  7109. });
  7110. } else {
  7111. enforceOrder(startLoc, modifier, "static", "readonly");
  7112. enforceOrder(startLoc, modifier, "static", "override");
  7113. enforceOrder(startLoc, modifier, "override", "readonly");
  7114. enforceOrder(startLoc, modifier, "abstract", "override");
  7115. incompatible(startLoc, modifier, "declare", "override");
  7116. incompatible(startLoc, modifier, "static", "abstract");
  7117. }
  7118. modified[modifier] = true;
  7119. }
  7120. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7121. this.raise(TSErrors.InvalidModifierOnTypeMember, {
  7122. at: startLoc,
  7123. modifier
  7124. });
  7125. }
  7126. }
  7127. }
  7128. tsIsListTerminator(kind) {
  7129. switch (kind) {
  7130. case "EnumMembers":
  7131. case "TypeMembers":
  7132. return this.match(8);
  7133. case "HeritageClauseElement":
  7134. return this.match(5);
  7135. case "TupleElementTypes":
  7136. return this.match(3);
  7137. case "TypeParametersOrArguments":
  7138. return this.match(48);
  7139. }
  7140. throw new Error("Unreachable");
  7141. }
  7142. tsParseList(kind, parseElement) {
  7143. const result = [];
  7144. while (!this.tsIsListTerminator(kind)) {
  7145. result.push(parseElement());
  7146. }
  7147. return result;
  7148. }
  7149. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7150. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7151. }
  7152. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7153. const result = [];
  7154. let trailingCommaPos = -1;
  7155. for (;;) {
  7156. if (this.tsIsListTerminator(kind)) {
  7157. break;
  7158. }
  7159. trailingCommaPos = -1;
  7160. const element = parseElement();
  7161. if (element == null) {
  7162. return undefined;
  7163. }
  7164. result.push(element);
  7165. if (this.eat(12)) {
  7166. trailingCommaPos = this.state.lastTokStart;
  7167. continue;
  7168. }
  7169. if (this.tsIsListTerminator(kind)) {
  7170. break;
  7171. }
  7172. if (expectSuccess) {
  7173. this.expect(12);
  7174. }
  7175. return undefined;
  7176. }
  7177. if (refTrailingCommaPos) {
  7178. refTrailingCommaPos.value = trailingCommaPos;
  7179. }
  7180. return result;
  7181. }
  7182. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7183. if (!skipFirstToken) {
  7184. if (bracket) {
  7185. this.expect(0);
  7186. } else {
  7187. this.expect(47);
  7188. }
  7189. }
  7190. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7191. if (bracket) {
  7192. this.expect(3);
  7193. } else {
  7194. this.expect(48);
  7195. }
  7196. return result;
  7197. }
  7198. tsParseImportType() {
  7199. const node = this.startNode();
  7200. this.expect(83);
  7201. this.expect(10);
  7202. if (!this.match(129)) {
  7203. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7204. at: this.state.startLoc
  7205. });
  7206. }
  7207. node.argument = this.parseExprAtom();
  7208. this.expect(11);
  7209. if (this.eat(16)) {
  7210. node.qualifier = this.tsParseEntityName();
  7211. }
  7212. if (this.match(47)) {
  7213. node.typeParameters = this.tsParseTypeArguments();
  7214. }
  7215. return this.finishNode(node, "TSImportType");
  7216. }
  7217. tsParseEntityName(allowReservedWords = true) {
  7218. let entity = this.parseIdentifier(allowReservedWords);
  7219. while (this.eat(16)) {
  7220. const node = this.startNodeAtNode(entity);
  7221. node.left = entity;
  7222. node.right = this.parseIdentifier(allowReservedWords);
  7223. entity = this.finishNode(node, "TSQualifiedName");
  7224. }
  7225. return entity;
  7226. }
  7227. tsParseTypeReference() {
  7228. const node = this.startNode();
  7229. node.typeName = this.tsParseEntityName();
  7230. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7231. node.typeParameters = this.tsParseTypeArguments();
  7232. }
  7233. return this.finishNode(node, "TSTypeReference");
  7234. }
  7235. tsParseThisTypePredicate(lhs) {
  7236. this.next();
  7237. const node = this.startNodeAtNode(lhs);
  7238. node.parameterName = lhs;
  7239. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7240. node.asserts = false;
  7241. return this.finishNode(node, "TSTypePredicate");
  7242. }
  7243. tsParseThisTypeNode() {
  7244. const node = this.startNode();
  7245. this.next();
  7246. return this.finishNode(node, "TSThisType");
  7247. }
  7248. tsParseTypeQuery() {
  7249. const node = this.startNode();
  7250. this.expect(87);
  7251. if (this.match(83)) {
  7252. node.exprName = this.tsParseImportType();
  7253. } else {
  7254. node.exprName = this.tsParseEntityName();
  7255. }
  7256. return this.finishNode(node, "TSTypeQuery");
  7257. }
  7258. tsParseTypeParameter() {
  7259. const node = this.startNode();
  7260. node.name = this.tsParseTypeParameterName();
  7261. node.constraint = this.tsEatThenParseType(81);
  7262. node.default = this.tsEatThenParseType(29);
  7263. return this.finishNode(node, "TSTypeParameter");
  7264. }
  7265. tsTryParseTypeParameters() {
  7266. if (this.match(47)) {
  7267. return this.tsParseTypeParameters();
  7268. }
  7269. }
  7270. tsParseTypeParameters() {
  7271. const node = this.startNode();
  7272. if (this.match(47) || this.match(138)) {
  7273. this.next();
  7274. } else {
  7275. this.unexpected();
  7276. }
  7277. const refTrailingCommaPos = {
  7278. value: -1
  7279. };
  7280. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true, refTrailingCommaPos);
  7281. if (node.params.length === 0) {
  7282. this.raise(TSErrors.EmptyTypeParameters, {
  7283. at: node
  7284. });
  7285. }
  7286. if (refTrailingCommaPos.value !== -1) {
  7287. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7288. }
  7289. return this.finishNode(node, "TSTypeParameterDeclaration");
  7290. }
  7291. tsTryNextParseConstantContext() {
  7292. if (this.lookahead().type !== 75) return null;
  7293. this.next();
  7294. const typeReference = this.tsParseTypeReference();
  7295. if (typeReference.typeParameters) {
  7296. this.raise(TSErrors.CannotFindName, {
  7297. at: typeReference.typeName,
  7298. name: "const"
  7299. });
  7300. }
  7301. return typeReference;
  7302. }
  7303. tsFillSignature(returnToken, signature) {
  7304. const returnTokenRequired = returnToken === 19;
  7305. const paramsKey = "parameters";
  7306. const returnTypeKey = "typeAnnotation";
  7307. signature.typeParameters = this.tsTryParseTypeParameters();
  7308. this.expect(10);
  7309. signature[paramsKey] = this.tsParseBindingListForSignature();
  7310. if (returnTokenRequired) {
  7311. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7312. } else if (this.match(returnToken)) {
  7313. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7314. }
  7315. }
  7316. tsParseBindingListForSignature() {
  7317. return this.parseBindingList(11, 41).map(pattern => {
  7318. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7319. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7320. at: pattern,
  7321. type: pattern.type
  7322. });
  7323. }
  7324. return pattern;
  7325. });
  7326. }
  7327. tsParseTypeMemberSemicolon() {
  7328. if (!this.eat(12) && !this.isLineTerminator()) {
  7329. this.expect(13);
  7330. }
  7331. }
  7332. tsParseSignatureMember(kind, node) {
  7333. this.tsFillSignature(14, node);
  7334. this.tsParseTypeMemberSemicolon();
  7335. return this.finishNode(node, kind);
  7336. }
  7337. tsIsUnambiguouslyIndexSignature() {
  7338. this.next();
  7339. if (tokenIsIdentifier(this.state.type)) {
  7340. this.next();
  7341. return this.match(14);
  7342. }
  7343. return false;
  7344. }
  7345. tsTryParseIndexSignature(node) {
  7346. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7347. return undefined;
  7348. }
  7349. this.expect(0);
  7350. const id = this.parseIdentifier();
  7351. id.typeAnnotation = this.tsParseTypeAnnotation();
  7352. this.resetEndLocation(id);
  7353. this.expect(3);
  7354. node.parameters = [id];
  7355. const type = this.tsTryParseTypeAnnotation();
  7356. if (type) node.typeAnnotation = type;
  7357. this.tsParseTypeMemberSemicolon();
  7358. return this.finishNode(node, "TSIndexSignature");
  7359. }
  7360. tsParsePropertyOrMethodSignature(node, readonly) {
  7361. if (this.eat(17)) node.optional = true;
  7362. const nodeAny = node;
  7363. if (this.match(10) || this.match(47)) {
  7364. if (readonly) {
  7365. this.raise(TSErrors.ReadonlyForMethodSignature, {
  7366. at: node
  7367. });
  7368. }
  7369. const method = nodeAny;
  7370. if (method.kind && this.match(47)) {
  7371. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  7372. at: this.state.curPosition()
  7373. });
  7374. }
  7375. this.tsFillSignature(14, method);
  7376. this.tsParseTypeMemberSemicolon();
  7377. const paramsKey = "parameters";
  7378. const returnTypeKey = "typeAnnotation";
  7379. if (method.kind === "get") {
  7380. if (method[paramsKey].length > 0) {
  7381. this.raise(Errors.BadGetterArity, {
  7382. at: this.state.curPosition()
  7383. });
  7384. if (this.isThisParam(method[paramsKey][0])) {
  7385. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7386. at: this.state.curPosition()
  7387. });
  7388. }
  7389. }
  7390. } else if (method.kind === "set") {
  7391. if (method[paramsKey].length !== 1) {
  7392. this.raise(Errors.BadSetterArity, {
  7393. at: this.state.curPosition()
  7394. });
  7395. } else {
  7396. const firstParameter = method[paramsKey][0];
  7397. if (this.isThisParam(firstParameter)) {
  7398. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7399. at: this.state.curPosition()
  7400. });
  7401. }
  7402. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7403. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  7404. at: this.state.curPosition()
  7405. });
  7406. }
  7407. if (firstParameter.type === "RestElement") {
  7408. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  7409. at: this.state.curPosition()
  7410. });
  7411. }
  7412. }
  7413. if (method[returnTypeKey]) {
  7414. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  7415. at: method[returnTypeKey]
  7416. });
  7417. }
  7418. } else {
  7419. method.kind = "method";
  7420. }
  7421. return this.finishNode(method, "TSMethodSignature");
  7422. } else {
  7423. const property = nodeAny;
  7424. if (readonly) property.readonly = true;
  7425. const type = this.tsTryParseTypeAnnotation();
  7426. if (type) property.typeAnnotation = type;
  7427. this.tsParseTypeMemberSemicolon();
  7428. return this.finishNode(property, "TSPropertySignature");
  7429. }
  7430. }
  7431. tsParseTypeMember() {
  7432. const node = this.startNode();
  7433. if (this.match(10) || this.match(47)) {
  7434. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7435. }
  7436. if (this.match(77)) {
  7437. const id = this.startNode();
  7438. this.next();
  7439. if (this.match(10) || this.match(47)) {
  7440. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7441. } else {
  7442. node.key = this.createIdentifier(id, "new");
  7443. return this.tsParsePropertyOrMethodSignature(node, false);
  7444. }
  7445. }
  7446. this.tsParseModifiers({
  7447. modified: node,
  7448. allowedModifiers: ["readonly"],
  7449. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  7450. });
  7451. const idx = this.tsTryParseIndexSignature(node);
  7452. if (idx) {
  7453. return idx;
  7454. }
  7455. this.parsePropertyName(node);
  7456. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7457. node.kind = node.key.name;
  7458. this.parsePropertyName(node);
  7459. }
  7460. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7461. }
  7462. tsParseTypeLiteral() {
  7463. const node = this.startNode();
  7464. node.members = this.tsParseObjectTypeMembers();
  7465. return this.finishNode(node, "TSTypeLiteral");
  7466. }
  7467. tsParseObjectTypeMembers() {
  7468. this.expect(5);
  7469. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7470. this.expect(8);
  7471. return members;
  7472. }
  7473. tsIsStartOfMappedType() {
  7474. this.next();
  7475. if (this.eat(53)) {
  7476. return this.isContextual(118);
  7477. }
  7478. if (this.isContextual(118)) {
  7479. this.next();
  7480. }
  7481. if (!this.match(0)) {
  7482. return false;
  7483. }
  7484. this.next();
  7485. if (!this.tsIsIdentifier()) {
  7486. return false;
  7487. }
  7488. this.next();
  7489. return this.match(58);
  7490. }
  7491. tsParseMappedTypeParameter() {
  7492. const node = this.startNode();
  7493. node.name = this.tsParseTypeParameterName();
  7494. node.constraint = this.tsExpectThenParseType(58);
  7495. return this.finishNode(node, "TSTypeParameter");
  7496. }
  7497. tsParseMappedType() {
  7498. const node = this.startNode();
  7499. this.expect(5);
  7500. if (this.match(53)) {
  7501. node.readonly = this.state.value;
  7502. this.next();
  7503. this.expectContextual(118);
  7504. } else if (this.eatContextual(118)) {
  7505. node.readonly = true;
  7506. }
  7507. this.expect(0);
  7508. node.typeParameter = this.tsParseMappedTypeParameter();
  7509. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  7510. this.expect(3);
  7511. if (this.match(53)) {
  7512. node.optional = this.state.value;
  7513. this.next();
  7514. this.expect(17);
  7515. } else if (this.eat(17)) {
  7516. node.optional = true;
  7517. }
  7518. node.typeAnnotation = this.tsTryParseType();
  7519. this.semicolon();
  7520. this.expect(8);
  7521. return this.finishNode(node, "TSMappedType");
  7522. }
  7523. tsParseTupleType() {
  7524. const node = this.startNode();
  7525. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7526. let seenOptionalElement = false;
  7527. let labeledElements = null;
  7528. node.elementTypes.forEach(elementNode => {
  7529. var _labeledElements;
  7530. let {
  7531. type
  7532. } = elementNode;
  7533. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  7534. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  7535. at: elementNode
  7536. });
  7537. }
  7538. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  7539. if (type === "TSRestType") {
  7540. elementNode = elementNode.typeAnnotation;
  7541. type = elementNode.type;
  7542. }
  7543. const isLabeled = type === "TSNamedTupleMember";
  7544. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  7545. if (labeledElements !== isLabeled) {
  7546. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  7547. at: elementNode
  7548. });
  7549. }
  7550. });
  7551. return this.finishNode(node, "TSTupleType");
  7552. }
  7553. tsParseTupleElementType() {
  7554. const {
  7555. start: startPos,
  7556. startLoc
  7557. } = this.state;
  7558. const rest = this.eat(21);
  7559. let type = this.tsParseType();
  7560. const optional = this.eat(17);
  7561. const labeled = this.eat(14);
  7562. if (labeled) {
  7563. const labeledNode = this.startNodeAtNode(type);
  7564. labeledNode.optional = optional;
  7565. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  7566. labeledNode.label = type.typeName;
  7567. } else {
  7568. this.raise(TSErrors.InvalidTupleMemberLabel, {
  7569. at: type
  7570. });
  7571. labeledNode.label = type;
  7572. }
  7573. labeledNode.elementType = this.tsParseType();
  7574. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  7575. } else if (optional) {
  7576. const optionalTypeNode = this.startNodeAtNode(type);
  7577. optionalTypeNode.typeAnnotation = type;
  7578. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  7579. }
  7580. if (rest) {
  7581. const restNode = this.startNodeAt(startPos, startLoc);
  7582. restNode.typeAnnotation = type;
  7583. type = this.finishNode(restNode, "TSRestType");
  7584. }
  7585. return type;
  7586. }
  7587. tsParseParenthesizedType() {
  7588. const node = this.startNode();
  7589. this.expect(10);
  7590. node.typeAnnotation = this.tsParseType();
  7591. this.expect(11);
  7592. return this.finishNode(node, "TSParenthesizedType");
  7593. }
  7594. tsParseFunctionOrConstructorType(type, abstract) {
  7595. const node = this.startNode();
  7596. if (type === "TSConstructorType") {
  7597. node.abstract = !!abstract;
  7598. if (abstract) this.next();
  7599. this.next();
  7600. }
  7601. this.tsFillSignature(19, node);
  7602. return this.finishNode(node, type);
  7603. }
  7604. tsParseLiteralTypeNode() {
  7605. const node = this.startNode();
  7606. node.literal = (() => {
  7607. switch (this.state.type) {
  7608. case 130:
  7609. case 131:
  7610. case 129:
  7611. case 85:
  7612. case 86:
  7613. return this.parseExprAtom();
  7614. default:
  7615. throw this.unexpected();
  7616. }
  7617. })();
  7618. return this.finishNode(node, "TSLiteralType");
  7619. }
  7620. tsParseTemplateLiteralType() {
  7621. const node = this.startNode();
  7622. node.literal = this.parseTemplate(false);
  7623. return this.finishNode(node, "TSLiteralType");
  7624. }
  7625. parseTemplateSubstitution() {
  7626. if (this.state.inType) return this.tsParseType();
  7627. return super.parseTemplateSubstitution();
  7628. }
  7629. tsParseThisTypeOrThisTypePredicate() {
  7630. const thisKeyword = this.tsParseThisTypeNode();
  7631. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  7632. return this.tsParseThisTypePredicate(thisKeyword);
  7633. } else {
  7634. return thisKeyword;
  7635. }
  7636. }
  7637. tsParseNonArrayType() {
  7638. switch (this.state.type) {
  7639. case 129:
  7640. case 130:
  7641. case 131:
  7642. case 85:
  7643. case 86:
  7644. return this.tsParseLiteralTypeNode();
  7645. case 53:
  7646. if (this.state.value === "-") {
  7647. const node = this.startNode();
  7648. const nextToken = this.lookahead();
  7649. if (nextToken.type !== 130 && nextToken.type !== 131) {
  7650. throw this.unexpected();
  7651. }
  7652. node.literal = this.parseMaybeUnary();
  7653. return this.finishNode(node, "TSLiteralType");
  7654. }
  7655. break;
  7656. case 78:
  7657. return this.tsParseThisTypeOrThisTypePredicate();
  7658. case 87:
  7659. return this.tsParseTypeQuery();
  7660. case 83:
  7661. return this.tsParseImportType();
  7662. case 5:
  7663. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7664. case 0:
  7665. return this.tsParseTupleType();
  7666. case 10:
  7667. return this.tsParseParenthesizedType();
  7668. case 25:
  7669. case 24:
  7670. return this.tsParseTemplateLiteralType();
  7671. default:
  7672. {
  7673. const {
  7674. type
  7675. } = this.state;
  7676. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  7677. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7678. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  7679. const node = this.startNode();
  7680. this.next();
  7681. return this.finishNode(node, nodeType);
  7682. }
  7683. return this.tsParseTypeReference();
  7684. }
  7685. }
  7686. }
  7687. throw this.unexpected();
  7688. }
  7689. tsParseArrayTypeOrHigher() {
  7690. let type = this.tsParseNonArrayType();
  7691. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  7692. if (this.match(3)) {
  7693. const node = this.startNodeAtNode(type);
  7694. node.elementType = type;
  7695. this.expect(3);
  7696. type = this.finishNode(node, "TSArrayType");
  7697. } else {
  7698. const node = this.startNodeAtNode(type);
  7699. node.objectType = type;
  7700. node.indexType = this.tsParseType();
  7701. this.expect(3);
  7702. type = this.finishNode(node, "TSIndexedAccessType");
  7703. }
  7704. }
  7705. return type;
  7706. }
  7707. tsParseTypeOperator() {
  7708. const node = this.startNode();
  7709. const operator = this.state.value;
  7710. this.next();
  7711. node.operator = operator;
  7712. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7713. if (operator === "readonly") {
  7714. this.tsCheckTypeAnnotationForReadOnly(node);
  7715. }
  7716. return this.finishNode(node, "TSTypeOperator");
  7717. }
  7718. tsCheckTypeAnnotationForReadOnly(node) {
  7719. switch (node.typeAnnotation.type) {
  7720. case "TSTupleType":
  7721. case "TSArrayType":
  7722. return;
  7723. default:
  7724. this.raise(TSErrors.UnexpectedReadonly, {
  7725. at: node
  7726. });
  7727. }
  7728. }
  7729. tsParseInferType() {
  7730. const node = this.startNode();
  7731. this.expectContextual(112);
  7732. const typeParameter = this.startNode();
  7733. typeParameter.name = this.tsParseTypeParameterName();
  7734. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7735. return this.finishNode(node, "TSInferType");
  7736. }
  7737. tsParseTypeOperatorOrHigher() {
  7738. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  7739. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(112) ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  7740. }
  7741. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7742. const node = this.startNode();
  7743. const hasLeadingOperator = this.eat(operator);
  7744. const types = [];
  7745. do {
  7746. types.push(parseConstituentType());
  7747. } while (this.eat(operator));
  7748. if (types.length === 1 && !hasLeadingOperator) {
  7749. return types[0];
  7750. }
  7751. node.types = types;
  7752. return this.finishNode(node, kind);
  7753. }
  7754. tsParseIntersectionTypeOrHigher() {
  7755. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  7756. }
  7757. tsParseUnionTypeOrHigher() {
  7758. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  7759. }
  7760. tsIsStartOfFunctionType() {
  7761. if (this.match(47)) {
  7762. return true;
  7763. }
  7764. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  7765. }
  7766. tsSkipParameterStart() {
  7767. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  7768. this.next();
  7769. return true;
  7770. }
  7771. if (this.match(5)) {
  7772. const {
  7773. errors
  7774. } = this.state;
  7775. const previousErrorCount = errors.length;
  7776. try {
  7777. this.parseObjectLike(8, true);
  7778. return errors.length === previousErrorCount;
  7779. } catch (_unused) {
  7780. return false;
  7781. }
  7782. }
  7783. if (this.match(0)) {
  7784. this.next();
  7785. const {
  7786. errors
  7787. } = this.state;
  7788. const previousErrorCount = errors.length;
  7789. try {
  7790. this.parseBindingList(3, 93, true);
  7791. return errors.length === previousErrorCount;
  7792. } catch (_unused2) {
  7793. return false;
  7794. }
  7795. }
  7796. return false;
  7797. }
  7798. tsIsUnambiguouslyStartOfFunctionType() {
  7799. this.next();
  7800. if (this.match(11) || this.match(21)) {
  7801. return true;
  7802. }
  7803. if (this.tsSkipParameterStart()) {
  7804. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  7805. return true;
  7806. }
  7807. if (this.match(11)) {
  7808. this.next();
  7809. if (this.match(19)) {
  7810. return true;
  7811. }
  7812. }
  7813. }
  7814. return false;
  7815. }
  7816. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  7817. return this.tsInType(() => {
  7818. const t = this.startNode();
  7819. this.expect(returnToken);
  7820. const node = this.startNode();
  7821. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  7822. if (asserts && this.match(78)) {
  7823. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  7824. if (thisTypePredicate.type === "TSThisType") {
  7825. node.parameterName = thisTypePredicate;
  7826. node.asserts = true;
  7827. node.typeAnnotation = null;
  7828. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  7829. } else {
  7830. this.resetStartLocationFromNode(thisTypePredicate, node);
  7831. thisTypePredicate.asserts = true;
  7832. }
  7833. t.typeAnnotation = thisTypePredicate;
  7834. return this.finishNode(t, "TSTypeAnnotation");
  7835. }
  7836. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  7837. if (!typePredicateVariable) {
  7838. if (!asserts) {
  7839. return this.tsParseTypeAnnotation(false, t);
  7840. }
  7841. node.parameterName = this.parseIdentifier();
  7842. node.asserts = asserts;
  7843. node.typeAnnotation = null;
  7844. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7845. return this.finishNode(t, "TSTypeAnnotation");
  7846. }
  7847. const type = this.tsParseTypeAnnotation(false);
  7848. node.parameterName = typePredicateVariable;
  7849. node.typeAnnotation = type;
  7850. node.asserts = asserts;
  7851. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7852. return this.finishNode(t, "TSTypeAnnotation");
  7853. });
  7854. }
  7855. tsTryParseTypeOrTypePredicateAnnotation() {
  7856. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  7857. }
  7858. tsTryParseTypeAnnotation() {
  7859. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  7860. }
  7861. tsTryParseType() {
  7862. return this.tsEatThenParseType(14);
  7863. }
  7864. tsParseTypePredicatePrefix() {
  7865. const id = this.parseIdentifier();
  7866. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  7867. this.next();
  7868. return id;
  7869. }
  7870. }
  7871. tsParseTypePredicateAsserts() {
  7872. if (this.state.type !== 106) {
  7873. return false;
  7874. }
  7875. const containsEsc = this.state.containsEsc;
  7876. this.next();
  7877. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  7878. return false;
  7879. }
  7880. if (containsEsc) {
  7881. this.raise(Errors.InvalidEscapedReservedWord, {
  7882. at: this.state.lastTokStartLoc,
  7883. reservedWord: "asserts"
  7884. });
  7885. }
  7886. return true;
  7887. }
  7888. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  7889. this.tsInType(() => {
  7890. if (eatColon) this.expect(14);
  7891. t.typeAnnotation = this.tsParseType();
  7892. });
  7893. return this.finishNode(t, "TSTypeAnnotation");
  7894. }
  7895. tsParseType() {
  7896. assert(this.state.inType);
  7897. const type = this.tsParseNonConditionalType();
  7898. if (this.hasPrecedingLineBreak() || !this.eat(81)) {
  7899. return type;
  7900. }
  7901. const node = this.startNodeAtNode(type);
  7902. node.checkType = type;
  7903. node.extendsType = this.tsParseNonConditionalType();
  7904. this.expect(17);
  7905. node.trueType = this.tsParseType();
  7906. this.expect(14);
  7907. node.falseType = this.tsParseType();
  7908. return this.finishNode(node, "TSConditionalType");
  7909. }
  7910. isAbstractConstructorSignature() {
  7911. return this.isContextual(120) && this.lookahead().type === 77;
  7912. }
  7913. tsParseNonConditionalType() {
  7914. if (this.tsIsStartOfFunctionType()) {
  7915. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  7916. }
  7917. if (this.match(77)) {
  7918. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  7919. } else if (this.isAbstractConstructorSignature()) {
  7920. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  7921. }
  7922. return this.tsParseUnionTypeOrHigher();
  7923. }
  7924. tsParseTypeAssertion() {
  7925. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  7926. this.raise(TSErrors.ReservedTypeAssertion, {
  7927. at: this.state.startLoc
  7928. });
  7929. }
  7930. const node = this.startNode();
  7931. const _const = this.tsTryNextParseConstantContext();
  7932. node.typeAnnotation = _const || this.tsNextThenParseType();
  7933. this.expect(48);
  7934. node.expression = this.parseMaybeUnary();
  7935. return this.finishNode(node, "TSTypeAssertion");
  7936. }
  7937. tsParseHeritageClause(token) {
  7938. const originalStartLoc = this.state.startLoc;
  7939. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  7940. if (!delimitedList.length) {
  7941. this.raise(TSErrors.EmptyHeritageClauseType, {
  7942. at: originalStartLoc,
  7943. token
  7944. });
  7945. }
  7946. return delimitedList;
  7947. }
  7948. tsParseExpressionWithTypeArguments() {
  7949. const node = this.startNode();
  7950. node.expression = this.tsParseEntityName();
  7951. if (this.match(47)) {
  7952. node.typeParameters = this.tsParseTypeArguments();
  7953. }
  7954. return this.finishNode(node, "TSExpressionWithTypeArguments");
  7955. }
  7956. tsParseInterfaceDeclaration(node, properties = {}) {
  7957. if (this.hasFollowingLineBreak()) return null;
  7958. this.expectContextual(125);
  7959. if (properties.declare) node.declare = true;
  7960. if (tokenIsIdentifier(this.state.type)) {
  7961. node.id = this.parseIdentifier();
  7962. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  7963. } else {
  7964. node.id = null;
  7965. this.raise(TSErrors.MissingInterfaceName, {
  7966. at: this.state.startLoc
  7967. });
  7968. }
  7969. node.typeParameters = this.tsTryParseTypeParameters();
  7970. if (this.eat(81)) {
  7971. node.extends = this.tsParseHeritageClause("extends");
  7972. }
  7973. const body = this.startNode();
  7974. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  7975. node.body = this.finishNode(body, "TSInterfaceBody");
  7976. return this.finishNode(node, "TSInterfaceDeclaration");
  7977. }
  7978. tsParseTypeAliasDeclaration(node) {
  7979. node.id = this.parseIdentifier();
  7980. this.checkIdentifier(node.id, BIND_TS_TYPE);
  7981. node.typeAnnotation = this.tsInType(() => {
  7982. node.typeParameters = this.tsTryParseTypeParameters();
  7983. this.expect(29);
  7984. if (this.isContextual(111) && this.lookahead().type !== 16) {
  7985. const node = this.startNode();
  7986. this.next();
  7987. return this.finishNode(node, "TSIntrinsicKeyword");
  7988. }
  7989. return this.tsParseType();
  7990. });
  7991. this.semicolon();
  7992. return this.finishNode(node, "TSTypeAliasDeclaration");
  7993. }
  7994. tsInNoContext(cb) {
  7995. const oldContext = this.state.context;
  7996. this.state.context = [oldContext[0]];
  7997. try {
  7998. return cb();
  7999. } finally {
  8000. this.state.context = oldContext;
  8001. }
  8002. }
  8003. tsInType(cb) {
  8004. const oldInType = this.state.inType;
  8005. this.state.inType = true;
  8006. try {
  8007. return cb();
  8008. } finally {
  8009. this.state.inType = oldInType;
  8010. }
  8011. }
  8012. tsEatThenParseType(token) {
  8013. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8014. }
  8015. tsExpectThenParseType(token) {
  8016. return this.tsDoThenParseType(() => this.expect(token));
  8017. }
  8018. tsNextThenParseType() {
  8019. return this.tsDoThenParseType(() => this.next());
  8020. }
  8021. tsDoThenParseType(cb) {
  8022. return this.tsInType(() => {
  8023. cb();
  8024. return this.tsParseType();
  8025. });
  8026. }
  8027. tsParseEnumMember() {
  8028. const node = this.startNode();
  8029. node.id = this.match(129) ? this.parseExprAtom() : this.parseIdentifier(true);
  8030. if (this.eat(29)) {
  8031. node.initializer = this.parseMaybeAssignAllowIn();
  8032. }
  8033. return this.finishNode(node, "TSEnumMember");
  8034. }
  8035. tsParseEnumDeclaration(node, properties = {}) {
  8036. if (properties.const) node.const = true;
  8037. if (properties.declare) node.declare = true;
  8038. this.expectContextual(122);
  8039. node.id = this.parseIdentifier();
  8040. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8041. this.expect(5);
  8042. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8043. this.expect(8);
  8044. return this.finishNode(node, "TSEnumDeclaration");
  8045. }
  8046. tsParseModuleBlock() {
  8047. const node = this.startNode();
  8048. this.scope.enter(SCOPE_OTHER);
  8049. this.expect(5);
  8050. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8051. this.scope.exit();
  8052. return this.finishNode(node, "TSModuleBlock");
  8053. }
  8054. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8055. node.id = this.parseIdentifier();
  8056. if (!nested) {
  8057. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8058. }
  8059. if (this.eat(16)) {
  8060. const inner = this.startNode();
  8061. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8062. node.body = inner;
  8063. } else {
  8064. this.scope.enter(SCOPE_TS_MODULE);
  8065. this.prodParam.enter(PARAM);
  8066. node.body = this.tsParseModuleBlock();
  8067. this.prodParam.exit();
  8068. this.scope.exit();
  8069. }
  8070. return this.finishNode(node, "TSModuleDeclaration");
  8071. }
  8072. tsParseAmbientExternalModuleDeclaration(node) {
  8073. if (this.isContextual(109)) {
  8074. node.global = true;
  8075. node.id = this.parseIdentifier();
  8076. } else if (this.match(129)) {
  8077. node.id = this.parseExprAtom();
  8078. } else {
  8079. this.unexpected();
  8080. }
  8081. if (this.match(5)) {
  8082. this.scope.enter(SCOPE_TS_MODULE);
  8083. this.prodParam.enter(PARAM);
  8084. node.body = this.tsParseModuleBlock();
  8085. this.prodParam.exit();
  8086. this.scope.exit();
  8087. } else {
  8088. this.semicolon();
  8089. }
  8090. return this.finishNode(node, "TSModuleDeclaration");
  8091. }
  8092. tsParseImportEqualsDeclaration(node, isExport) {
  8093. node.isExport = isExport || false;
  8094. node.id = this.parseIdentifier();
  8095. this.checkIdentifier(node.id, BIND_LEXICAL);
  8096. this.expect(29);
  8097. const moduleReference = this.tsParseModuleReference();
  8098. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8099. this.raise(TSErrors.ImportAliasHasImportType, {
  8100. at: moduleReference
  8101. });
  8102. }
  8103. node.moduleReference = moduleReference;
  8104. this.semicolon();
  8105. return this.finishNode(node, "TSImportEqualsDeclaration");
  8106. }
  8107. tsIsExternalModuleReference() {
  8108. return this.isContextual(116) && this.lookaheadCharCode() === 40;
  8109. }
  8110. tsParseModuleReference() {
  8111. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8112. }
  8113. tsParseExternalModuleReference() {
  8114. const node = this.startNode();
  8115. this.expectContextual(116);
  8116. this.expect(10);
  8117. if (!this.match(129)) {
  8118. throw this.unexpected();
  8119. }
  8120. node.expression = this.parseExprAtom();
  8121. this.expect(11);
  8122. return this.finishNode(node, "TSExternalModuleReference");
  8123. }
  8124. tsLookAhead(f) {
  8125. const state = this.state.clone();
  8126. const res = f();
  8127. this.state = state;
  8128. return res;
  8129. }
  8130. tsTryParseAndCatch(f) {
  8131. const result = this.tryParse(abort => f() || abort());
  8132. if (result.aborted || !result.node) return undefined;
  8133. if (result.error) this.state = result.failState;
  8134. return result.node;
  8135. }
  8136. tsTryParse(f) {
  8137. const state = this.state.clone();
  8138. const result = f();
  8139. if (result !== undefined && result !== false) {
  8140. return result;
  8141. } else {
  8142. this.state = state;
  8143. return undefined;
  8144. }
  8145. }
  8146. tsTryParseDeclare(nany) {
  8147. if (this.isLineTerminator()) {
  8148. return;
  8149. }
  8150. let starttype = this.state.type;
  8151. let kind;
  8152. if (this.isContextual(99)) {
  8153. starttype = 74;
  8154. kind = "let";
  8155. }
  8156. return this.tsInAmbientContext(() => {
  8157. if (starttype === 68) {
  8158. nany.declare = true;
  8159. return this.parseFunctionStatement(nany, false, true);
  8160. }
  8161. if (starttype === 80) {
  8162. nany.declare = true;
  8163. return this.parseClass(nany, true, false);
  8164. }
  8165. if (starttype === 122) {
  8166. return this.tsParseEnumDeclaration(nany, {
  8167. declare: true
  8168. });
  8169. }
  8170. if (starttype === 109) {
  8171. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8172. }
  8173. if (starttype === 75 || starttype === 74) {
  8174. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8175. nany.declare = true;
  8176. return this.parseVarStatement(nany, kind || this.state.value, true);
  8177. }
  8178. this.expect(75);
  8179. return this.tsParseEnumDeclaration(nany, {
  8180. const: true,
  8181. declare: true
  8182. });
  8183. }
  8184. if (starttype === 125) {
  8185. const result = this.tsParseInterfaceDeclaration(nany, {
  8186. declare: true
  8187. });
  8188. if (result) return result;
  8189. }
  8190. if (tokenIsIdentifier(starttype)) {
  8191. return this.tsParseDeclaration(nany, this.state.value, true);
  8192. }
  8193. });
  8194. }
  8195. tsTryParseExportDeclaration() {
  8196. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  8197. }
  8198. tsParseExpressionStatement(node, expr) {
  8199. switch (expr.name) {
  8200. case "declare":
  8201. {
  8202. const declaration = this.tsTryParseDeclare(node);
  8203. if (declaration) {
  8204. declaration.declare = true;
  8205. return declaration;
  8206. }
  8207. break;
  8208. }
  8209. case "global":
  8210. if (this.match(5)) {
  8211. this.scope.enter(SCOPE_TS_MODULE);
  8212. this.prodParam.enter(PARAM);
  8213. const mod = node;
  8214. mod.global = true;
  8215. mod.id = expr;
  8216. mod.body = this.tsParseModuleBlock();
  8217. this.scope.exit();
  8218. this.prodParam.exit();
  8219. return this.finishNode(mod, "TSModuleDeclaration");
  8220. }
  8221. break;
  8222. default:
  8223. return this.tsParseDeclaration(node, expr.name, false);
  8224. }
  8225. }
  8226. tsParseDeclaration(node, value, next) {
  8227. switch (value) {
  8228. case "abstract":
  8229. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8230. return this.tsParseAbstractDeclaration(node);
  8231. }
  8232. break;
  8233. case "module":
  8234. if (this.tsCheckLineTerminator(next)) {
  8235. if (this.match(129)) {
  8236. return this.tsParseAmbientExternalModuleDeclaration(node);
  8237. } else if (tokenIsIdentifier(this.state.type)) {
  8238. return this.tsParseModuleOrNamespaceDeclaration(node);
  8239. }
  8240. }
  8241. break;
  8242. case "namespace":
  8243. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8244. return this.tsParseModuleOrNamespaceDeclaration(node);
  8245. }
  8246. break;
  8247. case "type":
  8248. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8249. return this.tsParseTypeAliasDeclaration(node);
  8250. }
  8251. break;
  8252. }
  8253. }
  8254. tsCheckLineTerminator(next) {
  8255. if (next) {
  8256. if (this.hasFollowingLineBreak()) return false;
  8257. this.next();
  8258. return true;
  8259. }
  8260. return !this.isLineTerminator();
  8261. }
  8262. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  8263. if (!this.match(47)) {
  8264. return undefined;
  8265. }
  8266. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8267. this.state.maybeInArrowParameters = true;
  8268. const res = this.tsTryParseAndCatch(() => {
  8269. const node = this.startNodeAt(startPos, startLoc);
  8270. node.typeParameters = this.tsParseTypeParameters();
  8271. super.parseFunctionParams(node);
  8272. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8273. this.expect(19);
  8274. return node;
  8275. });
  8276. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8277. if (!res) {
  8278. return undefined;
  8279. }
  8280. return this.parseArrowExpression(res, null, true);
  8281. }
  8282. tsParseTypeArgumentsInExpression() {
  8283. if (this.reScan_lt() !== 47) {
  8284. return undefined;
  8285. }
  8286. return this.tsParseTypeArguments();
  8287. }
  8288. tsParseTypeArguments() {
  8289. const node = this.startNode();
  8290. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8291. this.expect(47);
  8292. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8293. }));
  8294. if (node.params.length === 0) {
  8295. this.raise(TSErrors.EmptyTypeArguments, {
  8296. at: node
  8297. });
  8298. }
  8299. this.expect(48);
  8300. return this.finishNode(node, "TSTypeParameterInstantiation");
  8301. }
  8302. tsIsDeclarationStart() {
  8303. return tokenIsTSDeclarationStart(this.state.type);
  8304. }
  8305. isExportDefaultSpecifier() {
  8306. if (this.tsIsDeclarationStart()) return false;
  8307. return super.isExportDefaultSpecifier();
  8308. }
  8309. parseAssignableListItem(allowModifiers, decorators) {
  8310. const startPos = this.state.start;
  8311. const startLoc = this.state.startLoc;
  8312. let accessibility;
  8313. let readonly = false;
  8314. let override = false;
  8315. if (allowModifiers !== undefined) {
  8316. const modified = {};
  8317. this.tsParseModifiers({
  8318. modified,
  8319. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8320. });
  8321. accessibility = modified.accessibility;
  8322. override = modified.override;
  8323. readonly = modified.readonly;
  8324. if (allowModifiers === false && (accessibility || readonly || override)) {
  8325. this.raise(TSErrors.UnexpectedParameterModifier, {
  8326. at: startLoc
  8327. });
  8328. }
  8329. }
  8330. const left = this.parseMaybeDefault();
  8331. this.parseAssignableListItemTypes(left);
  8332. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8333. if (accessibility || readonly || override) {
  8334. const pp = this.startNodeAt(startPos, startLoc);
  8335. if (decorators.length) {
  8336. pp.decorators = decorators;
  8337. }
  8338. if (accessibility) pp.accessibility = accessibility;
  8339. if (readonly) pp.readonly = readonly;
  8340. if (override) pp.override = override;
  8341. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8342. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  8343. at: pp
  8344. });
  8345. }
  8346. pp.parameter = elt;
  8347. return this.finishNode(pp, "TSParameterProperty");
  8348. }
  8349. if (decorators.length) {
  8350. left.decorators = decorators;
  8351. }
  8352. return elt;
  8353. }
  8354. isSimpleParameter(node) {
  8355. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  8356. }
  8357. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8358. if (this.match(14)) {
  8359. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8360. }
  8361. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  8362. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  8363. this.finishNode(node, bodilessType);
  8364. return;
  8365. }
  8366. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8367. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  8368. at: node
  8369. });
  8370. if (node.declare) {
  8371. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8372. return;
  8373. }
  8374. }
  8375. super.parseFunctionBodyAndFinish(node, type, isMethod);
  8376. }
  8377. registerFunctionStatementId(node) {
  8378. if (!node.body && node.id) {
  8379. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  8380. } else {
  8381. super.registerFunctionStatementId(...arguments);
  8382. }
  8383. }
  8384. tsCheckForInvalidTypeCasts(items) {
  8385. items.forEach(node => {
  8386. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8387. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  8388. at: node.typeAnnotation
  8389. });
  8390. }
  8391. });
  8392. }
  8393. toReferencedList(exprList, isInParens) {
  8394. this.tsCheckForInvalidTypeCasts(exprList);
  8395. return exprList;
  8396. }
  8397. parseArrayLike(...args) {
  8398. const node = super.parseArrayLike(...args);
  8399. if (node.type === "ArrayExpression") {
  8400. this.tsCheckForInvalidTypeCasts(node.elements);
  8401. }
  8402. return node;
  8403. }
  8404. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8405. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8406. this.state.canStartJSXElement = false;
  8407. this.next();
  8408. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  8409. nonNullExpression.expression = base;
  8410. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8411. }
  8412. let isOptionalCall = false;
  8413. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8414. if (noCalls) {
  8415. state.stop = true;
  8416. return base;
  8417. }
  8418. state.optionalChainMember = isOptionalCall = true;
  8419. this.next();
  8420. }
  8421. if (this.match(47) || this.match(51)) {
  8422. let missingParenErrorLoc;
  8423. const result = this.tsTryParseAndCatch(() => {
  8424. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8425. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  8426. if (asyncArrowFn) {
  8427. return asyncArrowFn;
  8428. }
  8429. }
  8430. const node = this.startNodeAt(startPos, startLoc);
  8431. node.callee = base;
  8432. const typeArguments = this.tsParseTypeArgumentsInExpression();
  8433. if (typeArguments) {
  8434. if (isOptionalCall && !this.match(10)) {
  8435. missingParenErrorLoc = this.state.curPosition();
  8436. this.unexpected();
  8437. }
  8438. if (!noCalls && this.eat(10)) {
  8439. node.arguments = this.parseCallExpressionArguments(11, false);
  8440. this.tsCheckForInvalidTypeCasts(node.arguments);
  8441. node.typeParameters = typeArguments;
  8442. if (state.optionalChainMember) {
  8443. node.optional = isOptionalCall;
  8444. }
  8445. return this.finishCallExpression(node, state.optionalChainMember);
  8446. } else if (tokenIsTemplate(this.state.type)) {
  8447. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8448. result.typeParameters = typeArguments;
  8449. return result;
  8450. }
  8451. }
  8452. this.unexpected();
  8453. });
  8454. if (missingParenErrorLoc) {
  8455. this.unexpected(missingParenErrorLoc, 10);
  8456. }
  8457. if (result) return result;
  8458. }
  8459. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  8460. }
  8461. parseNewArguments(node) {
  8462. if (this.match(47) || this.match(51)) {
  8463. const typeParameters = this.tsTryParseAndCatch(() => {
  8464. const args = this.tsParseTypeArgumentsInExpression();
  8465. if (!this.match(10)) this.unexpected();
  8466. return args;
  8467. });
  8468. if (typeParameters) {
  8469. node.typeParameters = typeParameters;
  8470. }
  8471. }
  8472. super.parseNewArguments(node);
  8473. }
  8474. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8475. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual(93)) {
  8476. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8477. node.expression = left;
  8478. const _const = this.tsTryNextParseConstantContext();
  8479. if (_const) {
  8480. node.typeAnnotation = _const;
  8481. } else {
  8482. node.typeAnnotation = this.tsNextThenParseType();
  8483. }
  8484. this.finishNode(node, "TSAsExpression");
  8485. this.reScan_lt_gt();
  8486. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8487. }
  8488. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  8489. }
  8490. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  8491. if (!this.state.isAmbientContext) {
  8492. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  8493. }
  8494. }
  8495. checkDuplicateExports() {}
  8496. parseImport(node) {
  8497. node.importKind = "value";
  8498. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  8499. let ahead = this.lookahead();
  8500. if (this.isContextual(126) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  8501. node.importKind = "type";
  8502. this.next();
  8503. ahead = this.lookahead();
  8504. }
  8505. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  8506. return this.tsParseImportEqualsDeclaration(node);
  8507. }
  8508. }
  8509. const importNode = super.parseImport(node);
  8510. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  8511. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  8512. at: importNode
  8513. });
  8514. }
  8515. return importNode;
  8516. }
  8517. parseExport(node) {
  8518. if (this.match(83)) {
  8519. this.next();
  8520. if (this.isContextual(126) && this.lookaheadCharCode() !== 61) {
  8521. node.importKind = "type";
  8522. this.next();
  8523. } else {
  8524. node.importKind = "value";
  8525. }
  8526. return this.tsParseImportEqualsDeclaration(node, true);
  8527. } else if (this.eat(29)) {
  8528. const assign = node;
  8529. assign.expression = this.parseExpression();
  8530. this.semicolon();
  8531. return this.finishNode(assign, "TSExportAssignment");
  8532. } else if (this.eatContextual(93)) {
  8533. const decl = node;
  8534. this.expectContextual(124);
  8535. decl.id = this.parseIdentifier();
  8536. this.semicolon();
  8537. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  8538. } else {
  8539. if (this.isContextual(126) && this.lookahead().type === 5) {
  8540. this.next();
  8541. node.exportKind = "type";
  8542. } else {
  8543. node.exportKind = "value";
  8544. }
  8545. return super.parseExport(node);
  8546. }
  8547. }
  8548. isAbstractClass() {
  8549. return this.isContextual(120) && this.lookahead().type === 80;
  8550. }
  8551. parseExportDefaultExpression() {
  8552. if (this.isAbstractClass()) {
  8553. const cls = this.startNode();
  8554. this.next();
  8555. cls.abstract = true;
  8556. this.parseClass(cls, true, true);
  8557. return cls;
  8558. }
  8559. if (this.match(125)) {
  8560. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8561. if (result) return result;
  8562. }
  8563. return super.parseExportDefaultExpression();
  8564. }
  8565. parseVarStatement(node, kind, allowMissingInitializer = false) {
  8566. const {
  8567. isAmbientContext
  8568. } = this.state;
  8569. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  8570. if (!isAmbientContext) return declaration;
  8571. for (const {
  8572. id,
  8573. init
  8574. } of declaration.declarations) {
  8575. if (!init) continue;
  8576. if (kind !== "const" || !!id.typeAnnotation) {
  8577. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  8578. at: init
  8579. });
  8580. } else if (init.type !== "StringLiteral" && init.type !== "BooleanLiteral" && init.type !== "NumericLiteral" && init.type !== "BigIntLiteral" && (init.type !== "TemplateLiteral" || init.expressions.length > 0) && !isPossiblyLiteralEnum(init)) {
  8581. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  8582. at: init
  8583. });
  8584. }
  8585. }
  8586. return declaration;
  8587. }
  8588. parseStatementContent(context, topLevel) {
  8589. if (this.match(75) && this.isLookaheadContextual("enum")) {
  8590. const node = this.startNode();
  8591. this.expect(75);
  8592. return this.tsParseEnumDeclaration(node, {
  8593. const: true
  8594. });
  8595. }
  8596. if (this.isContextual(122)) {
  8597. return this.tsParseEnumDeclaration(this.startNode());
  8598. }
  8599. if (this.isContextual(125)) {
  8600. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8601. if (result) return result;
  8602. }
  8603. return super.parseStatementContent(context, topLevel);
  8604. }
  8605. parseAccessModifier() {
  8606. return this.tsParseModifier(["public", "protected", "private"]);
  8607. }
  8608. tsHasSomeModifiers(member, modifiers) {
  8609. return modifiers.some(modifier => {
  8610. if (tsIsAccessModifier(modifier)) {
  8611. return member.accessibility === modifier;
  8612. }
  8613. return !!member[modifier];
  8614. });
  8615. }
  8616. tsIsStartOfStaticBlocks() {
  8617. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  8618. }
  8619. parseClassMember(classBody, member, state) {
  8620. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  8621. this.tsParseModifiers({
  8622. modified: member,
  8623. allowedModifiers: modifiers,
  8624. stopOnStartOfClassStaticBlock: true
  8625. });
  8626. const callParseClassMemberWithIsStatic = () => {
  8627. if (this.tsIsStartOfStaticBlocks()) {
  8628. this.next();
  8629. this.next();
  8630. if (this.tsHasSomeModifiers(member, modifiers)) {
  8631. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  8632. at: this.state.curPosition()
  8633. });
  8634. }
  8635. this.parseClassStaticBlock(classBody, member);
  8636. } else {
  8637. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  8638. }
  8639. };
  8640. if (member.declare) {
  8641. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  8642. } else {
  8643. callParseClassMemberWithIsStatic();
  8644. }
  8645. }
  8646. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  8647. const idx = this.tsTryParseIndexSignature(member);
  8648. if (idx) {
  8649. classBody.body.push(idx);
  8650. if (member.abstract) {
  8651. this.raise(TSErrors.IndexSignatureHasAbstract, {
  8652. at: member
  8653. });
  8654. }
  8655. if (member.accessibility) {
  8656. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  8657. at: member,
  8658. modifier: member.accessibility
  8659. });
  8660. }
  8661. if (member.declare) {
  8662. this.raise(TSErrors.IndexSignatureHasDeclare, {
  8663. at: member
  8664. });
  8665. }
  8666. if (member.override) {
  8667. this.raise(TSErrors.IndexSignatureHasOverride, {
  8668. at: member
  8669. });
  8670. }
  8671. return;
  8672. }
  8673. if (!this.state.inAbstractClass && member.abstract) {
  8674. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  8675. at: member
  8676. });
  8677. }
  8678. if (member.override) {
  8679. if (!state.hadSuperClass) {
  8680. this.raise(TSErrors.OverrideNotInSubClass, {
  8681. at: member
  8682. });
  8683. }
  8684. }
  8685. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  8686. }
  8687. parsePostMemberNameModifiers(methodOrProp) {
  8688. const optional = this.eat(17);
  8689. if (optional) methodOrProp.optional = true;
  8690. if (methodOrProp.readonly && this.match(10)) {
  8691. this.raise(TSErrors.ClassMethodHasReadonly, {
  8692. at: methodOrProp
  8693. });
  8694. }
  8695. if (methodOrProp.declare && this.match(10)) {
  8696. this.raise(TSErrors.ClassMethodHasDeclare, {
  8697. at: methodOrProp
  8698. });
  8699. }
  8700. }
  8701. parseExpressionStatement(node, expr) {
  8702. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  8703. return decl || super.parseExpressionStatement(node, expr);
  8704. }
  8705. shouldParseExportDeclaration() {
  8706. if (this.tsIsDeclarationStart()) return true;
  8707. return super.shouldParseExportDeclaration();
  8708. }
  8709. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8710. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  8711. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8712. }
  8713. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  8714. if (!result.node) {
  8715. if (result.error) {
  8716. super.setOptionalParametersError(refExpressionErrors, result.error);
  8717. }
  8718. return expr;
  8719. }
  8720. if (result.error) this.state = result.failState;
  8721. return result.node;
  8722. }
  8723. parseParenItem(node, startPos, startLoc) {
  8724. node = super.parseParenItem(node, startPos, startLoc);
  8725. if (this.eat(17)) {
  8726. node.optional = true;
  8727. this.resetEndLocation(node);
  8728. }
  8729. if (this.match(14)) {
  8730. const typeCastNode = this.startNodeAt(startPos, startLoc);
  8731. typeCastNode.expression = node;
  8732. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  8733. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  8734. }
  8735. return node;
  8736. }
  8737. parseExportDeclaration(node) {
  8738. if (!this.state.isAmbientContext && this.isContextual(121)) {
  8739. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  8740. }
  8741. const startPos = this.state.start;
  8742. const startLoc = this.state.startLoc;
  8743. const isDeclare = this.eatContextual(121);
  8744. if (isDeclare && (this.isContextual(121) || !this.shouldParseExportDeclaration())) {
  8745. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  8746. at: this.state.startLoc
  8747. });
  8748. }
  8749. const isIdentifier = tokenIsIdentifier(this.state.type);
  8750. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  8751. if (!declaration) return null;
  8752. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  8753. node.exportKind = "type";
  8754. }
  8755. if (isDeclare) {
  8756. this.resetStartLocation(declaration, startPos, startLoc);
  8757. declaration.declare = true;
  8758. }
  8759. return declaration;
  8760. }
  8761. parseClassId(node, isStatement, optionalId) {
  8762. if ((!isStatement || optionalId) && this.isContextual(110)) {
  8763. return;
  8764. }
  8765. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  8766. const typeParameters = this.tsTryParseTypeParameters();
  8767. if (typeParameters) node.typeParameters = typeParameters;
  8768. }
  8769. parseClassPropertyAnnotation(node) {
  8770. if (!node.optional && this.eat(35)) {
  8771. node.definite = true;
  8772. }
  8773. const type = this.tsTryParseTypeAnnotation();
  8774. if (type) node.typeAnnotation = type;
  8775. }
  8776. parseClassProperty(node) {
  8777. this.parseClassPropertyAnnotation(node);
  8778. if (this.state.isAmbientContext && this.match(29)) {
  8779. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  8780. at: this.state.startLoc
  8781. });
  8782. }
  8783. if (node.abstract && this.match(29)) {
  8784. const {
  8785. key
  8786. } = node;
  8787. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  8788. at: this.state.startLoc,
  8789. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  8790. });
  8791. }
  8792. return super.parseClassProperty(node);
  8793. }
  8794. parseClassPrivateProperty(node) {
  8795. if (node.abstract) {
  8796. this.raise(TSErrors.PrivateElementHasAbstract, {
  8797. at: node
  8798. });
  8799. }
  8800. if (node.accessibility) {
  8801. this.raise(TSErrors.PrivateElementHasAccessibility, {
  8802. at: node,
  8803. modifier: node.accessibility
  8804. });
  8805. }
  8806. this.parseClassPropertyAnnotation(node);
  8807. return super.parseClassPrivateProperty(node);
  8808. }
  8809. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  8810. const typeParameters = this.tsTryParseTypeParameters();
  8811. if (typeParameters && isConstructor) {
  8812. this.raise(TSErrors.ConstructorHasTypeParameters, {
  8813. at: typeParameters
  8814. });
  8815. }
  8816. const {
  8817. declare = false,
  8818. kind
  8819. } = method;
  8820. if (declare && (kind === "get" || kind === "set")) {
  8821. this.raise(TSErrors.DeclareAccessor, {
  8822. at: method,
  8823. kind
  8824. });
  8825. }
  8826. if (typeParameters) method.typeParameters = typeParameters;
  8827. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  8828. }
  8829. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  8830. const typeParameters = this.tsTryParseTypeParameters();
  8831. if (typeParameters) method.typeParameters = typeParameters;
  8832. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  8833. }
  8834. declareClassPrivateMethodInScope(node, kind) {
  8835. if (node.type === "TSDeclareMethod") return;
  8836. if (node.type === "MethodDefinition" && !node.value.body) return;
  8837. super.declareClassPrivateMethodInScope(node, kind);
  8838. }
  8839. parseClassSuper(node) {
  8840. super.parseClassSuper(node);
  8841. if (node.superClass && (this.match(47) || this.match(51))) {
  8842. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  8843. }
  8844. if (this.eatContextual(110)) {
  8845. node.implements = this.tsParseHeritageClause("implements");
  8846. }
  8847. }
  8848. parseObjPropValue(prop, ...args) {
  8849. const typeParameters = this.tsTryParseTypeParameters();
  8850. if (typeParameters) prop.typeParameters = typeParameters;
  8851. super.parseObjPropValue(prop, ...args);
  8852. }
  8853. parseFunctionParams(node, allowModifiers) {
  8854. const typeParameters = this.tsTryParseTypeParameters();
  8855. if (typeParameters) node.typeParameters = typeParameters;
  8856. super.parseFunctionParams(node, allowModifiers);
  8857. }
  8858. parseVarId(decl, kind) {
  8859. super.parseVarId(decl, kind);
  8860. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  8861. decl.definite = true;
  8862. }
  8863. const type = this.tsTryParseTypeAnnotation();
  8864. if (type) {
  8865. decl.id.typeAnnotation = type;
  8866. this.resetEndLocation(decl.id);
  8867. }
  8868. }
  8869. parseAsyncArrowFromCallExpression(node, call) {
  8870. if (this.match(14)) {
  8871. node.returnType = this.tsParseTypeAnnotation();
  8872. }
  8873. return super.parseAsyncArrowFromCallExpression(node, call);
  8874. }
  8875. parseMaybeAssign(...args) {
  8876. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  8877. let state;
  8878. let jsx;
  8879. let typeCast;
  8880. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  8881. state = this.state.clone();
  8882. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8883. if (!jsx.error) return jsx.node;
  8884. const {
  8885. context
  8886. } = this.state;
  8887. const currentContext = context[context.length - 1];
  8888. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  8889. context.pop();
  8890. }
  8891. }
  8892. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  8893. return super.parseMaybeAssign(...args);
  8894. }
  8895. let typeParameters;
  8896. state = state || this.state.clone();
  8897. const arrow = this.tryParse(abort => {
  8898. var _expr$extra, _typeParameters, _expr$typeParameters$;
  8899. typeParameters = this.tsParseTypeParameters();
  8900. const expr = super.parseMaybeAssign(...args);
  8901. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  8902. abort();
  8903. }
  8904. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  8905. this.resetStartLocationFromNode(expr, typeParameters);
  8906. }
  8907. expr.typeParameters = typeParameters;
  8908. if (this.hasPlugin("jsx") && expr.typeParameters.params.length === 1 && !((_expr$typeParameters$ = expr.typeParameters.extra) != null && _expr$typeParameters$.trailingComma)) {
  8909. const parameter = expr.typeParameters.params[0];
  8910. if (!parameter.constraint) ;
  8911. }
  8912. return expr;
  8913. }, state);
  8914. if (!arrow.error && !arrow.aborted) {
  8915. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8916. return arrow.node;
  8917. }
  8918. if (!jsx) {
  8919. assert(!this.hasPlugin("jsx"));
  8920. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8921. if (!typeCast.error) return typeCast.node;
  8922. }
  8923. if ((_jsx2 = jsx) != null && _jsx2.node) {
  8924. this.state = jsx.failState;
  8925. return jsx.node;
  8926. }
  8927. if (arrow.node) {
  8928. this.state = arrow.failState;
  8929. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8930. return arrow.node;
  8931. }
  8932. if ((_typeCast = typeCast) != null && _typeCast.node) {
  8933. this.state = typeCast.failState;
  8934. return typeCast.node;
  8935. }
  8936. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  8937. if (arrow.thrown) throw arrow.error;
  8938. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  8939. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  8940. }
  8941. reportReservedArrowTypeParam(node) {
  8942. var _node$extra;
  8943. if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8944. this.raise(TSErrors.ReservedArrowTypeParam, {
  8945. at: node
  8946. });
  8947. }
  8948. }
  8949. parseMaybeUnary(refExpressionErrors) {
  8950. if (!this.hasPlugin("jsx") && this.match(47)) {
  8951. return this.tsParseTypeAssertion();
  8952. } else {
  8953. return super.parseMaybeUnary(refExpressionErrors);
  8954. }
  8955. }
  8956. parseArrow(node) {
  8957. if (this.match(14)) {
  8958. const result = this.tryParse(abort => {
  8959. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8960. if (this.canInsertSemicolon() || !this.match(19)) abort();
  8961. return returnType;
  8962. });
  8963. if (result.aborted) return;
  8964. if (!result.thrown) {
  8965. if (result.error) this.state = result.failState;
  8966. node.returnType = result.node;
  8967. }
  8968. }
  8969. return super.parseArrow(node);
  8970. }
  8971. parseAssignableListItemTypes(param) {
  8972. if (this.eat(17)) {
  8973. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  8974. this.raise(TSErrors.PatternIsOptional, {
  8975. at: param
  8976. });
  8977. }
  8978. param.optional = true;
  8979. }
  8980. const type = this.tsTryParseTypeAnnotation();
  8981. if (type) param.typeAnnotation = type;
  8982. this.resetEndLocation(param);
  8983. return param;
  8984. }
  8985. isAssignable(node, isBinding) {
  8986. switch (node.type) {
  8987. case "TSTypeCastExpression":
  8988. return this.isAssignable(node.expression, isBinding);
  8989. case "TSParameterProperty":
  8990. return true;
  8991. default:
  8992. return super.isAssignable(node, isBinding);
  8993. }
  8994. }
  8995. toAssignable(node, isLHS = false) {
  8996. switch (node.type) {
  8997. case "ParenthesizedExpression":
  8998. this.toAssignableParenthesizedExpression(node, isLHS);
  8999. break;
  9000. case "TSAsExpression":
  9001. case "TSNonNullExpression":
  9002. case "TSTypeAssertion":
  9003. if (isLHS) {
  9004. this.expressionScope.recordArrowParemeterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9005. at: node
  9006. });
  9007. } else {
  9008. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9009. at: node
  9010. });
  9011. }
  9012. this.toAssignable(node.expression, isLHS);
  9013. break;
  9014. case "AssignmentExpression":
  9015. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9016. node.left = this.typeCastToParameter(node.left);
  9017. }
  9018. default:
  9019. super.toAssignable(node, isLHS);
  9020. }
  9021. }
  9022. toAssignableParenthesizedExpression(node, isLHS) {
  9023. switch (node.expression.type) {
  9024. case "TSAsExpression":
  9025. case "TSNonNullExpression":
  9026. case "TSTypeAssertion":
  9027. case "ParenthesizedExpression":
  9028. this.toAssignable(node.expression, isLHS);
  9029. break;
  9030. default:
  9031. super.toAssignable(node, isLHS);
  9032. }
  9033. }
  9034. checkToRestConversion(node, allowPattern) {
  9035. switch (node.type) {
  9036. case "TSAsExpression":
  9037. case "TSTypeAssertion":
  9038. case "TSNonNullExpression":
  9039. this.checkToRestConversion(node.expression, false);
  9040. break;
  9041. default:
  9042. super.checkToRestConversion(node, allowPattern);
  9043. }
  9044. }
  9045. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9046. return getOwn$1({
  9047. TSTypeCastExpression: true,
  9048. TSParameterProperty: "parameter",
  9049. TSNonNullExpression: "expression",
  9050. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9051. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9052. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9053. }
  9054. parseBindingAtom() {
  9055. switch (this.state.type) {
  9056. case 78:
  9057. return this.parseIdentifier(true);
  9058. default:
  9059. return super.parseBindingAtom();
  9060. }
  9061. }
  9062. parseMaybeDecoratorArguments(expr) {
  9063. if (this.match(47) || this.match(51)) {
  9064. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9065. if (this.match(10)) {
  9066. const call = super.parseMaybeDecoratorArguments(expr);
  9067. call.typeParameters = typeArguments;
  9068. return call;
  9069. }
  9070. this.unexpected(null, 10);
  9071. }
  9072. return super.parseMaybeDecoratorArguments(expr);
  9073. }
  9074. checkCommaAfterRest(close) {
  9075. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9076. this.next();
  9077. return false;
  9078. } else {
  9079. return super.checkCommaAfterRest(close);
  9080. }
  9081. }
  9082. isClassMethod() {
  9083. return this.match(47) || super.isClassMethod();
  9084. }
  9085. isClassProperty() {
  9086. return this.match(35) || this.match(14) || super.isClassProperty();
  9087. }
  9088. parseMaybeDefault(...args) {
  9089. const node = super.parseMaybeDefault(...args);
  9090. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9091. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9092. at: node.typeAnnotation
  9093. });
  9094. }
  9095. return node;
  9096. }
  9097. getTokenFromCode(code) {
  9098. if (this.state.inType) {
  9099. if (code === 62) {
  9100. return this.finishOp(48, 1);
  9101. }
  9102. if (code === 60) {
  9103. return this.finishOp(47, 1);
  9104. }
  9105. }
  9106. return super.getTokenFromCode(code);
  9107. }
  9108. reScan_lt_gt() {
  9109. const {
  9110. type
  9111. } = this.state;
  9112. if (type === 47) {
  9113. this.state.pos -= 1;
  9114. this.readToken_lt();
  9115. } else if (type === 48) {
  9116. this.state.pos -= 1;
  9117. this.readToken_gt();
  9118. }
  9119. }
  9120. reScan_lt() {
  9121. const {
  9122. type
  9123. } = this.state;
  9124. if (type === 51) {
  9125. this.state.pos -= 2;
  9126. this.finishOp(47, 1);
  9127. return 47;
  9128. }
  9129. return type;
  9130. }
  9131. toAssignableList(exprList) {
  9132. for (let i = 0; i < exprList.length; i++) {
  9133. const expr = exprList[i];
  9134. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9135. exprList[i] = this.typeCastToParameter(expr);
  9136. }
  9137. }
  9138. super.toAssignableList(...arguments);
  9139. }
  9140. typeCastToParameter(node) {
  9141. node.expression.typeAnnotation = node.typeAnnotation;
  9142. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9143. return node.expression;
  9144. }
  9145. shouldParseArrow(params) {
  9146. if (this.match(14)) {
  9147. return params.every(expr => this.isAssignable(expr, true));
  9148. }
  9149. return super.shouldParseArrow(params);
  9150. }
  9151. shouldParseAsyncArrow() {
  9152. return this.match(14) || super.shouldParseAsyncArrow();
  9153. }
  9154. canHaveLeadingDecorator() {
  9155. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9156. }
  9157. jsxParseOpeningElementAfterName(node) {
  9158. if (this.match(47) || this.match(51)) {
  9159. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9160. if (typeArguments) node.typeParameters = typeArguments;
  9161. }
  9162. return super.jsxParseOpeningElementAfterName(node);
  9163. }
  9164. getGetterSetterExpectedParamCount(method) {
  9165. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9166. const params = this.getObjectOrClassMethodParams(method);
  9167. const firstParam = params[0];
  9168. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9169. return hasContextParam ? baseCount + 1 : baseCount;
  9170. }
  9171. parseCatchClauseParam() {
  9172. const param = super.parseCatchClauseParam();
  9173. const type = this.tsTryParseTypeAnnotation();
  9174. if (type) {
  9175. param.typeAnnotation = type;
  9176. this.resetEndLocation(param);
  9177. }
  9178. return param;
  9179. }
  9180. tsInAmbientContext(cb) {
  9181. const oldIsAmbientContext = this.state.isAmbientContext;
  9182. this.state.isAmbientContext = true;
  9183. try {
  9184. return cb();
  9185. } finally {
  9186. this.state.isAmbientContext = oldIsAmbientContext;
  9187. }
  9188. }
  9189. parseClass(node, ...args) {
  9190. const oldInAbstractClass = this.state.inAbstractClass;
  9191. this.state.inAbstractClass = !!node.abstract;
  9192. try {
  9193. return super.parseClass(node, ...args);
  9194. } finally {
  9195. this.state.inAbstractClass = oldInAbstractClass;
  9196. }
  9197. }
  9198. tsParseAbstractDeclaration(node) {
  9199. if (this.match(80)) {
  9200. node.abstract = true;
  9201. return this.parseClass(node, true, false);
  9202. } else if (this.isContextual(125)) {
  9203. if (!this.hasFollowingLineBreak()) {
  9204. node.abstract = true;
  9205. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9206. at: node
  9207. });
  9208. return this.tsParseInterfaceDeclaration(node);
  9209. }
  9210. } else {
  9211. this.unexpected(null, 80);
  9212. }
  9213. }
  9214. parseMethod(...args) {
  9215. const method = super.parseMethod(...args);
  9216. if (method.abstract) {
  9217. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9218. if (hasBody) {
  9219. const {
  9220. key
  9221. } = method;
  9222. this.raise(TSErrors.AbstractMethodHasImplementation, {
  9223. at: method,
  9224. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9225. });
  9226. }
  9227. }
  9228. return method;
  9229. }
  9230. tsParseTypeParameterName() {
  9231. const typeName = this.parseIdentifier();
  9232. return typeName.name;
  9233. }
  9234. shouldParseAsAmbientContext() {
  9235. return !!this.getPluginOption("typescript", "dts");
  9236. }
  9237. parse() {
  9238. if (this.shouldParseAsAmbientContext()) {
  9239. this.state.isAmbientContext = true;
  9240. }
  9241. return super.parse();
  9242. }
  9243. getExpression() {
  9244. if (this.shouldParseAsAmbientContext()) {
  9245. this.state.isAmbientContext = true;
  9246. }
  9247. return super.getExpression();
  9248. }
  9249. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9250. if (!isString && isMaybeTypeOnly) {
  9251. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9252. return this.finishNode(node, "ExportSpecifier");
  9253. }
  9254. node.exportKind = "value";
  9255. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9256. }
  9257. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  9258. if (!importedIsString && isMaybeTypeOnly) {
  9259. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9260. return this.finishNode(specifier, "ImportSpecifier");
  9261. }
  9262. specifier.importKind = "value";
  9263. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly);
  9264. }
  9265. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9266. const leftOfAsKey = isImport ? "imported" : "local";
  9267. const rightOfAsKey = isImport ? "local" : "exported";
  9268. let leftOfAs = node[leftOfAsKey];
  9269. let rightOfAs;
  9270. let hasTypeSpecifier = false;
  9271. let canParseAsKeyword = true;
  9272. const loc = leftOfAs.loc.start;
  9273. if (this.isContextual(93)) {
  9274. const firstAs = this.parseIdentifier();
  9275. if (this.isContextual(93)) {
  9276. const secondAs = this.parseIdentifier();
  9277. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9278. hasTypeSpecifier = true;
  9279. leftOfAs = firstAs;
  9280. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9281. canParseAsKeyword = false;
  9282. } else {
  9283. rightOfAs = secondAs;
  9284. canParseAsKeyword = false;
  9285. }
  9286. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9287. canParseAsKeyword = false;
  9288. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9289. } else {
  9290. hasTypeSpecifier = true;
  9291. leftOfAs = firstAs;
  9292. }
  9293. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9294. hasTypeSpecifier = true;
  9295. if (isImport) {
  9296. leftOfAs = this.parseIdentifier(true);
  9297. if (!this.isContextual(93)) {
  9298. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  9299. }
  9300. } else {
  9301. leftOfAs = this.parseModuleExportName();
  9302. }
  9303. }
  9304. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  9305. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  9306. at: loc
  9307. });
  9308. }
  9309. node[leftOfAsKey] = leftOfAs;
  9310. node[rightOfAsKey] = rightOfAs;
  9311. const kindKey = isImport ? "importKind" : "exportKind";
  9312. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  9313. if (canParseAsKeyword && this.eatContextual(93)) {
  9314. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9315. }
  9316. if (!node[rightOfAsKey]) {
  9317. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  9318. }
  9319. if (isImport) {
  9320. this.checkIdentifier(node[rightOfAsKey], BIND_LEXICAL);
  9321. }
  9322. }
  9323. });
  9324. function isPossiblyLiteralEnum(expression) {
  9325. if (expression.type !== "MemberExpression") return false;
  9326. const {
  9327. computed,
  9328. property
  9329. } = expression;
  9330. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  9331. return false;
  9332. }
  9333. return isUncomputedMemberExpressionChain(expression.object);
  9334. }
  9335. function isUncomputedMemberExpressionChain(expression) {
  9336. if (expression.type === "Identifier") return true;
  9337. if (expression.type !== "MemberExpression") return false;
  9338. if (expression.computed) return false;
  9339. return isUncomputedMemberExpressionChain(expression.object);
  9340. }
  9341. const PlaceholderErrors = ParseErrorEnum`placeholders`(_ => ({
  9342. ClassNameIsRequired: _("A class name is required."),
  9343. UnexpectedSpace: _("Unexpected space in placeholder.")
  9344. }));
  9345. var placeholders = (superClass => class extends superClass {
  9346. parsePlaceholder(expectedNode) {
  9347. if (this.match(140)) {
  9348. const node = this.startNode();
  9349. this.next();
  9350. this.assertNoSpace();
  9351. node.name = super.parseIdentifier(true);
  9352. this.assertNoSpace();
  9353. this.expect(140);
  9354. return this.finishPlaceholder(node, expectedNode);
  9355. }
  9356. }
  9357. finishPlaceholder(node, expectedNode) {
  9358. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  9359. node.expectedNode = expectedNode;
  9360. return isFinished ? node : this.finishNode(node, "Placeholder");
  9361. }
  9362. getTokenFromCode(code) {
  9363. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  9364. return this.finishOp(140, 2);
  9365. }
  9366. return super.getTokenFromCode(...arguments);
  9367. }
  9368. parseExprAtom() {
  9369. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  9370. }
  9371. parseIdentifier() {
  9372. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  9373. }
  9374. checkReservedWord(word) {
  9375. if (word !== undefined) super.checkReservedWord(...arguments);
  9376. }
  9377. parseBindingAtom() {
  9378. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  9379. }
  9380. isValidLVal(type, ...rest) {
  9381. return type === "Placeholder" || super.isValidLVal(type, ...rest);
  9382. }
  9383. toAssignable(node) {
  9384. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  9385. node.expectedNode = "Pattern";
  9386. } else {
  9387. super.toAssignable(...arguments);
  9388. }
  9389. }
  9390. isLet(context) {
  9391. if (super.isLet(context)) {
  9392. return true;
  9393. }
  9394. if (!this.isContextual(99)) {
  9395. return false;
  9396. }
  9397. if (context) return false;
  9398. const nextToken = this.lookahead();
  9399. if (nextToken.type === 140) {
  9400. return true;
  9401. }
  9402. return false;
  9403. }
  9404. verifyBreakContinue(node) {
  9405. if (node.label && node.label.type === "Placeholder") return;
  9406. super.verifyBreakContinue(...arguments);
  9407. }
  9408. parseExpressionStatement(node, expr) {
  9409. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  9410. return super.parseExpressionStatement(...arguments);
  9411. }
  9412. if (this.match(14)) {
  9413. const stmt = node;
  9414. stmt.label = this.finishPlaceholder(expr, "Identifier");
  9415. this.next();
  9416. stmt.body = this.parseStatement("label");
  9417. return this.finishNode(stmt, "LabeledStatement");
  9418. }
  9419. this.semicolon();
  9420. node.name = expr.name;
  9421. return this.finishPlaceholder(node, "Statement");
  9422. }
  9423. parseBlock() {
  9424. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  9425. }
  9426. parseFunctionId() {
  9427. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  9428. }
  9429. parseClass(node, isStatement, optionalId) {
  9430. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  9431. this.next();
  9432. this.takeDecorators(node);
  9433. const oldStrict = this.state.strict;
  9434. const placeholder = this.parsePlaceholder("Identifier");
  9435. if (placeholder) {
  9436. if (this.match(81) || this.match(140) || this.match(5)) {
  9437. node.id = placeholder;
  9438. } else if (optionalId || !isStatement) {
  9439. node.id = null;
  9440. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  9441. return this.finishNode(node, type);
  9442. } else {
  9443. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  9444. at: this.state.startLoc
  9445. });
  9446. }
  9447. } else {
  9448. this.parseClassId(node, isStatement, optionalId);
  9449. }
  9450. this.parseClassSuper(node);
  9451. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  9452. return this.finishNode(node, type);
  9453. }
  9454. parseExport(node) {
  9455. const placeholder = this.parsePlaceholder("Identifier");
  9456. if (!placeholder) return super.parseExport(...arguments);
  9457. if (!this.isContextual(97) && !this.match(12)) {
  9458. node.specifiers = [];
  9459. node.source = null;
  9460. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  9461. return this.finishNode(node, "ExportNamedDeclaration");
  9462. }
  9463. this.expectPlugin("exportDefaultFrom");
  9464. const specifier = this.startNode();
  9465. specifier.exported = placeholder;
  9466. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9467. return super.parseExport(node);
  9468. }
  9469. isExportDefaultSpecifier() {
  9470. if (this.match(65)) {
  9471. const next = this.nextTokenStart();
  9472. if (this.isUnparsedContextual(next, "from")) {
  9473. if (this.input.startsWith(tokenLabelName(140), this.nextTokenStartSince(next + 4))) {
  9474. return true;
  9475. }
  9476. }
  9477. }
  9478. return super.isExportDefaultSpecifier();
  9479. }
  9480. maybeParseExportDefaultSpecifier(node) {
  9481. if (node.specifiers && node.specifiers.length > 0) {
  9482. return true;
  9483. }
  9484. return super.maybeParseExportDefaultSpecifier(...arguments);
  9485. }
  9486. checkExport(node) {
  9487. const {
  9488. specifiers
  9489. } = node;
  9490. if (specifiers != null && specifiers.length) {
  9491. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  9492. }
  9493. super.checkExport(node);
  9494. node.specifiers = specifiers;
  9495. }
  9496. parseImport(node) {
  9497. const placeholder = this.parsePlaceholder("Identifier");
  9498. if (!placeholder) return super.parseImport(...arguments);
  9499. node.specifiers = [];
  9500. if (!this.isContextual(97) && !this.match(12)) {
  9501. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  9502. this.semicolon();
  9503. return this.finishNode(node, "ImportDeclaration");
  9504. }
  9505. const specifier = this.startNodeAtNode(placeholder);
  9506. specifier.local = placeholder;
  9507. this.finishNode(specifier, "ImportDefaultSpecifier");
  9508. node.specifiers.push(specifier);
  9509. if (this.eat(12)) {
  9510. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  9511. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  9512. }
  9513. this.expectContextual(97);
  9514. node.source = this.parseImportSource();
  9515. this.semicolon();
  9516. return this.finishNode(node, "ImportDeclaration");
  9517. }
  9518. parseImportSource() {
  9519. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  9520. }
  9521. assertNoSpace() {
  9522. if (this.state.start > this.state.lastTokEndLoc.index) {
  9523. this.raise(PlaceholderErrors.UnexpectedSpace, {
  9524. at: this.state.lastTokEndLoc
  9525. });
  9526. }
  9527. }
  9528. });
  9529. var v8intrinsic = (superClass => class extends superClass {
  9530. parseV8Intrinsic() {
  9531. if (this.match(54)) {
  9532. const v8IntrinsicStartLoc = this.state.startLoc;
  9533. const node = this.startNode();
  9534. this.next();
  9535. if (tokenIsIdentifier(this.state.type)) {
  9536. const name = this.parseIdentifierName(this.state.start);
  9537. const identifier = this.createIdentifier(node, name);
  9538. identifier.type = "V8IntrinsicIdentifier";
  9539. if (this.match(10)) {
  9540. return identifier;
  9541. }
  9542. }
  9543. this.unexpected(v8IntrinsicStartLoc);
  9544. }
  9545. }
  9546. parseExprAtom() {
  9547. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  9548. }
  9549. });
  9550. function hasPlugin(plugins, expectedConfig) {
  9551. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  9552. const expectedKeys = Object.keys(expectedOptions);
  9553. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  9554. return plugins.some(p => {
  9555. if (typeof p === "string") {
  9556. return expectedOptionsIsEmpty && p === expectedName;
  9557. } else {
  9558. const [pluginName, pluginOptions] = p;
  9559. if (pluginName !== expectedName) {
  9560. return false;
  9561. }
  9562. for (const key of expectedKeys) {
  9563. if (pluginOptions[key] !== expectedOptions[key]) {
  9564. return false;
  9565. }
  9566. }
  9567. return true;
  9568. }
  9569. });
  9570. }
  9571. function getPluginOption(plugins, name, option) {
  9572. const plugin = plugins.find(plugin => {
  9573. if (Array.isArray(plugin)) {
  9574. return plugin[0] === name;
  9575. } else {
  9576. return plugin === name;
  9577. }
  9578. });
  9579. if (plugin && Array.isArray(plugin)) {
  9580. return plugin[1][option];
  9581. }
  9582. return null;
  9583. }
  9584. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  9585. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  9586. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  9587. function validatePlugins(plugins) {
  9588. if (hasPlugin(plugins, "decorators")) {
  9589. if (hasPlugin(plugins, "decorators-legacy")) {
  9590. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  9591. }
  9592. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  9593. if (decoratorsBeforeExport == null) {
  9594. 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'.");
  9595. } else if (typeof decoratorsBeforeExport !== "boolean") {
  9596. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  9597. }
  9598. }
  9599. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  9600. throw new Error("Cannot combine flow and typescript plugins.");
  9601. }
  9602. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  9603. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  9604. }
  9605. if (hasPlugin(plugins, "pipelineOperator")) {
  9606. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  9607. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  9608. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  9609. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  9610. }
  9611. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  9612. syntaxType: "hash"
  9613. }]);
  9614. if (proposal === "hack") {
  9615. if (hasPlugin(plugins, "placeholders")) {
  9616. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  9617. }
  9618. if (hasPlugin(plugins, "v8intrinsic")) {
  9619. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  9620. }
  9621. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  9622. if (!TOPIC_TOKENS.includes(topicToken)) {
  9623. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  9624. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  9625. }
  9626. if (topicToken === "#" && tupleSyntaxIsHash) {
  9627. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9628. }
  9629. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  9630. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9631. }
  9632. }
  9633. if (hasPlugin(plugins, "moduleAttributes")) {
  9634. {
  9635. if (hasPlugin(plugins, "importAssertions")) {
  9636. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  9637. }
  9638. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  9639. if (moduleAttributesVerionPluginOption !== "may-2020") {
  9640. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  9641. }
  9642. }
  9643. }
  9644. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  9645. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  9646. }
  9647. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  9648. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  9649. error.missingPlugins = "doExpressions";
  9650. throw error;
  9651. }
  9652. }
  9653. const mixinPlugins = {
  9654. estree,
  9655. jsx,
  9656. flow,
  9657. typescript,
  9658. v8intrinsic,
  9659. placeholders
  9660. };
  9661. const mixinPluginNames = Object.keys(mixinPlugins);
  9662. const defaultOptions = {
  9663. sourceType: "script",
  9664. sourceFilename: undefined,
  9665. startColumn: 0,
  9666. startLine: 1,
  9667. allowAwaitOutsideFunction: false,
  9668. allowReturnOutsideFunction: false,
  9669. allowImportExportEverywhere: false,
  9670. allowSuperOutsideMethod: false,
  9671. allowUndeclaredExports: false,
  9672. plugins: [],
  9673. strictMode: null,
  9674. ranges: false,
  9675. tokens: false,
  9676. createParenthesizedExpressions: false,
  9677. errorRecovery: false,
  9678. attachComment: true
  9679. };
  9680. function getOptions(opts) {
  9681. const options = {};
  9682. for (const key of Object.keys(defaultOptions)) {
  9683. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  9684. }
  9685. return options;
  9686. }
  9687. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  9688. const unwrapParenthesizedExpression = node => {
  9689. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  9690. };
  9691. class LValParser extends NodeUtils {
  9692. toAssignable(node, isLHS = false) {
  9693. var _node$extra, _node$extra3;
  9694. let parenthesized = undefined;
  9695. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  9696. parenthesized = unwrapParenthesizedExpression(node);
  9697. if (isLHS) {
  9698. if (parenthesized.type === "Identifier") {
  9699. this.expressionScope.recordArrowParemeterBindingError(Errors.InvalidParenthesizedAssignment, {
  9700. at: node
  9701. });
  9702. } else if (parenthesized.type !== "MemberExpression") {
  9703. this.raise(Errors.InvalidParenthesizedAssignment, {
  9704. at: node
  9705. });
  9706. }
  9707. } else {
  9708. this.raise(Errors.InvalidParenthesizedAssignment, {
  9709. at: node
  9710. });
  9711. }
  9712. }
  9713. switch (node.type) {
  9714. case "Identifier":
  9715. case "ObjectPattern":
  9716. case "ArrayPattern":
  9717. case "AssignmentPattern":
  9718. case "RestElement":
  9719. break;
  9720. case "ObjectExpression":
  9721. node.type = "ObjectPattern";
  9722. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  9723. var _node$extra2;
  9724. const prop = node.properties[i];
  9725. const isLast = i === last;
  9726. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  9727. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  9728. this.raise(Errors.RestTrailingComma, {
  9729. at: node.extra.trailingCommaLoc
  9730. });
  9731. }
  9732. }
  9733. break;
  9734. case "ObjectProperty":
  9735. {
  9736. const {
  9737. key,
  9738. value
  9739. } = node;
  9740. if (this.isPrivateName(key)) {
  9741. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  9742. }
  9743. this.toAssignable(value, isLHS);
  9744. break;
  9745. }
  9746. case "SpreadElement":
  9747. {
  9748. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  9749. }
  9750. case "ArrayExpression":
  9751. node.type = "ArrayPattern";
  9752. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  9753. break;
  9754. case "AssignmentExpression":
  9755. if (node.operator !== "=") {
  9756. this.raise(Errors.MissingEqInAssignment, {
  9757. at: node.left.loc.end
  9758. });
  9759. }
  9760. node.type = "AssignmentPattern";
  9761. delete node.operator;
  9762. this.toAssignable(node.left, isLHS);
  9763. break;
  9764. case "ParenthesizedExpression":
  9765. this.toAssignable(parenthesized, isLHS);
  9766. break;
  9767. }
  9768. }
  9769. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  9770. if (prop.type === "ObjectMethod") {
  9771. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  9772. at: prop.key
  9773. });
  9774. } else if (prop.type === "SpreadElement") {
  9775. prop.type = "RestElement";
  9776. const arg = prop.argument;
  9777. this.checkToRestConversion(arg, false);
  9778. this.toAssignable(arg, isLHS);
  9779. if (!isLast) {
  9780. this.raise(Errors.RestTrailingComma, {
  9781. at: prop
  9782. });
  9783. }
  9784. } else {
  9785. this.toAssignable(prop, isLHS);
  9786. }
  9787. }
  9788. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9789. const end = exprList.length - 1;
  9790. for (let i = 0; i <= end; i++) {
  9791. const elt = exprList[i];
  9792. if (!elt) continue;
  9793. if (elt.type === "SpreadElement") {
  9794. elt.type = "RestElement";
  9795. const arg = elt.argument;
  9796. this.checkToRestConversion(arg, true);
  9797. this.toAssignable(arg, isLHS);
  9798. } else {
  9799. this.toAssignable(elt, isLHS);
  9800. }
  9801. if (elt.type === "RestElement") {
  9802. if (i < end) {
  9803. this.raise(Errors.RestTrailingComma, {
  9804. at: elt
  9805. });
  9806. } else if (trailingCommaLoc) {
  9807. this.raise(Errors.RestTrailingComma, {
  9808. at: trailingCommaLoc
  9809. });
  9810. }
  9811. }
  9812. }
  9813. }
  9814. isAssignable(node, isBinding) {
  9815. switch (node.type) {
  9816. case "Identifier":
  9817. case "ObjectPattern":
  9818. case "ArrayPattern":
  9819. case "AssignmentPattern":
  9820. case "RestElement":
  9821. return true;
  9822. case "ObjectExpression":
  9823. {
  9824. const last = node.properties.length - 1;
  9825. return node.properties.every((prop, i) => {
  9826. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  9827. });
  9828. }
  9829. case "ObjectProperty":
  9830. return this.isAssignable(node.value);
  9831. case "SpreadElement":
  9832. return this.isAssignable(node.argument);
  9833. case "ArrayExpression":
  9834. return node.elements.every(element => element === null || this.isAssignable(element));
  9835. case "AssignmentExpression":
  9836. return node.operator === "=";
  9837. case "ParenthesizedExpression":
  9838. return this.isAssignable(node.expression);
  9839. case "MemberExpression":
  9840. case "OptionalMemberExpression":
  9841. return !isBinding;
  9842. default:
  9843. return false;
  9844. }
  9845. }
  9846. toReferencedList(exprList, isParenthesizedExpr) {
  9847. return exprList;
  9848. }
  9849. toReferencedListDeep(exprList, isParenthesizedExpr) {
  9850. this.toReferencedList(exprList, isParenthesizedExpr);
  9851. for (const expr of exprList) {
  9852. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  9853. this.toReferencedListDeep(expr.elements);
  9854. }
  9855. }
  9856. }
  9857. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  9858. const node = this.startNode();
  9859. this.next();
  9860. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  9861. return this.finishNode(node, "SpreadElement");
  9862. }
  9863. parseRestBinding() {
  9864. const node = this.startNode();
  9865. this.next();
  9866. node.argument = this.parseBindingAtom();
  9867. return this.finishNode(node, "RestElement");
  9868. }
  9869. parseBindingAtom() {
  9870. switch (this.state.type) {
  9871. case 0:
  9872. {
  9873. const node = this.startNode();
  9874. this.next();
  9875. node.elements = this.parseBindingList(3, 93, true);
  9876. return this.finishNode(node, "ArrayPattern");
  9877. }
  9878. case 5:
  9879. return this.parseObjectLike(8, true);
  9880. }
  9881. return this.parseIdentifier();
  9882. }
  9883. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  9884. const elts = [];
  9885. let first = true;
  9886. while (!this.eat(close)) {
  9887. if (first) {
  9888. first = false;
  9889. } else {
  9890. this.expect(12);
  9891. }
  9892. if (allowEmpty && this.match(12)) {
  9893. elts.push(null);
  9894. } else if (this.eat(close)) {
  9895. break;
  9896. } else if (this.match(21)) {
  9897. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  9898. if (!this.checkCommaAfterRest(closeCharCode)) {
  9899. this.expect(close);
  9900. break;
  9901. }
  9902. } else {
  9903. const decorators = [];
  9904. if (this.match(26) && this.hasPlugin("decorators")) {
  9905. this.raise(Errors.UnsupportedParameterDecorator, {
  9906. at: this.state.startLoc
  9907. });
  9908. }
  9909. while (this.match(26)) {
  9910. decorators.push(this.parseDecorator());
  9911. }
  9912. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  9913. }
  9914. }
  9915. return elts;
  9916. }
  9917. parseBindingRestProperty(prop) {
  9918. this.next();
  9919. prop.argument = this.parseIdentifier();
  9920. this.checkCommaAfterRest(125);
  9921. return this.finishNode(prop, "RestElement");
  9922. }
  9923. parseBindingProperty() {
  9924. const prop = this.startNode();
  9925. const {
  9926. type,
  9927. start: startPos,
  9928. startLoc
  9929. } = this.state;
  9930. if (type === 21) {
  9931. return this.parseBindingRestProperty(prop);
  9932. } else if (type === 134) {
  9933. this.expectPlugin("destructuringPrivate", startLoc);
  9934. this.classScope.usePrivateName(this.state.value, startLoc);
  9935. prop.key = this.parsePrivateName();
  9936. } else {
  9937. this.parsePropertyName(prop);
  9938. }
  9939. prop.method = false;
  9940. this.parseObjPropValue(prop, startPos, startLoc, false, false, true, false);
  9941. return prop;
  9942. }
  9943. parseAssignableListItem(allowModifiers, decorators) {
  9944. const left = this.parseMaybeDefault();
  9945. this.parseAssignableListItemTypes(left);
  9946. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  9947. if (decorators.length) {
  9948. left.decorators = decorators;
  9949. }
  9950. return elt;
  9951. }
  9952. parseAssignableListItemTypes(param) {
  9953. return param;
  9954. }
  9955. parseMaybeDefault(startPos, startLoc, left) {
  9956. var _startLoc, _startPos, _left;
  9957. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  9958. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  9959. left = (_left = left) != null ? _left : this.parseBindingAtom();
  9960. if (!this.eat(29)) return left;
  9961. const node = this.startNodeAt(startPos, startLoc);
  9962. node.left = left;
  9963. node.right = this.parseMaybeAssignAllowIn();
  9964. return this.finishNode(node, "AssignmentPattern");
  9965. }
  9966. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9967. return getOwn({
  9968. AssignmentPattern: "left",
  9969. RestElement: "argument",
  9970. ObjectProperty: "value",
  9971. ParenthesizedExpression: "expression",
  9972. ArrayPattern: "elements",
  9973. ObjectPattern: "properties"
  9974. }, type);
  9975. }
  9976. checkLVal(expression, {
  9977. in: ancestor,
  9978. binding = BIND_NONE,
  9979. checkClashes = false,
  9980. strictModeChanged = false,
  9981. allowingSloppyLetBinding = !(binding & BIND_SCOPE_LEXICAL),
  9982. hasParenthesizedAncestor = false
  9983. }) {
  9984. var _expression$extra;
  9985. const type = expression.type;
  9986. if (this.isObjectMethod(expression)) return;
  9987. if (type === "MemberExpression") {
  9988. if (binding !== BIND_NONE) {
  9989. this.raise(Errors.InvalidPropertyBindingPattern, {
  9990. at: expression
  9991. });
  9992. }
  9993. return;
  9994. }
  9995. if (expression.type === "Identifier") {
  9996. this.checkIdentifier(expression, binding, strictModeChanged, allowingSloppyLetBinding);
  9997. const {
  9998. name
  9999. } = expression;
  10000. if (checkClashes) {
  10001. if (checkClashes.has(name)) {
  10002. this.raise(Errors.ParamDupe, {
  10003. at: expression
  10004. });
  10005. } else {
  10006. checkClashes.add(name);
  10007. }
  10008. }
  10009. return;
  10010. }
  10011. const validity = this.isValidLVal(expression.type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  10012. if (validity === true) return;
  10013. if (validity === false) {
  10014. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  10015. this.raise(ParseErrorClass, {
  10016. at: expression,
  10017. ancestor: ancestor.type === "UpdateExpression" ? {
  10018. type: "UpdateExpression",
  10019. prefix: ancestor.prefix
  10020. } : {
  10021. type: ancestor.type
  10022. }
  10023. });
  10024. return;
  10025. }
  10026. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  10027. const nextAncestor = expression.type === "ArrayPattern" || expression.type === "ObjectPattern" || expression.type === "ParenthesizedExpression" ? expression : ancestor;
  10028. for (const child of [].concat(expression[key])) {
  10029. if (child) {
  10030. this.checkLVal(child, {
  10031. in: nextAncestor,
  10032. binding,
  10033. checkClashes,
  10034. allowingSloppyLetBinding,
  10035. strictModeChanged,
  10036. hasParenthesizedAncestor: isParenthesizedExpression
  10037. });
  10038. }
  10039. }
  10040. }
  10041. checkIdentifier(at, bindingType, strictModeChanged = false, allowLetBinding = !(bindingType & BIND_SCOPE_LEXICAL)) {
  10042. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  10043. if (bindingType === BIND_NONE) {
  10044. this.raise(Errors.StrictEvalArguments, {
  10045. at,
  10046. referenceName: at.name
  10047. });
  10048. } else {
  10049. this.raise(Errors.StrictEvalArgumentsBinding, {
  10050. at,
  10051. bindingName: at.name
  10052. });
  10053. }
  10054. }
  10055. if (!allowLetBinding && at.name === "let") {
  10056. this.raise(Errors.LetInLexicalBinding, {
  10057. at
  10058. });
  10059. }
  10060. if (!(bindingType & BIND_NONE)) {
  10061. this.declareNameFromIdentifier(at, bindingType);
  10062. }
  10063. }
  10064. declareNameFromIdentifier(identifier, binding) {
  10065. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  10066. }
  10067. checkToRestConversion(node, allowPattern) {
  10068. switch (node.type) {
  10069. case "ParenthesizedExpression":
  10070. this.checkToRestConversion(node.expression, allowPattern);
  10071. break;
  10072. case "Identifier":
  10073. case "MemberExpression":
  10074. break;
  10075. case "ArrayExpression":
  10076. case "ObjectExpression":
  10077. if (allowPattern) break;
  10078. default:
  10079. this.raise(Errors.InvalidRestAssignmentPattern, {
  10080. at: node
  10081. });
  10082. }
  10083. }
  10084. checkCommaAfterRest(close) {
  10085. if (!this.match(12)) {
  10086. return false;
  10087. }
  10088. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  10089. at: this.state.startLoc
  10090. });
  10091. return true;
  10092. }
  10093. }
  10094. class ExpressionParser extends LValParser {
  10095. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10096. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10097. return;
  10098. }
  10099. const key = prop.key;
  10100. const name = key.type === "Identifier" ? key.name : key.value;
  10101. if (name === "__proto__") {
  10102. if (isRecord) {
  10103. this.raise(Errors.RecordNoProto, {
  10104. at: key
  10105. });
  10106. return;
  10107. }
  10108. if (protoRef.used) {
  10109. if (refExpressionErrors) {
  10110. if (refExpressionErrors.doubleProtoLoc === null) {
  10111. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10112. }
  10113. } else {
  10114. this.raise(Errors.DuplicateProto, {
  10115. at: key
  10116. });
  10117. }
  10118. }
  10119. protoRef.used = true;
  10120. }
  10121. }
  10122. shouldExitDescending(expr, potentialArrowAt) {
  10123. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10124. }
  10125. getExpression() {
  10126. this.enterInitialScopes();
  10127. this.nextToken();
  10128. const expr = this.parseExpression();
  10129. if (!this.match(135)) {
  10130. this.unexpected();
  10131. }
  10132. this.finalizeRemainingComments();
  10133. expr.comments = this.state.comments;
  10134. expr.errors = this.state.errors;
  10135. if (this.options.tokens) {
  10136. expr.tokens = this.tokens;
  10137. }
  10138. return expr;
  10139. }
  10140. parseExpression(disallowIn, refExpressionErrors) {
  10141. if (disallowIn) {
  10142. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10143. }
  10144. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10145. }
  10146. parseExpressionBase(refExpressionErrors) {
  10147. const startPos = this.state.start;
  10148. const startLoc = this.state.startLoc;
  10149. const expr = this.parseMaybeAssign(refExpressionErrors);
  10150. if (this.match(12)) {
  10151. const node = this.startNodeAt(startPos, startLoc);
  10152. node.expressions = [expr];
  10153. while (this.eat(12)) {
  10154. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10155. }
  10156. this.toReferencedList(node.expressions);
  10157. return this.finishNode(node, "SequenceExpression");
  10158. }
  10159. return expr;
  10160. }
  10161. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10162. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10163. }
  10164. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10165. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10166. }
  10167. setOptionalParametersError(refExpressionErrors, resultError) {
  10168. var _resultError$loc;
  10169. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10170. }
  10171. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10172. const startPos = this.state.start;
  10173. const startLoc = this.state.startLoc;
  10174. if (this.isContextual(105)) {
  10175. if (this.prodParam.hasYield) {
  10176. let left = this.parseYield();
  10177. if (afterLeftParse) {
  10178. left = afterLeftParse.call(this, left, startPos, startLoc);
  10179. }
  10180. return left;
  10181. }
  10182. }
  10183. let ownExpressionErrors;
  10184. if (refExpressionErrors) {
  10185. ownExpressionErrors = false;
  10186. } else {
  10187. refExpressionErrors = new ExpressionErrors();
  10188. ownExpressionErrors = true;
  10189. }
  10190. const {
  10191. type
  10192. } = this.state;
  10193. if (type === 10 || tokenIsIdentifier(type)) {
  10194. this.state.potentialArrowAt = this.state.start;
  10195. }
  10196. let left = this.parseMaybeConditional(refExpressionErrors);
  10197. if (afterLeftParse) {
  10198. left = afterLeftParse.call(this, left, startPos, startLoc);
  10199. }
  10200. if (tokenIsAssignment(this.state.type)) {
  10201. const node = this.startNodeAt(startPos, startLoc);
  10202. const operator = this.state.value;
  10203. node.operator = operator;
  10204. if (this.match(29)) {
  10205. this.toAssignable(left, true);
  10206. node.left = left;
  10207. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startPos) {
  10208. refExpressionErrors.doubleProtoLoc = null;
  10209. }
  10210. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startPos) {
  10211. refExpressionErrors.shorthandAssignLoc = null;
  10212. }
  10213. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startPos) {
  10214. this.checkDestructuringPrivate(refExpressionErrors);
  10215. refExpressionErrors.privateKeyLoc = null;
  10216. }
  10217. } else {
  10218. node.left = left;
  10219. }
  10220. this.next();
  10221. node.right = this.parseMaybeAssign();
  10222. this.checkLVal(left, {
  10223. in: this.finishNode(node, "AssignmentExpression")
  10224. });
  10225. return node;
  10226. } else if (ownExpressionErrors) {
  10227. this.checkExpressionErrors(refExpressionErrors, true);
  10228. }
  10229. return left;
  10230. }
  10231. parseMaybeConditional(refExpressionErrors) {
  10232. const startPos = this.state.start;
  10233. const startLoc = this.state.startLoc;
  10234. const potentialArrowAt = this.state.potentialArrowAt;
  10235. const expr = this.parseExprOps(refExpressionErrors);
  10236. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10237. return expr;
  10238. }
  10239. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  10240. }
  10241. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  10242. if (this.eat(17)) {
  10243. const node = this.startNodeAt(startPos, startLoc);
  10244. node.test = expr;
  10245. node.consequent = this.parseMaybeAssignAllowIn();
  10246. this.expect(14);
  10247. node.alternate = this.parseMaybeAssign();
  10248. return this.finishNode(node, "ConditionalExpression");
  10249. }
  10250. return expr;
  10251. }
  10252. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10253. return this.match(134) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10254. }
  10255. parseExprOps(refExpressionErrors) {
  10256. const startPos = this.state.start;
  10257. const startLoc = this.state.startLoc;
  10258. const potentialArrowAt = this.state.potentialArrowAt;
  10259. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10260. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10261. return expr;
  10262. }
  10263. return this.parseExprOp(expr, startPos, startLoc, -1);
  10264. }
  10265. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  10266. if (this.isPrivateName(left)) {
  10267. const value = this.getPrivateNameSV(left);
  10268. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10269. this.raise(Errors.PrivateInExpectedIn, {
  10270. at: left,
  10271. identifierName: value
  10272. });
  10273. }
  10274. this.classScope.usePrivateName(value, left.loc.start);
  10275. }
  10276. const op = this.state.type;
  10277. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10278. let prec = tokenOperatorPrecedence(op);
  10279. if (prec > minPrec) {
  10280. if (op === 39) {
  10281. this.expectPlugin("pipelineOperator");
  10282. if (this.state.inFSharpPipelineDirectBody) {
  10283. return left;
  10284. }
  10285. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10286. }
  10287. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  10288. node.left = left;
  10289. node.operator = this.state.value;
  10290. const logical = op === 41 || op === 42;
  10291. const coalesce = op === 40;
  10292. if (coalesce) {
  10293. prec = tokenOperatorPrecedence(42);
  10294. }
  10295. this.next();
  10296. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10297. proposal: "minimal"
  10298. }])) {
  10299. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10300. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10301. at: this.state.startLoc
  10302. });
  10303. }
  10304. }
  10305. node.right = this.parseExprOpRightExpr(op, prec);
  10306. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10307. const nextOp = this.state.type;
  10308. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10309. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10310. at: this.state.startLoc
  10311. });
  10312. }
  10313. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  10314. }
  10315. }
  10316. return left;
  10317. }
  10318. parseExprOpRightExpr(op, prec) {
  10319. const startPos = this.state.start;
  10320. const startLoc = this.state.startLoc;
  10321. switch (op) {
  10322. case 39:
  10323. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10324. case "hack":
  10325. return this.withTopicBindingContext(() => {
  10326. return this.parseHackPipeBody();
  10327. });
  10328. case "smart":
  10329. return this.withTopicBindingContext(() => {
  10330. if (this.prodParam.hasYield && this.isContextual(105)) {
  10331. throw this.raise(Errors.PipeBodyIsTighter, {
  10332. at: this.state.startLoc
  10333. });
  10334. }
  10335. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  10336. });
  10337. case "fsharp":
  10338. return this.withSoloAwaitPermittingContext(() => {
  10339. return this.parseFSharpPipelineBody(prec);
  10340. });
  10341. }
  10342. default:
  10343. return this.parseExprOpBaseRightExpr(op, prec);
  10344. }
  10345. }
  10346. parseExprOpBaseRightExpr(op, prec) {
  10347. const startPos = this.state.start;
  10348. const startLoc = this.state.startLoc;
  10349. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10350. }
  10351. parseHackPipeBody() {
  10352. var _body$extra;
  10353. const {
  10354. startLoc
  10355. } = this.state;
  10356. const body = this.parseMaybeAssign();
  10357. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10358. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10359. this.raise(Errors.PipeUnparenthesizedBody, {
  10360. at: startLoc,
  10361. type: body.type
  10362. });
  10363. }
  10364. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10365. this.raise(Errors.PipeTopicUnused, {
  10366. at: startLoc
  10367. });
  10368. }
  10369. return body;
  10370. }
  10371. checkExponentialAfterUnary(node) {
  10372. if (this.match(57)) {
  10373. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10374. at: node.argument
  10375. });
  10376. }
  10377. }
  10378. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10379. const startPos = this.state.start;
  10380. const startLoc = this.state.startLoc;
  10381. const isAwait = this.isContextual(96);
  10382. if (isAwait && this.isAwaitAllowed()) {
  10383. this.next();
  10384. const expr = this.parseAwait(startPos, startLoc);
  10385. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10386. return expr;
  10387. }
  10388. const update = this.match(34);
  10389. const node = this.startNode();
  10390. if (tokenIsPrefix(this.state.type)) {
  10391. node.operator = this.state.value;
  10392. node.prefix = true;
  10393. if (this.match(72)) {
  10394. this.expectPlugin("throwExpressions");
  10395. }
  10396. const isDelete = this.match(89);
  10397. this.next();
  10398. node.argument = this.parseMaybeUnary(null, true);
  10399. this.checkExpressionErrors(refExpressionErrors, true);
  10400. if (this.state.strict && isDelete) {
  10401. const arg = node.argument;
  10402. if (arg.type === "Identifier") {
  10403. this.raise(Errors.StrictDelete, {
  10404. at: node
  10405. });
  10406. } else if (this.hasPropertyAsPrivateName(arg)) {
  10407. this.raise(Errors.DeletePrivateField, {
  10408. at: node
  10409. });
  10410. }
  10411. }
  10412. if (!update) {
  10413. if (!sawUnary) this.checkExponentialAfterUnary(node);
  10414. return this.finishNode(node, "UnaryExpression");
  10415. }
  10416. }
  10417. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10418. if (isAwait) {
  10419. const {
  10420. type
  10421. } = this.state;
  10422. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10423. if (startsExpr && !this.isAmbiguousAwait()) {
  10424. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10425. at: startLoc
  10426. });
  10427. return this.parseAwait(startPos, startLoc);
  10428. }
  10429. }
  10430. return expr;
  10431. }
  10432. parseUpdate(node, update, refExpressionErrors) {
  10433. if (update) {
  10434. this.checkLVal(node.argument, {
  10435. in: this.finishNode(node, "UpdateExpression")
  10436. });
  10437. return node;
  10438. }
  10439. const startPos = this.state.start;
  10440. const startLoc = this.state.startLoc;
  10441. let expr = this.parseExprSubscripts(refExpressionErrors);
  10442. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10443. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10444. const node = this.startNodeAt(startPos, startLoc);
  10445. node.operator = this.state.value;
  10446. node.prefix = false;
  10447. node.argument = expr;
  10448. this.next();
  10449. this.checkLVal(expr, {
  10450. in: expr = this.finishNode(node, "UpdateExpression")
  10451. });
  10452. }
  10453. return expr;
  10454. }
  10455. parseExprSubscripts(refExpressionErrors) {
  10456. const startPos = this.state.start;
  10457. const startLoc = this.state.startLoc;
  10458. const potentialArrowAt = this.state.potentialArrowAt;
  10459. const expr = this.parseExprAtom(refExpressionErrors);
  10460. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10461. return expr;
  10462. }
  10463. return this.parseSubscripts(expr, startPos, startLoc);
  10464. }
  10465. parseSubscripts(base, startPos, startLoc, noCalls) {
  10466. const state = {
  10467. optionalChainMember: false,
  10468. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10469. stop: false
  10470. };
  10471. do {
  10472. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  10473. state.maybeAsyncArrow = false;
  10474. } while (!state.stop);
  10475. return base;
  10476. }
  10477. parseSubscript(base, startPos, startLoc, noCalls, state) {
  10478. const {
  10479. type
  10480. } = this.state;
  10481. if (!noCalls && type === 15) {
  10482. return this.parseBind(base, startPos, startLoc, noCalls, state);
  10483. } else if (tokenIsTemplate(type)) {
  10484. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  10485. }
  10486. let optional = false;
  10487. if (type === 18) {
  10488. if (noCalls && this.lookaheadCharCode() === 40) {
  10489. state.stop = true;
  10490. return base;
  10491. }
  10492. state.optionalChainMember = optional = true;
  10493. this.next();
  10494. }
  10495. if (!noCalls && this.match(10)) {
  10496. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  10497. } else {
  10498. const computed = this.eat(0);
  10499. if (computed || optional || this.eat(16)) {
  10500. return this.parseMember(base, startPos, startLoc, state, computed, optional);
  10501. } else {
  10502. state.stop = true;
  10503. return base;
  10504. }
  10505. }
  10506. }
  10507. parseMember(base, startPos, startLoc, state, computed, optional) {
  10508. const node = this.startNodeAt(startPos, startLoc);
  10509. node.object = base;
  10510. node.computed = computed;
  10511. if (computed) {
  10512. node.property = this.parseExpression();
  10513. this.expect(3);
  10514. } else if (this.match(134)) {
  10515. if (base.type === "Super") {
  10516. this.raise(Errors.SuperPrivateField, {
  10517. at: startLoc
  10518. });
  10519. }
  10520. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10521. node.property = this.parsePrivateName();
  10522. } else {
  10523. node.property = this.parseIdentifier(true);
  10524. }
  10525. if (state.optionalChainMember) {
  10526. node.optional = optional;
  10527. return this.finishNode(node, "OptionalMemberExpression");
  10528. } else {
  10529. return this.finishNode(node, "MemberExpression");
  10530. }
  10531. }
  10532. parseBind(base, startPos, startLoc, noCalls, state) {
  10533. const node = this.startNodeAt(startPos, startLoc);
  10534. node.object = base;
  10535. this.next();
  10536. node.callee = this.parseNoCallExpr();
  10537. state.stop = true;
  10538. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  10539. }
  10540. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  10541. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10542. let refExpressionErrors = null;
  10543. this.state.maybeInArrowParameters = true;
  10544. this.next();
  10545. let node = this.startNodeAt(startPos, startLoc);
  10546. node.callee = base;
  10547. const {
  10548. maybeAsyncArrow,
  10549. optionalChainMember
  10550. } = state;
  10551. if (maybeAsyncArrow) {
  10552. this.expressionScope.enter(newAsyncArrowScope());
  10553. refExpressionErrors = new ExpressionErrors();
  10554. }
  10555. if (optionalChainMember) {
  10556. node.optional = optional;
  10557. }
  10558. if (optional) {
  10559. node.arguments = this.parseCallExpressionArguments(11);
  10560. } else {
  10561. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10562. }
  10563. this.finishCallExpression(node, optionalChainMember);
  10564. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10565. state.stop = true;
  10566. this.checkDestructuringPrivate(refExpressionErrors);
  10567. this.expressionScope.validateAsPattern();
  10568. this.expressionScope.exit();
  10569. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  10570. } else {
  10571. if (maybeAsyncArrow) {
  10572. this.checkExpressionErrors(refExpressionErrors, true);
  10573. this.expressionScope.exit();
  10574. }
  10575. this.toReferencedArguments(node);
  10576. }
  10577. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10578. return node;
  10579. }
  10580. toReferencedArguments(node, isParenthesizedExpr) {
  10581. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10582. }
  10583. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  10584. const node = this.startNodeAt(startPos, startLoc);
  10585. node.tag = base;
  10586. node.quasi = this.parseTemplate(true);
  10587. if (state.optionalChainMember) {
  10588. this.raise(Errors.OptionalChainingNoTemplate, {
  10589. at: startLoc
  10590. });
  10591. }
  10592. return this.finishNode(node, "TaggedTemplateExpression");
  10593. }
  10594. atPossibleAsyncArrow(base) {
  10595. 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;
  10596. }
  10597. finishCallExpression(node, optional) {
  10598. if (node.callee.type === "Import") {
  10599. if (node.arguments.length === 2) {
  10600. {
  10601. if (!this.hasPlugin("moduleAttributes")) {
  10602. this.expectPlugin("importAssertions");
  10603. }
  10604. }
  10605. }
  10606. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10607. this.raise(Errors.ImportCallArity, {
  10608. at: node,
  10609. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10610. });
  10611. } else {
  10612. for (const arg of node.arguments) {
  10613. if (arg.type === "SpreadElement") {
  10614. this.raise(Errors.ImportCallSpreadArgument, {
  10615. at: arg
  10616. });
  10617. }
  10618. }
  10619. }
  10620. }
  10621. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10622. }
  10623. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10624. const elts = [];
  10625. let first = true;
  10626. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10627. this.state.inFSharpPipelineDirectBody = false;
  10628. while (!this.eat(close)) {
  10629. if (first) {
  10630. first = false;
  10631. } else {
  10632. this.expect(12);
  10633. if (this.match(close)) {
  10634. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10635. this.raise(Errors.ImportCallArgumentTrailingComma, {
  10636. at: this.state.lastTokStartLoc
  10637. });
  10638. }
  10639. if (nodeForExtra) {
  10640. this.addTrailingCommaExtraToNode(nodeForExtra);
  10641. }
  10642. this.next();
  10643. break;
  10644. }
  10645. }
  10646. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10647. }
  10648. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10649. return elts;
  10650. }
  10651. shouldParseAsyncArrow() {
  10652. return this.match(19) && !this.canInsertSemicolon();
  10653. }
  10654. parseAsyncArrowFromCallExpression(node, call) {
  10655. var _call$extra;
  10656. this.resetPreviousNodeTrailingComments(call);
  10657. this.expect(19);
  10658. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  10659. if (call.innerComments) {
  10660. setInnerComments(node, call.innerComments);
  10661. }
  10662. if (call.callee.trailingComments) {
  10663. setInnerComments(node, call.callee.trailingComments);
  10664. }
  10665. return node;
  10666. }
  10667. parseNoCallExpr() {
  10668. const startPos = this.state.start;
  10669. const startLoc = this.state.startLoc;
  10670. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  10671. }
  10672. parseExprAtom(refExpressionErrors) {
  10673. let node;
  10674. const {
  10675. type
  10676. } = this.state;
  10677. switch (type) {
  10678. case 79:
  10679. return this.parseSuper();
  10680. case 83:
  10681. node = this.startNode();
  10682. this.next();
  10683. if (this.match(16)) {
  10684. return this.parseImportMetaProperty(node);
  10685. }
  10686. if (!this.match(10)) {
  10687. this.raise(Errors.UnsupportedImport, {
  10688. at: this.state.lastTokStartLoc
  10689. });
  10690. }
  10691. return this.finishNode(node, "Import");
  10692. case 78:
  10693. node = this.startNode();
  10694. this.next();
  10695. return this.finishNode(node, "ThisExpression");
  10696. case 90:
  10697. {
  10698. return this.parseDo(this.startNode(), false);
  10699. }
  10700. case 56:
  10701. case 31:
  10702. {
  10703. this.readRegexp();
  10704. return this.parseRegExpLiteral(this.state.value);
  10705. }
  10706. case 130:
  10707. return this.parseNumericLiteral(this.state.value);
  10708. case 131:
  10709. return this.parseBigIntLiteral(this.state.value);
  10710. case 132:
  10711. return this.parseDecimalLiteral(this.state.value);
  10712. case 129:
  10713. return this.parseStringLiteral(this.state.value);
  10714. case 84:
  10715. return this.parseNullLiteral();
  10716. case 85:
  10717. return this.parseBooleanLiteral(true);
  10718. case 86:
  10719. return this.parseBooleanLiteral(false);
  10720. case 10:
  10721. {
  10722. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10723. return this.parseParenAndDistinguishExpression(canBeArrow);
  10724. }
  10725. case 2:
  10726. case 1:
  10727. {
  10728. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  10729. }
  10730. case 0:
  10731. {
  10732. return this.parseArrayLike(3, true, false, refExpressionErrors);
  10733. }
  10734. case 6:
  10735. case 7:
  10736. {
  10737. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  10738. }
  10739. case 5:
  10740. {
  10741. return this.parseObjectLike(8, false, false, refExpressionErrors);
  10742. }
  10743. case 68:
  10744. return this.parseFunctionOrFunctionSent();
  10745. case 26:
  10746. this.parseDecorators();
  10747. case 80:
  10748. node = this.startNode();
  10749. this.takeDecorators(node);
  10750. return this.parseClass(node, false);
  10751. case 77:
  10752. return this.parseNewOrNewTarget();
  10753. case 25:
  10754. case 24:
  10755. return this.parseTemplate(false);
  10756. case 15:
  10757. {
  10758. node = this.startNode();
  10759. this.next();
  10760. node.object = null;
  10761. const callee = node.callee = this.parseNoCallExpr();
  10762. if (callee.type === "MemberExpression") {
  10763. return this.finishNode(node, "BindExpression");
  10764. } else {
  10765. throw this.raise(Errors.UnsupportedBind, {
  10766. at: callee
  10767. });
  10768. }
  10769. }
  10770. case 134:
  10771. {
  10772. this.raise(Errors.PrivateInExpectedIn, {
  10773. at: this.state.startLoc,
  10774. identifierName: this.state.value
  10775. });
  10776. return this.parsePrivateName();
  10777. }
  10778. case 33:
  10779. {
  10780. return this.parseTopicReferenceThenEqualsSign(54, "%");
  10781. }
  10782. case 32:
  10783. {
  10784. return this.parseTopicReferenceThenEqualsSign(44, "^");
  10785. }
  10786. case 37:
  10787. case 38:
  10788. {
  10789. return this.parseTopicReference("hack");
  10790. }
  10791. case 44:
  10792. case 54:
  10793. case 27:
  10794. {
  10795. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10796. if (pipeProposal) {
  10797. return this.parseTopicReference(pipeProposal);
  10798. } else {
  10799. throw this.unexpected();
  10800. }
  10801. }
  10802. case 47:
  10803. {
  10804. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  10805. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  10806. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  10807. break;
  10808. } else {
  10809. throw this.unexpected();
  10810. }
  10811. }
  10812. default:
  10813. if (tokenIsIdentifier(type)) {
  10814. if (this.isContextual(123) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  10815. return this.parseModuleExpression();
  10816. }
  10817. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10818. const containsEsc = this.state.containsEsc;
  10819. const id = this.parseIdentifier();
  10820. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  10821. const {
  10822. type
  10823. } = this.state;
  10824. if (type === 68) {
  10825. this.resetPreviousNodeTrailingComments(id);
  10826. this.next();
  10827. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  10828. } else if (tokenIsIdentifier(type)) {
  10829. if (this.lookaheadCharCode() === 61) {
  10830. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  10831. } else {
  10832. return id;
  10833. }
  10834. } else if (type === 90) {
  10835. this.resetPreviousNodeTrailingComments(id);
  10836. return this.parseDo(this.startNodeAtNode(id), true);
  10837. }
  10838. }
  10839. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  10840. this.next();
  10841. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  10842. }
  10843. return id;
  10844. } else {
  10845. throw this.unexpected();
  10846. }
  10847. }
  10848. }
  10849. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  10850. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10851. if (pipeProposal) {
  10852. this.state.type = topicTokenType;
  10853. this.state.value = topicTokenValue;
  10854. this.state.pos--;
  10855. this.state.end--;
  10856. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  10857. return this.parseTopicReference(pipeProposal);
  10858. } else {
  10859. throw this.unexpected();
  10860. }
  10861. }
  10862. parseTopicReference(pipeProposal) {
  10863. const node = this.startNode();
  10864. const startLoc = this.state.startLoc;
  10865. const tokenType = this.state.type;
  10866. this.next();
  10867. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  10868. }
  10869. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  10870. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  10871. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  10872. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  10873. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  10874. at: startLoc
  10875. });
  10876. }
  10877. this.registerTopicReference();
  10878. return this.finishNode(node, nodeType);
  10879. } else {
  10880. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  10881. at: startLoc,
  10882. token: tokenLabelName(tokenType)
  10883. });
  10884. }
  10885. }
  10886. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  10887. switch (pipeProposal) {
  10888. case "hack":
  10889. {
  10890. return this.hasPlugin(["pipelineOperator", {
  10891. topicToken: tokenLabelName(tokenType)
  10892. }]);
  10893. }
  10894. case "smart":
  10895. return tokenType === 27;
  10896. default:
  10897. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  10898. at: startLoc
  10899. });
  10900. }
  10901. }
  10902. parseAsyncArrowUnaryFunction(node) {
  10903. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  10904. const params = [this.parseIdentifier()];
  10905. this.prodParam.exit();
  10906. if (this.hasPrecedingLineBreak()) {
  10907. this.raise(Errors.LineTerminatorBeforeArrow, {
  10908. at: this.state.curPosition()
  10909. });
  10910. }
  10911. this.expect(19);
  10912. this.parseArrowExpression(node, params, true);
  10913. return node;
  10914. }
  10915. parseDo(node, isAsync) {
  10916. this.expectPlugin("doExpressions");
  10917. if (isAsync) {
  10918. this.expectPlugin("asyncDoExpressions");
  10919. }
  10920. node.async = isAsync;
  10921. this.next();
  10922. const oldLabels = this.state.labels;
  10923. this.state.labels = [];
  10924. if (isAsync) {
  10925. this.prodParam.enter(PARAM_AWAIT);
  10926. node.body = this.parseBlock();
  10927. this.prodParam.exit();
  10928. } else {
  10929. node.body = this.parseBlock();
  10930. }
  10931. this.state.labels = oldLabels;
  10932. return this.finishNode(node, "DoExpression");
  10933. }
  10934. parseSuper() {
  10935. const node = this.startNode();
  10936. this.next();
  10937. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  10938. this.raise(Errors.SuperNotAllowed, {
  10939. at: node
  10940. });
  10941. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  10942. this.raise(Errors.UnexpectedSuper, {
  10943. at: node
  10944. });
  10945. }
  10946. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  10947. this.raise(Errors.UnsupportedSuper, {
  10948. at: node
  10949. });
  10950. }
  10951. return this.finishNode(node, "Super");
  10952. }
  10953. parsePrivateName() {
  10954. const node = this.startNode();
  10955. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart, this.state.start + 1));
  10956. const name = this.state.value;
  10957. this.next();
  10958. node.id = this.createIdentifier(id, name);
  10959. return this.finishNode(node, "PrivateName");
  10960. }
  10961. parseFunctionOrFunctionSent() {
  10962. const node = this.startNode();
  10963. this.next();
  10964. if (this.prodParam.hasYield && this.match(16)) {
  10965. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  10966. this.next();
  10967. if (this.match(102)) {
  10968. this.expectPlugin("functionSent");
  10969. } else if (!this.hasPlugin("functionSent")) {
  10970. this.unexpected();
  10971. }
  10972. return this.parseMetaProperty(node, meta, "sent");
  10973. }
  10974. return this.parseFunction(node);
  10975. }
  10976. parseMetaProperty(node, meta, propertyName) {
  10977. node.meta = meta;
  10978. const containsEsc = this.state.containsEsc;
  10979. node.property = this.parseIdentifier(true);
  10980. if (node.property.name !== propertyName || containsEsc) {
  10981. this.raise(Errors.UnsupportedMetaProperty, {
  10982. at: node.property,
  10983. target: meta.name,
  10984. onlyValidPropertyName: propertyName
  10985. });
  10986. }
  10987. return this.finishNode(node, "MetaProperty");
  10988. }
  10989. parseImportMetaProperty(node) {
  10990. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  10991. this.next();
  10992. if (this.isContextual(100)) {
  10993. if (!this.inModule) {
  10994. this.raise(Errors.ImportMetaOutsideModule, {
  10995. at: id
  10996. });
  10997. }
  10998. this.sawUnambiguousESM = true;
  10999. }
  11000. return this.parseMetaProperty(node, id, "meta");
  11001. }
  11002. parseLiteralAtNode(value, type, node) {
  11003. this.addExtra(node, "rawValue", value);
  11004. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11005. node.value = value;
  11006. this.next();
  11007. return this.finishNode(node, type);
  11008. }
  11009. parseLiteral(value, type) {
  11010. const node = this.startNode();
  11011. return this.parseLiteralAtNode(value, type, node);
  11012. }
  11013. parseStringLiteral(value) {
  11014. return this.parseLiteral(value, "StringLiteral");
  11015. }
  11016. parseNumericLiteral(value) {
  11017. return this.parseLiteral(value, "NumericLiteral");
  11018. }
  11019. parseBigIntLiteral(value) {
  11020. return this.parseLiteral(value, "BigIntLiteral");
  11021. }
  11022. parseDecimalLiteral(value) {
  11023. return this.parseLiteral(value, "DecimalLiteral");
  11024. }
  11025. parseRegExpLiteral(value) {
  11026. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11027. node.pattern = value.pattern;
  11028. node.flags = value.flags;
  11029. return node;
  11030. }
  11031. parseBooleanLiteral(value) {
  11032. const node = this.startNode();
  11033. node.value = value;
  11034. this.next();
  11035. return this.finishNode(node, "BooleanLiteral");
  11036. }
  11037. parseNullLiteral() {
  11038. const node = this.startNode();
  11039. this.next();
  11040. return this.finishNode(node, "NullLiteral");
  11041. }
  11042. parseParenAndDistinguishExpression(canBeArrow) {
  11043. const startPos = this.state.start;
  11044. const startLoc = this.state.startLoc;
  11045. let val;
  11046. this.next();
  11047. this.expressionScope.enter(newArrowHeadScope());
  11048. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11049. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11050. this.state.maybeInArrowParameters = true;
  11051. this.state.inFSharpPipelineDirectBody = false;
  11052. const innerStartPos = this.state.start;
  11053. const innerStartLoc = this.state.startLoc;
  11054. const exprList = [];
  11055. const refExpressionErrors = new ExpressionErrors();
  11056. let first = true;
  11057. let spreadStartLoc;
  11058. let optionalCommaStartLoc;
  11059. while (!this.match(11)) {
  11060. if (first) {
  11061. first = false;
  11062. } else {
  11063. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11064. if (this.match(11)) {
  11065. optionalCommaStartLoc = this.state.startLoc;
  11066. break;
  11067. }
  11068. }
  11069. if (this.match(21)) {
  11070. const spreadNodeStartPos = this.state.start;
  11071. const spreadNodeStartLoc = this.state.startLoc;
  11072. spreadStartLoc = this.state.startLoc;
  11073. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  11074. if (!this.checkCommaAfterRest(41)) {
  11075. break;
  11076. }
  11077. } else {
  11078. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11079. }
  11080. }
  11081. const innerEndLoc = this.state.lastTokEndLoc;
  11082. this.expect(11);
  11083. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11084. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11085. let arrowNode = this.startNodeAt(startPos, startLoc);
  11086. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11087. this.checkDestructuringPrivate(refExpressionErrors);
  11088. this.expressionScope.validateAsPattern();
  11089. this.expressionScope.exit();
  11090. this.parseArrowExpression(arrowNode, exprList, false);
  11091. return arrowNode;
  11092. }
  11093. this.expressionScope.exit();
  11094. if (!exprList.length) {
  11095. this.unexpected(this.state.lastTokStartLoc);
  11096. }
  11097. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11098. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11099. this.checkExpressionErrors(refExpressionErrors, true);
  11100. this.toReferencedListDeep(exprList, true);
  11101. if (exprList.length > 1) {
  11102. val = this.startNodeAt(innerStartPos, innerStartLoc);
  11103. val.expressions = exprList;
  11104. this.finishNode(val, "SequenceExpression");
  11105. this.resetEndLocation(val, innerEndLoc);
  11106. } else {
  11107. val = exprList[0];
  11108. }
  11109. return this.wrapParenthesis(startPos, startLoc, val);
  11110. }
  11111. wrapParenthesis(startPos, startLoc, expression) {
  11112. if (!this.options.createParenthesizedExpressions) {
  11113. this.addExtra(expression, "parenthesized", true);
  11114. this.addExtra(expression, "parenStart", startPos);
  11115. this.takeSurroundingComments(expression, startPos, this.state.lastTokEndLoc.index);
  11116. return expression;
  11117. }
  11118. const parenExpression = this.startNodeAt(startPos, startLoc);
  11119. parenExpression.expression = expression;
  11120. this.finishNode(parenExpression, "ParenthesizedExpression");
  11121. return parenExpression;
  11122. }
  11123. shouldParseArrow(params) {
  11124. return !this.canInsertSemicolon();
  11125. }
  11126. parseArrow(node) {
  11127. if (this.eat(19)) {
  11128. return node;
  11129. }
  11130. }
  11131. parseParenItem(node, startPos, startLoc) {
  11132. return node;
  11133. }
  11134. parseNewOrNewTarget() {
  11135. const node = this.startNode();
  11136. this.next();
  11137. if (this.match(16)) {
  11138. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11139. this.next();
  11140. const metaProp = this.parseMetaProperty(node, meta, "target");
  11141. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  11142. this.raise(Errors.UnexpectedNewTarget, {
  11143. at: metaProp
  11144. });
  11145. }
  11146. return metaProp;
  11147. }
  11148. return this.parseNew(node);
  11149. }
  11150. parseNew(node) {
  11151. node.callee = this.parseNoCallExpr();
  11152. if (node.callee.type === "Import") {
  11153. this.raise(Errors.ImportCallNotNewExpression, {
  11154. at: node.callee
  11155. });
  11156. } else if (this.isOptionalChain(node.callee)) {
  11157. this.raise(Errors.OptionalChainingNoNew, {
  11158. at: this.state.lastTokEndLoc
  11159. });
  11160. } else if (this.eat(18)) {
  11161. this.raise(Errors.OptionalChainingNoNew, {
  11162. at: this.state.startLoc
  11163. });
  11164. }
  11165. this.parseNewArguments(node);
  11166. return this.finishNode(node, "NewExpression");
  11167. }
  11168. parseNewArguments(node) {
  11169. if (this.eat(10)) {
  11170. const args = this.parseExprList(11);
  11171. this.toReferencedList(args);
  11172. node.arguments = args;
  11173. } else {
  11174. node.arguments = [];
  11175. }
  11176. }
  11177. parseTemplateElement(isTagged) {
  11178. const {
  11179. start,
  11180. startLoc,
  11181. end,
  11182. value
  11183. } = this.state;
  11184. const elemStart = start + 1;
  11185. const elem = this.startNodeAt(elemStart, createPositionWithColumnOffset(startLoc, 1));
  11186. if (value === null) {
  11187. if (!isTagged) {
  11188. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11189. at: createPositionWithColumnOffset(startLoc, 2)
  11190. });
  11191. }
  11192. }
  11193. const isTail = this.match(24);
  11194. const endOffset = isTail ? -1 : -2;
  11195. const elemEnd = end + endOffset;
  11196. elem.value = {
  11197. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11198. cooked: value === null ? null : value.slice(1, endOffset)
  11199. };
  11200. elem.tail = isTail;
  11201. this.next();
  11202. this.finishNode(elem, "TemplateElement");
  11203. this.resetEndLocation(elem, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11204. return elem;
  11205. }
  11206. parseTemplate(isTagged) {
  11207. const node = this.startNode();
  11208. node.expressions = [];
  11209. let curElt = this.parseTemplateElement(isTagged);
  11210. node.quasis = [curElt];
  11211. while (!curElt.tail) {
  11212. node.expressions.push(this.parseTemplateSubstitution());
  11213. this.readTemplateContinuation();
  11214. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11215. }
  11216. return this.finishNode(node, "TemplateLiteral");
  11217. }
  11218. parseTemplateSubstitution() {
  11219. return this.parseExpression();
  11220. }
  11221. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11222. if (isRecord) {
  11223. this.expectPlugin("recordAndTuple");
  11224. }
  11225. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11226. this.state.inFSharpPipelineDirectBody = false;
  11227. const propHash = Object.create(null);
  11228. let first = true;
  11229. const node = this.startNode();
  11230. node.properties = [];
  11231. this.next();
  11232. while (!this.match(close)) {
  11233. if (first) {
  11234. first = false;
  11235. } else {
  11236. this.expect(12);
  11237. if (this.match(close)) {
  11238. this.addTrailingCommaExtraToNode(node);
  11239. break;
  11240. }
  11241. }
  11242. let prop;
  11243. if (isPattern) {
  11244. prop = this.parseBindingProperty();
  11245. } else {
  11246. prop = this.parsePropertyDefinition(refExpressionErrors);
  11247. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11248. }
  11249. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11250. this.raise(Errors.InvalidRecordProperty, {
  11251. at: prop
  11252. });
  11253. }
  11254. if (prop.shorthand) {
  11255. this.addExtra(prop, "shorthand", true);
  11256. }
  11257. node.properties.push(prop);
  11258. }
  11259. this.next();
  11260. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11261. let type = "ObjectExpression";
  11262. if (isPattern) {
  11263. type = "ObjectPattern";
  11264. } else if (isRecord) {
  11265. type = "RecordExpression";
  11266. }
  11267. return this.finishNode(node, type);
  11268. }
  11269. addTrailingCommaExtraToNode(node) {
  11270. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11271. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11272. }
  11273. maybeAsyncOrAccessorProp(prop) {
  11274. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11275. }
  11276. parsePropertyDefinition(refExpressionErrors) {
  11277. let decorators = [];
  11278. if (this.match(26)) {
  11279. if (this.hasPlugin("decorators")) {
  11280. this.raise(Errors.UnsupportedPropertyDecorator, {
  11281. at: this.state.startLoc
  11282. });
  11283. }
  11284. while (this.match(26)) {
  11285. decorators.push(this.parseDecorator());
  11286. }
  11287. }
  11288. const prop = this.startNode();
  11289. let isAsync = false;
  11290. let isAccessor = false;
  11291. let startPos;
  11292. let startLoc;
  11293. if (this.match(21)) {
  11294. if (decorators.length) this.unexpected();
  11295. return this.parseSpread();
  11296. }
  11297. if (decorators.length) {
  11298. prop.decorators = decorators;
  11299. decorators = [];
  11300. }
  11301. prop.method = false;
  11302. if (refExpressionErrors) {
  11303. startPos = this.state.start;
  11304. startLoc = this.state.startLoc;
  11305. }
  11306. let isGenerator = this.eat(55);
  11307. this.parsePropertyNamePrefixOperator(prop);
  11308. const containsEsc = this.state.containsEsc;
  11309. const key = this.parsePropertyName(prop, refExpressionErrors);
  11310. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11311. const keyName = key.name;
  11312. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11313. isAsync = true;
  11314. this.resetPreviousNodeTrailingComments(key);
  11315. isGenerator = this.eat(55);
  11316. this.parsePropertyName(prop);
  11317. }
  11318. if (keyName === "get" || keyName === "set") {
  11319. isAccessor = true;
  11320. this.resetPreviousNodeTrailingComments(key);
  11321. prop.kind = keyName;
  11322. if (this.match(55)) {
  11323. isGenerator = true;
  11324. this.raise(Errors.AccessorIsGenerator, {
  11325. at: this.state.curPosition(),
  11326. kind: keyName
  11327. });
  11328. this.next();
  11329. }
  11330. this.parsePropertyName(prop);
  11331. }
  11332. }
  11333. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11334. return prop;
  11335. }
  11336. getGetterSetterExpectedParamCount(method) {
  11337. return method.kind === "get" ? 0 : 1;
  11338. }
  11339. getObjectOrClassMethodParams(method) {
  11340. return method.params;
  11341. }
  11342. checkGetterSetterParams(method) {
  11343. var _params;
  11344. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11345. const params = this.getObjectOrClassMethodParams(method);
  11346. if (params.length !== paramCount) {
  11347. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11348. at: method
  11349. });
  11350. }
  11351. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11352. this.raise(Errors.BadSetterRestParameter, {
  11353. at: method
  11354. });
  11355. }
  11356. }
  11357. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11358. if (isAccessor) {
  11359. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11360. this.checkGetterSetterParams(prop);
  11361. return prop;
  11362. }
  11363. if (isAsync || isGenerator || this.match(10)) {
  11364. if (isPattern) this.unexpected();
  11365. prop.kind = "method";
  11366. prop.method = true;
  11367. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11368. }
  11369. }
  11370. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  11371. prop.shorthand = false;
  11372. if (this.eat(14)) {
  11373. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11374. return this.finishNode(prop, "ObjectProperty");
  11375. }
  11376. if (!prop.computed && prop.key.type === "Identifier") {
  11377. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11378. if (isPattern) {
  11379. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11380. } else if (this.match(29)) {
  11381. const shorthandAssignLoc = this.state.startLoc;
  11382. if (refExpressionErrors != null) {
  11383. if (refExpressionErrors.shorthandAssignLoc === null) {
  11384. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11385. }
  11386. } else {
  11387. this.raise(Errors.InvalidCoverInitializedName, {
  11388. at: shorthandAssignLoc
  11389. });
  11390. }
  11391. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11392. } else {
  11393. prop.value = cloneIdentifier(prop.key);
  11394. }
  11395. prop.shorthand = true;
  11396. return this.finishNode(prop, "ObjectProperty");
  11397. }
  11398. }
  11399. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11400. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  11401. if (!node) this.unexpected();
  11402. return node;
  11403. }
  11404. parsePropertyName(prop, refExpressionErrors) {
  11405. if (this.eat(0)) {
  11406. prop.computed = true;
  11407. prop.key = this.parseMaybeAssignAllowIn();
  11408. this.expect(3);
  11409. } else {
  11410. const {
  11411. type,
  11412. value
  11413. } = this.state;
  11414. let key;
  11415. if (tokenIsKeywordOrIdentifier(type)) {
  11416. key = this.parseIdentifier(true);
  11417. } else {
  11418. switch (type) {
  11419. case 130:
  11420. key = this.parseNumericLiteral(value);
  11421. break;
  11422. case 129:
  11423. key = this.parseStringLiteral(value);
  11424. break;
  11425. case 131:
  11426. key = this.parseBigIntLiteral(value);
  11427. break;
  11428. case 132:
  11429. key = this.parseDecimalLiteral(value);
  11430. break;
  11431. case 134:
  11432. {
  11433. const privateKeyLoc = this.state.startLoc;
  11434. if (refExpressionErrors != null) {
  11435. if (refExpressionErrors.privateKeyLoc === null) {
  11436. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11437. }
  11438. } else {
  11439. this.raise(Errors.UnexpectedPrivateField, {
  11440. at: privateKeyLoc
  11441. });
  11442. }
  11443. key = this.parsePrivateName();
  11444. break;
  11445. }
  11446. default:
  11447. throw this.unexpected();
  11448. }
  11449. }
  11450. prop.key = key;
  11451. if (type !== 134) {
  11452. prop.computed = false;
  11453. }
  11454. }
  11455. return prop.key;
  11456. }
  11457. initFunction(node, isAsync) {
  11458. node.id = null;
  11459. node.generator = false;
  11460. node.async = !!isAsync;
  11461. }
  11462. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11463. this.initFunction(node, isAsync);
  11464. node.generator = !!isGenerator;
  11465. const allowModifiers = isConstructor;
  11466. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11467. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11468. this.parseFunctionParams(node, allowModifiers);
  11469. this.parseFunctionBodyAndFinish(node, type, true);
  11470. this.prodParam.exit();
  11471. this.scope.exit();
  11472. return node;
  11473. }
  11474. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11475. if (isTuple) {
  11476. this.expectPlugin("recordAndTuple");
  11477. }
  11478. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11479. this.state.inFSharpPipelineDirectBody = false;
  11480. const node = this.startNode();
  11481. this.next();
  11482. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11483. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11484. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11485. }
  11486. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11487. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11488. let flags = functionFlags(isAsync, false);
  11489. if (!this.match(5) && this.prodParam.hasIn) {
  11490. flags |= PARAM_IN;
  11491. }
  11492. this.prodParam.enter(flags);
  11493. this.initFunction(node, isAsync);
  11494. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11495. if (params) {
  11496. this.state.maybeInArrowParameters = true;
  11497. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11498. }
  11499. this.state.maybeInArrowParameters = false;
  11500. this.parseFunctionBody(node, true);
  11501. this.prodParam.exit();
  11502. this.scope.exit();
  11503. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11504. return this.finishNode(node, "ArrowFunctionExpression");
  11505. }
  11506. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11507. this.toAssignableList(params, trailingCommaLoc, false);
  11508. node.params = params;
  11509. }
  11510. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11511. this.parseFunctionBody(node, false, isMethod);
  11512. this.finishNode(node, type);
  11513. }
  11514. parseFunctionBody(node, allowExpression, isMethod = false) {
  11515. const isExpression = allowExpression && !this.match(5);
  11516. this.expressionScope.enter(newExpressionScope());
  11517. if (isExpression) {
  11518. node.body = this.parseMaybeAssign();
  11519. this.checkParams(node, false, allowExpression, false);
  11520. } else {
  11521. const oldStrict = this.state.strict;
  11522. const oldLabels = this.state.labels;
  11523. this.state.labels = [];
  11524. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11525. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11526. const nonSimple = !this.isSimpleParamList(node.params);
  11527. if (hasStrictModeDirective && nonSimple) {
  11528. this.raise(Errors.IllegalLanguageModeDirective, {
  11529. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11530. });
  11531. }
  11532. const strictModeChanged = !oldStrict && this.state.strict;
  11533. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11534. if (this.state.strict && node.id) {
  11535. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11536. }
  11537. });
  11538. this.prodParam.exit();
  11539. this.state.labels = oldLabels;
  11540. }
  11541. this.expressionScope.exit();
  11542. }
  11543. isSimpleParameter(node) {
  11544. return node.type === "Identifier";
  11545. }
  11546. isSimpleParamList(params) {
  11547. for (let i = 0, len = params.length; i < len; i++) {
  11548. if (!this.isSimpleParameter(params[i])) return false;
  11549. }
  11550. return true;
  11551. }
  11552. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11553. const checkClashes = !allowDuplicates && new Set();
  11554. const formalParameters = {
  11555. type: "FormalParameters"
  11556. };
  11557. for (const param of node.params) {
  11558. this.checkLVal(param, {
  11559. in: formalParameters,
  11560. binding: BIND_VAR,
  11561. checkClashes,
  11562. strictModeChanged
  11563. });
  11564. }
  11565. }
  11566. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11567. const elts = [];
  11568. let first = true;
  11569. while (!this.eat(close)) {
  11570. if (first) {
  11571. first = false;
  11572. } else {
  11573. this.expect(12);
  11574. if (this.match(close)) {
  11575. if (nodeForExtra) {
  11576. this.addTrailingCommaExtraToNode(nodeForExtra);
  11577. }
  11578. this.next();
  11579. break;
  11580. }
  11581. }
  11582. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11583. }
  11584. return elts;
  11585. }
  11586. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11587. let elt;
  11588. if (this.match(12)) {
  11589. if (!allowEmpty) {
  11590. this.raise(Errors.UnexpectedToken, {
  11591. at: this.state.curPosition(),
  11592. unexpected: ","
  11593. });
  11594. }
  11595. elt = null;
  11596. } else if (this.match(21)) {
  11597. const spreadNodeStartPos = this.state.start;
  11598. const spreadNodeStartLoc = this.state.startLoc;
  11599. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  11600. } else if (this.match(17)) {
  11601. this.expectPlugin("partialApplication");
  11602. if (!allowPlaceholder) {
  11603. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11604. at: this.state.startLoc
  11605. });
  11606. }
  11607. const node = this.startNode();
  11608. this.next();
  11609. elt = this.finishNode(node, "ArgumentPlaceholder");
  11610. } else {
  11611. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11612. }
  11613. return elt;
  11614. }
  11615. parseIdentifier(liberal) {
  11616. const node = this.startNode();
  11617. const name = this.parseIdentifierName(node.start, liberal);
  11618. return this.createIdentifier(node, name);
  11619. }
  11620. createIdentifier(node, name) {
  11621. node.name = name;
  11622. node.loc.identifierName = name;
  11623. return this.finishNode(node, "Identifier");
  11624. }
  11625. parseIdentifierName(pos, liberal) {
  11626. let name;
  11627. const {
  11628. startLoc,
  11629. type
  11630. } = this.state;
  11631. if (tokenIsKeywordOrIdentifier(type)) {
  11632. name = this.state.value;
  11633. } else {
  11634. throw this.unexpected();
  11635. }
  11636. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11637. if (liberal) {
  11638. if (tokenIsKeyword) {
  11639. this.replaceToken(128);
  11640. }
  11641. } else {
  11642. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11643. }
  11644. this.next();
  11645. return name;
  11646. }
  11647. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11648. if (word.length > 10) {
  11649. return;
  11650. }
  11651. if (!canBeReservedWord(word)) {
  11652. return;
  11653. }
  11654. if (word === "yield") {
  11655. if (this.prodParam.hasYield) {
  11656. this.raise(Errors.YieldBindingIdentifier, {
  11657. at: startLoc
  11658. });
  11659. return;
  11660. }
  11661. } else if (word === "await") {
  11662. if (this.prodParam.hasAwait) {
  11663. this.raise(Errors.AwaitBindingIdentifier, {
  11664. at: startLoc
  11665. });
  11666. return;
  11667. }
  11668. if (this.scope.inStaticBlock) {
  11669. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  11670. at: startLoc
  11671. });
  11672. return;
  11673. }
  11674. this.expressionScope.recordAsyncArrowParametersError({
  11675. at: startLoc
  11676. });
  11677. } else if (word === "arguments") {
  11678. if (this.scope.inClassAndNotInNonArrowFunction) {
  11679. this.raise(Errors.ArgumentsInClass, {
  11680. at: startLoc
  11681. });
  11682. return;
  11683. }
  11684. }
  11685. if (checkKeywords && isKeyword(word)) {
  11686. this.raise(Errors.UnexpectedKeyword, {
  11687. at: startLoc,
  11688. keyword: word
  11689. });
  11690. return;
  11691. }
  11692. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  11693. if (reservedTest(word, this.inModule)) {
  11694. this.raise(Errors.UnexpectedReservedWord, {
  11695. at: startLoc,
  11696. reservedWord: word
  11697. });
  11698. }
  11699. }
  11700. isAwaitAllowed() {
  11701. if (this.prodParam.hasAwait) return true;
  11702. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  11703. return true;
  11704. }
  11705. return false;
  11706. }
  11707. parseAwait(startPos, startLoc) {
  11708. const node = this.startNodeAt(startPos, startLoc);
  11709. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  11710. at: node
  11711. });
  11712. if (this.eat(55)) {
  11713. this.raise(Errors.ObsoleteAwaitStar, {
  11714. at: node
  11715. });
  11716. }
  11717. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  11718. if (this.isAmbiguousAwait()) {
  11719. this.ambiguousScriptDifferentAst = true;
  11720. } else {
  11721. this.sawUnambiguousESM = true;
  11722. }
  11723. }
  11724. if (!this.state.soloAwait) {
  11725. node.argument = this.parseMaybeUnary(null, true);
  11726. }
  11727. return this.finishNode(node, "AwaitExpression");
  11728. }
  11729. isAmbiguousAwait() {
  11730. if (this.hasPrecedingLineBreak()) return true;
  11731. const {
  11732. type
  11733. } = this.state;
  11734. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 133 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  11735. }
  11736. parseYield() {
  11737. const node = this.startNode();
  11738. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  11739. at: node
  11740. });
  11741. this.next();
  11742. let delegating = false;
  11743. let argument = null;
  11744. if (!this.hasPrecedingLineBreak()) {
  11745. delegating = this.eat(55);
  11746. switch (this.state.type) {
  11747. case 13:
  11748. case 135:
  11749. case 8:
  11750. case 11:
  11751. case 3:
  11752. case 9:
  11753. case 14:
  11754. case 12:
  11755. if (!delegating) break;
  11756. default:
  11757. argument = this.parseMaybeAssign();
  11758. }
  11759. }
  11760. node.delegate = delegating;
  11761. node.argument = argument;
  11762. return this.finishNode(node, "YieldExpression");
  11763. }
  11764. checkPipelineAtInfixOperator(left, leftStartLoc) {
  11765. if (this.hasPlugin(["pipelineOperator", {
  11766. proposal: "smart"
  11767. }])) {
  11768. if (left.type === "SequenceExpression") {
  11769. this.raise(Errors.PipelineHeadSequenceExpression, {
  11770. at: leftStartLoc
  11771. });
  11772. }
  11773. }
  11774. }
  11775. parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
  11776. const bodyNode = this.startNodeAt(startPos, startLoc);
  11777. if (this.isSimpleReference(childExpr)) {
  11778. bodyNode.callee = childExpr;
  11779. return this.finishNode(bodyNode, "PipelineBareFunction");
  11780. } else {
  11781. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  11782. bodyNode.expression = childExpr;
  11783. return this.finishNode(bodyNode, "PipelineTopicExpression");
  11784. }
  11785. }
  11786. isSimpleReference(expression) {
  11787. switch (expression.type) {
  11788. case "MemberExpression":
  11789. return !expression.computed && this.isSimpleReference(expression.object);
  11790. case "Identifier":
  11791. return true;
  11792. default:
  11793. return false;
  11794. }
  11795. }
  11796. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  11797. if (this.match(19)) {
  11798. throw this.raise(Errors.PipelineBodyNoArrow, {
  11799. at: this.state.startLoc
  11800. });
  11801. }
  11802. if (!this.topicReferenceWasUsedInCurrentContext()) {
  11803. this.raise(Errors.PipelineTopicUnused, {
  11804. at: startLoc
  11805. });
  11806. }
  11807. }
  11808. withTopicBindingContext(callback) {
  11809. const outerContextTopicState = this.state.topicContext;
  11810. this.state.topicContext = {
  11811. maxNumOfResolvableTopics: 1,
  11812. maxTopicIndex: null
  11813. };
  11814. try {
  11815. return callback();
  11816. } finally {
  11817. this.state.topicContext = outerContextTopicState;
  11818. }
  11819. }
  11820. withSmartMixTopicForbiddingContext(callback) {
  11821. if (this.hasPlugin(["pipelineOperator", {
  11822. proposal: "smart"
  11823. }])) {
  11824. const outerContextTopicState = this.state.topicContext;
  11825. this.state.topicContext = {
  11826. maxNumOfResolvableTopics: 0,
  11827. maxTopicIndex: null
  11828. };
  11829. try {
  11830. return callback();
  11831. } finally {
  11832. this.state.topicContext = outerContextTopicState;
  11833. }
  11834. } else {
  11835. return callback();
  11836. }
  11837. }
  11838. withSoloAwaitPermittingContext(callback) {
  11839. const outerContextSoloAwaitState = this.state.soloAwait;
  11840. this.state.soloAwait = true;
  11841. try {
  11842. return callback();
  11843. } finally {
  11844. this.state.soloAwait = outerContextSoloAwaitState;
  11845. }
  11846. }
  11847. allowInAnd(callback) {
  11848. const flags = this.prodParam.currentFlags();
  11849. const prodParamToSet = PARAM_IN & ~flags;
  11850. if (prodParamToSet) {
  11851. this.prodParam.enter(flags | PARAM_IN);
  11852. try {
  11853. return callback();
  11854. } finally {
  11855. this.prodParam.exit();
  11856. }
  11857. }
  11858. return callback();
  11859. }
  11860. disallowInAnd(callback) {
  11861. const flags = this.prodParam.currentFlags();
  11862. const prodParamToClear = PARAM_IN & flags;
  11863. if (prodParamToClear) {
  11864. this.prodParam.enter(flags & ~PARAM_IN);
  11865. try {
  11866. return callback();
  11867. } finally {
  11868. this.prodParam.exit();
  11869. }
  11870. }
  11871. return callback();
  11872. }
  11873. registerTopicReference() {
  11874. this.state.topicContext.maxTopicIndex = 0;
  11875. }
  11876. topicReferenceIsAllowedInCurrentContext() {
  11877. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  11878. }
  11879. topicReferenceWasUsedInCurrentContext() {
  11880. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  11881. }
  11882. parseFSharpPipelineBody(prec) {
  11883. const startPos = this.state.start;
  11884. const startLoc = this.state.startLoc;
  11885. this.state.potentialArrowAt = this.state.start;
  11886. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11887. this.state.inFSharpPipelineDirectBody = true;
  11888. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
  11889. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11890. return ret;
  11891. }
  11892. parseModuleExpression() {
  11893. this.expectPlugin("moduleBlocks");
  11894. const node = this.startNode();
  11895. this.next();
  11896. this.eat(5);
  11897. const revertScopes = this.initializeScopes(true);
  11898. this.enterInitialScopes();
  11899. const program = this.startNode();
  11900. try {
  11901. node.body = this.parseProgram(program, 8, "module");
  11902. } finally {
  11903. revertScopes();
  11904. }
  11905. this.eat(8);
  11906. return this.finishNode(node, "ModuleExpression");
  11907. }
  11908. parsePropertyNamePrefixOperator(prop) {}
  11909. }
  11910. const loopLabel = {
  11911. kind: "loop"
  11912. },
  11913. switchLabel = {
  11914. kind: "switch"
  11915. };
  11916. const FUNC_NO_FLAGS = 0b000,
  11917. FUNC_STATEMENT = 0b001,
  11918. FUNC_HANGING_STATEMENT = 0b010,
  11919. FUNC_NULLABLE_ID = 0b100;
  11920. const loneSurrogate = /[\uD800-\uDFFF]/u;
  11921. const keywordRelationalOperator = /in(?:stanceof)?/y;
  11922. function babel7CompatTokens(tokens, input) {
  11923. for (let i = 0; i < tokens.length; i++) {
  11924. const token = tokens[i];
  11925. const {
  11926. type
  11927. } = token;
  11928. if (typeof type === "number") {
  11929. {
  11930. if (type === 134) {
  11931. const {
  11932. loc,
  11933. start,
  11934. value,
  11935. end
  11936. } = token;
  11937. const hashEndPos = start + 1;
  11938. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  11939. tokens.splice(i, 1, new Token({
  11940. type: getExportedToken(27),
  11941. value: "#",
  11942. start: start,
  11943. end: hashEndPos,
  11944. startLoc: loc.start,
  11945. endLoc: hashEndLoc
  11946. }), new Token({
  11947. type: getExportedToken(128),
  11948. value: value,
  11949. start: hashEndPos,
  11950. end: end,
  11951. startLoc: hashEndLoc,
  11952. endLoc: loc.end
  11953. }));
  11954. i++;
  11955. continue;
  11956. }
  11957. if (tokenIsTemplate(type)) {
  11958. const {
  11959. loc,
  11960. start,
  11961. value,
  11962. end
  11963. } = token;
  11964. const backquoteEnd = start + 1;
  11965. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  11966. let startToken;
  11967. if (input.charCodeAt(start) === 96) {
  11968. startToken = new Token({
  11969. type: getExportedToken(22),
  11970. value: "`",
  11971. start: start,
  11972. end: backquoteEnd,
  11973. startLoc: loc.start,
  11974. endLoc: backquoteEndLoc
  11975. });
  11976. } else {
  11977. startToken = new Token({
  11978. type: getExportedToken(8),
  11979. value: "}",
  11980. start: start,
  11981. end: backquoteEnd,
  11982. startLoc: loc.start,
  11983. endLoc: backquoteEndLoc
  11984. });
  11985. }
  11986. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  11987. if (type === 24) {
  11988. templateElementEnd = end - 1;
  11989. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  11990. templateValue = value === null ? null : value.slice(1, -1);
  11991. endToken = new Token({
  11992. type: getExportedToken(22),
  11993. value: "`",
  11994. start: templateElementEnd,
  11995. end: end,
  11996. startLoc: templateElementEndLoc,
  11997. endLoc: loc.end
  11998. });
  11999. } else {
  12000. templateElementEnd = end - 2;
  12001. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12002. templateValue = value === null ? null : value.slice(1, -2);
  12003. endToken = new Token({
  12004. type: getExportedToken(23),
  12005. value: "${",
  12006. start: templateElementEnd,
  12007. end: end,
  12008. startLoc: templateElementEndLoc,
  12009. endLoc: loc.end
  12010. });
  12011. }
  12012. tokens.splice(i, 1, startToken, new Token({
  12013. type: getExportedToken(20),
  12014. value: templateValue,
  12015. start: backquoteEnd,
  12016. end: templateElementEnd,
  12017. startLoc: backquoteEndLoc,
  12018. endLoc: templateElementEndLoc
  12019. }), endToken);
  12020. i += 2;
  12021. continue;
  12022. }
  12023. }
  12024. token.type = getExportedToken(type);
  12025. }
  12026. }
  12027. return tokens;
  12028. }
  12029. class StatementParser extends ExpressionParser {
  12030. parseTopLevel(file, program) {
  12031. file.program = this.parseProgram(program);
  12032. file.comments = this.state.comments;
  12033. if (this.options.tokens) {
  12034. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12035. }
  12036. return this.finishNode(file, "File");
  12037. }
  12038. parseProgram(program, end = 135, sourceType = this.options.sourceType) {
  12039. program.sourceType = sourceType;
  12040. program.interpreter = this.parseInterpreterDirective();
  12041. this.parseBlockBody(program, true, true, end);
  12042. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12043. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12044. this.raise(Errors.ModuleExportUndefined, {
  12045. at,
  12046. localName
  12047. });
  12048. }
  12049. }
  12050. return this.finishNode(program, "Program");
  12051. }
  12052. stmtToDirective(stmt) {
  12053. const directive = stmt;
  12054. directive.type = "Directive";
  12055. directive.value = directive.expression;
  12056. delete directive.expression;
  12057. const directiveLiteral = directive.value;
  12058. const expressionValue = directiveLiteral.value;
  12059. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12060. const val = directiveLiteral.value = raw.slice(1, -1);
  12061. this.addExtra(directiveLiteral, "raw", raw);
  12062. this.addExtra(directiveLiteral, "rawValue", val);
  12063. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12064. directiveLiteral.type = "DirectiveLiteral";
  12065. return directive;
  12066. }
  12067. parseInterpreterDirective() {
  12068. if (!this.match(28)) {
  12069. return null;
  12070. }
  12071. const node = this.startNode();
  12072. node.value = this.state.value;
  12073. this.next();
  12074. return this.finishNode(node, "InterpreterDirective");
  12075. }
  12076. isLet(context) {
  12077. if (!this.isContextual(99)) {
  12078. return false;
  12079. }
  12080. return this.isLetKeyword(context);
  12081. }
  12082. isLetKeyword(context) {
  12083. const next = this.nextTokenStart();
  12084. const nextCh = this.codePointAtPos(next);
  12085. if (nextCh === 92 || nextCh === 91) {
  12086. return true;
  12087. }
  12088. if (context) return false;
  12089. if (nextCh === 123) return true;
  12090. if (isIdentifierStart(nextCh)) {
  12091. keywordRelationalOperator.lastIndex = next;
  12092. if (keywordRelationalOperator.test(this.input)) {
  12093. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12094. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12095. return false;
  12096. }
  12097. }
  12098. return true;
  12099. }
  12100. return false;
  12101. }
  12102. parseStatement(context, topLevel) {
  12103. if (this.match(26)) {
  12104. this.parseDecorators(true);
  12105. }
  12106. return this.parseStatementContent(context, topLevel);
  12107. }
  12108. parseStatementContent(context, topLevel) {
  12109. let starttype = this.state.type;
  12110. const node = this.startNode();
  12111. let kind;
  12112. if (this.isLet(context)) {
  12113. starttype = 74;
  12114. kind = "let";
  12115. }
  12116. switch (starttype) {
  12117. case 60:
  12118. return this.parseBreakContinueStatement(node, true);
  12119. case 63:
  12120. return this.parseBreakContinueStatement(node, false);
  12121. case 64:
  12122. return this.parseDebuggerStatement(node);
  12123. case 90:
  12124. return this.parseDoStatement(node);
  12125. case 91:
  12126. return this.parseForStatement(node);
  12127. case 68:
  12128. if (this.lookaheadCharCode() === 46) break;
  12129. if (context) {
  12130. if (this.state.strict) {
  12131. this.raise(Errors.StrictFunction, {
  12132. at: this.state.startLoc
  12133. });
  12134. } else if (context !== "if" && context !== "label") {
  12135. this.raise(Errors.SloppyFunction, {
  12136. at: this.state.startLoc
  12137. });
  12138. }
  12139. }
  12140. return this.parseFunctionStatement(node, false, !context);
  12141. case 80:
  12142. if (context) this.unexpected();
  12143. return this.parseClass(node, true);
  12144. case 69:
  12145. return this.parseIfStatement(node);
  12146. case 70:
  12147. return this.parseReturnStatement(node);
  12148. case 71:
  12149. return this.parseSwitchStatement(node);
  12150. case 72:
  12151. return this.parseThrowStatement(node);
  12152. case 73:
  12153. return this.parseTryStatement(node);
  12154. case 75:
  12155. case 74:
  12156. kind = kind || this.state.value;
  12157. if (context && kind !== "var") {
  12158. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12159. at: this.state.startLoc
  12160. });
  12161. }
  12162. return this.parseVarStatement(node, kind);
  12163. case 92:
  12164. return this.parseWhileStatement(node);
  12165. case 76:
  12166. return this.parseWithStatement(node);
  12167. case 5:
  12168. return this.parseBlock();
  12169. case 13:
  12170. return this.parseEmptyStatement(node);
  12171. case 83:
  12172. {
  12173. const nextTokenCharCode = this.lookaheadCharCode();
  12174. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12175. break;
  12176. }
  12177. }
  12178. case 82:
  12179. {
  12180. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12181. this.raise(Errors.UnexpectedImportExport, {
  12182. at: this.state.startLoc
  12183. });
  12184. }
  12185. this.next();
  12186. let result;
  12187. if (starttype === 83) {
  12188. result = this.parseImport(node);
  12189. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12190. this.sawUnambiguousESM = true;
  12191. }
  12192. } else {
  12193. result = this.parseExport(node);
  12194. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12195. this.sawUnambiguousESM = true;
  12196. }
  12197. }
  12198. this.assertModuleNodeAllowed(node);
  12199. return result;
  12200. }
  12201. default:
  12202. {
  12203. if (this.isAsyncFunction()) {
  12204. if (context) {
  12205. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12206. at: this.state.startLoc
  12207. });
  12208. }
  12209. this.next();
  12210. return this.parseFunctionStatement(node, true, !context);
  12211. }
  12212. }
  12213. }
  12214. const maybeName = this.state.value;
  12215. const expr = this.parseExpression();
  12216. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12217. return this.parseLabeledStatement(node, maybeName, expr, context);
  12218. } else {
  12219. return this.parseExpressionStatement(node, expr);
  12220. }
  12221. }
  12222. assertModuleNodeAllowed(node) {
  12223. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12224. this.raise(Errors.ImportOutsideModule, {
  12225. at: node
  12226. });
  12227. }
  12228. }
  12229. takeDecorators(node) {
  12230. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12231. if (decorators.length) {
  12232. node.decorators = decorators;
  12233. this.resetStartLocationFromNode(node, decorators[0]);
  12234. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  12235. }
  12236. }
  12237. canHaveLeadingDecorator() {
  12238. return this.match(80);
  12239. }
  12240. parseDecorators(allowExport) {
  12241. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12242. while (this.match(26)) {
  12243. const decorator = this.parseDecorator();
  12244. currentContextDecorators.push(decorator);
  12245. }
  12246. if (this.match(82)) {
  12247. if (!allowExport) {
  12248. this.unexpected();
  12249. }
  12250. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12251. this.raise(Errors.DecoratorExportClass, {
  12252. at: this.state.startLoc
  12253. });
  12254. }
  12255. } else if (!this.canHaveLeadingDecorator()) {
  12256. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12257. at: this.state.startLoc
  12258. });
  12259. }
  12260. }
  12261. parseDecorator() {
  12262. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  12263. const node = this.startNode();
  12264. this.next();
  12265. if (this.hasPlugin("decorators")) {
  12266. this.state.decoratorStack.push([]);
  12267. const startPos = this.state.start;
  12268. const startLoc = this.state.startLoc;
  12269. let expr;
  12270. if (this.match(10)) {
  12271. const startPos = this.state.start;
  12272. const startLoc = this.state.startLoc;
  12273. this.next();
  12274. expr = this.parseExpression();
  12275. this.expect(11);
  12276. expr = this.wrapParenthesis(startPos, startLoc, expr);
  12277. } else {
  12278. expr = this.parseIdentifier(false);
  12279. while (this.eat(16)) {
  12280. const node = this.startNodeAt(startPos, startLoc);
  12281. node.object = expr;
  12282. node.property = this.parseIdentifier(true);
  12283. node.computed = false;
  12284. expr = this.finishNode(node, "MemberExpression");
  12285. }
  12286. }
  12287. node.expression = this.parseMaybeDecoratorArguments(expr);
  12288. this.state.decoratorStack.pop();
  12289. } else {
  12290. node.expression = this.parseExprSubscripts();
  12291. }
  12292. return this.finishNode(node, "Decorator");
  12293. }
  12294. parseMaybeDecoratorArguments(expr) {
  12295. if (this.eat(10)) {
  12296. const node = this.startNodeAtNode(expr);
  12297. node.callee = expr;
  12298. node.arguments = this.parseCallExpressionArguments(11, false);
  12299. this.toReferencedList(node.arguments);
  12300. return this.finishNode(node, "CallExpression");
  12301. }
  12302. return expr;
  12303. }
  12304. parseBreakContinueStatement(node, isBreak) {
  12305. this.next();
  12306. if (this.isLineTerminator()) {
  12307. node.label = null;
  12308. } else {
  12309. node.label = this.parseIdentifier();
  12310. this.semicolon();
  12311. }
  12312. this.verifyBreakContinue(node, isBreak);
  12313. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12314. }
  12315. verifyBreakContinue(node, isBreak) {
  12316. let i;
  12317. for (i = 0; i < this.state.labels.length; ++i) {
  12318. const lab = this.state.labels[i];
  12319. if (node.label == null || lab.name === node.label.name) {
  12320. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12321. if (node.label && isBreak) break;
  12322. }
  12323. }
  12324. if (i === this.state.labels.length) {
  12325. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12326. this.raise(Errors.IllegalBreakContinue, {
  12327. at: node,
  12328. type
  12329. });
  12330. }
  12331. }
  12332. parseDebuggerStatement(node) {
  12333. this.next();
  12334. this.semicolon();
  12335. return this.finishNode(node, "DebuggerStatement");
  12336. }
  12337. parseHeaderExpression() {
  12338. this.expect(10);
  12339. const val = this.parseExpression();
  12340. this.expect(11);
  12341. return val;
  12342. }
  12343. parseDoStatement(node) {
  12344. this.next();
  12345. this.state.labels.push(loopLabel);
  12346. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
  12347. this.state.labels.pop();
  12348. this.expect(92);
  12349. node.test = this.parseHeaderExpression();
  12350. this.eat(13);
  12351. return this.finishNode(node, "DoWhileStatement");
  12352. }
  12353. parseForStatement(node) {
  12354. this.next();
  12355. this.state.labels.push(loopLabel);
  12356. let awaitAt = null;
  12357. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12358. awaitAt = this.state.lastTokStartLoc;
  12359. }
  12360. this.scope.enter(SCOPE_OTHER);
  12361. this.expect(10);
  12362. if (this.match(13)) {
  12363. if (awaitAt !== null) {
  12364. this.unexpected(awaitAt);
  12365. }
  12366. return this.parseFor(node, null);
  12367. }
  12368. const startsWithLet = this.isContextual(99);
  12369. const isLet = startsWithLet && this.isLetKeyword();
  12370. if (this.match(74) || this.match(75) || isLet) {
  12371. const init = this.startNode();
  12372. const kind = isLet ? "let" : this.state.value;
  12373. this.next();
  12374. this.parseVar(init, true, kind);
  12375. this.finishNode(init, "VariableDeclaration");
  12376. if ((this.match(58) || this.isContextual(101)) && init.declarations.length === 1) {
  12377. return this.parseForIn(node, init, awaitAt);
  12378. }
  12379. if (awaitAt !== null) {
  12380. this.unexpected(awaitAt);
  12381. }
  12382. return this.parseFor(node, init);
  12383. }
  12384. const startsWithAsync = this.isContextual(95);
  12385. const refExpressionErrors = new ExpressionErrors();
  12386. const init = this.parseExpression(true, refExpressionErrors);
  12387. const isForOf = this.isContextual(101);
  12388. if (isForOf) {
  12389. if (startsWithLet) {
  12390. this.raise(Errors.ForOfLet, {
  12391. at: init
  12392. });
  12393. }
  12394. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12395. this.raise(Errors.ForOfAsync, {
  12396. at: init
  12397. });
  12398. }
  12399. }
  12400. if (isForOf || this.match(58)) {
  12401. this.checkDestructuringPrivate(refExpressionErrors);
  12402. this.toAssignable(init, true);
  12403. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12404. this.checkLVal(init, {
  12405. in: {
  12406. type
  12407. }
  12408. });
  12409. return this.parseForIn(node, init, awaitAt);
  12410. } else {
  12411. this.checkExpressionErrors(refExpressionErrors, true);
  12412. }
  12413. if (awaitAt !== null) {
  12414. this.unexpected(awaitAt);
  12415. }
  12416. return this.parseFor(node, init);
  12417. }
  12418. parseFunctionStatement(node, isAsync, declarationPosition) {
  12419. this.next();
  12420. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  12421. }
  12422. parseIfStatement(node) {
  12423. this.next();
  12424. node.test = this.parseHeaderExpression();
  12425. node.consequent = this.parseStatement("if");
  12426. node.alternate = this.eat(66) ? this.parseStatement("if") : null;
  12427. return this.finishNode(node, "IfStatement");
  12428. }
  12429. parseReturnStatement(node) {
  12430. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12431. this.raise(Errors.IllegalReturn, {
  12432. at: this.state.startLoc
  12433. });
  12434. }
  12435. this.next();
  12436. if (this.isLineTerminator()) {
  12437. node.argument = null;
  12438. } else {
  12439. node.argument = this.parseExpression();
  12440. this.semicolon();
  12441. }
  12442. return this.finishNode(node, "ReturnStatement");
  12443. }
  12444. parseSwitchStatement(node) {
  12445. this.next();
  12446. node.discriminant = this.parseHeaderExpression();
  12447. const cases = node.cases = [];
  12448. this.expect(5);
  12449. this.state.labels.push(switchLabel);
  12450. this.scope.enter(SCOPE_OTHER);
  12451. let cur;
  12452. for (let sawDefault; !this.match(8);) {
  12453. if (this.match(61) || this.match(65)) {
  12454. const isCase = this.match(61);
  12455. if (cur) this.finishNode(cur, "SwitchCase");
  12456. cases.push(cur = this.startNode());
  12457. cur.consequent = [];
  12458. this.next();
  12459. if (isCase) {
  12460. cur.test = this.parseExpression();
  12461. } else {
  12462. if (sawDefault) {
  12463. this.raise(Errors.MultipleDefaultsInSwitch, {
  12464. at: this.state.lastTokStartLoc
  12465. });
  12466. }
  12467. sawDefault = true;
  12468. cur.test = null;
  12469. }
  12470. this.expect(14);
  12471. } else {
  12472. if (cur) {
  12473. cur.consequent.push(this.parseStatement(null));
  12474. } else {
  12475. this.unexpected();
  12476. }
  12477. }
  12478. }
  12479. this.scope.exit();
  12480. if (cur) this.finishNode(cur, "SwitchCase");
  12481. this.next();
  12482. this.state.labels.pop();
  12483. return this.finishNode(node, "SwitchStatement");
  12484. }
  12485. parseThrowStatement(node) {
  12486. this.next();
  12487. if (this.hasPrecedingLineBreak()) {
  12488. this.raise(Errors.NewlineAfterThrow, {
  12489. at: this.state.lastTokEndLoc
  12490. });
  12491. }
  12492. node.argument = this.parseExpression();
  12493. this.semicolon();
  12494. return this.finishNode(node, "ThrowStatement");
  12495. }
  12496. parseCatchClauseParam() {
  12497. const param = this.parseBindingAtom();
  12498. const simple = param.type === "Identifier";
  12499. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  12500. this.checkLVal(param, {
  12501. in: {
  12502. type: "CatchClause"
  12503. },
  12504. binding: BIND_LEXICAL,
  12505. allowingSloppyLetBinding: true
  12506. });
  12507. return param;
  12508. }
  12509. parseTryStatement(node) {
  12510. this.next();
  12511. node.block = this.parseBlock();
  12512. node.handler = null;
  12513. if (this.match(62)) {
  12514. const clause = this.startNode();
  12515. this.next();
  12516. if (this.match(10)) {
  12517. this.expect(10);
  12518. clause.param = this.parseCatchClauseParam();
  12519. this.expect(11);
  12520. } else {
  12521. clause.param = null;
  12522. this.scope.enter(SCOPE_OTHER);
  12523. }
  12524. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12525. this.scope.exit();
  12526. node.handler = this.finishNode(clause, "CatchClause");
  12527. }
  12528. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12529. if (!node.handler && !node.finalizer) {
  12530. this.raise(Errors.NoCatchOrFinally, {
  12531. at: node
  12532. });
  12533. }
  12534. return this.finishNode(node, "TryStatement");
  12535. }
  12536. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12537. this.next();
  12538. this.parseVar(node, false, kind, allowMissingInitializer);
  12539. this.semicolon();
  12540. return this.finishNode(node, "VariableDeclaration");
  12541. }
  12542. parseWhileStatement(node) {
  12543. this.next();
  12544. node.test = this.parseHeaderExpression();
  12545. this.state.labels.push(loopLabel);
  12546. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
  12547. this.state.labels.pop();
  12548. return this.finishNode(node, "WhileStatement");
  12549. }
  12550. parseWithStatement(node) {
  12551. if (this.state.strict) {
  12552. this.raise(Errors.StrictWith, {
  12553. at: this.state.startLoc
  12554. });
  12555. }
  12556. this.next();
  12557. node.object = this.parseHeaderExpression();
  12558. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
  12559. return this.finishNode(node, "WithStatement");
  12560. }
  12561. parseEmptyStatement(node) {
  12562. this.next();
  12563. return this.finishNode(node, "EmptyStatement");
  12564. }
  12565. parseLabeledStatement(node, maybeName, expr, context) {
  12566. for (const label of this.state.labels) {
  12567. if (label.name === maybeName) {
  12568. this.raise(Errors.LabelRedeclaration, {
  12569. at: expr,
  12570. labelName: maybeName
  12571. });
  12572. }
  12573. }
  12574. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  12575. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  12576. const label = this.state.labels[i];
  12577. if (label.statementStart === node.start) {
  12578. label.statementStart = this.state.start;
  12579. label.kind = kind;
  12580. } else {
  12581. break;
  12582. }
  12583. }
  12584. this.state.labels.push({
  12585. name: maybeName,
  12586. kind: kind,
  12587. statementStart: this.state.start
  12588. });
  12589. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  12590. this.state.labels.pop();
  12591. node.label = expr;
  12592. return this.finishNode(node, "LabeledStatement");
  12593. }
  12594. parseExpressionStatement(node, expr) {
  12595. node.expression = expr;
  12596. this.semicolon();
  12597. return this.finishNode(node, "ExpressionStatement");
  12598. }
  12599. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  12600. const node = this.startNode();
  12601. if (allowDirectives) {
  12602. this.state.strictErrors.clear();
  12603. }
  12604. this.expect(5);
  12605. if (createNewLexicalScope) {
  12606. this.scope.enter(SCOPE_OTHER);
  12607. }
  12608. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  12609. if (createNewLexicalScope) {
  12610. this.scope.exit();
  12611. }
  12612. return this.finishNode(node, "BlockStatement");
  12613. }
  12614. isValidDirective(stmt) {
  12615. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  12616. }
  12617. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  12618. const body = node.body = [];
  12619. const directives = node.directives = [];
  12620. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  12621. }
  12622. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  12623. const oldStrict = this.state.strict;
  12624. let hasStrictModeDirective = false;
  12625. let parsedNonDirective = false;
  12626. while (!this.match(end)) {
  12627. const stmt = this.parseStatement(null, topLevel);
  12628. if (directives && !parsedNonDirective) {
  12629. if (this.isValidDirective(stmt)) {
  12630. const directive = this.stmtToDirective(stmt);
  12631. directives.push(directive);
  12632. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  12633. hasStrictModeDirective = true;
  12634. this.setStrict(true);
  12635. }
  12636. continue;
  12637. }
  12638. parsedNonDirective = true;
  12639. this.state.strictErrors.clear();
  12640. }
  12641. body.push(stmt);
  12642. }
  12643. if (afterBlockParse) {
  12644. afterBlockParse.call(this, hasStrictModeDirective);
  12645. }
  12646. if (!oldStrict) {
  12647. this.setStrict(false);
  12648. }
  12649. this.next();
  12650. }
  12651. parseFor(node, init) {
  12652. node.init = init;
  12653. this.semicolon(false);
  12654. node.test = this.match(13) ? null : this.parseExpression();
  12655. this.semicolon(false);
  12656. node.update = this.match(11) ? null : this.parseExpression();
  12657. this.expect(11);
  12658. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12659. this.scope.exit();
  12660. this.state.labels.pop();
  12661. return this.finishNode(node, "ForStatement");
  12662. }
  12663. parseForIn(node, init, awaitAt) {
  12664. const isForIn = this.match(58);
  12665. this.next();
  12666. if (isForIn) {
  12667. if (awaitAt !== null) this.unexpected(awaitAt);
  12668. } else {
  12669. node.await = awaitAt !== null;
  12670. }
  12671. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  12672. this.raise(Errors.ForInOfLoopInitializer, {
  12673. at: init,
  12674. type: isForIn ? "ForInStatement" : "ForOfStatement"
  12675. });
  12676. }
  12677. if (init.type === "AssignmentPattern") {
  12678. this.raise(Errors.InvalidLhs, {
  12679. at: init,
  12680. ancestor: {
  12681. type: "ForStatement"
  12682. }
  12683. });
  12684. }
  12685. node.left = init;
  12686. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  12687. this.expect(11);
  12688. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12689. this.scope.exit();
  12690. this.state.labels.pop();
  12691. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  12692. }
  12693. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  12694. const declarations = node.declarations = [];
  12695. node.kind = kind;
  12696. for (;;) {
  12697. const decl = this.startNode();
  12698. this.parseVarId(decl, kind);
  12699. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  12700. if (decl.init === null && !allowMissingInitializer) {
  12701. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  12702. this.raise(Errors.DeclarationMissingInitializer, {
  12703. at: this.state.lastTokEndLoc,
  12704. kind: "destructuring"
  12705. });
  12706. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  12707. this.raise(Errors.DeclarationMissingInitializer, {
  12708. at: this.state.lastTokEndLoc,
  12709. kind: "const"
  12710. });
  12711. }
  12712. }
  12713. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  12714. if (!this.eat(12)) break;
  12715. }
  12716. return node;
  12717. }
  12718. parseVarId(decl, kind) {
  12719. decl.id = this.parseBindingAtom();
  12720. this.checkLVal(decl.id, {
  12721. in: {
  12722. type: "VariableDeclarator"
  12723. },
  12724. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  12725. });
  12726. }
  12727. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  12728. const isStatement = statement & FUNC_STATEMENT;
  12729. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  12730. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  12731. this.initFunction(node, isAsync);
  12732. if (this.match(55) && isHangingStatement) {
  12733. this.raise(Errors.GeneratorInSingleStatementContext, {
  12734. at: this.state.startLoc
  12735. });
  12736. }
  12737. node.generator = this.eat(55);
  12738. if (isStatement) {
  12739. node.id = this.parseFunctionId(requireId);
  12740. }
  12741. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12742. this.state.maybeInArrowParameters = false;
  12743. this.scope.enter(SCOPE_FUNCTION);
  12744. this.prodParam.enter(functionFlags(isAsync, node.generator));
  12745. if (!isStatement) {
  12746. node.id = this.parseFunctionId();
  12747. }
  12748. this.parseFunctionParams(node, false);
  12749. this.withSmartMixTopicForbiddingContext(() => {
  12750. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  12751. });
  12752. this.prodParam.exit();
  12753. this.scope.exit();
  12754. if (isStatement && !isHangingStatement) {
  12755. this.registerFunctionStatementId(node);
  12756. }
  12757. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12758. return node;
  12759. }
  12760. parseFunctionId(requireId) {
  12761. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  12762. }
  12763. parseFunctionParams(node, allowModifiers) {
  12764. this.expect(10);
  12765. this.expressionScope.enter(newParameterDeclarationScope());
  12766. node.params = this.parseBindingList(11, 41, false, allowModifiers);
  12767. this.expressionScope.exit();
  12768. }
  12769. registerFunctionStatementId(node) {
  12770. if (!node.id) return;
  12771. 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);
  12772. }
  12773. parseClass(node, isStatement, optionalId) {
  12774. this.next();
  12775. this.takeDecorators(node);
  12776. const oldStrict = this.state.strict;
  12777. this.state.strict = true;
  12778. this.parseClassId(node, isStatement, optionalId);
  12779. this.parseClassSuper(node);
  12780. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  12781. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  12782. }
  12783. isClassProperty() {
  12784. return this.match(29) || this.match(13) || this.match(8);
  12785. }
  12786. isClassMethod() {
  12787. return this.match(10);
  12788. }
  12789. isNonstaticConstructor(method) {
  12790. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  12791. }
  12792. parseClassBody(hadSuperClass, oldStrict) {
  12793. this.classScope.enter();
  12794. const state = {
  12795. hadConstructor: false,
  12796. hadSuperClass
  12797. };
  12798. let decorators = [];
  12799. const classBody = this.startNode();
  12800. classBody.body = [];
  12801. this.expect(5);
  12802. this.withSmartMixTopicForbiddingContext(() => {
  12803. while (!this.match(8)) {
  12804. if (this.eat(13)) {
  12805. if (decorators.length > 0) {
  12806. throw this.raise(Errors.DecoratorSemicolon, {
  12807. at: this.state.lastTokEndLoc
  12808. });
  12809. }
  12810. continue;
  12811. }
  12812. if (this.match(26)) {
  12813. decorators.push(this.parseDecorator());
  12814. continue;
  12815. }
  12816. const member = this.startNode();
  12817. if (decorators.length) {
  12818. member.decorators = decorators;
  12819. this.resetStartLocationFromNode(member, decorators[0]);
  12820. decorators = [];
  12821. }
  12822. this.parseClassMember(classBody, member, state);
  12823. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  12824. this.raise(Errors.DecoratorConstructor, {
  12825. at: member
  12826. });
  12827. }
  12828. }
  12829. });
  12830. this.state.strict = oldStrict;
  12831. this.next();
  12832. if (decorators.length) {
  12833. throw this.raise(Errors.TrailingDecorator, {
  12834. at: this.state.startLoc
  12835. });
  12836. }
  12837. this.classScope.exit();
  12838. return this.finishNode(classBody, "ClassBody");
  12839. }
  12840. parseClassMemberFromModifier(classBody, member) {
  12841. const key = this.parseIdentifier(true);
  12842. if (this.isClassMethod()) {
  12843. const method = member;
  12844. method.kind = "method";
  12845. method.computed = false;
  12846. method.key = key;
  12847. method.static = false;
  12848. this.pushClassMethod(classBody, method, false, false, false, false);
  12849. return true;
  12850. } else if (this.isClassProperty()) {
  12851. const prop = member;
  12852. prop.computed = false;
  12853. prop.key = key;
  12854. prop.static = false;
  12855. classBody.body.push(this.parseClassProperty(prop));
  12856. return true;
  12857. }
  12858. this.resetPreviousNodeTrailingComments(key);
  12859. return false;
  12860. }
  12861. parseClassMember(classBody, member, state) {
  12862. const isStatic = this.isContextual(104);
  12863. if (isStatic) {
  12864. if (this.parseClassMemberFromModifier(classBody, member)) {
  12865. return;
  12866. }
  12867. if (this.eat(5)) {
  12868. this.parseClassStaticBlock(classBody, member);
  12869. return;
  12870. }
  12871. }
  12872. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  12873. }
  12874. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  12875. const publicMethod = member;
  12876. const privateMethod = member;
  12877. const publicProp = member;
  12878. const privateProp = member;
  12879. const accessorProp = member;
  12880. const method = publicMethod;
  12881. const publicMember = publicMethod;
  12882. member.static = isStatic;
  12883. this.parsePropertyNamePrefixOperator(member);
  12884. if (this.eat(55)) {
  12885. method.kind = "method";
  12886. const isPrivateName = this.match(134);
  12887. this.parseClassElementName(method);
  12888. if (isPrivateName) {
  12889. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  12890. return;
  12891. }
  12892. if (this.isNonstaticConstructor(publicMethod)) {
  12893. this.raise(Errors.ConstructorIsGenerator, {
  12894. at: publicMethod.key
  12895. });
  12896. }
  12897. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  12898. return;
  12899. }
  12900. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  12901. const isPrivate = this.match(134);
  12902. const key = this.parseClassElementName(member);
  12903. const maybeQuestionTokenStartLoc = this.state.startLoc;
  12904. this.parsePostMemberNameModifiers(publicMember);
  12905. if (this.isClassMethod()) {
  12906. method.kind = "method";
  12907. if (isPrivate) {
  12908. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  12909. return;
  12910. }
  12911. const isConstructor = this.isNonstaticConstructor(publicMethod);
  12912. let allowsDirectSuper = false;
  12913. if (isConstructor) {
  12914. publicMethod.kind = "constructor";
  12915. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  12916. this.raise(Errors.DuplicateConstructor, {
  12917. at: key
  12918. });
  12919. }
  12920. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  12921. this.raise(Errors.OverrideOnConstructor, {
  12922. at: key
  12923. });
  12924. }
  12925. state.hadConstructor = true;
  12926. allowsDirectSuper = state.hadSuperClass;
  12927. }
  12928. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  12929. } else if (this.isClassProperty()) {
  12930. if (isPrivate) {
  12931. this.pushClassPrivateProperty(classBody, privateProp);
  12932. } else {
  12933. this.pushClassProperty(classBody, publicProp);
  12934. }
  12935. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  12936. this.resetPreviousNodeTrailingComments(key);
  12937. const isGenerator = this.eat(55);
  12938. if (publicMember.optional) {
  12939. this.unexpected(maybeQuestionTokenStartLoc);
  12940. }
  12941. method.kind = "method";
  12942. const isPrivate = this.match(134);
  12943. this.parseClassElementName(method);
  12944. this.parsePostMemberNameModifiers(publicMember);
  12945. if (isPrivate) {
  12946. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  12947. } else {
  12948. if (this.isNonstaticConstructor(publicMethod)) {
  12949. this.raise(Errors.ConstructorIsAsync, {
  12950. at: publicMethod.key
  12951. });
  12952. }
  12953. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  12954. }
  12955. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  12956. this.resetPreviousNodeTrailingComments(key);
  12957. method.kind = key.name;
  12958. const isPrivate = this.match(134);
  12959. this.parseClassElementName(publicMethod);
  12960. if (isPrivate) {
  12961. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  12962. } else {
  12963. if (this.isNonstaticConstructor(publicMethod)) {
  12964. this.raise(Errors.ConstructorIsAccessor, {
  12965. at: publicMethod.key
  12966. });
  12967. }
  12968. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  12969. }
  12970. this.checkGetterSetterParams(publicMethod);
  12971. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  12972. this.expectPlugin("decoratorAutoAccessors");
  12973. this.resetPreviousNodeTrailingComments(key);
  12974. const isPrivate = this.match(134);
  12975. this.parseClassElementName(publicProp);
  12976. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  12977. } else if (this.isLineTerminator()) {
  12978. if (isPrivate) {
  12979. this.pushClassPrivateProperty(classBody, privateProp);
  12980. } else {
  12981. this.pushClassProperty(classBody, publicProp);
  12982. }
  12983. } else {
  12984. this.unexpected();
  12985. }
  12986. }
  12987. parseClassElementName(member) {
  12988. const {
  12989. type,
  12990. value
  12991. } = this.state;
  12992. if ((type === 128 || type === 129) && member.static && value === "prototype") {
  12993. this.raise(Errors.StaticPrototype, {
  12994. at: this.state.startLoc
  12995. });
  12996. }
  12997. if (type === 134) {
  12998. if (value === "constructor") {
  12999. this.raise(Errors.ConstructorClassPrivateField, {
  13000. at: this.state.startLoc
  13001. });
  13002. }
  13003. const key = this.parsePrivateName();
  13004. member.key = key;
  13005. return key;
  13006. }
  13007. return this.parsePropertyName(member);
  13008. }
  13009. parseClassStaticBlock(classBody, member) {
  13010. var _member$decorators;
  13011. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13012. const oldLabels = this.state.labels;
  13013. this.state.labels = [];
  13014. this.prodParam.enter(PARAM);
  13015. const body = member.body = [];
  13016. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13017. this.prodParam.exit();
  13018. this.scope.exit();
  13019. this.state.labels = oldLabels;
  13020. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13021. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13022. this.raise(Errors.DecoratorStaticBlock, {
  13023. at: member
  13024. });
  13025. }
  13026. }
  13027. pushClassProperty(classBody, prop) {
  13028. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13029. this.raise(Errors.ConstructorClassField, {
  13030. at: prop.key
  13031. });
  13032. }
  13033. classBody.body.push(this.parseClassProperty(prop));
  13034. }
  13035. pushClassPrivateProperty(classBody, prop) {
  13036. const node = this.parseClassPrivateProperty(prop);
  13037. classBody.body.push(node);
  13038. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13039. }
  13040. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13041. if (!isPrivate && !prop.computed) {
  13042. const key = prop.key;
  13043. if (key.name === "constructor" || key.value === "constructor") {
  13044. this.raise(Errors.ConstructorClassField, {
  13045. at: key
  13046. });
  13047. }
  13048. }
  13049. const node = this.parseClassAccessorProperty(prop);
  13050. classBody.body.push(node);
  13051. if (isPrivate) {
  13052. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13053. }
  13054. }
  13055. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13056. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13057. }
  13058. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13059. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13060. classBody.body.push(node);
  13061. 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;
  13062. this.declareClassPrivateMethodInScope(node, kind);
  13063. }
  13064. declareClassPrivateMethodInScope(node, kind) {
  13065. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13066. }
  13067. parsePostMemberNameModifiers(methodOrProp) {}
  13068. parseClassPrivateProperty(node) {
  13069. this.parseInitializer(node);
  13070. this.semicolon();
  13071. return this.finishNode(node, "ClassPrivateProperty");
  13072. }
  13073. parseClassProperty(node) {
  13074. this.parseInitializer(node);
  13075. this.semicolon();
  13076. return this.finishNode(node, "ClassProperty");
  13077. }
  13078. parseClassAccessorProperty(node) {
  13079. this.parseInitializer(node);
  13080. this.semicolon();
  13081. return this.finishNode(node, "ClassAccessorProperty");
  13082. }
  13083. parseInitializer(node) {
  13084. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13085. this.expressionScope.enter(newExpressionScope());
  13086. this.prodParam.enter(PARAM);
  13087. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13088. this.expressionScope.exit();
  13089. this.prodParam.exit();
  13090. this.scope.exit();
  13091. }
  13092. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13093. if (tokenIsIdentifier(this.state.type)) {
  13094. node.id = this.parseIdentifier();
  13095. if (isStatement) {
  13096. this.declareNameFromIdentifier(node.id, bindingType);
  13097. }
  13098. } else {
  13099. if (optionalId || !isStatement) {
  13100. node.id = null;
  13101. } else {
  13102. throw this.raise(Errors.MissingClassName, {
  13103. at: this.state.startLoc
  13104. });
  13105. }
  13106. }
  13107. }
  13108. parseClassSuper(node) {
  13109. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13110. }
  13111. parseExport(node) {
  13112. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13113. const parseAfterDefault = !hasDefault || this.eat(12);
  13114. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13115. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13116. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13117. const isFromRequired = hasDefault || hasStar;
  13118. if (hasStar && !hasNamespace) {
  13119. if (hasDefault) this.unexpected();
  13120. this.parseExportFrom(node, true);
  13121. return this.finishNode(node, "ExportAllDeclaration");
  13122. }
  13123. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13124. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  13125. throw this.unexpected(null, 5);
  13126. }
  13127. let hasDeclaration;
  13128. if (isFromRequired || hasSpecifiers) {
  13129. hasDeclaration = false;
  13130. this.parseExportFrom(node, isFromRequired);
  13131. } else {
  13132. hasDeclaration = this.maybeParseExportDeclaration(node);
  13133. }
  13134. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13135. this.checkExport(node, true, false, !!node.source);
  13136. return this.finishNode(node, "ExportNamedDeclaration");
  13137. }
  13138. if (this.eat(65)) {
  13139. node.declaration = this.parseExportDefaultExpression();
  13140. this.checkExport(node, true, true);
  13141. return this.finishNode(node, "ExportDefaultDeclaration");
  13142. }
  13143. throw this.unexpected(null, 5);
  13144. }
  13145. eatExportStar(node) {
  13146. return this.eat(55);
  13147. }
  13148. maybeParseExportDefaultSpecifier(node) {
  13149. if (this.isExportDefaultSpecifier()) {
  13150. this.expectPlugin("exportDefaultFrom");
  13151. const specifier = this.startNode();
  13152. specifier.exported = this.parseIdentifier(true);
  13153. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13154. return true;
  13155. }
  13156. return false;
  13157. }
  13158. maybeParseExportNamespaceSpecifier(node) {
  13159. if (this.isContextual(93)) {
  13160. if (!node.specifiers) node.specifiers = [];
  13161. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  13162. this.next();
  13163. specifier.exported = this.parseModuleExportName();
  13164. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13165. return true;
  13166. }
  13167. return false;
  13168. }
  13169. maybeParseExportNamedSpecifiers(node) {
  13170. if (this.match(5)) {
  13171. if (!node.specifiers) node.specifiers = [];
  13172. const isTypeExport = node.exportKind === "type";
  13173. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13174. node.source = null;
  13175. node.declaration = null;
  13176. if (this.hasPlugin("importAssertions")) {
  13177. node.assertions = [];
  13178. }
  13179. return true;
  13180. }
  13181. return false;
  13182. }
  13183. maybeParseExportDeclaration(node) {
  13184. if (this.shouldParseExportDeclaration()) {
  13185. node.specifiers = [];
  13186. node.source = null;
  13187. if (this.hasPlugin("importAssertions")) {
  13188. node.assertions = [];
  13189. }
  13190. node.declaration = this.parseExportDeclaration(node);
  13191. return true;
  13192. }
  13193. return false;
  13194. }
  13195. isAsyncFunction() {
  13196. if (!this.isContextual(95)) return false;
  13197. const next = this.nextTokenStart();
  13198. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13199. }
  13200. parseExportDefaultExpression() {
  13201. const expr = this.startNode();
  13202. const isAsync = this.isAsyncFunction();
  13203. if (this.match(68) || isAsync) {
  13204. this.next();
  13205. if (isAsync) {
  13206. this.next();
  13207. }
  13208. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  13209. }
  13210. if (this.match(80)) {
  13211. return this.parseClass(expr, true, true);
  13212. }
  13213. if (this.match(26)) {
  13214. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13215. this.raise(Errors.DecoratorBeforeExport, {
  13216. at: this.state.startLoc
  13217. });
  13218. }
  13219. this.parseDecorators(false);
  13220. return this.parseClass(expr, true, true);
  13221. }
  13222. if (this.match(75) || this.match(74) || this.isLet()) {
  13223. throw this.raise(Errors.UnsupportedDefaultExport, {
  13224. at: this.state.startLoc
  13225. });
  13226. }
  13227. const res = this.parseMaybeAssignAllowIn();
  13228. this.semicolon();
  13229. return res;
  13230. }
  13231. parseExportDeclaration(node) {
  13232. return this.parseStatement(null);
  13233. }
  13234. isExportDefaultSpecifier() {
  13235. const {
  13236. type
  13237. } = this.state;
  13238. if (tokenIsIdentifier(type)) {
  13239. if (type === 95 && !this.state.containsEsc || type === 99) {
  13240. return false;
  13241. }
  13242. if ((type === 126 || type === 125) && !this.state.containsEsc) {
  13243. const {
  13244. type: nextType
  13245. } = this.lookahead();
  13246. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13247. this.expectOnePlugin(["flow", "typescript"]);
  13248. return false;
  13249. }
  13250. }
  13251. } else if (!this.match(65)) {
  13252. return false;
  13253. }
  13254. const next = this.nextTokenStart();
  13255. const hasFrom = this.isUnparsedContextual(next, "from");
  13256. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13257. return true;
  13258. }
  13259. if (this.match(65) && hasFrom) {
  13260. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13261. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13262. }
  13263. return false;
  13264. }
  13265. parseExportFrom(node, expect) {
  13266. if (this.eatContextual(97)) {
  13267. node.source = this.parseImportSource();
  13268. this.checkExport(node);
  13269. const assertions = this.maybeParseImportAssertions();
  13270. if (assertions) {
  13271. node.assertions = assertions;
  13272. }
  13273. } else if (expect) {
  13274. this.unexpected();
  13275. }
  13276. this.semicolon();
  13277. }
  13278. shouldParseExportDeclaration() {
  13279. const {
  13280. type
  13281. } = this.state;
  13282. if (type === 26) {
  13283. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13284. if (this.hasPlugin("decorators")) {
  13285. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13286. throw this.raise(Errors.DecoratorBeforeExport, {
  13287. at: this.state.startLoc
  13288. });
  13289. }
  13290. return true;
  13291. }
  13292. }
  13293. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13294. }
  13295. checkExport(node, checkNames, isDefault, isFrom) {
  13296. if (checkNames) {
  13297. if (isDefault) {
  13298. this.checkDuplicateExports(node, "default");
  13299. if (this.hasPlugin("exportDefaultFrom")) {
  13300. var _declaration$extra;
  13301. const declaration = node.declaration;
  13302. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13303. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13304. at: declaration
  13305. });
  13306. }
  13307. }
  13308. } else if (node.specifiers && node.specifiers.length) {
  13309. for (const specifier of node.specifiers) {
  13310. const {
  13311. exported
  13312. } = specifier;
  13313. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13314. this.checkDuplicateExports(specifier, exportName);
  13315. if (!isFrom && specifier.local) {
  13316. const {
  13317. local
  13318. } = specifier;
  13319. if (local.type !== "Identifier") {
  13320. this.raise(Errors.ExportBindingIsString, {
  13321. at: specifier,
  13322. localName: local.value,
  13323. exportName
  13324. });
  13325. } else {
  13326. this.checkReservedWord(local.name, local.loc.start, true, false);
  13327. this.scope.checkLocalExport(local);
  13328. }
  13329. }
  13330. }
  13331. } else if (node.declaration) {
  13332. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13333. const id = node.declaration.id;
  13334. if (!id) throw new Error("Assertion failure");
  13335. this.checkDuplicateExports(node, id.name);
  13336. } else if (node.declaration.type === "VariableDeclaration") {
  13337. for (const declaration of node.declaration.declarations) {
  13338. this.checkDeclaration(declaration.id);
  13339. }
  13340. }
  13341. }
  13342. }
  13343. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  13344. if (currentContextDecorators.length) {
  13345. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13346. at: node
  13347. });
  13348. }
  13349. }
  13350. checkDeclaration(node) {
  13351. if (node.type === "Identifier") {
  13352. this.checkDuplicateExports(node, node.name);
  13353. } else if (node.type === "ObjectPattern") {
  13354. for (const prop of node.properties) {
  13355. this.checkDeclaration(prop);
  13356. }
  13357. } else if (node.type === "ArrayPattern") {
  13358. for (const elem of node.elements) {
  13359. if (elem) {
  13360. this.checkDeclaration(elem);
  13361. }
  13362. }
  13363. } else if (node.type === "ObjectProperty") {
  13364. this.checkDeclaration(node.value);
  13365. } else if (node.type === "RestElement") {
  13366. this.checkDeclaration(node.argument);
  13367. } else if (node.type === "AssignmentPattern") {
  13368. this.checkDeclaration(node.left);
  13369. }
  13370. }
  13371. checkDuplicateExports(node, exportName) {
  13372. if (this.exportedIdentifiers.has(exportName)) {
  13373. if (exportName === "default") {
  13374. this.raise(Errors.DuplicateDefaultExport, {
  13375. at: node
  13376. });
  13377. } else {
  13378. this.raise(Errors.DuplicateExport, {
  13379. at: node,
  13380. exportName
  13381. });
  13382. }
  13383. }
  13384. this.exportedIdentifiers.add(exportName);
  13385. }
  13386. parseExportSpecifiers(isInTypeExport) {
  13387. const nodes = [];
  13388. let first = true;
  13389. this.expect(5);
  13390. while (!this.eat(8)) {
  13391. if (first) {
  13392. first = false;
  13393. } else {
  13394. this.expect(12);
  13395. if (this.eat(8)) break;
  13396. }
  13397. const isMaybeTypeOnly = this.isContextual(126);
  13398. const isString = this.match(129);
  13399. const node = this.startNode();
  13400. node.local = this.parseModuleExportName();
  13401. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13402. }
  13403. return nodes;
  13404. }
  13405. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13406. if (this.eatContextual(93)) {
  13407. node.exported = this.parseModuleExportName();
  13408. } else if (isString) {
  13409. node.exported = cloneStringLiteral(node.local);
  13410. } else if (!node.exported) {
  13411. node.exported = cloneIdentifier(node.local);
  13412. }
  13413. return this.finishNode(node, "ExportSpecifier");
  13414. }
  13415. parseModuleExportName() {
  13416. if (this.match(129)) {
  13417. const result = this.parseStringLiteral(this.state.value);
  13418. const surrogate = result.value.match(loneSurrogate);
  13419. if (surrogate) {
  13420. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13421. at: result,
  13422. surrogateCharCode: surrogate[0].charCodeAt(0)
  13423. });
  13424. }
  13425. return result;
  13426. }
  13427. return this.parseIdentifier(true);
  13428. }
  13429. parseImport(node) {
  13430. node.specifiers = [];
  13431. if (!this.match(129)) {
  13432. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  13433. const parseNext = !hasDefault || this.eat(12);
  13434. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  13435. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  13436. this.expectContextual(97);
  13437. }
  13438. node.source = this.parseImportSource();
  13439. const assertions = this.maybeParseImportAssertions();
  13440. if (assertions) {
  13441. node.assertions = assertions;
  13442. } else {
  13443. const attributes = this.maybeParseModuleAttributes();
  13444. if (attributes) {
  13445. node.attributes = attributes;
  13446. }
  13447. }
  13448. this.semicolon();
  13449. return this.finishNode(node, "ImportDeclaration");
  13450. }
  13451. parseImportSource() {
  13452. if (!this.match(129)) this.unexpected();
  13453. return this.parseExprAtom();
  13454. }
  13455. shouldParseDefaultImport(node) {
  13456. return tokenIsIdentifier(this.state.type);
  13457. }
  13458. parseImportSpecifierLocal(node, specifier, type) {
  13459. specifier.local = this.parseIdentifier();
  13460. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  13461. }
  13462. finishImportSpecifier(specifier, type) {
  13463. this.checkLVal(specifier.local, {
  13464. in: specifier,
  13465. binding: BIND_LEXICAL
  13466. });
  13467. return this.finishNode(specifier, type);
  13468. }
  13469. parseAssertEntries() {
  13470. const attrs = [];
  13471. const attrNames = new Set();
  13472. do {
  13473. if (this.match(8)) {
  13474. break;
  13475. }
  13476. const node = this.startNode();
  13477. const keyName = this.state.value;
  13478. if (attrNames.has(keyName)) {
  13479. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13480. at: this.state.startLoc,
  13481. key: keyName
  13482. });
  13483. }
  13484. attrNames.add(keyName);
  13485. if (this.match(129)) {
  13486. node.key = this.parseStringLiteral(keyName);
  13487. } else {
  13488. node.key = this.parseIdentifier(true);
  13489. }
  13490. this.expect(14);
  13491. if (!this.match(129)) {
  13492. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13493. at: this.state.startLoc
  13494. });
  13495. }
  13496. node.value = this.parseStringLiteral(this.state.value);
  13497. this.finishNode(node, "ImportAttribute");
  13498. attrs.push(node);
  13499. } while (this.eat(12));
  13500. return attrs;
  13501. }
  13502. maybeParseModuleAttributes() {
  13503. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  13504. this.expectPlugin("moduleAttributes");
  13505. this.next();
  13506. } else {
  13507. if (this.hasPlugin("moduleAttributes")) return [];
  13508. return null;
  13509. }
  13510. const attrs = [];
  13511. const attributes = new Set();
  13512. do {
  13513. const node = this.startNode();
  13514. node.key = this.parseIdentifier(true);
  13515. if (node.key.name !== "type") {
  13516. this.raise(Errors.ModuleAttributeDifferentFromType, {
  13517. at: node.key
  13518. });
  13519. }
  13520. if (attributes.has(node.key.name)) {
  13521. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13522. at: node.key,
  13523. key: node.key.name
  13524. });
  13525. }
  13526. attributes.add(node.key.name);
  13527. this.expect(14);
  13528. if (!this.match(129)) {
  13529. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13530. at: this.state.startLoc
  13531. });
  13532. }
  13533. node.value = this.parseStringLiteral(this.state.value);
  13534. this.finishNode(node, "ImportAttribute");
  13535. attrs.push(node);
  13536. } while (this.eat(12));
  13537. return attrs;
  13538. }
  13539. maybeParseImportAssertions() {
  13540. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  13541. this.expectPlugin("importAssertions");
  13542. this.next();
  13543. } else {
  13544. if (this.hasPlugin("importAssertions")) return [];
  13545. return null;
  13546. }
  13547. this.eat(5);
  13548. const attrs = this.parseAssertEntries();
  13549. this.eat(8);
  13550. return attrs;
  13551. }
  13552. maybeParseDefaultImportSpecifier(node) {
  13553. if (this.shouldParseDefaultImport(node)) {
  13554. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  13555. return true;
  13556. }
  13557. return false;
  13558. }
  13559. maybeParseStarImportSpecifier(node) {
  13560. if (this.match(55)) {
  13561. const specifier = this.startNode();
  13562. this.next();
  13563. this.expectContextual(93);
  13564. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  13565. return true;
  13566. }
  13567. return false;
  13568. }
  13569. parseNamedImportSpecifiers(node) {
  13570. let first = true;
  13571. this.expect(5);
  13572. while (!this.eat(8)) {
  13573. if (first) {
  13574. first = false;
  13575. } else {
  13576. if (this.eat(14)) {
  13577. throw this.raise(Errors.DestructureNamedImport, {
  13578. at: this.state.startLoc
  13579. });
  13580. }
  13581. this.expect(12);
  13582. if (this.eat(8)) break;
  13583. }
  13584. const specifier = this.startNode();
  13585. const importedIsString = this.match(129);
  13586. const isMaybeTypeOnly = this.isContextual(126);
  13587. specifier.imported = this.parseModuleExportName();
  13588. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly);
  13589. node.specifiers.push(importSpecifier);
  13590. }
  13591. }
  13592. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  13593. if (this.eatContextual(93)) {
  13594. specifier.local = this.parseIdentifier();
  13595. } else {
  13596. const {
  13597. imported
  13598. } = specifier;
  13599. if (importedIsString) {
  13600. throw this.raise(Errors.ImportBindingIsString, {
  13601. at: specifier,
  13602. importName: imported.value
  13603. });
  13604. }
  13605. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  13606. if (!specifier.local) {
  13607. specifier.local = cloneIdentifier(imported);
  13608. }
  13609. }
  13610. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  13611. }
  13612. isThisParam(param) {
  13613. return param.type === "Identifier" && param.name === "this";
  13614. }
  13615. }
  13616. class Parser extends StatementParser {
  13617. constructor(options, input) {
  13618. options = getOptions(options);
  13619. super(options, input);
  13620. this.options = options;
  13621. this.initializeScopes();
  13622. this.plugins = pluginsMap(this.options.plugins);
  13623. this.filename = options.sourceFilename;
  13624. }
  13625. getScopeHandler() {
  13626. return ScopeHandler;
  13627. }
  13628. parse() {
  13629. this.enterInitialScopes();
  13630. const file = this.startNode();
  13631. const program = this.startNode();
  13632. this.nextToken();
  13633. file.errors = null;
  13634. this.parseTopLevel(file, program);
  13635. file.errors = this.state.errors;
  13636. return file;
  13637. }
  13638. }
  13639. function pluginsMap(plugins) {
  13640. const pluginMap = new Map();
  13641. for (const plugin of plugins) {
  13642. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  13643. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  13644. }
  13645. return pluginMap;
  13646. }
  13647. function parse(input, options) {
  13648. var _options;
  13649. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  13650. options = Object.assign({}, options);
  13651. try {
  13652. options.sourceType = "module";
  13653. const parser = getParser(options, input);
  13654. const ast = parser.parse();
  13655. if (parser.sawUnambiguousESM) {
  13656. return ast;
  13657. }
  13658. if (parser.ambiguousScriptDifferentAst) {
  13659. try {
  13660. options.sourceType = "script";
  13661. return getParser(options, input).parse();
  13662. } catch (_unused) {}
  13663. } else {
  13664. ast.program.sourceType = "script";
  13665. }
  13666. return ast;
  13667. } catch (moduleError) {
  13668. try {
  13669. options.sourceType = "script";
  13670. return getParser(options, input).parse();
  13671. } catch (_unused2) {}
  13672. throw moduleError;
  13673. }
  13674. } else {
  13675. return getParser(options, input).parse();
  13676. }
  13677. }
  13678. function parseExpression(input, options) {
  13679. const parser = getParser(options, input);
  13680. if (parser.options.strictMode) {
  13681. parser.state.strict = true;
  13682. }
  13683. return parser.getExpression();
  13684. }
  13685. function generateExportedTokenTypes(internalTokenTypes) {
  13686. const tokenTypes = {};
  13687. for (const typeName of Object.keys(internalTokenTypes)) {
  13688. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  13689. }
  13690. return tokenTypes;
  13691. }
  13692. const tokTypes = generateExportedTokenTypes(tt);
  13693. function getParser(options, input) {
  13694. let cls = Parser;
  13695. if (options != null && options.plugins) {
  13696. validatePlugins(options.plugins);
  13697. cls = getParserClass(options.plugins);
  13698. }
  13699. return new cls(options, input);
  13700. }
  13701. const parserClassCache = {};
  13702. function getParserClass(pluginsFromOptions) {
  13703. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  13704. const key = pluginList.join("/");
  13705. let cls = parserClassCache[key];
  13706. if (!cls) {
  13707. cls = Parser;
  13708. for (const plugin of pluginList) {
  13709. cls = mixinPlugins[plugin](cls);
  13710. }
  13711. parserClassCache[key] = cls;
  13712. }
  13713. return cls;
  13714. }
  13715. exports.parse = parse;
  13716. exports.parseExpression = parseExpression;
  13717. exports.tokTypes = tokTypes;
  13718. //# sourceMappingURL=index.js.map