index.js 470 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _objectWithoutPropertiesLoose(source, excluded) {
  4. if (source == null) return {};
  5. var target = {};
  6. var sourceKeys = Object.keys(source);
  7. var key, i;
  8. for (i = 0; i < sourceKeys.length; i++) {
  9. key = sourceKeys[i];
  10. if (excluded.indexOf(key) >= 0) continue;
  11. target[key] = source[key];
  12. }
  13. return target;
  14. }
  15. class Position {
  16. constructor(line, col, index) {
  17. this.line = void 0;
  18. this.column = void 0;
  19. this.index = void 0;
  20. this.line = line;
  21. this.column = col;
  22. this.index = index;
  23. }
  24. }
  25. class SourceLocation {
  26. constructor(start, end) {
  27. this.start = void 0;
  28. this.end = void 0;
  29. this.filename = void 0;
  30. this.identifierName = void 0;
  31. this.start = start;
  32. this.end = end;
  33. }
  34. }
  35. function createPositionWithColumnOffset(position, columnOffset) {
  36. const {
  37. line,
  38. column,
  39. index
  40. } = position;
  41. return new Position(line, column + columnOffset, index + columnOffset);
  42. }
  43. var ParseErrorCode = {
  44. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  45. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  46. };
  47. const reflect = (keys, last = keys.length - 1) => ({
  48. get() {
  49. return keys.reduce((object, key) => object[key], this);
  50. },
  51. set(value) {
  52. keys.reduce((item, key, i) => i === last ? item[key] = value : item[key], this);
  53. }
  54. });
  55. const instantiate = (constructor, properties, descriptors) => Object.keys(descriptors).map(key => [key, descriptors[key]]).filter(([, descriptor]) => !!descriptor).map(([key, descriptor]) => [key, typeof descriptor === "function" ? {
  56. value: descriptor,
  57. enumerable: false
  58. } : typeof descriptor.reflect === "string" ? Object.assign({}, descriptor, reflect(descriptor.reflect.split("."))) : descriptor]).reduce((instance, [key, descriptor]) => Object.defineProperty(instance, key, Object.assign({
  59. configurable: true
  60. }, descriptor)), Object.assign(new constructor(), properties));
  61. var ModuleErrors = {
  62. ImportMetaOutsideModule: {
  63. message: `import.meta may appear only with 'sourceType: "module"'`,
  64. code: ParseErrorCode.SourceTypeModuleError
  65. },
  66. ImportOutsideModule: {
  67. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  68. code: ParseErrorCode.SourceTypeModuleError
  69. }
  70. };
  71. const NodeDescriptions = {
  72. ArrayPattern: "array destructuring pattern",
  73. AssignmentExpression: "assignment expression",
  74. AssignmentPattern: "assignment expression",
  75. ArrowFunctionExpression: "arrow function expression",
  76. ConditionalExpression: "conditional expression",
  77. CatchClause: "catch clause",
  78. ForOfStatement: "for-of statement",
  79. ForInStatement: "for-in statement",
  80. ForStatement: "for-loop",
  81. FormalParameters: "function parameter list",
  82. Identifier: "identifier",
  83. ImportSpecifier: "import specifier",
  84. ImportDefaultSpecifier: "import default specifier",
  85. ImportNamespaceSpecifier: "import namespace specifier",
  86. ObjectPattern: "object destructuring pattern",
  87. ParenthesizedExpression: "parenthesized expression",
  88. RestElement: "rest element",
  89. UpdateExpression: {
  90. true: "prefix operation",
  91. false: "postfix operation"
  92. },
  93. VariableDeclarator: "variable declaration",
  94. YieldExpression: "yield expression"
  95. };
  96. const toNodeDescription = ({
  97. type,
  98. prefix
  99. }) => type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[String(prefix)] : NodeDescriptions[type];
  100. var StandardErrors = {
  101. AccessorIsGenerator: ({
  102. kind
  103. }) => `A ${kind}ter cannot be a generator.`,
  104. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  105. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  106. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  107. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  108. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  109. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  110. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  111. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  112. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  113. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  114. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  115. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  116. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  117. ConstructorIsAsync: "Constructor can't be an async function.",
  118. ConstructorIsGenerator: "Constructor can't be a generator.",
  119. DeclarationMissingInitializer: ({
  120. kind
  121. }) => `Missing initializer in ${kind} declaration.`,
  122. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  123. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  124. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  125. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  126. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  127. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  128. DeletePrivateField: "Deleting a private field is not allowed.",
  129. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  130. DuplicateConstructor: "Duplicate constructor in the same class.",
  131. DuplicateDefaultExport: "Only one default export allowed per module.",
  132. DuplicateExport: ({
  133. exportName
  134. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  135. DuplicateProto: "Redefinition of __proto__ property.",
  136. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  137. ElementAfterRest: "Rest element must be last element.",
  138. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  139. ExportBindingIsString: ({
  140. localName,
  141. exportName
  142. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  143. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  144. ForInOfLoopInitializer: ({
  145. type
  146. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  147. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  148. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  149. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  150. IllegalBreakContinue: ({
  151. type
  152. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  153. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  154. IllegalReturn: "'return' outside of function.",
  155. ImportBindingIsString: ({
  156. importName
  157. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  158. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  159. ImportCallArity: ({
  160. maxArgumentCount
  161. }) => `\`import()\` requires exactly ${maxArgumentCount === 1 ? "one argument" : "one or two arguments"}.`,
  162. ImportCallNotNewExpression: "Cannot use new with import(...).",
  163. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  164. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  165. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  166. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  167. InvalidCodePoint: "Code point out of bounds.",
  168. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  169. InvalidDecimal: "Invalid decimal.",
  170. InvalidDigit: ({
  171. radix
  172. }) => `Expected number in radix ${radix}.`,
  173. InvalidEscapeSequence: "Bad character escape sequence.",
  174. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  175. InvalidEscapedReservedWord: ({
  176. reservedWord
  177. }) => `Escape sequence in keyword ${reservedWord}.`,
  178. InvalidIdentifier: ({
  179. identifierName
  180. }) => `Invalid identifier ${identifierName}.`,
  181. InvalidLhs: ({
  182. ancestor
  183. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  184. InvalidLhsBinding: ({
  185. ancestor
  186. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  187. InvalidNumber: "Invalid number.",
  188. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  189. InvalidOrUnexpectedToken: ({
  190. unexpected
  191. }) => `Unexpected character '${unexpected}'.`,
  192. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  193. InvalidPrivateFieldResolution: ({
  194. identifierName
  195. }) => `Private name #${identifierName} is not defined.`,
  196. InvalidPropertyBindingPattern: "Binding member expression.",
  197. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  198. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  199. LabelRedeclaration: ({
  200. labelName
  201. }) => `Label '${labelName}' is already declared.`,
  202. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  203. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  204. MalformedRegExpFlags: "Invalid regular expression flag.",
  205. MissingClassName: "A class name is required.",
  206. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  207. MissingSemicolon: "Missing semicolon.",
  208. MissingPlugin: ({
  209. missingPlugin
  210. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  211. MissingOneOfPlugins: ({
  212. missingPlugin
  213. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  214. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  215. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  216. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  217. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  218. ModuleAttributesWithDuplicateKeys: ({
  219. key
  220. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  221. ModuleExportNameHasLoneSurrogate: ({
  222. surrogateCharCode
  223. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  224. ModuleExportUndefined: ({
  225. localName
  226. }) => `Export '${localName}' is not defined.`,
  227. MultipleDefaultsInSwitch: "Multiple default clauses.",
  228. NewlineAfterThrow: "Illegal newline after throw.",
  229. NoCatchOrFinally: "Missing catch or finally clause.",
  230. NumberIdentifier: "Identifier directly after number.",
  231. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  232. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  233. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  234. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  235. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  236. ParamDupe: "Argument name clash.",
  237. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  238. PatternHasMethod: "Object pattern can't contain methods.",
  239. PrivateInExpectedIn: ({
  240. identifierName
  241. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  242. PrivateNameRedeclaration: ({
  243. identifierName
  244. }) => `Duplicate private name #${identifierName}.`,
  245. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  246. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  247. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  248. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  249. RestTrailingComma: "Unexpected trailing comma after rest element.",
  250. 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.",
  251. StaticPrototype: "Classes may not have static property named prototype.",
  252. 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?",
  253. SuperPrivateField: "Private fields can't be accessed on super.",
  254. TrailingDecorator: "Decorators must be attached to a class element.",
  255. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  256. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  257. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  258. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  259. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  260. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  261. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  262. UnexpectedKeyword: ({
  263. keyword
  264. }) => `Unexpected keyword '${keyword}'.`,
  265. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  266. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  267. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  268. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  269. UnexpectedPrivateField: "Unexpected private name.",
  270. UnexpectedReservedWord: ({
  271. reservedWord
  272. }) => `Unexpected reserved word '${reservedWord}'.`,
  273. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  274. UnexpectedToken: ({
  275. expected,
  276. unexpected
  277. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  278. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  279. UnsupportedBind: "Binding should be performed on object property.",
  280. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  281. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  282. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  283. UnsupportedMetaProperty: ({
  284. target,
  285. onlyValidPropertyName
  286. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  287. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  288. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  289. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  290. UnterminatedComment: "Unterminated comment.",
  291. UnterminatedRegExp: "Unterminated regular expression.",
  292. UnterminatedString: "Unterminated string constant.",
  293. UnterminatedTemplate: "Unterminated template.",
  294. VarRedeclaration: ({
  295. identifierName
  296. }) => `Identifier '${identifierName}' has already been declared.`,
  297. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  298. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  299. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  300. };
  301. var StrictModeErrors = {
  302. StrictDelete: "Deleting local variable in strict mode.",
  303. StrictEvalArguments: ({
  304. referenceName
  305. }) => `Assigning to '${referenceName}' in strict mode.`,
  306. StrictEvalArgumentsBinding: ({
  307. bindingName
  308. }) => `Binding '${bindingName}' in strict mode.`,
  309. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  310. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  311. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  312. StrictWith: "'with' in strict mode."
  313. };
  314. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  315. var PipelineOperatorErrors = {
  316. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  317. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  318. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  319. PipeTopicUnconfiguredToken: ({
  320. token
  321. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  322. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  323. PipeUnparenthesizedBody: ({
  324. type
  325. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  326. type
  327. })}; please wrap it in parentheses.`,
  328. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  329. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  330. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  331. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  332. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  333. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  334. };
  335. const _excluded$1 = ["toMessage"],
  336. _excluded2$1 = ["message"];
  337. function toParseErrorConstructor(_ref) {
  338. let {
  339. toMessage
  340. } = _ref,
  341. properties = _objectWithoutPropertiesLoose(_ref, _excluded$1);
  342. return function constructor({
  343. loc,
  344. details
  345. }) {
  346. return instantiate(SyntaxError, Object.assign({}, properties, {
  347. loc
  348. }), {
  349. clone(overrides = {}) {
  350. const loc = overrides.loc || {};
  351. return constructor({
  352. loc: new Position("line" in loc ? loc.line : this.loc.line, "column" in loc ? loc.column : this.loc.column, "index" in loc ? loc.index : this.loc.index),
  353. details: Object.assign({}, this.details, overrides.details)
  354. });
  355. },
  356. details: {
  357. value: details,
  358. enumerable: false
  359. },
  360. message: {
  361. get() {
  362. return `${toMessage(this.details)} (${this.loc.line}:${this.loc.column})`;
  363. },
  364. set(value) {
  365. Object.defineProperty(this, "message", {
  366. value
  367. });
  368. }
  369. },
  370. pos: {
  371. reflect: "loc.index",
  372. enumerable: true
  373. },
  374. missingPlugin: "missingPlugin" in details && {
  375. reflect: "details.missingPlugin",
  376. enumerable: true
  377. }
  378. });
  379. };
  380. }
  381. function ParseErrorEnum(argument, syntaxPlugin) {
  382. if (Array.isArray(argument)) {
  383. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  384. }
  385. const ParseErrorConstructors = {};
  386. for (const reasonCode of Object.keys(argument)) {
  387. const template = argument[reasonCode];
  388. const _ref2 = typeof template === "string" ? {
  389. message: () => template
  390. } : typeof template === "function" ? {
  391. message: template
  392. } : template,
  393. {
  394. message
  395. } = _ref2,
  396. rest = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
  397. const toMessage = typeof message === "string" ? () => message : message;
  398. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  399. code: ParseErrorCode.SyntaxError,
  400. reasonCode,
  401. toMessage
  402. }, syntaxPlugin ? {
  403. syntaxPlugin
  404. } : {}, rest));
  405. }
  406. return ParseErrorConstructors;
  407. }
  408. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors));
  409. const {
  410. defineProperty
  411. } = Object;
  412. const toUnenumerable = (object, key) => defineProperty(object, key, {
  413. enumerable: false,
  414. value: object[key]
  415. });
  416. function toESTreeLocation(node) {
  417. node.loc.start && toUnenumerable(node.loc.start, "index");
  418. node.loc.end && toUnenumerable(node.loc.end, "index");
  419. return node;
  420. }
  421. var estree = (superClass => class ESTreeParserMixin extends superClass {
  422. parse() {
  423. const file = toESTreeLocation(super.parse());
  424. if (this.options.tokens) {
  425. file.tokens = file.tokens.map(toESTreeLocation);
  426. }
  427. return file;
  428. }
  429. parseRegExpLiteral({
  430. pattern,
  431. flags
  432. }) {
  433. let regex = null;
  434. try {
  435. regex = new RegExp(pattern, flags);
  436. } catch (e) {}
  437. const node = this.estreeParseLiteral(regex);
  438. node.regex = {
  439. pattern,
  440. flags
  441. };
  442. return node;
  443. }
  444. parseBigIntLiteral(value) {
  445. let bigInt;
  446. try {
  447. bigInt = BigInt(value);
  448. } catch (_unused) {
  449. bigInt = null;
  450. }
  451. const node = this.estreeParseLiteral(bigInt);
  452. node.bigint = String(node.value || value);
  453. return node;
  454. }
  455. parseDecimalLiteral(value) {
  456. const decimal = null;
  457. const node = this.estreeParseLiteral(decimal);
  458. node.decimal = String(node.value || value);
  459. return node;
  460. }
  461. estreeParseLiteral(value) {
  462. return this.parseLiteral(value, "Literal");
  463. }
  464. parseStringLiteral(value) {
  465. return this.estreeParseLiteral(value);
  466. }
  467. parseNumericLiteral(value) {
  468. return this.estreeParseLiteral(value);
  469. }
  470. parseNullLiteral() {
  471. return this.estreeParseLiteral(null);
  472. }
  473. parseBooleanLiteral(value) {
  474. return this.estreeParseLiteral(value);
  475. }
  476. directiveToStmt(directive) {
  477. const expression = directive.value;
  478. delete directive.value;
  479. expression.type = "Literal";
  480. expression.raw = expression.extra.raw;
  481. expression.value = expression.extra.expressionValue;
  482. const stmt = directive;
  483. stmt.type = "ExpressionStatement";
  484. stmt.expression = expression;
  485. stmt.directive = expression.extra.rawValue;
  486. delete expression.extra;
  487. return stmt;
  488. }
  489. initFunction(node, isAsync) {
  490. super.initFunction(node, isAsync);
  491. node.expression = false;
  492. }
  493. checkDeclaration(node) {
  494. if (node != null && this.isObjectProperty(node)) {
  495. this.checkDeclaration(node.value);
  496. } else {
  497. super.checkDeclaration(node);
  498. }
  499. }
  500. getObjectOrClassMethodParams(method) {
  501. return method.value.params;
  502. }
  503. isValidDirective(stmt) {
  504. var _stmt$expression$extr;
  505. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  506. }
  507. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  508. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  509. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  510. node.body = directiveStatements.concat(node.body);
  511. delete node.directives;
  512. }
  513. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  514. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  515. if (method.typeParameters) {
  516. method.value.typeParameters = method.typeParameters;
  517. delete method.typeParameters;
  518. }
  519. classBody.body.push(method);
  520. }
  521. parsePrivateName() {
  522. const node = super.parsePrivateName();
  523. {
  524. if (!this.getPluginOption("estree", "classFeatures")) {
  525. return node;
  526. }
  527. }
  528. return this.convertPrivateNameToPrivateIdentifier(node);
  529. }
  530. convertPrivateNameToPrivateIdentifier(node) {
  531. const name = super.getPrivateNameSV(node);
  532. node = node;
  533. delete node.id;
  534. node.name = name;
  535. node.type = "PrivateIdentifier";
  536. return node;
  537. }
  538. isPrivateName(node) {
  539. {
  540. if (!this.getPluginOption("estree", "classFeatures")) {
  541. return super.isPrivateName(node);
  542. }
  543. }
  544. return node.type === "PrivateIdentifier";
  545. }
  546. getPrivateNameSV(node) {
  547. {
  548. if (!this.getPluginOption("estree", "classFeatures")) {
  549. return super.getPrivateNameSV(node);
  550. }
  551. }
  552. return node.name;
  553. }
  554. parseLiteral(value, type) {
  555. const node = super.parseLiteral(value, type);
  556. node.raw = node.extra.raw;
  557. delete node.extra;
  558. return node;
  559. }
  560. parseFunctionBody(node, allowExpression, isMethod = false) {
  561. super.parseFunctionBody(node, allowExpression, isMethod);
  562. node.expression = node.body.type !== "BlockStatement";
  563. }
  564. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  565. let funcNode = this.startNode();
  566. funcNode.kind = node.kind;
  567. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  568. funcNode.type = "FunctionExpression";
  569. delete funcNode.kind;
  570. node.value = funcNode;
  571. if (type === "ClassPrivateMethod") {
  572. node.computed = false;
  573. }
  574. return this.finishNode(node, "MethodDefinition");
  575. }
  576. parseClassProperty(...args) {
  577. const propertyNode = super.parseClassProperty(...args);
  578. {
  579. if (!this.getPluginOption("estree", "classFeatures")) {
  580. return propertyNode;
  581. }
  582. }
  583. propertyNode.type = "PropertyDefinition";
  584. return propertyNode;
  585. }
  586. parseClassPrivateProperty(...args) {
  587. const propertyNode = super.parseClassPrivateProperty(...args);
  588. {
  589. if (!this.getPluginOption("estree", "classFeatures")) {
  590. return propertyNode;
  591. }
  592. }
  593. propertyNode.type = "PropertyDefinition";
  594. propertyNode.computed = false;
  595. return propertyNode;
  596. }
  597. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  598. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  599. if (node) {
  600. node.type = "Property";
  601. if (node.kind === "method") {
  602. node.kind = "init";
  603. }
  604. node.shorthand = false;
  605. }
  606. return node;
  607. }
  608. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  609. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  610. if (node) {
  611. node.kind = "init";
  612. node.type = "Property";
  613. }
  614. return node;
  615. }
  616. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  617. return type === "Property" ? "value" : super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  618. }
  619. isAssignable(node, isBinding) {
  620. if (node != null && this.isObjectProperty(node)) {
  621. return this.isAssignable(node.value, isBinding);
  622. }
  623. return super.isAssignable(node, isBinding);
  624. }
  625. toAssignable(node, isLHS = false) {
  626. if (node != null && this.isObjectProperty(node)) {
  627. const {
  628. key,
  629. value
  630. } = node;
  631. if (this.isPrivateName(key)) {
  632. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  633. }
  634. this.toAssignable(value, isLHS);
  635. } else {
  636. super.toAssignable(node, isLHS);
  637. }
  638. }
  639. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  640. if (prop.kind === "get" || prop.kind === "set") {
  641. this.raise(Errors.PatternHasAccessor, {
  642. at: prop.key
  643. });
  644. } else if (prop.method) {
  645. this.raise(Errors.PatternHasMethod, {
  646. at: prop.key
  647. });
  648. } else {
  649. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  650. }
  651. }
  652. finishCallExpression(unfinished, optional) {
  653. const node = super.finishCallExpression(unfinished, optional);
  654. if (node.callee.type === "Import") {
  655. node.type = "ImportExpression";
  656. node.source = node.arguments[0];
  657. if (this.hasPlugin("importAssertions")) {
  658. var _node$arguments$;
  659. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  660. }
  661. delete node.arguments;
  662. delete node.callee;
  663. }
  664. return node;
  665. }
  666. toReferencedArguments(node) {
  667. if (node.type === "ImportExpression") {
  668. return;
  669. }
  670. super.toReferencedArguments(node);
  671. }
  672. parseExport(unfinished) {
  673. const node = super.parseExport(unfinished);
  674. switch (node.type) {
  675. case "ExportAllDeclaration":
  676. node.exported = null;
  677. break;
  678. case "ExportNamedDeclaration":
  679. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  680. node.type = "ExportAllDeclaration";
  681. node.exported = node.specifiers[0].exported;
  682. delete node.specifiers;
  683. }
  684. break;
  685. }
  686. return node;
  687. }
  688. parseSubscript(base, startPos, startLoc, noCalls, state) {
  689. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  690. if (state.optionalChainMember) {
  691. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  692. node.type = node.type.substring(8);
  693. }
  694. if (state.stop) {
  695. const chain = this.startNodeAtNode(node);
  696. chain.expression = node;
  697. return this.finishNode(chain, "ChainExpression");
  698. }
  699. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  700. node.optional = false;
  701. }
  702. return node;
  703. }
  704. hasPropertyAsPrivateName(node) {
  705. if (node.type === "ChainExpression") {
  706. node = node.expression;
  707. }
  708. return super.hasPropertyAsPrivateName(node);
  709. }
  710. isOptionalChain(node) {
  711. return node.type === "ChainExpression";
  712. }
  713. isObjectProperty(node) {
  714. return node.type === "Property" && node.kind === "init" && !node.method;
  715. }
  716. isObjectMethod(node) {
  717. return node.method || node.kind === "get" || node.kind === "set";
  718. }
  719. finishNodeAt(node, type, endLoc) {
  720. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  721. }
  722. resetStartLocation(node, start, startLoc) {
  723. super.resetStartLocation(node, start, startLoc);
  724. toESTreeLocation(node);
  725. }
  726. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  727. super.resetEndLocation(node, endLoc);
  728. toESTreeLocation(node);
  729. }
  730. });
  731. class TokContext {
  732. constructor(token, preserveSpace) {
  733. this.token = void 0;
  734. this.preserveSpace = void 0;
  735. this.token = token;
  736. this.preserveSpace = !!preserveSpace;
  737. }
  738. }
  739. const types = {
  740. brace: new TokContext("{"),
  741. j_oTag: new TokContext("<tag"),
  742. j_cTag: new TokContext("</tag"),
  743. j_expr: new TokContext("<tag>...</tag>", true)
  744. };
  745. {
  746. types.template = new TokContext("`", true);
  747. }
  748. const beforeExpr = true;
  749. const startsExpr = true;
  750. const isLoop = true;
  751. const isAssign = true;
  752. const prefix = true;
  753. const postfix = true;
  754. class ExportedTokenType {
  755. constructor(label, conf = {}) {
  756. this.label = void 0;
  757. this.keyword = void 0;
  758. this.beforeExpr = void 0;
  759. this.startsExpr = void 0;
  760. this.rightAssociative = void 0;
  761. this.isLoop = void 0;
  762. this.isAssign = void 0;
  763. this.prefix = void 0;
  764. this.postfix = void 0;
  765. this.binop = void 0;
  766. this.label = label;
  767. this.keyword = conf.keyword;
  768. this.beforeExpr = !!conf.beforeExpr;
  769. this.startsExpr = !!conf.startsExpr;
  770. this.rightAssociative = !!conf.rightAssociative;
  771. this.isLoop = !!conf.isLoop;
  772. this.isAssign = !!conf.isAssign;
  773. this.prefix = !!conf.prefix;
  774. this.postfix = !!conf.postfix;
  775. this.binop = conf.binop != null ? conf.binop : null;
  776. {
  777. this.updateContext = null;
  778. }
  779. }
  780. }
  781. const keywords$1 = new Map();
  782. function createKeyword(name, options = {}) {
  783. options.keyword = name;
  784. const token = createToken(name, options);
  785. keywords$1.set(name, token);
  786. return token;
  787. }
  788. function createBinop(name, binop) {
  789. return createToken(name, {
  790. beforeExpr,
  791. binop
  792. });
  793. }
  794. let tokenTypeCounter = -1;
  795. const tokenTypes = [];
  796. const tokenLabels = [];
  797. const tokenBinops = [];
  798. const tokenBeforeExprs = [];
  799. const tokenStartsExprs = [];
  800. const tokenPrefixes = [];
  801. function createToken(name, options = {}) {
  802. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  803. ++tokenTypeCounter;
  804. tokenLabels.push(name);
  805. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  806. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  807. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  808. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  809. tokenTypes.push(new ExportedTokenType(name, options));
  810. return tokenTypeCounter;
  811. }
  812. function createKeywordLike(name, options = {}) {
  813. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  814. ++tokenTypeCounter;
  815. keywords$1.set(name, tokenTypeCounter);
  816. tokenLabels.push(name);
  817. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  818. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  819. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  820. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  821. tokenTypes.push(new ExportedTokenType("name", options));
  822. return tokenTypeCounter;
  823. }
  824. const tt = {
  825. bracketL: createToken("[", {
  826. beforeExpr,
  827. startsExpr
  828. }),
  829. bracketHashL: createToken("#[", {
  830. beforeExpr,
  831. startsExpr
  832. }),
  833. bracketBarL: createToken("[|", {
  834. beforeExpr,
  835. startsExpr
  836. }),
  837. bracketR: createToken("]"),
  838. bracketBarR: createToken("|]"),
  839. braceL: createToken("{", {
  840. beforeExpr,
  841. startsExpr
  842. }),
  843. braceBarL: createToken("{|", {
  844. beforeExpr,
  845. startsExpr
  846. }),
  847. braceHashL: createToken("#{", {
  848. beforeExpr,
  849. startsExpr
  850. }),
  851. braceR: createToken("}"),
  852. braceBarR: createToken("|}"),
  853. parenL: createToken("(", {
  854. beforeExpr,
  855. startsExpr
  856. }),
  857. parenR: createToken(")"),
  858. comma: createToken(",", {
  859. beforeExpr
  860. }),
  861. semi: createToken(";", {
  862. beforeExpr
  863. }),
  864. colon: createToken(":", {
  865. beforeExpr
  866. }),
  867. doubleColon: createToken("::", {
  868. beforeExpr
  869. }),
  870. dot: createToken("."),
  871. question: createToken("?", {
  872. beforeExpr
  873. }),
  874. questionDot: createToken("?."),
  875. arrow: createToken("=>", {
  876. beforeExpr
  877. }),
  878. template: createToken("template"),
  879. ellipsis: createToken("...", {
  880. beforeExpr
  881. }),
  882. backQuote: createToken("`", {
  883. startsExpr
  884. }),
  885. dollarBraceL: createToken("${", {
  886. beforeExpr,
  887. startsExpr
  888. }),
  889. templateTail: createToken("...`", {
  890. startsExpr
  891. }),
  892. templateNonTail: createToken("...${", {
  893. beforeExpr,
  894. startsExpr
  895. }),
  896. at: createToken("@"),
  897. hash: createToken("#", {
  898. startsExpr
  899. }),
  900. interpreterDirective: createToken("#!..."),
  901. eq: createToken("=", {
  902. beforeExpr,
  903. isAssign
  904. }),
  905. assign: createToken("_=", {
  906. beforeExpr,
  907. isAssign
  908. }),
  909. slashAssign: createToken("_=", {
  910. beforeExpr,
  911. isAssign
  912. }),
  913. xorAssign: createToken("_=", {
  914. beforeExpr,
  915. isAssign
  916. }),
  917. moduloAssign: createToken("_=", {
  918. beforeExpr,
  919. isAssign
  920. }),
  921. incDec: createToken("++/--", {
  922. prefix,
  923. postfix,
  924. startsExpr
  925. }),
  926. bang: createToken("!", {
  927. beforeExpr,
  928. prefix,
  929. startsExpr
  930. }),
  931. tilde: createToken("~", {
  932. beforeExpr,
  933. prefix,
  934. startsExpr
  935. }),
  936. doubleCaret: createToken("^^", {
  937. startsExpr
  938. }),
  939. doubleAt: createToken("@@", {
  940. startsExpr
  941. }),
  942. pipeline: createBinop("|>", 0),
  943. nullishCoalescing: createBinop("??", 1),
  944. logicalOR: createBinop("||", 1),
  945. logicalAND: createBinop("&&", 2),
  946. bitwiseOR: createBinop("|", 3),
  947. bitwiseXOR: createBinop("^", 4),
  948. bitwiseAND: createBinop("&", 5),
  949. equality: createBinop("==/!=/===/!==", 6),
  950. lt: createBinop("</>/<=/>=", 7),
  951. gt: createBinop("</>/<=/>=", 7),
  952. relational: createBinop("</>/<=/>=", 7),
  953. bitShift: createBinop("<</>>/>>>", 8),
  954. bitShiftL: createBinop("<</>>/>>>", 8),
  955. bitShiftR: createBinop("<</>>/>>>", 8),
  956. plusMin: createToken("+/-", {
  957. beforeExpr,
  958. binop: 9,
  959. prefix,
  960. startsExpr
  961. }),
  962. modulo: createToken("%", {
  963. binop: 10,
  964. startsExpr
  965. }),
  966. star: createToken("*", {
  967. binop: 10
  968. }),
  969. slash: createBinop("/", 10),
  970. exponent: createToken("**", {
  971. beforeExpr,
  972. binop: 11,
  973. rightAssociative: true
  974. }),
  975. _in: createKeyword("in", {
  976. beforeExpr,
  977. binop: 7
  978. }),
  979. _instanceof: createKeyword("instanceof", {
  980. beforeExpr,
  981. binop: 7
  982. }),
  983. _break: createKeyword("break"),
  984. _case: createKeyword("case", {
  985. beforeExpr
  986. }),
  987. _catch: createKeyword("catch"),
  988. _continue: createKeyword("continue"),
  989. _debugger: createKeyword("debugger"),
  990. _default: createKeyword("default", {
  991. beforeExpr
  992. }),
  993. _else: createKeyword("else", {
  994. beforeExpr
  995. }),
  996. _finally: createKeyword("finally"),
  997. _function: createKeyword("function", {
  998. startsExpr
  999. }),
  1000. _if: createKeyword("if"),
  1001. _return: createKeyword("return", {
  1002. beforeExpr
  1003. }),
  1004. _switch: createKeyword("switch"),
  1005. _throw: createKeyword("throw", {
  1006. beforeExpr,
  1007. prefix,
  1008. startsExpr
  1009. }),
  1010. _try: createKeyword("try"),
  1011. _var: createKeyword("var"),
  1012. _const: createKeyword("const"),
  1013. _with: createKeyword("with"),
  1014. _new: createKeyword("new", {
  1015. beforeExpr,
  1016. startsExpr
  1017. }),
  1018. _this: createKeyword("this", {
  1019. startsExpr
  1020. }),
  1021. _super: createKeyword("super", {
  1022. startsExpr
  1023. }),
  1024. _class: createKeyword("class", {
  1025. startsExpr
  1026. }),
  1027. _extends: createKeyword("extends", {
  1028. beforeExpr
  1029. }),
  1030. _export: createKeyword("export"),
  1031. _import: createKeyword("import", {
  1032. startsExpr
  1033. }),
  1034. _null: createKeyword("null", {
  1035. startsExpr
  1036. }),
  1037. _true: createKeyword("true", {
  1038. startsExpr
  1039. }),
  1040. _false: createKeyword("false", {
  1041. startsExpr
  1042. }),
  1043. _typeof: createKeyword("typeof", {
  1044. beforeExpr,
  1045. prefix,
  1046. startsExpr
  1047. }),
  1048. _void: createKeyword("void", {
  1049. beforeExpr,
  1050. prefix,
  1051. startsExpr
  1052. }),
  1053. _delete: createKeyword("delete", {
  1054. beforeExpr,
  1055. prefix,
  1056. startsExpr
  1057. }),
  1058. _do: createKeyword("do", {
  1059. isLoop,
  1060. beforeExpr
  1061. }),
  1062. _for: createKeyword("for", {
  1063. isLoop
  1064. }),
  1065. _while: createKeyword("while", {
  1066. isLoop
  1067. }),
  1068. _as: createKeywordLike("as", {
  1069. startsExpr
  1070. }),
  1071. _assert: createKeywordLike("assert", {
  1072. startsExpr
  1073. }),
  1074. _async: createKeywordLike("async", {
  1075. startsExpr
  1076. }),
  1077. _await: createKeywordLike("await", {
  1078. startsExpr
  1079. }),
  1080. _from: createKeywordLike("from", {
  1081. startsExpr
  1082. }),
  1083. _get: createKeywordLike("get", {
  1084. startsExpr
  1085. }),
  1086. _let: createKeywordLike("let", {
  1087. startsExpr
  1088. }),
  1089. _meta: createKeywordLike("meta", {
  1090. startsExpr
  1091. }),
  1092. _of: createKeywordLike("of", {
  1093. startsExpr
  1094. }),
  1095. _sent: createKeywordLike("sent", {
  1096. startsExpr
  1097. }),
  1098. _set: createKeywordLike("set", {
  1099. startsExpr
  1100. }),
  1101. _static: createKeywordLike("static", {
  1102. startsExpr
  1103. }),
  1104. _yield: createKeywordLike("yield", {
  1105. startsExpr
  1106. }),
  1107. _asserts: createKeywordLike("asserts", {
  1108. startsExpr
  1109. }),
  1110. _checks: createKeywordLike("checks", {
  1111. startsExpr
  1112. }),
  1113. _exports: createKeywordLike("exports", {
  1114. startsExpr
  1115. }),
  1116. _global: createKeywordLike("global", {
  1117. startsExpr
  1118. }),
  1119. _implements: createKeywordLike("implements", {
  1120. startsExpr
  1121. }),
  1122. _intrinsic: createKeywordLike("intrinsic", {
  1123. startsExpr
  1124. }),
  1125. _infer: createKeywordLike("infer", {
  1126. startsExpr
  1127. }),
  1128. _is: createKeywordLike("is", {
  1129. startsExpr
  1130. }),
  1131. _mixins: createKeywordLike("mixins", {
  1132. startsExpr
  1133. }),
  1134. _proto: createKeywordLike("proto", {
  1135. startsExpr
  1136. }),
  1137. _require: createKeywordLike("require", {
  1138. startsExpr
  1139. }),
  1140. _keyof: createKeywordLike("keyof", {
  1141. startsExpr
  1142. }),
  1143. _readonly: createKeywordLike("readonly", {
  1144. startsExpr
  1145. }),
  1146. _unique: createKeywordLike("unique", {
  1147. startsExpr
  1148. }),
  1149. _abstract: createKeywordLike("abstract", {
  1150. startsExpr
  1151. }),
  1152. _declare: createKeywordLike("declare", {
  1153. startsExpr
  1154. }),
  1155. _enum: createKeywordLike("enum", {
  1156. startsExpr
  1157. }),
  1158. _module: createKeywordLike("module", {
  1159. startsExpr
  1160. }),
  1161. _namespace: createKeywordLike("namespace", {
  1162. startsExpr
  1163. }),
  1164. _interface: createKeywordLike("interface", {
  1165. startsExpr
  1166. }),
  1167. _type: createKeywordLike("type", {
  1168. startsExpr
  1169. }),
  1170. _opaque: createKeywordLike("opaque", {
  1171. startsExpr
  1172. }),
  1173. name: createToken("name", {
  1174. startsExpr
  1175. }),
  1176. string: createToken("string", {
  1177. startsExpr
  1178. }),
  1179. num: createToken("num", {
  1180. startsExpr
  1181. }),
  1182. bigint: createToken("bigint", {
  1183. startsExpr
  1184. }),
  1185. decimal: createToken("decimal", {
  1186. startsExpr
  1187. }),
  1188. regexp: createToken("regexp", {
  1189. startsExpr
  1190. }),
  1191. privateName: createToken("#name", {
  1192. startsExpr
  1193. }),
  1194. eof: createToken("eof"),
  1195. jsxName: createToken("jsxName"),
  1196. jsxText: createToken("jsxText", {
  1197. beforeExpr: true
  1198. }),
  1199. jsxTagStart: createToken("jsxTagStart", {
  1200. startsExpr: true
  1201. }),
  1202. jsxTagEnd: createToken("jsxTagEnd"),
  1203. placeholder: createToken("%%", {
  1204. startsExpr: true
  1205. })
  1206. };
  1207. function tokenIsIdentifier(token) {
  1208. return token >= 93 && token <= 128;
  1209. }
  1210. function tokenKeywordOrIdentifierIsKeyword(token) {
  1211. return token <= 92;
  1212. }
  1213. function tokenIsKeywordOrIdentifier(token) {
  1214. return token >= 58 && token <= 128;
  1215. }
  1216. function tokenIsLiteralPropertyName(token) {
  1217. return token >= 58 && token <= 132;
  1218. }
  1219. function tokenComesBeforeExpression(token) {
  1220. return tokenBeforeExprs[token];
  1221. }
  1222. function tokenCanStartExpression(token) {
  1223. return tokenStartsExprs[token];
  1224. }
  1225. function tokenIsAssignment(token) {
  1226. return token >= 29 && token <= 33;
  1227. }
  1228. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1229. return token >= 125 && token <= 127;
  1230. }
  1231. function tokenIsLoop(token) {
  1232. return token >= 90 && token <= 92;
  1233. }
  1234. function tokenIsKeyword(token) {
  1235. return token >= 58 && token <= 92;
  1236. }
  1237. function tokenIsOperator(token) {
  1238. return token >= 39 && token <= 59;
  1239. }
  1240. function tokenIsPostfix(token) {
  1241. return token === 34;
  1242. }
  1243. function tokenIsPrefix(token) {
  1244. return tokenPrefixes[token];
  1245. }
  1246. function tokenIsTSTypeOperator(token) {
  1247. return token >= 117 && token <= 119;
  1248. }
  1249. function tokenIsTSDeclarationStart(token) {
  1250. return token >= 120 && token <= 126;
  1251. }
  1252. function tokenLabelName(token) {
  1253. return tokenLabels[token];
  1254. }
  1255. function tokenOperatorPrecedence(token) {
  1256. return tokenBinops[token];
  1257. }
  1258. function tokenIsRightAssociative(token) {
  1259. return token === 57;
  1260. }
  1261. function tokenIsTemplate(token) {
  1262. return token >= 24 && token <= 25;
  1263. }
  1264. function getExportedToken(token) {
  1265. return tokenTypes[token];
  1266. }
  1267. {
  1268. tokenTypes[8].updateContext = context => {
  1269. context.pop();
  1270. };
  1271. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1272. context.push(types.brace);
  1273. };
  1274. tokenTypes[22].updateContext = context => {
  1275. if (context[context.length - 1] === types.template) {
  1276. context.pop();
  1277. } else {
  1278. context.push(types.template);
  1279. }
  1280. };
  1281. tokenTypes[138].updateContext = context => {
  1282. context.push(types.j_expr, types.j_oTag);
  1283. };
  1284. }
  1285. 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";
  1286. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u0898-\u089f\u08ca-\u08e1\u08e3-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u09fe\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0afa-\u0aff\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b55-\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c00-\u0c04\u0c3c\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0cf3\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ece\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1715\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u180f-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf-\u1ace\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf4\u1cf7-\u1cf9\u1dc0-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  1287. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1288. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1289. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1290. const astralIdentifierStartCodes = [0, 11, 2, 25, 2, 18, 2, 1, 2, 14, 3, 13, 35, 122, 70, 52, 268, 28, 4, 48, 48, 31, 14, 29, 6, 37, 11, 29, 3, 35, 5, 7, 2, 4, 43, 157, 19, 35, 5, 35, 5, 39, 9, 51, 13, 10, 2, 14, 2, 6, 2, 1, 2, 10, 2, 14, 2, 6, 2, 1, 68, 310, 10, 21, 11, 7, 25, 5, 2, 41, 2, 8, 70, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 43, 17, 47, 20, 28, 22, 13, 52, 58, 1, 3, 0, 14, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 20, 1, 64, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 38, 6, 186, 43, 117, 63, 32, 7, 3, 0, 3, 7, 2, 1, 2, 23, 16, 0, 2, 0, 95, 7, 3, 38, 17, 0, 2, 0, 29, 0, 11, 39, 8, 0, 22, 0, 12, 45, 20, 0, 19, 72, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 4026, 582, 8634, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8936, 3, 2, 6, 2, 1, 2, 290, 16, 0, 30, 2, 3, 0, 15, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 1845, 30, 7, 5, 262, 61, 147, 44, 11, 6, 17, 0, 322, 29, 19, 43, 485, 27, 757, 6, 2, 3, 2, 1, 2, 14, 2, 196, 60, 67, 8, 0, 1205, 3, 2, 26, 2, 1, 2, 0, 3, 0, 2, 9, 2, 3, 2, 0, 2, 0, 7, 0, 5, 0, 2, 0, 2, 0, 2, 2, 2, 1, 2, 0, 3, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 1, 2, 0, 3, 3, 2, 6, 2, 3, 2, 3, 2, 0, 2, 9, 2, 16, 6, 2, 2, 4, 2, 16, 4421, 42719, 33, 4153, 7, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938, 6, 4191];
  1291. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1292. function isInAstralSet(code, set) {
  1293. let pos = 0x10000;
  1294. for (let i = 0, length = set.length; i < length; i += 2) {
  1295. pos += set[i];
  1296. if (pos > code) return false;
  1297. pos += set[i + 1];
  1298. if (pos >= code) return true;
  1299. }
  1300. return false;
  1301. }
  1302. function isIdentifierStart(code) {
  1303. if (code < 65) return code === 36;
  1304. if (code <= 90) return true;
  1305. if (code < 97) return code === 95;
  1306. if (code <= 122) return true;
  1307. if (code <= 0xffff) {
  1308. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1309. }
  1310. return isInAstralSet(code, astralIdentifierStartCodes);
  1311. }
  1312. function isIdentifierChar(code) {
  1313. if (code < 48) return code === 36;
  1314. if (code < 58) return true;
  1315. if (code < 65) return false;
  1316. if (code <= 90) return true;
  1317. if (code < 97) return code === 95;
  1318. if (code <= 122) return true;
  1319. if (code <= 0xffff) {
  1320. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1321. }
  1322. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1323. }
  1324. const reservedWords = {
  1325. 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"],
  1326. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1327. strictBind: ["eval", "arguments"]
  1328. };
  1329. const keywords = new Set(reservedWords.keyword);
  1330. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1331. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1332. function isReservedWord(word, inModule) {
  1333. return inModule && word === "await" || word === "enum";
  1334. }
  1335. function isStrictReservedWord(word, inModule) {
  1336. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1337. }
  1338. function isStrictBindOnlyReservedWord(word) {
  1339. return reservedWordsStrictBindSet.has(word);
  1340. }
  1341. function isStrictBindReservedWord(word, inModule) {
  1342. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1343. }
  1344. function isKeyword(word) {
  1345. return keywords.has(word);
  1346. }
  1347. function isIteratorStart(current, next, next2) {
  1348. return current === 64 && next === 64 && isIdentifierStart(next2);
  1349. }
  1350. 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"]);
  1351. function canBeReservedWord(word) {
  1352. return reservedWordLikeSet.has(word);
  1353. }
  1354. const SCOPE_OTHER = 0b000000000,
  1355. SCOPE_PROGRAM = 0b000000001,
  1356. SCOPE_FUNCTION = 0b000000010,
  1357. SCOPE_ARROW = 0b000000100,
  1358. SCOPE_SIMPLE_CATCH = 0b000001000,
  1359. SCOPE_SUPER = 0b000010000,
  1360. SCOPE_DIRECT_SUPER = 0b000100000,
  1361. SCOPE_CLASS = 0b001000000,
  1362. SCOPE_STATIC_BLOCK = 0b010000000,
  1363. SCOPE_TS_MODULE = 0b100000000,
  1364. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1365. const BIND_KIND_VALUE = 0b000000000001,
  1366. BIND_KIND_TYPE = 0b000000000010,
  1367. BIND_SCOPE_VAR = 0b000000000100,
  1368. BIND_SCOPE_LEXICAL = 0b000000001000,
  1369. BIND_SCOPE_FUNCTION = 0b000000010000,
  1370. BIND_FLAGS_NONE = 0b0000001000000,
  1371. BIND_FLAGS_CLASS = 0b0000010000000,
  1372. BIND_FLAGS_TS_ENUM = 0b0000100000000,
  1373. BIND_FLAGS_TS_CONST_ENUM = 0b0001000000000,
  1374. BIND_FLAGS_TS_EXPORT_ONLY = 0b0010000000000,
  1375. BIND_FLAGS_FLOW_DECLARE_FN = 0b0100000000000,
  1376. BIND_FLAGS_TS_IMPORT = 0b1000000000000;
  1377. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1378. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1379. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1380. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1381. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1382. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1383. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1384. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1385. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1386. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1387. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1388. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1389. BIND_TS_TYPE_IMPORT = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_TS_IMPORT,
  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(parser, inModule) {
  1410. this.parser = void 0;
  1411. this.scopeStack = [];
  1412. this.inModule = void 0;
  1413. this.undefinedExports = new Map();
  1414. this.parser = parser;
  1415. this.inModule = inModule;
  1416. }
  1417. get inFunction() {
  1418. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1419. }
  1420. get allowSuper() {
  1421. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1422. }
  1423. get allowDirectSuper() {
  1424. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1425. }
  1426. get inClass() {
  1427. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1428. }
  1429. get inClassAndNotInNonArrowFunction() {
  1430. const flags = this.currentThisScopeFlags();
  1431. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1432. }
  1433. get inStaticBlock() {
  1434. for (let i = this.scopeStack.length - 1;; i--) {
  1435. const {
  1436. flags
  1437. } = this.scopeStack[i];
  1438. if (flags & SCOPE_STATIC_BLOCK) {
  1439. return true;
  1440. }
  1441. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1442. return false;
  1443. }
  1444. }
  1445. }
  1446. get inNonArrowFunction() {
  1447. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1448. }
  1449. get treatFunctionsAsVar() {
  1450. return this.treatFunctionsAsVarInScope(this.currentScope());
  1451. }
  1452. createScope(flags) {
  1453. return new Scope(flags);
  1454. }
  1455. enter(flags) {
  1456. this.scopeStack.push(this.createScope(flags));
  1457. }
  1458. exit() {
  1459. const scope = this.scopeStack.pop();
  1460. return scope.flags;
  1461. }
  1462. treatFunctionsAsVarInScope(scope) {
  1463. return !!(scope.flags & (SCOPE_FUNCTION | SCOPE_STATIC_BLOCK) || !this.parser.inModule && scope.flags & SCOPE_PROGRAM);
  1464. }
  1465. declareName(name, bindingType, loc) {
  1466. let scope = this.currentScope();
  1467. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1468. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1469. if (bindingType & BIND_SCOPE_FUNCTION) {
  1470. scope.functions.add(name);
  1471. } else {
  1472. scope.lexical.add(name);
  1473. }
  1474. if (bindingType & BIND_SCOPE_LEXICAL) {
  1475. this.maybeExportDefined(scope, name);
  1476. }
  1477. } else if (bindingType & BIND_SCOPE_VAR) {
  1478. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1479. scope = this.scopeStack[i];
  1480. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1481. scope.var.add(name);
  1482. this.maybeExportDefined(scope, name);
  1483. if (scope.flags & SCOPE_VAR) break;
  1484. }
  1485. }
  1486. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1487. this.undefinedExports.delete(name);
  1488. }
  1489. }
  1490. maybeExportDefined(scope, name) {
  1491. if (this.parser.inModule && scope.flags & SCOPE_PROGRAM) {
  1492. this.undefinedExports.delete(name);
  1493. }
  1494. }
  1495. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1496. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1497. this.parser.raise(Errors.VarRedeclaration, {
  1498. at: loc,
  1499. identifierName: name
  1500. });
  1501. }
  1502. }
  1503. isRedeclaredInScope(scope, name, bindingType) {
  1504. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1505. if (bindingType & BIND_SCOPE_LEXICAL) {
  1506. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1507. }
  1508. if (bindingType & BIND_SCOPE_FUNCTION) {
  1509. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1510. }
  1511. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1512. }
  1513. checkLocalExport(id) {
  1514. const {
  1515. name
  1516. } = id;
  1517. const topLevelScope = this.scopeStack[0];
  1518. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1519. this.undefinedExports.set(name, id.loc.start);
  1520. }
  1521. }
  1522. currentScope() {
  1523. return this.scopeStack[this.scopeStack.length - 1];
  1524. }
  1525. currentVarScopeFlags() {
  1526. for (let i = this.scopeStack.length - 1;; i--) {
  1527. const {
  1528. flags
  1529. } = this.scopeStack[i];
  1530. if (flags & SCOPE_VAR) {
  1531. return flags;
  1532. }
  1533. }
  1534. }
  1535. currentThisScopeFlags() {
  1536. for (let i = this.scopeStack.length - 1;; i--) {
  1537. const {
  1538. flags
  1539. } = this.scopeStack[i];
  1540. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1541. return flags;
  1542. }
  1543. }
  1544. }
  1545. }
  1546. class FlowScope extends Scope {
  1547. constructor(...args) {
  1548. super(...args);
  1549. this.declareFunctions = new Set();
  1550. }
  1551. }
  1552. class FlowScopeHandler extends ScopeHandler {
  1553. createScope(flags) {
  1554. return new FlowScope(flags);
  1555. }
  1556. declareName(name, bindingType, loc) {
  1557. const scope = this.currentScope();
  1558. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1559. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1560. this.maybeExportDefined(scope, name);
  1561. scope.declareFunctions.add(name);
  1562. return;
  1563. }
  1564. super.declareName(name, bindingType, loc);
  1565. }
  1566. isRedeclaredInScope(scope, name, bindingType) {
  1567. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1568. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1569. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1570. }
  1571. return false;
  1572. }
  1573. checkLocalExport(id) {
  1574. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1575. super.checkLocalExport(id);
  1576. }
  1577. }
  1578. }
  1579. class BaseParser {
  1580. constructor() {
  1581. this.sawUnambiguousESM = false;
  1582. this.ambiguousScriptDifferentAst = false;
  1583. }
  1584. hasPlugin(pluginConfig) {
  1585. if (typeof pluginConfig === "string") {
  1586. return this.plugins.has(pluginConfig);
  1587. } else {
  1588. const [pluginName, pluginOptions] = pluginConfig;
  1589. if (!this.hasPlugin(pluginName)) {
  1590. return false;
  1591. }
  1592. const actualOptions = this.plugins.get(pluginName);
  1593. for (const key of Object.keys(pluginOptions)) {
  1594. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  1595. return false;
  1596. }
  1597. }
  1598. return true;
  1599. }
  1600. }
  1601. getPluginOption(plugin, name) {
  1602. var _this$plugins$get;
  1603. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  1604. }
  1605. }
  1606. function setTrailingComments(node, comments) {
  1607. if (node.trailingComments === undefined) {
  1608. node.trailingComments = comments;
  1609. } else {
  1610. node.trailingComments.unshift(...comments);
  1611. }
  1612. }
  1613. function setLeadingComments(node, comments) {
  1614. if (node.leadingComments === undefined) {
  1615. node.leadingComments = comments;
  1616. } else {
  1617. node.leadingComments.unshift(...comments);
  1618. }
  1619. }
  1620. function setInnerComments(node, comments) {
  1621. if (node.innerComments === undefined) {
  1622. node.innerComments = comments;
  1623. } else {
  1624. node.innerComments.unshift(...comments);
  1625. }
  1626. }
  1627. function adjustInnerComments(node, elements, commentWS) {
  1628. let lastElement = null;
  1629. let i = elements.length;
  1630. while (lastElement === null && i > 0) {
  1631. lastElement = elements[--i];
  1632. }
  1633. if (lastElement === null || lastElement.start > commentWS.start) {
  1634. setInnerComments(node, commentWS.comments);
  1635. } else {
  1636. setTrailingComments(lastElement, commentWS.comments);
  1637. }
  1638. }
  1639. class CommentsParser extends BaseParser {
  1640. addComment(comment) {
  1641. if (this.filename) comment.loc.filename = this.filename;
  1642. this.state.comments.push(comment);
  1643. }
  1644. processComment(node) {
  1645. const {
  1646. commentStack
  1647. } = this.state;
  1648. const commentStackLength = commentStack.length;
  1649. if (commentStackLength === 0) return;
  1650. let i = commentStackLength - 1;
  1651. const lastCommentWS = commentStack[i];
  1652. if (lastCommentWS.start === node.end) {
  1653. lastCommentWS.leadingNode = node;
  1654. i--;
  1655. }
  1656. const {
  1657. start: nodeStart
  1658. } = node;
  1659. for (; i >= 0; i--) {
  1660. const commentWS = commentStack[i];
  1661. const commentEnd = commentWS.end;
  1662. if (commentEnd > nodeStart) {
  1663. commentWS.containingNode = node;
  1664. this.finalizeComment(commentWS);
  1665. commentStack.splice(i, 1);
  1666. } else {
  1667. if (commentEnd === nodeStart) {
  1668. commentWS.trailingNode = node;
  1669. }
  1670. break;
  1671. }
  1672. }
  1673. }
  1674. finalizeComment(commentWS) {
  1675. const {
  1676. comments
  1677. } = commentWS;
  1678. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  1679. if (commentWS.leadingNode !== null) {
  1680. setTrailingComments(commentWS.leadingNode, comments);
  1681. }
  1682. if (commentWS.trailingNode !== null) {
  1683. setLeadingComments(commentWS.trailingNode, comments);
  1684. }
  1685. } else {
  1686. const {
  1687. containingNode: node,
  1688. start: commentStart
  1689. } = commentWS;
  1690. if (this.input.charCodeAt(commentStart - 1) === 44) {
  1691. switch (node.type) {
  1692. case "ObjectExpression":
  1693. case "ObjectPattern":
  1694. case "RecordExpression":
  1695. adjustInnerComments(node, node.properties, commentWS);
  1696. break;
  1697. case "CallExpression":
  1698. case "OptionalCallExpression":
  1699. adjustInnerComments(node, node.arguments, commentWS);
  1700. break;
  1701. case "FunctionDeclaration":
  1702. case "FunctionExpression":
  1703. case "ArrowFunctionExpression":
  1704. case "ObjectMethod":
  1705. case "ClassMethod":
  1706. case "ClassPrivateMethod":
  1707. adjustInnerComments(node, node.params, commentWS);
  1708. break;
  1709. case "ArrayExpression":
  1710. case "ArrayPattern":
  1711. case "TupleExpression":
  1712. adjustInnerComments(node, node.elements, commentWS);
  1713. break;
  1714. case "ExportNamedDeclaration":
  1715. case "ImportDeclaration":
  1716. adjustInnerComments(node, node.specifiers, commentWS);
  1717. break;
  1718. default:
  1719. {
  1720. setInnerComments(node, comments);
  1721. }
  1722. }
  1723. } else {
  1724. setInnerComments(node, comments);
  1725. }
  1726. }
  1727. }
  1728. finalizeRemainingComments() {
  1729. const {
  1730. commentStack
  1731. } = this.state;
  1732. for (let i = commentStack.length - 1; i >= 0; i--) {
  1733. this.finalizeComment(commentStack[i]);
  1734. }
  1735. this.state.commentStack = [];
  1736. }
  1737. resetPreviousNodeTrailingComments(node) {
  1738. const {
  1739. commentStack
  1740. } = this.state;
  1741. const {
  1742. length
  1743. } = commentStack;
  1744. if (length === 0) return;
  1745. const commentWS = commentStack[length - 1];
  1746. if (commentWS.leadingNode === node) {
  1747. commentWS.leadingNode = null;
  1748. }
  1749. }
  1750. takeSurroundingComments(node, start, end) {
  1751. const {
  1752. commentStack
  1753. } = this.state;
  1754. const commentStackLength = commentStack.length;
  1755. if (commentStackLength === 0) return;
  1756. let i = commentStackLength - 1;
  1757. for (; i >= 0; i--) {
  1758. const commentWS = commentStack[i];
  1759. const commentEnd = commentWS.end;
  1760. const commentStart = commentWS.start;
  1761. if (commentStart === end) {
  1762. commentWS.leadingNode = node;
  1763. } else if (commentEnd === start) {
  1764. commentWS.trailingNode = node;
  1765. } else if (commentEnd < start) {
  1766. break;
  1767. }
  1768. }
  1769. }
  1770. }
  1771. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  1772. const lineBreakG = new RegExp(lineBreak.source, "g");
  1773. function isNewLine(code) {
  1774. switch (code) {
  1775. case 10:
  1776. case 13:
  1777. case 8232:
  1778. case 8233:
  1779. return true;
  1780. default:
  1781. return false;
  1782. }
  1783. }
  1784. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  1785. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  1786. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  1787. function isWhitespace(code) {
  1788. switch (code) {
  1789. case 0x0009:
  1790. case 0x000b:
  1791. case 0x000c:
  1792. case 32:
  1793. case 160:
  1794. case 5760:
  1795. case 0x2000:
  1796. case 0x2001:
  1797. case 0x2002:
  1798. case 0x2003:
  1799. case 0x2004:
  1800. case 0x2005:
  1801. case 0x2006:
  1802. case 0x2007:
  1803. case 0x2008:
  1804. case 0x2009:
  1805. case 0x200a:
  1806. case 0x202f:
  1807. case 0x205f:
  1808. case 0x3000:
  1809. case 0xfeff:
  1810. return true;
  1811. default:
  1812. return false;
  1813. }
  1814. }
  1815. class State {
  1816. constructor() {
  1817. this.strict = void 0;
  1818. this.curLine = void 0;
  1819. this.lineStart = void 0;
  1820. this.startLoc = void 0;
  1821. this.endLoc = void 0;
  1822. this.errors = [];
  1823. this.potentialArrowAt = -1;
  1824. this.noArrowAt = [];
  1825. this.noArrowParamsConversionAt = [];
  1826. this.maybeInArrowParameters = false;
  1827. this.inType = false;
  1828. this.noAnonFunctionType = false;
  1829. this.hasFlowComment = false;
  1830. this.isAmbientContext = false;
  1831. this.inAbstractClass = false;
  1832. this.inDisallowConditionalTypesContext = false;
  1833. this.topicContext = {
  1834. maxNumOfResolvableTopics: 0,
  1835. maxTopicIndex: null
  1836. };
  1837. this.soloAwait = false;
  1838. this.inFSharpPipelineDirectBody = false;
  1839. this.labels = [];
  1840. this.decoratorStack = [[]];
  1841. this.comments = [];
  1842. this.commentStack = [];
  1843. this.pos = 0;
  1844. this.type = 135;
  1845. this.value = null;
  1846. this.start = 0;
  1847. this.end = 0;
  1848. this.lastTokEndLoc = null;
  1849. this.lastTokStartLoc = null;
  1850. this.lastTokStart = 0;
  1851. this.context = [types.brace];
  1852. this.canStartJSXElement = true;
  1853. this.containsEsc = false;
  1854. this.firstInvalidTemplateEscapePos = null;
  1855. this.strictErrors = new Map();
  1856. this.tokensLength = 0;
  1857. }
  1858. init({
  1859. strictMode,
  1860. sourceType,
  1861. startLine,
  1862. startColumn
  1863. }) {
  1864. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  1865. this.curLine = startLine;
  1866. this.lineStart = -startColumn;
  1867. this.startLoc = this.endLoc = new Position(startLine, startColumn, 0);
  1868. }
  1869. curPosition() {
  1870. return new Position(this.curLine, this.pos - this.lineStart, this.pos);
  1871. }
  1872. clone(skipArrays) {
  1873. const state = new State();
  1874. const keys = Object.keys(this);
  1875. for (let i = 0, length = keys.length; i < length; i++) {
  1876. const key = keys[i];
  1877. let val = this[key];
  1878. if (!skipArrays && Array.isArray(val)) {
  1879. val = val.slice();
  1880. }
  1881. state[key] = val;
  1882. }
  1883. return state;
  1884. }
  1885. }
  1886. var _isDigit = function isDigit(code) {
  1887. return code >= 48 && code <= 57;
  1888. };
  1889. const forbiddenNumericSeparatorSiblings = {
  1890. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  1891. hex: new Set([46, 88, 95, 120])
  1892. };
  1893. const isAllowedNumericSeparatorSibling = {
  1894. bin: ch => ch === 48 || ch === 49,
  1895. oct: ch => ch >= 48 && ch <= 55,
  1896. dec: ch => ch >= 48 && ch <= 57,
  1897. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  1898. };
  1899. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  1900. const initialPos = pos;
  1901. const initialLineStart = lineStart;
  1902. const initialCurLine = curLine;
  1903. let out = "";
  1904. let firstInvalidLoc = null;
  1905. let chunkStart = pos;
  1906. const {
  1907. length
  1908. } = input;
  1909. for (;;) {
  1910. if (pos >= length) {
  1911. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1912. out += input.slice(chunkStart, pos);
  1913. break;
  1914. }
  1915. const ch = input.charCodeAt(pos);
  1916. if (isStringEnd(type, ch, input, pos)) {
  1917. out += input.slice(chunkStart, pos);
  1918. break;
  1919. }
  1920. if (ch === 92) {
  1921. out += input.slice(chunkStart, pos);
  1922. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  1923. if (res.ch === null && !firstInvalidLoc) {
  1924. firstInvalidLoc = {
  1925. pos,
  1926. lineStart,
  1927. curLine
  1928. };
  1929. } else {
  1930. out += res.ch;
  1931. }
  1932. ({
  1933. pos,
  1934. lineStart,
  1935. curLine
  1936. } = res);
  1937. chunkStart = pos;
  1938. } else if (ch === 8232 || ch === 8233) {
  1939. ++pos;
  1940. ++curLine;
  1941. lineStart = pos;
  1942. } else if (ch === 10 || ch === 13) {
  1943. if (type === "template") {
  1944. out += input.slice(chunkStart, pos) + "\n";
  1945. ++pos;
  1946. if (ch === 13 && input.charCodeAt(pos) === 10) {
  1947. ++pos;
  1948. }
  1949. ++curLine;
  1950. chunkStart = lineStart = pos;
  1951. } else {
  1952. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  1953. }
  1954. } else {
  1955. ++pos;
  1956. }
  1957. }
  1958. return {
  1959. pos,
  1960. str: out,
  1961. firstInvalidLoc,
  1962. lineStart,
  1963. curLine,
  1964. containsInvalid: !!firstInvalidLoc
  1965. };
  1966. }
  1967. function isStringEnd(type, ch, input, pos) {
  1968. if (type === "template") {
  1969. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  1970. }
  1971. return ch === (type === "double" ? 34 : 39);
  1972. }
  1973. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  1974. const throwOnInvalid = !inTemplate;
  1975. pos++;
  1976. const res = ch => ({
  1977. pos,
  1978. ch,
  1979. lineStart,
  1980. curLine
  1981. });
  1982. const ch = input.charCodeAt(pos++);
  1983. switch (ch) {
  1984. case 110:
  1985. return res("\n");
  1986. case 114:
  1987. return res("\r");
  1988. case 120:
  1989. {
  1990. let code;
  1991. ({
  1992. code,
  1993. pos
  1994. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  1995. return res(code === null ? null : String.fromCharCode(code));
  1996. }
  1997. case 117:
  1998. {
  1999. let code;
  2000. ({
  2001. code,
  2002. pos
  2003. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  2004. return res(code === null ? null : String.fromCodePoint(code));
  2005. }
  2006. case 116:
  2007. return res("\t");
  2008. case 98:
  2009. return res("\b");
  2010. case 118:
  2011. return res("\u000b");
  2012. case 102:
  2013. return res("\f");
  2014. case 13:
  2015. if (input.charCodeAt(pos) === 10) {
  2016. ++pos;
  2017. }
  2018. case 10:
  2019. lineStart = pos;
  2020. ++curLine;
  2021. case 8232:
  2022. case 8233:
  2023. return res("");
  2024. case 56:
  2025. case 57:
  2026. if (inTemplate) {
  2027. return res(null);
  2028. } else {
  2029. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  2030. }
  2031. default:
  2032. if (ch >= 48 && ch <= 55) {
  2033. const startPos = pos - 1;
  2034. const match = input.slice(startPos, pos + 2).match(/^[0-7]+/);
  2035. let octalStr = match[0];
  2036. let octal = parseInt(octalStr, 8);
  2037. if (octal > 255) {
  2038. octalStr = octalStr.slice(0, -1);
  2039. octal = parseInt(octalStr, 8);
  2040. }
  2041. pos += octalStr.length - 1;
  2042. const next = input.charCodeAt(pos);
  2043. if (octalStr !== "0" || next === 56 || next === 57) {
  2044. if (inTemplate) {
  2045. return res(null);
  2046. } else {
  2047. errors.strictNumericEscape(startPos, lineStart, curLine);
  2048. }
  2049. }
  2050. return res(String.fromCharCode(octal));
  2051. }
  2052. return res(String.fromCharCode(ch));
  2053. }
  2054. }
  2055. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  2056. const initialPos = pos;
  2057. let n;
  2058. ({
  2059. n,
  2060. pos
  2061. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  2062. if (n === null) {
  2063. if (throwOnInvalid) {
  2064. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  2065. } else {
  2066. pos = initialPos - 1;
  2067. }
  2068. }
  2069. return {
  2070. code: n,
  2071. pos
  2072. };
  2073. }
  2074. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  2075. const start = pos;
  2076. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2077. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  2078. let invalid = false;
  2079. let total = 0;
  2080. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2081. const code = input.charCodeAt(pos);
  2082. let val;
  2083. if (code === 95 && allowNumSeparator !== "bail") {
  2084. const prev = input.charCodeAt(pos - 1);
  2085. const next = input.charCodeAt(pos + 1);
  2086. if (!allowNumSeparator) {
  2087. if (bailOnError) return {
  2088. n: null,
  2089. pos
  2090. };
  2091. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  2092. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  2093. if (bailOnError) return {
  2094. n: null,
  2095. pos
  2096. };
  2097. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  2098. }
  2099. ++pos;
  2100. continue;
  2101. }
  2102. if (code >= 97) {
  2103. val = code - 97 + 10;
  2104. } else if (code >= 65) {
  2105. val = code - 65 + 10;
  2106. } else if (_isDigit(code)) {
  2107. val = code - 48;
  2108. } else {
  2109. val = Infinity;
  2110. }
  2111. if (val >= radix) {
  2112. if (val <= 9 && bailOnError) {
  2113. return {
  2114. n: null,
  2115. pos
  2116. };
  2117. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  2118. val = 0;
  2119. } else if (forceLen) {
  2120. val = 0;
  2121. invalid = true;
  2122. } else {
  2123. break;
  2124. }
  2125. }
  2126. ++pos;
  2127. total = total * radix + val;
  2128. }
  2129. if (pos === start || len != null && pos - start !== len || invalid) {
  2130. return {
  2131. n: null,
  2132. pos
  2133. };
  2134. }
  2135. return {
  2136. n: total,
  2137. pos
  2138. };
  2139. }
  2140. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  2141. const ch = input.charCodeAt(pos);
  2142. let code;
  2143. if (ch === 123) {
  2144. ++pos;
  2145. ({
  2146. code,
  2147. pos
  2148. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  2149. ++pos;
  2150. if (code !== null && code > 0x10ffff) {
  2151. if (throwOnInvalid) {
  2152. errors.invalidCodePoint(pos, lineStart, curLine);
  2153. } else {
  2154. return {
  2155. code: null,
  2156. pos
  2157. };
  2158. }
  2159. }
  2160. } else {
  2161. ({
  2162. code,
  2163. pos
  2164. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  2165. }
  2166. return {
  2167. code,
  2168. pos
  2169. };
  2170. }
  2171. const _excluded = ["at"],
  2172. _excluded2 = ["at"];
  2173. function buildPosition(pos, lineStart, curLine) {
  2174. return new Position(curLine, pos - lineStart, pos);
  2175. }
  2176. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  2177. class Token {
  2178. constructor(state) {
  2179. this.type = state.type;
  2180. this.value = state.value;
  2181. this.start = state.start;
  2182. this.end = state.end;
  2183. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  2184. }
  2185. }
  2186. class Tokenizer extends CommentsParser {
  2187. constructor(options, input) {
  2188. super();
  2189. this.isLookahead = void 0;
  2190. this.tokens = [];
  2191. this.errorHandlers_readInt = {
  2192. invalidDigit: (pos, lineStart, curLine, radix) => {
  2193. if (!this.options.errorRecovery) return false;
  2194. this.raise(Errors.InvalidDigit, {
  2195. at: buildPosition(pos, lineStart, curLine),
  2196. radix
  2197. });
  2198. return true;
  2199. },
  2200. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  2201. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  2202. };
  2203. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  2204. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  2205. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  2206. });
  2207. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  2208. strictNumericEscape: (pos, lineStart, curLine) => {
  2209. this.recordStrictModeErrors(Errors.StrictNumericEscape, {
  2210. at: buildPosition(pos, lineStart, curLine)
  2211. });
  2212. },
  2213. unterminated: (pos, lineStart, curLine) => {
  2214. throw this.raise(Errors.UnterminatedString, {
  2215. at: buildPosition(pos - 1, lineStart, curLine)
  2216. });
  2217. }
  2218. });
  2219. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  2220. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  2221. unterminated: (pos, lineStart, curLine) => {
  2222. throw this.raise(Errors.UnterminatedTemplate, {
  2223. at: buildPosition(pos, lineStart, curLine)
  2224. });
  2225. }
  2226. });
  2227. this.state = new State();
  2228. this.state.init(options);
  2229. this.input = input;
  2230. this.length = input.length;
  2231. this.isLookahead = false;
  2232. }
  2233. pushToken(token) {
  2234. this.tokens.length = this.state.tokensLength;
  2235. this.tokens.push(token);
  2236. ++this.state.tokensLength;
  2237. }
  2238. next() {
  2239. this.checkKeywordEscapes();
  2240. if (this.options.tokens) {
  2241. this.pushToken(new Token(this.state));
  2242. }
  2243. this.state.lastTokStart = this.state.start;
  2244. this.state.lastTokEndLoc = this.state.endLoc;
  2245. this.state.lastTokStartLoc = this.state.startLoc;
  2246. this.nextToken();
  2247. }
  2248. eat(type) {
  2249. if (this.match(type)) {
  2250. this.next();
  2251. return true;
  2252. } else {
  2253. return false;
  2254. }
  2255. }
  2256. match(type) {
  2257. return this.state.type === type;
  2258. }
  2259. createLookaheadState(state) {
  2260. return {
  2261. pos: state.pos,
  2262. value: null,
  2263. type: state.type,
  2264. start: state.start,
  2265. end: state.end,
  2266. context: [this.curContext()],
  2267. inType: state.inType,
  2268. startLoc: state.startLoc,
  2269. lastTokEndLoc: state.lastTokEndLoc,
  2270. curLine: state.curLine,
  2271. lineStart: state.lineStart,
  2272. curPosition: state.curPosition
  2273. };
  2274. }
  2275. lookahead() {
  2276. const old = this.state;
  2277. this.state = this.createLookaheadState(old);
  2278. this.isLookahead = true;
  2279. this.nextToken();
  2280. this.isLookahead = false;
  2281. const curr = this.state;
  2282. this.state = old;
  2283. return curr;
  2284. }
  2285. nextTokenStart() {
  2286. return this.nextTokenStartSince(this.state.pos);
  2287. }
  2288. nextTokenStartSince(pos) {
  2289. skipWhiteSpace.lastIndex = pos;
  2290. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  2291. }
  2292. lookaheadCharCode() {
  2293. return this.input.charCodeAt(this.nextTokenStart());
  2294. }
  2295. codePointAtPos(pos) {
  2296. let cp = this.input.charCodeAt(pos);
  2297. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  2298. const trail = this.input.charCodeAt(pos);
  2299. if ((trail & 0xfc00) === 0xdc00) {
  2300. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  2301. }
  2302. }
  2303. return cp;
  2304. }
  2305. setStrict(strict) {
  2306. this.state.strict = strict;
  2307. if (strict) {
  2308. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, {
  2309. at
  2310. }));
  2311. this.state.strictErrors.clear();
  2312. }
  2313. }
  2314. curContext() {
  2315. return this.state.context[this.state.context.length - 1];
  2316. }
  2317. nextToken() {
  2318. this.skipSpace();
  2319. this.state.start = this.state.pos;
  2320. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  2321. if (this.state.pos >= this.length) {
  2322. this.finishToken(135);
  2323. return;
  2324. }
  2325. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  2326. }
  2327. skipBlockComment() {
  2328. let startLoc;
  2329. if (!this.isLookahead) startLoc = this.state.curPosition();
  2330. const start = this.state.pos;
  2331. const end = this.input.indexOf("*/", start + 2);
  2332. if (end === -1) {
  2333. throw this.raise(Errors.UnterminatedComment, {
  2334. at: this.state.curPosition()
  2335. });
  2336. }
  2337. this.state.pos = end + 2;
  2338. lineBreakG.lastIndex = start + 2;
  2339. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  2340. ++this.state.curLine;
  2341. this.state.lineStart = lineBreakG.lastIndex;
  2342. }
  2343. if (this.isLookahead) return;
  2344. const comment = {
  2345. type: "CommentBlock",
  2346. value: this.input.slice(start + 2, end),
  2347. start,
  2348. end: end + 2,
  2349. loc: new SourceLocation(startLoc, this.state.curPosition())
  2350. };
  2351. if (this.options.tokens) this.pushToken(comment);
  2352. return comment;
  2353. }
  2354. skipLineComment(startSkip) {
  2355. const start = this.state.pos;
  2356. let startLoc;
  2357. if (!this.isLookahead) startLoc = this.state.curPosition();
  2358. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  2359. if (this.state.pos < this.length) {
  2360. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2361. ch = this.input.charCodeAt(this.state.pos);
  2362. }
  2363. }
  2364. if (this.isLookahead) return;
  2365. const end = this.state.pos;
  2366. const value = this.input.slice(start + startSkip, end);
  2367. const comment = {
  2368. type: "CommentLine",
  2369. value,
  2370. start,
  2371. end,
  2372. loc: new SourceLocation(startLoc, this.state.curPosition())
  2373. };
  2374. if (this.options.tokens) this.pushToken(comment);
  2375. return comment;
  2376. }
  2377. skipSpace() {
  2378. const spaceStart = this.state.pos;
  2379. const comments = [];
  2380. loop: while (this.state.pos < this.length) {
  2381. const ch = this.input.charCodeAt(this.state.pos);
  2382. switch (ch) {
  2383. case 32:
  2384. case 160:
  2385. case 9:
  2386. ++this.state.pos;
  2387. break;
  2388. case 13:
  2389. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  2390. ++this.state.pos;
  2391. }
  2392. case 10:
  2393. case 8232:
  2394. case 8233:
  2395. ++this.state.pos;
  2396. ++this.state.curLine;
  2397. this.state.lineStart = this.state.pos;
  2398. break;
  2399. case 47:
  2400. switch (this.input.charCodeAt(this.state.pos + 1)) {
  2401. case 42:
  2402. {
  2403. const comment = this.skipBlockComment();
  2404. if (comment !== undefined) {
  2405. this.addComment(comment);
  2406. if (this.options.attachComment) comments.push(comment);
  2407. }
  2408. break;
  2409. }
  2410. case 47:
  2411. {
  2412. const comment = this.skipLineComment(2);
  2413. if (comment !== undefined) {
  2414. this.addComment(comment);
  2415. if (this.options.attachComment) comments.push(comment);
  2416. }
  2417. break;
  2418. }
  2419. default:
  2420. break loop;
  2421. }
  2422. break;
  2423. default:
  2424. if (isWhitespace(ch)) {
  2425. ++this.state.pos;
  2426. } else if (ch === 45 && !this.inModule) {
  2427. const pos = this.state.pos;
  2428. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  2429. const comment = this.skipLineComment(3);
  2430. if (comment !== undefined) {
  2431. this.addComment(comment);
  2432. if (this.options.attachComment) comments.push(comment);
  2433. }
  2434. } else {
  2435. break loop;
  2436. }
  2437. } else if (ch === 60 && !this.inModule) {
  2438. const pos = this.state.pos;
  2439. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  2440. const comment = this.skipLineComment(4);
  2441. if (comment !== undefined) {
  2442. this.addComment(comment);
  2443. if (this.options.attachComment) comments.push(comment);
  2444. }
  2445. } else {
  2446. break loop;
  2447. }
  2448. } else {
  2449. break loop;
  2450. }
  2451. }
  2452. }
  2453. if (comments.length > 0) {
  2454. const end = this.state.pos;
  2455. const commentWhitespace = {
  2456. start: spaceStart,
  2457. end,
  2458. comments,
  2459. leadingNode: null,
  2460. trailingNode: null,
  2461. containingNode: null
  2462. };
  2463. this.state.commentStack.push(commentWhitespace);
  2464. }
  2465. }
  2466. finishToken(type, val) {
  2467. this.state.end = this.state.pos;
  2468. this.state.endLoc = this.state.curPosition();
  2469. const prevType = this.state.type;
  2470. this.state.type = type;
  2471. this.state.value = val;
  2472. if (!this.isLookahead) {
  2473. this.updateContext(prevType);
  2474. }
  2475. }
  2476. replaceToken(type) {
  2477. this.state.type = type;
  2478. this.updateContext();
  2479. }
  2480. readToken_numberSign() {
  2481. if (this.state.pos === 0 && this.readToken_interpreter()) {
  2482. return;
  2483. }
  2484. const nextPos = this.state.pos + 1;
  2485. const next = this.codePointAtPos(nextPos);
  2486. if (next >= 48 && next <= 57) {
  2487. throw this.raise(Errors.UnexpectedDigitAfterHash, {
  2488. at: this.state.curPosition()
  2489. });
  2490. }
  2491. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  2492. this.expectPlugin("recordAndTuple");
  2493. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  2494. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, {
  2495. at: this.state.curPosition()
  2496. });
  2497. }
  2498. this.state.pos += 2;
  2499. if (next === 123) {
  2500. this.finishToken(7);
  2501. } else {
  2502. this.finishToken(1);
  2503. }
  2504. } else if (isIdentifierStart(next)) {
  2505. ++this.state.pos;
  2506. this.finishToken(134, this.readWord1(next));
  2507. } else if (next === 92) {
  2508. ++this.state.pos;
  2509. this.finishToken(134, this.readWord1());
  2510. } else {
  2511. this.finishOp(27, 1);
  2512. }
  2513. }
  2514. readToken_dot() {
  2515. const next = this.input.charCodeAt(this.state.pos + 1);
  2516. if (next >= 48 && next <= 57) {
  2517. this.readNumber(true);
  2518. return;
  2519. }
  2520. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  2521. this.state.pos += 3;
  2522. this.finishToken(21);
  2523. } else {
  2524. ++this.state.pos;
  2525. this.finishToken(16);
  2526. }
  2527. }
  2528. readToken_slash() {
  2529. const next = this.input.charCodeAt(this.state.pos + 1);
  2530. if (next === 61) {
  2531. this.finishOp(31, 2);
  2532. } else {
  2533. this.finishOp(56, 1);
  2534. }
  2535. }
  2536. readToken_interpreter() {
  2537. if (this.state.pos !== 0 || this.length < 2) return false;
  2538. let ch = this.input.charCodeAt(this.state.pos + 1);
  2539. if (ch !== 33) return false;
  2540. const start = this.state.pos;
  2541. this.state.pos += 1;
  2542. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  2543. ch = this.input.charCodeAt(this.state.pos);
  2544. }
  2545. const value = this.input.slice(start + 2, this.state.pos);
  2546. this.finishToken(28, value);
  2547. return true;
  2548. }
  2549. readToken_mult_modulo(code) {
  2550. let type = code === 42 ? 55 : 54;
  2551. let width = 1;
  2552. let next = this.input.charCodeAt(this.state.pos + 1);
  2553. if (code === 42 && next === 42) {
  2554. width++;
  2555. next = this.input.charCodeAt(this.state.pos + 2);
  2556. type = 57;
  2557. }
  2558. if (next === 61 && !this.state.inType) {
  2559. width++;
  2560. type = code === 37 ? 33 : 30;
  2561. }
  2562. this.finishOp(type, width);
  2563. }
  2564. readToken_pipe_amp(code) {
  2565. const next = this.input.charCodeAt(this.state.pos + 1);
  2566. if (next === code) {
  2567. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  2568. this.finishOp(30, 3);
  2569. } else {
  2570. this.finishOp(code === 124 ? 41 : 42, 2);
  2571. }
  2572. return;
  2573. }
  2574. if (code === 124) {
  2575. if (next === 62) {
  2576. this.finishOp(39, 2);
  2577. return;
  2578. }
  2579. if (this.hasPlugin("recordAndTuple") && next === 125) {
  2580. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2581. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, {
  2582. at: this.state.curPosition()
  2583. });
  2584. }
  2585. this.state.pos += 2;
  2586. this.finishToken(9);
  2587. return;
  2588. }
  2589. if (this.hasPlugin("recordAndTuple") && next === 93) {
  2590. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2591. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, {
  2592. at: this.state.curPosition()
  2593. });
  2594. }
  2595. this.state.pos += 2;
  2596. this.finishToken(4);
  2597. return;
  2598. }
  2599. }
  2600. if (next === 61) {
  2601. this.finishOp(30, 2);
  2602. return;
  2603. }
  2604. this.finishOp(code === 124 ? 43 : 45, 1);
  2605. }
  2606. readToken_caret() {
  2607. const next = this.input.charCodeAt(this.state.pos + 1);
  2608. if (next === 61 && !this.state.inType) {
  2609. this.finishOp(32, 2);
  2610. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  2611. proposal: "hack",
  2612. topicToken: "^^"
  2613. }])) {
  2614. this.finishOp(37, 2);
  2615. const lookaheadCh = this.input.codePointAt(this.state.pos);
  2616. if (lookaheadCh === 94) {
  2617. throw this.unexpected();
  2618. }
  2619. } else {
  2620. this.finishOp(44, 1);
  2621. }
  2622. }
  2623. readToken_atSign() {
  2624. const next = this.input.charCodeAt(this.state.pos + 1);
  2625. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  2626. proposal: "hack",
  2627. topicToken: "@@"
  2628. }])) {
  2629. this.finishOp(38, 2);
  2630. } else {
  2631. this.finishOp(26, 1);
  2632. }
  2633. }
  2634. readToken_plus_min(code) {
  2635. const next = this.input.charCodeAt(this.state.pos + 1);
  2636. if (next === code) {
  2637. this.finishOp(34, 2);
  2638. return;
  2639. }
  2640. if (next === 61) {
  2641. this.finishOp(30, 2);
  2642. } else {
  2643. this.finishOp(53, 1);
  2644. }
  2645. }
  2646. readToken_lt() {
  2647. const {
  2648. pos
  2649. } = this.state;
  2650. const next = this.input.charCodeAt(pos + 1);
  2651. if (next === 60) {
  2652. if (this.input.charCodeAt(pos + 2) === 61) {
  2653. this.finishOp(30, 3);
  2654. return;
  2655. }
  2656. this.finishOp(51, 2);
  2657. return;
  2658. }
  2659. if (next === 61) {
  2660. this.finishOp(49, 2);
  2661. return;
  2662. }
  2663. this.finishOp(47, 1);
  2664. }
  2665. readToken_gt() {
  2666. const {
  2667. pos
  2668. } = this.state;
  2669. const next = this.input.charCodeAt(pos + 1);
  2670. if (next === 62) {
  2671. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  2672. if (this.input.charCodeAt(pos + size) === 61) {
  2673. this.finishOp(30, size + 1);
  2674. return;
  2675. }
  2676. this.finishOp(52, size);
  2677. return;
  2678. }
  2679. if (next === 61) {
  2680. this.finishOp(49, 2);
  2681. return;
  2682. }
  2683. this.finishOp(48, 1);
  2684. }
  2685. readToken_eq_excl(code) {
  2686. const next = this.input.charCodeAt(this.state.pos + 1);
  2687. if (next === 61) {
  2688. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  2689. return;
  2690. }
  2691. if (code === 61 && next === 62) {
  2692. this.state.pos += 2;
  2693. this.finishToken(19);
  2694. return;
  2695. }
  2696. this.finishOp(code === 61 ? 29 : 35, 1);
  2697. }
  2698. readToken_question() {
  2699. const next = this.input.charCodeAt(this.state.pos + 1);
  2700. const next2 = this.input.charCodeAt(this.state.pos + 2);
  2701. if (next === 63) {
  2702. if (next2 === 61) {
  2703. this.finishOp(30, 3);
  2704. } else {
  2705. this.finishOp(40, 2);
  2706. }
  2707. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  2708. this.state.pos += 2;
  2709. this.finishToken(18);
  2710. } else {
  2711. ++this.state.pos;
  2712. this.finishToken(17);
  2713. }
  2714. }
  2715. getTokenFromCode(code) {
  2716. switch (code) {
  2717. case 46:
  2718. this.readToken_dot();
  2719. return;
  2720. case 40:
  2721. ++this.state.pos;
  2722. this.finishToken(10);
  2723. return;
  2724. case 41:
  2725. ++this.state.pos;
  2726. this.finishToken(11);
  2727. return;
  2728. case 59:
  2729. ++this.state.pos;
  2730. this.finishToken(13);
  2731. return;
  2732. case 44:
  2733. ++this.state.pos;
  2734. this.finishToken(12);
  2735. return;
  2736. case 91:
  2737. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2738. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2739. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, {
  2740. at: this.state.curPosition()
  2741. });
  2742. }
  2743. this.state.pos += 2;
  2744. this.finishToken(2);
  2745. } else {
  2746. ++this.state.pos;
  2747. this.finishToken(0);
  2748. }
  2749. return;
  2750. case 93:
  2751. ++this.state.pos;
  2752. this.finishToken(3);
  2753. return;
  2754. case 123:
  2755. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  2756. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  2757. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, {
  2758. at: this.state.curPosition()
  2759. });
  2760. }
  2761. this.state.pos += 2;
  2762. this.finishToken(6);
  2763. } else {
  2764. ++this.state.pos;
  2765. this.finishToken(5);
  2766. }
  2767. return;
  2768. case 125:
  2769. ++this.state.pos;
  2770. this.finishToken(8);
  2771. return;
  2772. case 58:
  2773. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  2774. this.finishOp(15, 2);
  2775. } else {
  2776. ++this.state.pos;
  2777. this.finishToken(14);
  2778. }
  2779. return;
  2780. case 63:
  2781. this.readToken_question();
  2782. return;
  2783. case 96:
  2784. this.readTemplateToken();
  2785. return;
  2786. case 48:
  2787. {
  2788. const next = this.input.charCodeAt(this.state.pos + 1);
  2789. if (next === 120 || next === 88) {
  2790. this.readRadixNumber(16);
  2791. return;
  2792. }
  2793. if (next === 111 || next === 79) {
  2794. this.readRadixNumber(8);
  2795. return;
  2796. }
  2797. if (next === 98 || next === 66) {
  2798. this.readRadixNumber(2);
  2799. return;
  2800. }
  2801. }
  2802. case 49:
  2803. case 50:
  2804. case 51:
  2805. case 52:
  2806. case 53:
  2807. case 54:
  2808. case 55:
  2809. case 56:
  2810. case 57:
  2811. this.readNumber(false);
  2812. return;
  2813. case 34:
  2814. case 39:
  2815. this.readString(code);
  2816. return;
  2817. case 47:
  2818. this.readToken_slash();
  2819. return;
  2820. case 37:
  2821. case 42:
  2822. this.readToken_mult_modulo(code);
  2823. return;
  2824. case 124:
  2825. case 38:
  2826. this.readToken_pipe_amp(code);
  2827. return;
  2828. case 94:
  2829. this.readToken_caret();
  2830. return;
  2831. case 43:
  2832. case 45:
  2833. this.readToken_plus_min(code);
  2834. return;
  2835. case 60:
  2836. this.readToken_lt();
  2837. return;
  2838. case 62:
  2839. this.readToken_gt();
  2840. return;
  2841. case 61:
  2842. case 33:
  2843. this.readToken_eq_excl(code);
  2844. return;
  2845. case 126:
  2846. this.finishOp(36, 1);
  2847. return;
  2848. case 64:
  2849. this.readToken_atSign();
  2850. return;
  2851. case 35:
  2852. this.readToken_numberSign();
  2853. return;
  2854. case 92:
  2855. this.readWord();
  2856. return;
  2857. default:
  2858. if (isIdentifierStart(code)) {
  2859. this.readWord(code);
  2860. return;
  2861. }
  2862. }
  2863. throw this.raise(Errors.InvalidOrUnexpectedToken, {
  2864. at: this.state.curPosition(),
  2865. unexpected: String.fromCodePoint(code)
  2866. });
  2867. }
  2868. finishOp(type, size) {
  2869. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2870. this.state.pos += size;
  2871. this.finishToken(type, str);
  2872. }
  2873. readRegexp() {
  2874. const startLoc = this.state.startLoc;
  2875. const start = this.state.start + 1;
  2876. let escaped, inClass;
  2877. let {
  2878. pos
  2879. } = this.state;
  2880. for (;; ++pos) {
  2881. if (pos >= this.length) {
  2882. throw this.raise(Errors.UnterminatedRegExp, {
  2883. at: createPositionWithColumnOffset(startLoc, 1)
  2884. });
  2885. }
  2886. const ch = this.input.charCodeAt(pos);
  2887. if (isNewLine(ch)) {
  2888. throw this.raise(Errors.UnterminatedRegExp, {
  2889. at: createPositionWithColumnOffset(startLoc, 1)
  2890. });
  2891. }
  2892. if (escaped) {
  2893. escaped = false;
  2894. } else {
  2895. if (ch === 91) {
  2896. inClass = true;
  2897. } else if (ch === 93 && inClass) {
  2898. inClass = false;
  2899. } else if (ch === 47 && !inClass) {
  2900. break;
  2901. }
  2902. escaped = ch === 92;
  2903. }
  2904. }
  2905. const content = this.input.slice(start, pos);
  2906. ++pos;
  2907. let mods = "";
  2908. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  2909. while (pos < this.length) {
  2910. const cp = this.codePointAtPos(pos);
  2911. const char = String.fromCharCode(cp);
  2912. if (VALID_REGEX_FLAGS.has(cp)) {
  2913. if (cp === 118) {
  2914. this.expectPlugin("regexpUnicodeSets", nextPos());
  2915. if (mods.includes("u")) {
  2916. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2917. at: nextPos()
  2918. });
  2919. }
  2920. } else if (cp === 117) {
  2921. if (mods.includes("v")) {
  2922. this.raise(Errors.IncompatibleRegExpUVFlags, {
  2923. at: nextPos()
  2924. });
  2925. }
  2926. }
  2927. if (mods.includes(char)) {
  2928. this.raise(Errors.DuplicateRegExpFlags, {
  2929. at: nextPos()
  2930. });
  2931. }
  2932. } else if (isIdentifierChar(cp) || cp === 92) {
  2933. this.raise(Errors.MalformedRegExpFlags, {
  2934. at: nextPos()
  2935. });
  2936. } else {
  2937. break;
  2938. }
  2939. ++pos;
  2940. mods += char;
  2941. }
  2942. this.state.pos = pos;
  2943. this.finishToken(133, {
  2944. pattern: content,
  2945. flags: mods
  2946. });
  2947. }
  2948. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  2949. const {
  2950. n,
  2951. pos
  2952. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  2953. this.state.pos = pos;
  2954. return n;
  2955. }
  2956. readRadixNumber(radix) {
  2957. const startLoc = this.state.curPosition();
  2958. let isBigInt = false;
  2959. this.state.pos += 2;
  2960. const val = this.readInt(radix);
  2961. if (val == null) {
  2962. this.raise(Errors.InvalidDigit, {
  2963. at: createPositionWithColumnOffset(startLoc, 2),
  2964. radix
  2965. });
  2966. }
  2967. const next = this.input.charCodeAt(this.state.pos);
  2968. if (next === 110) {
  2969. ++this.state.pos;
  2970. isBigInt = true;
  2971. } else if (next === 109) {
  2972. throw this.raise(Errors.InvalidDecimal, {
  2973. at: startLoc
  2974. });
  2975. }
  2976. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2977. throw this.raise(Errors.NumberIdentifier, {
  2978. at: this.state.curPosition()
  2979. });
  2980. }
  2981. if (isBigInt) {
  2982. const str = this.input.slice(startLoc.index, this.state.pos).replace(/[_n]/g, "");
  2983. this.finishToken(131, str);
  2984. return;
  2985. }
  2986. this.finishToken(130, val);
  2987. }
  2988. readNumber(startsWithDot) {
  2989. const start = this.state.pos;
  2990. const startLoc = this.state.curPosition();
  2991. let isFloat = false;
  2992. let isBigInt = false;
  2993. let isDecimal = false;
  2994. let hasExponent = false;
  2995. let isOctal = false;
  2996. if (!startsWithDot && this.readInt(10) === null) {
  2997. this.raise(Errors.InvalidNumber, {
  2998. at: this.state.curPosition()
  2999. });
  3000. }
  3001. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  3002. if (hasLeadingZero) {
  3003. const integer = this.input.slice(start, this.state.pos);
  3004. this.recordStrictModeErrors(Errors.StrictOctalLiteral, {
  3005. at: startLoc
  3006. });
  3007. if (!this.state.strict) {
  3008. const underscorePos = integer.indexOf("_");
  3009. if (underscorePos > 0) {
  3010. this.raise(Errors.ZeroDigitNumericSeparator, {
  3011. at: createPositionWithColumnOffset(startLoc, underscorePos)
  3012. });
  3013. }
  3014. }
  3015. isOctal = hasLeadingZero && !/[89]/.test(integer);
  3016. }
  3017. let next = this.input.charCodeAt(this.state.pos);
  3018. if (next === 46 && !isOctal) {
  3019. ++this.state.pos;
  3020. this.readInt(10);
  3021. isFloat = true;
  3022. next = this.input.charCodeAt(this.state.pos);
  3023. }
  3024. if ((next === 69 || next === 101) && !isOctal) {
  3025. next = this.input.charCodeAt(++this.state.pos);
  3026. if (next === 43 || next === 45) {
  3027. ++this.state.pos;
  3028. }
  3029. if (this.readInt(10) === null) {
  3030. this.raise(Errors.InvalidOrMissingExponent, {
  3031. at: startLoc
  3032. });
  3033. }
  3034. isFloat = true;
  3035. hasExponent = true;
  3036. next = this.input.charCodeAt(this.state.pos);
  3037. }
  3038. if (next === 110) {
  3039. if (isFloat || hasLeadingZero) {
  3040. this.raise(Errors.InvalidBigIntLiteral, {
  3041. at: startLoc
  3042. });
  3043. }
  3044. ++this.state.pos;
  3045. isBigInt = true;
  3046. }
  3047. if (next === 109) {
  3048. this.expectPlugin("decimal", this.state.curPosition());
  3049. if (hasExponent || hasLeadingZero) {
  3050. this.raise(Errors.InvalidDecimal, {
  3051. at: startLoc
  3052. });
  3053. }
  3054. ++this.state.pos;
  3055. isDecimal = true;
  3056. }
  3057. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  3058. throw this.raise(Errors.NumberIdentifier, {
  3059. at: this.state.curPosition()
  3060. });
  3061. }
  3062. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  3063. if (isBigInt) {
  3064. this.finishToken(131, str);
  3065. return;
  3066. }
  3067. if (isDecimal) {
  3068. this.finishToken(132, str);
  3069. return;
  3070. }
  3071. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  3072. this.finishToken(130, val);
  3073. }
  3074. readCodePoint(throwOnInvalid) {
  3075. const {
  3076. code,
  3077. pos
  3078. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  3079. this.state.pos = pos;
  3080. return code;
  3081. }
  3082. readString(quote) {
  3083. const {
  3084. str,
  3085. pos,
  3086. curLine,
  3087. lineStart
  3088. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  3089. this.state.pos = pos + 1;
  3090. this.state.lineStart = lineStart;
  3091. this.state.curLine = curLine;
  3092. this.finishToken(129, str);
  3093. }
  3094. readTemplateContinuation() {
  3095. if (!this.match(8)) {
  3096. this.unexpected(null, 8);
  3097. }
  3098. this.state.pos--;
  3099. this.readTemplateToken();
  3100. }
  3101. readTemplateToken() {
  3102. const opening = this.input[this.state.pos];
  3103. const {
  3104. str,
  3105. firstInvalidLoc,
  3106. pos,
  3107. curLine,
  3108. lineStart
  3109. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  3110. this.state.pos = pos + 1;
  3111. this.state.lineStart = lineStart;
  3112. this.state.curLine = curLine;
  3113. if (firstInvalidLoc) {
  3114. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, firstInvalidLoc.pos);
  3115. }
  3116. if (this.input.codePointAt(pos) === 96) {
  3117. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  3118. } else {
  3119. this.state.pos++;
  3120. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  3121. }
  3122. }
  3123. recordStrictModeErrors(toParseError, {
  3124. at
  3125. }) {
  3126. const index = at.index;
  3127. if (this.state.strict && !this.state.strictErrors.has(index)) {
  3128. this.raise(toParseError, {
  3129. at
  3130. });
  3131. } else {
  3132. this.state.strictErrors.set(index, [toParseError, at]);
  3133. }
  3134. }
  3135. readWord1(firstCode) {
  3136. this.state.containsEsc = false;
  3137. let word = "";
  3138. const start = this.state.pos;
  3139. let chunkStart = this.state.pos;
  3140. if (firstCode !== undefined) {
  3141. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  3142. }
  3143. while (this.state.pos < this.length) {
  3144. const ch = this.codePointAtPos(this.state.pos);
  3145. if (isIdentifierChar(ch)) {
  3146. this.state.pos += ch <= 0xffff ? 1 : 2;
  3147. } else if (ch === 92) {
  3148. this.state.containsEsc = true;
  3149. word += this.input.slice(chunkStart, this.state.pos);
  3150. const escStart = this.state.curPosition();
  3151. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  3152. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  3153. this.raise(Errors.MissingUnicodeEscape, {
  3154. at: this.state.curPosition()
  3155. });
  3156. chunkStart = this.state.pos - 1;
  3157. continue;
  3158. }
  3159. ++this.state.pos;
  3160. const esc = this.readCodePoint(true);
  3161. if (esc !== null) {
  3162. if (!identifierCheck(esc)) {
  3163. this.raise(Errors.EscapedCharNotAnIdentifier, {
  3164. at: escStart
  3165. });
  3166. }
  3167. word += String.fromCodePoint(esc);
  3168. }
  3169. chunkStart = this.state.pos;
  3170. } else {
  3171. break;
  3172. }
  3173. }
  3174. return word + this.input.slice(chunkStart, this.state.pos);
  3175. }
  3176. readWord(firstCode) {
  3177. const word = this.readWord1(firstCode);
  3178. const type = keywords$1.get(word);
  3179. if (type !== undefined) {
  3180. this.finishToken(type, tokenLabelName(type));
  3181. } else {
  3182. this.finishToken(128, word);
  3183. }
  3184. }
  3185. checkKeywordEscapes() {
  3186. const {
  3187. type
  3188. } = this.state;
  3189. if (tokenIsKeyword(type) && this.state.containsEsc) {
  3190. this.raise(Errors.InvalidEscapedReservedWord, {
  3191. at: this.state.startLoc,
  3192. reservedWord: tokenLabelName(type)
  3193. });
  3194. }
  3195. }
  3196. raise(toParseError, raiseProperties) {
  3197. const {
  3198. at
  3199. } = raiseProperties,
  3200. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded);
  3201. const loc = at instanceof Position ? at : at.loc.start;
  3202. const error = toParseError({
  3203. loc,
  3204. details
  3205. });
  3206. if (!this.options.errorRecovery) throw error;
  3207. if (!this.isLookahead) this.state.errors.push(error);
  3208. return error;
  3209. }
  3210. raiseOverwrite(toParseError, raiseProperties) {
  3211. const {
  3212. at
  3213. } = raiseProperties,
  3214. details = _objectWithoutPropertiesLoose(raiseProperties, _excluded2);
  3215. const loc = at instanceof Position ? at : at.loc.start;
  3216. const pos = loc.index;
  3217. const errors = this.state.errors;
  3218. for (let i = errors.length - 1; i >= 0; i--) {
  3219. const error = errors[i];
  3220. if (error.loc.index === pos) {
  3221. return errors[i] = toParseError({
  3222. loc,
  3223. details
  3224. });
  3225. }
  3226. if (error.loc.index < pos) break;
  3227. }
  3228. return this.raise(toParseError, raiseProperties);
  3229. }
  3230. updateContext(prevType) {}
  3231. unexpected(loc, type) {
  3232. throw this.raise(Errors.UnexpectedToken, {
  3233. expected: type ? tokenLabelName(type) : null,
  3234. at: loc != null ? loc : this.state.startLoc
  3235. });
  3236. }
  3237. expectPlugin(pluginName, loc) {
  3238. if (this.hasPlugin(pluginName)) {
  3239. return true;
  3240. }
  3241. throw this.raise(Errors.MissingPlugin, {
  3242. at: loc != null ? loc : this.state.startLoc,
  3243. missingPlugin: [pluginName]
  3244. });
  3245. }
  3246. expectOnePlugin(pluginNames) {
  3247. if (!pluginNames.some(name => this.hasPlugin(name))) {
  3248. throw this.raise(Errors.MissingOneOfPlugins, {
  3249. at: this.state.startLoc,
  3250. missingPlugin: pluginNames
  3251. });
  3252. }
  3253. }
  3254. errorBuilder(error) {
  3255. return (pos, lineStart, curLine) => {
  3256. this.raise(error, {
  3257. at: buildPosition(pos, lineStart, curLine)
  3258. });
  3259. };
  3260. }
  3261. }
  3262. class ClassScope {
  3263. constructor() {
  3264. this.privateNames = new Set();
  3265. this.loneAccessors = new Map();
  3266. this.undefinedPrivateNames = new Map();
  3267. }
  3268. }
  3269. class ClassScopeHandler {
  3270. constructor(parser) {
  3271. this.parser = void 0;
  3272. this.stack = [];
  3273. this.undefinedPrivateNames = new Map();
  3274. this.parser = parser;
  3275. }
  3276. current() {
  3277. return this.stack[this.stack.length - 1];
  3278. }
  3279. enter() {
  3280. this.stack.push(new ClassScope());
  3281. }
  3282. exit() {
  3283. const oldClassScope = this.stack.pop();
  3284. const current = this.current();
  3285. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  3286. if (current) {
  3287. if (!current.undefinedPrivateNames.has(name)) {
  3288. current.undefinedPrivateNames.set(name, loc);
  3289. }
  3290. } else {
  3291. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3292. at: loc,
  3293. identifierName: name
  3294. });
  3295. }
  3296. }
  3297. }
  3298. declarePrivateName(name, elementType, loc) {
  3299. const {
  3300. privateNames,
  3301. loneAccessors,
  3302. undefinedPrivateNames
  3303. } = this.current();
  3304. let redefined = privateNames.has(name);
  3305. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  3306. const accessor = redefined && loneAccessors.get(name);
  3307. if (accessor) {
  3308. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  3309. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  3310. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  3311. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  3312. redefined = oldKind === newKind || oldStatic !== newStatic;
  3313. if (!redefined) loneAccessors.delete(name);
  3314. } else if (!redefined) {
  3315. loneAccessors.set(name, elementType);
  3316. }
  3317. }
  3318. if (redefined) {
  3319. this.parser.raise(Errors.PrivateNameRedeclaration, {
  3320. at: loc,
  3321. identifierName: name
  3322. });
  3323. }
  3324. privateNames.add(name);
  3325. undefinedPrivateNames.delete(name);
  3326. }
  3327. usePrivateName(name, loc) {
  3328. let classScope;
  3329. for (classScope of this.stack) {
  3330. if (classScope.privateNames.has(name)) return;
  3331. }
  3332. if (classScope) {
  3333. classScope.undefinedPrivateNames.set(name, loc);
  3334. } else {
  3335. this.parser.raise(Errors.InvalidPrivateFieldResolution, {
  3336. at: loc,
  3337. identifierName: name
  3338. });
  3339. }
  3340. }
  3341. }
  3342. const kExpression = 0,
  3343. kMaybeArrowParameterDeclaration = 1,
  3344. kMaybeAsyncArrowParameterDeclaration = 2,
  3345. kParameterDeclaration = 3;
  3346. class ExpressionScope {
  3347. constructor(type = kExpression) {
  3348. this.type = void 0;
  3349. this.type = type;
  3350. }
  3351. canBeArrowParameterDeclaration() {
  3352. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  3353. }
  3354. isCertainlyParameterDeclaration() {
  3355. return this.type === kParameterDeclaration;
  3356. }
  3357. }
  3358. class ArrowHeadParsingScope extends ExpressionScope {
  3359. constructor(type) {
  3360. super(type);
  3361. this.declarationErrors = new Map();
  3362. }
  3363. recordDeclarationError(ParsingErrorClass, {
  3364. at
  3365. }) {
  3366. const index = at.index;
  3367. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  3368. }
  3369. clearDeclarationError(index) {
  3370. this.declarationErrors.delete(index);
  3371. }
  3372. iterateErrors(iterator) {
  3373. this.declarationErrors.forEach(iterator);
  3374. }
  3375. }
  3376. class ExpressionScopeHandler {
  3377. constructor(parser) {
  3378. this.parser = void 0;
  3379. this.stack = [new ExpressionScope()];
  3380. this.parser = parser;
  3381. }
  3382. enter(scope) {
  3383. this.stack.push(scope);
  3384. }
  3385. exit() {
  3386. this.stack.pop();
  3387. }
  3388. recordParameterInitializerError(toParseError, {
  3389. at: node
  3390. }) {
  3391. const origin = {
  3392. at: node.loc.start
  3393. };
  3394. const {
  3395. stack
  3396. } = this;
  3397. let i = stack.length - 1;
  3398. let scope = stack[i];
  3399. while (!scope.isCertainlyParameterDeclaration()) {
  3400. if (scope.canBeArrowParameterDeclaration()) {
  3401. scope.recordDeclarationError(toParseError, origin);
  3402. } else {
  3403. return;
  3404. }
  3405. scope = stack[--i];
  3406. }
  3407. this.parser.raise(toParseError, origin);
  3408. }
  3409. recordArrowParemeterBindingError(error, {
  3410. at: node
  3411. }) {
  3412. const {
  3413. stack
  3414. } = this;
  3415. const scope = stack[stack.length - 1];
  3416. const origin = {
  3417. at: node.loc.start
  3418. };
  3419. if (scope.isCertainlyParameterDeclaration()) {
  3420. this.parser.raise(error, origin);
  3421. } else if (scope.canBeArrowParameterDeclaration()) {
  3422. scope.recordDeclarationError(error, origin);
  3423. } else {
  3424. return;
  3425. }
  3426. }
  3427. recordAsyncArrowParametersError({
  3428. at
  3429. }) {
  3430. const {
  3431. stack
  3432. } = this;
  3433. let i = stack.length - 1;
  3434. let scope = stack[i];
  3435. while (scope.canBeArrowParameterDeclaration()) {
  3436. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  3437. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, {
  3438. at
  3439. });
  3440. }
  3441. scope = stack[--i];
  3442. }
  3443. }
  3444. validateAsPattern() {
  3445. const {
  3446. stack
  3447. } = this;
  3448. const currentScope = stack[stack.length - 1];
  3449. if (!currentScope.canBeArrowParameterDeclaration()) return;
  3450. currentScope.iterateErrors(([toParseError, loc]) => {
  3451. this.parser.raise(toParseError, {
  3452. at: loc
  3453. });
  3454. let i = stack.length - 2;
  3455. let scope = stack[i];
  3456. while (scope.canBeArrowParameterDeclaration()) {
  3457. scope.clearDeclarationError(loc.index);
  3458. scope = stack[--i];
  3459. }
  3460. });
  3461. }
  3462. }
  3463. function newParameterDeclarationScope() {
  3464. return new ExpressionScope(kParameterDeclaration);
  3465. }
  3466. function newArrowHeadScope() {
  3467. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  3468. }
  3469. function newAsyncArrowScope() {
  3470. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  3471. }
  3472. function newExpressionScope() {
  3473. return new ExpressionScope();
  3474. }
  3475. const PARAM = 0b0000,
  3476. PARAM_YIELD = 0b0001,
  3477. PARAM_AWAIT = 0b0010,
  3478. PARAM_RETURN = 0b0100,
  3479. PARAM_IN = 0b1000;
  3480. class ProductionParameterHandler {
  3481. constructor() {
  3482. this.stacks = [];
  3483. }
  3484. enter(flags) {
  3485. this.stacks.push(flags);
  3486. }
  3487. exit() {
  3488. this.stacks.pop();
  3489. }
  3490. currentFlags() {
  3491. return this.stacks[this.stacks.length - 1];
  3492. }
  3493. get hasAwait() {
  3494. return (this.currentFlags() & PARAM_AWAIT) > 0;
  3495. }
  3496. get hasYield() {
  3497. return (this.currentFlags() & PARAM_YIELD) > 0;
  3498. }
  3499. get hasReturn() {
  3500. return (this.currentFlags() & PARAM_RETURN) > 0;
  3501. }
  3502. get hasIn() {
  3503. return (this.currentFlags() & PARAM_IN) > 0;
  3504. }
  3505. }
  3506. function functionFlags(isAsync, isGenerator) {
  3507. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  3508. }
  3509. class UtilParser extends Tokenizer {
  3510. addExtra(node, key, value, enumerable = true) {
  3511. if (!node) return;
  3512. const extra = node.extra = node.extra || {};
  3513. if (enumerable) {
  3514. extra[key] = value;
  3515. } else {
  3516. Object.defineProperty(extra, key, {
  3517. enumerable,
  3518. value
  3519. });
  3520. }
  3521. }
  3522. isContextual(token) {
  3523. return this.state.type === token && !this.state.containsEsc;
  3524. }
  3525. isUnparsedContextual(nameStart, name) {
  3526. const nameEnd = nameStart + name.length;
  3527. if (this.input.slice(nameStart, nameEnd) === name) {
  3528. const nextCh = this.input.charCodeAt(nameEnd);
  3529. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  3530. }
  3531. return false;
  3532. }
  3533. isLookaheadContextual(name) {
  3534. const next = this.nextTokenStart();
  3535. return this.isUnparsedContextual(next, name);
  3536. }
  3537. eatContextual(token) {
  3538. if (this.isContextual(token)) {
  3539. this.next();
  3540. return true;
  3541. }
  3542. return false;
  3543. }
  3544. expectContextual(token, toParseError) {
  3545. if (!this.eatContextual(token)) {
  3546. if (toParseError != null) {
  3547. throw this.raise(toParseError, {
  3548. at: this.state.startLoc
  3549. });
  3550. }
  3551. throw this.unexpected(null, token);
  3552. }
  3553. }
  3554. canInsertSemicolon() {
  3555. return this.match(135) || this.match(8) || this.hasPrecedingLineBreak();
  3556. }
  3557. hasPrecedingLineBreak() {
  3558. return lineBreak.test(this.input.slice(this.state.lastTokEndLoc.index, this.state.start));
  3559. }
  3560. hasFollowingLineBreak() {
  3561. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  3562. return skipWhiteSpaceToLineBreak.test(this.input);
  3563. }
  3564. isLineTerminator() {
  3565. return this.eat(13) || this.canInsertSemicolon();
  3566. }
  3567. semicolon(allowAsi = true) {
  3568. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  3569. this.raise(Errors.MissingSemicolon, {
  3570. at: this.state.lastTokEndLoc
  3571. });
  3572. }
  3573. expect(type, loc) {
  3574. this.eat(type) || this.unexpected(loc, type);
  3575. }
  3576. tryParse(fn, oldState = this.state.clone()) {
  3577. const abortSignal = {
  3578. node: null
  3579. };
  3580. try {
  3581. const node = fn((node = null) => {
  3582. abortSignal.node = node;
  3583. throw abortSignal;
  3584. });
  3585. if (this.state.errors.length > oldState.errors.length) {
  3586. const failState = this.state;
  3587. this.state = oldState;
  3588. this.state.tokensLength = failState.tokensLength;
  3589. return {
  3590. node,
  3591. error: failState.errors[oldState.errors.length],
  3592. thrown: false,
  3593. aborted: false,
  3594. failState
  3595. };
  3596. }
  3597. return {
  3598. node,
  3599. error: null,
  3600. thrown: false,
  3601. aborted: false,
  3602. failState: null
  3603. };
  3604. } catch (error) {
  3605. const failState = this.state;
  3606. this.state = oldState;
  3607. if (error instanceof SyntaxError) {
  3608. return {
  3609. node: null,
  3610. error,
  3611. thrown: true,
  3612. aborted: false,
  3613. failState
  3614. };
  3615. }
  3616. if (error === abortSignal) {
  3617. return {
  3618. node: abortSignal.node,
  3619. error: null,
  3620. thrown: false,
  3621. aborted: true,
  3622. failState
  3623. };
  3624. }
  3625. throw error;
  3626. }
  3627. }
  3628. checkExpressionErrors(refExpressionErrors, andThrow) {
  3629. if (!refExpressionErrors) return false;
  3630. const {
  3631. shorthandAssignLoc,
  3632. doubleProtoLoc,
  3633. privateKeyLoc,
  3634. optionalParametersLoc
  3635. } = refExpressionErrors;
  3636. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc;
  3637. if (!andThrow) {
  3638. return hasErrors;
  3639. }
  3640. if (shorthandAssignLoc != null) {
  3641. this.raise(Errors.InvalidCoverInitializedName, {
  3642. at: shorthandAssignLoc
  3643. });
  3644. }
  3645. if (doubleProtoLoc != null) {
  3646. this.raise(Errors.DuplicateProto, {
  3647. at: doubleProtoLoc
  3648. });
  3649. }
  3650. if (privateKeyLoc != null) {
  3651. this.raise(Errors.UnexpectedPrivateField, {
  3652. at: privateKeyLoc
  3653. });
  3654. }
  3655. if (optionalParametersLoc != null) {
  3656. this.unexpected(optionalParametersLoc);
  3657. }
  3658. }
  3659. isLiteralPropertyName() {
  3660. return tokenIsLiteralPropertyName(this.state.type);
  3661. }
  3662. isPrivateName(node) {
  3663. return node.type === "PrivateName";
  3664. }
  3665. getPrivateNameSV(node) {
  3666. return node.id.name;
  3667. }
  3668. hasPropertyAsPrivateName(node) {
  3669. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  3670. }
  3671. isOptionalChain(node) {
  3672. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  3673. }
  3674. isObjectProperty(node) {
  3675. return node.type === "ObjectProperty";
  3676. }
  3677. isObjectMethod(node) {
  3678. return node.type === "ObjectMethod";
  3679. }
  3680. initializeScopes(inModule = this.options.sourceType === "module") {
  3681. const oldLabels = this.state.labels;
  3682. this.state.labels = [];
  3683. const oldExportedIdentifiers = this.exportedIdentifiers;
  3684. this.exportedIdentifiers = new Set();
  3685. const oldInModule = this.inModule;
  3686. this.inModule = inModule;
  3687. const oldScope = this.scope;
  3688. const ScopeHandler = this.getScopeHandler();
  3689. this.scope = new ScopeHandler(this, inModule);
  3690. const oldProdParam = this.prodParam;
  3691. this.prodParam = new ProductionParameterHandler();
  3692. const oldClassScope = this.classScope;
  3693. this.classScope = new ClassScopeHandler(this);
  3694. const oldExpressionScope = this.expressionScope;
  3695. this.expressionScope = new ExpressionScopeHandler(this);
  3696. return () => {
  3697. this.state.labels = oldLabels;
  3698. this.exportedIdentifiers = oldExportedIdentifiers;
  3699. this.inModule = oldInModule;
  3700. this.scope = oldScope;
  3701. this.prodParam = oldProdParam;
  3702. this.classScope = oldClassScope;
  3703. this.expressionScope = oldExpressionScope;
  3704. };
  3705. }
  3706. enterInitialScopes() {
  3707. let paramFlags = PARAM;
  3708. if (this.inModule) {
  3709. paramFlags |= PARAM_AWAIT;
  3710. }
  3711. this.scope.enter(SCOPE_PROGRAM);
  3712. this.prodParam.enter(paramFlags);
  3713. }
  3714. checkDestructuringPrivate(refExpressionErrors) {
  3715. const {
  3716. privateKeyLoc
  3717. } = refExpressionErrors;
  3718. if (privateKeyLoc !== null) {
  3719. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  3720. }
  3721. }
  3722. }
  3723. class ExpressionErrors {
  3724. constructor() {
  3725. this.shorthandAssignLoc = null;
  3726. this.doubleProtoLoc = null;
  3727. this.privateKeyLoc = null;
  3728. this.optionalParametersLoc = null;
  3729. }
  3730. }
  3731. class Node {
  3732. constructor(parser, pos, loc) {
  3733. this.type = "";
  3734. this.start = pos;
  3735. this.end = 0;
  3736. this.loc = new SourceLocation(loc);
  3737. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  3738. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  3739. }
  3740. }
  3741. const NodePrototype = Node.prototype;
  3742. {
  3743. NodePrototype.__clone = function () {
  3744. const newNode = new Node(undefined, this.start, this.loc.start);
  3745. const keys = Object.keys(this);
  3746. for (let i = 0, length = keys.length; i < length; i++) {
  3747. const key = keys[i];
  3748. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  3749. newNode[key] = this[key];
  3750. }
  3751. }
  3752. return newNode;
  3753. };
  3754. }
  3755. function clonePlaceholder(node) {
  3756. return cloneIdentifier(node);
  3757. }
  3758. function cloneIdentifier(node) {
  3759. const {
  3760. type,
  3761. start,
  3762. end,
  3763. loc,
  3764. range,
  3765. extra,
  3766. name
  3767. } = node;
  3768. const cloned = Object.create(NodePrototype);
  3769. cloned.type = type;
  3770. cloned.start = start;
  3771. cloned.end = end;
  3772. cloned.loc = loc;
  3773. cloned.range = range;
  3774. cloned.extra = extra;
  3775. cloned.name = name;
  3776. if (type === "Placeholder") {
  3777. cloned.expectedNode = node.expectedNode;
  3778. }
  3779. return cloned;
  3780. }
  3781. function cloneStringLiteral(node) {
  3782. const {
  3783. type,
  3784. start,
  3785. end,
  3786. loc,
  3787. range,
  3788. extra
  3789. } = node;
  3790. if (type === "Placeholder") {
  3791. return clonePlaceholder(node);
  3792. }
  3793. const cloned = Object.create(NodePrototype);
  3794. cloned.type = type;
  3795. cloned.start = start;
  3796. cloned.end = end;
  3797. cloned.loc = loc;
  3798. cloned.range = range;
  3799. if (node.raw !== undefined) {
  3800. cloned.raw = node.raw;
  3801. } else {
  3802. cloned.extra = extra;
  3803. }
  3804. cloned.value = node.value;
  3805. return cloned;
  3806. }
  3807. class NodeUtils extends UtilParser {
  3808. startNode() {
  3809. return new Node(this, this.state.start, this.state.startLoc);
  3810. }
  3811. startNodeAt(pos, loc) {
  3812. return new Node(this, pos, loc);
  3813. }
  3814. startNodeAtNode(type) {
  3815. return this.startNodeAt(type.start, type.loc.start);
  3816. }
  3817. finishNode(node, type) {
  3818. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  3819. }
  3820. finishNodeAt(node, type, endLoc) {
  3821. node.type = type;
  3822. node.end = endLoc.index;
  3823. node.loc.end = endLoc;
  3824. if (this.options.ranges) node.range[1] = endLoc.index;
  3825. if (this.options.attachComment) this.processComment(node);
  3826. return node;
  3827. }
  3828. resetStartLocation(node, start, startLoc) {
  3829. node.start = start;
  3830. node.loc.start = startLoc;
  3831. if (this.options.ranges) node.range[0] = start;
  3832. }
  3833. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  3834. node.end = endLoc.index;
  3835. node.loc.end = endLoc;
  3836. if (this.options.ranges) node.range[1] = endLoc.index;
  3837. }
  3838. resetStartLocationFromNode(node, locationNode) {
  3839. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  3840. }
  3841. }
  3842. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3843. const FlowErrors = ParseErrorEnum`flow`({
  3844. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3845. 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.",
  3846. AssignReservedType: ({
  3847. reservedType
  3848. }) => `Cannot overwrite reserved type ${reservedType}.`,
  3849. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3850. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3851. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3852. EnumBooleanMemberNotInitialized: ({
  3853. memberName,
  3854. enumName
  3855. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  3856. EnumDuplicateMemberName: ({
  3857. memberName,
  3858. enumName
  3859. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  3860. EnumInconsistentMemberValues: ({
  3861. enumName
  3862. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  3863. EnumInvalidExplicitType: ({
  3864. invalidEnumType,
  3865. enumName
  3866. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3867. EnumInvalidExplicitTypeUnknownSupplied: ({
  3868. enumName
  3869. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  3870. EnumInvalidMemberInitializerPrimaryType: ({
  3871. enumName,
  3872. memberName,
  3873. explicitType
  3874. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  3875. EnumInvalidMemberInitializerSymbolType: ({
  3876. enumName,
  3877. memberName
  3878. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  3879. EnumInvalidMemberInitializerUnknownType: ({
  3880. enumName,
  3881. memberName
  3882. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  3883. EnumInvalidMemberName: ({
  3884. enumName,
  3885. memberName,
  3886. suggestion
  3887. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  3888. EnumNumberMemberNotInitialized: ({
  3889. enumName,
  3890. memberName
  3891. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  3892. EnumStringMemberInconsistentlyInitailized: ({
  3893. enumName
  3894. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  3895. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3896. 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.",
  3897. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3898. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3899. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3900. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3901. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3902. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3903. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3904. PatternIsOptional: Object.assign({
  3905. message: "A binding pattern parameter cannot be optional in an implementation signature."
  3906. }, {
  3907. reasonCode: "OptionalBindingPattern"
  3908. }),
  3909. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3910. SpreadVariance: "Spread properties cannot have variance.",
  3911. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3912. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3913. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3914. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3915. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3916. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3917. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3918. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3919. UnexpectedReservedType: ({
  3920. reservedType
  3921. }) => `Unexpected reserved type ${reservedType}.`,
  3922. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3923. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3924. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3925. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3926. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3927. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3928. UnsupportedDeclareExportKind: ({
  3929. unsupportedExportKind,
  3930. suggestion
  3931. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  3932. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3933. UnterminatedFlowComment: "Unterminated flow-comment."
  3934. });
  3935. function isEsModuleType(bodyElement) {
  3936. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3937. }
  3938. function hasTypeImportKind(node) {
  3939. return node.importKind === "type" || node.importKind === "typeof";
  3940. }
  3941. function isMaybeDefaultImport(type) {
  3942. return tokenIsKeywordOrIdentifier(type) && type !== 97;
  3943. }
  3944. const exportSuggestions = {
  3945. const: "declare export var",
  3946. let: "declare export var",
  3947. type: "export type",
  3948. interface: "export interface"
  3949. };
  3950. function partition(list, test) {
  3951. const list1 = [];
  3952. const list2 = [];
  3953. for (let i = 0; i < list.length; i++) {
  3954. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3955. }
  3956. return [list1, list2];
  3957. }
  3958. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3959. var flow = (superClass => class FlowParserMixin extends superClass {
  3960. constructor(...args) {
  3961. super(...args);
  3962. this.flowPragma = undefined;
  3963. }
  3964. getScopeHandler() {
  3965. return FlowScopeHandler;
  3966. }
  3967. shouldParseTypes() {
  3968. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3969. }
  3970. shouldParseEnums() {
  3971. return !!this.getPluginOption("flow", "enums");
  3972. }
  3973. finishToken(type, val) {
  3974. if (type !== 129 && type !== 13 && type !== 28) {
  3975. if (this.flowPragma === undefined) {
  3976. this.flowPragma = null;
  3977. }
  3978. }
  3979. return super.finishToken(type, val);
  3980. }
  3981. addComment(comment) {
  3982. if (this.flowPragma === undefined) {
  3983. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3984. if (!matches) ; else if (matches[1] === "flow") {
  3985. this.flowPragma = "flow";
  3986. } else if (matches[1] === "noflow") {
  3987. this.flowPragma = "noflow";
  3988. } else {
  3989. throw new Error("Unexpected flow pragma");
  3990. }
  3991. }
  3992. return super.addComment(comment);
  3993. }
  3994. flowParseTypeInitialiser(tok) {
  3995. const oldInType = this.state.inType;
  3996. this.state.inType = true;
  3997. this.expect(tok || 14);
  3998. const type = this.flowParseType();
  3999. this.state.inType = oldInType;
  4000. return type;
  4001. }
  4002. flowParsePredicate() {
  4003. const node = this.startNode();
  4004. const moduloLoc = this.state.startLoc;
  4005. this.next();
  4006. this.expectContextual(107);
  4007. if (this.state.lastTokStart > moduloLoc.index + 1) {
  4008. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, {
  4009. at: moduloLoc
  4010. });
  4011. }
  4012. if (this.eat(10)) {
  4013. node.value = super.parseExpression();
  4014. this.expect(11);
  4015. return this.finishNode(node, "DeclaredPredicate");
  4016. } else {
  4017. return this.finishNode(node, "InferredPredicate");
  4018. }
  4019. }
  4020. flowParseTypeAndPredicateInitialiser() {
  4021. const oldInType = this.state.inType;
  4022. this.state.inType = true;
  4023. this.expect(14);
  4024. let type = null;
  4025. let predicate = null;
  4026. if (this.match(54)) {
  4027. this.state.inType = oldInType;
  4028. predicate = this.flowParsePredicate();
  4029. } else {
  4030. type = this.flowParseType();
  4031. this.state.inType = oldInType;
  4032. if (this.match(54)) {
  4033. predicate = this.flowParsePredicate();
  4034. }
  4035. }
  4036. return [type, predicate];
  4037. }
  4038. flowParseDeclareClass(node) {
  4039. this.next();
  4040. this.flowParseInterfaceish(node, true);
  4041. return this.finishNode(node, "DeclareClass");
  4042. }
  4043. flowParseDeclareFunction(node) {
  4044. this.next();
  4045. const id = node.id = this.parseIdentifier();
  4046. const typeNode = this.startNode();
  4047. const typeContainer = this.startNode();
  4048. if (this.match(47)) {
  4049. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  4050. } else {
  4051. typeNode.typeParameters = null;
  4052. }
  4053. this.expect(10);
  4054. const tmp = this.flowParseFunctionTypeParams();
  4055. typeNode.params = tmp.params;
  4056. typeNode.rest = tmp.rest;
  4057. typeNode.this = tmp._this;
  4058. this.expect(11);
  4059. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4060. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  4061. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  4062. this.resetEndLocation(id);
  4063. this.semicolon();
  4064. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.loc.start);
  4065. return this.finishNode(node, "DeclareFunction");
  4066. }
  4067. flowParseDeclare(node, insideModule) {
  4068. if (this.match(80)) {
  4069. return this.flowParseDeclareClass(node);
  4070. } else if (this.match(68)) {
  4071. return this.flowParseDeclareFunction(node);
  4072. } else if (this.match(74)) {
  4073. return this.flowParseDeclareVariable(node);
  4074. } else if (this.eatContextual(123)) {
  4075. if (this.match(16)) {
  4076. return this.flowParseDeclareModuleExports(node);
  4077. } else {
  4078. if (insideModule) {
  4079. this.raise(FlowErrors.NestedDeclareModule, {
  4080. at: this.state.lastTokStartLoc
  4081. });
  4082. }
  4083. return this.flowParseDeclareModule(node);
  4084. }
  4085. } else if (this.isContextual(126)) {
  4086. return this.flowParseDeclareTypeAlias(node);
  4087. } else if (this.isContextual(127)) {
  4088. return this.flowParseDeclareOpaqueType(node);
  4089. } else if (this.isContextual(125)) {
  4090. return this.flowParseDeclareInterface(node);
  4091. } else if (this.match(82)) {
  4092. return this.flowParseDeclareExportDeclaration(node, insideModule);
  4093. } else {
  4094. throw this.unexpected();
  4095. }
  4096. }
  4097. flowParseDeclareVariable(node) {
  4098. this.next();
  4099. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  4100. this.scope.declareName(node.id.name, BIND_VAR, node.id.loc.start);
  4101. this.semicolon();
  4102. return this.finishNode(node, "DeclareVariable");
  4103. }
  4104. flowParseDeclareModule(node) {
  4105. this.scope.enter(SCOPE_OTHER);
  4106. if (this.match(129)) {
  4107. node.id = super.parseExprAtom();
  4108. } else {
  4109. node.id = this.parseIdentifier();
  4110. }
  4111. const bodyNode = node.body = this.startNode();
  4112. const body = bodyNode.body = [];
  4113. this.expect(5);
  4114. while (!this.match(8)) {
  4115. let bodyNode = this.startNode();
  4116. if (this.match(83)) {
  4117. this.next();
  4118. if (!this.isContextual(126) && !this.match(87)) {
  4119. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, {
  4120. at: this.state.lastTokStartLoc
  4121. });
  4122. }
  4123. super.parseImport(bodyNode);
  4124. } else {
  4125. this.expectContextual(121, FlowErrors.UnsupportedStatementInDeclareModule);
  4126. bodyNode = this.flowParseDeclare(bodyNode, true);
  4127. }
  4128. body.push(bodyNode);
  4129. }
  4130. this.scope.exit();
  4131. this.expect(8);
  4132. this.finishNode(bodyNode, "BlockStatement");
  4133. let kind = null;
  4134. let hasModuleExport = false;
  4135. body.forEach(bodyElement => {
  4136. if (isEsModuleType(bodyElement)) {
  4137. if (kind === "CommonJS") {
  4138. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4139. at: bodyElement
  4140. });
  4141. }
  4142. kind = "ES";
  4143. } else if (bodyElement.type === "DeclareModuleExports") {
  4144. if (hasModuleExport) {
  4145. this.raise(FlowErrors.DuplicateDeclareModuleExports, {
  4146. at: bodyElement
  4147. });
  4148. }
  4149. if (kind === "ES") {
  4150. this.raise(FlowErrors.AmbiguousDeclareModuleKind, {
  4151. at: bodyElement
  4152. });
  4153. }
  4154. kind = "CommonJS";
  4155. hasModuleExport = true;
  4156. }
  4157. });
  4158. node.kind = kind || "CommonJS";
  4159. return this.finishNode(node, "DeclareModule");
  4160. }
  4161. flowParseDeclareExportDeclaration(node, insideModule) {
  4162. this.expect(82);
  4163. if (this.eat(65)) {
  4164. if (this.match(68) || this.match(80)) {
  4165. node.declaration = this.flowParseDeclare(this.startNode());
  4166. } else {
  4167. node.declaration = this.flowParseType();
  4168. this.semicolon();
  4169. }
  4170. node.default = true;
  4171. return this.finishNode(node, "DeclareExportDeclaration");
  4172. } else {
  4173. if (this.match(75) || this.isLet() || (this.isContextual(126) || this.isContextual(125)) && !insideModule) {
  4174. const label = this.state.value;
  4175. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, {
  4176. at: this.state.startLoc,
  4177. unsupportedExportKind: label,
  4178. suggestion: exportSuggestions[label]
  4179. });
  4180. }
  4181. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(127)) {
  4182. node.declaration = this.flowParseDeclare(this.startNode());
  4183. node.default = false;
  4184. return this.finishNode(node, "DeclareExportDeclaration");
  4185. } else if (this.match(55) || this.match(5) || this.isContextual(125) || this.isContextual(126) || this.isContextual(127)) {
  4186. node = this.parseExport(node);
  4187. if (node.type === "ExportNamedDeclaration") {
  4188. node.type = "ExportDeclaration";
  4189. node.default = false;
  4190. delete node.exportKind;
  4191. }
  4192. node.type = "Declare" + node.type;
  4193. return node;
  4194. }
  4195. }
  4196. throw this.unexpected();
  4197. }
  4198. flowParseDeclareModuleExports(node) {
  4199. this.next();
  4200. this.expectContextual(108);
  4201. node.typeAnnotation = this.flowParseTypeAnnotation();
  4202. this.semicolon();
  4203. return this.finishNode(node, "DeclareModuleExports");
  4204. }
  4205. flowParseDeclareTypeAlias(node) {
  4206. this.next();
  4207. const finished = this.flowParseTypeAlias(node);
  4208. finished.type = "DeclareTypeAlias";
  4209. return finished;
  4210. }
  4211. flowParseDeclareOpaqueType(node) {
  4212. this.next();
  4213. const finished = this.flowParseOpaqueType(node, true);
  4214. finished.type = "DeclareOpaqueType";
  4215. return finished;
  4216. }
  4217. flowParseDeclareInterface(node) {
  4218. this.next();
  4219. this.flowParseInterfaceish(node);
  4220. return this.finishNode(node, "DeclareInterface");
  4221. }
  4222. flowParseInterfaceish(node, isClass = false) {
  4223. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  4224. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.loc.start);
  4225. if (this.match(47)) {
  4226. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4227. } else {
  4228. node.typeParameters = null;
  4229. }
  4230. node.extends = [];
  4231. node.implements = [];
  4232. node.mixins = [];
  4233. if (this.eat(81)) {
  4234. do {
  4235. node.extends.push(this.flowParseInterfaceExtends());
  4236. } while (!isClass && this.eat(12));
  4237. }
  4238. if (this.isContextual(114)) {
  4239. this.next();
  4240. do {
  4241. node.mixins.push(this.flowParseInterfaceExtends());
  4242. } while (this.eat(12));
  4243. }
  4244. if (this.isContextual(110)) {
  4245. this.next();
  4246. do {
  4247. node.implements.push(this.flowParseInterfaceExtends());
  4248. } while (this.eat(12));
  4249. }
  4250. node.body = this.flowParseObjectType({
  4251. allowStatic: isClass,
  4252. allowExact: false,
  4253. allowSpread: false,
  4254. allowProto: isClass,
  4255. allowInexact: false
  4256. });
  4257. }
  4258. flowParseInterfaceExtends() {
  4259. const node = this.startNode();
  4260. node.id = this.flowParseQualifiedTypeIdentifier();
  4261. if (this.match(47)) {
  4262. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4263. } else {
  4264. node.typeParameters = null;
  4265. }
  4266. return this.finishNode(node, "InterfaceExtends");
  4267. }
  4268. flowParseInterface(node) {
  4269. this.flowParseInterfaceish(node);
  4270. return this.finishNode(node, "InterfaceDeclaration");
  4271. }
  4272. checkNotUnderscore(word) {
  4273. if (word === "_") {
  4274. this.raise(FlowErrors.UnexpectedReservedUnderscore, {
  4275. at: this.state.startLoc
  4276. });
  4277. }
  4278. }
  4279. checkReservedType(word, startLoc, declaration) {
  4280. if (!reservedTypes.has(word)) return;
  4281. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, {
  4282. at: startLoc,
  4283. reservedType: word
  4284. });
  4285. }
  4286. flowParseRestrictedIdentifier(liberal, declaration) {
  4287. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  4288. return this.parseIdentifier(liberal);
  4289. }
  4290. flowParseTypeAlias(node) {
  4291. node.id = this.flowParseRestrictedIdentifier(false, true);
  4292. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4293. if (this.match(47)) {
  4294. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4295. } else {
  4296. node.typeParameters = null;
  4297. }
  4298. node.right = this.flowParseTypeInitialiser(29);
  4299. this.semicolon();
  4300. return this.finishNode(node, "TypeAlias");
  4301. }
  4302. flowParseOpaqueType(node, declare) {
  4303. this.expectContextual(126);
  4304. node.id = this.flowParseRestrictedIdentifier(true, true);
  4305. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.loc.start);
  4306. if (this.match(47)) {
  4307. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4308. } else {
  4309. node.typeParameters = null;
  4310. }
  4311. node.supertype = null;
  4312. if (this.match(14)) {
  4313. node.supertype = this.flowParseTypeInitialiser(14);
  4314. }
  4315. node.impltype = null;
  4316. if (!declare) {
  4317. node.impltype = this.flowParseTypeInitialiser(29);
  4318. }
  4319. this.semicolon();
  4320. return this.finishNode(node, "OpaqueType");
  4321. }
  4322. flowParseTypeParameter(requireDefault = false) {
  4323. const nodeStartLoc = this.state.startLoc;
  4324. const node = this.startNode();
  4325. const variance = this.flowParseVariance();
  4326. const ident = this.flowParseTypeAnnotatableIdentifier();
  4327. node.name = ident.name;
  4328. node.variance = variance;
  4329. node.bound = ident.typeAnnotation;
  4330. if (this.match(29)) {
  4331. this.eat(29);
  4332. node.default = this.flowParseType();
  4333. } else {
  4334. if (requireDefault) {
  4335. this.raise(FlowErrors.MissingTypeParamDefault, {
  4336. at: nodeStartLoc
  4337. });
  4338. }
  4339. }
  4340. return this.finishNode(node, "TypeParameter");
  4341. }
  4342. flowParseTypeParameterDeclaration() {
  4343. const oldInType = this.state.inType;
  4344. const node = this.startNode();
  4345. node.params = [];
  4346. this.state.inType = true;
  4347. if (this.match(47) || this.match(138)) {
  4348. this.next();
  4349. } else {
  4350. this.unexpected();
  4351. }
  4352. let defaultRequired = false;
  4353. do {
  4354. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  4355. node.params.push(typeParameter);
  4356. if (typeParameter.default) {
  4357. defaultRequired = true;
  4358. }
  4359. if (!this.match(48)) {
  4360. this.expect(12);
  4361. }
  4362. } while (!this.match(48));
  4363. this.expect(48);
  4364. this.state.inType = oldInType;
  4365. return this.finishNode(node, "TypeParameterDeclaration");
  4366. }
  4367. flowParseTypeParameterInstantiation() {
  4368. const node = this.startNode();
  4369. const oldInType = this.state.inType;
  4370. node.params = [];
  4371. this.state.inType = true;
  4372. this.expect(47);
  4373. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4374. this.state.noAnonFunctionType = false;
  4375. while (!this.match(48)) {
  4376. node.params.push(this.flowParseType());
  4377. if (!this.match(48)) {
  4378. this.expect(12);
  4379. }
  4380. }
  4381. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4382. this.expect(48);
  4383. this.state.inType = oldInType;
  4384. return this.finishNode(node, "TypeParameterInstantiation");
  4385. }
  4386. flowParseTypeParameterInstantiationCallOrNew() {
  4387. const node = this.startNode();
  4388. const oldInType = this.state.inType;
  4389. node.params = [];
  4390. this.state.inType = true;
  4391. this.expect(47);
  4392. while (!this.match(48)) {
  4393. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  4394. if (!this.match(48)) {
  4395. this.expect(12);
  4396. }
  4397. }
  4398. this.expect(48);
  4399. this.state.inType = oldInType;
  4400. return this.finishNode(node, "TypeParameterInstantiation");
  4401. }
  4402. flowParseInterfaceType() {
  4403. const node = this.startNode();
  4404. this.expectContextual(125);
  4405. node.extends = [];
  4406. if (this.eat(81)) {
  4407. do {
  4408. node.extends.push(this.flowParseInterfaceExtends());
  4409. } while (this.eat(12));
  4410. }
  4411. node.body = this.flowParseObjectType({
  4412. allowStatic: false,
  4413. allowExact: false,
  4414. allowSpread: false,
  4415. allowProto: false,
  4416. allowInexact: false
  4417. });
  4418. return this.finishNode(node, "InterfaceTypeAnnotation");
  4419. }
  4420. flowParseObjectPropertyKey() {
  4421. return this.match(130) || this.match(129) ? super.parseExprAtom() : this.parseIdentifier(true);
  4422. }
  4423. flowParseObjectTypeIndexer(node, isStatic, variance) {
  4424. node.static = isStatic;
  4425. if (this.lookahead().type === 14) {
  4426. node.id = this.flowParseObjectPropertyKey();
  4427. node.key = this.flowParseTypeInitialiser();
  4428. } else {
  4429. node.id = null;
  4430. node.key = this.flowParseType();
  4431. }
  4432. this.expect(3);
  4433. node.value = this.flowParseTypeInitialiser();
  4434. node.variance = variance;
  4435. return this.finishNode(node, "ObjectTypeIndexer");
  4436. }
  4437. flowParseObjectTypeInternalSlot(node, isStatic) {
  4438. node.static = isStatic;
  4439. node.id = this.flowParseObjectPropertyKey();
  4440. this.expect(3);
  4441. this.expect(3);
  4442. if (this.match(47) || this.match(10)) {
  4443. node.method = true;
  4444. node.optional = false;
  4445. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4446. } else {
  4447. node.method = false;
  4448. if (this.eat(17)) {
  4449. node.optional = true;
  4450. }
  4451. node.value = this.flowParseTypeInitialiser();
  4452. }
  4453. return this.finishNode(node, "ObjectTypeInternalSlot");
  4454. }
  4455. flowParseObjectTypeMethodish(node) {
  4456. node.params = [];
  4457. node.rest = null;
  4458. node.typeParameters = null;
  4459. node.this = null;
  4460. if (this.match(47)) {
  4461. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4462. }
  4463. this.expect(10);
  4464. if (this.match(78)) {
  4465. node.this = this.flowParseFunctionTypeParam(true);
  4466. node.this.name = null;
  4467. if (!this.match(11)) {
  4468. this.expect(12);
  4469. }
  4470. }
  4471. while (!this.match(11) && !this.match(21)) {
  4472. node.params.push(this.flowParseFunctionTypeParam(false));
  4473. if (!this.match(11)) {
  4474. this.expect(12);
  4475. }
  4476. }
  4477. if (this.eat(21)) {
  4478. node.rest = this.flowParseFunctionTypeParam(false);
  4479. }
  4480. this.expect(11);
  4481. node.returnType = this.flowParseTypeInitialiser();
  4482. return this.finishNode(node, "FunctionTypeAnnotation");
  4483. }
  4484. flowParseObjectTypeCallProperty(node, isStatic) {
  4485. const valueNode = this.startNode();
  4486. node.static = isStatic;
  4487. node.value = this.flowParseObjectTypeMethodish(valueNode);
  4488. return this.finishNode(node, "ObjectTypeCallProperty");
  4489. }
  4490. flowParseObjectType({
  4491. allowStatic,
  4492. allowExact,
  4493. allowSpread,
  4494. allowProto,
  4495. allowInexact
  4496. }) {
  4497. const oldInType = this.state.inType;
  4498. this.state.inType = true;
  4499. const nodeStart = this.startNode();
  4500. nodeStart.callProperties = [];
  4501. nodeStart.properties = [];
  4502. nodeStart.indexers = [];
  4503. nodeStart.internalSlots = [];
  4504. let endDelim;
  4505. let exact;
  4506. let inexact = false;
  4507. if (allowExact && this.match(6)) {
  4508. this.expect(6);
  4509. endDelim = 9;
  4510. exact = true;
  4511. } else {
  4512. this.expect(5);
  4513. endDelim = 8;
  4514. exact = false;
  4515. }
  4516. nodeStart.exact = exact;
  4517. while (!this.match(endDelim)) {
  4518. let isStatic = false;
  4519. let protoStartLoc = null;
  4520. let inexactStartLoc = null;
  4521. const node = this.startNode();
  4522. if (allowProto && this.isContextual(115)) {
  4523. const lookahead = this.lookahead();
  4524. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4525. this.next();
  4526. protoStartLoc = this.state.startLoc;
  4527. allowStatic = false;
  4528. }
  4529. }
  4530. if (allowStatic && this.isContextual(104)) {
  4531. const lookahead = this.lookahead();
  4532. if (lookahead.type !== 14 && lookahead.type !== 17) {
  4533. this.next();
  4534. isStatic = true;
  4535. }
  4536. }
  4537. const variance = this.flowParseVariance();
  4538. if (this.eat(0)) {
  4539. if (protoStartLoc != null) {
  4540. this.unexpected(protoStartLoc);
  4541. }
  4542. if (this.eat(0)) {
  4543. if (variance) {
  4544. this.unexpected(variance.loc.start);
  4545. }
  4546. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  4547. } else {
  4548. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  4549. }
  4550. } else if (this.match(10) || this.match(47)) {
  4551. if (protoStartLoc != null) {
  4552. this.unexpected(protoStartLoc);
  4553. }
  4554. if (variance) {
  4555. this.unexpected(variance.loc.start);
  4556. }
  4557. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  4558. } else {
  4559. let kind = "init";
  4560. if (this.isContextual(98) || this.isContextual(103)) {
  4561. const lookahead = this.lookahead();
  4562. if (tokenIsLiteralPropertyName(lookahead.type)) {
  4563. kind = this.state.value;
  4564. this.next();
  4565. }
  4566. }
  4567. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  4568. if (propOrInexact === null) {
  4569. inexact = true;
  4570. inexactStartLoc = this.state.lastTokStartLoc;
  4571. } else {
  4572. nodeStart.properties.push(propOrInexact);
  4573. }
  4574. }
  4575. this.flowObjectTypeSemicolon();
  4576. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  4577. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, {
  4578. at: inexactStartLoc
  4579. });
  4580. }
  4581. }
  4582. this.expect(endDelim);
  4583. if (allowSpread) {
  4584. nodeStart.inexact = inexact;
  4585. }
  4586. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  4587. this.state.inType = oldInType;
  4588. return out;
  4589. }
  4590. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  4591. if (this.eat(21)) {
  4592. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  4593. if (isInexactToken) {
  4594. if (!allowSpread) {
  4595. this.raise(FlowErrors.InexactInsideNonObject, {
  4596. at: this.state.lastTokStartLoc
  4597. });
  4598. } else if (!allowInexact) {
  4599. this.raise(FlowErrors.InexactInsideExact, {
  4600. at: this.state.lastTokStartLoc
  4601. });
  4602. }
  4603. if (variance) {
  4604. this.raise(FlowErrors.InexactVariance, {
  4605. at: variance
  4606. });
  4607. }
  4608. return null;
  4609. }
  4610. if (!allowSpread) {
  4611. this.raise(FlowErrors.UnexpectedSpreadType, {
  4612. at: this.state.lastTokStartLoc
  4613. });
  4614. }
  4615. if (protoStartLoc != null) {
  4616. this.unexpected(protoStartLoc);
  4617. }
  4618. if (variance) {
  4619. this.raise(FlowErrors.SpreadVariance, {
  4620. at: variance
  4621. });
  4622. }
  4623. node.argument = this.flowParseType();
  4624. return this.finishNode(node, "ObjectTypeSpreadProperty");
  4625. } else {
  4626. node.key = this.flowParseObjectPropertyKey();
  4627. node.static = isStatic;
  4628. node.proto = protoStartLoc != null;
  4629. node.kind = kind;
  4630. let optional = false;
  4631. if (this.match(47) || this.match(10)) {
  4632. node.method = true;
  4633. if (protoStartLoc != null) {
  4634. this.unexpected(protoStartLoc);
  4635. }
  4636. if (variance) {
  4637. this.unexpected(variance.loc.start);
  4638. }
  4639. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  4640. if (kind === "get" || kind === "set") {
  4641. this.flowCheckGetterSetterParams(node);
  4642. }
  4643. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  4644. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  4645. at: node.value.this
  4646. });
  4647. }
  4648. } else {
  4649. if (kind !== "init") this.unexpected();
  4650. node.method = false;
  4651. if (this.eat(17)) {
  4652. optional = true;
  4653. }
  4654. node.value = this.flowParseTypeInitialiser();
  4655. node.variance = variance;
  4656. }
  4657. node.optional = optional;
  4658. return this.finishNode(node, "ObjectTypeProperty");
  4659. }
  4660. }
  4661. flowCheckGetterSetterParams(property) {
  4662. const paramCount = property.kind === "get" ? 0 : 1;
  4663. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  4664. if (property.value.this) {
  4665. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, {
  4666. at: property.value.this
  4667. });
  4668. }
  4669. if (length !== paramCount) {
  4670. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  4671. at: property
  4672. });
  4673. }
  4674. if (property.kind === "set" && property.value.rest) {
  4675. this.raise(Errors.BadSetterRestParameter, {
  4676. at: property
  4677. });
  4678. }
  4679. }
  4680. flowObjectTypeSemicolon() {
  4681. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  4682. this.unexpected();
  4683. }
  4684. }
  4685. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  4686. startPos = startPos || this.state.start;
  4687. startLoc = startLoc || this.state.startLoc;
  4688. let node = id || this.flowParseRestrictedIdentifier(true);
  4689. while (this.eat(16)) {
  4690. const node2 = this.startNodeAt(startPos, startLoc);
  4691. node2.qualification = node;
  4692. node2.id = this.flowParseRestrictedIdentifier(true);
  4693. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  4694. }
  4695. return node;
  4696. }
  4697. flowParseGenericType(startPos, startLoc, id) {
  4698. const node = this.startNodeAt(startPos, startLoc);
  4699. node.typeParameters = null;
  4700. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  4701. if (this.match(47)) {
  4702. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4703. }
  4704. return this.finishNode(node, "GenericTypeAnnotation");
  4705. }
  4706. flowParseTypeofType() {
  4707. const node = this.startNode();
  4708. this.expect(87);
  4709. node.argument = this.flowParsePrimaryType();
  4710. return this.finishNode(node, "TypeofTypeAnnotation");
  4711. }
  4712. flowParseTupleType() {
  4713. const node = this.startNode();
  4714. node.types = [];
  4715. this.expect(0);
  4716. while (this.state.pos < this.length && !this.match(3)) {
  4717. node.types.push(this.flowParseType());
  4718. if (this.match(3)) break;
  4719. this.expect(12);
  4720. }
  4721. this.expect(3);
  4722. return this.finishNode(node, "TupleTypeAnnotation");
  4723. }
  4724. flowParseFunctionTypeParam(first) {
  4725. let name = null;
  4726. let optional = false;
  4727. let typeAnnotation = null;
  4728. const node = this.startNode();
  4729. const lh = this.lookahead();
  4730. const isThis = this.state.type === 78;
  4731. if (lh.type === 14 || lh.type === 17) {
  4732. if (isThis && !first) {
  4733. this.raise(FlowErrors.ThisParamMustBeFirst, {
  4734. at: node
  4735. });
  4736. }
  4737. name = this.parseIdentifier(isThis);
  4738. if (this.eat(17)) {
  4739. optional = true;
  4740. if (isThis) {
  4741. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  4742. at: node
  4743. });
  4744. }
  4745. }
  4746. typeAnnotation = this.flowParseTypeInitialiser();
  4747. } else {
  4748. typeAnnotation = this.flowParseType();
  4749. }
  4750. node.name = name;
  4751. node.optional = optional;
  4752. node.typeAnnotation = typeAnnotation;
  4753. return this.finishNode(node, "FunctionTypeParam");
  4754. }
  4755. reinterpretTypeAsFunctionTypeParam(type) {
  4756. const node = this.startNodeAt(type.start, type.loc.start);
  4757. node.name = null;
  4758. node.optional = false;
  4759. node.typeAnnotation = type;
  4760. return this.finishNode(node, "FunctionTypeParam");
  4761. }
  4762. flowParseFunctionTypeParams(params = []) {
  4763. let rest = null;
  4764. let _this = null;
  4765. if (this.match(78)) {
  4766. _this = this.flowParseFunctionTypeParam(true);
  4767. _this.name = null;
  4768. if (!this.match(11)) {
  4769. this.expect(12);
  4770. }
  4771. }
  4772. while (!this.match(11) && !this.match(21)) {
  4773. params.push(this.flowParseFunctionTypeParam(false));
  4774. if (!this.match(11)) {
  4775. this.expect(12);
  4776. }
  4777. }
  4778. if (this.eat(21)) {
  4779. rest = this.flowParseFunctionTypeParam(false);
  4780. }
  4781. return {
  4782. params,
  4783. rest,
  4784. _this
  4785. };
  4786. }
  4787. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  4788. switch (id.name) {
  4789. case "any":
  4790. return this.finishNode(node, "AnyTypeAnnotation");
  4791. case "bool":
  4792. case "boolean":
  4793. return this.finishNode(node, "BooleanTypeAnnotation");
  4794. case "mixed":
  4795. return this.finishNode(node, "MixedTypeAnnotation");
  4796. case "empty":
  4797. return this.finishNode(node, "EmptyTypeAnnotation");
  4798. case "number":
  4799. return this.finishNode(node, "NumberTypeAnnotation");
  4800. case "string":
  4801. return this.finishNode(node, "StringTypeAnnotation");
  4802. case "symbol":
  4803. return this.finishNode(node, "SymbolTypeAnnotation");
  4804. default:
  4805. this.checkNotUnderscore(id.name);
  4806. return this.flowParseGenericType(startPos, startLoc, id);
  4807. }
  4808. }
  4809. flowParsePrimaryType() {
  4810. const startPos = this.state.start;
  4811. const startLoc = this.state.startLoc;
  4812. const node = this.startNode();
  4813. let tmp;
  4814. let type;
  4815. let isGroupedType = false;
  4816. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4817. switch (this.state.type) {
  4818. case 5:
  4819. return this.flowParseObjectType({
  4820. allowStatic: false,
  4821. allowExact: false,
  4822. allowSpread: true,
  4823. allowProto: false,
  4824. allowInexact: true
  4825. });
  4826. case 6:
  4827. return this.flowParseObjectType({
  4828. allowStatic: false,
  4829. allowExact: true,
  4830. allowSpread: true,
  4831. allowProto: false,
  4832. allowInexact: false
  4833. });
  4834. case 0:
  4835. this.state.noAnonFunctionType = false;
  4836. type = this.flowParseTupleType();
  4837. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4838. return type;
  4839. case 47:
  4840. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4841. this.expect(10);
  4842. tmp = this.flowParseFunctionTypeParams();
  4843. node.params = tmp.params;
  4844. node.rest = tmp.rest;
  4845. node.this = tmp._this;
  4846. this.expect(11);
  4847. this.expect(19);
  4848. node.returnType = this.flowParseType();
  4849. return this.finishNode(node, "FunctionTypeAnnotation");
  4850. case 10:
  4851. this.next();
  4852. if (!this.match(11) && !this.match(21)) {
  4853. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  4854. const token = this.lookahead().type;
  4855. isGroupedType = token !== 17 && token !== 14;
  4856. } else {
  4857. isGroupedType = true;
  4858. }
  4859. }
  4860. if (isGroupedType) {
  4861. this.state.noAnonFunctionType = false;
  4862. type = this.flowParseType();
  4863. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4864. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  4865. this.expect(11);
  4866. return type;
  4867. } else {
  4868. this.eat(12);
  4869. }
  4870. }
  4871. if (type) {
  4872. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  4873. } else {
  4874. tmp = this.flowParseFunctionTypeParams();
  4875. }
  4876. node.params = tmp.params;
  4877. node.rest = tmp.rest;
  4878. node.this = tmp._this;
  4879. this.expect(11);
  4880. this.expect(19);
  4881. node.returnType = this.flowParseType();
  4882. node.typeParameters = null;
  4883. return this.finishNode(node, "FunctionTypeAnnotation");
  4884. case 129:
  4885. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  4886. case 85:
  4887. case 86:
  4888. node.value = this.match(85);
  4889. this.next();
  4890. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  4891. case 53:
  4892. if (this.state.value === "-") {
  4893. this.next();
  4894. if (this.match(130)) {
  4895. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  4896. }
  4897. if (this.match(131)) {
  4898. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  4899. }
  4900. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, {
  4901. at: this.state.startLoc
  4902. });
  4903. }
  4904. throw this.unexpected();
  4905. case 130:
  4906. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  4907. case 131:
  4908. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  4909. case 88:
  4910. this.next();
  4911. return this.finishNode(node, "VoidTypeAnnotation");
  4912. case 84:
  4913. this.next();
  4914. return this.finishNode(node, "NullLiteralTypeAnnotation");
  4915. case 78:
  4916. this.next();
  4917. return this.finishNode(node, "ThisTypeAnnotation");
  4918. case 55:
  4919. this.next();
  4920. return this.finishNode(node, "ExistsTypeAnnotation");
  4921. case 87:
  4922. return this.flowParseTypeofType();
  4923. default:
  4924. if (tokenIsKeyword(this.state.type)) {
  4925. const label = tokenLabelName(this.state.type);
  4926. this.next();
  4927. return super.createIdentifier(node, label);
  4928. } else if (tokenIsIdentifier(this.state.type)) {
  4929. if (this.isContextual(125)) {
  4930. return this.flowParseInterfaceType();
  4931. }
  4932. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  4933. }
  4934. }
  4935. throw this.unexpected();
  4936. }
  4937. flowParsePostfixType() {
  4938. const startPos = this.state.start;
  4939. const startLoc = this.state.startLoc;
  4940. let type = this.flowParsePrimaryType();
  4941. let seenOptionalIndexedAccess = false;
  4942. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  4943. const node = this.startNodeAt(startPos, startLoc);
  4944. const optional = this.eat(18);
  4945. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4946. this.expect(0);
  4947. if (!optional && this.match(3)) {
  4948. node.elementType = type;
  4949. this.next();
  4950. type = this.finishNode(node, "ArrayTypeAnnotation");
  4951. } else {
  4952. node.objectType = type;
  4953. node.indexType = this.flowParseType();
  4954. this.expect(3);
  4955. if (seenOptionalIndexedAccess) {
  4956. node.optional = optional;
  4957. type = this.finishNode(node, "OptionalIndexedAccessType");
  4958. } else {
  4959. type = this.finishNode(node, "IndexedAccessType");
  4960. }
  4961. }
  4962. }
  4963. return type;
  4964. }
  4965. flowParsePrefixType() {
  4966. const node = this.startNode();
  4967. if (this.eat(17)) {
  4968. node.typeAnnotation = this.flowParsePrefixType();
  4969. return this.finishNode(node, "NullableTypeAnnotation");
  4970. } else {
  4971. return this.flowParsePostfixType();
  4972. }
  4973. }
  4974. flowParseAnonFunctionWithoutParens() {
  4975. const param = this.flowParsePrefixType();
  4976. if (!this.state.noAnonFunctionType && this.eat(19)) {
  4977. const node = this.startNodeAt(param.start, param.loc.start);
  4978. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4979. node.rest = null;
  4980. node.this = null;
  4981. node.returnType = this.flowParseType();
  4982. node.typeParameters = null;
  4983. return this.finishNode(node, "FunctionTypeAnnotation");
  4984. }
  4985. return param;
  4986. }
  4987. flowParseIntersectionType() {
  4988. const node = this.startNode();
  4989. this.eat(45);
  4990. const type = this.flowParseAnonFunctionWithoutParens();
  4991. node.types = [type];
  4992. while (this.eat(45)) {
  4993. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4994. }
  4995. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4996. }
  4997. flowParseUnionType() {
  4998. const node = this.startNode();
  4999. this.eat(43);
  5000. const type = this.flowParseIntersectionType();
  5001. node.types = [type];
  5002. while (this.eat(43)) {
  5003. node.types.push(this.flowParseIntersectionType());
  5004. }
  5005. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  5006. }
  5007. flowParseType() {
  5008. const oldInType = this.state.inType;
  5009. this.state.inType = true;
  5010. const type = this.flowParseUnionType();
  5011. this.state.inType = oldInType;
  5012. return type;
  5013. }
  5014. flowParseTypeOrImplicitInstantiation() {
  5015. if (this.state.type === 128 && this.state.value === "_") {
  5016. const startPos = this.state.start;
  5017. const startLoc = this.state.startLoc;
  5018. const node = this.parseIdentifier();
  5019. return this.flowParseGenericType(startPos, startLoc, node);
  5020. } else {
  5021. return this.flowParseType();
  5022. }
  5023. }
  5024. flowParseTypeAnnotation() {
  5025. const node = this.startNode();
  5026. node.typeAnnotation = this.flowParseTypeInitialiser();
  5027. return this.finishNode(node, "TypeAnnotation");
  5028. }
  5029. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  5030. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  5031. if (this.match(14)) {
  5032. ident.typeAnnotation = this.flowParseTypeAnnotation();
  5033. this.resetEndLocation(ident);
  5034. }
  5035. return ident;
  5036. }
  5037. typeCastToParameter(node) {
  5038. node.expression.typeAnnotation = node.typeAnnotation;
  5039. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  5040. return node.expression;
  5041. }
  5042. flowParseVariance() {
  5043. let variance = null;
  5044. if (this.match(53)) {
  5045. variance = this.startNode();
  5046. if (this.state.value === "+") {
  5047. variance.kind = "plus";
  5048. } else {
  5049. variance.kind = "minus";
  5050. }
  5051. this.next();
  5052. return this.finishNode(variance, "Variance");
  5053. }
  5054. return variance;
  5055. }
  5056. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  5057. if (allowExpressionBody) {
  5058. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  5059. }
  5060. return super.parseFunctionBody(node, false, isMethod);
  5061. }
  5062. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  5063. if (this.match(14)) {
  5064. const typeNode = this.startNode();
  5065. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5066. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  5067. }
  5068. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  5069. }
  5070. parseStatement(context, topLevel) {
  5071. if (this.state.strict && this.isContextual(125)) {
  5072. const lookahead = this.lookahead();
  5073. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  5074. const node = this.startNode();
  5075. this.next();
  5076. return this.flowParseInterface(node);
  5077. }
  5078. } else if (this.shouldParseEnums() && this.isContextual(122)) {
  5079. const node = this.startNode();
  5080. this.next();
  5081. return this.flowParseEnumDeclaration(node);
  5082. }
  5083. const stmt = super.parseStatement(context, topLevel);
  5084. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  5085. this.flowPragma = null;
  5086. }
  5087. return stmt;
  5088. }
  5089. parseExpressionStatement(node, expr) {
  5090. if (expr.type === "Identifier") {
  5091. if (expr.name === "declare") {
  5092. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  5093. return this.flowParseDeclare(node);
  5094. }
  5095. } else if (tokenIsIdentifier(this.state.type)) {
  5096. if (expr.name === "interface") {
  5097. return this.flowParseInterface(node);
  5098. } else if (expr.name === "type") {
  5099. return this.flowParseTypeAlias(node);
  5100. } else if (expr.name === "opaque") {
  5101. return this.flowParseOpaqueType(node, false);
  5102. }
  5103. }
  5104. }
  5105. return super.parseExpressionStatement(node, expr);
  5106. }
  5107. shouldParseExportDeclaration() {
  5108. const {
  5109. type
  5110. } = this.state;
  5111. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
  5112. return !this.state.containsEsc;
  5113. }
  5114. return super.shouldParseExportDeclaration();
  5115. }
  5116. isExportDefaultSpecifier() {
  5117. const {
  5118. type
  5119. } = this.state;
  5120. if (tokenIsFlowInterfaceOrTypeOrOpaque(type) || this.shouldParseEnums() && type === 122) {
  5121. return this.state.containsEsc;
  5122. }
  5123. return super.isExportDefaultSpecifier();
  5124. }
  5125. parseExportDefaultExpression() {
  5126. if (this.shouldParseEnums() && this.isContextual(122)) {
  5127. const node = this.startNode();
  5128. this.next();
  5129. return this.flowParseEnumDeclaration(node);
  5130. }
  5131. return super.parseExportDefaultExpression();
  5132. }
  5133. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  5134. if (!this.match(17)) return expr;
  5135. if (this.state.maybeInArrowParameters) {
  5136. const nextCh = this.lookaheadCharCode();
  5137. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  5138. this.setOptionalParametersError(refExpressionErrors);
  5139. return expr;
  5140. }
  5141. }
  5142. this.expect(17);
  5143. const state = this.state.clone();
  5144. const originalNoArrowAt = this.state.noArrowAt;
  5145. const node = this.startNodeAt(startPos, startLoc);
  5146. let {
  5147. consequent,
  5148. failed
  5149. } = this.tryParseConditionalConsequent();
  5150. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5151. if (failed || invalid.length > 0) {
  5152. const noArrowAt = [...originalNoArrowAt];
  5153. if (invalid.length > 0) {
  5154. this.state = state;
  5155. this.state.noArrowAt = noArrowAt;
  5156. for (let i = 0; i < invalid.length; i++) {
  5157. noArrowAt.push(invalid[i].start);
  5158. }
  5159. ({
  5160. consequent,
  5161. failed
  5162. } = this.tryParseConditionalConsequent());
  5163. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  5164. }
  5165. if (failed && valid.length > 1) {
  5166. this.raise(FlowErrors.AmbiguousConditionalArrow, {
  5167. at: state.startLoc
  5168. });
  5169. }
  5170. if (failed && valid.length === 1) {
  5171. this.state = state;
  5172. noArrowAt.push(valid[0].start);
  5173. this.state.noArrowAt = noArrowAt;
  5174. ({
  5175. consequent,
  5176. failed
  5177. } = this.tryParseConditionalConsequent());
  5178. }
  5179. }
  5180. this.getArrowLikeExpressions(consequent, true);
  5181. this.state.noArrowAt = originalNoArrowAt;
  5182. this.expect(14);
  5183. node.test = expr;
  5184. node.consequent = consequent;
  5185. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  5186. return this.finishNode(node, "ConditionalExpression");
  5187. }
  5188. tryParseConditionalConsequent() {
  5189. this.state.noArrowParamsConversionAt.push(this.state.start);
  5190. const consequent = this.parseMaybeAssignAllowIn();
  5191. const failed = !this.match(14);
  5192. this.state.noArrowParamsConversionAt.pop();
  5193. return {
  5194. consequent,
  5195. failed
  5196. };
  5197. }
  5198. getArrowLikeExpressions(node, disallowInvalid) {
  5199. const stack = [node];
  5200. const arrows = [];
  5201. while (stack.length !== 0) {
  5202. const node = stack.pop();
  5203. if (node.type === "ArrowFunctionExpression") {
  5204. if (node.typeParameters || !node.returnType) {
  5205. this.finishArrowValidation(node);
  5206. } else {
  5207. arrows.push(node);
  5208. }
  5209. stack.push(node.body);
  5210. } else if (node.type === "ConditionalExpression") {
  5211. stack.push(node.consequent);
  5212. stack.push(node.alternate);
  5213. }
  5214. }
  5215. if (disallowInvalid) {
  5216. arrows.forEach(node => this.finishArrowValidation(node));
  5217. return [arrows, []];
  5218. }
  5219. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  5220. }
  5221. finishArrowValidation(node) {
  5222. var _node$extra;
  5223. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  5224. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  5225. super.checkParams(node, false, true);
  5226. this.scope.exit();
  5227. }
  5228. forwardNoArrowParamsConversionAt(node, parse) {
  5229. let result;
  5230. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5231. this.state.noArrowParamsConversionAt.push(this.state.start);
  5232. result = parse();
  5233. this.state.noArrowParamsConversionAt.pop();
  5234. } else {
  5235. result = parse();
  5236. }
  5237. return result;
  5238. }
  5239. parseParenItem(node, startPos, startLoc) {
  5240. node = super.parseParenItem(node, startPos, startLoc);
  5241. if (this.eat(17)) {
  5242. node.optional = true;
  5243. this.resetEndLocation(node);
  5244. }
  5245. if (this.match(14)) {
  5246. const typeCastNode = this.startNodeAt(startPos, startLoc);
  5247. typeCastNode.expression = node;
  5248. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  5249. return this.finishNode(typeCastNode, "TypeCastExpression");
  5250. }
  5251. return node;
  5252. }
  5253. assertModuleNodeAllowed(node) {
  5254. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  5255. return;
  5256. }
  5257. super.assertModuleNodeAllowed(node);
  5258. }
  5259. parseExport(node) {
  5260. const decl = super.parseExport(node);
  5261. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  5262. decl.exportKind = decl.exportKind || "value";
  5263. }
  5264. return decl;
  5265. }
  5266. parseExportDeclaration(node) {
  5267. if (this.isContextual(126)) {
  5268. node.exportKind = "type";
  5269. const declarationNode = this.startNode();
  5270. this.next();
  5271. if (this.match(5)) {
  5272. node.specifiers = this.parseExportSpecifiers(true);
  5273. super.parseExportFrom(node);
  5274. return null;
  5275. } else {
  5276. return this.flowParseTypeAlias(declarationNode);
  5277. }
  5278. } else if (this.isContextual(127)) {
  5279. node.exportKind = "type";
  5280. const declarationNode = this.startNode();
  5281. this.next();
  5282. return this.flowParseOpaqueType(declarationNode, false);
  5283. } else if (this.isContextual(125)) {
  5284. node.exportKind = "type";
  5285. const declarationNode = this.startNode();
  5286. this.next();
  5287. return this.flowParseInterface(declarationNode);
  5288. } else if (this.shouldParseEnums() && this.isContextual(122)) {
  5289. node.exportKind = "value";
  5290. const declarationNode = this.startNode();
  5291. this.next();
  5292. return this.flowParseEnumDeclaration(declarationNode);
  5293. } else {
  5294. return super.parseExportDeclaration(node);
  5295. }
  5296. }
  5297. eatExportStar(node) {
  5298. if (super.eatExportStar(node)) return true;
  5299. if (this.isContextual(126) && this.lookahead().type === 55) {
  5300. node.exportKind = "type";
  5301. this.next();
  5302. this.next();
  5303. return true;
  5304. }
  5305. return false;
  5306. }
  5307. maybeParseExportNamespaceSpecifier(node) {
  5308. const {
  5309. startLoc
  5310. } = this.state;
  5311. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  5312. if (hasNamespace && node.exportKind === "type") {
  5313. this.unexpected(startLoc);
  5314. }
  5315. return hasNamespace;
  5316. }
  5317. parseClassId(node, isStatement, optionalId) {
  5318. super.parseClassId(node, isStatement, optionalId);
  5319. if (this.match(47)) {
  5320. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5321. }
  5322. }
  5323. parseClassMember(classBody, member, state) {
  5324. const {
  5325. startLoc
  5326. } = this.state;
  5327. if (this.isContextual(121)) {
  5328. if (super.parseClassMemberFromModifier(classBody, member)) {
  5329. return;
  5330. }
  5331. member.declare = true;
  5332. }
  5333. super.parseClassMember(classBody, member, state);
  5334. if (member.declare) {
  5335. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  5336. this.raise(FlowErrors.DeclareClassElement, {
  5337. at: startLoc
  5338. });
  5339. } else if (member.value) {
  5340. this.raise(FlowErrors.DeclareClassFieldInitializer, {
  5341. at: member.value
  5342. });
  5343. }
  5344. }
  5345. }
  5346. isIterator(word) {
  5347. return word === "iterator" || word === "asyncIterator";
  5348. }
  5349. readIterator() {
  5350. const word = super.readWord1();
  5351. const fullWord = "@@" + word;
  5352. if (!this.isIterator(word) || !this.state.inType) {
  5353. this.raise(Errors.InvalidIdentifier, {
  5354. at: this.state.curPosition(),
  5355. identifierName: fullWord
  5356. });
  5357. }
  5358. this.finishToken(128, fullWord);
  5359. }
  5360. getTokenFromCode(code) {
  5361. const next = this.input.charCodeAt(this.state.pos + 1);
  5362. if (code === 123 && next === 124) {
  5363. return this.finishOp(6, 2);
  5364. } else if (this.state.inType && (code === 62 || code === 60)) {
  5365. return this.finishOp(code === 62 ? 48 : 47, 1);
  5366. } else if (this.state.inType && code === 63) {
  5367. if (next === 46) {
  5368. return this.finishOp(18, 2);
  5369. }
  5370. return this.finishOp(17, 1);
  5371. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  5372. this.state.pos += 2;
  5373. return this.readIterator();
  5374. } else {
  5375. return super.getTokenFromCode(code);
  5376. }
  5377. }
  5378. isAssignable(node, isBinding) {
  5379. if (node.type === "TypeCastExpression") {
  5380. return this.isAssignable(node.expression, isBinding);
  5381. } else {
  5382. return super.isAssignable(node, isBinding);
  5383. }
  5384. }
  5385. toAssignable(node, isLHS = false) {
  5386. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  5387. node.left = this.typeCastToParameter(node.left);
  5388. }
  5389. super.toAssignable(node, isLHS);
  5390. }
  5391. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  5392. for (let i = 0; i < exprList.length; i++) {
  5393. const expr = exprList[i];
  5394. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  5395. exprList[i] = this.typeCastToParameter(expr);
  5396. }
  5397. }
  5398. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  5399. }
  5400. toReferencedList(exprList, isParenthesizedExpr) {
  5401. for (let i = 0; i < exprList.length; i++) {
  5402. var _expr$extra;
  5403. const expr = exprList[i];
  5404. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  5405. this.raise(FlowErrors.TypeCastInPattern, {
  5406. at: expr.typeAnnotation
  5407. });
  5408. }
  5409. }
  5410. return exprList;
  5411. }
  5412. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  5413. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  5414. if (canBePattern && !this.state.maybeInArrowParameters) {
  5415. this.toReferencedList(node.elements);
  5416. }
  5417. return node;
  5418. }
  5419. isValidLVal(type, isParenthesized, binding) {
  5420. return type === "TypeCastExpression" || super.isValidLVal(type, isParenthesized, binding);
  5421. }
  5422. parseClassProperty(node) {
  5423. if (this.match(14)) {
  5424. node.typeAnnotation = this.flowParseTypeAnnotation();
  5425. }
  5426. return super.parseClassProperty(node);
  5427. }
  5428. parseClassPrivateProperty(node) {
  5429. if (this.match(14)) {
  5430. node.typeAnnotation = this.flowParseTypeAnnotation();
  5431. }
  5432. return super.parseClassPrivateProperty(node);
  5433. }
  5434. isClassMethod() {
  5435. return this.match(47) || super.isClassMethod();
  5436. }
  5437. isClassProperty() {
  5438. return this.match(14) || super.isClassProperty();
  5439. }
  5440. isNonstaticConstructor(method) {
  5441. return !this.match(14) && super.isNonstaticConstructor(method);
  5442. }
  5443. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  5444. if (method.variance) {
  5445. this.unexpected(method.variance.loc.start);
  5446. }
  5447. delete method.variance;
  5448. if (this.match(47)) {
  5449. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5450. }
  5451. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  5452. if (method.params && isConstructor) {
  5453. const params = method.params;
  5454. if (params.length > 0 && this.isThisParam(params[0])) {
  5455. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5456. at: method
  5457. });
  5458. }
  5459. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  5460. const params = method.value.params;
  5461. if (params.length > 0 && this.isThisParam(params[0])) {
  5462. this.raise(FlowErrors.ThisParamBannedInConstructor, {
  5463. at: method
  5464. });
  5465. }
  5466. }
  5467. }
  5468. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  5469. if (method.variance) {
  5470. this.unexpected(method.variance.loc.start);
  5471. }
  5472. delete method.variance;
  5473. if (this.match(47)) {
  5474. method.typeParameters = this.flowParseTypeParameterDeclaration();
  5475. }
  5476. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  5477. }
  5478. parseClassSuper(node) {
  5479. super.parseClassSuper(node);
  5480. if (node.superClass && this.match(47)) {
  5481. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  5482. }
  5483. if (this.isContextual(110)) {
  5484. this.next();
  5485. const implemented = node.implements = [];
  5486. do {
  5487. const node = this.startNode();
  5488. node.id = this.flowParseRestrictedIdentifier(true);
  5489. if (this.match(47)) {
  5490. node.typeParameters = this.flowParseTypeParameterInstantiation();
  5491. } else {
  5492. node.typeParameters = null;
  5493. }
  5494. implemented.push(this.finishNode(node, "ClassImplements"));
  5495. } while (this.eat(12));
  5496. }
  5497. }
  5498. checkGetterSetterParams(method) {
  5499. super.checkGetterSetterParams(method);
  5500. const params = this.getObjectOrClassMethodParams(method);
  5501. if (params.length > 0) {
  5502. const param = params[0];
  5503. if (this.isThisParam(param) && method.kind === "get") {
  5504. this.raise(FlowErrors.GetterMayNotHaveThisParam, {
  5505. at: param
  5506. });
  5507. } else if (this.isThisParam(param)) {
  5508. this.raise(FlowErrors.SetterMayNotHaveThisParam, {
  5509. at: param
  5510. });
  5511. }
  5512. }
  5513. }
  5514. parsePropertyNamePrefixOperator(node) {
  5515. node.variance = this.flowParseVariance();
  5516. }
  5517. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  5518. if (prop.variance) {
  5519. this.unexpected(prop.variance.loc.start);
  5520. }
  5521. delete prop.variance;
  5522. let typeParameters;
  5523. if (this.match(47) && !isAccessor) {
  5524. typeParameters = this.flowParseTypeParameterDeclaration();
  5525. if (!this.match(10)) this.unexpected();
  5526. }
  5527. const result = super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  5528. if (typeParameters) {
  5529. (result.value || result).typeParameters = typeParameters;
  5530. }
  5531. return result;
  5532. }
  5533. parseAssignableListItemTypes(param) {
  5534. if (this.eat(17)) {
  5535. if (param.type !== "Identifier") {
  5536. this.raise(FlowErrors.PatternIsOptional, {
  5537. at: param
  5538. });
  5539. }
  5540. if (this.isThisParam(param)) {
  5541. this.raise(FlowErrors.ThisParamMayNotBeOptional, {
  5542. at: param
  5543. });
  5544. }
  5545. param.optional = true;
  5546. }
  5547. if (this.match(14)) {
  5548. param.typeAnnotation = this.flowParseTypeAnnotation();
  5549. } else if (this.isThisParam(param)) {
  5550. this.raise(FlowErrors.ThisParamAnnotationRequired, {
  5551. at: param
  5552. });
  5553. }
  5554. if (this.match(29) && this.isThisParam(param)) {
  5555. this.raise(FlowErrors.ThisParamNoDefault, {
  5556. at: param
  5557. });
  5558. }
  5559. this.resetEndLocation(param);
  5560. return param;
  5561. }
  5562. parseMaybeDefault(startPos, startLoc, left) {
  5563. const node = super.parseMaybeDefault(startPos, startLoc, left);
  5564. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  5565. this.raise(FlowErrors.TypeBeforeInitializer, {
  5566. at: node.typeAnnotation
  5567. });
  5568. }
  5569. return node;
  5570. }
  5571. shouldParseDefaultImport(node) {
  5572. if (!hasTypeImportKind(node)) {
  5573. return super.shouldParseDefaultImport(node);
  5574. }
  5575. return isMaybeDefaultImport(this.state.type);
  5576. }
  5577. parseImportSpecifierLocal(node, specifier, type) {
  5578. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  5579. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  5580. }
  5581. maybeParseDefaultImportSpecifier(node) {
  5582. node.importKind = "value";
  5583. let kind = null;
  5584. if (this.match(87)) {
  5585. kind = "typeof";
  5586. } else if (this.isContextual(126)) {
  5587. kind = "type";
  5588. }
  5589. if (kind) {
  5590. const lh = this.lookahead();
  5591. const {
  5592. type
  5593. } = lh;
  5594. if (kind === "type" && type === 55) {
  5595. this.unexpected(null, lh.type);
  5596. }
  5597. if (isMaybeDefaultImport(type) || type === 5 || type === 55) {
  5598. this.next();
  5599. node.importKind = kind;
  5600. }
  5601. }
  5602. return super.maybeParseDefaultImportSpecifier(node);
  5603. }
  5604. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  5605. const firstIdent = specifier.imported;
  5606. let specifierTypeKind = null;
  5607. if (firstIdent.type === "Identifier") {
  5608. if (firstIdent.name === "type") {
  5609. specifierTypeKind = "type";
  5610. } else if (firstIdent.name === "typeof") {
  5611. specifierTypeKind = "typeof";
  5612. }
  5613. }
  5614. let isBinding = false;
  5615. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  5616. const as_ident = this.parseIdentifier(true);
  5617. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  5618. specifier.imported = as_ident;
  5619. specifier.importKind = specifierTypeKind;
  5620. specifier.local = cloneIdentifier(as_ident);
  5621. } else {
  5622. specifier.imported = firstIdent;
  5623. specifier.importKind = null;
  5624. specifier.local = this.parseIdentifier();
  5625. }
  5626. } else {
  5627. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  5628. specifier.imported = this.parseIdentifier(true);
  5629. specifier.importKind = specifierTypeKind;
  5630. } else {
  5631. if (importedIsString) {
  5632. throw this.raise(Errors.ImportBindingIsString, {
  5633. at: specifier,
  5634. importName: firstIdent.value
  5635. });
  5636. }
  5637. specifier.imported = firstIdent;
  5638. specifier.importKind = null;
  5639. }
  5640. if (this.eatContextual(93)) {
  5641. specifier.local = this.parseIdentifier();
  5642. } else {
  5643. isBinding = true;
  5644. specifier.local = cloneIdentifier(specifier.imported);
  5645. }
  5646. }
  5647. const specifierIsTypeImport = hasTypeImportKind(specifier);
  5648. if (isInTypeOnlyImport && specifierIsTypeImport) {
  5649. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, {
  5650. at: specifier
  5651. });
  5652. }
  5653. if (isInTypeOnlyImport || specifierIsTypeImport) {
  5654. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  5655. }
  5656. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  5657. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  5658. }
  5659. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  5660. }
  5661. parseBindingAtom() {
  5662. switch (this.state.type) {
  5663. case 78:
  5664. return this.parseIdentifier(true);
  5665. default:
  5666. return super.parseBindingAtom();
  5667. }
  5668. }
  5669. parseFunctionParams(node, allowModifiers) {
  5670. const kind = node.kind;
  5671. if (kind !== "get" && kind !== "set" && this.match(47)) {
  5672. node.typeParameters = this.flowParseTypeParameterDeclaration();
  5673. }
  5674. super.parseFunctionParams(node, allowModifiers);
  5675. }
  5676. parseVarId(decl, kind) {
  5677. super.parseVarId(decl, kind);
  5678. if (this.match(14)) {
  5679. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  5680. this.resetEndLocation(decl.id);
  5681. }
  5682. }
  5683. parseAsyncArrowFromCallExpression(node, call) {
  5684. if (this.match(14)) {
  5685. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5686. this.state.noAnonFunctionType = true;
  5687. node.returnType = this.flowParseTypeAnnotation();
  5688. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5689. }
  5690. return super.parseAsyncArrowFromCallExpression(node, call);
  5691. }
  5692. shouldParseAsyncArrow() {
  5693. return this.match(14) || super.shouldParseAsyncArrow();
  5694. }
  5695. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  5696. var _jsx;
  5697. let state = null;
  5698. let jsx;
  5699. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  5700. state = this.state.clone();
  5701. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  5702. if (!jsx.error) return jsx.node;
  5703. const {
  5704. context
  5705. } = this.state;
  5706. const currentContext = context[context.length - 1];
  5707. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  5708. context.pop();
  5709. }
  5710. }
  5711. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  5712. var _jsx2, _jsx3;
  5713. state = state || this.state.clone();
  5714. let typeParameters;
  5715. const arrow = this.tryParse(abort => {
  5716. var _arrowExpression$extr;
  5717. typeParameters = this.flowParseTypeParameterDeclaration();
  5718. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  5719. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5720. this.resetStartLocationFromNode(result, typeParameters);
  5721. return result;
  5722. });
  5723. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  5724. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  5725. if (expr.type !== "ArrowFunctionExpression") abort();
  5726. expr.typeParameters = typeParameters;
  5727. this.resetStartLocationFromNode(expr, typeParameters);
  5728. return arrowExpression;
  5729. }, state);
  5730. let arrowExpression = null;
  5731. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  5732. if (!arrow.error && !arrow.aborted) {
  5733. if (arrow.node.async) {
  5734. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, {
  5735. at: typeParameters
  5736. });
  5737. }
  5738. return arrow.node;
  5739. }
  5740. arrowExpression = arrow.node;
  5741. }
  5742. if ((_jsx2 = jsx) != null && _jsx2.node) {
  5743. this.state = jsx.failState;
  5744. return jsx.node;
  5745. }
  5746. if (arrowExpression) {
  5747. this.state = arrow.failState;
  5748. return arrowExpression;
  5749. }
  5750. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  5751. if (arrow.thrown) throw arrow.error;
  5752. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, {
  5753. at: typeParameters
  5754. });
  5755. }
  5756. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  5757. }
  5758. parseArrow(node) {
  5759. if (this.match(14)) {
  5760. const result = this.tryParse(() => {
  5761. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  5762. this.state.noAnonFunctionType = true;
  5763. const typeNode = this.startNode();
  5764. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  5765. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  5766. if (this.canInsertSemicolon()) this.unexpected();
  5767. if (!this.match(19)) this.unexpected();
  5768. return typeNode;
  5769. });
  5770. if (result.thrown) return null;
  5771. if (result.error) this.state = result.failState;
  5772. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  5773. }
  5774. return super.parseArrow(node);
  5775. }
  5776. shouldParseArrow(params) {
  5777. return this.match(14) || super.shouldParseArrow(params);
  5778. }
  5779. setArrowFunctionParameters(node, params) {
  5780. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5781. node.params = params;
  5782. } else {
  5783. super.setArrowFunctionParameters(node, params);
  5784. }
  5785. }
  5786. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  5787. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  5788. return;
  5789. }
  5790. for (let i = 0; i < node.params.length; i++) {
  5791. if (this.isThisParam(node.params[i]) && i > 0) {
  5792. this.raise(FlowErrors.ThisParamMustBeFirst, {
  5793. at: node.params[i]
  5794. });
  5795. }
  5796. }
  5797. return super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  5798. }
  5799. parseParenAndDistinguishExpression(canBeArrow) {
  5800. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  5801. }
  5802. parseSubscripts(base, startPos, startLoc, noCalls) {
  5803. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  5804. this.next();
  5805. const node = this.startNodeAt(startPos, startLoc);
  5806. node.callee = base;
  5807. node.arguments = super.parseCallExpressionArguments(11, false);
  5808. base = this.finishNode(node, "CallExpression");
  5809. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  5810. const state = this.state.clone();
  5811. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  5812. if (!arrow.error && !arrow.aborted) return arrow.node;
  5813. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  5814. if (result.node && !result.error) return result.node;
  5815. if (arrow.node) {
  5816. this.state = arrow.failState;
  5817. return arrow.node;
  5818. }
  5819. if (result.node) {
  5820. this.state = result.failState;
  5821. return result.node;
  5822. }
  5823. throw arrow.error || result.error;
  5824. }
  5825. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  5826. }
  5827. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  5828. if (this.match(18) && this.isLookaheadToken_lt()) {
  5829. subscriptState.optionalChainMember = true;
  5830. if (noCalls) {
  5831. subscriptState.stop = true;
  5832. return base;
  5833. }
  5834. this.next();
  5835. const node = this.startNodeAt(startPos, startLoc);
  5836. node.callee = base;
  5837. node.typeArguments = this.flowParseTypeParameterInstantiation();
  5838. this.expect(10);
  5839. node.arguments = this.parseCallExpressionArguments(11, false);
  5840. node.optional = true;
  5841. return this.finishCallExpression(node, true);
  5842. } else if (!noCalls && this.shouldParseTypes() && this.match(47)) {
  5843. const node = this.startNodeAt(startPos, startLoc);
  5844. node.callee = base;
  5845. const result = this.tryParse(() => {
  5846. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  5847. this.expect(10);
  5848. node.arguments = super.parseCallExpressionArguments(11, false);
  5849. if (subscriptState.optionalChainMember) {
  5850. node.optional = false;
  5851. }
  5852. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  5853. });
  5854. if (result.node) {
  5855. if (result.error) this.state = result.failState;
  5856. return result.node;
  5857. }
  5858. }
  5859. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  5860. }
  5861. parseNewCallee(node) {
  5862. super.parseNewCallee(node);
  5863. let targs = null;
  5864. if (this.shouldParseTypes() && this.match(47)) {
  5865. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  5866. }
  5867. node.typeArguments = targs;
  5868. }
  5869. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  5870. const node = this.startNodeAt(startPos, startLoc);
  5871. this.parseFunctionParams(node);
  5872. if (!this.parseArrow(node)) return;
  5873. return super.parseArrowExpression(node, undefined, true);
  5874. }
  5875. readToken_mult_modulo(code) {
  5876. const next = this.input.charCodeAt(this.state.pos + 1);
  5877. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  5878. this.state.hasFlowComment = false;
  5879. this.state.pos += 2;
  5880. this.nextToken();
  5881. return;
  5882. }
  5883. super.readToken_mult_modulo(code);
  5884. }
  5885. readToken_pipe_amp(code) {
  5886. const next = this.input.charCodeAt(this.state.pos + 1);
  5887. if (code === 124 && next === 125) {
  5888. this.finishOp(9, 2);
  5889. return;
  5890. }
  5891. super.readToken_pipe_amp(code);
  5892. }
  5893. parseTopLevel(file, program) {
  5894. const fileNode = super.parseTopLevel(file, program);
  5895. if (this.state.hasFlowComment) {
  5896. this.raise(FlowErrors.UnterminatedFlowComment, {
  5897. at: this.state.curPosition()
  5898. });
  5899. }
  5900. return fileNode;
  5901. }
  5902. skipBlockComment() {
  5903. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  5904. if (this.state.hasFlowComment) {
  5905. throw this.raise(FlowErrors.NestedFlowComment, {
  5906. at: this.state.startLoc
  5907. });
  5908. }
  5909. this.hasFlowCommentCompletion();
  5910. const commentSkip = this.skipFlowComment();
  5911. if (commentSkip) {
  5912. this.state.pos += commentSkip;
  5913. this.state.hasFlowComment = true;
  5914. }
  5915. return;
  5916. }
  5917. if (this.state.hasFlowComment) {
  5918. const end = this.input.indexOf("*-/", this.state.pos + 2);
  5919. if (end === -1) {
  5920. throw this.raise(Errors.UnterminatedComment, {
  5921. at: this.state.curPosition()
  5922. });
  5923. }
  5924. this.state.pos = end + 2 + 3;
  5925. return;
  5926. }
  5927. return super.skipBlockComment();
  5928. }
  5929. skipFlowComment() {
  5930. const {
  5931. pos
  5932. } = this.state;
  5933. let shiftToFirstNonWhiteSpace = 2;
  5934. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  5935. shiftToFirstNonWhiteSpace++;
  5936. }
  5937. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  5938. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  5939. if (ch2 === 58 && ch3 === 58) {
  5940. return shiftToFirstNonWhiteSpace + 2;
  5941. }
  5942. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  5943. return shiftToFirstNonWhiteSpace + 12;
  5944. }
  5945. if (ch2 === 58 && ch3 !== 58) {
  5946. return shiftToFirstNonWhiteSpace;
  5947. }
  5948. return false;
  5949. }
  5950. hasFlowCommentCompletion() {
  5951. const end = this.input.indexOf("*/", this.state.pos);
  5952. if (end === -1) {
  5953. throw this.raise(Errors.UnterminatedComment, {
  5954. at: this.state.curPosition()
  5955. });
  5956. }
  5957. }
  5958. flowEnumErrorBooleanMemberNotInitialized(loc, {
  5959. enumName,
  5960. memberName
  5961. }) {
  5962. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, {
  5963. at: loc,
  5964. memberName,
  5965. enumName
  5966. });
  5967. }
  5968. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  5969. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, Object.assign({
  5970. at: loc
  5971. }, enumContext));
  5972. }
  5973. flowEnumErrorNumberMemberNotInitialized(loc, {
  5974. enumName,
  5975. memberName
  5976. }) {
  5977. this.raise(FlowErrors.EnumNumberMemberNotInitialized, {
  5978. at: loc,
  5979. enumName,
  5980. memberName
  5981. });
  5982. }
  5983. flowEnumErrorStringMemberInconsistentlyInitailized(node, {
  5984. enumName
  5985. }) {
  5986. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitailized, {
  5987. at: node,
  5988. enumName
  5989. });
  5990. }
  5991. flowEnumMemberInit() {
  5992. const startLoc = this.state.startLoc;
  5993. const endOfInit = () => this.match(12) || this.match(8);
  5994. switch (this.state.type) {
  5995. case 130:
  5996. {
  5997. const literal = this.parseNumericLiteral(this.state.value);
  5998. if (endOfInit()) {
  5999. return {
  6000. type: "number",
  6001. loc: literal.loc.start,
  6002. value: literal
  6003. };
  6004. }
  6005. return {
  6006. type: "invalid",
  6007. loc: startLoc
  6008. };
  6009. }
  6010. case 129:
  6011. {
  6012. const literal = this.parseStringLiteral(this.state.value);
  6013. if (endOfInit()) {
  6014. return {
  6015. type: "string",
  6016. loc: literal.loc.start,
  6017. value: literal
  6018. };
  6019. }
  6020. return {
  6021. type: "invalid",
  6022. loc: startLoc
  6023. };
  6024. }
  6025. case 85:
  6026. case 86:
  6027. {
  6028. const literal = this.parseBooleanLiteral(this.match(85));
  6029. if (endOfInit()) {
  6030. return {
  6031. type: "boolean",
  6032. loc: literal.loc.start,
  6033. value: literal
  6034. };
  6035. }
  6036. return {
  6037. type: "invalid",
  6038. loc: startLoc
  6039. };
  6040. }
  6041. default:
  6042. return {
  6043. type: "invalid",
  6044. loc: startLoc
  6045. };
  6046. }
  6047. }
  6048. flowEnumMemberRaw() {
  6049. const loc = this.state.startLoc;
  6050. const id = this.parseIdentifier(true);
  6051. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  6052. type: "none",
  6053. loc
  6054. };
  6055. return {
  6056. id,
  6057. init
  6058. };
  6059. }
  6060. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  6061. const {
  6062. explicitType
  6063. } = context;
  6064. if (explicitType === null) {
  6065. return;
  6066. }
  6067. if (explicitType !== expectedType) {
  6068. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  6069. }
  6070. }
  6071. flowEnumMembers({
  6072. enumName,
  6073. explicitType
  6074. }) {
  6075. const seenNames = new Set();
  6076. const members = {
  6077. booleanMembers: [],
  6078. numberMembers: [],
  6079. stringMembers: [],
  6080. defaultedMembers: []
  6081. };
  6082. let hasUnknownMembers = false;
  6083. while (!this.match(8)) {
  6084. if (this.eat(21)) {
  6085. hasUnknownMembers = true;
  6086. break;
  6087. }
  6088. const memberNode = this.startNode();
  6089. const {
  6090. id,
  6091. init
  6092. } = this.flowEnumMemberRaw();
  6093. const memberName = id.name;
  6094. if (memberName === "") {
  6095. continue;
  6096. }
  6097. if (/^[a-z]/.test(memberName)) {
  6098. this.raise(FlowErrors.EnumInvalidMemberName, {
  6099. at: id,
  6100. memberName,
  6101. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  6102. enumName
  6103. });
  6104. }
  6105. if (seenNames.has(memberName)) {
  6106. this.raise(FlowErrors.EnumDuplicateMemberName, {
  6107. at: id,
  6108. memberName,
  6109. enumName
  6110. });
  6111. }
  6112. seenNames.add(memberName);
  6113. const context = {
  6114. enumName,
  6115. explicitType,
  6116. memberName
  6117. };
  6118. memberNode.id = id;
  6119. switch (init.type) {
  6120. case "boolean":
  6121. {
  6122. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  6123. memberNode.init = init.value;
  6124. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  6125. break;
  6126. }
  6127. case "number":
  6128. {
  6129. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  6130. memberNode.init = init.value;
  6131. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  6132. break;
  6133. }
  6134. case "string":
  6135. {
  6136. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  6137. memberNode.init = init.value;
  6138. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  6139. break;
  6140. }
  6141. case "invalid":
  6142. {
  6143. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  6144. }
  6145. case "none":
  6146. {
  6147. switch (explicitType) {
  6148. case "boolean":
  6149. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  6150. break;
  6151. case "number":
  6152. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  6153. break;
  6154. default:
  6155. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  6156. }
  6157. }
  6158. }
  6159. if (!this.match(8)) {
  6160. this.expect(12);
  6161. }
  6162. }
  6163. return {
  6164. members,
  6165. hasUnknownMembers
  6166. };
  6167. }
  6168. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  6169. enumName
  6170. }) {
  6171. if (initializedMembers.length === 0) {
  6172. return defaultedMembers;
  6173. } else if (defaultedMembers.length === 0) {
  6174. return initializedMembers;
  6175. } else if (defaultedMembers.length > initializedMembers.length) {
  6176. for (const member of initializedMembers) {
  6177. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6178. enumName
  6179. });
  6180. }
  6181. return defaultedMembers;
  6182. } else {
  6183. for (const member of defaultedMembers) {
  6184. this.flowEnumErrorStringMemberInconsistentlyInitailized(member, {
  6185. enumName
  6186. });
  6187. }
  6188. return initializedMembers;
  6189. }
  6190. }
  6191. flowEnumParseExplicitType({
  6192. enumName
  6193. }) {
  6194. if (!this.eatContextual(101)) return null;
  6195. if (!tokenIsIdentifier(this.state.type)) {
  6196. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, {
  6197. at: this.state.startLoc,
  6198. enumName
  6199. });
  6200. }
  6201. const {
  6202. value
  6203. } = this.state;
  6204. this.next();
  6205. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  6206. this.raise(FlowErrors.EnumInvalidExplicitType, {
  6207. at: this.state.startLoc,
  6208. enumName,
  6209. invalidEnumType: value
  6210. });
  6211. }
  6212. return value;
  6213. }
  6214. flowEnumBody(node, id) {
  6215. const enumName = id.name;
  6216. const nameLoc = id.loc.start;
  6217. const explicitType = this.flowEnumParseExplicitType({
  6218. enumName
  6219. });
  6220. this.expect(5);
  6221. const {
  6222. members,
  6223. hasUnknownMembers
  6224. } = this.flowEnumMembers({
  6225. enumName,
  6226. explicitType
  6227. });
  6228. node.hasUnknownMembers = hasUnknownMembers;
  6229. switch (explicitType) {
  6230. case "boolean":
  6231. node.explicitType = true;
  6232. node.members = members.booleanMembers;
  6233. this.expect(8);
  6234. return this.finishNode(node, "EnumBooleanBody");
  6235. case "number":
  6236. node.explicitType = true;
  6237. node.members = members.numberMembers;
  6238. this.expect(8);
  6239. return this.finishNode(node, "EnumNumberBody");
  6240. case "string":
  6241. node.explicitType = true;
  6242. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6243. enumName
  6244. });
  6245. this.expect(8);
  6246. return this.finishNode(node, "EnumStringBody");
  6247. case "symbol":
  6248. node.members = members.defaultedMembers;
  6249. this.expect(8);
  6250. return this.finishNode(node, "EnumSymbolBody");
  6251. default:
  6252. {
  6253. const empty = () => {
  6254. node.members = [];
  6255. this.expect(8);
  6256. return this.finishNode(node, "EnumStringBody");
  6257. };
  6258. node.explicitType = false;
  6259. const boolsLen = members.booleanMembers.length;
  6260. const numsLen = members.numberMembers.length;
  6261. const strsLen = members.stringMembers.length;
  6262. const defaultedLen = members.defaultedMembers.length;
  6263. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  6264. return empty();
  6265. } else if (!boolsLen && !numsLen) {
  6266. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  6267. enumName
  6268. });
  6269. this.expect(8);
  6270. return this.finishNode(node, "EnumStringBody");
  6271. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  6272. for (const member of members.defaultedMembers) {
  6273. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  6274. enumName,
  6275. memberName: member.id.name
  6276. });
  6277. }
  6278. node.members = members.booleanMembers;
  6279. this.expect(8);
  6280. return this.finishNode(node, "EnumBooleanBody");
  6281. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  6282. for (const member of members.defaultedMembers) {
  6283. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  6284. enumName,
  6285. memberName: member.id.name
  6286. });
  6287. }
  6288. node.members = members.numberMembers;
  6289. this.expect(8);
  6290. return this.finishNode(node, "EnumNumberBody");
  6291. } else {
  6292. this.raise(FlowErrors.EnumInconsistentMemberValues, {
  6293. at: nameLoc,
  6294. enumName
  6295. });
  6296. return empty();
  6297. }
  6298. }
  6299. }
  6300. }
  6301. flowParseEnumDeclaration(node) {
  6302. const id = this.parseIdentifier();
  6303. node.id = id;
  6304. node.body = this.flowEnumBody(this.startNode(), id);
  6305. return this.finishNode(node, "EnumDeclaration");
  6306. }
  6307. isLookaheadToken_lt() {
  6308. const next = this.nextTokenStart();
  6309. if (this.input.charCodeAt(next) === 60) {
  6310. const afterNext = this.input.charCodeAt(next + 1);
  6311. return afterNext !== 60 && afterNext !== 61;
  6312. }
  6313. return false;
  6314. }
  6315. maybeUnwrapTypeCastExpression(node) {
  6316. return node.type === "TypeCastExpression" ? node.expression : node;
  6317. }
  6318. });
  6319. const entities = {
  6320. __proto__: null,
  6321. quot: "\u0022",
  6322. amp: "&",
  6323. apos: "\u0027",
  6324. lt: "<",
  6325. gt: ">",
  6326. nbsp: "\u00A0",
  6327. iexcl: "\u00A1",
  6328. cent: "\u00A2",
  6329. pound: "\u00A3",
  6330. curren: "\u00A4",
  6331. yen: "\u00A5",
  6332. brvbar: "\u00A6",
  6333. sect: "\u00A7",
  6334. uml: "\u00A8",
  6335. copy: "\u00A9",
  6336. ordf: "\u00AA",
  6337. laquo: "\u00AB",
  6338. not: "\u00AC",
  6339. shy: "\u00AD",
  6340. reg: "\u00AE",
  6341. macr: "\u00AF",
  6342. deg: "\u00B0",
  6343. plusmn: "\u00B1",
  6344. sup2: "\u00B2",
  6345. sup3: "\u00B3",
  6346. acute: "\u00B4",
  6347. micro: "\u00B5",
  6348. para: "\u00B6",
  6349. middot: "\u00B7",
  6350. cedil: "\u00B8",
  6351. sup1: "\u00B9",
  6352. ordm: "\u00BA",
  6353. raquo: "\u00BB",
  6354. frac14: "\u00BC",
  6355. frac12: "\u00BD",
  6356. frac34: "\u00BE",
  6357. iquest: "\u00BF",
  6358. Agrave: "\u00C0",
  6359. Aacute: "\u00C1",
  6360. Acirc: "\u00C2",
  6361. Atilde: "\u00C3",
  6362. Auml: "\u00C4",
  6363. Aring: "\u00C5",
  6364. AElig: "\u00C6",
  6365. Ccedil: "\u00C7",
  6366. Egrave: "\u00C8",
  6367. Eacute: "\u00C9",
  6368. Ecirc: "\u00CA",
  6369. Euml: "\u00CB",
  6370. Igrave: "\u00CC",
  6371. Iacute: "\u00CD",
  6372. Icirc: "\u00CE",
  6373. Iuml: "\u00CF",
  6374. ETH: "\u00D0",
  6375. Ntilde: "\u00D1",
  6376. Ograve: "\u00D2",
  6377. Oacute: "\u00D3",
  6378. Ocirc: "\u00D4",
  6379. Otilde: "\u00D5",
  6380. Ouml: "\u00D6",
  6381. times: "\u00D7",
  6382. Oslash: "\u00D8",
  6383. Ugrave: "\u00D9",
  6384. Uacute: "\u00DA",
  6385. Ucirc: "\u00DB",
  6386. Uuml: "\u00DC",
  6387. Yacute: "\u00DD",
  6388. THORN: "\u00DE",
  6389. szlig: "\u00DF",
  6390. agrave: "\u00E0",
  6391. aacute: "\u00E1",
  6392. acirc: "\u00E2",
  6393. atilde: "\u00E3",
  6394. auml: "\u00E4",
  6395. aring: "\u00E5",
  6396. aelig: "\u00E6",
  6397. ccedil: "\u00E7",
  6398. egrave: "\u00E8",
  6399. eacute: "\u00E9",
  6400. ecirc: "\u00EA",
  6401. euml: "\u00EB",
  6402. igrave: "\u00EC",
  6403. iacute: "\u00ED",
  6404. icirc: "\u00EE",
  6405. iuml: "\u00EF",
  6406. eth: "\u00F0",
  6407. ntilde: "\u00F1",
  6408. ograve: "\u00F2",
  6409. oacute: "\u00F3",
  6410. ocirc: "\u00F4",
  6411. otilde: "\u00F5",
  6412. ouml: "\u00F6",
  6413. divide: "\u00F7",
  6414. oslash: "\u00F8",
  6415. ugrave: "\u00F9",
  6416. uacute: "\u00FA",
  6417. ucirc: "\u00FB",
  6418. uuml: "\u00FC",
  6419. yacute: "\u00FD",
  6420. thorn: "\u00FE",
  6421. yuml: "\u00FF",
  6422. OElig: "\u0152",
  6423. oelig: "\u0153",
  6424. Scaron: "\u0160",
  6425. scaron: "\u0161",
  6426. Yuml: "\u0178",
  6427. fnof: "\u0192",
  6428. circ: "\u02C6",
  6429. tilde: "\u02DC",
  6430. Alpha: "\u0391",
  6431. Beta: "\u0392",
  6432. Gamma: "\u0393",
  6433. Delta: "\u0394",
  6434. Epsilon: "\u0395",
  6435. Zeta: "\u0396",
  6436. Eta: "\u0397",
  6437. Theta: "\u0398",
  6438. Iota: "\u0399",
  6439. Kappa: "\u039A",
  6440. Lambda: "\u039B",
  6441. Mu: "\u039C",
  6442. Nu: "\u039D",
  6443. Xi: "\u039E",
  6444. Omicron: "\u039F",
  6445. Pi: "\u03A0",
  6446. Rho: "\u03A1",
  6447. Sigma: "\u03A3",
  6448. Tau: "\u03A4",
  6449. Upsilon: "\u03A5",
  6450. Phi: "\u03A6",
  6451. Chi: "\u03A7",
  6452. Psi: "\u03A8",
  6453. Omega: "\u03A9",
  6454. alpha: "\u03B1",
  6455. beta: "\u03B2",
  6456. gamma: "\u03B3",
  6457. delta: "\u03B4",
  6458. epsilon: "\u03B5",
  6459. zeta: "\u03B6",
  6460. eta: "\u03B7",
  6461. theta: "\u03B8",
  6462. iota: "\u03B9",
  6463. kappa: "\u03BA",
  6464. lambda: "\u03BB",
  6465. mu: "\u03BC",
  6466. nu: "\u03BD",
  6467. xi: "\u03BE",
  6468. omicron: "\u03BF",
  6469. pi: "\u03C0",
  6470. rho: "\u03C1",
  6471. sigmaf: "\u03C2",
  6472. sigma: "\u03C3",
  6473. tau: "\u03C4",
  6474. upsilon: "\u03C5",
  6475. phi: "\u03C6",
  6476. chi: "\u03C7",
  6477. psi: "\u03C8",
  6478. omega: "\u03C9",
  6479. thetasym: "\u03D1",
  6480. upsih: "\u03D2",
  6481. piv: "\u03D6",
  6482. ensp: "\u2002",
  6483. emsp: "\u2003",
  6484. thinsp: "\u2009",
  6485. zwnj: "\u200C",
  6486. zwj: "\u200D",
  6487. lrm: "\u200E",
  6488. rlm: "\u200F",
  6489. ndash: "\u2013",
  6490. mdash: "\u2014",
  6491. lsquo: "\u2018",
  6492. rsquo: "\u2019",
  6493. sbquo: "\u201A",
  6494. ldquo: "\u201C",
  6495. rdquo: "\u201D",
  6496. bdquo: "\u201E",
  6497. dagger: "\u2020",
  6498. Dagger: "\u2021",
  6499. bull: "\u2022",
  6500. hellip: "\u2026",
  6501. permil: "\u2030",
  6502. prime: "\u2032",
  6503. Prime: "\u2033",
  6504. lsaquo: "\u2039",
  6505. rsaquo: "\u203A",
  6506. oline: "\u203E",
  6507. frasl: "\u2044",
  6508. euro: "\u20AC",
  6509. image: "\u2111",
  6510. weierp: "\u2118",
  6511. real: "\u211C",
  6512. trade: "\u2122",
  6513. alefsym: "\u2135",
  6514. larr: "\u2190",
  6515. uarr: "\u2191",
  6516. rarr: "\u2192",
  6517. darr: "\u2193",
  6518. harr: "\u2194",
  6519. crarr: "\u21B5",
  6520. lArr: "\u21D0",
  6521. uArr: "\u21D1",
  6522. rArr: "\u21D2",
  6523. dArr: "\u21D3",
  6524. hArr: "\u21D4",
  6525. forall: "\u2200",
  6526. part: "\u2202",
  6527. exist: "\u2203",
  6528. empty: "\u2205",
  6529. nabla: "\u2207",
  6530. isin: "\u2208",
  6531. notin: "\u2209",
  6532. ni: "\u220B",
  6533. prod: "\u220F",
  6534. sum: "\u2211",
  6535. minus: "\u2212",
  6536. lowast: "\u2217",
  6537. radic: "\u221A",
  6538. prop: "\u221D",
  6539. infin: "\u221E",
  6540. ang: "\u2220",
  6541. and: "\u2227",
  6542. or: "\u2228",
  6543. cap: "\u2229",
  6544. cup: "\u222A",
  6545. int: "\u222B",
  6546. there4: "\u2234",
  6547. sim: "\u223C",
  6548. cong: "\u2245",
  6549. asymp: "\u2248",
  6550. ne: "\u2260",
  6551. equiv: "\u2261",
  6552. le: "\u2264",
  6553. ge: "\u2265",
  6554. sub: "\u2282",
  6555. sup: "\u2283",
  6556. nsub: "\u2284",
  6557. sube: "\u2286",
  6558. supe: "\u2287",
  6559. oplus: "\u2295",
  6560. otimes: "\u2297",
  6561. perp: "\u22A5",
  6562. sdot: "\u22C5",
  6563. lceil: "\u2308",
  6564. rceil: "\u2309",
  6565. lfloor: "\u230A",
  6566. rfloor: "\u230B",
  6567. lang: "\u2329",
  6568. rang: "\u232A",
  6569. loz: "\u25CA",
  6570. spades: "\u2660",
  6571. clubs: "\u2663",
  6572. hearts: "\u2665",
  6573. diams: "\u2666"
  6574. };
  6575. const JsxErrors = ParseErrorEnum`jsx`({
  6576. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  6577. MissingClosingTagElement: ({
  6578. openingTagName
  6579. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  6580. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  6581. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  6582. UnexpectedToken: ({
  6583. unexpected,
  6584. HTMLEntity
  6585. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  6586. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  6587. UnterminatedJsxContent: "Unterminated JSX contents.",
  6588. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  6589. });
  6590. function isFragment(object) {
  6591. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  6592. }
  6593. function getQualifiedJSXName(object) {
  6594. if (object.type === "JSXIdentifier") {
  6595. return object.name;
  6596. }
  6597. if (object.type === "JSXNamespacedName") {
  6598. return object.namespace.name + ":" + object.name.name;
  6599. }
  6600. if (object.type === "JSXMemberExpression") {
  6601. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  6602. }
  6603. throw new Error("Node had unexpected type: " + object.type);
  6604. }
  6605. var jsx = (superClass => class JSXParserMixin extends superClass {
  6606. jsxReadToken() {
  6607. let out = "";
  6608. let chunkStart = this.state.pos;
  6609. for (;;) {
  6610. if (this.state.pos >= this.length) {
  6611. throw this.raise(JsxErrors.UnterminatedJsxContent, {
  6612. at: this.state.startLoc
  6613. });
  6614. }
  6615. const ch = this.input.charCodeAt(this.state.pos);
  6616. switch (ch) {
  6617. case 60:
  6618. case 123:
  6619. if (this.state.pos === this.state.start) {
  6620. if (ch === 60 && this.state.canStartJSXElement) {
  6621. ++this.state.pos;
  6622. return this.finishToken(138);
  6623. }
  6624. return super.getTokenFromCode(ch);
  6625. }
  6626. out += this.input.slice(chunkStart, this.state.pos);
  6627. return this.finishToken(137, out);
  6628. case 38:
  6629. out += this.input.slice(chunkStart, this.state.pos);
  6630. out += this.jsxReadEntity();
  6631. chunkStart = this.state.pos;
  6632. break;
  6633. case 62:
  6634. case 125:
  6635. default:
  6636. if (isNewLine(ch)) {
  6637. out += this.input.slice(chunkStart, this.state.pos);
  6638. out += this.jsxReadNewLine(true);
  6639. chunkStart = this.state.pos;
  6640. } else {
  6641. ++this.state.pos;
  6642. }
  6643. }
  6644. }
  6645. }
  6646. jsxReadNewLine(normalizeCRLF) {
  6647. const ch = this.input.charCodeAt(this.state.pos);
  6648. let out;
  6649. ++this.state.pos;
  6650. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  6651. ++this.state.pos;
  6652. out = normalizeCRLF ? "\n" : "\r\n";
  6653. } else {
  6654. out = String.fromCharCode(ch);
  6655. }
  6656. ++this.state.curLine;
  6657. this.state.lineStart = this.state.pos;
  6658. return out;
  6659. }
  6660. jsxReadString(quote) {
  6661. let out = "";
  6662. let chunkStart = ++this.state.pos;
  6663. for (;;) {
  6664. if (this.state.pos >= this.length) {
  6665. throw this.raise(Errors.UnterminatedString, {
  6666. at: this.state.startLoc
  6667. });
  6668. }
  6669. const ch = this.input.charCodeAt(this.state.pos);
  6670. if (ch === quote) break;
  6671. if (ch === 38) {
  6672. out += this.input.slice(chunkStart, this.state.pos);
  6673. out += this.jsxReadEntity();
  6674. chunkStart = this.state.pos;
  6675. } else if (isNewLine(ch)) {
  6676. out += this.input.slice(chunkStart, this.state.pos);
  6677. out += this.jsxReadNewLine(false);
  6678. chunkStart = this.state.pos;
  6679. } else {
  6680. ++this.state.pos;
  6681. }
  6682. }
  6683. out += this.input.slice(chunkStart, this.state.pos++);
  6684. return this.finishToken(129, out);
  6685. }
  6686. jsxReadEntity() {
  6687. const startPos = ++this.state.pos;
  6688. if (this.codePointAtPos(this.state.pos) === 35) {
  6689. ++this.state.pos;
  6690. let radix = 10;
  6691. if (this.codePointAtPos(this.state.pos) === 120) {
  6692. radix = 16;
  6693. ++this.state.pos;
  6694. }
  6695. const codePoint = this.readInt(radix, undefined, false, "bail");
  6696. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  6697. ++this.state.pos;
  6698. return String.fromCodePoint(codePoint);
  6699. }
  6700. } else {
  6701. let count = 0;
  6702. let semi = false;
  6703. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) == 59)) {
  6704. ++this.state.pos;
  6705. }
  6706. if (semi) {
  6707. const desc = this.input.slice(startPos, this.state.pos);
  6708. const entity = entities[desc];
  6709. ++this.state.pos;
  6710. if (entity) {
  6711. return entity;
  6712. }
  6713. }
  6714. }
  6715. this.state.pos = startPos;
  6716. return "&";
  6717. }
  6718. jsxReadWord() {
  6719. let ch;
  6720. const start = this.state.pos;
  6721. do {
  6722. ch = this.input.charCodeAt(++this.state.pos);
  6723. } while (isIdentifierChar(ch) || ch === 45);
  6724. return this.finishToken(136, this.input.slice(start, this.state.pos));
  6725. }
  6726. jsxParseIdentifier() {
  6727. const node = this.startNode();
  6728. if (this.match(136)) {
  6729. node.name = this.state.value;
  6730. } else if (tokenIsKeyword(this.state.type)) {
  6731. node.name = tokenLabelName(this.state.type);
  6732. } else {
  6733. this.unexpected();
  6734. }
  6735. this.next();
  6736. return this.finishNode(node, "JSXIdentifier");
  6737. }
  6738. jsxParseNamespacedName() {
  6739. const startPos = this.state.start;
  6740. const startLoc = this.state.startLoc;
  6741. const name = this.jsxParseIdentifier();
  6742. if (!this.eat(14)) return name;
  6743. const node = this.startNodeAt(startPos, startLoc);
  6744. node.namespace = name;
  6745. node.name = this.jsxParseIdentifier();
  6746. return this.finishNode(node, "JSXNamespacedName");
  6747. }
  6748. jsxParseElementName() {
  6749. const startPos = this.state.start;
  6750. const startLoc = this.state.startLoc;
  6751. let node = this.jsxParseNamespacedName();
  6752. if (node.type === "JSXNamespacedName") {
  6753. return node;
  6754. }
  6755. while (this.eat(16)) {
  6756. const newNode = this.startNodeAt(startPos, startLoc);
  6757. newNode.object = node;
  6758. newNode.property = this.jsxParseIdentifier();
  6759. node = this.finishNode(newNode, "JSXMemberExpression");
  6760. }
  6761. return node;
  6762. }
  6763. jsxParseAttributeValue() {
  6764. let node;
  6765. switch (this.state.type) {
  6766. case 5:
  6767. node = this.startNode();
  6768. this.setContext(types.brace);
  6769. this.next();
  6770. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  6771. if (node.expression.type === "JSXEmptyExpression") {
  6772. this.raise(JsxErrors.AttributeIsEmpty, {
  6773. at: node
  6774. });
  6775. }
  6776. return node;
  6777. case 138:
  6778. case 129:
  6779. return this.parseExprAtom();
  6780. default:
  6781. throw this.raise(JsxErrors.UnsupportedJsxValue, {
  6782. at: this.state.startLoc
  6783. });
  6784. }
  6785. }
  6786. jsxParseEmptyExpression() {
  6787. const node = this.startNodeAt(this.state.lastTokEndLoc.index, this.state.lastTokEndLoc);
  6788. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  6789. }
  6790. jsxParseSpreadChild(node) {
  6791. this.next();
  6792. node.expression = this.parseExpression();
  6793. this.setContext(types.j_expr);
  6794. this.state.canStartJSXElement = true;
  6795. this.expect(8);
  6796. return this.finishNode(node, "JSXSpreadChild");
  6797. }
  6798. jsxParseExpressionContainer(node, previousContext) {
  6799. if (this.match(8)) {
  6800. node.expression = this.jsxParseEmptyExpression();
  6801. } else {
  6802. const expression = this.parseExpression();
  6803. node.expression = expression;
  6804. }
  6805. this.setContext(previousContext);
  6806. this.state.canStartJSXElement = true;
  6807. this.expect(8);
  6808. return this.finishNode(node, "JSXExpressionContainer");
  6809. }
  6810. jsxParseAttribute() {
  6811. const node = this.startNode();
  6812. if (this.match(5)) {
  6813. this.setContext(types.brace);
  6814. this.next();
  6815. this.expect(21);
  6816. node.argument = this.parseMaybeAssignAllowIn();
  6817. this.setContext(types.j_oTag);
  6818. this.state.canStartJSXElement = true;
  6819. this.expect(8);
  6820. return this.finishNode(node, "JSXSpreadAttribute");
  6821. }
  6822. node.name = this.jsxParseNamespacedName();
  6823. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  6824. return this.finishNode(node, "JSXAttribute");
  6825. }
  6826. jsxParseOpeningElementAt(startPos, startLoc) {
  6827. const node = this.startNodeAt(startPos, startLoc);
  6828. if (this.eat(139)) {
  6829. return this.finishNode(node, "JSXOpeningFragment");
  6830. }
  6831. node.name = this.jsxParseElementName();
  6832. return this.jsxParseOpeningElementAfterName(node);
  6833. }
  6834. jsxParseOpeningElementAfterName(node) {
  6835. const attributes = [];
  6836. while (!this.match(56) && !this.match(139)) {
  6837. attributes.push(this.jsxParseAttribute());
  6838. }
  6839. node.attributes = attributes;
  6840. node.selfClosing = this.eat(56);
  6841. this.expect(139);
  6842. return this.finishNode(node, "JSXOpeningElement");
  6843. }
  6844. jsxParseClosingElementAt(startPos, startLoc) {
  6845. const node = this.startNodeAt(startPos, startLoc);
  6846. if (this.eat(139)) {
  6847. return this.finishNode(node, "JSXClosingFragment");
  6848. }
  6849. node.name = this.jsxParseElementName();
  6850. this.expect(139);
  6851. return this.finishNode(node, "JSXClosingElement");
  6852. }
  6853. jsxParseElementAt(startPos, startLoc) {
  6854. const node = this.startNodeAt(startPos, startLoc);
  6855. const children = [];
  6856. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  6857. let closingElement = null;
  6858. if (!openingElement.selfClosing) {
  6859. contents: for (;;) {
  6860. switch (this.state.type) {
  6861. case 138:
  6862. startPos = this.state.start;
  6863. startLoc = this.state.startLoc;
  6864. this.next();
  6865. if (this.eat(56)) {
  6866. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  6867. break contents;
  6868. }
  6869. children.push(this.jsxParseElementAt(startPos, startLoc));
  6870. break;
  6871. case 137:
  6872. children.push(this.parseExprAtom());
  6873. break;
  6874. case 5:
  6875. {
  6876. const node = this.startNode();
  6877. this.setContext(types.brace);
  6878. this.next();
  6879. if (this.match(21)) {
  6880. children.push(this.jsxParseSpreadChild(node));
  6881. } else {
  6882. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  6883. }
  6884. break;
  6885. }
  6886. default:
  6887. throw this.unexpected();
  6888. }
  6889. }
  6890. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  6891. this.raise(JsxErrors.MissingClosingTagFragment, {
  6892. at: closingElement
  6893. });
  6894. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  6895. this.raise(JsxErrors.MissingClosingTagElement, {
  6896. at: closingElement,
  6897. openingTagName: getQualifiedJSXName(openingElement.name)
  6898. });
  6899. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  6900. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  6901. this.raise(JsxErrors.MissingClosingTagElement, {
  6902. at: closingElement,
  6903. openingTagName: getQualifiedJSXName(openingElement.name)
  6904. });
  6905. }
  6906. }
  6907. }
  6908. if (isFragment(openingElement)) {
  6909. node.openingFragment = openingElement;
  6910. node.closingFragment = closingElement;
  6911. } else {
  6912. node.openingElement = openingElement;
  6913. node.closingElement = closingElement;
  6914. }
  6915. node.children = children;
  6916. if (this.match(47)) {
  6917. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, {
  6918. at: this.state.startLoc
  6919. });
  6920. }
  6921. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  6922. }
  6923. jsxParseElement() {
  6924. const startPos = this.state.start;
  6925. const startLoc = this.state.startLoc;
  6926. this.next();
  6927. return this.jsxParseElementAt(startPos, startLoc);
  6928. }
  6929. setContext(newContext) {
  6930. const {
  6931. context
  6932. } = this.state;
  6933. context[context.length - 1] = newContext;
  6934. }
  6935. parseExprAtom(refExpressionErrors) {
  6936. if (this.match(137)) {
  6937. return this.parseLiteral(this.state.value, "JSXText");
  6938. } else if (this.match(138)) {
  6939. return this.jsxParseElement();
  6940. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  6941. this.replaceToken(138);
  6942. return this.jsxParseElement();
  6943. } else {
  6944. return super.parseExprAtom(refExpressionErrors);
  6945. }
  6946. }
  6947. skipSpace() {
  6948. const curContext = this.curContext();
  6949. if (!curContext.preserveSpace) super.skipSpace();
  6950. }
  6951. getTokenFromCode(code) {
  6952. const context = this.curContext();
  6953. if (context === types.j_expr) {
  6954. return this.jsxReadToken();
  6955. }
  6956. if (context === types.j_oTag || context === types.j_cTag) {
  6957. if (isIdentifierStart(code)) {
  6958. return this.jsxReadWord();
  6959. }
  6960. if (code === 62) {
  6961. ++this.state.pos;
  6962. return this.finishToken(139);
  6963. }
  6964. if ((code === 34 || code === 39) && context === types.j_oTag) {
  6965. return this.jsxReadString(code);
  6966. }
  6967. }
  6968. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6969. ++this.state.pos;
  6970. return this.finishToken(138);
  6971. }
  6972. return super.getTokenFromCode(code);
  6973. }
  6974. updateContext(prevType) {
  6975. const {
  6976. context,
  6977. type
  6978. } = this.state;
  6979. if (type === 56 && prevType === 138) {
  6980. context.splice(-2, 2, types.j_cTag);
  6981. this.state.canStartJSXElement = false;
  6982. } else if (type === 138) {
  6983. context.push(types.j_oTag);
  6984. } else if (type === 139) {
  6985. const out = context[context.length - 1];
  6986. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  6987. context.pop();
  6988. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  6989. } else {
  6990. this.setContext(types.j_expr);
  6991. this.state.canStartJSXElement = true;
  6992. }
  6993. } else {
  6994. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  6995. }
  6996. }
  6997. });
  6998. class TypeScriptScope extends Scope {
  6999. constructor(...args) {
  7000. super(...args);
  7001. this.types = new Set();
  7002. this.enums = new Set();
  7003. this.constEnums = new Set();
  7004. this.classes = new Set();
  7005. this.exportOnlyBindings = new Set();
  7006. }
  7007. }
  7008. class TypeScriptScopeHandler extends ScopeHandler {
  7009. constructor(...args) {
  7010. super(...args);
  7011. this.importsStack = [];
  7012. }
  7013. createScope(flags) {
  7014. this.importsStack.push(new Set());
  7015. return new TypeScriptScope(flags);
  7016. }
  7017. enter(flags) {
  7018. if (flags == SCOPE_TS_MODULE) {
  7019. this.importsStack.push(new Set());
  7020. }
  7021. super.enter(flags);
  7022. }
  7023. exit() {
  7024. const flags = super.exit();
  7025. if (flags == SCOPE_TS_MODULE) {
  7026. this.importsStack.pop();
  7027. }
  7028. return flags;
  7029. }
  7030. hasImport(name, allowShadow) {
  7031. const len = this.importsStack.length;
  7032. if (this.importsStack[len - 1].has(name)) {
  7033. return true;
  7034. }
  7035. if (!allowShadow && len > 1) {
  7036. for (let i = 0; i < len - 1; i++) {
  7037. if (this.importsStack[i].has(name)) return true;
  7038. }
  7039. }
  7040. return false;
  7041. }
  7042. declareName(name, bindingType, loc) {
  7043. if (bindingType & BIND_FLAGS_TS_IMPORT) {
  7044. if (this.hasImport(name, true)) {
  7045. this.parser.raise(Errors.VarRedeclaration, {
  7046. at: loc,
  7047. identifierName: name
  7048. });
  7049. }
  7050. this.importsStack[this.importsStack.length - 1].add(name);
  7051. return;
  7052. }
  7053. const scope = this.currentScope();
  7054. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  7055. this.maybeExportDefined(scope, name);
  7056. scope.exportOnlyBindings.add(name);
  7057. return;
  7058. }
  7059. super.declareName(name, bindingType, loc);
  7060. if (bindingType & BIND_KIND_TYPE) {
  7061. if (!(bindingType & BIND_KIND_VALUE)) {
  7062. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  7063. this.maybeExportDefined(scope, name);
  7064. }
  7065. scope.types.add(name);
  7066. }
  7067. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  7068. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  7069. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  7070. }
  7071. isRedeclaredInScope(scope, name, bindingType) {
  7072. if (scope.enums.has(name)) {
  7073. if (bindingType & BIND_FLAGS_TS_ENUM) {
  7074. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  7075. const wasConst = scope.constEnums.has(name);
  7076. return isConst !== wasConst;
  7077. }
  7078. return true;
  7079. }
  7080. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  7081. if (scope.lexical.has(name)) {
  7082. return !!(bindingType & BIND_KIND_VALUE);
  7083. } else {
  7084. return false;
  7085. }
  7086. }
  7087. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  7088. return true;
  7089. }
  7090. return super.isRedeclaredInScope(scope, name, bindingType);
  7091. }
  7092. checkLocalExport(id) {
  7093. const {
  7094. name
  7095. } = id;
  7096. if (this.hasImport(name)) return;
  7097. const len = this.scopeStack.length;
  7098. for (let i = len - 1; i >= 0; i--) {
  7099. const scope = this.scopeStack[i];
  7100. if (scope.types.has(name) || scope.exportOnlyBindings.has(name)) return;
  7101. }
  7102. super.checkLocalExport(id);
  7103. }
  7104. }
  7105. const getOwn$1 = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  7106. function nonNull(x) {
  7107. if (x == null) {
  7108. throw new Error(`Unexpected ${x} value.`);
  7109. }
  7110. return x;
  7111. }
  7112. function assert(x) {
  7113. if (!x) {
  7114. throw new Error("Assert fail");
  7115. }
  7116. }
  7117. const TSErrors = ParseErrorEnum`typescript`({
  7118. AbstractMethodHasImplementation: ({
  7119. methodName
  7120. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7121. AbstractPropertyHasInitializer: ({
  7122. propertyName
  7123. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7124. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7125. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7126. CannotFindName: ({
  7127. name
  7128. }) => `Cannot find name '${name}'.`,
  7129. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7130. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7131. ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7132. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7133. DeclareAccessor: ({
  7134. kind
  7135. }) => `'declare' is not allowed in ${kind}ters.`,
  7136. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7137. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7138. DuplicateAccessibilityModifier: ({
  7139. modifier
  7140. }) => `Accessibility modifier already seen.`,
  7141. DuplicateModifier: ({
  7142. modifier
  7143. }) => `Duplicate modifier: '${modifier}'.`,
  7144. EmptyHeritageClauseType: ({
  7145. token
  7146. }) => `'${token}' list cannot be empty.`,
  7147. EmptyTypeArguments: "Type argument list cannot be empty.",
  7148. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7149. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7150. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7151. IncompatibleModifiers: ({
  7152. modifiers
  7153. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7154. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7155. IndexSignatureHasAccessibility: ({
  7156. modifier
  7157. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7158. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7159. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7160. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7161. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7162. InvalidModifierOnTypeMember: ({
  7163. modifier
  7164. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7165. InvalidModifierOnTypeParameter: ({
  7166. modifier
  7167. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7168. InvalidModifierOnTypeParameterPositions: ({
  7169. modifier
  7170. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7171. InvalidModifiersOrder: ({
  7172. orderedModifiers
  7173. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7174. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7175. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7176. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7177. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  7178. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7179. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7180. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7181. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7182. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7183. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7184. PrivateElementHasAccessibility: ({
  7185. modifier
  7186. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7187. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7188. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7189. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7190. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7191. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7192. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7193. SingleTypeParameterWithoutTrailingComma: ({
  7194. typeParameterName
  7195. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7196. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7197. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7198. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7199. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7200. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7201. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7202. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7203. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7204. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7205. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7206. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7207. UnsupportedSignatureParameterKind: ({
  7208. type
  7209. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`
  7210. });
  7211. function keywordTypeFromName(value) {
  7212. switch (value) {
  7213. case "any":
  7214. return "TSAnyKeyword";
  7215. case "boolean":
  7216. return "TSBooleanKeyword";
  7217. case "bigint":
  7218. return "TSBigIntKeyword";
  7219. case "never":
  7220. return "TSNeverKeyword";
  7221. case "number":
  7222. return "TSNumberKeyword";
  7223. case "object":
  7224. return "TSObjectKeyword";
  7225. case "string":
  7226. return "TSStringKeyword";
  7227. case "symbol":
  7228. return "TSSymbolKeyword";
  7229. case "undefined":
  7230. return "TSUndefinedKeyword";
  7231. case "unknown":
  7232. return "TSUnknownKeyword";
  7233. default:
  7234. return undefined;
  7235. }
  7236. }
  7237. function tsIsAccessModifier(modifier) {
  7238. return modifier === "private" || modifier === "public" || modifier === "protected";
  7239. }
  7240. function tsIsVarianceAnnotations(modifier) {
  7241. return modifier === "in" || modifier === "out";
  7242. }
  7243. var typescript = (superClass => class TypeScriptParserMixin extends superClass {
  7244. getScopeHandler() {
  7245. return TypeScriptScopeHandler;
  7246. }
  7247. tsIsIdentifier() {
  7248. return tokenIsIdentifier(this.state.type);
  7249. }
  7250. tsTokenCanFollowModifier() {
  7251. return (this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(134) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  7252. }
  7253. tsNextTokenCanFollowModifier() {
  7254. this.next();
  7255. return this.tsTokenCanFollowModifier();
  7256. }
  7257. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  7258. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58) {
  7259. return undefined;
  7260. }
  7261. const modifier = this.state.value;
  7262. if (allowedModifiers.indexOf(modifier) !== -1) {
  7263. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7264. return undefined;
  7265. }
  7266. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7267. return modifier;
  7268. }
  7269. }
  7270. return undefined;
  7271. }
  7272. tsParseModifiers({
  7273. modified,
  7274. allowedModifiers,
  7275. disallowedModifiers,
  7276. stopOnStartOfClassStaticBlock,
  7277. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7278. }) {
  7279. const enforceOrder = (loc, modifier, before, after) => {
  7280. if (modifier === before && modified[after]) {
  7281. this.raise(TSErrors.InvalidModifiersOrder, {
  7282. at: loc,
  7283. orderedModifiers: [before, after]
  7284. });
  7285. }
  7286. };
  7287. const incompatible = (loc, modifier, mod1, mod2) => {
  7288. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7289. this.raise(TSErrors.IncompatibleModifiers, {
  7290. at: loc,
  7291. modifiers: [mod1, mod2]
  7292. });
  7293. }
  7294. };
  7295. for (;;) {
  7296. const {
  7297. startLoc
  7298. } = this.state;
  7299. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  7300. if (!modifier) break;
  7301. if (tsIsAccessModifier(modifier)) {
  7302. if (modified.accessibility) {
  7303. this.raise(TSErrors.DuplicateAccessibilityModifier, {
  7304. at: startLoc,
  7305. modifier
  7306. });
  7307. } else {
  7308. enforceOrder(startLoc, modifier, modifier, "override");
  7309. enforceOrder(startLoc, modifier, modifier, "static");
  7310. enforceOrder(startLoc, modifier, modifier, "readonly");
  7311. modified.accessibility = modifier;
  7312. }
  7313. } else if (tsIsVarianceAnnotations(modifier)) {
  7314. if (modified[modifier]) {
  7315. this.raise(TSErrors.DuplicateModifier, {
  7316. at: startLoc,
  7317. modifier
  7318. });
  7319. }
  7320. modified[modifier] = true;
  7321. enforceOrder(startLoc, modifier, "in", "out");
  7322. } else {
  7323. if (Object.hasOwnProperty.call(modified, modifier)) {
  7324. this.raise(TSErrors.DuplicateModifier, {
  7325. at: startLoc,
  7326. modifier
  7327. });
  7328. } else {
  7329. enforceOrder(startLoc, modifier, "static", "readonly");
  7330. enforceOrder(startLoc, modifier, "static", "override");
  7331. enforceOrder(startLoc, modifier, "override", "readonly");
  7332. enforceOrder(startLoc, modifier, "abstract", "override");
  7333. incompatible(startLoc, modifier, "declare", "override");
  7334. incompatible(startLoc, modifier, "static", "abstract");
  7335. }
  7336. modified[modifier] = true;
  7337. }
  7338. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7339. this.raise(errorTemplate, {
  7340. at: startLoc,
  7341. modifier
  7342. });
  7343. }
  7344. }
  7345. }
  7346. tsIsListTerminator(kind) {
  7347. switch (kind) {
  7348. case "EnumMembers":
  7349. case "TypeMembers":
  7350. return this.match(8);
  7351. case "HeritageClauseElement":
  7352. return this.match(5);
  7353. case "TupleElementTypes":
  7354. return this.match(3);
  7355. case "TypeParametersOrArguments":
  7356. return this.match(48);
  7357. }
  7358. throw new Error("Unreachable");
  7359. }
  7360. tsParseList(kind, parseElement) {
  7361. const result = [];
  7362. while (!this.tsIsListTerminator(kind)) {
  7363. result.push(parseElement());
  7364. }
  7365. return result;
  7366. }
  7367. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7368. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7369. }
  7370. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7371. const result = [];
  7372. let trailingCommaPos = -1;
  7373. for (;;) {
  7374. if (this.tsIsListTerminator(kind)) {
  7375. break;
  7376. }
  7377. trailingCommaPos = -1;
  7378. const element = parseElement();
  7379. if (element == null) {
  7380. return undefined;
  7381. }
  7382. result.push(element);
  7383. if (this.eat(12)) {
  7384. trailingCommaPos = this.state.lastTokStart;
  7385. continue;
  7386. }
  7387. if (this.tsIsListTerminator(kind)) {
  7388. break;
  7389. }
  7390. if (expectSuccess) {
  7391. this.expect(12);
  7392. }
  7393. return undefined;
  7394. }
  7395. if (refTrailingCommaPos) {
  7396. refTrailingCommaPos.value = trailingCommaPos;
  7397. }
  7398. return result;
  7399. }
  7400. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7401. if (!skipFirstToken) {
  7402. if (bracket) {
  7403. this.expect(0);
  7404. } else {
  7405. this.expect(47);
  7406. }
  7407. }
  7408. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7409. if (bracket) {
  7410. this.expect(3);
  7411. } else {
  7412. this.expect(48);
  7413. }
  7414. return result;
  7415. }
  7416. tsParseImportType() {
  7417. const node = this.startNode();
  7418. this.expect(83);
  7419. this.expect(10);
  7420. if (!this.match(129)) {
  7421. this.raise(TSErrors.UnsupportedImportTypeArgument, {
  7422. at: this.state.startLoc
  7423. });
  7424. }
  7425. node.argument = super.parseExprAtom();
  7426. this.expect(11);
  7427. if (this.eat(16)) {
  7428. node.qualifier = this.tsParseEntityName();
  7429. }
  7430. if (this.match(47)) {
  7431. node.typeParameters = this.tsParseTypeArguments();
  7432. }
  7433. return this.finishNode(node, "TSImportType");
  7434. }
  7435. tsParseEntityName(allowReservedWords = true) {
  7436. let entity = this.parseIdentifier(allowReservedWords);
  7437. while (this.eat(16)) {
  7438. const node = this.startNodeAtNode(entity);
  7439. node.left = entity;
  7440. node.right = this.parseIdentifier(allowReservedWords);
  7441. entity = this.finishNode(node, "TSQualifiedName");
  7442. }
  7443. return entity;
  7444. }
  7445. tsParseTypeReference() {
  7446. const node = this.startNode();
  7447. node.typeName = this.tsParseEntityName();
  7448. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7449. node.typeParameters = this.tsParseTypeArguments();
  7450. }
  7451. return this.finishNode(node, "TSTypeReference");
  7452. }
  7453. tsParseThisTypePredicate(lhs) {
  7454. this.next();
  7455. const node = this.startNodeAtNode(lhs);
  7456. node.parameterName = lhs;
  7457. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  7458. node.asserts = false;
  7459. return this.finishNode(node, "TSTypePredicate");
  7460. }
  7461. tsParseThisTypeNode() {
  7462. const node = this.startNode();
  7463. this.next();
  7464. return this.finishNode(node, "TSThisType");
  7465. }
  7466. tsParseTypeQuery() {
  7467. const node = this.startNode();
  7468. this.expect(87);
  7469. if (this.match(83)) {
  7470. node.exprName = this.tsParseImportType();
  7471. } else {
  7472. node.exprName = this.tsParseEntityName();
  7473. }
  7474. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  7475. node.typeParameters = this.tsParseTypeArguments();
  7476. }
  7477. return this.finishNode(node, "TSTypeQuery");
  7478. }
  7479. tsParseInOutModifiers(node) {
  7480. this.tsParseModifiers({
  7481. modified: node,
  7482. allowedModifiers: ["in", "out"],
  7483. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7484. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7485. });
  7486. }
  7487. tsParseNoneModifiers(node) {
  7488. this.tsParseModifiers({
  7489. modified: node,
  7490. allowedModifiers: [],
  7491. disallowedModifiers: ["in", "out"],
  7492. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7493. });
  7494. }
  7495. tsParseTypeParameter(parseModifiers = this.tsParseNoneModifiers.bind(this)) {
  7496. const node = this.startNode();
  7497. parseModifiers(node);
  7498. node.name = this.tsParseTypeParameterName();
  7499. node.constraint = this.tsEatThenParseType(81);
  7500. node.default = this.tsEatThenParseType(29);
  7501. return this.finishNode(node, "TSTypeParameter");
  7502. }
  7503. tsTryParseTypeParameters(parseModifiers) {
  7504. if (this.match(47)) {
  7505. return this.tsParseTypeParameters(parseModifiers);
  7506. }
  7507. }
  7508. tsParseTypeParameters(parseModifiers) {
  7509. const node = this.startNode();
  7510. if (this.match(47) || this.match(138)) {
  7511. this.next();
  7512. } else {
  7513. this.unexpected();
  7514. }
  7515. const refTrailingCommaPos = {
  7516. value: -1
  7517. };
  7518. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  7519. if (node.params.length === 0) {
  7520. this.raise(TSErrors.EmptyTypeParameters, {
  7521. at: node
  7522. });
  7523. }
  7524. if (refTrailingCommaPos.value !== -1) {
  7525. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  7526. }
  7527. return this.finishNode(node, "TSTypeParameterDeclaration");
  7528. }
  7529. tsTryNextParseConstantContext() {
  7530. if (this.lookahead().type !== 75) return null;
  7531. this.next();
  7532. const typeReference = this.tsParseTypeReference();
  7533. if (typeReference.typeParameters) {
  7534. this.raise(TSErrors.CannotFindName, {
  7535. at: typeReference.typeName,
  7536. name: "const"
  7537. });
  7538. }
  7539. return typeReference;
  7540. }
  7541. tsFillSignature(returnToken, signature) {
  7542. const returnTokenRequired = returnToken === 19;
  7543. const paramsKey = "parameters";
  7544. const returnTypeKey = "typeAnnotation";
  7545. signature.typeParameters = this.tsTryParseTypeParameters();
  7546. this.expect(10);
  7547. signature[paramsKey] = this.tsParseBindingListForSignature();
  7548. if (returnTokenRequired) {
  7549. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7550. } else if (this.match(returnToken)) {
  7551. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  7552. }
  7553. }
  7554. tsParseBindingListForSignature() {
  7555. return super.parseBindingList(11, 41).map(pattern => {
  7556. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  7557. this.raise(TSErrors.UnsupportedSignatureParameterKind, {
  7558. at: pattern,
  7559. type: pattern.type
  7560. });
  7561. }
  7562. return pattern;
  7563. });
  7564. }
  7565. tsParseTypeMemberSemicolon() {
  7566. if (!this.eat(12) && !this.isLineTerminator()) {
  7567. this.expect(13);
  7568. }
  7569. }
  7570. tsParseSignatureMember(kind, node) {
  7571. this.tsFillSignature(14, node);
  7572. this.tsParseTypeMemberSemicolon();
  7573. return this.finishNode(node, kind);
  7574. }
  7575. tsIsUnambiguouslyIndexSignature() {
  7576. this.next();
  7577. if (tokenIsIdentifier(this.state.type)) {
  7578. this.next();
  7579. return this.match(14);
  7580. }
  7581. return false;
  7582. }
  7583. tsTryParseIndexSignature(node) {
  7584. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  7585. return undefined;
  7586. }
  7587. this.expect(0);
  7588. const id = this.parseIdentifier();
  7589. id.typeAnnotation = this.tsParseTypeAnnotation();
  7590. this.resetEndLocation(id);
  7591. this.expect(3);
  7592. node.parameters = [id];
  7593. const type = this.tsTryParseTypeAnnotation();
  7594. if (type) node.typeAnnotation = type;
  7595. this.tsParseTypeMemberSemicolon();
  7596. return this.finishNode(node, "TSIndexSignature");
  7597. }
  7598. tsParsePropertyOrMethodSignature(node, readonly) {
  7599. if (this.eat(17)) node.optional = true;
  7600. const nodeAny = node;
  7601. if (this.match(10) || this.match(47)) {
  7602. if (readonly) {
  7603. this.raise(TSErrors.ReadonlyForMethodSignature, {
  7604. at: node
  7605. });
  7606. }
  7607. const method = nodeAny;
  7608. if (method.kind && this.match(47)) {
  7609. this.raise(TSErrors.AccesorCannotHaveTypeParameters, {
  7610. at: this.state.curPosition()
  7611. });
  7612. }
  7613. this.tsFillSignature(14, method);
  7614. this.tsParseTypeMemberSemicolon();
  7615. const paramsKey = "parameters";
  7616. const returnTypeKey = "typeAnnotation";
  7617. if (method.kind === "get") {
  7618. if (method[paramsKey].length > 0) {
  7619. this.raise(Errors.BadGetterArity, {
  7620. at: this.state.curPosition()
  7621. });
  7622. if (this.isThisParam(method[paramsKey][0])) {
  7623. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7624. at: this.state.curPosition()
  7625. });
  7626. }
  7627. }
  7628. } else if (method.kind === "set") {
  7629. if (method[paramsKey].length !== 1) {
  7630. this.raise(Errors.BadSetterArity, {
  7631. at: this.state.curPosition()
  7632. });
  7633. } else {
  7634. const firstParameter = method[paramsKey][0];
  7635. if (this.isThisParam(firstParameter)) {
  7636. this.raise(TSErrors.AccesorCannotDeclareThisParameter, {
  7637. at: this.state.curPosition()
  7638. });
  7639. }
  7640. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  7641. this.raise(TSErrors.SetAccesorCannotHaveOptionalParameter, {
  7642. at: this.state.curPosition()
  7643. });
  7644. }
  7645. if (firstParameter.type === "RestElement") {
  7646. this.raise(TSErrors.SetAccesorCannotHaveRestParameter, {
  7647. at: this.state.curPosition()
  7648. });
  7649. }
  7650. }
  7651. if (method[returnTypeKey]) {
  7652. this.raise(TSErrors.SetAccesorCannotHaveReturnType, {
  7653. at: method[returnTypeKey]
  7654. });
  7655. }
  7656. } else {
  7657. method.kind = "method";
  7658. }
  7659. return this.finishNode(method, "TSMethodSignature");
  7660. } else {
  7661. const property = nodeAny;
  7662. if (readonly) property.readonly = true;
  7663. const type = this.tsTryParseTypeAnnotation();
  7664. if (type) property.typeAnnotation = type;
  7665. this.tsParseTypeMemberSemicolon();
  7666. return this.finishNode(property, "TSPropertySignature");
  7667. }
  7668. }
  7669. tsParseTypeMember() {
  7670. const node = this.startNode();
  7671. if (this.match(10) || this.match(47)) {
  7672. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  7673. }
  7674. if (this.match(77)) {
  7675. const id = this.startNode();
  7676. this.next();
  7677. if (this.match(10) || this.match(47)) {
  7678. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  7679. } else {
  7680. node.key = this.createIdentifier(id, "new");
  7681. return this.tsParsePropertyOrMethodSignature(node, false);
  7682. }
  7683. }
  7684. this.tsParseModifiers({
  7685. modified: node,
  7686. allowedModifiers: ["readonly"],
  7687. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  7688. });
  7689. const idx = this.tsTryParseIndexSignature(node);
  7690. if (idx) {
  7691. return idx;
  7692. }
  7693. super.parsePropertyName(node);
  7694. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  7695. node.kind = node.key.name;
  7696. super.parsePropertyName(node);
  7697. }
  7698. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  7699. }
  7700. tsParseTypeLiteral() {
  7701. const node = this.startNode();
  7702. node.members = this.tsParseObjectTypeMembers();
  7703. return this.finishNode(node, "TSTypeLiteral");
  7704. }
  7705. tsParseObjectTypeMembers() {
  7706. this.expect(5);
  7707. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  7708. this.expect(8);
  7709. return members;
  7710. }
  7711. tsIsStartOfMappedType() {
  7712. this.next();
  7713. if (this.eat(53)) {
  7714. return this.isContextual(118);
  7715. }
  7716. if (this.isContextual(118)) {
  7717. this.next();
  7718. }
  7719. if (!this.match(0)) {
  7720. return false;
  7721. }
  7722. this.next();
  7723. if (!this.tsIsIdentifier()) {
  7724. return false;
  7725. }
  7726. this.next();
  7727. return this.match(58);
  7728. }
  7729. tsParseMappedTypeParameter() {
  7730. const node = this.startNode();
  7731. node.name = this.tsParseTypeParameterName();
  7732. node.constraint = this.tsExpectThenParseType(58);
  7733. return this.finishNode(node, "TSTypeParameter");
  7734. }
  7735. tsParseMappedType() {
  7736. const node = this.startNode();
  7737. this.expect(5);
  7738. if (this.match(53)) {
  7739. node.readonly = this.state.value;
  7740. this.next();
  7741. this.expectContextual(118);
  7742. } else if (this.eatContextual(118)) {
  7743. node.readonly = true;
  7744. }
  7745. this.expect(0);
  7746. node.typeParameter = this.tsParseMappedTypeParameter();
  7747. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  7748. this.expect(3);
  7749. if (this.match(53)) {
  7750. node.optional = this.state.value;
  7751. this.next();
  7752. this.expect(17);
  7753. } else if (this.eat(17)) {
  7754. node.optional = true;
  7755. }
  7756. node.typeAnnotation = this.tsTryParseType();
  7757. this.semicolon();
  7758. this.expect(8);
  7759. return this.finishNode(node, "TSMappedType");
  7760. }
  7761. tsParseTupleType() {
  7762. const node = this.startNode();
  7763. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  7764. let seenOptionalElement = false;
  7765. let labeledElements = null;
  7766. node.elementTypes.forEach(elementNode => {
  7767. var _labeledElements;
  7768. const {
  7769. type
  7770. } = elementNode;
  7771. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  7772. this.raise(TSErrors.OptionalTypeBeforeRequired, {
  7773. at: elementNode
  7774. });
  7775. }
  7776. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  7777. let checkType = type;
  7778. if (type === "TSRestType") {
  7779. elementNode = elementNode.typeAnnotation;
  7780. checkType = elementNode.type;
  7781. }
  7782. const isLabeled = checkType === "TSNamedTupleMember";
  7783. (_labeledElements = labeledElements) != null ? _labeledElements : labeledElements = isLabeled;
  7784. if (labeledElements !== isLabeled) {
  7785. this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
  7786. at: elementNode
  7787. });
  7788. }
  7789. });
  7790. return this.finishNode(node, "TSTupleType");
  7791. }
  7792. tsParseTupleElementType() {
  7793. const {
  7794. start: startPos,
  7795. startLoc
  7796. } = this.state;
  7797. const rest = this.eat(21);
  7798. let type = this.tsParseType();
  7799. const optional = this.eat(17);
  7800. const labeled = this.eat(14);
  7801. if (labeled) {
  7802. const labeledNode = this.startNodeAtNode(type);
  7803. labeledNode.optional = optional;
  7804. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  7805. labeledNode.label = type.typeName;
  7806. } else {
  7807. this.raise(TSErrors.InvalidTupleMemberLabel, {
  7808. at: type
  7809. });
  7810. labeledNode.label = type;
  7811. }
  7812. labeledNode.elementType = this.tsParseType();
  7813. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  7814. } else if (optional) {
  7815. const optionalTypeNode = this.startNodeAtNode(type);
  7816. optionalTypeNode.typeAnnotation = type;
  7817. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  7818. }
  7819. if (rest) {
  7820. const restNode = this.startNodeAt(startPos, startLoc);
  7821. restNode.typeAnnotation = type;
  7822. type = this.finishNode(restNode, "TSRestType");
  7823. }
  7824. return type;
  7825. }
  7826. tsParseParenthesizedType() {
  7827. const node = this.startNode();
  7828. this.expect(10);
  7829. node.typeAnnotation = this.tsParseType();
  7830. this.expect(11);
  7831. return this.finishNode(node, "TSParenthesizedType");
  7832. }
  7833. tsParseFunctionOrConstructorType(type, abstract) {
  7834. const node = this.startNode();
  7835. if (type === "TSConstructorType") {
  7836. node.abstract = !!abstract;
  7837. if (abstract) this.next();
  7838. this.next();
  7839. }
  7840. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  7841. return this.finishNode(node, type);
  7842. }
  7843. tsParseLiteralTypeNode() {
  7844. const node = this.startNode();
  7845. node.literal = (() => {
  7846. switch (this.state.type) {
  7847. case 130:
  7848. case 131:
  7849. case 129:
  7850. case 85:
  7851. case 86:
  7852. return super.parseExprAtom();
  7853. default:
  7854. throw this.unexpected();
  7855. }
  7856. })();
  7857. return this.finishNode(node, "TSLiteralType");
  7858. }
  7859. tsParseTemplateLiteralType() {
  7860. const node = this.startNode();
  7861. node.literal = super.parseTemplate(false);
  7862. return this.finishNode(node, "TSLiteralType");
  7863. }
  7864. parseTemplateSubstitution() {
  7865. if (this.state.inType) return this.tsParseType();
  7866. return super.parseTemplateSubstitution();
  7867. }
  7868. tsParseThisTypeOrThisTypePredicate() {
  7869. const thisKeyword = this.tsParseThisTypeNode();
  7870. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  7871. return this.tsParseThisTypePredicate(thisKeyword);
  7872. } else {
  7873. return thisKeyword;
  7874. }
  7875. }
  7876. tsParseNonArrayType() {
  7877. switch (this.state.type) {
  7878. case 129:
  7879. case 130:
  7880. case 131:
  7881. case 85:
  7882. case 86:
  7883. return this.tsParseLiteralTypeNode();
  7884. case 53:
  7885. if (this.state.value === "-") {
  7886. const node = this.startNode();
  7887. const nextToken = this.lookahead();
  7888. if (nextToken.type !== 130 && nextToken.type !== 131) {
  7889. throw this.unexpected();
  7890. }
  7891. node.literal = this.parseMaybeUnary();
  7892. return this.finishNode(node, "TSLiteralType");
  7893. }
  7894. break;
  7895. case 78:
  7896. return this.tsParseThisTypeOrThisTypePredicate();
  7897. case 87:
  7898. return this.tsParseTypeQuery();
  7899. case 83:
  7900. return this.tsParseImportType();
  7901. case 5:
  7902. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  7903. case 0:
  7904. return this.tsParseTupleType();
  7905. case 10:
  7906. return this.tsParseParenthesizedType();
  7907. case 25:
  7908. case 24:
  7909. return this.tsParseTemplateLiteralType();
  7910. default:
  7911. {
  7912. const {
  7913. type
  7914. } = this.state;
  7915. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  7916. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  7917. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  7918. const node = this.startNode();
  7919. this.next();
  7920. return this.finishNode(node, nodeType);
  7921. }
  7922. return this.tsParseTypeReference();
  7923. }
  7924. }
  7925. }
  7926. throw this.unexpected();
  7927. }
  7928. tsParseArrayTypeOrHigher() {
  7929. let type = this.tsParseNonArrayType();
  7930. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  7931. if (this.match(3)) {
  7932. const node = this.startNodeAtNode(type);
  7933. node.elementType = type;
  7934. this.expect(3);
  7935. type = this.finishNode(node, "TSArrayType");
  7936. } else {
  7937. const node = this.startNodeAtNode(type);
  7938. node.objectType = type;
  7939. node.indexType = this.tsParseType();
  7940. this.expect(3);
  7941. type = this.finishNode(node, "TSIndexedAccessType");
  7942. }
  7943. }
  7944. return type;
  7945. }
  7946. tsParseTypeOperator() {
  7947. const node = this.startNode();
  7948. const operator = this.state.value;
  7949. this.next();
  7950. node.operator = operator;
  7951. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  7952. if (operator === "readonly") {
  7953. this.tsCheckTypeAnnotationForReadOnly(node);
  7954. }
  7955. return this.finishNode(node, "TSTypeOperator");
  7956. }
  7957. tsCheckTypeAnnotationForReadOnly(node) {
  7958. switch (node.typeAnnotation.type) {
  7959. case "TSTupleType":
  7960. case "TSArrayType":
  7961. return;
  7962. default:
  7963. this.raise(TSErrors.UnexpectedReadonly, {
  7964. at: node
  7965. });
  7966. }
  7967. }
  7968. tsParseInferType() {
  7969. const node = this.startNode();
  7970. this.expectContextual(112);
  7971. const typeParameter = this.startNode();
  7972. typeParameter.name = this.tsParseTypeParameterName();
  7973. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  7974. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  7975. return this.finishNode(node, "TSInferType");
  7976. }
  7977. tsParseConstraintForInferType() {
  7978. if (this.eat(81)) {
  7979. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  7980. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  7981. return constraint;
  7982. }
  7983. }
  7984. }
  7985. tsParseTypeOperatorOrHigher() {
  7986. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  7987. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(112) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  7988. }
  7989. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  7990. const node = this.startNode();
  7991. const hasLeadingOperator = this.eat(operator);
  7992. const types = [];
  7993. do {
  7994. types.push(parseConstituentType());
  7995. } while (this.eat(operator));
  7996. if (types.length === 1 && !hasLeadingOperator) {
  7997. return types[0];
  7998. }
  7999. node.types = types;
  8000. return this.finishNode(node, kind);
  8001. }
  8002. tsParseIntersectionTypeOrHigher() {
  8003. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8004. }
  8005. tsParseUnionTypeOrHigher() {
  8006. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8007. }
  8008. tsIsStartOfFunctionType() {
  8009. if (this.match(47)) {
  8010. return true;
  8011. }
  8012. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8013. }
  8014. tsSkipParameterStart() {
  8015. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8016. this.next();
  8017. return true;
  8018. }
  8019. if (this.match(5)) {
  8020. const {
  8021. errors
  8022. } = this.state;
  8023. const previousErrorCount = errors.length;
  8024. try {
  8025. this.parseObjectLike(8, true);
  8026. return errors.length === previousErrorCount;
  8027. } catch (_unused) {
  8028. return false;
  8029. }
  8030. }
  8031. if (this.match(0)) {
  8032. this.next();
  8033. const {
  8034. errors
  8035. } = this.state;
  8036. const previousErrorCount = errors.length;
  8037. try {
  8038. super.parseBindingList(3, 93, true);
  8039. return errors.length === previousErrorCount;
  8040. } catch (_unused2) {
  8041. return false;
  8042. }
  8043. }
  8044. return false;
  8045. }
  8046. tsIsUnambiguouslyStartOfFunctionType() {
  8047. this.next();
  8048. if (this.match(11) || this.match(21)) {
  8049. return true;
  8050. }
  8051. if (this.tsSkipParameterStart()) {
  8052. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8053. return true;
  8054. }
  8055. if (this.match(11)) {
  8056. this.next();
  8057. if (this.match(19)) {
  8058. return true;
  8059. }
  8060. }
  8061. }
  8062. return false;
  8063. }
  8064. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8065. return this.tsInType(() => {
  8066. const t = this.startNode();
  8067. this.expect(returnToken);
  8068. const node = this.startNode();
  8069. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8070. if (asserts && this.match(78)) {
  8071. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8072. if (thisTypePredicate.type === "TSThisType") {
  8073. node.parameterName = thisTypePredicate;
  8074. node.asserts = true;
  8075. node.typeAnnotation = null;
  8076. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8077. } else {
  8078. this.resetStartLocationFromNode(thisTypePredicate, node);
  8079. thisTypePredicate.asserts = true;
  8080. }
  8081. t.typeAnnotation = thisTypePredicate;
  8082. return this.finishNode(t, "TSTypeAnnotation");
  8083. }
  8084. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8085. if (!typePredicateVariable) {
  8086. if (!asserts) {
  8087. return this.tsParseTypeAnnotation(false, t);
  8088. }
  8089. node.parameterName = this.parseIdentifier();
  8090. node.asserts = asserts;
  8091. node.typeAnnotation = null;
  8092. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8093. return this.finishNode(t, "TSTypeAnnotation");
  8094. }
  8095. const type = this.tsParseTypeAnnotation(false);
  8096. node.parameterName = typePredicateVariable;
  8097. node.typeAnnotation = type;
  8098. node.asserts = asserts;
  8099. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8100. return this.finishNode(t, "TSTypeAnnotation");
  8101. });
  8102. }
  8103. tsTryParseTypeOrTypePredicateAnnotation() {
  8104. return this.match(14) ? this.tsParseTypeOrTypePredicateAnnotation(14) : undefined;
  8105. }
  8106. tsTryParseTypeAnnotation() {
  8107. return this.match(14) ? this.tsParseTypeAnnotation() : undefined;
  8108. }
  8109. tsTryParseType() {
  8110. return this.tsEatThenParseType(14);
  8111. }
  8112. tsParseTypePredicatePrefix() {
  8113. const id = this.parseIdentifier();
  8114. if (this.isContextual(113) && !this.hasPrecedingLineBreak()) {
  8115. this.next();
  8116. return id;
  8117. }
  8118. }
  8119. tsParseTypePredicateAsserts() {
  8120. if (this.state.type !== 106) {
  8121. return false;
  8122. }
  8123. const containsEsc = this.state.containsEsc;
  8124. this.next();
  8125. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8126. return false;
  8127. }
  8128. if (containsEsc) {
  8129. this.raise(Errors.InvalidEscapedReservedWord, {
  8130. at: this.state.lastTokStartLoc,
  8131. reservedWord: "asserts"
  8132. });
  8133. }
  8134. return true;
  8135. }
  8136. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8137. this.tsInType(() => {
  8138. if (eatColon) this.expect(14);
  8139. t.typeAnnotation = this.tsParseType();
  8140. });
  8141. return this.finishNode(t, "TSTypeAnnotation");
  8142. }
  8143. tsParseType() {
  8144. assert(this.state.inType);
  8145. const type = this.tsParseNonConditionalType();
  8146. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8147. return type;
  8148. }
  8149. const node = this.startNodeAtNode(type);
  8150. node.checkType = type;
  8151. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8152. this.expect(17);
  8153. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8154. this.expect(14);
  8155. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8156. return this.finishNode(node, "TSConditionalType");
  8157. }
  8158. isAbstractConstructorSignature() {
  8159. return this.isContextual(120) && this.lookahead().type === 77;
  8160. }
  8161. tsParseNonConditionalType() {
  8162. if (this.tsIsStartOfFunctionType()) {
  8163. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8164. }
  8165. if (this.match(77)) {
  8166. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8167. } else if (this.isAbstractConstructorSignature()) {
  8168. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8169. }
  8170. return this.tsParseUnionTypeOrHigher();
  8171. }
  8172. tsParseTypeAssertion() {
  8173. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8174. this.raise(TSErrors.ReservedTypeAssertion, {
  8175. at: this.state.startLoc
  8176. });
  8177. }
  8178. const node = this.startNode();
  8179. const _const = this.tsTryNextParseConstantContext();
  8180. node.typeAnnotation = _const || this.tsNextThenParseType();
  8181. this.expect(48);
  8182. node.expression = this.parseMaybeUnary();
  8183. return this.finishNode(node, "TSTypeAssertion");
  8184. }
  8185. tsParseHeritageClause(token) {
  8186. const originalStartLoc = this.state.startLoc;
  8187. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8188. const node = this.startNode();
  8189. node.expression = this.tsParseEntityName();
  8190. if (this.match(47)) {
  8191. node.typeParameters = this.tsParseTypeArguments();
  8192. }
  8193. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8194. });
  8195. if (!delimitedList.length) {
  8196. this.raise(TSErrors.EmptyHeritageClauseType, {
  8197. at: originalStartLoc,
  8198. token
  8199. });
  8200. }
  8201. return delimitedList;
  8202. }
  8203. tsParseInterfaceDeclaration(node, properties = {}) {
  8204. if (this.hasFollowingLineBreak()) return null;
  8205. this.expectContextual(125);
  8206. if (properties.declare) node.declare = true;
  8207. if (tokenIsIdentifier(this.state.type)) {
  8208. node.id = this.parseIdentifier();
  8209. this.checkIdentifier(node.id, BIND_TS_INTERFACE);
  8210. } else {
  8211. node.id = null;
  8212. this.raise(TSErrors.MissingInterfaceName, {
  8213. at: this.state.startLoc
  8214. });
  8215. }
  8216. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  8217. if (this.eat(81)) {
  8218. node.extends = this.tsParseHeritageClause("extends");
  8219. }
  8220. const body = this.startNode();
  8221. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8222. node.body = this.finishNode(body, "TSInterfaceBody");
  8223. return this.finishNode(node, "TSInterfaceDeclaration");
  8224. }
  8225. tsParseTypeAliasDeclaration(node) {
  8226. node.id = this.parseIdentifier();
  8227. this.checkIdentifier(node.id, BIND_TS_TYPE);
  8228. node.typeAnnotation = this.tsInType(() => {
  8229. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  8230. this.expect(29);
  8231. if (this.isContextual(111) && this.lookahead().type !== 16) {
  8232. const node = this.startNode();
  8233. this.next();
  8234. return this.finishNode(node, "TSIntrinsicKeyword");
  8235. }
  8236. return this.tsParseType();
  8237. });
  8238. this.semicolon();
  8239. return this.finishNode(node, "TSTypeAliasDeclaration");
  8240. }
  8241. tsInNoContext(cb) {
  8242. const oldContext = this.state.context;
  8243. this.state.context = [oldContext[0]];
  8244. try {
  8245. return cb();
  8246. } finally {
  8247. this.state.context = oldContext;
  8248. }
  8249. }
  8250. tsInType(cb) {
  8251. const oldInType = this.state.inType;
  8252. this.state.inType = true;
  8253. try {
  8254. return cb();
  8255. } finally {
  8256. this.state.inType = oldInType;
  8257. }
  8258. }
  8259. tsInDisallowConditionalTypesContext(cb) {
  8260. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8261. this.state.inDisallowConditionalTypesContext = true;
  8262. try {
  8263. return cb();
  8264. } finally {
  8265. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8266. }
  8267. }
  8268. tsInAllowConditionalTypesContext(cb) {
  8269. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8270. this.state.inDisallowConditionalTypesContext = false;
  8271. try {
  8272. return cb();
  8273. } finally {
  8274. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8275. }
  8276. }
  8277. tsEatThenParseType(token) {
  8278. return !this.match(token) ? undefined : this.tsNextThenParseType();
  8279. }
  8280. tsExpectThenParseType(token) {
  8281. return this.tsDoThenParseType(() => this.expect(token));
  8282. }
  8283. tsNextThenParseType() {
  8284. return this.tsDoThenParseType(() => this.next());
  8285. }
  8286. tsDoThenParseType(cb) {
  8287. return this.tsInType(() => {
  8288. cb();
  8289. return this.tsParseType();
  8290. });
  8291. }
  8292. tsParseEnumMember() {
  8293. const node = this.startNode();
  8294. node.id = this.match(129) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8295. if (this.eat(29)) {
  8296. node.initializer = super.parseMaybeAssignAllowIn();
  8297. }
  8298. return this.finishNode(node, "TSEnumMember");
  8299. }
  8300. tsParseEnumDeclaration(node, properties = {}) {
  8301. if (properties.const) node.const = true;
  8302. if (properties.declare) node.declare = true;
  8303. this.expectContextual(122);
  8304. node.id = this.parseIdentifier();
  8305. this.checkIdentifier(node.id, node.const ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  8306. this.expect(5);
  8307. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8308. this.expect(8);
  8309. return this.finishNode(node, "TSEnumDeclaration");
  8310. }
  8311. tsParseModuleBlock() {
  8312. const node = this.startNode();
  8313. this.scope.enter(SCOPE_OTHER);
  8314. this.expect(5);
  8315. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8316. this.next();
  8317. this.scope.exit();
  8318. return this.finishNode(node, "TSModuleBlock");
  8319. }
  8320. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8321. node.id = this.parseIdentifier();
  8322. if (!nested) {
  8323. this.checkIdentifier(node.id, BIND_TS_NAMESPACE);
  8324. }
  8325. if (this.eat(16)) {
  8326. const inner = this.startNode();
  8327. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8328. node.body = inner;
  8329. } else {
  8330. this.scope.enter(SCOPE_TS_MODULE);
  8331. this.prodParam.enter(PARAM);
  8332. node.body = this.tsParseModuleBlock();
  8333. this.prodParam.exit();
  8334. this.scope.exit();
  8335. }
  8336. return this.finishNode(node, "TSModuleDeclaration");
  8337. }
  8338. tsParseAmbientExternalModuleDeclaration(node) {
  8339. if (this.isContextual(109)) {
  8340. node.global = true;
  8341. node.id = this.parseIdentifier();
  8342. } else if (this.match(129)) {
  8343. node.id = super.parseStringLiteral(this.state.value);
  8344. } else {
  8345. this.unexpected();
  8346. }
  8347. if (this.match(5)) {
  8348. this.scope.enter(SCOPE_TS_MODULE);
  8349. this.prodParam.enter(PARAM);
  8350. node.body = this.tsParseModuleBlock();
  8351. this.prodParam.exit();
  8352. this.scope.exit();
  8353. } else {
  8354. this.semicolon();
  8355. }
  8356. return this.finishNode(node, "TSModuleDeclaration");
  8357. }
  8358. tsParseImportEqualsDeclaration(node, isExport) {
  8359. node.isExport = isExport || false;
  8360. node.id = this.parseIdentifier();
  8361. this.checkIdentifier(node.id, BIND_FLAGS_TS_IMPORT);
  8362. this.expect(29);
  8363. const moduleReference = this.tsParseModuleReference();
  8364. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8365. this.raise(TSErrors.ImportAliasHasImportType, {
  8366. at: moduleReference
  8367. });
  8368. }
  8369. node.moduleReference = moduleReference;
  8370. this.semicolon();
  8371. return this.finishNode(node, "TSImportEqualsDeclaration");
  8372. }
  8373. tsIsExternalModuleReference() {
  8374. return this.isContextual(116) && this.lookaheadCharCode() === 40;
  8375. }
  8376. tsParseModuleReference() {
  8377. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  8378. }
  8379. tsParseExternalModuleReference() {
  8380. const node = this.startNode();
  8381. this.expectContextual(116);
  8382. this.expect(10);
  8383. if (!this.match(129)) {
  8384. throw this.unexpected();
  8385. }
  8386. node.expression = super.parseExprAtom();
  8387. this.expect(11);
  8388. return this.finishNode(node, "TSExternalModuleReference");
  8389. }
  8390. tsLookAhead(f) {
  8391. const state = this.state.clone();
  8392. const res = f();
  8393. this.state = state;
  8394. return res;
  8395. }
  8396. tsTryParseAndCatch(f) {
  8397. const result = this.tryParse(abort => f() || abort());
  8398. if (result.aborted || !result.node) return undefined;
  8399. if (result.error) this.state = result.failState;
  8400. return result.node;
  8401. }
  8402. tsTryParse(f) {
  8403. const state = this.state.clone();
  8404. const result = f();
  8405. if (result !== undefined && result !== false) {
  8406. return result;
  8407. } else {
  8408. this.state = state;
  8409. return undefined;
  8410. }
  8411. }
  8412. tsTryParseDeclare(nany) {
  8413. if (this.isLineTerminator()) {
  8414. return;
  8415. }
  8416. let starttype = this.state.type;
  8417. let kind;
  8418. if (this.isContextual(99)) {
  8419. starttype = 74;
  8420. kind = "let";
  8421. }
  8422. return this.tsInAmbientContext(() => {
  8423. if (starttype === 68) {
  8424. nany.declare = true;
  8425. return super.parseFunctionStatement(nany, false, true);
  8426. }
  8427. if (starttype === 80) {
  8428. nany.declare = true;
  8429. return this.parseClass(nany, true, false);
  8430. }
  8431. if (starttype === 122) {
  8432. return this.tsParseEnumDeclaration(nany, {
  8433. declare: true
  8434. });
  8435. }
  8436. if (starttype === 109) {
  8437. return this.tsParseAmbientExternalModuleDeclaration(nany);
  8438. }
  8439. if (starttype === 75 || starttype === 74) {
  8440. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  8441. nany.declare = true;
  8442. return this.parseVarStatement(nany, kind || this.state.value, true);
  8443. }
  8444. this.expect(75);
  8445. return this.tsParseEnumDeclaration(nany, {
  8446. const: true,
  8447. declare: true
  8448. });
  8449. }
  8450. if (starttype === 125) {
  8451. const result = this.tsParseInterfaceDeclaration(nany, {
  8452. declare: true
  8453. });
  8454. if (result) return result;
  8455. }
  8456. if (tokenIsIdentifier(starttype)) {
  8457. return this.tsParseDeclaration(nany, this.state.value, true);
  8458. }
  8459. });
  8460. }
  8461. tsTryParseExportDeclaration() {
  8462. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  8463. }
  8464. tsParseExpressionStatement(node, expr) {
  8465. switch (expr.name) {
  8466. case "declare":
  8467. {
  8468. const declaration = this.tsTryParseDeclare(node);
  8469. if (declaration) {
  8470. declaration.declare = true;
  8471. return declaration;
  8472. }
  8473. break;
  8474. }
  8475. case "global":
  8476. if (this.match(5)) {
  8477. this.scope.enter(SCOPE_TS_MODULE);
  8478. this.prodParam.enter(PARAM);
  8479. const mod = node;
  8480. mod.global = true;
  8481. mod.id = expr;
  8482. mod.body = this.tsParseModuleBlock();
  8483. this.scope.exit();
  8484. this.prodParam.exit();
  8485. return this.finishNode(mod, "TSModuleDeclaration");
  8486. }
  8487. break;
  8488. default:
  8489. return this.tsParseDeclaration(node, expr.name, false);
  8490. }
  8491. }
  8492. tsParseDeclaration(node, value, next) {
  8493. switch (value) {
  8494. case "abstract":
  8495. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  8496. return this.tsParseAbstractDeclaration(node);
  8497. }
  8498. break;
  8499. case "module":
  8500. if (this.tsCheckLineTerminator(next)) {
  8501. if (this.match(129)) {
  8502. return this.tsParseAmbientExternalModuleDeclaration(node);
  8503. } else if (tokenIsIdentifier(this.state.type)) {
  8504. return this.tsParseModuleOrNamespaceDeclaration(node);
  8505. }
  8506. }
  8507. break;
  8508. case "namespace":
  8509. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8510. return this.tsParseModuleOrNamespaceDeclaration(node);
  8511. }
  8512. break;
  8513. case "type":
  8514. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  8515. return this.tsParseTypeAliasDeclaration(node);
  8516. }
  8517. break;
  8518. }
  8519. }
  8520. tsCheckLineTerminator(next) {
  8521. if (next) {
  8522. if (this.hasFollowingLineBreak()) return false;
  8523. this.next();
  8524. return true;
  8525. }
  8526. return !this.isLineTerminator();
  8527. }
  8528. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  8529. if (!this.match(47)) {
  8530. return undefined;
  8531. }
  8532. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  8533. this.state.maybeInArrowParameters = true;
  8534. const res = this.tsTryParseAndCatch(() => {
  8535. const node = this.startNodeAt(startPos, startLoc);
  8536. node.typeParameters = this.tsParseTypeParameters();
  8537. super.parseFunctionParams(node);
  8538. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  8539. this.expect(19);
  8540. return node;
  8541. });
  8542. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  8543. if (!res) {
  8544. return undefined;
  8545. }
  8546. return super.parseArrowExpression(res, null, true);
  8547. }
  8548. tsParseTypeArgumentsInExpression() {
  8549. if (this.reScan_lt() !== 47) {
  8550. return undefined;
  8551. }
  8552. return this.tsParseTypeArguments();
  8553. }
  8554. tsParseTypeArguments() {
  8555. const node = this.startNode();
  8556. node.params = this.tsInType(() => this.tsInNoContext(() => {
  8557. this.expect(47);
  8558. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  8559. }));
  8560. if (node.params.length === 0) {
  8561. this.raise(TSErrors.EmptyTypeArguments, {
  8562. at: node
  8563. });
  8564. }
  8565. this.expect(48);
  8566. return this.finishNode(node, "TSTypeParameterInstantiation");
  8567. }
  8568. tsIsDeclarationStart() {
  8569. return tokenIsTSDeclarationStart(this.state.type);
  8570. }
  8571. isExportDefaultSpecifier() {
  8572. if (this.tsIsDeclarationStart()) return false;
  8573. return super.isExportDefaultSpecifier();
  8574. }
  8575. parseAssignableListItem(allowModifiers, decorators) {
  8576. const startPos = this.state.start;
  8577. const startLoc = this.state.startLoc;
  8578. let accessibility;
  8579. let readonly = false;
  8580. let override = false;
  8581. if (allowModifiers !== undefined) {
  8582. const modified = {};
  8583. this.tsParseModifiers({
  8584. modified,
  8585. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  8586. });
  8587. accessibility = modified.accessibility;
  8588. override = modified.override;
  8589. readonly = modified.readonly;
  8590. if (allowModifiers === false && (accessibility || readonly || override)) {
  8591. this.raise(TSErrors.UnexpectedParameterModifier, {
  8592. at: startLoc
  8593. });
  8594. }
  8595. }
  8596. const left = this.parseMaybeDefault();
  8597. this.parseAssignableListItemTypes(left);
  8598. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8599. if (accessibility || readonly || override) {
  8600. const pp = this.startNodeAt(startPos, startLoc);
  8601. if (decorators.length) {
  8602. pp.decorators = decorators;
  8603. }
  8604. if (accessibility) pp.accessibility = accessibility;
  8605. if (readonly) pp.readonly = readonly;
  8606. if (override) pp.override = override;
  8607. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  8608. this.raise(TSErrors.UnsupportedParameterPropertyKind, {
  8609. at: pp
  8610. });
  8611. }
  8612. pp.parameter = elt;
  8613. return this.finishNode(pp, "TSParameterProperty");
  8614. }
  8615. if (decorators.length) {
  8616. left.decorators = decorators;
  8617. }
  8618. return elt;
  8619. }
  8620. isSimpleParameter(node) {
  8621. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  8622. }
  8623. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  8624. if (this.match(14)) {
  8625. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  8626. }
  8627. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  8628. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  8629. return this.finishNode(node, bodilessType);
  8630. }
  8631. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  8632. this.raise(TSErrors.DeclareFunctionHasImplementation, {
  8633. at: node
  8634. });
  8635. if (node.declare) {
  8636. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  8637. }
  8638. }
  8639. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  8640. }
  8641. registerFunctionStatementId(node) {
  8642. if (!node.body && node.id) {
  8643. this.checkIdentifier(node.id, BIND_TS_AMBIENT);
  8644. } else {
  8645. super.registerFunctionStatementId(node);
  8646. }
  8647. }
  8648. tsCheckForInvalidTypeCasts(items) {
  8649. items.forEach(node => {
  8650. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  8651. this.raise(TSErrors.UnexpectedTypeAnnotation, {
  8652. at: node.typeAnnotation
  8653. });
  8654. }
  8655. });
  8656. }
  8657. toReferencedList(exprList, isInParens) {
  8658. this.tsCheckForInvalidTypeCasts(exprList);
  8659. return exprList;
  8660. }
  8661. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  8662. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  8663. if (node.type === "ArrayExpression") {
  8664. this.tsCheckForInvalidTypeCasts(node.elements);
  8665. }
  8666. return node;
  8667. }
  8668. parseSubscript(base, startPos, startLoc, noCalls, state) {
  8669. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  8670. this.state.canStartJSXElement = false;
  8671. this.next();
  8672. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  8673. nonNullExpression.expression = base;
  8674. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  8675. }
  8676. let isOptionalCall = false;
  8677. if (this.match(18) && this.lookaheadCharCode() === 60) {
  8678. if (noCalls) {
  8679. state.stop = true;
  8680. return base;
  8681. }
  8682. state.optionalChainMember = isOptionalCall = true;
  8683. this.next();
  8684. }
  8685. if (this.match(47) || this.match(51)) {
  8686. let missingParenErrorLoc;
  8687. const result = this.tsTryParseAndCatch(() => {
  8688. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  8689. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  8690. if (asyncArrowFn) {
  8691. return asyncArrowFn;
  8692. }
  8693. }
  8694. const typeArguments = this.tsParseTypeArgumentsInExpression();
  8695. if (!typeArguments) return;
  8696. if (isOptionalCall && !this.match(10)) {
  8697. missingParenErrorLoc = this.state.curPosition();
  8698. return;
  8699. }
  8700. if (tokenIsTemplate(this.state.type)) {
  8701. const result = super.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  8702. result.typeParameters = typeArguments;
  8703. return result;
  8704. }
  8705. if (!noCalls && this.eat(10)) {
  8706. const node = this.startNodeAt(startPos, startLoc);
  8707. node.callee = base;
  8708. node.arguments = this.parseCallExpressionArguments(11, false);
  8709. this.tsCheckForInvalidTypeCasts(node.arguments);
  8710. node.typeParameters = typeArguments;
  8711. if (state.optionalChainMember) {
  8712. node.optional = isOptionalCall;
  8713. }
  8714. return this.finishCallExpression(node, state.optionalChainMember);
  8715. }
  8716. const tokenType = this.state.type;
  8717. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  8718. return;
  8719. }
  8720. const node = this.startNodeAt(startPos, startLoc);
  8721. node.expression = base;
  8722. node.typeParameters = typeArguments;
  8723. return this.finishNode(node, "TSInstantiationExpression");
  8724. });
  8725. if (missingParenErrorLoc) {
  8726. this.unexpected(missingParenErrorLoc, 10);
  8727. }
  8728. if (result) {
  8729. if (result.type === "TSInstantiationExpression" && (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40)) {
  8730. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, {
  8731. at: this.state.startLoc
  8732. });
  8733. }
  8734. return result;
  8735. }
  8736. }
  8737. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  8738. }
  8739. parseNewCallee(node) {
  8740. var _callee$extra;
  8741. super.parseNewCallee(node);
  8742. const {
  8743. callee
  8744. } = node;
  8745. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  8746. node.typeParameters = callee.typeParameters;
  8747. node.callee = callee.expression;
  8748. }
  8749. }
  8750. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8751. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual(93)) {
  8752. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8753. node.expression = left;
  8754. const _const = this.tsTryNextParseConstantContext();
  8755. if (_const) {
  8756. node.typeAnnotation = _const;
  8757. } else {
  8758. node.typeAnnotation = this.tsNextThenParseType();
  8759. }
  8760. this.finishNode(node, "TSAsExpression");
  8761. this.reScan_lt_gt();
  8762. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  8763. }
  8764. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  8765. }
  8766. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  8767. if (!this.state.isAmbientContext) {
  8768. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  8769. }
  8770. }
  8771. checkDuplicateExports() {}
  8772. parseImport(node) {
  8773. node.importKind = "value";
  8774. if (tokenIsIdentifier(this.state.type) || this.match(55) || this.match(5)) {
  8775. let ahead = this.lookahead();
  8776. if (this.isContextual(126) && ahead.type !== 12 && ahead.type !== 97 && ahead.type !== 29) {
  8777. node.importKind = "type";
  8778. this.next();
  8779. ahead = this.lookahead();
  8780. }
  8781. if (tokenIsIdentifier(this.state.type) && ahead.type === 29) {
  8782. return this.tsParseImportEqualsDeclaration(node);
  8783. }
  8784. }
  8785. const importNode = super.parseImport(node);
  8786. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  8787. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, {
  8788. at: importNode
  8789. });
  8790. }
  8791. return importNode;
  8792. }
  8793. parseExport(node) {
  8794. if (this.match(83)) {
  8795. this.next();
  8796. if (this.isContextual(126) && this.lookaheadCharCode() !== 61) {
  8797. node.importKind = "type";
  8798. this.next();
  8799. } else {
  8800. node.importKind = "value";
  8801. }
  8802. return this.tsParseImportEqualsDeclaration(node, true);
  8803. } else if (this.eat(29)) {
  8804. const assign = node;
  8805. assign.expression = super.parseExpression();
  8806. this.semicolon();
  8807. return this.finishNode(assign, "TSExportAssignment");
  8808. } else if (this.eatContextual(93)) {
  8809. const decl = node;
  8810. this.expectContextual(124);
  8811. decl.id = this.parseIdentifier();
  8812. this.semicolon();
  8813. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  8814. } else {
  8815. if (this.isContextual(126) && this.lookahead().type === 5) {
  8816. this.next();
  8817. node.exportKind = "type";
  8818. } else {
  8819. node.exportKind = "value";
  8820. }
  8821. return super.parseExport(node);
  8822. }
  8823. }
  8824. isAbstractClass() {
  8825. return this.isContextual(120) && this.lookahead().type === 80;
  8826. }
  8827. parseExportDefaultExpression() {
  8828. if (this.isAbstractClass()) {
  8829. const cls = this.startNode();
  8830. this.next();
  8831. cls.abstract = true;
  8832. return this.parseClass(cls, true, true);
  8833. }
  8834. if (this.match(125)) {
  8835. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8836. if (result) return result;
  8837. }
  8838. return super.parseExportDefaultExpression();
  8839. }
  8840. parseVarStatement(node, kind, allowMissingInitializer = false) {
  8841. const {
  8842. isAmbientContext
  8843. } = this.state;
  8844. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  8845. if (!isAmbientContext) return declaration;
  8846. for (const {
  8847. id,
  8848. init
  8849. } of declaration.declarations) {
  8850. if (!init) continue;
  8851. if (kind !== "const" || !!id.typeAnnotation) {
  8852. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, {
  8853. at: init
  8854. });
  8855. } else if (init.type !== "StringLiteral" && init.type !== "BooleanLiteral" && init.type !== "NumericLiteral" && init.type !== "BigIntLiteral" && (init.type !== "TemplateLiteral" || init.expressions.length > 0) && !isPossiblyLiteralEnum(init)) {
  8856. this.raise(TSErrors.ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference, {
  8857. at: init
  8858. });
  8859. }
  8860. }
  8861. return declaration;
  8862. }
  8863. parseStatementContent(context, topLevel) {
  8864. if (this.match(75) && this.isLookaheadContextual("enum")) {
  8865. const node = this.startNode();
  8866. this.expect(75);
  8867. return this.tsParseEnumDeclaration(node, {
  8868. const: true
  8869. });
  8870. }
  8871. if (this.isContextual(122)) {
  8872. return this.tsParseEnumDeclaration(this.startNode());
  8873. }
  8874. if (this.isContextual(125)) {
  8875. const result = this.tsParseInterfaceDeclaration(this.startNode());
  8876. if (result) return result;
  8877. }
  8878. return super.parseStatementContent(context, topLevel);
  8879. }
  8880. parseAccessModifier() {
  8881. return this.tsParseModifier(["public", "protected", "private"]);
  8882. }
  8883. tsHasSomeModifiers(member, modifiers) {
  8884. return modifiers.some(modifier => {
  8885. if (tsIsAccessModifier(modifier)) {
  8886. return member.accessibility === modifier;
  8887. }
  8888. return !!member[modifier];
  8889. });
  8890. }
  8891. tsIsStartOfStaticBlocks() {
  8892. return this.isContextual(104) && this.lookaheadCharCode() === 123;
  8893. }
  8894. parseClassMember(classBody, member, state) {
  8895. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  8896. this.tsParseModifiers({
  8897. modified: member,
  8898. allowedModifiers: modifiers,
  8899. disallowedModifiers: ["in", "out"],
  8900. stopOnStartOfClassStaticBlock: true,
  8901. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  8902. });
  8903. const callParseClassMemberWithIsStatic = () => {
  8904. if (this.tsIsStartOfStaticBlocks()) {
  8905. this.next();
  8906. this.next();
  8907. if (this.tsHasSomeModifiers(member, modifiers)) {
  8908. this.raise(TSErrors.StaticBlockCannotHaveModifier, {
  8909. at: this.state.curPosition()
  8910. });
  8911. }
  8912. super.parseClassStaticBlock(classBody, member);
  8913. } else {
  8914. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  8915. }
  8916. };
  8917. if (member.declare) {
  8918. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  8919. } else {
  8920. callParseClassMemberWithIsStatic();
  8921. }
  8922. }
  8923. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  8924. const idx = this.tsTryParseIndexSignature(member);
  8925. if (idx) {
  8926. classBody.body.push(idx);
  8927. if (member.abstract) {
  8928. this.raise(TSErrors.IndexSignatureHasAbstract, {
  8929. at: member
  8930. });
  8931. }
  8932. if (member.accessibility) {
  8933. this.raise(TSErrors.IndexSignatureHasAccessibility, {
  8934. at: member,
  8935. modifier: member.accessibility
  8936. });
  8937. }
  8938. if (member.declare) {
  8939. this.raise(TSErrors.IndexSignatureHasDeclare, {
  8940. at: member
  8941. });
  8942. }
  8943. if (member.override) {
  8944. this.raise(TSErrors.IndexSignatureHasOverride, {
  8945. at: member
  8946. });
  8947. }
  8948. return;
  8949. }
  8950. if (!this.state.inAbstractClass && member.abstract) {
  8951. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, {
  8952. at: member
  8953. });
  8954. }
  8955. if (member.override) {
  8956. if (!state.hadSuperClass) {
  8957. this.raise(TSErrors.OverrideNotInSubClass, {
  8958. at: member
  8959. });
  8960. }
  8961. }
  8962. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  8963. }
  8964. parsePostMemberNameModifiers(methodOrProp) {
  8965. const optional = this.eat(17);
  8966. if (optional) methodOrProp.optional = true;
  8967. if (methodOrProp.readonly && this.match(10)) {
  8968. this.raise(TSErrors.ClassMethodHasReadonly, {
  8969. at: methodOrProp
  8970. });
  8971. }
  8972. if (methodOrProp.declare && this.match(10)) {
  8973. this.raise(TSErrors.ClassMethodHasDeclare, {
  8974. at: methodOrProp
  8975. });
  8976. }
  8977. }
  8978. parseExpressionStatement(node, expr) {
  8979. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  8980. return decl || super.parseExpressionStatement(node, expr);
  8981. }
  8982. shouldParseExportDeclaration() {
  8983. if (this.tsIsDeclarationStart()) return true;
  8984. return super.shouldParseExportDeclaration();
  8985. }
  8986. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8987. if (!this.state.maybeInArrowParameters || !this.match(17)) {
  8988. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8989. }
  8990. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  8991. if (!result.node) {
  8992. if (result.error) {
  8993. super.setOptionalParametersError(refExpressionErrors, result.error);
  8994. }
  8995. return expr;
  8996. }
  8997. if (result.error) this.state = result.failState;
  8998. return result.node;
  8999. }
  9000. parseParenItem(node, startPos, startLoc) {
  9001. node = super.parseParenItem(node, startPos, startLoc);
  9002. if (this.eat(17)) {
  9003. node.optional = true;
  9004. this.resetEndLocation(node);
  9005. }
  9006. if (this.match(14)) {
  9007. const typeCastNode = this.startNodeAt(startPos, startLoc);
  9008. typeCastNode.expression = node;
  9009. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9010. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9011. }
  9012. return node;
  9013. }
  9014. parseExportDeclaration(node) {
  9015. if (!this.state.isAmbientContext && this.isContextual(121)) {
  9016. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9017. }
  9018. const startPos = this.state.start;
  9019. const startLoc = this.state.startLoc;
  9020. const isDeclare = this.eatContextual(121);
  9021. if (isDeclare && (this.isContextual(121) || !this.shouldParseExportDeclaration())) {
  9022. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, {
  9023. at: this.state.startLoc
  9024. });
  9025. }
  9026. const isIdentifier = tokenIsIdentifier(this.state.type);
  9027. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9028. if (!declaration) return null;
  9029. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9030. node.exportKind = "type";
  9031. }
  9032. if (isDeclare) {
  9033. this.resetStartLocation(declaration, startPos, startLoc);
  9034. declaration.declare = true;
  9035. }
  9036. return declaration;
  9037. }
  9038. parseClassId(node, isStatement, optionalId, bindingType) {
  9039. if ((!isStatement || optionalId) && this.isContextual(110)) {
  9040. return;
  9041. }
  9042. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  9043. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers.bind(this));
  9044. if (typeParameters) node.typeParameters = typeParameters;
  9045. }
  9046. parseClassPropertyAnnotation(node) {
  9047. if (!node.optional && this.eat(35)) {
  9048. node.definite = true;
  9049. }
  9050. const type = this.tsTryParseTypeAnnotation();
  9051. if (type) node.typeAnnotation = type;
  9052. }
  9053. parseClassProperty(node) {
  9054. this.parseClassPropertyAnnotation(node);
  9055. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9056. this.raise(TSErrors.DeclareClassFieldHasInitializer, {
  9057. at: this.state.startLoc
  9058. });
  9059. }
  9060. if (node.abstract && this.match(29)) {
  9061. const {
  9062. key
  9063. } = node;
  9064. this.raise(TSErrors.AbstractPropertyHasInitializer, {
  9065. at: this.state.startLoc,
  9066. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9067. });
  9068. }
  9069. return super.parseClassProperty(node);
  9070. }
  9071. parseClassPrivateProperty(node) {
  9072. if (node.abstract) {
  9073. this.raise(TSErrors.PrivateElementHasAbstract, {
  9074. at: node
  9075. });
  9076. }
  9077. if (node.accessibility) {
  9078. this.raise(TSErrors.PrivateElementHasAccessibility, {
  9079. at: node,
  9080. modifier: node.accessibility
  9081. });
  9082. }
  9083. this.parseClassPropertyAnnotation(node);
  9084. return super.parseClassPrivateProperty(node);
  9085. }
  9086. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9087. const typeParameters = this.tsTryParseTypeParameters();
  9088. if (typeParameters && isConstructor) {
  9089. this.raise(TSErrors.ConstructorHasTypeParameters, {
  9090. at: typeParameters
  9091. });
  9092. }
  9093. const {
  9094. declare = false,
  9095. kind
  9096. } = method;
  9097. if (declare && (kind === "get" || kind === "set")) {
  9098. this.raise(TSErrors.DeclareAccessor, {
  9099. at: method,
  9100. kind
  9101. });
  9102. }
  9103. if (typeParameters) method.typeParameters = typeParameters;
  9104. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9105. }
  9106. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9107. const typeParameters = this.tsTryParseTypeParameters();
  9108. if (typeParameters) method.typeParameters = typeParameters;
  9109. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9110. }
  9111. declareClassPrivateMethodInScope(node, kind) {
  9112. if (node.type === "TSDeclareMethod") return;
  9113. if (node.type === "MethodDefinition" && !node.value.body) return;
  9114. super.declareClassPrivateMethodInScope(node, kind);
  9115. }
  9116. parseClassSuper(node) {
  9117. super.parseClassSuper(node);
  9118. if (node.superClass && (this.match(47) || this.match(51))) {
  9119. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9120. }
  9121. if (this.eatContextual(110)) {
  9122. node.implements = this.tsParseHeritageClause("implements");
  9123. }
  9124. }
  9125. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9126. const typeParameters = this.tsTryParseTypeParameters();
  9127. if (typeParameters) prop.typeParameters = typeParameters;
  9128. return super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9129. }
  9130. parseFunctionParams(node, allowModifiers) {
  9131. const typeParameters = this.tsTryParseTypeParameters();
  9132. if (typeParameters) node.typeParameters = typeParameters;
  9133. super.parseFunctionParams(node, allowModifiers);
  9134. }
  9135. parseVarId(decl, kind) {
  9136. super.parseVarId(decl, kind);
  9137. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9138. decl.definite = true;
  9139. }
  9140. const type = this.tsTryParseTypeAnnotation();
  9141. if (type) {
  9142. decl.id.typeAnnotation = type;
  9143. this.resetEndLocation(decl.id);
  9144. }
  9145. }
  9146. parseAsyncArrowFromCallExpression(node, call) {
  9147. if (this.match(14)) {
  9148. node.returnType = this.tsParseTypeAnnotation();
  9149. }
  9150. return super.parseAsyncArrowFromCallExpression(node, call);
  9151. }
  9152. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9153. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  9154. let state;
  9155. let jsx;
  9156. let typeCast;
  9157. if (this.hasPlugin("jsx") && (this.match(138) || this.match(47))) {
  9158. state = this.state.clone();
  9159. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9160. if (!jsx.error) return jsx.node;
  9161. const {
  9162. context
  9163. } = this.state;
  9164. const currentContext = context[context.length - 1];
  9165. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9166. context.pop();
  9167. }
  9168. }
  9169. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9170. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9171. }
  9172. if (!state || state === this.state) state = this.state.clone();
  9173. let typeParameters;
  9174. const arrow = this.tryParse(abort => {
  9175. var _expr$extra, _typeParameters;
  9176. typeParameters = this.tsParseTypeParameters();
  9177. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9178. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9179. abort();
  9180. }
  9181. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9182. this.resetStartLocationFromNode(expr, typeParameters);
  9183. }
  9184. expr.typeParameters = typeParameters;
  9185. return expr;
  9186. }, state);
  9187. if (!arrow.error && !arrow.aborted) {
  9188. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9189. return arrow.node;
  9190. }
  9191. if (!jsx) {
  9192. assert(!this.hasPlugin("jsx"));
  9193. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9194. if (!typeCast.error) return typeCast.node;
  9195. }
  9196. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9197. this.state = jsx.failState;
  9198. return jsx.node;
  9199. }
  9200. if (arrow.node) {
  9201. this.state = arrow.failState;
  9202. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9203. return arrow.node;
  9204. }
  9205. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9206. this.state = typeCast.failState;
  9207. return typeCast.node;
  9208. }
  9209. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  9210. if (arrow.thrown) throw arrow.error;
  9211. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  9212. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  9213. }
  9214. reportReservedArrowTypeParam(node) {
  9215. var _node$extra;
  9216. if (node.params.length === 1 && !((_node$extra = node.extra) != null && _node$extra.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9217. this.raise(TSErrors.ReservedArrowTypeParam, {
  9218. at: node
  9219. });
  9220. }
  9221. }
  9222. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9223. if (!this.hasPlugin("jsx") && this.match(47)) {
  9224. return this.tsParseTypeAssertion();
  9225. } else {
  9226. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9227. }
  9228. }
  9229. parseArrow(node) {
  9230. if (this.match(14)) {
  9231. const result = this.tryParse(abort => {
  9232. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9233. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9234. return returnType;
  9235. });
  9236. if (result.aborted) return;
  9237. if (!result.thrown) {
  9238. if (result.error) this.state = result.failState;
  9239. node.returnType = result.node;
  9240. }
  9241. }
  9242. return super.parseArrow(node);
  9243. }
  9244. parseAssignableListItemTypes(param) {
  9245. if (this.eat(17)) {
  9246. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  9247. this.raise(TSErrors.PatternIsOptional, {
  9248. at: param
  9249. });
  9250. }
  9251. param.optional = true;
  9252. }
  9253. const type = this.tsTryParseTypeAnnotation();
  9254. if (type) param.typeAnnotation = type;
  9255. this.resetEndLocation(param);
  9256. return param;
  9257. }
  9258. isAssignable(node, isBinding) {
  9259. switch (node.type) {
  9260. case "TSTypeCastExpression":
  9261. return this.isAssignable(node.expression, isBinding);
  9262. case "TSParameterProperty":
  9263. return true;
  9264. default:
  9265. return super.isAssignable(node, isBinding);
  9266. }
  9267. }
  9268. toAssignable(node, isLHS = false) {
  9269. switch (node.type) {
  9270. case "ParenthesizedExpression":
  9271. this.toAssignableParenthesizedExpression(node, isLHS);
  9272. break;
  9273. case "TSAsExpression":
  9274. case "TSNonNullExpression":
  9275. case "TSTypeAssertion":
  9276. if (isLHS) {
  9277. this.expressionScope.recordArrowParemeterBindingError(TSErrors.UnexpectedTypeCastInParameter, {
  9278. at: node
  9279. });
  9280. } else {
  9281. this.raise(TSErrors.UnexpectedTypeCastInParameter, {
  9282. at: node
  9283. });
  9284. }
  9285. this.toAssignable(node.expression, isLHS);
  9286. break;
  9287. case "AssignmentExpression":
  9288. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9289. node.left = this.typeCastToParameter(node.left);
  9290. }
  9291. default:
  9292. super.toAssignable(node, isLHS);
  9293. }
  9294. }
  9295. toAssignableParenthesizedExpression(node, isLHS) {
  9296. switch (node.expression.type) {
  9297. case "TSAsExpression":
  9298. case "TSNonNullExpression":
  9299. case "TSTypeAssertion":
  9300. case "ParenthesizedExpression":
  9301. this.toAssignable(node.expression, isLHS);
  9302. break;
  9303. default:
  9304. super.toAssignable(node, isLHS);
  9305. }
  9306. }
  9307. checkToRestConversion(node, allowPattern) {
  9308. switch (node.type) {
  9309. case "TSAsExpression":
  9310. case "TSTypeAssertion":
  9311. case "TSNonNullExpression":
  9312. this.checkToRestConversion(node.expression, false);
  9313. break;
  9314. default:
  9315. super.checkToRestConversion(node, allowPattern);
  9316. }
  9317. }
  9318. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  9319. return getOwn$1({
  9320. TSTypeCastExpression: true,
  9321. TSParameterProperty: "parameter",
  9322. TSNonNullExpression: "expression",
  9323. TSAsExpression: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true],
  9324. TSTypeAssertion: (binding !== BIND_NONE || !isUnparenthesizedInAssign) && ["expression", true]
  9325. }, type) || super.isValidLVal(type, isUnparenthesizedInAssign, binding);
  9326. }
  9327. parseBindingAtom() {
  9328. switch (this.state.type) {
  9329. case 78:
  9330. return this.parseIdentifier(true);
  9331. default:
  9332. return super.parseBindingAtom();
  9333. }
  9334. }
  9335. parseMaybeDecoratorArguments(expr) {
  9336. if (this.match(47) || this.match(51)) {
  9337. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9338. if (this.match(10)) {
  9339. const call = super.parseMaybeDecoratorArguments(expr);
  9340. call.typeParameters = typeArguments;
  9341. return call;
  9342. }
  9343. this.unexpected(null, 10);
  9344. }
  9345. return super.parseMaybeDecoratorArguments(expr);
  9346. }
  9347. checkCommaAfterRest(close) {
  9348. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9349. this.next();
  9350. return false;
  9351. } else {
  9352. return super.checkCommaAfterRest(close);
  9353. }
  9354. }
  9355. isClassMethod() {
  9356. return this.match(47) || super.isClassMethod();
  9357. }
  9358. isClassProperty() {
  9359. return this.match(35) || this.match(14) || super.isClassProperty();
  9360. }
  9361. parseMaybeDefault(startPos, startLoc, left) {
  9362. const node = super.parseMaybeDefault(startPos, startLoc, left);
  9363. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9364. this.raise(TSErrors.TypeAnnotationAfterAssign, {
  9365. at: node.typeAnnotation
  9366. });
  9367. }
  9368. return node;
  9369. }
  9370. getTokenFromCode(code) {
  9371. if (this.state.inType) {
  9372. if (code === 62) {
  9373. return this.finishOp(48, 1);
  9374. }
  9375. if (code === 60) {
  9376. return this.finishOp(47, 1);
  9377. }
  9378. }
  9379. return super.getTokenFromCode(code);
  9380. }
  9381. reScan_lt_gt() {
  9382. const {
  9383. type
  9384. } = this.state;
  9385. if (type === 47) {
  9386. this.state.pos -= 1;
  9387. this.readToken_lt();
  9388. } else if (type === 48) {
  9389. this.state.pos -= 1;
  9390. this.readToken_gt();
  9391. }
  9392. }
  9393. reScan_lt() {
  9394. const {
  9395. type
  9396. } = this.state;
  9397. if (type === 51) {
  9398. this.state.pos -= 2;
  9399. this.finishOp(47, 1);
  9400. return 47;
  9401. }
  9402. return type;
  9403. }
  9404. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  9405. for (let i = 0; i < exprList.length; i++) {
  9406. const expr = exprList[i];
  9407. if ((expr == null ? void 0 : expr.type) === "TSTypeCastExpression") {
  9408. exprList[i] = this.typeCastToParameter(expr);
  9409. }
  9410. }
  9411. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  9412. }
  9413. typeCastToParameter(node) {
  9414. node.expression.typeAnnotation = node.typeAnnotation;
  9415. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  9416. return node.expression;
  9417. }
  9418. shouldParseArrow(params) {
  9419. if (this.match(14)) {
  9420. return params.every(expr => this.isAssignable(expr, true));
  9421. }
  9422. return super.shouldParseArrow(params);
  9423. }
  9424. shouldParseAsyncArrow() {
  9425. return this.match(14) || super.shouldParseAsyncArrow();
  9426. }
  9427. canHaveLeadingDecorator() {
  9428. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  9429. }
  9430. jsxParseOpeningElementAfterName(node) {
  9431. if (this.match(47) || this.match(51)) {
  9432. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  9433. if (typeArguments) node.typeParameters = typeArguments;
  9434. }
  9435. return super.jsxParseOpeningElementAfterName(node);
  9436. }
  9437. getGetterSetterExpectedParamCount(method) {
  9438. const baseCount = super.getGetterSetterExpectedParamCount(method);
  9439. const params = this.getObjectOrClassMethodParams(method);
  9440. const firstParam = params[0];
  9441. const hasContextParam = firstParam && this.isThisParam(firstParam);
  9442. return hasContextParam ? baseCount + 1 : baseCount;
  9443. }
  9444. parseCatchClauseParam() {
  9445. const param = super.parseCatchClauseParam();
  9446. const type = this.tsTryParseTypeAnnotation();
  9447. if (type) {
  9448. param.typeAnnotation = type;
  9449. this.resetEndLocation(param);
  9450. }
  9451. return param;
  9452. }
  9453. tsInAmbientContext(cb) {
  9454. const oldIsAmbientContext = this.state.isAmbientContext;
  9455. this.state.isAmbientContext = true;
  9456. try {
  9457. return cb();
  9458. } finally {
  9459. this.state.isAmbientContext = oldIsAmbientContext;
  9460. }
  9461. }
  9462. parseClass(node, isStatement, optionalId) {
  9463. const oldInAbstractClass = this.state.inAbstractClass;
  9464. this.state.inAbstractClass = !!node.abstract;
  9465. try {
  9466. return super.parseClass(node, isStatement, optionalId);
  9467. } finally {
  9468. this.state.inAbstractClass = oldInAbstractClass;
  9469. }
  9470. }
  9471. tsParseAbstractDeclaration(node) {
  9472. if (this.match(80)) {
  9473. node.abstract = true;
  9474. return this.parseClass(node, true, false);
  9475. } else if (this.isContextual(125)) {
  9476. if (!this.hasFollowingLineBreak()) {
  9477. node.abstract = true;
  9478. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifer, {
  9479. at: node
  9480. });
  9481. return this.tsParseInterfaceDeclaration(node);
  9482. }
  9483. } else {
  9484. this.unexpected(null, 80);
  9485. }
  9486. }
  9487. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  9488. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  9489. if (method.abstract) {
  9490. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  9491. if (hasBody) {
  9492. const {
  9493. key
  9494. } = method;
  9495. this.raise(TSErrors.AbstractMethodHasImplementation, {
  9496. at: method,
  9497. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`
  9498. });
  9499. }
  9500. }
  9501. return method;
  9502. }
  9503. tsParseTypeParameterName() {
  9504. const typeName = this.parseIdentifier();
  9505. return typeName.name;
  9506. }
  9507. shouldParseAsAmbientContext() {
  9508. return !!this.getPluginOption("typescript", "dts");
  9509. }
  9510. parse() {
  9511. if (this.shouldParseAsAmbientContext()) {
  9512. this.state.isAmbientContext = true;
  9513. }
  9514. return super.parse();
  9515. }
  9516. getExpression() {
  9517. if (this.shouldParseAsAmbientContext()) {
  9518. this.state.isAmbientContext = true;
  9519. }
  9520. return super.getExpression();
  9521. }
  9522. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  9523. if (!isString && isMaybeTypeOnly) {
  9524. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  9525. return this.finishNode(node, "ExportSpecifier");
  9526. }
  9527. node.exportKind = "value";
  9528. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  9529. }
  9530. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  9531. if (!importedIsString && isMaybeTypeOnly) {
  9532. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  9533. return this.finishNode(specifier, "ImportSpecifier");
  9534. }
  9535. specifier.importKind = "value";
  9536. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  9537. }
  9538. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  9539. const leftOfAsKey = isImport ? "imported" : "local";
  9540. const rightOfAsKey = isImport ? "local" : "exported";
  9541. let leftOfAs = node[leftOfAsKey];
  9542. let rightOfAs;
  9543. let hasTypeSpecifier = false;
  9544. let canParseAsKeyword = true;
  9545. const loc = leftOfAs.loc.start;
  9546. if (this.isContextual(93)) {
  9547. const firstAs = this.parseIdentifier();
  9548. if (this.isContextual(93)) {
  9549. const secondAs = this.parseIdentifier();
  9550. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9551. hasTypeSpecifier = true;
  9552. leftOfAs = firstAs;
  9553. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9554. canParseAsKeyword = false;
  9555. } else {
  9556. rightOfAs = secondAs;
  9557. canParseAsKeyword = false;
  9558. }
  9559. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9560. canParseAsKeyword = false;
  9561. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9562. } else {
  9563. hasTypeSpecifier = true;
  9564. leftOfAs = firstAs;
  9565. }
  9566. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  9567. hasTypeSpecifier = true;
  9568. if (isImport) {
  9569. leftOfAs = this.parseIdentifier(true);
  9570. if (!this.isContextual(93)) {
  9571. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  9572. }
  9573. } else {
  9574. leftOfAs = this.parseModuleExportName();
  9575. }
  9576. }
  9577. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  9578. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, {
  9579. at: loc
  9580. });
  9581. }
  9582. node[leftOfAsKey] = leftOfAs;
  9583. node[rightOfAsKey] = rightOfAs;
  9584. const kindKey = isImport ? "importKind" : "exportKind";
  9585. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  9586. if (canParseAsKeyword && this.eatContextual(93)) {
  9587. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  9588. }
  9589. if (!node[rightOfAsKey]) {
  9590. node[rightOfAsKey] = cloneIdentifier(node[leftOfAsKey]);
  9591. }
  9592. if (isImport) {
  9593. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? BIND_TS_TYPE_IMPORT : BIND_FLAGS_TS_IMPORT);
  9594. }
  9595. }
  9596. });
  9597. function isPossiblyLiteralEnum(expression) {
  9598. if (expression.type !== "MemberExpression") return false;
  9599. const {
  9600. computed,
  9601. property
  9602. } = expression;
  9603. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  9604. return false;
  9605. }
  9606. return isUncomputedMemberExpressionChain(expression.object);
  9607. }
  9608. function isUncomputedMemberExpressionChain(expression) {
  9609. if (expression.type === "Identifier") return true;
  9610. if (expression.type !== "MemberExpression") return false;
  9611. if (expression.computed) return false;
  9612. return isUncomputedMemberExpressionChain(expression.object);
  9613. }
  9614. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  9615. ClassNameIsRequired: "A class name is required.",
  9616. UnexpectedSpace: "Unexpected space in placeholder."
  9617. });
  9618. var placeholders = (superClass => class PlaceholdersParserMixin extends superClass {
  9619. parsePlaceholder(expectedNode) {
  9620. if (this.match(140)) {
  9621. const node = this.startNode();
  9622. this.next();
  9623. this.assertNoSpace();
  9624. node.name = super.parseIdentifier(true);
  9625. this.assertNoSpace();
  9626. this.expect(140);
  9627. return this.finishPlaceholder(node, expectedNode);
  9628. }
  9629. }
  9630. finishPlaceholder(node, expectedNode) {
  9631. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  9632. node.expectedNode = expectedNode;
  9633. return isFinished ? node : this.finishNode(node, "Placeholder");
  9634. }
  9635. getTokenFromCode(code) {
  9636. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  9637. return this.finishOp(140, 2);
  9638. }
  9639. return super.getTokenFromCode(code);
  9640. }
  9641. parseExprAtom(refExpressionErrors) {
  9642. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  9643. }
  9644. parseIdentifier(liberal) {
  9645. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  9646. }
  9647. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9648. if (word !== undefined) {
  9649. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9650. }
  9651. }
  9652. parseBindingAtom() {
  9653. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  9654. }
  9655. isValidLVal(type, isParenthesized, binding) {
  9656. return type === "Placeholder" || super.isValidLVal(type, isParenthesized, binding);
  9657. }
  9658. toAssignable(node, isLHS) {
  9659. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  9660. node.expectedNode = "Pattern";
  9661. } else {
  9662. super.toAssignable(node, isLHS);
  9663. }
  9664. }
  9665. isLet(context) {
  9666. if (super.isLet(context)) {
  9667. return true;
  9668. }
  9669. if (!this.isContextual(99)) {
  9670. return false;
  9671. }
  9672. if (context) return false;
  9673. const nextToken = this.lookahead();
  9674. if (nextToken.type === 140) {
  9675. return true;
  9676. }
  9677. return false;
  9678. }
  9679. verifyBreakContinue(node, isBreak) {
  9680. if (node.label && node.label.type === "Placeholder") return;
  9681. super.verifyBreakContinue(node, isBreak);
  9682. }
  9683. parseExpressionStatement(node, expr) {
  9684. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  9685. return super.parseExpressionStatement(node, expr);
  9686. }
  9687. if (this.match(14)) {
  9688. const stmt = node;
  9689. stmt.label = this.finishPlaceholder(expr, "Identifier");
  9690. this.next();
  9691. stmt.body = super.parseStatement("label");
  9692. return this.finishNode(stmt, "LabeledStatement");
  9693. }
  9694. this.semicolon();
  9695. node.name = expr.name;
  9696. return this.finishPlaceholder(node, "Statement");
  9697. }
  9698. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  9699. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  9700. }
  9701. parseFunctionId(requireId) {
  9702. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  9703. }
  9704. parseClass(node, isStatement, optionalId) {
  9705. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  9706. this.next();
  9707. this.takeDecorators(node);
  9708. const oldStrict = this.state.strict;
  9709. const placeholder = this.parsePlaceholder("Identifier");
  9710. if (placeholder) {
  9711. if (this.match(81) || this.match(140) || this.match(5)) {
  9712. node.id = placeholder;
  9713. } else if (optionalId || !isStatement) {
  9714. node.id = null;
  9715. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  9716. return this.finishNode(node, type);
  9717. } else {
  9718. throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
  9719. at: this.state.startLoc
  9720. });
  9721. }
  9722. } else {
  9723. this.parseClassId(node, isStatement, optionalId);
  9724. }
  9725. super.parseClassSuper(node);
  9726. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  9727. return this.finishNode(node, type);
  9728. }
  9729. parseExport(node) {
  9730. const placeholder = this.parsePlaceholder("Identifier");
  9731. if (!placeholder) return super.parseExport(node);
  9732. if (!this.isContextual(97) && !this.match(12)) {
  9733. node.specifiers = [];
  9734. node.source = null;
  9735. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  9736. return this.finishNode(node, "ExportNamedDeclaration");
  9737. }
  9738. this.expectPlugin("exportDefaultFrom");
  9739. const specifier = this.startNode();
  9740. specifier.exported = placeholder;
  9741. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  9742. return super.parseExport(node);
  9743. }
  9744. isExportDefaultSpecifier() {
  9745. if (this.match(65)) {
  9746. const next = this.nextTokenStart();
  9747. if (this.isUnparsedContextual(next, "from")) {
  9748. if (this.input.startsWith(tokenLabelName(140), this.nextTokenStartSince(next + 4))) {
  9749. return true;
  9750. }
  9751. }
  9752. }
  9753. return super.isExportDefaultSpecifier();
  9754. }
  9755. maybeParseExportDefaultSpecifier(node) {
  9756. if (node.specifiers && node.specifiers.length > 0) {
  9757. return true;
  9758. }
  9759. return super.maybeParseExportDefaultSpecifier(node);
  9760. }
  9761. checkExport(node) {
  9762. const {
  9763. specifiers
  9764. } = node;
  9765. if (specifiers != null && specifiers.length) {
  9766. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  9767. }
  9768. super.checkExport(node);
  9769. node.specifiers = specifiers;
  9770. }
  9771. parseImport(node) {
  9772. const placeholder = this.parsePlaceholder("Identifier");
  9773. if (!placeholder) return super.parseImport(node);
  9774. node.specifiers = [];
  9775. if (!this.isContextual(97) && !this.match(12)) {
  9776. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  9777. this.semicolon();
  9778. return this.finishNode(node, "ImportDeclaration");
  9779. }
  9780. const specifier = this.startNodeAtNode(placeholder);
  9781. specifier.local = placeholder;
  9782. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  9783. if (this.eat(12)) {
  9784. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  9785. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  9786. }
  9787. this.expectContextual(97);
  9788. node.source = this.parseImportSource();
  9789. this.semicolon();
  9790. return this.finishNode(node, "ImportDeclaration");
  9791. }
  9792. parseImportSource() {
  9793. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  9794. }
  9795. assertNoSpace() {
  9796. if (this.state.start > this.state.lastTokEndLoc.index) {
  9797. this.raise(PlaceholderErrors.UnexpectedSpace, {
  9798. at: this.state.lastTokEndLoc
  9799. });
  9800. }
  9801. }
  9802. });
  9803. var v8intrinsic = (superClass => class V8IntrinsicMixin extends superClass {
  9804. parseV8Intrinsic() {
  9805. if (this.match(54)) {
  9806. const v8IntrinsicStartLoc = this.state.startLoc;
  9807. const node = this.startNode();
  9808. this.next();
  9809. if (tokenIsIdentifier(this.state.type)) {
  9810. const name = this.parseIdentifierName(this.state.start);
  9811. const identifier = this.createIdentifier(node, name);
  9812. identifier.type = "V8IntrinsicIdentifier";
  9813. if (this.match(10)) {
  9814. return identifier;
  9815. }
  9816. }
  9817. this.unexpected(v8IntrinsicStartLoc);
  9818. }
  9819. }
  9820. parseExprAtom(refExpressionErrors) {
  9821. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  9822. }
  9823. });
  9824. function hasPlugin(plugins, expectedConfig) {
  9825. const [expectedName, expectedOptions] = typeof expectedConfig === "string" ? [expectedConfig, {}] : expectedConfig;
  9826. const expectedKeys = Object.keys(expectedOptions);
  9827. const expectedOptionsIsEmpty = expectedKeys.length === 0;
  9828. return plugins.some(p => {
  9829. if (typeof p === "string") {
  9830. return expectedOptionsIsEmpty && p === expectedName;
  9831. } else {
  9832. const [pluginName, pluginOptions] = p;
  9833. if (pluginName !== expectedName) {
  9834. return false;
  9835. }
  9836. for (const key of expectedKeys) {
  9837. if (pluginOptions[key] !== expectedOptions[key]) {
  9838. return false;
  9839. }
  9840. }
  9841. return true;
  9842. }
  9843. });
  9844. }
  9845. function getPluginOption(plugins, name, option) {
  9846. const plugin = plugins.find(plugin => {
  9847. if (Array.isArray(plugin)) {
  9848. return plugin[0] === name;
  9849. } else {
  9850. return plugin === name;
  9851. }
  9852. });
  9853. if (plugin && Array.isArray(plugin) && plugin.length > 1) {
  9854. return plugin[1][option];
  9855. }
  9856. return null;
  9857. }
  9858. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  9859. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  9860. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  9861. function validatePlugins(plugins) {
  9862. if (hasPlugin(plugins, "decorators")) {
  9863. if (hasPlugin(plugins, "decorators-legacy")) {
  9864. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  9865. }
  9866. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  9867. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  9868. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  9869. }
  9870. const allowCallParenthesized = getPluginOption(plugins, "decorators", "allowCallParenthesized");
  9871. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  9872. throw new Error("'allowCallParenthesized' must be a boolean.");
  9873. }
  9874. }
  9875. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  9876. throw new Error("Cannot combine flow and typescript plugins.");
  9877. }
  9878. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  9879. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  9880. }
  9881. if (hasPlugin(plugins, "pipelineOperator")) {
  9882. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  9883. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  9884. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  9885. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  9886. }
  9887. const tupleSyntaxIsHash = hasPlugin(plugins, ["recordAndTuple", {
  9888. syntaxType: "hash"
  9889. }]);
  9890. if (proposal === "hack") {
  9891. if (hasPlugin(plugins, "placeholders")) {
  9892. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  9893. }
  9894. if (hasPlugin(plugins, "v8intrinsic")) {
  9895. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  9896. }
  9897. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  9898. if (!TOPIC_TOKENS.includes(topicToken)) {
  9899. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  9900. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  9901. }
  9902. if (topicToken === "#" && tupleSyntaxIsHash) {
  9903. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9904. }
  9905. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  9906. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  9907. }
  9908. }
  9909. if (hasPlugin(plugins, "moduleAttributes")) {
  9910. {
  9911. if (hasPlugin(plugins, "importAssertions")) {
  9912. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  9913. }
  9914. const moduleAttributesVersionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  9915. if (moduleAttributesVersionPluginOption !== "may-2020") {
  9916. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  9917. }
  9918. }
  9919. }
  9920. if (hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") != null && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  9921. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  9922. }
  9923. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  9924. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  9925. error.missingPlugins = "doExpressions";
  9926. throw error;
  9927. }
  9928. }
  9929. const mixinPlugins = {
  9930. estree,
  9931. jsx,
  9932. flow,
  9933. typescript,
  9934. v8intrinsic,
  9935. placeholders
  9936. };
  9937. const mixinPluginNames = Object.keys(mixinPlugins);
  9938. const defaultOptions = {
  9939. sourceType: "script",
  9940. sourceFilename: undefined,
  9941. startColumn: 0,
  9942. startLine: 1,
  9943. allowAwaitOutsideFunction: false,
  9944. allowReturnOutsideFunction: false,
  9945. allowImportExportEverywhere: false,
  9946. allowSuperOutsideMethod: false,
  9947. allowUndeclaredExports: false,
  9948. plugins: [],
  9949. strictMode: null,
  9950. ranges: false,
  9951. tokens: false,
  9952. createParenthesizedExpressions: false,
  9953. errorRecovery: false,
  9954. attachComment: true
  9955. };
  9956. function getOptions(opts) {
  9957. const options = {};
  9958. for (const key of Object.keys(defaultOptions)) {
  9959. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  9960. }
  9961. return options;
  9962. }
  9963. const getOwn = (object, key) => Object.hasOwnProperty.call(object, key) && object[key];
  9964. const unwrapParenthesizedExpression = node => {
  9965. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  9966. };
  9967. class LValParser extends NodeUtils {
  9968. toAssignable(node, isLHS = false) {
  9969. var _node$extra, _node$extra3;
  9970. let parenthesized = undefined;
  9971. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  9972. parenthesized = unwrapParenthesizedExpression(node);
  9973. if (isLHS) {
  9974. if (parenthesized.type === "Identifier") {
  9975. this.expressionScope.recordArrowParemeterBindingError(Errors.InvalidParenthesizedAssignment, {
  9976. at: node
  9977. });
  9978. } else if (parenthesized.type !== "MemberExpression") {
  9979. this.raise(Errors.InvalidParenthesizedAssignment, {
  9980. at: node
  9981. });
  9982. }
  9983. } else {
  9984. this.raise(Errors.InvalidParenthesizedAssignment, {
  9985. at: node
  9986. });
  9987. }
  9988. }
  9989. switch (node.type) {
  9990. case "Identifier":
  9991. case "ObjectPattern":
  9992. case "ArrayPattern":
  9993. case "AssignmentPattern":
  9994. case "RestElement":
  9995. break;
  9996. case "ObjectExpression":
  9997. node.type = "ObjectPattern";
  9998. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  9999. var _node$extra2;
  10000. const prop = node.properties[i];
  10001. const isLast = i === last;
  10002. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  10003. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  10004. this.raise(Errors.RestTrailingComma, {
  10005. at: node.extra.trailingCommaLoc
  10006. });
  10007. }
  10008. }
  10009. break;
  10010. case "ObjectProperty":
  10011. {
  10012. const {
  10013. key,
  10014. value
  10015. } = node;
  10016. if (this.isPrivateName(key)) {
  10017. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  10018. }
  10019. this.toAssignable(value, isLHS);
  10020. break;
  10021. }
  10022. case "SpreadElement":
  10023. {
  10024. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  10025. }
  10026. case "ArrayExpression":
  10027. node.type = "ArrayPattern";
  10028. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  10029. break;
  10030. case "AssignmentExpression":
  10031. if (node.operator !== "=") {
  10032. this.raise(Errors.MissingEqInAssignment, {
  10033. at: node.left.loc.end
  10034. });
  10035. }
  10036. node.type = "AssignmentPattern";
  10037. delete node.operator;
  10038. this.toAssignable(node.left, isLHS);
  10039. break;
  10040. case "ParenthesizedExpression":
  10041. this.toAssignable(parenthesized, isLHS);
  10042. break;
  10043. }
  10044. }
  10045. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  10046. if (prop.type === "ObjectMethod") {
  10047. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, {
  10048. at: prop.key
  10049. });
  10050. } else if (prop.type === "SpreadElement") {
  10051. prop.type = "RestElement";
  10052. const arg = prop.argument;
  10053. this.checkToRestConversion(arg, false);
  10054. this.toAssignable(arg, isLHS);
  10055. if (!isLast) {
  10056. this.raise(Errors.RestTrailingComma, {
  10057. at: prop
  10058. });
  10059. }
  10060. } else {
  10061. this.toAssignable(prop, isLHS);
  10062. }
  10063. }
  10064. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  10065. const end = exprList.length - 1;
  10066. for (let i = 0; i <= end; i++) {
  10067. const elt = exprList[i];
  10068. if (!elt) continue;
  10069. if (elt.type === "SpreadElement") {
  10070. elt.type = "RestElement";
  10071. const arg = elt.argument;
  10072. this.checkToRestConversion(arg, true);
  10073. this.toAssignable(arg, isLHS);
  10074. } else {
  10075. this.toAssignable(elt, isLHS);
  10076. }
  10077. if (elt.type === "RestElement") {
  10078. if (i < end) {
  10079. this.raise(Errors.RestTrailingComma, {
  10080. at: elt
  10081. });
  10082. } else if (trailingCommaLoc) {
  10083. this.raise(Errors.RestTrailingComma, {
  10084. at: trailingCommaLoc
  10085. });
  10086. }
  10087. }
  10088. }
  10089. }
  10090. isAssignable(node, isBinding) {
  10091. switch (node.type) {
  10092. case "Identifier":
  10093. case "ObjectPattern":
  10094. case "ArrayPattern":
  10095. case "AssignmentPattern":
  10096. case "RestElement":
  10097. return true;
  10098. case "ObjectExpression":
  10099. {
  10100. const last = node.properties.length - 1;
  10101. return node.properties.every((prop, i) => {
  10102. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  10103. });
  10104. }
  10105. case "ObjectProperty":
  10106. return this.isAssignable(node.value);
  10107. case "SpreadElement":
  10108. return this.isAssignable(node.argument);
  10109. case "ArrayExpression":
  10110. return node.elements.every(element => element === null || this.isAssignable(element));
  10111. case "AssignmentExpression":
  10112. return node.operator === "=";
  10113. case "ParenthesizedExpression":
  10114. return this.isAssignable(node.expression);
  10115. case "MemberExpression":
  10116. case "OptionalMemberExpression":
  10117. return !isBinding;
  10118. default:
  10119. return false;
  10120. }
  10121. }
  10122. toReferencedList(exprList, isParenthesizedExpr) {
  10123. return exprList;
  10124. }
  10125. toReferencedListDeep(exprList, isParenthesizedExpr) {
  10126. this.toReferencedList(exprList, isParenthesizedExpr);
  10127. for (const expr of exprList) {
  10128. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  10129. this.toReferencedListDeep(expr.elements);
  10130. }
  10131. }
  10132. }
  10133. parseSpread(refExpressionErrors) {
  10134. const node = this.startNode();
  10135. this.next();
  10136. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  10137. return this.finishNode(node, "SpreadElement");
  10138. }
  10139. parseRestBinding() {
  10140. const node = this.startNode();
  10141. this.next();
  10142. node.argument = this.parseBindingAtom();
  10143. return this.finishNode(node, "RestElement");
  10144. }
  10145. parseBindingAtom() {
  10146. switch (this.state.type) {
  10147. case 0:
  10148. {
  10149. const node = this.startNode();
  10150. this.next();
  10151. node.elements = this.parseBindingList(3, 93, true);
  10152. return this.finishNode(node, "ArrayPattern");
  10153. }
  10154. case 5:
  10155. return this.parseObjectLike(8, true);
  10156. }
  10157. return this.parseIdentifier();
  10158. }
  10159. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  10160. const elts = [];
  10161. let first = true;
  10162. while (!this.eat(close)) {
  10163. if (first) {
  10164. first = false;
  10165. } else {
  10166. this.expect(12);
  10167. }
  10168. if (allowEmpty && this.match(12)) {
  10169. elts.push(null);
  10170. } else if (this.eat(close)) {
  10171. break;
  10172. } else if (this.match(21)) {
  10173. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  10174. if (!this.checkCommaAfterRest(closeCharCode)) {
  10175. this.expect(close);
  10176. break;
  10177. }
  10178. } else {
  10179. const decorators = [];
  10180. if (this.match(26) && this.hasPlugin("decorators")) {
  10181. this.raise(Errors.UnsupportedParameterDecorator, {
  10182. at: this.state.startLoc
  10183. });
  10184. }
  10185. while (this.match(26)) {
  10186. decorators.push(this.parseDecorator());
  10187. }
  10188. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  10189. }
  10190. }
  10191. return elts;
  10192. }
  10193. parseBindingRestProperty(prop) {
  10194. this.next();
  10195. prop.argument = this.parseIdentifier();
  10196. this.checkCommaAfterRest(125);
  10197. return this.finishNode(prop, "RestElement");
  10198. }
  10199. parseBindingProperty() {
  10200. const prop = this.startNode();
  10201. const {
  10202. type,
  10203. start: startPos,
  10204. startLoc
  10205. } = this.state;
  10206. if (type === 21) {
  10207. return this.parseBindingRestProperty(prop);
  10208. } else if (type === 134) {
  10209. this.expectPlugin("destructuringPrivate", startLoc);
  10210. this.classScope.usePrivateName(this.state.value, startLoc);
  10211. prop.key = this.parsePrivateName();
  10212. } else {
  10213. this.parsePropertyName(prop);
  10214. }
  10215. prop.method = false;
  10216. return this.parseObjPropValue(prop, startPos, startLoc, false, false, true, false);
  10217. }
  10218. parseAssignableListItem(allowModifiers, decorators) {
  10219. const left = this.parseMaybeDefault();
  10220. this.parseAssignableListItemTypes(left);
  10221. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  10222. if (decorators.length) {
  10223. left.decorators = decorators;
  10224. }
  10225. return elt;
  10226. }
  10227. parseAssignableListItemTypes(param) {
  10228. return param;
  10229. }
  10230. parseMaybeDefault(startPos, startLoc, left) {
  10231. var _startLoc, _startPos, _left;
  10232. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  10233. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  10234. left = (_left = left) != null ? _left : this.parseBindingAtom();
  10235. if (!this.eat(29)) return left;
  10236. const node = this.startNodeAt(startPos, startLoc);
  10237. node.left = left;
  10238. node.right = this.parseMaybeAssignAllowIn();
  10239. return this.finishNode(node, "AssignmentPattern");
  10240. }
  10241. isValidLVal(type, isUnparenthesizedInAssign, binding) {
  10242. return getOwn({
  10243. AssignmentPattern: "left",
  10244. RestElement: "argument",
  10245. ObjectProperty: "value",
  10246. ParenthesizedExpression: "expression",
  10247. ArrayPattern: "elements",
  10248. ObjectPattern: "properties"
  10249. }, type);
  10250. }
  10251. checkLVal(expression, {
  10252. in: ancestor,
  10253. binding = BIND_NONE,
  10254. checkClashes = false,
  10255. strictModeChanged = false,
  10256. allowingSloppyLetBinding = !(binding & BIND_SCOPE_LEXICAL),
  10257. hasParenthesizedAncestor = false
  10258. }) {
  10259. var _expression$extra;
  10260. const type = expression.type;
  10261. if (this.isObjectMethod(expression)) return;
  10262. if (type === "MemberExpression") {
  10263. if (binding !== BIND_NONE) {
  10264. this.raise(Errors.InvalidPropertyBindingPattern, {
  10265. at: expression
  10266. });
  10267. }
  10268. return;
  10269. }
  10270. if (expression.type === "Identifier") {
  10271. this.checkIdentifier(expression, binding, strictModeChanged, allowingSloppyLetBinding);
  10272. const {
  10273. name
  10274. } = expression;
  10275. if (checkClashes) {
  10276. if (checkClashes.has(name)) {
  10277. this.raise(Errors.ParamDupe, {
  10278. at: expression
  10279. });
  10280. } else {
  10281. checkClashes.add(name);
  10282. }
  10283. }
  10284. return;
  10285. }
  10286. const validity = this.isValidLVal(expression.type, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  10287. if (validity === true) return;
  10288. if (validity === false) {
  10289. const ParseErrorClass = binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  10290. this.raise(ParseErrorClass, {
  10291. at: expression,
  10292. ancestor: ancestor.type === "UpdateExpression" ? {
  10293. type: "UpdateExpression",
  10294. prefix: ancestor.prefix
  10295. } : {
  10296. type: ancestor.type
  10297. }
  10298. });
  10299. return;
  10300. }
  10301. const [key, isParenthesizedExpression] = Array.isArray(validity) ? validity : [validity, type === "ParenthesizedExpression"];
  10302. const nextAncestor = expression.type === "ArrayPattern" || expression.type === "ObjectPattern" || expression.type === "ParenthesizedExpression" ? expression : ancestor;
  10303. for (const child of [].concat(expression[key])) {
  10304. if (child) {
  10305. this.checkLVal(child, {
  10306. in: nextAncestor,
  10307. binding,
  10308. checkClashes,
  10309. allowingSloppyLetBinding,
  10310. strictModeChanged,
  10311. hasParenthesizedAncestor: isParenthesizedExpression
  10312. });
  10313. }
  10314. }
  10315. }
  10316. checkIdentifier(at, bindingType, strictModeChanged = false, allowLetBinding = !(bindingType & BIND_SCOPE_LEXICAL)) {
  10317. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  10318. if (bindingType === BIND_NONE) {
  10319. this.raise(Errors.StrictEvalArguments, {
  10320. at,
  10321. referenceName: at.name
  10322. });
  10323. } else {
  10324. this.raise(Errors.StrictEvalArgumentsBinding, {
  10325. at,
  10326. bindingName: at.name
  10327. });
  10328. }
  10329. }
  10330. if (!allowLetBinding && at.name === "let") {
  10331. this.raise(Errors.LetInLexicalBinding, {
  10332. at
  10333. });
  10334. }
  10335. if (!(bindingType & BIND_NONE)) {
  10336. this.declareNameFromIdentifier(at, bindingType);
  10337. }
  10338. }
  10339. declareNameFromIdentifier(identifier, binding) {
  10340. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  10341. }
  10342. checkToRestConversion(node, allowPattern) {
  10343. switch (node.type) {
  10344. case "ParenthesizedExpression":
  10345. this.checkToRestConversion(node.expression, allowPattern);
  10346. break;
  10347. case "Identifier":
  10348. case "MemberExpression":
  10349. break;
  10350. case "ArrayExpression":
  10351. case "ObjectExpression":
  10352. if (allowPattern) break;
  10353. default:
  10354. this.raise(Errors.InvalidRestAssignmentPattern, {
  10355. at: node
  10356. });
  10357. }
  10358. }
  10359. checkCommaAfterRest(close) {
  10360. if (!this.match(12)) {
  10361. return false;
  10362. }
  10363. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, {
  10364. at: this.state.startLoc
  10365. });
  10366. return true;
  10367. }
  10368. }
  10369. class ExpressionParser extends LValParser {
  10370. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  10371. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10372. return;
  10373. }
  10374. const key = prop.key;
  10375. const name = key.type === "Identifier" ? key.name : key.value;
  10376. if (name === "__proto__") {
  10377. if (isRecord) {
  10378. this.raise(Errors.RecordNoProto, {
  10379. at: key
  10380. });
  10381. return;
  10382. }
  10383. if (protoRef.used) {
  10384. if (refExpressionErrors) {
  10385. if (refExpressionErrors.doubleProtoLoc === null) {
  10386. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10387. }
  10388. } else {
  10389. this.raise(Errors.DuplicateProto, {
  10390. at: key
  10391. });
  10392. }
  10393. }
  10394. protoRef.used = true;
  10395. }
  10396. }
  10397. shouldExitDescending(expr, potentialArrowAt) {
  10398. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  10399. }
  10400. getExpression() {
  10401. this.enterInitialScopes();
  10402. this.nextToken();
  10403. const expr = this.parseExpression();
  10404. if (!this.match(135)) {
  10405. this.unexpected();
  10406. }
  10407. this.finalizeRemainingComments();
  10408. expr.comments = this.state.comments;
  10409. expr.errors = this.state.errors;
  10410. if (this.options.tokens) {
  10411. expr.tokens = this.tokens;
  10412. }
  10413. return expr;
  10414. }
  10415. parseExpression(disallowIn, refExpressionErrors) {
  10416. if (disallowIn) {
  10417. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10418. }
  10419. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10420. }
  10421. parseExpressionBase(refExpressionErrors) {
  10422. const startPos = this.state.start;
  10423. const startLoc = this.state.startLoc;
  10424. const expr = this.parseMaybeAssign(refExpressionErrors);
  10425. if (this.match(12)) {
  10426. const node = this.startNodeAt(startPos, startLoc);
  10427. node.expressions = [expr];
  10428. while (this.eat(12)) {
  10429. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10430. }
  10431. this.toReferencedList(node.expressions);
  10432. return this.finishNode(node, "SequenceExpression");
  10433. }
  10434. return expr;
  10435. }
  10436. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10437. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10438. }
  10439. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10440. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10441. }
  10442. setOptionalParametersError(refExpressionErrors, resultError) {
  10443. var _resultError$loc;
  10444. refExpressionErrors.optionalParametersLoc = (_resultError$loc = resultError == null ? void 0 : resultError.loc) != null ? _resultError$loc : this.state.startLoc;
  10445. }
  10446. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10447. const startPos = this.state.start;
  10448. const startLoc = this.state.startLoc;
  10449. if (this.isContextual(105)) {
  10450. if (this.prodParam.hasYield) {
  10451. let left = this.parseYield();
  10452. if (afterLeftParse) {
  10453. left = afterLeftParse.call(this, left, startPos, startLoc);
  10454. }
  10455. return left;
  10456. }
  10457. }
  10458. let ownExpressionErrors;
  10459. if (refExpressionErrors) {
  10460. ownExpressionErrors = false;
  10461. } else {
  10462. refExpressionErrors = new ExpressionErrors();
  10463. ownExpressionErrors = true;
  10464. }
  10465. const {
  10466. type
  10467. } = this.state;
  10468. if (type === 10 || tokenIsIdentifier(type)) {
  10469. this.state.potentialArrowAt = this.state.start;
  10470. }
  10471. let left = this.parseMaybeConditional(refExpressionErrors);
  10472. if (afterLeftParse) {
  10473. left = afterLeftParse.call(this, left, startPos, startLoc);
  10474. }
  10475. if (tokenIsAssignment(this.state.type)) {
  10476. const node = this.startNodeAt(startPos, startLoc);
  10477. const operator = this.state.value;
  10478. node.operator = operator;
  10479. if (this.match(29)) {
  10480. this.toAssignable(left, true);
  10481. node.left = left;
  10482. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startPos) {
  10483. refExpressionErrors.doubleProtoLoc = null;
  10484. }
  10485. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startPos) {
  10486. refExpressionErrors.shorthandAssignLoc = null;
  10487. }
  10488. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startPos) {
  10489. this.checkDestructuringPrivate(refExpressionErrors);
  10490. refExpressionErrors.privateKeyLoc = null;
  10491. }
  10492. } else {
  10493. node.left = left;
  10494. }
  10495. this.next();
  10496. node.right = this.parseMaybeAssign();
  10497. this.checkLVal(left, {
  10498. in: this.finishNode(node, "AssignmentExpression")
  10499. });
  10500. return node;
  10501. } else if (ownExpressionErrors) {
  10502. this.checkExpressionErrors(refExpressionErrors, true);
  10503. }
  10504. return left;
  10505. }
  10506. parseMaybeConditional(refExpressionErrors) {
  10507. const startPos = this.state.start;
  10508. const startLoc = this.state.startLoc;
  10509. const potentialArrowAt = this.state.potentialArrowAt;
  10510. const expr = this.parseExprOps(refExpressionErrors);
  10511. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10512. return expr;
  10513. }
  10514. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  10515. }
  10516. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  10517. if (this.eat(17)) {
  10518. const node = this.startNodeAt(startPos, startLoc);
  10519. node.test = expr;
  10520. node.consequent = this.parseMaybeAssignAllowIn();
  10521. this.expect(14);
  10522. node.alternate = this.parseMaybeAssign();
  10523. return this.finishNode(node, "ConditionalExpression");
  10524. }
  10525. return expr;
  10526. }
  10527. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10528. return this.match(134) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10529. }
  10530. parseExprOps(refExpressionErrors) {
  10531. const startPos = this.state.start;
  10532. const startLoc = this.state.startLoc;
  10533. const potentialArrowAt = this.state.potentialArrowAt;
  10534. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10535. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10536. return expr;
  10537. }
  10538. return this.parseExprOp(expr, startPos, startLoc, -1);
  10539. }
  10540. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  10541. if (this.isPrivateName(left)) {
  10542. const value = this.getPrivateNameSV(left);
  10543. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10544. this.raise(Errors.PrivateInExpectedIn, {
  10545. at: left,
  10546. identifierName: value
  10547. });
  10548. }
  10549. this.classScope.usePrivateName(value, left.loc.start);
  10550. }
  10551. const op = this.state.type;
  10552. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10553. let prec = tokenOperatorPrecedence(op);
  10554. if (prec > minPrec) {
  10555. if (op === 39) {
  10556. this.expectPlugin("pipelineOperator");
  10557. if (this.state.inFSharpPipelineDirectBody) {
  10558. return left;
  10559. }
  10560. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10561. }
  10562. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  10563. node.left = left;
  10564. node.operator = this.state.value;
  10565. const logical = op === 41 || op === 42;
  10566. const coalesce = op === 40;
  10567. if (coalesce) {
  10568. prec = tokenOperatorPrecedence(42);
  10569. }
  10570. this.next();
  10571. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10572. proposal: "minimal"
  10573. }])) {
  10574. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10575. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, {
  10576. at: this.state.startLoc
  10577. });
  10578. }
  10579. }
  10580. node.right = this.parseExprOpRightExpr(op, prec);
  10581. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10582. const nextOp = this.state.type;
  10583. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10584. throw this.raise(Errors.MixingCoalesceWithLogical, {
  10585. at: this.state.startLoc
  10586. });
  10587. }
  10588. return this.parseExprOp(finishedNode, leftStartPos, leftStartLoc, minPrec);
  10589. }
  10590. }
  10591. return left;
  10592. }
  10593. parseExprOpRightExpr(op, prec) {
  10594. const startPos = this.state.start;
  10595. const startLoc = this.state.startLoc;
  10596. switch (op) {
  10597. case 39:
  10598. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10599. case "hack":
  10600. return this.withTopicBindingContext(() => {
  10601. return this.parseHackPipeBody();
  10602. });
  10603. case "smart":
  10604. return this.withTopicBindingContext(() => {
  10605. if (this.prodParam.hasYield && this.isContextual(105)) {
  10606. throw this.raise(Errors.PipeBodyIsTighter, {
  10607. at: this.state.startLoc
  10608. });
  10609. }
  10610. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
  10611. });
  10612. case "fsharp":
  10613. return this.withSoloAwaitPermittingContext(() => {
  10614. return this.parseFSharpPipelineBody(prec);
  10615. });
  10616. }
  10617. default:
  10618. return this.parseExprOpBaseRightExpr(op, prec);
  10619. }
  10620. }
  10621. parseExprOpBaseRightExpr(op, prec) {
  10622. const startPos = this.state.start;
  10623. const startLoc = this.state.startLoc;
  10624. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  10625. }
  10626. parseHackPipeBody() {
  10627. var _body$extra;
  10628. const {
  10629. startLoc
  10630. } = this.state;
  10631. const body = this.parseMaybeAssign();
  10632. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  10633. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  10634. this.raise(Errors.PipeUnparenthesizedBody, {
  10635. at: startLoc,
  10636. type: body.type
  10637. });
  10638. }
  10639. if (!this.topicReferenceWasUsedInCurrentContext()) {
  10640. this.raise(Errors.PipeTopicUnused, {
  10641. at: startLoc
  10642. });
  10643. }
  10644. return body;
  10645. }
  10646. checkExponentialAfterUnary(node) {
  10647. if (this.match(57)) {
  10648. this.raise(Errors.UnexpectedTokenUnaryExponentiation, {
  10649. at: node.argument
  10650. });
  10651. }
  10652. }
  10653. parseMaybeUnary(refExpressionErrors, sawUnary) {
  10654. const startPos = this.state.start;
  10655. const startLoc = this.state.startLoc;
  10656. const isAwait = this.isContextual(96);
  10657. if (isAwait && this.isAwaitAllowed()) {
  10658. this.next();
  10659. const expr = this.parseAwait(startPos, startLoc);
  10660. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  10661. return expr;
  10662. }
  10663. const update = this.match(34);
  10664. const node = this.startNode();
  10665. if (tokenIsPrefix(this.state.type)) {
  10666. node.operator = this.state.value;
  10667. node.prefix = true;
  10668. if (this.match(72)) {
  10669. this.expectPlugin("throwExpressions");
  10670. }
  10671. const isDelete = this.match(89);
  10672. this.next();
  10673. node.argument = this.parseMaybeUnary(null, true);
  10674. this.checkExpressionErrors(refExpressionErrors, true);
  10675. if (this.state.strict && isDelete) {
  10676. const arg = node.argument;
  10677. if (arg.type === "Identifier") {
  10678. this.raise(Errors.StrictDelete, {
  10679. at: node
  10680. });
  10681. } else if (this.hasPropertyAsPrivateName(arg)) {
  10682. this.raise(Errors.DeletePrivateField, {
  10683. at: node
  10684. });
  10685. }
  10686. }
  10687. if (!update) {
  10688. if (!sawUnary) {
  10689. this.checkExponentialAfterUnary(node);
  10690. }
  10691. return this.finishNode(node, "UnaryExpression");
  10692. }
  10693. }
  10694. const expr = this.parseUpdate(node, update, refExpressionErrors);
  10695. if (isAwait) {
  10696. const {
  10697. type
  10698. } = this.state;
  10699. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10700. if (startsExpr && !this.isAmbiguousAwait()) {
  10701. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, {
  10702. at: startLoc
  10703. });
  10704. return this.parseAwait(startPos, startLoc);
  10705. }
  10706. }
  10707. return expr;
  10708. }
  10709. parseUpdate(node, update, refExpressionErrors) {
  10710. if (update) {
  10711. const updateExpressionNode = node;
  10712. this.checkLVal(updateExpressionNode.argument, {
  10713. in: this.finishNode(updateExpressionNode, "UpdateExpression")
  10714. });
  10715. return node;
  10716. }
  10717. const startPos = this.state.start;
  10718. const startLoc = this.state.startLoc;
  10719. let expr = this.parseExprSubscripts(refExpressionErrors);
  10720. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  10721. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  10722. const node = this.startNodeAt(startPos, startLoc);
  10723. node.operator = this.state.value;
  10724. node.prefix = false;
  10725. node.argument = expr;
  10726. this.next();
  10727. this.checkLVal(expr, {
  10728. in: expr = this.finishNode(node, "UpdateExpression")
  10729. });
  10730. }
  10731. return expr;
  10732. }
  10733. parseExprSubscripts(refExpressionErrors) {
  10734. const startPos = this.state.start;
  10735. const startLoc = this.state.startLoc;
  10736. const potentialArrowAt = this.state.potentialArrowAt;
  10737. const expr = this.parseExprAtom(refExpressionErrors);
  10738. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10739. return expr;
  10740. }
  10741. return this.parseSubscripts(expr, startPos, startLoc);
  10742. }
  10743. parseSubscripts(base, startPos, startLoc, noCalls) {
  10744. const state = {
  10745. optionalChainMember: false,
  10746. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  10747. stop: false
  10748. };
  10749. do {
  10750. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  10751. state.maybeAsyncArrow = false;
  10752. } while (!state.stop);
  10753. return base;
  10754. }
  10755. parseSubscript(base, startPos, startLoc, noCalls, state) {
  10756. const {
  10757. type
  10758. } = this.state;
  10759. if (!noCalls && type === 15) {
  10760. return this.parseBind(base, startPos, startLoc, noCalls, state);
  10761. } else if (tokenIsTemplate(type)) {
  10762. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  10763. }
  10764. let optional = false;
  10765. if (type === 18) {
  10766. if (noCalls && this.lookaheadCharCode() === 40) {
  10767. state.stop = true;
  10768. return base;
  10769. }
  10770. state.optionalChainMember = optional = true;
  10771. this.next();
  10772. }
  10773. if (!noCalls && this.match(10)) {
  10774. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  10775. } else {
  10776. const computed = this.eat(0);
  10777. if (computed || optional || this.eat(16)) {
  10778. return this.parseMember(base, startPos, startLoc, state, computed, optional);
  10779. } else {
  10780. state.stop = true;
  10781. return base;
  10782. }
  10783. }
  10784. }
  10785. parseMember(base, startPos, startLoc, state, computed, optional) {
  10786. const node = this.startNodeAt(startPos, startLoc);
  10787. node.object = base;
  10788. node.computed = computed;
  10789. if (computed) {
  10790. node.property = this.parseExpression();
  10791. this.expect(3);
  10792. } else if (this.match(134)) {
  10793. if (base.type === "Super") {
  10794. this.raise(Errors.SuperPrivateField, {
  10795. at: startLoc
  10796. });
  10797. }
  10798. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  10799. node.property = this.parsePrivateName();
  10800. } else {
  10801. node.property = this.parseIdentifier(true);
  10802. }
  10803. if (state.optionalChainMember) {
  10804. node.optional = optional;
  10805. return this.finishNode(node, "OptionalMemberExpression");
  10806. } else {
  10807. return this.finishNode(node, "MemberExpression");
  10808. }
  10809. }
  10810. parseBind(base, startPos, startLoc, noCalls, state) {
  10811. const node = this.startNodeAt(startPos, startLoc);
  10812. node.object = base;
  10813. this.next();
  10814. node.callee = this.parseNoCallExpr();
  10815. state.stop = true;
  10816. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  10817. }
  10818. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  10819. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10820. let refExpressionErrors = null;
  10821. this.state.maybeInArrowParameters = true;
  10822. this.next();
  10823. const node = this.startNodeAt(startPos, startLoc);
  10824. node.callee = base;
  10825. const {
  10826. maybeAsyncArrow,
  10827. optionalChainMember
  10828. } = state;
  10829. if (maybeAsyncArrow) {
  10830. this.expressionScope.enter(newAsyncArrowScope());
  10831. refExpressionErrors = new ExpressionErrors();
  10832. }
  10833. if (optionalChainMember) {
  10834. node.optional = optional;
  10835. }
  10836. if (optional) {
  10837. node.arguments = this.parseCallExpressionArguments(11);
  10838. } else {
  10839. node.arguments = this.parseCallExpressionArguments(11, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  10840. }
  10841. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  10842. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  10843. state.stop = true;
  10844. this.checkDestructuringPrivate(refExpressionErrors);
  10845. this.expressionScope.validateAsPattern();
  10846. this.expressionScope.exit();
  10847. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), finishedNode);
  10848. } else {
  10849. if (maybeAsyncArrow) {
  10850. this.checkExpressionErrors(refExpressionErrors, true);
  10851. this.expressionScope.exit();
  10852. }
  10853. this.toReferencedArguments(finishedNode);
  10854. }
  10855. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10856. return finishedNode;
  10857. }
  10858. toReferencedArguments(node, isParenthesizedExpr) {
  10859. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  10860. }
  10861. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  10862. const node = this.startNodeAt(startPos, startLoc);
  10863. node.tag = base;
  10864. node.quasi = this.parseTemplate(true);
  10865. if (state.optionalChainMember) {
  10866. this.raise(Errors.OptionalChainingNoTemplate, {
  10867. at: startLoc
  10868. });
  10869. }
  10870. return this.finishNode(node, "TaggedTemplateExpression");
  10871. }
  10872. atPossibleAsyncArrow(base) {
  10873. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  10874. }
  10875. finishCallExpression(node, optional) {
  10876. if (node.callee.type === "Import") {
  10877. if (node.arguments.length === 2) {
  10878. {
  10879. if (!this.hasPlugin("moduleAttributes")) {
  10880. this.expectPlugin("importAssertions");
  10881. }
  10882. }
  10883. }
  10884. if (node.arguments.length === 0 || node.arguments.length > 2) {
  10885. this.raise(Errors.ImportCallArity, {
  10886. at: node,
  10887. maxArgumentCount: this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 : 1
  10888. });
  10889. } else {
  10890. for (const arg of node.arguments) {
  10891. if (arg.type === "SpreadElement") {
  10892. this.raise(Errors.ImportCallSpreadArgument, {
  10893. at: arg
  10894. });
  10895. }
  10896. }
  10897. }
  10898. }
  10899. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  10900. }
  10901. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  10902. const elts = [];
  10903. let first = true;
  10904. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10905. this.state.inFSharpPipelineDirectBody = false;
  10906. while (!this.eat(close)) {
  10907. if (first) {
  10908. first = false;
  10909. } else {
  10910. this.expect(12);
  10911. if (this.match(close)) {
  10912. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  10913. this.raise(Errors.ImportCallArgumentTrailingComma, {
  10914. at: this.state.lastTokStartLoc
  10915. });
  10916. }
  10917. if (nodeForExtra) {
  10918. this.addTrailingCommaExtraToNode(nodeForExtra);
  10919. }
  10920. this.next();
  10921. break;
  10922. }
  10923. }
  10924. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  10925. }
  10926. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10927. return elts;
  10928. }
  10929. shouldParseAsyncArrow() {
  10930. return this.match(19) && !this.canInsertSemicolon();
  10931. }
  10932. parseAsyncArrowFromCallExpression(node, call) {
  10933. var _call$extra;
  10934. this.resetPreviousNodeTrailingComments(call);
  10935. this.expect(19);
  10936. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  10937. if (call.innerComments) {
  10938. setInnerComments(node, call.innerComments);
  10939. }
  10940. if (call.callee.trailingComments) {
  10941. setInnerComments(node, call.callee.trailingComments);
  10942. }
  10943. return node;
  10944. }
  10945. parseNoCallExpr() {
  10946. const startPos = this.state.start;
  10947. const startLoc = this.state.startLoc;
  10948. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  10949. }
  10950. parseExprAtom(refExpressionErrors) {
  10951. let node;
  10952. const {
  10953. type
  10954. } = this.state;
  10955. switch (type) {
  10956. case 79:
  10957. return this.parseSuper();
  10958. case 83:
  10959. node = this.startNode();
  10960. this.next();
  10961. if (this.match(16)) {
  10962. return this.parseImportMetaProperty(node);
  10963. }
  10964. if (!this.match(10)) {
  10965. this.raise(Errors.UnsupportedImport, {
  10966. at: this.state.lastTokStartLoc
  10967. });
  10968. }
  10969. return this.finishNode(node, "Import");
  10970. case 78:
  10971. node = this.startNode();
  10972. this.next();
  10973. return this.finishNode(node, "ThisExpression");
  10974. case 90:
  10975. {
  10976. return this.parseDo(this.startNode(), false);
  10977. }
  10978. case 56:
  10979. case 31:
  10980. {
  10981. this.readRegexp();
  10982. return this.parseRegExpLiteral(this.state.value);
  10983. }
  10984. case 130:
  10985. return this.parseNumericLiteral(this.state.value);
  10986. case 131:
  10987. return this.parseBigIntLiteral(this.state.value);
  10988. case 132:
  10989. return this.parseDecimalLiteral(this.state.value);
  10990. case 129:
  10991. return this.parseStringLiteral(this.state.value);
  10992. case 84:
  10993. return this.parseNullLiteral();
  10994. case 85:
  10995. return this.parseBooleanLiteral(true);
  10996. case 86:
  10997. return this.parseBooleanLiteral(false);
  10998. case 10:
  10999. {
  11000. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11001. return this.parseParenAndDistinguishExpression(canBeArrow);
  11002. }
  11003. case 2:
  11004. case 1:
  11005. {
  11006. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, false, true);
  11007. }
  11008. case 0:
  11009. {
  11010. return this.parseArrayLike(3, true, false, refExpressionErrors);
  11011. }
  11012. case 6:
  11013. case 7:
  11014. {
  11015. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11016. }
  11017. case 5:
  11018. {
  11019. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11020. }
  11021. case 68:
  11022. return this.parseFunctionOrFunctionSent();
  11023. case 26:
  11024. this.parseDecorators();
  11025. case 80:
  11026. node = this.startNode();
  11027. this.takeDecorators(node);
  11028. return this.parseClass(node, false);
  11029. case 77:
  11030. return this.parseNewOrNewTarget();
  11031. case 25:
  11032. case 24:
  11033. return this.parseTemplate(false);
  11034. case 15:
  11035. {
  11036. node = this.startNode();
  11037. this.next();
  11038. node.object = null;
  11039. const callee = node.callee = this.parseNoCallExpr();
  11040. if (callee.type === "MemberExpression") {
  11041. return this.finishNode(node, "BindExpression");
  11042. } else {
  11043. throw this.raise(Errors.UnsupportedBind, {
  11044. at: callee
  11045. });
  11046. }
  11047. }
  11048. case 134:
  11049. {
  11050. this.raise(Errors.PrivateInExpectedIn, {
  11051. at: this.state.startLoc,
  11052. identifierName: this.state.value
  11053. });
  11054. return this.parsePrivateName();
  11055. }
  11056. case 33:
  11057. {
  11058. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11059. }
  11060. case 32:
  11061. {
  11062. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11063. }
  11064. case 37:
  11065. case 38:
  11066. {
  11067. return this.parseTopicReference("hack");
  11068. }
  11069. case 44:
  11070. case 54:
  11071. case 27:
  11072. {
  11073. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11074. if (pipeProposal) {
  11075. return this.parseTopicReference(pipeProposal);
  11076. } else {
  11077. throw this.unexpected();
  11078. }
  11079. }
  11080. case 47:
  11081. {
  11082. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11083. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11084. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11085. break;
  11086. } else {
  11087. throw this.unexpected();
  11088. }
  11089. }
  11090. default:
  11091. if (tokenIsIdentifier(type)) {
  11092. if (this.isContextual(123) && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  11093. return this.parseModuleExpression();
  11094. }
  11095. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11096. const containsEsc = this.state.containsEsc;
  11097. const id = this.parseIdentifier();
  11098. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11099. const {
  11100. type
  11101. } = this.state;
  11102. if (type === 68) {
  11103. this.resetPreviousNodeTrailingComments(id);
  11104. this.next();
  11105. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  11106. } else if (tokenIsIdentifier(type)) {
  11107. if (this.lookaheadCharCode() === 61) {
  11108. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11109. } else {
  11110. return id;
  11111. }
  11112. } else if (type === 90) {
  11113. this.resetPreviousNodeTrailingComments(id);
  11114. return this.parseDo(this.startNodeAtNode(id), true);
  11115. }
  11116. }
  11117. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11118. this.next();
  11119. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11120. }
  11121. return id;
  11122. } else {
  11123. throw this.unexpected();
  11124. }
  11125. }
  11126. }
  11127. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11128. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11129. if (pipeProposal) {
  11130. this.state.type = topicTokenType;
  11131. this.state.value = topicTokenValue;
  11132. this.state.pos--;
  11133. this.state.end--;
  11134. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11135. return this.parseTopicReference(pipeProposal);
  11136. } else {
  11137. throw this.unexpected();
  11138. }
  11139. }
  11140. parseTopicReference(pipeProposal) {
  11141. const node = this.startNode();
  11142. const startLoc = this.state.startLoc;
  11143. const tokenType = this.state.type;
  11144. this.next();
  11145. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11146. }
  11147. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11148. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11149. const nodeType = pipeProposal === "smart" ? "PipelinePrimaryTopicReference" : "TopicReference";
  11150. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11151. this.raise(pipeProposal === "smart" ? Errors.PrimaryTopicNotAllowed : Errors.PipeTopicUnbound, {
  11152. at: startLoc
  11153. });
  11154. }
  11155. this.registerTopicReference();
  11156. return this.finishNode(node, nodeType);
  11157. } else {
  11158. throw this.raise(Errors.PipeTopicUnconfiguredToken, {
  11159. at: startLoc,
  11160. token: tokenLabelName(tokenType)
  11161. });
  11162. }
  11163. }
  11164. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11165. switch (pipeProposal) {
  11166. case "hack":
  11167. {
  11168. return this.hasPlugin(["pipelineOperator", {
  11169. topicToken: tokenLabelName(tokenType)
  11170. }]);
  11171. }
  11172. case "smart":
  11173. return tokenType === 27;
  11174. default:
  11175. throw this.raise(Errors.PipeTopicRequiresHackPipes, {
  11176. at: startLoc
  11177. });
  11178. }
  11179. }
  11180. parseAsyncArrowUnaryFunction(node) {
  11181. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11182. const params = [this.parseIdentifier()];
  11183. this.prodParam.exit();
  11184. if (this.hasPrecedingLineBreak()) {
  11185. this.raise(Errors.LineTerminatorBeforeArrow, {
  11186. at: this.state.curPosition()
  11187. });
  11188. }
  11189. this.expect(19);
  11190. return this.parseArrowExpression(node, params, true);
  11191. }
  11192. parseDo(node, isAsync) {
  11193. this.expectPlugin("doExpressions");
  11194. if (isAsync) {
  11195. this.expectPlugin("asyncDoExpressions");
  11196. }
  11197. node.async = isAsync;
  11198. this.next();
  11199. const oldLabels = this.state.labels;
  11200. this.state.labels = [];
  11201. if (isAsync) {
  11202. this.prodParam.enter(PARAM_AWAIT);
  11203. node.body = this.parseBlock();
  11204. this.prodParam.exit();
  11205. } else {
  11206. node.body = this.parseBlock();
  11207. }
  11208. this.state.labels = oldLabels;
  11209. return this.finishNode(node, "DoExpression");
  11210. }
  11211. parseSuper() {
  11212. const node = this.startNode();
  11213. this.next();
  11214. if (this.match(10) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  11215. this.raise(Errors.SuperNotAllowed, {
  11216. at: node
  11217. });
  11218. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  11219. this.raise(Errors.UnexpectedSuper, {
  11220. at: node
  11221. });
  11222. }
  11223. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11224. this.raise(Errors.UnsupportedSuper, {
  11225. at: node
  11226. });
  11227. }
  11228. return this.finishNode(node, "Super");
  11229. }
  11230. parsePrivateName() {
  11231. const node = this.startNode();
  11232. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart, this.state.start + 1));
  11233. const name = this.state.value;
  11234. this.next();
  11235. node.id = this.createIdentifier(id, name);
  11236. return this.finishNode(node, "PrivateName");
  11237. }
  11238. parseFunctionOrFunctionSent() {
  11239. const node = this.startNode();
  11240. this.next();
  11241. if (this.prodParam.hasYield && this.match(16)) {
  11242. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11243. this.next();
  11244. if (this.match(102)) {
  11245. this.expectPlugin("functionSent");
  11246. } else if (!this.hasPlugin("functionSent")) {
  11247. this.unexpected();
  11248. }
  11249. return this.parseMetaProperty(node, meta, "sent");
  11250. }
  11251. return this.parseFunction(node);
  11252. }
  11253. parseMetaProperty(node, meta, propertyName) {
  11254. node.meta = meta;
  11255. const containsEsc = this.state.containsEsc;
  11256. node.property = this.parseIdentifier(true);
  11257. if (node.property.name !== propertyName || containsEsc) {
  11258. this.raise(Errors.UnsupportedMetaProperty, {
  11259. at: node.property,
  11260. target: meta.name,
  11261. onlyValidPropertyName: propertyName
  11262. });
  11263. }
  11264. return this.finishNode(node, "MetaProperty");
  11265. }
  11266. parseImportMetaProperty(node) {
  11267. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  11268. this.next();
  11269. if (this.isContextual(100)) {
  11270. if (!this.inModule) {
  11271. this.raise(Errors.ImportMetaOutsideModule, {
  11272. at: id
  11273. });
  11274. }
  11275. this.sawUnambiguousESM = true;
  11276. }
  11277. return this.parseMetaProperty(node, id, "meta");
  11278. }
  11279. parseLiteralAtNode(value, type, node) {
  11280. this.addExtra(node, "rawValue", value);
  11281. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  11282. node.value = value;
  11283. this.next();
  11284. return this.finishNode(node, type);
  11285. }
  11286. parseLiteral(value, type) {
  11287. const node = this.startNode();
  11288. return this.parseLiteralAtNode(value, type, node);
  11289. }
  11290. parseStringLiteral(value) {
  11291. return this.parseLiteral(value, "StringLiteral");
  11292. }
  11293. parseNumericLiteral(value) {
  11294. return this.parseLiteral(value, "NumericLiteral");
  11295. }
  11296. parseBigIntLiteral(value) {
  11297. return this.parseLiteral(value, "BigIntLiteral");
  11298. }
  11299. parseDecimalLiteral(value) {
  11300. return this.parseLiteral(value, "DecimalLiteral");
  11301. }
  11302. parseRegExpLiteral(value) {
  11303. const node = this.parseLiteral(value.value, "RegExpLiteral");
  11304. node.pattern = value.pattern;
  11305. node.flags = value.flags;
  11306. return node;
  11307. }
  11308. parseBooleanLiteral(value) {
  11309. const node = this.startNode();
  11310. node.value = value;
  11311. this.next();
  11312. return this.finishNode(node, "BooleanLiteral");
  11313. }
  11314. parseNullLiteral() {
  11315. const node = this.startNode();
  11316. this.next();
  11317. return this.finishNode(node, "NullLiteral");
  11318. }
  11319. parseParenAndDistinguishExpression(canBeArrow) {
  11320. const startPos = this.state.start;
  11321. const startLoc = this.state.startLoc;
  11322. let val;
  11323. this.next();
  11324. this.expressionScope.enter(newArrowHeadScope());
  11325. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11326. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11327. this.state.maybeInArrowParameters = true;
  11328. this.state.inFSharpPipelineDirectBody = false;
  11329. const innerStartPos = this.state.start;
  11330. const innerStartLoc = this.state.startLoc;
  11331. const exprList = [];
  11332. const refExpressionErrors = new ExpressionErrors();
  11333. let first = true;
  11334. let spreadStartLoc;
  11335. let optionalCommaStartLoc;
  11336. while (!this.match(11)) {
  11337. if (first) {
  11338. first = false;
  11339. } else {
  11340. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11341. if (this.match(11)) {
  11342. optionalCommaStartLoc = this.state.startLoc;
  11343. break;
  11344. }
  11345. }
  11346. if (this.match(21)) {
  11347. const spreadNodeStartPos = this.state.start;
  11348. const spreadNodeStartLoc = this.state.startLoc;
  11349. spreadStartLoc = this.state.startLoc;
  11350. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  11351. if (!this.checkCommaAfterRest(41)) {
  11352. break;
  11353. }
  11354. } else {
  11355. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  11356. }
  11357. }
  11358. const innerEndLoc = this.state.lastTokEndLoc;
  11359. this.expect(11);
  11360. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11361. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11362. let arrowNode = this.startNodeAt(startPos, startLoc);
  11363. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11364. this.checkDestructuringPrivate(refExpressionErrors);
  11365. this.expressionScope.validateAsPattern();
  11366. this.expressionScope.exit();
  11367. this.parseArrowExpression(arrowNode, exprList, false);
  11368. return arrowNode;
  11369. }
  11370. this.expressionScope.exit();
  11371. if (!exprList.length) {
  11372. this.unexpected(this.state.lastTokStartLoc);
  11373. }
  11374. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11375. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11376. this.checkExpressionErrors(refExpressionErrors, true);
  11377. this.toReferencedListDeep(exprList, true);
  11378. if (exprList.length > 1) {
  11379. val = this.startNodeAt(innerStartPos, innerStartLoc);
  11380. val.expressions = exprList;
  11381. this.finishNode(val, "SequenceExpression");
  11382. this.resetEndLocation(val, innerEndLoc);
  11383. } else {
  11384. val = exprList[0];
  11385. }
  11386. return this.wrapParenthesis(startPos, startLoc, val);
  11387. }
  11388. wrapParenthesis(startPos, startLoc, expression) {
  11389. if (!this.options.createParenthesizedExpressions) {
  11390. this.addExtra(expression, "parenthesized", true);
  11391. this.addExtra(expression, "parenStart", startPos);
  11392. this.takeSurroundingComments(expression, startPos, this.state.lastTokEndLoc.index);
  11393. return expression;
  11394. }
  11395. const parenExpression = this.startNodeAt(startPos, startLoc);
  11396. parenExpression.expression = expression;
  11397. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11398. }
  11399. shouldParseArrow(params) {
  11400. return !this.canInsertSemicolon();
  11401. }
  11402. parseArrow(node) {
  11403. if (this.eat(19)) {
  11404. return node;
  11405. }
  11406. }
  11407. parseParenItem(node, startPos, startLoc) {
  11408. return node;
  11409. }
  11410. parseNewOrNewTarget() {
  11411. const node = this.startNode();
  11412. this.next();
  11413. if (this.match(16)) {
  11414. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11415. this.next();
  11416. const metaProp = this.parseMetaProperty(node, meta, "target");
  11417. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  11418. this.raise(Errors.UnexpectedNewTarget, {
  11419. at: metaProp
  11420. });
  11421. }
  11422. return metaProp;
  11423. }
  11424. return this.parseNew(node);
  11425. }
  11426. parseNew(node) {
  11427. this.parseNewCallee(node);
  11428. if (this.eat(10)) {
  11429. const args = this.parseExprList(11);
  11430. this.toReferencedList(args);
  11431. node.arguments = args;
  11432. } else {
  11433. node.arguments = [];
  11434. }
  11435. return this.finishNode(node, "NewExpression");
  11436. }
  11437. parseNewCallee(node) {
  11438. node.callee = this.parseNoCallExpr();
  11439. if (node.callee.type === "Import") {
  11440. this.raise(Errors.ImportCallNotNewExpression, {
  11441. at: node.callee
  11442. });
  11443. } else if (this.isOptionalChain(node.callee)) {
  11444. this.raise(Errors.OptionalChainingNoNew, {
  11445. at: this.state.lastTokEndLoc
  11446. });
  11447. } else if (this.eat(18)) {
  11448. this.raise(Errors.OptionalChainingNoNew, {
  11449. at: this.state.startLoc
  11450. });
  11451. }
  11452. }
  11453. parseTemplateElement(isTagged) {
  11454. const {
  11455. start,
  11456. startLoc,
  11457. end,
  11458. value
  11459. } = this.state;
  11460. const elemStart = start + 1;
  11461. const elem = this.startNodeAt(elemStart, createPositionWithColumnOffset(startLoc, 1));
  11462. if (value === null) {
  11463. if (!isTagged) {
  11464. this.raise(Errors.InvalidEscapeSequenceTemplate, {
  11465. at: createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1)
  11466. });
  11467. }
  11468. }
  11469. const isTail = this.match(24);
  11470. const endOffset = isTail ? -1 : -2;
  11471. const elemEnd = end + endOffset;
  11472. elem.value = {
  11473. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11474. cooked: value === null ? null : value.slice(1, endOffset)
  11475. };
  11476. elem.tail = isTail;
  11477. this.next();
  11478. const finishedNode = this.finishNode(elem, "TemplateElement");
  11479. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11480. return finishedNode;
  11481. }
  11482. parseTemplate(isTagged) {
  11483. const node = this.startNode();
  11484. node.expressions = [];
  11485. let curElt = this.parseTemplateElement(isTagged);
  11486. node.quasis = [curElt];
  11487. while (!curElt.tail) {
  11488. node.expressions.push(this.parseTemplateSubstitution());
  11489. this.readTemplateContinuation();
  11490. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  11491. }
  11492. return this.finishNode(node, "TemplateLiteral");
  11493. }
  11494. parseTemplateSubstitution() {
  11495. return this.parseExpression();
  11496. }
  11497. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11498. if (isRecord) {
  11499. this.expectPlugin("recordAndTuple");
  11500. }
  11501. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11502. this.state.inFSharpPipelineDirectBody = false;
  11503. const propHash = Object.create(null);
  11504. let first = true;
  11505. const node = this.startNode();
  11506. node.properties = [];
  11507. this.next();
  11508. while (!this.match(close)) {
  11509. if (first) {
  11510. first = false;
  11511. } else {
  11512. this.expect(12);
  11513. if (this.match(close)) {
  11514. this.addTrailingCommaExtraToNode(node);
  11515. break;
  11516. }
  11517. }
  11518. let prop;
  11519. if (isPattern) {
  11520. prop = this.parseBindingProperty();
  11521. } else {
  11522. prop = this.parsePropertyDefinition(refExpressionErrors);
  11523. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  11524. }
  11525. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11526. this.raise(Errors.InvalidRecordProperty, {
  11527. at: prop
  11528. });
  11529. }
  11530. if (prop.shorthand) {
  11531. this.addExtra(prop, "shorthand", true);
  11532. }
  11533. node.properties.push(prop);
  11534. }
  11535. this.next();
  11536. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11537. let type = "ObjectExpression";
  11538. if (isPattern) {
  11539. type = "ObjectPattern";
  11540. } else if (isRecord) {
  11541. type = "RecordExpression";
  11542. }
  11543. return this.finishNode(node, type);
  11544. }
  11545. addTrailingCommaExtraToNode(node) {
  11546. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  11547. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11548. }
  11549. maybeAsyncOrAccessorProp(prop) {
  11550. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11551. }
  11552. parsePropertyDefinition(refExpressionErrors) {
  11553. let decorators = [];
  11554. if (this.match(26)) {
  11555. if (this.hasPlugin("decorators")) {
  11556. this.raise(Errors.UnsupportedPropertyDecorator, {
  11557. at: this.state.startLoc
  11558. });
  11559. }
  11560. while (this.match(26)) {
  11561. decorators.push(this.parseDecorator());
  11562. }
  11563. }
  11564. const prop = this.startNode();
  11565. let isAsync = false;
  11566. let isAccessor = false;
  11567. let startPos;
  11568. let startLoc;
  11569. if (this.match(21)) {
  11570. if (decorators.length) this.unexpected();
  11571. return this.parseSpread();
  11572. }
  11573. if (decorators.length) {
  11574. prop.decorators = decorators;
  11575. decorators = [];
  11576. }
  11577. prop.method = false;
  11578. if (refExpressionErrors) {
  11579. startPos = this.state.start;
  11580. startLoc = this.state.startLoc;
  11581. }
  11582. let isGenerator = this.eat(55);
  11583. this.parsePropertyNamePrefixOperator(prop);
  11584. const containsEsc = this.state.containsEsc;
  11585. const key = this.parsePropertyName(prop, refExpressionErrors);
  11586. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11587. const keyName = key.name;
  11588. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11589. isAsync = true;
  11590. this.resetPreviousNodeTrailingComments(key);
  11591. isGenerator = this.eat(55);
  11592. this.parsePropertyName(prop);
  11593. }
  11594. if (keyName === "get" || keyName === "set") {
  11595. isAccessor = true;
  11596. this.resetPreviousNodeTrailingComments(key);
  11597. prop.kind = keyName;
  11598. if (this.match(55)) {
  11599. isGenerator = true;
  11600. this.raise(Errors.AccessorIsGenerator, {
  11601. at: this.state.curPosition(),
  11602. kind: keyName
  11603. });
  11604. this.next();
  11605. }
  11606. this.parsePropertyName(prop);
  11607. }
  11608. }
  11609. return this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11610. }
  11611. getGetterSetterExpectedParamCount(method) {
  11612. return method.kind === "get" ? 0 : 1;
  11613. }
  11614. getObjectOrClassMethodParams(method) {
  11615. return method.params;
  11616. }
  11617. checkGetterSetterParams(method) {
  11618. var _params;
  11619. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11620. const params = this.getObjectOrClassMethodParams(method);
  11621. if (params.length !== paramCount) {
  11622. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, {
  11623. at: method
  11624. });
  11625. }
  11626. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11627. this.raise(Errors.BadSetterRestParameter, {
  11628. at: method
  11629. });
  11630. }
  11631. }
  11632. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11633. if (isAccessor) {
  11634. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11635. this.checkGetterSetterParams(finishedProp);
  11636. return finishedProp;
  11637. }
  11638. if (isAsync || isGenerator || this.match(10)) {
  11639. if (isPattern) this.unexpected();
  11640. prop.kind = "method";
  11641. prop.method = true;
  11642. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11643. }
  11644. }
  11645. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  11646. prop.shorthand = false;
  11647. if (this.eat(14)) {
  11648. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  11649. return this.finishNode(prop, "ObjectProperty");
  11650. }
  11651. if (!prop.computed && prop.key.type === "Identifier") {
  11652. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11653. if (isPattern) {
  11654. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11655. } else if (this.match(29)) {
  11656. const shorthandAssignLoc = this.state.startLoc;
  11657. if (refExpressionErrors != null) {
  11658. if (refExpressionErrors.shorthandAssignLoc === null) {
  11659. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11660. }
  11661. } else {
  11662. this.raise(Errors.InvalidCoverInitializedName, {
  11663. at: shorthandAssignLoc
  11664. });
  11665. }
  11666. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  11667. } else {
  11668. prop.value = cloneIdentifier(prop.key);
  11669. }
  11670. prop.shorthand = true;
  11671. return this.finishNode(prop, "ObjectProperty");
  11672. }
  11673. }
  11674. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  11675. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  11676. if (!node) this.unexpected();
  11677. return node;
  11678. }
  11679. parsePropertyName(prop, refExpressionErrors) {
  11680. if (this.eat(0)) {
  11681. prop.computed = true;
  11682. prop.key = this.parseMaybeAssignAllowIn();
  11683. this.expect(3);
  11684. } else {
  11685. const {
  11686. type,
  11687. value
  11688. } = this.state;
  11689. let key;
  11690. if (tokenIsKeywordOrIdentifier(type)) {
  11691. key = this.parseIdentifier(true);
  11692. } else {
  11693. switch (type) {
  11694. case 130:
  11695. key = this.parseNumericLiteral(value);
  11696. break;
  11697. case 129:
  11698. key = this.parseStringLiteral(value);
  11699. break;
  11700. case 131:
  11701. key = this.parseBigIntLiteral(value);
  11702. break;
  11703. case 132:
  11704. key = this.parseDecimalLiteral(value);
  11705. break;
  11706. case 134:
  11707. {
  11708. const privateKeyLoc = this.state.startLoc;
  11709. if (refExpressionErrors != null) {
  11710. if (refExpressionErrors.privateKeyLoc === null) {
  11711. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  11712. }
  11713. } else {
  11714. this.raise(Errors.UnexpectedPrivateField, {
  11715. at: privateKeyLoc
  11716. });
  11717. }
  11718. key = this.parsePrivateName();
  11719. break;
  11720. }
  11721. default:
  11722. throw this.unexpected();
  11723. }
  11724. }
  11725. prop.key = key;
  11726. if (type !== 134) {
  11727. prop.computed = false;
  11728. }
  11729. }
  11730. return prop.key;
  11731. }
  11732. initFunction(node, isAsync) {
  11733. node.id = null;
  11734. node.generator = false;
  11735. node.async = !!isAsync;
  11736. }
  11737. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  11738. this.initFunction(node, isAsync);
  11739. node.generator = !!isGenerator;
  11740. const allowModifiers = isConstructor;
  11741. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  11742. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11743. this.parseFunctionParams(node, allowModifiers);
  11744. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  11745. this.prodParam.exit();
  11746. this.scope.exit();
  11747. return finishedNode;
  11748. }
  11749. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  11750. if (isTuple) {
  11751. this.expectPlugin("recordAndTuple");
  11752. }
  11753. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11754. this.state.inFSharpPipelineDirectBody = false;
  11755. const node = this.startNode();
  11756. this.next();
  11757. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  11758. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11759. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  11760. }
  11761. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  11762. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  11763. let flags = functionFlags(isAsync, false);
  11764. if (!this.match(5) && this.prodParam.hasIn) {
  11765. flags |= PARAM_IN;
  11766. }
  11767. this.prodParam.enter(flags);
  11768. this.initFunction(node, isAsync);
  11769. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11770. if (params) {
  11771. this.state.maybeInArrowParameters = true;
  11772. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  11773. }
  11774. this.state.maybeInArrowParameters = false;
  11775. this.parseFunctionBody(node, true);
  11776. this.prodParam.exit();
  11777. this.scope.exit();
  11778. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11779. return this.finishNode(node, "ArrowFunctionExpression");
  11780. }
  11781. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  11782. this.toAssignableList(params, trailingCommaLoc, false);
  11783. node.params = params;
  11784. }
  11785. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  11786. this.parseFunctionBody(node, false, isMethod);
  11787. return this.finishNode(node, type);
  11788. }
  11789. parseFunctionBody(node, allowExpression, isMethod = false) {
  11790. const isExpression = allowExpression && !this.match(5);
  11791. this.expressionScope.enter(newExpressionScope());
  11792. if (isExpression) {
  11793. node.body = this.parseMaybeAssign();
  11794. this.checkParams(node, false, allowExpression, false);
  11795. } else {
  11796. const oldStrict = this.state.strict;
  11797. const oldLabels = this.state.labels;
  11798. this.state.labels = [];
  11799. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  11800. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  11801. const nonSimple = !this.isSimpleParamList(node.params);
  11802. if (hasStrictModeDirective && nonSimple) {
  11803. this.raise(Errors.IllegalLanguageModeDirective, {
  11804. at: (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node
  11805. });
  11806. }
  11807. const strictModeChanged = !oldStrict && this.state.strict;
  11808. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  11809. if (this.state.strict && node.id) {
  11810. this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
  11811. }
  11812. });
  11813. this.prodParam.exit();
  11814. this.state.labels = oldLabels;
  11815. }
  11816. this.expressionScope.exit();
  11817. }
  11818. isSimpleParameter(node) {
  11819. return node.type === "Identifier";
  11820. }
  11821. isSimpleParamList(params) {
  11822. for (let i = 0, len = params.length; i < len; i++) {
  11823. if (!this.isSimpleParameter(params[i])) return false;
  11824. }
  11825. return true;
  11826. }
  11827. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  11828. const checkClashes = !allowDuplicates && new Set();
  11829. const formalParameters = {
  11830. type: "FormalParameters"
  11831. };
  11832. for (const param of node.params) {
  11833. this.checkLVal(param, {
  11834. in: formalParameters,
  11835. binding: BIND_VAR,
  11836. checkClashes,
  11837. strictModeChanged
  11838. });
  11839. }
  11840. }
  11841. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  11842. const elts = [];
  11843. let first = true;
  11844. while (!this.eat(close)) {
  11845. if (first) {
  11846. first = false;
  11847. } else {
  11848. this.expect(12);
  11849. if (this.match(close)) {
  11850. if (nodeForExtra) {
  11851. this.addTrailingCommaExtraToNode(nodeForExtra);
  11852. }
  11853. this.next();
  11854. break;
  11855. }
  11856. }
  11857. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  11858. }
  11859. return elts;
  11860. }
  11861. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  11862. let elt;
  11863. if (this.match(12)) {
  11864. if (!allowEmpty) {
  11865. this.raise(Errors.UnexpectedToken, {
  11866. at: this.state.curPosition(),
  11867. unexpected: ","
  11868. });
  11869. }
  11870. elt = null;
  11871. } else if (this.match(21)) {
  11872. const spreadNodeStartPos = this.state.start;
  11873. const spreadNodeStartLoc = this.state.startLoc;
  11874. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  11875. } else if (this.match(17)) {
  11876. this.expectPlugin("partialApplication");
  11877. if (!allowPlaceholder) {
  11878. this.raise(Errors.UnexpectedArgumentPlaceholder, {
  11879. at: this.state.startLoc
  11880. });
  11881. }
  11882. const node = this.startNode();
  11883. this.next();
  11884. elt = this.finishNode(node, "ArgumentPlaceholder");
  11885. } else {
  11886. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  11887. }
  11888. return elt;
  11889. }
  11890. parseIdentifier(liberal) {
  11891. const node = this.startNode();
  11892. const name = this.parseIdentifierName(node.start, liberal);
  11893. return this.createIdentifier(node, name);
  11894. }
  11895. createIdentifier(node, name) {
  11896. node.name = name;
  11897. node.loc.identifierName = name;
  11898. return this.finishNode(node, "Identifier");
  11899. }
  11900. parseIdentifierName(pos, liberal) {
  11901. let name;
  11902. const {
  11903. startLoc,
  11904. type
  11905. } = this.state;
  11906. if (tokenIsKeywordOrIdentifier(type)) {
  11907. name = this.state.value;
  11908. } else {
  11909. throw this.unexpected();
  11910. }
  11911. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  11912. if (liberal) {
  11913. if (tokenIsKeyword) {
  11914. this.replaceToken(128);
  11915. }
  11916. } else {
  11917. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  11918. }
  11919. this.next();
  11920. return name;
  11921. }
  11922. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  11923. if (word.length > 10) {
  11924. return;
  11925. }
  11926. if (!canBeReservedWord(word)) {
  11927. return;
  11928. }
  11929. if (word === "yield") {
  11930. if (this.prodParam.hasYield) {
  11931. this.raise(Errors.YieldBindingIdentifier, {
  11932. at: startLoc
  11933. });
  11934. return;
  11935. }
  11936. } else if (word === "await") {
  11937. if (this.prodParam.hasAwait) {
  11938. this.raise(Errors.AwaitBindingIdentifier, {
  11939. at: startLoc
  11940. });
  11941. return;
  11942. }
  11943. if (this.scope.inStaticBlock) {
  11944. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, {
  11945. at: startLoc
  11946. });
  11947. return;
  11948. }
  11949. this.expressionScope.recordAsyncArrowParametersError({
  11950. at: startLoc
  11951. });
  11952. } else if (word === "arguments") {
  11953. if (this.scope.inClassAndNotInNonArrowFunction) {
  11954. this.raise(Errors.ArgumentsInClass, {
  11955. at: startLoc
  11956. });
  11957. return;
  11958. }
  11959. }
  11960. if (checkKeywords && isKeyword(word)) {
  11961. this.raise(Errors.UnexpectedKeyword, {
  11962. at: startLoc,
  11963. keyword: word
  11964. });
  11965. return;
  11966. }
  11967. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  11968. if (reservedTest(word, this.inModule)) {
  11969. this.raise(Errors.UnexpectedReservedWord, {
  11970. at: startLoc,
  11971. reservedWord: word
  11972. });
  11973. }
  11974. }
  11975. isAwaitAllowed() {
  11976. if (this.prodParam.hasAwait) return true;
  11977. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  11978. return true;
  11979. }
  11980. return false;
  11981. }
  11982. parseAwait(startPos, startLoc) {
  11983. const node = this.startNodeAt(startPos, startLoc);
  11984. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, {
  11985. at: node
  11986. });
  11987. if (this.eat(55)) {
  11988. this.raise(Errors.ObsoleteAwaitStar, {
  11989. at: node
  11990. });
  11991. }
  11992. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  11993. if (this.isAmbiguousAwait()) {
  11994. this.ambiguousScriptDifferentAst = true;
  11995. } else {
  11996. this.sawUnambiguousESM = true;
  11997. }
  11998. }
  11999. if (!this.state.soloAwait) {
  12000. node.argument = this.parseMaybeUnary(null, true);
  12001. }
  12002. return this.finishNode(node, "AwaitExpression");
  12003. }
  12004. isAmbiguousAwait() {
  12005. if (this.hasPrecedingLineBreak()) return true;
  12006. const {
  12007. type
  12008. } = this.state;
  12009. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 133 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12010. }
  12011. parseYield() {
  12012. const node = this.startNode();
  12013. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, {
  12014. at: node
  12015. });
  12016. this.next();
  12017. let delegating = false;
  12018. let argument = null;
  12019. if (!this.hasPrecedingLineBreak()) {
  12020. delegating = this.eat(55);
  12021. switch (this.state.type) {
  12022. case 13:
  12023. case 135:
  12024. case 8:
  12025. case 11:
  12026. case 3:
  12027. case 9:
  12028. case 14:
  12029. case 12:
  12030. if (!delegating) break;
  12031. default:
  12032. argument = this.parseMaybeAssign();
  12033. }
  12034. }
  12035. node.delegate = delegating;
  12036. node.argument = argument;
  12037. return this.finishNode(node, "YieldExpression");
  12038. }
  12039. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12040. if (this.hasPlugin(["pipelineOperator", {
  12041. proposal: "smart"
  12042. }])) {
  12043. if (left.type === "SequenceExpression") {
  12044. this.raise(Errors.PipelineHeadSequenceExpression, {
  12045. at: leftStartLoc
  12046. });
  12047. }
  12048. }
  12049. }
  12050. parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
  12051. if (this.isSimpleReference(childExpr)) {
  12052. const bodyNode = this.startNodeAt(startPos, startLoc);
  12053. bodyNode.callee = childExpr;
  12054. return this.finishNode(bodyNode, "PipelineBareFunction");
  12055. } else {
  12056. const bodyNode = this.startNodeAt(startPos, startLoc);
  12057. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12058. bodyNode.expression = childExpr;
  12059. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12060. }
  12061. }
  12062. isSimpleReference(expression) {
  12063. switch (expression.type) {
  12064. case "MemberExpression":
  12065. return !expression.computed && this.isSimpleReference(expression.object);
  12066. case "Identifier":
  12067. return true;
  12068. default:
  12069. return false;
  12070. }
  12071. }
  12072. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12073. if (this.match(19)) {
  12074. throw this.raise(Errors.PipelineBodyNoArrow, {
  12075. at: this.state.startLoc
  12076. });
  12077. }
  12078. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12079. this.raise(Errors.PipelineTopicUnused, {
  12080. at: startLoc
  12081. });
  12082. }
  12083. }
  12084. withTopicBindingContext(callback) {
  12085. const outerContextTopicState = this.state.topicContext;
  12086. this.state.topicContext = {
  12087. maxNumOfResolvableTopics: 1,
  12088. maxTopicIndex: null
  12089. };
  12090. try {
  12091. return callback();
  12092. } finally {
  12093. this.state.topicContext = outerContextTopicState;
  12094. }
  12095. }
  12096. withSmartMixTopicForbiddingContext(callback) {
  12097. if (this.hasPlugin(["pipelineOperator", {
  12098. proposal: "smart"
  12099. }])) {
  12100. const outerContextTopicState = this.state.topicContext;
  12101. this.state.topicContext = {
  12102. maxNumOfResolvableTopics: 0,
  12103. maxTopicIndex: null
  12104. };
  12105. try {
  12106. return callback();
  12107. } finally {
  12108. this.state.topicContext = outerContextTopicState;
  12109. }
  12110. } else {
  12111. return callback();
  12112. }
  12113. }
  12114. withSoloAwaitPermittingContext(callback) {
  12115. const outerContextSoloAwaitState = this.state.soloAwait;
  12116. this.state.soloAwait = true;
  12117. try {
  12118. return callback();
  12119. } finally {
  12120. this.state.soloAwait = outerContextSoloAwaitState;
  12121. }
  12122. }
  12123. allowInAnd(callback) {
  12124. const flags = this.prodParam.currentFlags();
  12125. const prodParamToSet = PARAM_IN & ~flags;
  12126. if (prodParamToSet) {
  12127. this.prodParam.enter(flags | PARAM_IN);
  12128. try {
  12129. return callback();
  12130. } finally {
  12131. this.prodParam.exit();
  12132. }
  12133. }
  12134. return callback();
  12135. }
  12136. disallowInAnd(callback) {
  12137. const flags = this.prodParam.currentFlags();
  12138. const prodParamToClear = PARAM_IN & flags;
  12139. if (prodParamToClear) {
  12140. this.prodParam.enter(flags & ~PARAM_IN);
  12141. try {
  12142. return callback();
  12143. } finally {
  12144. this.prodParam.exit();
  12145. }
  12146. }
  12147. return callback();
  12148. }
  12149. registerTopicReference() {
  12150. this.state.topicContext.maxTopicIndex = 0;
  12151. }
  12152. topicReferenceIsAllowedInCurrentContext() {
  12153. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12154. }
  12155. topicReferenceWasUsedInCurrentContext() {
  12156. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12157. }
  12158. parseFSharpPipelineBody(prec) {
  12159. const startPos = this.state.start;
  12160. const startLoc = this.state.startLoc;
  12161. this.state.potentialArrowAt = this.state.start;
  12162. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12163. this.state.inFSharpPipelineDirectBody = true;
  12164. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
  12165. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12166. return ret;
  12167. }
  12168. parseModuleExpression() {
  12169. this.expectPlugin("moduleBlocks");
  12170. const node = this.startNode();
  12171. this.next();
  12172. this.expect(5);
  12173. const revertScopes = this.initializeScopes(true);
  12174. this.enterInitialScopes();
  12175. const program = this.startNode();
  12176. try {
  12177. node.body = this.parseProgram(program, 8, "module");
  12178. } finally {
  12179. revertScopes();
  12180. }
  12181. this.eat(8);
  12182. return this.finishNode(node, "ModuleExpression");
  12183. }
  12184. parsePropertyNamePrefixOperator(prop) {}
  12185. }
  12186. const loopLabel = {
  12187. kind: "loop"
  12188. },
  12189. switchLabel = {
  12190. kind: "switch"
  12191. };
  12192. const FUNC_NO_FLAGS = 0b000,
  12193. FUNC_STATEMENT = 0b001,
  12194. FUNC_HANGING_STATEMENT = 0b010,
  12195. FUNC_NULLABLE_ID = 0b100;
  12196. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12197. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12198. function babel7CompatTokens(tokens, input) {
  12199. for (let i = 0; i < tokens.length; i++) {
  12200. const token = tokens[i];
  12201. const {
  12202. type
  12203. } = token;
  12204. if (typeof type === "number") {
  12205. {
  12206. if (type === 134) {
  12207. const {
  12208. loc,
  12209. start,
  12210. value,
  12211. end
  12212. } = token;
  12213. const hashEndPos = start + 1;
  12214. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12215. tokens.splice(i, 1, new Token({
  12216. type: getExportedToken(27),
  12217. value: "#",
  12218. start: start,
  12219. end: hashEndPos,
  12220. startLoc: loc.start,
  12221. endLoc: hashEndLoc
  12222. }), new Token({
  12223. type: getExportedToken(128),
  12224. value: value,
  12225. start: hashEndPos,
  12226. end: end,
  12227. startLoc: hashEndLoc,
  12228. endLoc: loc.end
  12229. }));
  12230. i++;
  12231. continue;
  12232. }
  12233. if (tokenIsTemplate(type)) {
  12234. const {
  12235. loc,
  12236. start,
  12237. value,
  12238. end
  12239. } = token;
  12240. const backquoteEnd = start + 1;
  12241. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12242. let startToken;
  12243. if (input.charCodeAt(start) === 96) {
  12244. startToken = new Token({
  12245. type: getExportedToken(22),
  12246. value: "`",
  12247. start: start,
  12248. end: backquoteEnd,
  12249. startLoc: loc.start,
  12250. endLoc: backquoteEndLoc
  12251. });
  12252. } else {
  12253. startToken = new Token({
  12254. type: getExportedToken(8),
  12255. value: "}",
  12256. start: start,
  12257. end: backquoteEnd,
  12258. startLoc: loc.start,
  12259. endLoc: backquoteEndLoc
  12260. });
  12261. }
  12262. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12263. if (type === 24) {
  12264. templateElementEnd = end - 1;
  12265. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12266. templateValue = value === null ? null : value.slice(1, -1);
  12267. endToken = new Token({
  12268. type: getExportedToken(22),
  12269. value: "`",
  12270. start: templateElementEnd,
  12271. end: end,
  12272. startLoc: templateElementEndLoc,
  12273. endLoc: loc.end
  12274. });
  12275. } else {
  12276. templateElementEnd = end - 2;
  12277. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12278. templateValue = value === null ? null : value.slice(1, -2);
  12279. endToken = new Token({
  12280. type: getExportedToken(23),
  12281. value: "${",
  12282. start: templateElementEnd,
  12283. end: end,
  12284. startLoc: templateElementEndLoc,
  12285. endLoc: loc.end
  12286. });
  12287. }
  12288. tokens.splice(i, 1, startToken, new Token({
  12289. type: getExportedToken(20),
  12290. value: templateValue,
  12291. start: backquoteEnd,
  12292. end: templateElementEnd,
  12293. startLoc: backquoteEndLoc,
  12294. endLoc: templateElementEndLoc
  12295. }), endToken);
  12296. i += 2;
  12297. continue;
  12298. }
  12299. }
  12300. token.type = getExportedToken(type);
  12301. }
  12302. }
  12303. return tokens;
  12304. }
  12305. class StatementParser extends ExpressionParser {
  12306. parseTopLevel(file, program) {
  12307. file.program = this.parseProgram(program);
  12308. file.comments = this.state.comments;
  12309. if (this.options.tokens) {
  12310. file.tokens = babel7CompatTokens(this.tokens, this.input);
  12311. }
  12312. return this.finishNode(file, "File");
  12313. }
  12314. parseProgram(program, end = 135, sourceType = this.options.sourceType) {
  12315. program.sourceType = sourceType;
  12316. program.interpreter = this.parseInterpreterDirective();
  12317. this.parseBlockBody(program, true, true, end);
  12318. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  12319. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12320. this.raise(Errors.ModuleExportUndefined, {
  12321. at,
  12322. localName
  12323. });
  12324. }
  12325. }
  12326. if (end === 135) {
  12327. this.next();
  12328. }
  12329. return this.finishNode(program, "Program");
  12330. }
  12331. stmtToDirective(stmt) {
  12332. const directive = stmt;
  12333. directive.type = "Directive";
  12334. directive.value = directive.expression;
  12335. delete directive.expression;
  12336. const directiveLiteral = directive.value;
  12337. const expressionValue = directiveLiteral.value;
  12338. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  12339. const val = directiveLiteral.value = raw.slice(1, -1);
  12340. this.addExtra(directiveLiteral, "raw", raw);
  12341. this.addExtra(directiveLiteral, "rawValue", val);
  12342. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12343. directiveLiteral.type = "DirectiveLiteral";
  12344. return directive;
  12345. }
  12346. parseInterpreterDirective() {
  12347. if (!this.match(28)) {
  12348. return null;
  12349. }
  12350. const node = this.startNode();
  12351. node.value = this.state.value;
  12352. this.next();
  12353. return this.finishNode(node, "InterpreterDirective");
  12354. }
  12355. isLet(context) {
  12356. if (!this.isContextual(99)) {
  12357. return false;
  12358. }
  12359. return this.isLetKeyword(context);
  12360. }
  12361. isLetKeyword(context) {
  12362. const next = this.nextTokenStart();
  12363. const nextCh = this.codePointAtPos(next);
  12364. if (nextCh === 92 || nextCh === 91) {
  12365. return true;
  12366. }
  12367. if (context) return false;
  12368. if (nextCh === 123) return true;
  12369. if (isIdentifierStart(nextCh)) {
  12370. keywordRelationalOperator.lastIndex = next;
  12371. if (keywordRelationalOperator.test(this.input)) {
  12372. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12373. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12374. return false;
  12375. }
  12376. }
  12377. return true;
  12378. }
  12379. return false;
  12380. }
  12381. parseStatement(context, topLevel) {
  12382. if (this.match(26)) {
  12383. this.parseDecorators(true);
  12384. }
  12385. return this.parseStatementContent(context, topLevel);
  12386. }
  12387. parseStatementContent(context, topLevel) {
  12388. let starttype = this.state.type;
  12389. const node = this.startNode();
  12390. let kind;
  12391. if (this.isLet(context)) {
  12392. starttype = 74;
  12393. kind = "let";
  12394. }
  12395. switch (starttype) {
  12396. case 60:
  12397. return this.parseBreakContinueStatement(node, true);
  12398. case 63:
  12399. return this.parseBreakContinueStatement(node, false);
  12400. case 64:
  12401. return this.parseDebuggerStatement(node);
  12402. case 90:
  12403. return this.parseDoStatement(node);
  12404. case 91:
  12405. return this.parseForStatement(node);
  12406. case 68:
  12407. if (this.lookaheadCharCode() === 46) break;
  12408. if (context) {
  12409. if (this.state.strict) {
  12410. this.raise(Errors.StrictFunction, {
  12411. at: this.state.startLoc
  12412. });
  12413. } else if (context !== "if" && context !== "label") {
  12414. this.raise(Errors.SloppyFunction, {
  12415. at: this.state.startLoc
  12416. });
  12417. }
  12418. }
  12419. return this.parseFunctionStatement(node, false, !context);
  12420. case 80:
  12421. if (context) this.unexpected();
  12422. return this.parseClass(node, true);
  12423. case 69:
  12424. return this.parseIfStatement(node);
  12425. case 70:
  12426. return this.parseReturnStatement(node);
  12427. case 71:
  12428. return this.parseSwitchStatement(node);
  12429. case 72:
  12430. return this.parseThrowStatement(node);
  12431. case 73:
  12432. return this.parseTryStatement(node);
  12433. case 75:
  12434. case 74:
  12435. kind = kind || this.state.value;
  12436. if (context && kind !== "var") {
  12437. this.raise(Errors.UnexpectedLexicalDeclaration, {
  12438. at: this.state.startLoc
  12439. });
  12440. }
  12441. return this.parseVarStatement(node, kind);
  12442. case 92:
  12443. return this.parseWhileStatement(node);
  12444. case 76:
  12445. return this.parseWithStatement(node);
  12446. case 5:
  12447. return this.parseBlock();
  12448. case 13:
  12449. return this.parseEmptyStatement(node);
  12450. case 83:
  12451. {
  12452. const nextTokenCharCode = this.lookaheadCharCode();
  12453. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12454. break;
  12455. }
  12456. }
  12457. case 82:
  12458. {
  12459. if (!this.options.allowImportExportEverywhere && !topLevel) {
  12460. this.raise(Errors.UnexpectedImportExport, {
  12461. at: this.state.startLoc
  12462. });
  12463. }
  12464. this.next();
  12465. let result;
  12466. if (starttype === 83) {
  12467. result = this.parseImport(node);
  12468. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  12469. this.sawUnambiguousESM = true;
  12470. }
  12471. } else {
  12472. result = this.parseExport(node);
  12473. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  12474. this.sawUnambiguousESM = true;
  12475. }
  12476. }
  12477. this.assertModuleNodeAllowed(result);
  12478. return result;
  12479. }
  12480. default:
  12481. {
  12482. if (this.isAsyncFunction()) {
  12483. if (context) {
  12484. this.raise(Errors.AsyncFunctionInSingleStatementContext, {
  12485. at: this.state.startLoc
  12486. });
  12487. }
  12488. this.next();
  12489. return this.parseFunctionStatement(node, true, !context);
  12490. }
  12491. }
  12492. }
  12493. const maybeName = this.state.value;
  12494. const expr = this.parseExpression();
  12495. if (tokenIsIdentifier(starttype) && expr.type === "Identifier" && this.eat(14)) {
  12496. return this.parseLabeledStatement(node, maybeName, expr, context);
  12497. } else {
  12498. return this.parseExpressionStatement(node, expr);
  12499. }
  12500. }
  12501. assertModuleNodeAllowed(node) {
  12502. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  12503. this.raise(Errors.ImportOutsideModule, {
  12504. at: node
  12505. });
  12506. }
  12507. }
  12508. takeDecorators(node) {
  12509. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12510. if (decorators.length) {
  12511. node.decorators = decorators;
  12512. this.resetStartLocationFromNode(node, decorators[0]);
  12513. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  12514. }
  12515. }
  12516. canHaveLeadingDecorator() {
  12517. return this.match(80);
  12518. }
  12519. parseDecorators(allowExport) {
  12520. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  12521. while (this.match(26)) {
  12522. const decorator = this.parseDecorator();
  12523. currentContextDecorators.push(decorator);
  12524. }
  12525. if (this.match(82)) {
  12526. if (!allowExport) {
  12527. this.unexpected();
  12528. }
  12529. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  12530. this.raise(Errors.DecoratorExportClass, {
  12531. at: this.state.startLoc
  12532. });
  12533. }
  12534. } else if (!this.canHaveLeadingDecorator()) {
  12535. throw this.raise(Errors.UnexpectedLeadingDecorator, {
  12536. at: this.state.startLoc
  12537. });
  12538. }
  12539. }
  12540. parseDecorator() {
  12541. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12542. const node = this.startNode();
  12543. this.next();
  12544. if (this.hasPlugin("decorators")) {
  12545. this.state.decoratorStack.push([]);
  12546. const startPos = this.state.start;
  12547. const startLoc = this.state.startLoc;
  12548. let expr;
  12549. if (this.match(10)) {
  12550. const startPos = this.state.start;
  12551. const startLoc = this.state.startLoc;
  12552. this.next();
  12553. expr = this.parseExpression();
  12554. this.expect(11);
  12555. expr = this.wrapParenthesis(startPos, startLoc, expr);
  12556. const paramsStartLoc = this.state.startLoc;
  12557. node.expression = this.parseMaybeDecoratorArguments(expr);
  12558. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12559. this.raise(Errors.DecoratorArgumentsOutsideParentheses, {
  12560. at: paramsStartLoc
  12561. });
  12562. }
  12563. } else {
  12564. expr = this.parseIdentifier(false);
  12565. while (this.eat(16)) {
  12566. const node = this.startNodeAt(startPos, startLoc);
  12567. node.object = expr;
  12568. if (this.match(134)) {
  12569. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  12570. node.property = this.parsePrivateName();
  12571. } else {
  12572. node.property = this.parseIdentifier(true);
  12573. }
  12574. node.computed = false;
  12575. expr = this.finishNode(node, "MemberExpression");
  12576. }
  12577. node.expression = this.parseMaybeDecoratorArguments(expr);
  12578. }
  12579. this.state.decoratorStack.pop();
  12580. } else {
  12581. node.expression = this.parseExprSubscripts();
  12582. }
  12583. return this.finishNode(node, "Decorator");
  12584. }
  12585. parseMaybeDecoratorArguments(expr) {
  12586. if (this.eat(10)) {
  12587. const node = this.startNodeAtNode(expr);
  12588. node.callee = expr;
  12589. node.arguments = this.parseCallExpressionArguments(11, false);
  12590. this.toReferencedList(node.arguments);
  12591. return this.finishNode(node, "CallExpression");
  12592. }
  12593. return expr;
  12594. }
  12595. parseBreakContinueStatement(node, isBreak) {
  12596. this.next();
  12597. if (this.isLineTerminator()) {
  12598. node.label = null;
  12599. } else {
  12600. node.label = this.parseIdentifier();
  12601. this.semicolon();
  12602. }
  12603. this.verifyBreakContinue(node, isBreak);
  12604. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  12605. }
  12606. verifyBreakContinue(node, isBreak) {
  12607. let i;
  12608. for (i = 0; i < this.state.labels.length; ++i) {
  12609. const lab = this.state.labels[i];
  12610. if (node.label == null || lab.name === node.label.name) {
  12611. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  12612. if (node.label && isBreak) break;
  12613. }
  12614. }
  12615. if (i === this.state.labels.length) {
  12616. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  12617. this.raise(Errors.IllegalBreakContinue, {
  12618. at: node,
  12619. type
  12620. });
  12621. }
  12622. }
  12623. parseDebuggerStatement(node) {
  12624. this.next();
  12625. this.semicolon();
  12626. return this.finishNode(node, "DebuggerStatement");
  12627. }
  12628. parseHeaderExpression() {
  12629. this.expect(10);
  12630. const val = this.parseExpression();
  12631. this.expect(11);
  12632. return val;
  12633. }
  12634. parseDoStatement(node) {
  12635. this.next();
  12636. this.state.labels.push(loopLabel);
  12637. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
  12638. this.state.labels.pop();
  12639. this.expect(92);
  12640. node.test = this.parseHeaderExpression();
  12641. this.eat(13);
  12642. return this.finishNode(node, "DoWhileStatement");
  12643. }
  12644. parseForStatement(node) {
  12645. this.next();
  12646. this.state.labels.push(loopLabel);
  12647. let awaitAt = null;
  12648. if (this.isAwaitAllowed() && this.eatContextual(96)) {
  12649. awaitAt = this.state.lastTokStartLoc;
  12650. }
  12651. this.scope.enter(SCOPE_OTHER);
  12652. this.expect(10);
  12653. if (this.match(13)) {
  12654. if (awaitAt !== null) {
  12655. this.unexpected(awaitAt);
  12656. }
  12657. return this.parseFor(node, null);
  12658. }
  12659. const startsWithLet = this.isContextual(99);
  12660. const isLet = startsWithLet && this.isLetKeyword();
  12661. if (this.match(74) || this.match(75) || isLet) {
  12662. const initNode = this.startNode();
  12663. const kind = isLet ? "let" : this.state.value;
  12664. this.next();
  12665. this.parseVar(initNode, true, kind);
  12666. const init = this.finishNode(initNode, "VariableDeclaration");
  12667. if ((this.match(58) || this.isContextual(101)) && init.declarations.length === 1) {
  12668. return this.parseForIn(node, init, awaitAt);
  12669. }
  12670. if (awaitAt !== null) {
  12671. this.unexpected(awaitAt);
  12672. }
  12673. return this.parseFor(node, init);
  12674. }
  12675. const startsWithAsync = this.isContextual(95);
  12676. const refExpressionErrors = new ExpressionErrors();
  12677. const init = this.parseExpression(true, refExpressionErrors);
  12678. const isForOf = this.isContextual(101);
  12679. if (isForOf) {
  12680. if (startsWithLet) {
  12681. this.raise(Errors.ForOfLet, {
  12682. at: init
  12683. });
  12684. }
  12685. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  12686. this.raise(Errors.ForOfAsync, {
  12687. at: init
  12688. });
  12689. }
  12690. }
  12691. if (isForOf || this.match(58)) {
  12692. this.checkDestructuringPrivate(refExpressionErrors);
  12693. this.toAssignable(init, true);
  12694. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  12695. this.checkLVal(init, {
  12696. in: {
  12697. type
  12698. }
  12699. });
  12700. return this.parseForIn(node, init, awaitAt);
  12701. } else {
  12702. this.checkExpressionErrors(refExpressionErrors, true);
  12703. }
  12704. if (awaitAt !== null) {
  12705. this.unexpected(awaitAt);
  12706. }
  12707. return this.parseFor(node, init);
  12708. }
  12709. parseFunctionStatement(node, isAsync, declarationPosition) {
  12710. this.next();
  12711. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  12712. }
  12713. parseIfStatement(node) {
  12714. this.next();
  12715. node.test = this.parseHeaderExpression();
  12716. node.consequent = this.parseStatement("if");
  12717. node.alternate = this.eat(66) ? this.parseStatement("if") : null;
  12718. return this.finishNode(node, "IfStatement");
  12719. }
  12720. parseReturnStatement(node) {
  12721. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  12722. this.raise(Errors.IllegalReturn, {
  12723. at: this.state.startLoc
  12724. });
  12725. }
  12726. this.next();
  12727. if (this.isLineTerminator()) {
  12728. node.argument = null;
  12729. } else {
  12730. node.argument = this.parseExpression();
  12731. this.semicolon();
  12732. }
  12733. return this.finishNode(node, "ReturnStatement");
  12734. }
  12735. parseSwitchStatement(node) {
  12736. this.next();
  12737. node.discriminant = this.parseHeaderExpression();
  12738. const cases = node.cases = [];
  12739. this.expect(5);
  12740. this.state.labels.push(switchLabel);
  12741. this.scope.enter(SCOPE_OTHER);
  12742. let cur;
  12743. for (let sawDefault; !this.match(8);) {
  12744. if (this.match(61) || this.match(65)) {
  12745. const isCase = this.match(61);
  12746. if (cur) this.finishNode(cur, "SwitchCase");
  12747. cases.push(cur = this.startNode());
  12748. cur.consequent = [];
  12749. this.next();
  12750. if (isCase) {
  12751. cur.test = this.parseExpression();
  12752. } else {
  12753. if (sawDefault) {
  12754. this.raise(Errors.MultipleDefaultsInSwitch, {
  12755. at: this.state.lastTokStartLoc
  12756. });
  12757. }
  12758. sawDefault = true;
  12759. cur.test = null;
  12760. }
  12761. this.expect(14);
  12762. } else {
  12763. if (cur) {
  12764. cur.consequent.push(this.parseStatement(null));
  12765. } else {
  12766. this.unexpected();
  12767. }
  12768. }
  12769. }
  12770. this.scope.exit();
  12771. if (cur) this.finishNode(cur, "SwitchCase");
  12772. this.next();
  12773. this.state.labels.pop();
  12774. return this.finishNode(node, "SwitchStatement");
  12775. }
  12776. parseThrowStatement(node) {
  12777. this.next();
  12778. if (this.hasPrecedingLineBreak()) {
  12779. this.raise(Errors.NewlineAfterThrow, {
  12780. at: this.state.lastTokEndLoc
  12781. });
  12782. }
  12783. node.argument = this.parseExpression();
  12784. this.semicolon();
  12785. return this.finishNode(node, "ThrowStatement");
  12786. }
  12787. parseCatchClauseParam() {
  12788. const param = this.parseBindingAtom();
  12789. const simple = param.type === "Identifier";
  12790. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  12791. this.checkLVal(param, {
  12792. in: {
  12793. type: "CatchClause"
  12794. },
  12795. binding: BIND_LEXICAL,
  12796. allowingSloppyLetBinding: true
  12797. });
  12798. return param;
  12799. }
  12800. parseTryStatement(node) {
  12801. this.next();
  12802. node.block = this.parseBlock();
  12803. node.handler = null;
  12804. if (this.match(62)) {
  12805. const clause = this.startNode();
  12806. this.next();
  12807. if (this.match(10)) {
  12808. this.expect(10);
  12809. clause.param = this.parseCatchClauseParam();
  12810. this.expect(11);
  12811. } else {
  12812. clause.param = null;
  12813. this.scope.enter(SCOPE_OTHER);
  12814. }
  12815. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  12816. this.scope.exit();
  12817. node.handler = this.finishNode(clause, "CatchClause");
  12818. }
  12819. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  12820. if (!node.handler && !node.finalizer) {
  12821. this.raise(Errors.NoCatchOrFinally, {
  12822. at: node
  12823. });
  12824. }
  12825. return this.finishNode(node, "TryStatement");
  12826. }
  12827. parseVarStatement(node, kind, allowMissingInitializer = false) {
  12828. this.next();
  12829. this.parseVar(node, false, kind, allowMissingInitializer);
  12830. this.semicolon();
  12831. return this.finishNode(node, "VariableDeclaration");
  12832. }
  12833. parseWhileStatement(node) {
  12834. this.next();
  12835. node.test = this.parseHeaderExpression();
  12836. this.state.labels.push(loopLabel);
  12837. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
  12838. this.state.labels.pop();
  12839. return this.finishNode(node, "WhileStatement");
  12840. }
  12841. parseWithStatement(node) {
  12842. if (this.state.strict) {
  12843. this.raise(Errors.StrictWith, {
  12844. at: this.state.startLoc
  12845. });
  12846. }
  12847. this.next();
  12848. node.object = this.parseHeaderExpression();
  12849. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
  12850. return this.finishNode(node, "WithStatement");
  12851. }
  12852. parseEmptyStatement(node) {
  12853. this.next();
  12854. return this.finishNode(node, "EmptyStatement");
  12855. }
  12856. parseLabeledStatement(node, maybeName, expr, context) {
  12857. for (const label of this.state.labels) {
  12858. if (label.name === maybeName) {
  12859. this.raise(Errors.LabelRedeclaration, {
  12860. at: expr,
  12861. labelName: maybeName
  12862. });
  12863. }
  12864. }
  12865. const kind = tokenIsLoop(this.state.type) ? "loop" : this.match(71) ? "switch" : null;
  12866. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  12867. const label = this.state.labels[i];
  12868. if (label.statementStart === node.start) {
  12869. label.statementStart = this.state.start;
  12870. label.kind = kind;
  12871. } else {
  12872. break;
  12873. }
  12874. }
  12875. this.state.labels.push({
  12876. name: maybeName,
  12877. kind: kind,
  12878. statementStart: this.state.start
  12879. });
  12880. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  12881. this.state.labels.pop();
  12882. node.label = expr;
  12883. return this.finishNode(node, "LabeledStatement");
  12884. }
  12885. parseExpressionStatement(node, expr) {
  12886. node.expression = expr;
  12887. this.semicolon();
  12888. return this.finishNode(node, "ExpressionStatement");
  12889. }
  12890. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  12891. const node = this.startNode();
  12892. if (allowDirectives) {
  12893. this.state.strictErrors.clear();
  12894. }
  12895. this.expect(5);
  12896. if (createNewLexicalScope) {
  12897. this.scope.enter(SCOPE_OTHER);
  12898. }
  12899. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  12900. this.next();
  12901. if (createNewLexicalScope) {
  12902. this.scope.exit();
  12903. }
  12904. return this.finishNode(node, "BlockStatement");
  12905. }
  12906. isValidDirective(stmt) {
  12907. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  12908. }
  12909. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  12910. const body = node.body = [];
  12911. const directives = node.directives = [];
  12912. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  12913. }
  12914. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  12915. const oldStrict = this.state.strict;
  12916. let hasStrictModeDirective = false;
  12917. let parsedNonDirective = false;
  12918. while (!this.match(end)) {
  12919. const stmt = this.parseStatement(null, topLevel);
  12920. if (directives && !parsedNonDirective) {
  12921. if (this.isValidDirective(stmt)) {
  12922. const directive = this.stmtToDirective(stmt);
  12923. directives.push(directive);
  12924. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  12925. hasStrictModeDirective = true;
  12926. this.setStrict(true);
  12927. }
  12928. continue;
  12929. }
  12930. parsedNonDirective = true;
  12931. this.state.strictErrors.clear();
  12932. }
  12933. body.push(stmt);
  12934. }
  12935. if (afterBlockParse) {
  12936. afterBlockParse.call(this, hasStrictModeDirective);
  12937. }
  12938. if (!oldStrict) {
  12939. this.setStrict(false);
  12940. }
  12941. }
  12942. parseFor(node, init) {
  12943. node.init = init;
  12944. this.semicolon(false);
  12945. node.test = this.match(13) ? null : this.parseExpression();
  12946. this.semicolon(false);
  12947. node.update = this.match(11) ? null : this.parseExpression();
  12948. this.expect(11);
  12949. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12950. this.scope.exit();
  12951. this.state.labels.pop();
  12952. return this.finishNode(node, "ForStatement");
  12953. }
  12954. parseForIn(node, init, awaitAt) {
  12955. const isForIn = this.match(58);
  12956. this.next();
  12957. if (isForIn) {
  12958. if (awaitAt !== null) this.unexpected(awaitAt);
  12959. } else {
  12960. node.await = awaitAt !== null;
  12961. }
  12962. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  12963. this.raise(Errors.ForInOfLoopInitializer, {
  12964. at: init,
  12965. type: isForIn ? "ForInStatement" : "ForOfStatement"
  12966. });
  12967. }
  12968. if (init.type === "AssignmentPattern") {
  12969. this.raise(Errors.InvalidLhs, {
  12970. at: init,
  12971. ancestor: {
  12972. type: "ForStatement"
  12973. }
  12974. });
  12975. }
  12976. node.left = init;
  12977. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  12978. this.expect(11);
  12979. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  12980. this.scope.exit();
  12981. this.state.labels.pop();
  12982. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  12983. }
  12984. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  12985. const declarations = node.declarations = [];
  12986. node.kind = kind;
  12987. for (;;) {
  12988. const decl = this.startNode();
  12989. this.parseVarId(decl, kind);
  12990. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  12991. if (decl.init === null && !allowMissingInitializer) {
  12992. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(101)))) {
  12993. this.raise(Errors.DeclarationMissingInitializer, {
  12994. at: this.state.lastTokEndLoc,
  12995. kind: "destructuring"
  12996. });
  12997. } else if (kind === "const" && !(this.match(58) || this.isContextual(101))) {
  12998. this.raise(Errors.DeclarationMissingInitializer, {
  12999. at: this.state.lastTokEndLoc,
  13000. kind: "const"
  13001. });
  13002. }
  13003. }
  13004. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13005. if (!this.eat(12)) break;
  13006. }
  13007. return node;
  13008. }
  13009. parseVarId(decl, kind) {
  13010. decl.id = this.parseBindingAtom();
  13011. this.checkLVal(decl.id, {
  13012. in: {
  13013. type: "VariableDeclarator"
  13014. },
  13015. binding: kind === "var" ? BIND_VAR : BIND_LEXICAL
  13016. });
  13017. }
  13018. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  13019. const isStatement = statement & FUNC_STATEMENT;
  13020. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  13021. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  13022. this.initFunction(node, isAsync);
  13023. if (this.match(55) && isHangingStatement) {
  13024. this.raise(Errors.GeneratorInSingleStatementContext, {
  13025. at: this.state.startLoc
  13026. });
  13027. }
  13028. node.generator = this.eat(55);
  13029. if (isStatement) {
  13030. node.id = this.parseFunctionId(requireId);
  13031. }
  13032. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13033. this.state.maybeInArrowParameters = false;
  13034. this.scope.enter(SCOPE_FUNCTION);
  13035. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13036. if (!isStatement) {
  13037. node.id = this.parseFunctionId();
  13038. }
  13039. this.parseFunctionParams(node, false);
  13040. this.withSmartMixTopicForbiddingContext(() => {
  13041. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  13042. });
  13043. this.prodParam.exit();
  13044. this.scope.exit();
  13045. if (isStatement && !isHangingStatement) {
  13046. this.registerFunctionStatementId(node);
  13047. }
  13048. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13049. return node;
  13050. }
  13051. parseFunctionId(requireId) {
  13052. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13053. }
  13054. parseFunctionParams(node, allowModifiers) {
  13055. this.expect(10);
  13056. this.expressionScope.enter(newParameterDeclarationScope());
  13057. node.params = this.parseBindingList(11, 41, false, allowModifiers);
  13058. this.expressionScope.exit();
  13059. }
  13060. registerFunctionStatementId(node) {
  13061. if (!node.id) return;
  13062. 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);
  13063. }
  13064. parseClass(node, isStatement, optionalId) {
  13065. this.next();
  13066. this.takeDecorators(node);
  13067. const oldStrict = this.state.strict;
  13068. this.state.strict = true;
  13069. this.parseClassId(node, isStatement, optionalId);
  13070. this.parseClassSuper(node);
  13071. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13072. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13073. }
  13074. isClassProperty() {
  13075. return this.match(29) || this.match(13) || this.match(8);
  13076. }
  13077. isClassMethod() {
  13078. return this.match(10);
  13079. }
  13080. isNonstaticConstructor(method) {
  13081. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  13082. }
  13083. parseClassBody(hadSuperClass, oldStrict) {
  13084. this.classScope.enter();
  13085. const state = {
  13086. hadConstructor: false,
  13087. hadSuperClass
  13088. };
  13089. let decorators = [];
  13090. const classBody = this.startNode();
  13091. classBody.body = [];
  13092. this.expect(5);
  13093. this.withSmartMixTopicForbiddingContext(() => {
  13094. while (!this.match(8)) {
  13095. if (this.eat(13)) {
  13096. if (decorators.length > 0) {
  13097. throw this.raise(Errors.DecoratorSemicolon, {
  13098. at: this.state.lastTokEndLoc
  13099. });
  13100. }
  13101. continue;
  13102. }
  13103. if (this.match(26)) {
  13104. decorators.push(this.parseDecorator());
  13105. continue;
  13106. }
  13107. const member = this.startNode();
  13108. if (decorators.length) {
  13109. member.decorators = decorators;
  13110. this.resetStartLocationFromNode(member, decorators[0]);
  13111. decorators = [];
  13112. }
  13113. this.parseClassMember(classBody, member, state);
  13114. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13115. this.raise(Errors.DecoratorConstructor, {
  13116. at: member
  13117. });
  13118. }
  13119. }
  13120. });
  13121. this.state.strict = oldStrict;
  13122. this.next();
  13123. if (decorators.length) {
  13124. throw this.raise(Errors.TrailingDecorator, {
  13125. at: this.state.startLoc
  13126. });
  13127. }
  13128. this.classScope.exit();
  13129. return this.finishNode(classBody, "ClassBody");
  13130. }
  13131. parseClassMemberFromModifier(classBody, member) {
  13132. const key = this.parseIdentifier(true);
  13133. if (this.isClassMethod()) {
  13134. const method = member;
  13135. method.kind = "method";
  13136. method.computed = false;
  13137. method.key = key;
  13138. method.static = false;
  13139. this.pushClassMethod(classBody, method, false, false, false, false);
  13140. return true;
  13141. } else if (this.isClassProperty()) {
  13142. const prop = member;
  13143. prop.computed = false;
  13144. prop.key = key;
  13145. prop.static = false;
  13146. classBody.body.push(this.parseClassProperty(prop));
  13147. return true;
  13148. }
  13149. this.resetPreviousNodeTrailingComments(key);
  13150. return false;
  13151. }
  13152. parseClassMember(classBody, member, state) {
  13153. const isStatic = this.isContextual(104);
  13154. if (isStatic) {
  13155. if (this.parseClassMemberFromModifier(classBody, member)) {
  13156. return;
  13157. }
  13158. if (this.eat(5)) {
  13159. this.parseClassStaticBlock(classBody, member);
  13160. return;
  13161. }
  13162. }
  13163. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13164. }
  13165. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13166. const publicMethod = member;
  13167. const privateMethod = member;
  13168. const publicProp = member;
  13169. const privateProp = member;
  13170. const accessorProp = member;
  13171. const method = publicMethod;
  13172. const publicMember = publicMethod;
  13173. member.static = isStatic;
  13174. this.parsePropertyNamePrefixOperator(member);
  13175. if (this.eat(55)) {
  13176. method.kind = "method";
  13177. const isPrivateName = this.match(134);
  13178. this.parseClassElementName(method);
  13179. if (isPrivateName) {
  13180. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13181. return;
  13182. }
  13183. if (this.isNonstaticConstructor(publicMethod)) {
  13184. this.raise(Errors.ConstructorIsGenerator, {
  13185. at: publicMethod.key
  13186. });
  13187. }
  13188. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13189. return;
  13190. }
  13191. const isContextual = tokenIsIdentifier(this.state.type) && !this.state.containsEsc;
  13192. const isPrivate = this.match(134);
  13193. const key = this.parseClassElementName(member);
  13194. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13195. this.parsePostMemberNameModifiers(publicMember);
  13196. if (this.isClassMethod()) {
  13197. method.kind = "method";
  13198. if (isPrivate) {
  13199. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13200. return;
  13201. }
  13202. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13203. let allowsDirectSuper = false;
  13204. if (isConstructor) {
  13205. publicMethod.kind = "constructor";
  13206. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13207. this.raise(Errors.DuplicateConstructor, {
  13208. at: key
  13209. });
  13210. }
  13211. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13212. this.raise(Errors.OverrideOnConstructor, {
  13213. at: key
  13214. });
  13215. }
  13216. state.hadConstructor = true;
  13217. allowsDirectSuper = state.hadSuperClass;
  13218. }
  13219. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13220. } else if (this.isClassProperty()) {
  13221. if (isPrivate) {
  13222. this.pushClassPrivateProperty(classBody, privateProp);
  13223. } else {
  13224. this.pushClassProperty(classBody, publicProp);
  13225. }
  13226. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  13227. this.resetPreviousNodeTrailingComments(key);
  13228. const isGenerator = this.eat(55);
  13229. if (publicMember.optional) {
  13230. this.unexpected(maybeQuestionTokenStartLoc);
  13231. }
  13232. method.kind = "method";
  13233. const isPrivate = this.match(134);
  13234. this.parseClassElementName(method);
  13235. this.parsePostMemberNameModifiers(publicMember);
  13236. if (isPrivate) {
  13237. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13238. } else {
  13239. if (this.isNonstaticConstructor(publicMethod)) {
  13240. this.raise(Errors.ConstructorIsAsync, {
  13241. at: publicMethod.key
  13242. });
  13243. }
  13244. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13245. }
  13246. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(55) && this.isLineTerminator())) {
  13247. this.resetPreviousNodeTrailingComments(key);
  13248. method.kind = key.name;
  13249. const isPrivate = this.match(134);
  13250. this.parseClassElementName(publicMethod);
  13251. if (isPrivate) {
  13252. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13253. } else {
  13254. if (this.isNonstaticConstructor(publicMethod)) {
  13255. this.raise(Errors.ConstructorIsAccessor, {
  13256. at: publicMethod.key
  13257. });
  13258. }
  13259. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13260. }
  13261. this.checkGetterSetterParams(publicMethod);
  13262. } else if (isContextual && key.name === "accessor" && !this.isLineTerminator()) {
  13263. this.expectPlugin("decoratorAutoAccessors");
  13264. this.resetPreviousNodeTrailingComments(key);
  13265. const isPrivate = this.match(134);
  13266. this.parseClassElementName(publicProp);
  13267. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13268. } else if (this.isLineTerminator()) {
  13269. if (isPrivate) {
  13270. this.pushClassPrivateProperty(classBody, privateProp);
  13271. } else {
  13272. this.pushClassProperty(classBody, publicProp);
  13273. }
  13274. } else {
  13275. this.unexpected();
  13276. }
  13277. }
  13278. parseClassElementName(member) {
  13279. const {
  13280. type,
  13281. value
  13282. } = this.state;
  13283. if ((type === 128 || type === 129) && member.static && value === "prototype") {
  13284. this.raise(Errors.StaticPrototype, {
  13285. at: this.state.startLoc
  13286. });
  13287. }
  13288. if (type === 134) {
  13289. if (value === "constructor") {
  13290. this.raise(Errors.ConstructorClassPrivateField, {
  13291. at: this.state.startLoc
  13292. });
  13293. }
  13294. const key = this.parsePrivateName();
  13295. member.key = key;
  13296. return key;
  13297. }
  13298. return this.parsePropertyName(member);
  13299. }
  13300. parseClassStaticBlock(classBody, member) {
  13301. var _member$decorators;
  13302. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  13303. const oldLabels = this.state.labels;
  13304. this.state.labels = [];
  13305. this.prodParam.enter(PARAM);
  13306. const body = member.body = [];
  13307. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13308. this.next();
  13309. this.prodParam.exit();
  13310. this.scope.exit();
  13311. this.state.labels = oldLabels;
  13312. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13313. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13314. this.raise(Errors.DecoratorStaticBlock, {
  13315. at: member
  13316. });
  13317. }
  13318. }
  13319. pushClassProperty(classBody, prop) {
  13320. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  13321. this.raise(Errors.ConstructorClassField, {
  13322. at: prop.key
  13323. });
  13324. }
  13325. classBody.body.push(this.parseClassProperty(prop));
  13326. }
  13327. pushClassPrivateProperty(classBody, prop) {
  13328. const node = this.parseClassPrivateProperty(prop);
  13329. classBody.body.push(node);
  13330. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13331. }
  13332. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13333. if (!isPrivate && !prop.computed) {
  13334. const key = prop.key;
  13335. if (key.name === "constructor" || key.value === "constructor") {
  13336. this.raise(Errors.ConstructorClassField, {
  13337. at: key
  13338. });
  13339. }
  13340. }
  13341. const node = this.parseClassAccessorProperty(prop);
  13342. classBody.body.push(node);
  13343. if (isPrivate) {
  13344. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.loc.start);
  13345. }
  13346. }
  13347. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13348. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13349. }
  13350. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13351. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13352. classBody.body.push(node);
  13353. 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;
  13354. this.declareClassPrivateMethodInScope(node, kind);
  13355. }
  13356. declareClassPrivateMethodInScope(node, kind) {
  13357. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13358. }
  13359. parsePostMemberNameModifiers(methodOrProp) {}
  13360. parseClassPrivateProperty(node) {
  13361. this.parseInitializer(node);
  13362. this.semicolon();
  13363. return this.finishNode(node, "ClassPrivateProperty");
  13364. }
  13365. parseClassProperty(node) {
  13366. this.parseInitializer(node);
  13367. this.semicolon();
  13368. return this.finishNode(node, "ClassProperty");
  13369. }
  13370. parseClassAccessorProperty(node) {
  13371. this.parseInitializer(node);
  13372. this.semicolon();
  13373. return this.finishNode(node, "ClassAccessorProperty");
  13374. }
  13375. parseInitializer(node) {
  13376. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  13377. this.expressionScope.enter(newExpressionScope());
  13378. this.prodParam.enter(PARAM);
  13379. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13380. this.expressionScope.exit();
  13381. this.prodParam.exit();
  13382. this.scope.exit();
  13383. }
  13384. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  13385. if (tokenIsIdentifier(this.state.type)) {
  13386. node.id = this.parseIdentifier();
  13387. if (isStatement) {
  13388. this.declareNameFromIdentifier(node.id, bindingType);
  13389. }
  13390. } else {
  13391. if (optionalId || !isStatement) {
  13392. node.id = null;
  13393. } else {
  13394. throw this.raise(Errors.MissingClassName, {
  13395. at: this.state.startLoc
  13396. });
  13397. }
  13398. }
  13399. }
  13400. parseClassSuper(node) {
  13401. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13402. }
  13403. parseExport(node) {
  13404. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  13405. const parseAfterDefault = !hasDefault || this.eat(12);
  13406. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13407. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13408. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13409. const isFromRequired = hasDefault || hasStar;
  13410. if (hasStar && !hasNamespace) {
  13411. if (hasDefault) this.unexpected();
  13412. this.parseExportFrom(node, true);
  13413. return this.finishNode(node, "ExportAllDeclaration");
  13414. }
  13415. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13416. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  13417. throw this.unexpected(null, 5);
  13418. }
  13419. let hasDeclaration;
  13420. if (isFromRequired || hasSpecifiers) {
  13421. hasDeclaration = false;
  13422. this.parseExportFrom(node, isFromRequired);
  13423. } else {
  13424. hasDeclaration = this.maybeParseExportDeclaration(node);
  13425. }
  13426. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13427. this.checkExport(node, true, false, !!node.source);
  13428. return this.finishNode(node, "ExportNamedDeclaration");
  13429. }
  13430. if (this.eat(65)) {
  13431. node.declaration = this.parseExportDefaultExpression();
  13432. this.checkExport(node, true, true);
  13433. return this.finishNode(node, "ExportDefaultDeclaration");
  13434. }
  13435. throw this.unexpected(null, 5);
  13436. }
  13437. eatExportStar(node) {
  13438. return this.eat(55);
  13439. }
  13440. maybeParseExportDefaultSpecifier(node) {
  13441. if (this.isExportDefaultSpecifier()) {
  13442. this.expectPlugin("exportDefaultFrom");
  13443. const specifier = this.startNode();
  13444. specifier.exported = this.parseIdentifier(true);
  13445. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13446. return true;
  13447. }
  13448. return false;
  13449. }
  13450. maybeParseExportNamespaceSpecifier(node) {
  13451. if (this.isContextual(93)) {
  13452. if (!node.specifiers) node.specifiers = [];
  13453. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  13454. this.next();
  13455. specifier.exported = this.parseModuleExportName();
  13456. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13457. return true;
  13458. }
  13459. return false;
  13460. }
  13461. maybeParseExportNamedSpecifiers(node) {
  13462. if (this.match(5)) {
  13463. if (!node.specifiers) node.specifiers = [];
  13464. const isTypeExport = node.exportKind === "type";
  13465. node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13466. node.source = null;
  13467. node.declaration = null;
  13468. if (this.hasPlugin("importAssertions")) {
  13469. node.assertions = [];
  13470. }
  13471. return true;
  13472. }
  13473. return false;
  13474. }
  13475. maybeParseExportDeclaration(node) {
  13476. if (this.shouldParseExportDeclaration()) {
  13477. node.specifiers = [];
  13478. node.source = null;
  13479. if (this.hasPlugin("importAssertions")) {
  13480. node.assertions = [];
  13481. }
  13482. node.declaration = this.parseExportDeclaration(node);
  13483. return true;
  13484. }
  13485. return false;
  13486. }
  13487. isAsyncFunction() {
  13488. if (!this.isContextual(95)) return false;
  13489. const next = this.nextTokenStart();
  13490. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  13491. }
  13492. parseExportDefaultExpression() {
  13493. const expr = this.startNode();
  13494. const isAsync = this.isAsyncFunction();
  13495. if (this.match(68) || isAsync) {
  13496. this.next();
  13497. if (isAsync) {
  13498. this.next();
  13499. }
  13500. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  13501. }
  13502. if (this.match(80)) {
  13503. return this.parseClass(expr, true, true);
  13504. }
  13505. if (this.match(26)) {
  13506. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13507. this.raise(Errors.DecoratorBeforeExport, {
  13508. at: this.state.startLoc
  13509. });
  13510. }
  13511. this.parseDecorators(false);
  13512. return this.parseClass(expr, true, true);
  13513. }
  13514. if (this.match(75) || this.match(74) || this.isLet()) {
  13515. throw this.raise(Errors.UnsupportedDefaultExport, {
  13516. at: this.state.startLoc
  13517. });
  13518. }
  13519. const res = this.parseMaybeAssignAllowIn();
  13520. this.semicolon();
  13521. return res;
  13522. }
  13523. parseExportDeclaration(node) {
  13524. return this.parseStatement(null);
  13525. }
  13526. isExportDefaultSpecifier() {
  13527. const {
  13528. type
  13529. } = this.state;
  13530. if (tokenIsIdentifier(type)) {
  13531. if (type === 95 && !this.state.containsEsc || type === 99) {
  13532. return false;
  13533. }
  13534. if ((type === 126 || type === 125) && !this.state.containsEsc) {
  13535. const {
  13536. type: nextType
  13537. } = this.lookahead();
  13538. if (tokenIsIdentifier(nextType) && nextType !== 97 || nextType === 5) {
  13539. this.expectOnePlugin(["flow", "typescript"]);
  13540. return false;
  13541. }
  13542. }
  13543. } else if (!this.match(65)) {
  13544. return false;
  13545. }
  13546. const next = this.nextTokenStart();
  13547. const hasFrom = this.isUnparsedContextual(next, "from");
  13548. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13549. return true;
  13550. }
  13551. if (this.match(65) && hasFrom) {
  13552. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13553. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13554. }
  13555. return false;
  13556. }
  13557. parseExportFrom(node, expect) {
  13558. if (this.eatContextual(97)) {
  13559. node.source = this.parseImportSource();
  13560. this.checkExport(node);
  13561. const assertions = this.maybeParseImportAssertions();
  13562. if (assertions) {
  13563. node.assertions = assertions;
  13564. this.checkJSONModuleImport(node);
  13565. }
  13566. } else if (expect) {
  13567. this.unexpected();
  13568. }
  13569. this.semicolon();
  13570. }
  13571. shouldParseExportDeclaration() {
  13572. const {
  13573. type
  13574. } = this.state;
  13575. if (type === 26) {
  13576. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  13577. if (this.hasPlugin("decorators")) {
  13578. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  13579. throw this.raise(Errors.DecoratorBeforeExport, {
  13580. at: this.state.startLoc
  13581. });
  13582. }
  13583. return true;
  13584. }
  13585. }
  13586. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  13587. }
  13588. checkExport(node, checkNames, isDefault, isFrom) {
  13589. if (checkNames) {
  13590. if (isDefault) {
  13591. this.checkDuplicateExports(node, "default");
  13592. if (this.hasPlugin("exportDefaultFrom")) {
  13593. var _declaration$extra;
  13594. const declaration = node.declaration;
  13595. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  13596. this.raise(Errors.ExportDefaultFromAsIdentifier, {
  13597. at: declaration
  13598. });
  13599. }
  13600. }
  13601. } else if (node.specifiers && node.specifiers.length) {
  13602. for (const specifier of node.specifiers) {
  13603. const {
  13604. exported
  13605. } = specifier;
  13606. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  13607. this.checkDuplicateExports(specifier, exportName);
  13608. if (!isFrom && specifier.local) {
  13609. const {
  13610. local
  13611. } = specifier;
  13612. if (local.type !== "Identifier") {
  13613. this.raise(Errors.ExportBindingIsString, {
  13614. at: specifier,
  13615. localName: local.value,
  13616. exportName
  13617. });
  13618. } else {
  13619. this.checkReservedWord(local.name, local.loc.start, true, false);
  13620. this.scope.checkLocalExport(local);
  13621. }
  13622. }
  13623. }
  13624. } else if (node.declaration) {
  13625. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  13626. const id = node.declaration.id;
  13627. if (!id) throw new Error("Assertion failure");
  13628. this.checkDuplicateExports(node, id.name);
  13629. } else if (node.declaration.type === "VariableDeclaration") {
  13630. for (const declaration of node.declaration.declarations) {
  13631. this.checkDeclaration(declaration.id);
  13632. }
  13633. }
  13634. }
  13635. }
  13636. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  13637. if (currentContextDecorators.length) {
  13638. throw this.raise(Errors.UnsupportedDecoratorExport, {
  13639. at: node
  13640. });
  13641. }
  13642. }
  13643. checkDeclaration(node) {
  13644. if (node.type === "Identifier") {
  13645. this.checkDuplicateExports(node, node.name);
  13646. } else if (node.type === "ObjectPattern") {
  13647. for (const prop of node.properties) {
  13648. this.checkDeclaration(prop);
  13649. }
  13650. } else if (node.type === "ArrayPattern") {
  13651. for (const elem of node.elements) {
  13652. if (elem) {
  13653. this.checkDeclaration(elem);
  13654. }
  13655. }
  13656. } else if (node.type === "ObjectProperty") {
  13657. this.checkDeclaration(node.value);
  13658. } else if (node.type === "RestElement") {
  13659. this.checkDeclaration(node.argument);
  13660. } else if (node.type === "AssignmentPattern") {
  13661. this.checkDeclaration(node.left);
  13662. }
  13663. }
  13664. checkDuplicateExports(node, exportName) {
  13665. if (this.exportedIdentifiers.has(exportName)) {
  13666. if (exportName === "default") {
  13667. this.raise(Errors.DuplicateDefaultExport, {
  13668. at: node
  13669. });
  13670. } else {
  13671. this.raise(Errors.DuplicateExport, {
  13672. at: node,
  13673. exportName
  13674. });
  13675. }
  13676. }
  13677. this.exportedIdentifiers.add(exportName);
  13678. }
  13679. parseExportSpecifiers(isInTypeExport) {
  13680. const nodes = [];
  13681. let first = true;
  13682. this.expect(5);
  13683. while (!this.eat(8)) {
  13684. if (first) {
  13685. first = false;
  13686. } else {
  13687. this.expect(12);
  13688. if (this.eat(8)) break;
  13689. }
  13690. const isMaybeTypeOnly = this.isContextual(126);
  13691. const isString = this.match(129);
  13692. const node = this.startNode();
  13693. node.local = this.parseModuleExportName();
  13694. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  13695. }
  13696. return nodes;
  13697. }
  13698. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  13699. if (this.eatContextual(93)) {
  13700. node.exported = this.parseModuleExportName();
  13701. } else if (isString) {
  13702. node.exported = cloneStringLiteral(node.local);
  13703. } else if (!node.exported) {
  13704. node.exported = cloneIdentifier(node.local);
  13705. }
  13706. return this.finishNode(node, "ExportSpecifier");
  13707. }
  13708. parseModuleExportName() {
  13709. if (this.match(129)) {
  13710. const result = this.parseStringLiteral(this.state.value);
  13711. const surrogate = result.value.match(loneSurrogate);
  13712. if (surrogate) {
  13713. this.raise(Errors.ModuleExportNameHasLoneSurrogate, {
  13714. at: result,
  13715. surrogateCharCode: surrogate[0].charCodeAt(0)
  13716. });
  13717. }
  13718. return result;
  13719. }
  13720. return this.parseIdentifier(true);
  13721. }
  13722. isJSONModuleImport(node) {
  13723. if (node.assertions != null) {
  13724. return node.assertions.some(({
  13725. key,
  13726. value
  13727. }) => {
  13728. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  13729. });
  13730. }
  13731. return false;
  13732. }
  13733. checkJSONModuleImport(node) {
  13734. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  13735. const {
  13736. specifiers
  13737. } = node;
  13738. if (specifiers != null) {
  13739. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  13740. let imported;
  13741. if (specifier.type === "ExportSpecifier") {
  13742. imported = specifier.local;
  13743. } else if (specifier.type === "ImportSpecifier") {
  13744. imported = specifier.imported;
  13745. }
  13746. if (imported !== undefined) {
  13747. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  13748. }
  13749. });
  13750. if (nonDefaultNamedSpecifier !== undefined) {
  13751. this.raise(Errors.ImportJSONBindingNotDefault, {
  13752. at: nonDefaultNamedSpecifier.loc.start
  13753. });
  13754. }
  13755. }
  13756. }
  13757. }
  13758. parseImport(node) {
  13759. node.specifiers = [];
  13760. if (!this.match(129)) {
  13761. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  13762. const parseNext = !hasDefault || this.eat(12);
  13763. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  13764. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  13765. this.expectContextual(97);
  13766. }
  13767. node.source = this.parseImportSource();
  13768. const assertions = this.maybeParseImportAssertions();
  13769. if (assertions) {
  13770. node.assertions = assertions;
  13771. } else {
  13772. const attributes = this.maybeParseModuleAttributes();
  13773. if (attributes) {
  13774. node.attributes = attributes;
  13775. }
  13776. }
  13777. this.checkJSONModuleImport(node);
  13778. this.semicolon();
  13779. return this.finishNode(node, "ImportDeclaration");
  13780. }
  13781. parseImportSource() {
  13782. if (!this.match(129)) this.unexpected();
  13783. return this.parseExprAtom();
  13784. }
  13785. shouldParseDefaultImport(node) {
  13786. return tokenIsIdentifier(this.state.type);
  13787. }
  13788. parseImportSpecifierLocal(node, specifier, type) {
  13789. specifier.local = this.parseIdentifier();
  13790. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  13791. }
  13792. finishImportSpecifier(specifier, type, bindingType = BIND_LEXICAL) {
  13793. this.checkLVal(specifier.local, {
  13794. in: specifier,
  13795. binding: bindingType
  13796. });
  13797. return this.finishNode(specifier, type);
  13798. }
  13799. parseAssertEntries() {
  13800. const attrs = [];
  13801. const attrNames = new Set();
  13802. do {
  13803. if (this.match(8)) {
  13804. break;
  13805. }
  13806. const node = this.startNode();
  13807. const keyName = this.state.value;
  13808. if (attrNames.has(keyName)) {
  13809. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13810. at: this.state.startLoc,
  13811. key: keyName
  13812. });
  13813. }
  13814. attrNames.add(keyName);
  13815. if (this.match(129)) {
  13816. node.key = this.parseStringLiteral(keyName);
  13817. } else {
  13818. node.key = this.parseIdentifier(true);
  13819. }
  13820. this.expect(14);
  13821. if (!this.match(129)) {
  13822. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13823. at: this.state.startLoc
  13824. });
  13825. }
  13826. node.value = this.parseStringLiteral(this.state.value);
  13827. attrs.push(this.finishNode(node, "ImportAttribute"));
  13828. } while (this.eat(12));
  13829. return attrs;
  13830. }
  13831. maybeParseModuleAttributes() {
  13832. if (this.match(76) && !this.hasPrecedingLineBreak()) {
  13833. this.expectPlugin("moduleAttributes");
  13834. this.next();
  13835. } else {
  13836. if (this.hasPlugin("moduleAttributes")) return [];
  13837. return null;
  13838. }
  13839. const attrs = [];
  13840. const attributes = new Set();
  13841. do {
  13842. const node = this.startNode();
  13843. node.key = this.parseIdentifier(true);
  13844. if (node.key.name !== "type") {
  13845. this.raise(Errors.ModuleAttributeDifferentFromType, {
  13846. at: node.key
  13847. });
  13848. }
  13849. if (attributes.has(node.key.name)) {
  13850. this.raise(Errors.ModuleAttributesWithDuplicateKeys, {
  13851. at: node.key,
  13852. key: node.key.name
  13853. });
  13854. }
  13855. attributes.add(node.key.name);
  13856. this.expect(14);
  13857. if (!this.match(129)) {
  13858. throw this.raise(Errors.ModuleAttributeInvalidValue, {
  13859. at: this.state.startLoc
  13860. });
  13861. }
  13862. node.value = this.parseStringLiteral(this.state.value);
  13863. this.finishNode(node, "ImportAttribute");
  13864. attrs.push(node);
  13865. } while (this.eat(12));
  13866. return attrs;
  13867. }
  13868. maybeParseImportAssertions() {
  13869. if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  13870. this.expectPlugin("importAssertions");
  13871. this.next();
  13872. } else {
  13873. if (this.hasPlugin("importAssertions")) return [];
  13874. return null;
  13875. }
  13876. this.eat(5);
  13877. const attrs = this.parseAssertEntries();
  13878. this.eat(8);
  13879. return attrs;
  13880. }
  13881. maybeParseDefaultImportSpecifier(node) {
  13882. if (this.shouldParseDefaultImport(node)) {
  13883. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  13884. return true;
  13885. }
  13886. return false;
  13887. }
  13888. maybeParseStarImportSpecifier(node) {
  13889. if (this.match(55)) {
  13890. const specifier = this.startNode();
  13891. this.next();
  13892. this.expectContextual(93);
  13893. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  13894. return true;
  13895. }
  13896. return false;
  13897. }
  13898. parseNamedImportSpecifiers(node) {
  13899. let first = true;
  13900. this.expect(5);
  13901. while (!this.eat(8)) {
  13902. if (first) {
  13903. first = false;
  13904. } else {
  13905. if (this.eat(14)) {
  13906. throw this.raise(Errors.DestructureNamedImport, {
  13907. at: this.state.startLoc
  13908. });
  13909. }
  13910. this.expect(12);
  13911. if (this.eat(8)) break;
  13912. }
  13913. const specifier = this.startNode();
  13914. const importedIsString = this.match(129);
  13915. const isMaybeTypeOnly = this.isContextual(126);
  13916. specifier.imported = this.parseModuleExportName();
  13917. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  13918. node.specifiers.push(importSpecifier);
  13919. }
  13920. }
  13921. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  13922. if (this.eatContextual(93)) {
  13923. specifier.local = this.parseIdentifier();
  13924. } else {
  13925. const {
  13926. imported
  13927. } = specifier;
  13928. if (importedIsString) {
  13929. throw this.raise(Errors.ImportBindingIsString, {
  13930. at: specifier,
  13931. importName: imported.value
  13932. });
  13933. }
  13934. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  13935. if (!specifier.local) {
  13936. specifier.local = cloneIdentifier(imported);
  13937. }
  13938. }
  13939. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  13940. }
  13941. isThisParam(param) {
  13942. return param.type === "Identifier" && param.name === "this";
  13943. }
  13944. }
  13945. class Parser extends StatementParser {
  13946. constructor(options, input) {
  13947. options = getOptions(options);
  13948. super(options, input);
  13949. this.options = options;
  13950. this.initializeScopes();
  13951. this.plugins = pluginsMap(this.options.plugins);
  13952. this.filename = options.sourceFilename;
  13953. }
  13954. getScopeHandler() {
  13955. return ScopeHandler;
  13956. }
  13957. parse() {
  13958. this.enterInitialScopes();
  13959. const file = this.startNode();
  13960. const program = this.startNode();
  13961. this.nextToken();
  13962. file.errors = null;
  13963. this.parseTopLevel(file, program);
  13964. file.errors = this.state.errors;
  13965. return file;
  13966. }
  13967. }
  13968. function pluginsMap(plugins) {
  13969. const pluginMap = new Map();
  13970. for (const plugin of plugins) {
  13971. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  13972. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  13973. }
  13974. return pluginMap;
  13975. }
  13976. function parse(input, options) {
  13977. var _options;
  13978. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  13979. options = Object.assign({}, options);
  13980. try {
  13981. options.sourceType = "module";
  13982. const parser = getParser(options, input);
  13983. const ast = parser.parse();
  13984. if (parser.sawUnambiguousESM) {
  13985. return ast;
  13986. }
  13987. if (parser.ambiguousScriptDifferentAst) {
  13988. try {
  13989. options.sourceType = "script";
  13990. return getParser(options, input).parse();
  13991. } catch (_unused) {}
  13992. } else {
  13993. ast.program.sourceType = "script";
  13994. }
  13995. return ast;
  13996. } catch (moduleError) {
  13997. try {
  13998. options.sourceType = "script";
  13999. return getParser(options, input).parse();
  14000. } catch (_unused2) {}
  14001. throw moduleError;
  14002. }
  14003. } else {
  14004. return getParser(options, input).parse();
  14005. }
  14006. }
  14007. function parseExpression(input, options) {
  14008. const parser = getParser(options, input);
  14009. if (parser.options.strictMode) {
  14010. parser.state.strict = true;
  14011. }
  14012. return parser.getExpression();
  14013. }
  14014. function generateExportedTokenTypes(internalTokenTypes) {
  14015. const tokenTypes = {};
  14016. for (const typeName of Object.keys(internalTokenTypes)) {
  14017. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14018. }
  14019. return tokenTypes;
  14020. }
  14021. const tokTypes = generateExportedTokenTypes(tt);
  14022. function getParser(options, input) {
  14023. let cls = Parser;
  14024. if (options != null && options.plugins) {
  14025. validatePlugins(options.plugins);
  14026. cls = getParserClass(options.plugins);
  14027. }
  14028. return new cls(options, input);
  14029. }
  14030. const parserClassCache = {};
  14031. function getParserClass(pluginsFromOptions) {
  14032. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  14033. const key = pluginList.join("/");
  14034. let cls = parserClassCache[key];
  14035. if (!cls) {
  14036. cls = Parser;
  14037. for (const plugin of pluginList) {
  14038. cls = mixinPlugins[plugin](cls);
  14039. }
  14040. parserClassCache[key] = cls;
  14041. }
  14042. return cls;
  14043. }
  14044. exports.parse = parse;
  14045. exports.parseExpression = parseExpression;
  14046. exports.tokTypes = tokTypes;
  14047. //# sourceMappingURL=index.js.map