index.js 442 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. class Position {
  4. constructor(line, col, index) {
  5. this.line = void 0;
  6. this.column = void 0;
  7. this.index = void 0;
  8. this.line = line;
  9. this.column = col;
  10. indexes.set(this, index);
  11. }
  12. }
  13. class SourceLocation {
  14. constructor(start, end) {
  15. this.start = void 0;
  16. this.end = void 0;
  17. this.filename = void 0;
  18. this.identifierName = void 0;
  19. this.start = start;
  20. this.end = end;
  21. }
  22. }
  23. const indexes = new WeakMap();
  24. function createPositionWithColumnOffset(position, columnOffset) {
  25. const {
  26. line,
  27. column
  28. } = position;
  29. return new Position(line, column + columnOffset, indexes.get(position) + columnOffset);
  30. }
  31. class BaseParser {
  32. constructor() {
  33. this.sawUnambiguousESM = false;
  34. this.ambiguousScriptDifferentAst = false;
  35. }
  36. hasPlugin(pluginConfig) {
  37. if (typeof pluginConfig === "string") {
  38. return this.plugins.has(pluginConfig);
  39. } else {
  40. const [pluginName, pluginOptions] = pluginConfig;
  41. if (!this.hasPlugin(pluginName)) {
  42. return false;
  43. }
  44. const actualOptions = this.plugins.get(pluginName);
  45. for (const key of Object.keys(pluginOptions)) {
  46. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. }
  53. getPluginOption(plugin, name) {
  54. var _this$plugins$get;
  55. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  56. }
  57. }
  58. function setTrailingComments(node, comments) {
  59. if (node.trailingComments === undefined) {
  60. node.trailingComments = comments;
  61. } else {
  62. node.trailingComments.unshift(...comments);
  63. }
  64. }
  65. function setLeadingComments(node, comments) {
  66. if (node.leadingComments === undefined) {
  67. node.leadingComments = comments;
  68. } else {
  69. node.leadingComments.unshift(...comments);
  70. }
  71. }
  72. function setInnerComments(node, comments) {
  73. if (node.innerComments === undefined) {
  74. node.innerComments = comments;
  75. } else {
  76. node.innerComments.unshift(...comments);
  77. }
  78. }
  79. function adjustInnerComments(node, elements, commentWS) {
  80. let lastElement = null;
  81. let i = elements.length;
  82. while (lastElement === null && i > 0) {
  83. lastElement = elements[--i];
  84. }
  85. if (lastElement === null || lastElement.start > commentWS.start) {
  86. setInnerComments(node, commentWS.comments);
  87. } else {
  88. setTrailingComments(lastElement, commentWS.comments);
  89. }
  90. }
  91. class CommentsParser extends BaseParser {
  92. addComment(comment) {
  93. if (this.filename) comment.loc.filename = this.filename;
  94. this.state.comments.push(comment);
  95. }
  96. processComment(node) {
  97. const {
  98. commentStack
  99. } = this.state;
  100. const commentStackLength = commentStack.length;
  101. if (commentStackLength === 0) return;
  102. let i = commentStackLength - 1;
  103. const lastCommentWS = commentStack[i];
  104. if (lastCommentWS.start === node.end) {
  105. lastCommentWS.leadingNode = node;
  106. i--;
  107. }
  108. const {
  109. start: nodeStart
  110. } = node;
  111. for (; i >= 0; i--) {
  112. const commentWS = commentStack[i];
  113. const commentEnd = commentWS.end;
  114. if (commentEnd > nodeStart) {
  115. commentWS.containingNode = node;
  116. this.finalizeComment(commentWS);
  117. commentStack.splice(i, 1);
  118. } else {
  119. if (commentEnd === nodeStart) {
  120. commentWS.trailingNode = node;
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. finalizeComment(commentWS) {
  127. const {
  128. comments
  129. } = commentWS;
  130. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  131. if (commentWS.leadingNode !== null) {
  132. setTrailingComments(commentWS.leadingNode, comments);
  133. }
  134. if (commentWS.trailingNode !== null) {
  135. setLeadingComments(commentWS.trailingNode, comments);
  136. }
  137. } else {
  138. const {
  139. containingNode: node,
  140. start: commentStart
  141. } = commentWS;
  142. if (this.input.charCodeAt(commentStart - 1) === 44) {
  143. switch (node.type) {
  144. case "ObjectExpression":
  145. case "ObjectPattern":
  146. case "RecordExpression":
  147. adjustInnerComments(node, node.properties, commentWS);
  148. break;
  149. case "CallExpression":
  150. case "OptionalCallExpression":
  151. adjustInnerComments(node, node.arguments, commentWS);
  152. break;
  153. case "FunctionDeclaration":
  154. case "FunctionExpression":
  155. case "ArrowFunctionExpression":
  156. case "ObjectMethod":
  157. case "ClassMethod":
  158. case "ClassPrivateMethod":
  159. adjustInnerComments(node, node.params, commentWS);
  160. break;
  161. case "ArrayExpression":
  162. case "ArrayPattern":
  163. case "TupleExpression":
  164. adjustInnerComments(node, node.elements, commentWS);
  165. break;
  166. case "ExportNamedDeclaration":
  167. case "ImportDeclaration":
  168. adjustInnerComments(node, node.specifiers, commentWS);
  169. break;
  170. default:
  171. {
  172. setInnerComments(node, comments);
  173. }
  174. }
  175. } else {
  176. setInnerComments(node, comments);
  177. }
  178. }
  179. }
  180. finalizeRemainingComments() {
  181. const {
  182. commentStack
  183. } = this.state;
  184. for (let i = commentStack.length - 1; i >= 0; i--) {
  185. this.finalizeComment(commentStack[i]);
  186. }
  187. this.state.commentStack = [];
  188. }
  189. resetPreviousNodeTrailingComments(node) {
  190. const {
  191. commentStack
  192. } = this.state;
  193. const {
  194. length
  195. } = commentStack;
  196. if (length === 0) return;
  197. const commentWS = commentStack[length - 1];
  198. if (commentWS.leadingNode === node) {
  199. commentWS.leadingNode = null;
  200. }
  201. }
  202. takeSurroundingComments(node, start, end) {
  203. const {
  204. commentStack
  205. } = this.state;
  206. const commentStackLength = commentStack.length;
  207. if (commentStackLength === 0) return;
  208. let i = commentStackLength - 1;
  209. for (; i >= 0; i--) {
  210. const commentWS = commentStack[i];
  211. const commentEnd = commentWS.end;
  212. const commentStart = commentWS.start;
  213. if (commentStart === end) {
  214. commentWS.leadingNode = node;
  215. } else if (commentEnd === start) {
  216. commentWS.trailingNode = node;
  217. } else if (commentEnd < start) {
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. const ErrorCodes = Object.freeze({
  224. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  225. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  226. });
  227. const ErrorMessages = makeErrorTemplates({
  228. AccessorIsGenerator: "A %0ter cannot be a generator.",
  229. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  230. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  231. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  232. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  233. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  234. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  235. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  236. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  237. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  238. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  239. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  240. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  241. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  242. ConstructorIsAsync: "Constructor can't be an async function.",
  243. ConstructorIsGenerator: "Constructor can't be a generator.",
  244. DeclarationMissingInitializer: "'%0' require an initialization value.",
  245. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  246. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  247. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  248. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  249. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  250. DeletePrivateField: "Deleting a private field is not allowed.",
  251. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  252. DuplicateConstructor: "Duplicate constructor in the same class.",
  253. DuplicateDefaultExport: "Only one default export allowed per module.",
  254. DuplicateExport: "`%0` has already been exported. Exported identifiers must be unique.",
  255. DuplicateProto: "Redefinition of __proto__ property.",
  256. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  257. ElementAfterRest: "Rest element must be last element.",
  258. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  259. ExportBindingIsString: "A string literal cannot be used as an exported binding without `from`.\n- Did you mean `export { '%0' as '%1' } from 'some-module'`?",
  260. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  261. ForInOfLoopInitializer: "'%0' loop variable declaration may not have an initializer.",
  262. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  263. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  264. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  265. IllegalBreakContinue: "Unsyntactic %0.",
  266. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  267. IllegalReturn: "'return' outside of function.",
  268. ImportBindingIsString: 'A string literal cannot be used as an imported binding.\n- Did you mean `import { "%0" as foo }`?',
  269. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  270. ImportCallArity: "`import()` requires exactly %0.",
  271. ImportCallNotNewExpression: "Cannot use new with import(...).",
  272. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  273. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  274. InvalidCodePoint: "Code point out of bounds.",
  275. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  276. InvalidDecimal: "Invalid decimal.",
  277. InvalidDigit: "Expected number in radix %0.",
  278. InvalidEscapeSequence: "Bad character escape sequence.",
  279. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  280. InvalidEscapedReservedWord: "Escape sequence in keyword %0.",
  281. InvalidIdentifier: "Invalid identifier %0.",
  282. InvalidLhs: "Invalid left-hand side in %0.",
  283. InvalidLhsBinding: "Binding invalid left-hand side in %0.",
  284. InvalidNumber: "Invalid number.",
  285. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  286. InvalidOrUnexpectedToken: "Unexpected character '%0'.",
  287. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  288. InvalidPrivateFieldResolution: "Private name #%0 is not defined.",
  289. InvalidPropertyBindingPattern: "Binding member expression.",
  290. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  291. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  292. LabelRedeclaration: "Label '%0' is already declared.",
  293. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  294. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  295. MalformedRegExpFlags: "Invalid regular expression flag.",
  296. MissingClassName: "A class name is required.",
  297. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  298. MissingSemicolon: "Missing semicolon.",
  299. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  300. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  301. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  302. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  303. ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes.',
  304. ModuleExportNameHasLoneSurrogate: "An export name cannot include a lone surrogate, found '\\u%0'.",
  305. ModuleExportUndefined: "Export '%0' is not defined.",
  306. MultipleDefaultsInSwitch: "Multiple default clauses.",
  307. NewlineAfterThrow: "Illegal newline after throw.",
  308. NoCatchOrFinally: "Missing catch or finally clause.",
  309. NumberIdentifier: "Identifier directly after number.",
  310. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  311. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  312. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  313. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  314. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  315. ParamDupe: "Argument name clash.",
  316. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  317. PatternHasMethod: "Object pattern can't contain methods.",
  318. PipeBodyIsTighter: "Unexpected %0 after pipeline body; any %0 expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  319. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  320. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  321. PipeTopicUnconfiguredToken: 'Invalid topic token %0. In order to use %0 as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "%0" }.',
  322. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  323. PipeUnparenthesizedBody: "Hack-style pipe body cannot be an unparenthesized %0 expression; please wrap it in parentheses.",
  324. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  325. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  326. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  327. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  328. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  329. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  330. PrivateInExpectedIn: "Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
  331. PrivateNameRedeclaration: "Duplicate private name #%0.",
  332. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  333. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  334. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  335. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  336. RestTrailingComma: "Unexpected trailing comma after rest element.",
  337. 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.",
  338. StaticPrototype: "Classes may not have static property named prototype.",
  339. StrictDelete: "Deleting local variable in strict mode.",
  340. StrictEvalArguments: "Assigning to '%0' in strict mode.",
  341. StrictEvalArgumentsBinding: "Binding '%0' in strict mode.",
  342. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  343. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  344. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  345. StrictWith: "'with' in strict mode.",
  346. 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?",
  347. SuperPrivateField: "Private fields can't be accessed on super.",
  348. TrailingDecorator: "Decorators must be attached to a class element.",
  349. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  350. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  351. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  352. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  353. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  354. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  355. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  356. UnexpectedKeyword: "Unexpected keyword '%0'.",
  357. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  358. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  359. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  360. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  361. UnexpectedPrivateField: "Unexpected private name.",
  362. UnexpectedReservedWord: "Unexpected reserved word '%0'.",
  363. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  364. UnexpectedToken: "Unexpected token '%0'.",
  365. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  366. UnsupportedBind: "Binding should be performed on object property.",
  367. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  368. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  369. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  370. UnsupportedMetaProperty: "The only valid meta property for %0 is %0.%1.",
  371. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  372. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  373. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  374. UnterminatedComment: "Unterminated comment.",
  375. UnterminatedRegExp: "Unterminated regular expression.",
  376. UnterminatedString: "Unterminated string constant.",
  377. UnterminatedTemplate: "Unterminated template.",
  378. VarRedeclaration: "Identifier '%0' has already been declared.",
  379. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  380. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  381. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  382. }, ErrorCodes.SyntaxError);
  383. const SourceTypeModuleErrorMessages = makeErrorTemplates({
  384. ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
  385. ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`
  386. }, ErrorCodes.SourceTypeModuleError);
  387. function keepReasonCodeCompat(reasonCode, syntaxPlugin) {
  388. {
  389. if (syntaxPlugin === "flow" && reasonCode === "PatternIsOptional") {
  390. return "OptionalBindingPattern";
  391. }
  392. }
  393. return reasonCode;
  394. }
  395. function makeErrorTemplates(messages, code, syntaxPlugin) {
  396. const templates = {};
  397. Object.keys(messages).forEach(reasonCode => {
  398. templates[reasonCode] = Object.freeze({
  399. code,
  400. reasonCode: keepReasonCodeCompat(reasonCode, syntaxPlugin),
  401. template: messages[reasonCode]
  402. });
  403. });
  404. return Object.freeze(templates);
  405. }
  406. class ParserError extends CommentsParser {
  407. raise({
  408. code,
  409. reasonCode,
  410. template
  411. }, origin, ...params) {
  412. return this.raiseWithData(origin.node ? origin.node.loc.start : origin.at, {
  413. code,
  414. reasonCode
  415. }, template, ...params);
  416. }
  417. raiseOverwrite(loc, {
  418. code,
  419. template
  420. }, ...params) {
  421. const pos = indexes.get(loc);
  422. const message = template.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  423. if (this.options.errorRecovery) {
  424. const errors = this.state.errors;
  425. for (let i = errors.length - 1; i >= 0; i--) {
  426. const error = errors[i];
  427. if (error.pos === pos) {
  428. return Object.assign(error, {
  429. message
  430. });
  431. } else if (error.pos < pos) {
  432. break;
  433. }
  434. }
  435. }
  436. return this._raise({
  437. code,
  438. loc,
  439. pos
  440. }, message);
  441. }
  442. raiseWithData(loc, data, errorTemplate, ...params) {
  443. const pos = indexes.get(loc);
  444. const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  445. return this._raise(Object.assign({
  446. loc,
  447. pos
  448. }, data), message);
  449. }
  450. _raise(errorContext, message) {
  451. const err = new SyntaxError(message);
  452. Object.assign(err, errorContext);
  453. if (this.options.errorRecovery) {
  454. if (!this.isLookahead) this.state.errors.push(err);
  455. return err;
  456. } else {
  457. throw err;
  458. }
  459. }
  460. }
  461. var estree = (superClass => class extends superClass {
  462. parseRegExpLiteral({
  463. pattern,
  464. flags
  465. }) {
  466. let regex = null;
  467. try {
  468. regex = new RegExp(pattern, flags);
  469. } catch (e) {}
  470. const node = this.estreeParseLiteral(regex);
  471. node.regex = {
  472. pattern,
  473. flags
  474. };
  475. return node;
  476. }
  477. parseBigIntLiteral(value) {
  478. let bigInt;
  479. try {
  480. bigInt = BigInt(value);
  481. } catch (_unused) {
  482. bigInt = null;
  483. }
  484. const node = this.estreeParseLiteral(bigInt);
  485. node.bigint = String(node.value || value);
  486. return node;
  487. }
  488. parseDecimalLiteral(value) {
  489. const decimal = null;
  490. const node = this.estreeParseLiteral(decimal);
  491. node.decimal = String(node.value || value);
  492. return node;
  493. }
  494. estreeParseLiteral(value) {
  495. return this.parseLiteral(value, "Literal");
  496. }
  497. parseStringLiteral(value) {
  498. return this.estreeParseLiteral(value);
  499. }
  500. parseNumericLiteral(value) {
  501. return this.estreeParseLiteral(value);
  502. }
  503. parseNullLiteral() {
  504. return this.estreeParseLiteral(null);
  505. }
  506. parseBooleanLiteral(value) {
  507. return this.estreeParseLiteral(value);
  508. }
  509. directiveToStmt(directive) {
  510. const directiveLiteral = directive.value;
  511. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  512. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  513. expression.value = directiveLiteral.extra.expressionValue;
  514. expression.raw = directiveLiteral.extra.raw;
  515. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.loc.end);
  516. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  517. return this.finishNodeAt(stmt, "ExpressionStatement", directive.loc.end);
  518. }
  519. initFunction(node, isAsync) {
  520. super.initFunction(node, isAsync);
  521. node.expression = false;
  522. }
  523. checkDeclaration(node) {
  524. if (node != null && this.isObjectProperty(node)) {
  525. this.checkDeclaration(node.value);
  526. } else {
  527. super.checkDeclaration(node);
  528. }
  529. }
  530. getObjectOrClassMethodParams(method) {
  531. return method.value.params;
  532. }
  533. isValidDirective(stmt) {
  534. var _stmt$expression$extr;
  535. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  536. }
  537. parseBlockBody(node, ...args) {
  538. super.parseBlockBody(node, ...args);
  539. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  540. node.body = directiveStatements.concat(node.body);
  541. delete node.directives;
  542. }
  543. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  544. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  545. if (method.typeParameters) {
  546. method.value.typeParameters = method.typeParameters;
  547. delete method.typeParameters;
  548. }
  549. classBody.body.push(method);
  550. }
  551. parsePrivateName() {
  552. const node = super.parsePrivateName();
  553. {
  554. if (!this.getPluginOption("estree", "classFeatures")) {
  555. return node;
  556. }
  557. }
  558. return this.convertPrivateNameToPrivateIdentifier(node);
  559. }
  560. convertPrivateNameToPrivateIdentifier(node) {
  561. const name = super.getPrivateNameSV(node);
  562. node = node;
  563. delete node.id;
  564. node.name = name;
  565. node.type = "PrivateIdentifier";
  566. return node;
  567. }
  568. isPrivateName(node) {
  569. {
  570. if (!this.getPluginOption("estree", "classFeatures")) {
  571. return super.isPrivateName(node);
  572. }
  573. }
  574. return node.type === "PrivateIdentifier";
  575. }
  576. getPrivateNameSV(node) {
  577. {
  578. if (!this.getPluginOption("estree", "classFeatures")) {
  579. return super.getPrivateNameSV(node);
  580. }
  581. }
  582. return node.name;
  583. }
  584. parseLiteral(value, type) {
  585. const node = super.parseLiteral(value, type);
  586. node.raw = node.extra.raw;
  587. delete node.extra;
  588. return node;
  589. }
  590. parseFunctionBody(node, allowExpression, isMethod = false) {
  591. super.parseFunctionBody(node, allowExpression, isMethod);
  592. node.expression = node.body.type !== "BlockStatement";
  593. }
  594. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  595. let funcNode = this.startNode();
  596. funcNode.kind = node.kind;
  597. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  598. funcNode.type = "FunctionExpression";
  599. delete funcNode.kind;
  600. node.value = funcNode;
  601. if (type === "ClassPrivateMethod") {
  602. node.computed = false;
  603. }
  604. type = "MethodDefinition";
  605. return this.finishNode(node, type);
  606. }
  607. parseClassProperty(...args) {
  608. const propertyNode = super.parseClassProperty(...args);
  609. {
  610. if (!this.getPluginOption("estree", "classFeatures")) {
  611. return propertyNode;
  612. }
  613. }
  614. propertyNode.type = "PropertyDefinition";
  615. return propertyNode;
  616. }
  617. parseClassPrivateProperty(...args) {
  618. const propertyNode = super.parseClassPrivateProperty(...args);
  619. {
  620. if (!this.getPluginOption("estree", "classFeatures")) {
  621. return propertyNode;
  622. }
  623. }
  624. propertyNode.type = "PropertyDefinition";
  625. propertyNode.computed = false;
  626. return propertyNode;
  627. }
  628. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  629. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  630. if (node) {
  631. node.type = "Property";
  632. if (node.kind === "method") node.kind = "init";
  633. node.shorthand = false;
  634. }
  635. return node;
  636. }
  637. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  638. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  639. if (node) {
  640. node.kind = "init";
  641. node.type = "Property";
  642. }
  643. return node;
  644. }
  645. isAssignable(node, isBinding) {
  646. if (node != null && this.isObjectProperty(node)) {
  647. return this.isAssignable(node.value, isBinding);
  648. }
  649. return super.isAssignable(node, isBinding);
  650. }
  651. toAssignable(node, isLHS = false) {
  652. if (node != null && this.isObjectProperty(node)) {
  653. this.toAssignable(node.value, isLHS);
  654. return node;
  655. }
  656. return super.toAssignable(node, isLHS);
  657. }
  658. toAssignableObjectExpressionProp(prop, ...args) {
  659. if (prop.kind === "get" || prop.kind === "set") {
  660. this.raise(ErrorMessages.PatternHasAccessor, {
  661. node: prop.key
  662. });
  663. } else if (prop.method) {
  664. this.raise(ErrorMessages.PatternHasMethod, {
  665. node: prop.key
  666. });
  667. } else {
  668. super.toAssignableObjectExpressionProp(prop, ...args);
  669. }
  670. }
  671. finishCallExpression(node, optional) {
  672. super.finishCallExpression(node, optional);
  673. if (node.callee.type === "Import") {
  674. node.type = "ImportExpression";
  675. node.source = node.arguments[0];
  676. if (this.hasPlugin("importAssertions")) {
  677. var _node$arguments$;
  678. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  679. }
  680. delete node.arguments;
  681. delete node.callee;
  682. }
  683. return node;
  684. }
  685. toReferencedArguments(node) {
  686. if (node.type === "ImportExpression") {
  687. return;
  688. }
  689. super.toReferencedArguments(node);
  690. }
  691. parseExport(node) {
  692. super.parseExport(node);
  693. switch (node.type) {
  694. case "ExportAllDeclaration":
  695. node.exported = null;
  696. break;
  697. case "ExportNamedDeclaration":
  698. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  699. node.type = "ExportAllDeclaration";
  700. node.exported = node.specifiers[0].exported;
  701. delete node.specifiers;
  702. }
  703. break;
  704. }
  705. return node;
  706. }
  707. parseSubscript(base, startPos, startLoc, noCalls, state) {
  708. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  709. if (state.optionalChainMember) {
  710. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  711. node.type = node.type.substring(8);
  712. }
  713. if (state.stop) {
  714. const chain = this.startNodeAtNode(node);
  715. chain.expression = node;
  716. return this.finishNode(chain, "ChainExpression");
  717. }
  718. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  719. node.optional = false;
  720. }
  721. return node;
  722. }
  723. hasPropertyAsPrivateName(node) {
  724. if (node.type === "ChainExpression") {
  725. node = node.expression;
  726. }
  727. return super.hasPropertyAsPrivateName(node);
  728. }
  729. isOptionalChain(node) {
  730. return node.type === "ChainExpression";
  731. }
  732. isObjectProperty(node) {
  733. return node.type === "Property" && node.kind === "init" && !node.method;
  734. }
  735. isObjectMethod(node) {
  736. return node.method || node.kind === "get" || node.kind === "set";
  737. }
  738. });
  739. class TokContext {
  740. constructor(token, preserveSpace) {
  741. this.token = void 0;
  742. this.preserveSpace = void 0;
  743. this.token = token;
  744. this.preserveSpace = !!preserveSpace;
  745. }
  746. }
  747. const types = {
  748. brace: new TokContext("{"),
  749. j_oTag: new TokContext("<tag"),
  750. j_cTag: new TokContext("</tag"),
  751. j_expr: new TokContext("<tag>...</tag>", true)
  752. };
  753. {
  754. types.template = new TokContext("`", true);
  755. }
  756. const beforeExpr = true;
  757. const startsExpr = true;
  758. const isLoop = true;
  759. const isAssign = true;
  760. const prefix = true;
  761. const postfix = true;
  762. class ExportedTokenType {
  763. constructor(label, conf = {}) {
  764. this.label = void 0;
  765. this.keyword = void 0;
  766. this.beforeExpr = void 0;
  767. this.startsExpr = void 0;
  768. this.rightAssociative = void 0;
  769. this.isLoop = void 0;
  770. this.isAssign = void 0;
  771. this.prefix = void 0;
  772. this.postfix = void 0;
  773. this.binop = void 0;
  774. this.label = label;
  775. this.keyword = conf.keyword;
  776. this.beforeExpr = !!conf.beforeExpr;
  777. this.startsExpr = !!conf.startsExpr;
  778. this.rightAssociative = !!conf.rightAssociative;
  779. this.isLoop = !!conf.isLoop;
  780. this.isAssign = !!conf.isAssign;
  781. this.prefix = !!conf.prefix;
  782. this.postfix = !!conf.postfix;
  783. this.binop = conf.binop != null ? conf.binop : null;
  784. {
  785. this.updateContext = null;
  786. }
  787. }
  788. }
  789. const keywords$1 = new Map();
  790. function createKeyword(name, options = {}) {
  791. options.keyword = name;
  792. const token = createToken(name, options);
  793. keywords$1.set(name, token);
  794. return token;
  795. }
  796. function createBinop(name, binop) {
  797. return createToken(name, {
  798. beforeExpr,
  799. binop
  800. });
  801. }
  802. let tokenTypeCounter = -1;
  803. const tokenTypes = [];
  804. const tokenLabels = [];
  805. const tokenBinops = [];
  806. const tokenBeforeExprs = [];
  807. const tokenStartsExprs = [];
  808. const tokenPrefixes = [];
  809. function createToken(name, options = {}) {
  810. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  811. ++tokenTypeCounter;
  812. tokenLabels.push(name);
  813. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  814. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  815. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  816. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  817. tokenTypes.push(new ExportedTokenType(name, options));
  818. return tokenTypeCounter;
  819. }
  820. function createKeywordLike(name, options = {}) {
  821. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  822. ++tokenTypeCounter;
  823. keywords$1.set(name, tokenTypeCounter);
  824. tokenLabels.push(name);
  825. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  826. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  827. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  828. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  829. tokenTypes.push(new ExportedTokenType("name", options));
  830. return tokenTypeCounter;
  831. }
  832. const tt = {
  833. bracketL: createToken("[", {
  834. beforeExpr,
  835. startsExpr
  836. }),
  837. bracketHashL: createToken("#[", {
  838. beforeExpr,
  839. startsExpr
  840. }),
  841. bracketBarL: createToken("[|", {
  842. beforeExpr,
  843. startsExpr
  844. }),
  845. bracketR: createToken("]"),
  846. bracketBarR: createToken("|]"),
  847. braceL: createToken("{", {
  848. beforeExpr,
  849. startsExpr
  850. }),
  851. braceBarL: createToken("{|", {
  852. beforeExpr,
  853. startsExpr
  854. }),
  855. braceHashL: createToken("#{", {
  856. beforeExpr,
  857. startsExpr
  858. }),
  859. braceR: createToken("}", {
  860. beforeExpr
  861. }),
  862. braceBarR: createToken("|}"),
  863. parenL: createToken("(", {
  864. beforeExpr,
  865. startsExpr
  866. }),
  867. parenR: createToken(")"),
  868. comma: createToken(",", {
  869. beforeExpr
  870. }),
  871. semi: createToken(";", {
  872. beforeExpr
  873. }),
  874. colon: createToken(":", {
  875. beforeExpr
  876. }),
  877. doubleColon: createToken("::", {
  878. beforeExpr
  879. }),
  880. dot: createToken("."),
  881. question: createToken("?", {
  882. beforeExpr
  883. }),
  884. questionDot: createToken("?."),
  885. arrow: createToken("=>", {
  886. beforeExpr
  887. }),
  888. template: createToken("template"),
  889. ellipsis: createToken("...", {
  890. beforeExpr
  891. }),
  892. backQuote: createToken("`", {
  893. startsExpr
  894. }),
  895. dollarBraceL: createToken("${", {
  896. beforeExpr,
  897. startsExpr
  898. }),
  899. templateTail: createToken("...`", {
  900. startsExpr
  901. }),
  902. templateNonTail: createToken("...${", {
  903. beforeExpr,
  904. startsExpr
  905. }),
  906. at: createToken("@"),
  907. hash: createToken("#", {
  908. startsExpr
  909. }),
  910. interpreterDirective: createToken("#!..."),
  911. eq: createToken("=", {
  912. beforeExpr,
  913. isAssign
  914. }),
  915. assign: createToken("_=", {
  916. beforeExpr,
  917. isAssign
  918. }),
  919. slashAssign: createToken("_=", {
  920. beforeExpr,
  921. isAssign
  922. }),
  923. xorAssign: createToken("_=", {
  924. beforeExpr,
  925. isAssign
  926. }),
  927. moduloAssign: createToken("_=", {
  928. beforeExpr,
  929. isAssign
  930. }),
  931. incDec: createToken("++/--", {
  932. prefix,
  933. postfix,
  934. startsExpr
  935. }),
  936. bang: createToken("!", {
  937. beforeExpr,
  938. prefix,
  939. startsExpr
  940. }),
  941. tilde: createToken("~", {
  942. beforeExpr,
  943. prefix,
  944. startsExpr
  945. }),
  946. pipeline: createBinop("|>", 0),
  947. nullishCoalescing: createBinop("??", 1),
  948. logicalOR: createBinop("||", 1),
  949. logicalAND: createBinop("&&", 2),
  950. bitwiseOR: createBinop("|", 3),
  951. bitwiseXOR: createBinop("^", 4),
  952. bitwiseAND: createBinop("&", 5),
  953. equality: createBinop("==/!=/===/!==", 6),
  954. lt: createBinop("</>/<=/>=", 7),
  955. gt: createBinop("</>/<=/>=", 7),
  956. relational: createBinop("</>/<=/>=", 7),
  957. bitShift: createBinop("<</>>/>>>", 8),
  958. plusMin: createToken("+/-", {
  959. beforeExpr,
  960. binop: 9,
  961. prefix,
  962. startsExpr
  963. }),
  964. modulo: createToken("%", {
  965. binop: 10,
  966. startsExpr
  967. }),
  968. star: createToken("*", {
  969. binop: 10
  970. }),
  971. slash: createBinop("/", 10),
  972. exponent: createToken("**", {
  973. beforeExpr,
  974. binop: 11,
  975. rightAssociative: true
  976. }),
  977. _in: createKeyword("in", {
  978. beforeExpr,
  979. binop: 7
  980. }),
  981. _instanceof: createKeyword("instanceof", {
  982. beforeExpr,
  983. binop: 7
  984. }),
  985. _break: createKeyword("break"),
  986. _case: createKeyword("case", {
  987. beforeExpr
  988. }),
  989. _catch: createKeyword("catch"),
  990. _continue: createKeyword("continue"),
  991. _debugger: createKeyword("debugger"),
  992. _default: createKeyword("default", {
  993. beforeExpr
  994. }),
  995. _else: createKeyword("else", {
  996. beforeExpr
  997. }),
  998. _finally: createKeyword("finally"),
  999. _function: createKeyword("function", {
  1000. startsExpr
  1001. }),
  1002. _if: createKeyword("if"),
  1003. _return: createKeyword("return", {
  1004. beforeExpr
  1005. }),
  1006. _switch: createKeyword("switch"),
  1007. _throw: createKeyword("throw", {
  1008. beforeExpr,
  1009. prefix,
  1010. startsExpr
  1011. }),
  1012. _try: createKeyword("try"),
  1013. _var: createKeyword("var"),
  1014. _const: createKeyword("const"),
  1015. _with: createKeyword("with"),
  1016. _new: createKeyword("new", {
  1017. beforeExpr,
  1018. startsExpr
  1019. }),
  1020. _this: createKeyword("this", {
  1021. startsExpr
  1022. }),
  1023. _super: createKeyword("super", {
  1024. startsExpr
  1025. }),
  1026. _class: createKeyword("class", {
  1027. startsExpr
  1028. }),
  1029. _extends: createKeyword("extends", {
  1030. beforeExpr
  1031. }),
  1032. _export: createKeyword("export"),
  1033. _import: createKeyword("import", {
  1034. startsExpr
  1035. }),
  1036. _null: createKeyword("null", {
  1037. startsExpr
  1038. }),
  1039. _true: createKeyword("true", {
  1040. startsExpr
  1041. }),
  1042. _false: createKeyword("false", {
  1043. startsExpr
  1044. }),
  1045. _typeof: createKeyword("typeof", {
  1046. beforeExpr,
  1047. prefix,
  1048. startsExpr
  1049. }),
  1050. _void: createKeyword("void", {
  1051. beforeExpr,
  1052. prefix,
  1053. startsExpr
  1054. }),
  1055. _delete: createKeyword("delete", {
  1056. beforeExpr,
  1057. prefix,
  1058. startsExpr
  1059. }),
  1060. _do: createKeyword("do", {
  1061. isLoop,
  1062. beforeExpr
  1063. }),
  1064. _for: createKeyword("for", {
  1065. isLoop
  1066. }),
  1067. _while: createKeyword("while", {
  1068. isLoop
  1069. }),
  1070. _as: createKeywordLike("as", {
  1071. startsExpr
  1072. }),
  1073. _assert: createKeywordLike("assert", {
  1074. startsExpr
  1075. }),
  1076. _async: createKeywordLike("async", {
  1077. startsExpr
  1078. }),
  1079. _await: createKeywordLike("await", {
  1080. startsExpr
  1081. }),
  1082. _from: createKeywordLike("from", {
  1083. startsExpr
  1084. }),
  1085. _get: createKeywordLike("get", {
  1086. startsExpr
  1087. }),
  1088. _let: createKeywordLike("let", {
  1089. startsExpr
  1090. }),
  1091. _meta: createKeywordLike("meta", {
  1092. startsExpr
  1093. }),
  1094. _of: createKeywordLike("of", {
  1095. startsExpr
  1096. }),
  1097. _sent: createKeywordLike("sent", {
  1098. startsExpr
  1099. }),
  1100. _set: createKeywordLike("set", {
  1101. startsExpr
  1102. }),
  1103. _static: createKeywordLike("static", {
  1104. startsExpr
  1105. }),
  1106. _yield: createKeywordLike("yield", {
  1107. startsExpr
  1108. }),
  1109. _asserts: createKeywordLike("asserts", {
  1110. startsExpr
  1111. }),
  1112. _checks: createKeywordLike("checks", {
  1113. startsExpr
  1114. }),
  1115. _exports: createKeywordLike("exports", {
  1116. startsExpr
  1117. }),
  1118. _global: createKeywordLike("global", {
  1119. startsExpr
  1120. }),
  1121. _implements: createKeywordLike("implements", {
  1122. startsExpr
  1123. }),
  1124. _intrinsic: createKeywordLike("intrinsic", {
  1125. startsExpr
  1126. }),
  1127. _infer: createKeywordLike("infer", {
  1128. startsExpr
  1129. }),
  1130. _is: createKeywordLike("is", {
  1131. startsExpr
  1132. }),
  1133. _mixins: createKeywordLike("mixins", {
  1134. startsExpr
  1135. }),
  1136. _proto: createKeywordLike("proto", {
  1137. startsExpr
  1138. }),
  1139. _require: createKeywordLike("require", {
  1140. startsExpr
  1141. }),
  1142. _keyof: createKeywordLike("keyof", {
  1143. startsExpr
  1144. }),
  1145. _readonly: createKeywordLike("readonly", {
  1146. startsExpr
  1147. }),
  1148. _unique: createKeywordLike("unique", {
  1149. startsExpr
  1150. }),
  1151. _abstract: createKeywordLike("abstract", {
  1152. startsExpr
  1153. }),
  1154. _declare: createKeywordLike("declare", {
  1155. startsExpr
  1156. }),
  1157. _enum: createKeywordLike("enum", {
  1158. startsExpr
  1159. }),
  1160. _module: createKeywordLike("module", {
  1161. startsExpr
  1162. }),
  1163. _namespace: createKeywordLike("namespace", {
  1164. startsExpr
  1165. }),
  1166. _interface: createKeywordLike("interface", {
  1167. startsExpr
  1168. }),
  1169. _type: createKeywordLike("type", {
  1170. startsExpr
  1171. }),
  1172. _opaque: createKeywordLike("opaque", {
  1173. startsExpr
  1174. }),
  1175. name: createToken("name", {
  1176. startsExpr
  1177. }),
  1178. string: createToken("string", {
  1179. startsExpr
  1180. }),
  1181. num: createToken("num", {
  1182. startsExpr
  1183. }),
  1184. bigint: createToken("bigint", {
  1185. startsExpr
  1186. }),
  1187. decimal: createToken("decimal", {
  1188. startsExpr
  1189. }),
  1190. regexp: createToken("regexp", {
  1191. startsExpr
  1192. }),
  1193. privateName: createToken("#name", {
  1194. startsExpr
  1195. }),
  1196. eof: createToken("eof"),
  1197. jsxName: createToken("jsxName"),
  1198. jsxText: createToken("jsxText", {
  1199. beforeExpr: true
  1200. }),
  1201. jsxTagStart: createToken("jsxTagStart", {
  1202. startsExpr: true
  1203. }),
  1204. jsxTagEnd: createToken("jsxTagEnd"),
  1205. placeholder: createToken("%%", {
  1206. startsExpr: true
  1207. })
  1208. };
  1209. function tokenIsIdentifier(token) {
  1210. return token >= 89 && token <= 124;
  1211. }
  1212. function tokenKeywordOrIdentifierIsKeyword(token) {
  1213. return token <= 88;
  1214. }
  1215. function tokenIsKeywordOrIdentifier(token) {
  1216. return token >= 54 && token <= 124;
  1217. }
  1218. function tokenIsLiteralPropertyName(token) {
  1219. return token >= 54 && token <= 128;
  1220. }
  1221. function tokenComesBeforeExpression(token) {
  1222. return tokenBeforeExprs[token];
  1223. }
  1224. function tokenCanStartExpression(token) {
  1225. return tokenStartsExprs[token];
  1226. }
  1227. function tokenIsAssignment(token) {
  1228. return token >= 29 && token <= 33;
  1229. }
  1230. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1231. return token >= 121 && token <= 123;
  1232. }
  1233. function tokenIsLoop(token) {
  1234. return token >= 86 && token <= 88;
  1235. }
  1236. function tokenIsKeyword(token) {
  1237. return token >= 54 && token <= 88;
  1238. }
  1239. function tokenIsOperator(token) {
  1240. return token >= 37 && token <= 55;
  1241. }
  1242. function tokenIsPostfix(token) {
  1243. return token === 34;
  1244. }
  1245. function tokenIsPrefix(token) {
  1246. return tokenPrefixes[token];
  1247. }
  1248. function tokenIsTSTypeOperator(token) {
  1249. return token >= 113 && token <= 115;
  1250. }
  1251. function tokenIsTSDeclarationStart(token) {
  1252. return token >= 116 && token <= 122;
  1253. }
  1254. function tokenLabelName(token) {
  1255. return tokenLabels[token];
  1256. }
  1257. function tokenOperatorPrecedence(token) {
  1258. return tokenBinops[token];
  1259. }
  1260. function tokenIsRightAssociative(token) {
  1261. return token === 53;
  1262. }
  1263. function tokenIsTemplate(token) {
  1264. return token >= 24 && token <= 25;
  1265. }
  1266. function getExportedToken(token) {
  1267. return tokenTypes[token];
  1268. }
  1269. {
  1270. tokenTypes[8].updateContext = context => {
  1271. context.pop();
  1272. };
  1273. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1274. context.push(types.brace);
  1275. };
  1276. tokenTypes[22].updateContext = context => {
  1277. if (context[context.length - 1] === types.template) {
  1278. context.pop();
  1279. } else {
  1280. context.push(types.template);
  1281. }
  1282. };
  1283. tokenTypes[134].updateContext = context => {
  1284. context.push(types.j_expr, types.j_oTag);
  1285. };
  1286. }
  1287. 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";
  1288. 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";
  1289. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1290. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1291. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1292. 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];
  1293. 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];
  1294. function isInAstralSet(code, set) {
  1295. let pos = 0x10000;
  1296. for (let i = 0, length = set.length; i < length; i += 2) {
  1297. pos += set[i];
  1298. if (pos > code) return false;
  1299. pos += set[i + 1];
  1300. if (pos >= code) return true;
  1301. }
  1302. return false;
  1303. }
  1304. function isIdentifierStart(code) {
  1305. if (code < 65) return code === 36;
  1306. if (code <= 90) return true;
  1307. if (code < 97) return code === 95;
  1308. if (code <= 122) return true;
  1309. if (code <= 0xffff) {
  1310. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1311. }
  1312. return isInAstralSet(code, astralIdentifierStartCodes);
  1313. }
  1314. function isIdentifierChar(code) {
  1315. if (code < 48) return code === 36;
  1316. if (code < 58) return true;
  1317. if (code < 65) return false;
  1318. if (code <= 90) return true;
  1319. if (code < 97) return code === 95;
  1320. if (code <= 122) return true;
  1321. if (code <= 0xffff) {
  1322. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1323. }
  1324. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1325. }
  1326. const reservedWords = {
  1327. 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"],
  1328. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1329. strictBind: ["eval", "arguments"]
  1330. };
  1331. const keywords = new Set(reservedWords.keyword);
  1332. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1333. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1334. function isReservedWord(word, inModule) {
  1335. return inModule && word === "await" || word === "enum";
  1336. }
  1337. function isStrictReservedWord(word, inModule) {
  1338. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1339. }
  1340. function isStrictBindOnlyReservedWord(word) {
  1341. return reservedWordsStrictBindSet.has(word);
  1342. }
  1343. function isStrictBindReservedWord(word, inModule) {
  1344. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1345. }
  1346. function isKeyword(word) {
  1347. return keywords.has(word);
  1348. }
  1349. function isIteratorStart(current, next) {
  1350. return current === 64 && next === 64;
  1351. }
  1352. 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"]);
  1353. function canBeReservedWord(word) {
  1354. return reservedWordLikeSet.has(word);
  1355. }
  1356. const SCOPE_OTHER = 0b000000000,
  1357. SCOPE_PROGRAM = 0b000000001,
  1358. SCOPE_FUNCTION = 0b000000010,
  1359. SCOPE_ARROW = 0b000000100,
  1360. SCOPE_SIMPLE_CATCH = 0b000001000,
  1361. SCOPE_SUPER = 0b000010000,
  1362. SCOPE_DIRECT_SUPER = 0b000100000,
  1363. SCOPE_CLASS = 0b001000000,
  1364. SCOPE_STATIC_BLOCK = 0b010000000,
  1365. SCOPE_TS_MODULE = 0b100000000,
  1366. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1367. const BIND_KIND_VALUE = 0b000000000001,
  1368. BIND_KIND_TYPE = 0b000000000010,
  1369. BIND_SCOPE_VAR = 0b000000000100,
  1370. BIND_SCOPE_LEXICAL = 0b000000001000,
  1371. BIND_SCOPE_FUNCTION = 0b000000010000,
  1372. BIND_FLAGS_NONE = 0b000001000000,
  1373. BIND_FLAGS_CLASS = 0b000010000000,
  1374. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1375. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1376. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1377. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1378. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1379. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1380. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1381. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1382. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1383. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1384. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1385. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1386. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1387. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1388. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1389. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1390. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1391. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1392. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1393. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1394. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1395. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1396. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1397. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1398. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1399. CLASS_ELEMENT_OTHER = 0;
  1400. class Scope {
  1401. constructor(flags) {
  1402. this.var = new Set();
  1403. this.lexical = new Set();
  1404. this.functions = new Set();
  1405. this.flags = flags;
  1406. }
  1407. }
  1408. class ScopeHandler {
  1409. constructor(raise, inModule) {
  1410. this.scopeStack = [];
  1411. this.undefinedExports = new Map();
  1412. this.raise = raise;
  1413. this.inModule = inModule;
  1414. }
  1415. get inFunction() {
  1416. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1417. }
  1418. get allowSuper() {
  1419. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1420. }
  1421. get allowDirectSuper() {
  1422. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1423. }
  1424. get inClass() {
  1425. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1426. }
  1427. get inClassAndNotInNonArrowFunction() {
  1428. const flags = this.currentThisScopeFlags();
  1429. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1430. }
  1431. get inStaticBlock() {
  1432. for (let i = this.scopeStack.length - 1;; i--) {
  1433. const {
  1434. flags
  1435. } = this.scopeStack[i];
  1436. if (flags & SCOPE_STATIC_BLOCK) {
  1437. return true;
  1438. }
  1439. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1440. return false;
  1441. }
  1442. }
  1443. }
  1444. get inNonArrowFunction() {
  1445. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1446. }
  1447. get treatFunctionsAsVar() {
  1448. return this.treatFunctionsAsVarInScope(this.currentScope());
  1449. }
  1450. createScope(flags) {
  1451. return new Scope(flags);
  1452. }
  1453. enter(flags) {
  1454. this.scopeStack.push(this.createScope(flags));
  1455. }
  1456. exit() {
  1457. this.scopeStack.pop();
  1458. }
  1459. treatFunctionsAsVarInScope(scope) {
  1460. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  1461. }
  1462. declareName(name, bindingType, loc) {
  1463. let scope = this.currentScope();
  1464. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1465. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1466. if (bindingType & BIND_SCOPE_FUNCTION) {
  1467. scope.functions.add(name);
  1468. } else {
  1469. scope.lexical.add(name);
  1470. }
  1471. if (bindingType & BIND_SCOPE_LEXICAL) {
  1472. this.maybeExportDefined(scope, name);
  1473. }
  1474. } else if (bindingType & BIND_SCOPE_VAR) {
  1475. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1476. scope = this.scopeStack[i];
  1477. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1478. scope.var.add(name);
  1479. this.maybeExportDefined(scope, name);
  1480. if (scope.flags & SCOPE_VAR) break;
  1481. }
  1482. }
  1483. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1484. this.undefinedExports.delete(name);
  1485. }
  1486. }
  1487. maybeExportDefined(scope, name) {
  1488. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1489. this.undefinedExports.delete(name);
  1490. }
  1491. }
  1492. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1493. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1494. this.raise(ErrorMessages.VarRedeclaration, {
  1495. at: loc
  1496. }, name);
  1497. }
  1498. }
  1499. isRedeclaredInScope(scope, name, bindingType) {
  1500. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1501. if (bindingType & BIND_SCOPE_LEXICAL) {
  1502. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1503. }
  1504. if (bindingType & BIND_SCOPE_FUNCTION) {
  1505. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1506. }
  1507. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1508. }
  1509. checkLocalExport(id) {
  1510. const {
  1511. name
  1512. } = id;
  1513. const topLevelScope = this.scopeStack[0];
  1514. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1515. this.undefinedExports.set(name, id.loc.start);
  1516. }
  1517. }
  1518. currentScope() {
  1519. return this.scopeStack[this.scopeStack.length - 1];
  1520. }
  1521. currentVarScopeFlags() {
  1522. for (let i = this.scopeStack.length - 1;; i--) {
  1523. const {
  1524. flags
  1525. } = this.scopeStack[i];
  1526. if (flags & SCOPE_VAR) {
  1527. return flags;
  1528. }
  1529. }
  1530. }
  1531. currentThisScopeFlags() {
  1532. for (let i = this.scopeStack.length - 1;; i--) {
  1533. const {
  1534. flags
  1535. } = this.scopeStack[i];
  1536. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1537. return flags;
  1538. }
  1539. }
  1540. }
  1541. }
  1542. class FlowScope extends Scope {
  1543. constructor(...args) {
  1544. super(...args);
  1545. this.declareFunctions = new Set();
  1546. }
  1547. }
  1548. class FlowScopeHandler extends ScopeHandler {
  1549. createScope(flags) {
  1550. return new FlowScope(flags);
  1551. }
  1552. declareName(name, bindingType, loc) {
  1553. const scope = this.currentScope();
  1554. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1555. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1556. this.maybeExportDefined(scope, name);
  1557. scope.declareFunctions.add(name);
  1558. return;
  1559. }
  1560. super.declareName(...arguments);
  1561. }
  1562. isRedeclaredInScope(scope, name, bindingType) {
  1563. if (super.isRedeclaredInScope(...arguments)) return true;
  1564. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1565. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1566. }
  1567. return false;
  1568. }
  1569. checkLocalExport(id) {
  1570. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1571. super.checkLocalExport(id);
  1572. }
  1573. }
  1574. }
  1575. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1576. const lineBreakG = new RegExp(lineBreak.source, "g");
  1577. function isNewLine(code) {
  1578. switch (code) {
  1579. case 10:
  1580. case 13:
  1581. case 8232:
  1582. case 8233:
  1583. return true;
  1584. default:
  1585. return false;
  1586. }
  1587. }
  1588. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1589. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1590. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1591. function isWhitespace(code) {
  1592. switch (code) {
  1593. case 0x0009:
  1594. case 0x000b:
  1595. case 0x000c:
  1596. case 32:
  1597. case 160:
  1598. case 5760:
  1599. case 0x2000:
  1600. case 0x2001:
  1601. case 0x2002:
  1602. case 0x2003:
  1603. case 0x2004:
  1604. case 0x2005:
  1605. case 0x2006:
  1606. case 0x2007:
  1607. case 0x2008:
  1608. case 0x2009:
  1609. case 0x200a:
  1610. case 0x202f:
  1611. case 0x205f:
  1612. case 0x3000:
  1613. case 0xfeff:
  1614. return true;
  1615. default:
  1616. return false;
  1617. }
  1618. }
  1619. class State {
  1620. constructor() {
  1621. this.strict = void 0;
  1622. this.curLine = void 0;
  1623. this.lineStart = void 0;
  1624. this.startLoc = void 0;
  1625. this.endLoc = void 0;
  1626. this.errors = [];
  1627. this.potentialArrowAt = -1;
  1628. this.noArrowAt = [];
  1629. this.noArrowParamsConversionAt = [];
  1630. this.maybeInArrowParameters = false;
  1631. this.inType = false;
  1632. this.noAnonFunctionType = false;
  1633. this.hasFlowComment = false;
  1634. this.isAmbientContext = false;
  1635. this.inAbstractClass = false;
  1636. this.topicContext = {
  1637. maxNumOfResolvableTopics: 0,
  1638. maxTopicIndex: null
  1639. };
  1640. this.soloAwait = false;
  1641. this.inFSharpPipelineDirectBody = false;
  1642. this.labels = [];
  1643. this.decoratorStack = [[]];
  1644. this.comments = [];
  1645. this.commentStack = [];
  1646. this.pos = 0;
  1647. this.type = 131;
  1648. this.value = null;
  1649. this.start = 0;
  1650. this.end = 0;
  1651. this.lastTokEndLoc = null;
  1652. this.lastTokStartLoc = null;
  1653. this.lastTokStart = 0;
  1654. this.context = [types.brace];
  1655. this.canStartJSXElement = true;
  1656. this.containsEsc = false;
  1657. this.strictErrors = new Map();
  1658. this.tokensLength = 0;
  1659. }
  1660. init({
  1661. strictMode,
  1662. sourceType,
  1663. startLine,
  1664. startColumn
  1665. }) {
  1666. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1667. this.curLine = startLine;
  1668. this.lineStart = -startColumn;
  1669. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1670. }
  1671. curPosition() {
  1672. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1673. }
  1674. clone(skipArrays) {
  1675. const state = new State();
  1676. const keys = Object.keys(this);
  1677. for (let i = 0, length = keys.length; i < length; i++) {
  1678. const key = keys[i];
  1679. let val = this[key];
  1680. if (!skipArrays && Array.isArray(val)) {
  1681. val = val.slice();
  1682. }
  1683. state[key] = val;
  1684. }
  1685. return state;
  1686. }
  1687. }
  1688. var _isDigit = function isDigit(code) {
  1689. return code >= 48 && code <= 57;
  1690. };
  1691. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100]);
  1692. const forbiddenNumericSeparatorSiblings = {
  1693. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  1694. hex: [46, 88, 95, 120]
  1695. };
  1696. const allowedNumericSeparatorSiblings = {};
  1697. allowedNumericSeparatorSiblings.bin = [48, 49];
  1698. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  1699. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  1700. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  1701. class Token {
  1702. constructor(state) {
  1703. this.type = state.type;
  1704. this.value = state.value;
  1705. this.start = state.start;
  1706. this.end = state.end;
  1707. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  1708. }
  1709. }
  1710. class Tokenizer extends ParserError {
  1711. constructor(options, input) {
  1712. super();
  1713. this.isLookahead = void 0;
  1714. this.tokens = [];
  1715. this.state = new State();
  1716. this.state.init(options);
  1717. this.input = input;
  1718. this.length = input.length;
  1719. this.isLookahead = false;
  1720. }
  1721. pushToken(token) {
  1722. this.tokens.length = this.state.tokensLength;
  1723. this.tokens.push(token);
  1724. ++this.state.tokensLength;
  1725. }
  1726. next() {
  1727. this.checkKeywordEscapes();
  1728. if (this.options.tokens) {
  1729. this.pushToken(new Token(this.state));
  1730. }
  1731. this.state.lastTokStart = this.state.start;
  1732. this.state.lastTokEndLoc = this.state.endLoc;
  1733. this.state.lastTokStartLoc = this.state.startLoc;
  1734. this.nextToken();
  1735. }
  1736. eat(type) {
  1737. if (this.match(type)) {
  1738. this.next();
  1739. return true;
  1740. } else {
  1741. return false;
  1742. }
  1743. }
  1744. match(type) {
  1745. return this.state.type === type;
  1746. }
  1747. createLookaheadState(state) {
  1748. return {
  1749. pos: state.pos,
  1750. value: null,
  1751. type: state.type,
  1752. start: state.start,
  1753. end: state.end,
  1754. context: [this.curContext()],
  1755. inType: state.inType,
  1756. startLoc: state.startLoc,
  1757. lastTokEndLoc: state.lastTokEndLoc,
  1758. curLine: state.curLine,
  1759. lineStart: state.lineStart,
  1760. curPosition: state.curPosition
  1761. };
  1762. }
  1763. lookahead() {
  1764. const old = this.state;
  1765. this.state = this.createLookaheadState(old);
  1766. this.isLookahead = true;
  1767. this.nextToken();
  1768. this.isLookahead = false;
  1769. const curr = this.state;
  1770. this.state = old;
  1771. return curr;
  1772. }
  1773. nextTokenStart() {
  1774. return this.nextTokenStartSince(this.state.pos);
  1775. }
  1776. nextTokenStartSince(pos) {
  1777. skipWhiteSpace.lastIndex = pos;
  1778. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  1779. }
  1780. lookaheadCharCode() {
  1781. return this.input.charCodeAt(this.nextTokenStart());
  1782. }
  1783. codePointAtPos(pos) {
  1784. let cp = this.input.charCodeAt(pos);
  1785. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  1786. const trail = this.input.charCodeAt(pos);
  1787. if ((trail & 0xfc00) === 0xdc00) {
  1788. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  1789. }
  1790. }
  1791. return cp;
  1792. }
  1793. setStrict(strict) {
  1794. this.state.strict = strict;
  1795. if (strict) {
  1796. this.state.strictErrors.forEach(({
  1797. message,
  1798. loc
  1799. }) => this.raise(message, {
  1800. at: loc
  1801. }));
  1802. this.state.strictErrors.clear();
  1803. }
  1804. }
  1805. curContext() {
  1806. return this.state.context[this.state.context.length - 1];
  1807. }
  1808. nextToken() {
  1809. this.skipSpace();
  1810. this.state.start = this.state.pos;
  1811. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  1812. if (this.state.pos >= this.length) {
  1813. this.finishToken(131);
  1814. return;
  1815. }
  1816. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  1817. }
  1818. skipBlockComment() {
  1819. let startLoc;
  1820. if (!this.isLookahead) startLoc = this.state.curPosition();
  1821. const start = this.state.pos;
  1822. const end = this.input.indexOf("*/", start + 2);
  1823. if (end === -1) {
  1824. throw this.raise(ErrorMessages.UnterminatedComment, {
  1825. at: this.state.curPosition()
  1826. });
  1827. }
  1828. this.state.pos = end + 2;
  1829. lineBreakG.lastIndex = start + 2;
  1830. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  1831. ++this.state.curLine;
  1832. this.state.lineStart = lineBreakG.lastIndex;
  1833. }
  1834. if (this.isLookahead) return;
  1835. const comment = {
  1836. type: "CommentBlock",
  1837. value: this.input.slice(start + 2, end),
  1838. start,
  1839. end: end + 2,
  1840. loc: new SourceLocation(startLoc, this.state.curPosition())
  1841. };
  1842. if (this.options.tokens) this.pushToken(comment);
  1843. return comment;
  1844. }
  1845. skipLineComment(startSkip) {
  1846. const start = this.state.pos;
  1847. let startLoc;
  1848. if (!this.isLookahead) startLoc = this.state.curPosition();
  1849. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  1850. if (this.state.pos < this.length) {
  1851. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  1852. ch = this.input.charCodeAt(this.state.pos);
  1853. }
  1854. }
  1855. if (this.isLookahead) return;
  1856. const end = this.state.pos;
  1857. const value = this.input.slice(start + startSkip, end);
  1858. const comment = {
  1859. type: "CommentLine",
  1860. value,
  1861. start,
  1862. end,
  1863. loc: new SourceLocation(startLoc, this.state.curPosition())
  1864. };
  1865. if (this.options.tokens) this.pushToken(comment);
  1866. return comment;
  1867. }
  1868. skipSpace() {
  1869. const spaceStart = this.state.pos;
  1870. const comments = [];
  1871. loop: while (this.state.pos < this.length) {
  1872. const ch = this.input.charCodeAt(this.state.pos);
  1873. switch (ch) {
  1874. case 32:
  1875. case 160:
  1876. case 9:
  1877. ++this.state.pos;
  1878. break;
  1879. case 13:
  1880. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  1881. ++this.state.pos;
  1882. }
  1883. case 10:
  1884. case 8232:
  1885. case 8233:
  1886. ++this.state.pos;
  1887. ++this.state.curLine;
  1888. this.state.lineStart = this.state.pos;
  1889. break;
  1890. case 47:
  1891. switch (this.input.charCodeAt(this.state.pos + 1)) {
  1892. case 42:
  1893. {
  1894. const comment = this.skipBlockComment();
  1895. if (comment !== undefined) {
  1896. this.addComment(comment);
  1897. if (this.options.attachComment) comments.push(comment);
  1898. }
  1899. break;
  1900. }
  1901. case 47:
  1902. {
  1903. const comment = this.skipLineComment(2);
  1904. if (comment !== undefined) {
  1905. this.addComment(comment);
  1906. if (this.options.attachComment) comments.push(comment);
  1907. }
  1908. break;
  1909. }
  1910. default:
  1911. break loop;
  1912. }
  1913. break;
  1914. default:
  1915. if (isWhitespace(ch)) {
  1916. ++this.state.pos;
  1917. } else if (ch === 45 && !this.inModule) {
  1918. const pos = this.state.pos;
  1919. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  1920. const comment = this.skipLineComment(3);
  1921. if (comment !== undefined) {
  1922. this.addComment(comment);
  1923. if (this.options.attachComment) comments.push(comment);
  1924. }
  1925. } else {
  1926. break loop;
  1927. }
  1928. } else if (ch === 60 && !this.inModule) {
  1929. const pos = this.state.pos;
  1930. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  1931. const comment = this.skipLineComment(4);
  1932. if (comment !== undefined) {
  1933. this.addComment(comment);
  1934. if (this.options.attachComment) comments.push(comment);
  1935. }
  1936. } else {
  1937. break loop;
  1938. }
  1939. } else {
  1940. break loop;
  1941. }
  1942. }
  1943. }
  1944. if (comments.length > 0) {
  1945. const end = this.state.pos;
  1946. const CommentWhitespace = {
  1947. start: spaceStart,
  1948. end,
  1949. comments,
  1950. leadingNode: null,
  1951. trailingNode: null,
  1952. containingNode: null
  1953. };
  1954. this.state.commentStack.push(CommentWhitespace);
  1955. }
  1956. }
  1957. finishToken(type, val) {
  1958. this.state.end = this.state.pos;
  1959. this.state.endLoc = this.state.curPosition();
  1960. const prevType = this.state.type;
  1961. this.state.type = type;
  1962. this.state.value = val;
  1963. if (!this.isLookahead) {
  1964. this.updateContext(prevType);
  1965. }
  1966. }
  1967. replaceToken(type) {
  1968. this.state.type = type;
  1969. this.updateContext();
  1970. }
  1971. readToken_numberSign() {
  1972. if (this.state.pos === 0 && this.readToken_interpreter()) {
  1973. return;
  1974. }
  1975. const nextPos = this.state.pos + 1;
  1976. const next = this.codePointAtPos(nextPos);
  1977. if (next >= 48 && next <= 57) {
  1978. throw this.raise(ErrorMessages.UnexpectedDigitAfterHash, {
  1979. at: this.state.curPosition()
  1980. });
  1981. }
  1982. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  1983. this.expectPlugin("recordAndTuple");
  1984. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  1985. throw this.raise(next === 123 ? ErrorMessages.RecordExpressionHashIncorrectStartSyntaxType : ErrorMessages.TupleExpressionHashIncorrectStartSyntaxType, {
  1986. at: this.state.curPosition()
  1987. });
  1988. }
  1989. this.state.pos += 2;
  1990. if (next === 123) {
  1991. this.finishToken(7);
  1992. } else {
  1993. this.finishToken(1);
  1994. }
  1995. } else if (isIdentifierStart(next)) {
  1996. ++this.state.pos;
  1997. this.finishToken(130, this.readWord1(next));
  1998. } else if (next === 92) {
  1999. ++this.state.pos;
  2000. this.finishToken(130, this.readWord1());
  2001. } else {
  2002. this.finishOp(27, 1);
  2003. }
  2004. }
  2005. readToken_dot() {
  2006. const next = this.input.charCodeAt(this.state.pos + 1);
  2007. if (next >= 48 && next <= 57) {
  2008. this.readNumber(true);
  2009. return;
  2010. }
  2011. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2012. this.state.pos += 3;
  2013. this.finishToken(21);
  2014. } else {
  2015. ++this.state.pos;
  2016. this.finishToken(16);
  2017. }
  2018. }
  2019. readToken_slash() {
  2020. const next = this.input.charCodeAt(this.state.pos + 1);
  2021. if (next === 61) {
  2022. this.finishOp(31, 2);
  2023. } else {
  2024. this.finishOp(52, 1);
  2025. }
  2026. }
  2027. readToken_interpreter() {
  2028. if (this.state.pos !== 0 || this.length < 2) return false;
  2029. let ch = this.input.charCodeAt(this.state.pos + 1);
  2030. if (ch !== 33) return false;
  2031. const start = this.state.pos;
  2032. this.state.pos += 1;
  2033. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2034. ch = this.input.charCodeAt(this.state.pos);
  2035. }
  2036. const value = this.input.slice(start + 2, this.state.pos);
  2037. this.finishToken(28, value);
  2038. return true;
  2039. }
  2040. readToken_mult_modulo(code) {
  2041. let type = code === 42 ? 51 : 50;
  2042. let width = 1;
  2043. let next = this.input.charCodeAt(this.state.pos + 1);
  2044. if (code === 42 && next === 42) {
  2045. width++;
  2046. next = this.input.charCodeAt(this.state.pos + 2);
  2047. type = 53;
  2048. }
  2049. if (next === 61 && !this.state.inType) {
  2050. width++;
  2051. type = code === 37 ? 33 : 30;
  2052. }
  2053. this.finishOp(type, width);
  2054. }
  2055. readToken_pipe_amp(code) {
  2056. const next = this.input.charCodeAt(this.state.pos + 1);
  2057. if (next === code) {
  2058. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2059. this.finishOp(30, 3);
  2060. } else {
  2061. this.finishOp(code === 124 ? 39 : 40, 2);
  2062. }
  2063. return;
  2064. }
  2065. if (code === 124) {
  2066. if (next === 62) {
  2067. this.finishOp(37, 2);
  2068. return;
  2069. }
  2070. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2071. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2072. throw this.raise(ErrorMessages.RecordExpressionBarIncorrectEndSyntaxType, {
  2073. at: this.state.curPosition()
  2074. });
  2075. }
  2076. this.state.pos += 2;
  2077. this.finishToken(9);
  2078. return;
  2079. }
  2080. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2081. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2082. throw this.raise(ErrorMessages.TupleExpressionBarIncorrectEndSyntaxType, {
  2083. at: this.state.curPosition()
  2084. });
  2085. }
  2086. this.state.pos += 2;
  2087. this.finishToken(4);
  2088. return;
  2089. }
  2090. }
  2091. if (next === 61) {
  2092. this.finishOp(30, 2);
  2093. return;
  2094. }
  2095. this.finishOp(code === 124 ? 41 : 43, 1);
  2096. }
  2097. readToken_caret() {
  2098. const next = this.input.charCodeAt(this.state.pos + 1);
  2099. if (next === 61 && !this.state.inType) {
  2100. this.finishOp(32, 2);
  2101. } else {
  2102. this.finishOp(42, 1);
  2103. }
  2104. }
  2105. readToken_plus_min(code) {
  2106. const next = this.input.charCodeAt(this.state.pos + 1);
  2107. if (next === code) {
  2108. this.finishOp(34, 2);
  2109. return;
  2110. }
  2111. if (next === 61) {
  2112. this.finishOp(30, 2);
  2113. } else {
  2114. this.finishOp(49, 1);
  2115. }
  2116. }
  2117. readToken_lt() {
  2118. const {
  2119. pos
  2120. } = this.state;
  2121. const next = this.input.charCodeAt(pos + 1);
  2122. if (next === 60) {
  2123. if (this.input.charCodeAt(pos + 2) === 61) {
  2124. this.finishOp(30, 3);
  2125. return;
  2126. }
  2127. this.finishOp(48, 2);
  2128. return;
  2129. }
  2130. if (next === 61) {
  2131. this.finishOp(47, 2);
  2132. return;
  2133. }
  2134. this.finishOp(45, 1);
  2135. }
  2136. readToken_gt() {
  2137. const {
  2138. pos
  2139. } = this.state;
  2140. const next = this.input.charCodeAt(pos + 1);
  2141. if (next === 62) {
  2142. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2143. if (this.input.charCodeAt(pos + size) === 61) {
  2144. this.finishOp(30, size + 1);
  2145. return;
  2146. }
  2147. this.finishOp(48, size);
  2148. return;
  2149. }
  2150. if (next === 61) {
  2151. this.finishOp(47, 2);
  2152. return;
  2153. }
  2154. this.finishOp(46, 1);
  2155. }
  2156. readToken_eq_excl(code) {
  2157. const next = this.input.charCodeAt(this.state.pos + 1);
  2158. if (next === 61) {
  2159. this.finishOp(44, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2160. return;
  2161. }
  2162. if (code === 61 && next === 62) {
  2163. this.state.pos += 2;
  2164. this.finishToken(19);
  2165. return;
  2166. }
  2167. this.finishOp(code === 61 ? 29 : 35, 1);
  2168. }
  2169. readToken_question() {
  2170. const next = this.input.charCodeAt(this.state.pos + 1);
  2171. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2172. if (next === 63) {
  2173. if (next2 === 61) {
  2174. this.finishOp(30, 3);
  2175. } else {
  2176. this.finishOp(38, 2);
  2177. }
  2178. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2179. this.state.pos += 2;
  2180. this.finishToken(18);
  2181. } else {
  2182. ++this.state.pos;
  2183. this.finishToken(17);
  2184. }
  2185. }
  2186. getTokenFromCode(code) {
  2187. switch (code) {
  2188. case 46:
  2189. this.readToken_dot();
  2190. return;
  2191. case 40:
  2192. ++this.state.pos;
  2193. this.finishToken(10);
  2194. return;
  2195. case 41:
  2196. ++this.state.pos;
  2197. this.finishToken(11);
  2198. return;
  2199. case 59:
  2200. ++this.state.pos;
  2201. this.finishToken(13);
  2202. return;
  2203. case 44:
  2204. ++this.state.pos;
  2205. this.finishToken(12);
  2206. return;
  2207. case 91:
  2208. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2209. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2210. throw this.raise(ErrorMessages.TupleExpressionBarIncorrectStartSyntaxType, {
  2211. at: this.state.curPosition()
  2212. });
  2213. }
  2214. this.state.pos += 2;
  2215. this.finishToken(2);
  2216. } else {
  2217. ++this.state.pos;
  2218. this.finishToken(0);
  2219. }
  2220. return;
  2221. case 93:
  2222. ++this.state.pos;
  2223. this.finishToken(3);
  2224. return;
  2225. case 123:
  2226. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2227. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2228. throw this.raise(ErrorMessages.RecordExpressionBarIncorrectStartSyntaxType, {
  2229. at: this.state.curPosition()
  2230. });
  2231. }
  2232. this.state.pos += 2;
  2233. this.finishToken(6);
  2234. } else {
  2235. ++this.state.pos;
  2236. this.finishToken(5);
  2237. }
  2238. return;
  2239. case 125:
  2240. ++this.state.pos;
  2241. this.finishToken(8);
  2242. return;
  2243. case 58:
  2244. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2245. this.finishOp(15, 2);
  2246. } else {
  2247. ++this.state.pos;
  2248. this.finishToken(14);
  2249. }
  2250. return;
  2251. case 63:
  2252. this.readToken_question();
  2253. return;
  2254. case 96:
  2255. this.readTemplateToken();
  2256. return;
  2257. case 48:
  2258. {
  2259. const next = this.input.charCodeAt(this.state.pos + 1);
  2260. if (next === 120 || next === 88) {
  2261. this.readRadixNumber(16);
  2262. return;
  2263. }
  2264. if (next === 111 || next === 79) {
  2265. this.readRadixNumber(8);
  2266. return;
  2267. }
  2268. if (next === 98 || next === 66) {
  2269. this.readRadixNumber(2);
  2270. return;
  2271. }
  2272. }
  2273. case 49:
  2274. case 50:
  2275. case 51:
  2276. case 52:
  2277. case 53:
  2278. case 54:
  2279. case 55:
  2280. case 56:
  2281. case 57:
  2282. this.readNumber(false);
  2283. return;
  2284. case 34:
  2285. case 39:
  2286. this.readString(code);
  2287. return;
  2288. case 47:
  2289. this.readToken_slash();
  2290. return;
  2291. case 37:
  2292. case 42:
  2293. this.readToken_mult_modulo(code);
  2294. return;
  2295. case 124:
  2296. case 38:
  2297. this.readToken_pipe_amp(code);
  2298. return;
  2299. case 94:
  2300. this.readToken_caret();
  2301. return;
  2302. case 43:
  2303. case 45:
  2304. this.readToken_plus_min(code);
  2305. return;
  2306. case 60:
  2307. this.readToken_lt();
  2308. return;
  2309. case 62:
  2310. this.readToken_gt();
  2311. return;
  2312. case 61:
  2313. case 33:
  2314. this.readToken_eq_excl(code);
  2315. return;
  2316. case 126:
  2317. this.finishOp(36, 1);
  2318. return;
  2319. case 64:
  2320. ++this.state.pos;
  2321. this.finishToken(26);
  2322. return;
  2323. case 35:
  2324. this.readToken_numberSign();
  2325. return;
  2326. case 92:
  2327. this.readWord();
  2328. return;
  2329. default:
  2330. if (isIdentifierStart(code)) {
  2331. this.readWord(code);
  2332. return;
  2333. }
  2334. }
  2335. throw this.raise(ErrorMessages.InvalidOrUnexpectedToken, {
  2336. at: this.state.curPosition()
  2337. }, String.fromCodePoint(code));
  2338. }
  2339. finishOp(type, size) {
  2340. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2341. this.state.pos += size;
  2342. this.finishToken(type, str);
  2343. }
  2344. readRegexp() {
  2345. const startLoc = this.state.startLoc;
  2346. const start = this.state.start + 1;
  2347. let escaped, inClass;
  2348. let {
  2349. pos
  2350. } = this.state;
  2351. for (;; ++pos) {
  2352. if (pos >= this.length) {
  2353. throw this.raise(ErrorMessages.UnterminatedRegExp, {
  2354. at: createPositionWithColumnOffset(startLoc, 1)
  2355. });
  2356. }
  2357. const ch = this.input.charCodeAt(pos);
  2358. if (isNewLine(ch)) {
  2359. throw this.raise(ErrorMessages.UnterminatedRegExp, {
  2360. at: createPositionWithColumnOffset(startLoc, 1)
  2361. });
  2362. }
  2363. if (escaped) {
  2364. escaped = false;
  2365. } else {
  2366. if (ch === 91) {
  2367. inClass = true;
  2368. } else if (ch === 93 && inClass) {
  2369. inClass = false;
  2370. } else if (ch === 47 && !inClass) {
  2371. break;
  2372. }
  2373. escaped = ch === 92;
  2374. }
  2375. }
  2376. const content = this.input.slice(start, pos);
  2377. ++pos;
  2378. let mods = "";
  2379. while (pos < this.length) {
  2380. const cp = this.codePointAtPos(pos);
  2381. const char = String.fromCharCode(cp);
  2382. if (VALID_REGEX_FLAGS.has(cp)) {
  2383. if (mods.includes(char)) {
  2384. this.raise(ErrorMessages.DuplicateRegExpFlags, {
  2385. at: createPositionWithColumnOffset(startLoc, pos + 2 - start)
  2386. });
  2387. }
  2388. } else if (isIdentifierChar(cp) || cp === 92) {
  2389. this.raise(ErrorMessages.MalformedRegExpFlags, {
  2390. at: createPositionWithColumnOffset(startLoc, pos + 2 - start)
  2391. });
  2392. } else {
  2393. break;
  2394. }
  2395. ++pos;
  2396. mods += char;
  2397. }
  2398. this.state.pos = pos;
  2399. this.finishToken(129, {
  2400. pattern: content,
  2401. flags: mods
  2402. });
  2403. }
  2404. readInt(radix, len, forceLen, allowNumSeparator = true) {
  2405. const start = this.state.pos;
  2406. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2407. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  2408. let invalid = false;
  2409. let total = 0;
  2410. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2411. const code = this.input.charCodeAt(this.state.pos);
  2412. let val;
  2413. if (code === 95) {
  2414. const prev = this.input.charCodeAt(this.state.pos - 1);
  2415. const next = this.input.charCodeAt(this.state.pos + 1);
  2416. if (allowedSiblings.indexOf(next) === -1) {
  2417. this.raise(ErrorMessages.UnexpectedNumericSeparator, {
  2418. at: this.state.curPosition()
  2419. });
  2420. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  2421. this.raise(ErrorMessages.UnexpectedNumericSeparator, {
  2422. at: this.state.curPosition()
  2423. });
  2424. }
  2425. if (!allowNumSeparator) {
  2426. this.raise(ErrorMessages.NumericSeparatorInEscapeSequence, {
  2427. at: this.state.curPosition()
  2428. });
  2429. }
  2430. ++this.state.pos;
  2431. continue;
  2432. }
  2433. if (code >= 97) {
  2434. val = code - 97 + 10;
  2435. } else if (code >= 65) {
  2436. val = code - 65 + 10;
  2437. } else if (_isDigit(code)) {
  2438. val = code - 48;
  2439. } else {
  2440. val = Infinity;
  2441. }
  2442. if (val >= radix) {
  2443. if (this.options.errorRecovery && val <= 9) {
  2444. val = 0;
  2445. this.raise(ErrorMessages.InvalidDigit, {
  2446. at: this.state.curPosition()
  2447. }, radix);
  2448. } else if (forceLen) {
  2449. val = 0;
  2450. invalid = true;
  2451. } else {
  2452. break;
  2453. }
  2454. }
  2455. ++this.state.pos;
  2456. total = total * radix + val;
  2457. }
  2458. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  2459. return null;
  2460. }
  2461. return total;
  2462. }
  2463. readRadixNumber(radix) {
  2464. const startLoc = this.state.curPosition();
  2465. let isBigInt = false;
  2466. this.state.pos += 2;
  2467. const val = this.readInt(radix);
  2468. if (val == null) {
  2469. this.raise(ErrorMessages.InvalidDigit, {
  2470. at: createPositionWithColumnOffset(startLoc, 2)
  2471. }, radix);
  2472. }
  2473. const next = this.input.charCodeAt(this.state.pos);
  2474. if (next === 110) {
  2475. ++this.state.pos;
  2476. isBigInt = true;
  2477. } else if (next === 109) {
  2478. throw this.raise(ErrorMessages.InvalidDecimal, {
  2479. at: startLoc
  2480. });
  2481. }
  2482. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2483. throw this.raise(ErrorMessages.NumberIdentifier, {
  2484. at: this.state.curPosition()
  2485. });
  2486. }
  2487. if (isBigInt) {
  2488. const str = this.input.slice(indexes.get(startLoc), this.state.pos).replace(/[_n]/g, "");
  2489. this.finishToken(127, str);
  2490. return;
  2491. }
  2492. this.finishToken(126, val);
  2493. }
  2494. readNumber(startsWithDot) {
  2495. const start = this.state.pos;
  2496. const startLoc = this.state.curPosition();
  2497. let isFloat = false;
  2498. let isBigInt = false;
  2499. let isDecimal = false;
  2500. let hasExponent = false;
  2501. let isOctal = false;
  2502. if (!startsWithDot && this.readInt(10) === null) {
  2503. this.raise(ErrorMessages.InvalidNumber, {
  2504. at: this.state.curPosition()
  2505. });
  2506. }
  2507. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  2508. if (hasLeadingZero) {
  2509. const integer = this.input.slice(start, this.state.pos);
  2510. this.recordStrictModeErrors(ErrorMessages.StrictOctalLiteral, startLoc);
  2511. if (!this.state.strict) {
  2512. const underscorePos = integer.indexOf("_");
  2513. if (underscorePos > 0) {
  2514. this.raise(ErrorMessages.ZeroDigitNumericSeparator, {
  2515. at: createPositionWithColumnOffset(startLoc, underscorePos)
  2516. });
  2517. }
  2518. }
  2519. isOctal = hasLeadingZero && !/[89]/.test(integer);
  2520. }
  2521. let next = this.input.charCodeAt(this.state.pos);
  2522. if (next === 46 && !isOctal) {
  2523. ++this.state.pos;
  2524. this.readInt(10);
  2525. isFloat = true;
  2526. next = this.input.charCodeAt(this.state.pos);
  2527. }
  2528. if ((next === 69 || next === 101) && !isOctal) {
  2529. next = this.input.charCodeAt(++this.state.pos);
  2530. if (next === 43 || next === 45) {
  2531. ++this.state.pos;
  2532. }
  2533. if (this.readInt(10) === null) {
  2534. this.raise(ErrorMessages.InvalidOrMissingExponent, {
  2535. at: startLoc
  2536. });
  2537. }
  2538. isFloat = true;
  2539. hasExponent = true;
  2540. next = this.input.charCodeAt(this.state.pos);
  2541. }
  2542. if (next === 110) {
  2543. if (isFloat || hasLeadingZero) {
  2544. this.raise(ErrorMessages.InvalidBigIntLiteral, {
  2545. at: startLoc
  2546. });
  2547. }
  2548. ++this.state.pos;
  2549. isBigInt = true;
  2550. }
  2551. if (next === 109) {
  2552. this.expectPlugin("decimal", this.state.curPosition());
  2553. if (hasExponent || hasLeadingZero) {
  2554. this.raise(ErrorMessages.InvalidDecimal, {
  2555. at: startLoc
  2556. });
  2557. }
  2558. ++this.state.pos;
  2559. isDecimal = true;
  2560. }
  2561. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2562. throw this.raise(ErrorMessages.NumberIdentifier, {
  2563. at: this.state.curPosition()
  2564. });
  2565. }
  2566. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  2567. if (isBigInt) {
  2568. this.finishToken(127, str);
  2569. return;
  2570. }
  2571. if (isDecimal) {
  2572. this.finishToken(128, str);
  2573. return;
  2574. }
  2575. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  2576. this.finishToken(126, val);
  2577. }
  2578. readCodePoint(throwOnInvalid) {
  2579. const ch = this.input.charCodeAt(this.state.pos);
  2580. let code;
  2581. if (ch === 123) {
  2582. ++this.state.pos;
  2583. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  2584. ++this.state.pos;
  2585. if (code !== null && code > 0x10ffff) {
  2586. if (throwOnInvalid) {
  2587. this.raise(ErrorMessages.InvalidCodePoint, {
  2588. at: this.state.curPosition()
  2589. });
  2590. } else {
  2591. return null;
  2592. }
  2593. }
  2594. } else {
  2595. code = this.readHexChar(4, false, throwOnInvalid);
  2596. }
  2597. return code;
  2598. }
  2599. readString(quote) {
  2600. let out = "",
  2601. chunkStart = ++this.state.pos;
  2602. for (;;) {
  2603. if (this.state.pos >= this.length) {
  2604. throw this.raise(ErrorMessages.UnterminatedString, {
  2605. at: this.state.startLoc
  2606. });
  2607. }
  2608. const ch = this.input.charCodeAt(this.state.pos);
  2609. if (ch === quote) break;
  2610. if (ch === 92) {
  2611. out += this.input.slice(chunkStart, this.state.pos);
  2612. out += this.readEscapedChar(false);
  2613. chunkStart = this.state.pos;
  2614. } else if (ch === 8232 || ch === 8233) {
  2615. ++this.state.pos;
  2616. ++this.state.curLine;
  2617. this.state.lineStart = this.state.pos;
  2618. } else if (isNewLine(ch)) {
  2619. throw this.raise(ErrorMessages.UnterminatedString, {
  2620. at: this.state.startLoc
  2621. });
  2622. } else {
  2623. ++this.state.pos;
  2624. }
  2625. }
  2626. out += this.input.slice(chunkStart, this.state.pos++);
  2627. this.finishToken(125, out);
  2628. }
  2629. readTemplateContinuation() {
  2630. if (!this.match(8)) {
  2631. this.unexpected(null, 8);
  2632. }
  2633. this.state.pos--;
  2634. this.readTemplateToken();
  2635. }
  2636. readTemplateToken() {
  2637. let out = "",
  2638. chunkStart = this.state.pos,
  2639. containsInvalid = false;
  2640. ++this.state.pos;
  2641. for (;;) {
  2642. if (this.state.pos >= this.length) {
  2643. throw this.raise(ErrorMessages.UnterminatedTemplate, {
  2644. at: createPositionWithColumnOffset(this.state.startLoc, 1)
  2645. });
  2646. }
  2647. const ch = this.input.charCodeAt(this.state.pos);
  2648. if (ch === 96) {
  2649. ++this.state.pos;
  2650. out += this.input.slice(chunkStart, this.state.pos);
  2651. this.finishToken(24, containsInvalid ? null : out);
  2652. return;
  2653. }
  2654. if (ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  2655. this.state.pos += 2;
  2656. out += this.input.slice(chunkStart, this.state.pos);
  2657. this.finishToken(25, containsInvalid ? null : out);
  2658. return;
  2659. }
  2660. if (ch === 92) {
  2661. out += this.input.slice(chunkStart, this.state.pos);
  2662. const escaped = this.readEscapedChar(true);
  2663. if (escaped === null) {
  2664. containsInvalid = true;
  2665. } else {
  2666. out += escaped;
  2667. }
  2668. chunkStart = this.state.pos;
  2669. } else if (isNewLine(ch)) {
  2670. out += this.input.slice(chunkStart, this.state.pos);
  2671. ++this.state.pos;
  2672. switch (ch) {
  2673. case 13:
  2674. if (this.input.charCodeAt(this.state.pos) === 10) {
  2675. ++this.state.pos;
  2676. }
  2677. case 10:
  2678. out += "\n";
  2679. break;
  2680. default:
  2681. out += String.fromCharCode(ch);
  2682. break;
  2683. }
  2684. ++this.state.curLine;
  2685. this.state.lineStart = this.state.pos;
  2686. chunkStart = this.state.pos;
  2687. } else {
  2688. ++this.state.pos;
  2689. }
  2690. }
  2691. }
  2692. recordStrictModeErrors(message, loc) {
  2693. const index = indexes.get(loc);
  2694. if (this.state.strict && !this.state.strictErrors.has(index)) {
  2695. this.raise(message, {
  2696. at: loc
  2697. });
  2698. } else {
  2699. this.state.strictErrors.set(index, {
  2700. loc,
  2701. message
  2702. });
  2703. }
  2704. }
  2705. readEscapedChar(inTemplate) {
  2706. const throwOnInvalid = !inTemplate;
  2707. const ch = this.input.charCodeAt(++this.state.pos);
  2708. ++this.state.pos;
  2709. switch (ch) {
  2710. case 110:
  2711. return "\n";
  2712. case 114:
  2713. return "\r";
  2714. case 120:
  2715. {
  2716. const code = this.readHexChar(2, false, throwOnInvalid);
  2717. return code === null ? null : String.fromCharCode(code);
  2718. }
  2719. case 117:
  2720. {
  2721. const code = this.readCodePoint(throwOnInvalid);
  2722. return code === null ? null : String.fromCodePoint(code);
  2723. }
  2724. case 116:
  2725. return "\t";
  2726. case 98:
  2727. return "\b";
  2728. case 118:
  2729. return "\u000b";
  2730. case 102:
  2731. return "\f";
  2732. case 13:
  2733. if (this.input.charCodeAt(this.state.pos) === 10) {
  2734. ++this.state.pos;
  2735. }
  2736. case 10:
  2737. this.state.lineStart = this.state.pos;
  2738. ++this.state.curLine;
  2739. case 8232:
  2740. case 8233:
  2741. return "";
  2742. case 56:
  2743. case 57:
  2744. if (inTemplate) {
  2745. return null;
  2746. } else {
  2747. this.recordStrictModeErrors(ErrorMessages.StrictNumericEscape, createPositionWithColumnOffset(this.state.curPosition(), -1));
  2748. }
  2749. default:
  2750. if (ch >= 48 && ch <= 55) {
  2751. const codePos = createPositionWithColumnOffset(this.state.curPosition(), -1);
  2752. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  2753. let octalStr = match[0];
  2754. let octal = parseInt(octalStr, 8);
  2755. if (octal > 255) {
  2756. octalStr = octalStr.slice(0, -1);
  2757. octal = parseInt(octalStr, 8);
  2758. }
  2759. this.state.pos += octalStr.length - 1;
  2760. const next = this.input.charCodeAt(this.state.pos);
  2761. if (octalStr !== "0" || next === 56 || next === 57) {
  2762. if (inTemplate) {
  2763. return null;
  2764. } else {
  2765. this.recordStrictModeErrors(ErrorMessages.StrictNumericEscape, codePos);
  2766. }
  2767. }
  2768. return String.fromCharCode(octal);
  2769. }
  2770. return String.fromCharCode(ch);
  2771. }
  2772. }
  2773. readHexChar(len, forceLen, throwOnInvalid) {
  2774. const codeLoc = this.state.curPosition();
  2775. const n = this.readInt(16, len, forceLen, false);
  2776. if (n === null) {
  2777. if (throwOnInvalid) {
  2778. this.raise(ErrorMessages.InvalidEscapeSequence, {
  2779. at: codeLoc
  2780. });
  2781. } else {
  2782. this.state.pos = indexes.get(codeLoc) - 1;
  2783. }
  2784. }
  2785. return n;
  2786. }
  2787. readWord1(firstCode) {
  2788. this.state.containsEsc = false;
  2789. let word = "";
  2790. const start = this.state.pos;
  2791. let chunkStart = this.state.pos;
  2792. if (firstCode !== undefined) {
  2793. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  2794. }
  2795. while (this.state.pos < this.length) {
  2796. const ch = this.codePointAtPos(this.state.pos);
  2797. if (isIdentifierChar(ch)) {
  2798. this.state.pos += ch <= 0xffff ? 1 : 2;
  2799. } else if (ch === 92) {
  2800. this.state.containsEsc = true;
  2801. word += this.input.slice(chunkStart, this.state.pos);
  2802. const escStart = this.state.curPosition();
  2803. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  2804. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  2805. this.raise(ErrorMessages.MissingUnicodeEscape, {
  2806. at: this.state.curPosition()
  2807. });
  2808. chunkStart = this.state.pos - 1;
  2809. continue;
  2810. }
  2811. ++this.state.pos;
  2812. const esc = this.readCodePoint(true);
  2813. if (esc !== null) {
  2814. if (!identifierCheck(esc)) {
  2815. this.raise(ErrorMessages.EscapedCharNotAnIdentifier, {
  2816. at: escStart
  2817. });
  2818. }
  2819. word += String.fromCodePoint(esc);
  2820. }
  2821. chunkStart = this.state.pos;
  2822. } else {
  2823. break;
  2824. }
  2825. }
  2826. return word + this.input.slice(chunkStart, this.state.pos);
  2827. }
  2828. readWord(firstCode) {
  2829. const word = this.readWord1(firstCode);
  2830. const type = keywords$1.get(word);
  2831. if (type !== undefined) {
  2832. this.finishToken(type, tokenLabelName(type));
  2833. } else {
  2834. this.finishToken(124, word);
  2835. }
  2836. }
  2837. checkKeywordEscapes() {
  2838. const {
  2839. type
  2840. } = this.state;
  2841. if (tokenIsKeyword(type) && this.state.containsEsc) {
  2842. this.raise(ErrorMessages.InvalidEscapedReservedWord, {
  2843. at: this.state.startLoc
  2844. }, tokenLabelName(type));
  2845. }
  2846. }
  2847. updateContext(prevType) {}
  2848. }
  2849. class ClassScope {
  2850. constructor() {
  2851. this.privateNames = new Set();
  2852. this.loneAccessors = new Map();
  2853. this.undefinedPrivateNames = new Map();
  2854. }
  2855. }
  2856. class ClassScopeHandler {
  2857. constructor(raise) {
  2858. this.stack = [];
  2859. this.undefinedPrivateNames = new Map();
  2860. this.raise = raise;
  2861. }
  2862. current() {
  2863. return this.stack[this.stack.length - 1];
  2864. }
  2865. enter() {
  2866. this.stack.push(new ClassScope());
  2867. }
  2868. exit() {
  2869. const oldClassScope = this.stack.pop();
  2870. const current = this.current();
  2871. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  2872. if (current) {
  2873. if (!current.undefinedPrivateNames.has(name)) {
  2874. current.undefinedPrivateNames.set(name, loc);
  2875. }
  2876. } else {
  2877. this.raise(ErrorMessages.InvalidPrivateFieldResolution, {
  2878. at: loc
  2879. }, name);
  2880. }
  2881. }
  2882. }
  2883. declarePrivateName(name, elementType, loc) {
  2884. const {
  2885. privateNames,
  2886. loneAccessors,
  2887. undefinedPrivateNames
  2888. } = this.current();
  2889. let redefined = privateNames.has(name);
  2890. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  2891. const accessor = redefined && loneAccessors.get(name);
  2892. if (accessor) {
  2893. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  2894. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  2895. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  2896. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  2897. redefined = oldKind === newKind || oldStatic !== newStatic;
  2898. if (!redefined) loneAccessors.delete(name);
  2899. } else if (!redefined) {
  2900. loneAccessors.set(name, elementType);
  2901. }
  2902. }
  2903. if (redefined) {
  2904. this.raise(ErrorMessages.PrivateNameRedeclaration, {
  2905. at: loc
  2906. }, name);
  2907. }
  2908. privateNames.add(name);
  2909. undefinedPrivateNames.delete(name);
  2910. }
  2911. usePrivateName(name, loc) {
  2912. let classScope;
  2913. for (classScope of this.stack) {
  2914. if (classScope.privateNames.has(name)) return;
  2915. }
  2916. if (classScope) {
  2917. classScope.undefinedPrivateNames.set(name, loc);
  2918. } else {
  2919. this.raise(ErrorMessages.InvalidPrivateFieldResolution, {
  2920. at: loc
  2921. }, name);
  2922. }
  2923. }
  2924. }
  2925. const kExpression = 0,
  2926. kMaybeArrowParameterDeclaration = 1,
  2927. kMaybeAsyncArrowParameterDeclaration = 2,
  2928. kParameterDeclaration = 3;
  2929. class ExpressionScope {
  2930. constructor(type = kExpression) {
  2931. this.type = void 0;
  2932. this.type = type;
  2933. }
  2934. canBeArrowParameterDeclaration() {
  2935. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  2936. }
  2937. isCertainlyParameterDeclaration() {
  2938. return this.type === kParameterDeclaration;
  2939. }
  2940. }
  2941. class ArrowHeadParsingScope extends ExpressionScope {
  2942. constructor(type) {
  2943. super(type);
  2944. this.errors = new Map();
  2945. }
  2946. recordDeclarationError(message, loc) {
  2947. this.errors.set(indexes.get(loc), {
  2948. message,
  2949. loc
  2950. });
  2951. }
  2952. clearDeclarationError(loc) {
  2953. this.errors.delete(indexes.get(loc));
  2954. }
  2955. iterateErrors(iterator) {
  2956. this.errors.forEach(iterator);
  2957. }
  2958. }
  2959. class ExpressionScopeHandler {
  2960. constructor(raise) {
  2961. this.stack = [new ExpressionScope()];
  2962. this.raise = raise;
  2963. }
  2964. enter(scope) {
  2965. this.stack.push(scope);
  2966. }
  2967. exit() {
  2968. this.stack.pop();
  2969. }
  2970. recordParameterInitializerError(loc, template) {
  2971. const {
  2972. stack
  2973. } = this;
  2974. let i = stack.length - 1;
  2975. let scope = stack[i];
  2976. while (!scope.isCertainlyParameterDeclaration()) {
  2977. if (scope.canBeArrowParameterDeclaration()) {
  2978. scope.recordDeclarationError(template, loc);
  2979. } else {
  2980. return;
  2981. }
  2982. scope = stack[--i];
  2983. }
  2984. this.raise(template, {
  2985. at: loc
  2986. });
  2987. }
  2988. recordParenthesizedIdentifierError(template, loc) {
  2989. const {
  2990. stack
  2991. } = this;
  2992. const scope = stack[stack.length - 1];
  2993. if (scope.isCertainlyParameterDeclaration()) {
  2994. this.raise(template, {
  2995. at: loc
  2996. });
  2997. } else if (scope.canBeArrowParameterDeclaration()) {
  2998. scope.recordDeclarationError(template, loc);
  2999. } else {
  3000. return;
  3001. }
  3002. }
  3003. recordAsyncArrowParametersError(template, loc) {
  3004. const {
  3005. stack
  3006. } = this;
  3007. let i = stack.length - 1;
  3008. let scope = stack[i];
  3009. while (scope.canBeArrowParameterDeclaration()) {
  3010. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3011. scope.recordDeclarationError(template, loc);
  3012. }
  3013. scope = stack[--i];
  3014. }
  3015. }
  3016. validateAsPattern() {
  3017. const {
  3018. stack
  3019. } = this;
  3020. const currentScope = stack[stack.length - 1];
  3021. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3022. currentScope.iterateErrors(({
  3023. message,
  3024. loc
  3025. }) => {
  3026. this.raise(message, {
  3027. at: loc
  3028. });
  3029. let i = stack.length - 2;
  3030. let scope = stack[i];
  3031. while (scope.canBeArrowParameterDeclaration()) {
  3032. scope.clearDeclarationError(loc);
  3033. scope = stack[--i];
  3034. }
  3035. });
  3036. }
  3037. }
  3038. function newParameterDeclarationScope() {
  3039. return new ExpressionScope(kParameterDeclaration);
  3040. }
  3041. function newArrowHeadScope() {
  3042. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3043. }
  3044. function newAsyncArrowScope() {
  3045. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3046. }
  3047. function newExpressionScope() {
  3048. return new ExpressionScope();
  3049. }
  3050. const PARAM = 0b0000,
  3051. PARAM_YIELD = 0b0001,
  3052. PARAM_AWAIT = 0b0010,
  3053. PARAM_RETURN = 0b0100,
  3054. PARAM_IN = 0b1000;
  3055. class ProductionParameterHandler {
  3056. constructor() {
  3057. this.stacks = [];
  3058. }
  3059. enter(flags) {
  3060. this.stacks.push(flags);
  3061. }
  3062. exit() {
  3063. this.stacks.pop();
  3064. }
  3065. currentFlags() {
  3066. return this.stacks[this.stacks.length - 1];
  3067. }
  3068. get hasAwait() {
  3069. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3070. }
  3071. get hasYield() {
  3072. return (this.currentFlags() & PARAM_YIELD) > 0;
  3073. }
  3074. get hasReturn() {
  3075. return (this.currentFlags() & PARAM_RETURN) > 0;
  3076. }
  3077. get hasIn() {
  3078. return (this.currentFlags() & PARAM_IN) > 0;
  3079. }
  3080. }
  3081. function functionFlags(isAsync, isGenerator) {
  3082. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3083. }
  3084. class UtilParser extends Tokenizer {
  3085. addExtra(node, key, value, enumerable = true) {
  3086. if (!node) return;
  3087. const extra = node.extra = node.extra || {};
  3088. if (enumerable) {
  3089. extra[key] = value;
  3090. } else {
  3091. Object.defineProperty(extra, key, {
  3092. enumerable,
  3093. value
  3094. });
  3095. }
  3096. }
  3097. isContextual(token) {
  3098. return this.state.type === token && !this.state.containsEsc;
  3099. }
  3100. isUnparsedContextual(nameStart, name) {
  3101. const nameEnd = nameStart + name.length;
  3102. if (this.input.slice(nameStart, nameEnd) === name) {
  3103. const nextCh = this.input.charCodeAt(nameEnd);
  3104. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3105. }
  3106. return false;
  3107. }
  3108. isLookaheadContextual(name) {
  3109. const next = this.nextTokenStart();
  3110. return this.isUnparsedContextual(next, name);
  3111. }
  3112. eatContextual(token) {
  3113. if (this.isContextual(token)) {
  3114. this.next();
  3115. return true;
  3116. }
  3117. return false;
  3118. }
  3119. expectContextual(token, template) {
  3120. if (!this.eatContextual(token)) {
  3121. if (template != null) {
  3122. throw this.raise(template, {
  3123. at: this.state.startLoc
  3124. });
  3125. }
  3126. throw this.unexpected(null, token);
  3127. }
  3128. }
  3129. canInsertSemicolon() {
  3130. return this.match(131) || this.match(8) || this.hasPrecedingLineBreak();
  3131. }
  3132. hasPrecedingLineBreak() {
  3133. return lineBreak.test(this.input.slice(indexes.get(this.state.lastTokEndLoc), this.state.start));
  3134. }
  3135. hasFollowingLineBreak() {
  3136. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3137. return skipWhiteSpaceToLineBreak.test(this.input);
  3138. }
  3139. isLineTerminator() {
  3140. return this.eat(13) || this.canInsertSemicolon();
  3141. }
  3142. semicolon(allowAsi = true) {
  3143. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3144. this.raise(ErrorMessages.MissingSemicolon, {
  3145. at: this.state.lastTokEndLoc
  3146. });
  3147. }
  3148. expect(type, loc) {
  3149. this.eat(type) || this.unexpected(loc, type);
  3150. }
  3151. assertNoSpace(message = "Unexpected space.") {
  3152. if (this.state.start > indexes.get(this.state.lastTokEndLoc)) {
  3153. this.raise({
  3154. code: ErrorCodes.SyntaxError,
  3155. reasonCode: "UnexpectedSpace",
  3156. template: message
  3157. }, {
  3158. at: this.state.lastTokEndLoc
  3159. });
  3160. }
  3161. }
  3162. unexpected(loc, type) {
  3163. throw this.raise({
  3164. code: ErrorCodes.SyntaxError,
  3165. reasonCode: "UnexpectedToken",
  3166. template: type != null ? `Unexpected token, expected "${tokenLabelName(type)}"` : "Unexpected token"
  3167. }, {
  3168. at: loc != null ? loc : this.state.startLoc
  3169. });
  3170. }
  3171. getPluginNamesFromConfigs(pluginConfigs) {
  3172. return pluginConfigs.map(c => {
  3173. if (typeof c === "string") {
  3174. return c;
  3175. } else {
  3176. return c[0];
  3177. }
  3178. });
  3179. }
  3180. expectPlugin(pluginConfig, loc) {
  3181. if (!this.hasPlugin(pluginConfig)) {
  3182. throw this.raiseWithData(loc != null ? loc : this.state.startLoc, {
  3183. missingPlugin: this.getPluginNamesFromConfigs([pluginConfig])
  3184. }, `This experimental syntax requires enabling the parser plugin: ${JSON.stringify(pluginConfig)}.`);
  3185. }
  3186. return true;
  3187. }
  3188. expectOnePlugin(pluginConfigs) {
  3189. if (!pluginConfigs.some(c => this.hasPlugin(c))) {
  3190. throw this.raiseWithData(this.state.startLoc, {
  3191. missingPlugin: this.getPluginNamesFromConfigs(pluginConfigs)
  3192. }, `This experimental syntax requires enabling one of the following parser plugin(s): ${pluginConfigs.map(c => JSON.stringify(c)).join(", ")}.`);
  3193. }
  3194. }
  3195. tryParse(fn, oldState = this.state.clone()) {
  3196. const abortSignal = {
  3197. node: null
  3198. };
  3199. try {
  3200. const node = fn((node = null) => {
  3201. abortSignal.node = node;
  3202. throw abortSignal;
  3203. });
  3204. if (this.state.errors.length > oldState.errors.length) {
  3205. const failState = this.state;
  3206. this.state = oldState;
  3207. this.state.tokensLength = failState.tokensLength;
  3208. return {
  3209. node,
  3210. error: failState.errors[oldState.errors.length],
  3211. thrown: false,
  3212. aborted: false,
  3213. failState
  3214. };
  3215. }
  3216. return {
  3217. node,
  3218. error: null,
  3219. thrown: false,
  3220. aborted: false,
  3221. failState: null
  3222. };
  3223. } catch (error) {
  3224. const failState = this.state;
  3225. this.state = oldState;
  3226. if (error instanceof SyntaxError) {
  3227. return {
  3228. node: null,
  3229. error,
  3230. thrown: true,
  3231. aborted: false,
  3232. failState
  3233. };
  3234. }
  3235. if (error === abortSignal) {
  3236. return {
  3237. node: abortSignal.node,
  3238. error: null,
  3239. thrown: false,
  3240. aborted: true,
  3241. failState
  3242. };
  3243. }
  3244. throw error;
  3245. }
  3246. }
  3247. checkExpressionErrors(refExpressionErrors, andThrow) {
  3248. if (!refExpressionErrors) return false;
  3249. const {
  3250. shorthandAssignLoc,
  3251. doubleProtoLoc,
  3252. optionalParametersLoc
  3253. } = refExpressionErrors;
  3254. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc;
  3255. if (!andThrow) {
  3256. return hasErrors;
  3257. }
  3258. if (shorthandAssignLoc != null) {
  3259. this.raise(ErrorMessages.InvalidCoverInitializedName, {
  3260. at: shorthandAssignLoc
  3261. });
  3262. }
  3263. if (doubleProtoLoc != null) {
  3264. this.raise(ErrorMessages.DuplicateProto, {
  3265. at: doubleProtoLoc
  3266. });
  3267. }
  3268. if (optionalParametersLoc != null) {
  3269. this.unexpected(optionalParametersLoc);
  3270. }
  3271. }
  3272. isLiteralPropertyName() {
  3273. return tokenIsLiteralPropertyName(this.state.type);
  3274. }
  3275. isPrivateName(node) {
  3276. return node.type === "PrivateName";
  3277. }
  3278. getPrivateNameSV(node) {
  3279. return node.id.name;
  3280. }
  3281. hasPropertyAsPrivateName(node) {
  3282. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3283. }
  3284. isOptionalChain(node) {
  3285. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  3286. }
  3287. isObjectProperty(node) {
  3288. return node.type === "ObjectProperty";
  3289. }
  3290. isObjectMethod(node) {
  3291. return node.type === "ObjectMethod";
  3292. }
  3293. initializeScopes(inModule = this.options.sourceType === "module") {
  3294. const oldLabels = this.state.labels;
  3295. this.state.labels = [];
  3296. const oldExportedIdentifiers = this.exportedIdentifiers;
  3297. this.exportedIdentifiers = new Set();
  3298. const oldInModule = this.inModule;
  3299. this.inModule = inModule;
  3300. const oldScope = this.scope;
  3301. const ScopeHandler = this.getScopeHandler();
  3302. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  3303. const oldProdParam = this.prodParam;
  3304. this.prodParam = new ProductionParameterHandler();
  3305. const oldClassScope = this.classScope;
  3306. this.classScope = new ClassScopeHandler(this.raise.bind(this));
  3307. const oldExpressionScope = this.expressionScope;
  3308. this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
  3309. return () => {
  3310. this.state.labels = oldLabels;
  3311. this.exportedIdentifiers = oldExportedIdentifiers;
  3312. this.inModule = oldInModule;
  3313. this.scope = oldScope;
  3314. this.prodParam = oldProdParam;
  3315. this.classScope = oldClassScope;
  3316. this.expressionScope = oldExpressionScope;
  3317. };
  3318. }
  3319. enterInitialScopes() {
  3320. let paramFlags = PARAM;
  3321. if (this.inModule) {
  3322. paramFlags |= PARAM_AWAIT;
  3323. }
  3324. this.scope.enter(SCOPE_PROGRAM);
  3325. this.prodParam.enter(paramFlags);
  3326. }
  3327. }
  3328. class ExpressionErrors {
  3329. constructor() {
  3330. this.shorthandAssignLoc = null;
  3331. this.doubleProtoLoc = null;
  3332. this.optionalParametersLoc = null;
  3333. }
  3334. }
  3335. class Node {
  3336. constructor(parser, pos, loc) {
  3337. this.type = "";
  3338. this.start = pos;
  3339. this.end = 0;
  3340. this.loc = new SourceLocation(loc);
  3341. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3342. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3343. }
  3344. }
  3345. const NodePrototype = Node.prototype;
  3346. {
  3347. NodePrototype.__clone = function () {
  3348. const newNode = new Node();
  3349. const keys = Object.keys(this);
  3350. for (let i = 0, length = keys.length; i < length; i++) {
  3351. const key = keys[i];
  3352. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3353. newNode[key] = this[key];
  3354. }
  3355. }
  3356. return newNode;
  3357. };
  3358. }
  3359. function clonePlaceholder(node) {
  3360. return cloneIdentifier(node);
  3361. }
  3362. function cloneIdentifier(node) {
  3363. const {
  3364. type,
  3365. start,
  3366. end,
  3367. loc,
  3368. range,
  3369. extra,
  3370. name
  3371. } = node;
  3372. const cloned = Object.create(NodePrototype);
  3373. cloned.type = type;
  3374. cloned.start = start;
  3375. cloned.end = end;
  3376. cloned.loc = loc;
  3377. cloned.range = range;
  3378. cloned.extra = extra;
  3379. cloned.name = name;
  3380. if (type === "Placeholder") {
  3381. cloned.expectedNode = node.expectedNode;
  3382. }
  3383. return cloned;
  3384. }
  3385. function cloneStringLiteral(node) {
  3386. const {
  3387. type,
  3388. start,
  3389. end,
  3390. loc,
  3391. range,
  3392. extra
  3393. } = node;
  3394. if (type === "Placeholder") {
  3395. return clonePlaceholder(node);
  3396. }
  3397. const cloned = Object.create(NodePrototype);
  3398. cloned.type = type;
  3399. cloned.start = start;
  3400. cloned.end = end;
  3401. cloned.loc = loc;
  3402. cloned.range = range;
  3403. if (node.raw !== undefined) {
  3404. cloned.raw = node.raw;
  3405. } else {
  3406. cloned.extra = extra;
  3407. }
  3408. cloned.value = node.value;
  3409. return cloned;
  3410. }
  3411. class NodeUtils extends UtilParser {
  3412. startNode() {
  3413. return new Node(this, this.state.start, this.state.startLoc);
  3414. }
  3415. startNodeAt(pos, loc) {
  3416. return new Node(this, pos, loc);
  3417. }
  3418. startNodeAtNode(type) {
  3419. return this.startNodeAt(type.start, type.loc.start);
  3420. }
  3421. finishNode(node, type) {
  3422. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3423. }
  3424. finishNodeAt(node, type, endLoc) {
  3425. node.type = type;
  3426. node.end = indexes.get(endLoc);
  3427. node.loc.end = endLoc;
  3428. if (this.options.ranges) node.range[1] = node.end;
  3429. if (this.options.attachComment) this.processComment(node);
  3430. return node;
  3431. }
  3432. resetStartLocation(node, start, startLoc) {
  3433. node.start = start;
  3434. node.loc.start = startLoc;
  3435. if (this.options.ranges) node.range[0] = start;
  3436. }
  3437. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3438. node.end = indexes.get(endLoc);
  3439. node.loc.end = endLoc;
  3440. if (this.options.ranges) node.range[1] = node.end;
  3441. }
  3442. resetStartLocationFromNode(node, locationNode) {
  3443. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  3444. }
  3445. }
  3446. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3447. const FlowErrors = makeErrorTemplates({
  3448. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3449. 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.",
  3450. AssignReservedType: "Cannot overwrite reserved type %0.",
  3451. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3452. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3453. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3454. EnumBooleanMemberNotInitialized: "Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
  3455. EnumDuplicateMemberName: "Enum member names need to be unique, but the name `%0` has already been used before in enum `%1`.",
  3456. EnumInconsistentMemberValues: "Enum `%0` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.",
  3457. EnumInvalidExplicitType: "Enum type `%1` is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  3458. EnumInvalidExplicitTypeUnknownSupplied: "Supplied enum type is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  3459. EnumInvalidMemberInitializerPrimaryType: "Enum `%0` has type `%2`, so the initializer of `%1` needs to be a %2 literal.",
  3460. EnumInvalidMemberInitializerSymbolType: "Symbol enum members cannot be initialized. Use `%1,` in enum `%0`.",
  3461. EnumInvalidMemberInitializerUnknownType: "The enum member initializer for `%1` needs to be a literal (either a boolean, number, or string) in enum `%0`.",
  3462. EnumInvalidMemberName: "Enum member names cannot start with lowercase 'a' through 'z'. Instead of using `%0`, consider using `%1`, in enum `%2`.",
  3463. EnumNumberMemberNotInitialized: "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.",
  3464. EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.",
  3465. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3466. 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.",
  3467. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3468. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3469. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3470. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3471. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3472. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3473. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3474. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  3475. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3476. SpreadVariance: "Spread properties cannot have variance.",
  3477. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3478. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3479. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3480. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3481. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3482. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3483. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3484. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3485. UnexpectedReservedType: "Unexpected reserved type %0.",
  3486. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3487. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3488. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3489. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3490. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3491. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3492. UnsupportedDeclareExportKind: "`declare export %0` is not supported. Use `%1` instead.",
  3493. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3494. UnterminatedFlowComment: "Unterminated flow-comment."
  3495. }, ErrorCodes.SyntaxError, "flow");
  3496. function isEsModuleType(bodyElement) {
  3497. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3498. }
  3499. function hasTypeImportKind(node) {
  3500. return node.importKind === "type" || node.importKind === "typeof";
  3501. }
  3502. function isMaybeDefaultImport(type) {
  3503. return tokenIsKeywordOrIdentifier(type) && type !== 93;
  3504. }
  3505. const exportSuggestions = {
  3506. const: "declare export var",
  3507. let: "declare export var",
  3508. type: "export type",
  3509. interface: "export interface"
  3510. };
  3511. function partition(list, test) {
  3512. const list1 = [];
  3513. const list2 = [];
  3514. for (let i = 0; i < list.length; i++) {
  3515. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3516. }
  3517. return [list1, list2];
  3518. }
  3519. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3520. var flow = (superClass => class extends superClass {
  3521. constructor(...args) {
  3522. super(...args);
  3523. this.flowPragma = undefined;
  3524. }
  3525. getScopeHandler() {
  3526. return FlowScopeHandler;
  3527. }
  3528. shouldParseTypes() {
  3529. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3530. }
  3531. shouldParseEnums() {
  3532. return !!this.getPluginOption("flow", "enums");
  3533. }
  3534. finishToken(type, val) {
  3535. if (type !== 125 && type !== 13 && type !== 28) {
  3536. if (this.flowPragma === undefined) {
  3537. this.flowPragma = null;
  3538. }
  3539. }
  3540. return super.finishToken(type, val);
  3541. }
  3542. addComment(comment) {
  3543. if (this.flowPragma === undefined) {
  3544. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3545. if (!matches) ; else if (matches[1] === "flow") {
  3546. this.flowPragma = "flow";
  3547. } else if (matches[1] === "noflow") {
  3548. this.flowPragma = "noflow";
  3549. } else {
  3550. throw new Error("Unexpected flow pragma");
  3551. }
  3552. }
  3553. return super.addComment(comment);
  3554. }
  3555. flowParseTypeInitialiser(tok) {
  3556. const oldInType = this.state.inType;
  3557. this.state.inType = true;
  3558. this.expect(tok || 14);
  3559. const type = this.flowParseType();
  3560. this.state.inType = oldInType;
  3561. return type;
  3562. }
  3563. flowParsePredicate() {
  3564. const node = this.startNode();
  3565. const moduloLoc = this.state.startLoc;
  3566. this.next();
  3567. this.expectContextual(103);
  3568. if (this.state.lastTokStart > indexes.get(moduloLoc) + 1) {
  3569. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  3570. at: moduloLoc
  3571. });
  3572. }
  3573. if (this.eat(10)) {
  3574. node.value = this.parseExpression();
  3575. this.expect(11);
  3576. return this.finishNode(node, "DeclaredPredicate");
  3577. } else {
  3578. return this.finishNode(node, "InferredPredicate");
  3579. }
  3580. }
  3581. flowParseTypeAndPredicateInitialiser() {
  3582. const oldInType = this.state.inType;
  3583. this.state.inType = true;
  3584. this.expect(14);
  3585. let type = null;
  3586. let predicate = null;
  3587. if (this.match(50)) {
  3588. this.state.inType = oldInType;
  3589. predicate = this.flowParsePredicate();
  3590. } else {
  3591. type = this.flowParseType();
  3592. this.state.inType = oldInType;
  3593. if (this.match(50)) {
  3594. predicate = this.flowParsePredicate();
  3595. }
  3596. }
  3597. return [type, predicate];
  3598. }
  3599. flowParseDeclareClass(node) {
  3600. this.next();
  3601. this.flowParseInterfaceish(node, true);
  3602. return this.finishNode(node, "DeclareClass");
  3603. }
  3604. flowParseDeclareFunction(node) {
  3605. this.next();
  3606. const id = node.id = this.parseIdentifier();
  3607. const typeNode = this.startNode();
  3608. const typeContainer = this.startNode();
  3609. if (this.match(45)) {
  3610. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  3611. } else {
  3612. typeNode.typeParameters = null;
  3613. }
  3614. this.expect(10);
  3615. const tmp = this.flowParseFunctionTypeParams();
  3616. typeNode.params = tmp.params;
  3617. typeNode.rest = tmp.rest;
  3618. typeNode.this = tmp._this;
  3619. this.expect(11);
  3620. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3621. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  3622. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  3623. this.resetEndLocation(id);
  3624. this.semicolon();
  3625. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  3626. return this.finishNode(node, "DeclareFunction");
  3627. }
  3628. flowParseDeclare(node, insideModule) {
  3629. if (this.match(76)) {
  3630. return this.flowParseDeclareClass(node);
  3631. } else if (this.match(64)) {
  3632. return this.flowParseDeclareFunction(node);
  3633. } else if (this.match(70)) {
  3634. return this.flowParseDeclareVariable(node);
  3635. } else if (this.eatContextual(119)) {
  3636. if (this.match(16)) {
  3637. return this.flowParseDeclareModuleExports(node);
  3638. } else {
  3639. if (insideModule) {
  3640. this.raise(FlowErrors.NestedDeclareModule, {
  3641. at: this.state.lastTokStartLoc
  3642. });
  3643. }
  3644. return this.flowParseDeclareModule(node);
  3645. }
  3646. } else if (this.isContextual(122)) {
  3647. return this.flowParseDeclareTypeAlias(node);
  3648. } else if (this.isContextual(123)) {
  3649. return this.flowParseDeclareOpaqueType(node);
  3650. } else if (this.isContextual(121)) {
  3651. return this.flowParseDeclareInterface(node);
  3652. } else if (this.match(78)) {
  3653. return this.flowParseDeclareExportDeclaration(node, insideModule);
  3654. } else {
  3655. throw this.unexpected();
  3656. }
  3657. }
  3658. flowParseDeclareVariable(node) {
  3659. this.next();
  3660. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  3661. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  3662. this.semicolon();
  3663. return this.finishNode(node, "DeclareVariable");
  3664. }
  3665. flowParseDeclareModule(node) {
  3666. this.scope.enter(SCOPE_OTHER);
  3667. if (this.match(125)) {
  3668. node.id = this.parseExprAtom();
  3669. } else {
  3670. node.id = this.parseIdentifier();
  3671. }
  3672. const bodyNode = node.body = this.startNode();
  3673. const body = bodyNode.body = [];
  3674. this.expect(5);
  3675. while (!this.match(8)) {
  3676. let bodyNode = this.startNode();
  3677. if (this.match(79)) {
  3678. this.next();
  3679. if (!this.isContextual(122) && !this.match(83)) {
  3680. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  3681. at: this.state.lastTokStartLoc
  3682. });
  3683. }
  3684. this.parseImport(bodyNode);
  3685. } else {
  3686. this.expectContextual(117, FlowErrors.UnsupportedStatementInDeclareModule);
  3687. bodyNode = this.flowParseDeclare(bodyNode, true);
  3688. }
  3689. body.push(bodyNode);
  3690. }
  3691. this.scope.exit();
  3692. this.expect(8);
  3693. this.finishNode(bodyNode, "BlockStatement");
  3694. let kind = null;
  3695. let hasModuleExport = false;
  3696. body.forEach(bodyElement => {
  3697. if (isEsModuleType(bodyElement)) {
  3698. if (kind === "CommonJS") {
  3699. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  3700. node: bodyElement
  3701. });
  3702. }
  3703. kind = "ES";
  3704. } else if (bodyElement.type === "DeclareModuleExports") {
  3705. if (hasModuleExport) {
  3706. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  3707. node: bodyElement
  3708. });
  3709. }
  3710. if (kind === "ES") {
  3711. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  3712. node: bodyElement
  3713. });
  3714. }
  3715. kind = "CommonJS";
  3716. hasModuleExport = true;
  3717. }
  3718. });
  3719. node.kind = kind || "CommonJS";
  3720. return this.finishNode(node, "DeclareModule");
  3721. }
  3722. flowParseDeclareExportDeclaration(node, insideModule) {
  3723. this.expect(78);
  3724. if (this.eat(61)) {
  3725. if (this.match(64) || this.match(76)) {
  3726. node.declaration = this.flowParseDeclare(this.startNode());
  3727. } else {
  3728. node.declaration = this.flowParseType();
  3729. this.semicolon();
  3730. }
  3731. node.default = true;
  3732. return this.finishNode(node, "DeclareExportDeclaration");
  3733. } else {
  3734. if (this.match(71) || this.isLet() || (this.isContextual(122) || this.isContextual(121)) && !insideModule) {
  3735. const label = this.state.value;
  3736. const suggestion = exportSuggestions[label];
  3737. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  3738. at: this.state.startLoc
  3739. }, label, suggestion);
  3740. }
  3741. if (this.match(70) || this.match(64) || this.match(76) || this.isContextual(123)) {
  3742. node.declaration = this.flowParseDeclare(this.startNode());
  3743. node.default = false;
  3744. return this.finishNode(node, "DeclareExportDeclaration");
  3745. } else if (this.match(51) || this.match(5) || this.isContextual(121) || this.isContextual(122) || this.isContextual(123)) {
  3746. node = this.parseExport(node);
  3747. if (node.type === "ExportNamedDeclaration") {
  3748. node.type = "ExportDeclaration";
  3749. node.default = false;
  3750. delete node.exportKind;
  3751. }
  3752. node.type = "Declare" + node.type;
  3753. return node;
  3754. }
  3755. }
  3756. throw this.unexpected();
  3757. }
  3758. flowParseDeclareModuleExports(node) {
  3759. this.next();
  3760. this.expectContextual(104);
  3761. node.typeAnnotation = this.flowParseTypeAnnotation();
  3762. this.semicolon();
  3763. return this.finishNode(node, "DeclareModuleExports");
  3764. }
  3765. flowParseDeclareTypeAlias(node) {
  3766. this.next();
  3767. this.flowParseTypeAlias(node);
  3768. node.type = "DeclareTypeAlias";
  3769. return node;
  3770. }
  3771. flowParseDeclareOpaqueType(node) {
  3772. this.next();
  3773. this.flowParseOpaqueType(node, true);
  3774. node.type = "DeclareOpaqueType";
  3775. return node;
  3776. }
  3777. flowParseDeclareInterface(node) {
  3778. this.next();
  3779. this.flowParseInterfaceish(node);
  3780. return this.finishNode(node, "DeclareInterface");
  3781. }
  3782. flowParseInterfaceish(node, isClass = false) {
  3783. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  3784. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  3785. if (this.match(45)) {
  3786. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3787. } else {
  3788. node.typeParameters = null;
  3789. }
  3790. node.extends = [];
  3791. node.implements = [];
  3792. node.mixins = [];
  3793. if (this.eat(77)) {
  3794. do {
  3795. node.extends.push(this.flowParseInterfaceExtends());
  3796. } while (!isClass && this.eat(12));
  3797. }
  3798. if (this.isContextual(110)) {
  3799. this.next();
  3800. do {
  3801. node.mixins.push(this.flowParseInterfaceExtends());
  3802. } while (this.eat(12));
  3803. }
  3804. if (this.isContextual(106)) {
  3805. this.next();
  3806. do {
  3807. node.implements.push(this.flowParseInterfaceExtends());
  3808. } while (this.eat(12));
  3809. }
  3810. node.body = this.flowParseObjectType({
  3811. allowStatic: isClass,
  3812. allowExact: false,
  3813. allowSpread: false,
  3814. allowProto: isClass,
  3815. allowInexact: false
  3816. });
  3817. }
  3818. flowParseInterfaceExtends() {
  3819. const node = this.startNode();
  3820. node.id = this.flowParseQualifiedTypeIdentifier();
  3821. if (this.match(45)) {
  3822. node.typeParameters = this.flowParseTypeParameterInstantiation();
  3823. } else {
  3824. node.typeParameters = null;
  3825. }
  3826. return this.finishNode(node, "InterfaceExtends");
  3827. }
  3828. flowParseInterface(node) {
  3829. this.flowParseInterfaceish(node);
  3830. return this.finishNode(node, "InterfaceDeclaration");
  3831. }
  3832. checkNotUnderscore(word) {
  3833. if (word === "_") {
  3834. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  3835. at: this.state.startLoc
  3836. });
  3837. }
  3838. }
  3839. checkReservedType(word, startLoc, declaration) {
  3840. if (!reservedTypes.has(word)) return;
  3841. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  3842. at: startLoc
  3843. }, word);
  3844. }
  3845. flowParseRestrictedIdentifier(liberal, declaration) {
  3846. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  3847. return this.parseIdentifier(liberal);
  3848. }
  3849. flowParseTypeAlias(node) {
  3850. node.id = this.flowParseRestrictedIdentifier(false, true);
  3851. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  3852. if (this.match(45)) {
  3853. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3854. } else {
  3855. node.typeParameters = null;
  3856. }
  3857. node.right = this.flowParseTypeInitialiser(29);
  3858. this.semicolon();
  3859. return this.finishNode(node, "TypeAlias");
  3860. }
  3861. flowParseOpaqueType(node, declare) {
  3862. this.expectContextual(122);
  3863. node.id = this.flowParseRestrictedIdentifier(true, true);
  3864. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  3865. if (this.match(45)) {
  3866. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3867. } else {
  3868. node.typeParameters = null;
  3869. }
  3870. node.supertype = null;
  3871. if (this.match(14)) {
  3872. node.supertype = this.flowParseTypeInitialiser(14);
  3873. }
  3874. node.impltype = null;
  3875. if (!declare) {
  3876. node.impltype = this.flowParseTypeInitialiser(29);
  3877. }
  3878. this.semicolon();
  3879. return this.finishNode(node, "OpaqueType");
  3880. }
  3881. flowParseTypeParameter(requireDefault = false) {
  3882. const nodeStartLoc = this.state.startLoc;
  3883. const node = this.startNode();
  3884. const variance = this.flowParseVariance();
  3885. const ident = this.flowParseTypeAnnotatableIdentifier();
  3886. node.name = ident.name;
  3887. node.variance = variance;
  3888. node.bound = ident.typeAnnotation;
  3889. if (this.match(29)) {
  3890. this.eat(29);
  3891. node.default = this.flowParseType();
  3892. } else {
  3893. if (requireDefault) {
  3894. this.raise(FlowErrors.MissingTypeParamDefault, {
  3895. at: nodeStartLoc
  3896. });
  3897. }
  3898. }
  3899. return this.finishNode(node, "TypeParameter");
  3900. }
  3901. flowParseTypeParameterDeclaration() {
  3902. const oldInType = this.state.inType;
  3903. const node = this.startNode();
  3904. node.params = [];
  3905. this.state.inType = true;
  3906. if (this.match(45) || this.match(134)) {
  3907. this.next();
  3908. } else {
  3909. this.unexpected();
  3910. }
  3911. let defaultRequired = false;
  3912. do {
  3913. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  3914. node.params.push(typeParameter);
  3915. if (typeParameter.default) {
  3916. defaultRequired = true;
  3917. }
  3918. if (!this.match(46)) {
  3919. this.expect(12);
  3920. }
  3921. } while (!this.match(46));
  3922. this.expect(46);
  3923. this.state.inType = oldInType;
  3924. return this.finishNode(node, "TypeParameterDeclaration");
  3925. }
  3926. flowParseTypeParameterInstantiation() {
  3927. const node = this.startNode();
  3928. const oldInType = this.state.inType;
  3929. node.params = [];
  3930. this.state.inType = true;
  3931. this.expect(45);
  3932. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3933. this.state.noAnonFunctionType = false;
  3934. while (!this.match(46)) {
  3935. node.params.push(this.flowParseType());
  3936. if (!this.match(46)) {
  3937. this.expect(12);
  3938. }
  3939. }
  3940. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3941. this.expect(46);
  3942. this.state.inType = oldInType;
  3943. return this.finishNode(node, "TypeParameterInstantiation");
  3944. }
  3945. flowParseTypeParameterInstantiationCallOrNew() {
  3946. const node = this.startNode();
  3947. const oldInType = this.state.inType;
  3948. node.params = [];
  3949. this.state.inType = true;
  3950. this.expect(45);
  3951. while (!this.match(46)) {
  3952. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  3953. if (!this.match(46)) {
  3954. this.expect(12);
  3955. }
  3956. }
  3957. this.expect(46);
  3958. this.state.inType = oldInType;
  3959. return this.finishNode(node, "TypeParameterInstantiation");
  3960. }
  3961. flowParseInterfaceType() {
  3962. const node = this.startNode();
  3963. this.expectContextual(121);
  3964. node.extends = [];
  3965. if (this.eat(77)) {
  3966. do {
  3967. node.extends.push(this.flowParseInterfaceExtends());
  3968. } while (this.eat(12));
  3969. }
  3970. node.body = this.flowParseObjectType({
  3971. allowStatic: false,
  3972. allowExact: false,
  3973. allowSpread: false,
  3974. allowProto: false,
  3975. allowInexact: false
  3976. });
  3977. return this.finishNode(node, "InterfaceTypeAnnotation");
  3978. }
  3979. flowParseObjectPropertyKey() {
  3980. return this.match(126) || this.match(125) ? this.parseExprAtom() : this.parseIdentifier(true);
  3981. }
  3982. flowParseObjectTypeIndexer(node, isStatic, variance) {
  3983. node.static = isStatic;
  3984. if (this.lookahead().type === 14) {
  3985. node.id = this.flowParseObjectPropertyKey();
  3986. node.key = this.flowParseTypeInitialiser();
  3987. } else {
  3988. node.id = null;
  3989. node.key = this.flowParseType();
  3990. }
  3991. this.expect(3);
  3992. node.value = this.flowParseTypeInitialiser();
  3993. node.variance = variance;
  3994. return this.finishNode(node, "ObjectTypeIndexer");
  3995. }
  3996. flowParseObjectTypeInternalSlot(node, isStatic) {
  3997. node.static = isStatic;
  3998. node.id = this.flowParseObjectPropertyKey();
  3999. this.expect(3);
  4000. this.expect(3);
  4001. if (this.match(45) || this.match(10)) {
  4002. node.method = true;
  4003. node.optional = false;
  4004. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4005. } else {
  4006. node.method = false;
  4007. if (this.eat(17)) {
  4008. node.optional = true;
  4009. }
  4010. node.value = this.flowParseTypeInitialiser();
  4011. }
  4012. return this.finishNode(node, "ObjectTypeInternalSlot");
  4013. }
  4014. flowParseObjectTypeMethodish(node) {
  4015. node.params = [];
  4016. node.rest = null;
  4017. node.typeParameters = null;
  4018. node.this = null;
  4019. if (this.match(45)) {
  4020. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4021. }
  4022. this.expect(10);
  4023. if (this.match(74)) {
  4024. node.this = this.flowParseFunctionTypeParam(true);
  4025. node.this.name = null;
  4026. if (!this.match(11)) {
  4027. this.expect(12);
  4028. }
  4029. }
  4030. while (!this.match(11) && !this.match(21)) {
  4031. node.params.push(this.flowParseFunctionTypeParam(false));
  4032. if (!this.match(11)) {
  4033. this.expect(12);
  4034. }
  4035. }
  4036. if (this.eat(21)) {
  4037. node.rest = this.flowParseFunctionTypeParam(false);
  4038. }
  4039. this.expect(11);
  4040. node.returnType = this.flowParseTypeInitialiser();
  4041. return this.finishNode(node, "FunctionTypeAnnotation");
  4042. }
  4043. flowParseObjectTypeCallProperty(node, isStatic) {
  4044. const valueNode = this.startNode();
  4045. node.static = isStatic;
  4046. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4047. return this.finishNode(node, "ObjectTypeCallProperty");
  4048. }
  4049. flowParseObjectType({
  4050. allowStatic,
  4051. allowExact,
  4052. allowSpread,
  4053. allowProto,
  4054. allowInexact
  4055. }) {
  4056. const oldInType = this.state.inType;
  4057. this.state.inType = true;
  4058. const nodeStart = this.startNode();
  4059. nodeStart.callProperties = [];
  4060. nodeStart.properties = [];
  4061. nodeStart.indexers = [];
  4062. nodeStart.internalSlots = [];
  4063. let endDelim;
  4064. let exact;
  4065. let inexact = false;
  4066. if (allowExact && this.match(6)) {
  4067. this.expect(6);
  4068. endDelim = 9;
  4069. exact = true;
  4070. } else {
  4071. this.expect(5);
  4072. endDelim = 8;
  4073. exact = false;
  4074. }
  4075. nodeStart.exact = exact;
  4076. while (!this.match(endDelim)) {
  4077. let isStatic = false;
  4078. let protoStartLoc = null;
  4079. let inexactStartLoc = null;
  4080. const node = this.startNode();
  4081. if (allowProto && this.isContextual(111)) {
  4082. const lookahead = this.lookahead();
  4083. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4084. this.next();
  4085. protoStartLoc = this.state.startLoc;
  4086. allowStatic = false;
  4087. }
  4088. }
  4089. if (allowStatic && this.isContextual(100)) {
  4090. const lookahead = this.lookahead();
  4091. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4092. this.next();
  4093. isStatic = true;
  4094. }
  4095. }
  4096. const variance = this.flowParseVariance();
  4097. if (this.eat(0)) {
  4098. if (protoStartLoc != null) {
  4099. this.unexpected(protoStartLoc);
  4100. }
  4101. if (this.eat(0)) {
  4102. if (variance) {
  4103. this.unexpected(variance.loc.start);
  4104. }
  4105. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4106. } else {
  4107. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4108. }
  4109. } else if (this.match(10) || this.match(45)) {
  4110. if (protoStartLoc != null) {
  4111. this.unexpected(protoStartLoc);
  4112. }
  4113. if (variance) {
  4114. this.unexpected(variance.loc.start);
  4115. }
  4116. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4117. } else {
  4118. let kind = "init";
  4119. if (this.isContextual(94) || this.isContextual(99)) {
  4120. const lookahead = this.lookahead();
  4121. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4122. kind = this.state.value;
  4123. this.next();
  4124. }
  4125. }
  4126. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4127. if (propOrInexact === null) {
  4128. inexact = true;
  4129. inexactStartLoc = this.state.lastTokStartLoc;
  4130. } else {
  4131. nodeStart.properties.push(propOrInexact);
  4132. }
  4133. }
  4134. this.flowObjectTypeSemicolon();
  4135. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4136. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4137. at: inexactStartLoc
  4138. });
  4139. }
  4140. }
  4141. this.expect(endDelim);
  4142. if (allowSpread) {
  4143. nodeStart.inexact = inexact;
  4144. }
  4145. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4146. this.state.inType = oldInType;
  4147. return out;
  4148. }
  4149. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4150. if (this.eat(21)) {
  4151. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4152. if (isInexactToken) {
  4153. if (!allowSpread) {
  4154. this.raise(FlowErrors.InexactInsideNonObject, {
  4155. at: this.state.lastTokStartLoc
  4156. });
  4157. } else if (!allowInexact) {
  4158. this.raise(FlowErrors.InexactInsideExact, {
  4159. at: this.state.lastTokStartLoc
  4160. });
  4161. }
  4162. if (variance) {
  4163. this.raise(FlowErrors.InexactVariance, {
  4164. node: variance
  4165. });
  4166. }
  4167. return null;
  4168. }
  4169. if (!allowSpread) {
  4170. this.raise(FlowErrors.UnexpectedSpreadType, {
  4171. at: this.state.lastTokStartLoc
  4172. });
  4173. }
  4174. if (protoStartLoc != null) {
  4175. this.unexpected(protoStartLoc);
  4176. }
  4177. if (variance) {
  4178. this.raise(FlowErrors.SpreadVariance, {
  4179. node: variance
  4180. });
  4181. }
  4182. node.argument = this.flowParseType();
  4183. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4184. } else {
  4185. node.key = this.flowParseObjectPropertyKey();
  4186. node.static = isStatic;
  4187. node.proto = protoStartLoc != null;
  4188. node.kind = kind;
  4189. let optional = false;
  4190. if (this.match(45) || this.match(10)) {
  4191. node.method = true;
  4192. if (protoStartLoc != null) {
  4193. this.unexpected(protoStartLoc);
  4194. }
  4195. if (variance) {
  4196. this.unexpected(variance.loc.start);
  4197. }
  4198. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4199. if (kind === "get" || kind === "set") {
  4200. this.flowCheckGetterSetterParams(node);
  4201. }
  4202. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4203. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4204. node: node.value.this
  4205. });
  4206. }
  4207. } else {
  4208. if (kind !== "init") this.unexpected();
  4209. node.method = false;
  4210. if (this.eat(17)) {
  4211. optional = true;
  4212. }
  4213. node.value = this.flowParseTypeInitialiser();
  4214. node.variance = variance;
  4215. }
  4216. node.optional = optional;
  4217. return this.finishNode(node, "ObjectTypeProperty");
  4218. }
  4219. }
  4220. flowCheckGetterSetterParams(property) {
  4221. const paramCount = property.kind === "get" ? 0 : 1;
  4222. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4223. if (property.value.this) {
  4224. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4225. node: property.value.this
  4226. });
  4227. }
  4228. if (length !== paramCount) {
  4229. this.raise(property.kind === "get" ? ErrorMessages.BadGetterArity : ErrorMessages.BadSetterArity, {
  4230. node: property
  4231. });
  4232. }
  4233. if (property.kind === "set" && property.value.rest) {
  4234. this.raise(ErrorMessages.BadSetterRestParameter, {
  4235. node: property
  4236. });
  4237. }
  4238. }
  4239. flowObjectTypeSemicolon() {
  4240. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4241. this.unexpected();
  4242. }
  4243. }
  4244. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  4245. startPos = startPos || this.state.start;
  4246. startLoc = startLoc || this.state.startLoc;
  4247. let node = id || this.flowParseRestrictedIdentifier(true);
  4248. while (this.eat(16)) {
  4249. const node2 = this.startNodeAt(startPos, startLoc);
  4250. node2.qualification = node;
  4251. node2.id = this.flowParseRestrictedIdentifier(true);
  4252. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4253. }
  4254. return node;
  4255. }
  4256. flowParseGenericType(startPos, startLoc, id) {
  4257. const node = this.startNodeAt(startPos, startLoc);
  4258. node.typeParameters = null;
  4259. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  4260. if (this.match(45)) {
  4261. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4262. }
  4263. return this.finishNode(node, "GenericTypeAnnotation");
  4264. }
  4265. flowParseTypeofType() {
  4266. const node = this.startNode();
  4267. this.expect(83);
  4268. node.argument = this.flowParsePrimaryType();
  4269. return this.finishNode(node, "TypeofTypeAnnotation");
  4270. }
  4271. flowParseTupleType() {
  4272. const node = this.startNode();
  4273. node.types = [];
  4274. this.expect(0);
  4275. while (this.state.pos < this.length && !this.match(3)) {
  4276. node.types.push(this.flowParseType());
  4277. if (this.match(3)) break;
  4278. this.expect(12);
  4279. }
  4280. this.expect(3);
  4281. return this.finishNode(node, "TupleTypeAnnotation");
  4282. }
  4283. flowParseFunctionTypeParam(first) {
  4284. let name = null;
  4285. let optional = false;
  4286. let typeAnnotation = null;
  4287. const node = this.startNode();
  4288. const lh = this.lookahead();
  4289. const isThis = this.state.type === 74;
  4290. if (lh.type === 14 || lh.type === 17) {
  4291. if (isThis && !first) {
  4292. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4293. node
  4294. });
  4295. }
  4296. name = this.parseIdentifier(isThis);
  4297. if (this.eat(17)) {
  4298. optional = true;
  4299. if (isThis) {
  4300. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4301. node
  4302. });
  4303. }
  4304. }
  4305. typeAnnotation = this.flowParseTypeInitialiser();
  4306. } else {
  4307. typeAnnotation = this.flowParseType();
  4308. }
  4309. node.name = name;
  4310. node.optional = optional;
  4311. node.typeAnnotation = typeAnnotation;
  4312. return this.finishNode(node, "FunctionTypeParam");
  4313. }
  4314. reinterpretTypeAsFunctionTypeParam(type) {
  4315. const node = this.startNodeAt(type.start, type.loc.start);
  4316. node.name = null;
  4317. node.optional = false;
  4318. node.typeAnnotation = type;
  4319. return this.finishNode(node, "FunctionTypeParam");
  4320. }
  4321. flowParseFunctionTypeParams(params = []) {
  4322. let rest = null;
  4323. let _this = null;
  4324. if (this.match(74)) {
  4325. _this = this.flowParseFunctionTypeParam(true);
  4326. _this.name = null;
  4327. if (!this.match(11)) {
  4328. this.expect(12);
  4329. }
  4330. }
  4331. while (!this.match(11) && !this.match(21)) {
  4332. params.push(this.flowParseFunctionTypeParam(false));
  4333. if (!this.match(11)) {
  4334. this.expect(12);
  4335. }
  4336. }
  4337. if (this.eat(21)) {
  4338. rest = this.flowParseFunctionTypeParam(false);
  4339. }
  4340. return {
  4341. params,
  4342. rest,
  4343. _this
  4344. };
  4345. }
  4346. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  4347. switch (id.name) {
  4348. case "any":
  4349. return this.finishNode(node, "AnyTypeAnnotation");
  4350. case "bool":
  4351. case "boolean":
  4352. return this.finishNode(node, "BooleanTypeAnnotation");
  4353. case "mixed":
  4354. return this.finishNode(node, "MixedTypeAnnotation");
  4355. case "empty":
  4356. return this.finishNode(node, "EmptyTypeAnnotation");
  4357. case "number":
  4358. return this.finishNode(node, "NumberTypeAnnotation");
  4359. case "string":
  4360. return this.finishNode(node, "StringTypeAnnotation");
  4361. case "symbol":
  4362. return this.finishNode(node, "SymbolTypeAnnotation");
  4363. default:
  4364. this.checkNotUnderscore(id.name);
  4365. return this.flowParseGenericType(startPos, startLoc, id);
  4366. }
  4367. }
  4368. flowParsePrimaryType() {
  4369. const startPos = this.state.start;
  4370. const startLoc = this.state.startLoc;
  4371. const node = this.startNode();
  4372. let tmp;
  4373. let type;
  4374. let isGroupedType = false;
  4375. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4376. switch (this.state.type) {
  4377. case 5:
  4378. return this.flowParseObjectType({
  4379. allowStatic: false,
  4380. allowExact: false,
  4381. allowSpread: true,
  4382. allowProto: false,
  4383. allowInexact: true
  4384. });
  4385. case 6:
  4386. return this.flowParseObjectType({
  4387. allowStatic: false,
  4388. allowExact: true,
  4389. allowSpread: true,
  4390. allowProto: false,
  4391. allowInexact: false
  4392. });
  4393. case 0:
  4394. this.state.noAnonFunctionType = false;
  4395. type = this.flowParseTupleType();
  4396. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4397. return type;
  4398. case 45:
  4399. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4400. this.expect(10);
  4401. tmp = this.flowParseFunctionTypeParams();
  4402. node.params = tmp.params;
  4403. node.rest = tmp.rest;
  4404. node.this = tmp._this;
  4405. this.expect(11);
  4406. this.expect(19);
  4407. node.returnType = this.flowParseType();
  4408. return this.finishNode(node, "FunctionTypeAnnotation");
  4409. case 10:
  4410. this.next();
  4411. if (!this.match(11) && !this.match(21)) {
  4412. if (tokenIsIdentifier(this.state.type) || this.match(74)) {
  4413. const token = this.lookahead().type;
  4414. isGroupedType = token !== 17 && token !== 14;
  4415. } else {
  4416. isGroupedType = true;
  4417. }
  4418. }
  4419. if (isGroupedType) {
  4420. this.state.noAnonFunctionType = false;
  4421. type = this.flowParseType();
  4422. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4423. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4424. this.expect(11);
  4425. return type;
  4426. } else {
  4427. this.eat(12);
  4428. }
  4429. }
  4430. if (type) {
  4431. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4432. } else {
  4433. tmp = this.flowParseFunctionTypeParams();
  4434. }
  4435. node.params = tmp.params;
  4436. node.rest = tmp.rest;
  4437. node.this = tmp._this;
  4438. this.expect(11);
  4439. this.expect(19);
  4440. node.returnType = this.flowParseType();
  4441. node.typeParameters = null;
  4442. return this.finishNode(node, "FunctionTypeAnnotation");
  4443. case 125:
  4444. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4445. case 81:
  4446. case 82:
  4447. node.value = this.match(81);
  4448. this.next();
  4449. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4450. case 49:
  4451. if (this.state.value === "-") {
  4452. this.next();
  4453. if (this.match(126)) {
  4454. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4455. }
  4456. if (this.match(127)) {
  4457. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4458. }
  4459. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4460. at: this.state.startLoc
  4461. });
  4462. }
  4463. throw this.unexpected();
  4464. case 126:
  4465. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4466. case 127:
  4467. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4468. case 84:
  4469. this.next();
  4470. return this.finishNode(node, "VoidTypeAnnotation");
  4471. case 80:
  4472. this.next();
  4473. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4474. case 74:
  4475. this.next();
  4476. return this.finishNode(node, "ThisTypeAnnotation");
  4477. case 51:
  4478. this.next();
  4479. return this.finishNode(node, "ExistsTypeAnnotation");
  4480. case 83:
  4481. return this.flowParseTypeofType();
  4482. default:
  4483. if (tokenIsKeyword(this.state.type)) {
  4484. const label = tokenLabelName(this.state.type);
  4485. this.next();
  4486. return super.createIdentifier(node, label);
  4487. } else if (tokenIsIdentifier(this.state.type)) {
  4488. if (this.isContextual(121)) {
  4489. return this.flowParseInterfaceType();
  4490. }
  4491. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  4492. }
  4493. }
  4494. throw this.unexpected();
  4495. }
  4496. flowParsePostfixType() {
  4497. const startPos = this.state.start;
  4498. const startLoc = this.state.startLoc;
  4499. let type = this.flowParsePrimaryType();
  4500. let seenOptionalIndexedAccess = false;
  4501. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4502. const node = this.startNodeAt(startPos, startLoc);
  4503. const optional = this.eat(18);
  4504. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4505. this.expect(0);
  4506. if (!optional && this.match(3)) {
  4507. node.elementType = type;
  4508. this.next();
  4509. type = this.finishNode(node, "ArrayTypeAnnotation");
  4510. } else {
  4511. node.objectType = type;
  4512. node.indexType = this.flowParseType();
  4513. this.expect(3);
  4514. if (seenOptionalIndexedAccess) {
  4515. node.optional = optional;
  4516. type = this.finishNode(node, "OptionalIndexedAccessType");
  4517. } else {
  4518. type = this.finishNode(node, "IndexedAccessType");
  4519. }
  4520. }
  4521. }
  4522. return type;
  4523. }
  4524. flowParsePrefixType() {
  4525. const node = this.startNode();
  4526. if (this.eat(17)) {
  4527. node.typeAnnotation = this.flowParsePrefixType();
  4528. return this.finishNode(node, "NullableTypeAnnotation");
  4529. } else {
  4530. return this.flowParsePostfixType();
  4531. }
  4532. }
  4533. flowParseAnonFunctionWithoutParens() {
  4534. const param = this.flowParsePrefixType();
  4535. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4536. const node = this.startNodeAt(param.start, param.loc.start);
  4537. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4538. node.rest = null;
  4539. node.this = null;
  4540. node.returnType = this.flowParseType();
  4541. node.typeParameters = null;
  4542. return this.finishNode(node, "FunctionTypeAnnotation");
  4543. }
  4544. return param;
  4545. }
  4546. flowParseIntersectionType() {
  4547. const node = this.startNode();
  4548. this.eat(43);
  4549. const type = this.flowParseAnonFunctionWithoutParens();
  4550. node.types = [type];
  4551. while (this.eat(43)) {
  4552. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4553. }
  4554. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4555. }
  4556. flowParseUnionType() {
  4557. const node = this.startNode();
  4558. this.eat(41);
  4559. const type = this.flowParseIntersectionType();
  4560. node.types = [type];
  4561. while (this.eat(41)) {
  4562. node.types.push(this.flowParseIntersectionType());
  4563. }
  4564. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  4565. }
  4566. flowParseType() {
  4567. const oldInType = this.state.inType;
  4568. this.state.inType = true;
  4569. const type = this.flowParseUnionType();
  4570. this.state.inType = oldInType;
  4571. return type;
  4572. }
  4573. flowParseTypeOrImplicitInstantiation() {
  4574. if (this.state.type === 124 && this.state.value === "_") {
  4575. const startPos = this.state.start;
  4576. const startLoc = this.state.startLoc;
  4577. const node = this.parseIdentifier();
  4578. return this.flowParseGenericType(startPos, startLoc, node);
  4579. } else {
  4580. return this.flowParseType();
  4581. }
  4582. }
  4583. flowParseTypeAnnotation() {
  4584. const node = this.startNode();
  4585. node.typeAnnotation = this.flowParseTypeInitialiser();
  4586. return this.finishNode(node, "TypeAnnotation");
  4587. }
  4588. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  4589. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  4590. if (this.match(14)) {
  4591. ident.typeAnnotation = this.flowParseTypeAnnotation();
  4592. this.resetEndLocation(ident);
  4593. }
  4594. return ident;
  4595. }
  4596. typeCastToParameter(node) {
  4597. node.expression.typeAnnotation = node.typeAnnotation;
  4598. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  4599. return node.expression;
  4600. }
  4601. flowParseVariance() {
  4602. let variance = null;
  4603. if (this.match(49)) {
  4604. variance = this.startNode();
  4605. if (this.state.value === "+") {
  4606. variance.kind = "plus";
  4607. } else {
  4608. variance.kind = "minus";
  4609. }
  4610. this.next();
  4611. this.finishNode(variance, "Variance");
  4612. }
  4613. return variance;
  4614. }
  4615. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  4616. if (allowExpressionBody) {
  4617. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  4618. }
  4619. return super.parseFunctionBody(node, false, isMethod);
  4620. }
  4621. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4622. if (this.match(14)) {
  4623. const typeNode = this.startNode();
  4624. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4625. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  4626. }
  4627. super.parseFunctionBodyAndFinish(node, type, isMethod);
  4628. }
  4629. parseStatement(context, topLevel) {
  4630. if (this.state.strict && this.isContextual(121)) {
  4631. const lookahead = this.lookahead();
  4632. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  4633. const node = this.startNode();
  4634. this.next();
  4635. return this.flowParseInterface(node);
  4636. }
  4637. } else if (this.shouldParseEnums() && this.isContextual(118)) {
  4638. const node = this.startNode();
  4639. this.next();
  4640. return this.flowParseEnumDeclaration(node);
  4641. }
  4642. const stmt = super.parseStatement(context, topLevel);
  4643. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  4644. this.flowPragma = null;
  4645. }
  4646. return stmt;
  4647. }
  4648. parseExpressionStatement(node, expr) {
  4649. if (expr.type === "Identifier") {
  4650. if (expr.name === "declare") {
  4651. if (this.match(76) || tokenIsIdentifier(this.state.type) || this.match(64) || this.match(70) || this.match(78)) {
  4652. return this.flowParseDeclare(node);
  4653. }
  4654. } else if (tokenIsIdentifier(this.state.type)) {
  4655. if (expr.name === "interface") {
  4656. return this.flowParseInterface(node);
  4657. } else if (expr.name === "type") {
  4658. return this.flowParseTypeAlias(node);
  4659. } else if (expr.name === "opaque") {
  4660. return this.flowParseOpaqueType(node, false);
  4661. }
  4662. }
  4663. }
  4664. return super.parseExpressionStatement(node, expr);
  4665. }
  4666. shouldParseExportDeclaration() {
  4667. const {
  4668. type
  4669. } = this.state;
  4670. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 118) {
  4671. return !this.state.containsEsc;
  4672. }
  4673. return super.shouldParseExportDeclaration();
  4674. }
  4675. isExportDefaultSpecifier() {
  4676. const {
  4677. type
  4678. } = this.state;
  4679. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 118) {
  4680. return this.state.containsEsc;
  4681. }
  4682. return super.isExportDefaultSpecifier();
  4683. }
  4684. parseExportDefaultExpression() {
  4685. if (this.shouldParseEnums() && this.isContextual(118)) {
  4686. const node = this.startNode();
  4687. this.next();
  4688. return this.flowParseEnumDeclaration(node);
  4689. }
  4690. return super.parseExportDefaultExpression();
  4691. }
  4692. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  4693. if (!this.match(17)) return expr;
  4694. if (this.state.maybeInArrowParameters) {
  4695. const nextCh = this.lookaheadCharCode();
  4696. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  4697. this.setOptionalParametersError(refExpressionErrors);
  4698. return expr;
  4699. }
  4700. }
  4701. this.expect(17);
  4702. const state = this.state.clone();
  4703. const originalNoArrowAt = this.state.noArrowAt;
  4704. const node = this.startNodeAt(startPos, startLoc);
  4705. let {
  4706. consequent,
  4707. failed
  4708. } = this.tryParseConditionalConsequent();
  4709. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  4710. if (failed || invalid.length > 0) {
  4711. const noArrowAt = [...originalNoArrowAt];
  4712. if (invalid.length > 0) {
  4713. this.state = state;
  4714. this.state.noArrowAt = noArrowAt;
  4715. for (let i = 0; i < invalid.length; i++) {
  4716. noArrowAt.push(invalid[i].start);
  4717. }
  4718. ({
  4719. consequent,
  4720. failed
  4721. } = this.tryParseConditionalConsequent());
  4722. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  4723. }
  4724. if (failed && valid.length > 1) {
  4725. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  4726. at: state.startLoc
  4727. });
  4728. }
  4729. if (failed && valid.length === 1) {
  4730. this.state = state;
  4731. noArrowAt.push(valid[0].start);
  4732. this.state.noArrowAt = noArrowAt;
  4733. ({
  4734. consequent,
  4735. failed
  4736. } = this.tryParseConditionalConsequent());
  4737. }
  4738. }
  4739. this.getArrowLikeExpressions(consequent, true);
  4740. this.state.noArrowAt = originalNoArrowAt;
  4741. this.expect(14);
  4742. node.test = expr;
  4743. node.consequent = consequent;
  4744. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  4745. return this.finishNode(node, "ConditionalExpression");
  4746. }
  4747. tryParseConditionalConsequent() {
  4748. this.state.noArrowParamsConversionAt.push(this.state.start);
  4749. const consequent = this.parseMaybeAssignAllowIn();
  4750. const failed = !this.match(14);
  4751. this.state.noArrowParamsConversionAt.pop();
  4752. return {
  4753. consequent,
  4754. failed
  4755. };
  4756. }
  4757. getArrowLikeExpressions(node, disallowInvalid) {
  4758. const stack = [node];
  4759. const arrows = [];
  4760. while (stack.length !== 0) {
  4761. const node = stack.pop();
  4762. if (node.type === "ArrowFunctionExpression") {
  4763. if (node.typeParameters || !node.returnType) {
  4764. this.finishArrowValidation(node);
  4765. } else {
  4766. arrows.push(node);
  4767. }
  4768. stack.push(node.body);
  4769. } else if (node.type === "ConditionalExpression") {
  4770. stack.push(node.consequent);
  4771. stack.push(node.alternate);
  4772. }
  4773. }
  4774. if (disallowInvalid) {
  4775. arrows.forEach(node => this.finishArrowValidation(node));
  4776. return [arrows, []];
  4777. }
  4778. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  4779. }
  4780. finishArrowValidation(node) {
  4781. var _node$extra;
  4782. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  4783. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  4784. super.checkParams(node, false, true);
  4785. this.scope.exit();
  4786. }
  4787. forwardNoArrowParamsConversionAt(node, parse) {
  4788. let result;
  4789. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  4790. this.state.noArrowParamsConversionAt.push(this.state.start);
  4791. result = parse();
  4792. this.state.noArrowParamsConversionAt.pop();
  4793. } else {
  4794. result = parse();
  4795. }
  4796. return result;
  4797. }
  4798. parseParenItem(node, startPos, startLoc) {
  4799. node = super.parseParenItem(node, startPos, startLoc);
  4800. if (this.eat(17)) {
  4801. node.optional = true;
  4802. this.resetEndLocation(node);
  4803. }
  4804. if (this.match(14)) {
  4805. const typeCastNode = this.startNodeAt(startPos, startLoc);
  4806. typeCastNode.expression = node;
  4807. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  4808. return this.finishNode(typeCastNode, "TypeCastExpression");
  4809. }
  4810. return node;
  4811. }
  4812. assertModuleNodeAllowed(node) {
  4813. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  4814. return;
  4815. }
  4816. super.assertModuleNodeAllowed(node);
  4817. }
  4818. parseExport(node) {
  4819. const decl = super.parseExport(node);
  4820. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  4821. decl.exportKind = decl.exportKind || "value";
  4822. }
  4823. return decl;
  4824. }
  4825. parseExportDeclaration(node) {
  4826. if (this.isContextual(122)) {
  4827. node.exportKind = "type";
  4828. const declarationNode = this.startNode();
  4829. this.next();
  4830. if (this.match(5)) {
  4831. node.specifiers = this.parseExportSpecifiers(true);
  4832. this.parseExportFrom(node);
  4833. return null;
  4834. } else {
  4835. return this.flowParseTypeAlias(declarationNode);
  4836. }
  4837. } else if (this.isContextual(123)) {
  4838. node.exportKind = "type";
  4839. const declarationNode = this.startNode();
  4840. this.next();
  4841. return this.flowParseOpaqueType(declarationNode, false);
  4842. } else if (this.isContextual(121)) {
  4843. node.exportKind = "type";
  4844. const declarationNode = this.startNode();
  4845. this.next();
  4846. return this.flowParseInterface(declarationNode);
  4847. } else if (this.shouldParseEnums() && this.isContextual(118)) {
  4848. node.exportKind = "value";
  4849. const declarationNode = this.startNode();
  4850. this.next();
  4851. return this.flowParseEnumDeclaration(declarationNode);
  4852. } else {
  4853. return super.parseExportDeclaration(node);
  4854. }
  4855. }
  4856. eatExportStar(node) {
  4857. if (super.eatExportStar(...arguments)) return true;
  4858. if (this.isContextual(122) && this.lookahead().type === 51) {
  4859. node.exportKind = "type";
  4860. this.next();
  4861. this.next();
  4862. return true;
  4863. }
  4864. return false;
  4865. }
  4866. maybeParseExportNamespaceSpecifier(node) {
  4867. const {
  4868. startLoc
  4869. } = this.state;
  4870. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  4871. if (hasNamespace && node.exportKind === "type") {
  4872. this.unexpected(startLoc);
  4873. }
  4874. return hasNamespace;
  4875. }
  4876. parseClassId(node, isStatement, optionalId) {
  4877. super.parseClassId(node, isStatement, optionalId);
  4878. if (this.match(45)) {
  4879. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4880. }
  4881. }
  4882. parseClassMember(classBody, member, state) {
  4883. const {
  4884. startLoc
  4885. } = this.state;
  4886. if (this.isContextual(117)) {
  4887. if (this.parseClassMemberFromModifier(classBody, member)) {
  4888. return;
  4889. }
  4890. member.declare = true;
  4891. }
  4892. super.parseClassMember(classBody, member, state);
  4893. if (member.declare) {
  4894. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  4895. this.raise(FlowErrors.DeclareClassElement, {
  4896. at: startLoc
  4897. });
  4898. } else if (member.value) {
  4899. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  4900. node: member.value
  4901. });
  4902. }
  4903. }
  4904. }
  4905. isIterator(word) {
  4906. return word === "iterator" || word === "asyncIterator";
  4907. }
  4908. readIterator() {
  4909. const word = super.readWord1();
  4910. const fullWord = "@@" + word;
  4911. if (!this.isIterator(word) || !this.state.inType) {
  4912. this.raise(ErrorMessages.InvalidIdentifier, {
  4913. at: this.state.curPosition()
  4914. }, fullWord);
  4915. }
  4916. this.finishToken(124, fullWord);
  4917. }
  4918. getTokenFromCode(code) {
  4919. const next = this.input.charCodeAt(this.state.pos + 1);
  4920. if (code === 123 && next === 124) {
  4921. return this.finishOp(6, 2);
  4922. } else if (this.state.inType && (code === 62 || code === 60)) {
  4923. return this.finishOp(code === 62 ? 46 : 45, 1);
  4924. } else if (this.state.inType && code === 63) {
  4925. if (next === 46) {
  4926. return this.finishOp(18, 2);
  4927. }
  4928. return this.finishOp(17, 1);
  4929. } else if (isIteratorStart(code, next)) {
  4930. this.state.pos += 2;
  4931. return this.readIterator();
  4932. } else {
  4933. return super.getTokenFromCode(code);
  4934. }
  4935. }
  4936. isAssignable(node, isBinding) {
  4937. if (node.type === "TypeCastExpression") {
  4938. return this.isAssignable(node.expression, isBinding);
  4939. } else {
  4940. return super.isAssignable(node, isBinding);
  4941. }
  4942. }
  4943. toAssignable(node, isLHS = false) {
  4944. if (node.type === "TypeCastExpression") {
  4945. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  4946. } else {
  4947. return super.toAssignable(node, isLHS);
  4948. }
  4949. }
  4950. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  4951. for (let i = 0; i < exprList.length; i++) {
  4952. const expr = exprList[i];
  4953. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  4954. exprList[i] = this.typeCastToParameter(expr);
  4955. }
  4956. }
  4957. return super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  4958. }
  4959. toReferencedList(exprList, isParenthesizedExpr) {
  4960. for (let i = 0; i < exprList.length; i++) {
  4961. var _expr$extra;
  4962. const expr = exprList[i];
  4963. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  4964. this.raise(FlowErrors.TypeCastInPattern, {
  4965. node: expr.typeAnnotation
  4966. });
  4967. }
  4968. }
  4969. return exprList;
  4970. }
  4971. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  4972. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  4973. if (canBePattern && !this.state.maybeInArrowParameters) {
  4974. this.toReferencedList(node.elements);
  4975. }
  4976. return node;
  4977. }
  4978. checkLVal(expr, ...args) {
  4979. if (expr.type !== "TypeCastExpression") {
  4980. return super.checkLVal(expr, ...args);
  4981. }
  4982. }
  4983. parseClassProperty(node) {
  4984. if (this.match(14)) {
  4985. node.typeAnnotation = this.flowParseTypeAnnotation();
  4986. }
  4987. return super.parseClassProperty(node);
  4988. }
  4989. parseClassPrivateProperty(node) {
  4990. if (this.match(14)) {
  4991. node.typeAnnotation = this.flowParseTypeAnnotation();
  4992. }
  4993. return super.parseClassPrivateProperty(node);
  4994. }
  4995. isClassMethod() {
  4996. return this.match(45) || super.isClassMethod();
  4997. }
  4998. isClassProperty() {
  4999. return this.match(14) || super.isClassProperty();
  5000. }
  5001. isNonstaticConstructor(method) {
  5002. return !this.match(14) && super.isNonstaticConstructor(method);
  5003. }
  5004. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5005. if (method.variance) {
  5006. this.unexpected(method.variance.loc.start);
  5007. }
  5008. delete method.variance;
  5009. if (this.match(45)) {
  5010. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5011. }
  5012. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5013. if (method.params && isConstructor) {
  5014. const params = method.params;
  5015. if (params.length > 0 && this.isThisParam(params[0])) {
  5016. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5017. node: method
  5018. });
  5019. }
  5020. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5021. const params = method.value.params;
  5022. if (params.length > 0 && this.isThisParam(params[0])) {
  5023. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5024. node: method
  5025. });
  5026. }
  5027. }
  5028. }
  5029. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5030. if (method.variance) {
  5031. this.unexpected(method.variance.loc.start);
  5032. }
  5033. delete method.variance;
  5034. if (this.match(45)) {
  5035. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5036. }
  5037. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5038. }
  5039. parseClassSuper(node) {
  5040. super.parseClassSuper(node);
  5041. if (node.superClass && this.match(45)) {
  5042. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5043. }
  5044. if (this.isContextual(106)) {
  5045. this.next();
  5046. const implemented = node.implements = [];
  5047. do {
  5048. const node = this.startNode();
  5049. node.id = this.flowParseRestrictedIdentifier(true);
  5050. if (this.match(45)) {
  5051. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5052. } else {
  5053. node.typeParameters = null;
  5054. }
  5055. implemented.push(this.finishNode(node, "ClassImplements"));
  5056. } while (this.eat(12));
  5057. }
  5058. }
  5059. checkGetterSetterParams(method) {
  5060. super.checkGetterSetterParams(method);
  5061. const params = this.getObjectOrClassMethodParams(method);
  5062. if (params.length > 0) {
  5063. const param = params[0];
  5064. if (this.isThisParam(param) && method.kind === "get") {
  5065. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5066. node: param
  5067. });
  5068. } else if (this.isThisParam(param)) {
  5069. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5070. node: param
  5071. });
  5072. }
  5073. }
  5074. }
  5075. parsePropertyNamePrefixOperator(node) {
  5076. node.variance = this.flowParseVariance();
  5077. }
  5078. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5079. if (prop.variance) {
  5080. this.unexpected(prop.variance.loc.start);
  5081. }
  5082. delete prop.variance;
  5083. let typeParameters;
  5084. if (this.match(45) && !isAccessor) {
  5085. typeParameters = this.flowParseTypeParameterDeclaration();
  5086. if (!this.match(10)) this.unexpected();
  5087. }
  5088. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5089. if (typeParameters) {
  5090. (prop.value || prop).typeParameters = typeParameters;
  5091. }
  5092. }
  5093. parseAssignableListItemTypes(param) {
  5094. if (this.eat(17)) {
  5095. if (param.type !== "Identifier") {
  5096. this.raise(FlowErrors.PatternIsOptional, {
  5097. node: param
  5098. });
  5099. }
  5100. if (this.isThisParam(param)) {
  5101. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5102. node: param
  5103. });
  5104. }
  5105. param.optional = true;
  5106. }
  5107. if (this.match(14)) {
  5108. param.typeAnnotation = this.flowParseTypeAnnotation();
  5109. } else if (this.isThisParam(param)) {
  5110. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5111. node: param
  5112. });
  5113. }
  5114. if (this.match(29) && this.isThisParam(param)) {
  5115. this.raise(FlowErrors.ThisParamNoDefault, {
  5116. node: param
  5117. });
  5118. }
  5119. this.resetEndLocation(param);
  5120. return param;
  5121. }
  5122. parseMaybeDefault(startPos, startLoc, left) {
  5123. const node = super.parseMaybeDefault(startPos, startLoc, left);
  5124. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5125. this.raise(FlowErrors.TypeBeforeInitializer, {
  5126. node: node.typeAnnotation
  5127. });
  5128. }
  5129. return node;
  5130. }
  5131. shouldParseDefaultImport(node) {
  5132. if (!hasTypeImportKind(node)) {
  5133. return super.shouldParseDefaultImport(node);
  5134. }
  5135. return isMaybeDefaultImport(this.state.type);
  5136. }
  5137. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  5138. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5139. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  5140. node.specifiers.push(this.finishNode(specifier, type));
  5141. }
  5142. maybeParseDefaultImportSpecifier(node) {
  5143. node.importKind = "value";
  5144. let kind = null;
  5145. if (this.match(83)) {
  5146. kind = "typeof";
  5147. } else if (this.isContextual(122)) {
  5148. kind = "type";
  5149. }
  5150. if (kind) {
  5151. const lh = this.lookahead();
  5152. const {
  5153. type
  5154. } = lh;
  5155. if (kind === "type" && type === 51) {
  5156. this.unexpected(null, lh.type);
  5157. }
  5158. if (isMaybeDefaultImport(type) || type === 5 || type === 51) {
  5159. this.next();
  5160. node.importKind = kind;
  5161. }
  5162. }
  5163. return super.maybeParseDefaultImportSpecifier(node);
  5164. }
  5165. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  5166. const firstIdent = specifier.imported;
  5167. let specifierTypeKind = null;
  5168. if (firstIdent.type === "Identifier") {
  5169. if (firstIdent.name === "type") {
  5170. specifierTypeKind = "type";
  5171. } else if (firstIdent.name === "typeof") {
  5172. specifierTypeKind = "typeof";
  5173. }
  5174. }
  5175. let isBinding = false;
  5176. if (this.isContextual(89) && !this.isLookaheadContextual("as")) {
  5177. const as_ident = this.parseIdentifier(true);
  5178. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5179. specifier.imported = as_ident;
  5180. specifier.importKind = specifierTypeKind;
  5181. specifier.local = cloneIdentifier(as_ident);
  5182. } else {
  5183. specifier.imported = firstIdent;
  5184. specifier.importKind = null;
  5185. specifier.local = this.parseIdentifier();
  5186. }
  5187. } else {
  5188. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5189. specifier.imported = this.parseIdentifier(true);
  5190. specifier.importKind = specifierTypeKind;
  5191. } else {
  5192. if (importedIsString) {
  5193. throw this.raise(ErrorMessages.ImportBindingIsString, {
  5194. node: specifier
  5195. }, firstIdent.value);
  5196. }
  5197. specifier.imported = firstIdent;
  5198. specifier.importKind = null;
  5199. }
  5200. if (this.eatContextual(89)) {
  5201. specifier.local = this.parseIdentifier();
  5202. } else {
  5203. isBinding = true;
  5204. specifier.local = cloneIdentifier(specifier.imported);
  5205. }
  5206. }
  5207. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5208. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5209. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5210. node: specifier
  5211. });
  5212. }
  5213. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5214. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5215. }
  5216. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5217. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5218. }
  5219. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  5220. return this.finishNode(specifier, "ImportSpecifier");
  5221. }
  5222. parseBindingAtom() {
  5223. switch (this.state.type) {
  5224. case 74:
  5225. return this.parseIdentifier(true);
  5226. default:
  5227. return super.parseBindingAtom();
  5228. }
  5229. }
  5230. parseFunctionParams(node, allowModifiers) {
  5231. const kind = node.kind;
  5232. if (kind !== "get" && kind !== "set" && this.match(45)) {
  5233. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5234. }
  5235. super.parseFunctionParams(node, allowModifiers);
  5236. }
  5237. parseVarId(decl, kind) {
  5238. super.parseVarId(decl, kind);
  5239. if (this.match(14)) {
  5240. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5241. this.resetEndLocation(decl.id);
  5242. }
  5243. }
  5244. parseAsyncArrowFromCallExpression(node, call) {
  5245. if (this.match(14)) {
  5246. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5247. this.state.noAnonFunctionType = true;
  5248. node.returnType = this.flowParseTypeAnnotation();
  5249. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5250. }
  5251. return super.parseAsyncArrowFromCallExpression(node, call);
  5252. }
  5253. shouldParseAsyncArrow() {
  5254. return this.match(14) || super.shouldParseAsyncArrow();
  5255. }
  5256. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5257. var _jsx;
  5258. let state = null;
  5259. let jsx;
  5260. if (this.hasPlugin("jsx") && (this.match(134) || this.match(45))) {
  5261. state = this.state.clone();
  5262. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5263. if (!jsx.error) return jsx.node;
  5264. const {
  5265. context
  5266. } = this.state;
  5267. const currentContext = context[context.length - 1];
  5268. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5269. context.pop();
  5270. }
  5271. }
  5272. if ((_jsx = jsx) != null && _jsx.error || this.match(45)) {
  5273. var _jsx2, _jsx3;
  5274. state = state || this.state.clone();
  5275. let typeParameters;
  5276. const arrow = this.tryParse(abort => {
  5277. var _arrowExpression$extr;
  5278. typeParameters = this.flowParseTypeParameterDeclaration();
  5279. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5280. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5281. this.resetStartLocationFromNode(result, typeParameters);
  5282. return result;
  5283. });
  5284. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5285. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5286. if (expr.type !== "ArrowFunctionExpression") abort();
  5287. expr.typeParameters = typeParameters;
  5288. this.resetStartLocationFromNode(expr, typeParameters);
  5289. return arrowExpression;
  5290. }, state);
  5291. let arrowExpression = null;
  5292. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5293. if (!arrow.error && !arrow.aborted) {
  5294. if (arrow.node.async) {
  5295. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5296. node: typeParameters
  5297. });
  5298. }
  5299. return arrow.node;
  5300. }
  5301. arrowExpression = arrow.node;
  5302. }
  5303. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5304. this.state = jsx.failState;
  5305. return jsx.node;
  5306. }
  5307. if (arrowExpression) {
  5308. this.state = arrow.failState;
  5309. return arrowExpression;
  5310. }
  5311. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5312. if (arrow.thrown) throw arrow.error;
  5313. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5314. node: typeParameters
  5315. });
  5316. }
  5317. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5318. }
  5319. parseArrow(node) {
  5320. if (this.match(14)) {
  5321. const result = this.tryParse(() => {
  5322. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5323. this.state.noAnonFunctionType = true;
  5324. const typeNode = this.startNode();
  5325. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5326. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5327. if (this.canInsertSemicolon()) this.unexpected();
  5328. if (!this.match(19)) this.unexpected();
  5329. return typeNode;
  5330. });
  5331. if (result.thrown) return null;
  5332. if (result.error) this.state = result.failState;
  5333. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5334. }
  5335. return super.parseArrow(node);
  5336. }
  5337. shouldParseArrow(params) {
  5338. return this.match(14) || super.shouldParseArrow(params);
  5339. }
  5340. setArrowFunctionParameters(node, params) {
  5341. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5342. node.params = params;
  5343. } else {
  5344. super.setArrowFunctionParameters(node, params);
  5345. }
  5346. }
  5347. checkParams(node, allowDuplicates, isArrowFunction) {
  5348. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5349. return;
  5350. }
  5351. for (let i = 0; i < node.params.length; i++) {
  5352. if (this.isThisParam(node.params[i]) && i > 0) {
  5353. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5354. node: node.params[i]
  5355. });
  5356. }
  5357. }
  5358. return super.checkParams(...arguments);
  5359. }
  5360. parseParenAndDistinguishExpression(canBeArrow) {
  5361. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5362. }
  5363. parseSubscripts(base, startPos, startLoc, noCalls) {
  5364. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  5365. this.next();
  5366. const node = this.startNodeAt(startPos, startLoc);
  5367. node.callee = base;
  5368. node.arguments = this.parseCallExpressionArguments(11, false);
  5369. base = this.finishNode(node, "CallExpression");
  5370. } else if (base.type === "Identifier" && base.name === "async" && this.match(45)) {
  5371. const state = this.state.clone();
  5372. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  5373. if (!arrow.error && !arrow.aborted) return arrow.node;
  5374. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  5375. if (result.node && !result.error) return result.node;
  5376. if (arrow.node) {
  5377. this.state = arrow.failState;
  5378. return arrow.node;
  5379. }
  5380. if (result.node) {
  5381. this.state = result.failState;
  5382. return result.node;
  5383. }
  5384. throw arrow.error || result.error;
  5385. }
  5386. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  5387. }
  5388. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  5389. if (this.match(18) && this.isLookaheadToken_lt()) {
  5390. subscriptState.optionalChainMember = true;
  5391. if (noCalls) {
  5392. subscriptState.stop = true;
  5393. return base;
  5394. }
  5395. this.next();
  5396. const node = this.startNodeAt(startPos, startLoc);
  5397. node.callee = base;
  5398. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5399. this.expect(10);
  5400. node.arguments = this.parseCallExpressionArguments(11, false);
  5401. node.optional = true;
  5402. return this.finishCallExpression(node, true);
  5403. } else if (!noCalls && this.shouldParseTypes() && this.match(45)) {
  5404. const node = this.startNodeAt(startPos, startLoc);
  5405. node.callee = base;
  5406. const result = this.tryParse(() => {
  5407. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5408. this.expect(10);
  5409. node.arguments = this.parseCallExpressionArguments(11, false);
  5410. if (subscriptState.optionalChainMember) node.optional = false;
  5411. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5412. });
  5413. if (result.node) {
  5414. if (result.error) this.state = result.failState;
  5415. return result.node;
  5416. }
  5417. }
  5418. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  5419. }
  5420. parseNewArguments(node) {
  5421. let targs = null;
  5422. if (this.shouldParseTypes() && this.match(45)) {
  5423. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5424. }
  5425. node.typeArguments = targs;
  5426. super.parseNewArguments(node);
  5427. }
  5428. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  5429. const node = this.startNodeAt(startPos, startLoc);
  5430. this.parseFunctionParams(node);
  5431. if (!this.parseArrow(node)) return;
  5432. return this.parseArrowExpression(node, undefined, true);
  5433. }
  5434. readToken_mult_modulo(code) {
  5435. const next = this.input.charCodeAt(this.state.pos + 1);
  5436. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5437. this.state.hasFlowComment = false;
  5438. this.state.pos += 2;
  5439. this.nextToken();
  5440. return;
  5441. }
  5442. super.readToken_mult_modulo(code);
  5443. }
  5444. readToken_pipe_amp(code) {
  5445. const next = this.input.charCodeAt(this.state.pos + 1);
  5446. if (code === 124 && next === 125) {
  5447. this.finishOp(9, 2);
  5448. return;
  5449. }
  5450. super.readToken_pipe_amp(code);
  5451. }
  5452. parseTopLevel(file, program) {
  5453. const fileNode = super.parseTopLevel(file, program);
  5454. if (this.state.hasFlowComment) {
  5455. this.raise(FlowErrors.UnterminatedFlowComment, {
  5456. at: this.state.curPosition()
  5457. });
  5458. }
  5459. return fileNode;
  5460. }
  5461. skipBlockComment() {
  5462. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5463. if (this.state.hasFlowComment) {
  5464. throw this.raise(FlowErrors.NestedFlowComment, {
  5465. at: this.state.startLoc
  5466. });
  5467. }
  5468. this.hasFlowCommentCompletion();
  5469. this.state.pos += this.skipFlowComment();
  5470. this.state.hasFlowComment = true;
  5471. return;
  5472. }
  5473. if (this.state.hasFlowComment) {
  5474. const end = this.input.indexOf("*-/", this.state.pos + 2);
  5475. if (end === -1) {
  5476. throw this.raise(ErrorMessages.UnterminatedComment, {
  5477. at: this.state.curPosition()
  5478. });
  5479. }
  5480. this.state.pos = end + 2 + 3;
  5481. return;
  5482. }
  5483. return super.skipBlockComment();
  5484. }
  5485. skipFlowComment() {
  5486. const {
  5487. pos
  5488. } = this.state;
  5489. let shiftToFirstNonWhiteSpace = 2;
  5490. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5491. shiftToFirstNonWhiteSpace++;
  5492. }
  5493. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5494. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5495. if (ch2 === 58 && ch3 === 58) {
  5496. return shiftToFirstNonWhiteSpace + 2;
  5497. }
  5498. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5499. return shiftToFirstNonWhiteSpace + 12;
  5500. }
  5501. if (ch2 === 58 && ch3 !== 58) {
  5502. return shiftToFirstNonWhiteSpace;
  5503. }
  5504. return false;
  5505. }
  5506. hasFlowCommentCompletion() {
  5507. const end = this.input.indexOf("*/", this.state.pos);
  5508. if (end === -1) {
  5509. throw this.raise(ErrorMessages.UnterminatedComment, {
  5510. at: this.state.curPosition()
  5511. });
  5512. }
  5513. }
  5514. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5515. enumName,
  5516. memberName
  5517. }) {
  5518. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5519. at: loc
  5520. }, memberName, enumName);
  5521. }
  5522. flowEnumErrorInvalidExplicitType(loc, {
  5523. enumName,
  5524. suppliedType
  5525. }) {
  5526. return this.raise(suppliedType === null ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied : FlowErrors.EnumInvalidExplicitType, {
  5527. at: loc
  5528. }, enumName, suppliedType);
  5529. }
  5530. flowEnumErrorInvalidMemberInitializer(loc, {
  5531. enumName,
  5532. explicitType,
  5533. memberName
  5534. }) {
  5535. return this.raise(explicitType === "boolean" || explicitType === "number" || explicitType === "string" ? FlowErrors.EnumInvalidMemberInitializerPrimaryType : explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerUnknownType, {
  5536. at: loc
  5537. }, enumName, memberName, explicitType);
  5538. }
  5539. flowEnumErrorNumberMemberNotInitialized(loc, {
  5540. enumName,
  5541. memberName
  5542. }) {
  5543. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  5544. at: loc
  5545. }, enumName, memberName);
  5546. }
  5547. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  5548. enumName
  5549. }) {
  5550. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  5551. node
  5552. }, enumName);
  5553. }
  5554. flowEnumMemberInit() {
  5555. const startLoc = this.state.startLoc;
  5556. const endOfInit = () => this.match(12) || this.match(8);
  5557. switch (this.state.type) {
  5558. case 126:
  5559. {
  5560. const literal = this.parseNumericLiteral(this.state.value);
  5561. if (endOfInit()) {
  5562. return {
  5563. type: "number",
  5564. loc: literal.loc.start,
  5565. value: literal
  5566. };
  5567. }
  5568. return {
  5569. type: "invalid",
  5570. loc: startLoc
  5571. };
  5572. }
  5573. case 125:
  5574. {
  5575. const literal = this.parseStringLiteral(this.state.value);
  5576. if (endOfInit()) {
  5577. return {
  5578. type: "string",
  5579. loc: literal.loc.start,
  5580. value: literal
  5581. };
  5582. }
  5583. return {
  5584. type: "invalid",
  5585. loc: startLoc
  5586. };
  5587. }
  5588. case 81:
  5589. case 82:
  5590. {
  5591. const literal = this.parseBooleanLiteral(this.match(81));
  5592. if (endOfInit()) {
  5593. return {
  5594. type: "boolean",
  5595. loc: literal.loc.start,
  5596. value: literal
  5597. };
  5598. }
  5599. return {
  5600. type: "invalid",
  5601. loc: startLoc
  5602. };
  5603. }
  5604. default:
  5605. return {
  5606. type: "invalid",
  5607. loc: startLoc
  5608. };
  5609. }
  5610. }
  5611. flowEnumMemberRaw() {
  5612. const loc = this.state.startLoc;
  5613. const id = this.parseIdentifier(true);
  5614. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  5615. type: "none",
  5616. loc
  5617. };
  5618. return {
  5619. id,
  5620. init
  5621. };
  5622. }
  5623. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  5624. const {
  5625. explicitType
  5626. } = context;
  5627. if (explicitType === null) {
  5628. return;
  5629. }
  5630. if (explicitType !== expectedType) {
  5631. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  5632. }
  5633. }
  5634. flowEnumMembers({
  5635. enumName,
  5636. explicitType
  5637. }) {
  5638. const seenNames = new Set();
  5639. const members = {
  5640. booleanMembers: [],
  5641. numberMembers: [],
  5642. stringMembers: [],
  5643. defaultedMembers: []
  5644. };
  5645. let hasUnknownMembers = false;
  5646. while (!this.match(8)) {
  5647. if (this.eat(21)) {
  5648. hasUnknownMembers = true;
  5649. break;
  5650. }
  5651. const memberNode = this.startNode();
  5652. const {
  5653. id,
  5654. init
  5655. } = this.flowEnumMemberRaw();
  5656. const memberName = id.name;
  5657. if (memberName === "") {
  5658. continue;
  5659. }
  5660. if (/^[a-z]/.test(memberName)) {
  5661. this.raise(FlowErrors.EnumInvalidMemberName, {
  5662. node: id
  5663. }, memberName, memberName[0].toUpperCase() + memberName.slice(1), enumName);
  5664. }
  5665. if (seenNames.has(memberName)) {
  5666. this.raise(FlowErrors.EnumDuplicateMemberName, {
  5667. node: id
  5668. }, memberName, enumName);
  5669. }
  5670. seenNames.add(memberName);
  5671. const context = {
  5672. enumName,
  5673. explicitType,
  5674. memberName
  5675. };
  5676. memberNode.id = id;
  5677. switch (init.type) {
  5678. case "boolean":
  5679. {
  5680. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  5681. memberNode.init = init.value;
  5682. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  5683. break;
  5684. }
  5685. case "number":
  5686. {
  5687. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  5688. memberNode.init = init.value;
  5689. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  5690. break;
  5691. }
  5692. case "string":
  5693. {
  5694. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  5695. memberNode.init = init.value;
  5696. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  5697. break;
  5698. }
  5699. case "invalid":
  5700. {
  5701. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  5702. }
  5703. case "none":
  5704. {
  5705. switch (explicitType) {
  5706. case "boolean":
  5707. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  5708. break;
  5709. case "number":
  5710. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  5711. break;
  5712. default:
  5713. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  5714. }
  5715. }
  5716. }
  5717. if (!this.match(8)) {
  5718. this.expect(12);
  5719. }
  5720. }
  5721. return {
  5722. members,
  5723. hasUnknownMembers
  5724. };
  5725. }
  5726. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  5727. enumName
  5728. }) {
  5729. if (initializedMembers.length === 0) {
  5730. return defaultedMembers;
  5731. } else if (defaultedMembers.length === 0) {
  5732. return initializedMembers;
  5733. } else if (defaultedMembers.length > initializedMembers.length) {
  5734. for (const member of initializedMembers) {
  5735. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  5736. enumName
  5737. });
  5738. }
  5739. return defaultedMembers;
  5740. } else {
  5741. for (const member of defaultedMembers) {
  5742. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  5743. enumName
  5744. });
  5745. }
  5746. return initializedMembers;
  5747. }
  5748. }
  5749. flowEnumParseExplicitType({
  5750. enumName
  5751. }) {
  5752. if (this.eatContextual(97)) {
  5753. if (!tokenIsIdentifier(this.state.type)) {
  5754. throw this.flowEnumErrorInvalidExplicitType(this.state.startLoc, {
  5755. enumName,
  5756. suppliedType: null
  5757. });
  5758. }
  5759. const {
  5760. value
  5761. } = this.state;
  5762. this.next();
  5763. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  5764. this.flowEnumErrorInvalidExplicitType(this.state.startLoc, {
  5765. enumName,
  5766. suppliedType: value
  5767. });
  5768. }
  5769. return value;
  5770. }
  5771. return null;
  5772. }
  5773. flowEnumBody(node, id) {
  5774. const enumName = id.name;
  5775. const nameLoc = id.loc.start;
  5776. const explicitType = this.flowEnumParseExplicitType({
  5777. enumName
  5778. });
  5779. this.expect(5);
  5780. const {
  5781. members,
  5782. hasUnknownMembers
  5783. } = this.flowEnumMembers({
  5784. enumName,
  5785. explicitType
  5786. });
  5787. node.hasUnknownMembers = hasUnknownMembers;
  5788. switch (explicitType) {
  5789. case "boolean":
  5790. node.explicitType = true;
  5791. node.members = members.booleanMembers;
  5792. this.expect(8);
  5793. return this.finishNode(node, "EnumBooleanBody");
  5794. case "number":
  5795. node.explicitType = true;
  5796. node.members = members.numberMembers;
  5797. this.expect(8);
  5798. return this.finishNode(node, "EnumNumberBody");
  5799. case "string":
  5800. node.explicitType = true;
  5801. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  5802. enumName
  5803. });
  5804. this.expect(8);
  5805. return this.finishNode(node, "EnumStringBody");
  5806. case "symbol":
  5807. node.members = members.defaultedMembers;
  5808. this.expect(8);
  5809. return this.finishNode(node, "EnumSymbolBody");
  5810. default:
  5811. {
  5812. const empty = () => {
  5813. node.members = [];
  5814. this.expect(8);
  5815. return this.finishNode(node, "EnumStringBody");
  5816. };
  5817. node.explicitType = false;
  5818. const boolsLen = members.booleanMembers.length;
  5819. const numsLen = members.numberMembers.length;
  5820. const strsLen = members.stringMembers.length;
  5821. const defaultedLen = members.defaultedMembers.length;
  5822. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  5823. return empty();
  5824. } else if (!boolsLen && !numsLen) {
  5825. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  5826. enumName
  5827. });
  5828. this.expect(8);
  5829. return this.finishNode(node, "EnumStringBody");
  5830. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  5831. for (const member of members.defaultedMembers) {
  5832. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  5833. enumName,
  5834. memberName: member.id.name
  5835. });
  5836. }
  5837. node.members = members.booleanMembers;
  5838. this.expect(8);
  5839. return this.finishNode(node, "EnumBooleanBody");
  5840. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  5841. for (const member of members.defaultedMembers) {
  5842. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  5843. enumName,
  5844. memberName: member.id.name
  5845. });
  5846. }
  5847. node.members = members.numberMembers;
  5848. this.expect(8);
  5849. return this.finishNode(node, "EnumNumberBody");
  5850. } else {
  5851. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  5852. at: nameLoc
  5853. }, enumName);
  5854. return empty();
  5855. }
  5856. }
  5857. }
  5858. }
  5859. flowParseEnumDeclaration(node) {
  5860. const id = this.parseIdentifier();
  5861. node.id = id;
  5862. node.body = this.flowEnumBody(this.startNode(), id);
  5863. return this.finishNode(node, "EnumDeclaration");
  5864. }
  5865. isLookaheadToken_lt() {
  5866. const next = this.nextTokenStart();
  5867. if (this.input.charCodeAt(next) === 60) {
  5868. const afterNext = this.input.charCodeAt(next + 1);
  5869. return afterNext !== 60 && afterNext !== 61;
  5870. }
  5871. return false;
  5872. }
  5873. maybeUnwrapTypeCastExpression(node) {
  5874. return node.type === "TypeCastExpression" ? node.expression : node;
  5875. }
  5876. });
  5877. const entities = {
  5878. quot: "\u0022",
  5879. amp: "&",
  5880. apos: "\u0027",
  5881. lt: "<",
  5882. gt: ">",
  5883. nbsp: "\u00A0",
  5884. iexcl: "\u00A1",
  5885. cent: "\u00A2",
  5886. pound: "\u00A3",
  5887. curren: "\u00A4",
  5888. yen: "\u00A5",
  5889. brvbar: "\u00A6",
  5890. sect: "\u00A7",
  5891. uml: "\u00A8",
  5892. copy: "\u00A9",
  5893. ordf: "\u00AA",
  5894. laquo: "\u00AB",
  5895. not: "\u00AC",
  5896. shy: "\u00AD",
  5897. reg: "\u00AE",
  5898. macr: "\u00AF",
  5899. deg: "\u00B0",
  5900. plusmn: "\u00B1",
  5901. sup2: "\u00B2",
  5902. sup3: "\u00B3",
  5903. acute: "\u00B4",
  5904. micro: "\u00B5",
  5905. para: "\u00B6",
  5906. middot: "\u00B7",
  5907. cedil: "\u00B8",
  5908. sup1: "\u00B9",
  5909. ordm: "\u00BA",
  5910. raquo: "\u00BB",
  5911. frac14: "\u00BC",
  5912. frac12: "\u00BD",
  5913. frac34: "\u00BE",
  5914. iquest: "\u00BF",
  5915. Agrave: "\u00C0",
  5916. Aacute: "\u00C1",
  5917. Acirc: "\u00C2",
  5918. Atilde: "\u00C3",
  5919. Auml: "\u00C4",
  5920. Aring: "\u00C5",
  5921. AElig: "\u00C6",
  5922. Ccedil: "\u00C7",
  5923. Egrave: "\u00C8",
  5924. Eacute: "\u00C9",
  5925. Ecirc: "\u00CA",
  5926. Euml: "\u00CB",
  5927. Igrave: "\u00CC",
  5928. Iacute: "\u00CD",
  5929. Icirc: "\u00CE",
  5930. Iuml: "\u00CF",
  5931. ETH: "\u00D0",
  5932. Ntilde: "\u00D1",
  5933. Ograve: "\u00D2",
  5934. Oacute: "\u00D3",
  5935. Ocirc: "\u00D4",
  5936. Otilde: "\u00D5",
  5937. Ouml: "\u00D6",
  5938. times: "\u00D7",
  5939. Oslash: "\u00D8",
  5940. Ugrave: "\u00D9",
  5941. Uacute: "\u00DA",
  5942. Ucirc: "\u00DB",
  5943. Uuml: "\u00DC",
  5944. Yacute: "\u00DD",
  5945. THORN: "\u00DE",
  5946. szlig: "\u00DF",
  5947. agrave: "\u00E0",
  5948. aacute: "\u00E1",
  5949. acirc: "\u00E2",
  5950. atilde: "\u00E3",
  5951. auml: "\u00E4",
  5952. aring: "\u00E5",
  5953. aelig: "\u00E6",
  5954. ccedil: "\u00E7",
  5955. egrave: "\u00E8",
  5956. eacute: "\u00E9",
  5957. ecirc: "\u00EA",
  5958. euml: "\u00EB",
  5959. igrave: "\u00EC",
  5960. iacute: "\u00ED",
  5961. icirc: "\u00EE",
  5962. iuml: "\u00EF",
  5963. eth: "\u00F0",
  5964. ntilde: "\u00F1",
  5965. ograve: "\u00F2",
  5966. oacute: "\u00F3",
  5967. ocirc: "\u00F4",
  5968. otilde: "\u00F5",
  5969. ouml: "\u00F6",
  5970. divide: "\u00F7",
  5971. oslash: "\u00F8",
  5972. ugrave: "\u00F9",
  5973. uacute: "\u00FA",
  5974. ucirc: "\u00FB",
  5975. uuml: "\u00FC",
  5976. yacute: "\u00FD",
  5977. thorn: "\u00FE",
  5978. yuml: "\u00FF",
  5979. OElig: "\u0152",
  5980. oelig: "\u0153",
  5981. Scaron: "\u0160",
  5982. scaron: "\u0161",
  5983. Yuml: "\u0178",
  5984. fnof: "\u0192",
  5985. circ: "\u02C6",
  5986. tilde: "\u02DC",
  5987. Alpha: "\u0391",
  5988. Beta: "\u0392",
  5989. Gamma: "\u0393",
  5990. Delta: "\u0394",
  5991. Epsilon: "\u0395",
  5992. Zeta: "\u0396",
  5993. Eta: "\u0397",
  5994. Theta: "\u0398",
  5995. Iota: "\u0399",
  5996. Kappa: "\u039A",
  5997. Lambda: "\u039B",
  5998. Mu: "\u039C",
  5999. Nu: "\u039D",
  6000. Xi: "\u039E",
  6001. Omicron: "\u039F",
  6002. Pi: "\u03A0",
  6003. Rho: "\u03A1",
  6004. Sigma: "\u03A3",
  6005. Tau: "\u03A4",
  6006. Upsilon: "\u03A5",
  6007. Phi: "\u03A6",
  6008. Chi: "\u03A7",
  6009. Psi: "\u03A8",
  6010. Omega: "\u03A9",
  6011. alpha: "\u03B1",
  6012. beta: "\u03B2",
  6013. gamma: "\u03B3",
  6014. delta: "\u03B4",
  6015. epsilon: "\u03B5",
  6016. zeta: "\u03B6",
  6017. eta: "\u03B7",
  6018. theta: "\u03B8",
  6019. iota: "\u03B9",
  6020. kappa: "\u03BA",
  6021. lambda: "\u03BB",
  6022. mu: "\u03BC",
  6023. nu: "\u03BD",
  6024. xi: "\u03BE",
  6025. omicron: "\u03BF",
  6026. pi: "\u03C0",
  6027. rho: "\u03C1",
  6028. sigmaf: "\u03C2",
  6029. sigma: "\u03C3",
  6030. tau: "\u03C4",
  6031. upsilon: "\u03C5",
  6032. phi: "\u03C6",
  6033. chi: "\u03C7",
  6034. psi: "\u03C8",
  6035. omega: "\u03C9",
  6036. thetasym: "\u03D1",
  6037. upsih: "\u03D2",
  6038. piv: "\u03D6",
  6039. ensp: "\u2002",
  6040. emsp: "\u2003",
  6041. thinsp: "\u2009",
  6042. zwnj: "\u200C",
  6043. zwj: "\u200D",
  6044. lrm: "\u200E",
  6045. rlm: "\u200F",
  6046. ndash: "\u2013",
  6047. mdash: "\u2014",
  6048. lsquo: "\u2018",
  6049. rsquo: "\u2019",
  6050. sbquo: "\u201A",
  6051. ldquo: "\u201C",
  6052. rdquo: "\u201D",
  6053. bdquo: "\u201E",
  6054. dagger: "\u2020",
  6055. Dagger: "\u2021",
  6056. bull: "\u2022",
  6057. hellip: "\u2026",
  6058. permil: "\u2030",
  6059. prime: "\u2032",
  6060. Prime: "\u2033",
  6061. lsaquo: "\u2039",
  6062. rsaquo: "\u203A",
  6063. oline: "\u203E",
  6064. frasl: "\u2044",
  6065. euro: "\u20AC",
  6066. image: "\u2111",
  6067. weierp: "\u2118",
  6068. real: "\u211C",
  6069. trade: "\u2122",
  6070. alefsym: "\u2135",
  6071. larr: "\u2190",
  6072. uarr: "\u2191",
  6073. rarr: "\u2192",
  6074. darr: "\u2193",
  6075. harr: "\u2194",
  6076. crarr: "\u21B5",
  6077. lArr: "\u21D0",
  6078. uArr: "\u21D1",
  6079. rArr: "\u21D2",
  6080. dArr: "\u21D3",
  6081. hArr: "\u21D4",
  6082. forall: "\u2200",
  6083. part: "\u2202",
  6084. exist: "\u2203",
  6085. empty: "\u2205",
  6086. nabla: "\u2207",
  6087. isin: "\u2208",
  6088. notin: "\u2209",
  6089. ni: "\u220B",
  6090. prod: "\u220F",
  6091. sum: "\u2211",
  6092. minus: "\u2212",
  6093. lowast: "\u2217",
  6094. radic: "\u221A",
  6095. prop: "\u221D",
  6096. infin: "\u221E",
  6097. ang: "\u2220",
  6098. and: "\u2227",
  6099. or: "\u2228",
  6100. cap: "\u2229",
  6101. cup: "\u222A",
  6102. int: "\u222B",
  6103. there4: "\u2234",
  6104. sim: "\u223C",
  6105. cong: "\u2245",
  6106. asymp: "\u2248",
  6107. ne: "\u2260",
  6108. equiv: "\u2261",
  6109. le: "\u2264",
  6110. ge: "\u2265",
  6111. sub: "\u2282",
  6112. sup: "\u2283",
  6113. nsub: "\u2284",
  6114. sube: "\u2286",
  6115. supe: "\u2287",
  6116. oplus: "\u2295",
  6117. otimes: "\u2297",
  6118. perp: "\u22A5",
  6119. sdot: "\u22C5",
  6120. lceil: "\u2308",
  6121. rceil: "\u2309",
  6122. lfloor: "\u230A",
  6123. rfloor: "\u230B",
  6124. lang: "\u2329",
  6125. rang: "\u232A",
  6126. loz: "\u25CA",
  6127. spades: "\u2660",
  6128. clubs: "\u2663",
  6129. hearts: "\u2665",
  6130. diams: "\u2666"
  6131. };
  6132. const HEX_NUMBER = /^[\da-fA-F]+$/;
  6133. const DECIMAL_NUMBER = /^\d+$/;
  6134. const JsxErrors = makeErrorTemplates({
  6135. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6136. MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>.",
  6137. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6138. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6139. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6140. UnterminatedJsxContent: "Unterminated JSX contents.",
  6141. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6142. }, ErrorCodes.SyntaxError, "jsx");
  6143. function isFragment(object) {
  6144. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6145. }
  6146. function getQualifiedJSXName(object) {
  6147. if (object.type === "JSXIdentifier") {
  6148. return object.name;
  6149. }
  6150. if (object.type === "JSXNamespacedName") {
  6151. return object.namespace.name + ":" + object.name.name;
  6152. }
  6153. if (object.type === "JSXMemberExpression") {
  6154. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6155. }
  6156. throw new Error("Node had unexpected type: " + object.type);
  6157. }
  6158. var jsx = (superClass => class extends superClass {
  6159. jsxReadToken() {
  6160. let out = "";
  6161. let chunkStart = this.state.pos;
  6162. for (;;) {
  6163. if (this.state.pos >= this.length) {
  6164. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6165. at: this.state.startLoc
  6166. });
  6167. }
  6168. const ch = this.input.charCodeAt(this.state.pos);
  6169. switch (ch) {
  6170. case 60:
  6171. case 123:
  6172. if (this.state.pos === this.state.start) {
  6173. if (ch === 60 && this.state.canStartJSXElement) {
  6174. ++this.state.pos;
  6175. return this.finishToken(134);
  6176. }
  6177. return super.getTokenFromCode(ch);
  6178. }
  6179. out += this.input.slice(chunkStart, this.state.pos);
  6180. return this.finishToken(133, out);
  6181. case 38:
  6182. out += this.input.slice(chunkStart, this.state.pos);
  6183. out += this.jsxReadEntity();
  6184. chunkStart = this.state.pos;
  6185. break;
  6186. case 62:
  6187. case 125:
  6188. default:
  6189. if (isNewLine(ch)) {
  6190. out += this.input.slice(chunkStart, this.state.pos);
  6191. out += this.jsxReadNewLine(true);
  6192. chunkStart = this.state.pos;
  6193. } else {
  6194. ++this.state.pos;
  6195. }
  6196. }
  6197. }
  6198. }
  6199. jsxReadNewLine(normalizeCRLF) {
  6200. const ch = this.input.charCodeAt(this.state.pos);
  6201. let out;
  6202. ++this.state.pos;
  6203. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6204. ++this.state.pos;
  6205. out = normalizeCRLF ? "\n" : "\r\n";
  6206. } else {
  6207. out = String.fromCharCode(ch);
  6208. }
  6209. ++this.state.curLine;
  6210. this.state.lineStart = this.state.pos;
  6211. return out;
  6212. }
  6213. jsxReadString(quote) {
  6214. let out = "";
  6215. let chunkStart = ++this.state.pos;
  6216. for (;;) {
  6217. if (this.state.pos >= this.length) {
  6218. throw this.raise(ErrorMessages.UnterminatedString, {
  6219. at: this.state.startLoc
  6220. });
  6221. }
  6222. const ch = this.input.charCodeAt(this.state.pos);
  6223. if (ch === quote) break;
  6224. if (ch === 38) {
  6225. out += this.input.slice(chunkStart, this.state.pos);
  6226. out += this.jsxReadEntity();
  6227. chunkStart = this.state.pos;
  6228. } else if (isNewLine(ch)) {
  6229. out += this.input.slice(chunkStart, this.state.pos);
  6230. out += this.jsxReadNewLine(false);
  6231. chunkStart = this.state.pos;
  6232. } else {
  6233. ++this.state.pos;
  6234. }
  6235. }
  6236. out += this.input.slice(chunkStart, this.state.pos++);
  6237. return this.finishToken(125, out);
  6238. }
  6239. jsxReadEntity() {
  6240. let str = "";
  6241. let count = 0;
  6242. let entity;
  6243. let ch = this.input[this.state.pos];
  6244. const startPos = ++this.state.pos;
  6245. while (this.state.pos < this.length && count++ < 10) {
  6246. ch = this.input[this.state.pos++];
  6247. if (ch === ";") {
  6248. if (str[0] === "#") {
  6249. if (str[1] === "x") {
  6250. str = str.substr(2);
  6251. if (HEX_NUMBER.test(str)) {
  6252. entity = String.fromCodePoint(parseInt(str, 16));
  6253. }
  6254. } else {
  6255. str = str.substr(1);
  6256. if (DECIMAL_NUMBER.test(str)) {
  6257. entity = String.fromCodePoint(parseInt(str, 10));
  6258. }
  6259. }
  6260. } else {
  6261. entity = entities[str];
  6262. }
  6263. break;
  6264. }
  6265. str += ch;
  6266. }
  6267. if (!entity) {
  6268. this.state.pos = startPos;
  6269. return "&";
  6270. }
  6271. return entity;
  6272. }
  6273. jsxReadWord() {
  6274. let ch;
  6275. const start = this.state.pos;
  6276. do {
  6277. ch = this.input.charCodeAt(++this.state.pos);
  6278. } while (isIdentifierChar(ch) || ch === 45);
  6279. return this.finishToken(132, this.input.slice(start, this.state.pos));
  6280. }
  6281. jsxParseIdentifier() {
  6282. const node = this.startNode();
  6283. if (this.match(132)) {
  6284. node.name = this.state.value;
  6285. } else if (tokenIsKeyword(this.state.type)) {
  6286. node.name = tokenLabelName(this.state.type);
  6287. } else {
  6288. this.unexpected();
  6289. }
  6290. this.next();
  6291. return this.finishNode(node, "JSXIdentifier");
  6292. }
  6293. jsxParseNamespacedName() {
  6294. const startPos = this.state.start;
  6295. const startLoc = this.state.startLoc;
  6296. const name = this.jsxParseIdentifier();
  6297. if (!this.eat(14)) return name;
  6298. const node = this.startNodeAt(startPos, startLoc);
  6299. node.namespace = name;
  6300. node.name = this.jsxParseIdentifier();
  6301. return this.finishNode(node, "JSXNamespacedName");
  6302. }
  6303. jsxParseElementName() {
  6304. const startPos = this.state.start;
  6305. const startLoc = this.state.startLoc;
  6306. let node = this.jsxParseNamespacedName();
  6307. if (node.type === "JSXNamespacedName") {
  6308. return node;
  6309. }
  6310. while (this.eat(16)) {
  6311. const newNode = this.startNodeAt(startPos, startLoc);
  6312. newNode.object = node;
  6313. newNode.property = this.jsxParseIdentifier();
  6314. node = this.finishNode(newNode, "JSXMemberExpression");
  6315. }
  6316. return node;
  6317. }
  6318. jsxParseAttributeValue() {
  6319. let node;
  6320. switch (this.state.type) {
  6321. case 5:
  6322. node = this.startNode();
  6323. this.setContext(types.brace);
  6324. this.next();
  6325. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6326. if (node.expression.type === "JSXEmptyExpression") {
  6327. this.raise(JsxErrors.AttributeIsEmpty, {
  6328. node
  6329. });
  6330. }
  6331. return node;
  6332. case 134:
  6333. case 125:
  6334. return this.parseExprAtom();
  6335. default:
  6336. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6337. at: this.state.startLoc
  6338. });
  6339. }
  6340. }
  6341. jsxParseEmptyExpression() {
  6342. const node = this.startNodeAt(indexes.get(this.state.lastTokEndLoc), this.state.lastTokEndLoc);
  6343. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6344. }
  6345. jsxParseSpreadChild(node) {
  6346. this.next();
  6347. node.expression = this.parseExpression();
  6348. this.setContext(types.j_oTag);
  6349. this.expect(8);
  6350. return this.finishNode(node, "JSXSpreadChild");
  6351. }
  6352. jsxParseExpressionContainer(node, previousContext) {
  6353. if (this.match(8)) {
  6354. node.expression = this.jsxParseEmptyExpression();
  6355. } else {
  6356. const expression = this.parseExpression();
  6357. node.expression = expression;
  6358. }
  6359. this.setContext(previousContext);
  6360. this.expect(8);
  6361. return this.finishNode(node, "JSXExpressionContainer");
  6362. }
  6363. jsxParseAttribute() {
  6364. const node = this.startNode();
  6365. if (this.match(5)) {
  6366. this.setContext(types.brace);
  6367. this.next();
  6368. this.expect(21);
  6369. node.argument = this.parseMaybeAssignAllowIn();
  6370. this.setContext(types.j_oTag);
  6371. this.expect(8);
  6372. return this.finishNode(node, "JSXSpreadAttribute");
  6373. }
  6374. node.name = this.jsxParseNamespacedName();
  6375. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6376. return this.finishNode(node, "JSXAttribute");
  6377. }
  6378. jsxParseOpeningElementAt(startPos, startLoc) {
  6379. const node = this.startNodeAt(startPos, startLoc);
  6380. if (this.match(135)) {
  6381. this.expect(135);
  6382. return this.finishNode(node, "JSXOpeningFragment");
  6383. }
  6384. node.name = this.jsxParseElementName();
  6385. return this.jsxParseOpeningElementAfterName(node);
  6386. }
  6387. jsxParseOpeningElementAfterName(node) {
  6388. const attributes = [];
  6389. while (!this.match(52) && !this.match(135)) {
  6390. attributes.push(this.jsxParseAttribute());
  6391. }
  6392. node.attributes = attributes;
  6393. node.selfClosing = this.eat(52);
  6394. this.expect(135);
  6395. return this.finishNode(node, "JSXOpeningElement");
  6396. }
  6397. jsxParseClosingElementAt(startPos, startLoc) {
  6398. const node = this.startNodeAt(startPos, startLoc);
  6399. if (this.match(135)) {
  6400. this.expect(135);
  6401. return this.finishNode(node, "JSXClosingFragment");
  6402. }
  6403. node.name = this.jsxParseElementName();
  6404. this.expect(135);
  6405. return this.finishNode(node, "JSXClosingElement");
  6406. }
  6407. jsxParseElementAt(startPos, startLoc) {
  6408. const node = this.startNodeAt(startPos, startLoc);
  6409. const children = [];
  6410. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  6411. let closingElement = null;
  6412. if (!openingElement.selfClosing) {
  6413. contents: for (;;) {
  6414. switch (this.state.type) {
  6415. case 134:
  6416. startPos = this.state.start;
  6417. startLoc = this.state.startLoc;
  6418. this.next();
  6419. if (this.eat(52)) {
  6420. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  6421. break contents;
  6422. }
  6423. children.push(this.jsxParseElementAt(startPos, startLoc));
  6424. break;
  6425. case 133:
  6426. children.push(this.parseExprAtom());
  6427. break;
  6428. case 5:
  6429. {
  6430. const node = this.startNode();
  6431. this.setContext(types.brace);
  6432. this.next();
  6433. if (this.match(21)) {
  6434. children.push(this.jsxParseSpreadChild(node));
  6435. } else {
  6436. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6437. }
  6438. break;
  6439. }
  6440. default:
  6441. throw this.unexpected();
  6442. }
  6443. }
  6444. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6445. this.raise(JsxErrors.MissingClosingTagFragment, {
  6446. node: closingElement
  6447. });
  6448. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6449. this.raise(JsxErrors.MissingClosingTagElement, {
  6450. node: closingElement
  6451. }, getQualifiedJSXName(openingElement.name));
  6452. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6453. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6454. this.raise(JsxErrors.MissingClosingTagElement, {
  6455. node: closingElement
  6456. }, getQualifiedJSXName(openingElement.name));
  6457. }
  6458. }
  6459. }
  6460. if (isFragment(openingElement)) {
  6461. node.openingFragment = openingElement;
  6462. node.closingFragment = closingElement;
  6463. } else {
  6464. node.openingElement = openingElement;
  6465. node.closingElement = closingElement;
  6466. }
  6467. node.children = children;
  6468. if (this.match(45)) {
  6469. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6470. at: this.state.startLoc
  6471. });
  6472. }
  6473. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6474. }
  6475. jsxParseElement() {
  6476. const startPos = this.state.start;
  6477. const startLoc = this.state.startLoc;
  6478. this.next();
  6479. return this.jsxParseElementAt(startPos, startLoc);
  6480. }
  6481. setContext(newContext) {
  6482. const {
  6483. context
  6484. } = this.state;
  6485. context[context.length - 1] = newContext;
  6486. }
  6487. parseExprAtom(refExpressionErrors) {
  6488. if (this.match(133)) {
  6489. return this.parseLiteral(this.state.value, "JSXText");
  6490. } else if (this.match(134)) {
  6491. return this.jsxParseElement();
  6492. } else if (this.match(45) && this.input.charCodeAt(this.state.pos) !== 33) {
  6493. this.replaceToken(134);
  6494. return this.jsxParseElement();
  6495. } else {
  6496. return super.parseExprAtom(refExpressionErrors);
  6497. }
  6498. }
  6499. skipSpace() {
  6500. const curContext = this.curContext();
  6501. if (!curContext.preserveSpace) super.skipSpace();
  6502. }
  6503. getTokenFromCode(code) {
  6504. const context = this.curContext();
  6505. if (context === types.j_expr) {
  6506. return this.jsxReadToken();
  6507. }
  6508. if (context === types.j_oTag || context === types.j_cTag) {
  6509. if (isIdentifierStart(code)) {
  6510. return this.jsxReadWord();
  6511. }
  6512. if (code === 62) {
  6513. ++this.state.pos;
  6514. return this.finishToken(135);
  6515. }
  6516. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6517. return this.jsxReadString(code);
  6518. }
  6519. }
  6520. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6521. ++this.state.pos;
  6522. return this.finishToken(134);
  6523. }
  6524. return super.getTokenFromCode(code);
  6525. }
  6526. updateContext(prevType) {
  6527. const {
  6528. context,
  6529. type
  6530. } = this.state;
  6531. if (type === 52 && prevType === 134) {
  6532. context.splice(-2, 2, types.j_cTag);
  6533. this.state.canStartJSXElement = false;
  6534. } else if (type === 134) {
  6535. context.push(types.j_oTag);
  6536. } else if (type === 135) {
  6537. const out = context[context.length - 1];
  6538. if (out === types.j_oTag && prevType === 52 || out === types.j_cTag) {
  6539. context.pop();
  6540. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6541. } else {
  6542. this.setContext(types.j_expr);
  6543. this.state.canStartJSXElement = true;
  6544. }
  6545. } else {
  6546. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6547. }
  6548. }
  6549. });
  6550. class TypeScriptScope extends Scope {
  6551. constructor(...args) {
  6552. super(...args);
  6553. this.types = new Set();
  6554. this.enums = new Set();
  6555. this.constEnums = new Set();
  6556. this.classes = new Set();
  6557. this.exportOnlyBindings = new Set();
  6558. }
  6559. }
  6560. class TypeScriptScopeHandler extends ScopeHandler {
  6561. createScope(flags) {
  6562. return new TypeScriptScope(flags);
  6563. }
  6564. declareName(name, bindingType, loc) {
  6565. const scope = this.currentScope();
  6566. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  6567. this.maybeExportDefined(scope, name);
  6568. scope.exportOnlyBindings.add(name);
  6569. return;
  6570. }
  6571. super.declareName(...arguments);
  6572. if (bindingType & BIND_KIND_TYPE) {
  6573. if (!(bindingType & BIND_KIND_VALUE)) {
  6574. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  6575. this.maybeExportDefined(scope, name);
  6576. }
  6577. scope.types.add(name);
  6578. }
  6579. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  6580. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  6581. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  6582. }
  6583. isRedeclaredInScope(scope, name, bindingType) {
  6584. if (scope.enums.has(name)) {
  6585. if (bindingType & BIND_FLAGS_TS_ENUM) {
  6586. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  6587. const wasConst = scope.constEnums.has(name);
  6588. return isConst !== wasConst;
  6589. }
  6590. return true;
  6591. }
  6592. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  6593. if (scope.lexical.has(name)) {
  6594. return !!(bindingType & BIND_KIND_VALUE);
  6595. } else {
  6596. return false;
  6597. }
  6598. }
  6599. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  6600. return true;
  6601. }
  6602. return super.isRedeclaredInScope(...arguments);
  6603. }
  6604. checkLocalExport(id) {
  6605. const topLevelScope = this.scopeStack[0];
  6606. const {
  6607. name
  6608. } = id;
  6609. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  6610. super.checkLocalExport(id);
  6611. }
  6612. }
  6613. }
  6614. function nonNull(x) {
  6615. if (x == null) {
  6616. throw new Error(`Unexpected ${x} value.`);
  6617. }
  6618. return x;
  6619. }
  6620. function assert(x) {
  6621. if (!x) {
  6622. throw new Error("Assert fail");
  6623. }
  6624. }
  6625. const TSErrors = makeErrorTemplates({
  6626. AbstractMethodHasImplementation: "Method '%0' cannot have an implementation because it is marked abstract.",
  6627. AbstractPropertyHasInitializer: "Property '%0' cannot have an initializer because it is marked abstract.",
  6628. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  6629. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  6630. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  6631. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  6632. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  6633. DeclareAccessor: "'declare' is not allowed in %0ters.",
  6634. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  6635. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  6636. DuplicateAccessibilityModifier: "Accessibility modifier already seen.",
  6637. DuplicateModifier: "Duplicate modifier: '%0'.",
  6638. EmptyHeritageClauseType: "'%0' list cannot be empty.",
  6639. EmptyTypeArguments: "Type argument list cannot be empty.",
  6640. EmptyTypeParameters: "Type parameter list cannot be empty.",
  6641. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  6642. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  6643. IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.",
  6644. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  6645. IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0').",
  6646. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  6647. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  6648. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  6649. InvalidModifierOnTypeMember: "'%0' modifier cannot appear on a type member.",
  6650. InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
  6651. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  6652. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  6653. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  6654. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  6655. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  6656. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  6657. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  6658. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  6659. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  6660. PrivateElementHasAccessibility: "Private elements cannot have an accessibility modifier ('%0').",
  6661. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  6662. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  6663. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  6664. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  6665. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  6666. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  6667. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  6668. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  6669. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  6670. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  6671. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  6672. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  6673. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  6674. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  6675. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  6676. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  6677. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  6678. UnsupportedSignatureParameterKind: "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0."
  6679. }, ErrorCodes.SyntaxError, "typescript");
  6680. function keywordTypeFromName(value) {
  6681. switch (value) {
  6682. case "any":
  6683. return "TSAnyKeyword";
  6684. case "boolean":
  6685. return "TSBooleanKeyword";
  6686. case "bigint":
  6687. return "TSBigIntKeyword";
  6688. case "never":
  6689. return "TSNeverKeyword";
  6690. case "number":
  6691. return "TSNumberKeyword";
  6692. case "object":
  6693. return "TSObjectKeyword";
  6694. case "string":
  6695. return "TSStringKeyword";
  6696. case "symbol":
  6697. return "TSSymbolKeyword";
  6698. case "undefined":
  6699. return "TSUndefinedKeyword";
  6700. case "unknown":
  6701. return "TSUnknownKeyword";
  6702. default:
  6703. return undefined;
  6704. }
  6705. }
  6706. function tsIsAccessModifier(modifier) {
  6707. return modifier === "private" || modifier === "public" || modifier === "protected";
  6708. }
  6709. var typescript = (superClass => class extends superClass {
  6710. getScopeHandler() {
  6711. return TypeScriptScopeHandler;
  6712. }
  6713. tsIsIdentifier() {
  6714. return tokenIsIdentifier(this.state.type);
  6715. }
  6716. tsTokenCanFollowModifier() {
  6717. return (this.match(0) || this.match(5) || this.match(51) || this.match(21) || this.match(130) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  6718. }
  6719. tsNextTokenCanFollowModifier() {
  6720. this.next();
  6721. return this.tsTokenCanFollowModifier();
  6722. }
  6723. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  6724. if (!tokenIsIdentifier(this.state.type)) {
  6725. return undefined;
  6726. }
  6727. const modifier = this.state.value;
  6728. if (allowedModifiers.indexOf(modifier) !== -1) {
  6729. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  6730. return undefined;
  6731. }
  6732. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  6733. return modifier;
  6734. }
  6735. }
  6736. return undefined;
  6737. }
  6738. tsParseModifiers(modified, allowedModifiers, disallowedModifiers, errorTemplate, stopOnStartOfClassStaticBlock) {
  6739. const enforceOrder = (loc, modifier, before, after) => {
  6740. if (modifier === before && modified[after]) {
  6741. this.raise(TSErrors.InvalidModifiersOrder, {
  6742. at: loc
  6743. }, before, after);
  6744. }
  6745. };
  6746. const incompatible = (loc, modifier, mod1, mod2) => {
  6747. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  6748. this.raise(TSErrors.IncompatibleModifiers, {
  6749. at: loc
  6750. }, mod1, mod2);
  6751. }
  6752. };
  6753. for (;;) {
  6754. const {
  6755. startLoc
  6756. } = this.state;
  6757. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  6758. if (!modifier) break;
  6759. if (tsIsAccessModifier(modifier)) {
  6760. if (modified.accessibility) {
  6761. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  6762. at: startLoc
  6763. });
  6764. } else {
  6765. enforceOrder(startLoc, modifier, modifier, "override");
  6766. enforceOrder(startLoc, modifier, modifier, "static");
  6767. enforceOrder(startLoc, modifier, modifier, "readonly");
  6768. modified.accessibility = modifier;
  6769. }
  6770. } else {
  6771. if (Object.hasOwnProperty.call(modified, modifier)) {
  6772. this.raise(TSErrors.DuplicateModifier, {
  6773. at: startLoc
  6774. }, modifier);
  6775. } else {
  6776. enforceOrder(startLoc, modifier, "static", "readonly");
  6777. enforceOrder(startLoc, modifier, "static", "override");
  6778. enforceOrder(startLoc, modifier, "override", "readonly");
  6779. enforceOrder(startLoc, modifier, "abstract", "override");
  6780. incompatible(startLoc, modifier, "declare", "override");
  6781. incompatible(startLoc, modifier, "static", "abstract");
  6782. }
  6783. modified[modifier] = true;
  6784. }
  6785. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  6786. this.raise(errorTemplate, {
  6787. at: startLoc
  6788. }, modifier);
  6789. }
  6790. }
  6791. }
  6792. tsIsListTerminator(kind) {
  6793. switch (kind) {
  6794. case "EnumMembers":
  6795. case "TypeMembers":
  6796. return this.match(8);
  6797. case "HeritageClauseElement":
  6798. return this.match(5);
  6799. case "TupleElementTypes":
  6800. return this.match(3);
  6801. case "TypeParametersOrArguments":
  6802. return this.match(46);
  6803. }
  6804. throw new Error("Unreachable");
  6805. }
  6806. tsParseList(kind, parseElement) {
  6807. const result = [];
  6808. while (!this.tsIsListTerminator(kind)) {
  6809. result.push(parseElement());
  6810. }
  6811. return result;
  6812. }
  6813. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  6814. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  6815. }
  6816. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  6817. const result = [];
  6818. let trailingCommaPos = -1;
  6819. for (;;) {
  6820. if (this.tsIsListTerminator(kind)) {
  6821. break;
  6822. }
  6823. trailingCommaPos = -1;
  6824. const element = parseElement();
  6825. if (element == null) {
  6826. return undefined;
  6827. }
  6828. result.push(element);
  6829. if (this.eat(12)) {
  6830. trailingCommaPos = this.state.lastTokStart;
  6831. continue;
  6832. }
  6833. if (this.tsIsListTerminator(kind)) {
  6834. break;
  6835. }
  6836. if (expectSuccess) {
  6837. this.expect(12);
  6838. }
  6839. return undefined;
  6840. }
  6841. if (refTrailingCommaPos) {
  6842. refTrailingCommaPos.value = trailingCommaPos;
  6843. }
  6844. return result;
  6845. }
  6846. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  6847. if (!skipFirstToken) {
  6848. if (bracket) {
  6849. this.expect(0);
  6850. } else {
  6851. this.expect(45);
  6852. }
  6853. }
  6854. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  6855. if (bracket) {
  6856. this.expect(3);
  6857. } else {
  6858. this.expect(46);
  6859. }
  6860. return result;
  6861. }
  6862. tsParseImportType() {
  6863. const node = this.startNode();
  6864. this.expect(79);
  6865. this.expect(10);
  6866. if (!this.match(125)) {
  6867. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  6868. at: this.state.startLoc
  6869. });
  6870. }
  6871. node.argument = this.parseExprAtom();
  6872. this.expect(11);
  6873. if (this.eat(16)) {
  6874. node.qualifier = this.tsParseEntityName(true);
  6875. }
  6876. if (this.match(45)) {
  6877. node.typeParameters = this.tsParseTypeArguments();
  6878. }
  6879. return this.finishNode(node, "TSImportType");
  6880. }
  6881. tsParseEntityName(allowReservedWords) {
  6882. let entity = this.parseIdentifier();
  6883. while (this.eat(16)) {
  6884. const node = this.startNodeAtNode(entity);
  6885. node.left = entity;
  6886. node.right = this.parseIdentifier(allowReservedWords);
  6887. entity = this.finishNode(node, "TSQualifiedName");
  6888. }
  6889. return entity;
  6890. }
  6891. tsParseTypeReference() {
  6892. const node = this.startNode();
  6893. node.typeName = this.tsParseEntityName(false);
  6894. if (!this.hasPrecedingLineBreak() && this.match(45)) {
  6895. node.typeParameters = this.tsParseTypeArguments();
  6896. }
  6897. return this.finishNode(node, "TSTypeReference");
  6898. }
  6899. tsParseThisTypePredicate(lhs) {
  6900. this.next();
  6901. const node = this.startNodeAtNode(lhs);
  6902. node.parameterName = lhs;
  6903. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  6904. node.asserts = false;
  6905. return this.finishNode(node, "TSTypePredicate");
  6906. }
  6907. tsParseThisTypeNode() {
  6908. const node = this.startNode();
  6909. this.next();
  6910. return this.finishNode(node, "TSThisType");
  6911. }
  6912. tsParseTypeQuery() {
  6913. const node = this.startNode();
  6914. this.expect(83);
  6915. if (this.match(79)) {
  6916. node.exprName = this.tsParseImportType();
  6917. } else {
  6918. node.exprName = this.tsParseEntityName(true);
  6919. }
  6920. return this.finishNode(node, "TSTypeQuery");
  6921. }
  6922. tsParseTypeParameter() {
  6923. const node = this.startNode();
  6924. node.name = this.tsParseTypeParameterName();
  6925. node.constraint = this.tsEatThenParseType(77);
  6926. node.default = this.tsEatThenParseType(29);
  6927. return this.finishNode(node, "TSTypeParameter");
  6928. }
  6929. tsTryParseTypeParameters() {
  6930. if (this.match(45)) {
  6931. return this.tsParseTypeParameters();
  6932. }
  6933. }
  6934. tsParseTypeParameters() {
  6935. const node = this.startNode();
  6936. if (this.match(45) || this.match(134)) {
  6937. this.next();
  6938. } else {
  6939. this.unexpected();
  6940. }
  6941. const refTrailingCommaPos = {
  6942. value: -1
  6943. };
  6944. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true, refTrailingCommaPos);
  6945. if (node.params.length === 0) {
  6946. this.raise(TSErrors.EmptyTypeParameters, {
  6947. node
  6948. });
  6949. }
  6950. if (refTrailingCommaPos.value !== -1) {
  6951. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  6952. }
  6953. return this.finishNode(node, "TSTypeParameterDeclaration");
  6954. }
  6955. tsTryNextParseConstantContext() {
  6956. if (this.lookahead().type === 71) {
  6957. this.next();
  6958. return this.tsParseTypeReference();
  6959. }
  6960. return null;
  6961. }
  6962. tsFillSignature(returnToken, signature) {
  6963. const returnTokenRequired = returnToken === 19;
  6964. const paramsKey = "parameters";
  6965. const returnTypeKey = "typeAnnotation";
  6966. signature.typeParameters = this.tsTryParseTypeParameters();
  6967. this.expect(10);
  6968. signature[paramsKey] = this.tsParseBindingListForSignature();
  6969. if (returnTokenRequired) {
  6970. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  6971. } else if (this.match(returnToken)) {
  6972. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  6973. }
  6974. }
  6975. tsParseBindingListForSignature() {
  6976. return this.parseBindingList(11, 41).map(pattern => {
  6977. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  6978. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  6979. node: pattern
  6980. }, pattern.type);
  6981. }
  6982. return pattern;
  6983. });
  6984. }
  6985. tsParseTypeMemberSemicolon() {
  6986. if (!this.eat(12) && !this.isLineTerminator()) {
  6987. this.expect(13);
  6988. }
  6989. }
  6990. tsParseSignatureMember(kind, node) {
  6991. this.tsFillSignature(14, node);
  6992. this.tsParseTypeMemberSemicolon();
  6993. return this.finishNode(node, kind);
  6994. }
  6995. tsIsUnambiguouslyIndexSignature() {
  6996. this.next();
  6997. if (tokenIsIdentifier(this.state.type)) {
  6998. this.next();
  6999. return this.match(14);
  7000. }
  7001. return false;
  7002. }
  7003. tsTryParseIndexSignature(node) {
  7004. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7005. return undefined;
  7006. }
  7007. this.expect(0);
  7008. const id = this.parseIdentifier();
  7009. id.typeAnnotation = this.tsParseTypeAnnotation();
  7010. this.resetEndLocation(id);
  7011. this.expect(3);
  7012. node.parameters = [id];
  7013. const type = this.tsTryParseTypeAnnotation();
  7014. if (type) node.typeAnnotation = type;
  7015. this.tsParseTypeMemberSemicolon();
  7016. return this.finishNode(node, "TSIndexSignature");
  7017. }
  7018. tsParsePropertyOrMethodSignature(node, readonly) {
  7019. if (this.eat(17)) node.optional = true;
  7020. const nodeAny = node;
  7021. if (this.match(10) || this.match(45)) {
  7022. if (readonly) {
  7023. this.raise(TSErrors.ReadonlyForMethodSignature, {
  7024. node
  7025. });
  7026. }
  7027. const method = nodeAny;
  7028. if (method.kind && this.match(45)) {
  7029. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  7030. at: this.state.curPosition()
  7031. });
  7032. }
  7033. this.tsFillSignature(14, method);
  7034. this.tsParseTypeMemberSemicolon();
  7035. const paramsKey = "parameters";
  7036. const returnTypeKey = "typeAnnotation";
  7037. if (method.kind === "get") {
  7038. if (method[paramsKey].length > 0) {
  7039. this.raise(ErrorMessages.BadGetterArity, {
  7040. at: this.state.curPosition()
  7041. });
  7042. if (this.isThisParam(method[paramsKey][0])) {
  7043. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7044. at: this.state.curPosition()
  7045. });
  7046. }
  7047. }
  7048. } else if (method.kind === "set") {
  7049. if (method[paramsKey].length !== 1) {
  7050. this.raise(ErrorMessages.BadSetterArity, {
  7051. at: this.state.curPosition()
  7052. });
  7053. } else {
  7054. const firstParameter = method[paramsKey][0];
  7055. if (this.isThisParam(firstParameter)) {
  7056. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7057. at: this.state.curPosition()
  7058. });
  7059. }
  7060. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7061. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  7062. at: this.state.curPosition()
  7063. });
  7064. }
  7065. if (firstParameter.type === "RestElement") {
  7066. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  7067. at: this.state.curPosition()
  7068. });
  7069. }
  7070. }
  7071. if (method[returnTypeKey]) {
  7072. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  7073. node: method[returnTypeKey]
  7074. });
  7075. }
  7076. } else {
  7077. method.kind = "method";
  7078. }
  7079. return this.finishNode(method, "TSMethodSignature");
  7080. } else {
  7081. const property = nodeAny;
  7082. if (readonly) property.readonly = true;
  7083. const type = this.tsTryParseTypeAnnotation();
  7084. if (type) property.typeAnnotation = type;
  7085. this.tsParseTypeMemberSemicolon();
  7086. return this.finishNode(property, "TSPropertySignature");
  7087. }
  7088. }
  7089. tsParseTypeMember() {
  7090. const node = this.startNode();
  7091. if (this.match(10) || this.match(45)) {
  7092. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7093. }
  7094. if (this.match(73)) {
  7095. const id = this.startNode();
  7096. this.next();
  7097. if (this.match(10) || this.match(45)) {
  7098. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7099. } else {
  7100. node.key = this.createIdentifier(id, "new");
  7101. return this.tsParsePropertyOrMethodSignature(node, false);
  7102. }
  7103. }
  7104. this.tsParseModifiers(node, ["readonly"], ["declare", "abstract", "private", "protected", "public", "static", "override"], TSErrors.InvalidModifierOnTypeMember);
  7105. const idx = this.tsTryParseIndexSignature(node);
  7106. if (idx) {
  7107. return idx;
  7108. }
  7109. this.parsePropertyName(node);
  7110. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7111. node.kind = node.key.name;
  7112. this.parsePropertyName(node);
  7113. }
  7114. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7115. }
  7116. tsParseTypeLiteral() {
  7117. const node = this.startNode();
  7118. node.members = this.tsParseObjectTypeMembers();
  7119. return this.finishNode(node, "TSTypeLiteral");
  7120. }
  7121. tsParseObjectTypeMembers() {
  7122. this.expect(5);
  7123. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7124. this.expect(8);
  7125. return members;
  7126. }
  7127. tsIsStartOfMappedType() {
  7128. this.next();
  7129. if (this.eat(49)) {
  7130. return this.isContextual(114);
  7131. }
  7132. if (this.isContextual(114)) {
  7133. this.next();
  7134. }
  7135. if (!this.match(0)) {
  7136. return false;
  7137. }
  7138. this.next();
  7139. if (!this.tsIsIdentifier()) {
  7140. return false;
  7141. }
  7142. this.next();
  7143. return this.match(54);
  7144. }
  7145. tsParseMappedTypeParameter() {
  7146. const node = this.startNode();
  7147. node.name = this.tsParseTypeParameterName();
  7148. node.constraint = this.tsExpectThenParseType(54);
  7149. return this.finishNode(node, "TSTypeParameter");
  7150. }
  7151. tsParseMappedType() {
  7152. const node = this.startNode();
  7153. this.expect(5);
  7154. if (this.match(49)) {
  7155. node.readonly = this.state.value;
  7156. this.next();
  7157. this.expectContextual(114);
  7158. } else if (this.eatContextual(114)) {
  7159. node.readonly = true;
  7160. }
  7161. this.expect(0);
  7162. node.typeParameter = this.tsParseMappedTypeParameter();
  7163. node.nameType = this.eatContextual(89) ? this.tsParseType() : null;
  7164. this.expect(3);
  7165. if (this.match(49)) {
  7166. node.optional = this.state.value;
  7167. this.next();
  7168. this.expect(17);
  7169. } else if (this.eat(17)) {
  7170. node.optional = true;
  7171. }
  7172. node.typeAnnotation = this.tsTryParseType();
  7173. this.semicolon();
  7174. this.expect(8);
  7175. return this.finishNode(node, "TSMappedType");
  7176. }
  7177. tsParseTupleType() {
  7178. const node = this.startNode();
  7179. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7180. let seenOptionalElement = false;
  7181. let labeledElements = null;
  7182. node.elementTypes.forEach(elementNode => {
  7183. var _labeledElements;
  7184. let {
  7185. type
  7186. } = elementNode;
  7187. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  7188. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  7189. node: elementNode
  7190. });
  7191. }
  7192. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  7193. if (type === "TSRestType") {
  7194. elementNode = elementNode.typeAnnotation;
  7195. type = elementNode.type;
  7196. }
  7197. const isLabeled = type === "TSNamedTupleMember";
  7198. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  7199. if (labeledElements !== isLabeled) {
  7200. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  7201. node: elementNode
  7202. });
  7203. }
  7204. });
  7205. return this.finishNode(node, "TSTupleType");
  7206. }
  7207. tsParseTupleElementType() {
  7208. const {
  7209. start: startPos,
  7210. startLoc
  7211. } = this.state;
  7212. const rest = this.eat(21);
  7213. let type = this.tsParseType();
  7214. const optional = this.eat(17);
  7215. const labeled = this.eat(14);
  7216. if (labeled) {
  7217. const labeledNode = this.startNodeAtNode(type);
  7218. labeledNode.optional = optional;
  7219. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  7220. labeledNode.label = type.typeName;
  7221. } else {
  7222. this.raise(TSErrors.InvalidTupleMemberLabel, {
  7223. node: type
  7224. });
  7225. labeledNode.label = type;
  7226. }
  7227. labeledNode.elementType = this.tsParseType();
  7228. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  7229. } else if (optional) {
  7230. const optionalTypeNode = this.startNodeAtNode(type);
  7231. optionalTypeNode.typeAnnotation = type;
  7232. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  7233. }
  7234. if (rest) {
  7235. const restNode = this.startNodeAt(startPos, startLoc);
  7236. restNode.typeAnnotation = type;
  7237. type = this.finishNode(restNode, "TSRestType");
  7238. }
  7239. return type;
  7240. }
  7241. tsParseParenthesizedType() {
  7242. const node = this.startNode();
  7243. this.expect(10);
  7244. node.typeAnnotation = this.tsParseType();
  7245. this.expect(11);
  7246. return this.finishNode(node, "TSParenthesizedType");
  7247. }
  7248. tsParseFunctionOrConstructorType(type, abstract) {
  7249. const node = this.startNode();
  7250. if (type === "TSConstructorType") {
  7251. node.abstract = !!abstract;
  7252. if (abstract) this.next();
  7253. this.next();
  7254. }
  7255. this.tsFillSignature(19, node);
  7256. return this.finishNode(node, type);
  7257. }
  7258. tsParseLiteralTypeNode() {
  7259. const node = this.startNode();
  7260. node.literal = (() => {
  7261. switch (this.state.type) {
  7262. case 126:
  7263. case 127:
  7264. case 125:
  7265. case 81:
  7266. case 82:
  7267. return this.parseExprAtom();
  7268. default:
  7269. throw this.unexpected();
  7270. }
  7271. })();
  7272. return this.finishNode(node, "TSLiteralType");
  7273. }
  7274. tsParseTemplateLiteralType() {
  7275. const node = this.startNode();
  7276. node.literal = this.parseTemplate(false);
  7277. return this.finishNode(node, "TSLiteralType");
  7278. }
  7279. parseTemplateSubstitution() {
  7280. if (this.state.inType) return this.tsParseType();
  7281. return super.parseTemplateSubstitution();
  7282. }
  7283. tsParseThisTypeOrThisTypePredicate() {
  7284. const thisKeyword = this.tsParseThisTypeNode();
  7285. if (this.isContextual(109) && !this.hasPrecedingLineBreak()) {
  7286. return this.tsParseThisTypePredicate(thisKeyword);
  7287. } else {
  7288. return thisKeyword;
  7289. }
  7290. }
  7291. tsParseNonArrayType() {
  7292. switch (this.state.type) {
  7293. case 125:
  7294. case 126:
  7295. case 127:
  7296. case 81:
  7297. case 82:
  7298. return this.tsParseLiteralTypeNode();
  7299. case 49:
  7300. if (this.state.value === "-") {
  7301. const node = this.startNode();
  7302. const nextToken = this.lookahead();
  7303. if (nextToken.type !== 126 && nextToken.type !== 127) {
  7304. throw this.unexpected();
  7305. }
  7306. node.literal = this.parseMaybeUnary();
  7307. return this.finishNode(node, "TSLiteralType");
  7308. }
  7309. break;
  7310. case 74:
  7311. return this.tsParseThisTypeOrThisTypePredicate();
  7312. case 83:
  7313. return this.tsParseTypeQuery();
  7314. case 79:
  7315. return this.tsParseImportType();
  7316. case 5:
  7317. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7318. case 0:
  7319. return this.tsParseTupleType();
  7320. case 10:
  7321. return this.tsParseParenthesizedType();
  7322. case 25:
  7323. case 24:
  7324. return this.tsParseTemplateLiteralType();
  7325. default:
  7326. {
  7327. const {
  7328. type
  7329. } = this.state;
  7330. if (tokenIsIdentifier(type) || type === 84 || type === 80) {
  7331. const nodeType = type === 84 ? "TSVoidKeyword" : type === 80 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7332. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  7333. const node = this.startNode();
  7334. this.next();
  7335. return this.finishNode(node, nodeType);
  7336. }
  7337. return this.tsParseTypeReference();
  7338. }
  7339. }
  7340. }
  7341. throw this.unexpected();
  7342. }
  7343. tsParseArrayTypeOrHigher() {
  7344. let type = this.tsParseNonArrayType();
  7345. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  7346. if (this.match(3)) {
  7347. const node = this.startNodeAtNode(type);
  7348. node.elementType = type;
  7349. this.expect(3);
  7350. type = this.finishNode(node, "TSArrayType");
  7351. } else {
  7352. const node = this.startNodeAtNode(type);
  7353. node.objectType = type;
  7354. node.indexType = this.tsParseType();
  7355. this.expect(3);
  7356. type = this.finishNode(node, "TSIndexedAccessType");
  7357. }
  7358. }
  7359. return type;
  7360. }
  7361. tsParseTypeOperator() {
  7362. const node = this.startNode();
  7363. const operator = this.state.value;
  7364. this.next();
  7365. node.operator = operator;
  7366. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7367. if (operator === "readonly") {
  7368. this.tsCheckTypeAnnotationForReadOnly(node);
  7369. }
  7370. return this.finishNode(node, "TSTypeOperator");
  7371. }
  7372. tsCheckTypeAnnotationForReadOnly(node) {
  7373. switch (node.typeAnnotation.type) {
  7374. case "TSTupleType":
  7375. case "TSArrayType":
  7376. return;
  7377. default:
  7378. this.raise(TSErrors.UnexpectedReadonly, {
  7379. node
  7380. });
  7381. }
  7382. }
  7383. tsParseInferType() {
  7384. const node = this.startNode();
  7385. this.expectContextual(108);
  7386. const typeParameter = this.startNode();
  7387. typeParameter.name = this.tsParseTypeParameterName();
  7388. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7389. return this.finishNode(node, "TSInferType");
  7390. }
  7391. tsParseTypeOperatorOrHigher() {
  7392. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  7393. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(108) ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  7394. }
  7395. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7396. const node = this.startNode();
  7397. const hasLeadingOperator = this.eat(operator);
  7398. const types = [];
  7399. do {
  7400. types.push(parseConstituentType());
  7401. } while (this.eat(operator));
  7402. if (types.length === 1 && !hasLeadingOperator) {
  7403. return types[0];
  7404. }
  7405. node.types = types;
  7406. return this.finishNode(node, kind);
  7407. }
  7408. tsParseIntersectionTypeOrHigher() {
  7409. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 43);
  7410. }
  7411. tsParseUnionTypeOrHigher() {
  7412. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 41);
  7413. }
  7414. tsIsStartOfFunctionType() {
  7415. if (this.match(45)) {
  7416. return true;
  7417. }
  7418. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  7419. }
  7420. tsSkipParameterStart() {
  7421. if (tokenIsIdentifier(this.state.type) || this.match(74)) {
  7422. this.next();
  7423. return true;
  7424. }
  7425. if (this.match(5)) {
  7426. let braceStackCounter = 1;
  7427. this.next();
  7428. while (braceStackCounter > 0) {
  7429. if (this.match(5)) {
  7430. ++braceStackCounter;
  7431. } else if (this.match(8)) {
  7432. --braceStackCounter;
  7433. }
  7434. this.next();
  7435. }
  7436. return true;
  7437. }
  7438. if (this.match(0)) {
  7439. let braceStackCounter = 1;
  7440. this.next();
  7441. while (braceStackCounter > 0) {
  7442. if (this.match(0)) {
  7443. ++braceStackCounter;
  7444. } else if (this.match(3)) {
  7445. --braceStackCounter;
  7446. }
  7447. this.next();
  7448. }
  7449. return true;
  7450. }
  7451. return false;
  7452. }
  7453. tsIsUnambiguouslyStartOfFunctionType() {
  7454. this.next();
  7455. if (this.match(11) || this.match(21)) {
  7456. return true;
  7457. }
  7458. if (this.tsSkipParameterStart()) {
  7459. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  7460. return true;
  7461. }
  7462. if (this.match(11)) {
  7463. this.next();
  7464. if (this.match(19)) {
  7465. return true;
  7466. }
  7467. }
  7468. }
  7469. return false;
  7470. }
  7471. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  7472. return this.tsInType(() => {
  7473. const t = this.startNode();
  7474. this.expect(returnToken);
  7475. const node = this.startNode();
  7476. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  7477. if (asserts && this.match(74)) {
  7478. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  7479. if (thisTypePredicate.type === "TSThisType") {
  7480. node.parameterName = thisTypePredicate;
  7481. node.asserts = true;
  7482. node.typeAnnotation = null;
  7483. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  7484. } else {
  7485. this.resetStartLocationFromNode(thisTypePredicate, node);
  7486. thisTypePredicate.asserts = true;
  7487. }
  7488. t.typeAnnotation = thisTypePredicate;
  7489. return this.finishNode(t, "TSTypeAnnotation");
  7490. }
  7491. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  7492. if (!typePredicateVariable) {
  7493. if (!asserts) {
  7494. return this.tsParseTypeAnnotation(false, t);
  7495. }
  7496. node.parameterName = this.parseIdentifier();
  7497. node.asserts = asserts;
  7498. node.typeAnnotation = null;
  7499. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7500. return this.finishNode(t, "TSTypeAnnotation");
  7501. }
  7502. const type = this.tsParseTypeAnnotation(false);
  7503. node.parameterName = typePredicateVariable;
  7504. node.typeAnnotation = type;
  7505. node.asserts = asserts;
  7506. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  7507. return this.finishNode(t, "TSTypeAnnotation");
  7508. });
  7509. }
  7510. tsTryParseTypeOrTypePredicateAnnotation() {
  7511. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  7512. }
  7513. tsTryParseTypeAnnotation() {
  7514. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  7515. }
  7516. tsTryParseType() {
  7517. return this.tsEatThenParseType(14);
  7518. }
  7519. tsParseTypePredicatePrefix() {
  7520. const id = this.parseIdentifier();
  7521. if (this.isContextual(109) && !this.hasPrecedingLineBreak()) {
  7522. this.next();
  7523. return id;
  7524. }
  7525. }
  7526. tsParseTypePredicateAsserts() {
  7527. if (this.state.type !== 102) {
  7528. return false;
  7529. }
  7530. const containsEsc = this.state.containsEsc;
  7531. this.next();
  7532. if (!tokenIsIdentifier(this.state.type) && !this.match(74)) {
  7533. return false;
  7534. }
  7535. if (containsEsc) {
  7536. this.raise(ErrorMessages.InvalidEscapedReservedWord, {
  7537. at: this.state.lastTokStartLoc
  7538. }, "asserts");
  7539. }
  7540. return true;
  7541. }
  7542. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  7543. this.tsInType(() => {
  7544. if (eatColon) this.expect(14);
  7545. t.typeAnnotation = this.tsParseType();
  7546. });
  7547. return this.finishNode(t, "TSTypeAnnotation");
  7548. }
  7549. tsParseType() {
  7550. assert(this.state.inType);
  7551. const type = this.tsParseNonConditionalType();
  7552. if (this.hasPrecedingLineBreak() || !this.eat(77)) {
  7553. return type;
  7554. }
  7555. const node = this.startNodeAtNode(type);
  7556. node.checkType = type;
  7557. node.extendsType = this.tsParseNonConditionalType();
  7558. this.expect(17);
  7559. node.trueType = this.tsParseType();
  7560. this.expect(14);
  7561. node.falseType = this.tsParseType();
  7562. return this.finishNode(node, "TSConditionalType");
  7563. }
  7564. isAbstractConstructorSignature() {
  7565. return this.isContextual(116) && this.lookahead().type === 73;
  7566. }
  7567. tsParseNonConditionalType() {
  7568. if (this.tsIsStartOfFunctionType()) {
  7569. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  7570. }
  7571. if (this.match(73)) {
  7572. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  7573. } else if (this.isAbstractConstructorSignature()) {
  7574. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  7575. }
  7576. return this.tsParseUnionTypeOrHigher();
  7577. }
  7578. tsParseTypeAssertion() {
  7579. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  7580. this.raise(TSErrors.ReservedTypeAssertion, {
  7581. at: this.state.startLoc
  7582. });
  7583. }
  7584. const node = this.startNode();
  7585. const _const = this.tsTryNextParseConstantContext();
  7586. node.typeAnnotation = _const || this.tsNextThenParseType();
  7587. this.expect(46);
  7588. node.expression = this.parseMaybeUnary();
  7589. return this.finishNode(node, "TSTypeAssertion");
  7590. }
  7591. tsParseHeritageClause(descriptor) {
  7592. const originalStartLoc = this.state.startLoc;
  7593. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  7594. if (!delimitedList.length) {
  7595. this.raise(TSErrors.EmptyHeritageClauseType, {
  7596. at: originalStartLoc
  7597. }, descriptor);
  7598. }
  7599. return delimitedList;
  7600. }
  7601. tsParseExpressionWithTypeArguments() {
  7602. const node = this.startNode();
  7603. node.expression = this.tsParseEntityName(false);
  7604. if (this.match(45)) {
  7605. node.typeParameters = this.tsParseTypeArguments();
  7606. }
  7607. return this.finishNode(node, "TSExpressionWithTypeArguments");
  7608. }
  7609. tsParseInterfaceDeclaration(node) {
  7610. if (tokenIsIdentifier(this.state.type)) {
  7611. node.id = this.parseIdentifier();
  7612. this.checkLVal(node.id, "typescript interface declaration", BIND_TS_INTERFACE);
  7613. } else {
  7614. node.id = null;
  7615. this.raise(TSErrors.MissingInterfaceName, {
  7616. at: this.state.startLoc
  7617. });
  7618. }
  7619. node.typeParameters = this.tsTryParseTypeParameters();
  7620. if (this.eat(77)) {
  7621. node.extends = this.tsParseHeritageClause("extends");
  7622. }
  7623. const body = this.startNode();
  7624. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  7625. node.body = this.finishNode(body, "TSInterfaceBody");
  7626. return this.finishNode(node, "TSInterfaceDeclaration");
  7627. }
  7628. tsParseTypeAliasDeclaration(node) {
  7629. node.id = this.parseIdentifier();
  7630. this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
  7631. node.typeParameters = this.tsTryParseTypeParameters();
  7632. node.typeAnnotation = this.tsInType(() => {
  7633. this.expect(29);
  7634. if (this.isContextual(107) && this.lookahead().type !== 16) {
  7635. const node = this.startNode();
  7636. this.next();
  7637. return this.finishNode(node, "TSIntrinsicKeyword");
  7638. }
  7639. return this.tsParseType();
  7640. });
  7641. this.semicolon();
  7642. return this.finishNode(node, "TSTypeAliasDeclaration");
  7643. }
  7644. tsInNoContext(cb) {
  7645. const oldContext = this.state.context;
  7646. this.state.context = [oldContext[0]];
  7647. try {
  7648. return cb();
  7649. } finally {
  7650. this.state.context = oldContext;
  7651. }
  7652. }
  7653. tsInType(cb) {
  7654. const oldInType = this.state.inType;
  7655. this.state.inType = true;
  7656. try {
  7657. return cb();
  7658. } finally {
  7659. this.state.inType = oldInType;
  7660. }
  7661. }
  7662. tsEatThenParseType(token) {
  7663. return !this.match(token) ? undefined : this.tsNextThenParseType();
  7664. }
  7665. tsExpectThenParseType(token) {
  7666. return this.tsDoThenParseType(() => this.expect(token));
  7667. }
  7668. tsNextThenParseType() {
  7669. return this.tsDoThenParseType(() => this.next());
  7670. }
  7671. tsDoThenParseType(cb) {
  7672. return this.tsInType(() => {
  7673. cb();
  7674. return this.tsParseType();
  7675. });
  7676. }
  7677. tsParseEnumMember() {
  7678. const node = this.startNode();
  7679. node.id = this.match(125) ? this.parseExprAtom() : this.parseIdentifier(true);
  7680. if (this.eat(29)) {
  7681. node.initializer = this.parseMaybeAssignAllowIn();
  7682. }
  7683. return this.finishNode(node, "TSEnumMember");
  7684. }
  7685. tsParseEnumDeclaration(node, isConst) {
  7686. if (isConst) node.const = true;
  7687. node.id = this.parseIdentifier();
  7688. this.checkLVal(node.id, "typescript enum declaration", isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  7689. this.expect(5);
  7690. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  7691. this.expect(8);
  7692. return this.finishNode(node, "TSEnumDeclaration");
  7693. }
  7694. tsParseModuleBlock() {
  7695. const node = this.startNode();
  7696. this.scope.enter(SCOPE_OTHER);
  7697. this.expect(5);
  7698. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  7699. this.scope.exit();
  7700. return this.finishNode(node, "TSModuleBlock");
  7701. }
  7702. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  7703. node.id = this.parseIdentifier();
  7704. if (!nested) {
  7705. this.checkLVal(node.id, "module or namespace declaration", BIND_TS_NAMESPACE);
  7706. }
  7707. if (this.eat(16)) {
  7708. const inner = this.startNode();
  7709. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  7710. node.body = inner;
  7711. } else {
  7712. this.scope.enter(SCOPE_TS_MODULE);
  7713. this.prodParam.enter(PARAM);
  7714. node.body = this.tsParseModuleBlock();
  7715. this.prodParam.exit();
  7716. this.scope.exit();
  7717. }
  7718. return this.finishNode(node, "TSModuleDeclaration");
  7719. }
  7720. tsParseAmbientExternalModuleDeclaration(node) {
  7721. if (this.isContextual(105)) {
  7722. node.global = true;
  7723. node.id = this.parseIdentifier();
  7724. } else if (this.match(125)) {
  7725. node.id = this.parseExprAtom();
  7726. } else {
  7727. this.unexpected();
  7728. }
  7729. if (this.match(5)) {
  7730. this.scope.enter(SCOPE_TS_MODULE);
  7731. this.prodParam.enter(PARAM);
  7732. node.body = this.tsParseModuleBlock();
  7733. this.prodParam.exit();
  7734. this.scope.exit();
  7735. } else {
  7736. this.semicolon();
  7737. }
  7738. return this.finishNode(node, "TSModuleDeclaration");
  7739. }
  7740. tsParseImportEqualsDeclaration(node, isExport) {
  7741. node.isExport = isExport || false;
  7742. node.id = this.parseIdentifier();
  7743. this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
  7744. this.expect(29);
  7745. const moduleReference = this.tsParseModuleReference();
  7746. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  7747. this.raise(TSErrors.ImportAliasHasImportType, {
  7748. node: moduleReference
  7749. });
  7750. }
  7751. node.moduleReference = moduleReference;
  7752. this.semicolon();
  7753. return this.finishNode(node, "TSImportEqualsDeclaration");
  7754. }
  7755. tsIsExternalModuleReference() {
  7756. return this.isContextual(112) && this.lookaheadCharCode() === 40;
  7757. }
  7758. tsParseModuleReference() {
  7759. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  7760. }
  7761. tsParseExternalModuleReference() {
  7762. const node = this.startNode();
  7763. this.expectContextual(112);
  7764. this.expect(10);
  7765. if (!this.match(125)) {
  7766. throw this.unexpected();
  7767. }
  7768. node.expression = this.parseExprAtom();
  7769. this.expect(11);
  7770. return this.finishNode(node, "TSExternalModuleReference");
  7771. }
  7772. tsLookAhead(f) {
  7773. const state = this.state.clone();
  7774. const res = f();
  7775. this.state = state;
  7776. return res;
  7777. }
  7778. tsTryParseAndCatch(f) {
  7779. const result = this.tryParse(abort => f() || abort());
  7780. if (result.aborted || !result.node) return undefined;
  7781. if (result.error) this.state = result.failState;
  7782. return result.node;
  7783. }
  7784. tsTryParse(f) {
  7785. const state = this.state.clone();
  7786. const result = f();
  7787. if (result !== undefined && result !== false) {
  7788. return result;
  7789. } else {
  7790. this.state = state;
  7791. return undefined;
  7792. }
  7793. }
  7794. tsTryParseDeclare(nany) {
  7795. if (this.isLineTerminator()) {
  7796. return;
  7797. }
  7798. let starttype = this.state.type;
  7799. let kind;
  7800. if (this.isContextual(95)) {
  7801. starttype = 70;
  7802. kind = "let";
  7803. }
  7804. return this.tsInAmbientContext(() => {
  7805. switch (starttype) {
  7806. case 64:
  7807. nany.declare = true;
  7808. return this.parseFunctionStatement(nany, false, true);
  7809. case 76:
  7810. nany.declare = true;
  7811. return this.parseClass(nany, true, false);
  7812. case 71:
  7813. if (this.match(71) && this.isLookaheadContextual("enum")) {
  7814. this.expect(71);
  7815. this.expectContextual(118);
  7816. return this.tsParseEnumDeclaration(nany, true);
  7817. }
  7818. case 70:
  7819. kind = kind || this.state.value;
  7820. return this.parseVarStatement(nany, kind);
  7821. case 105:
  7822. return this.tsParseAmbientExternalModuleDeclaration(nany);
  7823. default:
  7824. {
  7825. if (tokenIsIdentifier(starttype)) {
  7826. return this.tsParseDeclaration(nany, this.state.value, true);
  7827. }
  7828. }
  7829. }
  7830. });
  7831. }
  7832. tsTryParseExportDeclaration() {
  7833. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  7834. }
  7835. tsParseExpressionStatement(node, expr) {
  7836. switch (expr.name) {
  7837. case "declare":
  7838. {
  7839. const declaration = this.tsTryParseDeclare(node);
  7840. if (declaration) {
  7841. declaration.declare = true;
  7842. return declaration;
  7843. }
  7844. break;
  7845. }
  7846. case "global":
  7847. if (this.match(5)) {
  7848. this.scope.enter(SCOPE_TS_MODULE);
  7849. this.prodParam.enter(PARAM);
  7850. const mod = node;
  7851. mod.global = true;
  7852. mod.id = expr;
  7853. mod.body = this.tsParseModuleBlock();
  7854. this.scope.exit();
  7855. this.prodParam.exit();
  7856. return this.finishNode(mod, "TSModuleDeclaration");
  7857. }
  7858. break;
  7859. default:
  7860. return this.tsParseDeclaration(node, expr.name, false);
  7861. }
  7862. }
  7863. tsParseDeclaration(node, value, next) {
  7864. switch (value) {
  7865. case "abstract":
  7866. if (this.tsCheckLineTerminator(next) && (this.match(76) || tokenIsIdentifier(this.state.type))) {
  7867. return this.tsParseAbstractDeclaration(node);
  7868. }
  7869. break;
  7870. case "enum":
  7871. if (next || tokenIsIdentifier(this.state.type)) {
  7872. if (next) this.next();
  7873. return this.tsParseEnumDeclaration(node, false);
  7874. }
  7875. break;
  7876. case "interface":
  7877. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  7878. return this.tsParseInterfaceDeclaration(node);
  7879. }
  7880. break;
  7881. case "module":
  7882. if (this.tsCheckLineTerminator(next)) {
  7883. if (this.match(125)) {
  7884. return this.tsParseAmbientExternalModuleDeclaration(node);
  7885. } else if (tokenIsIdentifier(this.state.type)) {
  7886. return this.tsParseModuleOrNamespaceDeclaration(node);
  7887. }
  7888. }
  7889. break;
  7890. case "namespace":
  7891. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  7892. return this.tsParseModuleOrNamespaceDeclaration(node);
  7893. }
  7894. break;
  7895. case "type":
  7896. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  7897. return this.tsParseTypeAliasDeclaration(node);
  7898. }
  7899. break;
  7900. }
  7901. }
  7902. tsCheckLineTerminator(next) {
  7903. if (next) {
  7904. if (this.hasFollowingLineBreak()) return false;
  7905. this.next();
  7906. return true;
  7907. }
  7908. return !this.isLineTerminator();
  7909. }
  7910. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  7911. if (!this.match(45)) {
  7912. return undefined;
  7913. }
  7914. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7915. this.state.maybeInArrowParameters = true;
  7916. const res = this.tsTryParseAndCatch(() => {
  7917. const node = this.startNodeAt(startPos, startLoc);
  7918. node.typeParameters = this.tsParseTypeParameters();
  7919. super.parseFunctionParams(node);
  7920. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  7921. this.expect(19);
  7922. return node;
  7923. });
  7924. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7925. if (!res) {
  7926. return undefined;
  7927. }
  7928. return this.parseArrowExpression(res, null, true);
  7929. }
  7930. tsParseTypeArguments() {
  7931. const node = this.startNode();
  7932. node.params = this.tsInType(() => this.tsInNoContext(() => {
  7933. this.expect(45);
  7934. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  7935. }));
  7936. if (node.params.length === 0) {
  7937. this.raise(TSErrors.EmptyTypeArguments, {
  7938. node
  7939. });
  7940. }
  7941. this.expect(46);
  7942. return this.finishNode(node, "TSTypeParameterInstantiation");
  7943. }
  7944. tsIsDeclarationStart() {
  7945. return tokenIsTSDeclarationStart(this.state.type);
  7946. }
  7947. isExportDefaultSpecifier() {
  7948. if (this.tsIsDeclarationStart()) return false;
  7949. return super.isExportDefaultSpecifier();
  7950. }
  7951. parseAssignableListItem(allowModifiers, decorators) {
  7952. const startPos = this.state.start;
  7953. const startLoc = this.state.startLoc;
  7954. let accessibility;
  7955. let readonly = false;
  7956. let override = false;
  7957. if (allowModifiers !== undefined) {
  7958. const modified = {};
  7959. this.tsParseModifiers(modified, ["public", "private", "protected", "override", "readonly"]);
  7960. accessibility = modified.accessibility;
  7961. override = modified.override;
  7962. readonly = modified.readonly;
  7963. if (allowModifiers === false && (accessibility || readonly || override)) {
  7964. this.raise(TSErrors.UnexpectedParameterModifier, {
  7965. at: startLoc
  7966. });
  7967. }
  7968. }
  7969. const left = this.parseMaybeDefault();
  7970. this.parseAssignableListItemTypes(left);
  7971. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  7972. if (accessibility || readonly || override) {
  7973. const pp = this.startNodeAt(startPos, startLoc);
  7974. if (decorators.length) {
  7975. pp.decorators = decorators;
  7976. }
  7977. if (accessibility) pp.accessibility = accessibility;
  7978. if (readonly) pp.readonly = readonly;
  7979. if (override) pp.override = override;
  7980. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  7981. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  7982. node: pp
  7983. });
  7984. }
  7985. pp.parameter = elt;
  7986. return this.finishNode(pp, "TSParameterProperty");
  7987. }
  7988. if (decorators.length) {
  7989. left.decorators = decorators;
  7990. }
  7991. return elt;
  7992. }
  7993. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  7994. if (this.match(14)) {
  7995. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  7996. }
  7997. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  7998. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  7999. this.finishNode(node, bodilessType);
  8000. return;
  8001. }
  8002. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8003. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  8004. node
  8005. });
  8006. if (node.declare) {
  8007. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8008. return;
  8009. }
  8010. }
  8011. super.parseFunctionBodyAndFinish(node, type, isMethod);
  8012. }
  8013. registerFunctionStatementId(node) {
  8014. if (!node.body && node.id) {
  8015. this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
  8016. } else {
  8017. super.registerFunctionStatementId(...arguments);
  8018. }
  8019. }
  8020. tsCheckForInvalidTypeCasts(items) {
  8021. items.forEach(node => {
  8022. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8023. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  8024. node: node.typeAnnotation
  8025. });
  8026. }
  8027. });
  8028. }
  8029. toReferencedList(exprList, isInParens) {
  8030. this.tsCheckForInvalidTypeCasts(exprList);
  8031. return exprList;
  8032. }
  8033. parseArrayLike(...args) {
  8034. const node = super.parseArrayLike(...args);
  8035. if (node.type === "ArrayExpression") {
  8036. this.tsCheckForInvalidTypeCasts(node.elements);
  8037. }
  8038. return node;
  8039. }
  8040. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8041. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8042. this.state.canStartJSXElement = false;
  8043. this.next();
  8044. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  8045. nonNullExpression.expression = base;
  8046. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8047. }
  8048. let isOptionalCall = false;
  8049. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8050. if (noCalls) {
  8051. state.stop = true;
  8052. return base;
  8053. }
  8054. state.optionalChainMember = isOptionalCall = true;
  8055. this.next();
  8056. }
  8057. if (this.match(45)) {
  8058. let missingParenErrorLoc;
  8059. const result = this.tsTryParseAndCatch(() => {
  8060. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8061. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  8062. if (asyncArrowFn) {
  8063. return asyncArrowFn;
  8064. }
  8065. }
  8066. const node = this.startNodeAt(startPos, startLoc);
  8067. node.callee = base;
  8068. const typeArguments = this.tsParseTypeArguments();
  8069. if (typeArguments) {
  8070. if (isOptionalCall && !this.match(10)) {
  8071. missingParenErrorLoc = this.state.curPosition();
  8072. this.unexpected();
  8073. }
  8074. if (!noCalls && this.eat(10)) {
  8075. node.arguments = this.parseCallExpressionArguments(11, false);
  8076. this.tsCheckForInvalidTypeCasts(node.arguments);
  8077. node.typeParameters = typeArguments;
  8078. if (state.optionalChainMember) {
  8079. node.optional = isOptionalCall;
  8080. }
  8081. return this.finishCallExpression(node, state.optionalChainMember);
  8082. } else if (tokenIsTemplate(this.state.type)) {
  8083. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8084. result.typeParameters = typeArguments;
  8085. return result;
  8086. }
  8087. }
  8088. this.unexpected();
  8089. });
  8090. if (missingParenErrorLoc) {
  8091. this.unexpected(missingParenErrorLoc, 10);
  8092. }
  8093. if (result) return result;
  8094. }
  8095. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  8096. }
  8097. parseNewArguments(node) {
  8098. if (this.match(45)) {
  8099. const typeParameters = this.tsTryParseAndCatch(() => {
  8100. const args = this.tsParseTypeArguments();
  8101. if (!this.match(10)) this.unexpected();
  8102. return args;
  8103. });
  8104. if (typeParameters) {
  8105. node.typeParameters = typeParameters;
  8106. }
  8107. }
  8108. super.parseNewArguments(node);
  8109. }
  8110. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8111. if (tokenOperatorPrecedence(54) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual(89)) {
  8112. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8113. node.expression = left;
  8114. const _const = this.tsTryNextParseConstantContext();
  8115. if (_const) {
  8116. node.typeAnnotation = _const;
  8117. } else {
  8118. node.typeAnnotation = this.tsNextThenParseType();
  8119. }
  8120. this.finishNode(node, "TSAsExpression");
  8121. this.reScan_lt_gt();
  8122. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8123. }
  8124. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  8125. }
  8126. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  8127. checkDuplicateExports() {}
  8128. parseImport(node) {
  8129. node.importKind = "value";
  8130. if (tokenIsIdentifier(this.state.type) || this.match(51) || this.match(5)) {
  8131. let ahead = this.lookahead();
  8132. if (this.isContextual(122) && ahead.type !== 12 && ahead.type !== 93 && ahead.type !== 29) {
  8133. node.importKind = "type";
  8134. this.next();
  8135. ahead = this.lookahead();
  8136. }
  8137. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  8138. return this.tsParseImportEqualsDeclaration(node);
  8139. }
  8140. }
  8141. const importNode = super.parseImport(node);
  8142. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  8143. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  8144. node: importNode
  8145. });
  8146. }
  8147. return importNode;
  8148. }
  8149. parseExport(node) {
  8150. if (this.match(79)) {
  8151. this.next();
  8152. if (this.isContextual(122) && this.lookaheadCharCode() !== 61) {
  8153. node.importKind = "type";
  8154. this.next();
  8155. } else {
  8156. node.importKind = "value";
  8157. }
  8158. return this.tsParseImportEqualsDeclaration(node, true);
  8159. } else if (this.eat(29)) {
  8160. const assign = node;
  8161. assign.expression = this.parseExpression();
  8162. this.semicolon();
  8163. return this.finishNode(assign, "TSExportAssignment");
  8164. } else if (this.eatContextual(89)) {
  8165. const decl = node;
  8166. this.expectContextual(120);
  8167. decl.id = this.parseIdentifier();
  8168. this.semicolon();
  8169. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  8170. } else {
  8171. if (this.isContextual(122) && this.lookahead().type === 5) {
  8172. this.next();
  8173. node.exportKind = "type";
  8174. } else {
  8175. node.exportKind = "value";
  8176. }
  8177. return super.parseExport(node);
  8178. }
  8179. }
  8180. isAbstractClass() {
  8181. return this.isContextual(116) && this.lookahead().type === 76;
  8182. }
  8183. parseExportDefaultExpression() {
  8184. if (this.isAbstractClass()) {
  8185. const cls = this.startNode();
  8186. this.next();
  8187. cls.abstract = true;
  8188. this.parseClass(cls, true, true);
  8189. return cls;
  8190. }
  8191. if (this.match(121)) {
  8192. const interfaceNode = this.startNode();
  8193. this.next();
  8194. const result = this.tsParseInterfaceDeclaration(interfaceNode);
  8195. if (result) return result;
  8196. }
  8197. return super.parseExportDefaultExpression();
  8198. }
  8199. parseStatementContent(context, topLevel) {
  8200. if (this.state.type === 71) {
  8201. const ahead = this.lookahead();
  8202. if (ahead.type === 118) {
  8203. const node = this.startNode();
  8204. this.next();
  8205. this.expectContextual(118);
  8206. return this.tsParseEnumDeclaration(node, true);
  8207. }
  8208. }
  8209. return super.parseStatementContent(context, topLevel);
  8210. }
  8211. parseAccessModifier() {
  8212. return this.tsParseModifier(["public", "protected", "private"]);
  8213. }
  8214. tsHasSomeModifiers(member, modifiers) {
  8215. return modifiers.some(modifier => {
  8216. if (tsIsAccessModifier(modifier)) {
  8217. return member.accessibility === modifier;
  8218. }
  8219. return !!member[modifier];
  8220. });
  8221. }
  8222. tsIsStartOfStaticBlocks() {
  8223. return this.isContextual(100) && this.lookaheadCharCode() === 123;
  8224. }
  8225. parseClassMember(classBody, member, state) {
  8226. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  8227. this.tsParseModifiers(member, modifiers, undefined, undefined, true);
  8228. const callParseClassMemberWithIsStatic = () => {
  8229. if (this.tsIsStartOfStaticBlocks()) {
  8230. this.next();
  8231. this.next();
  8232. if (this.tsHasSomeModifiers(member, modifiers)) {
  8233. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  8234. at: this.state.curPosition()
  8235. });
  8236. }
  8237. this.parseClassStaticBlock(classBody, member);
  8238. } else {
  8239. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  8240. }
  8241. };
  8242. if (member.declare) {
  8243. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  8244. } else {
  8245. callParseClassMemberWithIsStatic();
  8246. }
  8247. }
  8248. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  8249. const idx = this.tsTryParseIndexSignature(member);
  8250. if (idx) {
  8251. classBody.body.push(idx);
  8252. if (member.abstract) {
  8253. this.raise(TSErrors.IndexSignatureHasAbstract, {
  8254. node: member
  8255. });
  8256. }
  8257. if (member.accessibility) {
  8258. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  8259. node: member
  8260. }, member.accessibility);
  8261. }
  8262. if (member.declare) {
  8263. this.raise(TSErrors.IndexSignatureHasDeclare, {
  8264. node: member
  8265. });
  8266. }
  8267. if (member.override) {
  8268. this.raise(TSErrors.IndexSignatureHasOverride, {
  8269. node: member
  8270. });
  8271. }
  8272. return;
  8273. }
  8274. if (!this.state.inAbstractClass && member.abstract) {
  8275. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  8276. node: member
  8277. });
  8278. }
  8279. if (member.override) {
  8280. if (!state.hadSuperClass) {
  8281. this.raise(TSErrors.OverrideNotInSubClass, {
  8282. node: member
  8283. });
  8284. }
  8285. }
  8286. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  8287. }
  8288. parsePostMemberNameModifiers(methodOrProp) {
  8289. const optional = this.eat(17);
  8290. if (optional) methodOrProp.optional = true;
  8291. if (methodOrProp.readonly && this.match(10)) {
  8292. this.raise(TSErrors.ClassMethodHasReadonly, {
  8293. node: methodOrProp
  8294. });
  8295. }
  8296. if (methodOrProp.declare && this.match(10)) {
  8297. this.raise(TSErrors.ClassMethodHasDeclare, {
  8298. node: methodOrProp
  8299. });
  8300. }
  8301. }
  8302. parseExpressionStatement(node, expr) {
  8303. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  8304. return decl || super.parseExpressionStatement(node, expr);
  8305. }
  8306. shouldParseExportDeclaration() {
  8307. if (this.tsIsDeclarationStart()) return true;
  8308. return super.shouldParseExportDeclaration();
  8309. }
  8310. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8311. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  8312. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8313. }
  8314. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  8315. if (!result.node) {
  8316. if (result.error) {
  8317. super.setOptionalParametersError(refExpressionErrors, result.error);
  8318. }
  8319. return expr;
  8320. }
  8321. if (result.error) this.state = result.failState;
  8322. return result.node;
  8323. }
  8324. parseParenItem(node, startPos, startLoc) {
  8325. node = super.parseParenItem(node, startPos, startLoc);
  8326. if (this.eat(17)) {
  8327. node.optional = true;
  8328. this.resetEndLocation(node);
  8329. }
  8330. if (this.match(14)) {
  8331. const typeCastNode = this.startNodeAt(startPos, startLoc);
  8332. typeCastNode.expression = node;
  8333. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  8334. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  8335. }
  8336. return node;
  8337. }
  8338. parseExportDeclaration(node) {
  8339. const startPos = this.state.start;
  8340. const startLoc = this.state.startLoc;
  8341. const isDeclare = this.eatContextual(117);
  8342. if (isDeclare && (this.isContextual(117) || !this.shouldParseExportDeclaration())) {
  8343. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  8344. at: this.state.startLoc
  8345. });
  8346. }
  8347. let declaration;
  8348. if (tokenIsIdentifier(this.state.type)) {
  8349. declaration = this.tsTryParseExportDeclaration();
  8350. }
  8351. if (!declaration) {
  8352. declaration = super.parseExportDeclaration(node);
  8353. }
  8354. if (declaration && (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare)) {
  8355. node.exportKind = "type";
  8356. }
  8357. if (declaration && isDeclare) {
  8358. this.resetStartLocation(declaration, startPos, startLoc);
  8359. declaration.declare = true;
  8360. }
  8361. return declaration;
  8362. }
  8363. parseClassId(node, isStatement, optionalId) {
  8364. if ((!isStatement || optionalId) && this.isContextual(106)) {
  8365. return;
  8366. }
  8367. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  8368. const typeParameters = this.tsTryParseTypeParameters();
  8369. if (typeParameters) node.typeParameters = typeParameters;
  8370. }
  8371. parseClassPropertyAnnotation(node) {
  8372. if (!node.optional && this.eat(35)) {
  8373. node.definite = true;
  8374. }
  8375. const type = this.tsTryParseTypeAnnotation();
  8376. if (type) node.typeAnnotation = type;
  8377. }
  8378. parseClassProperty(node) {
  8379. this.parseClassPropertyAnnotation(node);
  8380. if (this.state.isAmbientContext && this.match(29)) {
  8381. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  8382. at: this.state.startLoc
  8383. });
  8384. }
  8385. if (node.abstract && this.match(29)) {
  8386. const {
  8387. key
  8388. } = node;
  8389. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  8390. at: this.state.startLoc
  8391. }, key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  8392. }
  8393. return super.parseClassProperty(node);
  8394. }
  8395. parseClassPrivateProperty(node) {
  8396. if (node.abstract) {
  8397. this.raise(TSErrors.PrivateElementHasAbstract, {
  8398. node
  8399. });
  8400. }
  8401. if (node.accessibility) {
  8402. this.raise(TSErrors.PrivateElementHasAccessibility, {
  8403. node
  8404. }, node.accessibility);
  8405. }
  8406. this.parseClassPropertyAnnotation(node);
  8407. return super.parseClassPrivateProperty(node);
  8408. }
  8409. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  8410. const typeParameters = this.tsTryParseTypeParameters();
  8411. if (typeParameters && isConstructor) {
  8412. this.raise(TSErrors.ConstructorHasTypeParameters, {
  8413. node: typeParameters
  8414. });
  8415. }
  8416. if (method.declare && (method.kind === "get" || method.kind === "set")) {
  8417. this.raise(TSErrors.DeclareAccessor, {
  8418. node: method
  8419. }, method.kind);
  8420. }
  8421. if (typeParameters) method.typeParameters = typeParameters;
  8422. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  8423. }
  8424. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  8425. const typeParameters = this.tsTryParseTypeParameters();
  8426. if (typeParameters) method.typeParameters = typeParameters;
  8427. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  8428. }
  8429. declareClassPrivateMethodInScope(node, kind) {
  8430. if (node.type === "TSDeclareMethod") return;
  8431. if (node.type === "MethodDefinition" && !node.value.body) return;
  8432. super.declareClassPrivateMethodInScope(node, kind);
  8433. }
  8434. parseClassSuper(node) {
  8435. super.parseClassSuper(node);
  8436. if (node.superClass && this.match(45)) {
  8437. node.superTypeParameters = this.tsParseTypeArguments();
  8438. }
  8439. if (this.eatContextual(106)) {
  8440. node.implements = this.tsParseHeritageClause("implements");
  8441. }
  8442. }
  8443. parseObjPropValue(prop, ...args) {
  8444. const typeParameters = this.tsTryParseTypeParameters();
  8445. if (typeParameters) prop.typeParameters = typeParameters;
  8446. super.parseObjPropValue(prop, ...args);
  8447. }
  8448. parseFunctionParams(node, allowModifiers) {
  8449. const typeParameters = this.tsTryParseTypeParameters();
  8450. if (typeParameters) node.typeParameters = typeParameters;
  8451. super.parseFunctionParams(node, allowModifiers);
  8452. }
  8453. parseVarId(decl, kind) {
  8454. super.parseVarId(decl, kind);
  8455. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  8456. decl.definite = true;
  8457. }
  8458. const type = this.tsTryParseTypeAnnotation();
  8459. if (type) {
  8460. decl.id.typeAnnotation = type;
  8461. this.resetEndLocation(decl.id);
  8462. }
  8463. }
  8464. parseAsyncArrowFromCallExpression(node, call) {
  8465. if (this.match(14)) {
  8466. node.returnType = this.tsParseTypeAnnotation();
  8467. }
  8468. return super.parseAsyncArrowFromCallExpression(node, call);
  8469. }
  8470. parseMaybeAssign(...args) {
  8471. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  8472. let state;
  8473. let jsx;
  8474. let typeCast;
  8475. if (this.hasPlugin("jsx") && (this.match(134) || this.match(45))) {
  8476. state = this.state.clone();
  8477. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8478. if (!jsx.error) return jsx.node;
  8479. const {
  8480. context
  8481. } = this.state;
  8482. const currentContext = context[context.length - 1];
  8483. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  8484. context.pop();
  8485. }
  8486. }
  8487. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(45)) {
  8488. return super.parseMaybeAssign(...args);
  8489. }
  8490. let typeParameters;
  8491. state = state || this.state.clone();
  8492. const arrow = this.tryParse(abort => {
  8493. var _expr$extra, _typeParameters;
  8494. typeParameters = this.tsParseTypeParameters();
  8495. const expr = super.parseMaybeAssign(...args);
  8496. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  8497. abort();
  8498. }
  8499. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  8500. this.resetStartLocationFromNode(expr, typeParameters);
  8501. }
  8502. expr.typeParameters = typeParameters;
  8503. return expr;
  8504. }, state);
  8505. if (!arrow.error && !arrow.aborted) {
  8506. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8507. return arrow.node;
  8508. }
  8509. if (!jsx) {
  8510. assert(!this.hasPlugin("jsx"));
  8511. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  8512. if (!typeCast.error) return typeCast.node;
  8513. }
  8514. if ((_jsx2 = jsx) != null && _jsx2.node) {
  8515. this.state = jsx.failState;
  8516. return jsx.node;
  8517. }
  8518. if (arrow.node) {
  8519. this.state = arrow.failState;
  8520. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  8521. return arrow.node;
  8522. }
  8523. if ((_typeCast = typeCast) != null && _typeCast.node) {
  8524. this.state = typeCast.failState;
  8525. return typeCast.node;
  8526. }
  8527. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  8528. if (arrow.thrown) throw arrow.error;
  8529. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  8530. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  8531. }
  8532. reportReservedArrowTypeParam(node) {
  8533. var _node$extra;
  8534. if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8535. this.raise(TSErrors.ReservedArrowTypeParam, {
  8536. node
  8537. });
  8538. }
  8539. }
  8540. parseMaybeUnary(refExpressionErrors) {
  8541. if (!this.hasPlugin("jsx") && this.match(45)) {
  8542. return this.tsParseTypeAssertion();
  8543. } else {
  8544. return super.parseMaybeUnary(refExpressionErrors);
  8545. }
  8546. }
  8547. parseArrow(node) {
  8548. if (this.match(14)) {
  8549. const result = this.tryParse(abort => {
  8550. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8551. if (this.canInsertSemicolon() || !this.match(19)) abort();
  8552. return returnType;
  8553. });
  8554. if (result.aborted) return;
  8555. if (!result.thrown) {
  8556. if (result.error) this.state = result.failState;
  8557. node.returnType = result.node;
  8558. }
  8559. }
  8560. return super.parseArrow(node);
  8561. }
  8562. parseAssignableListItemTypes(param) {
  8563. if (this.eat(17)) {
  8564. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  8565. this.raise(TSErrors.PatternIsOptional, {
  8566. node: param
  8567. });
  8568. }
  8569. param.optional = true;
  8570. }
  8571. const type = this.tsTryParseTypeAnnotation();
  8572. if (type) param.typeAnnotation = type;
  8573. this.resetEndLocation(param);
  8574. return param;
  8575. }
  8576. isAssignable(node, isBinding) {
  8577. switch (node.type) {
  8578. case "TSTypeCastExpression":
  8579. return this.isAssignable(node.expression, isBinding);
  8580. case "TSParameterProperty":
  8581. return true;
  8582. default:
  8583. return super.isAssignable(node, isBinding);
  8584. }
  8585. }
  8586. toAssignable(node, isLHS = false) {
  8587. switch (node.type) {
  8588. case "TSTypeCastExpression":
  8589. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  8590. case "TSParameterProperty":
  8591. return super.toAssignable(node, isLHS);
  8592. case "ParenthesizedExpression":
  8593. return this.toAssignableParenthesizedExpression(node, isLHS);
  8594. case "TSAsExpression":
  8595. case "TSNonNullExpression":
  8596. case "TSTypeAssertion":
  8597. node.expression = this.toAssignable(node.expression, isLHS);
  8598. return node;
  8599. default:
  8600. return super.toAssignable(node, isLHS);
  8601. }
  8602. }
  8603. toAssignableParenthesizedExpression(node, isLHS) {
  8604. switch (node.expression.type) {
  8605. case "TSAsExpression":
  8606. case "TSNonNullExpression":
  8607. case "TSTypeAssertion":
  8608. case "ParenthesizedExpression":
  8609. node.expression = this.toAssignable(node.expression, isLHS);
  8610. return node;
  8611. default:
  8612. return super.toAssignable(node, isLHS);
  8613. }
  8614. }
  8615. checkLVal(expr, contextDescription, ...args) {
  8616. var _expr$extra2;
  8617. switch (expr.type) {
  8618. case "TSTypeCastExpression":
  8619. return;
  8620. case "TSParameterProperty":
  8621. this.checkLVal(expr.parameter, "parameter property", ...args);
  8622. return;
  8623. case "TSAsExpression":
  8624. case "TSTypeAssertion":
  8625. if (!args[0] && contextDescription !== "parenthesized expression" && !((_expr$extra2 = expr.extra) != null && _expr$extra2.parenthesized)) {
  8626. this.raise(ErrorMessages.InvalidLhs, {
  8627. node: expr
  8628. }, contextDescription);
  8629. break;
  8630. }
  8631. this.checkLVal(expr.expression, "parenthesized expression", ...args);
  8632. return;
  8633. case "TSNonNullExpression":
  8634. this.checkLVal(expr.expression, contextDescription, ...args);
  8635. return;
  8636. default:
  8637. super.checkLVal(expr, contextDescription, ...args);
  8638. return;
  8639. }
  8640. }
  8641. parseBindingAtom() {
  8642. switch (this.state.type) {
  8643. case 74:
  8644. return this.parseIdentifier(true);
  8645. default:
  8646. return super.parseBindingAtom();
  8647. }
  8648. }
  8649. parseMaybeDecoratorArguments(expr) {
  8650. if (this.match(45)) {
  8651. const typeArguments = this.tsParseTypeArguments();
  8652. if (this.match(10)) {
  8653. const call = super.parseMaybeDecoratorArguments(expr);
  8654. call.typeParameters = typeArguments;
  8655. return call;
  8656. }
  8657. this.unexpected(null, 10);
  8658. }
  8659. return super.parseMaybeDecoratorArguments(expr);
  8660. }
  8661. checkCommaAfterRest(close) {
  8662. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  8663. this.next();
  8664. return false;
  8665. } else {
  8666. return super.checkCommaAfterRest(close);
  8667. }
  8668. }
  8669. isClassMethod() {
  8670. return this.match(45) || super.isClassMethod();
  8671. }
  8672. isClassProperty() {
  8673. return this.match(35) || this.match(14) || super.isClassProperty();
  8674. }
  8675. parseMaybeDefault(...args) {
  8676. const node = super.parseMaybeDefault(...args);
  8677. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  8678. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  8679. node: node.typeAnnotation
  8680. });
  8681. }
  8682. return node;
  8683. }
  8684. getTokenFromCode(code) {
  8685. if (this.state.inType) {
  8686. if (code === 62) {
  8687. return this.finishOp(46, 1);
  8688. }
  8689. if (code === 60) {
  8690. return this.finishOp(45, 1);
  8691. }
  8692. }
  8693. return super.getTokenFromCode(code);
  8694. }
  8695. reScan_lt_gt() {
  8696. const {
  8697. type
  8698. } = this.state;
  8699. if (type === 45) {
  8700. this.state.pos -= 1;
  8701. this.readToken_lt();
  8702. } else if (type === 46) {
  8703. this.state.pos -= 1;
  8704. this.readToken_gt();
  8705. }
  8706. }
  8707. toAssignableList(exprList) {
  8708. for (let i = 0; i < exprList.length; i++) {
  8709. const expr = exprList[i];
  8710. if (!expr) continue;
  8711. switch (expr.type) {
  8712. case "TSTypeCastExpression":
  8713. exprList[i] = this.typeCastToParameter(expr);
  8714. break;
  8715. case "TSAsExpression":
  8716. case "TSTypeAssertion":
  8717. if (!this.state.maybeInArrowParameters) {
  8718. exprList[i] = this.typeCastToParameter(expr);
  8719. } else {
  8720. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  8721. node: expr
  8722. });
  8723. }
  8724. break;
  8725. }
  8726. }
  8727. return super.toAssignableList(...arguments);
  8728. }
  8729. typeCastToParameter(node) {
  8730. node.expression.typeAnnotation = node.typeAnnotation;
  8731. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  8732. return node.expression;
  8733. }
  8734. shouldParseArrow(params) {
  8735. if (this.match(14)) {
  8736. return params.every(expr => this.isAssignable(expr, true));
  8737. }
  8738. return super.shouldParseArrow(params);
  8739. }
  8740. shouldParseAsyncArrow() {
  8741. return this.match(14) || super.shouldParseAsyncArrow();
  8742. }
  8743. canHaveLeadingDecorator() {
  8744. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  8745. }
  8746. jsxParseOpeningElementAfterName(node) {
  8747. if (this.match(45)) {
  8748. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  8749. if (typeArguments) node.typeParameters = typeArguments;
  8750. }
  8751. return super.jsxParseOpeningElementAfterName(node);
  8752. }
  8753. getGetterSetterExpectedParamCount(method) {
  8754. const baseCount = super.getGetterSetterExpectedParamCount(method);
  8755. const params = this.getObjectOrClassMethodParams(method);
  8756. const firstParam = params[0];
  8757. const hasContextParam = firstParam && this.isThisParam(firstParam);
  8758. return hasContextParam ? baseCount + 1 : baseCount;
  8759. }
  8760. parseCatchClauseParam() {
  8761. const param = super.parseCatchClauseParam();
  8762. const type = this.tsTryParseTypeAnnotation();
  8763. if (type) {
  8764. param.typeAnnotation = type;
  8765. this.resetEndLocation(param);
  8766. }
  8767. return param;
  8768. }
  8769. tsInAmbientContext(cb) {
  8770. const oldIsAmbientContext = this.state.isAmbientContext;
  8771. this.state.isAmbientContext = true;
  8772. try {
  8773. return cb();
  8774. } finally {
  8775. this.state.isAmbientContext = oldIsAmbientContext;
  8776. }
  8777. }
  8778. parseClass(node, ...args) {
  8779. const oldInAbstractClass = this.state.inAbstractClass;
  8780. this.state.inAbstractClass = !!node.abstract;
  8781. try {
  8782. return super.parseClass(node, ...args);
  8783. } finally {
  8784. this.state.inAbstractClass = oldInAbstractClass;
  8785. }
  8786. }
  8787. tsParseAbstractDeclaration(node) {
  8788. if (this.match(76)) {
  8789. node.abstract = true;
  8790. return this.parseClass(node, true, false);
  8791. } else if (this.isContextual(121)) {
  8792. if (!this.hasFollowingLineBreak()) {
  8793. node.abstract = true;
  8794. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  8795. node
  8796. });
  8797. this.next();
  8798. return this.tsParseInterfaceDeclaration(node);
  8799. }
  8800. } else {
  8801. this.unexpected(null, 76);
  8802. }
  8803. }
  8804. parseMethod(...args) {
  8805. const method = super.parseMethod(...args);
  8806. if (method.abstract) {
  8807. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  8808. if (hasBody) {
  8809. const {
  8810. key
  8811. } = method;
  8812. this.raise(TSErrors.AbstractMethodHasImplementation, {
  8813. node: method
  8814. }, key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  8815. }
  8816. }
  8817. return method;
  8818. }
  8819. tsParseTypeParameterName() {
  8820. const typeName = this.parseIdentifier();
  8821. return typeName.name;
  8822. }
  8823. shouldParseAsAmbientContext() {
  8824. return !!this.getPluginOption("typescript", "dts");
  8825. }
  8826. parse() {
  8827. if (this.shouldParseAsAmbientContext()) {
  8828. this.state.isAmbientContext = true;
  8829. }
  8830. return super.parse();
  8831. }
  8832. getExpression() {
  8833. if (this.shouldParseAsAmbientContext()) {
  8834. this.state.isAmbientContext = true;
  8835. }
  8836. return super.getExpression();
  8837. }
  8838. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  8839. if (!isString && isMaybeTypeOnly) {
  8840. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  8841. return this.finishNode(node, "ExportSpecifier");
  8842. }
  8843. node.exportKind = "value";
  8844. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  8845. }
  8846. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  8847. if (!importedIsString && isMaybeTypeOnly) {
  8848. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  8849. return this.finishNode(specifier, "ImportSpecifier");
  8850. }
  8851. specifier.importKind = "value";
  8852. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly);
  8853. }
  8854. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  8855. const leftOfAsKey = isImport ? "imported" : "local";
  8856. const rightOfAsKey = isImport ? "local" : "exported";
  8857. let leftOfAs = node[leftOfAsKey];
  8858. let rightOfAs;
  8859. let hasTypeSpecifier = false;
  8860. let canParseAsKeyword = true;
  8861. const loc = leftOfAs.loc.start;
  8862. if (this.isContextual(89)) {
  8863. const firstAs = this.parseIdentifier();
  8864. if (this.isContextual(89)) {
  8865. const secondAs = this.parseIdentifier();
  8866. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  8867. hasTypeSpecifier = true;
  8868. leftOfAs = firstAs;
  8869. rightOfAs = this.parseIdentifier();
  8870. canParseAsKeyword = false;
  8871. } else {
  8872. rightOfAs = secondAs;
  8873. canParseAsKeyword = false;
  8874. }
  8875. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  8876. canParseAsKeyword = false;
  8877. rightOfAs = this.parseIdentifier();
  8878. } else {
  8879. hasTypeSpecifier = true;
  8880. leftOfAs = firstAs;
  8881. }
  8882. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  8883. hasTypeSpecifier = true;
  8884. leftOfAs = this.parseIdentifier();
  8885. }
  8886. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  8887. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  8888. at: loc
  8889. });
  8890. }
  8891. node[leftOfAsKey] = leftOfAs;
  8892. node[rightOfAsKey] = rightOfAs;
  8893. const kindKey = isImport ? "importKind" : "exportKind";
  8894. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  8895. if (canParseAsKeyword && this.eatContextual(89)) {
  8896. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  8897. }
  8898. if (!node[rightOfAsKey]) {
  8899. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  8900. }
  8901. if (isImport) {
  8902. this.checkLVal(node[rightOfAsKey], "import specifier", BIND_LEXICAL);
  8903. }
  8904. }
  8905. });
  8906. const PlaceholderErrors = makeErrorTemplates({
  8907. ClassNameIsRequired: "A class name is required."
  8908. }, ErrorCodes.SyntaxError, "placeholders");
  8909. var placeholders = (superClass => class extends superClass {
  8910. parsePlaceholder(expectedNode) {
  8911. if (this.match(136)) {
  8912. const node = this.startNode();
  8913. this.next();
  8914. this.assertNoSpace("Unexpected space in placeholder.");
  8915. node.name = super.parseIdentifier(true);
  8916. this.assertNoSpace("Unexpected space in placeholder.");
  8917. this.expect(136);
  8918. return this.finishPlaceholder(node, expectedNode);
  8919. }
  8920. }
  8921. finishPlaceholder(node, expectedNode) {
  8922. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  8923. node.expectedNode = expectedNode;
  8924. return isFinished ? node : this.finishNode(node, "Placeholder");
  8925. }
  8926. getTokenFromCode(code) {
  8927. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  8928. return this.finishOp(136, 2);
  8929. }
  8930. return super.getTokenFromCode(...arguments);
  8931. }
  8932. parseExprAtom() {
  8933. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  8934. }
  8935. parseIdentifier() {
  8936. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  8937. }
  8938. checkReservedWord(word) {
  8939. if (word !== undefined) super.checkReservedWord(...arguments);
  8940. }
  8941. parseBindingAtom() {
  8942. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  8943. }
  8944. checkLVal(expr) {
  8945. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  8946. }
  8947. toAssignable(node) {
  8948. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  8949. node.expectedNode = "Pattern";
  8950. return node;
  8951. }
  8952. return super.toAssignable(...arguments);
  8953. }
  8954. isLet(context) {
  8955. if (super.isLet(context)) {
  8956. return true;
  8957. }
  8958. if (!this.isContextual(95)) {
  8959. return false;
  8960. }
  8961. if (context) return false;
  8962. const nextToken = this.lookahead();
  8963. if (nextToken.type === 136) {
  8964. return true;
  8965. }
  8966. return false;
  8967. }
  8968. verifyBreakContinue(node) {
  8969. if (node.label && node.label.type === "Placeholder") return;
  8970. super.verifyBreakContinue(...arguments);
  8971. }
  8972. parseExpressionStatement(node, expr) {
  8973. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  8974. return super.parseExpressionStatement(...arguments);
  8975. }
  8976. if (this.match(14)) {
  8977. const stmt = node;
  8978. stmt.label = this.finishPlaceholder(expr, "Identifier");
  8979. this.next();
  8980. stmt.body = this.parseStatement("label");
  8981. return this.finishNode(stmt, "LabeledStatement");
  8982. }
  8983. this.semicolon();
  8984. node.name = expr.name;
  8985. return this.finishPlaceholder(node, "Statement");
  8986. }
  8987. parseBlock() {
  8988. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  8989. }
  8990. parseFunctionId() {
  8991. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  8992. }
  8993. parseClass(node, isStatement, optionalId) {
  8994. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  8995. this.next();
  8996. this.takeDecorators(node);
  8997. const oldStrict = this.state.strict;
  8998. const placeholder = this.parsePlaceholder("Identifier");
  8999. if (placeholder) {
  9000. if (this.match(77) || this.match(136) || this.match(5)) {
  9001. node.id = placeholder;
  9002. } else if (optionalId || !isStatement) {
  9003. node.id = null;
  9004. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  9005. return this.finishNode(node, type);
  9006. } else {
  9007. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  9008. at: this.state.startLoc
  9009. });
  9010. }
  9011. } else {
  9012. this.parseClassId(node, isStatement, optionalId);
  9013. }
  9014. this.parseClassSuper(node);
  9015. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  9016. return this.finishNode(node, type);
  9017. }
  9018. parseExport(node) {
  9019. const placeholder = this.parsePlaceholder("Identifier");
  9020. if (!placeholder) return super.parseExport(...arguments);
  9021. if (!this.isContextual(93) && !this.match(12)) {
  9022. node.specifiers = [];
  9023. node.source = null;
  9024. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  9025. return this.finishNode(node, "ExportNamedDeclaration");
  9026. }
  9027. this.expectPlugin("exportDefaultFrom");
  9028. const specifier = this.startNode();
  9029. specifier.exported = placeholder;
  9030. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9031. return super.parseExport(node);
  9032. }
  9033. isExportDefaultSpecifier() {
  9034. if (this.match(61)) {
  9035. const next = this.nextTokenStart();
  9036. if (this.isUnparsedContextual(next, "from")) {
  9037. if (this.input.startsWith(tokenLabelName(136), this.nextTokenStartSince(next + 4))) {
  9038. return true;
  9039. }
  9040. }
  9041. }
  9042. return super.isExportDefaultSpecifier();
  9043. }
  9044. maybeParseExportDefaultSpecifier(node) {
  9045. if (node.specifiers && node.specifiers.length > 0) {
  9046. return true;
  9047. }
  9048. return super.maybeParseExportDefaultSpecifier(...arguments);
  9049. }
  9050. checkExport(node) {
  9051. const {
  9052. specifiers
  9053. } = node;
  9054. if (specifiers != null && specifiers.length) {
  9055. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  9056. }
  9057. super.checkExport(node);
  9058. node.specifiers = specifiers;
  9059. }
  9060. parseImport(node) {
  9061. const placeholder = this.parsePlaceholder("Identifier");
  9062. if (!placeholder) return super.parseImport(...arguments);
  9063. node.specifiers = [];
  9064. if (!this.isContextual(93) && !this.match(12)) {
  9065. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  9066. this.semicolon();
  9067. return this.finishNode(node, "ImportDeclaration");
  9068. }
  9069. const specifier = this.startNodeAtNode(placeholder);
  9070. specifier.local = placeholder;
  9071. this.finishNode(specifier, "ImportDefaultSpecifier");
  9072. node.specifiers.push(specifier);
  9073. if (this.eat(12)) {
  9074. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  9075. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  9076. }
  9077. this.expectContextual(93);
  9078. node.source = this.parseImportSource();
  9079. this.semicolon();
  9080. return this.finishNode(node, "ImportDeclaration");
  9081. }
  9082. parseImportSource() {
  9083. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  9084. }
  9085. });
  9086. var v8intrinsic = (superClass => class extends superClass {
  9087. parseV8Intrinsic() {
  9088. if (this.match(50)) {
  9089. const v8IntrinsicStartLoc = this.state.startLoc;
  9090. const node = this.startNode();
  9091. this.next();
  9092. if (tokenIsIdentifier(this.state.type)) {
  9093. const name = this.parseIdentifierName(this.state.start);
  9094. const identifier = this.createIdentifier(node, name);
  9095. identifier.type = "V8IntrinsicIdentifier";
  9096. if (this.match(10)) {
  9097. return identifier;
  9098. }
  9099. }
  9100. this.unexpected(v8IntrinsicStartLoc);
  9101. }
  9102. }
  9103. parseExprAtom() {
  9104. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  9105. }
  9106. });
  9107. function hasPlugin(plugins, expectedConfig) {
  9108. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  9109. const expectedKeys = Object.keys(expectedOptions);
  9110. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  9111. return plugins.some(p => {
  9112. if (typeof p === "string") {
  9113. return expectedOptionsIsEmpty && p === expectedName;
  9114. } else {
  9115. const [pluginName, pluginOptions] = p;
  9116. if (pluginName !== expectedName) {
  9117. return false;
  9118. }
  9119. for (const key of expectedKeys) {
  9120. if (pluginOptions[key] !== expectedOptions[key]) {
  9121. return false;
  9122. }
  9123. }
  9124. return true;
  9125. }
  9126. });
  9127. }
  9128. function getPluginOption(plugins, name, option) {
  9129. const plugin = plugins.find(plugin => {
  9130. if (Array.isArray(plugin)) {
  9131. return plugin[0] === name;
  9132. } else {
  9133. return plugin === name;
  9134. }
  9135. });
  9136. if (plugin && Array.isArray(plugin)) {
  9137. return plugin[1][option];
  9138. }
  9139. return null;
  9140. }
  9141. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  9142. const TOPIC_TOKENS = ["^", "%", "#"];
  9143. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  9144. function validatePlugins(plugins) {
  9145. if (hasPlugin(plugins, "decorators")) {
  9146. if (hasPlugin(plugins, "decorators-legacy")) {
  9147. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  9148. }
  9149. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  9150. if (decoratorsBeforeExport == null) {
  9151. 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'.");
  9152. } else if (typeof decoratorsBeforeExport !== "boolean") {
  9153. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  9154. }
  9155. }
  9156. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  9157. throw new Error("Cannot combine flow and typescript plugins.");
  9158. }
  9159. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  9160. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  9161. }
  9162. if (hasPlugin(plugins, "pipelineOperator")) {
  9163. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  9164. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  9165. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  9166. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  9167. }
  9168. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  9169. syntaxType: "hash"
  9170. }]);
  9171. if (proposal === "hack") {
  9172. if (hasPlugin(plugins, "placeholders")) {
  9173. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  9174. }
  9175. if (hasPlugin(plugins, "v8intrinsic")) {
  9176. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  9177. }
  9178. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  9179. if (!TOPIC_TOKENS.includes(topicToken)) {
  9180. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  9181. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  9182. }
  9183. if (topicToken === "#" && tupleSyntaxIsHash) {
  9184. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9185. }
  9186. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  9187. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9188. }
  9189. }
  9190. if (hasPlugin(plugins, "moduleAttributes")) {
  9191. {
  9192. if (hasPlugin(plugins, "importAssertions")) {
  9193. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  9194. }
  9195. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  9196. if (moduleAttributesVerionPluginOption !== "may-2020") {
  9197. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  9198. }
  9199. }
  9200. }
  9201. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  9202. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  9203. }
  9204. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  9205. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  9206. error.missingPlugins = "doExpressions";
  9207. throw error;
  9208. }
  9209. }
  9210. const mixinPlugins = {
  9211. estree,
  9212. jsx,
  9213. flow,
  9214. typescript,
  9215. v8intrinsic,
  9216. placeholders
  9217. };
  9218. const mixinPluginNames = Object.keys(mixinPlugins);
  9219. const defaultOptions = {
  9220. sourceType: "script",
  9221. sourceFilename: undefined,
  9222. startColumn: 0,
  9223. startLine: 1,
  9224. allowAwaitOutsideFunction: false,
  9225. allowReturnOutsideFunction: false,
  9226. allowImportExportEverywhere: false,
  9227. allowSuperOutsideMethod: false,
  9228. allowUndeclaredExports: false,
  9229. plugins: [],
  9230. strictMode: null,
  9231. ranges: false,
  9232. tokens: false,
  9233. createParenthesizedExpressions: false,
  9234. errorRecovery: false,
  9235. attachComment: true
  9236. };
  9237. function getOptions(opts) {
  9238. const options = {};
  9239. for (const key of Object.keys(defaultOptions)) {
  9240. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  9241. }
  9242. return options;
  9243. }
  9244. const unwrapParenthesizedExpression = node => {
  9245. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  9246. };
  9247. class LValParser extends NodeUtils {
  9248. toAssignable(node, isLHS = false) {
  9249. var _node$extra, _node$extra3;
  9250. let parenthesized = undefined;
  9251. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  9252. parenthesized = unwrapParenthesizedExpression(node);
  9253. if (isLHS) {
  9254. if (parenthesized.type === "Identifier") {
  9255. this.expressionScope.recordParenthesizedIdentifierError(ErrorMessages.InvalidParenthesizedAssignment, node.loc.start);
  9256. } else if (parenthesized.type !== "MemberExpression") {
  9257. this.raise(ErrorMessages.InvalidParenthesizedAssignment, {
  9258. node
  9259. });
  9260. }
  9261. } else {
  9262. this.raise(ErrorMessages.InvalidParenthesizedAssignment, {
  9263. node
  9264. });
  9265. }
  9266. }
  9267. switch (node.type) {
  9268. case "Identifier":
  9269. case "ObjectPattern":
  9270. case "ArrayPattern":
  9271. case "AssignmentPattern":
  9272. case "RestElement":
  9273. break;
  9274. case "ObjectExpression":
  9275. node.type = "ObjectPattern";
  9276. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  9277. var _node$extra2;
  9278. const prop = node.properties[i];
  9279. const isLast = i === last;
  9280. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  9281. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  9282. this.raise(ErrorMessages.RestTrailingComma, {
  9283. at: node.extra.trailingCommaLoc
  9284. });
  9285. }
  9286. }
  9287. break;
  9288. case "ObjectProperty":
  9289. this.toAssignable(node.value, isLHS);
  9290. break;
  9291. case "SpreadElement":
  9292. {
  9293. this.checkToRestConversion(node);
  9294. node.type = "RestElement";
  9295. const arg = node.argument;
  9296. this.toAssignable(arg, isLHS);
  9297. break;
  9298. }
  9299. case "ArrayExpression":
  9300. node.type = "ArrayPattern";
  9301. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  9302. break;
  9303. case "AssignmentExpression":
  9304. if (node.operator !== "=") {
  9305. this.raise(ErrorMessages.MissingEqInAssignment, {
  9306. at: node.left.loc.end
  9307. });
  9308. }
  9309. node.type = "AssignmentPattern";
  9310. delete node.operator;
  9311. this.toAssignable(node.left, isLHS);
  9312. break;
  9313. case "ParenthesizedExpression":
  9314. this.toAssignable(parenthesized, isLHS);
  9315. break;
  9316. }
  9317. return node;
  9318. }
  9319. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  9320. if (prop.type === "ObjectMethod") {
  9321. this.raise(prop.kind === "get" || prop.kind === "set" ? ErrorMessages.PatternHasAccessor : ErrorMessages.PatternHasMethod, {
  9322. node: prop.key
  9323. });
  9324. } else if (prop.type === "SpreadElement" && !isLast) {
  9325. this.raise(ErrorMessages.RestTrailingComma, {
  9326. node: prop
  9327. });
  9328. } else {
  9329. this.toAssignable(prop, isLHS);
  9330. }
  9331. }
  9332. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9333. let end = exprList.length;
  9334. if (end) {
  9335. const last = exprList[end - 1];
  9336. if ((last == null ? void 0 : last.type) === "RestElement") {
  9337. --end;
  9338. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  9339. last.type = "RestElement";
  9340. let arg = last.argument;
  9341. this.toAssignable(arg, isLHS);
  9342. arg = unwrapParenthesizedExpression(arg);
  9343. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  9344. this.unexpected(arg.start);
  9345. }
  9346. if (trailingCommaLoc) {
  9347. this.raise(ErrorMessages.RestTrailingComma, {
  9348. at: trailingCommaLoc
  9349. });
  9350. }
  9351. --end;
  9352. }
  9353. }
  9354. for (let i = 0; i < end; i++) {
  9355. const elt = exprList[i];
  9356. if (elt) {
  9357. this.toAssignable(elt, isLHS);
  9358. if (elt.type === "RestElement") {
  9359. this.raise(ErrorMessages.RestTrailingComma, {
  9360. node: elt
  9361. });
  9362. }
  9363. }
  9364. }
  9365. return exprList;
  9366. }
  9367. isAssignable(node, isBinding) {
  9368. switch (node.type) {
  9369. case "Identifier":
  9370. case "ObjectPattern":
  9371. case "ArrayPattern":
  9372. case "AssignmentPattern":
  9373. case "RestElement":
  9374. return true;
  9375. case "ObjectExpression":
  9376. {
  9377. const last = node.properties.length - 1;
  9378. return node.properties.every((prop, i) => {
  9379. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  9380. });
  9381. }
  9382. case "ObjectProperty":
  9383. return this.isAssignable(node.value);
  9384. case "SpreadElement":
  9385. return this.isAssignable(node.argument);
  9386. case "ArrayExpression":
  9387. return node.elements.every(element => element === null || this.isAssignable(element));
  9388. case "AssignmentExpression":
  9389. return node.operator === "=";
  9390. case "ParenthesizedExpression":
  9391. return this.isAssignable(node.expression);
  9392. case "MemberExpression":
  9393. case "OptionalMemberExpression":
  9394. return !isBinding;
  9395. default:
  9396. return false;
  9397. }
  9398. }
  9399. toReferencedList(exprList, isParenthesizedExpr) {
  9400. return exprList;
  9401. }
  9402. toReferencedListDeep(exprList, isParenthesizedExpr) {
  9403. this.toReferencedList(exprList, isParenthesizedExpr);
  9404. for (const expr of exprList) {
  9405. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  9406. this.toReferencedListDeep(expr.elements);
  9407. }
  9408. }
  9409. }
  9410. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  9411. const node = this.startNode();
  9412. this.next();
  9413. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  9414. return this.finishNode(node, "SpreadElement");
  9415. }
  9416. parseRestBinding() {
  9417. const node = this.startNode();
  9418. this.next();
  9419. node.argument = this.parseBindingAtom();
  9420. return this.finishNode(node, "RestElement");
  9421. }
  9422. parseBindingAtom() {
  9423. switch (this.state.type) {
  9424. case 0:
  9425. {
  9426. const node = this.startNode();
  9427. this.next();
  9428. node.elements = this.parseBindingList(3, 93, true);
  9429. return this.finishNode(node, "ArrayPattern");
  9430. }
  9431. case 5:
  9432. return this.parseObjectLike(8, true);
  9433. }
  9434. return this.parseIdentifier();
  9435. }
  9436. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  9437. const elts = [];
  9438. let first = true;
  9439. while (!this.eat(close)) {
  9440. if (first) {
  9441. first = false;
  9442. } else {
  9443. this.expect(12);
  9444. }
  9445. if (allowEmpty && this.match(12)) {
  9446. elts.push(null);
  9447. } else if (this.eat(close)) {
  9448. break;
  9449. } else if (this.match(21)) {
  9450. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  9451. if (!this.checkCommaAfterRest(closeCharCode)) {
  9452. this.expect(close);
  9453. break;
  9454. }
  9455. } else {
  9456. const decorators = [];
  9457. if (this.match(26) && this.hasPlugin("decorators")) {
  9458. this.raise(ErrorMessages.UnsupportedParameterDecorator, {
  9459. at: this.state.startLoc
  9460. });
  9461. }
  9462. while (this.match(26)) {
  9463. decorators.push(this.parseDecorator());
  9464. }
  9465. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  9466. }
  9467. }
  9468. return elts;
  9469. }
  9470. parseBindingRestProperty(prop) {
  9471. this.next();
  9472. prop.argument = this.parseIdentifier();
  9473. this.checkCommaAfterRest(125);
  9474. return this.finishNode(prop, "RestElement");
  9475. }
  9476. parseBindingProperty() {
  9477. const prop = this.startNode();
  9478. const {
  9479. type,
  9480. start: startPos,
  9481. startLoc
  9482. } = this.state;
  9483. if (type === 21) {
  9484. return this.parseBindingRestProperty(prop);
  9485. } else {
  9486. this.parsePropertyName(prop);
  9487. }
  9488. prop.method = false;
  9489. this.parseObjPropValue(prop, startPos, startLoc, false, false, true, false);
  9490. return prop;
  9491. }
  9492. parseAssignableListItem(allowModifiers, decorators) {
  9493. const left = this.parseMaybeDefault();
  9494. this.parseAssignableListItemTypes(left);
  9495. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  9496. if (decorators.length) {
  9497. left.decorators = decorators;
  9498. }
  9499. return elt;
  9500. }
  9501. parseAssignableListItemTypes(param) {
  9502. return param;
  9503. }
  9504. parseMaybeDefault(startPos, startLoc, left) {
  9505. var _startLoc, _startPos, _left;
  9506. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  9507. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  9508. left = (_left = left) != null ? _left : this.parseBindingAtom();
  9509. if (!this.eat(29)) return left;
  9510. const node = this.startNodeAt(startPos, startLoc);
  9511. node.left = left;
  9512. node.right = this.parseMaybeAssignAllowIn();
  9513. return this.finishNode(node, "AssignmentPattern");
  9514. }
  9515. checkLVal(expr, contextDescription, bindingType = BIND_NONE, checkClashes, disallowLetBinding, strictModeChanged = false) {
  9516. switch (expr.type) {
  9517. case "Identifier":
  9518. {
  9519. const {
  9520. name
  9521. } = expr;
  9522. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(name, this.inModule) : isStrictBindOnlyReservedWord(name))) {
  9523. this.raise(bindingType === BIND_NONE ? ErrorMessages.StrictEvalArguments : ErrorMessages.StrictEvalArgumentsBinding, {
  9524. node: expr
  9525. }, name);
  9526. }
  9527. if (checkClashes) {
  9528. if (checkClashes.has(name)) {
  9529. this.raise(ErrorMessages.ParamDupe, {
  9530. node: expr
  9531. });
  9532. } else {
  9533. checkClashes.add(name);
  9534. }
  9535. }
  9536. if (disallowLetBinding && name === "let") {
  9537. this.raise(ErrorMessages.LetInLexicalBinding, {
  9538. node: expr
  9539. });
  9540. }
  9541. if (!(bindingType & BIND_NONE)) {
  9542. this.scope.declareName(name, bindingType, expr.loc.start);
  9543. }
  9544. break;
  9545. }
  9546. case "MemberExpression":
  9547. if (bindingType !== BIND_NONE) {
  9548. this.raise(ErrorMessages.InvalidPropertyBindingPattern, {
  9549. node: expr
  9550. });
  9551. }
  9552. break;
  9553. case "ObjectPattern":
  9554. for (let prop of expr.properties) {
  9555. if (this.isObjectProperty(prop)) prop = prop.value;else if (this.isObjectMethod(prop)) continue;
  9556. this.checkLVal(prop, "object destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  9557. }
  9558. break;
  9559. case "ArrayPattern":
  9560. for (const elem of expr.elements) {
  9561. if (elem) {
  9562. this.checkLVal(elem, "array destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  9563. }
  9564. }
  9565. break;
  9566. case "AssignmentPattern":
  9567. this.checkLVal(expr.left, "assignment pattern", bindingType, checkClashes);
  9568. break;
  9569. case "RestElement":
  9570. this.checkLVal(expr.argument, "rest element", bindingType, checkClashes);
  9571. break;
  9572. case "ParenthesizedExpression":
  9573. this.checkLVal(expr.expression, "parenthesized expression", bindingType, checkClashes);
  9574. break;
  9575. default:
  9576. {
  9577. this.raise(bindingType === BIND_NONE ? ErrorMessages.InvalidLhs : ErrorMessages.InvalidLhsBinding, {
  9578. node: expr
  9579. }, contextDescription);
  9580. }
  9581. }
  9582. }
  9583. checkToRestConversion(node) {
  9584. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  9585. this.raise(ErrorMessages.InvalidRestAssignmentPattern, {
  9586. node: node.argument
  9587. });
  9588. }
  9589. }
  9590. checkCommaAfterRest(close) {
  9591. if (!this.match(12)) {
  9592. return false;
  9593. }
  9594. this.raise(this.lookaheadCharCode() === close ? ErrorMessages.RestTrailingComma : ErrorMessages.ElementAfterRest, {
  9595. at: this.state.startLoc
  9596. });
  9597. return true;
  9598. }
  9599. }
  9600. const invalidHackPipeBodies = new Map([["ArrowFunctionExpression", "arrow function"], ["AssignmentExpression", "assignment"], ["ConditionalExpression", "conditional"], ["YieldExpression", "yield"]]);
  9601. class ExpressionParser extends LValParser {
  9602. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  9603. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  9604. return;
  9605. }
  9606. const key = prop.key;
  9607. const name = key.type === "Identifier" ? key.name : key.value;
  9608. if (name === "__proto__") {
  9609. if (isRecord) {
  9610. this.raise(ErrorMessages.RecordNoProto, {
  9611. node: key
  9612. });
  9613. return;
  9614. }
  9615. if (protoRef.used) {
  9616. if (refExpressionErrors) {
  9617. if (refExpressionErrors.doubleProtoLoc === null) {
  9618. refExpressionErrors.doubleProtoLoc = key.loc.start;
  9619. }
  9620. } else {
  9621. this.raise(ErrorMessages.DuplicateProto, {
  9622. node: key
  9623. });
  9624. }
  9625. }
  9626. protoRef.used = true;
  9627. }
  9628. }
  9629. shouldExitDescending(expr, potentialArrowAt) {
  9630. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  9631. }
  9632. getExpression() {
  9633. this.enterInitialScopes();
  9634. this.nextToken();
  9635. const expr = this.parseExpression();
  9636. if (!this.match(131)) {
  9637. this.unexpected();
  9638. }
  9639. this.finalizeRemainingComments();
  9640. expr.comments = this.state.comments;
  9641. expr.errors = this.state.errors;
  9642. if (this.options.tokens) {
  9643. expr.tokens = this.tokens;
  9644. }
  9645. return expr;
  9646. }
  9647. parseExpression(disallowIn, refExpressionErrors) {
  9648. if (disallowIn) {
  9649. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  9650. }
  9651. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  9652. }
  9653. parseExpressionBase(refExpressionErrors) {
  9654. const startPos = this.state.start;
  9655. const startLoc = this.state.startLoc;
  9656. const expr = this.parseMaybeAssign(refExpressionErrors);
  9657. if (this.match(12)) {
  9658. const node = this.startNodeAt(startPos, startLoc);
  9659. node.expressions = [expr];
  9660. while (this.eat(12)) {
  9661. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  9662. }
  9663. this.toReferencedList(node.expressions);
  9664. return this.finishNode(node, "SequenceExpression");
  9665. }
  9666. return expr;
  9667. }
  9668. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  9669. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  9670. }
  9671. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  9672. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  9673. }
  9674. setOptionalParametersError(refExpressionErrors, resultError) {
  9675. var _resultError$loc;
  9676. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  9677. }
  9678. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9679. const startPos = this.state.start;
  9680. const startLoc = this.state.startLoc;
  9681. if (this.isContextual(101)) {
  9682. if (this.prodParam.hasYield) {
  9683. let left = this.parseYield();
  9684. if (afterLeftParse) {
  9685. left = afterLeftParse.call(this, left, startPos, startLoc);
  9686. }
  9687. return left;
  9688. }
  9689. }
  9690. let ownExpressionErrors;
  9691. if (refExpressionErrors) {
  9692. ownExpressionErrors = false;
  9693. } else {
  9694. refExpressionErrors = new ExpressionErrors();
  9695. ownExpressionErrors = true;
  9696. }
  9697. const {
  9698. type
  9699. } = this.state;
  9700. if (type === 10 || tokenIsIdentifier(type)) {
  9701. this.state.potentialArrowAt = this.state.start;
  9702. }
  9703. let left = this.parseMaybeConditional(refExpressionErrors);
  9704. if (afterLeftParse) {
  9705. left = afterLeftParse.call(this, left, startPos, startLoc);
  9706. }
  9707. if (tokenIsAssignment(this.state.type)) {
  9708. const node = this.startNodeAt(startPos, startLoc);
  9709. const operator = this.state.value;
  9710. node.operator = operator;
  9711. if (this.match(29)) {
  9712. node.left = this.toAssignable(left, true);
  9713. if (refExpressionErrors.doubleProtoLoc != null && indexes.get(refExpressionErrors.doubleProtoLoc) >= startPos) {
  9714. refExpressionErrors.doubleProtoLoc = null;
  9715. }
  9716. if (refExpressionErrors.shorthandAssignLoc != null && indexes.get(refExpressionErrors.shorthandAssignLoc) >= startPos) {
  9717. refExpressionErrors.shorthandAssignLoc = null;
  9718. }
  9719. } else {
  9720. node.left = left;
  9721. }
  9722. this.checkLVal(left, "assignment expression");
  9723. this.next();
  9724. node.right = this.parseMaybeAssign();
  9725. return this.finishNode(node, "AssignmentExpression");
  9726. } else if (ownExpressionErrors) {
  9727. this.checkExpressionErrors(refExpressionErrors, true);
  9728. }
  9729. return left;
  9730. }
  9731. parseMaybeConditional(refExpressionErrors) {
  9732. const startPos = this.state.start;
  9733. const startLoc = this.state.startLoc;
  9734. const potentialArrowAt = this.state.potentialArrowAt;
  9735. const expr = this.parseExprOps(refExpressionErrors);
  9736. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  9737. return expr;
  9738. }
  9739. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  9740. }
  9741. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  9742. if (this.eat(17)) {
  9743. const node = this.startNodeAt(startPos, startLoc);
  9744. node.test = expr;
  9745. node.consequent = this.parseMaybeAssignAllowIn();
  9746. this.expect(14);
  9747. node.alternate = this.parseMaybeAssign();
  9748. return this.finishNode(node, "ConditionalExpression");
  9749. }
  9750. return expr;
  9751. }
  9752. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  9753. return this.match(130) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  9754. }
  9755. parseExprOps(refExpressionErrors) {
  9756. const startPos = this.state.start;
  9757. const startLoc = this.state.startLoc;
  9758. const potentialArrowAt = this.state.potentialArrowAt;
  9759. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  9760. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  9761. return expr;
  9762. }
  9763. return this.parseExprOp(expr, startPos, startLoc, -1);
  9764. }
  9765. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  9766. if (this.isPrivateName(left)) {
  9767. const value = this.getPrivateNameSV(left);
  9768. if (minPrec >= tokenOperatorPrecedence(54) || !this.prodParam.hasIn || !this.match(54)) {
  9769. this.raise(ErrorMessages.PrivateInExpectedIn, {
  9770. node: left
  9771. }, value);
  9772. }
  9773. this.classScope.usePrivateName(value, left.loc.start);
  9774. }
  9775. const op = this.state.type;
  9776. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(54))) {
  9777. let prec = tokenOperatorPrecedence(op);
  9778. if (prec > minPrec) {
  9779. if (op === 37) {
  9780. this.expectPlugin("pipelineOperator");
  9781. if (this.state.inFSharpPipelineDirectBody) {
  9782. return left;
  9783. }
  9784. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  9785. }
  9786. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  9787. node.left = left;
  9788. node.operator = this.state.value;
  9789. const logical = op === 39 || op === 40;
  9790. const coalesce = op === 38;
  9791. if (coalesce) {
  9792. prec = tokenOperatorPrecedence(40);
  9793. }
  9794. this.next();
  9795. if (op === 37 && this.hasPlugin(["pipelineOperator", {
  9796. proposal: "minimal"
  9797. }])) {
  9798. if (this.state.type === 92 && this.prodParam.hasAwait) {
  9799. throw this.raise(ErrorMessages.UnexpectedAwaitAfterPipelineBody, {
  9800. at: this.state.startLoc
  9801. });
  9802. }
  9803. }
  9804. node.right = this.parseExprOpRightExpr(op, prec);
  9805. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  9806. const nextOp = this.state.type;
  9807. if (coalesce && (nextOp === 39 || nextOp === 40) || logical && nextOp === 38) {
  9808. throw this.raise(ErrorMessages.MixingCoalesceWithLogical, {
  9809. at: this.state.startLoc
  9810. });
  9811. }
  9812. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  9813. }
  9814. }
  9815. return left;
  9816. }
  9817. parseExprOpRightExpr(op, prec) {
  9818. const startPos = this.state.start;
  9819. const startLoc = this.state.startLoc;
  9820. switch (op) {
  9821. case 37:
  9822. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  9823. case "hack":
  9824. return this.withTopicBindingContext(() => {
  9825. return this.parseHackPipeBody();
  9826. });
  9827. case "smart":
  9828. return this.withTopicBindingContext(() => {
  9829. if (this.prodParam.hasYield && this.isContextual(101)) {
  9830. throw this.raise(ErrorMessages.PipeBodyIsTighter, {
  9831. at: this.state.startLoc
  9832. }, this.state.value);
  9833. }
  9834. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  9835. });
  9836. case "fsharp":
  9837. return this.withSoloAwaitPermittingContext(() => {
  9838. return this.parseFSharpPipelineBody(prec);
  9839. });
  9840. }
  9841. default:
  9842. return this.parseExprOpBaseRightExpr(op, prec);
  9843. }
  9844. }
  9845. parseExprOpBaseRightExpr(op, prec) {
  9846. const startPos = this.state.start;
  9847. const startLoc = this.state.startLoc;
  9848. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  9849. }
  9850. parseHackPipeBody() {
  9851. var _body$extra;
  9852. const {
  9853. startLoc
  9854. } = this.state;
  9855. const body = this.parseMaybeAssign();
  9856. if (invalidHackPipeBodies.has(body.type) && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  9857. this.raise(ErrorMessages.PipeUnparenthesizedBody, {
  9858. at: startLoc
  9859. }, invalidHackPipeBodies.get(body.type));
  9860. }
  9861. if (!this.topicReferenceWasUsedInCurrentContext()) {
  9862. this.raise(ErrorMessages.PipeTopicUnused, {
  9863. at: startLoc
  9864. });
  9865. }
  9866. return body;
  9867. }
  9868. checkExponentialAfterUnary(node) {
  9869. if (this.match(53)) {
  9870. this.raise(ErrorMessages.UnexpectedTokenUnaryExponentiation, {
  9871. node: node.argument
  9872. });
  9873. }
  9874. }
  9875. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9876. const startPos = this.state.start;
  9877. const startLoc = this.state.startLoc;
  9878. const isAwait = this.isContextual(92);
  9879. if (isAwait && this.isAwaitAllowed()) {
  9880. this.next();
  9881. const expr = this.parseAwait(startPos, startLoc);
  9882. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  9883. return expr;
  9884. }
  9885. const update = this.match(34);
  9886. const node = this.startNode();
  9887. if (tokenIsPrefix(this.state.type)) {
  9888. node.operator = this.state.value;
  9889. node.prefix = true;
  9890. if (this.match(68)) {
  9891. this.expectPlugin("throwExpressions");
  9892. }
  9893. const isDelete = this.match(85);
  9894. this.next();
  9895. node.argument = this.parseMaybeUnary(null, true);
  9896. this.checkExpressionErrors(refExpressionErrors, true);
  9897. if (this.state.strict && isDelete) {
  9898. const arg = node.argument;
  9899. if (arg.type === "Identifier") {
  9900. this.raise(ErrorMessages.StrictDelete, {
  9901. node
  9902. });
  9903. } else if (this.hasPropertyAsPrivateName(arg)) {
  9904. this.raise(ErrorMessages.DeletePrivateField, {
  9905. node
  9906. });
  9907. }
  9908. }
  9909. if (!update) {
  9910. if (!sawUnary) this.checkExponentialAfterUnary(node);
  9911. return this.finishNode(node, "UnaryExpression");
  9912. }
  9913. }
  9914. const expr = this.parseUpdate(node, update, refExpressionErrors);
  9915. if (isAwait) {
  9916. const {
  9917. type
  9918. } = this.state;
  9919. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(50);
  9920. if (startsExpr && !this.isAmbiguousAwait()) {
  9921. this.raiseOverwrite(startLoc, ErrorMessages.AwaitNotInAsyncContext);
  9922. return this.parseAwait(startPos, startLoc);
  9923. }
  9924. }
  9925. return expr;
  9926. }
  9927. parseUpdate(node, update, refExpressionErrors) {
  9928. if (update) {
  9929. this.checkLVal(node.argument, "prefix operation");
  9930. return this.finishNode(node, "UpdateExpression");
  9931. }
  9932. const startPos = this.state.start;
  9933. const startLoc = this.state.startLoc;
  9934. let expr = this.parseExprSubscripts(refExpressionErrors);
  9935. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  9936. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  9937. const node = this.startNodeAt(startPos, startLoc);
  9938. node.operator = this.state.value;
  9939. node.prefix = false;
  9940. node.argument = expr;
  9941. this.checkLVal(expr, "postfix operation");
  9942. this.next();
  9943. expr = this.finishNode(node, "UpdateExpression");
  9944. }
  9945. return expr;
  9946. }
  9947. parseExprSubscripts(refExpressionErrors) {
  9948. const startPos = this.state.start;
  9949. const startLoc = this.state.startLoc;
  9950. const potentialArrowAt = this.state.potentialArrowAt;
  9951. const expr = this.parseExprAtom(refExpressionErrors);
  9952. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  9953. return expr;
  9954. }
  9955. return this.parseSubscripts(expr, startPos, startLoc);
  9956. }
  9957. parseSubscripts(base, startPos, startLoc, noCalls) {
  9958. const state = {
  9959. optionalChainMember: false,
  9960. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  9961. stop: false
  9962. };
  9963. do {
  9964. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  9965. state.maybeAsyncArrow = false;
  9966. } while (!state.stop);
  9967. return base;
  9968. }
  9969. parseSubscript(base, startPos, startLoc, noCalls, state) {
  9970. const {
  9971. type
  9972. } = this.state;
  9973. if (!noCalls && type === 15) {
  9974. return this.parseBind(base, startPos, startLoc, noCalls, state);
  9975. } else if (tokenIsTemplate(type)) {
  9976. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  9977. }
  9978. let optional = false;
  9979. if (type === 18) {
  9980. if (noCalls && this.lookaheadCharCode() === 40) {
  9981. state.stop = true;
  9982. return base;
  9983. }
  9984. state.optionalChainMember = optional = true;
  9985. this.next();
  9986. }
  9987. if (!noCalls && this.match(10)) {
  9988. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  9989. } else {
  9990. const computed = this.eat(0);
  9991. if (computed || optional || this.eat(16)) {
  9992. return this.parseMember(base, startPos, startLoc, state, computed, optional);
  9993. } else {
  9994. state.stop = true;
  9995. return base;
  9996. }
  9997. }
  9998. }
  9999. parseMember(base, startPos, startLoc, state, computed, optional) {
  10000. const node = this.startNodeAt(startPos, startLoc);
  10001. node.object = base;
  10002. node.computed = computed;
  10003. if (computed) {
  10004. node.property = this.parseExpression();
  10005. this.expect(3);
  10006. } else if (this.match(130)) {
  10007. if (base.type === "Super") {
  10008. this.raise(ErrorMessages.SuperPrivateField, {
  10009. at: startLoc
  10010. });
  10011. }
  10012. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10013. node.property = this.parsePrivateName();
  10014. } else {
  10015. node.property = this.parseIdentifier(true);
  10016. }
  10017. if (state.optionalChainMember) {
  10018. node.optional = optional;
  10019. return this.finishNode(node, "OptionalMemberExpression");
  10020. } else {
  10021. return this.finishNode(node, "MemberExpression");
  10022. }
  10023. }
  10024. parseBind(base, startPos, startLoc, noCalls, state) {
  10025. const node = this.startNodeAt(startPos, startLoc);
  10026. node.object = base;
  10027. this.next();
  10028. node.callee = this.parseNoCallExpr();
  10029. state.stop = true;
  10030. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  10031. }
  10032. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  10033. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10034. let refExpressionErrors = null;
  10035. this.state.maybeInArrowParameters = true;
  10036. this.next();
  10037. let node = this.startNodeAt(startPos, startLoc);
  10038. node.callee = base;
  10039. if (state.maybeAsyncArrow) {
  10040. this.expressionScope.enter(newAsyncArrowScope());
  10041. refExpressionErrors = new ExpressionErrors();
  10042. }
  10043. if (state.optionalChainMember) {
  10044. node.optional = optional;
  10045. }
  10046. if (optional) {
  10047. node.arguments = this.parseCallExpressionArguments(11);
  10048. } else {
  10049. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10050. }
  10051. this.finishCallExpression(node, state.optionalChainMember);
  10052. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10053. state.stop = true;
  10054. this.expressionScope.validateAsPattern();
  10055. this.expressionScope.exit();
  10056. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  10057. } else {
  10058. if (state.maybeAsyncArrow) {
  10059. this.checkExpressionErrors(refExpressionErrors, true);
  10060. this.expressionScope.exit();
  10061. }
  10062. this.toReferencedArguments(node);
  10063. }
  10064. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10065. return node;
  10066. }
  10067. toReferencedArguments(node, isParenthesizedExpr) {
  10068. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10069. }
  10070. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  10071. const node = this.startNodeAt(startPos, startLoc);
  10072. node.tag = base;
  10073. node.quasi = this.parseTemplate(true);
  10074. if (state.optionalChainMember) {
  10075. this.raise(ErrorMessages.OptionalChainingNoTemplate, {
  10076. at: startLoc
  10077. });
  10078. }
  10079. return this.finishNode(node, "TaggedTemplateExpression");
  10080. }
  10081. atPossibleAsyncArrow(base) {
  10082. return base.type === "Identifier" && base.name === "async" && indexes.get(this.state.lastTokEndLoc) === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  10083. }
  10084. finishCallExpression(node, optional) {
  10085. if (node.callee.type === "Import") {
  10086. if (node.arguments.length === 2) {
  10087. {
  10088. if (!this.hasPlugin("moduleAttributes")) {
  10089. this.expectPlugin("importAssertions");
  10090. }
  10091. }
  10092. }
  10093. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10094. this.raise(ErrorMessages.ImportCallArity, {
  10095. node
  10096. }, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
  10097. } else {
  10098. for (const arg of node.arguments) {
  10099. if (arg.type === "SpreadElement") {
  10100. this.raise(ErrorMessages.ImportCallSpreadArgument, {
  10101. node: arg
  10102. });
  10103. }
  10104. }
  10105. }
  10106. }
  10107. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10108. }
  10109. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10110. const elts = [];
  10111. let first = true;
  10112. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10113. this.state.inFSharpPipelineDirectBody = false;
  10114. while (!this.eat(close)) {
  10115. if (first) {
  10116. first = false;
  10117. } else {
  10118. this.expect(12);
  10119. if (this.match(close)) {
  10120. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10121. this.raise(ErrorMessages.ImportCallArgumentTrailingComma, {
  10122. at: this.state.lastTokStartLoc
  10123. });
  10124. }
  10125. if (nodeForExtra) {
  10126. this.addTrailingCommaExtraToNode(nodeForExtra);
  10127. }
  10128. this.next();
  10129. break;
  10130. }
  10131. }
  10132. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10133. }
  10134. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10135. return elts;
  10136. }
  10137. shouldParseAsyncArrow() {
  10138. return this.match(19) && !this.canInsertSemicolon();
  10139. }
  10140. parseAsyncArrowFromCallExpression(node, call) {
  10141. var _call$extra;
  10142. this.resetPreviousNodeTrailingComments(call);
  10143. this.expect(19);
  10144. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  10145. if (call.innerComments) {
  10146. setInnerComments(node, call.innerComments);
  10147. }
  10148. if (call.callee.trailingComments) {
  10149. setInnerComments(node, call.callee.trailingComments);
  10150. }
  10151. return node;
  10152. }
  10153. parseNoCallExpr() {
  10154. const startPos = this.state.start;
  10155. const startLoc = this.state.startLoc;
  10156. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  10157. }
  10158. parseExprAtom(refExpressionErrors) {
  10159. let node;
  10160. const {
  10161. type
  10162. } = this.state;
  10163. switch (type) {
  10164. case 75:
  10165. return this.parseSuper();
  10166. case 79:
  10167. node = this.startNode();
  10168. this.next();
  10169. if (this.match(16)) {
  10170. return this.parseImportMetaProperty(node);
  10171. }
  10172. if (!this.match(10)) {
  10173. this.raise(ErrorMessages.UnsupportedImport, {
  10174. at: this.state.lastTokStartLoc
  10175. });
  10176. }
  10177. return this.finishNode(node, "Import");
  10178. case 74:
  10179. node = this.startNode();
  10180. this.next();
  10181. return this.finishNode(node, "ThisExpression");
  10182. case 86:
  10183. {
  10184. return this.parseDo(this.startNode(), false);
  10185. }
  10186. case 52:
  10187. case 31:
  10188. {
  10189. this.readRegexp();
  10190. return this.parseRegExpLiteral(this.state.value);
  10191. }
  10192. case 126:
  10193. return this.parseNumericLiteral(this.state.value);
  10194. case 127:
  10195. return this.parseBigIntLiteral(this.state.value);
  10196. case 128:
  10197. return this.parseDecimalLiteral(this.state.value);
  10198. case 125:
  10199. return this.parseStringLiteral(this.state.value);
  10200. case 80:
  10201. return this.parseNullLiteral();
  10202. case 81:
  10203. return this.parseBooleanLiteral(true);
  10204. case 82:
  10205. return this.parseBooleanLiteral(false);
  10206. case 10:
  10207. {
  10208. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10209. return this.parseParenAndDistinguishExpression(canBeArrow);
  10210. }
  10211. case 2:
  10212. case 1:
  10213. {
  10214. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  10215. }
  10216. case 0:
  10217. {
  10218. return this.parseArrayLike(3, true, false, refExpressionErrors);
  10219. }
  10220. case 6:
  10221. case 7:
  10222. {
  10223. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  10224. }
  10225. case 5:
  10226. {
  10227. return this.parseObjectLike(8, false, false, refExpressionErrors);
  10228. }
  10229. case 64:
  10230. return this.parseFunctionOrFunctionSent();
  10231. case 26:
  10232. this.parseDecorators();
  10233. case 76:
  10234. node = this.startNode();
  10235. this.takeDecorators(node);
  10236. return this.parseClass(node, false);
  10237. case 73:
  10238. return this.parseNewOrNewTarget();
  10239. case 25:
  10240. case 24:
  10241. return this.parseTemplate(false);
  10242. case 15:
  10243. {
  10244. node = this.startNode();
  10245. this.next();
  10246. node.object = null;
  10247. const callee = node.callee = this.parseNoCallExpr();
  10248. if (callee.type === "MemberExpression") {
  10249. return this.finishNode(node, "BindExpression");
  10250. } else {
  10251. throw this.raise(ErrorMessages.UnsupportedBind, {
  10252. node: callee
  10253. });
  10254. }
  10255. }
  10256. case 130:
  10257. {
  10258. this.raise(ErrorMessages.PrivateInExpectedIn, {
  10259. at: this.state.startLoc
  10260. }, this.state.value);
  10261. return this.parsePrivateName();
  10262. }
  10263. case 33:
  10264. {
  10265. return this.parseTopicReferenceThenEqualsSign(50, "%");
  10266. }
  10267. case 32:
  10268. {
  10269. return this.parseTopicReferenceThenEqualsSign(42, "^");
  10270. }
  10271. case 42:
  10272. case 50:
  10273. case 27:
  10274. {
  10275. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10276. if (pipeProposal) {
  10277. return this.parseTopicReference(pipeProposal);
  10278. } else {
  10279. throw this.unexpected();
  10280. }
  10281. }
  10282. case 45:
  10283. {
  10284. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  10285. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  10286. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  10287. break;
  10288. } else {
  10289. throw this.unexpected();
  10290. }
  10291. }
  10292. default:
  10293. if (tokenIsIdentifier(type)) {
  10294. if (this.isContextual(119) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  10295. return this.parseModuleExpression();
  10296. }
  10297. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  10298. const containsEsc = this.state.containsEsc;
  10299. const id = this.parseIdentifier();
  10300. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  10301. const {
  10302. type
  10303. } = this.state;
  10304. if (type === 64) {
  10305. this.resetPreviousNodeTrailingComments(id);
  10306. this.next();
  10307. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  10308. } else if (tokenIsIdentifier(type)) {
  10309. if (this.lookaheadCharCode() === 61) {
  10310. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  10311. } else {
  10312. return id;
  10313. }
  10314. } else if (type === 86) {
  10315. this.resetPreviousNodeTrailingComments(id);
  10316. return this.parseDo(this.startNodeAtNode(id), true);
  10317. }
  10318. }
  10319. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  10320. this.next();
  10321. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  10322. }
  10323. return id;
  10324. } else {
  10325. throw this.unexpected();
  10326. }
  10327. }
  10328. }
  10329. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  10330. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  10331. if (pipeProposal) {
  10332. this.state.type = topicTokenType;
  10333. this.state.value = topicTokenValue;
  10334. this.state.pos--;
  10335. this.state.end--;
  10336. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  10337. return this.parseTopicReference(pipeProposal);
  10338. } else {
  10339. throw this.unexpected();
  10340. }
  10341. }
  10342. parseTopicReference(pipeProposal) {
  10343. const node = this.startNode();
  10344. const startLoc = this.state.startLoc;
  10345. const tokenType = this.state.type;
  10346. this.next();
  10347. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  10348. }
  10349. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  10350. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  10351. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  10352. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  10353. this.raise(pipeProposal === "smart" ? ErrorMessages.PrimaryTopicNotAllowed : ErrorMessages.PipeTopicUnbound, {
  10354. at: startLoc
  10355. });
  10356. }
  10357. this.registerTopicReference();
  10358. return this.finishNode(node, nodeType);
  10359. } else {
  10360. throw this.raise(ErrorMessages.PipeTopicUnconfiguredToken, {
  10361. at: startLoc
  10362. }, tokenLabelName(tokenType));
  10363. }
  10364. }
  10365. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  10366. switch (pipeProposal) {
  10367. case "hack":
  10368. {
  10369. return this.hasPlugin(["pipelineOperator", {
  10370. topicToken: tokenLabelName(tokenType)
  10371. }]);
  10372. }
  10373. case "smart":
  10374. return tokenType === 27;
  10375. default:
  10376. throw this.raise(ErrorMessages.PipeTopicRequiresHackPipes, {
  10377. at: startLoc
  10378. });
  10379. }
  10380. }
  10381. parseAsyncArrowUnaryFunction(node) {
  10382. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  10383. const params = [this.parseIdentifier()];
  10384. this.prodParam.exit();
  10385. if (this.hasPrecedingLineBreak()) {
  10386. this.raise(ErrorMessages.LineTerminatorBeforeArrow, {
  10387. at: this.state.curPosition()
  10388. });
  10389. }
  10390. this.expect(19);
  10391. this.parseArrowExpression(node, params, true);
  10392. return node;
  10393. }
  10394. parseDo(node, isAsync) {
  10395. this.expectPlugin("doExpressions");
  10396. if (isAsync) {
  10397. this.expectPlugin("asyncDoExpressions");
  10398. }
  10399. node.async = isAsync;
  10400. this.next();
  10401. const oldLabels = this.state.labels;
  10402. this.state.labels = [];
  10403. if (isAsync) {
  10404. this.prodParam.enter(PARAM_AWAIT);
  10405. node.body = this.parseBlock();
  10406. this.prodParam.exit();
  10407. } else {
  10408. node.body = this.parseBlock();
  10409. }
  10410. this.state.labels = oldLabels;
  10411. return this.finishNode(node, "DoExpression");
  10412. }
  10413. parseSuper() {
  10414. const node = this.startNode();
  10415. this.next();
  10416. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  10417. this.raise(ErrorMessages.SuperNotAllowed, {
  10418. node
  10419. });
  10420. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  10421. this.raise(ErrorMessages.UnexpectedSuper, {
  10422. node
  10423. });
  10424. }
  10425. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  10426. this.raise(ErrorMessages.UnsupportedSuper, {
  10427. node
  10428. });
  10429. }
  10430. return this.finishNode(node, "Super");
  10431. }
  10432. parsePrivateName() {
  10433. const node = this.startNode();
  10434. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart, this.state.start + 1));
  10435. const name = this.state.value;
  10436. this.next();
  10437. node.id = this.createIdentifier(id, name);
  10438. return this.finishNode(node, "PrivateName");
  10439. }
  10440. parseFunctionOrFunctionSent() {
  10441. const node = this.startNode();
  10442. this.next();
  10443. if (this.prodParam.hasYield && this.match(16)) {
  10444. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  10445. this.next();
  10446. if (this.match(98)) {
  10447. this.expectPlugin("functionSent");
  10448. } else if (!this.hasPlugin("functionSent")) {
  10449. this.unexpected();
  10450. }
  10451. return this.parseMetaProperty(node, meta, "sent");
  10452. }
  10453. return this.parseFunction(node);
  10454. }
  10455. parseMetaProperty(node, meta, propertyName) {
  10456. node.meta = meta;
  10457. const containsEsc = this.state.containsEsc;
  10458. node.property = this.parseIdentifier(true);
  10459. if (node.property.name !== propertyName || containsEsc) {
  10460. this.raise(ErrorMessages.UnsupportedMetaProperty, {
  10461. node: node.property
  10462. }, meta.name, propertyName);
  10463. }
  10464. return this.finishNode(node, "MetaProperty");
  10465. }
  10466. parseImportMetaProperty(node) {
  10467. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  10468. this.next();
  10469. if (this.isContextual(96)) {
  10470. if (!this.inModule) {
  10471. this.raise(SourceTypeModuleErrorMessages.ImportMetaOutsideModule, {
  10472. node: id
  10473. });
  10474. }
  10475. this.sawUnambiguousESM = true;
  10476. }
  10477. return this.parseMetaProperty(node, id, "meta");
  10478. }
  10479. parseLiteralAtNode(value, type, node) {
  10480. this.addExtra(node, "rawValue", value);
  10481. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  10482. node.value = value;
  10483. this.next();
  10484. return this.finishNode(node, type);
  10485. }
  10486. parseLiteral(value, type) {
  10487. const node = this.startNode();
  10488. return this.parseLiteralAtNode(value, type, node);
  10489. }
  10490. parseStringLiteral(value) {
  10491. return this.parseLiteral(value, "StringLiteral");
  10492. }
  10493. parseNumericLiteral(value) {
  10494. return this.parseLiteral(value, "NumericLiteral");
  10495. }
  10496. parseBigIntLiteral(value) {
  10497. return this.parseLiteral(value, "BigIntLiteral");
  10498. }
  10499. parseDecimalLiteral(value) {
  10500. return this.parseLiteral(value, "DecimalLiteral");
  10501. }
  10502. parseRegExpLiteral(value) {
  10503. const node = this.parseLiteral(value.value, "RegExpLiteral");
  10504. node.pattern = value.pattern;
  10505. node.flags = value.flags;
  10506. return node;
  10507. }
  10508. parseBooleanLiteral(value) {
  10509. const node = this.startNode();
  10510. node.value = value;
  10511. this.next();
  10512. return this.finishNode(node, "BooleanLiteral");
  10513. }
  10514. parseNullLiteral() {
  10515. const node = this.startNode();
  10516. this.next();
  10517. return this.finishNode(node, "NullLiteral");
  10518. }
  10519. parseParenAndDistinguishExpression(canBeArrow) {
  10520. const startPos = this.state.start;
  10521. const startLoc = this.state.startLoc;
  10522. let val;
  10523. this.next();
  10524. this.expressionScope.enter(newArrowHeadScope());
  10525. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10526. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10527. this.state.maybeInArrowParameters = true;
  10528. this.state.inFSharpPipelineDirectBody = false;
  10529. const innerStartPos = this.state.start;
  10530. const innerStartLoc = this.state.startLoc;
  10531. const exprList = [];
  10532. const refExpressionErrors = new ExpressionErrors();
  10533. let first = true;
  10534. let spreadStartLoc;
  10535. let optionalCommaStartLoc;
  10536. while (!this.match(11)) {
  10537. if (first) {
  10538. first = false;
  10539. } else {
  10540. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  10541. if (this.match(11)) {
  10542. optionalCommaStartLoc = this.state.startLoc;
  10543. break;
  10544. }
  10545. }
  10546. if (this.match(21)) {
  10547. const spreadNodeStartPos = this.state.start;
  10548. const spreadNodeStartLoc = this.state.startLoc;
  10549. spreadStartLoc = this.state.startLoc;
  10550. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  10551. if (!this.checkCommaAfterRest(41)) {
  10552. break;
  10553. }
  10554. } else {
  10555. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  10556. }
  10557. }
  10558. const innerEndLoc = this.state.lastTokEndLoc;
  10559. this.expect(11);
  10560. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10561. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10562. let arrowNode = this.startNodeAt(startPos, startLoc);
  10563. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  10564. this.expressionScope.validateAsPattern();
  10565. this.expressionScope.exit();
  10566. this.parseArrowExpression(arrowNode, exprList, false);
  10567. return arrowNode;
  10568. }
  10569. this.expressionScope.exit();
  10570. if (!exprList.length) {
  10571. this.unexpected(this.state.lastTokStartLoc);
  10572. }
  10573. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  10574. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  10575. this.checkExpressionErrors(refExpressionErrors, true);
  10576. this.toReferencedListDeep(exprList, true);
  10577. if (exprList.length > 1) {
  10578. val = this.startNodeAt(innerStartPos, innerStartLoc);
  10579. val.expressions = exprList;
  10580. this.finishNode(val, "SequenceExpression");
  10581. this.resetEndLocation(val, innerEndLoc);
  10582. } else {
  10583. val = exprList[0];
  10584. }
  10585. if (!this.options.createParenthesizedExpressions) {
  10586. this.addExtra(val, "parenthesized", true);
  10587. this.addExtra(val, "parenStart", startPos);
  10588. this.takeSurroundingComments(val, startPos, indexes.get(this.state.lastTokEndLoc));
  10589. return val;
  10590. }
  10591. const parenExpression = this.startNodeAt(startPos, startLoc);
  10592. parenExpression.expression = val;
  10593. this.finishNode(parenExpression, "ParenthesizedExpression");
  10594. return parenExpression;
  10595. }
  10596. shouldParseArrow(params) {
  10597. return !this.canInsertSemicolon();
  10598. }
  10599. parseArrow(node) {
  10600. if (this.eat(19)) {
  10601. return node;
  10602. }
  10603. }
  10604. parseParenItem(node, startPos, startLoc) {
  10605. return node;
  10606. }
  10607. parseNewOrNewTarget() {
  10608. const node = this.startNode();
  10609. this.next();
  10610. if (this.match(16)) {
  10611. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  10612. this.next();
  10613. const metaProp = this.parseMetaProperty(node, meta, "target");
  10614. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  10615. this.raise(ErrorMessages.UnexpectedNewTarget, {
  10616. node: metaProp
  10617. });
  10618. }
  10619. return metaProp;
  10620. }
  10621. return this.parseNew(node);
  10622. }
  10623. parseNew(node) {
  10624. node.callee = this.parseNoCallExpr();
  10625. if (node.callee.type === "Import") {
  10626. this.raise(ErrorMessages.ImportCallNotNewExpression, {
  10627. node: node.callee
  10628. });
  10629. } else if (this.isOptionalChain(node.callee)) {
  10630. this.raise(ErrorMessages.OptionalChainingNoNew, {
  10631. at: this.state.lastTokEndLoc
  10632. });
  10633. } else if (this.eat(18)) {
  10634. this.raise(ErrorMessages.OptionalChainingNoNew, {
  10635. at: this.state.startLoc
  10636. });
  10637. }
  10638. this.parseNewArguments(node);
  10639. return this.finishNode(node, "NewExpression");
  10640. }
  10641. parseNewArguments(node) {
  10642. if (this.eat(10)) {
  10643. const args = this.parseExprList(11);
  10644. this.toReferencedList(args);
  10645. node.arguments = args;
  10646. } else {
  10647. node.arguments = [];
  10648. }
  10649. }
  10650. parseTemplateElement(isTagged) {
  10651. const {
  10652. start,
  10653. startLoc,
  10654. end,
  10655. value
  10656. } = this.state;
  10657. const elemStart = start + 1;
  10658. const elem = this.startNodeAt(elemStart, createPositionWithColumnOffset(startLoc, 1));
  10659. if (value === null) {
  10660. if (!isTagged) {
  10661. this.raise(ErrorMessages.InvalidEscapeSequenceTemplate, {
  10662. at: createPositionWithColumnOffset(startLoc, 2)
  10663. });
  10664. }
  10665. }
  10666. const isTail = this.match(24);
  10667. const endOffset = isTail ? -1 : -2;
  10668. const elemEnd = end + endOffset;
  10669. elem.value = {
  10670. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  10671. cooked: value === null ? null : value.slice(1, endOffset)
  10672. };
  10673. elem.tail = isTail;
  10674. this.next();
  10675. this.finishNode(elem, "TemplateElement");
  10676. this.resetEndLocation(elem, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  10677. return elem;
  10678. }
  10679. parseTemplate(isTagged) {
  10680. const node = this.startNode();
  10681. node.expressions = [];
  10682. let curElt = this.parseTemplateElement(isTagged);
  10683. node.quasis = [curElt];
  10684. while (!curElt.tail) {
  10685. node.expressions.push(this.parseTemplateSubstitution());
  10686. this.readTemplateContinuation();
  10687. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  10688. }
  10689. return this.finishNode(node, "TemplateLiteral");
  10690. }
  10691. parseTemplateSubstitution() {
  10692. return this.parseExpression();
  10693. }
  10694. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  10695. if (isRecord) {
  10696. this.expectPlugin("recordAndTuple");
  10697. }
  10698. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10699. this.state.inFSharpPipelineDirectBody = false;
  10700. const propHash = Object.create(null);
  10701. let first = true;
  10702. const node = this.startNode();
  10703. node.properties = [];
  10704. this.next();
  10705. while (!this.match(close)) {
  10706. if (first) {
  10707. first = false;
  10708. } else {
  10709. this.expect(12);
  10710. if (this.match(close)) {
  10711. this.addTrailingCommaExtraToNode(node);
  10712. break;
  10713. }
  10714. }
  10715. let prop;
  10716. if (isPattern) {
  10717. prop = this.parseBindingProperty();
  10718. } else {
  10719. prop = this.parsePropertyDefinition(refExpressionErrors);
  10720. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  10721. }
  10722. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  10723. this.raise(ErrorMessages.InvalidRecordProperty, {
  10724. node: prop
  10725. });
  10726. }
  10727. if (prop.shorthand) {
  10728. this.addExtra(prop, "shorthand", true);
  10729. }
  10730. node.properties.push(prop);
  10731. }
  10732. this.next();
  10733. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10734. let type = "ObjectExpression";
  10735. if (isPattern) {
  10736. type = "ObjectPattern";
  10737. } else if (isRecord) {
  10738. type = "RecordExpression";
  10739. }
  10740. return this.finishNode(node, type);
  10741. }
  10742. addTrailingCommaExtraToNode(node) {
  10743. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  10744. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  10745. }
  10746. maybeAsyncOrAccessorProp(prop) {
  10747. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(51));
  10748. }
  10749. parsePropertyDefinition(refExpressionErrors) {
  10750. let decorators = [];
  10751. if (this.match(26)) {
  10752. if (this.hasPlugin("decorators")) {
  10753. this.raise(ErrorMessages.UnsupportedPropertyDecorator, {
  10754. at: this.state.startLoc
  10755. });
  10756. }
  10757. while (this.match(26)) {
  10758. decorators.push(this.parseDecorator());
  10759. }
  10760. }
  10761. const prop = this.startNode();
  10762. let isAsync = false;
  10763. let isAccessor = false;
  10764. let startPos;
  10765. let startLoc;
  10766. if (this.match(21)) {
  10767. if (decorators.length) this.unexpected();
  10768. return this.parseSpread();
  10769. }
  10770. if (decorators.length) {
  10771. prop.decorators = decorators;
  10772. decorators = [];
  10773. }
  10774. prop.method = false;
  10775. if (refExpressionErrors) {
  10776. startPos = this.state.start;
  10777. startLoc = this.state.startLoc;
  10778. }
  10779. let isGenerator = this.eat(51);
  10780. this.parsePropertyNamePrefixOperator(prop);
  10781. const containsEsc = this.state.containsEsc;
  10782. const key = this.parsePropertyName(prop);
  10783. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  10784. const keyName = key.name;
  10785. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  10786. isAsync = true;
  10787. this.resetPreviousNodeTrailingComments(key);
  10788. isGenerator = this.eat(51);
  10789. this.parsePropertyName(prop);
  10790. }
  10791. if (keyName === "get" || keyName === "set") {
  10792. isAccessor = true;
  10793. this.resetPreviousNodeTrailingComments(key);
  10794. prop.kind = keyName;
  10795. if (this.match(51)) {
  10796. isGenerator = true;
  10797. this.raise(ErrorMessages.AccessorIsGenerator, {
  10798. at: this.state.curPosition()
  10799. }, keyName);
  10800. this.next();
  10801. }
  10802. this.parsePropertyName(prop);
  10803. }
  10804. }
  10805. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  10806. return prop;
  10807. }
  10808. getGetterSetterExpectedParamCount(method) {
  10809. return method.kind === "get" ? 0 : 1;
  10810. }
  10811. getObjectOrClassMethodParams(method) {
  10812. return method.params;
  10813. }
  10814. checkGetterSetterParams(method) {
  10815. var _params;
  10816. const paramCount = this.getGetterSetterExpectedParamCount(method);
  10817. const params = this.getObjectOrClassMethodParams(method);
  10818. if (params.length !== paramCount) {
  10819. this.raise(method.kind === "get" ? ErrorMessages.BadGetterArity : ErrorMessages.BadSetterArity, {
  10820. node: method
  10821. });
  10822. }
  10823. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  10824. this.raise(ErrorMessages.BadSetterRestParameter, {
  10825. node: method
  10826. });
  10827. }
  10828. }
  10829. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  10830. if (isAccessor) {
  10831. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  10832. this.checkGetterSetterParams(prop);
  10833. return prop;
  10834. }
  10835. if (isAsync || isGenerator || this.match(10)) {
  10836. if (isPattern) this.unexpected();
  10837. prop.kind = "method";
  10838. prop.method = true;
  10839. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  10840. }
  10841. }
  10842. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  10843. prop.shorthand = false;
  10844. if (this.eat(14)) {
  10845. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  10846. return this.finishNode(prop, "ObjectProperty");
  10847. }
  10848. if (!prop.computed && prop.key.type === "Identifier") {
  10849. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  10850. if (isPattern) {
  10851. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  10852. } else if (this.match(29)) {
  10853. const shorthandAssignLoc = this.state.startLoc;
  10854. if (refExpressionErrors != null) {
  10855. if (refExpressionErrors.shorthandAssignLoc === null) {
  10856. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  10857. }
  10858. } else {
  10859. this.raise(ErrorMessages.InvalidCoverInitializedName, {
  10860. at: shorthandAssignLoc
  10861. });
  10862. }
  10863. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  10864. } else {
  10865. prop.value = cloneIdentifier(prop.key);
  10866. }
  10867. prop.shorthand = true;
  10868. return this.finishNode(prop, "ObjectProperty");
  10869. }
  10870. }
  10871. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  10872. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  10873. if (!node) this.unexpected();
  10874. return node;
  10875. }
  10876. parsePropertyName(prop) {
  10877. if (this.eat(0)) {
  10878. prop.computed = true;
  10879. prop.key = this.parseMaybeAssignAllowIn();
  10880. this.expect(3);
  10881. } else {
  10882. const {
  10883. type,
  10884. value
  10885. } = this.state;
  10886. let key;
  10887. if (tokenIsKeywordOrIdentifier(type)) {
  10888. key = this.parseIdentifier(true);
  10889. } else {
  10890. switch (type) {
  10891. case 126:
  10892. key = this.parseNumericLiteral(value);
  10893. break;
  10894. case 125:
  10895. key = this.parseStringLiteral(value);
  10896. break;
  10897. case 127:
  10898. key = this.parseBigIntLiteral(value);
  10899. break;
  10900. case 128:
  10901. key = this.parseDecimalLiteral(value);
  10902. break;
  10903. case 130:
  10904. {
  10905. this.raise(ErrorMessages.UnexpectedPrivateField, {
  10906. at: createPositionWithColumnOffset(this.state.startLoc, 1)
  10907. });
  10908. key = this.parsePrivateName();
  10909. break;
  10910. }
  10911. default:
  10912. throw this.unexpected();
  10913. }
  10914. }
  10915. prop.key = key;
  10916. if (type !== 130) {
  10917. prop.computed = false;
  10918. }
  10919. }
  10920. return prop.key;
  10921. }
  10922. initFunction(node, isAsync) {
  10923. node.id = null;
  10924. node.generator = false;
  10925. node.async = !!isAsync;
  10926. }
  10927. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  10928. this.initFunction(node, isAsync);
  10929. node.generator = !!isGenerator;
  10930. const allowModifiers = isConstructor;
  10931. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  10932. this.prodParam.enter(functionFlags(isAsync, node.generator));
  10933. this.parseFunctionParams(node, allowModifiers);
  10934. this.parseFunctionBodyAndFinish(node, type, true);
  10935. this.prodParam.exit();
  10936. this.scope.exit();
  10937. return node;
  10938. }
  10939. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  10940. if (isTuple) {
  10941. this.expectPlugin("recordAndTuple");
  10942. }
  10943. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10944. this.state.inFSharpPipelineDirectBody = false;
  10945. const node = this.startNode();
  10946. this.next();
  10947. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  10948. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10949. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  10950. }
  10951. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  10952. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  10953. let flags = functionFlags(isAsync, false);
  10954. if (!this.match(0) && this.prodParam.hasIn) {
  10955. flags |= PARAM_IN;
  10956. }
  10957. this.prodParam.enter(flags);
  10958. this.initFunction(node, isAsync);
  10959. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10960. if (params) {
  10961. this.state.maybeInArrowParameters = true;
  10962. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  10963. }
  10964. this.state.maybeInArrowParameters = false;
  10965. this.parseFunctionBody(node, true);
  10966. this.prodParam.exit();
  10967. this.scope.exit();
  10968. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10969. return this.finishNode(node, "ArrowFunctionExpression");
  10970. }
  10971. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  10972. node.params = this.toAssignableList(params, trailingCommaLoc, false);
  10973. }
  10974. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  10975. this.parseFunctionBody(node, false, isMethod);
  10976. this.finishNode(node, type);
  10977. }
  10978. parseFunctionBody(node, allowExpression, isMethod = false) {
  10979. const isExpression = allowExpression && !this.match(5);
  10980. this.expressionScope.enter(newExpressionScope());
  10981. if (isExpression) {
  10982. node.body = this.parseMaybeAssign();
  10983. this.checkParams(node, false, allowExpression, false);
  10984. } else {
  10985. const oldStrict = this.state.strict;
  10986. const oldLabels = this.state.labels;
  10987. this.state.labels = [];
  10988. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  10989. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  10990. const nonSimple = !this.isSimpleParamList(node.params);
  10991. if (hasStrictModeDirective && nonSimple) {
  10992. const errorOrigin = (node.kind === "method" || node.kind === "constructor") && !!node.key ? {
  10993. at: node.key.loc.end
  10994. } : {
  10995. node
  10996. };
  10997. this.raise(ErrorMessages.IllegalLanguageModeDirective, errorOrigin);
  10998. }
  10999. const strictModeChanged = !oldStrict && this.state.strict;
  11000. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11001. if (this.state.strict && node.id) {
  11002. this.checkLVal(node.id, "function name", BIND_OUTSIDE, undefined, undefined, strictModeChanged);
  11003. }
  11004. });
  11005. this.prodParam.exit();
  11006. this.state.labels = oldLabels;
  11007. }
  11008. this.expressionScope.exit();
  11009. }
  11010. isSimpleParamList(params) {
  11011. for (let i = 0, len = params.length; i < len; i++) {
  11012. if (params[i].type !== "Identifier") return false;
  11013. }
  11014. return true;
  11015. }
  11016. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11017. const checkClashes = new Set();
  11018. for (const param of node.params) {
  11019. this.checkLVal(param, "function parameter list", BIND_VAR, allowDuplicates ? null : checkClashes, undefined, strictModeChanged);
  11020. }
  11021. }
  11022. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11023. const elts = [];
  11024. let first = true;
  11025. while (!this.eat(close)) {
  11026. if (first) {
  11027. first = false;
  11028. } else {
  11029. this.expect(12);
  11030. if (this.match(close)) {
  11031. if (nodeForExtra) {
  11032. this.addTrailingCommaExtraToNode(nodeForExtra);
  11033. }
  11034. this.next();
  11035. break;
  11036. }
  11037. }
  11038. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11039. }
  11040. return elts;
  11041. }
  11042. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11043. let elt;
  11044. if (this.match(12)) {
  11045. if (!allowEmpty) {
  11046. this.raise(ErrorMessages.UnexpectedToken, {
  11047. at: this.state.curPosition()
  11048. }, ",");
  11049. }
  11050. elt = null;
  11051. } else if (this.match(21)) {
  11052. const spreadNodeStartPos = this.state.start;
  11053. const spreadNodeStartLoc = this.state.startLoc;
  11054. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  11055. } else if (this.match(17)) {
  11056. this.expectPlugin("partialApplication");
  11057. if (!allowPlaceholder) {
  11058. this.raise(ErrorMessages.UnexpectedArgumentPlaceholder, {
  11059. at: this.state.startLoc
  11060. });
  11061. }
  11062. const node = this.startNode();
  11063. this.next();
  11064. elt = this.finishNode(node, "ArgumentPlaceholder");
  11065. } else {
  11066. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11067. }
  11068. return elt;
  11069. }
  11070. parseIdentifier(liberal) {
  11071. const node = this.startNode();
  11072. const name = this.parseIdentifierName(node.start, liberal);
  11073. return this.createIdentifier(node, name);
  11074. }
  11075. createIdentifier(node, name) {
  11076. node.name = name;
  11077. node.loc.identifierName = name;
  11078. return this.finishNode(node, "Identifier");
  11079. }
  11080. parseIdentifierName(pos, liberal) {
  11081. let name;
  11082. const {
  11083. startLoc,
  11084. type
  11085. } = this.state;
  11086. if (tokenIsKeywordOrIdentifier(type)) {
  11087. name = this.state.value;
  11088. } else {
  11089. throw this.unexpected();
  11090. }
  11091. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11092. if (liberal) {
  11093. if (tokenIsKeyword) {
  11094. this.replaceToken(124);
  11095. }
  11096. } else {
  11097. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11098. }
  11099. this.next();
  11100. return name;
  11101. }
  11102. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11103. if (word.length > 10) {
  11104. return;
  11105. }
  11106. if (!canBeReservedWord(word)) {
  11107. return;
  11108. }
  11109. if (word === "yield") {
  11110. if (this.prodParam.hasYield) {
  11111. this.raise(ErrorMessages.YieldBindingIdentifier, {
  11112. at: startLoc
  11113. });
  11114. return;
  11115. }
  11116. } else if (word === "await") {
  11117. if (this.prodParam.hasAwait) {
  11118. this.raise(ErrorMessages.AwaitBindingIdentifier, {
  11119. at: startLoc
  11120. });
  11121. return;
  11122. }
  11123. if (this.scope.inStaticBlock) {
  11124. this.raise(ErrorMessages.AwaitBindingIdentifierInStaticBlock, {
  11125. at: startLoc
  11126. });
  11127. return;
  11128. }
  11129. this.expressionScope.recordAsyncArrowParametersError(ErrorMessages.AwaitBindingIdentifier, startLoc);
  11130. } else if (word === "arguments") {
  11131. if (this.scope.inClassAndNotInNonArrowFunction) {
  11132. this.raise(ErrorMessages.ArgumentsInClass, {
  11133. at: startLoc
  11134. });
  11135. return;
  11136. }
  11137. }
  11138. if (checkKeywords && isKeyword(word)) {
  11139. this.raise(ErrorMessages.UnexpectedKeyword, {
  11140. at: startLoc
  11141. }, word);
  11142. return;
  11143. }
  11144. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  11145. if (reservedTest(word, this.inModule)) {
  11146. this.raise(ErrorMessages.UnexpectedReservedWord, {
  11147. at: startLoc
  11148. }, word);
  11149. }
  11150. }
  11151. isAwaitAllowed() {
  11152. if (this.prodParam.hasAwait) return true;
  11153. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  11154. return true;
  11155. }
  11156. return false;
  11157. }
  11158. parseAwait(startPos, startLoc) {
  11159. const node = this.startNodeAt(startPos, startLoc);
  11160. this.expressionScope.recordParameterInitializerError(node.loc.start, ErrorMessages.AwaitExpressionFormalParameter);
  11161. if (this.eat(51)) {
  11162. this.raise(ErrorMessages.ObsoleteAwaitStar, {
  11163. node
  11164. });
  11165. }
  11166. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  11167. if (this.isAmbiguousAwait()) {
  11168. this.ambiguousScriptDifferentAst = true;
  11169. } else {
  11170. this.sawUnambiguousESM = true;
  11171. }
  11172. }
  11173. if (!this.state.soloAwait) {
  11174. node.argument = this.parseMaybeUnary(null, true);
  11175. }
  11176. return this.finishNode(node, "AwaitExpression");
  11177. }
  11178. isAmbiguousAwait() {
  11179. if (this.hasPrecedingLineBreak()) return true;
  11180. const {
  11181. type
  11182. } = this.state;
  11183. return type === 49 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 129 || type === 52 || this.hasPlugin("v8intrinsic") && type === 50;
  11184. }
  11185. parseYield() {
  11186. const node = this.startNode();
  11187. this.expressionScope.recordParameterInitializerError(node.loc.start, ErrorMessages.YieldInParameter);
  11188. this.next();
  11189. let delegating = false;
  11190. let argument = null;
  11191. if (!this.hasPrecedingLineBreak()) {
  11192. delegating = this.eat(51);
  11193. switch (this.state.type) {
  11194. case 13:
  11195. case 131:
  11196. case 8:
  11197. case 11:
  11198. case 3:
  11199. case 9:
  11200. case 14:
  11201. case 12:
  11202. if (!delegating) break;
  11203. default:
  11204. argument = this.parseMaybeAssign();
  11205. }
  11206. }
  11207. node.delegate = delegating;
  11208. node.argument = argument;
  11209. return this.finishNode(node, "YieldExpression");
  11210. }
  11211. checkPipelineAtInfixOperator(left, leftStartLoc) {
  11212. if (this.hasPlugin(["pipelineOperator", {
  11213. proposal: "smart"
  11214. }])) {
  11215. if (left.type === "SequenceExpression") {
  11216. this.raise(ErrorMessages.PipelineHeadSequenceExpression, {
  11217. at: leftStartLoc
  11218. });
  11219. }
  11220. }
  11221. }
  11222. parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
  11223. const bodyNode = this.startNodeAt(startPos, startLoc);
  11224. if (this.isSimpleReference(childExpr)) {
  11225. bodyNode.callee = childExpr;
  11226. return this.finishNode(bodyNode, "PipelineBareFunction");
  11227. } else {
  11228. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  11229. bodyNode.expression = childExpr;
  11230. return this.finishNode(bodyNode, "PipelineTopicExpression");
  11231. }
  11232. }
  11233. isSimpleReference(expression) {
  11234. switch (expression.type) {
  11235. case "MemberExpression":
  11236. return !expression.computed && this.isSimpleReference(expression.object);
  11237. case "Identifier":
  11238. return true;
  11239. default:
  11240. return false;
  11241. }
  11242. }
  11243. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  11244. if (this.match(19)) {
  11245. throw this.raise(ErrorMessages.PipelineBodyNoArrow, {
  11246. at: this.state.startLoc
  11247. });
  11248. }
  11249. if (!this.topicReferenceWasUsedInCurrentContext()) {
  11250. this.raise(ErrorMessages.PipelineTopicUnused, {
  11251. at: startLoc
  11252. });
  11253. }
  11254. }
  11255. withTopicBindingContext(callback) {
  11256. const outerContextTopicState = this.state.topicContext;
  11257. this.state.topicContext = {
  11258. maxNumOfResolvableTopics: 1,
  11259. maxTopicIndex: null
  11260. };
  11261. try {
  11262. return callback();
  11263. } finally {
  11264. this.state.topicContext = outerContextTopicState;
  11265. }
  11266. }
  11267. withSmartMixTopicForbiddingContext(callback) {
  11268. if (this.hasPlugin(["pipelineOperator", {
  11269. proposal: "smart"
  11270. }])) {
  11271. const outerContextTopicState = this.state.topicContext;
  11272. this.state.topicContext = {
  11273. maxNumOfResolvableTopics: 0,
  11274. maxTopicIndex: null
  11275. };
  11276. try {
  11277. return callback();
  11278. } finally {
  11279. this.state.topicContext = outerContextTopicState;
  11280. }
  11281. } else {
  11282. return callback();
  11283. }
  11284. }
  11285. withSoloAwaitPermittingContext(callback) {
  11286. const outerContextSoloAwaitState = this.state.soloAwait;
  11287. this.state.soloAwait = true;
  11288. try {
  11289. return callback();
  11290. } finally {
  11291. this.state.soloAwait = outerContextSoloAwaitState;
  11292. }
  11293. }
  11294. allowInAnd(callback) {
  11295. const flags = this.prodParam.currentFlags();
  11296. const prodParamToSet = PARAM_IN & ~flags;
  11297. if (prodParamToSet) {
  11298. this.prodParam.enter(flags | PARAM_IN);
  11299. try {
  11300. return callback();
  11301. } finally {
  11302. this.prodParam.exit();
  11303. }
  11304. }
  11305. return callback();
  11306. }
  11307. disallowInAnd(callback) {
  11308. const flags = this.prodParam.currentFlags();
  11309. const prodParamToClear = PARAM_IN & flags;
  11310. if (prodParamToClear) {
  11311. this.prodParam.enter(flags & ~PARAM_IN);
  11312. try {
  11313. return callback();
  11314. } finally {
  11315. this.prodParam.exit();
  11316. }
  11317. }
  11318. return callback();
  11319. }
  11320. registerTopicReference() {
  11321. this.state.topicContext.maxTopicIndex = 0;
  11322. }
  11323. topicReferenceIsAllowedInCurrentContext() {
  11324. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  11325. }
  11326. topicReferenceWasUsedInCurrentContext() {
  11327. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  11328. }
  11329. parseFSharpPipelineBody(prec) {
  11330. const startPos = this.state.start;
  11331. const startLoc = this.state.startLoc;
  11332. this.state.potentialArrowAt = this.state.start;
  11333. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11334. this.state.inFSharpPipelineDirectBody = true;
  11335. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
  11336. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11337. return ret;
  11338. }
  11339. parseModuleExpression() {
  11340. this.expectPlugin("moduleBlocks");
  11341. const node = this.startNode();
  11342. this.next();
  11343. this.eat(5);
  11344. const revertScopes = this.initializeScopes(true);
  11345. this.enterInitialScopes();
  11346. const program = this.startNode();
  11347. try {
  11348. node.body = this.parseProgram(program, 8, "module");
  11349. } finally {
  11350. revertScopes();
  11351. }
  11352. this.eat(8);
  11353. return this.finishNode(node, "ModuleExpression");
  11354. }
  11355. parsePropertyNamePrefixOperator(prop) {}
  11356. }
  11357. const loopLabel = {
  11358. kind: "loop"
  11359. },
  11360. switchLabel = {
  11361. kind: "switch"
  11362. };
  11363. const FUNC_NO_FLAGS = 0b000,
  11364. FUNC_STATEMENT = 0b001,
  11365. FUNC_HANGING_STATEMENT = 0b010,
  11366. FUNC_NULLABLE_ID = 0b100;
  11367. const loneSurrogate = /[\uD800-\uDFFF]/u;
  11368. const keywordRelationalOperator = /in(?:stanceof)?/y;
  11369. function babel7CompatTokens(tokens, input) {
  11370. for (let i = 0; i < tokens.length; i++) {
  11371. const token = tokens[i];
  11372. const {
  11373. type
  11374. } = token;
  11375. if (typeof type === "number") {
  11376. {
  11377. if (type === 130) {
  11378. const {
  11379. loc,
  11380. start,
  11381. value,
  11382. end
  11383. } = token;
  11384. const hashEndPos = start + 1;
  11385. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  11386. tokens.splice(i, 1, new Token({
  11387. type: getExportedToken(27),
  11388. value: "#",
  11389. start: start,
  11390. end: hashEndPos,
  11391. startLoc: loc.start,
  11392. endLoc: hashEndLoc
  11393. }), new Token({
  11394. type: getExportedToken(124),
  11395. value: value,
  11396. start: hashEndPos,
  11397. end: end,
  11398. startLoc: hashEndLoc,
  11399. endLoc: loc.end
  11400. }));
  11401. i++;
  11402. continue;
  11403. }
  11404. if (tokenIsTemplate(type)) {
  11405. const {
  11406. loc,
  11407. start,
  11408. value,
  11409. end
  11410. } = token;
  11411. const backquoteEnd = start + 1;
  11412. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  11413. let startToken;
  11414. if (input.charCodeAt(start) === 96) {
  11415. startToken = new Token({
  11416. type: getExportedToken(22),
  11417. value: "`",
  11418. start: start,
  11419. end: backquoteEnd,
  11420. startLoc: loc.start,
  11421. endLoc: backquoteEndLoc
  11422. });
  11423. } else {
  11424. startToken = new Token({
  11425. type: getExportedToken(8),
  11426. value: "}",
  11427. start: start,
  11428. end: backquoteEnd,
  11429. startLoc: loc.start,
  11430. endLoc: backquoteEndLoc
  11431. });
  11432. }
  11433. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  11434. if (type === 24) {
  11435. templateElementEnd = end - 1;
  11436. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  11437. templateValue = value === null ? null : value.slice(1, -1);
  11438. endToken = new Token({
  11439. type: getExportedToken(22),
  11440. value: "`",
  11441. start: templateElementEnd,
  11442. end: end,
  11443. startLoc: templateElementEndLoc,
  11444. endLoc: loc.end
  11445. });
  11446. } else {
  11447. templateElementEnd = end - 2;
  11448. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  11449. templateValue = value === null ? null : value.slice(1, -2);
  11450. endToken = new Token({
  11451. type: getExportedToken(23),
  11452. value: "${",
  11453. start: templateElementEnd,
  11454. end: end,
  11455. startLoc: templateElementEndLoc,
  11456. endLoc: loc.end
  11457. });
  11458. }
  11459. tokens.splice(i, 1, startToken, new Token({
  11460. type: getExportedToken(20),
  11461. value: templateValue,
  11462. start: backquoteEnd,
  11463. end: templateElementEnd,
  11464. startLoc: backquoteEndLoc,
  11465. endLoc: templateElementEndLoc
  11466. }), endToken);
  11467. i += 2;
  11468. continue;
  11469. }
  11470. }
  11471. token.type = getExportedToken(type);
  11472. }
  11473. }
  11474. return tokens;
  11475. }
  11476. class StatementParser extends ExpressionParser {
  11477. parseTopLevel(file, program) {
  11478. file.program = this.parseProgram(program);
  11479. file.comments = this.state.comments;
  11480. if (this.options.tokens) {
  11481. file.tokens = babel7CompatTokens(this.tokens, this.input);
  11482. }
  11483. return this.finishNode(file, "File");
  11484. }
  11485. parseProgram(program, end = 131, sourceType = this.options.sourceType) {
  11486. program.sourceType = sourceType;
  11487. program.interpreter = this.parseInterpreterDirective();
  11488. this.parseBlockBody(program, true, true, end);
  11489. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  11490. for (const [name, loc] of Array.from(this.scope.undefinedExports)) {
  11491. this.raise(ErrorMessages.ModuleExportUndefined, {
  11492. at: loc
  11493. }, name);
  11494. }
  11495. }
  11496. return this.finishNode(program, "Program");
  11497. }
  11498. stmtToDirective(stmt) {
  11499. const directive = stmt;
  11500. directive.type = "Directive";
  11501. directive.value = directive.expression;
  11502. delete directive.expression;
  11503. const directiveLiteral = directive.value;
  11504. const expressionValue = directiveLiteral.value;
  11505. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  11506. const val = directiveLiteral.value = raw.slice(1, -1);
  11507. this.addExtra(directiveLiteral, "raw", raw);
  11508. this.addExtra(directiveLiteral, "rawValue", val);
  11509. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  11510. directiveLiteral.type = "DirectiveLiteral";
  11511. return directive;
  11512. }
  11513. parseInterpreterDirective() {
  11514. if (!this.match(28)) {
  11515. return null;
  11516. }
  11517. const node = this.startNode();
  11518. node.value = this.state.value;
  11519. this.next();
  11520. return this.finishNode(node, "InterpreterDirective");
  11521. }
  11522. isLet(context) {
  11523. if (!this.isContextual(95)) {
  11524. return false;
  11525. }
  11526. return this.isLetKeyword(context);
  11527. }
  11528. isLetKeyword(context) {
  11529. const next = this.nextTokenStart();
  11530. const nextCh = this.codePointAtPos(next);
  11531. if (nextCh === 92 || nextCh === 91) {
  11532. return true;
  11533. }
  11534. if (context) return false;
  11535. if (nextCh === 123) return true;
  11536. if (isIdentifierStart(nextCh)) {
  11537. keywordRelationalOperator.lastIndex = next;
  11538. if (keywordRelationalOperator.test(this.input)) {
  11539. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  11540. if (!isIdentifierChar(endCh) && endCh !== 92) {
  11541. return false;
  11542. }
  11543. }
  11544. return true;
  11545. }
  11546. return false;
  11547. }
  11548. parseStatement(context, topLevel) {
  11549. if (this.match(26)) {
  11550. this.parseDecorators(true);
  11551. }
  11552. return this.parseStatementContent(context, topLevel);
  11553. }
  11554. parseStatementContent(context, topLevel) {
  11555. let starttype = this.state.type;
  11556. const node = this.startNode();
  11557. let kind;
  11558. if (this.isLet(context)) {
  11559. starttype = 70;
  11560. kind = "let";
  11561. }
  11562. switch (starttype) {
  11563. case 56:
  11564. return this.parseBreakContinueStatement(node, true);
  11565. case 59:
  11566. return this.parseBreakContinueStatement(node, false);
  11567. case 60:
  11568. return this.parseDebuggerStatement(node);
  11569. case 86:
  11570. return this.parseDoStatement(node);
  11571. case 87:
  11572. return this.parseForStatement(node);
  11573. case 64:
  11574. if (this.lookaheadCharCode() === 46) break;
  11575. if (context) {
  11576. if (this.state.strict) {
  11577. this.raise(ErrorMessages.StrictFunction, {
  11578. at: this.state.startLoc
  11579. });
  11580. } else if (context !== "if" && context !== "label") {
  11581. this.raise(ErrorMessages.SloppyFunction, {
  11582. at: this.state.startLoc
  11583. });
  11584. }
  11585. }
  11586. return this.parseFunctionStatement(node, false, !context);
  11587. case 76:
  11588. if (context) this.unexpected();
  11589. return this.parseClass(node, true);
  11590. case 65:
  11591. return this.parseIfStatement(node);
  11592. case 66:
  11593. return this.parseReturnStatement(node);
  11594. case 67:
  11595. return this.parseSwitchStatement(node);
  11596. case 68:
  11597. return this.parseThrowStatement(node);
  11598. case 69:
  11599. return this.parseTryStatement(node);
  11600. case 71:
  11601. case 70:
  11602. kind = kind || this.state.value;
  11603. if (context && kind !== "var") {
  11604. this.raise(ErrorMessages.UnexpectedLexicalDeclaration, {
  11605. at: this.state.startLoc
  11606. });
  11607. }
  11608. return this.parseVarStatement(node, kind);
  11609. case 88:
  11610. return this.parseWhileStatement(node);
  11611. case 72:
  11612. return this.parseWithStatement(node);
  11613. case 5:
  11614. return this.parseBlock();
  11615. case 13:
  11616. return this.parseEmptyStatement(node);
  11617. case 79:
  11618. {
  11619. const nextTokenCharCode = this.lookaheadCharCode();
  11620. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  11621. break;
  11622. }
  11623. }
  11624. case 78:
  11625. {
  11626. if (!this.options.allowImportExportEverywhere && !topLevel) {
  11627. this.raise(ErrorMessages.UnexpectedImportExport, {
  11628. at: this.state.startLoc
  11629. });
  11630. }
  11631. this.next();
  11632. let result;
  11633. if (starttype === 79) {
  11634. result = this.parseImport(node);
  11635. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  11636. this.sawUnambiguousESM = true;
  11637. }
  11638. } else {
  11639. result = this.parseExport(node);
  11640. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  11641. this.sawUnambiguousESM = true;
  11642. }
  11643. }
  11644. this.assertModuleNodeAllowed(node);
  11645. return result;
  11646. }
  11647. default:
  11648. {
  11649. if (this.isAsyncFunction()) {
  11650. if (context) {
  11651. this.raise(ErrorMessages.AsyncFunctionInSingleStatementContext, {
  11652. at: this.state.startLoc
  11653. });
  11654. }
  11655. this.next();
  11656. return this.parseFunctionStatement(node, true, !context);
  11657. }
  11658. }
  11659. }
  11660. const maybeName = this.state.value;
  11661. const expr = this.parseExpression();
  11662. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  11663. return this.parseLabeledStatement(node, maybeName, expr, context);
  11664. } else {
  11665. return this.parseExpressionStatement(node, expr);
  11666. }
  11667. }
  11668. assertModuleNodeAllowed(node) {
  11669. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  11670. this.raise(SourceTypeModuleErrorMessages.ImportOutsideModule, {
  11671. node
  11672. });
  11673. }
  11674. }
  11675. takeDecorators(node) {
  11676. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11677. if (decorators.length) {
  11678. node.decorators = decorators;
  11679. this.resetStartLocationFromNode(node, decorators[0]);
  11680. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  11681. }
  11682. }
  11683. canHaveLeadingDecorator() {
  11684. return this.match(76);
  11685. }
  11686. parseDecorators(allowExport) {
  11687. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11688. while (this.match(26)) {
  11689. const decorator = this.parseDecorator();
  11690. currentContextDecorators.push(decorator);
  11691. }
  11692. if (this.match(78)) {
  11693. if (!allowExport) {
  11694. this.unexpected();
  11695. }
  11696. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11697. this.raise(ErrorMessages.DecoratorExportClass, {
  11698. at: this.state.startLoc
  11699. });
  11700. }
  11701. } else if (!this.canHaveLeadingDecorator()) {
  11702. throw this.raise(ErrorMessages.UnexpectedLeadingDecorator, {
  11703. at: this.state.startLoc
  11704. });
  11705. }
  11706. }
  11707. parseDecorator() {
  11708. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  11709. const node = this.startNode();
  11710. this.next();
  11711. if (this.hasPlugin("decorators")) {
  11712. this.state.decoratorStack.push([]);
  11713. const startPos = this.state.start;
  11714. const startLoc = this.state.startLoc;
  11715. let expr;
  11716. if (this.eat(10)) {
  11717. expr = this.parseExpression();
  11718. this.expect(11);
  11719. } else {
  11720. expr = this.parseIdentifier(false);
  11721. while (this.eat(16)) {
  11722. const node = this.startNodeAt(startPos, startLoc);
  11723. node.object = expr;
  11724. node.property = this.parseIdentifier(true);
  11725. node.computed = false;
  11726. expr = this.finishNode(node, "MemberExpression");
  11727. }
  11728. }
  11729. node.expression = this.parseMaybeDecoratorArguments(expr);
  11730. this.state.decoratorStack.pop();
  11731. } else {
  11732. node.expression = this.parseExprSubscripts();
  11733. }
  11734. return this.finishNode(node, "Decorator");
  11735. }
  11736. parseMaybeDecoratorArguments(expr) {
  11737. if (this.eat(10)) {
  11738. const node = this.startNodeAtNode(expr);
  11739. node.callee = expr;
  11740. node.arguments = this.parseCallExpressionArguments(11, false);
  11741. this.toReferencedList(node.arguments);
  11742. return this.finishNode(node, "CallExpression");
  11743. }
  11744. return expr;
  11745. }
  11746. parseBreakContinueStatement(node, isBreak) {
  11747. this.next();
  11748. if (this.isLineTerminator()) {
  11749. node.label = null;
  11750. } else {
  11751. node.label = this.parseIdentifier();
  11752. this.semicolon();
  11753. }
  11754. this.verifyBreakContinue(node, isBreak);
  11755. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  11756. }
  11757. verifyBreakContinue(node, isBreak) {
  11758. let i;
  11759. for (i = 0; i < this.state.labels.length; ++i) {
  11760. const lab = this.state.labels[i];
  11761. if (node.label == null || lab.name === node.label.name) {
  11762. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  11763. if (node.label && isBreak) break;
  11764. }
  11765. }
  11766. if (i === this.state.labels.length) {
  11767. this.raise(ErrorMessages.IllegalBreakContinue, {
  11768. node
  11769. }, isBreak ? "break" : "continue");
  11770. }
  11771. }
  11772. parseDebuggerStatement(node) {
  11773. this.next();
  11774. this.semicolon();
  11775. return this.finishNode(node, "DebuggerStatement");
  11776. }
  11777. parseHeaderExpression() {
  11778. this.expect(10);
  11779. const val = this.parseExpression();
  11780. this.expect(11);
  11781. return val;
  11782. }
  11783. parseDoStatement(node) {
  11784. this.next();
  11785. this.state.labels.push(loopLabel);
  11786. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
  11787. this.state.labels.pop();
  11788. this.expect(88);
  11789. node.test = this.parseHeaderExpression();
  11790. this.eat(13);
  11791. return this.finishNode(node, "DoWhileStatement");
  11792. }
  11793. parseForStatement(node) {
  11794. this.next();
  11795. this.state.labels.push(loopLabel);
  11796. let awaitAt = null;
  11797. if (this.isAwaitAllowed() && this.eatContextual(92)) {
  11798. awaitAt = this.state.lastTokStartLoc;
  11799. }
  11800. this.scope.enter(SCOPE_OTHER);
  11801. this.expect(10);
  11802. if (this.match(13)) {
  11803. if (awaitAt !== null) {
  11804. this.unexpected(awaitAt);
  11805. }
  11806. return this.parseFor(node, null);
  11807. }
  11808. const startsWithLet = this.isContextual(95);
  11809. const isLet = startsWithLet && this.isLetKeyword();
  11810. if (this.match(70) || this.match(71) || isLet) {
  11811. const init = this.startNode();
  11812. const kind = isLet ? "let" : this.state.value;
  11813. this.next();
  11814. this.parseVar(init, true, kind);
  11815. this.finishNode(init, "VariableDeclaration");
  11816. if ((this.match(54) || this.isContextual(97)) && init.declarations.length === 1) {
  11817. return this.parseForIn(node, init, awaitAt);
  11818. }
  11819. if (awaitAt !== null) {
  11820. this.unexpected(awaitAt);
  11821. }
  11822. return this.parseFor(node, init);
  11823. }
  11824. const startsWithAsync = this.isContextual(91);
  11825. const refExpressionErrors = new ExpressionErrors();
  11826. const init = this.parseExpression(true, refExpressionErrors);
  11827. const isForOf = this.isContextual(97);
  11828. if (isForOf) {
  11829. if (startsWithLet) {
  11830. this.raise(ErrorMessages.ForOfLet, {
  11831. node: init
  11832. });
  11833. }
  11834. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  11835. this.raise(ErrorMessages.ForOfAsync, {
  11836. node: init
  11837. });
  11838. }
  11839. }
  11840. if (isForOf || this.match(54)) {
  11841. this.toAssignable(init, true);
  11842. const description = isForOf ? "for-of statement" : "for-in statement";
  11843. this.checkLVal(init, description);
  11844. return this.parseForIn(node, init, awaitAt);
  11845. } else {
  11846. this.checkExpressionErrors(refExpressionErrors, true);
  11847. }
  11848. if (awaitAt !== null) {
  11849. this.unexpected(awaitAt);
  11850. }
  11851. return this.parseFor(node, init);
  11852. }
  11853. parseFunctionStatement(node, isAsync, declarationPosition) {
  11854. this.next();
  11855. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  11856. }
  11857. parseIfStatement(node) {
  11858. this.next();
  11859. node.test = this.parseHeaderExpression();
  11860. node.consequent = this.parseStatement("if");
  11861. node.alternate = this.eat(62) ? this.parseStatement("if") : null;
  11862. return this.finishNode(node, "IfStatement");
  11863. }
  11864. parseReturnStatement(node) {
  11865. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  11866. this.raise(ErrorMessages.IllegalReturn, {
  11867. at: this.state.startLoc
  11868. });
  11869. }
  11870. this.next();
  11871. if (this.isLineTerminator()) {
  11872. node.argument = null;
  11873. } else {
  11874. node.argument = this.parseExpression();
  11875. this.semicolon();
  11876. }
  11877. return this.finishNode(node, "ReturnStatement");
  11878. }
  11879. parseSwitchStatement(node) {
  11880. this.next();
  11881. node.discriminant = this.parseHeaderExpression();
  11882. const cases = node.cases = [];
  11883. this.expect(5);
  11884. this.state.labels.push(switchLabel);
  11885. this.scope.enter(SCOPE_OTHER);
  11886. let cur;
  11887. for (let sawDefault; !this.match(8);) {
  11888. if (this.match(57) || this.match(61)) {
  11889. const isCase = this.match(57);
  11890. if (cur) this.finishNode(cur, "SwitchCase");
  11891. cases.push(cur = this.startNode());
  11892. cur.consequent = [];
  11893. this.next();
  11894. if (isCase) {
  11895. cur.test = this.parseExpression();
  11896. } else {
  11897. if (sawDefault) {
  11898. this.raise(ErrorMessages.MultipleDefaultsInSwitch, {
  11899. at: this.state.lastTokStartLoc
  11900. });
  11901. }
  11902. sawDefault = true;
  11903. cur.test = null;
  11904. }
  11905. this.expect(14);
  11906. } else {
  11907. if (cur) {
  11908. cur.consequent.push(this.parseStatement(null));
  11909. } else {
  11910. this.unexpected();
  11911. }
  11912. }
  11913. }
  11914. this.scope.exit();
  11915. if (cur) this.finishNode(cur, "SwitchCase");
  11916. this.next();
  11917. this.state.labels.pop();
  11918. return this.finishNode(node, "SwitchStatement");
  11919. }
  11920. parseThrowStatement(node) {
  11921. this.next();
  11922. if (this.hasPrecedingLineBreak()) {
  11923. this.raise(ErrorMessages.NewlineAfterThrow, {
  11924. at: this.state.lastTokEndLoc
  11925. });
  11926. }
  11927. node.argument = this.parseExpression();
  11928. this.semicolon();
  11929. return this.finishNode(node, "ThrowStatement");
  11930. }
  11931. parseCatchClauseParam() {
  11932. const param = this.parseBindingAtom();
  11933. const simple = param.type === "Identifier";
  11934. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  11935. this.checkLVal(param, "catch clause", BIND_LEXICAL);
  11936. return param;
  11937. }
  11938. parseTryStatement(node) {
  11939. this.next();
  11940. node.block = this.parseBlock();
  11941. node.handler = null;
  11942. if (this.match(58)) {
  11943. const clause = this.startNode();
  11944. this.next();
  11945. if (this.match(10)) {
  11946. this.expect(10);
  11947. clause.param = this.parseCatchClauseParam();
  11948. this.expect(11);
  11949. } else {
  11950. clause.param = null;
  11951. this.scope.enter(SCOPE_OTHER);
  11952. }
  11953. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  11954. this.scope.exit();
  11955. node.handler = this.finishNode(clause, "CatchClause");
  11956. }
  11957. node.finalizer = this.eat(63) ? this.parseBlock() : null;
  11958. if (!node.handler && !node.finalizer) {
  11959. this.raise(ErrorMessages.NoCatchOrFinally, {
  11960. node
  11961. });
  11962. }
  11963. return this.finishNode(node, "TryStatement");
  11964. }
  11965. parseVarStatement(node, kind) {
  11966. this.next();
  11967. this.parseVar(node, false, kind);
  11968. this.semicolon();
  11969. return this.finishNode(node, "VariableDeclaration");
  11970. }
  11971. parseWhileStatement(node) {
  11972. this.next();
  11973. node.test = this.parseHeaderExpression();
  11974. this.state.labels.push(loopLabel);
  11975. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
  11976. this.state.labels.pop();
  11977. return this.finishNode(node, "WhileStatement");
  11978. }
  11979. parseWithStatement(node) {
  11980. if (this.state.strict) {
  11981. this.raise(ErrorMessages.StrictWith, {
  11982. at: this.state.startLoc
  11983. });
  11984. }
  11985. this.next();
  11986. node.object = this.parseHeaderExpression();
  11987. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
  11988. return this.finishNode(node, "WithStatement");
  11989. }
  11990. parseEmptyStatement(node) {
  11991. this.next();
  11992. return this.finishNode(node, "EmptyStatement");
  11993. }
  11994. parseLabeledStatement(node, maybeName, expr, context) {
  11995. for (const label of this.state.labels) {
  11996. if (label.name === maybeName) {
  11997. this.raise(ErrorMessages.LabelRedeclaration, {
  11998. node: expr
  11999. }, maybeName);
  12000. }
  12001. }
  12002. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(67) ? "switch" : null;
  12003. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  12004. const label = this.state.labels[i];
  12005. if (label.statementStart === node.start) {
  12006. label.statementStart = this.state.start;
  12007. label.kind = kind;
  12008. } else {
  12009. break;
  12010. }
  12011. }
  12012. this.state.labels.push({
  12013. name: maybeName,
  12014. kind: kind,
  12015. statementStart: this.state.start
  12016. });
  12017. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  12018. this.state.labels.pop();
  12019. node.label = expr;
  12020. return this.finishNode(node, "LabeledStatement");
  12021. }
  12022. parseExpressionStatement(node, expr) {
  12023. node.expression = expr;
  12024. this.semicolon();
  12025. return this.finishNode(node, "ExpressionStatement");
  12026. }
  12027. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  12028. const node = this.startNode();
  12029. if (allowDirectives) {
  12030. this.state.strictErrors.clear();
  12031. }
  12032. this.expect(5);
  12033. if (createNewLexicalScope) {
  12034. this.scope.enter(SCOPE_OTHER);
  12035. }
  12036. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  12037. if (createNewLexicalScope) {
  12038. this.scope.exit();
  12039. }
  12040. return this.finishNode(node, "BlockStatement");
  12041. }
  12042. isValidDirective(stmt) {
  12043. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  12044. }
  12045. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  12046. const body = node.body = [];
  12047. const directives = node.directives = [];
  12048. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  12049. }
  12050. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  12051. const oldStrict = this.state.strict;
  12052. let hasStrictModeDirective = false;
  12053. let parsedNonDirective = false;
  12054. while (!this.match(end)) {
  12055. const stmt = this.parseStatement(null, topLevel);
  12056. if (directives && !parsedNonDirective) {
  12057. if (this.isValidDirective(stmt)) {
  12058. const directive = this.stmtToDirective(stmt);
  12059. directives.push(directive);
  12060. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  12061. hasStrictModeDirective = true;
  12062. this.setStrict(true);
  12063. }
  12064. continue;
  12065. }
  12066. parsedNonDirective = true;
  12067. this.state.strictErrors.clear();
  12068. }
  12069. body.push(stmt);
  12070. }
  12071. if (afterBlockParse) {
  12072. afterBlockParse.call(this, hasStrictModeDirective);
  12073. }
  12074. if (!oldStrict) {
  12075. this.setStrict(false);
  12076. }
  12077. this.next();
  12078. }
  12079. parseFor(node, init) {
  12080. node.init = init;
  12081. this.semicolon(false);
  12082. node.test = this.match(13) ? null : this.parseExpression();
  12083. this.semicolon(false);
  12084. node.update = this.match(11) ? null : this.parseExpression();
  12085. this.expect(11);
  12086. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12087. this.scope.exit();
  12088. this.state.labels.pop();
  12089. return this.finishNode(node, "ForStatement");
  12090. }
  12091. parseForIn(node, init, awaitAt) {
  12092. const isForIn = this.match(54);
  12093. this.next();
  12094. if (isForIn) {
  12095. if (awaitAt !== null) this.unexpected(awaitAt);
  12096. } else {
  12097. node.await = awaitAt !== null;
  12098. }
  12099. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  12100. this.raise(ErrorMessages.ForInOfLoopInitializer, {
  12101. node: init
  12102. }, isForIn ? "for-in" : "for-of");
  12103. }
  12104. if (init.type === "AssignmentPattern") {
  12105. this.raise(ErrorMessages.InvalidLhs, {
  12106. node: init
  12107. }, "for-loop");
  12108. }
  12109. node.left = init;
  12110. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  12111. this.expect(11);
  12112. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12113. this.scope.exit();
  12114. this.state.labels.pop();
  12115. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  12116. }
  12117. parseVar(node, isFor, kind) {
  12118. const declarations = node.declarations = [];
  12119. const isTypescript = this.hasPlugin("typescript");
  12120. node.kind = kind;
  12121. for (;;) {
  12122. const decl = this.startNode();
  12123. this.parseVarId(decl, kind);
  12124. if (this.eat(29)) {
  12125. decl.init = isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  12126. } else {
  12127. if (kind === "const" && !(this.match(54) || this.isContextual(97))) {
  12128. if (!isTypescript) {
  12129. this.raise(ErrorMessages.DeclarationMissingInitializer, {
  12130. at: this.state.lastTokEndLoc
  12131. }, "Const declarations");
  12132. }
  12133. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(54) || this.isContextual(97)))) {
  12134. this.raise(ErrorMessages.DeclarationMissingInitializer, {
  12135. at: this.state.lastTokEndLoc
  12136. }, "Complex binding patterns");
  12137. }
  12138. decl.init = null;
  12139. }
  12140. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  12141. if (!this.eat(12)) break;
  12142. }
  12143. return node;
  12144. }
  12145. parseVarId(decl, kind) {
  12146. decl.id = this.parseBindingAtom();
  12147. this.checkLVal(decl.id, "variable declaration", kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, kind !== "var");
  12148. }
  12149. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  12150. const isStatement = statement & FUNC_STATEMENT;
  12151. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  12152. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  12153. this.initFunction(node, isAsync);
  12154. if (this.match(51) && isHangingStatement) {
  12155. this.raise(ErrorMessages.GeneratorInSingleStatementContext, {
  12156. at: this.state.startLoc
  12157. });
  12158. }
  12159. node.generator = this.eat(51);
  12160. if (isStatement) {
  12161. node.id = this.parseFunctionId(requireId);
  12162. }
  12163. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12164. this.state.maybeInArrowParameters = false;
  12165. this.scope.enter(SCOPE_FUNCTION);
  12166. this.prodParam.enter(functionFlags(isAsync, node.generator));
  12167. if (!isStatement) {
  12168. node.id = this.parseFunctionId();
  12169. }
  12170. this.parseFunctionParams(node, false);
  12171. this.withSmartMixTopicForbiddingContext(() => {
  12172. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  12173. });
  12174. this.prodParam.exit();
  12175. this.scope.exit();
  12176. if (isStatement && !isHangingStatement) {
  12177. this.registerFunctionStatementId(node);
  12178. }
  12179. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12180. return node;
  12181. }
  12182. parseFunctionId(requireId) {
  12183. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  12184. }
  12185. parseFunctionParams(node, allowModifiers) {
  12186. this.expect(10);
  12187. this.expressionScope.enter(newParameterDeclarationScope());
  12188. node.params = this.parseBindingList(11, 41, false, allowModifiers);
  12189. this.expressionScope.exit();
  12190. }
  12191. registerFunctionStatementId(node) {
  12192. if (!node.id) return;
  12193. 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);
  12194. }
  12195. parseClass(node, isStatement, optionalId) {
  12196. this.next();
  12197. this.takeDecorators(node);
  12198. const oldStrict = this.state.strict;
  12199. this.state.strict = true;
  12200. this.parseClassId(node, isStatement, optionalId);
  12201. this.parseClassSuper(node);
  12202. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  12203. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  12204. }
  12205. isClassProperty() {
  12206. return this.match(29) || this.match(13) || this.match(8);
  12207. }
  12208. isClassMethod() {
  12209. return this.match(10);
  12210. }
  12211. isNonstaticConstructor(method) {
  12212. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  12213. }
  12214. parseClassBody(hadSuperClass, oldStrict) {
  12215. this.classScope.enter();
  12216. const state = {
  12217. hadConstructor: false,
  12218. hadSuperClass
  12219. };
  12220. let decorators = [];
  12221. const classBody = this.startNode();
  12222. classBody.body = [];
  12223. this.expect(5);
  12224. this.withSmartMixTopicForbiddingContext(() => {
  12225. while (!this.match(8)) {
  12226. if (this.eat(13)) {
  12227. if (decorators.length > 0) {
  12228. throw this.raise(ErrorMessages.DecoratorSemicolon, {
  12229. at: this.state.lastTokEndLoc
  12230. });
  12231. }
  12232. continue;
  12233. }
  12234. if (this.match(26)) {
  12235. decorators.push(this.parseDecorator());
  12236. continue;
  12237. }
  12238. const member = this.startNode();
  12239. if (decorators.length) {
  12240. member.decorators = decorators;
  12241. this.resetStartLocationFromNode(member, decorators[0]);
  12242. decorators = [];
  12243. }
  12244. this.parseClassMember(classBody, member, state);
  12245. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  12246. this.raise(ErrorMessages.DecoratorConstructor, {
  12247. node: member
  12248. });
  12249. }
  12250. }
  12251. });
  12252. this.state.strict = oldStrict;
  12253. this.next();
  12254. if (decorators.length) {
  12255. throw this.raise(ErrorMessages.TrailingDecorator, {
  12256. at: this.state.startLoc
  12257. });
  12258. }
  12259. this.classScope.exit();
  12260. return this.finishNode(classBody, "ClassBody");
  12261. }
  12262. parseClassMemberFromModifier(classBody, member) {
  12263. const key = this.parseIdentifier(true);
  12264. if (this.isClassMethod()) {
  12265. const method = member;
  12266. method.kind = "method";
  12267. method.computed = false;
  12268. method.key = key;
  12269. method.static = false;
  12270. this.pushClassMethod(classBody, method, false, false, false, false);
  12271. return true;
  12272. } else if (this.isClassProperty()) {
  12273. const prop = member;
  12274. prop.computed = false;
  12275. prop.key = key;
  12276. prop.static = false;
  12277. classBody.body.push(this.parseClassProperty(prop));
  12278. return true;
  12279. }
  12280. this.resetPreviousNodeTrailingComments(key);
  12281. return false;
  12282. }
  12283. parseClassMember(classBody, member, state) {
  12284. const isStatic = this.isContextual(100);
  12285. if (isStatic) {
  12286. if (this.parseClassMemberFromModifier(classBody, member)) {
  12287. return;
  12288. }
  12289. if (this.eat(5)) {
  12290. this.parseClassStaticBlock(classBody, member);
  12291. return;
  12292. }
  12293. }
  12294. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  12295. }
  12296. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  12297. const publicMethod = member;
  12298. const privateMethod = member;
  12299. const publicProp = member;
  12300. const privateProp = member;
  12301. const method = publicMethod;
  12302. const publicMember = publicMethod;
  12303. member.static = isStatic;
  12304. this.parsePropertyNamePrefixOperator(member);
  12305. if (this.eat(51)) {
  12306. method.kind = "method";
  12307. const isPrivateName = this.match(130);
  12308. this.parseClassElementName(method);
  12309. if (isPrivateName) {
  12310. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  12311. return;
  12312. }
  12313. if (this.isNonstaticConstructor(publicMethod)) {
  12314. this.raise(ErrorMessages.ConstructorIsGenerator, {
  12315. node: publicMethod.key
  12316. });
  12317. }
  12318. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  12319. return;
  12320. }
  12321. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  12322. const isPrivate = this.match(130);
  12323. const key = this.parseClassElementName(member);
  12324. const maybeQuestionTokenStartLoc = this.state.startLoc;
  12325. this.parsePostMemberNameModifiers(publicMember);
  12326. if (this.isClassMethod()) {
  12327. method.kind = "method";
  12328. if (isPrivate) {
  12329. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  12330. return;
  12331. }
  12332. const isConstructor = this.isNonstaticConstructor(publicMethod);
  12333. let allowsDirectSuper = false;
  12334. if (isConstructor) {
  12335. publicMethod.kind = "constructor";
  12336. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  12337. this.raise(ErrorMessages.DuplicateConstructor, {
  12338. node: key
  12339. });
  12340. }
  12341. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  12342. this.raise(ErrorMessages.OverrideOnConstructor, {
  12343. node: key
  12344. });
  12345. }
  12346. state.hadConstructor = true;
  12347. allowsDirectSuper = state.hadSuperClass;
  12348. }
  12349. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  12350. } else if (this.isClassProperty()) {
  12351. if (isPrivate) {
  12352. this.pushClassPrivateProperty(classBody, privateProp);
  12353. } else {
  12354. this.pushClassProperty(classBody, publicProp);
  12355. }
  12356. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  12357. this.resetPreviousNodeTrailingComments(key);
  12358. const isGenerator = this.eat(51);
  12359. if (publicMember.optional) {
  12360. this.unexpected(maybeQuestionTokenStartLoc);
  12361. }
  12362. method.kind = "method";
  12363. const isPrivate = this.match(130);
  12364. this.parseClassElementName(method);
  12365. this.parsePostMemberNameModifiers(publicMember);
  12366. if (isPrivate) {
  12367. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  12368. } else {
  12369. if (this.isNonstaticConstructor(publicMethod)) {
  12370. this.raise(ErrorMessages.ConstructorIsAsync, {
  12371. node: publicMethod.key
  12372. });
  12373. }
  12374. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  12375. }
  12376. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(51) && this.isLineTerminator())) {
  12377. this.resetPreviousNodeTrailingComments(key);
  12378. method.kind = key.name;
  12379. const isPrivate = this.match(130);
  12380. this.parseClassElementName(publicMethod);
  12381. if (isPrivate) {
  12382. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  12383. } else {
  12384. if (this.isNonstaticConstructor(publicMethod)) {
  12385. this.raise(ErrorMessages.ConstructorIsAccessor, {
  12386. node: publicMethod.key
  12387. });
  12388. }
  12389. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  12390. }
  12391. this.checkGetterSetterParams(publicMethod);
  12392. } else if (this.isLineTerminator()) {
  12393. if (isPrivate) {
  12394. this.pushClassPrivateProperty(classBody, privateProp);
  12395. } else {
  12396. this.pushClassProperty(classBody, publicProp);
  12397. }
  12398. } else {
  12399. this.unexpected();
  12400. }
  12401. }
  12402. parseClassElementName(member) {
  12403. const {
  12404. type,
  12405. value
  12406. } = this.state;
  12407. if ((type === 124 || type === 125) && member.static && value === "prototype") {
  12408. this.raise(ErrorMessages.StaticPrototype, {
  12409. at: this.state.startLoc
  12410. });
  12411. }
  12412. if (type === 130) {
  12413. if (value === "constructor") {
  12414. this.raise(ErrorMessages.ConstructorClassPrivateField, {
  12415. at: this.state.startLoc
  12416. });
  12417. }
  12418. const key = this.parsePrivateName();
  12419. member.key = key;
  12420. return key;
  12421. }
  12422. return this.parsePropertyName(member);
  12423. }
  12424. parseClassStaticBlock(classBody, member) {
  12425. var _member$decorators;
  12426. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  12427. const oldLabels = this.state.labels;
  12428. this.state.labels = [];
  12429. this.prodParam.enter(PARAM);
  12430. const body = member.body = [];
  12431. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  12432. this.prodParam.exit();
  12433. this.scope.exit();
  12434. this.state.labels = oldLabels;
  12435. classBody.body.push(this.finishNode(member, "StaticBlock"));
  12436. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  12437. this.raise(ErrorMessages.DecoratorStaticBlock, {
  12438. node: member
  12439. });
  12440. }
  12441. }
  12442. pushClassProperty(classBody, prop) {
  12443. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  12444. this.raise(ErrorMessages.ConstructorClassField, {
  12445. node: prop.key
  12446. });
  12447. }
  12448. classBody.body.push(this.parseClassProperty(prop));
  12449. }
  12450. pushClassPrivateProperty(classBody, prop) {
  12451. const node = this.parseClassPrivateProperty(prop);
  12452. classBody.body.push(node);
  12453. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  12454. }
  12455. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  12456. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  12457. }
  12458. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  12459. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  12460. classBody.body.push(node);
  12461. 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;
  12462. this.declareClassPrivateMethodInScope(node, kind);
  12463. }
  12464. declareClassPrivateMethodInScope(node, kind) {
  12465. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  12466. }
  12467. parsePostMemberNameModifiers(methodOrProp) {}
  12468. parseClassPrivateProperty(node) {
  12469. this.parseInitializer(node);
  12470. this.semicolon();
  12471. return this.finishNode(node, "ClassPrivateProperty");
  12472. }
  12473. parseClassProperty(node) {
  12474. this.parseInitializer(node);
  12475. this.semicolon();
  12476. return this.finishNode(node, "ClassProperty");
  12477. }
  12478. parseInitializer(node) {
  12479. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  12480. this.expressionScope.enter(newExpressionScope());
  12481. this.prodParam.enter(PARAM);
  12482. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  12483. this.expressionScope.exit();
  12484. this.prodParam.exit();
  12485. this.scope.exit();
  12486. }
  12487. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  12488. if (tokenIsIdentifier(this.state.type)) {
  12489. node.id = this.parseIdentifier();
  12490. if (isStatement) {
  12491. this.checkLVal(node.id, "class name", bindingType);
  12492. }
  12493. } else {
  12494. if (optionalId || !isStatement) {
  12495. node.id = null;
  12496. } else {
  12497. throw this.raise(ErrorMessages.MissingClassName, {
  12498. at: this.state.startLoc
  12499. });
  12500. }
  12501. }
  12502. }
  12503. parseClassSuper(node) {
  12504. node.superClass = this.eat(77) ? this.parseExprSubscripts() : null;
  12505. }
  12506. parseExport(node) {
  12507. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  12508. const parseAfterDefault = !hasDefault || this.eat(12);
  12509. const hasStar = parseAfterDefault && this.eatExportStar(node);
  12510. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  12511. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  12512. const isFromRequired = hasDefault || hasStar;
  12513. if (hasStar && !hasNamespace) {
  12514. if (hasDefault) this.unexpected();
  12515. this.parseExportFrom(node, true);
  12516. return this.finishNode(node, "ExportAllDeclaration");
  12517. }
  12518. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  12519. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  12520. throw this.unexpected(null, 5);
  12521. }
  12522. let hasDeclaration;
  12523. if (isFromRequired || hasSpecifiers) {
  12524. hasDeclaration = false;
  12525. this.parseExportFrom(node, isFromRequired);
  12526. } else {
  12527. hasDeclaration = this.maybeParseExportDeclaration(node);
  12528. }
  12529. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  12530. this.checkExport(node, true, false, !!node.source);
  12531. return this.finishNode(node, "ExportNamedDeclaration");
  12532. }
  12533. if (this.eat(61)) {
  12534. node.declaration = this.parseExportDefaultExpression();
  12535. this.checkExport(node, true, true);
  12536. return this.finishNode(node, "ExportDefaultDeclaration");
  12537. }
  12538. throw this.unexpected(null, 5);
  12539. }
  12540. eatExportStar(node) {
  12541. return this.eat(51);
  12542. }
  12543. maybeParseExportDefaultSpecifier(node) {
  12544. if (this.isExportDefaultSpecifier()) {
  12545. this.expectPlugin("exportDefaultFrom");
  12546. const specifier = this.startNode();
  12547. specifier.exported = this.parseIdentifier(true);
  12548. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  12549. return true;
  12550. }
  12551. return false;
  12552. }
  12553. maybeParseExportNamespaceSpecifier(node) {
  12554. if (this.isContextual(89)) {
  12555. if (!node.specifiers) node.specifiers = [];
  12556. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  12557. this.next();
  12558. specifier.exported = this.parseModuleExportName();
  12559. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  12560. return true;
  12561. }
  12562. return false;
  12563. }
  12564. maybeParseExportNamedSpecifiers(node) {
  12565. if (this.match(5)) {
  12566. if (!node.specifiers) node.specifiers = [];
  12567. const isTypeExport = node.exportKind === "type";
  12568. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  12569. node.source = null;
  12570. node.declaration = null;
  12571. if (this.hasPlugin("importAssertions")) {
  12572. node.assertions = [];
  12573. }
  12574. return true;
  12575. }
  12576. return false;
  12577. }
  12578. maybeParseExportDeclaration(node) {
  12579. if (this.shouldParseExportDeclaration()) {
  12580. node.specifiers = [];
  12581. node.source = null;
  12582. if (this.hasPlugin("importAssertions")) {
  12583. node.assertions = [];
  12584. }
  12585. node.declaration = this.parseExportDeclaration(node);
  12586. return true;
  12587. }
  12588. return false;
  12589. }
  12590. isAsyncFunction() {
  12591. if (!this.isContextual(91)) return false;
  12592. const next = this.nextTokenStart();
  12593. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  12594. }
  12595. parseExportDefaultExpression() {
  12596. const expr = this.startNode();
  12597. const isAsync = this.isAsyncFunction();
  12598. if (this.match(64) || isAsync) {
  12599. this.next();
  12600. if (isAsync) {
  12601. this.next();
  12602. }
  12603. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  12604. }
  12605. if (this.match(76)) {
  12606. return this.parseClass(expr, true, true);
  12607. }
  12608. if (this.match(26)) {
  12609. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12610. this.raise(ErrorMessages.DecoratorBeforeExport, {
  12611. at: this.state.startLoc
  12612. });
  12613. }
  12614. this.parseDecorators(false);
  12615. return this.parseClass(expr, true, true);
  12616. }
  12617. if (this.match(71) || this.match(70) || this.isLet()) {
  12618. throw this.raise(ErrorMessages.UnsupportedDefaultExport, {
  12619. at: this.state.startLoc
  12620. });
  12621. }
  12622. const res = this.parseMaybeAssignAllowIn();
  12623. this.semicolon();
  12624. return res;
  12625. }
  12626. parseExportDeclaration(node) {
  12627. return this.parseStatement(null);
  12628. }
  12629. isExportDefaultSpecifier() {
  12630. const {
  12631. type
  12632. } = this.state;
  12633. if (tokenIsIdentifier(type)) {
  12634. if (type === 91 && !this.state.containsEsc || type === 95) {
  12635. return false;
  12636. }
  12637. if ((type === 122 || type === 121) && !this.state.containsEsc) {
  12638. const {
  12639. type: nextType
  12640. } = this.lookahead();
  12641. if (tokenIsIdentifier(nextType) && nextType !== 93 || nextType === 5) {
  12642. this.expectOnePlugin(["flow", "typescript"]);
  12643. return false;
  12644. }
  12645. }
  12646. } else if (!this.match(61)) {
  12647. return false;
  12648. }
  12649. const next = this.nextTokenStart();
  12650. const hasFrom = this.isUnparsedContextual(next, "from");
  12651. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  12652. return true;
  12653. }
  12654. if (this.match(61) && hasFrom) {
  12655. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  12656. return nextAfterFrom === 34 || nextAfterFrom === 39;
  12657. }
  12658. return false;
  12659. }
  12660. parseExportFrom(node, expect) {
  12661. if (this.eatContextual(93)) {
  12662. node.source = this.parseImportSource();
  12663. this.checkExport(node);
  12664. const assertions = this.maybeParseImportAssertions();
  12665. if (assertions) {
  12666. node.assertions = assertions;
  12667. }
  12668. } else if (expect) {
  12669. this.unexpected();
  12670. }
  12671. this.semicolon();
  12672. }
  12673. shouldParseExportDeclaration() {
  12674. const {
  12675. type
  12676. } = this.state;
  12677. if (type === 26) {
  12678. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12679. if (this.hasPlugin("decorators")) {
  12680. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12681. throw this.raise(ErrorMessages.DecoratorBeforeExport, {
  12682. at: this.state.startLoc
  12683. });
  12684. }
  12685. return true;
  12686. }
  12687. }
  12688. return type === 70 || type === 71 || type === 64 || type === 76 || this.isLet() || this.isAsyncFunction();
  12689. }
  12690. checkExport(node, checkNames, isDefault, isFrom) {
  12691. if (checkNames) {
  12692. if (isDefault) {
  12693. this.checkDuplicateExports(node, "default");
  12694. if (this.hasPlugin("exportDefaultFrom")) {
  12695. var _declaration$extra;
  12696. const declaration = node.declaration;
  12697. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  12698. this.raise(ErrorMessages.ExportDefaultFromAsIdentifier, {
  12699. node: declaration
  12700. });
  12701. }
  12702. }
  12703. } else if (node.specifiers && node.specifiers.length) {
  12704. for (const specifier of node.specifiers) {
  12705. const {
  12706. exported
  12707. } = specifier;
  12708. const exportedName = exported.type === "Identifier" ? exported.name : exported.value;
  12709. this.checkDuplicateExports(specifier, exportedName);
  12710. if (!isFrom && specifier.local) {
  12711. const {
  12712. local
  12713. } = specifier;
  12714. if (local.type !== "Identifier") {
  12715. this.raise(ErrorMessages.ExportBindingIsString, {
  12716. node: specifier
  12717. }, local.value, exportedName);
  12718. } else {
  12719. this.checkReservedWord(local.name, local.loc.start, true, false);
  12720. this.scope.checkLocalExport(local);
  12721. }
  12722. }
  12723. }
  12724. } else if (node.declaration) {
  12725. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  12726. const id = node.declaration.id;
  12727. if (!id) throw new Error("Assertion failure");
  12728. this.checkDuplicateExports(node, id.name);
  12729. } else if (node.declaration.type === "VariableDeclaration") {
  12730. for (const declaration of node.declaration.declarations) {
  12731. this.checkDeclaration(declaration.id);
  12732. }
  12733. }
  12734. }
  12735. }
  12736. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12737. if (currentContextDecorators.length) {
  12738. throw this.raise(ErrorMessages.UnsupportedDecoratorExport, {
  12739. node
  12740. });
  12741. }
  12742. }
  12743. checkDeclaration(node) {
  12744. if (node.type === "Identifier") {
  12745. this.checkDuplicateExports(node, node.name);
  12746. } else if (node.type === "ObjectPattern") {
  12747. for (const prop of node.properties) {
  12748. this.checkDeclaration(prop);
  12749. }
  12750. } else if (node.type === "ArrayPattern") {
  12751. for (const elem of node.elements) {
  12752. if (elem) {
  12753. this.checkDeclaration(elem);
  12754. }
  12755. }
  12756. } else if (node.type === "ObjectProperty") {
  12757. this.checkDeclaration(node.value);
  12758. } else if (node.type === "RestElement") {
  12759. this.checkDeclaration(node.argument);
  12760. } else if (node.type === "AssignmentPattern") {
  12761. this.checkDeclaration(node.left);
  12762. }
  12763. }
  12764. checkDuplicateExports(node, name) {
  12765. if (this.exportedIdentifiers.has(name)) {
  12766. this.raise(name === "default" ? ErrorMessages.DuplicateDefaultExport : ErrorMessages.DuplicateExport, {
  12767. node
  12768. }, name);
  12769. }
  12770. this.exportedIdentifiers.add(name);
  12771. }
  12772. parseExportSpecifiers(isInTypeExport) {
  12773. const nodes = [];
  12774. let first = true;
  12775. this.expect(5);
  12776. while (!this.eat(8)) {
  12777. if (first) {
  12778. first = false;
  12779. } else {
  12780. this.expect(12);
  12781. if (this.eat(8)) break;
  12782. }
  12783. const isMaybeTypeOnly = this.isContextual(122);
  12784. const isString = this.match(125);
  12785. const node = this.startNode();
  12786. node.local = this.parseModuleExportName();
  12787. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  12788. }
  12789. return nodes;
  12790. }
  12791. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  12792. if (this.eatContextual(89)) {
  12793. node.exported = this.parseModuleExportName();
  12794. } else if (isString) {
  12795. node.exported = cloneStringLiteral(node.local);
  12796. } else if (!node.exported) {
  12797. node.exported = cloneIdentifier(node.local);
  12798. }
  12799. return this.finishNode(node, "ExportSpecifier");
  12800. }
  12801. parseModuleExportName() {
  12802. if (this.match(125)) {
  12803. const result = this.parseStringLiteral(this.state.value);
  12804. const surrogate = result.value.match(loneSurrogate);
  12805. if (surrogate) {
  12806. this.raise(ErrorMessages.ModuleExportNameHasLoneSurrogate, {
  12807. node: result
  12808. }, surrogate[0].charCodeAt(0).toString(16));
  12809. }
  12810. return result;
  12811. }
  12812. return this.parseIdentifier(true);
  12813. }
  12814. parseImport(node) {
  12815. node.specifiers = [];
  12816. if (!this.match(125)) {
  12817. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  12818. const parseNext = !hasDefault || this.eat(12);
  12819. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  12820. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  12821. this.expectContextual(93);
  12822. }
  12823. node.source = this.parseImportSource();
  12824. const assertions = this.maybeParseImportAssertions();
  12825. if (assertions) {
  12826. node.assertions = assertions;
  12827. } else {
  12828. const attributes = this.maybeParseModuleAttributes();
  12829. if (attributes) {
  12830. node.attributes = attributes;
  12831. }
  12832. }
  12833. this.semicolon();
  12834. return this.finishNode(node, "ImportDeclaration");
  12835. }
  12836. parseImportSource() {
  12837. if (!this.match(125)) this.unexpected();
  12838. return this.parseExprAtom();
  12839. }
  12840. shouldParseDefaultImport(node) {
  12841. return tokenIsIdentifier(this.state.type);
  12842. }
  12843. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  12844. specifier.local = this.parseIdentifier();
  12845. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  12846. node.specifiers.push(this.finishNode(specifier, type));
  12847. }
  12848. parseAssertEntries() {
  12849. const attrs = [];
  12850. const attrNames = new Set();
  12851. do {
  12852. if (this.match(8)) {
  12853. break;
  12854. }
  12855. const node = this.startNode();
  12856. const keyName = this.state.value;
  12857. if (attrNames.has(keyName)) {
  12858. this.raise(ErrorMessages.ModuleAttributesWithDuplicateKeys, {
  12859. at: this.state.startLoc
  12860. }, keyName);
  12861. }
  12862. attrNames.add(keyName);
  12863. if (this.match(125)) {
  12864. node.key = this.parseStringLiteral(keyName);
  12865. } else {
  12866. node.key = this.parseIdentifier(true);
  12867. }
  12868. this.expect(14);
  12869. if (!this.match(125)) {
  12870. throw this.raise(ErrorMessages.ModuleAttributeInvalidValue, {
  12871. at: this.state.startLoc
  12872. });
  12873. }
  12874. node.value = this.parseStringLiteral(this.state.value);
  12875. this.finishNode(node, "ImportAttribute");
  12876. attrs.push(node);
  12877. } while (this.eat(12));
  12878. return attrs;
  12879. }
  12880. maybeParseModuleAttributes() {
  12881. if (this.match(72) && !this.hasPrecedingLineBreak()) {
  12882. this.expectPlugin("moduleAttributes");
  12883. this.next();
  12884. } else {
  12885. if (this.hasPlugin("moduleAttributes")) return [];
  12886. return null;
  12887. }
  12888. const attrs = [];
  12889. const attributes = new Set();
  12890. do {
  12891. const node = this.startNode();
  12892. node.key = this.parseIdentifier(true);
  12893. if (node.key.name !== "type") {
  12894. this.raise(ErrorMessages.ModuleAttributeDifferentFromType, {
  12895. node: node.key
  12896. }, node.key.name);
  12897. }
  12898. if (attributes.has(node.key.name)) {
  12899. this.raise(ErrorMessages.ModuleAttributesWithDuplicateKeys, {
  12900. node: node.key
  12901. }, node.key.name);
  12902. }
  12903. attributes.add(node.key.name);
  12904. this.expect(14);
  12905. if (!this.match(125)) {
  12906. throw this.raise(ErrorMessages.ModuleAttributeInvalidValue, {
  12907. at: this.state.startLoc
  12908. });
  12909. }
  12910. node.value = this.parseStringLiteral(this.state.value);
  12911. this.finishNode(node, "ImportAttribute");
  12912. attrs.push(node);
  12913. } while (this.eat(12));
  12914. return attrs;
  12915. }
  12916. maybeParseImportAssertions() {
  12917. if (this.isContextual(90) && !this.hasPrecedingLineBreak()) {
  12918. this.expectPlugin("importAssertions");
  12919. this.next();
  12920. } else {
  12921. if (this.hasPlugin("importAssertions")) return [];
  12922. return null;
  12923. }
  12924. this.eat(5);
  12925. const attrs = this.parseAssertEntries();
  12926. this.eat(8);
  12927. return attrs;
  12928. }
  12929. maybeParseDefaultImportSpecifier(node) {
  12930. if (this.shouldParseDefaultImport(node)) {
  12931. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  12932. return true;
  12933. }
  12934. return false;
  12935. }
  12936. maybeParseStarImportSpecifier(node) {
  12937. if (this.match(51)) {
  12938. const specifier = this.startNode();
  12939. this.next();
  12940. this.expectContextual(89);
  12941. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  12942. return true;
  12943. }
  12944. return false;
  12945. }
  12946. parseNamedImportSpecifiers(node) {
  12947. let first = true;
  12948. this.expect(5);
  12949. while (!this.eat(8)) {
  12950. if (first) {
  12951. first = false;
  12952. } else {
  12953. if (this.eat(14)) {
  12954. throw this.raise(ErrorMessages.DestructureNamedImport, {
  12955. at: this.state.startLoc
  12956. });
  12957. }
  12958. this.expect(12);
  12959. if (this.eat(8)) break;
  12960. }
  12961. const specifier = this.startNode();
  12962. const importedIsString = this.match(125);
  12963. const isMaybeTypeOnly = this.isContextual(122);
  12964. specifier.imported = this.parseModuleExportName();
  12965. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly);
  12966. node.specifiers.push(importSpecifier);
  12967. }
  12968. }
  12969. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly) {
  12970. if (this.eatContextual(89)) {
  12971. specifier.local = this.parseIdentifier();
  12972. } else {
  12973. const {
  12974. imported
  12975. } = specifier;
  12976. if (importedIsString) {
  12977. throw this.raise(ErrorMessages.ImportBindingIsString, {
  12978. node: specifier
  12979. }, imported.value);
  12980. }
  12981. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  12982. if (!specifier.local) {
  12983. specifier.local = cloneIdentifier(imported);
  12984. }
  12985. }
  12986. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  12987. return this.finishNode(specifier, "ImportSpecifier");
  12988. }
  12989. isThisParam(param) {
  12990. return param.type === "Identifier" && param.name === "this";
  12991. }
  12992. }
  12993. class Parser extends StatementParser {
  12994. constructor(options, input) {
  12995. options = getOptions(options);
  12996. super(options, input);
  12997. this.options = options;
  12998. this.initializeScopes();
  12999. this.plugins = pluginsMap(this.options.plugins);
  13000. this.filename = options.sourceFilename;
  13001. }
  13002. getScopeHandler() {
  13003. return ScopeHandler;
  13004. }
  13005. parse() {
  13006. this.enterInitialScopes();
  13007. const file = this.startNode();
  13008. const program = this.startNode();
  13009. this.nextToken();
  13010. file.errors = null;
  13011. this.parseTopLevel(file, program);
  13012. file.errors = this.state.errors;
  13013. return file;
  13014. }
  13015. }
  13016. function pluginsMap(plugins) {
  13017. const pluginMap = new Map();
  13018. for (const plugin of plugins) {
  13019. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  13020. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  13021. }
  13022. return pluginMap;
  13023. }
  13024. function parse(input, options) {
  13025. var _options;
  13026. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  13027. options = Object.assign({}, options);
  13028. try {
  13029. options.sourceType = "module";
  13030. const parser = getParser(options, input);
  13031. const ast = parser.parse();
  13032. if (parser.sawUnambiguousESM) {
  13033. return ast;
  13034. }
  13035. if (parser.ambiguousScriptDifferentAst) {
  13036. try {
  13037. options.sourceType = "script";
  13038. return getParser(options, input).parse();
  13039. } catch (_unused) {}
  13040. } else {
  13041. ast.program.sourceType = "script";
  13042. }
  13043. return ast;
  13044. } catch (moduleError) {
  13045. try {
  13046. options.sourceType = "script";
  13047. return getParser(options, input).parse();
  13048. } catch (_unused2) {}
  13049. throw moduleError;
  13050. }
  13051. } else {
  13052. return getParser(options, input).parse();
  13053. }
  13054. }
  13055. function parseExpression(input, options) {
  13056. const parser = getParser(options, input);
  13057. if (parser.options.strictMode) {
  13058. parser.state.strict = true;
  13059. }
  13060. return parser.getExpression();
  13061. }
  13062. function generateExportedTokenTypes(internalTokenTypes) {
  13063. const tokenTypes = {};
  13064. for (const typeName of Object.keys(internalTokenTypes)) {
  13065. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  13066. }
  13067. return tokenTypes;
  13068. }
  13069. const tokTypes = generateExportedTokenTypes(tt);
  13070. function getParser(options, input) {
  13071. let cls = Parser;
  13072. if (options != null && options.plugins) {
  13073. validatePlugins(options.plugins);
  13074. cls = getParserClass(options.plugins);
  13075. }
  13076. return new cls(options, input);
  13077. }
  13078. const parserClassCache = {};
  13079. function getParserClass(pluginsFromOptions) {
  13080. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  13081. const key = pluginList.join("/");
  13082. let cls = parserClassCache[key];
  13083. if (!cls) {
  13084. cls = Parser;
  13085. for (const plugin of pluginList) {
  13086. cls = mixinPlugins[plugin](cls);
  13087. }
  13088. parserClassCache[key] = cls;
  13089. }
  13090. return cls;
  13091. }
  13092. exports.parse = parse;
  13093. exports.parseExpression = parseExpression;
  13094. exports.tokTypes = tokTypes;
  13095. //# sourceMappingURL=index.js.map