index.js 501 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. function _objectWithoutPropertiesLoose(r, e) {
  6. if (null == r) return {};
  7. var t = {};
  8. for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
  9. if (-1 !== e.indexOf(n)) continue;
  10. t[n] = r[n];
  11. }
  12. return t;
  13. }
  14. class Position {
  15. constructor(line, col, index) {
  16. this.line = void 0;
  17. this.column = void 0;
  18. this.index = void 0;
  19. this.line = line;
  20. this.column = col;
  21. this.index = index;
  22. }
  23. }
  24. class SourceLocation {
  25. constructor(start, end) {
  26. this.start = void 0;
  27. this.end = void 0;
  28. this.filename = void 0;
  29. this.identifierName = void 0;
  30. this.start = start;
  31. this.end = end;
  32. }
  33. }
  34. function createPositionWithColumnOffset(position, columnOffset) {
  35. const {
  36. line,
  37. column,
  38. index
  39. } = position;
  40. return new Position(line, column + columnOffset, index + columnOffset);
  41. }
  42. const code = "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED";
  43. var ModuleErrors = {
  44. ImportMetaOutsideModule: {
  45. message: `import.meta may appear only with 'sourceType: "module"'`,
  46. code
  47. },
  48. ImportOutsideModule: {
  49. message: `'import' and 'export' may appear only with 'sourceType: "module"'`,
  50. code
  51. }
  52. };
  53. const NodeDescriptions = {
  54. ArrayPattern: "array destructuring pattern",
  55. AssignmentExpression: "assignment expression",
  56. AssignmentPattern: "assignment expression",
  57. ArrowFunctionExpression: "arrow function expression",
  58. ConditionalExpression: "conditional expression",
  59. CatchClause: "catch clause",
  60. ForOfStatement: "for-of statement",
  61. ForInStatement: "for-in statement",
  62. ForStatement: "for-loop",
  63. FormalParameters: "function parameter list",
  64. Identifier: "identifier",
  65. ImportSpecifier: "import specifier",
  66. ImportDefaultSpecifier: "import default specifier",
  67. ImportNamespaceSpecifier: "import namespace specifier",
  68. ObjectPattern: "object destructuring pattern",
  69. ParenthesizedExpression: "parenthesized expression",
  70. RestElement: "rest element",
  71. UpdateExpression: {
  72. true: "prefix operation",
  73. false: "postfix operation"
  74. },
  75. VariableDeclarator: "variable declaration",
  76. YieldExpression: "yield expression"
  77. };
  78. const toNodeDescription = node => node.type === "UpdateExpression" ? NodeDescriptions.UpdateExpression[`${node.prefix}`] : NodeDescriptions[node.type];
  79. var StandardErrors = {
  80. AccessorIsGenerator: ({
  81. kind
  82. }) => `A ${kind}ter cannot be a generator.`,
  83. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  84. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  85. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  86. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  87. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  88. AwaitUsingNotInAsyncContext: "'await using' is only allowed within async functions and at the top levels of modules.",
  89. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  90. BadGetterArity: "A 'get' accessor must not have any formal parameters.",
  91. BadSetterArity: "A 'set' accessor must have exactly one formal parameter.",
  92. BadSetterRestParameter: "A 'set' accessor function argument must not be a rest parameter.",
  93. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  94. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  95. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  96. ConstructorIsAsync: "Constructor can't be an async function.",
  97. ConstructorIsGenerator: "Constructor can't be a generator.",
  98. DeclarationMissingInitializer: ({
  99. kind
  100. }) => `Missing initializer in ${kind} declaration.`,
  101. DecoratorArgumentsOutsideParentheses: "Decorator arguments must be moved inside parentheses: use '@(decorator(args))' instead of '@(decorator)(args)'.",
  102. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. Remove the 'decoratorsBeforeExport: true' option to use the 'export @decorator class {}' syntax.",
  103. DecoratorsBeforeAfterExport: "Decorators can be placed *either* before or after the 'export' keyword, but not in both locations at the same time.",
  104. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  105. DecoratorExportClass: "Decorators must be placed *after* the 'export' keyword. Remove the 'decoratorsBeforeExport: false' option to use the '@decorator export class {}' syntax.",
  106. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  107. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  108. DeferImportRequiresNamespace: 'Only `import defer * as x from "./module"` is valid.',
  109. DeletePrivateField: "Deleting a private field is not allowed.",
  110. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  111. DuplicateConstructor: "Duplicate constructor in the same class.",
  112. DuplicateDefaultExport: "Only one default export allowed per module.",
  113. DuplicateExport: ({
  114. exportName
  115. }) => `\`${exportName}\` has already been exported. Exported identifiers must be unique.`,
  116. DuplicateProto: "Redefinition of __proto__ property.",
  117. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  118. ElementAfterRest: "Rest element must be last element.",
  119. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  120. ExportBindingIsString: ({
  121. localName,
  122. exportName
  123. }) => `A string literal cannot be used as an exported binding without \`from\`.\n- Did you mean \`export { '${localName}' as '${exportName}' } from 'some-module'\`?`,
  124. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  125. ForInOfLoopInitializer: ({
  126. type
  127. }) => `'${type === "ForInStatement" ? "for-in" : "for-of"}' loop variable declaration may not have an initializer.`,
  128. ForInUsing: "For-in loop may not start with 'using' declaration.",
  129. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  130. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  131. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  132. IllegalBreakContinue: ({
  133. type
  134. }) => `Unsyntactic ${type === "BreakStatement" ? "break" : "continue"}.`,
  135. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  136. IllegalReturn: "'return' outside of function.",
  137. ImportAttributesUseAssert: "The `assert` keyword in import attributes is deprecated and it has been replaced by the `with` keyword. You can enable the `deprecatedImportAssert` parser plugin to suppress this error.",
  138. ImportBindingIsString: ({
  139. importName
  140. }) => `A string literal cannot be used as an imported binding.\n- Did you mean \`import { "${importName}" as foo }\`?`,
  141. ImportCallArity: `\`import()\` requires exactly one or two arguments.`,
  142. ImportCallNotNewExpression: "Cannot use new with import(...).",
  143. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  144. ImportJSONBindingNotDefault: "A JSON module can only be imported with `default`.",
  145. ImportReflectionHasAssertion: "`import module x` cannot have assertions.",
  146. ImportReflectionNotBinding: 'Only `import module x from "./module"` is valid.',
  147. IncompatibleRegExpUVFlags: "The 'u' and 'v' regular expression flags cannot be enabled at the same time.",
  148. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  149. InvalidCodePoint: "Code point out of bounds.",
  150. InvalidCoverDiscardElement: "'void' must be followed by an expression when not used in a binding position.",
  151. InvalidCoverInitializedName: "Invalid shorthand property initializer.",
  152. InvalidDecimal: "Invalid decimal.",
  153. InvalidDigit: ({
  154. radix
  155. }) => `Expected number in radix ${radix}.`,
  156. InvalidEscapeSequence: "Bad character escape sequence.",
  157. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  158. InvalidEscapedReservedWord: ({
  159. reservedWord
  160. }) => `Escape sequence in keyword ${reservedWord}.`,
  161. InvalidIdentifier: ({
  162. identifierName
  163. }) => `Invalid identifier ${identifierName}.`,
  164. InvalidLhs: ({
  165. ancestor
  166. }) => `Invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  167. InvalidLhsBinding: ({
  168. ancestor
  169. }) => `Binding invalid left-hand side in ${toNodeDescription(ancestor)}.`,
  170. InvalidLhsOptionalChaining: ({
  171. ancestor
  172. }) => `Invalid optional chaining in the left-hand side of ${toNodeDescription(ancestor)}.`,
  173. InvalidNumber: "Invalid number.",
  174. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  175. InvalidOrUnexpectedToken: ({
  176. unexpected
  177. }) => `Unexpected character '${unexpected}'.`,
  178. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  179. InvalidPrivateFieldResolution: ({
  180. identifierName
  181. }) => `Private name #${identifierName} is not defined.`,
  182. InvalidPropertyBindingPattern: "Binding member expression.",
  183. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  184. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  185. LabelRedeclaration: ({
  186. labelName
  187. }) => `Label '${labelName}' is already declared.`,
  188. LetInLexicalBinding: "'let' is disallowed as a lexically bound name.",
  189. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  190. MalformedRegExpFlags: "Invalid regular expression flag.",
  191. MissingClassName: "A class name is required.",
  192. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  193. MissingSemicolon: "Missing semicolon.",
  194. MissingPlugin: ({
  195. missingPlugin
  196. }) => `This experimental syntax requires enabling the parser plugin: ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  197. MissingOneOfPlugins: ({
  198. missingPlugin
  199. }) => `This experimental syntax requires enabling one of the following parser plugin(s): ${missingPlugin.map(name => JSON.stringify(name)).join(", ")}.`,
  200. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  201. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  202. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  203. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  204. ModuleAttributesWithDuplicateKeys: ({
  205. key
  206. }) => `Duplicate key "${key}" is not allowed in module attributes.`,
  207. ModuleExportNameHasLoneSurrogate: ({
  208. surrogateCharCode
  209. }) => `An export name cannot include a lone surrogate, found '\\u${surrogateCharCode.toString(16)}'.`,
  210. ModuleExportUndefined: ({
  211. localName
  212. }) => `Export '${localName}' is not defined.`,
  213. MultipleDefaultsInSwitch: "Multiple default clauses.",
  214. NewlineAfterThrow: "Illegal newline after throw.",
  215. NoCatchOrFinally: "Missing catch or finally clause.",
  216. NumberIdentifier: "Identifier directly after number.",
  217. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  218. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  219. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  220. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  221. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  222. ParamDupe: "Argument name clash.",
  223. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  224. PatternHasMethod: "Object pattern can't contain methods.",
  225. PrivateInExpectedIn: ({
  226. identifierName
  227. }) => `Private names are only allowed in property accesses (\`obj.#${identifierName}\`) or in \`in\` expressions (\`#${identifierName} in obj\`).`,
  228. PrivateNameRedeclaration: ({
  229. identifierName
  230. }) => `Duplicate private name #${identifierName}.`,
  231. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  232. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  233. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  234. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  235. RestTrailingComma: "Unexpected trailing comma after rest element.",
  236. SloppyFunction: "In non-strict mode code, functions can only be declared at top level or inside a block.",
  237. SloppyFunctionAnnexB: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  238. SourcePhaseImportRequiresDefault: 'Only `import source x from "./module"` is valid.',
  239. StaticPrototype: "Classes may not have static property named prototype.",
  240. SuperCallNotNewExpression: "Cannot use new with super(...).",
  241. 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?",
  242. SuperPrivateField: "Private fields can't be accessed on super.",
  243. TrailingDecorator: "Decorators must be attached to a class element.",
  244. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  245. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  246. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  247. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  248. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  249. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  250. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  251. UnexpectedKeyword: ({
  252. keyword
  253. }) => `Unexpected keyword '${keyword}'.`,
  254. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  255. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  256. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  257. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  258. UnexpectedPrivateField: "Unexpected private name.",
  259. UnexpectedReservedWord: ({
  260. reservedWord
  261. }) => `Unexpected reserved word '${reservedWord}'.`,
  262. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  263. UnexpectedToken: ({
  264. expected,
  265. unexpected
  266. }) => `Unexpected token${unexpected ? ` '${unexpected}'.` : ""}${expected ? `, expected "${expected}"` : ""}`,
  267. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  268. UnexpectedUsingDeclaration: "Using declaration cannot appear in the top level when source type is `script` or in the bare case statement.",
  269. UnexpectedVoidPattern: "Unexpected void binding.",
  270. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  271. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  272. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  273. UnsupportedMetaProperty: ({
  274. target,
  275. onlyValidPropertyName
  276. }) => `The only valid meta property for ${target} is ${target}.${onlyValidPropertyName}.`,
  277. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  278. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  279. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  280. UnterminatedComment: "Unterminated comment.",
  281. UnterminatedRegExp: "Unterminated regular expression.",
  282. UnterminatedString: "Unterminated string constant.",
  283. UnterminatedTemplate: "Unterminated template.",
  284. UsingDeclarationExport: "Using declaration cannot be exported.",
  285. UsingDeclarationHasBindingPattern: "Using declaration cannot have destructuring patterns.",
  286. VarRedeclaration: ({
  287. identifierName
  288. }) => `Identifier '${identifierName}' has already been declared.`,
  289. VoidPatternCatchClauseParam: "A void binding can not be the catch clause parameter. Use `try { ... } catch { ... }` if you want to discard the caught error.",
  290. VoidPatternInitializer: "A void binding may not have an initializer.",
  291. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  292. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  293. YieldNotInGeneratorFunction: "'yield' is only allowed within generator functions.",
  294. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  295. };
  296. var StrictModeErrors = {
  297. StrictDelete: "Deleting local variable in strict mode.",
  298. StrictEvalArguments: ({
  299. referenceName
  300. }) => `Assigning to '${referenceName}' in strict mode.`,
  301. StrictEvalArgumentsBinding: ({
  302. bindingName
  303. }) => `Binding '${bindingName}' in strict mode.`,
  304. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  305. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  306. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  307. StrictWith: "'with' in strict mode."
  308. };
  309. var ParseExpressionErrors = {
  310. ParseExpressionEmptyInput: "Unexpected parseExpression() input: The input is empty or contains only comments.",
  311. ParseExpressionExpectsEOF: ({
  312. unexpected
  313. }) => `Unexpected parseExpression() input: The input should contain exactly one expression, but the first expression is followed by the unexpected character \`${String.fromCodePoint(unexpected)}\`.`
  314. };
  315. const UnparenthesizedPipeBodyDescriptions = new Set(["ArrowFunctionExpression", "AssignmentExpression", "ConditionalExpression", "YieldExpression"]);
  316. var PipelineOperatorErrors = Object.assign({
  317. PipeBodyIsTighter: "Unexpected yield after pipeline body; any yield expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  318. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  319. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  320. PipeTopicUnconfiguredToken: ({
  321. token
  322. }) => `Invalid topic token ${token}. In order to use ${token} as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "${token}" }.`,
  323. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  324. PipeUnparenthesizedBody: ({
  325. type
  326. }) => `Hack-style pipe body cannot be an unparenthesized ${toNodeDescription({
  327. type
  328. })}; please wrap it in parentheses.`
  329. }, {
  330. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  331. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  332. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  333. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  334. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  335. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.'
  336. });
  337. var FunctionBindErrors = {
  338. UnsupportedBind: "Binding should be performed on object property.",
  339. UnsupportedBindRHS: "The right-hand side of binding can not be super or import."
  340. };
  341. const _excluded = ["message"];
  342. function defineHidden(obj, key, value) {
  343. Object.defineProperty(obj, key, {
  344. enumerable: false,
  345. configurable: true,
  346. value
  347. });
  348. }
  349. function toParseErrorConstructor({
  350. toMessage,
  351. code,
  352. reasonCode,
  353. syntaxPlugin
  354. }) {
  355. const hasMissingPlugin = reasonCode === "MissingPlugin" || reasonCode === "MissingOneOfPlugins";
  356. const oldReasonCodes = {
  357. AccessorCannotDeclareThisParameter: "AccesorCannotDeclareThisParameter",
  358. AccessorCannotHaveTypeParameters: "AccesorCannotHaveTypeParameters",
  359. ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference: "ConstInitiailizerMustBeStringOrNumericLiteralOrLiteralEnumReference",
  360. SetAccessorCannotHaveOptionalParameter: "SetAccesorCannotHaveOptionalParameter",
  361. SetAccessorCannotHaveRestParameter: "SetAccesorCannotHaveRestParameter",
  362. SetAccessorCannotHaveReturnType: "SetAccesorCannotHaveReturnType"
  363. };
  364. if (oldReasonCodes[reasonCode]) {
  365. reasonCode = oldReasonCodes[reasonCode];
  366. }
  367. return function constructor(loc, details) {
  368. const error = new SyntaxError();
  369. error.code = code;
  370. error.reasonCode = reasonCode;
  371. error.loc = loc;
  372. error.pos = loc.index;
  373. error.syntaxPlugin = syntaxPlugin;
  374. if (hasMissingPlugin) {
  375. error.missingPlugin = details.missingPlugin;
  376. }
  377. defineHidden(error, "clone", function clone(overrides = {}) {
  378. var _overrides$loc;
  379. const {
  380. line,
  381. column,
  382. index
  383. } = (_overrides$loc = overrides.loc) != null ? _overrides$loc : loc;
  384. return constructor(new Position(line, column, index), Object.assign({}, details, overrides.details));
  385. });
  386. defineHidden(error, "details", details);
  387. Object.defineProperty(error, "message", {
  388. configurable: true,
  389. get() {
  390. const message = `${toMessage(details)} (${loc.line}:${loc.column})`;
  391. this.message = message;
  392. return message;
  393. },
  394. set(value) {
  395. Object.defineProperty(this, "message", {
  396. value,
  397. writable: true
  398. });
  399. }
  400. });
  401. return error;
  402. };
  403. }
  404. function ParseErrorEnum(argument, syntaxPlugin) {
  405. if (Array.isArray(argument)) {
  406. return parseErrorTemplates => ParseErrorEnum(parseErrorTemplates, argument[0]);
  407. }
  408. const ParseErrorConstructors = {};
  409. for (const reasonCode of Object.keys(argument)) {
  410. const template = argument[reasonCode];
  411. const _ref = typeof template === "string" ? {
  412. message: () => template
  413. } : typeof template === "function" ? {
  414. message: template
  415. } : template,
  416. {
  417. message
  418. } = _ref,
  419. rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  420. const toMessage = typeof message === "string" ? () => message : message;
  421. ParseErrorConstructors[reasonCode] = toParseErrorConstructor(Object.assign({
  422. code: "BABEL_PARSER_SYNTAX_ERROR",
  423. reasonCode,
  424. toMessage
  425. }, syntaxPlugin ? {
  426. syntaxPlugin
  427. } : {}, rest));
  428. }
  429. return ParseErrorConstructors;
  430. }
  431. const Errors = Object.assign({}, ParseErrorEnum(ModuleErrors), ParseErrorEnum(StandardErrors), ParseErrorEnum(StrictModeErrors), ParseErrorEnum(ParseExpressionErrors), ParseErrorEnum`pipelineOperator`(PipelineOperatorErrors), ParseErrorEnum`functionBind`(FunctionBindErrors));
  432. function createDefaultOptions() {
  433. return {
  434. sourceType: "script",
  435. sourceFilename: undefined,
  436. startIndex: 0,
  437. startColumn: 0,
  438. startLine: 1,
  439. allowAwaitOutsideFunction: false,
  440. allowReturnOutsideFunction: false,
  441. allowNewTargetOutsideFunction: false,
  442. allowImportExportEverywhere: false,
  443. allowSuperOutsideMethod: false,
  444. allowUndeclaredExports: false,
  445. allowYieldOutsideFunction: false,
  446. plugins: [],
  447. strictMode: undefined,
  448. ranges: false,
  449. tokens: false,
  450. createImportExpressions: false,
  451. createParenthesizedExpressions: false,
  452. errorRecovery: false,
  453. attachComment: true,
  454. annexB: true
  455. };
  456. }
  457. function getOptions(opts) {
  458. const options = createDefaultOptions();
  459. if (opts == null) {
  460. return options;
  461. }
  462. if (opts.annexB != null && opts.annexB !== false) {
  463. throw new Error("The `annexB` option can only be set to `false`.");
  464. }
  465. for (const key of Object.keys(options)) {
  466. if (opts[key] != null) options[key] = opts[key];
  467. }
  468. if (options.startLine === 1) {
  469. if (opts.startIndex == null && options.startColumn > 0) {
  470. options.startIndex = options.startColumn;
  471. } else if (opts.startColumn == null && options.startIndex > 0) {
  472. options.startColumn = options.startIndex;
  473. }
  474. } else if (opts.startColumn == null || opts.startIndex == null) {
  475. if (opts.startIndex != null) {
  476. throw new Error("With a `startLine > 1` you must also specify `startIndex` and `startColumn`.");
  477. }
  478. }
  479. if (options.sourceType === "commonjs") {
  480. if (opts.allowAwaitOutsideFunction != null) {
  481. throw new Error("The `allowAwaitOutsideFunction` option cannot be used with `sourceType: 'commonjs'`.");
  482. }
  483. if (opts.allowReturnOutsideFunction != null) {
  484. throw new Error("`sourceType: 'commonjs'` implies `allowReturnOutsideFunction: true`, please remove the `allowReturnOutsideFunction` option or use `sourceType: 'script'`.");
  485. }
  486. if (opts.allowNewTargetOutsideFunction != null) {
  487. throw new Error("`sourceType: 'commonjs'` implies `allowNewTargetOutsideFunction: true`, please remove the `allowNewTargetOutsideFunction` option or use `sourceType: 'script'`.");
  488. }
  489. }
  490. return options;
  491. }
  492. const {
  493. defineProperty
  494. } = Object;
  495. const toUnenumerable = (object, key) => {
  496. if (object) {
  497. defineProperty(object, key, {
  498. enumerable: false,
  499. value: object[key]
  500. });
  501. }
  502. };
  503. function toESTreeLocation(node) {
  504. toUnenumerable(node.loc.start, "index");
  505. toUnenumerable(node.loc.end, "index");
  506. return node;
  507. }
  508. var estree = superClass => class ESTreeParserMixin extends superClass {
  509. parse() {
  510. const file = toESTreeLocation(super.parse());
  511. if (this.optionFlags & 256) {
  512. file.tokens = file.tokens.map(toESTreeLocation);
  513. }
  514. return file;
  515. }
  516. parseRegExpLiteral({
  517. pattern,
  518. flags
  519. }) {
  520. let regex = null;
  521. try {
  522. regex = new RegExp(pattern, flags);
  523. } catch (_) {}
  524. const node = this.estreeParseLiteral(regex);
  525. node.regex = {
  526. pattern,
  527. flags
  528. };
  529. return node;
  530. }
  531. parseBigIntLiteral(value) {
  532. let bigInt;
  533. try {
  534. bigInt = BigInt(value);
  535. } catch (_unused) {
  536. bigInt = null;
  537. }
  538. const node = this.estreeParseLiteral(bigInt);
  539. node.bigint = String(node.value || value);
  540. return node;
  541. }
  542. parseDecimalLiteral(value) {
  543. const decimal = null;
  544. const node = this.estreeParseLiteral(decimal);
  545. node.decimal = String(node.value || value);
  546. return node;
  547. }
  548. estreeParseLiteral(value) {
  549. return this.parseLiteral(value, "Literal");
  550. }
  551. parseStringLiteral(value) {
  552. return this.estreeParseLiteral(value);
  553. }
  554. parseNumericLiteral(value) {
  555. return this.estreeParseLiteral(value);
  556. }
  557. parseNullLiteral() {
  558. return this.estreeParseLiteral(null);
  559. }
  560. parseBooleanLiteral(value) {
  561. return this.estreeParseLiteral(value);
  562. }
  563. estreeParseChainExpression(node, endLoc) {
  564. const chain = this.startNodeAtNode(node);
  565. chain.expression = node;
  566. return this.finishNodeAt(chain, "ChainExpression", endLoc);
  567. }
  568. directiveToStmt(directive) {
  569. const expression = directive.value;
  570. delete directive.value;
  571. this.castNodeTo(expression, "Literal");
  572. expression.raw = expression.extra.raw;
  573. expression.value = expression.extra.expressionValue;
  574. const stmt = this.castNodeTo(directive, "ExpressionStatement");
  575. stmt.expression = expression;
  576. stmt.directive = expression.extra.rawValue;
  577. delete expression.extra;
  578. return stmt;
  579. }
  580. fillOptionalPropertiesForTSESLint(node) {}
  581. cloneEstreeStringLiteral(node) {
  582. const {
  583. start,
  584. end,
  585. loc,
  586. range,
  587. raw,
  588. value
  589. } = node;
  590. const cloned = Object.create(node.constructor.prototype);
  591. cloned.type = "Literal";
  592. cloned.start = start;
  593. cloned.end = end;
  594. cloned.loc = loc;
  595. cloned.range = range;
  596. cloned.raw = raw;
  597. cloned.value = value;
  598. return cloned;
  599. }
  600. initFunction(node, isAsync) {
  601. super.initFunction(node, isAsync);
  602. node.expression = false;
  603. }
  604. checkDeclaration(node) {
  605. if (node != null && this.isObjectProperty(node)) {
  606. this.checkDeclaration(node.value);
  607. } else {
  608. super.checkDeclaration(node);
  609. }
  610. }
  611. getObjectOrClassMethodParams(method) {
  612. return method.value.params;
  613. }
  614. isValidDirective(stmt) {
  615. var _stmt$expression$extr;
  616. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  617. }
  618. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  619. super.parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse);
  620. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  621. node.body = directiveStatements.concat(node.body);
  622. delete node.directives;
  623. }
  624. parsePrivateName() {
  625. const node = super.parsePrivateName();
  626. if (!this.getPluginOption("estree", "classFeatures")) {
  627. return node;
  628. }
  629. return this.convertPrivateNameToPrivateIdentifier(node);
  630. }
  631. convertPrivateNameToPrivateIdentifier(node) {
  632. const name = super.getPrivateNameSV(node);
  633. delete node.id;
  634. node.name = name;
  635. return this.castNodeTo(node, "PrivateIdentifier");
  636. }
  637. isPrivateName(node) {
  638. if (!this.getPluginOption("estree", "classFeatures")) {
  639. return super.isPrivateName(node);
  640. }
  641. return node.type === "PrivateIdentifier";
  642. }
  643. getPrivateNameSV(node) {
  644. if (!this.getPluginOption("estree", "classFeatures")) {
  645. return super.getPrivateNameSV(node);
  646. }
  647. return node.name;
  648. }
  649. parseLiteral(value, type) {
  650. const node = super.parseLiteral(value, type);
  651. node.raw = node.extra.raw;
  652. delete node.extra;
  653. return node;
  654. }
  655. parseFunctionBody(node, allowExpression, isMethod = false) {
  656. super.parseFunctionBody(node, allowExpression, isMethod);
  657. node.expression = node.body.type !== "BlockStatement";
  658. }
  659. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  660. let funcNode = this.startNode();
  661. funcNode.kind = node.kind;
  662. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  663. delete funcNode.kind;
  664. const {
  665. typeParameters
  666. } = node;
  667. if (typeParameters) {
  668. delete node.typeParameters;
  669. funcNode.typeParameters = typeParameters;
  670. this.resetStartLocationFromNode(funcNode, typeParameters);
  671. }
  672. const valueNode = this.castNodeTo(funcNode, "FunctionExpression");
  673. node.value = valueNode;
  674. if (type === "ClassPrivateMethod") {
  675. node.computed = false;
  676. }
  677. if (type === "ObjectMethod") {
  678. if (node.kind === "method") {
  679. node.kind = "init";
  680. }
  681. node.shorthand = false;
  682. return this.finishNode(node, "Property");
  683. } else {
  684. return this.finishNode(node, "MethodDefinition");
  685. }
  686. }
  687. nameIsConstructor(key) {
  688. if (key.type === "Literal") return key.value === "constructor";
  689. return super.nameIsConstructor(key);
  690. }
  691. parseClassProperty(...args) {
  692. const propertyNode = super.parseClassProperty(...args);
  693. if (!this.getPluginOption("estree", "classFeatures")) {
  694. return propertyNode;
  695. }
  696. this.castNodeTo(propertyNode, "PropertyDefinition");
  697. return propertyNode;
  698. }
  699. parseClassPrivateProperty(...args) {
  700. const propertyNode = super.parseClassPrivateProperty(...args);
  701. if (!this.getPluginOption("estree", "classFeatures")) {
  702. return propertyNode;
  703. }
  704. this.castNodeTo(propertyNode, "PropertyDefinition");
  705. propertyNode.computed = false;
  706. return propertyNode;
  707. }
  708. parseClassAccessorProperty(node) {
  709. const accessorPropertyNode = super.parseClassAccessorProperty(node);
  710. if (!this.getPluginOption("estree", "classFeatures")) {
  711. return accessorPropertyNode;
  712. }
  713. if (accessorPropertyNode.abstract && this.hasPlugin("typescript")) {
  714. delete accessorPropertyNode.abstract;
  715. this.castNodeTo(accessorPropertyNode, "TSAbstractAccessorProperty");
  716. } else {
  717. this.castNodeTo(accessorPropertyNode, "AccessorProperty");
  718. }
  719. return accessorPropertyNode;
  720. }
  721. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  722. const node = super.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  723. if (node) {
  724. node.kind = "init";
  725. this.castNodeTo(node, "Property");
  726. }
  727. return node;
  728. }
  729. finishObjectProperty(node) {
  730. node.kind = "init";
  731. return this.finishNode(node, "Property");
  732. }
  733. isValidLVal(type, disallowCallExpression, isUnparenthesizedInAssign, binding) {
  734. return type === "Property" ? "value" : super.isValidLVal(type, disallowCallExpression, isUnparenthesizedInAssign, binding);
  735. }
  736. isAssignable(node, isBinding) {
  737. if (node != null && this.isObjectProperty(node)) {
  738. return this.isAssignable(node.value, isBinding);
  739. }
  740. return super.isAssignable(node, isBinding);
  741. }
  742. toAssignable(node, isLHS = false) {
  743. if (node != null && this.isObjectProperty(node)) {
  744. const {
  745. key,
  746. value
  747. } = node;
  748. if (this.isPrivateName(key)) {
  749. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  750. }
  751. this.toAssignable(value, isLHS);
  752. } else {
  753. super.toAssignable(node, isLHS);
  754. }
  755. }
  756. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  757. if (prop.type === "Property" && (prop.kind === "get" || prop.kind === "set")) {
  758. this.raise(Errors.PatternHasAccessor, prop.key);
  759. } else if (prop.type === "Property" && prop.method) {
  760. this.raise(Errors.PatternHasMethod, prop.key);
  761. } else {
  762. super.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  763. }
  764. }
  765. finishCallExpression(unfinished, optional) {
  766. const node = super.finishCallExpression(unfinished, optional);
  767. if (node.callee.type === "Import") {
  768. var _ref, _ref2;
  769. this.castNodeTo(node, "ImportExpression");
  770. node.source = node.arguments[0];
  771. node.options = (_ref = node.arguments[1]) != null ? _ref : null;
  772. node.attributes = (_ref2 = node.arguments[1]) != null ? _ref2 : null;
  773. delete node.arguments;
  774. delete node.callee;
  775. } else if (node.type === "OptionalCallExpression") {
  776. this.castNodeTo(node, "CallExpression");
  777. } else {
  778. node.optional = false;
  779. }
  780. return node;
  781. }
  782. toReferencedArguments(node) {
  783. if (node.type === "ImportExpression") {
  784. return;
  785. }
  786. super.toReferencedArguments(node);
  787. }
  788. parseExport(unfinished, decorators) {
  789. const exportStartLoc = this.state.lastTokStartLoc;
  790. const node = super.parseExport(unfinished, decorators);
  791. switch (node.type) {
  792. case "ExportAllDeclaration":
  793. node.exported = null;
  794. break;
  795. case "ExportNamedDeclaration":
  796. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  797. this.castNodeTo(node, "ExportAllDeclaration");
  798. node.exported = node.specifiers[0].exported;
  799. delete node.specifiers;
  800. }
  801. case "ExportDefaultDeclaration":
  802. {
  803. var _declaration$decorato;
  804. const {
  805. declaration
  806. } = node;
  807. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0 && declaration.start === node.start) {
  808. this.resetStartLocation(node, exportStartLoc);
  809. }
  810. }
  811. break;
  812. }
  813. return node;
  814. }
  815. stopParseSubscript(base, state) {
  816. const node = super.stopParseSubscript(base, state);
  817. if (state.optionalChainMember) {
  818. return this.estreeParseChainExpression(node, base.loc.end);
  819. }
  820. return node;
  821. }
  822. parseMember(base, startLoc, state, computed, optional) {
  823. const node = super.parseMember(base, startLoc, state, computed, optional);
  824. if (node.type === "OptionalMemberExpression") {
  825. this.castNodeTo(node, "MemberExpression");
  826. } else {
  827. node.optional = false;
  828. }
  829. return node;
  830. }
  831. isOptionalMemberExpression(node) {
  832. if (node.type === "ChainExpression") {
  833. return node.expression.type === "MemberExpression";
  834. }
  835. return super.isOptionalMemberExpression(node);
  836. }
  837. hasPropertyAsPrivateName(node) {
  838. if (node.type === "ChainExpression") {
  839. node = node.expression;
  840. }
  841. return super.hasPropertyAsPrivateName(node);
  842. }
  843. isObjectProperty(node) {
  844. return node.type === "Property" && node.kind === "init" && !node.method;
  845. }
  846. isObjectMethod(node) {
  847. return node.type === "Property" && (node.method || node.kind === "get" || node.kind === "set");
  848. }
  849. castNodeTo(node, type) {
  850. const result = super.castNodeTo(node, type);
  851. this.fillOptionalPropertiesForTSESLint(result);
  852. return result;
  853. }
  854. cloneIdentifier(node) {
  855. const cloned = super.cloneIdentifier(node);
  856. this.fillOptionalPropertiesForTSESLint(cloned);
  857. return cloned;
  858. }
  859. cloneStringLiteral(node) {
  860. if (node.type === "Literal") {
  861. return this.cloneEstreeStringLiteral(node);
  862. }
  863. return super.cloneStringLiteral(node);
  864. }
  865. finishNodeAt(node, type, endLoc) {
  866. return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
  867. }
  868. finishNode(node, type) {
  869. const result = super.finishNode(node, type);
  870. this.fillOptionalPropertiesForTSESLint(result);
  871. return result;
  872. }
  873. resetStartLocation(node, startLoc) {
  874. super.resetStartLocation(node, startLoc);
  875. toESTreeLocation(node);
  876. }
  877. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  878. super.resetEndLocation(node, endLoc);
  879. toESTreeLocation(node);
  880. }
  881. };
  882. class TokContext {
  883. constructor(token, preserveSpace) {
  884. this.token = void 0;
  885. this.preserveSpace = void 0;
  886. this.token = token;
  887. this.preserveSpace = !!preserveSpace;
  888. }
  889. }
  890. const types = {
  891. brace: new TokContext("{"),
  892. j_oTag: new TokContext("<tag"),
  893. j_cTag: new TokContext("</tag"),
  894. j_expr: new TokContext("<tag>...</tag>", true)
  895. };
  896. types.template = new TokContext("`", true);
  897. const beforeExpr = true;
  898. const startsExpr = true;
  899. const isLoop = true;
  900. const isAssign = true;
  901. const prefix = true;
  902. const postfix = true;
  903. class ExportedTokenType {
  904. constructor(label, conf = {}) {
  905. this.label = void 0;
  906. this.keyword = void 0;
  907. this.beforeExpr = void 0;
  908. this.startsExpr = void 0;
  909. this.rightAssociative = void 0;
  910. this.isLoop = void 0;
  911. this.isAssign = void 0;
  912. this.prefix = void 0;
  913. this.postfix = void 0;
  914. this.binop = void 0;
  915. this.label = label;
  916. this.keyword = conf.keyword;
  917. this.beforeExpr = !!conf.beforeExpr;
  918. this.startsExpr = !!conf.startsExpr;
  919. this.rightAssociative = !!conf.rightAssociative;
  920. this.isLoop = !!conf.isLoop;
  921. this.isAssign = !!conf.isAssign;
  922. this.prefix = !!conf.prefix;
  923. this.postfix = !!conf.postfix;
  924. this.binop = conf.binop != null ? conf.binop : null;
  925. this.updateContext = null;
  926. }
  927. }
  928. const keywords$1 = new Map();
  929. function createKeyword(name, options = {}) {
  930. options.keyword = name;
  931. const token = createToken(name, options);
  932. keywords$1.set(name, token);
  933. return token;
  934. }
  935. function createBinop(name, binop) {
  936. return createToken(name, {
  937. beforeExpr,
  938. binop
  939. });
  940. }
  941. let tokenTypeCounter = -1;
  942. const tokenTypes = [];
  943. const tokenLabels = [];
  944. const tokenBinops = [];
  945. const tokenBeforeExprs = [];
  946. const tokenStartsExprs = [];
  947. const tokenPrefixes = [];
  948. function createToken(name, options = {}) {
  949. var _options$binop, _options$beforeExpr, _options$startsExpr, _options$prefix;
  950. ++tokenTypeCounter;
  951. tokenLabels.push(name);
  952. tokenBinops.push((_options$binop = options.binop) != null ? _options$binop : -1);
  953. tokenBeforeExprs.push((_options$beforeExpr = options.beforeExpr) != null ? _options$beforeExpr : false);
  954. tokenStartsExprs.push((_options$startsExpr = options.startsExpr) != null ? _options$startsExpr : false);
  955. tokenPrefixes.push((_options$prefix = options.prefix) != null ? _options$prefix : false);
  956. tokenTypes.push(new ExportedTokenType(name, options));
  957. return tokenTypeCounter;
  958. }
  959. function createKeywordLike(name, options = {}) {
  960. var _options$binop2, _options$beforeExpr2, _options$startsExpr2, _options$prefix2;
  961. ++tokenTypeCounter;
  962. keywords$1.set(name, tokenTypeCounter);
  963. tokenLabels.push(name);
  964. tokenBinops.push((_options$binop2 = options.binop) != null ? _options$binop2 : -1);
  965. tokenBeforeExprs.push((_options$beforeExpr2 = options.beforeExpr) != null ? _options$beforeExpr2 : false);
  966. tokenStartsExprs.push((_options$startsExpr2 = options.startsExpr) != null ? _options$startsExpr2 : false);
  967. tokenPrefixes.push((_options$prefix2 = options.prefix) != null ? _options$prefix2 : false);
  968. tokenTypes.push(new ExportedTokenType("name", options));
  969. return tokenTypeCounter;
  970. }
  971. const tt = {
  972. bracketL: createToken("[", {
  973. beforeExpr,
  974. startsExpr
  975. }),
  976. bracketHashL: createToken("#[", {
  977. beforeExpr,
  978. startsExpr
  979. }),
  980. bracketBarL: createToken("[|", {
  981. beforeExpr,
  982. startsExpr
  983. }),
  984. bracketR: createToken("]"),
  985. bracketBarR: createToken("|]"),
  986. braceL: createToken("{", {
  987. beforeExpr,
  988. startsExpr
  989. }),
  990. braceBarL: createToken("{|", {
  991. beforeExpr,
  992. startsExpr
  993. }),
  994. braceHashL: createToken("#{", {
  995. beforeExpr,
  996. startsExpr
  997. }),
  998. braceR: createToken("}"),
  999. braceBarR: createToken("|}"),
  1000. parenL: createToken("(", {
  1001. beforeExpr,
  1002. startsExpr
  1003. }),
  1004. parenR: createToken(")"),
  1005. comma: createToken(",", {
  1006. beforeExpr
  1007. }),
  1008. semi: createToken(";", {
  1009. beforeExpr
  1010. }),
  1011. colon: createToken(":", {
  1012. beforeExpr
  1013. }),
  1014. doubleColon: createToken("::", {
  1015. beforeExpr
  1016. }),
  1017. dot: createToken("."),
  1018. question: createToken("?", {
  1019. beforeExpr
  1020. }),
  1021. questionDot: createToken("?."),
  1022. arrow: createToken("=>", {
  1023. beforeExpr
  1024. }),
  1025. template: createToken("template"),
  1026. ellipsis: createToken("...", {
  1027. beforeExpr
  1028. }),
  1029. backQuote: createToken("`", {
  1030. startsExpr
  1031. }),
  1032. dollarBraceL: createToken("${", {
  1033. beforeExpr,
  1034. startsExpr
  1035. }),
  1036. templateTail: createToken("...`", {
  1037. startsExpr
  1038. }),
  1039. templateNonTail: createToken("...${", {
  1040. beforeExpr,
  1041. startsExpr
  1042. }),
  1043. at: createToken("@"),
  1044. hash: createToken("#", {
  1045. startsExpr
  1046. }),
  1047. interpreterDirective: createToken("#!..."),
  1048. eq: createToken("=", {
  1049. beforeExpr,
  1050. isAssign
  1051. }),
  1052. assign: createToken("_=", {
  1053. beforeExpr,
  1054. isAssign
  1055. }),
  1056. slashAssign: createToken("_=", {
  1057. beforeExpr,
  1058. isAssign
  1059. }),
  1060. xorAssign: createToken("_=", {
  1061. beforeExpr,
  1062. isAssign
  1063. }),
  1064. moduloAssign: createToken("_=", {
  1065. beforeExpr,
  1066. isAssign
  1067. }),
  1068. incDec: createToken("++/--", {
  1069. prefix,
  1070. postfix,
  1071. startsExpr
  1072. }),
  1073. bang: createToken("!", {
  1074. beforeExpr,
  1075. prefix,
  1076. startsExpr
  1077. }),
  1078. tilde: createToken("~", {
  1079. beforeExpr,
  1080. prefix,
  1081. startsExpr
  1082. }),
  1083. doubleCaret: createToken("^^", {
  1084. startsExpr
  1085. }),
  1086. doubleAt: createToken("@@", {
  1087. startsExpr
  1088. }),
  1089. pipeline: createBinop("|>", 0),
  1090. nullishCoalescing: createBinop("??", 1),
  1091. logicalOR: createBinop("||", 1),
  1092. logicalAND: createBinop("&&", 2),
  1093. bitwiseOR: createBinop("|", 3),
  1094. bitwiseXOR: createBinop("^", 4),
  1095. bitwiseAND: createBinop("&", 5),
  1096. equality: createBinop("==/!=/===/!==", 6),
  1097. lt: createBinop("</>/<=/>=", 7),
  1098. gt: createBinop("</>/<=/>=", 7),
  1099. relational: createBinop("</>/<=/>=", 7),
  1100. bitShift: createBinop("<</>>/>>>", 8),
  1101. bitShiftL: createBinop("<</>>/>>>", 8),
  1102. bitShiftR: createBinop("<</>>/>>>", 8),
  1103. plusMin: createToken("+/-", {
  1104. beforeExpr,
  1105. binop: 9,
  1106. prefix,
  1107. startsExpr
  1108. }),
  1109. modulo: createToken("%", {
  1110. binop: 10,
  1111. startsExpr
  1112. }),
  1113. star: createToken("*", {
  1114. binop: 10
  1115. }),
  1116. slash: createBinop("/", 10),
  1117. exponent: createToken("**", {
  1118. beforeExpr,
  1119. binop: 11,
  1120. rightAssociative: true
  1121. }),
  1122. _in: createKeyword("in", {
  1123. beforeExpr,
  1124. binop: 7
  1125. }),
  1126. _instanceof: createKeyword("instanceof", {
  1127. beforeExpr,
  1128. binop: 7
  1129. }),
  1130. _break: createKeyword("break"),
  1131. _case: createKeyword("case", {
  1132. beforeExpr
  1133. }),
  1134. _catch: createKeyword("catch"),
  1135. _continue: createKeyword("continue"),
  1136. _debugger: createKeyword("debugger"),
  1137. _default: createKeyword("default", {
  1138. beforeExpr
  1139. }),
  1140. _else: createKeyword("else", {
  1141. beforeExpr
  1142. }),
  1143. _finally: createKeyword("finally"),
  1144. _function: createKeyword("function", {
  1145. startsExpr
  1146. }),
  1147. _if: createKeyword("if"),
  1148. _return: createKeyword("return", {
  1149. beforeExpr
  1150. }),
  1151. _switch: createKeyword("switch"),
  1152. _throw: createKeyword("throw", {
  1153. beforeExpr,
  1154. prefix,
  1155. startsExpr
  1156. }),
  1157. _try: createKeyword("try"),
  1158. _var: createKeyword("var"),
  1159. _const: createKeyword("const"),
  1160. _with: createKeyword("with"),
  1161. _new: createKeyword("new", {
  1162. beforeExpr,
  1163. startsExpr
  1164. }),
  1165. _this: createKeyword("this", {
  1166. startsExpr
  1167. }),
  1168. _super: createKeyword("super", {
  1169. startsExpr
  1170. }),
  1171. _class: createKeyword("class", {
  1172. startsExpr
  1173. }),
  1174. _extends: createKeyword("extends", {
  1175. beforeExpr
  1176. }),
  1177. _export: createKeyword("export"),
  1178. _import: createKeyword("import", {
  1179. startsExpr
  1180. }),
  1181. _null: createKeyword("null", {
  1182. startsExpr
  1183. }),
  1184. _true: createKeyword("true", {
  1185. startsExpr
  1186. }),
  1187. _false: createKeyword("false", {
  1188. startsExpr
  1189. }),
  1190. _typeof: createKeyword("typeof", {
  1191. beforeExpr,
  1192. prefix,
  1193. startsExpr
  1194. }),
  1195. _void: createKeyword("void", {
  1196. beforeExpr,
  1197. prefix,
  1198. startsExpr
  1199. }),
  1200. _delete: createKeyword("delete", {
  1201. beforeExpr,
  1202. prefix,
  1203. startsExpr
  1204. }),
  1205. _do: createKeyword("do", {
  1206. isLoop,
  1207. beforeExpr
  1208. }),
  1209. _for: createKeyword("for", {
  1210. isLoop
  1211. }),
  1212. _while: createKeyword("while", {
  1213. isLoop
  1214. }),
  1215. _as: createKeywordLike("as", {
  1216. startsExpr
  1217. }),
  1218. _assert: createKeywordLike("assert", {
  1219. startsExpr
  1220. }),
  1221. _async: createKeywordLike("async", {
  1222. startsExpr
  1223. }),
  1224. _await: createKeywordLike("await", {
  1225. startsExpr
  1226. }),
  1227. _defer: createKeywordLike("defer", {
  1228. startsExpr
  1229. }),
  1230. _from: createKeywordLike("from", {
  1231. startsExpr
  1232. }),
  1233. _get: createKeywordLike("get", {
  1234. startsExpr
  1235. }),
  1236. _let: createKeywordLike("let", {
  1237. startsExpr
  1238. }),
  1239. _meta: createKeywordLike("meta", {
  1240. startsExpr
  1241. }),
  1242. _of: createKeywordLike("of", {
  1243. startsExpr
  1244. }),
  1245. _sent: createKeywordLike("sent", {
  1246. startsExpr
  1247. }),
  1248. _set: createKeywordLike("set", {
  1249. startsExpr
  1250. }),
  1251. _source: createKeywordLike("source", {
  1252. startsExpr
  1253. }),
  1254. _static: createKeywordLike("static", {
  1255. startsExpr
  1256. }),
  1257. _using: createKeywordLike("using", {
  1258. startsExpr
  1259. }),
  1260. _yield: createKeywordLike("yield", {
  1261. startsExpr
  1262. }),
  1263. _asserts: createKeywordLike("asserts", {
  1264. startsExpr
  1265. }),
  1266. _checks: createKeywordLike("checks", {
  1267. startsExpr
  1268. }),
  1269. _exports: createKeywordLike("exports", {
  1270. startsExpr
  1271. }),
  1272. _global: createKeywordLike("global", {
  1273. startsExpr
  1274. }),
  1275. _implements: createKeywordLike("implements", {
  1276. startsExpr
  1277. }),
  1278. _intrinsic: createKeywordLike("intrinsic", {
  1279. startsExpr
  1280. }),
  1281. _infer: createKeywordLike("infer", {
  1282. startsExpr
  1283. }),
  1284. _is: createKeywordLike("is", {
  1285. startsExpr
  1286. }),
  1287. _mixins: createKeywordLike("mixins", {
  1288. startsExpr
  1289. }),
  1290. _proto: createKeywordLike("proto", {
  1291. startsExpr
  1292. }),
  1293. _require: createKeywordLike("require", {
  1294. startsExpr
  1295. }),
  1296. _satisfies: createKeywordLike("satisfies", {
  1297. startsExpr
  1298. }),
  1299. _keyof: createKeywordLike("keyof", {
  1300. startsExpr
  1301. }),
  1302. _readonly: createKeywordLike("readonly", {
  1303. startsExpr
  1304. }),
  1305. _unique: createKeywordLike("unique", {
  1306. startsExpr
  1307. }),
  1308. _abstract: createKeywordLike("abstract", {
  1309. startsExpr
  1310. }),
  1311. _declare: createKeywordLike("declare", {
  1312. startsExpr
  1313. }),
  1314. _enum: createKeywordLike("enum", {
  1315. startsExpr
  1316. }),
  1317. _module: createKeywordLike("module", {
  1318. startsExpr
  1319. }),
  1320. _namespace: createKeywordLike("namespace", {
  1321. startsExpr
  1322. }),
  1323. _interface: createKeywordLike("interface", {
  1324. startsExpr
  1325. }),
  1326. _type: createKeywordLike("type", {
  1327. startsExpr
  1328. }),
  1329. _opaque: createKeywordLike("opaque", {
  1330. startsExpr
  1331. }),
  1332. name: createToken("name", {
  1333. startsExpr
  1334. }),
  1335. placeholder: createToken("%%", {
  1336. startsExpr
  1337. }),
  1338. string: createToken("string", {
  1339. startsExpr
  1340. }),
  1341. num: createToken("num", {
  1342. startsExpr
  1343. }),
  1344. bigint: createToken("bigint", {
  1345. startsExpr
  1346. }),
  1347. decimal: createToken("decimal", {
  1348. startsExpr
  1349. }),
  1350. regexp: createToken("regexp", {
  1351. startsExpr
  1352. }),
  1353. privateName: createToken("#name", {
  1354. startsExpr
  1355. }),
  1356. eof: createToken("eof"),
  1357. jsxName: createToken("jsxName"),
  1358. jsxText: createToken("jsxText", {
  1359. beforeExpr
  1360. }),
  1361. jsxTagStart: createToken("jsxTagStart", {
  1362. startsExpr
  1363. }),
  1364. jsxTagEnd: createToken("jsxTagEnd")
  1365. };
  1366. function tokenIsIdentifier(token) {
  1367. return token >= 93 && token <= 133;
  1368. }
  1369. function tokenKeywordOrIdentifierIsKeyword(token) {
  1370. return token <= 92;
  1371. }
  1372. function tokenIsKeywordOrIdentifier(token) {
  1373. return token >= 58 && token <= 133;
  1374. }
  1375. function tokenIsLiteralPropertyName(token) {
  1376. return token >= 58 && token <= 137;
  1377. }
  1378. function tokenComesBeforeExpression(token) {
  1379. return tokenBeforeExprs[token];
  1380. }
  1381. function tokenCanStartExpression(token) {
  1382. return tokenStartsExprs[token];
  1383. }
  1384. function tokenIsAssignment(token) {
  1385. return token >= 29 && token <= 33;
  1386. }
  1387. function tokenIsFlowInterfaceOrTypeOrOpaque(token) {
  1388. return token >= 129 && token <= 131;
  1389. }
  1390. function tokenIsLoop(token) {
  1391. return token >= 90 && token <= 92;
  1392. }
  1393. function tokenIsKeyword(token) {
  1394. return token >= 58 && token <= 92;
  1395. }
  1396. function tokenIsOperator(token) {
  1397. return token >= 39 && token <= 59;
  1398. }
  1399. function tokenIsPostfix(token) {
  1400. return token === 34;
  1401. }
  1402. function tokenIsPrefix(token) {
  1403. return tokenPrefixes[token];
  1404. }
  1405. function tokenIsTSTypeOperator(token) {
  1406. return token >= 121 && token <= 123;
  1407. }
  1408. function tokenIsTSDeclarationStart(token) {
  1409. return token >= 124 && token <= 130;
  1410. }
  1411. function tokenLabelName(token) {
  1412. return tokenLabels[token];
  1413. }
  1414. function tokenOperatorPrecedence(token) {
  1415. return tokenBinops[token];
  1416. }
  1417. function tokenIsRightAssociative(token) {
  1418. return token === 57;
  1419. }
  1420. function tokenIsTemplate(token) {
  1421. return token >= 24 && token <= 25;
  1422. }
  1423. function getExportedToken(token) {
  1424. return tokenTypes[token];
  1425. }
  1426. tokenTypes[8].updateContext = context => {
  1427. context.pop();
  1428. };
  1429. tokenTypes[5].updateContext = tokenTypes[7].updateContext = tokenTypes[23].updateContext = context => {
  1430. context.push(types.brace);
  1431. };
  1432. tokenTypes[22].updateContext = context => {
  1433. if (context[context.length - 1] === types.template) {
  1434. context.pop();
  1435. } else {
  1436. context.push(types.template);
  1437. }
  1438. };
  1439. tokenTypes[143].updateContext = context => {
  1440. context.push(types.j_expr, types.j_oTag);
  1441. };
  1442. 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-\u088f\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\u0c5c\u0c5d\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cdc-\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-\u1c8a\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-\ua7dc\ua7f1-\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";
  1443. let nonASCIIidentifierChars = "\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\u0897-\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-\u1add\u1ae0-\u1aeb\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\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\u30fb\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\uff65";
  1444. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1445. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1446. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1447. 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, 4, 51, 13, 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, 7, 25, 39, 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, 39, 27, 10, 22, 251, 41, 7, 1, 17, 5, 57, 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, 31, 9, 2, 0, 3, 0, 2, 37, 2, 0, 26, 0, 2, 0, 45, 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, 200, 32, 32, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 24, 43, 261, 18, 16, 0, 2, 12, 2, 33, 125, 0, 80, 921, 103, 110, 18, 195, 2637, 96, 16, 1071, 18, 5, 26, 3994, 6, 582, 6842, 29, 1763, 568, 8, 30, 18, 78, 18, 29, 19, 47, 17, 3, 32, 20, 6, 18, 433, 44, 212, 63, 33, 24, 3, 24, 45, 74, 6, 0, 67, 12, 65, 1, 2, 0, 15, 4, 10, 7381, 42, 31, 98, 114, 8702, 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, 229, 29, 3, 0, 208, 30, 2, 2, 2, 1, 2, 6, 3, 4, 10, 1, 225, 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, 4381, 3, 5773, 3, 7472, 16, 621, 2467, 541, 1507, 4938, 6, 8489];
  1448. 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, 7, 9, 32, 4, 318, 1, 78, 5, 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, 68, 8, 2, 0, 3, 0, 2, 3, 2, 4, 2, 0, 15, 1, 83, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 7, 19, 58, 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, 199, 7, 137, 9, 54, 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, 55, 9, 266, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 10, 5350, 0, 7, 14, 11465, 27, 2343, 9, 87, 9, 39, 4, 60, 6, 26, 9, 535, 9, 470, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4178, 9, 519, 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, 245, 1, 2, 9, 233, 0, 3, 0, 8, 1, 6, 0, 475, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1449. function isInAstralSet(code, set) {
  1450. let pos = 0x10000;
  1451. for (let i = 0, length = set.length; i < length; i += 2) {
  1452. pos += set[i];
  1453. if (pos > code) return false;
  1454. pos += set[i + 1];
  1455. if (pos >= code) return true;
  1456. }
  1457. return false;
  1458. }
  1459. function isIdentifierStart(code) {
  1460. if (code < 65) return code === 36;
  1461. if (code <= 90) return true;
  1462. if (code < 97) return code === 95;
  1463. if (code <= 122) return true;
  1464. if (code <= 0xffff) {
  1465. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1466. }
  1467. return isInAstralSet(code, astralIdentifierStartCodes);
  1468. }
  1469. function isIdentifierChar(code) {
  1470. if (code < 48) return code === 36;
  1471. if (code < 58) return true;
  1472. if (code < 65) return false;
  1473. if (code <= 90) return true;
  1474. if (code < 97) return code === 95;
  1475. if (code <= 122) return true;
  1476. if (code <= 0xffff) {
  1477. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1478. }
  1479. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1480. }
  1481. const reservedWords = {
  1482. 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"],
  1483. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1484. strictBind: ["eval", "arguments"]
  1485. };
  1486. const keywords = new Set(reservedWords.keyword);
  1487. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1488. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1489. function isReservedWord(word, inModule) {
  1490. return inModule && word === "await" || word === "enum";
  1491. }
  1492. function isStrictReservedWord(word, inModule) {
  1493. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1494. }
  1495. function isStrictBindOnlyReservedWord(word) {
  1496. return reservedWordsStrictBindSet.has(word);
  1497. }
  1498. function isStrictBindReservedWord(word, inModule) {
  1499. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1500. }
  1501. function isKeyword(word) {
  1502. return keywords.has(word);
  1503. }
  1504. function isIteratorStart(current, next, next2) {
  1505. return current === 64 && next === 64 && isIdentifierStart(next2);
  1506. }
  1507. 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"]);
  1508. function canBeReservedWord(word) {
  1509. return reservedWordLikeSet.has(word);
  1510. }
  1511. class Scope {
  1512. constructor(flags) {
  1513. this.flags = 0;
  1514. this.names = new Map();
  1515. this.firstLexicalName = "";
  1516. this.flags = flags;
  1517. }
  1518. }
  1519. class ScopeHandler {
  1520. constructor(parser, inModule) {
  1521. this.parser = void 0;
  1522. this.scopeStack = [];
  1523. this.inModule = void 0;
  1524. this.undefinedExports = new Map();
  1525. this.parser = parser;
  1526. this.inModule = inModule;
  1527. }
  1528. get inTopLevel() {
  1529. return (this.currentScope().flags & 1) > 0;
  1530. }
  1531. get inFunction() {
  1532. return (this.currentVarScopeFlags() & 2) > 0;
  1533. }
  1534. get allowSuper() {
  1535. return (this.currentThisScopeFlags() & 16) > 0;
  1536. }
  1537. get allowDirectSuper() {
  1538. return (this.currentThisScopeFlags() & 32) > 0;
  1539. }
  1540. get allowNewTarget() {
  1541. return (this.currentThisScopeFlags() & 512) > 0;
  1542. }
  1543. get inClass() {
  1544. return (this.currentThisScopeFlags() & 64) > 0;
  1545. }
  1546. get inClassAndNotInNonArrowFunction() {
  1547. const flags = this.currentThisScopeFlags();
  1548. return (flags & 64) > 0 && (flags & 2) === 0;
  1549. }
  1550. get inStaticBlock() {
  1551. for (let i = this.scopeStack.length - 1;; i--) {
  1552. const {
  1553. flags
  1554. } = this.scopeStack[i];
  1555. if (flags & 128) {
  1556. return true;
  1557. }
  1558. if (flags & (1667 | 64)) {
  1559. return false;
  1560. }
  1561. }
  1562. }
  1563. get inNonArrowFunction() {
  1564. return (this.currentThisScopeFlags() & 2) > 0;
  1565. }
  1566. get inBareCaseStatement() {
  1567. return (this.currentScope().flags & 256) > 0;
  1568. }
  1569. get treatFunctionsAsVar() {
  1570. return this.treatFunctionsAsVarInScope(this.currentScope());
  1571. }
  1572. createScope(flags) {
  1573. return new Scope(flags);
  1574. }
  1575. enter(flags) {
  1576. this.scopeStack.push(this.createScope(flags));
  1577. }
  1578. exit() {
  1579. const scope = this.scopeStack.pop();
  1580. return scope.flags;
  1581. }
  1582. treatFunctionsAsVarInScope(scope) {
  1583. return !!(scope.flags & (2 | 128) || !this.parser.inModule && scope.flags & 1);
  1584. }
  1585. declareName(name, bindingType, loc) {
  1586. let scope = this.currentScope();
  1587. if (bindingType & 8 || bindingType & 16) {
  1588. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1589. let type = scope.names.get(name) || 0;
  1590. if (bindingType & 16) {
  1591. type = type | 4;
  1592. } else {
  1593. if (!scope.firstLexicalName) {
  1594. scope.firstLexicalName = name;
  1595. }
  1596. type = type | 2;
  1597. }
  1598. scope.names.set(name, type);
  1599. if (bindingType & 8) {
  1600. this.maybeExportDefined(scope, name);
  1601. }
  1602. } else if (bindingType & 4) {
  1603. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1604. scope = this.scopeStack[i];
  1605. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1606. scope.names.set(name, (scope.names.get(name) || 0) | 1);
  1607. this.maybeExportDefined(scope, name);
  1608. if (scope.flags & 1667) break;
  1609. }
  1610. }
  1611. if (this.parser.inModule && scope.flags & 1) {
  1612. this.undefinedExports.delete(name);
  1613. }
  1614. }
  1615. maybeExportDefined(scope, name) {
  1616. if (this.parser.inModule && scope.flags & 1) {
  1617. this.undefinedExports.delete(name);
  1618. }
  1619. }
  1620. checkRedeclarationInScope(scope, name, bindingType, loc) {
  1621. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1622. this.parser.raise(Errors.VarRedeclaration, loc, {
  1623. identifierName: name
  1624. });
  1625. }
  1626. }
  1627. isRedeclaredInScope(scope, name, bindingType) {
  1628. if (!(bindingType & 1)) return false;
  1629. if (bindingType & 8) {
  1630. return scope.names.has(name);
  1631. }
  1632. const type = scope.names.get(name) || 0;
  1633. if (bindingType & 16) {
  1634. return (type & 2) > 0 || !this.treatFunctionsAsVarInScope(scope) && (type & 1) > 0;
  1635. }
  1636. return (type & 2) > 0 && !(scope.flags & 8 && scope.firstLexicalName === name) || !this.treatFunctionsAsVarInScope(scope) && (type & 4) > 0;
  1637. }
  1638. checkLocalExport(id) {
  1639. const {
  1640. name
  1641. } = id;
  1642. const topLevelScope = this.scopeStack[0];
  1643. if (!topLevelScope.names.has(name)) {
  1644. this.undefinedExports.set(name, id.loc.start);
  1645. }
  1646. }
  1647. currentScope() {
  1648. return this.scopeStack[this.scopeStack.length - 1];
  1649. }
  1650. currentVarScopeFlags() {
  1651. for (let i = this.scopeStack.length - 1;; i--) {
  1652. const {
  1653. flags
  1654. } = this.scopeStack[i];
  1655. if (flags & 1667) {
  1656. return flags;
  1657. }
  1658. }
  1659. }
  1660. currentThisScopeFlags() {
  1661. for (let i = this.scopeStack.length - 1;; i--) {
  1662. const {
  1663. flags
  1664. } = this.scopeStack[i];
  1665. if (flags & (1667 | 64) && !(flags & 4)) {
  1666. return flags;
  1667. }
  1668. }
  1669. }
  1670. }
  1671. class FlowScope extends Scope {
  1672. constructor(...args) {
  1673. super(...args);
  1674. this.declareFunctions = new Set();
  1675. }
  1676. }
  1677. class FlowScopeHandler extends ScopeHandler {
  1678. createScope(flags) {
  1679. return new FlowScope(flags);
  1680. }
  1681. declareName(name, bindingType, loc) {
  1682. const scope = this.currentScope();
  1683. if (bindingType & 2048) {
  1684. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  1685. this.maybeExportDefined(scope, name);
  1686. scope.declareFunctions.add(name);
  1687. return;
  1688. }
  1689. super.declareName(name, bindingType, loc);
  1690. }
  1691. isRedeclaredInScope(scope, name, bindingType) {
  1692. if (super.isRedeclaredInScope(scope, name, bindingType)) return true;
  1693. if (bindingType & 2048 && !scope.declareFunctions.has(name)) {
  1694. const type = scope.names.get(name);
  1695. return (type & 4) > 0 || (type & 2) > 0;
  1696. }
  1697. return false;
  1698. }
  1699. checkLocalExport(id) {
  1700. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1701. super.checkLocalExport(id);
  1702. }
  1703. }
  1704. }
  1705. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  1706. const FlowErrors = ParseErrorEnum`flow`({
  1707. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  1708. 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.",
  1709. AssignReservedType: ({
  1710. reservedType
  1711. }) => `Cannot overwrite reserved type ${reservedType}.`,
  1712. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  1713. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  1714. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  1715. EnumBooleanMemberNotInitialized: ({
  1716. memberName,
  1717. enumName
  1718. }) => `Boolean enum members need to be initialized. Use either \`${memberName} = true,\` or \`${memberName} = false,\` in enum \`${enumName}\`.`,
  1719. EnumDuplicateMemberName: ({
  1720. memberName,
  1721. enumName
  1722. }) => `Enum member names need to be unique, but the name \`${memberName}\` has already been used before in enum \`${enumName}\`.`,
  1723. EnumInconsistentMemberValues: ({
  1724. enumName
  1725. }) => `Enum \`${enumName}\` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.`,
  1726. EnumInvalidExplicitType: ({
  1727. invalidEnumType,
  1728. enumName
  1729. }) => `Enum type \`${invalidEnumType}\` is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  1730. EnumInvalidExplicitTypeUnknownSupplied: ({
  1731. enumName
  1732. }) => `Supplied enum type is not valid. Use one of \`boolean\`, \`number\`, \`string\`, or \`symbol\` in enum \`${enumName}\`.`,
  1733. EnumInvalidMemberInitializerPrimaryType: ({
  1734. enumName,
  1735. memberName,
  1736. explicitType
  1737. }) => `Enum \`${enumName}\` has type \`${explicitType}\`, so the initializer of \`${memberName}\` needs to be a ${explicitType} literal.`,
  1738. EnumInvalidMemberInitializerSymbolType: ({
  1739. enumName,
  1740. memberName
  1741. }) => `Symbol enum members cannot be initialized. Use \`${memberName},\` in enum \`${enumName}\`.`,
  1742. EnumInvalidMemberInitializerUnknownType: ({
  1743. enumName,
  1744. memberName
  1745. }) => `The enum member initializer for \`${memberName}\` needs to be a literal (either a boolean, number, or string) in enum \`${enumName}\`.`,
  1746. EnumInvalidMemberName: ({
  1747. enumName,
  1748. memberName,
  1749. suggestion
  1750. }) => `Enum member names cannot start with lowercase 'a' through 'z'. Instead of using \`${memberName}\`, consider using \`${suggestion}\`, in enum \`${enumName}\`.`,
  1751. EnumNumberMemberNotInitialized: ({
  1752. enumName,
  1753. memberName
  1754. }) => `Number enum members need to be initialized, e.g. \`${memberName} = 1\` in enum \`${enumName}\`.`,
  1755. EnumStringMemberInconsistentlyInitialized: ({
  1756. enumName
  1757. }) => `String enum members need to consistently either all use initializers, or use no initializers, in enum \`${enumName}\`.`,
  1758. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  1759. ImportReflectionHasImportType: "An `import module` declaration can not use `type` or `typeof` keyword.",
  1760. 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.",
  1761. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  1762. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  1763. InexactVariance: "Explicit inexact syntax cannot have variance.",
  1764. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  1765. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  1766. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  1767. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  1768. PatternIsOptional: Object.assign({
  1769. message: "A binding pattern parameter cannot be optional in an implementation signature."
  1770. }, {
  1771. reasonCode: "OptionalBindingPattern"
  1772. }),
  1773. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  1774. SpreadVariance: "Spread properties cannot have variance.",
  1775. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  1776. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  1777. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  1778. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  1779. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  1780. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  1781. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  1782. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  1783. UnexpectedReservedType: ({
  1784. reservedType
  1785. }) => `Unexpected reserved type ${reservedType}.`,
  1786. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  1787. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  1788. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  1789. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  1790. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  1791. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  1792. UnsupportedDeclareExportKind: ({
  1793. unsupportedExportKind,
  1794. suggestion
  1795. }) => `\`declare export ${unsupportedExportKind}\` is not supported. Use \`${suggestion}\` instead.`,
  1796. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  1797. UnterminatedFlowComment: "Unterminated flow-comment."
  1798. });
  1799. function isEsModuleType(bodyElement) {
  1800. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  1801. }
  1802. function hasTypeImportKind(node) {
  1803. return node.importKind === "type" || node.importKind === "typeof";
  1804. }
  1805. const exportSuggestions = {
  1806. const: "declare export var",
  1807. let: "declare export var",
  1808. type: "export type",
  1809. interface: "export interface"
  1810. };
  1811. function partition(list, test) {
  1812. const list1 = [];
  1813. const list2 = [];
  1814. for (let i = 0; i < list.length; i++) {
  1815. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  1816. }
  1817. return [list1, list2];
  1818. }
  1819. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  1820. var flow = superClass => class FlowParserMixin extends superClass {
  1821. constructor(...args) {
  1822. super(...args);
  1823. this.flowPragma = undefined;
  1824. }
  1825. getScopeHandler() {
  1826. return FlowScopeHandler;
  1827. }
  1828. shouldParseTypes() {
  1829. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  1830. }
  1831. finishToken(type, val) {
  1832. if (type !== 134 && type !== 13 && type !== 28) {
  1833. if (this.flowPragma === undefined) {
  1834. this.flowPragma = null;
  1835. }
  1836. }
  1837. super.finishToken(type, val);
  1838. }
  1839. addComment(comment) {
  1840. if (this.flowPragma === undefined) {
  1841. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  1842. if (!matches) ;else if (matches[1] === "flow") {
  1843. this.flowPragma = "flow";
  1844. } else if (matches[1] === "noflow") {
  1845. this.flowPragma = "noflow";
  1846. } else {
  1847. throw new Error("Unexpected flow pragma");
  1848. }
  1849. }
  1850. super.addComment(comment);
  1851. }
  1852. flowParseTypeInitialiser(tok) {
  1853. const oldInType = this.state.inType;
  1854. this.state.inType = true;
  1855. this.expect(tok || 14);
  1856. const type = this.flowParseType();
  1857. this.state.inType = oldInType;
  1858. return type;
  1859. }
  1860. flowParsePredicate() {
  1861. const node = this.startNode();
  1862. const moduloLoc = this.state.startLoc;
  1863. this.next();
  1864. this.expectContextual(110);
  1865. if (this.state.lastTokStartLoc.index > moduloLoc.index + 1) {
  1866. this.raise(FlowErrors.UnexpectedSpaceBetweenModuloChecks, moduloLoc);
  1867. }
  1868. if (this.eat(10)) {
  1869. node.value = super.parseExpression();
  1870. this.expect(11);
  1871. return this.finishNode(node, "DeclaredPredicate");
  1872. } else {
  1873. return this.finishNode(node, "InferredPredicate");
  1874. }
  1875. }
  1876. flowParseTypeAndPredicateInitialiser() {
  1877. const oldInType = this.state.inType;
  1878. this.state.inType = true;
  1879. this.expect(14);
  1880. let type = null;
  1881. let predicate = null;
  1882. if (this.match(54)) {
  1883. this.state.inType = oldInType;
  1884. predicate = this.flowParsePredicate();
  1885. } else {
  1886. type = this.flowParseType();
  1887. this.state.inType = oldInType;
  1888. if (this.match(54)) {
  1889. predicate = this.flowParsePredicate();
  1890. }
  1891. }
  1892. return [type, predicate];
  1893. }
  1894. flowParseDeclareClass(node) {
  1895. this.next();
  1896. this.flowParseInterfaceish(node, true);
  1897. return this.finishNode(node, "DeclareClass");
  1898. }
  1899. flowParseDeclareFunction(node) {
  1900. this.next();
  1901. const id = node.id = this.parseIdentifier();
  1902. const typeNode = this.startNode();
  1903. const typeContainer = this.startNode();
  1904. if (this.match(47)) {
  1905. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  1906. } else {
  1907. typeNode.typeParameters = null;
  1908. }
  1909. this.expect(10);
  1910. const tmp = this.flowParseFunctionTypeParams();
  1911. typeNode.params = tmp.params;
  1912. typeNode.rest = tmp.rest;
  1913. typeNode.this = tmp._this;
  1914. this.expect(11);
  1915. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  1916. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  1917. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  1918. this.resetEndLocation(id);
  1919. this.semicolon();
  1920. this.scope.declareName(node.id.name, 2048, node.id.loc.start);
  1921. return this.finishNode(node, "DeclareFunction");
  1922. }
  1923. flowParseDeclare(node, insideModule) {
  1924. if (this.match(80)) {
  1925. return this.flowParseDeclareClass(node);
  1926. } else if (this.match(68)) {
  1927. return this.flowParseDeclareFunction(node);
  1928. } else if (this.match(74)) {
  1929. return this.flowParseDeclareVariable(node);
  1930. } else if (this.eatContextual(127)) {
  1931. if (this.match(16)) {
  1932. return this.flowParseDeclareModuleExports(node);
  1933. } else {
  1934. if (insideModule) {
  1935. this.raise(FlowErrors.NestedDeclareModule, this.state.lastTokStartLoc);
  1936. }
  1937. return this.flowParseDeclareModule(node);
  1938. }
  1939. } else if (this.isContextual(130)) {
  1940. return this.flowParseDeclareTypeAlias(node);
  1941. } else if (this.isContextual(131)) {
  1942. return this.flowParseDeclareOpaqueType(node);
  1943. } else if (this.isContextual(129)) {
  1944. return this.flowParseDeclareInterface(node);
  1945. } else if (this.match(82)) {
  1946. return this.flowParseDeclareExportDeclaration(node, insideModule);
  1947. }
  1948. throw this.unexpected();
  1949. }
  1950. flowParseDeclareVariable(node) {
  1951. this.next();
  1952. node.id = this.flowParseTypeAnnotatableIdentifier();
  1953. this.scope.declareName(node.id.name, 5, node.id.loc.start);
  1954. this.semicolon();
  1955. return this.finishNode(node, "DeclareVariable");
  1956. }
  1957. flowParseDeclareModule(node) {
  1958. this.scope.enter(0);
  1959. if (this.match(134)) {
  1960. node.id = super.parseExprAtom();
  1961. } else {
  1962. node.id = this.parseIdentifier();
  1963. }
  1964. const bodyNode = node.body = this.startNode();
  1965. const body = bodyNode.body = [];
  1966. this.expect(5);
  1967. while (!this.match(8)) {
  1968. const bodyNode = this.startNode();
  1969. if (this.match(83)) {
  1970. this.next();
  1971. if (!this.isContextual(130) && !this.match(87)) {
  1972. this.raise(FlowErrors.InvalidNonTypeImportInDeclareModule, this.state.lastTokStartLoc);
  1973. }
  1974. body.push(super.parseImport(bodyNode));
  1975. } else {
  1976. this.expectContextual(125, FlowErrors.UnsupportedStatementInDeclareModule);
  1977. body.push(this.flowParseDeclare(bodyNode, true));
  1978. }
  1979. }
  1980. this.scope.exit();
  1981. this.expect(8);
  1982. this.finishNode(bodyNode, "BlockStatement");
  1983. let kind = null;
  1984. let hasModuleExport = false;
  1985. body.forEach(bodyElement => {
  1986. if (isEsModuleType(bodyElement)) {
  1987. if (kind === "CommonJS") {
  1988. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  1989. }
  1990. kind = "ES";
  1991. } else if (bodyElement.type === "DeclareModuleExports") {
  1992. if (hasModuleExport) {
  1993. this.raise(FlowErrors.DuplicateDeclareModuleExports, bodyElement);
  1994. }
  1995. if (kind === "ES") {
  1996. this.raise(FlowErrors.AmbiguousDeclareModuleKind, bodyElement);
  1997. }
  1998. kind = "CommonJS";
  1999. hasModuleExport = true;
  2000. }
  2001. });
  2002. node.kind = kind || "CommonJS";
  2003. return this.finishNode(node, "DeclareModule");
  2004. }
  2005. flowParseDeclareExportDeclaration(node, insideModule) {
  2006. this.expect(82);
  2007. if (this.eat(65)) {
  2008. if (this.match(68) || this.match(80)) {
  2009. node.declaration = this.flowParseDeclare(this.startNode());
  2010. } else {
  2011. node.declaration = this.flowParseType();
  2012. this.semicolon();
  2013. }
  2014. node.default = true;
  2015. return this.finishNode(node, "DeclareExportDeclaration");
  2016. } else {
  2017. if (this.match(75) || this.isLet() || (this.isContextual(130) || this.isContextual(129)) && !insideModule) {
  2018. const label = this.state.value;
  2019. throw this.raise(FlowErrors.UnsupportedDeclareExportKind, this.state.startLoc, {
  2020. unsupportedExportKind: label,
  2021. suggestion: exportSuggestions[label]
  2022. });
  2023. }
  2024. if (this.match(74) || this.match(68) || this.match(80) || this.isContextual(131)) {
  2025. node.declaration = this.flowParseDeclare(this.startNode());
  2026. node.default = false;
  2027. return this.finishNode(node, "DeclareExportDeclaration");
  2028. } else if (this.match(55) || this.match(5) || this.isContextual(129) || this.isContextual(130) || this.isContextual(131)) {
  2029. node = this.parseExport(node, null);
  2030. if (node.type === "ExportNamedDeclaration") {
  2031. node.default = false;
  2032. delete node.exportKind;
  2033. return this.castNodeTo(node, "DeclareExportDeclaration");
  2034. } else {
  2035. return this.castNodeTo(node, "DeclareExportAllDeclaration");
  2036. }
  2037. }
  2038. }
  2039. throw this.unexpected();
  2040. }
  2041. flowParseDeclareModuleExports(node) {
  2042. this.next();
  2043. this.expectContextual(111);
  2044. node.typeAnnotation = this.flowParseTypeAnnotation();
  2045. this.semicolon();
  2046. return this.finishNode(node, "DeclareModuleExports");
  2047. }
  2048. flowParseDeclareTypeAlias(node) {
  2049. this.next();
  2050. const finished = this.flowParseTypeAlias(node);
  2051. this.castNodeTo(finished, "DeclareTypeAlias");
  2052. return finished;
  2053. }
  2054. flowParseDeclareOpaqueType(node) {
  2055. this.next();
  2056. const finished = this.flowParseOpaqueType(node, true);
  2057. this.castNodeTo(finished, "DeclareOpaqueType");
  2058. return finished;
  2059. }
  2060. flowParseDeclareInterface(node) {
  2061. this.next();
  2062. this.flowParseInterfaceish(node, false);
  2063. return this.finishNode(node, "DeclareInterface");
  2064. }
  2065. flowParseInterfaceish(node, isClass) {
  2066. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  2067. this.scope.declareName(node.id.name, isClass ? 17 : 8201, node.id.loc.start);
  2068. if (this.match(47)) {
  2069. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2070. } else {
  2071. node.typeParameters = null;
  2072. }
  2073. node.extends = [];
  2074. if (this.eat(81)) {
  2075. do {
  2076. node.extends.push(this.flowParseInterfaceExtends());
  2077. } while (!isClass && this.eat(12));
  2078. }
  2079. if (isClass) {
  2080. node.implements = [];
  2081. node.mixins = [];
  2082. if (this.eatContextual(117)) {
  2083. do {
  2084. node.mixins.push(this.flowParseInterfaceExtends());
  2085. } while (this.eat(12));
  2086. }
  2087. if (this.eatContextual(113)) {
  2088. do {
  2089. node.implements.push(this.flowParseInterfaceExtends());
  2090. } while (this.eat(12));
  2091. }
  2092. }
  2093. node.body = this.flowParseObjectType({
  2094. allowStatic: isClass,
  2095. allowExact: false,
  2096. allowSpread: false,
  2097. allowProto: isClass,
  2098. allowInexact: false
  2099. });
  2100. }
  2101. flowParseInterfaceExtends() {
  2102. const node = this.startNode();
  2103. node.id = this.flowParseQualifiedTypeIdentifier();
  2104. if (this.match(47)) {
  2105. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2106. } else {
  2107. node.typeParameters = null;
  2108. }
  2109. return this.finishNode(node, "InterfaceExtends");
  2110. }
  2111. flowParseInterface(node) {
  2112. this.flowParseInterfaceish(node, false);
  2113. return this.finishNode(node, "InterfaceDeclaration");
  2114. }
  2115. checkNotUnderscore(word) {
  2116. if (word === "_") {
  2117. this.raise(FlowErrors.UnexpectedReservedUnderscore, this.state.startLoc);
  2118. }
  2119. }
  2120. checkReservedType(word, startLoc, declaration) {
  2121. if (!reservedTypes.has(word)) return;
  2122. this.raise(declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, startLoc, {
  2123. reservedType: word
  2124. });
  2125. }
  2126. flowParseRestrictedIdentifierName(liberal, declaration) {
  2127. this.checkReservedType(this.state.value, this.state.startLoc, declaration);
  2128. return this.parseIdentifierName(liberal);
  2129. }
  2130. flowParseRestrictedIdentifier(liberal, declaration) {
  2131. const node = this.startNode();
  2132. const name = this.flowParseRestrictedIdentifierName(liberal, declaration);
  2133. return this.createIdentifier(node, name);
  2134. }
  2135. flowParseTypeAlias(node) {
  2136. node.id = this.flowParseRestrictedIdentifier(false, true);
  2137. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  2138. if (this.match(47)) {
  2139. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2140. } else {
  2141. node.typeParameters = null;
  2142. }
  2143. node.right = this.flowParseTypeInitialiser(29);
  2144. this.semicolon();
  2145. return this.finishNode(node, "TypeAlias");
  2146. }
  2147. flowParseOpaqueType(node, declare) {
  2148. this.expectContextual(130);
  2149. node.id = this.flowParseRestrictedIdentifier(true, true);
  2150. this.scope.declareName(node.id.name, 8201, node.id.loc.start);
  2151. if (this.match(47)) {
  2152. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2153. } else {
  2154. node.typeParameters = null;
  2155. }
  2156. node.supertype = null;
  2157. if (this.match(14)) {
  2158. node.supertype = this.flowParseTypeInitialiser(14);
  2159. }
  2160. node.impltype = null;
  2161. if (!declare) {
  2162. node.impltype = this.flowParseTypeInitialiser(29);
  2163. }
  2164. this.semicolon();
  2165. return this.finishNode(node, "OpaqueType");
  2166. }
  2167. flowParseTypeParameterBound() {
  2168. if (this.match(14) || this.isContextual(81)) {
  2169. const node = this.startNode();
  2170. this.next();
  2171. node.typeAnnotation = this.flowParseType();
  2172. return this.finishNode(node, "TypeAnnotation");
  2173. }
  2174. }
  2175. flowParseTypeParameter(requireDefault = false) {
  2176. const nodeStartLoc = this.state.startLoc;
  2177. const node = this.startNode();
  2178. const variance = this.flowParseVariance();
  2179. node.name = this.flowParseRestrictedIdentifierName();
  2180. node.variance = variance;
  2181. node.bound = this.flowParseTypeParameterBound();
  2182. if (this.match(29)) {
  2183. this.eat(29);
  2184. node.default = this.flowParseType();
  2185. } else {
  2186. if (requireDefault) {
  2187. this.raise(FlowErrors.MissingTypeParamDefault, nodeStartLoc);
  2188. }
  2189. }
  2190. return this.finishNode(node, "TypeParameter");
  2191. }
  2192. flowParseTypeParameterDeclaration() {
  2193. const oldInType = this.state.inType;
  2194. const node = this.startNode();
  2195. node.params = [];
  2196. this.state.inType = true;
  2197. if (this.match(47) || this.match(143)) {
  2198. this.next();
  2199. } else {
  2200. this.unexpected();
  2201. }
  2202. let defaultRequired = false;
  2203. do {
  2204. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  2205. node.params.push(typeParameter);
  2206. if (typeParameter.default) {
  2207. defaultRequired = true;
  2208. }
  2209. if (!this.match(48)) {
  2210. this.expect(12);
  2211. }
  2212. } while (!this.match(48));
  2213. this.expect(48);
  2214. this.state.inType = oldInType;
  2215. return this.finishNode(node, "TypeParameterDeclaration");
  2216. }
  2217. flowInTopLevelContext(cb) {
  2218. if (this.curContext() !== types.brace) {
  2219. const oldContext = this.state.context;
  2220. this.state.context = [oldContext[0]];
  2221. try {
  2222. return cb();
  2223. } finally {
  2224. this.state.context = oldContext;
  2225. }
  2226. } else {
  2227. return cb();
  2228. }
  2229. }
  2230. flowParseTypeParameterInstantiationInExpression() {
  2231. if (this.reScan_lt() !== 47) return;
  2232. return this.flowParseTypeParameterInstantiation();
  2233. }
  2234. flowParseTypeParameterInstantiation() {
  2235. const node = this.startNode();
  2236. const oldInType = this.state.inType;
  2237. this.state.inType = true;
  2238. node.params = [];
  2239. this.flowInTopLevelContext(() => {
  2240. this.expect(47);
  2241. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2242. this.state.noAnonFunctionType = false;
  2243. while (!this.match(48)) {
  2244. node.params.push(this.flowParseType());
  2245. if (!this.match(48)) {
  2246. this.expect(12);
  2247. }
  2248. }
  2249. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2250. });
  2251. this.state.inType = oldInType;
  2252. if (!this.state.inType && this.curContext() === types.brace) {
  2253. this.reScan_lt_gt();
  2254. }
  2255. this.expect(48);
  2256. return this.finishNode(node, "TypeParameterInstantiation");
  2257. }
  2258. flowParseTypeParameterInstantiationCallOrNew() {
  2259. if (this.reScan_lt() !== 47) return null;
  2260. const node = this.startNode();
  2261. const oldInType = this.state.inType;
  2262. node.params = [];
  2263. this.state.inType = true;
  2264. this.expect(47);
  2265. while (!this.match(48)) {
  2266. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  2267. if (!this.match(48)) {
  2268. this.expect(12);
  2269. }
  2270. }
  2271. this.expect(48);
  2272. this.state.inType = oldInType;
  2273. return this.finishNode(node, "TypeParameterInstantiation");
  2274. }
  2275. flowParseInterfaceType() {
  2276. const node = this.startNode();
  2277. this.expectContextual(129);
  2278. node.extends = [];
  2279. if (this.eat(81)) {
  2280. do {
  2281. node.extends.push(this.flowParseInterfaceExtends());
  2282. } while (this.eat(12));
  2283. }
  2284. node.body = this.flowParseObjectType({
  2285. allowStatic: false,
  2286. allowExact: false,
  2287. allowSpread: false,
  2288. allowProto: false,
  2289. allowInexact: false
  2290. });
  2291. return this.finishNode(node, "InterfaceTypeAnnotation");
  2292. }
  2293. flowParseObjectPropertyKey() {
  2294. return this.match(135) || this.match(134) ? super.parseExprAtom() : this.parseIdentifier(true);
  2295. }
  2296. flowParseObjectTypeIndexer(node, isStatic, variance) {
  2297. node.static = isStatic;
  2298. if (this.lookahead().type === 14) {
  2299. node.id = this.flowParseObjectPropertyKey();
  2300. node.key = this.flowParseTypeInitialiser();
  2301. } else {
  2302. node.id = null;
  2303. node.key = this.flowParseType();
  2304. }
  2305. this.expect(3);
  2306. node.value = this.flowParseTypeInitialiser();
  2307. node.variance = variance;
  2308. return this.finishNode(node, "ObjectTypeIndexer");
  2309. }
  2310. flowParseObjectTypeInternalSlot(node, isStatic) {
  2311. node.static = isStatic;
  2312. node.id = this.flowParseObjectPropertyKey();
  2313. this.expect(3);
  2314. this.expect(3);
  2315. if (this.match(47) || this.match(10)) {
  2316. node.method = true;
  2317. node.optional = false;
  2318. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  2319. } else {
  2320. node.method = false;
  2321. if (this.eat(17)) {
  2322. node.optional = true;
  2323. }
  2324. node.value = this.flowParseTypeInitialiser();
  2325. }
  2326. return this.finishNode(node, "ObjectTypeInternalSlot");
  2327. }
  2328. flowParseObjectTypeMethodish(node) {
  2329. node.params = [];
  2330. node.rest = null;
  2331. node.typeParameters = null;
  2332. node.this = null;
  2333. if (this.match(47)) {
  2334. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2335. }
  2336. this.expect(10);
  2337. if (this.match(78)) {
  2338. node.this = this.flowParseFunctionTypeParam(true);
  2339. node.this.name = null;
  2340. if (!this.match(11)) {
  2341. this.expect(12);
  2342. }
  2343. }
  2344. while (!this.match(11) && !this.match(21)) {
  2345. node.params.push(this.flowParseFunctionTypeParam(false));
  2346. if (!this.match(11)) {
  2347. this.expect(12);
  2348. }
  2349. }
  2350. if (this.eat(21)) {
  2351. node.rest = this.flowParseFunctionTypeParam(false);
  2352. }
  2353. this.expect(11);
  2354. node.returnType = this.flowParseTypeInitialiser();
  2355. return this.finishNode(node, "FunctionTypeAnnotation");
  2356. }
  2357. flowParseObjectTypeCallProperty(node, isStatic) {
  2358. const valueNode = this.startNode();
  2359. node.static = isStatic;
  2360. node.value = this.flowParseObjectTypeMethodish(valueNode);
  2361. return this.finishNode(node, "ObjectTypeCallProperty");
  2362. }
  2363. flowParseObjectType({
  2364. allowStatic,
  2365. allowExact,
  2366. allowSpread,
  2367. allowProto,
  2368. allowInexact
  2369. }) {
  2370. const oldInType = this.state.inType;
  2371. this.state.inType = true;
  2372. const nodeStart = this.startNode();
  2373. nodeStart.callProperties = [];
  2374. nodeStart.properties = [];
  2375. nodeStart.indexers = [];
  2376. nodeStart.internalSlots = [];
  2377. let endDelim;
  2378. let exact;
  2379. let inexact = false;
  2380. if (allowExact && this.match(6)) {
  2381. this.expect(6);
  2382. endDelim = 9;
  2383. exact = true;
  2384. } else {
  2385. this.expect(5);
  2386. endDelim = 8;
  2387. exact = false;
  2388. }
  2389. nodeStart.exact = exact;
  2390. while (!this.match(endDelim)) {
  2391. let isStatic = false;
  2392. let protoStartLoc = null;
  2393. let inexactStartLoc = null;
  2394. const node = this.startNode();
  2395. if (allowProto && this.isContextual(118)) {
  2396. const lookahead = this.lookahead();
  2397. if (lookahead.type !== 14 && lookahead.type !== 17) {
  2398. this.next();
  2399. protoStartLoc = this.state.startLoc;
  2400. allowStatic = false;
  2401. }
  2402. }
  2403. if (allowStatic && this.isContextual(106)) {
  2404. const lookahead = this.lookahead();
  2405. if (lookahead.type !== 14 && lookahead.type !== 17) {
  2406. this.next();
  2407. isStatic = true;
  2408. }
  2409. }
  2410. const variance = this.flowParseVariance();
  2411. if (this.eat(0)) {
  2412. if (protoStartLoc != null) {
  2413. this.unexpected(protoStartLoc);
  2414. }
  2415. if (this.eat(0)) {
  2416. if (variance) {
  2417. this.unexpected(variance.loc.start);
  2418. }
  2419. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  2420. } else {
  2421. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  2422. }
  2423. } else if (this.match(10) || this.match(47)) {
  2424. if (protoStartLoc != null) {
  2425. this.unexpected(protoStartLoc);
  2426. }
  2427. if (variance) {
  2428. this.unexpected(variance.loc.start);
  2429. }
  2430. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  2431. } else {
  2432. let kind = "init";
  2433. if (this.isContextual(99) || this.isContextual(104)) {
  2434. const lookahead = this.lookahead();
  2435. if (tokenIsLiteralPropertyName(lookahead.type)) {
  2436. kind = this.state.value;
  2437. this.next();
  2438. }
  2439. }
  2440. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  2441. if (propOrInexact === null) {
  2442. inexact = true;
  2443. inexactStartLoc = this.state.lastTokStartLoc;
  2444. } else {
  2445. nodeStart.properties.push(propOrInexact);
  2446. }
  2447. }
  2448. this.flowObjectTypeSemicolon();
  2449. if (inexactStartLoc && !this.match(8) && !this.match(9)) {
  2450. this.raise(FlowErrors.UnexpectedExplicitInexactInObject, inexactStartLoc);
  2451. }
  2452. }
  2453. this.expect(endDelim);
  2454. if (allowSpread) {
  2455. nodeStart.inexact = inexact;
  2456. }
  2457. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  2458. this.state.inType = oldInType;
  2459. return out;
  2460. }
  2461. flowParseObjectTypeProperty(node, isStatic, protoStartLoc, variance, kind, allowSpread, allowInexact) {
  2462. if (this.eat(21)) {
  2463. const isInexactToken = this.match(12) || this.match(13) || this.match(8) || this.match(9);
  2464. if (isInexactToken) {
  2465. if (!allowSpread) {
  2466. this.raise(FlowErrors.InexactInsideNonObject, this.state.lastTokStartLoc);
  2467. } else if (!allowInexact) {
  2468. this.raise(FlowErrors.InexactInsideExact, this.state.lastTokStartLoc);
  2469. }
  2470. if (variance) {
  2471. this.raise(FlowErrors.InexactVariance, variance);
  2472. }
  2473. return null;
  2474. }
  2475. if (!allowSpread) {
  2476. this.raise(FlowErrors.UnexpectedSpreadType, this.state.lastTokStartLoc);
  2477. }
  2478. if (protoStartLoc != null) {
  2479. this.unexpected(protoStartLoc);
  2480. }
  2481. if (variance) {
  2482. this.raise(FlowErrors.SpreadVariance, variance);
  2483. }
  2484. node.argument = this.flowParseType();
  2485. return this.finishNode(node, "ObjectTypeSpreadProperty");
  2486. } else {
  2487. node.key = this.flowParseObjectPropertyKey();
  2488. node.static = isStatic;
  2489. node.proto = protoStartLoc != null;
  2490. node.kind = kind;
  2491. let optional = false;
  2492. if (this.match(47) || this.match(10)) {
  2493. node.method = true;
  2494. if (protoStartLoc != null) {
  2495. this.unexpected(protoStartLoc);
  2496. }
  2497. if (variance) {
  2498. this.unexpected(variance.loc.start);
  2499. }
  2500. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.loc.start));
  2501. if (kind === "get" || kind === "set") {
  2502. this.flowCheckGetterSetterParams(node);
  2503. }
  2504. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  2505. this.raise(FlowErrors.ThisParamBannedInConstructor, node.value.this);
  2506. }
  2507. } else {
  2508. if (kind !== "init") this.unexpected();
  2509. node.method = false;
  2510. if (this.eat(17)) {
  2511. optional = true;
  2512. }
  2513. node.value = this.flowParseTypeInitialiser();
  2514. node.variance = variance;
  2515. }
  2516. node.optional = optional;
  2517. return this.finishNode(node, "ObjectTypeProperty");
  2518. }
  2519. }
  2520. flowCheckGetterSetterParams(property) {
  2521. const paramCount = property.kind === "get" ? 0 : 1;
  2522. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  2523. if (property.value.this) {
  2524. this.raise(property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam, property.value.this);
  2525. }
  2526. if (length !== paramCount) {
  2527. this.raise(property.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, property);
  2528. }
  2529. if (property.kind === "set" && property.value.rest) {
  2530. this.raise(Errors.BadSetterRestParameter, property);
  2531. }
  2532. }
  2533. flowObjectTypeSemicolon() {
  2534. if (!this.eat(13) && !this.eat(12) && !this.match(8) && !this.match(9)) {
  2535. this.unexpected();
  2536. }
  2537. }
  2538. flowParseQualifiedTypeIdentifier(startLoc, id) {
  2539. startLoc != null ? startLoc : startLoc = this.state.startLoc;
  2540. let node = id || this.flowParseRestrictedIdentifier(true);
  2541. while (this.eat(16)) {
  2542. const node2 = this.startNodeAt(startLoc);
  2543. node2.qualification = node;
  2544. node2.id = this.flowParseRestrictedIdentifier(true);
  2545. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  2546. }
  2547. return node;
  2548. }
  2549. flowParseGenericType(startLoc, id) {
  2550. const node = this.startNodeAt(startLoc);
  2551. node.typeParameters = null;
  2552. node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id);
  2553. if (this.match(47)) {
  2554. node.typeParameters = this.flowParseTypeParameterInstantiation();
  2555. }
  2556. return this.finishNode(node, "GenericTypeAnnotation");
  2557. }
  2558. flowParseTypeofType() {
  2559. const node = this.startNode();
  2560. this.expect(87);
  2561. node.argument = this.flowParsePrimaryType();
  2562. return this.finishNode(node, "TypeofTypeAnnotation");
  2563. }
  2564. flowParseTupleType() {
  2565. const node = this.startNode();
  2566. node.types = [];
  2567. this.expect(0);
  2568. while (this.state.pos < this.length && !this.match(3)) {
  2569. node.types.push(this.flowParseType());
  2570. if (this.match(3)) break;
  2571. this.expect(12);
  2572. }
  2573. this.expect(3);
  2574. return this.finishNode(node, "TupleTypeAnnotation");
  2575. }
  2576. flowParseFunctionTypeParam(first) {
  2577. let name = null;
  2578. let optional = false;
  2579. let typeAnnotation = null;
  2580. const node = this.startNode();
  2581. const lh = this.lookahead();
  2582. const isThis = this.state.type === 78;
  2583. if (lh.type === 14 || lh.type === 17) {
  2584. if (isThis && !first) {
  2585. this.raise(FlowErrors.ThisParamMustBeFirst, node);
  2586. }
  2587. name = this.parseIdentifier(isThis);
  2588. if (this.eat(17)) {
  2589. optional = true;
  2590. if (isThis) {
  2591. this.raise(FlowErrors.ThisParamMayNotBeOptional, node);
  2592. }
  2593. }
  2594. typeAnnotation = this.flowParseTypeInitialiser();
  2595. } else {
  2596. typeAnnotation = this.flowParseType();
  2597. }
  2598. node.name = name;
  2599. node.optional = optional;
  2600. node.typeAnnotation = typeAnnotation;
  2601. return this.finishNode(node, "FunctionTypeParam");
  2602. }
  2603. reinterpretTypeAsFunctionTypeParam(type) {
  2604. const node = this.startNodeAt(type.loc.start);
  2605. node.name = null;
  2606. node.optional = false;
  2607. node.typeAnnotation = type;
  2608. return this.finishNode(node, "FunctionTypeParam");
  2609. }
  2610. flowParseFunctionTypeParams(params = []) {
  2611. let rest = null;
  2612. let _this = null;
  2613. if (this.match(78)) {
  2614. _this = this.flowParseFunctionTypeParam(true);
  2615. _this.name = null;
  2616. if (!this.match(11)) {
  2617. this.expect(12);
  2618. }
  2619. }
  2620. while (!this.match(11) && !this.match(21)) {
  2621. params.push(this.flowParseFunctionTypeParam(false));
  2622. if (!this.match(11)) {
  2623. this.expect(12);
  2624. }
  2625. }
  2626. if (this.eat(21)) {
  2627. rest = this.flowParseFunctionTypeParam(false);
  2628. }
  2629. return {
  2630. params,
  2631. rest,
  2632. _this
  2633. };
  2634. }
  2635. flowIdentToTypeAnnotation(startLoc, node, id) {
  2636. switch (id.name) {
  2637. case "any":
  2638. return this.finishNode(node, "AnyTypeAnnotation");
  2639. case "bool":
  2640. case "boolean":
  2641. return this.finishNode(node, "BooleanTypeAnnotation");
  2642. case "mixed":
  2643. return this.finishNode(node, "MixedTypeAnnotation");
  2644. case "empty":
  2645. return this.finishNode(node, "EmptyTypeAnnotation");
  2646. case "number":
  2647. return this.finishNode(node, "NumberTypeAnnotation");
  2648. case "string":
  2649. return this.finishNode(node, "StringTypeAnnotation");
  2650. case "symbol":
  2651. return this.finishNode(node, "SymbolTypeAnnotation");
  2652. default:
  2653. this.checkNotUnderscore(id.name);
  2654. return this.flowParseGenericType(startLoc, id);
  2655. }
  2656. }
  2657. flowParsePrimaryType() {
  2658. const startLoc = this.state.startLoc;
  2659. const node = this.startNode();
  2660. let tmp;
  2661. let type;
  2662. let isGroupedType = false;
  2663. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  2664. switch (this.state.type) {
  2665. case 5:
  2666. return this.flowParseObjectType({
  2667. allowStatic: false,
  2668. allowExact: false,
  2669. allowSpread: true,
  2670. allowProto: false,
  2671. allowInexact: true
  2672. });
  2673. case 6:
  2674. return this.flowParseObjectType({
  2675. allowStatic: false,
  2676. allowExact: true,
  2677. allowSpread: true,
  2678. allowProto: false,
  2679. allowInexact: false
  2680. });
  2681. case 0:
  2682. this.state.noAnonFunctionType = false;
  2683. type = this.flowParseTupleType();
  2684. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2685. return type;
  2686. case 47:
  2687. {
  2688. const node = this.startNode();
  2689. node.typeParameters = this.flowParseTypeParameterDeclaration();
  2690. this.expect(10);
  2691. tmp = this.flowParseFunctionTypeParams();
  2692. node.params = tmp.params;
  2693. node.rest = tmp.rest;
  2694. node.this = tmp._this;
  2695. this.expect(11);
  2696. this.expect(19);
  2697. node.returnType = this.flowParseType();
  2698. return this.finishNode(node, "FunctionTypeAnnotation");
  2699. }
  2700. case 10:
  2701. {
  2702. const node = this.startNode();
  2703. this.next();
  2704. if (!this.match(11) && !this.match(21)) {
  2705. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  2706. const token = this.lookahead().type;
  2707. isGroupedType = token !== 17 && token !== 14;
  2708. } else {
  2709. isGroupedType = true;
  2710. }
  2711. }
  2712. if (isGroupedType) {
  2713. this.state.noAnonFunctionType = false;
  2714. type = this.flowParseType();
  2715. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  2716. if (this.state.noAnonFunctionType || !(this.match(12) || this.match(11) && this.lookahead().type === 19)) {
  2717. this.expect(11);
  2718. return type;
  2719. } else {
  2720. this.eat(12);
  2721. }
  2722. }
  2723. if (type) {
  2724. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  2725. } else {
  2726. tmp = this.flowParseFunctionTypeParams();
  2727. }
  2728. node.params = tmp.params;
  2729. node.rest = tmp.rest;
  2730. node.this = tmp._this;
  2731. this.expect(11);
  2732. this.expect(19);
  2733. node.returnType = this.flowParseType();
  2734. node.typeParameters = null;
  2735. return this.finishNode(node, "FunctionTypeAnnotation");
  2736. }
  2737. case 134:
  2738. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  2739. case 85:
  2740. case 86:
  2741. node.value = this.match(85);
  2742. this.next();
  2743. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  2744. case 53:
  2745. if (this.state.value === "-") {
  2746. this.next();
  2747. if (this.match(135)) {
  2748. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  2749. }
  2750. if (this.match(136)) {
  2751. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  2752. }
  2753. throw this.raise(FlowErrors.UnexpectedSubtractionOperand, this.state.startLoc);
  2754. }
  2755. throw this.unexpected();
  2756. case 135:
  2757. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  2758. case 136:
  2759. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  2760. case 88:
  2761. this.next();
  2762. return this.finishNode(node, "VoidTypeAnnotation");
  2763. case 84:
  2764. this.next();
  2765. return this.finishNode(node, "NullLiteralTypeAnnotation");
  2766. case 78:
  2767. this.next();
  2768. return this.finishNode(node, "ThisTypeAnnotation");
  2769. case 55:
  2770. this.next();
  2771. return this.finishNode(node, "ExistsTypeAnnotation");
  2772. case 87:
  2773. return this.flowParseTypeofType();
  2774. default:
  2775. if (tokenIsKeyword(this.state.type)) {
  2776. const label = tokenLabelName(this.state.type);
  2777. this.next();
  2778. return super.createIdentifier(node, label);
  2779. } else if (tokenIsIdentifier(this.state.type)) {
  2780. if (this.isContextual(129)) {
  2781. return this.flowParseInterfaceType();
  2782. }
  2783. return this.flowIdentToTypeAnnotation(startLoc, node, this.parseIdentifier());
  2784. }
  2785. }
  2786. throw this.unexpected();
  2787. }
  2788. flowParsePostfixType() {
  2789. const startLoc = this.state.startLoc;
  2790. let type = this.flowParsePrimaryType();
  2791. let seenOptionalIndexedAccess = false;
  2792. while ((this.match(0) || this.match(18)) && !this.canInsertSemicolon()) {
  2793. const node = this.startNodeAt(startLoc);
  2794. const optional = this.eat(18);
  2795. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  2796. this.expect(0);
  2797. if (!optional && this.match(3)) {
  2798. node.elementType = type;
  2799. this.next();
  2800. type = this.finishNode(node, "ArrayTypeAnnotation");
  2801. } else {
  2802. node.objectType = type;
  2803. node.indexType = this.flowParseType();
  2804. this.expect(3);
  2805. if (seenOptionalIndexedAccess) {
  2806. node.optional = optional;
  2807. type = this.finishNode(node, "OptionalIndexedAccessType");
  2808. } else {
  2809. type = this.finishNode(node, "IndexedAccessType");
  2810. }
  2811. }
  2812. }
  2813. return type;
  2814. }
  2815. flowParsePrefixType() {
  2816. const node = this.startNode();
  2817. if (this.eat(17)) {
  2818. node.typeAnnotation = this.flowParsePrefixType();
  2819. return this.finishNode(node, "NullableTypeAnnotation");
  2820. } else {
  2821. return this.flowParsePostfixType();
  2822. }
  2823. }
  2824. flowParseAnonFunctionWithoutParens() {
  2825. const param = this.flowParsePrefixType();
  2826. if (!this.state.noAnonFunctionType && this.eat(19)) {
  2827. const node = this.startNodeAt(param.loc.start);
  2828. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  2829. node.rest = null;
  2830. node.this = null;
  2831. node.returnType = this.flowParseType();
  2832. node.typeParameters = null;
  2833. return this.finishNode(node, "FunctionTypeAnnotation");
  2834. }
  2835. return param;
  2836. }
  2837. flowParseIntersectionType() {
  2838. const node = this.startNode();
  2839. this.eat(45);
  2840. const type = this.flowParseAnonFunctionWithoutParens();
  2841. node.types = [type];
  2842. while (this.eat(45)) {
  2843. node.types.push(this.flowParseAnonFunctionWithoutParens());
  2844. }
  2845. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  2846. }
  2847. flowParseUnionType() {
  2848. const node = this.startNode();
  2849. this.eat(43);
  2850. const type = this.flowParseIntersectionType();
  2851. node.types = [type];
  2852. while (this.eat(43)) {
  2853. node.types.push(this.flowParseIntersectionType());
  2854. }
  2855. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  2856. }
  2857. flowParseType() {
  2858. const oldInType = this.state.inType;
  2859. this.state.inType = true;
  2860. const type = this.flowParseUnionType();
  2861. this.state.inType = oldInType;
  2862. return type;
  2863. }
  2864. flowParseTypeOrImplicitInstantiation() {
  2865. if (this.state.type === 132 && this.state.value === "_") {
  2866. const startLoc = this.state.startLoc;
  2867. const node = this.parseIdentifier();
  2868. return this.flowParseGenericType(startLoc, node);
  2869. } else {
  2870. return this.flowParseType();
  2871. }
  2872. }
  2873. flowParseTypeAnnotation() {
  2874. const node = this.startNode();
  2875. node.typeAnnotation = this.flowParseTypeInitialiser();
  2876. return this.finishNode(node, "TypeAnnotation");
  2877. }
  2878. flowParseTypeAnnotatableIdentifier() {
  2879. const node = this.startNode();
  2880. const name = this.parseIdentifierName();
  2881. if (this.match(14)) {
  2882. node.typeAnnotation = this.flowParseTypeAnnotation();
  2883. }
  2884. return this.createIdentifier(node, name);
  2885. }
  2886. typeCastToParameter(node) {
  2887. node.expression.typeAnnotation = node.typeAnnotation;
  2888. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  2889. return node.expression;
  2890. }
  2891. flowParseVariance() {
  2892. let variance = null;
  2893. if (this.match(53)) {
  2894. variance = this.startNode();
  2895. if (this.state.value === "+") {
  2896. variance.kind = "plus";
  2897. } else {
  2898. variance.kind = "minus";
  2899. }
  2900. this.next();
  2901. return this.finishNode(variance, "Variance");
  2902. }
  2903. return variance;
  2904. }
  2905. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  2906. if (allowExpressionBody) {
  2907. this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  2908. return;
  2909. }
  2910. super.parseFunctionBody(node, false, isMethod);
  2911. }
  2912. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  2913. if (this.match(14)) {
  2914. const typeNode = this.startNode();
  2915. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  2916. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  2917. }
  2918. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  2919. }
  2920. parseStatementLike(flags) {
  2921. if (this.state.strict && this.isContextual(129)) {
  2922. const lookahead = this.lookahead();
  2923. if (tokenIsKeywordOrIdentifier(lookahead.type)) {
  2924. const node = this.startNode();
  2925. this.next();
  2926. return this.flowParseInterface(node);
  2927. }
  2928. } else if (this.isContextual(126)) {
  2929. const node = this.startNode();
  2930. this.next();
  2931. return this.flowParseEnumDeclaration(node);
  2932. }
  2933. const stmt = super.parseStatementLike(flags);
  2934. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  2935. this.flowPragma = null;
  2936. }
  2937. return stmt;
  2938. }
  2939. parseExpressionStatement(node, expr, decorators) {
  2940. if (expr.type === "Identifier") {
  2941. if (expr.name === "declare") {
  2942. if (this.match(80) || tokenIsIdentifier(this.state.type) || this.match(68) || this.match(74) || this.match(82)) {
  2943. return this.flowParseDeclare(node);
  2944. }
  2945. } else if (tokenIsIdentifier(this.state.type)) {
  2946. if (expr.name === "interface") {
  2947. return this.flowParseInterface(node);
  2948. } else if (expr.name === "type") {
  2949. return this.flowParseTypeAlias(node);
  2950. } else if (expr.name === "opaque") {
  2951. return this.flowParseOpaqueType(node, false);
  2952. }
  2953. }
  2954. }
  2955. return super.parseExpressionStatement(node, expr, decorators);
  2956. }
  2957. shouldParseExportDeclaration() {
  2958. const {
  2959. type
  2960. } = this.state;
  2961. if (type === 126 || tokenIsFlowInterfaceOrTypeOrOpaque(type)) {
  2962. return !this.state.containsEsc;
  2963. }
  2964. return super.shouldParseExportDeclaration();
  2965. }
  2966. isExportDefaultSpecifier() {
  2967. const {
  2968. type
  2969. } = this.state;
  2970. if (type === 126 || tokenIsFlowInterfaceOrTypeOrOpaque(type)) {
  2971. return this.state.containsEsc;
  2972. }
  2973. return super.isExportDefaultSpecifier();
  2974. }
  2975. parseExportDefaultExpression() {
  2976. if (this.isContextual(126)) {
  2977. const node = this.startNode();
  2978. this.next();
  2979. return this.flowParseEnumDeclaration(node);
  2980. }
  2981. return super.parseExportDefaultExpression();
  2982. }
  2983. parseConditional(expr, startLoc, refExpressionErrors) {
  2984. if (!this.match(17)) return expr;
  2985. if (this.state.maybeInArrowParameters) {
  2986. const nextCh = this.lookaheadCharCode();
  2987. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  2988. this.setOptionalParametersError(refExpressionErrors);
  2989. return expr;
  2990. }
  2991. }
  2992. this.expect(17);
  2993. const state = this.state.clone();
  2994. const originalNoArrowAt = this.state.noArrowAt;
  2995. const node = this.startNodeAt(startLoc);
  2996. let {
  2997. consequent,
  2998. failed
  2999. } = this.tryParseConditionalConsequent();
  3000. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  3001. if (failed || invalid.length > 0) {
  3002. const noArrowAt = [...originalNoArrowAt];
  3003. if (invalid.length > 0) {
  3004. this.state = state;
  3005. this.state.noArrowAt = noArrowAt;
  3006. for (let i = 0; i < invalid.length; i++) {
  3007. noArrowAt.push(invalid[i].start);
  3008. }
  3009. ({
  3010. consequent,
  3011. failed
  3012. } = this.tryParseConditionalConsequent());
  3013. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  3014. }
  3015. if (failed && valid.length > 1) {
  3016. this.raise(FlowErrors.AmbiguousConditionalArrow, state.startLoc);
  3017. }
  3018. if (failed && valid.length === 1) {
  3019. this.state = state;
  3020. noArrowAt.push(valid[0].start);
  3021. this.state.noArrowAt = noArrowAt;
  3022. ({
  3023. consequent,
  3024. failed
  3025. } = this.tryParseConditionalConsequent());
  3026. }
  3027. }
  3028. this.getArrowLikeExpressions(consequent, true);
  3029. this.state.noArrowAt = originalNoArrowAt;
  3030. this.expect(14);
  3031. node.test = expr;
  3032. node.consequent = consequent;
  3033. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  3034. return this.finishNode(node, "ConditionalExpression");
  3035. }
  3036. tryParseConditionalConsequent() {
  3037. this.state.noArrowParamsConversionAt.push(this.state.start);
  3038. const consequent = this.parseMaybeAssignAllowIn();
  3039. const failed = !this.match(14);
  3040. this.state.noArrowParamsConversionAt.pop();
  3041. return {
  3042. consequent,
  3043. failed
  3044. };
  3045. }
  3046. getArrowLikeExpressions(node, disallowInvalid) {
  3047. const stack = [node];
  3048. const arrows = [];
  3049. while (stack.length !== 0) {
  3050. const node = stack.pop();
  3051. if (node.type === "ArrowFunctionExpression" && node.body.type !== "BlockStatement") {
  3052. if (node.typeParameters || !node.returnType) {
  3053. this.finishArrowValidation(node);
  3054. } else {
  3055. arrows.push(node);
  3056. }
  3057. stack.push(node.body);
  3058. } else if (node.type === "ConditionalExpression") {
  3059. stack.push(node.consequent);
  3060. stack.push(node.alternate);
  3061. }
  3062. }
  3063. if (disallowInvalid) {
  3064. arrows.forEach(node => this.finishArrowValidation(node));
  3065. return [arrows, []];
  3066. }
  3067. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  3068. }
  3069. finishArrowValidation(node) {
  3070. var _node$extra;
  3071. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingCommaLoc, false);
  3072. this.scope.enter(514 | 4);
  3073. super.checkParams(node, false, true);
  3074. this.scope.exit();
  3075. }
  3076. forwardNoArrowParamsConversionAt(node, parse) {
  3077. let result;
  3078. if (this.state.noArrowParamsConversionAt.includes(this.offsetToSourcePos(node.start))) {
  3079. this.state.noArrowParamsConversionAt.push(this.state.start);
  3080. result = parse();
  3081. this.state.noArrowParamsConversionAt.pop();
  3082. } else {
  3083. result = parse();
  3084. }
  3085. return result;
  3086. }
  3087. parseParenItem(node, startLoc) {
  3088. const newNode = super.parseParenItem(node, startLoc);
  3089. if (this.eat(17)) {
  3090. newNode.optional = true;
  3091. this.resetEndLocation(node);
  3092. }
  3093. if (this.match(14)) {
  3094. const typeCastNode = this.startNodeAt(startLoc);
  3095. typeCastNode.expression = newNode;
  3096. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  3097. return this.finishNode(typeCastNode, "TypeCastExpression");
  3098. }
  3099. return newNode;
  3100. }
  3101. assertModuleNodeAllowed(node) {
  3102. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  3103. return;
  3104. }
  3105. super.assertModuleNodeAllowed(node);
  3106. }
  3107. parseExportDeclaration(node) {
  3108. if (this.isContextual(130)) {
  3109. node.exportKind = "type";
  3110. const declarationNode = this.startNode();
  3111. this.next();
  3112. if (this.match(5)) {
  3113. node.specifiers = this.parseExportSpecifiers(true);
  3114. super.parseExportFrom(node);
  3115. return null;
  3116. } else {
  3117. return this.flowParseTypeAlias(declarationNode);
  3118. }
  3119. } else if (this.isContextual(131)) {
  3120. node.exportKind = "type";
  3121. const declarationNode = this.startNode();
  3122. this.next();
  3123. return this.flowParseOpaqueType(declarationNode, false);
  3124. } else if (this.isContextual(129)) {
  3125. node.exportKind = "type";
  3126. const declarationNode = this.startNode();
  3127. this.next();
  3128. return this.flowParseInterface(declarationNode);
  3129. } else if (this.isContextual(126)) {
  3130. node.exportKind = "value";
  3131. const declarationNode = this.startNode();
  3132. this.next();
  3133. return this.flowParseEnumDeclaration(declarationNode);
  3134. } else {
  3135. return super.parseExportDeclaration(node);
  3136. }
  3137. }
  3138. eatExportStar(node) {
  3139. if (super.eatExportStar(node)) return true;
  3140. if (this.isContextual(130) && this.lookahead().type === 55) {
  3141. node.exportKind = "type";
  3142. this.next();
  3143. this.next();
  3144. return true;
  3145. }
  3146. return false;
  3147. }
  3148. maybeParseExportNamespaceSpecifier(node) {
  3149. const {
  3150. startLoc
  3151. } = this.state;
  3152. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  3153. if (hasNamespace && node.exportKind === "type") {
  3154. this.unexpected(startLoc);
  3155. }
  3156. return hasNamespace;
  3157. }
  3158. parseClassId(node, isStatement, optionalId) {
  3159. super.parseClassId(node, isStatement, optionalId);
  3160. if (this.match(47)) {
  3161. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3162. }
  3163. }
  3164. parseClassMember(classBody, member, state) {
  3165. const {
  3166. startLoc
  3167. } = this.state;
  3168. if (this.isContextual(125)) {
  3169. if (super.parseClassMemberFromModifier(classBody, member)) {
  3170. return;
  3171. }
  3172. member.declare = true;
  3173. }
  3174. super.parseClassMember(classBody, member, state);
  3175. if (member.declare) {
  3176. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  3177. this.raise(FlowErrors.DeclareClassElement, startLoc);
  3178. } else if (member.value) {
  3179. this.raise(FlowErrors.DeclareClassFieldInitializer, member.value);
  3180. }
  3181. }
  3182. }
  3183. isIterator(word) {
  3184. return word === "iterator" || word === "asyncIterator";
  3185. }
  3186. readIterator() {
  3187. const word = super.readWord1();
  3188. const fullWord = "@@" + word;
  3189. if (!this.isIterator(word) || !this.state.inType) {
  3190. this.raise(Errors.InvalidIdentifier, this.state.curPosition(), {
  3191. identifierName: fullWord
  3192. });
  3193. }
  3194. this.finishToken(132, fullWord);
  3195. }
  3196. getTokenFromCode(code) {
  3197. const next = this.input.charCodeAt(this.state.pos + 1);
  3198. if (code === 123 && next === 124) {
  3199. this.finishOp(6, 2);
  3200. } else if (this.state.inType && (code === 62 || code === 60)) {
  3201. this.finishOp(code === 62 ? 48 : 47, 1);
  3202. } else if (this.state.inType && code === 63) {
  3203. if (next === 46) {
  3204. this.finishOp(18, 2);
  3205. } else {
  3206. this.finishOp(17, 1);
  3207. }
  3208. } else if (isIteratorStart(code, next, this.input.charCodeAt(this.state.pos + 2))) {
  3209. this.state.pos += 2;
  3210. this.readIterator();
  3211. } else {
  3212. super.getTokenFromCode(code);
  3213. }
  3214. }
  3215. isAssignable(node, isBinding) {
  3216. if (node.type === "TypeCastExpression") {
  3217. return this.isAssignable(node.expression, isBinding);
  3218. } else {
  3219. return super.isAssignable(node, isBinding);
  3220. }
  3221. }
  3222. toAssignable(node, isLHS = false) {
  3223. if (!isLHS && node.type === "AssignmentExpression" && node.left.type === "TypeCastExpression") {
  3224. node.left = this.typeCastToParameter(node.left);
  3225. }
  3226. super.toAssignable(node, isLHS);
  3227. }
  3228. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  3229. for (let i = 0; i < exprList.length; i++) {
  3230. const expr = exprList[i];
  3231. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  3232. exprList[i] = this.typeCastToParameter(expr);
  3233. }
  3234. }
  3235. super.toAssignableList(exprList, trailingCommaLoc, isLHS);
  3236. }
  3237. toReferencedList(exprList, isParenthesizedExpr) {
  3238. for (let i = 0; i < exprList.length; i++) {
  3239. var _expr$extra;
  3240. const expr = exprList[i];
  3241. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  3242. this.raise(FlowErrors.TypeCastInPattern, expr.typeAnnotation);
  3243. }
  3244. }
  3245. return exprList;
  3246. }
  3247. parseArrayLike(close, isTuple, refExpressionErrors) {
  3248. const node = super.parseArrayLike(close, isTuple, refExpressionErrors);
  3249. if (refExpressionErrors != null && !this.state.maybeInArrowParameters) {
  3250. this.toReferencedList(node.elements);
  3251. }
  3252. return node;
  3253. }
  3254. isValidLVal(type, disallowCallExpression, isParenthesized, binding) {
  3255. return type === "TypeCastExpression" || super.isValidLVal(type, disallowCallExpression, isParenthesized, binding);
  3256. }
  3257. parseClassProperty(node) {
  3258. if (this.match(14)) {
  3259. node.typeAnnotation = this.flowParseTypeAnnotation();
  3260. }
  3261. return super.parseClassProperty(node);
  3262. }
  3263. parseClassPrivateProperty(node) {
  3264. if (this.match(14)) {
  3265. node.typeAnnotation = this.flowParseTypeAnnotation();
  3266. }
  3267. return super.parseClassPrivateProperty(node);
  3268. }
  3269. isClassMethod() {
  3270. return this.match(47) || super.isClassMethod();
  3271. }
  3272. isClassProperty() {
  3273. return this.match(14) || super.isClassProperty();
  3274. }
  3275. isNonstaticConstructor(method) {
  3276. return !this.match(14) && super.isNonstaticConstructor(method);
  3277. }
  3278. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  3279. if (method.variance) {
  3280. this.unexpected(method.variance.loc.start);
  3281. }
  3282. delete method.variance;
  3283. if (this.match(47)) {
  3284. method.typeParameters = this.flowParseTypeParameterDeclaration();
  3285. }
  3286. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  3287. if (method.params && isConstructor) {
  3288. const params = method.params;
  3289. if (params.length > 0 && this.isThisParam(params[0])) {
  3290. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  3291. }
  3292. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  3293. const params = method.value.params;
  3294. if (params.length > 0 && this.isThisParam(params[0])) {
  3295. this.raise(FlowErrors.ThisParamBannedInConstructor, method);
  3296. }
  3297. }
  3298. }
  3299. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  3300. if (method.variance) {
  3301. this.unexpected(method.variance.loc.start);
  3302. }
  3303. delete method.variance;
  3304. if (this.match(47)) {
  3305. method.typeParameters = this.flowParseTypeParameterDeclaration();
  3306. }
  3307. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  3308. }
  3309. parseClassSuper(node) {
  3310. super.parseClassSuper(node);
  3311. if (node.superClass && (this.match(47) || this.match(51))) {
  3312. node.superTypeParameters = this.flowParseTypeParameterInstantiationInExpression();
  3313. }
  3314. if (this.isContextual(113)) {
  3315. this.next();
  3316. const implemented = node.implements = [];
  3317. do {
  3318. const node = this.startNode();
  3319. node.id = this.flowParseRestrictedIdentifier(true);
  3320. if (this.match(47)) {
  3321. node.typeParameters = this.flowParseTypeParameterInstantiation();
  3322. } else {
  3323. node.typeParameters = null;
  3324. }
  3325. implemented.push(this.finishNode(node, "ClassImplements"));
  3326. } while (this.eat(12));
  3327. }
  3328. }
  3329. checkGetterSetterParams(method) {
  3330. super.checkGetterSetterParams(method);
  3331. const params = this.getObjectOrClassMethodParams(method);
  3332. if (params.length > 0) {
  3333. const param = params[0];
  3334. if (this.isThisParam(param) && method.kind === "get") {
  3335. this.raise(FlowErrors.GetterMayNotHaveThisParam, param);
  3336. } else if (this.isThisParam(param)) {
  3337. this.raise(FlowErrors.SetterMayNotHaveThisParam, param);
  3338. }
  3339. }
  3340. }
  3341. parsePropertyNamePrefixOperator(node) {
  3342. node.variance = this.flowParseVariance();
  3343. }
  3344. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  3345. if (prop.variance) {
  3346. this.unexpected(prop.variance.loc.start);
  3347. }
  3348. delete prop.variance;
  3349. let typeParameters;
  3350. if (this.match(47) && !isAccessor) {
  3351. typeParameters = this.flowParseTypeParameterDeclaration();
  3352. if (!this.match(10)) this.unexpected();
  3353. }
  3354. const result = super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  3355. if (typeParameters) {
  3356. (result.value || result).typeParameters = typeParameters;
  3357. }
  3358. return result;
  3359. }
  3360. parseFunctionParamType(param) {
  3361. if (this.eat(17)) {
  3362. if (param.type !== "Identifier") {
  3363. this.raise(FlowErrors.PatternIsOptional, param);
  3364. }
  3365. if (this.isThisParam(param)) {
  3366. this.raise(FlowErrors.ThisParamMayNotBeOptional, param);
  3367. }
  3368. param.optional = true;
  3369. }
  3370. if (this.match(14)) {
  3371. param.typeAnnotation = this.flowParseTypeAnnotation();
  3372. } else if (this.isThisParam(param)) {
  3373. this.raise(FlowErrors.ThisParamAnnotationRequired, param);
  3374. }
  3375. if (this.match(29) && this.isThisParam(param)) {
  3376. this.raise(FlowErrors.ThisParamNoDefault, param);
  3377. }
  3378. this.resetEndLocation(param);
  3379. return param;
  3380. }
  3381. parseMaybeDefault(startLoc, left) {
  3382. const node = super.parseMaybeDefault(startLoc, left);
  3383. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  3384. this.raise(FlowErrors.TypeBeforeInitializer, node.typeAnnotation);
  3385. }
  3386. return node;
  3387. }
  3388. checkImportReflection(node) {
  3389. super.checkImportReflection(node);
  3390. if (node.module && node.importKind !== "value") {
  3391. this.raise(FlowErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  3392. }
  3393. }
  3394. parseImportSpecifierLocal(node, specifier, type) {
  3395. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  3396. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  3397. }
  3398. isPotentialImportPhase(isExport) {
  3399. if (super.isPotentialImportPhase(isExport)) return true;
  3400. if (this.isContextual(130)) {
  3401. if (!isExport) return true;
  3402. const ch = this.lookaheadCharCode();
  3403. return ch === 123 || ch === 42;
  3404. }
  3405. return !isExport && this.isContextual(87);
  3406. }
  3407. applyImportPhase(node, isExport, phase, loc) {
  3408. super.applyImportPhase(node, isExport, phase, loc);
  3409. if (isExport) {
  3410. if (!phase && this.match(65)) {
  3411. return;
  3412. }
  3413. node.exportKind = phase === "type" ? phase : "value";
  3414. } else {
  3415. if (phase === "type" && this.match(55)) this.unexpected();
  3416. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  3417. }
  3418. }
  3419. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  3420. const firstIdent = specifier.imported;
  3421. let specifierTypeKind = null;
  3422. if (firstIdent.type === "Identifier") {
  3423. if (firstIdent.name === "type") {
  3424. specifierTypeKind = "type";
  3425. } else if (firstIdent.name === "typeof") {
  3426. specifierTypeKind = "typeof";
  3427. }
  3428. }
  3429. let isBinding = false;
  3430. if (this.isContextual(93) && !this.isLookaheadContextual("as")) {
  3431. const as_ident = this.parseIdentifier(true);
  3432. if (specifierTypeKind !== null && !tokenIsKeywordOrIdentifier(this.state.type)) {
  3433. specifier.imported = as_ident;
  3434. specifier.importKind = specifierTypeKind;
  3435. specifier.local = this.cloneIdentifier(as_ident);
  3436. } else {
  3437. specifier.imported = firstIdent;
  3438. specifier.importKind = null;
  3439. specifier.local = this.parseIdentifier();
  3440. }
  3441. } else {
  3442. if (specifierTypeKind !== null && tokenIsKeywordOrIdentifier(this.state.type)) {
  3443. specifier.imported = this.parseIdentifier(true);
  3444. specifier.importKind = specifierTypeKind;
  3445. } else {
  3446. if (importedIsString) {
  3447. throw this.raise(Errors.ImportBindingIsString, specifier, {
  3448. importName: firstIdent.value
  3449. });
  3450. }
  3451. specifier.imported = firstIdent;
  3452. specifier.importKind = null;
  3453. }
  3454. if (this.eatContextual(93)) {
  3455. specifier.local = this.parseIdentifier();
  3456. } else {
  3457. isBinding = true;
  3458. specifier.local = this.cloneIdentifier(specifier.imported);
  3459. }
  3460. }
  3461. const specifierIsTypeImport = hasTypeImportKind(specifier);
  3462. if (isInTypeOnlyImport && specifierIsTypeImport) {
  3463. this.raise(FlowErrors.ImportTypeShorthandOnlyInPureImport, specifier);
  3464. }
  3465. if (isInTypeOnlyImport || specifierIsTypeImport) {
  3466. this.checkReservedType(specifier.local.name, specifier.local.loc.start, true);
  3467. }
  3468. if (isBinding && !isInTypeOnlyImport && !specifierIsTypeImport) {
  3469. this.checkReservedWord(specifier.local.name, specifier.loc.start, true, true);
  3470. }
  3471. return this.finishImportSpecifier(specifier, "ImportSpecifier");
  3472. }
  3473. parseBindingAtom() {
  3474. switch (this.state.type) {
  3475. case 78:
  3476. return this.parseIdentifier(true);
  3477. default:
  3478. return super.parseBindingAtom();
  3479. }
  3480. }
  3481. parseFunctionParams(node, isConstructor) {
  3482. const kind = node.kind;
  3483. if (kind !== "get" && kind !== "set" && this.match(47)) {
  3484. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3485. }
  3486. super.parseFunctionParams(node, isConstructor);
  3487. }
  3488. parseVarId(decl, kind) {
  3489. super.parseVarId(decl, kind);
  3490. if (this.match(14)) {
  3491. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  3492. this.resetEndLocation(decl.id);
  3493. }
  3494. }
  3495. parseAsyncArrowFromCallExpression(node, call) {
  3496. if (this.match(14)) {
  3497. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3498. this.state.noAnonFunctionType = true;
  3499. node.returnType = this.flowParseTypeAnnotation();
  3500. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3501. }
  3502. return super.parseAsyncArrowFromCallExpression(node, call);
  3503. }
  3504. shouldParseAsyncArrow() {
  3505. return this.match(14) || super.shouldParseAsyncArrow();
  3506. }
  3507. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  3508. var _jsx;
  3509. let state = null;
  3510. let jsx;
  3511. if (this.hasPlugin("jsx") && (this.match(143) || this.match(47))) {
  3512. state = this.state.clone();
  3513. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  3514. if (!jsx.error) return jsx.node;
  3515. const {
  3516. context
  3517. } = this.state;
  3518. const currentContext = context[context.length - 1];
  3519. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  3520. context.pop();
  3521. }
  3522. }
  3523. if ((_jsx = jsx) != null && _jsx.error || this.match(47)) {
  3524. var _jsx2, _jsx3;
  3525. state = state || this.state.clone();
  3526. let typeParameters;
  3527. const arrow = this.tryParse(abort => {
  3528. var _arrowExpression$extr;
  3529. typeParameters = this.flowParseTypeParameterDeclaration();
  3530. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  3531. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  3532. this.resetStartLocationFromNode(result, typeParameters);
  3533. return result;
  3534. });
  3535. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  3536. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  3537. if (expr.type !== "ArrowFunctionExpression") abort();
  3538. expr.typeParameters = typeParameters;
  3539. this.resetStartLocationFromNode(expr, typeParameters);
  3540. return arrowExpression;
  3541. }, state);
  3542. let arrowExpression = null;
  3543. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  3544. if (!arrow.error && !arrow.aborted) {
  3545. if (arrow.node.async) {
  3546. this.raise(FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction, typeParameters);
  3547. }
  3548. return arrow.node;
  3549. }
  3550. arrowExpression = arrow.node;
  3551. }
  3552. if ((_jsx2 = jsx) != null && _jsx2.node) {
  3553. this.state = jsx.failState;
  3554. return jsx.node;
  3555. }
  3556. if (arrowExpression) {
  3557. this.state = arrow.failState;
  3558. return arrowExpression;
  3559. }
  3560. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  3561. if (arrow.thrown) throw arrow.error;
  3562. throw this.raise(FlowErrors.UnexpectedTokenAfterTypeParameter, typeParameters);
  3563. }
  3564. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  3565. }
  3566. parseArrow(node) {
  3567. if (this.match(14)) {
  3568. const result = this.tryParse(() => {
  3569. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3570. this.state.noAnonFunctionType = true;
  3571. const typeNode = this.startNode();
  3572. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3573. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3574. if (this.canInsertSemicolon()) this.unexpected();
  3575. if (!this.match(19)) this.unexpected();
  3576. return typeNode;
  3577. });
  3578. if (result.thrown) return null;
  3579. if (result.error) this.state = result.failState;
  3580. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  3581. }
  3582. return super.parseArrow(node);
  3583. }
  3584. shouldParseArrow(params) {
  3585. return this.match(14) || super.shouldParseArrow(params);
  3586. }
  3587. setArrowFunctionParameters(node, params) {
  3588. if (this.state.noArrowParamsConversionAt.includes(this.offsetToSourcePos(node.start))) {
  3589. node.params = params;
  3590. } else {
  3591. super.setArrowFunctionParameters(node, params);
  3592. }
  3593. }
  3594. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  3595. if (isArrowFunction && this.state.noArrowParamsConversionAt.includes(this.offsetToSourcePos(node.start))) {
  3596. return;
  3597. }
  3598. for (let i = 0; i < node.params.length; i++) {
  3599. if (this.isThisParam(node.params[i]) && i > 0) {
  3600. this.raise(FlowErrors.ThisParamMustBeFirst, node.params[i]);
  3601. }
  3602. }
  3603. super.checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged);
  3604. }
  3605. parseParenAndDistinguishExpression(canBeArrow) {
  3606. return super.parseParenAndDistinguishExpression(canBeArrow && !this.state.noArrowAt.includes(this.sourceToOffsetPos(this.state.start)));
  3607. }
  3608. parseSubscripts(base, startLoc, noCalls) {
  3609. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.includes(startLoc.index)) {
  3610. this.next();
  3611. const node = this.startNodeAt(startLoc);
  3612. node.callee = base;
  3613. node.arguments = super.parseCallExpressionArguments();
  3614. base = this.finishNode(node, "CallExpression");
  3615. } else if (base.type === "Identifier" && base.name === "async" && this.match(47)) {
  3616. const state = this.state.clone();
  3617. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state);
  3618. if (!arrow.error && !arrow.aborted) return arrow.node;
  3619. const result = this.tryParse(() => super.parseSubscripts(base, startLoc, noCalls), state);
  3620. if (result.node && !result.error) return result.node;
  3621. if (arrow.node) {
  3622. this.state = arrow.failState;
  3623. return arrow.node;
  3624. }
  3625. if (result.node) {
  3626. this.state = result.failState;
  3627. return result.node;
  3628. }
  3629. throw arrow.error || result.error;
  3630. }
  3631. return super.parseSubscripts(base, startLoc, noCalls);
  3632. }
  3633. parseSubscript(base, startLoc, noCalls, subscriptState) {
  3634. if (this.match(18) && this.isLookaheadToken_lt()) {
  3635. subscriptState.optionalChainMember = true;
  3636. if (noCalls) {
  3637. subscriptState.stop = true;
  3638. return base;
  3639. }
  3640. this.next();
  3641. const node = this.startNodeAt(startLoc);
  3642. node.callee = base;
  3643. node.typeArguments = this.flowParseTypeParameterInstantiationInExpression();
  3644. this.expect(10);
  3645. node.arguments = this.parseCallExpressionArguments();
  3646. node.optional = true;
  3647. return this.finishCallExpression(node, true);
  3648. } else if (!noCalls && this.shouldParseTypes() && (this.match(47) || this.match(51))) {
  3649. const node = this.startNodeAt(startLoc);
  3650. node.callee = base;
  3651. const result = this.tryParse(() => {
  3652. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  3653. this.expect(10);
  3654. node.arguments = super.parseCallExpressionArguments();
  3655. if (subscriptState.optionalChainMember) {
  3656. node.optional = false;
  3657. }
  3658. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  3659. });
  3660. if (result.node) {
  3661. if (result.error) this.state = result.failState;
  3662. return result.node;
  3663. }
  3664. }
  3665. return super.parseSubscript(base, startLoc, noCalls, subscriptState);
  3666. }
  3667. parseNewCallee(node) {
  3668. super.parseNewCallee(node);
  3669. let targs = null;
  3670. if (this.shouldParseTypes() && this.match(47)) {
  3671. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  3672. }
  3673. node.typeArguments = targs;
  3674. }
  3675. parseAsyncArrowWithTypeParameters(startLoc) {
  3676. const node = this.startNodeAt(startLoc);
  3677. this.parseFunctionParams(node, false);
  3678. if (!this.parseArrow(node)) return;
  3679. return super.parseArrowExpression(node, undefined, true);
  3680. }
  3681. readToken_mult_modulo(code) {
  3682. const next = this.input.charCodeAt(this.state.pos + 1);
  3683. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  3684. this.state.hasFlowComment = false;
  3685. this.state.pos += 2;
  3686. this.nextToken();
  3687. return;
  3688. }
  3689. super.readToken_mult_modulo(code);
  3690. }
  3691. readToken_pipe_amp(code) {
  3692. const next = this.input.charCodeAt(this.state.pos + 1);
  3693. if (code === 124 && next === 125) {
  3694. this.finishOp(9, 2);
  3695. return;
  3696. }
  3697. super.readToken_pipe_amp(code);
  3698. }
  3699. parseTopLevel(file, program) {
  3700. const fileNode = super.parseTopLevel(file, program);
  3701. if (this.state.hasFlowComment) {
  3702. this.raise(FlowErrors.UnterminatedFlowComment, this.state.curPosition());
  3703. }
  3704. return fileNode;
  3705. }
  3706. skipBlockComment() {
  3707. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  3708. if (this.state.hasFlowComment) {
  3709. throw this.raise(FlowErrors.NestedFlowComment, this.state.startLoc);
  3710. }
  3711. this.hasFlowCommentCompletion();
  3712. const commentSkip = this.skipFlowComment();
  3713. if (commentSkip) {
  3714. this.state.pos += commentSkip;
  3715. this.state.hasFlowComment = true;
  3716. }
  3717. return;
  3718. }
  3719. return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
  3720. }
  3721. skipFlowComment() {
  3722. const {
  3723. pos
  3724. } = this.state;
  3725. let shiftToFirstNonWhiteSpace = 2;
  3726. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  3727. shiftToFirstNonWhiteSpace++;
  3728. }
  3729. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  3730. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  3731. if (ch2 === 58 && ch3 === 58) {
  3732. return shiftToFirstNonWhiteSpace + 2;
  3733. }
  3734. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  3735. return shiftToFirstNonWhiteSpace + 12;
  3736. }
  3737. if (ch2 === 58 && ch3 !== 58) {
  3738. return shiftToFirstNonWhiteSpace;
  3739. }
  3740. return false;
  3741. }
  3742. hasFlowCommentCompletion() {
  3743. const end = this.input.indexOf("*/", this.state.pos);
  3744. if (end === -1) {
  3745. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  3746. }
  3747. }
  3748. flowEnumErrorBooleanMemberNotInitialized(loc, {
  3749. enumName,
  3750. memberName
  3751. }) {
  3752. this.raise(FlowErrors.EnumBooleanMemberNotInitialized, loc, {
  3753. memberName,
  3754. enumName
  3755. });
  3756. }
  3757. flowEnumErrorInvalidMemberInitializer(loc, enumContext) {
  3758. return this.raise(!enumContext.explicitType ? FlowErrors.EnumInvalidMemberInitializerUnknownType : enumContext.explicitType === "symbol" ? FlowErrors.EnumInvalidMemberInitializerSymbolType : FlowErrors.EnumInvalidMemberInitializerPrimaryType, loc, enumContext);
  3759. }
  3760. flowEnumErrorNumberMemberNotInitialized(loc, details) {
  3761. this.raise(FlowErrors.EnumNumberMemberNotInitialized, loc, details);
  3762. }
  3763. flowEnumErrorStringMemberInconsistentlyInitialized(node, details) {
  3764. this.raise(FlowErrors.EnumStringMemberInconsistentlyInitialized, node, details);
  3765. }
  3766. flowEnumMemberInit() {
  3767. const startLoc = this.state.startLoc;
  3768. const endOfInit = () => this.match(12) || this.match(8);
  3769. switch (this.state.type) {
  3770. case 135:
  3771. {
  3772. const literal = this.parseNumericLiteral(this.state.value);
  3773. if (endOfInit()) {
  3774. return {
  3775. type: "number",
  3776. loc: literal.loc.start,
  3777. value: literal
  3778. };
  3779. }
  3780. return {
  3781. type: "invalid",
  3782. loc: startLoc
  3783. };
  3784. }
  3785. case 134:
  3786. {
  3787. const literal = this.parseStringLiteral(this.state.value);
  3788. if (endOfInit()) {
  3789. return {
  3790. type: "string",
  3791. loc: literal.loc.start,
  3792. value: literal
  3793. };
  3794. }
  3795. return {
  3796. type: "invalid",
  3797. loc: startLoc
  3798. };
  3799. }
  3800. case 85:
  3801. case 86:
  3802. {
  3803. const literal = this.parseBooleanLiteral(this.match(85));
  3804. if (endOfInit()) {
  3805. return {
  3806. type: "boolean",
  3807. loc: literal.loc.start,
  3808. value: literal
  3809. };
  3810. }
  3811. return {
  3812. type: "invalid",
  3813. loc: startLoc
  3814. };
  3815. }
  3816. default:
  3817. return {
  3818. type: "invalid",
  3819. loc: startLoc
  3820. };
  3821. }
  3822. }
  3823. flowEnumMemberRaw() {
  3824. const loc = this.state.startLoc;
  3825. const id = this.parseIdentifier(true);
  3826. const init = this.eat(29) ? this.flowEnumMemberInit() : {
  3827. type: "none",
  3828. loc
  3829. };
  3830. return {
  3831. id,
  3832. init
  3833. };
  3834. }
  3835. flowEnumCheckExplicitTypeMismatch(loc, context, expectedType) {
  3836. const {
  3837. explicitType
  3838. } = context;
  3839. if (explicitType === null) {
  3840. return;
  3841. }
  3842. if (explicitType !== expectedType) {
  3843. this.flowEnumErrorInvalidMemberInitializer(loc, context);
  3844. }
  3845. }
  3846. flowEnumMembers({
  3847. enumName,
  3848. explicitType
  3849. }) {
  3850. const seenNames = new Set();
  3851. const members = {
  3852. booleanMembers: [],
  3853. numberMembers: [],
  3854. stringMembers: [],
  3855. defaultedMembers: []
  3856. };
  3857. let hasUnknownMembers = false;
  3858. while (!this.match(8)) {
  3859. if (this.eat(21)) {
  3860. hasUnknownMembers = true;
  3861. break;
  3862. }
  3863. const memberNode = this.startNode();
  3864. const {
  3865. id,
  3866. init
  3867. } = this.flowEnumMemberRaw();
  3868. const memberName = id.name;
  3869. if (memberName === "") {
  3870. continue;
  3871. }
  3872. if (/^[a-z]/.test(memberName)) {
  3873. this.raise(FlowErrors.EnumInvalidMemberName, id, {
  3874. memberName,
  3875. suggestion: memberName[0].toUpperCase() + memberName.slice(1),
  3876. enumName
  3877. });
  3878. }
  3879. if (seenNames.has(memberName)) {
  3880. this.raise(FlowErrors.EnumDuplicateMemberName, id, {
  3881. memberName,
  3882. enumName
  3883. });
  3884. }
  3885. seenNames.add(memberName);
  3886. const context = {
  3887. enumName,
  3888. explicitType,
  3889. memberName
  3890. };
  3891. memberNode.id = id;
  3892. switch (init.type) {
  3893. case "boolean":
  3894. {
  3895. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "boolean");
  3896. memberNode.init = init.value;
  3897. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  3898. break;
  3899. }
  3900. case "number":
  3901. {
  3902. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "number");
  3903. memberNode.init = init.value;
  3904. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  3905. break;
  3906. }
  3907. case "string":
  3908. {
  3909. this.flowEnumCheckExplicitTypeMismatch(init.loc, context, "string");
  3910. memberNode.init = init.value;
  3911. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  3912. break;
  3913. }
  3914. case "invalid":
  3915. {
  3916. throw this.flowEnumErrorInvalidMemberInitializer(init.loc, context);
  3917. }
  3918. case "none":
  3919. {
  3920. switch (explicitType) {
  3921. case "boolean":
  3922. this.flowEnumErrorBooleanMemberNotInitialized(init.loc, context);
  3923. break;
  3924. case "number":
  3925. this.flowEnumErrorNumberMemberNotInitialized(init.loc, context);
  3926. break;
  3927. default:
  3928. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  3929. }
  3930. }
  3931. }
  3932. if (!this.match(8)) {
  3933. this.expect(12);
  3934. }
  3935. }
  3936. return {
  3937. members,
  3938. hasUnknownMembers
  3939. };
  3940. }
  3941. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  3942. enumName
  3943. }) {
  3944. if (initializedMembers.length === 0) {
  3945. return defaultedMembers;
  3946. } else if (defaultedMembers.length === 0) {
  3947. return initializedMembers;
  3948. } else if (defaultedMembers.length > initializedMembers.length) {
  3949. for (const member of initializedMembers) {
  3950. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  3951. enumName
  3952. });
  3953. }
  3954. return defaultedMembers;
  3955. } else {
  3956. for (const member of defaultedMembers) {
  3957. this.flowEnumErrorStringMemberInconsistentlyInitialized(member, {
  3958. enumName
  3959. });
  3960. }
  3961. return initializedMembers;
  3962. }
  3963. }
  3964. flowEnumParseExplicitType({
  3965. enumName
  3966. }) {
  3967. if (!this.eatContextual(102)) return null;
  3968. if (!tokenIsIdentifier(this.state.type)) {
  3969. throw this.raise(FlowErrors.EnumInvalidExplicitTypeUnknownSupplied, this.state.startLoc, {
  3970. enumName
  3971. });
  3972. }
  3973. const {
  3974. value
  3975. } = this.state;
  3976. this.next();
  3977. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  3978. this.raise(FlowErrors.EnumInvalidExplicitType, this.state.startLoc, {
  3979. enumName,
  3980. invalidEnumType: value
  3981. });
  3982. }
  3983. return value;
  3984. }
  3985. flowEnumBody(node, id) {
  3986. const enumName = id.name;
  3987. const nameLoc = id.loc.start;
  3988. const explicitType = this.flowEnumParseExplicitType({
  3989. enumName
  3990. });
  3991. this.expect(5);
  3992. const {
  3993. members,
  3994. hasUnknownMembers
  3995. } = this.flowEnumMembers({
  3996. enumName,
  3997. explicitType
  3998. });
  3999. node.hasUnknownMembers = hasUnknownMembers;
  4000. switch (explicitType) {
  4001. case "boolean":
  4002. node.explicitType = true;
  4003. node.members = members.booleanMembers;
  4004. this.expect(8);
  4005. return this.finishNode(node, "EnumBooleanBody");
  4006. case "number":
  4007. node.explicitType = true;
  4008. node.members = members.numberMembers;
  4009. this.expect(8);
  4010. return this.finishNode(node, "EnumNumberBody");
  4011. case "string":
  4012. node.explicitType = true;
  4013. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  4014. enumName
  4015. });
  4016. this.expect(8);
  4017. return this.finishNode(node, "EnumStringBody");
  4018. case "symbol":
  4019. node.members = members.defaultedMembers;
  4020. this.expect(8);
  4021. return this.finishNode(node, "EnumSymbolBody");
  4022. default:
  4023. {
  4024. const empty = () => {
  4025. node.members = [];
  4026. this.expect(8);
  4027. return this.finishNode(node, "EnumStringBody");
  4028. };
  4029. node.explicitType = false;
  4030. const boolsLen = members.booleanMembers.length;
  4031. const numsLen = members.numberMembers.length;
  4032. const strsLen = members.stringMembers.length;
  4033. const defaultedLen = members.defaultedMembers.length;
  4034. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  4035. return empty();
  4036. } else if (!boolsLen && !numsLen) {
  4037. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  4038. enumName
  4039. });
  4040. this.expect(8);
  4041. return this.finishNode(node, "EnumStringBody");
  4042. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  4043. for (const member of members.defaultedMembers) {
  4044. this.flowEnumErrorBooleanMemberNotInitialized(member.loc.start, {
  4045. enumName,
  4046. memberName: member.id.name
  4047. });
  4048. }
  4049. node.members = members.booleanMembers;
  4050. this.expect(8);
  4051. return this.finishNode(node, "EnumBooleanBody");
  4052. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  4053. for (const member of members.defaultedMembers) {
  4054. this.flowEnumErrorNumberMemberNotInitialized(member.loc.start, {
  4055. enumName,
  4056. memberName: member.id.name
  4057. });
  4058. }
  4059. node.members = members.numberMembers;
  4060. this.expect(8);
  4061. return this.finishNode(node, "EnumNumberBody");
  4062. } else {
  4063. this.raise(FlowErrors.EnumInconsistentMemberValues, nameLoc, {
  4064. enumName
  4065. });
  4066. return empty();
  4067. }
  4068. }
  4069. }
  4070. }
  4071. flowParseEnumDeclaration(node) {
  4072. const id = this.parseIdentifier();
  4073. node.id = id;
  4074. node.body = this.flowEnumBody(this.startNode(), id);
  4075. return this.finishNode(node, "EnumDeclaration");
  4076. }
  4077. jsxParseOpeningElementAfterName(node) {
  4078. if (this.shouldParseTypes()) {
  4079. if (this.match(47) || this.match(51)) {
  4080. node.typeArguments = this.flowParseTypeParameterInstantiationInExpression();
  4081. }
  4082. }
  4083. return super.jsxParseOpeningElementAfterName(node);
  4084. }
  4085. isLookaheadToken_lt() {
  4086. const next = this.nextTokenStart();
  4087. if (this.input.charCodeAt(next) === 60) {
  4088. const afterNext = this.input.charCodeAt(next + 1);
  4089. return afterNext !== 60 && afterNext !== 61;
  4090. }
  4091. return false;
  4092. }
  4093. reScan_lt_gt() {
  4094. const {
  4095. type
  4096. } = this.state;
  4097. if (type === 47) {
  4098. this.state.pos -= 1;
  4099. this.readToken_lt();
  4100. } else if (type === 48) {
  4101. this.state.pos -= 1;
  4102. this.readToken_gt();
  4103. }
  4104. }
  4105. reScan_lt() {
  4106. const {
  4107. type
  4108. } = this.state;
  4109. if (type === 51) {
  4110. this.state.pos -= 2;
  4111. this.finishOp(47, 1);
  4112. return 47;
  4113. }
  4114. return type;
  4115. }
  4116. maybeUnwrapTypeCastExpression(node) {
  4117. return node.type === "TypeCastExpression" ? node.expression : node;
  4118. }
  4119. };
  4120. const entities = {
  4121. __proto__: null,
  4122. quot: "\u0022",
  4123. amp: "&",
  4124. apos: "\u0027",
  4125. lt: "<",
  4126. gt: ">",
  4127. nbsp: "\u00A0",
  4128. iexcl: "\u00A1",
  4129. cent: "\u00A2",
  4130. pound: "\u00A3",
  4131. curren: "\u00A4",
  4132. yen: "\u00A5",
  4133. brvbar: "\u00A6",
  4134. sect: "\u00A7",
  4135. uml: "\u00A8",
  4136. copy: "\u00A9",
  4137. ordf: "\u00AA",
  4138. laquo: "\u00AB",
  4139. not: "\u00AC",
  4140. shy: "\u00AD",
  4141. reg: "\u00AE",
  4142. macr: "\u00AF",
  4143. deg: "\u00B0",
  4144. plusmn: "\u00B1",
  4145. sup2: "\u00B2",
  4146. sup3: "\u00B3",
  4147. acute: "\u00B4",
  4148. micro: "\u00B5",
  4149. para: "\u00B6",
  4150. middot: "\u00B7",
  4151. cedil: "\u00B8",
  4152. sup1: "\u00B9",
  4153. ordm: "\u00BA",
  4154. raquo: "\u00BB",
  4155. frac14: "\u00BC",
  4156. frac12: "\u00BD",
  4157. frac34: "\u00BE",
  4158. iquest: "\u00BF",
  4159. Agrave: "\u00C0",
  4160. Aacute: "\u00C1",
  4161. Acirc: "\u00C2",
  4162. Atilde: "\u00C3",
  4163. Auml: "\u00C4",
  4164. Aring: "\u00C5",
  4165. AElig: "\u00C6",
  4166. Ccedil: "\u00C7",
  4167. Egrave: "\u00C8",
  4168. Eacute: "\u00C9",
  4169. Ecirc: "\u00CA",
  4170. Euml: "\u00CB",
  4171. Igrave: "\u00CC",
  4172. Iacute: "\u00CD",
  4173. Icirc: "\u00CE",
  4174. Iuml: "\u00CF",
  4175. ETH: "\u00D0",
  4176. Ntilde: "\u00D1",
  4177. Ograve: "\u00D2",
  4178. Oacute: "\u00D3",
  4179. Ocirc: "\u00D4",
  4180. Otilde: "\u00D5",
  4181. Ouml: "\u00D6",
  4182. times: "\u00D7",
  4183. Oslash: "\u00D8",
  4184. Ugrave: "\u00D9",
  4185. Uacute: "\u00DA",
  4186. Ucirc: "\u00DB",
  4187. Uuml: "\u00DC",
  4188. Yacute: "\u00DD",
  4189. THORN: "\u00DE",
  4190. szlig: "\u00DF",
  4191. agrave: "\u00E0",
  4192. aacute: "\u00E1",
  4193. acirc: "\u00E2",
  4194. atilde: "\u00E3",
  4195. auml: "\u00E4",
  4196. aring: "\u00E5",
  4197. aelig: "\u00E6",
  4198. ccedil: "\u00E7",
  4199. egrave: "\u00E8",
  4200. eacute: "\u00E9",
  4201. ecirc: "\u00EA",
  4202. euml: "\u00EB",
  4203. igrave: "\u00EC",
  4204. iacute: "\u00ED",
  4205. icirc: "\u00EE",
  4206. iuml: "\u00EF",
  4207. eth: "\u00F0",
  4208. ntilde: "\u00F1",
  4209. ograve: "\u00F2",
  4210. oacute: "\u00F3",
  4211. ocirc: "\u00F4",
  4212. otilde: "\u00F5",
  4213. ouml: "\u00F6",
  4214. divide: "\u00F7",
  4215. oslash: "\u00F8",
  4216. ugrave: "\u00F9",
  4217. uacute: "\u00FA",
  4218. ucirc: "\u00FB",
  4219. uuml: "\u00FC",
  4220. yacute: "\u00FD",
  4221. thorn: "\u00FE",
  4222. yuml: "\u00FF",
  4223. OElig: "\u0152",
  4224. oelig: "\u0153",
  4225. Scaron: "\u0160",
  4226. scaron: "\u0161",
  4227. Yuml: "\u0178",
  4228. fnof: "\u0192",
  4229. circ: "\u02C6",
  4230. tilde: "\u02DC",
  4231. Alpha: "\u0391",
  4232. Beta: "\u0392",
  4233. Gamma: "\u0393",
  4234. Delta: "\u0394",
  4235. Epsilon: "\u0395",
  4236. Zeta: "\u0396",
  4237. Eta: "\u0397",
  4238. Theta: "\u0398",
  4239. Iota: "\u0399",
  4240. Kappa: "\u039A",
  4241. Lambda: "\u039B",
  4242. Mu: "\u039C",
  4243. Nu: "\u039D",
  4244. Xi: "\u039E",
  4245. Omicron: "\u039F",
  4246. Pi: "\u03A0",
  4247. Rho: "\u03A1",
  4248. Sigma: "\u03A3",
  4249. Tau: "\u03A4",
  4250. Upsilon: "\u03A5",
  4251. Phi: "\u03A6",
  4252. Chi: "\u03A7",
  4253. Psi: "\u03A8",
  4254. Omega: "\u03A9",
  4255. alpha: "\u03B1",
  4256. beta: "\u03B2",
  4257. gamma: "\u03B3",
  4258. delta: "\u03B4",
  4259. epsilon: "\u03B5",
  4260. zeta: "\u03B6",
  4261. eta: "\u03B7",
  4262. theta: "\u03B8",
  4263. iota: "\u03B9",
  4264. kappa: "\u03BA",
  4265. lambda: "\u03BB",
  4266. mu: "\u03BC",
  4267. nu: "\u03BD",
  4268. xi: "\u03BE",
  4269. omicron: "\u03BF",
  4270. pi: "\u03C0",
  4271. rho: "\u03C1",
  4272. sigmaf: "\u03C2",
  4273. sigma: "\u03C3",
  4274. tau: "\u03C4",
  4275. upsilon: "\u03C5",
  4276. phi: "\u03C6",
  4277. chi: "\u03C7",
  4278. psi: "\u03C8",
  4279. omega: "\u03C9",
  4280. thetasym: "\u03D1",
  4281. upsih: "\u03D2",
  4282. piv: "\u03D6",
  4283. ensp: "\u2002",
  4284. emsp: "\u2003",
  4285. thinsp: "\u2009",
  4286. zwnj: "\u200C",
  4287. zwj: "\u200D",
  4288. lrm: "\u200E",
  4289. rlm: "\u200F",
  4290. ndash: "\u2013",
  4291. mdash: "\u2014",
  4292. lsquo: "\u2018",
  4293. rsquo: "\u2019",
  4294. sbquo: "\u201A",
  4295. ldquo: "\u201C",
  4296. rdquo: "\u201D",
  4297. bdquo: "\u201E",
  4298. dagger: "\u2020",
  4299. Dagger: "\u2021",
  4300. bull: "\u2022",
  4301. hellip: "\u2026",
  4302. permil: "\u2030",
  4303. prime: "\u2032",
  4304. Prime: "\u2033",
  4305. lsaquo: "\u2039",
  4306. rsaquo: "\u203A",
  4307. oline: "\u203E",
  4308. frasl: "\u2044",
  4309. euro: "\u20AC",
  4310. image: "\u2111",
  4311. weierp: "\u2118",
  4312. real: "\u211C",
  4313. trade: "\u2122",
  4314. alefsym: "\u2135",
  4315. larr: "\u2190",
  4316. uarr: "\u2191",
  4317. rarr: "\u2192",
  4318. darr: "\u2193",
  4319. harr: "\u2194",
  4320. crarr: "\u21B5",
  4321. lArr: "\u21D0",
  4322. uArr: "\u21D1",
  4323. rArr: "\u21D2",
  4324. dArr: "\u21D3",
  4325. hArr: "\u21D4",
  4326. forall: "\u2200",
  4327. part: "\u2202",
  4328. exist: "\u2203",
  4329. empty: "\u2205",
  4330. nabla: "\u2207",
  4331. isin: "\u2208",
  4332. notin: "\u2209",
  4333. ni: "\u220B",
  4334. prod: "\u220F",
  4335. sum: "\u2211",
  4336. minus: "\u2212",
  4337. lowast: "\u2217",
  4338. radic: "\u221A",
  4339. prop: "\u221D",
  4340. infin: "\u221E",
  4341. ang: "\u2220",
  4342. and: "\u2227",
  4343. or: "\u2228",
  4344. cap: "\u2229",
  4345. cup: "\u222A",
  4346. int: "\u222B",
  4347. there4: "\u2234",
  4348. sim: "\u223C",
  4349. cong: "\u2245",
  4350. asymp: "\u2248",
  4351. ne: "\u2260",
  4352. equiv: "\u2261",
  4353. le: "\u2264",
  4354. ge: "\u2265",
  4355. sub: "\u2282",
  4356. sup: "\u2283",
  4357. nsub: "\u2284",
  4358. sube: "\u2286",
  4359. supe: "\u2287",
  4360. oplus: "\u2295",
  4361. otimes: "\u2297",
  4362. perp: "\u22A5",
  4363. sdot: "\u22C5",
  4364. lceil: "\u2308",
  4365. rceil: "\u2309",
  4366. lfloor: "\u230A",
  4367. rfloor: "\u230B",
  4368. lang: "\u2329",
  4369. rang: "\u232A",
  4370. loz: "\u25CA",
  4371. spades: "\u2660",
  4372. clubs: "\u2663",
  4373. hearts: "\u2665",
  4374. diams: "\u2666"
  4375. };
  4376. const lineBreak = /\r\n|[\r\n\u2028\u2029]/;
  4377. const lineBreakG = new RegExp(lineBreak.source, "g");
  4378. function isNewLine(code) {
  4379. switch (code) {
  4380. case 10:
  4381. case 13:
  4382. case 8232:
  4383. case 8233:
  4384. return true;
  4385. default:
  4386. return false;
  4387. }
  4388. }
  4389. function hasNewLine(input, start, end) {
  4390. for (let i = start; i < end; i++) {
  4391. if (isNewLine(input.charCodeAt(i))) {
  4392. return true;
  4393. }
  4394. }
  4395. return false;
  4396. }
  4397. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  4398. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/g;
  4399. function isWhitespace(code) {
  4400. switch (code) {
  4401. case 0x0009:
  4402. case 0x000b:
  4403. case 0x000c:
  4404. case 32:
  4405. case 160:
  4406. case 5760:
  4407. case 0x2000:
  4408. case 0x2001:
  4409. case 0x2002:
  4410. case 0x2003:
  4411. case 0x2004:
  4412. case 0x2005:
  4413. case 0x2006:
  4414. case 0x2007:
  4415. case 0x2008:
  4416. case 0x2009:
  4417. case 0x200a:
  4418. case 0x202f:
  4419. case 0x205f:
  4420. case 0x3000:
  4421. case 0xfeff:
  4422. return true;
  4423. default:
  4424. return false;
  4425. }
  4426. }
  4427. const JsxErrors = ParseErrorEnum`jsx`({
  4428. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  4429. MissingClosingTagElement: ({
  4430. openingTagName
  4431. }) => `Expected corresponding JSX closing tag for <${openingTagName}>.`,
  4432. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  4433. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  4434. UnexpectedToken: ({
  4435. unexpected,
  4436. HTMLEntity
  4437. }) => `Unexpected token \`${unexpected}\`. Did you mean \`${HTMLEntity}\` or \`{'${unexpected}'}\`?`,
  4438. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  4439. UnterminatedJsxContent: "Unterminated JSX contents.",
  4440. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  4441. });
  4442. function isFragment(object) {
  4443. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  4444. }
  4445. function getQualifiedJSXName(object) {
  4446. if (object.type === "JSXIdentifier") {
  4447. return object.name;
  4448. }
  4449. if (object.type === "JSXNamespacedName") {
  4450. return object.namespace.name + ":" + object.name.name;
  4451. }
  4452. if (object.type === "JSXMemberExpression") {
  4453. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  4454. }
  4455. throw new Error("Node had unexpected type: " + object.type);
  4456. }
  4457. var jsx = superClass => class JSXParserMixin extends superClass {
  4458. jsxReadToken() {
  4459. let out = "";
  4460. let chunkStart = this.state.pos;
  4461. for (;;) {
  4462. if (this.state.pos >= this.length) {
  4463. throw this.raise(JsxErrors.UnterminatedJsxContent, this.state.startLoc);
  4464. }
  4465. const ch = this.input.charCodeAt(this.state.pos);
  4466. switch (ch) {
  4467. case 60:
  4468. case 123:
  4469. if (this.state.pos === this.state.start) {
  4470. if (ch === 60 && this.state.canStartJSXElement) {
  4471. ++this.state.pos;
  4472. this.finishToken(143);
  4473. } else {
  4474. super.getTokenFromCode(ch);
  4475. }
  4476. return;
  4477. }
  4478. out += this.input.slice(chunkStart, this.state.pos);
  4479. this.finishToken(142, out);
  4480. return;
  4481. case 38:
  4482. out += this.input.slice(chunkStart, this.state.pos);
  4483. out += this.jsxReadEntity();
  4484. chunkStart = this.state.pos;
  4485. break;
  4486. case 62:
  4487. case 125:
  4488. default:
  4489. if (isNewLine(ch)) {
  4490. out += this.input.slice(chunkStart, this.state.pos);
  4491. out += this.jsxReadNewLine(true);
  4492. chunkStart = this.state.pos;
  4493. } else {
  4494. ++this.state.pos;
  4495. }
  4496. }
  4497. }
  4498. }
  4499. jsxReadNewLine(normalizeCRLF) {
  4500. const ch = this.input.charCodeAt(this.state.pos);
  4501. let out;
  4502. ++this.state.pos;
  4503. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  4504. ++this.state.pos;
  4505. out = normalizeCRLF ? "\n" : "\r\n";
  4506. } else {
  4507. out = String.fromCharCode(ch);
  4508. }
  4509. ++this.state.curLine;
  4510. this.state.lineStart = this.state.pos;
  4511. return out;
  4512. }
  4513. jsxReadString(quote) {
  4514. let out = "";
  4515. let chunkStart = ++this.state.pos;
  4516. for (;;) {
  4517. if (this.state.pos >= this.length) {
  4518. throw this.raise(Errors.UnterminatedString, this.state.startLoc);
  4519. }
  4520. const ch = this.input.charCodeAt(this.state.pos);
  4521. if (ch === quote) break;
  4522. if (ch === 38) {
  4523. out += this.input.slice(chunkStart, this.state.pos);
  4524. out += this.jsxReadEntity();
  4525. chunkStart = this.state.pos;
  4526. } else if (isNewLine(ch)) {
  4527. out += this.input.slice(chunkStart, this.state.pos);
  4528. out += this.jsxReadNewLine(false);
  4529. chunkStart = this.state.pos;
  4530. } else {
  4531. ++this.state.pos;
  4532. }
  4533. }
  4534. out += this.input.slice(chunkStart, this.state.pos++);
  4535. this.finishToken(134, out);
  4536. }
  4537. jsxReadEntity() {
  4538. const startPos = ++this.state.pos;
  4539. if (this.codePointAtPos(this.state.pos) === 35) {
  4540. ++this.state.pos;
  4541. let radix = 10;
  4542. if (this.codePointAtPos(this.state.pos) === 120) {
  4543. radix = 16;
  4544. ++this.state.pos;
  4545. }
  4546. const codePoint = this.readInt(radix, undefined, false, "bail");
  4547. if (codePoint !== null && this.codePointAtPos(this.state.pos) === 59) {
  4548. ++this.state.pos;
  4549. return String.fromCodePoint(codePoint);
  4550. }
  4551. } else {
  4552. let count = 0;
  4553. let semi = false;
  4554. while (count++ < 10 && this.state.pos < this.length && !(semi = this.codePointAtPos(this.state.pos) === 59)) {
  4555. ++this.state.pos;
  4556. }
  4557. if (semi) {
  4558. const desc = this.input.slice(startPos, this.state.pos);
  4559. const entity = entities[desc];
  4560. ++this.state.pos;
  4561. if (entity) {
  4562. return entity;
  4563. }
  4564. }
  4565. }
  4566. this.state.pos = startPos;
  4567. return "&";
  4568. }
  4569. jsxReadWord() {
  4570. let ch;
  4571. const start = this.state.pos;
  4572. do {
  4573. ch = this.input.charCodeAt(++this.state.pos);
  4574. } while (isIdentifierChar(ch) || ch === 45);
  4575. this.finishToken(141, this.input.slice(start, this.state.pos));
  4576. }
  4577. jsxParseIdentifier() {
  4578. const node = this.startNode();
  4579. if (this.match(141)) {
  4580. node.name = this.state.value;
  4581. } else if (tokenIsKeyword(this.state.type)) {
  4582. node.name = tokenLabelName(this.state.type);
  4583. } else {
  4584. this.unexpected();
  4585. }
  4586. this.next();
  4587. return this.finishNode(node, "JSXIdentifier");
  4588. }
  4589. jsxParseNamespacedName() {
  4590. const startLoc = this.state.startLoc;
  4591. const name = this.jsxParseIdentifier();
  4592. if (!this.eat(14)) return name;
  4593. const node = this.startNodeAt(startLoc);
  4594. node.namespace = name;
  4595. node.name = this.jsxParseIdentifier();
  4596. return this.finishNode(node, "JSXNamespacedName");
  4597. }
  4598. jsxParseElementName() {
  4599. const startLoc = this.state.startLoc;
  4600. let node = this.jsxParseNamespacedName();
  4601. if (node.type === "JSXNamespacedName") {
  4602. return node;
  4603. }
  4604. while (this.eat(16)) {
  4605. const newNode = this.startNodeAt(startLoc);
  4606. newNode.object = node;
  4607. newNode.property = this.jsxParseIdentifier();
  4608. node = this.finishNode(newNode, "JSXMemberExpression");
  4609. }
  4610. return node;
  4611. }
  4612. jsxParseAttributeValue() {
  4613. let node;
  4614. switch (this.state.type) {
  4615. case 5:
  4616. node = this.startNode();
  4617. this.setContext(types.brace);
  4618. this.next();
  4619. node = this.jsxParseExpressionContainer(node, types.j_oTag);
  4620. if (node.expression.type === "JSXEmptyExpression") {
  4621. this.raise(JsxErrors.AttributeIsEmpty, node);
  4622. }
  4623. return node;
  4624. case 143:
  4625. case 134:
  4626. return this.parseExprAtom();
  4627. default:
  4628. throw this.raise(JsxErrors.UnsupportedJsxValue, this.state.startLoc);
  4629. }
  4630. }
  4631. jsxParseEmptyExpression() {
  4632. const node = this.startNodeAt(this.state.lastTokEndLoc);
  4633. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc);
  4634. }
  4635. jsxParseSpreadChild(node) {
  4636. this.next();
  4637. node.expression = this.parseExpression();
  4638. this.setContext(types.j_expr);
  4639. this.state.canStartJSXElement = true;
  4640. this.expect(8);
  4641. return this.finishNode(node, "JSXSpreadChild");
  4642. }
  4643. jsxParseExpressionContainer(node, previousContext) {
  4644. if (this.match(8)) {
  4645. node.expression = this.jsxParseEmptyExpression();
  4646. } else {
  4647. const expression = this.parseExpression();
  4648. node.expression = expression;
  4649. }
  4650. this.setContext(previousContext);
  4651. this.state.canStartJSXElement = true;
  4652. this.expect(8);
  4653. return this.finishNode(node, "JSXExpressionContainer");
  4654. }
  4655. jsxParseAttribute() {
  4656. const node = this.startNode();
  4657. if (this.match(5)) {
  4658. this.setContext(types.brace);
  4659. this.next();
  4660. this.expect(21);
  4661. node.argument = this.parseMaybeAssignAllowIn();
  4662. this.setContext(types.j_oTag);
  4663. this.state.canStartJSXElement = true;
  4664. this.expect(8);
  4665. return this.finishNode(node, "JSXSpreadAttribute");
  4666. }
  4667. node.name = this.jsxParseNamespacedName();
  4668. node.value = this.eat(29) ? this.jsxParseAttributeValue() : null;
  4669. return this.finishNode(node, "JSXAttribute");
  4670. }
  4671. jsxParseOpeningElementAt(startLoc) {
  4672. const node = this.startNodeAt(startLoc);
  4673. if (this.eat(144)) {
  4674. return this.finishNode(node, "JSXOpeningFragment");
  4675. }
  4676. node.name = this.jsxParseElementName();
  4677. return this.jsxParseOpeningElementAfterName(node);
  4678. }
  4679. jsxParseOpeningElementAfterName(node) {
  4680. const attributes = [];
  4681. while (!this.match(56) && !this.match(144)) {
  4682. attributes.push(this.jsxParseAttribute());
  4683. }
  4684. node.attributes = attributes;
  4685. node.selfClosing = this.eat(56);
  4686. this.expect(144);
  4687. return this.finishNode(node, "JSXOpeningElement");
  4688. }
  4689. jsxParseClosingElementAt(startLoc) {
  4690. const node = this.startNodeAt(startLoc);
  4691. if (this.eat(144)) {
  4692. return this.finishNode(node, "JSXClosingFragment");
  4693. }
  4694. node.name = this.jsxParseElementName();
  4695. this.expect(144);
  4696. return this.finishNode(node, "JSXClosingElement");
  4697. }
  4698. jsxParseElementAt(startLoc) {
  4699. const node = this.startNodeAt(startLoc);
  4700. const children = [];
  4701. const openingElement = this.jsxParseOpeningElementAt(startLoc);
  4702. let closingElement = null;
  4703. if (!openingElement.selfClosing) {
  4704. contents: for (;;) {
  4705. switch (this.state.type) {
  4706. case 143:
  4707. startLoc = this.state.startLoc;
  4708. this.next();
  4709. if (this.eat(56)) {
  4710. closingElement = this.jsxParseClosingElementAt(startLoc);
  4711. break contents;
  4712. }
  4713. children.push(this.jsxParseElementAt(startLoc));
  4714. break;
  4715. case 142:
  4716. children.push(this.parseLiteral(this.state.value, "JSXText"));
  4717. break;
  4718. case 5:
  4719. {
  4720. const node = this.startNode();
  4721. this.setContext(types.brace);
  4722. this.next();
  4723. if (this.match(21)) {
  4724. children.push(this.jsxParseSpreadChild(node));
  4725. } else {
  4726. children.push(this.jsxParseExpressionContainer(node, types.j_expr));
  4727. }
  4728. break;
  4729. }
  4730. default:
  4731. this.unexpected();
  4732. }
  4733. }
  4734. if (isFragment(openingElement) && !isFragment(closingElement) && closingElement !== null) {
  4735. this.raise(JsxErrors.MissingClosingTagFragment, closingElement);
  4736. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  4737. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  4738. openingTagName: getQualifiedJSXName(openingElement.name)
  4739. });
  4740. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  4741. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  4742. this.raise(JsxErrors.MissingClosingTagElement, closingElement, {
  4743. openingTagName: getQualifiedJSXName(openingElement.name)
  4744. });
  4745. }
  4746. }
  4747. }
  4748. if (isFragment(openingElement)) {
  4749. node.openingFragment = openingElement;
  4750. node.closingFragment = closingElement;
  4751. } else {
  4752. node.openingElement = openingElement;
  4753. node.closingElement = closingElement;
  4754. }
  4755. node.children = children;
  4756. if (this.match(47)) {
  4757. throw this.raise(JsxErrors.UnwrappedAdjacentJSXElements, this.state.startLoc);
  4758. }
  4759. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  4760. }
  4761. jsxParseElement() {
  4762. const startLoc = this.state.startLoc;
  4763. this.next();
  4764. return this.jsxParseElementAt(startLoc);
  4765. }
  4766. setContext(newContext) {
  4767. const {
  4768. context
  4769. } = this.state;
  4770. context[context.length - 1] = newContext;
  4771. }
  4772. parseExprAtom(refExpressionErrors) {
  4773. if (this.match(143)) {
  4774. return this.jsxParseElement();
  4775. } else if (this.match(47) && this.input.charCodeAt(this.state.pos) !== 33) {
  4776. this.replaceToken(143);
  4777. return this.jsxParseElement();
  4778. } else {
  4779. return super.parseExprAtom(refExpressionErrors);
  4780. }
  4781. }
  4782. skipSpace() {
  4783. const curContext = this.curContext();
  4784. if (!curContext.preserveSpace) super.skipSpace();
  4785. }
  4786. getTokenFromCode(code) {
  4787. const context = this.curContext();
  4788. if (context === types.j_expr) {
  4789. this.jsxReadToken();
  4790. return;
  4791. }
  4792. if (context === types.j_oTag || context === types.j_cTag) {
  4793. if (isIdentifierStart(code)) {
  4794. this.jsxReadWord();
  4795. return;
  4796. }
  4797. if (code === 62) {
  4798. ++this.state.pos;
  4799. this.finishToken(144);
  4800. return;
  4801. }
  4802. if ((code === 34 || code === 39) && context === types.j_oTag) {
  4803. this.jsxReadString(code);
  4804. return;
  4805. }
  4806. }
  4807. if (code === 60 && this.state.canStartJSXElement && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  4808. ++this.state.pos;
  4809. this.finishToken(143);
  4810. return;
  4811. }
  4812. super.getTokenFromCode(code);
  4813. }
  4814. updateContext(prevType) {
  4815. const {
  4816. context,
  4817. type
  4818. } = this.state;
  4819. if (type === 56 && prevType === 143) {
  4820. context.splice(-2, 2, types.j_cTag);
  4821. this.state.canStartJSXElement = false;
  4822. } else if (type === 143) {
  4823. context.push(types.j_oTag);
  4824. } else if (type === 144) {
  4825. const out = context[context.length - 1];
  4826. if (out === types.j_oTag && prevType === 56 || out === types.j_cTag) {
  4827. context.pop();
  4828. this.state.canStartJSXElement = context[context.length - 1] === types.j_expr;
  4829. } else {
  4830. this.setContext(types.j_expr);
  4831. this.state.canStartJSXElement = true;
  4832. }
  4833. } else {
  4834. this.state.canStartJSXElement = tokenComesBeforeExpression(type);
  4835. }
  4836. }
  4837. };
  4838. class TypeScriptScope extends Scope {
  4839. constructor(...args) {
  4840. super(...args);
  4841. this.tsNames = new Map();
  4842. }
  4843. }
  4844. class TypeScriptScopeHandler extends ScopeHandler {
  4845. constructor(...args) {
  4846. super(...args);
  4847. this.importsStack = [];
  4848. }
  4849. createScope(flags) {
  4850. this.importsStack.push(new Set());
  4851. return new TypeScriptScope(flags);
  4852. }
  4853. enter(flags) {
  4854. if (flags === 1024) {
  4855. this.importsStack.push(new Set());
  4856. }
  4857. super.enter(flags);
  4858. }
  4859. exit() {
  4860. const flags = super.exit();
  4861. if (flags === 1024) {
  4862. this.importsStack.pop();
  4863. }
  4864. return flags;
  4865. }
  4866. hasImport(name, allowShadow) {
  4867. const len = this.importsStack.length;
  4868. if (this.importsStack[len - 1].has(name)) {
  4869. return true;
  4870. }
  4871. if (!allowShadow && len > 1) {
  4872. for (let i = 0; i < len - 1; i++) {
  4873. if (this.importsStack[i].has(name)) return true;
  4874. }
  4875. }
  4876. return false;
  4877. }
  4878. declareName(name, bindingType, loc) {
  4879. if (bindingType & 4096) {
  4880. if (this.hasImport(name, true)) {
  4881. this.parser.raise(Errors.VarRedeclaration, loc, {
  4882. identifierName: name
  4883. });
  4884. }
  4885. this.importsStack[this.importsStack.length - 1].add(name);
  4886. return;
  4887. }
  4888. const scope = this.currentScope();
  4889. let type = scope.tsNames.get(name) || 0;
  4890. if (bindingType & 1024) {
  4891. this.maybeExportDefined(scope, name);
  4892. scope.tsNames.set(name, type | 16);
  4893. return;
  4894. }
  4895. super.declareName(name, bindingType, loc);
  4896. if (bindingType & 2) {
  4897. if (!(bindingType & 1)) {
  4898. this.checkRedeclarationInScope(scope, name, bindingType, loc);
  4899. this.maybeExportDefined(scope, name);
  4900. }
  4901. type = type | 1;
  4902. }
  4903. if (bindingType & 256) {
  4904. type = type | 2;
  4905. }
  4906. if (bindingType & 512) {
  4907. type = type | 4;
  4908. }
  4909. if (bindingType & 128) {
  4910. type = type | 8;
  4911. }
  4912. if (type) scope.tsNames.set(name, type);
  4913. }
  4914. isRedeclaredInScope(scope, name, bindingType) {
  4915. const type = scope.tsNames.get(name);
  4916. if ((type & 2) > 0) {
  4917. if (bindingType & 256) {
  4918. const isConst = !!(bindingType & 512);
  4919. const wasConst = (type & 4) > 0;
  4920. return isConst !== wasConst;
  4921. }
  4922. return true;
  4923. }
  4924. if (bindingType & 128 && (type & 8) > 0) {
  4925. if (scope.names.get(name) & 2) {
  4926. return !!(bindingType & 1);
  4927. } else {
  4928. return false;
  4929. }
  4930. }
  4931. if (bindingType & 2 && (type & 1) > 0) {
  4932. return true;
  4933. }
  4934. return super.isRedeclaredInScope(scope, name, bindingType);
  4935. }
  4936. checkLocalExport(id) {
  4937. const {
  4938. name
  4939. } = id;
  4940. if (this.hasImport(name)) return;
  4941. const len = this.scopeStack.length;
  4942. for (let i = len - 1; i >= 0; i--) {
  4943. const scope = this.scopeStack[i];
  4944. const type = scope.tsNames.get(name);
  4945. if ((type & 1) > 0 || (type & 16) > 0) {
  4946. return;
  4947. }
  4948. }
  4949. super.checkLocalExport(id);
  4950. }
  4951. }
  4952. class ProductionParameterHandler {
  4953. constructor() {
  4954. this.stacks = [];
  4955. }
  4956. enter(flags) {
  4957. this.stacks.push(flags);
  4958. }
  4959. exit() {
  4960. this.stacks.pop();
  4961. }
  4962. currentFlags() {
  4963. return this.stacks[this.stacks.length - 1];
  4964. }
  4965. get hasAwait() {
  4966. return (this.currentFlags() & 2) > 0;
  4967. }
  4968. get hasYield() {
  4969. return (this.currentFlags() & 1) > 0;
  4970. }
  4971. get hasReturn() {
  4972. return (this.currentFlags() & 4) > 0;
  4973. }
  4974. get hasIn() {
  4975. return (this.currentFlags() & 8) > 0;
  4976. }
  4977. }
  4978. function functionFlags(isAsync, isGenerator) {
  4979. return (isAsync ? 2 : 0) | (isGenerator ? 1 : 0);
  4980. }
  4981. class BaseParser {
  4982. constructor() {
  4983. this.sawUnambiguousESM = false;
  4984. this.ambiguousScriptDifferentAst = false;
  4985. }
  4986. sourceToOffsetPos(sourcePos) {
  4987. return sourcePos + this.startIndex;
  4988. }
  4989. offsetToSourcePos(offsetPos) {
  4990. return offsetPos - this.startIndex;
  4991. }
  4992. hasPlugin(pluginConfig) {
  4993. if (typeof pluginConfig === "string") {
  4994. return this.plugins.has(pluginConfig);
  4995. } else {
  4996. const [pluginName, pluginOptions] = pluginConfig;
  4997. if (!this.hasPlugin(pluginName)) {
  4998. return false;
  4999. }
  5000. const actualOptions = this.plugins.get(pluginName);
  5001. for (const key of Object.keys(pluginOptions)) {
  5002. if ((actualOptions == null ? void 0 : actualOptions[key]) !== pluginOptions[key]) {
  5003. return false;
  5004. }
  5005. }
  5006. return true;
  5007. }
  5008. }
  5009. getPluginOption(plugin, name) {
  5010. var _this$plugins$get;
  5011. return (_this$plugins$get = this.plugins.get(plugin)) == null ? void 0 : _this$plugins$get[name];
  5012. }
  5013. }
  5014. function setTrailingComments(node, comments) {
  5015. if (node.trailingComments === undefined) {
  5016. node.trailingComments = comments;
  5017. } else {
  5018. node.trailingComments.unshift(...comments);
  5019. }
  5020. }
  5021. function setLeadingComments(node, comments) {
  5022. if (node.leadingComments === undefined) {
  5023. node.leadingComments = comments;
  5024. } else {
  5025. node.leadingComments.unshift(...comments);
  5026. }
  5027. }
  5028. function setInnerComments(node, comments) {
  5029. if (node.innerComments === undefined) {
  5030. node.innerComments = comments;
  5031. } else {
  5032. node.innerComments.unshift(...comments);
  5033. }
  5034. }
  5035. function adjustInnerComments(node, elements, commentWS) {
  5036. let lastElement = null;
  5037. let i = elements.length;
  5038. while (lastElement === null && i > 0) {
  5039. lastElement = elements[--i];
  5040. }
  5041. if (lastElement === null || lastElement.start > commentWS.start) {
  5042. setInnerComments(node, commentWS.comments);
  5043. } else {
  5044. setTrailingComments(lastElement, commentWS.comments);
  5045. }
  5046. }
  5047. class CommentsParser extends BaseParser {
  5048. addComment(comment) {
  5049. if (this.filename) comment.loc.filename = this.filename;
  5050. const {
  5051. commentsLen
  5052. } = this.state;
  5053. if (this.comments.length !== commentsLen) {
  5054. this.comments.length = commentsLen;
  5055. }
  5056. this.comments.push(comment);
  5057. this.state.commentsLen++;
  5058. }
  5059. processComment(node) {
  5060. const {
  5061. commentStack
  5062. } = this.state;
  5063. const commentStackLength = commentStack.length;
  5064. if (commentStackLength === 0) return;
  5065. let i = commentStackLength - 1;
  5066. const lastCommentWS = commentStack[i];
  5067. if (lastCommentWS.start === node.end) {
  5068. lastCommentWS.leadingNode = node;
  5069. i--;
  5070. }
  5071. const {
  5072. start: nodeStart
  5073. } = node;
  5074. for (; i >= 0; i--) {
  5075. const commentWS = commentStack[i];
  5076. const commentEnd = commentWS.end;
  5077. if (commentEnd > nodeStart) {
  5078. commentWS.containingNode = node;
  5079. this.finalizeComment(commentWS);
  5080. commentStack.splice(i, 1);
  5081. } else {
  5082. if (commentEnd === nodeStart) {
  5083. commentWS.trailingNode = node;
  5084. }
  5085. break;
  5086. }
  5087. }
  5088. }
  5089. finalizeComment(commentWS) {
  5090. var _node$options;
  5091. const {
  5092. comments
  5093. } = commentWS;
  5094. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  5095. if (commentWS.leadingNode !== null) {
  5096. setTrailingComments(commentWS.leadingNode, comments);
  5097. }
  5098. if (commentWS.trailingNode !== null) {
  5099. setLeadingComments(commentWS.trailingNode, comments);
  5100. }
  5101. } else {
  5102. const node = commentWS.containingNode;
  5103. const commentStart = commentWS.start;
  5104. if (this.input.charCodeAt(this.offsetToSourcePos(commentStart) - 1) === 44) {
  5105. switch (node.type) {
  5106. case "ObjectExpression":
  5107. case "ObjectPattern":
  5108. adjustInnerComments(node, node.properties, commentWS);
  5109. break;
  5110. case "CallExpression":
  5111. case "NewExpression":
  5112. case "OptionalCallExpression":
  5113. adjustInnerComments(node, node.arguments, commentWS);
  5114. break;
  5115. case "ImportExpression":
  5116. adjustInnerComments(node, [node.source, (_node$options = node.options) != null ? _node$options : null], commentWS);
  5117. break;
  5118. case "FunctionDeclaration":
  5119. case "FunctionExpression":
  5120. case "ArrowFunctionExpression":
  5121. case "ObjectMethod":
  5122. case "ClassMethod":
  5123. case "ClassPrivateMethod":
  5124. case "TSTypeParameterDeclaration":
  5125. adjustInnerComments(node, node.params, commentWS);
  5126. break;
  5127. case "ArrayExpression":
  5128. case "ArrayPattern":
  5129. adjustInnerComments(node, node.elements, commentWS);
  5130. break;
  5131. case "ExportNamedDeclaration":
  5132. case "ImportDeclaration":
  5133. adjustInnerComments(node, node.specifiers, commentWS);
  5134. break;
  5135. case "TSEnumDeclaration":
  5136. adjustInnerComments(node, node.members, commentWS);
  5137. break;
  5138. case "TSEnumBody":
  5139. adjustInnerComments(node, node.members, commentWS);
  5140. break;
  5141. case "TSInterfaceBody":
  5142. adjustInnerComments(node, node.body, commentWS);
  5143. break;
  5144. default:
  5145. {
  5146. if (node.type === "RecordExpression") {
  5147. adjustInnerComments(node, node.properties, commentWS);
  5148. break;
  5149. }
  5150. if (node.type === "TupleExpression") {
  5151. adjustInnerComments(node, node.elements, commentWS);
  5152. break;
  5153. }
  5154. setInnerComments(node, comments);
  5155. }
  5156. }
  5157. } else {
  5158. setInnerComments(node, comments);
  5159. }
  5160. }
  5161. }
  5162. finalizeRemainingComments() {
  5163. const {
  5164. commentStack
  5165. } = this.state;
  5166. for (let i = commentStack.length - 1; i >= 0; i--) {
  5167. this.finalizeComment(commentStack[i]);
  5168. }
  5169. this.state.commentStack = [];
  5170. }
  5171. resetPreviousNodeTrailingComments(node) {
  5172. const {
  5173. commentStack
  5174. } = this.state;
  5175. const {
  5176. length
  5177. } = commentStack;
  5178. if (length === 0) return;
  5179. const commentWS = commentStack[length - 1];
  5180. if (commentWS.leadingNode === node) {
  5181. commentWS.leadingNode = null;
  5182. }
  5183. }
  5184. takeSurroundingComments(node, start, end) {
  5185. const {
  5186. commentStack
  5187. } = this.state;
  5188. const commentStackLength = commentStack.length;
  5189. if (commentStackLength === 0) return;
  5190. let i = commentStackLength - 1;
  5191. for (; i >= 0; i--) {
  5192. const commentWS = commentStack[i];
  5193. const commentEnd = commentWS.end;
  5194. const commentStart = commentWS.start;
  5195. if (commentStart === end) {
  5196. commentWS.leadingNode = node;
  5197. } else if (commentEnd === start) {
  5198. commentWS.trailingNode = node;
  5199. } else if (commentEnd < start) {
  5200. break;
  5201. }
  5202. }
  5203. }
  5204. }
  5205. class State {
  5206. constructor() {
  5207. this.flags = 1024;
  5208. this.startIndex = void 0;
  5209. this.curLine = void 0;
  5210. this.lineStart = void 0;
  5211. this.startLoc = void 0;
  5212. this.endLoc = void 0;
  5213. this.errors = [];
  5214. this.potentialArrowAt = -1;
  5215. this.noArrowAt = [];
  5216. this.noArrowParamsConversionAt = [];
  5217. this.topicContext = {
  5218. maxNumOfResolvableTopics: 0,
  5219. maxTopicIndex: null
  5220. };
  5221. this.labels = [];
  5222. this.commentsLen = 0;
  5223. this.commentStack = [];
  5224. this.pos = 0;
  5225. this.type = 140;
  5226. this.value = null;
  5227. this.start = 0;
  5228. this.end = 0;
  5229. this.lastTokEndLoc = null;
  5230. this.lastTokStartLoc = null;
  5231. this.context = [types.brace];
  5232. this.firstInvalidTemplateEscapePos = null;
  5233. this.strictErrors = new Map();
  5234. this.tokensLength = 0;
  5235. }
  5236. get strict() {
  5237. return (this.flags & 1) > 0;
  5238. }
  5239. set strict(v) {
  5240. if (v) this.flags |= 1;else this.flags &= -2;
  5241. }
  5242. init({
  5243. strictMode,
  5244. sourceType,
  5245. startIndex,
  5246. startLine,
  5247. startColumn
  5248. }) {
  5249. this.strict = strictMode === false ? false : strictMode === true ? true : sourceType === "module";
  5250. this.startIndex = startIndex;
  5251. this.curLine = startLine;
  5252. this.lineStart = -startColumn;
  5253. this.startLoc = this.endLoc = new Position(startLine, startColumn, startIndex);
  5254. }
  5255. get maybeInArrowParameters() {
  5256. return (this.flags & 2) > 0;
  5257. }
  5258. set maybeInArrowParameters(v) {
  5259. if (v) this.flags |= 2;else this.flags &= -3;
  5260. }
  5261. get inType() {
  5262. return (this.flags & 4) > 0;
  5263. }
  5264. set inType(v) {
  5265. if (v) this.flags |= 4;else this.flags &= -5;
  5266. }
  5267. get noAnonFunctionType() {
  5268. return (this.flags & 8) > 0;
  5269. }
  5270. set noAnonFunctionType(v) {
  5271. if (v) this.flags |= 8;else this.flags &= -9;
  5272. }
  5273. get hasFlowComment() {
  5274. return (this.flags & 16) > 0;
  5275. }
  5276. set hasFlowComment(v) {
  5277. if (v) this.flags |= 16;else this.flags &= -17;
  5278. }
  5279. get isAmbientContext() {
  5280. return (this.flags & 32) > 0;
  5281. }
  5282. set isAmbientContext(v) {
  5283. if (v) this.flags |= 32;else this.flags &= -33;
  5284. }
  5285. get inAbstractClass() {
  5286. return (this.flags & 64) > 0;
  5287. }
  5288. set inAbstractClass(v) {
  5289. if (v) this.flags |= 64;else this.flags &= -65;
  5290. }
  5291. get inDisallowConditionalTypesContext() {
  5292. return (this.flags & 128) > 0;
  5293. }
  5294. set inDisallowConditionalTypesContext(v) {
  5295. if (v) this.flags |= 128;else this.flags &= -129;
  5296. }
  5297. get soloAwait() {
  5298. return (this.flags & 256) > 0;
  5299. }
  5300. set soloAwait(v) {
  5301. if (v) this.flags |= 256;else this.flags &= -257;
  5302. }
  5303. get inFSharpPipelineDirectBody() {
  5304. return (this.flags & 512) > 0;
  5305. }
  5306. set inFSharpPipelineDirectBody(v) {
  5307. if (v) this.flags |= 512;else this.flags &= -513;
  5308. }
  5309. get canStartJSXElement() {
  5310. return (this.flags & 1024) > 0;
  5311. }
  5312. set canStartJSXElement(v) {
  5313. if (v) this.flags |= 1024;else this.flags &= -1025;
  5314. }
  5315. get containsEsc() {
  5316. return (this.flags & 2048) > 0;
  5317. }
  5318. set containsEsc(v) {
  5319. if (v) this.flags |= 2048;else this.flags &= -2049;
  5320. }
  5321. get hasTopLevelAwait() {
  5322. return (this.flags & 4096) > 0;
  5323. }
  5324. set hasTopLevelAwait(v) {
  5325. if (v) this.flags |= 4096;else this.flags &= -4097;
  5326. }
  5327. curPosition() {
  5328. return new Position(this.curLine, this.pos - this.lineStart, this.pos + this.startIndex);
  5329. }
  5330. clone() {
  5331. const state = new State();
  5332. state.flags = this.flags;
  5333. state.startIndex = this.startIndex;
  5334. state.curLine = this.curLine;
  5335. state.lineStart = this.lineStart;
  5336. state.startLoc = this.startLoc;
  5337. state.endLoc = this.endLoc;
  5338. state.errors = this.errors.slice();
  5339. state.potentialArrowAt = this.potentialArrowAt;
  5340. state.noArrowAt = this.noArrowAt.slice();
  5341. state.noArrowParamsConversionAt = this.noArrowParamsConversionAt.slice();
  5342. state.topicContext = this.topicContext;
  5343. state.labels = this.labels.slice();
  5344. state.commentsLen = this.commentsLen;
  5345. state.commentStack = this.commentStack.slice();
  5346. state.pos = this.pos;
  5347. state.type = this.type;
  5348. state.value = this.value;
  5349. state.start = this.start;
  5350. state.end = this.end;
  5351. state.lastTokEndLoc = this.lastTokEndLoc;
  5352. state.lastTokStartLoc = this.lastTokStartLoc;
  5353. state.context = this.context.slice();
  5354. state.firstInvalidTemplateEscapePos = this.firstInvalidTemplateEscapePos;
  5355. state.strictErrors = this.strictErrors;
  5356. state.tokensLength = this.tokensLength;
  5357. return state;
  5358. }
  5359. }
  5360. var _isDigit = function isDigit(code) {
  5361. return code >= 48 && code <= 57;
  5362. };
  5363. const forbiddenNumericSeparatorSiblings = {
  5364. decBinOct: new Set([46, 66, 69, 79, 95, 98, 101, 111]),
  5365. hex: new Set([46, 88, 95, 120])
  5366. };
  5367. const isAllowedNumericSeparatorSibling = {
  5368. bin: ch => ch === 48 || ch === 49,
  5369. oct: ch => ch >= 48 && ch <= 55,
  5370. dec: ch => ch >= 48 && ch <= 57,
  5371. hex: ch => ch >= 48 && ch <= 57 || ch >= 65 && ch <= 70 || ch >= 97 && ch <= 102
  5372. };
  5373. function readStringContents(type, input, pos, lineStart, curLine, errors) {
  5374. const initialPos = pos;
  5375. const initialLineStart = lineStart;
  5376. const initialCurLine = curLine;
  5377. let out = "";
  5378. let firstInvalidLoc = null;
  5379. let chunkStart = pos;
  5380. const {
  5381. length
  5382. } = input;
  5383. for (;;) {
  5384. if (pos >= length) {
  5385. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  5386. out += input.slice(chunkStart, pos);
  5387. break;
  5388. }
  5389. const ch = input.charCodeAt(pos);
  5390. if (isStringEnd(type, ch, input, pos)) {
  5391. out += input.slice(chunkStart, pos);
  5392. break;
  5393. }
  5394. if (ch === 92) {
  5395. out += input.slice(chunkStart, pos);
  5396. const res = readEscapedChar(input, pos, lineStart, curLine, type === "template", errors);
  5397. if (res.ch === null && !firstInvalidLoc) {
  5398. firstInvalidLoc = {
  5399. pos,
  5400. lineStart,
  5401. curLine
  5402. };
  5403. } else {
  5404. out += res.ch;
  5405. }
  5406. ({
  5407. pos,
  5408. lineStart,
  5409. curLine
  5410. } = res);
  5411. chunkStart = pos;
  5412. } else if (ch === 8232 || ch === 8233) {
  5413. ++pos;
  5414. ++curLine;
  5415. lineStart = pos;
  5416. } else if (ch === 10 || ch === 13) {
  5417. if (type === "template") {
  5418. out += input.slice(chunkStart, pos) + "\n";
  5419. ++pos;
  5420. if (ch === 13 && input.charCodeAt(pos) === 10) {
  5421. ++pos;
  5422. }
  5423. ++curLine;
  5424. chunkStart = lineStart = pos;
  5425. } else {
  5426. errors.unterminated(initialPos, initialLineStart, initialCurLine);
  5427. }
  5428. } else {
  5429. ++pos;
  5430. }
  5431. }
  5432. return {
  5433. pos,
  5434. str: out,
  5435. firstInvalidLoc,
  5436. lineStart,
  5437. curLine,
  5438. containsInvalid: !!firstInvalidLoc
  5439. };
  5440. }
  5441. function isStringEnd(type, ch, input, pos) {
  5442. if (type === "template") {
  5443. return ch === 96 || ch === 36 && input.charCodeAt(pos + 1) === 123;
  5444. }
  5445. return ch === (type === "double" ? 34 : 39);
  5446. }
  5447. function readEscapedChar(input, pos, lineStart, curLine, inTemplate, errors) {
  5448. const throwOnInvalid = !inTemplate;
  5449. pos++;
  5450. const res = ch => ({
  5451. pos,
  5452. ch,
  5453. lineStart,
  5454. curLine
  5455. });
  5456. const ch = input.charCodeAt(pos++);
  5457. switch (ch) {
  5458. case 110:
  5459. return res("\n");
  5460. case 114:
  5461. return res("\r");
  5462. case 120:
  5463. {
  5464. let code;
  5465. ({
  5466. code,
  5467. pos
  5468. } = readHexChar(input, pos, lineStart, curLine, 2, false, throwOnInvalid, errors));
  5469. return res(code === null ? null : String.fromCharCode(code));
  5470. }
  5471. case 117:
  5472. {
  5473. let code;
  5474. ({
  5475. code,
  5476. pos
  5477. } = readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors));
  5478. return res(code === null ? null : String.fromCodePoint(code));
  5479. }
  5480. case 116:
  5481. return res("\t");
  5482. case 98:
  5483. return res("\b");
  5484. case 118:
  5485. return res("\u000b");
  5486. case 102:
  5487. return res("\f");
  5488. case 13:
  5489. if (input.charCodeAt(pos) === 10) {
  5490. ++pos;
  5491. }
  5492. case 10:
  5493. lineStart = pos;
  5494. ++curLine;
  5495. case 8232:
  5496. case 8233:
  5497. return res("");
  5498. case 56:
  5499. case 57:
  5500. if (inTemplate) {
  5501. return res(null);
  5502. } else {
  5503. errors.strictNumericEscape(pos - 1, lineStart, curLine);
  5504. }
  5505. default:
  5506. if (ch >= 48 && ch <= 55) {
  5507. const startPos = pos - 1;
  5508. const match = /^[0-7]+/.exec(input.slice(startPos, pos + 2));
  5509. let octalStr = match[0];
  5510. let octal = parseInt(octalStr, 8);
  5511. if (octal > 255) {
  5512. octalStr = octalStr.slice(0, -1);
  5513. octal = parseInt(octalStr, 8);
  5514. }
  5515. pos += octalStr.length - 1;
  5516. const next = input.charCodeAt(pos);
  5517. if (octalStr !== "0" || next === 56 || next === 57) {
  5518. if (inTemplate) {
  5519. return res(null);
  5520. } else {
  5521. errors.strictNumericEscape(startPos, lineStart, curLine);
  5522. }
  5523. }
  5524. return res(String.fromCharCode(octal));
  5525. }
  5526. return res(String.fromCharCode(ch));
  5527. }
  5528. }
  5529. function readHexChar(input, pos, lineStart, curLine, len, forceLen, throwOnInvalid, errors) {
  5530. const initialPos = pos;
  5531. let n;
  5532. ({
  5533. n,
  5534. pos
  5535. } = readInt(input, pos, lineStart, curLine, 16, len, forceLen, false, errors, !throwOnInvalid));
  5536. if (n === null) {
  5537. if (throwOnInvalid) {
  5538. errors.invalidEscapeSequence(initialPos, lineStart, curLine);
  5539. } else {
  5540. pos = initialPos - 1;
  5541. }
  5542. }
  5543. return {
  5544. code: n,
  5545. pos
  5546. };
  5547. }
  5548. function readInt(input, pos, lineStart, curLine, radix, len, forceLen, allowNumSeparator, errors, bailOnError) {
  5549. const start = pos;
  5550. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  5551. const isAllowedSibling = radix === 16 ? isAllowedNumericSeparatorSibling.hex : radix === 10 ? isAllowedNumericSeparatorSibling.dec : radix === 8 ? isAllowedNumericSeparatorSibling.oct : isAllowedNumericSeparatorSibling.bin;
  5552. let invalid = false;
  5553. let total = 0;
  5554. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  5555. const code = input.charCodeAt(pos);
  5556. let val;
  5557. if (code === 95 && allowNumSeparator !== "bail") {
  5558. const prev = input.charCodeAt(pos - 1);
  5559. const next = input.charCodeAt(pos + 1);
  5560. if (!allowNumSeparator) {
  5561. if (bailOnError) return {
  5562. n: null,
  5563. pos
  5564. };
  5565. errors.numericSeparatorInEscapeSequence(pos, lineStart, curLine);
  5566. } else if (Number.isNaN(next) || !isAllowedSibling(next) || forbiddenSiblings.has(prev) || forbiddenSiblings.has(next)) {
  5567. if (bailOnError) return {
  5568. n: null,
  5569. pos
  5570. };
  5571. errors.unexpectedNumericSeparator(pos, lineStart, curLine);
  5572. }
  5573. ++pos;
  5574. continue;
  5575. }
  5576. if (code >= 97) {
  5577. val = code - 97 + 10;
  5578. } else if (code >= 65) {
  5579. val = code - 65 + 10;
  5580. } else if (_isDigit(code)) {
  5581. val = code - 48;
  5582. } else {
  5583. val = Infinity;
  5584. }
  5585. if (val >= radix) {
  5586. if (val <= 9 && bailOnError) {
  5587. return {
  5588. n: null,
  5589. pos
  5590. };
  5591. } else if (val <= 9 && errors.invalidDigit(pos, lineStart, curLine, radix)) {
  5592. val = 0;
  5593. } else if (forceLen) {
  5594. val = 0;
  5595. invalid = true;
  5596. } else {
  5597. break;
  5598. }
  5599. }
  5600. ++pos;
  5601. total = total * radix + val;
  5602. }
  5603. if (pos === start || len != null && pos - start !== len || invalid) {
  5604. return {
  5605. n: null,
  5606. pos
  5607. };
  5608. }
  5609. return {
  5610. n: total,
  5611. pos
  5612. };
  5613. }
  5614. function readCodePoint(input, pos, lineStart, curLine, throwOnInvalid, errors) {
  5615. const ch = input.charCodeAt(pos);
  5616. let code;
  5617. if (ch === 123) {
  5618. ++pos;
  5619. ({
  5620. code,
  5621. pos
  5622. } = readHexChar(input, pos, lineStart, curLine, input.indexOf("}", pos) - pos, true, throwOnInvalid, errors));
  5623. ++pos;
  5624. if (code !== null && code > 0x10ffff) {
  5625. if (throwOnInvalid) {
  5626. errors.invalidCodePoint(pos, lineStart, curLine);
  5627. } else {
  5628. return {
  5629. code: null,
  5630. pos
  5631. };
  5632. }
  5633. }
  5634. } else {
  5635. ({
  5636. code,
  5637. pos
  5638. } = readHexChar(input, pos, lineStart, curLine, 4, false, throwOnInvalid, errors));
  5639. }
  5640. return {
  5641. code,
  5642. pos
  5643. };
  5644. }
  5645. function buildPosition(pos, lineStart, curLine) {
  5646. return new Position(curLine, pos - lineStart, pos);
  5647. }
  5648. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100, 118]);
  5649. class Token {
  5650. constructor(state) {
  5651. const startIndex = state.startIndex || 0;
  5652. this.type = state.type;
  5653. this.value = state.value;
  5654. this.start = startIndex + state.start;
  5655. this.end = startIndex + state.end;
  5656. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  5657. }
  5658. }
  5659. class Tokenizer extends CommentsParser {
  5660. constructor(options, input) {
  5661. super();
  5662. this.isLookahead = void 0;
  5663. this.tokens = [];
  5664. this.errorHandlers_readInt = {
  5665. invalidDigit: (pos, lineStart, curLine, radix) => {
  5666. if (!(this.optionFlags & 2048)) return false;
  5667. this.raise(Errors.InvalidDigit, buildPosition(pos, lineStart, curLine), {
  5668. radix
  5669. });
  5670. return true;
  5671. },
  5672. numericSeparatorInEscapeSequence: this.errorBuilder(Errors.NumericSeparatorInEscapeSequence),
  5673. unexpectedNumericSeparator: this.errorBuilder(Errors.UnexpectedNumericSeparator)
  5674. };
  5675. this.errorHandlers_readCodePoint = Object.assign({}, this.errorHandlers_readInt, {
  5676. invalidEscapeSequence: this.errorBuilder(Errors.InvalidEscapeSequence),
  5677. invalidCodePoint: this.errorBuilder(Errors.InvalidCodePoint)
  5678. });
  5679. this.errorHandlers_readStringContents_string = Object.assign({}, this.errorHandlers_readCodePoint, {
  5680. strictNumericEscape: (pos, lineStart, curLine) => {
  5681. this.recordStrictModeErrors(Errors.StrictNumericEscape, buildPosition(pos, lineStart, curLine));
  5682. },
  5683. unterminated: (pos, lineStart, curLine) => {
  5684. throw this.raise(Errors.UnterminatedString, buildPosition(pos - 1, lineStart, curLine));
  5685. }
  5686. });
  5687. this.errorHandlers_readStringContents_template = Object.assign({}, this.errorHandlers_readCodePoint, {
  5688. strictNumericEscape: this.errorBuilder(Errors.StrictNumericEscape),
  5689. unterminated: (pos, lineStart, curLine) => {
  5690. throw this.raise(Errors.UnterminatedTemplate, buildPosition(pos, lineStart, curLine));
  5691. }
  5692. });
  5693. this.state = new State();
  5694. this.state.init(options);
  5695. this.input = input;
  5696. this.length = input.length;
  5697. this.comments = [];
  5698. this.isLookahead = false;
  5699. }
  5700. pushToken(token) {
  5701. this.tokens.length = this.state.tokensLength;
  5702. this.tokens.push(token);
  5703. ++this.state.tokensLength;
  5704. }
  5705. next() {
  5706. this.checkKeywordEscapes();
  5707. if (this.optionFlags & 256) {
  5708. this.pushToken(new Token(this.state));
  5709. }
  5710. this.state.lastTokEndLoc = this.state.endLoc;
  5711. this.state.lastTokStartLoc = this.state.startLoc;
  5712. this.nextToken();
  5713. }
  5714. eat(type) {
  5715. if (this.match(type)) {
  5716. this.next();
  5717. return true;
  5718. } else {
  5719. return false;
  5720. }
  5721. }
  5722. match(type) {
  5723. return this.state.type === type;
  5724. }
  5725. createLookaheadState(state) {
  5726. return {
  5727. pos: state.pos,
  5728. value: null,
  5729. type: state.type,
  5730. start: state.start,
  5731. end: state.end,
  5732. context: [this.curContext()],
  5733. inType: state.inType,
  5734. startLoc: state.startLoc,
  5735. lastTokEndLoc: state.lastTokEndLoc,
  5736. curLine: state.curLine,
  5737. lineStart: state.lineStart,
  5738. curPosition: state.curPosition
  5739. };
  5740. }
  5741. lookahead() {
  5742. const old = this.state;
  5743. this.state = this.createLookaheadState(old);
  5744. this.isLookahead = true;
  5745. this.nextToken();
  5746. this.isLookahead = false;
  5747. const curr = this.state;
  5748. this.state = old;
  5749. return curr;
  5750. }
  5751. nextTokenStart() {
  5752. return this.nextTokenStartSince(this.state.pos);
  5753. }
  5754. nextTokenStartSince(pos) {
  5755. skipWhiteSpace.lastIndex = pos;
  5756. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  5757. }
  5758. lookaheadCharCode() {
  5759. return this.lookaheadCharCodeSince(this.state.pos);
  5760. }
  5761. lookaheadCharCodeSince(pos) {
  5762. return this.input.charCodeAt(this.nextTokenStartSince(pos));
  5763. }
  5764. nextTokenInLineStart() {
  5765. return this.nextTokenInLineStartSince(this.state.pos);
  5766. }
  5767. nextTokenInLineStartSince(pos) {
  5768. skipWhiteSpaceInLine.lastIndex = pos;
  5769. return skipWhiteSpaceInLine.test(this.input) ? skipWhiteSpaceInLine.lastIndex : pos;
  5770. }
  5771. lookaheadInLineCharCode() {
  5772. return this.input.charCodeAt(this.nextTokenInLineStart());
  5773. }
  5774. codePointAtPos(pos) {
  5775. let cp = this.input.charCodeAt(pos);
  5776. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  5777. const trail = this.input.charCodeAt(pos);
  5778. if ((trail & 0xfc00) === 0xdc00) {
  5779. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  5780. }
  5781. }
  5782. return cp;
  5783. }
  5784. setStrict(strict) {
  5785. this.state.strict = strict;
  5786. if (strict) {
  5787. this.state.strictErrors.forEach(([toParseError, at]) => this.raise(toParseError, at));
  5788. this.state.strictErrors.clear();
  5789. }
  5790. }
  5791. curContext() {
  5792. return this.state.context[this.state.context.length - 1];
  5793. }
  5794. nextToken() {
  5795. this.skipSpace();
  5796. this.state.start = this.state.pos;
  5797. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  5798. if (this.state.pos >= this.length) {
  5799. this.finishToken(140);
  5800. return;
  5801. }
  5802. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  5803. }
  5804. skipBlockComment(commentEnd) {
  5805. let startLoc;
  5806. if (!this.isLookahead) startLoc = this.state.curPosition();
  5807. const start = this.state.pos;
  5808. const end = this.input.indexOf(commentEnd, start + 2);
  5809. if (end === -1) {
  5810. throw this.raise(Errors.UnterminatedComment, this.state.curPosition());
  5811. }
  5812. this.state.pos = end + commentEnd.length;
  5813. lineBreakG.lastIndex = start + 2;
  5814. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  5815. ++this.state.curLine;
  5816. this.state.lineStart = lineBreakG.lastIndex;
  5817. }
  5818. if (this.isLookahead) return;
  5819. const comment = {
  5820. type: "CommentBlock",
  5821. value: this.input.slice(start + 2, end),
  5822. start: this.sourceToOffsetPos(start),
  5823. end: this.sourceToOffsetPos(end + commentEnd.length),
  5824. loc: new SourceLocation(startLoc, this.state.curPosition())
  5825. };
  5826. if (this.optionFlags & 256) this.pushToken(comment);
  5827. return comment;
  5828. }
  5829. skipLineComment(startSkip) {
  5830. const start = this.state.pos;
  5831. let startLoc;
  5832. if (!this.isLookahead) startLoc = this.state.curPosition();
  5833. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  5834. if (this.state.pos < this.length) {
  5835. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  5836. ch = this.input.charCodeAt(this.state.pos);
  5837. }
  5838. }
  5839. if (this.isLookahead) return;
  5840. const end = this.state.pos;
  5841. const value = this.input.slice(start + startSkip, end);
  5842. const comment = {
  5843. type: "CommentLine",
  5844. value,
  5845. start: this.sourceToOffsetPos(start),
  5846. end: this.sourceToOffsetPos(end),
  5847. loc: new SourceLocation(startLoc, this.state.curPosition())
  5848. };
  5849. if (this.optionFlags & 256) this.pushToken(comment);
  5850. return comment;
  5851. }
  5852. skipSpace() {
  5853. const spaceStart = this.state.pos;
  5854. const comments = this.optionFlags & 4096 ? [] : null;
  5855. loop: while (this.state.pos < this.length) {
  5856. const ch = this.input.charCodeAt(this.state.pos);
  5857. switch (ch) {
  5858. case 32:
  5859. case 160:
  5860. case 9:
  5861. ++this.state.pos;
  5862. break;
  5863. case 13:
  5864. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  5865. ++this.state.pos;
  5866. }
  5867. case 10:
  5868. case 8232:
  5869. case 8233:
  5870. ++this.state.pos;
  5871. ++this.state.curLine;
  5872. this.state.lineStart = this.state.pos;
  5873. break;
  5874. case 47:
  5875. switch (this.input.charCodeAt(this.state.pos + 1)) {
  5876. case 42:
  5877. {
  5878. const comment = this.skipBlockComment("*/");
  5879. if (comment !== undefined) {
  5880. this.addComment(comment);
  5881. comments == null || comments.push(comment);
  5882. }
  5883. break;
  5884. }
  5885. case 47:
  5886. {
  5887. const comment = this.skipLineComment(2);
  5888. if (comment !== undefined) {
  5889. this.addComment(comment);
  5890. comments == null || comments.push(comment);
  5891. }
  5892. break;
  5893. }
  5894. default:
  5895. break loop;
  5896. }
  5897. break;
  5898. default:
  5899. if (isWhitespace(ch)) {
  5900. ++this.state.pos;
  5901. } else if (ch === 45 && !this.inModule && this.optionFlags & 8192) {
  5902. const pos = this.state.pos;
  5903. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  5904. const comment = this.skipLineComment(3);
  5905. if (comment !== undefined) {
  5906. this.addComment(comment);
  5907. comments == null || comments.push(comment);
  5908. }
  5909. } else {
  5910. break loop;
  5911. }
  5912. } else if (ch === 60 && !this.inModule && this.optionFlags & 8192) {
  5913. const pos = this.state.pos;
  5914. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  5915. const comment = this.skipLineComment(4);
  5916. if (comment !== undefined) {
  5917. this.addComment(comment);
  5918. comments == null || comments.push(comment);
  5919. }
  5920. } else {
  5921. break loop;
  5922. }
  5923. } else {
  5924. break loop;
  5925. }
  5926. }
  5927. }
  5928. if ((comments == null ? void 0 : comments.length) > 0) {
  5929. const end = this.state.pos;
  5930. const commentWhitespace = {
  5931. start: this.sourceToOffsetPos(spaceStart),
  5932. end: this.sourceToOffsetPos(end),
  5933. comments: comments,
  5934. leadingNode: null,
  5935. trailingNode: null,
  5936. containingNode: null
  5937. };
  5938. this.state.commentStack.push(commentWhitespace);
  5939. }
  5940. }
  5941. finishToken(type, val) {
  5942. this.state.end = this.state.pos;
  5943. this.state.endLoc = this.state.curPosition();
  5944. const prevType = this.state.type;
  5945. this.state.type = type;
  5946. this.state.value = val;
  5947. if (!this.isLookahead) {
  5948. this.updateContext(prevType);
  5949. }
  5950. }
  5951. replaceToken(type) {
  5952. this.state.type = type;
  5953. this.updateContext();
  5954. }
  5955. readToken_numberSign() {
  5956. if (this.state.pos === 0 && this.readToken_interpreter()) {
  5957. return;
  5958. }
  5959. const nextPos = this.state.pos + 1;
  5960. const next = this.codePointAtPos(nextPos);
  5961. if (next >= 48 && next <= 57) {
  5962. throw this.raise(Errors.UnexpectedDigitAfterHash, this.state.curPosition());
  5963. }
  5964. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  5965. this.expectPlugin("recordAndTuple");
  5966. if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
  5967. throw this.raise(next === 123 ? Errors.RecordExpressionHashIncorrectStartSyntaxType : Errors.TupleExpressionHashIncorrectStartSyntaxType, this.state.curPosition());
  5968. }
  5969. this.state.pos += 2;
  5970. if (next === 123) {
  5971. this.finishToken(7);
  5972. } else {
  5973. this.finishToken(1);
  5974. }
  5975. } else if (isIdentifierStart(next)) {
  5976. ++this.state.pos;
  5977. this.finishToken(139, this.readWord1(next));
  5978. } else if (next === 92) {
  5979. ++this.state.pos;
  5980. this.finishToken(139, this.readWord1());
  5981. } else {
  5982. this.finishOp(27, 1);
  5983. }
  5984. }
  5985. readToken_dot() {
  5986. const next = this.input.charCodeAt(this.state.pos + 1);
  5987. if (next >= 48 && next <= 57) {
  5988. this.readNumber(true);
  5989. return;
  5990. }
  5991. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  5992. this.state.pos += 3;
  5993. this.finishToken(21);
  5994. } else {
  5995. ++this.state.pos;
  5996. this.finishToken(16);
  5997. }
  5998. }
  5999. readToken_slash() {
  6000. const next = this.input.charCodeAt(this.state.pos + 1);
  6001. if (next === 61) {
  6002. this.finishOp(31, 2);
  6003. } else {
  6004. this.finishOp(56, 1);
  6005. }
  6006. }
  6007. readToken_interpreter() {
  6008. if (this.state.pos !== 0 || this.length < 2) return false;
  6009. let ch = this.input.charCodeAt(this.state.pos + 1);
  6010. if (ch !== 33) return false;
  6011. const start = this.state.pos;
  6012. this.state.pos += 1;
  6013. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  6014. ch = this.input.charCodeAt(this.state.pos);
  6015. }
  6016. const value = this.input.slice(start + 2, this.state.pos);
  6017. this.finishToken(28, value);
  6018. return true;
  6019. }
  6020. readToken_mult_modulo(code) {
  6021. let type = code === 42 ? 55 : 54;
  6022. let width = 1;
  6023. let next = this.input.charCodeAt(this.state.pos + 1);
  6024. if (code === 42 && next === 42) {
  6025. width++;
  6026. next = this.input.charCodeAt(this.state.pos + 2);
  6027. type = 57;
  6028. }
  6029. if (next === 61 && !this.state.inType) {
  6030. width++;
  6031. type = code === 37 ? 33 : 30;
  6032. }
  6033. this.finishOp(type, width);
  6034. }
  6035. readToken_pipe_amp(code) {
  6036. const next = this.input.charCodeAt(this.state.pos + 1);
  6037. if (next === code) {
  6038. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  6039. this.finishOp(30, 3);
  6040. } else {
  6041. this.finishOp(code === 124 ? 41 : 42, 2);
  6042. }
  6043. return;
  6044. }
  6045. if (code === 124) {
  6046. if (next === 62) {
  6047. this.finishOp(39, 2);
  6048. return;
  6049. }
  6050. if (this.hasPlugin("recordAndTuple") && next === 125) {
  6051. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6052. throw this.raise(Errors.RecordExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  6053. }
  6054. this.state.pos += 2;
  6055. this.finishToken(9);
  6056. return;
  6057. }
  6058. if (this.hasPlugin("recordAndTuple") && next === 93) {
  6059. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6060. throw this.raise(Errors.TupleExpressionBarIncorrectEndSyntaxType, this.state.curPosition());
  6061. }
  6062. this.state.pos += 2;
  6063. this.finishToken(4);
  6064. return;
  6065. }
  6066. }
  6067. if (next === 61) {
  6068. this.finishOp(30, 2);
  6069. return;
  6070. }
  6071. this.finishOp(code === 124 ? 43 : 45, 1);
  6072. }
  6073. readToken_caret() {
  6074. const next = this.input.charCodeAt(this.state.pos + 1);
  6075. if (next === 61 && !this.state.inType) {
  6076. this.finishOp(32, 2);
  6077. } else if (next === 94 && this.hasPlugin(["pipelineOperator", {
  6078. proposal: "hack",
  6079. topicToken: "^^"
  6080. }])) {
  6081. this.finishOp(37, 2);
  6082. const lookaheadCh = this.input.codePointAt(this.state.pos);
  6083. if (lookaheadCh === 94) {
  6084. this.unexpected();
  6085. }
  6086. } else {
  6087. this.finishOp(44, 1);
  6088. }
  6089. }
  6090. readToken_atSign() {
  6091. const next = this.input.charCodeAt(this.state.pos + 1);
  6092. if (next === 64 && this.hasPlugin(["pipelineOperator", {
  6093. proposal: "hack",
  6094. topicToken: "@@"
  6095. }])) {
  6096. this.finishOp(38, 2);
  6097. } else {
  6098. this.finishOp(26, 1);
  6099. }
  6100. }
  6101. readToken_plus_min(code) {
  6102. const next = this.input.charCodeAt(this.state.pos + 1);
  6103. if (next === code) {
  6104. this.finishOp(34, 2);
  6105. return;
  6106. }
  6107. if (next === 61) {
  6108. this.finishOp(30, 2);
  6109. } else {
  6110. this.finishOp(53, 1);
  6111. }
  6112. }
  6113. readToken_lt() {
  6114. const {
  6115. pos
  6116. } = this.state;
  6117. const next = this.input.charCodeAt(pos + 1);
  6118. if (next === 60) {
  6119. if (this.input.charCodeAt(pos + 2) === 61) {
  6120. this.finishOp(30, 3);
  6121. return;
  6122. }
  6123. this.finishOp(51, 2);
  6124. return;
  6125. }
  6126. if (next === 61) {
  6127. this.finishOp(49, 2);
  6128. return;
  6129. }
  6130. this.finishOp(47, 1);
  6131. }
  6132. readToken_gt() {
  6133. const {
  6134. pos
  6135. } = this.state;
  6136. const next = this.input.charCodeAt(pos + 1);
  6137. if (next === 62) {
  6138. const size = this.input.charCodeAt(pos + 2) === 62 ? 3 : 2;
  6139. if (this.input.charCodeAt(pos + size) === 61) {
  6140. this.finishOp(30, size + 1);
  6141. return;
  6142. }
  6143. this.finishOp(52, size);
  6144. return;
  6145. }
  6146. if (next === 61) {
  6147. this.finishOp(49, 2);
  6148. return;
  6149. }
  6150. this.finishOp(48, 1);
  6151. }
  6152. readToken_eq_excl(code) {
  6153. const next = this.input.charCodeAt(this.state.pos + 1);
  6154. if (next === 61) {
  6155. this.finishOp(46, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  6156. return;
  6157. }
  6158. if (code === 61 && next === 62) {
  6159. this.state.pos += 2;
  6160. this.finishToken(19);
  6161. return;
  6162. }
  6163. this.finishOp(code === 61 ? 29 : 35, 1);
  6164. }
  6165. readToken_question() {
  6166. const next = this.input.charCodeAt(this.state.pos + 1);
  6167. const next2 = this.input.charCodeAt(this.state.pos + 2);
  6168. if (next === 63) {
  6169. if (next2 === 61) {
  6170. this.finishOp(30, 3);
  6171. } else {
  6172. this.finishOp(40, 2);
  6173. }
  6174. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  6175. this.state.pos += 2;
  6176. this.finishToken(18);
  6177. } else {
  6178. ++this.state.pos;
  6179. this.finishToken(17);
  6180. }
  6181. }
  6182. getTokenFromCode(code) {
  6183. switch (code) {
  6184. case 46:
  6185. this.readToken_dot();
  6186. return;
  6187. case 40:
  6188. ++this.state.pos;
  6189. this.finishToken(10);
  6190. return;
  6191. case 41:
  6192. ++this.state.pos;
  6193. this.finishToken(11);
  6194. return;
  6195. case 59:
  6196. ++this.state.pos;
  6197. this.finishToken(13);
  6198. return;
  6199. case 44:
  6200. ++this.state.pos;
  6201. this.finishToken(12);
  6202. return;
  6203. case 91:
  6204. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  6205. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6206. throw this.raise(Errors.TupleExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  6207. }
  6208. this.state.pos += 2;
  6209. this.finishToken(2);
  6210. } else {
  6211. ++this.state.pos;
  6212. this.finishToken(0);
  6213. }
  6214. return;
  6215. case 93:
  6216. ++this.state.pos;
  6217. this.finishToken(3);
  6218. return;
  6219. case 123:
  6220. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  6221. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  6222. throw this.raise(Errors.RecordExpressionBarIncorrectStartSyntaxType, this.state.curPosition());
  6223. }
  6224. this.state.pos += 2;
  6225. this.finishToken(6);
  6226. } else {
  6227. ++this.state.pos;
  6228. this.finishToken(5);
  6229. }
  6230. return;
  6231. case 125:
  6232. ++this.state.pos;
  6233. this.finishToken(8);
  6234. return;
  6235. case 58:
  6236. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  6237. this.finishOp(15, 2);
  6238. } else {
  6239. ++this.state.pos;
  6240. this.finishToken(14);
  6241. }
  6242. return;
  6243. case 63:
  6244. this.readToken_question();
  6245. return;
  6246. case 96:
  6247. this.readTemplateToken();
  6248. return;
  6249. case 48:
  6250. {
  6251. const next = this.input.charCodeAt(this.state.pos + 1);
  6252. if (next === 120 || next === 88) {
  6253. this.readRadixNumber(16);
  6254. return;
  6255. }
  6256. if (next === 111 || next === 79) {
  6257. this.readRadixNumber(8);
  6258. return;
  6259. }
  6260. if (next === 98 || next === 66) {
  6261. this.readRadixNumber(2);
  6262. return;
  6263. }
  6264. }
  6265. case 49:
  6266. case 50:
  6267. case 51:
  6268. case 52:
  6269. case 53:
  6270. case 54:
  6271. case 55:
  6272. case 56:
  6273. case 57:
  6274. this.readNumber(false);
  6275. return;
  6276. case 34:
  6277. case 39:
  6278. this.readString(code);
  6279. return;
  6280. case 47:
  6281. this.readToken_slash();
  6282. return;
  6283. case 37:
  6284. case 42:
  6285. this.readToken_mult_modulo(code);
  6286. return;
  6287. case 124:
  6288. case 38:
  6289. this.readToken_pipe_amp(code);
  6290. return;
  6291. case 94:
  6292. this.readToken_caret();
  6293. return;
  6294. case 43:
  6295. case 45:
  6296. this.readToken_plus_min(code);
  6297. return;
  6298. case 60:
  6299. this.readToken_lt();
  6300. return;
  6301. case 62:
  6302. this.readToken_gt();
  6303. return;
  6304. case 61:
  6305. case 33:
  6306. this.readToken_eq_excl(code);
  6307. return;
  6308. case 126:
  6309. this.finishOp(36, 1);
  6310. return;
  6311. case 64:
  6312. this.readToken_atSign();
  6313. return;
  6314. case 35:
  6315. this.readToken_numberSign();
  6316. return;
  6317. case 92:
  6318. this.readWord();
  6319. return;
  6320. default:
  6321. if (isIdentifierStart(code)) {
  6322. this.readWord(code);
  6323. return;
  6324. }
  6325. }
  6326. throw this.raise(Errors.InvalidOrUnexpectedToken, this.state.curPosition(), {
  6327. unexpected: String.fromCodePoint(code)
  6328. });
  6329. }
  6330. finishOp(type, size) {
  6331. const str = this.input.slice(this.state.pos, this.state.pos + size);
  6332. this.state.pos += size;
  6333. this.finishToken(type, str);
  6334. }
  6335. readRegexp() {
  6336. const startLoc = this.state.startLoc;
  6337. const start = this.state.start + 1;
  6338. let escaped, inClass;
  6339. let {
  6340. pos
  6341. } = this.state;
  6342. for (;; ++pos) {
  6343. if (pos >= this.length) {
  6344. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  6345. }
  6346. const ch = this.input.charCodeAt(pos);
  6347. if (isNewLine(ch)) {
  6348. throw this.raise(Errors.UnterminatedRegExp, createPositionWithColumnOffset(startLoc, 1));
  6349. }
  6350. if (escaped) {
  6351. escaped = false;
  6352. } else {
  6353. if (ch === 91) {
  6354. inClass = true;
  6355. } else if (ch === 93 && inClass) {
  6356. inClass = false;
  6357. } else if (ch === 47 && !inClass) {
  6358. break;
  6359. }
  6360. escaped = ch === 92;
  6361. }
  6362. }
  6363. const content = this.input.slice(start, pos);
  6364. ++pos;
  6365. let mods = "";
  6366. const nextPos = () => createPositionWithColumnOffset(startLoc, pos + 2 - start);
  6367. while (pos < this.length) {
  6368. const cp = this.codePointAtPos(pos);
  6369. const char = String.fromCharCode(cp);
  6370. if (VALID_REGEX_FLAGS.has(cp)) {
  6371. if (cp === 118) {
  6372. if (mods.includes("u")) {
  6373. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  6374. }
  6375. } else if (cp === 117) {
  6376. if (mods.includes("v")) {
  6377. this.raise(Errors.IncompatibleRegExpUVFlags, nextPos());
  6378. }
  6379. }
  6380. if (mods.includes(char)) {
  6381. this.raise(Errors.DuplicateRegExpFlags, nextPos());
  6382. }
  6383. } else if (isIdentifierChar(cp) || cp === 92) {
  6384. this.raise(Errors.MalformedRegExpFlags, nextPos());
  6385. } else {
  6386. break;
  6387. }
  6388. ++pos;
  6389. mods += char;
  6390. }
  6391. this.state.pos = pos;
  6392. this.finishToken(138, {
  6393. pattern: content,
  6394. flags: mods
  6395. });
  6396. }
  6397. readInt(radix, len, forceLen = false, allowNumSeparator = true) {
  6398. const {
  6399. n,
  6400. pos
  6401. } = readInt(this.input, this.state.pos, this.state.lineStart, this.state.curLine, radix, len, forceLen, allowNumSeparator, this.errorHandlers_readInt, false);
  6402. this.state.pos = pos;
  6403. return n;
  6404. }
  6405. readRadixNumber(radix) {
  6406. const start = this.state.pos;
  6407. const startLoc = this.state.curPosition();
  6408. let isBigInt = false;
  6409. this.state.pos += 2;
  6410. const val = this.readInt(radix);
  6411. if (val == null) {
  6412. this.raise(Errors.InvalidDigit, createPositionWithColumnOffset(startLoc, 2), {
  6413. radix
  6414. });
  6415. }
  6416. const next = this.input.charCodeAt(this.state.pos);
  6417. if (next === 110) {
  6418. ++this.state.pos;
  6419. isBigInt = true;
  6420. } else if (next === 109) {
  6421. throw this.raise(Errors.InvalidDecimal, startLoc);
  6422. }
  6423. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  6424. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  6425. }
  6426. if (isBigInt) {
  6427. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  6428. this.finishToken(136, str);
  6429. return;
  6430. }
  6431. this.finishToken(135, val);
  6432. }
  6433. readNumber(startsWithDot) {
  6434. const start = this.state.pos;
  6435. const startLoc = this.state.curPosition();
  6436. let isFloat = false;
  6437. let isBigInt = false;
  6438. let hasExponent = false;
  6439. let isOctal = false;
  6440. if (!startsWithDot && this.readInt(10) === null) {
  6441. this.raise(Errors.InvalidNumber, this.state.curPosition());
  6442. }
  6443. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  6444. if (hasLeadingZero) {
  6445. const integer = this.input.slice(start, this.state.pos);
  6446. this.recordStrictModeErrors(Errors.StrictOctalLiteral, startLoc);
  6447. if (!this.state.strict) {
  6448. const underscorePos = integer.indexOf("_");
  6449. if (underscorePos > 0) {
  6450. this.raise(Errors.ZeroDigitNumericSeparator, createPositionWithColumnOffset(startLoc, underscorePos));
  6451. }
  6452. }
  6453. isOctal = hasLeadingZero && !/[89]/.test(integer);
  6454. }
  6455. let next = this.input.charCodeAt(this.state.pos);
  6456. if (next === 46 && !isOctal) {
  6457. ++this.state.pos;
  6458. this.readInt(10);
  6459. isFloat = true;
  6460. next = this.input.charCodeAt(this.state.pos);
  6461. }
  6462. if ((next === 69 || next === 101) && !isOctal) {
  6463. next = this.input.charCodeAt(++this.state.pos);
  6464. if (next === 43 || next === 45) {
  6465. ++this.state.pos;
  6466. }
  6467. if (this.readInt(10) === null) {
  6468. this.raise(Errors.InvalidOrMissingExponent, startLoc);
  6469. }
  6470. isFloat = true;
  6471. hasExponent = true;
  6472. next = this.input.charCodeAt(this.state.pos);
  6473. }
  6474. if (next === 110) {
  6475. if (isFloat || hasLeadingZero) {
  6476. this.raise(Errors.InvalidBigIntLiteral, startLoc);
  6477. }
  6478. ++this.state.pos;
  6479. isBigInt = true;
  6480. }
  6481. if (next === 109) {
  6482. this.expectPlugin("decimal", this.state.curPosition());
  6483. if (hasExponent || hasLeadingZero) {
  6484. this.raise(Errors.InvalidDecimal, startLoc);
  6485. }
  6486. ++this.state.pos;
  6487. var isDecimal = true;
  6488. }
  6489. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  6490. throw this.raise(Errors.NumberIdentifier, this.state.curPosition());
  6491. }
  6492. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  6493. if (isBigInt) {
  6494. this.finishToken(136, str);
  6495. return;
  6496. }
  6497. if (isDecimal) {
  6498. this.finishToken(137, str);
  6499. return;
  6500. }
  6501. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  6502. this.finishToken(135, val);
  6503. }
  6504. readCodePoint(throwOnInvalid) {
  6505. const {
  6506. code,
  6507. pos
  6508. } = readCodePoint(this.input, this.state.pos, this.state.lineStart, this.state.curLine, throwOnInvalid, this.errorHandlers_readCodePoint);
  6509. this.state.pos = pos;
  6510. return code;
  6511. }
  6512. readString(quote) {
  6513. const {
  6514. str,
  6515. pos,
  6516. curLine,
  6517. lineStart
  6518. } = readStringContents(quote === 34 ? "double" : "single", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_string);
  6519. this.state.pos = pos + 1;
  6520. this.state.lineStart = lineStart;
  6521. this.state.curLine = curLine;
  6522. this.finishToken(134, str);
  6523. }
  6524. readTemplateContinuation() {
  6525. if (!this.match(8)) {
  6526. this.unexpected(null, 8);
  6527. }
  6528. this.state.pos--;
  6529. this.readTemplateToken();
  6530. }
  6531. readTemplateToken() {
  6532. const opening = this.input[this.state.pos];
  6533. const {
  6534. str,
  6535. firstInvalidLoc,
  6536. pos,
  6537. curLine,
  6538. lineStart
  6539. } = readStringContents("template", this.input, this.state.pos + 1, this.state.lineStart, this.state.curLine, this.errorHandlers_readStringContents_template);
  6540. this.state.pos = pos + 1;
  6541. this.state.lineStart = lineStart;
  6542. this.state.curLine = curLine;
  6543. if (firstInvalidLoc) {
  6544. this.state.firstInvalidTemplateEscapePos = new Position(firstInvalidLoc.curLine, firstInvalidLoc.pos - firstInvalidLoc.lineStart, this.sourceToOffsetPos(firstInvalidLoc.pos));
  6545. }
  6546. if (this.input.codePointAt(pos) === 96) {
  6547. this.finishToken(24, firstInvalidLoc ? null : opening + str + "`");
  6548. } else {
  6549. this.state.pos++;
  6550. this.finishToken(25, firstInvalidLoc ? null : opening + str + "${");
  6551. }
  6552. }
  6553. recordStrictModeErrors(toParseError, at) {
  6554. const index = at.index;
  6555. if (this.state.strict && !this.state.strictErrors.has(index)) {
  6556. this.raise(toParseError, at);
  6557. } else {
  6558. this.state.strictErrors.set(index, [toParseError, at]);
  6559. }
  6560. }
  6561. readWord1(firstCode) {
  6562. this.state.containsEsc = false;
  6563. let word = "";
  6564. const start = this.state.pos;
  6565. let chunkStart = this.state.pos;
  6566. if (firstCode !== undefined) {
  6567. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  6568. }
  6569. while (this.state.pos < this.length) {
  6570. const ch = this.codePointAtPos(this.state.pos);
  6571. if (isIdentifierChar(ch)) {
  6572. this.state.pos += ch <= 0xffff ? 1 : 2;
  6573. } else if (ch === 92) {
  6574. this.state.containsEsc = true;
  6575. word += this.input.slice(chunkStart, this.state.pos);
  6576. const escStart = this.state.curPosition();
  6577. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  6578. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  6579. this.raise(Errors.MissingUnicodeEscape, this.state.curPosition());
  6580. chunkStart = this.state.pos - 1;
  6581. continue;
  6582. }
  6583. ++this.state.pos;
  6584. const esc = this.readCodePoint(true);
  6585. if (esc !== null) {
  6586. if (!identifierCheck(esc)) {
  6587. this.raise(Errors.EscapedCharNotAnIdentifier, escStart);
  6588. }
  6589. word += String.fromCodePoint(esc);
  6590. }
  6591. chunkStart = this.state.pos;
  6592. } else {
  6593. break;
  6594. }
  6595. }
  6596. return word + this.input.slice(chunkStart, this.state.pos);
  6597. }
  6598. readWord(firstCode) {
  6599. const word = this.readWord1(firstCode);
  6600. const type = keywords$1.get(word);
  6601. if (type !== undefined) {
  6602. this.finishToken(type, tokenLabelName(type));
  6603. } else {
  6604. this.finishToken(132, word);
  6605. }
  6606. }
  6607. checkKeywordEscapes() {
  6608. const {
  6609. type
  6610. } = this.state;
  6611. if (tokenIsKeyword(type) && this.state.containsEsc) {
  6612. this.raise(Errors.InvalidEscapedReservedWord, this.state.startLoc, {
  6613. reservedWord: tokenLabelName(type)
  6614. });
  6615. }
  6616. }
  6617. raise(toParseError, at, details = {}) {
  6618. const loc = at instanceof Position ? at : at.loc.start;
  6619. const error = toParseError(loc, details);
  6620. if (!(this.optionFlags & 2048)) throw error;
  6621. if (!this.isLookahead) this.state.errors.push(error);
  6622. return error;
  6623. }
  6624. raiseOverwrite(toParseError, at, details = {}) {
  6625. const loc = at instanceof Position ? at : at.loc.start;
  6626. const pos = loc.index;
  6627. const errors = this.state.errors;
  6628. for (let i = errors.length - 1; i >= 0; i--) {
  6629. const error = errors[i];
  6630. if (error.loc.index === pos) {
  6631. return errors[i] = toParseError(loc, details);
  6632. }
  6633. if (error.loc.index < pos) break;
  6634. }
  6635. return this.raise(toParseError, at, details);
  6636. }
  6637. updateContext(prevType) {}
  6638. unexpected(loc, type) {
  6639. throw this.raise(Errors.UnexpectedToken, loc != null ? loc : this.state.startLoc, {
  6640. expected: type ? tokenLabelName(type) : null
  6641. });
  6642. }
  6643. expectPlugin(pluginName, loc) {
  6644. if (this.hasPlugin(pluginName)) {
  6645. return true;
  6646. }
  6647. throw this.raise(Errors.MissingPlugin, loc != null ? loc : this.state.startLoc, {
  6648. missingPlugin: [pluginName]
  6649. });
  6650. }
  6651. expectOnePlugin(pluginNames) {
  6652. if (!pluginNames.some(name => this.hasPlugin(name))) {
  6653. throw this.raise(Errors.MissingOneOfPlugins, this.state.startLoc, {
  6654. missingPlugin: pluginNames
  6655. });
  6656. }
  6657. }
  6658. errorBuilder(error) {
  6659. return (pos, lineStart, curLine) => {
  6660. this.raise(error, buildPosition(pos, lineStart, curLine));
  6661. };
  6662. }
  6663. }
  6664. class ClassScope {
  6665. constructor() {
  6666. this.privateNames = new Set();
  6667. this.loneAccessors = new Map();
  6668. this.undefinedPrivateNames = new Map();
  6669. }
  6670. }
  6671. class ClassScopeHandler {
  6672. constructor(parser) {
  6673. this.parser = void 0;
  6674. this.stack = [];
  6675. this.undefinedPrivateNames = new Map();
  6676. this.parser = parser;
  6677. }
  6678. current() {
  6679. return this.stack[this.stack.length - 1];
  6680. }
  6681. enter() {
  6682. this.stack.push(new ClassScope());
  6683. }
  6684. exit() {
  6685. const oldClassScope = this.stack.pop();
  6686. const current = this.current();
  6687. for (const [name, loc] of Array.from(oldClassScope.undefinedPrivateNames)) {
  6688. if (current) {
  6689. if (!current.undefinedPrivateNames.has(name)) {
  6690. current.undefinedPrivateNames.set(name, loc);
  6691. }
  6692. } else {
  6693. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  6694. identifierName: name
  6695. });
  6696. }
  6697. }
  6698. }
  6699. declarePrivateName(name, elementType, loc) {
  6700. const {
  6701. privateNames,
  6702. loneAccessors,
  6703. undefinedPrivateNames
  6704. } = this.current();
  6705. let redefined = privateNames.has(name);
  6706. if (elementType & 3) {
  6707. const accessor = redefined && loneAccessors.get(name);
  6708. if (accessor) {
  6709. const oldStatic = accessor & 4;
  6710. const newStatic = elementType & 4;
  6711. const oldKind = accessor & 3;
  6712. const newKind = elementType & 3;
  6713. redefined = oldKind === newKind || oldStatic !== newStatic;
  6714. if (!redefined) loneAccessors.delete(name);
  6715. } else if (!redefined) {
  6716. loneAccessors.set(name, elementType);
  6717. }
  6718. }
  6719. if (redefined) {
  6720. this.parser.raise(Errors.PrivateNameRedeclaration, loc, {
  6721. identifierName: name
  6722. });
  6723. }
  6724. privateNames.add(name);
  6725. undefinedPrivateNames.delete(name);
  6726. }
  6727. usePrivateName(name, loc) {
  6728. let classScope;
  6729. for (classScope of this.stack) {
  6730. if (classScope.privateNames.has(name)) return;
  6731. }
  6732. if (classScope) {
  6733. classScope.undefinedPrivateNames.set(name, loc);
  6734. } else {
  6735. this.parser.raise(Errors.InvalidPrivateFieldResolution, loc, {
  6736. identifierName: name
  6737. });
  6738. }
  6739. }
  6740. }
  6741. class ExpressionScope {
  6742. constructor(type = 0) {
  6743. this.type = type;
  6744. }
  6745. canBeArrowParameterDeclaration() {
  6746. return this.type === 2 || this.type === 1;
  6747. }
  6748. isCertainlyParameterDeclaration() {
  6749. return this.type === 3;
  6750. }
  6751. }
  6752. class ArrowHeadParsingScope extends ExpressionScope {
  6753. constructor(type) {
  6754. super(type);
  6755. this.declarationErrors = new Map();
  6756. }
  6757. recordDeclarationError(ParsingErrorClass, at) {
  6758. const index = at.index;
  6759. this.declarationErrors.set(index, [ParsingErrorClass, at]);
  6760. }
  6761. clearDeclarationError(index) {
  6762. this.declarationErrors.delete(index);
  6763. }
  6764. iterateErrors(iterator) {
  6765. this.declarationErrors.forEach(iterator);
  6766. }
  6767. }
  6768. class ExpressionScopeHandler {
  6769. constructor(parser) {
  6770. this.parser = void 0;
  6771. this.stack = [new ExpressionScope()];
  6772. this.parser = parser;
  6773. }
  6774. enter(scope) {
  6775. this.stack.push(scope);
  6776. }
  6777. exit() {
  6778. this.stack.pop();
  6779. }
  6780. recordParameterInitializerError(toParseError, node) {
  6781. const origin = node.loc.start;
  6782. const {
  6783. stack
  6784. } = this;
  6785. let i = stack.length - 1;
  6786. let scope = stack[i];
  6787. while (!scope.isCertainlyParameterDeclaration()) {
  6788. if (scope.canBeArrowParameterDeclaration()) {
  6789. scope.recordDeclarationError(toParseError, origin);
  6790. } else {
  6791. return;
  6792. }
  6793. scope = stack[--i];
  6794. }
  6795. this.parser.raise(toParseError, origin);
  6796. }
  6797. recordArrowParameterBindingError(error, node) {
  6798. const {
  6799. stack
  6800. } = this;
  6801. const scope = stack[stack.length - 1];
  6802. const origin = node.loc.start;
  6803. if (scope.isCertainlyParameterDeclaration()) {
  6804. this.parser.raise(error, origin);
  6805. } else if (scope.canBeArrowParameterDeclaration()) {
  6806. scope.recordDeclarationError(error, origin);
  6807. } else {
  6808. return;
  6809. }
  6810. }
  6811. recordAsyncArrowParametersError(at) {
  6812. const {
  6813. stack
  6814. } = this;
  6815. let i = stack.length - 1;
  6816. let scope = stack[i];
  6817. while (scope.canBeArrowParameterDeclaration()) {
  6818. if (scope.type === 2) {
  6819. scope.recordDeclarationError(Errors.AwaitBindingIdentifier, at);
  6820. }
  6821. scope = stack[--i];
  6822. }
  6823. }
  6824. validateAsPattern() {
  6825. const {
  6826. stack
  6827. } = this;
  6828. const currentScope = stack[stack.length - 1];
  6829. if (!currentScope.canBeArrowParameterDeclaration()) return;
  6830. currentScope.iterateErrors(([toParseError, loc]) => {
  6831. this.parser.raise(toParseError, loc);
  6832. let i = stack.length - 2;
  6833. let scope = stack[i];
  6834. while (scope.canBeArrowParameterDeclaration()) {
  6835. scope.clearDeclarationError(loc.index);
  6836. scope = stack[--i];
  6837. }
  6838. });
  6839. }
  6840. }
  6841. function newParameterDeclarationScope() {
  6842. return new ExpressionScope(3);
  6843. }
  6844. function newArrowHeadScope() {
  6845. return new ArrowHeadParsingScope(1);
  6846. }
  6847. function newAsyncArrowScope() {
  6848. return new ArrowHeadParsingScope(2);
  6849. }
  6850. function newExpressionScope() {
  6851. return new ExpressionScope();
  6852. }
  6853. class UtilParser extends Tokenizer {
  6854. addExtra(node, key, value, enumerable = true) {
  6855. if (!node) return;
  6856. let {
  6857. extra
  6858. } = node;
  6859. if (extra == null) {
  6860. extra = {};
  6861. node.extra = extra;
  6862. }
  6863. if (enumerable) {
  6864. extra[key] = value;
  6865. } else {
  6866. Object.defineProperty(extra, key, {
  6867. enumerable,
  6868. value
  6869. });
  6870. }
  6871. }
  6872. isContextual(token) {
  6873. return this.state.type === token && !this.state.containsEsc;
  6874. }
  6875. isUnparsedContextual(nameStart, name) {
  6876. if (this.input.startsWith(name, nameStart)) {
  6877. const nextCh = this.input.charCodeAt(nameStart + name.length);
  6878. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  6879. }
  6880. return false;
  6881. }
  6882. isLookaheadContextual(name) {
  6883. const next = this.nextTokenStart();
  6884. return this.isUnparsedContextual(next, name);
  6885. }
  6886. eatContextual(token) {
  6887. if (this.isContextual(token)) {
  6888. this.next();
  6889. return true;
  6890. }
  6891. return false;
  6892. }
  6893. expectContextual(token, toParseError) {
  6894. if (!this.eatContextual(token)) {
  6895. if (toParseError != null) {
  6896. throw this.raise(toParseError, this.state.startLoc);
  6897. }
  6898. this.unexpected(null, token);
  6899. }
  6900. }
  6901. canInsertSemicolon() {
  6902. return this.match(140) || this.match(8) || this.hasPrecedingLineBreak();
  6903. }
  6904. hasPrecedingLineBreak() {
  6905. return hasNewLine(this.input, this.offsetToSourcePos(this.state.lastTokEndLoc.index), this.state.start);
  6906. }
  6907. hasFollowingLineBreak() {
  6908. return hasNewLine(this.input, this.state.end, this.nextTokenStart());
  6909. }
  6910. isLineTerminator() {
  6911. return this.eat(13) || this.canInsertSemicolon();
  6912. }
  6913. semicolon(allowAsi = true) {
  6914. if (allowAsi ? this.isLineTerminator() : this.eat(13)) return;
  6915. this.raise(Errors.MissingSemicolon, this.state.lastTokEndLoc);
  6916. }
  6917. expect(type, loc) {
  6918. if (!this.eat(type)) {
  6919. this.unexpected(loc, type);
  6920. }
  6921. }
  6922. tryParse(fn, oldState = this.state.clone()) {
  6923. const abortSignal = {
  6924. node: null
  6925. };
  6926. try {
  6927. const node = fn((node = null) => {
  6928. abortSignal.node = node;
  6929. throw abortSignal;
  6930. });
  6931. if (this.state.errors.length > oldState.errors.length) {
  6932. const failState = this.state;
  6933. this.state = oldState;
  6934. this.state.tokensLength = failState.tokensLength;
  6935. return {
  6936. node,
  6937. error: failState.errors[oldState.errors.length],
  6938. thrown: false,
  6939. aborted: false,
  6940. failState
  6941. };
  6942. }
  6943. return {
  6944. node: node,
  6945. error: null,
  6946. thrown: false,
  6947. aborted: false,
  6948. failState: null
  6949. };
  6950. } catch (error) {
  6951. const failState = this.state;
  6952. this.state = oldState;
  6953. if (error instanceof SyntaxError) {
  6954. return {
  6955. node: null,
  6956. error,
  6957. thrown: true,
  6958. aborted: false,
  6959. failState
  6960. };
  6961. }
  6962. if (error === abortSignal) {
  6963. return {
  6964. node: abortSignal.node,
  6965. error: null,
  6966. thrown: false,
  6967. aborted: true,
  6968. failState
  6969. };
  6970. }
  6971. throw error;
  6972. }
  6973. }
  6974. checkExpressionErrors(refExpressionErrors, andThrow) {
  6975. if (!refExpressionErrors) return false;
  6976. const {
  6977. shorthandAssignLoc,
  6978. doubleProtoLoc,
  6979. privateKeyLoc,
  6980. optionalParametersLoc,
  6981. voidPatternLoc
  6982. } = refExpressionErrors;
  6983. const hasErrors = !!shorthandAssignLoc || !!doubleProtoLoc || !!optionalParametersLoc || !!privateKeyLoc || !!voidPatternLoc;
  6984. if (!andThrow) {
  6985. return hasErrors;
  6986. }
  6987. if (shorthandAssignLoc != null) {
  6988. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  6989. }
  6990. if (doubleProtoLoc != null) {
  6991. this.raise(Errors.DuplicateProto, doubleProtoLoc);
  6992. }
  6993. if (privateKeyLoc != null) {
  6994. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  6995. }
  6996. if (optionalParametersLoc != null) {
  6997. this.unexpected(optionalParametersLoc);
  6998. }
  6999. if (voidPatternLoc != null) {
  7000. this.raise(Errors.InvalidCoverDiscardElement, voidPatternLoc);
  7001. }
  7002. }
  7003. isLiteralPropertyName() {
  7004. return tokenIsLiteralPropertyName(this.state.type);
  7005. }
  7006. isPrivateName(node) {
  7007. return node.type === "PrivateName";
  7008. }
  7009. getPrivateNameSV(node) {
  7010. return node.id.name;
  7011. }
  7012. hasPropertyAsPrivateName(node) {
  7013. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  7014. }
  7015. isObjectProperty(node) {
  7016. return node.type === "ObjectProperty";
  7017. }
  7018. isObjectMethod(node) {
  7019. return node.type === "ObjectMethod";
  7020. }
  7021. initializeScopes(inModule = this.options.sourceType === "module") {
  7022. const oldLabels = this.state.labels;
  7023. this.state.labels = [];
  7024. const oldExportedIdentifiers = this.exportedIdentifiers;
  7025. this.exportedIdentifiers = new Set();
  7026. const oldInModule = this.inModule;
  7027. this.inModule = inModule;
  7028. const oldScope = this.scope;
  7029. const ScopeHandler = this.getScopeHandler();
  7030. this.scope = new ScopeHandler(this, inModule);
  7031. const oldProdParam = this.prodParam;
  7032. this.prodParam = new ProductionParameterHandler();
  7033. const oldClassScope = this.classScope;
  7034. this.classScope = new ClassScopeHandler(this);
  7035. const oldExpressionScope = this.expressionScope;
  7036. this.expressionScope = new ExpressionScopeHandler(this);
  7037. return () => {
  7038. this.state.labels = oldLabels;
  7039. this.exportedIdentifiers = oldExportedIdentifiers;
  7040. this.inModule = oldInModule;
  7041. this.scope = oldScope;
  7042. this.prodParam = oldProdParam;
  7043. this.classScope = oldClassScope;
  7044. this.expressionScope = oldExpressionScope;
  7045. };
  7046. }
  7047. enterInitialScopes() {
  7048. let paramFlags = 0;
  7049. if (this.inModule || this.optionFlags & 1) {
  7050. paramFlags |= 2;
  7051. }
  7052. if (this.optionFlags & 32) {
  7053. paramFlags |= 1;
  7054. }
  7055. const isCommonJS = !this.inModule && this.options.sourceType === "commonjs";
  7056. if (isCommonJS || this.optionFlags & 2) {
  7057. paramFlags |= 4;
  7058. }
  7059. this.prodParam.enter(paramFlags);
  7060. let scopeFlags = isCommonJS ? 514 : 1;
  7061. if (this.optionFlags & 4) {
  7062. scopeFlags |= 512;
  7063. }
  7064. this.scope.enter(scopeFlags);
  7065. }
  7066. checkDestructuringPrivate(refExpressionErrors) {
  7067. const {
  7068. privateKeyLoc
  7069. } = refExpressionErrors;
  7070. if (privateKeyLoc !== null) {
  7071. this.expectPlugin("destructuringPrivate", privateKeyLoc);
  7072. }
  7073. }
  7074. }
  7075. class ExpressionErrors {
  7076. constructor() {
  7077. this.shorthandAssignLoc = null;
  7078. this.doubleProtoLoc = null;
  7079. this.privateKeyLoc = null;
  7080. this.optionalParametersLoc = null;
  7081. this.voidPatternLoc = null;
  7082. }
  7083. }
  7084. class Node {
  7085. constructor(parser, pos, loc) {
  7086. this.type = "";
  7087. this.start = pos;
  7088. this.end = 0;
  7089. this.loc = new SourceLocation(loc);
  7090. if ((parser == null ? void 0 : parser.optionFlags) & 128) this.range = [pos, 0];
  7091. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  7092. }
  7093. }
  7094. const NodePrototype = Node.prototype;
  7095. NodePrototype.__clone = function () {
  7096. const newNode = new Node(undefined, this.start, this.loc.start);
  7097. const keys = Object.keys(this);
  7098. for (let i = 0, length = keys.length; i < length; i++) {
  7099. const key = keys[i];
  7100. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  7101. newNode[key] = this[key];
  7102. }
  7103. }
  7104. return newNode;
  7105. };
  7106. class NodeUtils extends UtilParser {
  7107. startNode() {
  7108. const loc = this.state.startLoc;
  7109. return new Node(this, loc.index, loc);
  7110. }
  7111. startNodeAt(loc) {
  7112. return new Node(this, loc.index, loc);
  7113. }
  7114. startNodeAtNode(type) {
  7115. return this.startNodeAt(type.loc.start);
  7116. }
  7117. finishNode(node, type) {
  7118. return this.finishNodeAt(node, type, this.state.lastTokEndLoc);
  7119. }
  7120. finishNodeAt(node, type, endLoc) {
  7121. node.type = type;
  7122. node.end = endLoc.index;
  7123. node.loc.end = endLoc;
  7124. if (this.optionFlags & 128) node.range[1] = endLoc.index;
  7125. if (this.optionFlags & 4096) {
  7126. this.processComment(node);
  7127. }
  7128. return node;
  7129. }
  7130. resetStartLocation(node, startLoc) {
  7131. node.start = startLoc.index;
  7132. node.loc.start = startLoc;
  7133. if (this.optionFlags & 128) node.range[0] = startLoc.index;
  7134. }
  7135. resetEndLocation(node, endLoc = this.state.lastTokEndLoc) {
  7136. node.end = endLoc.index;
  7137. node.loc.end = endLoc;
  7138. if (this.optionFlags & 128) node.range[1] = endLoc.index;
  7139. }
  7140. resetStartLocationFromNode(node, locationNode) {
  7141. this.resetStartLocation(node, locationNode.loc.start);
  7142. }
  7143. castNodeTo(node, type) {
  7144. node.type = type;
  7145. return node;
  7146. }
  7147. cloneIdentifier(node) {
  7148. const {
  7149. type,
  7150. start,
  7151. end,
  7152. loc,
  7153. range,
  7154. name
  7155. } = node;
  7156. const cloned = Object.create(NodePrototype);
  7157. cloned.type = type;
  7158. cloned.start = start;
  7159. cloned.end = end;
  7160. cloned.loc = loc;
  7161. cloned.range = range;
  7162. cloned.name = name;
  7163. if (node.extra) cloned.extra = node.extra;
  7164. return cloned;
  7165. }
  7166. cloneStringLiteral(node) {
  7167. const {
  7168. type,
  7169. start,
  7170. end,
  7171. loc,
  7172. range,
  7173. extra
  7174. } = node;
  7175. const cloned = Object.create(NodePrototype);
  7176. cloned.type = type;
  7177. cloned.start = start;
  7178. cloned.end = end;
  7179. cloned.loc = loc;
  7180. cloned.range = range;
  7181. cloned.extra = extra;
  7182. cloned.value = node.value;
  7183. return cloned;
  7184. }
  7185. }
  7186. const unwrapParenthesizedExpression = node => {
  7187. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  7188. };
  7189. class LValParser extends NodeUtils {
  7190. toAssignable(node, isLHS = false) {
  7191. var _node$extra, _node$extra3;
  7192. let parenthesized = undefined;
  7193. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  7194. parenthesized = unwrapParenthesizedExpression(node);
  7195. if (isLHS) {
  7196. if (parenthesized.type === "Identifier") {
  7197. this.expressionScope.recordArrowParameterBindingError(Errors.InvalidParenthesizedAssignment, node);
  7198. } else if (parenthesized.type !== "CallExpression" && parenthesized.type !== "MemberExpression" && !this.isOptionalMemberExpression(parenthesized)) {
  7199. this.raise(Errors.InvalidParenthesizedAssignment, node);
  7200. }
  7201. } else {
  7202. this.raise(Errors.InvalidParenthesizedAssignment, node);
  7203. }
  7204. }
  7205. switch (node.type) {
  7206. case "Identifier":
  7207. case "ObjectPattern":
  7208. case "ArrayPattern":
  7209. case "AssignmentPattern":
  7210. case "RestElement":
  7211. case "VoidPattern":
  7212. break;
  7213. case "ObjectExpression":
  7214. this.castNodeTo(node, "ObjectPattern");
  7215. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  7216. var _node$extra2;
  7217. const prop = node.properties[i];
  7218. const isLast = i === last;
  7219. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  7220. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingCommaLoc) {
  7221. this.raise(Errors.RestTrailingComma, node.extra.trailingCommaLoc);
  7222. }
  7223. }
  7224. break;
  7225. case "ObjectProperty":
  7226. {
  7227. const {
  7228. key,
  7229. value
  7230. } = node;
  7231. if (this.isPrivateName(key)) {
  7232. this.classScope.usePrivateName(this.getPrivateNameSV(key), key.loc.start);
  7233. }
  7234. this.toAssignable(value, isLHS);
  7235. break;
  7236. }
  7237. case "SpreadElement":
  7238. {
  7239. throw new Error("Internal @babel/parser error (this is a bug, please report it)." + " SpreadElement should be converted by .toAssignable's caller.");
  7240. }
  7241. case "ArrayExpression":
  7242. this.castNodeTo(node, "ArrayPattern");
  7243. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingCommaLoc, isLHS);
  7244. break;
  7245. case "AssignmentExpression":
  7246. if (node.operator !== "=") {
  7247. this.raise(Errors.MissingEqInAssignment, node.left.loc.end);
  7248. }
  7249. this.castNodeTo(node, "AssignmentPattern");
  7250. delete node.operator;
  7251. if (node.left.type === "VoidPattern") {
  7252. this.raise(Errors.VoidPatternInitializer, node.left);
  7253. }
  7254. this.toAssignable(node.left, isLHS);
  7255. break;
  7256. case "ParenthesizedExpression":
  7257. this.toAssignable(parenthesized, isLHS);
  7258. break;
  7259. }
  7260. }
  7261. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  7262. if (prop.type === "ObjectMethod") {
  7263. this.raise(prop.kind === "get" || prop.kind === "set" ? Errors.PatternHasAccessor : Errors.PatternHasMethod, prop.key);
  7264. } else if (prop.type === "SpreadElement") {
  7265. this.castNodeTo(prop, "RestElement");
  7266. const arg = prop.argument;
  7267. this.checkToRestConversion(arg, false);
  7268. this.toAssignable(arg, isLHS);
  7269. if (!isLast) {
  7270. this.raise(Errors.RestTrailingComma, prop);
  7271. }
  7272. } else {
  7273. this.toAssignable(prop, isLHS);
  7274. }
  7275. }
  7276. toAssignableList(exprList, trailingCommaLoc, isLHS) {
  7277. const end = exprList.length - 1;
  7278. for (let i = 0; i <= end; i++) {
  7279. const elt = exprList[i];
  7280. if (!elt) continue;
  7281. this.toAssignableListItem(exprList, i, isLHS);
  7282. if (elt.type === "RestElement") {
  7283. if (i < end) {
  7284. this.raise(Errors.RestTrailingComma, elt);
  7285. } else if (trailingCommaLoc) {
  7286. this.raise(Errors.RestTrailingComma, trailingCommaLoc);
  7287. }
  7288. }
  7289. }
  7290. }
  7291. toAssignableListItem(exprList, index, isLHS) {
  7292. const node = exprList[index];
  7293. if (node.type === "SpreadElement") {
  7294. this.castNodeTo(node, "RestElement");
  7295. const arg = node.argument;
  7296. this.checkToRestConversion(arg, true);
  7297. this.toAssignable(arg, isLHS);
  7298. } else {
  7299. this.toAssignable(node, isLHS);
  7300. }
  7301. }
  7302. isAssignable(node, isBinding) {
  7303. switch (node.type) {
  7304. case "Identifier":
  7305. case "ObjectPattern":
  7306. case "ArrayPattern":
  7307. case "AssignmentPattern":
  7308. case "RestElement":
  7309. case "VoidPattern":
  7310. return true;
  7311. case "ObjectExpression":
  7312. {
  7313. const last = node.properties.length - 1;
  7314. return node.properties.every((prop, i) => {
  7315. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  7316. });
  7317. }
  7318. case "ObjectProperty":
  7319. return this.isAssignable(node.value);
  7320. case "SpreadElement":
  7321. return this.isAssignable(node.argument);
  7322. case "ArrayExpression":
  7323. return node.elements.every(element => element === null || this.isAssignable(element));
  7324. case "AssignmentExpression":
  7325. return node.operator === "=";
  7326. case "ParenthesizedExpression":
  7327. return this.isAssignable(node.expression);
  7328. case "MemberExpression":
  7329. case "OptionalMemberExpression":
  7330. return !isBinding;
  7331. default:
  7332. return false;
  7333. }
  7334. }
  7335. toReferencedList(exprList, isParenthesizedExpr) {
  7336. return exprList;
  7337. }
  7338. toReferencedListDeep(exprList, isParenthesizedExpr) {
  7339. this.toReferencedList(exprList, isParenthesizedExpr);
  7340. for (const expr of exprList) {
  7341. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  7342. this.toReferencedListDeep(expr.elements);
  7343. }
  7344. }
  7345. }
  7346. parseSpread(refExpressionErrors) {
  7347. const node = this.startNode();
  7348. this.next();
  7349. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined);
  7350. return this.finishNode(node, "SpreadElement");
  7351. }
  7352. parseRestBinding() {
  7353. const node = this.startNode();
  7354. this.next();
  7355. const argument = this.parseBindingAtom();
  7356. if (argument.type === "VoidPattern") {
  7357. this.raise(Errors.UnexpectedVoidPattern, argument);
  7358. }
  7359. node.argument = argument;
  7360. return this.finishNode(node, "RestElement");
  7361. }
  7362. parseBindingAtom() {
  7363. switch (this.state.type) {
  7364. case 0:
  7365. {
  7366. const node = this.startNode();
  7367. this.next();
  7368. node.elements = this.parseBindingList(3, 93, 1);
  7369. return this.finishNode(node, "ArrayPattern");
  7370. }
  7371. case 5:
  7372. return this.parseObjectLike(8, true);
  7373. case 88:
  7374. return this.parseVoidPattern(null);
  7375. }
  7376. return this.parseIdentifier();
  7377. }
  7378. parseBindingList(close, closeCharCode, flags) {
  7379. const allowEmpty = flags & 1;
  7380. const elts = [];
  7381. let first = true;
  7382. while (!this.eat(close)) {
  7383. if (first) {
  7384. first = false;
  7385. } else {
  7386. this.expect(12);
  7387. }
  7388. if (allowEmpty && this.match(12)) {
  7389. elts.push(null);
  7390. } else if (this.eat(close)) {
  7391. break;
  7392. } else if (this.match(21)) {
  7393. let rest = this.parseRestBinding();
  7394. if (this.hasPlugin("flow") || flags & 2) {
  7395. rest = this.parseFunctionParamType(rest);
  7396. }
  7397. elts.push(rest);
  7398. if (!this.checkCommaAfterRest(closeCharCode)) {
  7399. this.expect(close);
  7400. break;
  7401. }
  7402. } else {
  7403. const decorators = [];
  7404. if (flags & 2) {
  7405. if (this.match(26) && this.hasPlugin("decorators")) {
  7406. this.raise(Errors.UnsupportedParameterDecorator, this.state.startLoc);
  7407. }
  7408. while (this.match(26)) {
  7409. decorators.push(this.parseDecorator());
  7410. }
  7411. }
  7412. elts.push(this.parseBindingElement(flags, decorators));
  7413. }
  7414. }
  7415. return elts;
  7416. }
  7417. parseBindingRestProperty(prop) {
  7418. this.next();
  7419. if (this.hasPlugin("discardBinding") && this.match(88)) {
  7420. prop.argument = this.parseVoidPattern(null);
  7421. this.raise(Errors.UnexpectedVoidPattern, prop.argument);
  7422. } else {
  7423. prop.argument = this.parseIdentifier();
  7424. }
  7425. this.checkCommaAfterRest(125);
  7426. return this.finishNode(prop, "RestElement");
  7427. }
  7428. parseBindingProperty() {
  7429. const {
  7430. type,
  7431. startLoc
  7432. } = this.state;
  7433. if (type === 21) {
  7434. return this.parseBindingRestProperty(this.startNode());
  7435. }
  7436. const prop = this.startNode();
  7437. if (type === 139) {
  7438. this.expectPlugin("destructuringPrivate", startLoc);
  7439. this.classScope.usePrivateName(this.state.value, startLoc);
  7440. prop.key = this.parsePrivateName();
  7441. } else {
  7442. this.parsePropertyName(prop);
  7443. }
  7444. prop.method = false;
  7445. return this.parseObjPropValue(prop, startLoc, false, false, true, false);
  7446. }
  7447. parseBindingElement(flags, decorators) {
  7448. const left = this.parseMaybeDefault();
  7449. if (this.hasPlugin("flow") || flags & 2) {
  7450. this.parseFunctionParamType(left);
  7451. }
  7452. if (decorators.length) {
  7453. left.decorators = decorators;
  7454. this.resetStartLocationFromNode(left, decorators[0]);
  7455. }
  7456. const elt = this.parseMaybeDefault(left.loc.start, left);
  7457. return elt;
  7458. }
  7459. parseFunctionParamType(param) {
  7460. return param;
  7461. }
  7462. parseMaybeDefault(startLoc, left) {
  7463. startLoc != null ? startLoc : startLoc = this.state.startLoc;
  7464. left = left != null ? left : this.parseBindingAtom();
  7465. if (!this.eat(29)) return left;
  7466. const node = this.startNodeAt(startLoc);
  7467. if (left.type === "VoidPattern") {
  7468. this.raise(Errors.VoidPatternInitializer, left);
  7469. }
  7470. node.left = left;
  7471. node.right = this.parseMaybeAssignAllowIn();
  7472. return this.finishNode(node, "AssignmentPattern");
  7473. }
  7474. isValidLVal(type, disallowCallExpression, isUnparenthesizedInAssign, binding) {
  7475. switch (type) {
  7476. case "AssignmentPattern":
  7477. return "left";
  7478. case "RestElement":
  7479. return "argument";
  7480. case "ObjectProperty":
  7481. return "value";
  7482. case "ParenthesizedExpression":
  7483. return "expression";
  7484. case "ArrayPattern":
  7485. return "elements";
  7486. case "ObjectPattern":
  7487. return "properties";
  7488. case "VoidPattern":
  7489. return true;
  7490. case "CallExpression":
  7491. if (!disallowCallExpression && !this.state.strict && this.optionFlags & 8192) {
  7492. return true;
  7493. }
  7494. }
  7495. return false;
  7496. }
  7497. isOptionalMemberExpression(expression) {
  7498. return expression.type === "OptionalMemberExpression";
  7499. }
  7500. checkLVal(expression, ancestor, binding = 64, checkClashes = false, strictModeChanged = false, hasParenthesizedAncestor = false, disallowCallExpression = false) {
  7501. var _expression$extra;
  7502. const type = expression.type;
  7503. if (this.isObjectMethod(expression)) return;
  7504. const isOptionalMemberExpression = this.isOptionalMemberExpression(expression);
  7505. if (isOptionalMemberExpression || type === "MemberExpression") {
  7506. if (isOptionalMemberExpression) {
  7507. this.expectPlugin("optionalChainingAssign", expression.loc.start);
  7508. if (ancestor.type !== "AssignmentExpression") {
  7509. this.raise(Errors.InvalidLhsOptionalChaining, expression, {
  7510. ancestor
  7511. });
  7512. }
  7513. }
  7514. if (binding !== 64) {
  7515. this.raise(Errors.InvalidPropertyBindingPattern, expression);
  7516. }
  7517. return;
  7518. }
  7519. if (type === "Identifier") {
  7520. this.checkIdentifier(expression, binding, strictModeChanged);
  7521. const {
  7522. name
  7523. } = expression;
  7524. if (checkClashes) {
  7525. if (checkClashes.has(name)) {
  7526. this.raise(Errors.ParamDupe, expression);
  7527. } else {
  7528. checkClashes.add(name);
  7529. }
  7530. }
  7531. return;
  7532. } else if (type === "VoidPattern" && ancestor.type === "CatchClause") {
  7533. this.raise(Errors.VoidPatternCatchClauseParam, expression);
  7534. }
  7535. const unwrappedExpression = unwrapParenthesizedExpression(expression);
  7536. disallowCallExpression || (disallowCallExpression = unwrappedExpression.type === "CallExpression" && (unwrappedExpression.callee.type === "Import" || unwrappedExpression.callee.type === "Super"));
  7537. const validity = this.isValidLVal(type, disallowCallExpression, !(hasParenthesizedAncestor || (_expression$extra = expression.extra) != null && _expression$extra.parenthesized) && ancestor.type === "AssignmentExpression", binding);
  7538. if (validity === true) return;
  7539. if (validity === false) {
  7540. const ParseErrorClass = binding === 64 ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
  7541. this.raise(ParseErrorClass, expression, {
  7542. ancestor
  7543. });
  7544. return;
  7545. }
  7546. let key, isParenthesizedExpression;
  7547. if (typeof validity === "string") {
  7548. key = validity;
  7549. isParenthesizedExpression = type === "ParenthesizedExpression";
  7550. } else {
  7551. [key, isParenthesizedExpression] = validity;
  7552. }
  7553. const nextAncestor = type === "ArrayPattern" || type === "ObjectPattern" ? {
  7554. type
  7555. } : ancestor;
  7556. const val = expression[key];
  7557. if (Array.isArray(val)) {
  7558. for (const child of val) {
  7559. if (child) {
  7560. this.checkLVal(child, nextAncestor, binding, checkClashes, strictModeChanged, isParenthesizedExpression, true);
  7561. }
  7562. }
  7563. } else if (val) {
  7564. this.checkLVal(val, nextAncestor, binding, checkClashes, strictModeChanged, isParenthesizedExpression, disallowCallExpression);
  7565. }
  7566. }
  7567. checkIdentifier(at, bindingType, strictModeChanged = false) {
  7568. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(at.name, this.inModule) : isStrictBindOnlyReservedWord(at.name))) {
  7569. if (bindingType === 64) {
  7570. this.raise(Errors.StrictEvalArguments, at, {
  7571. referenceName: at.name
  7572. });
  7573. } else {
  7574. this.raise(Errors.StrictEvalArgumentsBinding, at, {
  7575. bindingName: at.name
  7576. });
  7577. }
  7578. }
  7579. if (bindingType & 8192 && at.name === "let") {
  7580. this.raise(Errors.LetInLexicalBinding, at);
  7581. }
  7582. if (!(bindingType & 64)) {
  7583. this.declareNameFromIdentifier(at, bindingType);
  7584. }
  7585. }
  7586. declareNameFromIdentifier(identifier, binding) {
  7587. this.scope.declareName(identifier.name, binding, identifier.loc.start);
  7588. }
  7589. checkToRestConversion(node, allowPattern) {
  7590. switch (node.type) {
  7591. case "ParenthesizedExpression":
  7592. this.checkToRestConversion(node.expression, allowPattern);
  7593. break;
  7594. case "Identifier":
  7595. case "MemberExpression":
  7596. break;
  7597. case "ArrayExpression":
  7598. case "ObjectExpression":
  7599. if (allowPattern) break;
  7600. default:
  7601. this.raise(Errors.InvalidRestAssignmentPattern, node);
  7602. }
  7603. }
  7604. checkCommaAfterRest(close) {
  7605. if (!this.match(12)) {
  7606. return false;
  7607. }
  7608. this.raise(this.lookaheadCharCode() === close ? Errors.RestTrailingComma : Errors.ElementAfterRest, this.state.startLoc);
  7609. return true;
  7610. }
  7611. }
  7612. const keywordAndTSRelationalOperator = /in(?:stanceof)?|as|satisfies/y;
  7613. function nonNull(x) {
  7614. if (x == null) {
  7615. throw new Error(`Unexpected ${x} value.`);
  7616. }
  7617. return x;
  7618. }
  7619. function assert(x) {
  7620. if (!x) {
  7621. throw new Error("Assert fail");
  7622. }
  7623. }
  7624. const TSErrors = ParseErrorEnum`typescript`({
  7625. AbstractMethodHasImplementation: ({
  7626. methodName
  7627. }) => `Method '${methodName}' cannot have an implementation because it is marked abstract.`,
  7628. AbstractPropertyHasInitializer: ({
  7629. propertyName
  7630. }) => `Property '${propertyName}' cannot have an initializer because it is marked abstract.`,
  7631. AccessorCannotBeOptional: "An 'accessor' property cannot be declared optional.",
  7632. AccessorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  7633. AccessorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  7634. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  7635. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  7636. ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference: "A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.",
  7637. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  7638. DeclareAccessor: ({
  7639. kind
  7640. }) => `'declare' is not allowed in ${kind}ters.`,
  7641. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  7642. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  7643. DuplicateAccessibilityModifier: ({
  7644. modifier
  7645. }) => `Accessibility modifier already seen: '${modifier}'.`,
  7646. DuplicateModifier: ({
  7647. modifier
  7648. }) => `Duplicate modifier: '${modifier}'.`,
  7649. EmptyHeritageClauseType: ({
  7650. token
  7651. }) => `'${token}' list cannot be empty.`,
  7652. EmptyTypeArguments: "Type argument list cannot be empty.",
  7653. EmptyTypeParameters: "Type parameter list cannot be empty.",
  7654. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  7655. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  7656. ImportReflectionHasImportType: "An `import module` declaration can not use `type` modifier",
  7657. IncompatibleModifiers: ({
  7658. modifiers
  7659. }) => `'${modifiers[0]}' modifier cannot be used with '${modifiers[1]}' modifier.`,
  7660. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  7661. IndexSignatureHasAccessibility: ({
  7662. modifier
  7663. }) => `Index signatures cannot have an accessibility modifier ('${modifier}').`,
  7664. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  7665. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  7666. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  7667. InitializerNotAllowedInAmbientContext: "Initializers are not allowed in ambient contexts.",
  7668. InvalidHeritageClauseType: ({
  7669. token
  7670. }) => `'${token}' list can only include identifiers or qualified-names with optional type arguments.`,
  7671. InvalidModifierOnAwaitUsingDeclaration: modifier => `'${modifier}' modifier cannot appear on an await using declaration.`,
  7672. InvalidModifierOnTypeMember: ({
  7673. modifier
  7674. }) => `'${modifier}' modifier cannot appear on a type member.`,
  7675. InvalidModifierOnTypeParameter: ({
  7676. modifier
  7677. }) => `'${modifier}' modifier cannot appear on a type parameter.`,
  7678. InvalidModifierOnTypeParameterPositions: ({
  7679. modifier
  7680. }) => `'${modifier}' modifier can only appear on a type parameter of a class, interface or type alias.`,
  7681. InvalidModifierOnUsingDeclaration: modifier => `'${modifier}' modifier cannot appear on a using declaration.`,
  7682. InvalidModifiersOrder: ({
  7683. orderedModifiers
  7684. }) => `'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
  7685. InvalidPropertyAccessAfterInstantiationExpression: "Invalid property access after an instantiation expression. " + "You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
  7686. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  7687. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  7688. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  7689. NonClassMethodPropertyHasAbstractModifier: "'abstract' modifier can only appear on a class, method, or property declaration.",
  7690. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  7691. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  7692. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  7693. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  7694. PrivateElementHasAccessibility: ({
  7695. modifier
  7696. }) => `Private elements cannot have an accessibility modifier ('${modifier}').`,
  7697. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  7698. ReservedArrowTypeParam: "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`.",
  7699. ReservedTypeAssertion: "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.",
  7700. SetAccessorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  7701. SetAccessorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  7702. SetAccessorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  7703. SingleTypeParameterWithoutTrailingComma: ({
  7704. typeParameterName
  7705. }) => `Single type parameter ${typeParameterName} should have a trailing comma. Example usage: <${typeParameterName},>.`,
  7706. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  7707. TupleOptionalAfterType: "A labeled tuple optional element must be declared using a question mark after the name and before the colon (`name?: type`), rather than after the type (`name: type?`).",
  7708. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  7709. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  7710. TypeModifierIsUsedInTypeExports: "The 'type' modifier cannot be used on a named export when 'export type' is used on its export statement.",
  7711. TypeModifierIsUsedInTypeImports: "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.",
  7712. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  7713. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  7714. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  7715. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  7716. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  7717. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  7718. UnsupportedSignatureParameterKind: ({
  7719. type
  7720. }) => `Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got ${type}.`,
  7721. UsingDeclarationInAmbientContext: kind => `'${kind}' declarations are not allowed in ambient contexts.`
  7722. });
  7723. function keywordTypeFromName(value) {
  7724. switch (value) {
  7725. case "any":
  7726. return "TSAnyKeyword";
  7727. case "boolean":
  7728. return "TSBooleanKeyword";
  7729. case "bigint":
  7730. return "TSBigIntKeyword";
  7731. case "never":
  7732. return "TSNeverKeyword";
  7733. case "number":
  7734. return "TSNumberKeyword";
  7735. case "object":
  7736. return "TSObjectKeyword";
  7737. case "string":
  7738. return "TSStringKeyword";
  7739. case "symbol":
  7740. return "TSSymbolKeyword";
  7741. case "undefined":
  7742. return "TSUndefinedKeyword";
  7743. case "unknown":
  7744. return "TSUnknownKeyword";
  7745. default:
  7746. return undefined;
  7747. }
  7748. }
  7749. function tsIsAccessModifier(modifier) {
  7750. return modifier === "private" || modifier === "public" || modifier === "protected";
  7751. }
  7752. function tsIsVarianceAnnotations(modifier) {
  7753. return modifier === "in" || modifier === "out";
  7754. }
  7755. var typescript = superClass => class TypeScriptParserMixin extends superClass {
  7756. constructor(...args) {
  7757. super(...args);
  7758. this.tsParseInOutModifiers = this.tsParseModifiers.bind(this, {
  7759. allowedModifiers: ["in", "out"],
  7760. disallowedModifiers: ["const", "public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7761. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7762. });
  7763. this.tsParseConstModifier = this.tsParseModifiers.bind(this, {
  7764. allowedModifiers: ["const"],
  7765. disallowedModifiers: ["in", "out"],
  7766. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  7767. });
  7768. this.tsParseInOutConstModifiers = this.tsParseModifiers.bind(this, {
  7769. allowedModifiers: ["in", "out", "const"],
  7770. disallowedModifiers: ["public", "private", "protected", "readonly", "declare", "abstract", "override"],
  7771. errorTemplate: TSErrors.InvalidModifierOnTypeParameter
  7772. });
  7773. }
  7774. getScopeHandler() {
  7775. return TypeScriptScopeHandler;
  7776. }
  7777. tsIsIdentifier() {
  7778. return tokenIsIdentifier(this.state.type);
  7779. }
  7780. tsTokenCanFollowModifier() {
  7781. return this.match(0) || this.match(5) || this.match(55) || this.match(21) || this.match(139) || this.isLiteralPropertyName();
  7782. }
  7783. tsNextTokenOnSameLineAndCanFollowModifier() {
  7784. this.next();
  7785. if (this.hasPrecedingLineBreak()) {
  7786. return false;
  7787. }
  7788. return this.tsTokenCanFollowModifier();
  7789. }
  7790. tsNextTokenCanFollowModifier() {
  7791. if (this.match(106)) {
  7792. this.next();
  7793. return this.tsTokenCanFollowModifier();
  7794. }
  7795. return this.tsNextTokenOnSameLineAndCanFollowModifier();
  7796. }
  7797. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock, hasSeenStaticModifier) {
  7798. if (!tokenIsIdentifier(this.state.type) && this.state.type !== 58 && this.state.type !== 75) {
  7799. return undefined;
  7800. }
  7801. const modifier = this.state.value;
  7802. if (allowedModifiers.includes(modifier)) {
  7803. if (hasSeenStaticModifier && this.match(106)) {
  7804. return undefined;
  7805. }
  7806. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  7807. return undefined;
  7808. }
  7809. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  7810. return modifier;
  7811. }
  7812. }
  7813. return undefined;
  7814. }
  7815. tsParseModifiers({
  7816. allowedModifiers,
  7817. disallowedModifiers,
  7818. stopOnStartOfClassStaticBlock,
  7819. errorTemplate = TSErrors.InvalidModifierOnTypeMember
  7820. }, modified) {
  7821. const enforceOrder = (loc, modifier, before, after) => {
  7822. if (modifier === before && modified[after]) {
  7823. this.raise(TSErrors.InvalidModifiersOrder, loc, {
  7824. orderedModifiers: [before, after]
  7825. });
  7826. }
  7827. };
  7828. const incompatible = (loc, modifier, mod1, mod2) => {
  7829. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  7830. this.raise(TSErrors.IncompatibleModifiers, loc, {
  7831. modifiers: [mod1, mod2]
  7832. });
  7833. }
  7834. };
  7835. for (;;) {
  7836. const {
  7837. startLoc
  7838. } = this.state;
  7839. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock, modified.static);
  7840. if (!modifier) break;
  7841. if (tsIsAccessModifier(modifier)) {
  7842. if (modified.accessibility) {
  7843. this.raise(TSErrors.DuplicateAccessibilityModifier, startLoc, {
  7844. modifier
  7845. });
  7846. } else {
  7847. enforceOrder(startLoc, modifier, modifier, "override");
  7848. enforceOrder(startLoc, modifier, modifier, "static");
  7849. enforceOrder(startLoc, modifier, modifier, "readonly");
  7850. modified.accessibility = modifier;
  7851. }
  7852. } else if (tsIsVarianceAnnotations(modifier)) {
  7853. if (modified[modifier]) {
  7854. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7855. modifier
  7856. });
  7857. }
  7858. modified[modifier] = true;
  7859. enforceOrder(startLoc, modifier, "in", "out");
  7860. } else {
  7861. if (hasOwnProperty.call(modified, modifier)) {
  7862. this.raise(TSErrors.DuplicateModifier, startLoc, {
  7863. modifier
  7864. });
  7865. } else {
  7866. enforceOrder(startLoc, modifier, "static", "readonly");
  7867. enforceOrder(startLoc, modifier, "static", "override");
  7868. enforceOrder(startLoc, modifier, "override", "readonly");
  7869. enforceOrder(startLoc, modifier, "abstract", "override");
  7870. incompatible(startLoc, modifier, "declare", "override");
  7871. incompatible(startLoc, modifier, "static", "abstract");
  7872. }
  7873. modified[modifier] = true;
  7874. }
  7875. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  7876. this.raise(errorTemplate, startLoc, {
  7877. modifier
  7878. });
  7879. }
  7880. }
  7881. }
  7882. tsIsListTerminator(kind) {
  7883. switch (kind) {
  7884. case "EnumMembers":
  7885. case "TypeMembers":
  7886. return this.match(8);
  7887. case "HeritageClauseElement":
  7888. return this.match(5);
  7889. case "TupleElementTypes":
  7890. return this.match(3);
  7891. case "TypeParametersOrArguments":
  7892. return this.match(48);
  7893. }
  7894. }
  7895. tsParseList(kind, parseElement) {
  7896. const result = [];
  7897. while (!this.tsIsListTerminator(kind)) {
  7898. result.push(parseElement());
  7899. }
  7900. return result;
  7901. }
  7902. tsParseDelimitedList(kind, parseElement, refTrailingCommaPos) {
  7903. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true, refTrailingCommaPos));
  7904. }
  7905. tsParseDelimitedListWorker(kind, parseElement, expectSuccess, refTrailingCommaPos) {
  7906. const result = [];
  7907. let trailingCommaPos = -1;
  7908. for (;;) {
  7909. if (this.tsIsListTerminator(kind)) {
  7910. break;
  7911. }
  7912. trailingCommaPos = -1;
  7913. const element = parseElement();
  7914. if (element == null) {
  7915. return undefined;
  7916. }
  7917. result.push(element);
  7918. if (this.eat(12)) {
  7919. trailingCommaPos = this.state.lastTokStartLoc.index;
  7920. continue;
  7921. }
  7922. if (this.tsIsListTerminator(kind)) {
  7923. break;
  7924. }
  7925. if (expectSuccess) {
  7926. this.expect(12);
  7927. }
  7928. return undefined;
  7929. }
  7930. if (refTrailingCommaPos) {
  7931. refTrailingCommaPos.value = trailingCommaPos;
  7932. }
  7933. return result;
  7934. }
  7935. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken, refTrailingCommaPos) {
  7936. if (!skipFirstToken) {
  7937. if (bracket) {
  7938. this.expect(0);
  7939. } else {
  7940. this.expect(47);
  7941. }
  7942. }
  7943. const result = this.tsParseDelimitedList(kind, parseElement, refTrailingCommaPos);
  7944. if (bracket) {
  7945. this.expect(3);
  7946. } else {
  7947. this.expect(48);
  7948. }
  7949. return result;
  7950. }
  7951. tsParseImportType() {
  7952. const node = this.startNode();
  7953. this.expect(83);
  7954. this.expect(10);
  7955. if (!this.match(134)) {
  7956. this.raise(TSErrors.UnsupportedImportTypeArgument, this.state.startLoc);
  7957. node.argument = super.parseExprAtom();
  7958. } else {
  7959. node.argument = this.parseStringLiteral(this.state.value);
  7960. }
  7961. if (this.eat(12)) {
  7962. node.options = this.tsParseImportTypeOptions();
  7963. } else {
  7964. node.options = null;
  7965. }
  7966. this.expect(11);
  7967. if (this.eat(16)) {
  7968. node.qualifier = this.tsParseEntityName(1 | 2);
  7969. }
  7970. if (this.match(47)) {
  7971. node.typeParameters = this.tsParseTypeArguments();
  7972. }
  7973. return this.finishNode(node, "TSImportType");
  7974. }
  7975. tsParseImportTypeOptions() {
  7976. const node = this.startNode();
  7977. this.expect(5);
  7978. const withProperty = this.startNode();
  7979. if (this.isContextual(76)) {
  7980. withProperty.method = false;
  7981. withProperty.key = this.parseIdentifier(true);
  7982. withProperty.computed = false;
  7983. withProperty.shorthand = false;
  7984. } else {
  7985. this.unexpected(null, 76);
  7986. }
  7987. this.expect(14);
  7988. withProperty.value = this.tsParseImportTypeWithPropertyValue();
  7989. node.properties = [this.finishObjectProperty(withProperty)];
  7990. this.eat(12);
  7991. this.expect(8);
  7992. return this.finishNode(node, "ObjectExpression");
  7993. }
  7994. tsParseImportTypeWithPropertyValue() {
  7995. const node = this.startNode();
  7996. const properties = [];
  7997. this.expect(5);
  7998. while (!this.match(8)) {
  7999. const type = this.state.type;
  8000. if (tokenIsIdentifier(type) || type === 134) {
  8001. properties.push(super.parsePropertyDefinition(null));
  8002. } else {
  8003. this.unexpected();
  8004. }
  8005. this.eat(12);
  8006. }
  8007. node.properties = properties;
  8008. this.next();
  8009. return this.finishNode(node, "ObjectExpression");
  8010. }
  8011. tsParseEntityName(flags) {
  8012. let entity;
  8013. if (flags & 1 && this.match(78)) {
  8014. if (flags & 2) {
  8015. entity = this.parseIdentifier(true);
  8016. } else {
  8017. const node = this.startNode();
  8018. this.next();
  8019. entity = this.finishNode(node, "ThisExpression");
  8020. }
  8021. } else {
  8022. entity = this.parseIdentifier(!!(flags & 1));
  8023. }
  8024. while (this.eat(16)) {
  8025. const node = this.startNodeAtNode(entity);
  8026. node.left = entity;
  8027. node.right = this.parseIdentifier(!!(flags & 1));
  8028. entity = this.finishNode(node, "TSQualifiedName");
  8029. }
  8030. return entity;
  8031. }
  8032. tsParseTypeReference() {
  8033. const node = this.startNode();
  8034. node.typeName = this.tsParseEntityName(1);
  8035. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  8036. node.typeParameters = this.tsParseTypeArguments();
  8037. }
  8038. return this.finishNode(node, "TSTypeReference");
  8039. }
  8040. tsParseThisTypePredicate(lhs) {
  8041. this.next();
  8042. const node = this.startNodeAtNode(lhs);
  8043. node.parameterName = lhs;
  8044. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  8045. node.asserts = false;
  8046. return this.finishNode(node, "TSTypePredicate");
  8047. }
  8048. tsParseThisTypeNode() {
  8049. const node = this.startNode();
  8050. this.next();
  8051. return this.finishNode(node, "TSThisType");
  8052. }
  8053. tsParseTypeQuery() {
  8054. const node = this.startNode();
  8055. this.expect(87);
  8056. if (this.match(83)) {
  8057. node.exprName = this.tsParseImportType();
  8058. } else {
  8059. node.exprName = this.tsParseEntityName(1 | 2);
  8060. }
  8061. if (!this.hasPrecedingLineBreak() && this.match(47)) {
  8062. node.typeParameters = this.tsParseTypeArguments();
  8063. }
  8064. return this.finishNode(node, "TSTypeQuery");
  8065. }
  8066. tsParseTypeParameter(parseModifiers) {
  8067. const node = this.startNode();
  8068. parseModifiers(node);
  8069. node.name = this.tsParseTypeParameterName();
  8070. node.constraint = this.tsEatThenParseType(81);
  8071. node.default = this.tsEatThenParseType(29);
  8072. return this.finishNode(node, "TSTypeParameter");
  8073. }
  8074. tsTryParseTypeParameters(parseModifiers) {
  8075. if (this.match(47)) {
  8076. return this.tsParseTypeParameters(parseModifiers);
  8077. }
  8078. }
  8079. tsParseTypeParameters(parseModifiers) {
  8080. const node = this.startNode();
  8081. if (this.match(47) || this.match(143)) {
  8082. this.next();
  8083. } else {
  8084. this.unexpected();
  8085. }
  8086. const refTrailingCommaPos = {
  8087. value: -1
  8088. };
  8089. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this, parseModifiers), false, true, refTrailingCommaPos);
  8090. if (node.params.length === 0) {
  8091. this.raise(TSErrors.EmptyTypeParameters, node);
  8092. }
  8093. if (refTrailingCommaPos.value !== -1) {
  8094. this.addExtra(node, "trailingComma", refTrailingCommaPos.value);
  8095. }
  8096. return this.finishNode(node, "TSTypeParameterDeclaration");
  8097. }
  8098. tsFillSignature(returnToken, signature) {
  8099. const returnTokenRequired = returnToken === 19;
  8100. const paramsKey = "parameters";
  8101. const returnTypeKey = "typeAnnotation";
  8102. signature.typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  8103. this.expect(10);
  8104. signature[paramsKey] = this.tsParseBindingListForSignature();
  8105. if (returnTokenRequired) {
  8106. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  8107. } else if (this.match(returnToken)) {
  8108. signature[returnTypeKey] = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  8109. }
  8110. }
  8111. tsParseBindingListForSignature() {
  8112. const list = super.parseBindingList(11, 41, 2);
  8113. for (const pattern of list) {
  8114. const {
  8115. type
  8116. } = pattern;
  8117. if (type === "AssignmentPattern" || type === "TSParameterProperty") {
  8118. this.raise(TSErrors.UnsupportedSignatureParameterKind, pattern, {
  8119. type
  8120. });
  8121. }
  8122. }
  8123. return list;
  8124. }
  8125. tsParseTypeMemberSemicolon() {
  8126. if (!this.eat(12) && !this.isLineTerminator()) {
  8127. this.expect(13);
  8128. }
  8129. }
  8130. tsParseSignatureMember(kind, node) {
  8131. this.tsFillSignature(14, node);
  8132. this.tsParseTypeMemberSemicolon();
  8133. return this.finishNode(node, kind);
  8134. }
  8135. tsIsUnambiguouslyIndexSignature() {
  8136. this.next();
  8137. if (tokenIsIdentifier(this.state.type)) {
  8138. this.next();
  8139. return this.match(14);
  8140. }
  8141. return false;
  8142. }
  8143. tsTryParseIndexSignature(node) {
  8144. if (!(this.match(0) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  8145. return;
  8146. }
  8147. this.expect(0);
  8148. const id = this.parseIdentifier();
  8149. id.typeAnnotation = this.tsParseTypeAnnotation();
  8150. this.resetEndLocation(id);
  8151. this.expect(3);
  8152. node.parameters = [id];
  8153. const type = this.tsTryParseTypeAnnotation();
  8154. if (type) node.typeAnnotation = type;
  8155. this.tsParseTypeMemberSemicolon();
  8156. return this.finishNode(node, "TSIndexSignature");
  8157. }
  8158. tsParsePropertyOrMethodSignature(node, readonly) {
  8159. if (this.eat(17)) node.optional = true;
  8160. if (this.match(10) || this.match(47)) {
  8161. if (readonly) {
  8162. this.raise(TSErrors.ReadonlyForMethodSignature, node);
  8163. }
  8164. const method = node;
  8165. if (method.kind && this.match(47)) {
  8166. this.raise(TSErrors.AccessorCannotHaveTypeParameters, this.state.curPosition());
  8167. }
  8168. this.tsFillSignature(14, method);
  8169. this.tsParseTypeMemberSemicolon();
  8170. const paramsKey = "parameters";
  8171. const returnTypeKey = "typeAnnotation";
  8172. if (method.kind === "get") {
  8173. if (method[paramsKey].length > 0) {
  8174. this.raise(Errors.BadGetterArity, this.state.curPosition());
  8175. if (this.isThisParam(method[paramsKey][0])) {
  8176. this.raise(TSErrors.AccessorCannotDeclareThisParameter, this.state.curPosition());
  8177. }
  8178. }
  8179. } else if (method.kind === "set") {
  8180. if (method[paramsKey].length !== 1) {
  8181. this.raise(Errors.BadSetterArity, this.state.curPosition());
  8182. } else {
  8183. const firstParameter = method[paramsKey][0];
  8184. if (this.isThisParam(firstParameter)) {
  8185. this.raise(TSErrors.AccessorCannotDeclareThisParameter, this.state.curPosition());
  8186. }
  8187. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  8188. this.raise(TSErrors.SetAccessorCannotHaveOptionalParameter, this.state.curPosition());
  8189. }
  8190. if (firstParameter.type === "RestElement") {
  8191. this.raise(TSErrors.SetAccessorCannotHaveRestParameter, this.state.curPosition());
  8192. }
  8193. }
  8194. if (method[returnTypeKey]) {
  8195. this.raise(TSErrors.SetAccessorCannotHaveReturnType, method[returnTypeKey]);
  8196. }
  8197. } else {
  8198. method.kind = "method";
  8199. }
  8200. return this.finishNode(method, "TSMethodSignature");
  8201. } else {
  8202. const property = node;
  8203. if (readonly) property.readonly = true;
  8204. const type = this.tsTryParseTypeAnnotation();
  8205. if (type) property.typeAnnotation = type;
  8206. this.tsParseTypeMemberSemicolon();
  8207. return this.finishNode(property, "TSPropertySignature");
  8208. }
  8209. }
  8210. tsParseTypeMember() {
  8211. const node = this.startNode();
  8212. if (this.match(10) || this.match(47)) {
  8213. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  8214. }
  8215. if (this.match(77)) {
  8216. const id = this.startNode();
  8217. this.next();
  8218. if (this.match(10) || this.match(47)) {
  8219. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  8220. } else {
  8221. node.key = this.createIdentifier(id, "new");
  8222. return this.tsParsePropertyOrMethodSignature(node, false);
  8223. }
  8224. }
  8225. this.tsParseModifiers({
  8226. allowedModifiers: ["readonly"],
  8227. disallowedModifiers: ["declare", "abstract", "private", "protected", "public", "static", "override"]
  8228. }, node);
  8229. const idx = this.tsTryParseIndexSignature(node);
  8230. if (idx) {
  8231. return idx;
  8232. }
  8233. super.parsePropertyName(node);
  8234. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  8235. node.kind = node.key.name;
  8236. super.parsePropertyName(node);
  8237. if (!this.match(10) && !this.match(47)) {
  8238. this.unexpected(null, 10);
  8239. }
  8240. }
  8241. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  8242. }
  8243. tsParseTypeLiteral() {
  8244. const node = this.startNode();
  8245. node.members = this.tsParseObjectTypeMembers();
  8246. return this.finishNode(node, "TSTypeLiteral");
  8247. }
  8248. tsParseObjectTypeMembers() {
  8249. this.expect(5);
  8250. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  8251. this.expect(8);
  8252. return members;
  8253. }
  8254. tsIsStartOfMappedType() {
  8255. this.next();
  8256. if (this.eat(53)) {
  8257. return this.isContextual(122);
  8258. }
  8259. if (this.isContextual(122)) {
  8260. this.next();
  8261. }
  8262. if (!this.match(0)) {
  8263. return false;
  8264. }
  8265. this.next();
  8266. if (!this.tsIsIdentifier()) {
  8267. return false;
  8268. }
  8269. this.next();
  8270. return this.match(58);
  8271. }
  8272. tsParseMappedType() {
  8273. const node = this.startNode();
  8274. this.expect(5);
  8275. if (this.match(53)) {
  8276. node.readonly = this.state.value;
  8277. this.next();
  8278. this.expectContextual(122);
  8279. } else if (this.eatContextual(122)) {
  8280. node.readonly = true;
  8281. }
  8282. this.expect(0);
  8283. const typeParameter = this.startNode();
  8284. typeParameter.name = this.tsParseTypeParameterName();
  8285. typeParameter.constraint = this.tsExpectThenParseType(58);
  8286. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8287. node.nameType = this.eatContextual(93) ? this.tsParseType() : null;
  8288. this.expect(3);
  8289. if (this.match(53)) {
  8290. node.optional = this.state.value;
  8291. this.next();
  8292. this.expect(17);
  8293. } else if (this.eat(17)) {
  8294. node.optional = true;
  8295. }
  8296. node.typeAnnotation = this.tsTryParseType();
  8297. this.semicolon();
  8298. this.expect(8);
  8299. return this.finishNode(node, "TSMappedType");
  8300. }
  8301. tsParseTupleType() {
  8302. const node = this.startNode();
  8303. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  8304. let seenOptionalElement = false;
  8305. node.elementTypes.forEach(elementNode => {
  8306. const {
  8307. type
  8308. } = elementNode;
  8309. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  8310. this.raise(TSErrors.OptionalTypeBeforeRequired, elementNode);
  8311. }
  8312. seenOptionalElement || (seenOptionalElement = type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType");
  8313. });
  8314. return this.finishNode(node, "TSTupleType");
  8315. }
  8316. tsParseTupleElementType() {
  8317. const restStartLoc = this.state.startLoc;
  8318. const rest = this.eat(21);
  8319. const {
  8320. startLoc
  8321. } = this.state;
  8322. let labeled;
  8323. let label;
  8324. let optional;
  8325. let type;
  8326. const isWord = tokenIsKeywordOrIdentifier(this.state.type);
  8327. const chAfterWord = isWord ? this.lookaheadCharCode() : null;
  8328. if (chAfterWord === 58) {
  8329. labeled = true;
  8330. optional = false;
  8331. label = this.parseIdentifier(true);
  8332. this.expect(14);
  8333. type = this.tsParseType();
  8334. } else if (chAfterWord === 63) {
  8335. optional = true;
  8336. const wordName = this.state.value;
  8337. const typeOrLabel = this.tsParseNonArrayType();
  8338. if (this.lookaheadCharCode() === 58) {
  8339. labeled = true;
  8340. label = this.createIdentifier(this.startNodeAt(startLoc), wordName);
  8341. this.expect(17);
  8342. this.expect(14);
  8343. type = this.tsParseType();
  8344. } else {
  8345. labeled = false;
  8346. type = typeOrLabel;
  8347. this.expect(17);
  8348. }
  8349. } else {
  8350. type = this.tsParseType();
  8351. optional = this.eat(17);
  8352. labeled = this.eat(14);
  8353. }
  8354. if (labeled) {
  8355. let labeledNode;
  8356. if (label) {
  8357. labeledNode = this.startNodeAt(startLoc);
  8358. labeledNode.optional = optional;
  8359. labeledNode.label = label;
  8360. labeledNode.elementType = type;
  8361. if (this.eat(17)) {
  8362. labeledNode.optional = true;
  8363. this.raise(TSErrors.TupleOptionalAfterType, this.state.lastTokStartLoc);
  8364. }
  8365. } else {
  8366. labeledNode = this.startNodeAt(startLoc);
  8367. labeledNode.optional = optional;
  8368. this.raise(TSErrors.InvalidTupleMemberLabel, type);
  8369. labeledNode.label = type;
  8370. labeledNode.elementType = this.tsParseType();
  8371. }
  8372. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  8373. } else if (optional) {
  8374. const optionalTypeNode = this.startNodeAt(startLoc);
  8375. optionalTypeNode.typeAnnotation = type;
  8376. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  8377. }
  8378. if (rest) {
  8379. const restNode = this.startNodeAt(restStartLoc);
  8380. restNode.typeAnnotation = type;
  8381. type = this.finishNode(restNode, "TSRestType");
  8382. }
  8383. return type;
  8384. }
  8385. tsParseParenthesizedType() {
  8386. const node = this.startNode();
  8387. this.expect(10);
  8388. node.typeAnnotation = this.tsParseType();
  8389. this.expect(11);
  8390. return this.finishNode(node, "TSParenthesizedType");
  8391. }
  8392. tsParseFunctionOrConstructorType(type, abstract) {
  8393. const node = this.startNode();
  8394. if (type === "TSConstructorType") {
  8395. node.abstract = !!abstract;
  8396. if (abstract) this.next();
  8397. this.next();
  8398. }
  8399. this.tsInAllowConditionalTypesContext(() => this.tsFillSignature(19, node));
  8400. return this.finishNode(node, type);
  8401. }
  8402. tsParseLiteralTypeNode() {
  8403. const node = this.startNode();
  8404. switch (this.state.type) {
  8405. case 135:
  8406. case 136:
  8407. case 134:
  8408. case 85:
  8409. case 86:
  8410. node.literal = super.parseExprAtom();
  8411. break;
  8412. default:
  8413. this.unexpected();
  8414. }
  8415. return this.finishNode(node, "TSLiteralType");
  8416. }
  8417. tsParseTemplateLiteralType() {
  8418. const node = this.startNode();
  8419. node.literal = super.parseTemplate(false);
  8420. return this.finishNode(node, "TSLiteralType");
  8421. }
  8422. parseTemplateSubstitution() {
  8423. if (this.state.inType) return this.tsParseType();
  8424. return super.parseTemplateSubstitution();
  8425. }
  8426. tsParseThisTypeOrThisTypePredicate() {
  8427. const thisKeyword = this.tsParseThisTypeNode();
  8428. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8429. return this.tsParseThisTypePredicate(thisKeyword);
  8430. } else {
  8431. return thisKeyword;
  8432. }
  8433. }
  8434. tsParseNonArrayType() {
  8435. switch (this.state.type) {
  8436. case 134:
  8437. case 135:
  8438. case 136:
  8439. case 85:
  8440. case 86:
  8441. return this.tsParseLiteralTypeNode();
  8442. case 53:
  8443. if (this.state.value === "-") {
  8444. const node = this.startNode();
  8445. const nextToken = this.lookahead();
  8446. if (nextToken.type !== 135 && nextToken.type !== 136) {
  8447. this.unexpected();
  8448. }
  8449. node.literal = this.parseMaybeUnary();
  8450. return this.finishNode(node, "TSLiteralType");
  8451. }
  8452. break;
  8453. case 78:
  8454. return this.tsParseThisTypeOrThisTypePredicate();
  8455. case 87:
  8456. return this.tsParseTypeQuery();
  8457. case 83:
  8458. return this.tsParseImportType();
  8459. case 5:
  8460. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  8461. case 0:
  8462. return this.tsParseTupleType();
  8463. case 10:
  8464. return this.tsParseParenthesizedType();
  8465. case 25:
  8466. case 24:
  8467. return this.tsParseTemplateLiteralType();
  8468. default:
  8469. {
  8470. const {
  8471. type
  8472. } = this.state;
  8473. if (tokenIsIdentifier(type) || type === 88 || type === 84) {
  8474. const nodeType = type === 88 ? "TSVoidKeyword" : type === 84 ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  8475. if (nodeType !== undefined && this.lookaheadCharCode() !== 46) {
  8476. const node = this.startNode();
  8477. this.next();
  8478. return this.finishNode(node, nodeType);
  8479. }
  8480. return this.tsParseTypeReference();
  8481. }
  8482. }
  8483. }
  8484. throw this.unexpected();
  8485. }
  8486. tsParseArrayTypeOrHigher() {
  8487. const {
  8488. startLoc
  8489. } = this.state;
  8490. let type = this.tsParseNonArrayType();
  8491. while (!this.hasPrecedingLineBreak() && this.eat(0)) {
  8492. if (this.match(3)) {
  8493. const node = this.startNodeAt(startLoc);
  8494. node.elementType = type;
  8495. this.expect(3);
  8496. type = this.finishNode(node, "TSArrayType");
  8497. } else {
  8498. const node = this.startNodeAt(startLoc);
  8499. node.objectType = type;
  8500. node.indexType = this.tsParseType();
  8501. this.expect(3);
  8502. type = this.finishNode(node, "TSIndexedAccessType");
  8503. }
  8504. }
  8505. return type;
  8506. }
  8507. tsParseTypeOperator() {
  8508. const node = this.startNode();
  8509. const operator = this.state.value;
  8510. this.next();
  8511. node.operator = operator;
  8512. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  8513. if (operator === "readonly") {
  8514. this.tsCheckTypeAnnotationForReadOnly(node);
  8515. }
  8516. return this.finishNode(node, "TSTypeOperator");
  8517. }
  8518. tsCheckTypeAnnotationForReadOnly(node) {
  8519. switch (node.typeAnnotation.type) {
  8520. case "TSTupleType":
  8521. case "TSArrayType":
  8522. return;
  8523. default:
  8524. this.raise(TSErrors.UnexpectedReadonly, node);
  8525. }
  8526. }
  8527. tsParseInferType() {
  8528. const node = this.startNode();
  8529. this.expectContextual(115);
  8530. const typeParameter = this.startNode();
  8531. typeParameter.name = this.tsParseTypeParameterName();
  8532. typeParameter.constraint = this.tsTryParse(() => this.tsParseConstraintForInferType());
  8533. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  8534. return this.finishNode(node, "TSInferType");
  8535. }
  8536. tsParseConstraintForInferType() {
  8537. if (this.eat(81)) {
  8538. const constraint = this.tsInDisallowConditionalTypesContext(() => this.tsParseType());
  8539. if (this.state.inDisallowConditionalTypesContext || !this.match(17)) {
  8540. return constraint;
  8541. }
  8542. }
  8543. }
  8544. tsParseTypeOperatorOrHigher() {
  8545. const isTypeOperator = tokenIsTSTypeOperator(this.state.type) && !this.state.containsEsc;
  8546. return isTypeOperator ? this.tsParseTypeOperator() : this.isContextual(115) ? this.tsParseInferType() : this.tsInAllowConditionalTypesContext(() => this.tsParseArrayTypeOrHigher());
  8547. }
  8548. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  8549. const node = this.startNode();
  8550. const hasLeadingOperator = this.eat(operator);
  8551. const types = [];
  8552. do {
  8553. types.push(parseConstituentType());
  8554. } while (this.eat(operator));
  8555. if (types.length === 1 && !hasLeadingOperator) {
  8556. return types[0];
  8557. }
  8558. node.types = types;
  8559. return this.finishNode(node, kind);
  8560. }
  8561. tsParseIntersectionTypeOrHigher() {
  8562. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), 45);
  8563. }
  8564. tsParseUnionTypeOrHigher() {
  8565. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), 43);
  8566. }
  8567. tsIsStartOfFunctionType() {
  8568. if (this.match(47)) {
  8569. return true;
  8570. }
  8571. return this.match(10) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  8572. }
  8573. tsSkipParameterStart() {
  8574. if (tokenIsIdentifier(this.state.type) || this.match(78)) {
  8575. this.next();
  8576. return true;
  8577. }
  8578. if (this.match(5)) {
  8579. const {
  8580. errors
  8581. } = this.state;
  8582. const previousErrorCount = errors.length;
  8583. try {
  8584. this.parseObjectLike(8, true);
  8585. return errors.length === previousErrorCount;
  8586. } catch (_unused) {
  8587. return false;
  8588. }
  8589. }
  8590. if (this.match(0)) {
  8591. this.next();
  8592. const {
  8593. errors
  8594. } = this.state;
  8595. const previousErrorCount = errors.length;
  8596. try {
  8597. super.parseBindingList(3, 93, 1);
  8598. return errors.length === previousErrorCount;
  8599. } catch (_unused2) {
  8600. return false;
  8601. }
  8602. }
  8603. return false;
  8604. }
  8605. tsIsUnambiguouslyStartOfFunctionType() {
  8606. this.next();
  8607. if (this.match(11) || this.match(21)) {
  8608. return true;
  8609. }
  8610. if (this.tsSkipParameterStart()) {
  8611. if (this.match(14) || this.match(12) || this.match(17) || this.match(29)) {
  8612. return true;
  8613. }
  8614. if (this.match(11)) {
  8615. this.next();
  8616. if (this.match(19)) {
  8617. return true;
  8618. }
  8619. }
  8620. }
  8621. return false;
  8622. }
  8623. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  8624. return this.tsInType(() => {
  8625. const t = this.startNode();
  8626. this.expect(returnToken);
  8627. const node = this.startNode();
  8628. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  8629. if (asserts && this.match(78)) {
  8630. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  8631. if (thisTypePredicate.type === "TSThisType") {
  8632. node.parameterName = thisTypePredicate;
  8633. node.asserts = true;
  8634. node.typeAnnotation = null;
  8635. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  8636. } else {
  8637. this.resetStartLocationFromNode(thisTypePredicate, node);
  8638. thisTypePredicate.asserts = true;
  8639. }
  8640. t.typeAnnotation = thisTypePredicate;
  8641. return this.finishNode(t, "TSTypeAnnotation");
  8642. }
  8643. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  8644. if (!typePredicateVariable) {
  8645. if (!asserts) {
  8646. return this.tsParseTypeAnnotation(false, t);
  8647. }
  8648. node.parameterName = this.parseIdentifier();
  8649. node.asserts = asserts;
  8650. node.typeAnnotation = null;
  8651. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8652. return this.finishNode(t, "TSTypeAnnotation");
  8653. }
  8654. const type = this.tsParseTypeAnnotation(false);
  8655. node.parameterName = typePredicateVariable;
  8656. node.typeAnnotation = type;
  8657. node.asserts = asserts;
  8658. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  8659. return this.finishNode(t, "TSTypeAnnotation");
  8660. });
  8661. }
  8662. tsTryParseTypeOrTypePredicateAnnotation() {
  8663. if (this.match(14)) {
  8664. return this.tsParseTypeOrTypePredicateAnnotation(14);
  8665. }
  8666. }
  8667. tsTryParseTypeAnnotation() {
  8668. if (this.match(14)) {
  8669. return this.tsParseTypeAnnotation();
  8670. }
  8671. }
  8672. tsTryParseType() {
  8673. return this.tsEatThenParseType(14);
  8674. }
  8675. tsParseTypePredicatePrefix() {
  8676. const id = this.parseIdentifier();
  8677. if (this.isContextual(116) && !this.hasPrecedingLineBreak()) {
  8678. this.next();
  8679. return id;
  8680. }
  8681. }
  8682. tsParseTypePredicateAsserts() {
  8683. if (this.state.type !== 109) {
  8684. return false;
  8685. }
  8686. const containsEsc = this.state.containsEsc;
  8687. this.next();
  8688. if (!tokenIsIdentifier(this.state.type) && !this.match(78)) {
  8689. return false;
  8690. }
  8691. if (containsEsc) {
  8692. this.raise(Errors.InvalidEscapedReservedWord, this.state.lastTokStartLoc, {
  8693. reservedWord: "asserts"
  8694. });
  8695. }
  8696. return true;
  8697. }
  8698. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  8699. this.tsInType(() => {
  8700. if (eatColon) this.expect(14);
  8701. t.typeAnnotation = this.tsParseType();
  8702. });
  8703. return this.finishNode(t, "TSTypeAnnotation");
  8704. }
  8705. tsParseType() {
  8706. assert(this.state.inType);
  8707. const type = this.tsParseNonConditionalType();
  8708. if (this.state.inDisallowConditionalTypesContext || this.hasPrecedingLineBreak() || !this.eat(81)) {
  8709. return type;
  8710. }
  8711. const node = this.startNodeAtNode(type);
  8712. node.checkType = type;
  8713. node.extendsType = this.tsInDisallowConditionalTypesContext(() => this.tsParseNonConditionalType());
  8714. this.expect(17);
  8715. node.trueType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8716. this.expect(14);
  8717. node.falseType = this.tsInAllowConditionalTypesContext(() => this.tsParseType());
  8718. return this.finishNode(node, "TSConditionalType");
  8719. }
  8720. isAbstractConstructorSignature() {
  8721. return this.isContextual(124) && this.isLookaheadContextual("new");
  8722. }
  8723. tsParseNonConditionalType() {
  8724. if (this.tsIsStartOfFunctionType()) {
  8725. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  8726. }
  8727. if (this.match(77)) {
  8728. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  8729. } else if (this.isAbstractConstructorSignature()) {
  8730. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  8731. }
  8732. return this.tsParseUnionTypeOrHigher();
  8733. }
  8734. tsParseTypeAssertion() {
  8735. if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  8736. this.raise(TSErrors.ReservedTypeAssertion, this.state.startLoc);
  8737. }
  8738. const node = this.startNode();
  8739. node.typeAnnotation = this.tsInType(() => {
  8740. this.next();
  8741. return this.match(75) ? this.tsParseTypeReference() : this.tsParseType();
  8742. });
  8743. this.expect(48);
  8744. node.expression = this.parseMaybeUnary();
  8745. return this.finishNode(node, "TSTypeAssertion");
  8746. }
  8747. tsParseHeritageClause(token) {
  8748. const originalStartLoc = this.state.startLoc;
  8749. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", () => {
  8750. const node = this.startNode();
  8751. node.expression = this.tsParseEntityName(1 | 2);
  8752. if (this.match(47)) {
  8753. node.typeParameters = this.tsParseTypeArguments();
  8754. }
  8755. return this.finishNode(node, "TSExpressionWithTypeArguments");
  8756. });
  8757. if (!delimitedList.length) {
  8758. this.raise(TSErrors.EmptyHeritageClauseType, originalStartLoc, {
  8759. token
  8760. });
  8761. }
  8762. return delimitedList;
  8763. }
  8764. tsParseInterfaceDeclaration(node, properties = {}) {
  8765. if (this.hasFollowingLineBreak()) return null;
  8766. this.expectContextual(129);
  8767. if (properties.declare) node.declare = true;
  8768. if (tokenIsIdentifier(this.state.type)) {
  8769. node.id = this.parseIdentifier();
  8770. this.checkIdentifier(node.id, 130);
  8771. } else {
  8772. node.id = null;
  8773. this.raise(TSErrors.MissingInterfaceName, this.state.startLoc);
  8774. }
  8775. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  8776. if (this.eat(81)) {
  8777. node.extends = this.tsParseHeritageClause("extends");
  8778. }
  8779. const body = this.startNode();
  8780. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  8781. node.body = this.finishNode(body, "TSInterfaceBody");
  8782. return this.finishNode(node, "TSInterfaceDeclaration");
  8783. }
  8784. tsParseTypeAliasDeclaration(node) {
  8785. node.id = this.parseIdentifier();
  8786. this.checkIdentifier(node.id, 2);
  8787. node.typeAnnotation = this.tsInType(() => {
  8788. node.typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutModifiers);
  8789. this.expect(29);
  8790. if (this.isContextual(114) && this.lookaheadCharCode() !== 46) {
  8791. const node = this.startNode();
  8792. this.next();
  8793. return this.finishNode(node, "TSIntrinsicKeyword");
  8794. }
  8795. return this.tsParseType();
  8796. });
  8797. this.semicolon();
  8798. return this.finishNode(node, "TSTypeAliasDeclaration");
  8799. }
  8800. tsInTopLevelContext(cb) {
  8801. if (this.curContext() !== types.brace) {
  8802. const oldContext = this.state.context;
  8803. this.state.context = [oldContext[0]];
  8804. try {
  8805. return cb();
  8806. } finally {
  8807. this.state.context = oldContext;
  8808. }
  8809. } else {
  8810. return cb();
  8811. }
  8812. }
  8813. tsInType(cb) {
  8814. const oldInType = this.state.inType;
  8815. this.state.inType = true;
  8816. try {
  8817. return cb();
  8818. } finally {
  8819. this.state.inType = oldInType;
  8820. }
  8821. }
  8822. tsInDisallowConditionalTypesContext(cb) {
  8823. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8824. this.state.inDisallowConditionalTypesContext = true;
  8825. try {
  8826. return cb();
  8827. } finally {
  8828. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8829. }
  8830. }
  8831. tsInAllowConditionalTypesContext(cb) {
  8832. const oldInDisallowConditionalTypesContext = this.state.inDisallowConditionalTypesContext;
  8833. this.state.inDisallowConditionalTypesContext = false;
  8834. try {
  8835. return cb();
  8836. } finally {
  8837. this.state.inDisallowConditionalTypesContext = oldInDisallowConditionalTypesContext;
  8838. }
  8839. }
  8840. tsEatThenParseType(token) {
  8841. if (this.match(token)) {
  8842. return this.tsNextThenParseType();
  8843. }
  8844. }
  8845. tsExpectThenParseType(token) {
  8846. return this.tsInType(() => {
  8847. this.expect(token);
  8848. return this.tsParseType();
  8849. });
  8850. }
  8851. tsNextThenParseType() {
  8852. return this.tsInType(() => {
  8853. this.next();
  8854. return this.tsParseType();
  8855. });
  8856. }
  8857. tsParseEnumMember() {
  8858. const node = this.startNode();
  8859. node.id = this.match(134) ? super.parseStringLiteral(this.state.value) : this.parseIdentifier(true);
  8860. if (this.eat(29)) {
  8861. node.initializer = super.parseMaybeAssignAllowIn();
  8862. }
  8863. return this.finishNode(node, "TSEnumMember");
  8864. }
  8865. tsParseEnumDeclaration(node, properties = {}) {
  8866. if (properties.const) node.const = true;
  8867. if (properties.declare) node.declare = true;
  8868. this.expectContextual(126);
  8869. node.id = this.parseIdentifier();
  8870. this.checkIdentifier(node.id, node.const ? 8971 : 8459);
  8871. this.expect(5);
  8872. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8873. this.expect(8);
  8874. return this.finishNode(node, "TSEnumDeclaration");
  8875. }
  8876. tsParseEnumBody() {
  8877. const node = this.startNode();
  8878. this.expect(5);
  8879. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  8880. this.expect(8);
  8881. return this.finishNode(node, "TSEnumBody");
  8882. }
  8883. tsParseModuleBlock() {
  8884. const node = this.startNode();
  8885. this.scope.enter(0);
  8886. this.expect(5);
  8887. super.parseBlockOrModuleBlockBody(node.body = [], undefined, true, 8);
  8888. this.scope.exit();
  8889. return this.finishNode(node, "TSModuleBlock");
  8890. }
  8891. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  8892. node.id = this.parseIdentifier();
  8893. if (!nested) {
  8894. this.checkIdentifier(node.id, 1024);
  8895. }
  8896. if (this.eat(16)) {
  8897. const inner = this.startNode();
  8898. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  8899. node.body = inner;
  8900. } else {
  8901. this.scope.enter(1024);
  8902. this.prodParam.enter(0);
  8903. node.body = this.tsParseModuleBlock();
  8904. this.prodParam.exit();
  8905. this.scope.exit();
  8906. }
  8907. return this.finishNode(node, "TSModuleDeclaration");
  8908. }
  8909. tsParseAmbientExternalModuleDeclaration(node) {
  8910. if (this.isContextual(112)) {
  8911. node.kind = "global";
  8912. node.global = true;
  8913. node.id = this.parseIdentifier();
  8914. } else if (this.match(134)) {
  8915. node.kind = "module";
  8916. node.id = super.parseStringLiteral(this.state.value);
  8917. } else {
  8918. this.unexpected();
  8919. }
  8920. if (this.match(5)) {
  8921. this.scope.enter(1024);
  8922. this.prodParam.enter(0);
  8923. node.body = this.tsParseModuleBlock();
  8924. this.prodParam.exit();
  8925. this.scope.exit();
  8926. } else {
  8927. this.semicolon();
  8928. }
  8929. return this.finishNode(node, "TSModuleDeclaration");
  8930. }
  8931. tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier, isExport) {
  8932. node.isExport = isExport || false;
  8933. node.id = maybeDefaultIdentifier || this.parseIdentifier();
  8934. this.checkIdentifier(node.id, 4096);
  8935. this.expect(29);
  8936. const moduleReference = this.tsParseModuleReference();
  8937. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  8938. this.raise(TSErrors.ImportAliasHasImportType, moduleReference);
  8939. }
  8940. node.moduleReference = moduleReference;
  8941. this.semicolon();
  8942. return this.finishNode(node, "TSImportEqualsDeclaration");
  8943. }
  8944. tsIsExternalModuleReference() {
  8945. return this.isContextual(119) && this.lookaheadCharCode() === 40;
  8946. }
  8947. tsParseModuleReference() {
  8948. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(0);
  8949. }
  8950. tsParseExternalModuleReference() {
  8951. const node = this.startNode();
  8952. this.expectContextual(119);
  8953. this.expect(10);
  8954. if (!this.match(134)) {
  8955. this.unexpected();
  8956. }
  8957. node.expression = super.parseExprAtom();
  8958. this.expect(11);
  8959. this.sawUnambiguousESM = true;
  8960. return this.finishNode(node, "TSExternalModuleReference");
  8961. }
  8962. tsLookAhead(f) {
  8963. const state = this.state.clone();
  8964. const res = f();
  8965. this.state = state;
  8966. return res;
  8967. }
  8968. tsTryParseAndCatch(f) {
  8969. const result = this.tryParse(abort => f() || abort());
  8970. if (result.aborted || !result.node) return;
  8971. if (result.error) this.state = result.failState;
  8972. return result.node;
  8973. }
  8974. tsTryParse(f) {
  8975. const state = this.state.clone();
  8976. const result = f();
  8977. if (result !== undefined && result !== false) {
  8978. return result;
  8979. }
  8980. this.state = state;
  8981. }
  8982. tsTryParseDeclare(node) {
  8983. if (this.isLineTerminator()) {
  8984. return;
  8985. }
  8986. const startType = this.state.type;
  8987. return this.tsInAmbientContext(() => {
  8988. switch (startType) {
  8989. case 68:
  8990. node.declare = true;
  8991. return super.parseFunctionStatement(node, false, false);
  8992. case 80:
  8993. node.declare = true;
  8994. return this.parseClass(node, true, false);
  8995. case 126:
  8996. return this.tsParseEnumDeclaration(node, {
  8997. declare: true
  8998. });
  8999. case 112:
  9000. return this.tsParseAmbientExternalModuleDeclaration(node);
  9001. case 100:
  9002. if (this.state.containsEsc) {
  9003. return;
  9004. }
  9005. case 75:
  9006. case 74:
  9007. if (!this.match(75) || !this.isLookaheadContextual("enum")) {
  9008. node.declare = true;
  9009. return this.parseVarStatement(node, this.state.value, true);
  9010. }
  9011. this.expect(75);
  9012. return this.tsParseEnumDeclaration(node, {
  9013. const: true,
  9014. declare: true
  9015. });
  9016. case 107:
  9017. if (this.isUsing()) {
  9018. this.raise(TSErrors.InvalidModifierOnUsingDeclaration, this.state.startLoc, "declare");
  9019. node.declare = true;
  9020. return this.parseVarStatement(node, "using", true);
  9021. }
  9022. break;
  9023. case 96:
  9024. if (this.isAwaitUsing()) {
  9025. this.raise(TSErrors.InvalidModifierOnAwaitUsingDeclaration, this.state.startLoc, "declare");
  9026. node.declare = true;
  9027. this.next();
  9028. return this.parseVarStatement(node, "await using", true);
  9029. }
  9030. break;
  9031. case 129:
  9032. {
  9033. const result = this.tsParseInterfaceDeclaration(node, {
  9034. declare: true
  9035. });
  9036. if (result) return result;
  9037. }
  9038. default:
  9039. if (tokenIsIdentifier(startType)) {
  9040. return this.tsParseDeclaration(node, this.state.type, true, null);
  9041. }
  9042. }
  9043. });
  9044. }
  9045. tsTryParseExportDeclaration() {
  9046. return this.tsParseDeclaration(this.startNode(), this.state.type, true, null);
  9047. }
  9048. tsParseDeclaration(node, type, next, decorators) {
  9049. switch (type) {
  9050. case 124:
  9051. if (this.tsCheckLineTerminator(next) && (this.match(80) || tokenIsIdentifier(this.state.type))) {
  9052. return this.tsParseAbstractDeclaration(node, decorators);
  9053. }
  9054. break;
  9055. case 127:
  9056. if (this.tsCheckLineTerminator(next)) {
  9057. if (this.match(134)) {
  9058. return this.tsParseAmbientExternalModuleDeclaration(node);
  9059. } else if (tokenIsIdentifier(this.state.type)) {
  9060. node.kind = "module";
  9061. return this.tsParseModuleOrNamespaceDeclaration(node);
  9062. }
  9063. }
  9064. break;
  9065. case 128:
  9066. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  9067. node.kind = "namespace";
  9068. return this.tsParseModuleOrNamespaceDeclaration(node);
  9069. }
  9070. break;
  9071. case 130:
  9072. if (this.tsCheckLineTerminator(next) && tokenIsIdentifier(this.state.type)) {
  9073. return this.tsParseTypeAliasDeclaration(node);
  9074. }
  9075. break;
  9076. }
  9077. }
  9078. tsCheckLineTerminator(next) {
  9079. if (next) {
  9080. if (this.hasFollowingLineBreak()) return false;
  9081. this.next();
  9082. return true;
  9083. }
  9084. return !this.isLineTerminator();
  9085. }
  9086. tsTryParseGenericAsyncArrowFunction(startLoc) {
  9087. if (!this.match(47)) return;
  9088. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9089. this.state.maybeInArrowParameters = true;
  9090. const res = this.tsTryParseAndCatch(() => {
  9091. const node = this.startNodeAt(startLoc);
  9092. node.typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9093. super.parseFunctionParams(node);
  9094. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  9095. this.expect(19);
  9096. return node;
  9097. });
  9098. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9099. if (!res) return;
  9100. return super.parseArrowExpression(res, null, true);
  9101. }
  9102. tsParseTypeArgumentsInExpression() {
  9103. if (this.reScan_lt() !== 47) return;
  9104. return this.tsParseTypeArguments();
  9105. }
  9106. tsParseTypeArguments() {
  9107. const node = this.startNode();
  9108. node.params = this.tsInType(() => this.tsInTopLevelContext(() => {
  9109. this.expect(47);
  9110. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  9111. }));
  9112. if (node.params.length === 0) {
  9113. this.raise(TSErrors.EmptyTypeArguments, node);
  9114. } else if (!this.state.inType && this.curContext() === types.brace) {
  9115. this.reScan_lt_gt();
  9116. }
  9117. this.expect(48);
  9118. return this.finishNode(node, "TSTypeParameterInstantiation");
  9119. }
  9120. tsIsDeclarationStart() {
  9121. return tokenIsTSDeclarationStart(this.state.type);
  9122. }
  9123. isExportDefaultSpecifier() {
  9124. if (this.tsIsDeclarationStart()) return false;
  9125. return super.isExportDefaultSpecifier();
  9126. }
  9127. parseBindingElement(flags, decorators) {
  9128. const startLoc = decorators.length ? decorators[0].loc.start : this.state.startLoc;
  9129. const modified = {};
  9130. this.tsParseModifiers({
  9131. allowedModifiers: ["public", "private", "protected", "override", "readonly"]
  9132. }, modified);
  9133. const accessibility = modified.accessibility;
  9134. const override = modified.override;
  9135. const readonly = modified.readonly;
  9136. if (!(flags & 4) && (accessibility || readonly || override)) {
  9137. this.raise(TSErrors.UnexpectedParameterModifier, startLoc);
  9138. }
  9139. const left = this.parseMaybeDefault();
  9140. if (flags & 2) {
  9141. this.parseFunctionParamType(left);
  9142. }
  9143. const elt = this.parseMaybeDefault(left.loc.start, left);
  9144. if (accessibility || readonly || override) {
  9145. const pp = this.startNodeAt(startLoc);
  9146. if (decorators.length) {
  9147. pp.decorators = decorators;
  9148. }
  9149. if (accessibility) pp.accessibility = accessibility;
  9150. if (readonly) pp.readonly = readonly;
  9151. if (override) pp.override = override;
  9152. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  9153. this.raise(TSErrors.UnsupportedParameterPropertyKind, pp);
  9154. }
  9155. pp.parameter = elt;
  9156. return this.finishNode(pp, "TSParameterProperty");
  9157. }
  9158. if (decorators.length) {
  9159. left.decorators = decorators;
  9160. }
  9161. return elt;
  9162. }
  9163. isSimpleParameter(node) {
  9164. return node.type === "TSParameterProperty" && super.isSimpleParameter(node.parameter) || super.isSimpleParameter(node);
  9165. }
  9166. tsDisallowOptionalPattern(node) {
  9167. for (const param of node.params) {
  9168. if (param.type !== "Identifier" && param.optional && !this.state.isAmbientContext) {
  9169. this.raise(TSErrors.PatternIsOptional, param);
  9170. }
  9171. }
  9172. }
  9173. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  9174. super.setArrowFunctionParameters(node, params, trailingCommaLoc);
  9175. this.tsDisallowOptionalPattern(node);
  9176. }
  9177. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  9178. if (this.match(14)) {
  9179. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9180. }
  9181. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" || type === "ClassPrivateMethod" ? "TSDeclareMethod" : undefined;
  9182. if (bodilessType && !this.match(5) && this.isLineTerminator()) {
  9183. return this.finishNode(node, bodilessType);
  9184. }
  9185. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  9186. this.raise(TSErrors.DeclareFunctionHasImplementation, node);
  9187. if (node.declare) {
  9188. return super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  9189. }
  9190. }
  9191. this.tsDisallowOptionalPattern(node);
  9192. return super.parseFunctionBodyAndFinish(node, type, isMethod);
  9193. }
  9194. registerFunctionStatementId(node) {
  9195. if (!node.body && node.id) {
  9196. this.checkIdentifier(node.id, 1024);
  9197. } else {
  9198. super.registerFunctionStatementId(node);
  9199. }
  9200. }
  9201. tsCheckForInvalidTypeCasts(items) {
  9202. items.forEach(node => {
  9203. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  9204. this.raise(TSErrors.UnexpectedTypeAnnotation, node.typeAnnotation);
  9205. }
  9206. });
  9207. }
  9208. toReferencedList(exprList, isInParens) {
  9209. this.tsCheckForInvalidTypeCasts(exprList);
  9210. return exprList;
  9211. }
  9212. parseArrayLike(close, isTuple, refExpressionErrors) {
  9213. const node = super.parseArrayLike(close, isTuple, refExpressionErrors);
  9214. if (node.type === "ArrayExpression") {
  9215. this.tsCheckForInvalidTypeCasts(node.elements);
  9216. }
  9217. return node;
  9218. }
  9219. parseSubscript(base, startLoc, noCalls, state) {
  9220. if (!this.hasPrecedingLineBreak() && this.match(35)) {
  9221. this.state.canStartJSXElement = false;
  9222. this.next();
  9223. const nonNullExpression = this.startNodeAt(startLoc);
  9224. nonNullExpression.expression = base;
  9225. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  9226. }
  9227. let isOptionalCall = false;
  9228. if (this.match(18) && this.lookaheadCharCode() === 60) {
  9229. if (noCalls) {
  9230. state.stop = true;
  9231. return base;
  9232. }
  9233. state.optionalChainMember = isOptionalCall = true;
  9234. this.next();
  9235. }
  9236. if (this.match(47) || this.match(51)) {
  9237. let missingParenErrorLoc;
  9238. const result = this.tsTryParseAndCatch(() => {
  9239. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  9240. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startLoc);
  9241. if (asyncArrowFn) {
  9242. state.stop = true;
  9243. return asyncArrowFn;
  9244. }
  9245. }
  9246. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9247. if (!typeArguments) return;
  9248. if (isOptionalCall && !this.match(10)) {
  9249. missingParenErrorLoc = this.state.curPosition();
  9250. return;
  9251. }
  9252. if (tokenIsTemplate(this.state.type)) {
  9253. const result = super.parseTaggedTemplateExpression(base, startLoc, state);
  9254. result.typeParameters = typeArguments;
  9255. return result;
  9256. }
  9257. if (!noCalls && this.eat(10)) {
  9258. const node = this.startNodeAt(startLoc);
  9259. node.callee = base;
  9260. node.arguments = this.parseCallExpressionArguments();
  9261. this.tsCheckForInvalidTypeCasts(node.arguments);
  9262. node.typeParameters = typeArguments;
  9263. if (state.optionalChainMember) {
  9264. node.optional = isOptionalCall;
  9265. }
  9266. return this.finishCallExpression(node, state.optionalChainMember);
  9267. }
  9268. const tokenType = this.state.type;
  9269. if (tokenType === 48 || tokenType === 52 || tokenType !== 10 && tokenType !== 93 && tokenType !== 120 && tokenCanStartExpression(tokenType) && !this.hasPrecedingLineBreak()) {
  9270. return;
  9271. }
  9272. const node = this.startNodeAt(startLoc);
  9273. node.expression = base;
  9274. node.typeParameters = typeArguments;
  9275. return this.finishNode(node, "TSInstantiationExpression");
  9276. });
  9277. if (missingParenErrorLoc) {
  9278. this.unexpected(missingParenErrorLoc, 10);
  9279. }
  9280. if (result) {
  9281. if (result.type === "TSInstantiationExpression") {
  9282. if (this.match(16) || this.match(18) && this.lookaheadCharCode() !== 40) {
  9283. this.raise(TSErrors.InvalidPropertyAccessAfterInstantiationExpression, this.state.startLoc);
  9284. }
  9285. if (!this.match(16) && !this.match(18)) {
  9286. result.expression = super.stopParseSubscript(base, state);
  9287. }
  9288. }
  9289. return result;
  9290. }
  9291. }
  9292. return super.parseSubscript(base, startLoc, noCalls, state);
  9293. }
  9294. parseNewCallee(node) {
  9295. var _callee$extra;
  9296. super.parseNewCallee(node);
  9297. const {
  9298. callee
  9299. } = node;
  9300. if (callee.type === "TSInstantiationExpression" && !((_callee$extra = callee.extra) != null && _callee$extra.parenthesized)) {
  9301. node.typeParameters = callee.typeParameters;
  9302. node.callee = callee.expression;
  9303. }
  9304. }
  9305. parseExprOp(left, leftStartLoc, minPrec) {
  9306. let isSatisfies;
  9307. if (tokenOperatorPrecedence(58) > minPrec && !this.hasPrecedingLineBreak() && (this.isContextual(93) || (isSatisfies = this.isContextual(120)))) {
  9308. const node = this.startNodeAt(leftStartLoc);
  9309. node.expression = left;
  9310. node.typeAnnotation = this.tsInType(() => {
  9311. this.next();
  9312. if (this.match(75)) {
  9313. if (isSatisfies) {
  9314. this.raise(Errors.UnexpectedKeyword, this.state.startLoc, {
  9315. keyword: "const"
  9316. });
  9317. }
  9318. return this.tsParseTypeReference();
  9319. }
  9320. return this.tsParseType();
  9321. });
  9322. this.finishNode(node, isSatisfies ? "TSSatisfiesExpression" : "TSAsExpression");
  9323. this.reScan_lt_gt();
  9324. return this.parseExprOp(node, leftStartLoc, minPrec);
  9325. }
  9326. return super.parseExprOp(left, leftStartLoc, minPrec);
  9327. }
  9328. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  9329. if (!this.state.isAmbientContext) {
  9330. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  9331. }
  9332. }
  9333. checkImportReflection(node) {
  9334. super.checkImportReflection(node);
  9335. if (node.module && node.importKind !== "value") {
  9336. this.raise(TSErrors.ImportReflectionHasImportType, node.specifiers[0].loc.start);
  9337. }
  9338. }
  9339. checkDuplicateExports() {}
  9340. isPotentialImportPhase(isExport) {
  9341. if (super.isPotentialImportPhase(isExport)) return true;
  9342. if (this.isContextual(130)) {
  9343. const ch = this.lookaheadCharCode();
  9344. return isExport ? ch === 123 || ch === 42 : ch !== 61;
  9345. }
  9346. return !isExport && this.isContextual(87);
  9347. }
  9348. applyImportPhase(node, isExport, phase, loc) {
  9349. super.applyImportPhase(node, isExport, phase, loc);
  9350. if (isExport) {
  9351. node.exportKind = phase === "type" ? "type" : "value";
  9352. } else {
  9353. node.importKind = phase === "type" || phase === "typeof" ? phase : "value";
  9354. }
  9355. }
  9356. parseImport(node) {
  9357. if (this.match(134)) {
  9358. node.importKind = "value";
  9359. return super.parseImport(node);
  9360. }
  9361. let importNode;
  9362. if (tokenIsIdentifier(this.state.type) && this.lookaheadCharCode() === 61) {
  9363. node.importKind = "value";
  9364. return this.tsParseImportEqualsDeclaration(node);
  9365. } else if (this.isContextual(130)) {
  9366. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, false);
  9367. if (this.lookaheadCharCode() === 61) {
  9368. return this.tsParseImportEqualsDeclaration(node, maybeDefaultIdentifier);
  9369. } else {
  9370. importNode = super.parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier);
  9371. }
  9372. } else {
  9373. importNode = super.parseImport(node);
  9374. }
  9375. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  9376. this.raise(TSErrors.TypeImportCannotSpecifyDefaultAndNamed, importNode);
  9377. }
  9378. return importNode;
  9379. }
  9380. parseExport(node, decorators) {
  9381. if (this.match(83)) {
  9382. const nodeImportEquals = node;
  9383. this.next();
  9384. let maybeDefaultIdentifier = null;
  9385. if (this.isContextual(130) && this.isPotentialImportPhase(false)) {
  9386. maybeDefaultIdentifier = this.parseMaybeImportPhase(nodeImportEquals, false);
  9387. } else {
  9388. nodeImportEquals.importKind = "value";
  9389. }
  9390. const declaration = this.tsParseImportEqualsDeclaration(nodeImportEquals, maybeDefaultIdentifier, true);
  9391. return declaration;
  9392. } else if (this.eat(29)) {
  9393. const assign = node;
  9394. assign.expression = super.parseExpression();
  9395. this.semicolon();
  9396. this.sawUnambiguousESM = true;
  9397. return this.finishNode(assign, "TSExportAssignment");
  9398. } else if (this.eatContextual(93)) {
  9399. const decl = node;
  9400. this.expectContextual(128);
  9401. decl.id = this.parseIdentifier();
  9402. this.semicolon();
  9403. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  9404. } else {
  9405. return super.parseExport(node, decorators);
  9406. }
  9407. }
  9408. isAbstractClass() {
  9409. return this.isContextual(124) && this.isLookaheadContextual("class");
  9410. }
  9411. parseExportDefaultExpression() {
  9412. if (this.isAbstractClass()) {
  9413. const cls = this.startNode();
  9414. this.next();
  9415. cls.abstract = true;
  9416. return this.parseClass(cls, true, true);
  9417. }
  9418. if (this.match(129)) {
  9419. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9420. if (result) return result;
  9421. }
  9422. return super.parseExportDefaultExpression();
  9423. }
  9424. parseVarStatement(node, kind, allowMissingInitializer = false) {
  9425. const {
  9426. isAmbientContext
  9427. } = this.state;
  9428. const declaration = super.parseVarStatement(node, kind, allowMissingInitializer || isAmbientContext);
  9429. if (!isAmbientContext) return declaration;
  9430. if (!node.declare && (kind === "using" || kind === "await using")) {
  9431. this.raiseOverwrite(TSErrors.UsingDeclarationInAmbientContext, node, kind);
  9432. return declaration;
  9433. }
  9434. for (const {
  9435. id,
  9436. init
  9437. } of declaration.declarations) {
  9438. if (!init) continue;
  9439. if (kind === "var" || kind === "let" || !!id.typeAnnotation) {
  9440. this.raise(TSErrors.InitializerNotAllowedInAmbientContext, init);
  9441. } else if (!isValidAmbientConstInitializer(init, this.hasPlugin("estree"))) {
  9442. this.raise(TSErrors.ConstInitializerMustBeStringOrNumericLiteralOrLiteralEnumReference, init);
  9443. }
  9444. }
  9445. return declaration;
  9446. }
  9447. parseStatementContent(flags, decorators) {
  9448. if (!this.state.containsEsc) {
  9449. switch (this.state.type) {
  9450. case 75:
  9451. {
  9452. if (this.isLookaheadContextual("enum")) {
  9453. const node = this.startNode();
  9454. this.expect(75);
  9455. return this.tsParseEnumDeclaration(node, {
  9456. const: true
  9457. });
  9458. }
  9459. break;
  9460. }
  9461. case 124:
  9462. case 125:
  9463. {
  9464. if (this.nextTokenIsIdentifierAndNotTSRelationalOperatorOnSameLine()) {
  9465. const token = this.state.type;
  9466. const node = this.startNode();
  9467. this.next();
  9468. const declaration = token === 125 ? this.tsTryParseDeclare(node) : this.tsParseAbstractDeclaration(node, decorators);
  9469. if (declaration) {
  9470. if (token === 125) {
  9471. declaration.declare = true;
  9472. }
  9473. return declaration;
  9474. } else {
  9475. node.expression = this.createIdentifier(this.startNodeAt(node.loc.start), token === 125 ? "declare" : "abstract");
  9476. this.semicolon(false);
  9477. return this.finishNode(node, "ExpressionStatement");
  9478. }
  9479. }
  9480. break;
  9481. }
  9482. case 126:
  9483. return this.tsParseEnumDeclaration(this.startNode());
  9484. case 112:
  9485. {
  9486. const nextCh = this.lookaheadCharCode();
  9487. if (nextCh === 123) {
  9488. const node = this.startNode();
  9489. return this.tsParseAmbientExternalModuleDeclaration(node);
  9490. }
  9491. break;
  9492. }
  9493. case 129:
  9494. {
  9495. const result = this.tsParseInterfaceDeclaration(this.startNode());
  9496. if (result) return result;
  9497. break;
  9498. }
  9499. case 127:
  9500. {
  9501. if (this.nextTokenIsIdentifierOrStringLiteralOnSameLine()) {
  9502. const node = this.startNode();
  9503. this.next();
  9504. return this.tsParseDeclaration(node, 127, false, decorators);
  9505. }
  9506. break;
  9507. }
  9508. case 128:
  9509. {
  9510. if (this.nextTokenIsIdentifierOnSameLine()) {
  9511. const node = this.startNode();
  9512. this.next();
  9513. return this.tsParseDeclaration(node, 128, false, decorators);
  9514. }
  9515. break;
  9516. }
  9517. case 130:
  9518. {
  9519. if (this.nextTokenIsIdentifierOnSameLine()) {
  9520. const node = this.startNode();
  9521. this.next();
  9522. return this.tsParseTypeAliasDeclaration(node);
  9523. }
  9524. break;
  9525. }
  9526. }
  9527. }
  9528. return super.parseStatementContent(flags, decorators);
  9529. }
  9530. parseAccessModifier() {
  9531. return this.tsParseModifier(["public", "protected", "private"]);
  9532. }
  9533. tsHasSomeModifiers(member, modifiers) {
  9534. return modifiers.some(modifier => {
  9535. if (tsIsAccessModifier(modifier)) {
  9536. return member.accessibility === modifier;
  9537. }
  9538. return !!member[modifier];
  9539. });
  9540. }
  9541. tsIsStartOfStaticBlocks() {
  9542. return this.isContextual(106) && this.lookaheadCharCode() === 123;
  9543. }
  9544. parseClassMember(classBody, member, state) {
  9545. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  9546. this.tsParseModifiers({
  9547. allowedModifiers: modifiers,
  9548. disallowedModifiers: ["in", "out"],
  9549. stopOnStartOfClassStaticBlock: true,
  9550. errorTemplate: TSErrors.InvalidModifierOnTypeParameterPositions
  9551. }, member);
  9552. const callParseClassMemberWithIsStatic = () => {
  9553. if (this.tsIsStartOfStaticBlocks()) {
  9554. this.next();
  9555. this.next();
  9556. if (this.tsHasSomeModifiers(member, modifiers)) {
  9557. this.raise(TSErrors.StaticBlockCannotHaveModifier, this.state.curPosition());
  9558. }
  9559. super.parseClassStaticBlock(classBody, member);
  9560. } else {
  9561. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  9562. }
  9563. };
  9564. if (member.declare) {
  9565. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  9566. } else {
  9567. callParseClassMemberWithIsStatic();
  9568. }
  9569. }
  9570. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  9571. const idx = this.tsTryParseIndexSignature(member);
  9572. if (idx) {
  9573. classBody.body.push(idx);
  9574. if (member.abstract) {
  9575. this.raise(TSErrors.IndexSignatureHasAbstract, member);
  9576. }
  9577. if (member.accessibility) {
  9578. this.raise(TSErrors.IndexSignatureHasAccessibility, member, {
  9579. modifier: member.accessibility
  9580. });
  9581. }
  9582. if (member.declare) {
  9583. this.raise(TSErrors.IndexSignatureHasDeclare, member);
  9584. }
  9585. if (member.override) {
  9586. this.raise(TSErrors.IndexSignatureHasOverride, member);
  9587. }
  9588. return;
  9589. }
  9590. if (!this.state.inAbstractClass && member.abstract) {
  9591. this.raise(TSErrors.NonAbstractClassHasAbstractMethod, member);
  9592. }
  9593. if (member.override) {
  9594. if (!state.hadSuperClass) {
  9595. this.raise(TSErrors.OverrideNotInSubClass, member);
  9596. }
  9597. }
  9598. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  9599. }
  9600. parsePostMemberNameModifiers(methodOrProp) {
  9601. const optional = this.eat(17);
  9602. if (optional) methodOrProp.optional = true;
  9603. if (methodOrProp.readonly && this.match(10)) {
  9604. this.raise(TSErrors.ClassMethodHasReadonly, methodOrProp);
  9605. }
  9606. if (methodOrProp.declare && this.match(10)) {
  9607. this.raise(TSErrors.ClassMethodHasDeclare, methodOrProp);
  9608. }
  9609. }
  9610. shouldParseExportDeclaration() {
  9611. if (this.tsIsDeclarationStart()) return true;
  9612. return super.shouldParseExportDeclaration();
  9613. }
  9614. parseConditional(expr, startLoc, refExpressionErrors) {
  9615. if (!this.match(17)) return expr;
  9616. if (this.state.maybeInArrowParameters) {
  9617. const nextCh = this.lookaheadCharCode();
  9618. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  9619. this.setOptionalParametersError(refExpressionErrors);
  9620. return expr;
  9621. }
  9622. }
  9623. return super.parseConditional(expr, startLoc, refExpressionErrors);
  9624. }
  9625. parseParenItem(node, startLoc) {
  9626. const newNode = super.parseParenItem(node, startLoc);
  9627. if (this.eat(17)) {
  9628. newNode.optional = true;
  9629. this.resetEndLocation(node);
  9630. }
  9631. if (this.match(14)) {
  9632. const typeCastNode = this.startNodeAt(startLoc);
  9633. typeCastNode.expression = node;
  9634. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  9635. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  9636. }
  9637. return node;
  9638. }
  9639. parseExportDeclaration(node) {
  9640. if (!this.state.isAmbientContext && this.isContextual(125)) {
  9641. return this.tsInAmbientContext(() => this.parseExportDeclaration(node));
  9642. }
  9643. const startLoc = this.state.startLoc;
  9644. const isDeclare = this.eatContextual(125);
  9645. if (isDeclare && (this.isContextual(125) || !this.shouldParseExportDeclaration())) {
  9646. throw this.raise(TSErrors.ExpectedAmbientAfterExportDeclare, this.state.startLoc);
  9647. }
  9648. const isIdentifier = tokenIsIdentifier(this.state.type);
  9649. const declaration = isIdentifier && this.tsTryParseExportDeclaration() || super.parseExportDeclaration(node);
  9650. if (!declaration) return null;
  9651. if (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare) {
  9652. node.exportKind = "type";
  9653. }
  9654. if (isDeclare && declaration.type !== "TSImportEqualsDeclaration") {
  9655. this.resetStartLocation(declaration, startLoc);
  9656. declaration.declare = true;
  9657. }
  9658. return declaration;
  9659. }
  9660. parseClassId(node, isStatement, optionalId, bindingType) {
  9661. if ((!isStatement || optionalId) && this.isContextual(113)) {
  9662. return;
  9663. }
  9664. super.parseClassId(node, isStatement, optionalId, node.declare ? 1024 : 8331);
  9665. const typeParameters = this.tsTryParseTypeParameters(this.tsParseInOutConstModifiers);
  9666. if (typeParameters) node.typeParameters = typeParameters;
  9667. }
  9668. parseClassPropertyAnnotation(node) {
  9669. if (!node.optional) {
  9670. if (this.eat(35)) {
  9671. node.definite = true;
  9672. } else if (this.eat(17)) {
  9673. node.optional = true;
  9674. }
  9675. }
  9676. const type = this.tsTryParseTypeAnnotation();
  9677. if (type) node.typeAnnotation = type;
  9678. }
  9679. parseClassProperty(node) {
  9680. this.parseClassPropertyAnnotation(node);
  9681. if (this.state.isAmbientContext && !(node.readonly && !node.typeAnnotation) && this.match(29)) {
  9682. this.raise(TSErrors.DeclareClassFieldHasInitializer, this.state.startLoc);
  9683. }
  9684. if (node.abstract && this.match(29)) {
  9685. const {
  9686. key
  9687. } = node;
  9688. this.raise(TSErrors.AbstractPropertyHasInitializer, this.state.startLoc, {
  9689. propertyName: key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(this.offsetToSourcePos(key.start), this.offsetToSourcePos(key.end))}]`
  9690. });
  9691. }
  9692. return super.parseClassProperty(node);
  9693. }
  9694. parseClassPrivateProperty(node) {
  9695. if (node.abstract) {
  9696. this.raise(TSErrors.PrivateElementHasAbstract, node);
  9697. }
  9698. if (node.accessibility) {
  9699. this.raise(TSErrors.PrivateElementHasAccessibility, node, {
  9700. modifier: node.accessibility
  9701. });
  9702. }
  9703. this.parseClassPropertyAnnotation(node);
  9704. return super.parseClassPrivateProperty(node);
  9705. }
  9706. parseClassAccessorProperty(node) {
  9707. this.parseClassPropertyAnnotation(node);
  9708. if (node.optional) {
  9709. this.raise(TSErrors.AccessorCannotBeOptional, node);
  9710. }
  9711. return super.parseClassAccessorProperty(node);
  9712. }
  9713. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  9714. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9715. if (typeParameters && isConstructor) {
  9716. this.raise(TSErrors.ConstructorHasTypeParameters, typeParameters);
  9717. }
  9718. const {
  9719. declare = false,
  9720. kind
  9721. } = method;
  9722. if (declare && (kind === "get" || kind === "set")) {
  9723. this.raise(TSErrors.DeclareAccessor, method, {
  9724. kind
  9725. });
  9726. }
  9727. if (typeParameters) method.typeParameters = typeParameters;
  9728. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  9729. }
  9730. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  9731. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9732. if (typeParameters) method.typeParameters = typeParameters;
  9733. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  9734. }
  9735. declareClassPrivateMethodInScope(node, kind) {
  9736. if (node.type === "TSDeclareMethod") return;
  9737. if (node.type === "MethodDefinition" && node.value.body == null) {
  9738. return;
  9739. }
  9740. super.declareClassPrivateMethodInScope(node, kind);
  9741. }
  9742. parseClassSuper(node) {
  9743. super.parseClassSuper(node);
  9744. if (node.superClass) {
  9745. if (node.superClass.type === "TSInstantiationExpression") {
  9746. const tsInstantiationExpression = node.superClass;
  9747. const superClass = tsInstantiationExpression.expression;
  9748. this.takeSurroundingComments(superClass, superClass.start, superClass.end);
  9749. const superTypeArguments = tsInstantiationExpression.typeParameters;
  9750. this.takeSurroundingComments(superTypeArguments, superTypeArguments.start, superTypeArguments.end);
  9751. node.superClass = superClass;
  9752. node.superTypeParameters = superTypeArguments;
  9753. } else if (this.match(47) || this.match(51)) {
  9754. node.superTypeParameters = this.tsParseTypeArgumentsInExpression();
  9755. }
  9756. }
  9757. if (this.eatContextual(113)) {
  9758. node.implements = this.tsParseHeritageClause("implements");
  9759. }
  9760. }
  9761. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9762. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9763. if (typeParameters) prop.typeParameters = typeParameters;
  9764. return super.parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9765. }
  9766. parseFunctionParams(node, isConstructor) {
  9767. const typeParameters = this.tsTryParseTypeParameters(this.tsParseConstModifier);
  9768. if (typeParameters) node.typeParameters = typeParameters;
  9769. super.parseFunctionParams(node, isConstructor);
  9770. }
  9771. parseVarId(decl, kind) {
  9772. super.parseVarId(decl, kind);
  9773. if (decl.id.type === "Identifier" && !this.hasPrecedingLineBreak() && this.eat(35)) {
  9774. decl.definite = true;
  9775. }
  9776. const type = this.tsTryParseTypeAnnotation();
  9777. if (type) {
  9778. decl.id.typeAnnotation = type;
  9779. this.resetEndLocation(decl.id);
  9780. }
  9781. }
  9782. parseAsyncArrowFromCallExpression(node, call) {
  9783. if (this.match(14)) {
  9784. node.returnType = this.tsParseTypeAnnotation();
  9785. }
  9786. return super.parseAsyncArrowFromCallExpression(node, call);
  9787. }
  9788. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  9789. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2;
  9790. let state;
  9791. let jsx;
  9792. let typeCast;
  9793. if (this.hasPlugin("jsx") && (this.match(143) || this.match(47))) {
  9794. state = this.state.clone();
  9795. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9796. if (!jsx.error) return jsx.node;
  9797. const {
  9798. context
  9799. } = this.state;
  9800. const currentContext = context[context.length - 1];
  9801. if (currentContext === types.j_oTag || currentContext === types.j_expr) {
  9802. context.pop();
  9803. }
  9804. }
  9805. if (!((_jsx = jsx) != null && _jsx.error) && !this.match(47)) {
  9806. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9807. }
  9808. if (!state || state === this.state) state = this.state.clone();
  9809. let typeParameters;
  9810. const arrow = this.tryParse(abort => {
  9811. var _expr$extra, _typeParameters;
  9812. typeParameters = this.tsParseTypeParameters(this.tsParseConstModifier);
  9813. const expr = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  9814. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  9815. abort();
  9816. }
  9817. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  9818. this.resetStartLocationFromNode(expr, typeParameters);
  9819. }
  9820. expr.typeParameters = typeParameters;
  9821. return expr;
  9822. }, state);
  9823. if (!arrow.error && !arrow.aborted) {
  9824. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9825. return arrow.node;
  9826. }
  9827. if (!jsx) {
  9828. assert(!this.hasPlugin("jsx"));
  9829. typeCast = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  9830. if (!typeCast.error) return typeCast.node;
  9831. }
  9832. if ((_jsx2 = jsx) != null && _jsx2.node) {
  9833. this.state = jsx.failState;
  9834. return jsx.node;
  9835. }
  9836. if (arrow.node) {
  9837. this.state = arrow.failState;
  9838. if (typeParameters) this.reportReservedArrowTypeParam(typeParameters);
  9839. return arrow.node;
  9840. }
  9841. if ((_typeCast = typeCast) != null && _typeCast.node) {
  9842. this.state = typeCast.failState;
  9843. return typeCast.node;
  9844. }
  9845. throw ((_jsx3 = jsx) == null ? void 0 : _jsx3.error) || arrow.error || ((_typeCast2 = typeCast) == null ? void 0 : _typeCast2.error);
  9846. }
  9847. reportReservedArrowTypeParam(node) {
  9848. var _node$extra2;
  9849. if (node.params.length === 1 && !node.params[0].constraint && !((_node$extra2 = node.extra) != null && _node$extra2.trailingComma) && this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) {
  9850. this.raise(TSErrors.ReservedArrowTypeParam, node);
  9851. }
  9852. }
  9853. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9854. if (!this.hasPlugin("jsx") && this.match(47)) {
  9855. return this.tsParseTypeAssertion();
  9856. }
  9857. return super.parseMaybeUnary(refExpressionErrors, sawUnary);
  9858. }
  9859. parseArrow(node) {
  9860. if (this.match(14)) {
  9861. const result = this.tryParse(abort => {
  9862. const returnType = this.tsParseTypeOrTypePredicateAnnotation(14);
  9863. if (this.canInsertSemicolon() || !this.match(19)) abort();
  9864. return returnType;
  9865. });
  9866. if (result.aborted) return;
  9867. if (!result.thrown) {
  9868. if (result.error) this.state = result.failState;
  9869. node.returnType = result.node;
  9870. }
  9871. }
  9872. return super.parseArrow(node);
  9873. }
  9874. parseFunctionParamType(param) {
  9875. if (this.eat(17)) {
  9876. param.optional = true;
  9877. }
  9878. const type = this.tsTryParseTypeAnnotation();
  9879. if (type) param.typeAnnotation = type;
  9880. this.resetEndLocation(param);
  9881. return param;
  9882. }
  9883. isAssignable(node, isBinding) {
  9884. switch (node.type) {
  9885. case "TSTypeCastExpression":
  9886. return this.isAssignable(node.expression, isBinding);
  9887. case "TSParameterProperty":
  9888. return true;
  9889. default:
  9890. return super.isAssignable(node, isBinding);
  9891. }
  9892. }
  9893. toAssignable(node, isLHS = false) {
  9894. switch (node.type) {
  9895. case "ParenthesizedExpression":
  9896. this.toAssignableParenthesizedExpression(node, isLHS);
  9897. break;
  9898. case "TSAsExpression":
  9899. case "TSSatisfiesExpression":
  9900. case "TSNonNullExpression":
  9901. case "TSTypeAssertion":
  9902. if (isLHS) {
  9903. this.expressionScope.recordArrowParameterBindingError(TSErrors.UnexpectedTypeCastInParameter, node);
  9904. } else {
  9905. this.raise(TSErrors.UnexpectedTypeCastInParameter, node);
  9906. }
  9907. this.toAssignable(node.expression, isLHS);
  9908. break;
  9909. case "AssignmentExpression":
  9910. if (!isLHS && node.left.type === "TSTypeCastExpression") {
  9911. node.left = this.typeCastToParameter(node.left);
  9912. }
  9913. default:
  9914. super.toAssignable(node, isLHS);
  9915. }
  9916. }
  9917. toAssignableParenthesizedExpression(node, isLHS) {
  9918. switch (node.expression.type) {
  9919. case "TSAsExpression":
  9920. case "TSSatisfiesExpression":
  9921. case "TSNonNullExpression":
  9922. case "TSTypeAssertion":
  9923. case "ParenthesizedExpression":
  9924. this.toAssignable(node.expression, isLHS);
  9925. break;
  9926. default:
  9927. super.toAssignable(node, isLHS);
  9928. }
  9929. }
  9930. checkToRestConversion(node, allowPattern) {
  9931. switch (node.type) {
  9932. case "TSAsExpression":
  9933. case "TSSatisfiesExpression":
  9934. case "TSTypeAssertion":
  9935. case "TSNonNullExpression":
  9936. this.checkToRestConversion(node.expression, false);
  9937. break;
  9938. default:
  9939. super.checkToRestConversion(node, allowPattern);
  9940. }
  9941. }
  9942. isValidLVal(type, disallowCallExpression, isUnparenthesizedInAssign, binding) {
  9943. switch (type) {
  9944. case "TSTypeCastExpression":
  9945. return true;
  9946. case "TSParameterProperty":
  9947. return "parameter";
  9948. case "TSNonNullExpression":
  9949. return "expression";
  9950. case "TSAsExpression":
  9951. case "TSSatisfiesExpression":
  9952. case "TSTypeAssertion":
  9953. return (binding !== 64 || !isUnparenthesizedInAssign) && ["expression", true];
  9954. default:
  9955. return super.isValidLVal(type, disallowCallExpression, isUnparenthesizedInAssign, binding);
  9956. }
  9957. }
  9958. parseBindingAtom() {
  9959. if (this.state.type === 78) {
  9960. return this.parseIdentifier(true);
  9961. }
  9962. return super.parseBindingAtom();
  9963. }
  9964. parseMaybeDecoratorArguments(expr, startLoc) {
  9965. if (this.match(47) || this.match(51)) {
  9966. const typeArguments = this.tsParseTypeArgumentsInExpression();
  9967. if (this.match(10)) {
  9968. const call = super.parseMaybeDecoratorArguments(expr, startLoc);
  9969. call.typeParameters = typeArguments;
  9970. return call;
  9971. }
  9972. this.unexpected(null, 10);
  9973. }
  9974. return super.parseMaybeDecoratorArguments(expr, startLoc);
  9975. }
  9976. checkCommaAfterRest(close) {
  9977. if (this.state.isAmbientContext && this.match(12) && this.lookaheadCharCode() === close) {
  9978. this.next();
  9979. return false;
  9980. }
  9981. return super.checkCommaAfterRest(close);
  9982. }
  9983. isClassMethod() {
  9984. return this.match(47) || super.isClassMethod();
  9985. }
  9986. isClassProperty() {
  9987. return this.match(35) || this.match(14) || super.isClassProperty();
  9988. }
  9989. parseMaybeDefault(startLoc, left) {
  9990. const node = super.parseMaybeDefault(startLoc, left);
  9991. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  9992. this.raise(TSErrors.TypeAnnotationAfterAssign, node.typeAnnotation);
  9993. }
  9994. return node;
  9995. }
  9996. getTokenFromCode(code) {
  9997. if (this.state.inType) {
  9998. if (code === 62) {
  9999. this.finishOp(48, 1);
  10000. return;
  10001. }
  10002. if (code === 60) {
  10003. this.finishOp(47, 1);
  10004. return;
  10005. }
  10006. }
  10007. super.getTokenFromCode(code);
  10008. }
  10009. reScan_lt_gt() {
  10010. const {
  10011. type
  10012. } = this.state;
  10013. if (type === 47) {
  10014. this.state.pos -= 1;
  10015. this.readToken_lt();
  10016. } else if (type === 48) {
  10017. this.state.pos -= 1;
  10018. this.readToken_gt();
  10019. }
  10020. }
  10021. reScan_lt() {
  10022. const {
  10023. type
  10024. } = this.state;
  10025. if (type === 51) {
  10026. this.state.pos -= 2;
  10027. this.finishOp(47, 1);
  10028. return 47;
  10029. }
  10030. return type;
  10031. }
  10032. toAssignableListItem(exprList, index, isLHS) {
  10033. const node = exprList[index];
  10034. if (node.type === "TSTypeCastExpression") {
  10035. exprList[index] = this.typeCastToParameter(node);
  10036. }
  10037. super.toAssignableListItem(exprList, index, isLHS);
  10038. }
  10039. typeCastToParameter(node) {
  10040. node.expression.typeAnnotation = node.typeAnnotation;
  10041. this.resetEndLocation(node.expression, node.typeAnnotation.loc.end);
  10042. return node.expression;
  10043. }
  10044. shouldParseArrow(params) {
  10045. if (this.match(14)) {
  10046. return params.every(expr => this.isAssignable(expr, true));
  10047. }
  10048. return super.shouldParseArrow(params);
  10049. }
  10050. shouldParseAsyncArrow() {
  10051. return this.match(14) || super.shouldParseAsyncArrow();
  10052. }
  10053. canHaveLeadingDecorator() {
  10054. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  10055. }
  10056. jsxParseOpeningElementAfterName(node) {
  10057. if (this.match(47) || this.match(51)) {
  10058. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArgumentsInExpression());
  10059. if (typeArguments) {
  10060. node.typeParameters = typeArguments;
  10061. }
  10062. }
  10063. return super.jsxParseOpeningElementAfterName(node);
  10064. }
  10065. getGetterSetterExpectedParamCount(method) {
  10066. const baseCount = super.getGetterSetterExpectedParamCount(method);
  10067. const params = this.getObjectOrClassMethodParams(method);
  10068. const firstParam = params[0];
  10069. const hasContextParam = firstParam && this.isThisParam(firstParam);
  10070. return hasContextParam ? baseCount + 1 : baseCount;
  10071. }
  10072. parseCatchClauseParam() {
  10073. const param = super.parseCatchClauseParam();
  10074. const type = this.tsTryParseTypeAnnotation();
  10075. if (type) {
  10076. param.typeAnnotation = type;
  10077. this.resetEndLocation(param);
  10078. }
  10079. return param;
  10080. }
  10081. tsInAmbientContext(cb) {
  10082. const {
  10083. isAmbientContext: oldIsAmbientContext,
  10084. strict: oldStrict
  10085. } = this.state;
  10086. this.state.isAmbientContext = true;
  10087. this.state.strict = false;
  10088. try {
  10089. return cb();
  10090. } finally {
  10091. this.state.isAmbientContext = oldIsAmbientContext;
  10092. this.state.strict = oldStrict;
  10093. }
  10094. }
  10095. parseClass(node, isStatement, optionalId) {
  10096. const oldInAbstractClass = this.state.inAbstractClass;
  10097. this.state.inAbstractClass = !!node.abstract;
  10098. try {
  10099. return super.parseClass(node, isStatement, optionalId);
  10100. } finally {
  10101. this.state.inAbstractClass = oldInAbstractClass;
  10102. }
  10103. }
  10104. tsParseAbstractDeclaration(node, decorators) {
  10105. if (this.match(80)) {
  10106. node.abstract = true;
  10107. return this.maybeTakeDecorators(decorators, this.parseClass(node, true, false));
  10108. } else if (this.isContextual(129)) {
  10109. if (!this.hasFollowingLineBreak()) {
  10110. node.abstract = true;
  10111. this.raise(TSErrors.NonClassMethodPropertyHasAbstractModifier, node);
  10112. return this.tsParseInterfaceDeclaration(node);
  10113. } else {
  10114. return null;
  10115. }
  10116. }
  10117. throw this.unexpected(null, 80);
  10118. }
  10119. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope) {
  10120. const method = super.parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  10121. if (method.abstract || method.type === "TSAbstractMethodDefinition") {
  10122. const hasEstreePlugin = this.hasPlugin("estree");
  10123. const methodFn = hasEstreePlugin ? method.value : method;
  10124. if (methodFn.body) {
  10125. const {
  10126. key
  10127. } = method;
  10128. this.raise(TSErrors.AbstractMethodHasImplementation, method, {
  10129. methodName: key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(this.offsetToSourcePos(key.start), this.offsetToSourcePos(key.end))}]`
  10130. });
  10131. }
  10132. }
  10133. return method;
  10134. }
  10135. tsParseTypeParameterName() {
  10136. const typeName = this.parseIdentifier();
  10137. return typeName.name;
  10138. }
  10139. shouldParseAsAmbientContext() {
  10140. return !!this.getPluginOption("typescript", "dts");
  10141. }
  10142. parse() {
  10143. if (this.shouldParseAsAmbientContext()) {
  10144. this.state.isAmbientContext = true;
  10145. }
  10146. return super.parse();
  10147. }
  10148. getExpression() {
  10149. if (this.shouldParseAsAmbientContext()) {
  10150. this.state.isAmbientContext = true;
  10151. }
  10152. return super.getExpression();
  10153. }
  10154. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  10155. if (!isString && isMaybeTypeOnly) {
  10156. this.parseTypeOnlyImportExportSpecifier(node, false, isInTypeExport);
  10157. return this.finishNode(node, "ExportSpecifier");
  10158. }
  10159. node.exportKind = "value";
  10160. return super.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly);
  10161. }
  10162. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  10163. if (!importedIsString && isMaybeTypeOnly) {
  10164. this.parseTypeOnlyImportExportSpecifier(specifier, true, isInTypeOnlyImport);
  10165. return this.finishNode(specifier, "ImportSpecifier");
  10166. }
  10167. specifier.importKind = "value";
  10168. return super.parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, isInTypeOnlyImport ? 4098 : 4096);
  10169. }
  10170. parseTypeOnlyImportExportSpecifier(node, isImport, isInTypeOnlyImportExport) {
  10171. const leftOfAsKey = isImport ? "imported" : "local";
  10172. const rightOfAsKey = isImport ? "local" : "exported";
  10173. let leftOfAs = node[leftOfAsKey];
  10174. let rightOfAs;
  10175. let hasTypeSpecifier = false;
  10176. let canParseAsKeyword = true;
  10177. const loc = leftOfAs.loc.start;
  10178. if (this.isContextual(93)) {
  10179. const firstAs = this.parseIdentifier();
  10180. if (this.isContextual(93)) {
  10181. const secondAs = this.parseIdentifier();
  10182. if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10183. hasTypeSpecifier = true;
  10184. leftOfAs = firstAs;
  10185. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10186. canParseAsKeyword = false;
  10187. } else {
  10188. rightOfAs = secondAs;
  10189. canParseAsKeyword = false;
  10190. }
  10191. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10192. canParseAsKeyword = false;
  10193. rightOfAs = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10194. } else {
  10195. hasTypeSpecifier = true;
  10196. leftOfAs = firstAs;
  10197. }
  10198. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  10199. hasTypeSpecifier = true;
  10200. if (isImport) {
  10201. leftOfAs = this.parseIdentifier(true);
  10202. if (!this.isContextual(93)) {
  10203. this.checkReservedWord(leftOfAs.name, leftOfAs.loc.start, true, true);
  10204. }
  10205. } else {
  10206. leftOfAs = this.parseModuleExportName();
  10207. }
  10208. }
  10209. if (hasTypeSpecifier && isInTypeOnlyImportExport) {
  10210. this.raise(isImport ? TSErrors.TypeModifierIsUsedInTypeImports : TSErrors.TypeModifierIsUsedInTypeExports, loc);
  10211. }
  10212. node[leftOfAsKey] = leftOfAs;
  10213. node[rightOfAsKey] = rightOfAs;
  10214. const kindKey = isImport ? "importKind" : "exportKind";
  10215. node[kindKey] = hasTypeSpecifier ? "type" : "value";
  10216. if (canParseAsKeyword && this.eatContextual(93)) {
  10217. node[rightOfAsKey] = isImport ? this.parseIdentifier() : this.parseModuleExportName();
  10218. }
  10219. if (!node[rightOfAsKey]) {
  10220. node[rightOfAsKey] = this.cloneIdentifier(node[leftOfAsKey]);
  10221. }
  10222. if (isImport) {
  10223. this.checkIdentifier(node[rightOfAsKey], hasTypeSpecifier ? 4098 : 4096);
  10224. }
  10225. }
  10226. fillOptionalPropertiesForTSESLint(node) {
  10227. var _node$directive, _node$decorators, _node$optional, _node$typeAnnotation, _node$accessibility, _node$decorators2, _node$override, _node$readonly, _node$static, _node$declare, _node$returnType, _node$typeParameters, _node$optional2, _node$optional3, _node$accessibility2, _node$readonly2, _node$static2, _node$declare2, _node$definite, _node$readonly3, _node$typeAnnotation2, _node$accessibility3, _node$decorators3, _node$override2, _node$optional4, _node$id, _node$abstract, _node$declare3, _node$decorators4, _node$implements, _node$superTypeArgume, _node$typeParameters2, _node$declare4, _node$definite2, _node$const, _node$declare5, _node$computed, _node$qualifier, _node$options, _node$declare6, _node$extends, _node$optional5, _node$readonly4, _node$declare7, _node$global, _node$const2, _node$in, _node$out;
  10228. switch (node.type) {
  10229. case "ExpressionStatement":
  10230. (_node$directive = node.directive) != null ? _node$directive : node.directive = undefined;
  10231. return;
  10232. case "RestElement":
  10233. node.value = undefined;
  10234. case "Identifier":
  10235. case "ArrayPattern":
  10236. case "AssignmentPattern":
  10237. case "ObjectPattern":
  10238. (_node$decorators = node.decorators) != null ? _node$decorators : node.decorators = [];
  10239. (_node$optional = node.optional) != null ? _node$optional : node.optional = false;
  10240. (_node$typeAnnotation = node.typeAnnotation) != null ? _node$typeAnnotation : node.typeAnnotation = undefined;
  10241. return;
  10242. case "TSParameterProperty":
  10243. (_node$accessibility = node.accessibility) != null ? _node$accessibility : node.accessibility = undefined;
  10244. (_node$decorators2 = node.decorators) != null ? _node$decorators2 : node.decorators = [];
  10245. (_node$override = node.override) != null ? _node$override : node.override = false;
  10246. (_node$readonly = node.readonly) != null ? _node$readonly : node.readonly = false;
  10247. (_node$static = node.static) != null ? _node$static : node.static = false;
  10248. return;
  10249. case "TSEmptyBodyFunctionExpression":
  10250. node.body = null;
  10251. case "TSDeclareFunction":
  10252. case "FunctionDeclaration":
  10253. case "FunctionExpression":
  10254. case "ClassMethod":
  10255. case "ClassPrivateMethod":
  10256. (_node$declare = node.declare) != null ? _node$declare : node.declare = false;
  10257. (_node$returnType = node.returnType) != null ? _node$returnType : node.returnType = undefined;
  10258. (_node$typeParameters = node.typeParameters) != null ? _node$typeParameters : node.typeParameters = undefined;
  10259. return;
  10260. case "Property":
  10261. (_node$optional2 = node.optional) != null ? _node$optional2 : node.optional = false;
  10262. return;
  10263. case "TSMethodSignature":
  10264. case "TSPropertySignature":
  10265. (_node$optional3 = node.optional) != null ? _node$optional3 : node.optional = false;
  10266. case "TSIndexSignature":
  10267. (_node$accessibility2 = node.accessibility) != null ? _node$accessibility2 : node.accessibility = undefined;
  10268. (_node$readonly2 = node.readonly) != null ? _node$readonly2 : node.readonly = false;
  10269. (_node$static2 = node.static) != null ? _node$static2 : node.static = false;
  10270. return;
  10271. case "TSAbstractPropertyDefinition":
  10272. case "PropertyDefinition":
  10273. case "TSAbstractAccessorProperty":
  10274. case "AccessorProperty":
  10275. (_node$declare2 = node.declare) != null ? _node$declare2 : node.declare = false;
  10276. (_node$definite = node.definite) != null ? _node$definite : node.definite = false;
  10277. (_node$readonly3 = node.readonly) != null ? _node$readonly3 : node.readonly = false;
  10278. (_node$typeAnnotation2 = node.typeAnnotation) != null ? _node$typeAnnotation2 : node.typeAnnotation = undefined;
  10279. case "TSAbstractMethodDefinition":
  10280. case "MethodDefinition":
  10281. (_node$accessibility3 = node.accessibility) != null ? _node$accessibility3 : node.accessibility = undefined;
  10282. (_node$decorators3 = node.decorators) != null ? _node$decorators3 : node.decorators = [];
  10283. (_node$override2 = node.override) != null ? _node$override2 : node.override = false;
  10284. (_node$optional4 = node.optional) != null ? _node$optional4 : node.optional = false;
  10285. return;
  10286. case "ClassExpression":
  10287. (_node$id = node.id) != null ? _node$id : node.id = null;
  10288. case "ClassDeclaration":
  10289. (_node$abstract = node.abstract) != null ? _node$abstract : node.abstract = false;
  10290. (_node$declare3 = node.declare) != null ? _node$declare3 : node.declare = false;
  10291. (_node$decorators4 = node.decorators) != null ? _node$decorators4 : node.decorators = [];
  10292. (_node$implements = node.implements) != null ? _node$implements : node.implements = [];
  10293. (_node$superTypeArgume = node.superTypeArguments) != null ? _node$superTypeArgume : node.superTypeArguments = undefined;
  10294. (_node$typeParameters2 = node.typeParameters) != null ? _node$typeParameters2 : node.typeParameters = undefined;
  10295. return;
  10296. case "TSTypeAliasDeclaration":
  10297. case "VariableDeclaration":
  10298. (_node$declare4 = node.declare) != null ? _node$declare4 : node.declare = false;
  10299. return;
  10300. case "VariableDeclarator":
  10301. (_node$definite2 = node.definite) != null ? _node$definite2 : node.definite = false;
  10302. return;
  10303. case "TSEnumDeclaration":
  10304. (_node$const = node.const) != null ? _node$const : node.const = false;
  10305. (_node$declare5 = node.declare) != null ? _node$declare5 : node.declare = false;
  10306. return;
  10307. case "TSEnumMember":
  10308. (_node$computed = node.computed) != null ? _node$computed : node.computed = false;
  10309. return;
  10310. case "TSImportType":
  10311. (_node$qualifier = node.qualifier) != null ? _node$qualifier : node.qualifier = null;
  10312. (_node$options = node.options) != null ? _node$options : node.options = null;
  10313. return;
  10314. case "TSInterfaceDeclaration":
  10315. (_node$declare6 = node.declare) != null ? _node$declare6 : node.declare = false;
  10316. (_node$extends = node.extends) != null ? _node$extends : node.extends = [];
  10317. return;
  10318. case "TSMappedType":
  10319. (_node$optional5 = node.optional) != null ? _node$optional5 : node.optional = false;
  10320. (_node$readonly4 = node.readonly) != null ? _node$readonly4 : node.readonly = undefined;
  10321. return;
  10322. case "TSModuleDeclaration":
  10323. (_node$declare7 = node.declare) != null ? _node$declare7 : node.declare = false;
  10324. (_node$global = node.global) != null ? _node$global : node.global = node.kind === "global";
  10325. return;
  10326. case "TSTypeParameter":
  10327. (_node$const2 = node.const) != null ? _node$const2 : node.const = false;
  10328. (_node$in = node.in) != null ? _node$in : node.in = false;
  10329. (_node$out = node.out) != null ? _node$out : node.out = false;
  10330. return;
  10331. }
  10332. }
  10333. chStartsBindingIdentifierAndNotRelationalOperator(ch, pos) {
  10334. if (isIdentifierStart(ch)) {
  10335. keywordAndTSRelationalOperator.lastIndex = pos;
  10336. if (keywordAndTSRelationalOperator.test(this.input)) {
  10337. const endCh = this.codePointAtPos(keywordAndTSRelationalOperator.lastIndex);
  10338. if (!isIdentifierChar(endCh) && endCh !== 92) {
  10339. return false;
  10340. }
  10341. }
  10342. return true;
  10343. } else if (ch === 92) {
  10344. return true;
  10345. } else {
  10346. return false;
  10347. }
  10348. }
  10349. nextTokenIsIdentifierAndNotTSRelationalOperatorOnSameLine() {
  10350. const next = this.nextTokenInLineStart();
  10351. const nextCh = this.codePointAtPos(next);
  10352. return this.chStartsBindingIdentifierAndNotRelationalOperator(nextCh, next);
  10353. }
  10354. nextTokenIsIdentifierOrStringLiteralOnSameLine() {
  10355. const next = this.nextTokenInLineStart();
  10356. const nextCh = this.codePointAtPos(next);
  10357. return this.chStartsBindingIdentifier(nextCh, next) || nextCh === 34 || nextCh === 39;
  10358. }
  10359. };
  10360. function isPossiblyLiteralEnum(expression) {
  10361. if (expression.type !== "MemberExpression") return false;
  10362. const {
  10363. computed,
  10364. property
  10365. } = expression;
  10366. if (computed && property.type !== "StringLiteral" && (property.type !== "TemplateLiteral" || property.expressions.length > 0)) {
  10367. return false;
  10368. }
  10369. return isUncomputedMemberExpressionChain(expression.object);
  10370. }
  10371. function isValidAmbientConstInitializer(expression, estree) {
  10372. var _expression$extra;
  10373. const {
  10374. type
  10375. } = expression;
  10376. if ((_expression$extra = expression.extra) != null && _expression$extra.parenthesized) {
  10377. return false;
  10378. }
  10379. if (estree) {
  10380. if (type === "Literal") {
  10381. const {
  10382. value
  10383. } = expression;
  10384. if (typeof value === "string" || typeof value === "boolean") {
  10385. return true;
  10386. }
  10387. }
  10388. } else {
  10389. if (type === "StringLiteral" || type === "BooleanLiteral") {
  10390. return true;
  10391. }
  10392. }
  10393. if (isNumber(expression, estree) || isNegativeNumber(expression, estree)) {
  10394. return true;
  10395. }
  10396. if (type === "TemplateLiteral" && expression.expressions.length === 0) {
  10397. return true;
  10398. }
  10399. if (isPossiblyLiteralEnum(expression)) {
  10400. return true;
  10401. }
  10402. return false;
  10403. }
  10404. function isNumber(expression, estree) {
  10405. if (estree) {
  10406. return expression.type === "Literal" && (typeof expression.value === "number" || "bigint" in expression);
  10407. }
  10408. return expression.type === "NumericLiteral" || expression.type === "BigIntLiteral";
  10409. }
  10410. function isNegativeNumber(expression, estree) {
  10411. if (expression.type === "UnaryExpression") {
  10412. const {
  10413. operator,
  10414. argument
  10415. } = expression;
  10416. if (operator === "-" && isNumber(argument, estree)) {
  10417. return true;
  10418. }
  10419. }
  10420. return false;
  10421. }
  10422. function isUncomputedMemberExpressionChain(expression) {
  10423. if (expression.type === "Identifier") return true;
  10424. if (expression.type !== "MemberExpression" || expression.computed) {
  10425. return false;
  10426. }
  10427. return isUncomputedMemberExpressionChain(expression.object);
  10428. }
  10429. const PlaceholderErrors = ParseErrorEnum`placeholders`({
  10430. ClassNameIsRequired: "A class name is required.",
  10431. UnexpectedSpace: "Unexpected space in placeholder."
  10432. });
  10433. var placeholders = superClass => class PlaceholdersParserMixin extends superClass {
  10434. parsePlaceholder(expectedNode) {
  10435. if (this.match(133)) {
  10436. const node = this.startNode();
  10437. this.next();
  10438. this.assertNoSpace();
  10439. node.name = super.parseIdentifier(true);
  10440. this.assertNoSpace();
  10441. this.expect(133);
  10442. return this.finishPlaceholder(node, expectedNode);
  10443. }
  10444. }
  10445. finishPlaceholder(node, expectedNode) {
  10446. let placeholder = node;
  10447. if (!placeholder.expectedNode || !placeholder.type) {
  10448. placeholder = this.finishNode(placeholder, "Placeholder");
  10449. }
  10450. placeholder.expectedNode = expectedNode;
  10451. return placeholder;
  10452. }
  10453. getTokenFromCode(code) {
  10454. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  10455. this.finishOp(133, 2);
  10456. } else {
  10457. super.getTokenFromCode(code);
  10458. }
  10459. }
  10460. parseExprAtom(refExpressionErrors) {
  10461. return this.parsePlaceholder("Expression") || super.parseExprAtom(refExpressionErrors);
  10462. }
  10463. parseIdentifier(liberal) {
  10464. return this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal);
  10465. }
  10466. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10467. if (word !== undefined) {
  10468. super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
  10469. }
  10470. }
  10471. cloneIdentifier(node) {
  10472. const cloned = super.cloneIdentifier(node);
  10473. if (cloned.type === "Placeholder") {
  10474. cloned.expectedNode = node.expectedNode;
  10475. }
  10476. return cloned;
  10477. }
  10478. cloneStringLiteral(node) {
  10479. if (node.type === "Placeholder") {
  10480. return this.cloneIdentifier(node);
  10481. }
  10482. return super.cloneStringLiteral(node);
  10483. }
  10484. parseBindingAtom() {
  10485. return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
  10486. }
  10487. isValidLVal(type, disallowCallExpression, isParenthesized, binding) {
  10488. return type === "Placeholder" || super.isValidLVal(type, disallowCallExpression, isParenthesized, binding);
  10489. }
  10490. toAssignable(node, isLHS) {
  10491. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  10492. node.expectedNode = "Pattern";
  10493. } else {
  10494. super.toAssignable(node, isLHS);
  10495. }
  10496. }
  10497. chStartsBindingIdentifier(ch, pos) {
  10498. if (super.chStartsBindingIdentifier(ch, pos)) {
  10499. return true;
  10500. }
  10501. const next = this.nextTokenStart();
  10502. if (this.input.charCodeAt(next) === 37 && this.input.charCodeAt(next + 1) === 37) {
  10503. return true;
  10504. }
  10505. return false;
  10506. }
  10507. verifyBreakContinue(node, isBreak) {
  10508. var _node$label;
  10509. if (((_node$label = node.label) == null ? void 0 : _node$label.type) === "Placeholder") return;
  10510. super.verifyBreakContinue(node, isBreak);
  10511. }
  10512. parseExpressionStatement(node, expr) {
  10513. var _expr$extra;
  10514. if (expr.type !== "Placeholder" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  10515. return super.parseExpressionStatement(node, expr);
  10516. }
  10517. if (this.match(14)) {
  10518. const stmt = node;
  10519. stmt.label = this.finishPlaceholder(expr, "Identifier");
  10520. this.next();
  10521. stmt.body = super.parseStatementOrSloppyAnnexBFunctionDeclaration();
  10522. return this.finishNode(stmt, "LabeledStatement");
  10523. }
  10524. this.semicolon();
  10525. const stmtPlaceholder = node;
  10526. stmtPlaceholder.name = expr.name;
  10527. return this.finishPlaceholder(stmtPlaceholder, "Statement");
  10528. }
  10529. parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse) {
  10530. return this.parsePlaceholder("BlockStatement") || super.parseBlock(allowDirectives, createNewLexicalScope, afterBlockParse);
  10531. }
  10532. parseFunctionId(requireId) {
  10533. return this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId);
  10534. }
  10535. parseClass(node, isStatement, optionalId) {
  10536. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  10537. this.next();
  10538. const oldStrict = this.state.strict;
  10539. const placeholder = this.parsePlaceholder("Identifier");
  10540. if (placeholder) {
  10541. if (this.match(81) || this.match(133) || this.match(5)) {
  10542. node.id = placeholder;
  10543. } else if (optionalId || !isStatement) {
  10544. node.id = null;
  10545. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  10546. return this.finishNode(node, type);
  10547. } else {
  10548. throw this.raise(PlaceholderErrors.ClassNameIsRequired, this.state.startLoc);
  10549. }
  10550. } else {
  10551. this.parseClassId(node, isStatement, optionalId);
  10552. }
  10553. super.parseClassSuper(node);
  10554. node.body = this.parsePlaceholder("ClassBody") || super.parseClassBody(!!node.superClass, oldStrict);
  10555. return this.finishNode(node, type);
  10556. }
  10557. parseExport(node, decorators) {
  10558. const placeholder = this.parsePlaceholder("Identifier");
  10559. if (!placeholder) return super.parseExport(node, decorators);
  10560. const node2 = node;
  10561. if (!this.isContextual(98) && !this.match(12)) {
  10562. node2.specifiers = [];
  10563. node2.source = null;
  10564. node2.declaration = this.finishPlaceholder(placeholder, "Declaration");
  10565. return this.finishNode(node2, "ExportNamedDeclaration");
  10566. }
  10567. this.expectPlugin("exportDefaultFrom");
  10568. const specifier = this.startNode();
  10569. specifier.exported = placeholder;
  10570. node2.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  10571. return super.parseExport(node2, decorators);
  10572. }
  10573. isExportDefaultSpecifier() {
  10574. if (this.match(65)) {
  10575. const next = this.nextTokenStart();
  10576. if (this.isUnparsedContextual(next, "from")) {
  10577. if (this.input.startsWith(tokenLabelName(133), this.nextTokenStartSince(next + 4))) {
  10578. return true;
  10579. }
  10580. }
  10581. }
  10582. return super.isExportDefaultSpecifier();
  10583. }
  10584. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  10585. var _specifiers;
  10586. if ((_specifiers = node.specifiers) != null && _specifiers.length) {
  10587. return true;
  10588. }
  10589. return super.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  10590. }
  10591. checkExport(node) {
  10592. const {
  10593. specifiers
  10594. } = node;
  10595. if (specifiers != null && specifiers.length) {
  10596. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  10597. }
  10598. super.checkExport(node);
  10599. node.specifiers = specifiers;
  10600. }
  10601. parseImport(node) {
  10602. const placeholder = this.parsePlaceholder("Identifier");
  10603. if (!placeholder) return super.parseImport(node);
  10604. node.specifiers = [];
  10605. if (!this.isContextual(98) && !this.match(12)) {
  10606. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  10607. this.semicolon();
  10608. return this.finishNode(node, "ImportDeclaration");
  10609. }
  10610. const specifier = this.startNodeAtNode(placeholder);
  10611. specifier.local = placeholder;
  10612. node.specifiers.push(this.finishNode(specifier, "ImportDefaultSpecifier"));
  10613. if (this.eat(12)) {
  10614. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  10615. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  10616. }
  10617. this.expectContextual(98);
  10618. node.source = this.parseImportSource();
  10619. this.semicolon();
  10620. return this.finishNode(node, "ImportDeclaration");
  10621. }
  10622. parseImportSource() {
  10623. return this.parsePlaceholder("StringLiteral") || super.parseImportSource();
  10624. }
  10625. assertNoSpace() {
  10626. if (this.state.start > this.offsetToSourcePos(this.state.lastTokEndLoc.index)) {
  10627. this.raise(PlaceholderErrors.UnexpectedSpace, this.state.lastTokEndLoc);
  10628. }
  10629. }
  10630. };
  10631. var v8intrinsic = superClass => class V8IntrinsicMixin extends superClass {
  10632. parseV8Intrinsic() {
  10633. if (this.match(54)) {
  10634. const v8IntrinsicStartLoc = this.state.startLoc;
  10635. const node = this.startNode();
  10636. this.next();
  10637. if (tokenIsIdentifier(this.state.type)) {
  10638. const name = this.parseIdentifierName();
  10639. const identifier = this.createIdentifier(node, name);
  10640. this.castNodeTo(identifier, "V8IntrinsicIdentifier");
  10641. if (this.match(10)) {
  10642. return identifier;
  10643. }
  10644. }
  10645. this.unexpected(v8IntrinsicStartLoc);
  10646. }
  10647. }
  10648. parseExprAtom(refExpressionErrors) {
  10649. return this.parseV8Intrinsic() || super.parseExprAtom(refExpressionErrors);
  10650. }
  10651. };
  10652. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  10653. const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
  10654. function validatePlugins(pluginsMap) {
  10655. if (pluginsMap.has("decorators")) {
  10656. if (pluginsMap.has("decorators-legacy")) {
  10657. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  10658. }
  10659. const decoratorsBeforeExport = pluginsMap.get("decorators").decoratorsBeforeExport;
  10660. if (decoratorsBeforeExport != null && typeof decoratorsBeforeExport !== "boolean") {
  10661. throw new Error("'decoratorsBeforeExport' must be a boolean, if specified.");
  10662. }
  10663. const allowCallParenthesized = pluginsMap.get("decorators").allowCallParenthesized;
  10664. if (allowCallParenthesized != null && typeof allowCallParenthesized !== "boolean") {
  10665. throw new Error("'allowCallParenthesized' must be a boolean.");
  10666. }
  10667. }
  10668. if (pluginsMap.has("flow") && pluginsMap.has("typescript")) {
  10669. throw new Error("Cannot combine flow and typescript plugins.");
  10670. }
  10671. if (pluginsMap.has("placeholders") && pluginsMap.has("v8intrinsic")) {
  10672. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  10673. }
  10674. if (pluginsMap.has("pipelineOperator")) {
  10675. var _pluginsMap$get2;
  10676. const proposal = pluginsMap.get("pipelineOperator").proposal;
  10677. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  10678. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  10679. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  10680. }
  10681. if (proposal === "hack") {
  10682. var _pluginsMap$get;
  10683. if (pluginsMap.has("placeholders")) {
  10684. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  10685. }
  10686. if (pluginsMap.has("v8intrinsic")) {
  10687. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  10688. }
  10689. const topicToken = pluginsMap.get("pipelineOperator").topicToken;
  10690. if (!TOPIC_TOKENS.includes(topicToken)) {
  10691. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  10692. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  10693. }
  10694. if (topicToken === "#" && ((_pluginsMap$get = pluginsMap.get("recordAndTuple")) == null ? void 0 : _pluginsMap$get.syntaxType) === "hash") {
  10695. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "hack", topicToken: "#" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`);
  10696. }
  10697. } else if (proposal === "smart" && ((_pluginsMap$get2 = pluginsMap.get("recordAndTuple")) == null ? void 0 : _pluginsMap$get2.syntaxType) === "hash") {
  10698. throw new Error(`Plugin conflict between \`["pipelineOperator", { proposal: "smart" }]\` and \`${JSON.stringify(["recordAndTuple", pluginsMap.get("recordAndTuple")])}\`.`);
  10699. }
  10700. }
  10701. if (pluginsMap.has("moduleAttributes")) {
  10702. if (pluginsMap.has("deprecatedImportAssert") || pluginsMap.has("importAssertions")) {
  10703. throw new Error("Cannot combine importAssertions, deprecatedImportAssert and moduleAttributes plugins.");
  10704. }
  10705. const moduleAttributesVersionPluginOption = pluginsMap.get("moduleAttributes").version;
  10706. if (moduleAttributesVersionPluginOption !== "may-2020") {
  10707. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  10708. }
  10709. }
  10710. if (pluginsMap.has("importAssertions")) {
  10711. if (pluginsMap.has("deprecatedImportAssert")) {
  10712. throw new Error("Cannot combine importAssertions and deprecatedImportAssert plugins.");
  10713. }
  10714. }
  10715. if (pluginsMap.has("deprecatedImportAssert")) ;else if (pluginsMap.has("importAttributes") && pluginsMap.get("importAttributes").deprecatedAssertSyntax) {
  10716. pluginsMap.set("deprecatedImportAssert", {});
  10717. }
  10718. if (pluginsMap.has("recordAndTuple")) {
  10719. const syntaxType = pluginsMap.get("recordAndTuple").syntaxType;
  10720. if (syntaxType != null) {
  10721. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  10722. if (!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(syntaxType)) {
  10723. throw new Error("The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  10724. }
  10725. }
  10726. }
  10727. if (pluginsMap.has("asyncDoExpressions") && !pluginsMap.has("doExpressions")) {
  10728. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  10729. error.missingPlugins = "doExpressions";
  10730. throw error;
  10731. }
  10732. if (pluginsMap.has("optionalChainingAssign") && pluginsMap.get("optionalChainingAssign").version !== "2023-07") {
  10733. throw new Error("The 'optionalChainingAssign' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is '2023-07'.");
  10734. }
  10735. if (pluginsMap.has("discardBinding") && pluginsMap.get("discardBinding").syntaxType !== "void") {
  10736. throw new Error("The 'discardBinding' plugin requires a 'syntaxType' option. Currently the only supported value is 'void'.");
  10737. }
  10738. }
  10739. const mixinPlugins = {
  10740. estree,
  10741. jsx,
  10742. flow,
  10743. typescript,
  10744. v8intrinsic,
  10745. placeholders
  10746. };
  10747. const mixinPluginNames = Object.keys(mixinPlugins);
  10748. class ExpressionParser extends LValParser {
  10749. checkProto(prop, isRecord, sawProto, refExpressionErrors) {
  10750. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  10751. return sawProto;
  10752. }
  10753. const key = prop.key;
  10754. const name = key.type === "Identifier" ? key.name : key.value;
  10755. if (name === "__proto__") {
  10756. if (isRecord) {
  10757. this.raise(Errors.RecordNoProto, key);
  10758. return true;
  10759. }
  10760. if (sawProto) {
  10761. if (refExpressionErrors) {
  10762. if (refExpressionErrors.doubleProtoLoc === null) {
  10763. refExpressionErrors.doubleProtoLoc = key.loc.start;
  10764. }
  10765. } else {
  10766. this.raise(Errors.DuplicateProto, key);
  10767. }
  10768. }
  10769. return true;
  10770. }
  10771. return sawProto;
  10772. }
  10773. shouldExitDescending(expr, potentialArrowAt) {
  10774. return expr.type === "ArrowFunctionExpression" && this.offsetToSourcePos(expr.start) === potentialArrowAt;
  10775. }
  10776. getExpression() {
  10777. this.enterInitialScopes();
  10778. this.nextToken();
  10779. if (this.match(140)) {
  10780. throw this.raise(Errors.ParseExpressionEmptyInput, this.state.startLoc);
  10781. }
  10782. const expr = this.parseExpression();
  10783. if (!this.match(140)) {
  10784. throw this.raise(Errors.ParseExpressionExpectsEOF, this.state.startLoc, {
  10785. unexpected: this.input.codePointAt(this.state.start)
  10786. });
  10787. }
  10788. this.finalizeRemainingComments();
  10789. expr.comments = this.comments;
  10790. expr.errors = this.state.errors;
  10791. if (this.optionFlags & 256) {
  10792. expr.tokens = this.tokens;
  10793. }
  10794. return expr;
  10795. }
  10796. parseExpression(disallowIn, refExpressionErrors) {
  10797. if (disallowIn) {
  10798. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10799. }
  10800. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  10801. }
  10802. parseExpressionBase(refExpressionErrors) {
  10803. const startLoc = this.state.startLoc;
  10804. const expr = this.parseMaybeAssign(refExpressionErrors);
  10805. if (this.match(12)) {
  10806. const node = this.startNodeAt(startLoc);
  10807. node.expressions = [expr];
  10808. while (this.eat(12)) {
  10809. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  10810. }
  10811. this.toReferencedList(node.expressions);
  10812. return this.finishNode(node, "SequenceExpression");
  10813. }
  10814. return expr;
  10815. }
  10816. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  10817. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10818. }
  10819. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  10820. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  10821. }
  10822. setOptionalParametersError(refExpressionErrors) {
  10823. refExpressionErrors.optionalParametersLoc = this.state.startLoc;
  10824. }
  10825. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  10826. const startLoc = this.state.startLoc;
  10827. const isYield = this.isContextual(108);
  10828. if (isYield) {
  10829. if (this.prodParam.hasYield) {
  10830. this.next();
  10831. let left = this.parseYield(startLoc);
  10832. if (afterLeftParse) {
  10833. left = afterLeftParse.call(this, left, startLoc);
  10834. }
  10835. return left;
  10836. }
  10837. }
  10838. let ownExpressionErrors;
  10839. if (refExpressionErrors) {
  10840. ownExpressionErrors = false;
  10841. } else {
  10842. refExpressionErrors = new ExpressionErrors();
  10843. ownExpressionErrors = true;
  10844. }
  10845. const {
  10846. type
  10847. } = this.state;
  10848. if (type === 10 || tokenIsIdentifier(type)) {
  10849. this.state.potentialArrowAt = this.state.start;
  10850. }
  10851. let left = this.parseMaybeConditional(refExpressionErrors);
  10852. if (afterLeftParse) {
  10853. left = afterLeftParse.call(this, left, startLoc);
  10854. }
  10855. if (tokenIsAssignment(this.state.type)) {
  10856. const node = this.startNodeAt(startLoc);
  10857. const operator = this.state.value;
  10858. node.operator = operator;
  10859. if (this.match(29)) {
  10860. this.toAssignable(left, true);
  10861. node.left = left;
  10862. const startIndex = startLoc.index;
  10863. if (refExpressionErrors.doubleProtoLoc != null && refExpressionErrors.doubleProtoLoc.index >= startIndex) {
  10864. refExpressionErrors.doubleProtoLoc = null;
  10865. }
  10866. if (refExpressionErrors.shorthandAssignLoc != null && refExpressionErrors.shorthandAssignLoc.index >= startIndex) {
  10867. refExpressionErrors.shorthandAssignLoc = null;
  10868. }
  10869. if (refExpressionErrors.privateKeyLoc != null && refExpressionErrors.privateKeyLoc.index >= startIndex) {
  10870. this.checkDestructuringPrivate(refExpressionErrors);
  10871. refExpressionErrors.privateKeyLoc = null;
  10872. }
  10873. if (refExpressionErrors.voidPatternLoc != null && refExpressionErrors.voidPatternLoc.index >= startIndex) {
  10874. refExpressionErrors.voidPatternLoc = null;
  10875. }
  10876. } else {
  10877. node.left = left;
  10878. }
  10879. this.next();
  10880. node.right = this.parseMaybeAssign();
  10881. this.checkLVal(left, this.finishNode(node, "AssignmentExpression"), undefined, undefined, undefined, undefined, operator === "||=" || operator === "&&=" || operator === "??=");
  10882. return node;
  10883. } else if (ownExpressionErrors) {
  10884. this.checkExpressionErrors(refExpressionErrors, true);
  10885. }
  10886. if (isYield) {
  10887. const {
  10888. type
  10889. } = this.state;
  10890. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  10891. if (startsExpr && !this.isAmbiguousPrefixOrIdentifier()) {
  10892. this.raiseOverwrite(Errors.YieldNotInGeneratorFunction, startLoc);
  10893. return this.parseYield(startLoc);
  10894. }
  10895. }
  10896. return left;
  10897. }
  10898. parseMaybeConditional(refExpressionErrors) {
  10899. const startLoc = this.state.startLoc;
  10900. const potentialArrowAt = this.state.potentialArrowAt;
  10901. const expr = this.parseExprOps(refExpressionErrors);
  10902. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10903. return expr;
  10904. }
  10905. return this.parseConditional(expr, startLoc, refExpressionErrors);
  10906. }
  10907. parseConditional(expr, startLoc, refExpressionErrors) {
  10908. if (this.eat(17)) {
  10909. const node = this.startNodeAt(startLoc);
  10910. node.test = expr;
  10911. node.consequent = this.parseMaybeAssignAllowIn();
  10912. this.expect(14);
  10913. node.alternate = this.parseMaybeAssign();
  10914. return this.finishNode(node, "ConditionalExpression");
  10915. }
  10916. return expr;
  10917. }
  10918. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  10919. return this.match(139) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  10920. }
  10921. parseExprOps(refExpressionErrors) {
  10922. const startLoc = this.state.startLoc;
  10923. const potentialArrowAt = this.state.potentialArrowAt;
  10924. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  10925. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  10926. return expr;
  10927. }
  10928. return this.parseExprOp(expr, startLoc, -1);
  10929. }
  10930. parseExprOp(left, leftStartLoc, minPrec) {
  10931. if (this.isPrivateName(left)) {
  10932. const value = this.getPrivateNameSV(left);
  10933. if (minPrec >= tokenOperatorPrecedence(58) || !this.prodParam.hasIn || !this.match(58)) {
  10934. this.raise(Errors.PrivateInExpectedIn, left, {
  10935. identifierName: value
  10936. });
  10937. }
  10938. this.classScope.usePrivateName(value, left.loc.start);
  10939. }
  10940. const op = this.state.type;
  10941. if (tokenIsOperator(op) && (this.prodParam.hasIn || !this.match(58))) {
  10942. let prec = tokenOperatorPrecedence(op);
  10943. if (prec > minPrec) {
  10944. if (op === 39) {
  10945. this.expectPlugin("pipelineOperator");
  10946. if (this.state.inFSharpPipelineDirectBody) {
  10947. return left;
  10948. }
  10949. this.checkPipelineAtInfixOperator(left, leftStartLoc);
  10950. }
  10951. const node = this.startNodeAt(leftStartLoc);
  10952. node.left = left;
  10953. node.operator = this.state.value;
  10954. const logical = op === 41 || op === 42;
  10955. const coalesce = op === 40;
  10956. if (coalesce) {
  10957. prec = tokenOperatorPrecedence(42);
  10958. }
  10959. this.next();
  10960. if (op === 39 && this.hasPlugin(["pipelineOperator", {
  10961. proposal: "minimal"
  10962. }])) {
  10963. if (this.state.type === 96 && this.prodParam.hasAwait) {
  10964. throw this.raise(Errors.UnexpectedAwaitAfterPipelineBody, this.state.startLoc);
  10965. }
  10966. }
  10967. node.right = this.parseExprOpRightExpr(op, prec);
  10968. const finishedNode = this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  10969. const nextOp = this.state.type;
  10970. if (coalesce && (nextOp === 41 || nextOp === 42) || logical && nextOp === 40) {
  10971. throw this.raise(Errors.MixingCoalesceWithLogical, this.state.startLoc);
  10972. }
  10973. return this.parseExprOp(finishedNode, leftStartLoc, minPrec);
  10974. }
  10975. }
  10976. return left;
  10977. }
  10978. parseExprOpRightExpr(op, prec) {
  10979. const startLoc = this.state.startLoc;
  10980. switch (op) {
  10981. case 39:
  10982. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  10983. case "hack":
  10984. return this.withTopicBindingContext(() => {
  10985. return this.parseHackPipeBody();
  10986. });
  10987. case "fsharp":
  10988. return this.withSoloAwaitPermittingContext(() => {
  10989. return this.parseFSharpPipelineBody(prec);
  10990. });
  10991. }
  10992. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  10993. return this.withTopicBindingContext(() => {
  10994. if (this.prodParam.hasYield && this.isContextual(108)) {
  10995. throw this.raise(Errors.PipeBodyIsTighter, this.state.startLoc);
  10996. }
  10997. return this.parseSmartPipelineBodyInStyle(this.parseExprOpBaseRightExpr(op, prec), startLoc);
  10998. });
  10999. }
  11000. default:
  11001. return this.parseExprOpBaseRightExpr(op, prec);
  11002. }
  11003. }
  11004. parseExprOpBaseRightExpr(op, prec) {
  11005. const startLoc = this.state.startLoc;
  11006. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec);
  11007. }
  11008. parseHackPipeBody() {
  11009. var _body$extra;
  11010. const {
  11011. startLoc
  11012. } = this.state;
  11013. const body = this.parseMaybeAssign();
  11014. const requiredParentheses = UnparenthesizedPipeBodyDescriptions.has(body.type);
  11015. if (requiredParentheses && !((_body$extra = body.extra) != null && _body$extra.parenthesized)) {
  11016. this.raise(Errors.PipeUnparenthesizedBody, startLoc, {
  11017. type: body.type
  11018. });
  11019. }
  11020. if (!this.topicReferenceWasUsedInCurrentContext()) {
  11021. this.raise(Errors.PipeTopicUnused, startLoc);
  11022. }
  11023. return body;
  11024. }
  11025. checkExponentialAfterUnary(node) {
  11026. if (this.match(57)) {
  11027. this.raise(Errors.UnexpectedTokenUnaryExponentiation, node.argument);
  11028. }
  11029. }
  11030. parseMaybeUnary(refExpressionErrors, sawUnary) {
  11031. const startLoc = this.state.startLoc;
  11032. const isAwait = this.isContextual(96);
  11033. if (isAwait && this.recordAwaitIfAllowed()) {
  11034. this.next();
  11035. const expr = this.parseAwait(startLoc);
  11036. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  11037. return expr;
  11038. }
  11039. const update = this.match(34);
  11040. const node = this.startNode();
  11041. if (tokenIsPrefix(this.state.type)) {
  11042. node.operator = this.state.value;
  11043. node.prefix = true;
  11044. if (this.match(72)) {
  11045. this.expectPlugin("throwExpressions");
  11046. }
  11047. const isDelete = this.match(89);
  11048. this.next();
  11049. node.argument = this.parseMaybeUnary(null, true);
  11050. this.checkExpressionErrors(refExpressionErrors, true);
  11051. if (this.state.strict && isDelete) {
  11052. const arg = node.argument;
  11053. if (arg.type === "Identifier") {
  11054. this.raise(Errors.StrictDelete, node);
  11055. } else if (this.hasPropertyAsPrivateName(arg)) {
  11056. this.raise(Errors.DeletePrivateField, node);
  11057. }
  11058. }
  11059. if (!update) {
  11060. if (!sawUnary) {
  11061. this.checkExponentialAfterUnary(node);
  11062. }
  11063. return this.finishNode(node, "UnaryExpression");
  11064. }
  11065. }
  11066. const expr = this.parseUpdate(node, update, refExpressionErrors);
  11067. if (isAwait) {
  11068. const {
  11069. type
  11070. } = this.state;
  11071. const startsExpr = this.hasPlugin("v8intrinsic") ? tokenCanStartExpression(type) : tokenCanStartExpression(type) && !this.match(54);
  11072. if (startsExpr && !this.isAmbiguousPrefixOrIdentifier()) {
  11073. this.raiseOverwrite(Errors.AwaitNotInAsyncContext, startLoc);
  11074. return this.parseAwait(startLoc);
  11075. }
  11076. }
  11077. return expr;
  11078. }
  11079. parseUpdate(node, update, refExpressionErrors) {
  11080. if (update) {
  11081. const updateExpressionNode = node;
  11082. this.checkLVal(updateExpressionNode.argument, this.finishNode(updateExpressionNode, "UpdateExpression"));
  11083. return node;
  11084. }
  11085. const startLoc = this.state.startLoc;
  11086. let expr = this.parseExprSubscripts(refExpressionErrors);
  11087. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  11088. while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) {
  11089. const node = this.startNodeAt(startLoc);
  11090. node.operator = this.state.value;
  11091. node.prefix = false;
  11092. node.argument = expr;
  11093. this.next();
  11094. this.checkLVal(expr, expr = this.finishNode(node, "UpdateExpression"));
  11095. }
  11096. return expr;
  11097. }
  11098. parseExprSubscripts(refExpressionErrors) {
  11099. const startLoc = this.state.startLoc;
  11100. const potentialArrowAt = this.state.potentialArrowAt;
  11101. const expr = this.parseExprAtom(refExpressionErrors);
  11102. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  11103. return expr;
  11104. }
  11105. return this.parseSubscripts(expr, startLoc);
  11106. }
  11107. parseSubscripts(base, startLoc, noCalls) {
  11108. const state = {
  11109. optionalChainMember: false,
  11110. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  11111. stop: false
  11112. };
  11113. do {
  11114. base = this.parseSubscript(base, startLoc, noCalls, state);
  11115. state.maybeAsyncArrow = false;
  11116. } while (!state.stop);
  11117. return base;
  11118. }
  11119. parseSubscript(base, startLoc, noCalls, state) {
  11120. const {
  11121. type
  11122. } = this.state;
  11123. if (!noCalls && type === 15) {
  11124. return this.parseBind(base, startLoc, state);
  11125. } else if (tokenIsTemplate(type)) {
  11126. return this.parseTaggedTemplateExpression(base, startLoc, state);
  11127. }
  11128. let optional = false;
  11129. if (type === 18) {
  11130. if (noCalls) {
  11131. this.raise(Errors.OptionalChainingNoNew, this.state.startLoc);
  11132. if (this.lookaheadCharCode() === 40) {
  11133. return this.stopParseSubscript(base, state);
  11134. }
  11135. }
  11136. state.optionalChainMember = optional = true;
  11137. this.next();
  11138. }
  11139. if (!noCalls && this.match(10)) {
  11140. return this.parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional);
  11141. } else {
  11142. const computed = this.eat(0);
  11143. if (computed || optional || this.eat(16)) {
  11144. return this.parseMember(base, startLoc, state, computed, optional);
  11145. } else {
  11146. return this.stopParseSubscript(base, state);
  11147. }
  11148. }
  11149. }
  11150. stopParseSubscript(base, state) {
  11151. state.stop = true;
  11152. return base;
  11153. }
  11154. parseMember(base, startLoc, state, computed, optional) {
  11155. const node = this.startNodeAt(startLoc);
  11156. node.object = base;
  11157. node.computed = computed;
  11158. if (computed) {
  11159. node.property = this.parseExpression();
  11160. this.expect(3);
  11161. } else if (this.match(139)) {
  11162. if (base.type === "Super") {
  11163. this.raise(Errors.SuperPrivateField, startLoc);
  11164. }
  11165. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  11166. node.property = this.parsePrivateName();
  11167. } else {
  11168. node.property = this.parseIdentifier(true);
  11169. }
  11170. if (state.optionalChainMember) {
  11171. node.optional = optional;
  11172. return this.finishNode(node, "OptionalMemberExpression");
  11173. } else {
  11174. return this.finishNode(node, "MemberExpression");
  11175. }
  11176. }
  11177. parseBind(base, startLoc, state) {
  11178. const node = this.startNodeAt(startLoc);
  11179. node.object = base;
  11180. this.next();
  11181. const isImport = this.match(83);
  11182. const callee = this.parseNoCallExpr();
  11183. if (callee.type === "Super" || isImport && callee.type === "ImportExpression" || callee.type === "Import") {
  11184. throw this.raise(Errors.UnsupportedBindRHS, callee);
  11185. }
  11186. node.callee = callee;
  11187. state.stop = true;
  11188. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startLoc, false);
  11189. }
  11190. parseCoverCallAndAsyncArrowHead(base, startLoc, state, optional) {
  11191. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11192. let refExpressionErrors = null;
  11193. this.state.maybeInArrowParameters = true;
  11194. this.next();
  11195. const node = this.startNodeAt(startLoc);
  11196. node.callee = base;
  11197. const {
  11198. maybeAsyncArrow,
  11199. optionalChainMember
  11200. } = state;
  11201. if (maybeAsyncArrow) {
  11202. this.expressionScope.enter(newAsyncArrowScope());
  11203. refExpressionErrors = new ExpressionErrors();
  11204. }
  11205. if (optionalChainMember) {
  11206. node.optional = optional;
  11207. }
  11208. if (optional) {
  11209. node.arguments = this.parseCallExpressionArguments();
  11210. } else {
  11211. node.arguments = this.parseCallExpressionArguments(base.type !== "Super", node, refExpressionErrors);
  11212. }
  11213. let finishedNode = this.finishCallExpression(node, optionalChainMember);
  11214. if (maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  11215. state.stop = true;
  11216. this.checkDestructuringPrivate(refExpressionErrors);
  11217. this.expressionScope.validateAsPattern();
  11218. this.expressionScope.exit();
  11219. finishedNode = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startLoc), finishedNode);
  11220. } else {
  11221. if (maybeAsyncArrow) {
  11222. this.checkExpressionErrors(refExpressionErrors, true);
  11223. this.expressionScope.exit();
  11224. }
  11225. this.toReferencedArguments(finishedNode);
  11226. }
  11227. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11228. return finishedNode;
  11229. }
  11230. toReferencedArguments(node, isParenthesizedExpr) {
  11231. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  11232. }
  11233. parseTaggedTemplateExpression(base, startLoc, state) {
  11234. const node = this.startNodeAt(startLoc);
  11235. node.tag = base;
  11236. node.quasi = this.parseTemplate(true);
  11237. if (state.optionalChainMember) {
  11238. this.raise(Errors.OptionalChainingNoTemplate, startLoc);
  11239. }
  11240. return this.finishNode(node, "TaggedTemplateExpression");
  11241. }
  11242. atPossibleAsyncArrow(base) {
  11243. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEndLoc.index === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && this.offsetToSourcePos(base.start) === this.state.potentialArrowAt;
  11244. }
  11245. finishCallExpression(node, optional) {
  11246. if (node.callee.type === "Import") {
  11247. if (node.arguments.length === 0 || node.arguments.length > 2) {
  11248. this.raise(Errors.ImportCallArity, node);
  11249. } else {
  11250. for (const arg of node.arguments) {
  11251. if (arg.type === "SpreadElement") {
  11252. this.raise(Errors.ImportCallSpreadArgument, arg);
  11253. }
  11254. }
  11255. }
  11256. }
  11257. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  11258. }
  11259. parseCallExpressionArguments(allowPlaceholder, nodeForExtra, refExpressionErrors) {
  11260. const elts = [];
  11261. let first = true;
  11262. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11263. this.state.inFSharpPipelineDirectBody = false;
  11264. while (!this.eat(11)) {
  11265. if (first) {
  11266. first = false;
  11267. } else {
  11268. this.expect(12);
  11269. if (this.match(11)) {
  11270. if (nodeForExtra) {
  11271. this.addTrailingCommaExtraToNode(nodeForExtra);
  11272. }
  11273. this.next();
  11274. break;
  11275. }
  11276. }
  11277. elts.push(this.parseExprListItem(11, false, refExpressionErrors, allowPlaceholder));
  11278. }
  11279. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11280. return elts;
  11281. }
  11282. shouldParseAsyncArrow() {
  11283. return this.match(19) && !this.canInsertSemicolon();
  11284. }
  11285. parseAsyncArrowFromCallExpression(node, call) {
  11286. var _call$extra;
  11287. this.resetPreviousNodeTrailingComments(call);
  11288. this.expect(19);
  11289. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingCommaLoc);
  11290. if (call.innerComments) {
  11291. setInnerComments(node, call.innerComments);
  11292. }
  11293. if (call.callee.trailingComments) {
  11294. setInnerComments(node, call.callee.trailingComments);
  11295. }
  11296. return node;
  11297. }
  11298. parseNoCallExpr() {
  11299. const startLoc = this.state.startLoc;
  11300. return this.parseSubscripts(this.parseExprAtom(), startLoc, true);
  11301. }
  11302. parseExprAtom(refExpressionErrors) {
  11303. let node;
  11304. let decorators = null;
  11305. const {
  11306. type
  11307. } = this.state;
  11308. switch (type) {
  11309. case 79:
  11310. return this.parseSuper();
  11311. case 83:
  11312. node = this.startNode();
  11313. this.next();
  11314. if (this.match(16)) {
  11315. return this.parseImportMetaPropertyOrPhaseCall(node);
  11316. }
  11317. if (this.match(10)) {
  11318. if (this.optionFlags & 512) {
  11319. return this.parseImportCall(node);
  11320. } else {
  11321. return this.finishNode(node, "Import");
  11322. }
  11323. } else {
  11324. this.raise(Errors.UnsupportedImport, this.state.lastTokStartLoc);
  11325. return this.finishNode(node, "Import");
  11326. }
  11327. case 78:
  11328. node = this.startNode();
  11329. this.next();
  11330. return this.finishNode(node, "ThisExpression");
  11331. case 90:
  11332. {
  11333. return this.parseDo(this.startNode(), false);
  11334. }
  11335. case 56:
  11336. case 31:
  11337. {
  11338. this.readRegexp();
  11339. return this.parseRegExpLiteral(this.state.value);
  11340. }
  11341. case 135:
  11342. return this.parseNumericLiteral(this.state.value);
  11343. case 136:
  11344. return this.parseBigIntLiteral(this.state.value);
  11345. case 134:
  11346. return this.parseStringLiteral(this.state.value);
  11347. case 84:
  11348. return this.parseNullLiteral();
  11349. case 85:
  11350. return this.parseBooleanLiteral(true);
  11351. case 86:
  11352. return this.parseBooleanLiteral(false);
  11353. case 10:
  11354. {
  11355. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11356. return this.parseParenAndDistinguishExpression(canBeArrow);
  11357. }
  11358. case 0:
  11359. {
  11360. return this.parseArrayLike(3, false, refExpressionErrors);
  11361. }
  11362. case 5:
  11363. {
  11364. return this.parseObjectLike(8, false, false, refExpressionErrors);
  11365. }
  11366. case 68:
  11367. return this.parseFunctionOrFunctionSent();
  11368. case 26:
  11369. decorators = this.parseDecorators();
  11370. case 80:
  11371. return this.parseClass(this.maybeTakeDecorators(decorators, this.startNode()), false);
  11372. case 77:
  11373. return this.parseNewOrNewTarget();
  11374. case 25:
  11375. case 24:
  11376. return this.parseTemplate(false);
  11377. case 15:
  11378. {
  11379. node = this.startNode();
  11380. this.next();
  11381. node.object = null;
  11382. const callee = node.callee = this.parseNoCallExpr();
  11383. if (callee.type === "MemberExpression") {
  11384. return this.finishNode(node, "BindExpression");
  11385. } else {
  11386. throw this.raise(Errors.UnsupportedBind, callee);
  11387. }
  11388. }
  11389. case 139:
  11390. {
  11391. this.raise(Errors.PrivateInExpectedIn, this.state.startLoc, {
  11392. identifierName: this.state.value
  11393. });
  11394. return this.parsePrivateName();
  11395. }
  11396. case 33:
  11397. {
  11398. return this.parseTopicReferenceThenEqualsSign(54, "%");
  11399. }
  11400. case 32:
  11401. {
  11402. return this.parseTopicReferenceThenEqualsSign(44, "^");
  11403. }
  11404. case 37:
  11405. case 38:
  11406. {
  11407. return this.parseTopicReference("hack");
  11408. }
  11409. case 44:
  11410. case 54:
  11411. case 27:
  11412. {
  11413. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11414. if (pipeProposal) {
  11415. return this.parseTopicReference(pipeProposal);
  11416. }
  11417. throw this.unexpected();
  11418. }
  11419. case 47:
  11420. {
  11421. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  11422. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  11423. throw this.expectOnePlugin(["jsx", "flow", "typescript"]);
  11424. }
  11425. throw this.unexpected();
  11426. }
  11427. default:
  11428. if (type === 137) {
  11429. return this.parseDecimalLiteral(this.state.value);
  11430. } else if (type === 2 || type === 1) {
  11431. return this.parseArrayLike(this.state.type === 2 ? 4 : 3, true);
  11432. } else if (type === 6 || type === 7) {
  11433. return this.parseObjectLike(this.state.type === 6 ? 9 : 8, false, true);
  11434. }
  11435. if (tokenIsIdentifier(type)) {
  11436. if (this.isContextual(127) && this.lookaheadInLineCharCode() === 123) {
  11437. return this.parseModuleExpression();
  11438. }
  11439. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  11440. const containsEsc = this.state.containsEsc;
  11441. const id = this.parseIdentifier();
  11442. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  11443. const {
  11444. type
  11445. } = this.state;
  11446. if (type === 68) {
  11447. this.resetPreviousNodeTrailingComments(id);
  11448. this.next();
  11449. return this.parseAsyncFunctionExpression(this.startNodeAtNode(id));
  11450. } else if (tokenIsIdentifier(type)) {
  11451. if (canBeArrow && this.lookaheadCharCode() === 61) {
  11452. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  11453. } else {
  11454. return id;
  11455. }
  11456. } else if (type === 90) {
  11457. this.resetPreviousNodeTrailingComments(id);
  11458. return this.parseDo(this.startNodeAtNode(id), true);
  11459. }
  11460. }
  11461. if (canBeArrow && this.match(19) && !this.canInsertSemicolon()) {
  11462. this.next();
  11463. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  11464. }
  11465. return id;
  11466. } else {
  11467. throw this.unexpected();
  11468. }
  11469. }
  11470. }
  11471. parseTopicReferenceThenEqualsSign(topicTokenType, topicTokenValue) {
  11472. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  11473. if (pipeProposal) {
  11474. this.state.type = topicTokenType;
  11475. this.state.value = topicTokenValue;
  11476. this.state.pos--;
  11477. this.state.end--;
  11478. this.state.endLoc = createPositionWithColumnOffset(this.state.endLoc, -1);
  11479. return this.parseTopicReference(pipeProposal);
  11480. }
  11481. throw this.unexpected();
  11482. }
  11483. parseTopicReference(pipeProposal) {
  11484. const node = this.startNode();
  11485. const startLoc = this.state.startLoc;
  11486. const tokenType = this.state.type;
  11487. this.next();
  11488. return this.finishTopicReference(node, startLoc, pipeProposal, tokenType);
  11489. }
  11490. finishTopicReference(node, startLoc, pipeProposal, tokenType) {
  11491. if (this.testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType)) {
  11492. if (pipeProposal === "hack") {
  11493. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11494. this.raise(Errors.PipeTopicUnbound, startLoc);
  11495. }
  11496. this.registerTopicReference();
  11497. return this.finishNode(node, "TopicReference");
  11498. } else {
  11499. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  11500. this.raise(Errors.PrimaryTopicNotAllowed, startLoc);
  11501. }
  11502. this.registerTopicReference();
  11503. return this.finishNode(node, "PipelinePrimaryTopicReference");
  11504. }
  11505. } else {
  11506. throw this.raise(Errors.PipeTopicUnconfiguredToken, startLoc, {
  11507. token: tokenLabelName(tokenType)
  11508. });
  11509. }
  11510. }
  11511. testTopicReferenceConfiguration(pipeProposal, startLoc, tokenType) {
  11512. switch (pipeProposal) {
  11513. case "hack":
  11514. {
  11515. return this.hasPlugin(["pipelineOperator", {
  11516. topicToken: tokenLabelName(tokenType)
  11517. }]);
  11518. }
  11519. case "smart":
  11520. return tokenType === 27;
  11521. default:
  11522. throw this.raise(Errors.PipeTopicRequiresHackPipes, startLoc);
  11523. }
  11524. }
  11525. parseAsyncArrowUnaryFunction(node) {
  11526. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  11527. const params = [this.parseIdentifier()];
  11528. this.prodParam.exit();
  11529. if (this.hasPrecedingLineBreak()) {
  11530. this.raise(Errors.LineTerminatorBeforeArrow, this.state.curPosition());
  11531. }
  11532. this.expect(19);
  11533. return this.parseArrowExpression(node, params, true);
  11534. }
  11535. parseDo(node, isAsync) {
  11536. this.expectPlugin("doExpressions");
  11537. if (isAsync) {
  11538. this.expectPlugin("asyncDoExpressions");
  11539. }
  11540. node.async = isAsync;
  11541. this.next();
  11542. const oldLabels = this.state.labels;
  11543. this.state.labels = [];
  11544. if (isAsync) {
  11545. this.prodParam.enter(2);
  11546. node.body = this.parseBlock();
  11547. this.prodParam.exit();
  11548. } else {
  11549. node.body = this.parseBlock();
  11550. }
  11551. this.state.labels = oldLabels;
  11552. return this.finishNode(node, "DoExpression");
  11553. }
  11554. parseSuper() {
  11555. const node = this.startNode();
  11556. this.next();
  11557. if (this.match(10) && !this.scope.allowDirectSuper) {
  11558. if (!(this.optionFlags & 16)) {
  11559. this.raise(Errors.SuperNotAllowed, node);
  11560. }
  11561. } else if (!this.scope.allowSuper) {
  11562. if (!(this.optionFlags & 16)) {
  11563. this.raise(Errors.UnexpectedSuper, node);
  11564. }
  11565. }
  11566. if (!this.match(10) && !this.match(0) && !this.match(16)) {
  11567. this.raise(Errors.UnsupportedSuper, node);
  11568. }
  11569. return this.finishNode(node, "Super");
  11570. }
  11571. parsePrivateName() {
  11572. const node = this.startNode();
  11573. const id = this.startNodeAt(createPositionWithColumnOffset(this.state.startLoc, 1));
  11574. const name = this.state.value;
  11575. this.next();
  11576. node.id = this.createIdentifier(id, name);
  11577. return this.finishNode(node, "PrivateName");
  11578. }
  11579. parseFunctionOrFunctionSent() {
  11580. const node = this.startNode();
  11581. this.next();
  11582. if (this.prodParam.hasYield && this.match(16)) {
  11583. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  11584. this.next();
  11585. if (this.match(103)) {
  11586. this.expectPlugin("functionSent");
  11587. } else if (!this.hasPlugin("functionSent")) {
  11588. this.unexpected();
  11589. }
  11590. return this.parseMetaProperty(node, meta, "sent");
  11591. }
  11592. return this.parseFunction(node);
  11593. }
  11594. parseMetaProperty(node, meta, propertyName) {
  11595. node.meta = meta;
  11596. const containsEsc = this.state.containsEsc;
  11597. node.property = this.parseIdentifier(true);
  11598. if (node.property.name !== propertyName || containsEsc) {
  11599. this.raise(Errors.UnsupportedMetaProperty, node.property, {
  11600. target: meta.name,
  11601. onlyValidPropertyName: propertyName
  11602. });
  11603. }
  11604. return this.finishNode(node, "MetaProperty");
  11605. }
  11606. parseImportMetaPropertyOrPhaseCall(node) {
  11607. this.next();
  11608. if (this.isContextual(105) || this.isContextual(97)) {
  11609. const isSource = this.isContextual(105);
  11610. this.expectPlugin(isSource ? "sourcePhaseImports" : "deferredImportEvaluation");
  11611. this.next();
  11612. node.phase = isSource ? "source" : "defer";
  11613. return this.parseImportCall(node);
  11614. } else {
  11615. const id = this.createIdentifierAt(this.startNodeAtNode(node), "import", this.state.lastTokStartLoc);
  11616. if (this.isContextual(101)) {
  11617. if (!this.inModule) {
  11618. this.raise(Errors.ImportMetaOutsideModule, id);
  11619. }
  11620. this.sawUnambiguousESM = true;
  11621. }
  11622. return this.parseMetaProperty(node, id, "meta");
  11623. }
  11624. }
  11625. parseLiteralAtNode(value, type, node) {
  11626. this.addExtra(node, "rawValue", value);
  11627. this.addExtra(node, "raw", this.input.slice(this.offsetToSourcePos(node.start), this.state.end));
  11628. node.value = value;
  11629. this.next();
  11630. return this.finishNode(node, type);
  11631. }
  11632. parseLiteral(value, type) {
  11633. const node = this.startNode();
  11634. return this.parseLiteralAtNode(value, type, node);
  11635. }
  11636. parseStringLiteral(value) {
  11637. return this.parseLiteral(value, "StringLiteral");
  11638. }
  11639. parseNumericLiteral(value) {
  11640. return this.parseLiteral(value, "NumericLiteral");
  11641. }
  11642. parseBigIntLiteral(value) {
  11643. return this.parseLiteral(value, "BigIntLiteral");
  11644. }
  11645. parseDecimalLiteral(value) {
  11646. return this.parseLiteral(value, "DecimalLiteral");
  11647. }
  11648. parseRegExpLiteral(value) {
  11649. const node = this.startNode();
  11650. this.addExtra(node, "raw", this.input.slice(this.offsetToSourcePos(node.start), this.state.end));
  11651. node.pattern = value.pattern;
  11652. node.flags = value.flags;
  11653. this.next();
  11654. return this.finishNode(node, "RegExpLiteral");
  11655. }
  11656. parseBooleanLiteral(value) {
  11657. const node = this.startNode();
  11658. node.value = value;
  11659. this.next();
  11660. return this.finishNode(node, "BooleanLiteral");
  11661. }
  11662. parseNullLiteral() {
  11663. const node = this.startNode();
  11664. this.next();
  11665. return this.finishNode(node, "NullLiteral");
  11666. }
  11667. parseParenAndDistinguishExpression(canBeArrow) {
  11668. const startLoc = this.state.startLoc;
  11669. let val;
  11670. this.next();
  11671. this.expressionScope.enter(newArrowHeadScope());
  11672. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11673. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11674. this.state.maybeInArrowParameters = true;
  11675. this.state.inFSharpPipelineDirectBody = false;
  11676. const innerStartLoc = this.state.startLoc;
  11677. const exprList = [];
  11678. const refExpressionErrors = new ExpressionErrors();
  11679. let first = true;
  11680. let spreadStartLoc;
  11681. let optionalCommaStartLoc;
  11682. while (!this.match(11)) {
  11683. if (first) {
  11684. first = false;
  11685. } else {
  11686. this.expect(12, refExpressionErrors.optionalParametersLoc === null ? null : refExpressionErrors.optionalParametersLoc);
  11687. if (this.match(11)) {
  11688. optionalCommaStartLoc = this.state.startLoc;
  11689. break;
  11690. }
  11691. }
  11692. if (this.match(21)) {
  11693. const spreadNodeStartLoc = this.state.startLoc;
  11694. spreadStartLoc = this.state.startLoc;
  11695. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc));
  11696. if (!this.checkCommaAfterRest(41)) {
  11697. break;
  11698. }
  11699. } else {
  11700. exprList.push(this.parseMaybeAssignAllowInOrVoidPattern(11, refExpressionErrors, this.parseParenItem));
  11701. }
  11702. }
  11703. const innerEndLoc = this.state.lastTokEndLoc;
  11704. this.expect(11);
  11705. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11706. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11707. let arrowNode = this.startNodeAt(startLoc);
  11708. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  11709. this.checkDestructuringPrivate(refExpressionErrors);
  11710. this.expressionScope.validateAsPattern();
  11711. this.expressionScope.exit();
  11712. this.parseArrowExpression(arrowNode, exprList, false);
  11713. return arrowNode;
  11714. }
  11715. this.expressionScope.exit();
  11716. if (!exprList.length) {
  11717. this.unexpected(this.state.lastTokStartLoc);
  11718. }
  11719. if (optionalCommaStartLoc) this.unexpected(optionalCommaStartLoc);
  11720. if (spreadStartLoc) this.unexpected(spreadStartLoc);
  11721. this.checkExpressionErrors(refExpressionErrors, true);
  11722. this.toReferencedListDeep(exprList, true);
  11723. if (exprList.length > 1) {
  11724. val = this.startNodeAt(innerStartLoc);
  11725. val.expressions = exprList;
  11726. this.finishNode(val, "SequenceExpression");
  11727. this.resetEndLocation(val, innerEndLoc);
  11728. } else {
  11729. val = exprList[0];
  11730. }
  11731. return this.wrapParenthesis(startLoc, val);
  11732. }
  11733. wrapParenthesis(startLoc, expression) {
  11734. if (!(this.optionFlags & 1024)) {
  11735. this.addExtra(expression, "parenthesized", true);
  11736. this.addExtra(expression, "parenStart", startLoc.index);
  11737. this.takeSurroundingComments(expression, startLoc.index, this.state.lastTokEndLoc.index);
  11738. return expression;
  11739. }
  11740. const parenExpression = this.startNodeAt(startLoc);
  11741. parenExpression.expression = expression;
  11742. return this.finishNode(parenExpression, "ParenthesizedExpression");
  11743. }
  11744. shouldParseArrow(params) {
  11745. return !this.canInsertSemicolon();
  11746. }
  11747. parseArrow(node) {
  11748. if (this.eat(19)) {
  11749. return node;
  11750. }
  11751. }
  11752. parseParenItem(node, startLoc) {
  11753. return node;
  11754. }
  11755. parseNewOrNewTarget() {
  11756. const node = this.startNode();
  11757. this.next();
  11758. if (this.match(16)) {
  11759. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  11760. this.next();
  11761. const metaProp = this.parseMetaProperty(node, meta, "target");
  11762. if (!this.scope.allowNewTarget) {
  11763. this.raise(Errors.UnexpectedNewTarget, metaProp);
  11764. }
  11765. return metaProp;
  11766. }
  11767. return this.parseNew(node);
  11768. }
  11769. parseNew(node) {
  11770. this.parseNewCallee(node);
  11771. if (this.eat(10)) {
  11772. const args = this.parseExprList(11);
  11773. this.toReferencedList(args);
  11774. node.arguments = args;
  11775. } else {
  11776. node.arguments = [];
  11777. }
  11778. return this.finishNode(node, "NewExpression");
  11779. }
  11780. parseNewCallee(node) {
  11781. const isImport = this.match(83);
  11782. const callee = this.parseNoCallExpr();
  11783. node.callee = callee;
  11784. if (isImport && callee.type === "ImportExpression" || callee.type === "Import") {
  11785. this.raise(Errors.ImportCallNotNewExpression, callee);
  11786. }
  11787. if (callee.type === "Super") {
  11788. this.raise(Errors.SuperCallNotNewExpression, callee);
  11789. }
  11790. }
  11791. parseTemplateElement(isTagged) {
  11792. const {
  11793. start,
  11794. startLoc,
  11795. end,
  11796. value
  11797. } = this.state;
  11798. const elemStart = start + 1;
  11799. const elem = this.startNodeAt(createPositionWithColumnOffset(startLoc, 1));
  11800. if (value === null) {
  11801. if (!isTagged) {
  11802. this.raise(Errors.InvalidEscapeSequenceTemplate, createPositionWithColumnOffset(this.state.firstInvalidTemplateEscapePos, 1));
  11803. }
  11804. }
  11805. const isTail = this.match(24);
  11806. const endOffset = isTail ? -1 : -2;
  11807. const elemEnd = end + endOffset;
  11808. elem.value = {
  11809. raw: this.input.slice(elemStart, elemEnd).replace(/\r\n?/g, "\n"),
  11810. cooked: value === null ? null : value.slice(1, endOffset)
  11811. };
  11812. elem.tail = isTail;
  11813. this.next();
  11814. const finishedNode = this.finishNode(elem, "TemplateElement");
  11815. this.resetEndLocation(finishedNode, createPositionWithColumnOffset(this.state.lastTokEndLoc, endOffset));
  11816. return finishedNode;
  11817. }
  11818. parseTemplate(isTagged) {
  11819. const node = this.startNode();
  11820. let curElt = this.parseTemplateElement(isTagged);
  11821. const quasis = [curElt];
  11822. const substitutions = [];
  11823. while (!curElt.tail) {
  11824. substitutions.push(this.parseTemplateSubstitution());
  11825. this.readTemplateContinuation();
  11826. quasis.push(curElt = this.parseTemplateElement(isTagged));
  11827. }
  11828. node.expressions = substitutions;
  11829. node.quasis = quasis;
  11830. return this.finishNode(node, "TemplateLiteral");
  11831. }
  11832. parseTemplateSubstitution() {
  11833. return this.parseExpression();
  11834. }
  11835. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  11836. if (isRecord) {
  11837. this.expectPlugin("recordAndTuple");
  11838. }
  11839. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  11840. this.state.inFSharpPipelineDirectBody = false;
  11841. let sawProto = false;
  11842. let first = true;
  11843. const node = this.startNode();
  11844. node.properties = [];
  11845. this.next();
  11846. while (!this.match(close)) {
  11847. if (first) {
  11848. first = false;
  11849. } else {
  11850. this.expect(12);
  11851. if (this.match(close)) {
  11852. this.addTrailingCommaExtraToNode(node);
  11853. break;
  11854. }
  11855. }
  11856. let prop;
  11857. if (isPattern) {
  11858. prop = this.parseBindingProperty();
  11859. } else {
  11860. prop = this.parsePropertyDefinition(refExpressionErrors);
  11861. sawProto = this.checkProto(prop, isRecord, sawProto, refExpressionErrors);
  11862. }
  11863. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  11864. this.raise(Errors.InvalidRecordProperty, prop);
  11865. }
  11866. if (prop.shorthand) {
  11867. this.addExtra(prop, "shorthand", true);
  11868. }
  11869. node.properties.push(prop);
  11870. }
  11871. this.next();
  11872. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  11873. let type = "ObjectExpression";
  11874. if (isPattern) {
  11875. type = "ObjectPattern";
  11876. } else if (isRecord) {
  11877. type = "RecordExpression";
  11878. }
  11879. return this.finishNode(node, type);
  11880. }
  11881. addTrailingCommaExtraToNode(node) {
  11882. this.addExtra(node, "trailingComma", this.state.lastTokStartLoc.index);
  11883. this.addExtra(node, "trailingCommaLoc", this.state.lastTokStartLoc, false);
  11884. }
  11885. maybeAsyncOrAccessorProp(prop) {
  11886. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(0) || this.match(55));
  11887. }
  11888. parsePropertyDefinition(refExpressionErrors) {
  11889. let decorators = [];
  11890. if (this.match(26)) {
  11891. if (this.hasPlugin("decorators")) {
  11892. this.raise(Errors.UnsupportedPropertyDecorator, this.state.startLoc);
  11893. }
  11894. while (this.match(26)) {
  11895. decorators.push(this.parseDecorator());
  11896. }
  11897. }
  11898. const prop = this.startNode();
  11899. let isAsync = false;
  11900. let isAccessor = false;
  11901. let startLoc;
  11902. if (this.match(21)) {
  11903. if (decorators.length) this.unexpected();
  11904. return this.parseSpread();
  11905. }
  11906. if (decorators.length) {
  11907. prop.decorators = decorators;
  11908. decorators = [];
  11909. }
  11910. prop.method = false;
  11911. if (refExpressionErrors) {
  11912. startLoc = this.state.startLoc;
  11913. }
  11914. let isGenerator = this.eat(55);
  11915. this.parsePropertyNamePrefixOperator(prop);
  11916. const containsEsc = this.state.containsEsc;
  11917. this.parsePropertyName(prop, refExpressionErrors);
  11918. if (!isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  11919. const {
  11920. key
  11921. } = prop;
  11922. const keyName = key.name;
  11923. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  11924. isAsync = true;
  11925. this.resetPreviousNodeTrailingComments(key);
  11926. isGenerator = this.eat(55);
  11927. this.parsePropertyName(prop);
  11928. }
  11929. if (keyName === "get" || keyName === "set") {
  11930. isAccessor = true;
  11931. this.resetPreviousNodeTrailingComments(key);
  11932. prop.kind = keyName;
  11933. if (this.match(55)) {
  11934. isGenerator = true;
  11935. this.raise(Errors.AccessorIsGenerator, this.state.curPosition(), {
  11936. kind: keyName
  11937. });
  11938. this.next();
  11939. }
  11940. this.parsePropertyName(prop);
  11941. }
  11942. }
  11943. return this.parseObjPropValue(prop, startLoc, isGenerator, isAsync, false, isAccessor, refExpressionErrors);
  11944. }
  11945. getGetterSetterExpectedParamCount(method) {
  11946. return method.kind === "get" ? 0 : 1;
  11947. }
  11948. getObjectOrClassMethodParams(method) {
  11949. return method.params;
  11950. }
  11951. checkGetterSetterParams(method) {
  11952. var _params;
  11953. const paramCount = this.getGetterSetterExpectedParamCount(method);
  11954. const params = this.getObjectOrClassMethodParams(method);
  11955. if (params.length !== paramCount) {
  11956. this.raise(method.kind === "get" ? Errors.BadGetterArity : Errors.BadSetterArity, method);
  11957. }
  11958. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  11959. this.raise(Errors.BadSetterRestParameter, method);
  11960. }
  11961. }
  11962. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  11963. if (isAccessor) {
  11964. const finishedProp = this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  11965. this.checkGetterSetterParams(finishedProp);
  11966. return finishedProp;
  11967. }
  11968. if (isAsync || isGenerator || this.match(10)) {
  11969. if (isPattern) this.unexpected();
  11970. prop.kind = "method";
  11971. prop.method = true;
  11972. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  11973. }
  11974. }
  11975. parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors) {
  11976. prop.shorthand = false;
  11977. if (this.eat(14)) {
  11978. prop.value = isPattern ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowInOrVoidPattern(8, refExpressionErrors);
  11979. return this.finishObjectProperty(prop);
  11980. }
  11981. if (!prop.computed && prop.key.type === "Identifier") {
  11982. this.checkReservedWord(prop.key.name, prop.key.loc.start, true, false);
  11983. if (isPattern) {
  11984. prop.value = this.parseMaybeDefault(startLoc, this.cloneIdentifier(prop.key));
  11985. } else if (this.match(29)) {
  11986. const shorthandAssignLoc = this.state.startLoc;
  11987. if (refExpressionErrors != null) {
  11988. if (refExpressionErrors.shorthandAssignLoc === null) {
  11989. refExpressionErrors.shorthandAssignLoc = shorthandAssignLoc;
  11990. }
  11991. } else {
  11992. this.raise(Errors.InvalidCoverInitializedName, shorthandAssignLoc);
  11993. }
  11994. prop.value = this.parseMaybeDefault(startLoc, this.cloneIdentifier(prop.key));
  11995. } else {
  11996. prop.value = this.cloneIdentifier(prop.key);
  11997. }
  11998. prop.shorthand = true;
  11999. return this.finishObjectProperty(prop);
  12000. }
  12001. }
  12002. finishObjectProperty(node) {
  12003. return this.finishNode(node, "ObjectProperty");
  12004. }
  12005. parseObjPropValue(prop, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  12006. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startLoc, isPattern, refExpressionErrors);
  12007. if (!node) this.unexpected();
  12008. return node;
  12009. }
  12010. parsePropertyName(prop, refExpressionErrors) {
  12011. if (this.eat(0)) {
  12012. prop.computed = true;
  12013. prop.key = this.parseMaybeAssignAllowIn();
  12014. this.expect(3);
  12015. } else {
  12016. const {
  12017. type,
  12018. value
  12019. } = this.state;
  12020. let key;
  12021. if (tokenIsKeywordOrIdentifier(type)) {
  12022. key = this.parseIdentifier(true);
  12023. } else {
  12024. switch (type) {
  12025. case 135:
  12026. key = this.parseNumericLiteral(value);
  12027. break;
  12028. case 134:
  12029. key = this.parseStringLiteral(value);
  12030. break;
  12031. case 136:
  12032. key = this.parseBigIntLiteral(value);
  12033. break;
  12034. case 139:
  12035. {
  12036. const privateKeyLoc = this.state.startLoc;
  12037. if (refExpressionErrors != null) {
  12038. if (refExpressionErrors.privateKeyLoc === null) {
  12039. refExpressionErrors.privateKeyLoc = privateKeyLoc;
  12040. }
  12041. } else {
  12042. this.raise(Errors.UnexpectedPrivateField, privateKeyLoc);
  12043. }
  12044. key = this.parsePrivateName();
  12045. break;
  12046. }
  12047. default:
  12048. if (type === 137) {
  12049. key = this.parseDecimalLiteral(value);
  12050. break;
  12051. }
  12052. this.unexpected();
  12053. }
  12054. }
  12055. prop.key = key;
  12056. if (type !== 139) {
  12057. prop.computed = false;
  12058. }
  12059. }
  12060. }
  12061. initFunction(node, isAsync) {
  12062. node.id = null;
  12063. node.generator = false;
  12064. node.async = isAsync;
  12065. }
  12066. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  12067. this.initFunction(node, isAsync);
  12068. node.generator = isGenerator;
  12069. this.scope.enter(514 | 16 | (inClassScope ? 576 : 0) | (allowDirectSuper ? 32 : 0));
  12070. this.prodParam.enter(functionFlags(isAsync, node.generator));
  12071. this.parseFunctionParams(node, isConstructor);
  12072. const finishedNode = this.parseFunctionBodyAndFinish(node, type, true);
  12073. this.prodParam.exit();
  12074. this.scope.exit();
  12075. return finishedNode;
  12076. }
  12077. parseArrayLike(close, isTuple, refExpressionErrors) {
  12078. if (isTuple) {
  12079. this.expectPlugin("recordAndTuple");
  12080. }
  12081. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12082. this.state.inFSharpPipelineDirectBody = false;
  12083. const node = this.startNode();
  12084. this.next();
  12085. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  12086. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12087. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  12088. }
  12089. parseArrowExpression(node, params, isAsync, trailingCommaLoc) {
  12090. this.scope.enter(514 | 4);
  12091. let flags = functionFlags(isAsync, false);
  12092. if (!this.match(5) && this.prodParam.hasIn) {
  12093. flags |= 8;
  12094. }
  12095. this.prodParam.enter(flags);
  12096. this.initFunction(node, isAsync);
  12097. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  12098. if (params) {
  12099. this.state.maybeInArrowParameters = true;
  12100. this.setArrowFunctionParameters(node, params, trailingCommaLoc);
  12101. }
  12102. this.state.maybeInArrowParameters = false;
  12103. this.parseFunctionBody(node, true);
  12104. this.prodParam.exit();
  12105. this.scope.exit();
  12106. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  12107. return this.finishNode(node, "ArrowFunctionExpression");
  12108. }
  12109. setArrowFunctionParameters(node, params, trailingCommaLoc) {
  12110. this.toAssignableList(params, trailingCommaLoc, false);
  12111. node.params = params;
  12112. }
  12113. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  12114. this.parseFunctionBody(node, false, isMethod);
  12115. return this.finishNode(node, type);
  12116. }
  12117. parseFunctionBody(node, allowExpression, isMethod = false) {
  12118. const isExpression = allowExpression && !this.match(5);
  12119. this.expressionScope.enter(newExpressionScope());
  12120. if (isExpression) {
  12121. node.body = this.parseMaybeAssign();
  12122. this.checkParams(node, false, allowExpression, false);
  12123. } else {
  12124. const oldStrict = this.state.strict;
  12125. const oldLabels = this.state.labels;
  12126. this.state.labels = [];
  12127. this.prodParam.enter(this.prodParam.currentFlags() | 4);
  12128. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  12129. const nonSimple = !this.isSimpleParamList(node.params);
  12130. if (hasStrictModeDirective && nonSimple) {
  12131. this.raise(Errors.IllegalLanguageModeDirective, (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.loc.end : node);
  12132. }
  12133. const strictModeChanged = !oldStrict && this.state.strict;
  12134. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  12135. if (this.state.strict && node.id) {
  12136. this.checkIdentifier(node.id, 65, strictModeChanged);
  12137. }
  12138. });
  12139. this.prodParam.exit();
  12140. this.state.labels = oldLabels;
  12141. }
  12142. this.expressionScope.exit();
  12143. }
  12144. isSimpleParameter(node) {
  12145. return node.type === "Identifier";
  12146. }
  12147. isSimpleParamList(params) {
  12148. for (let i = 0, len = params.length; i < len; i++) {
  12149. if (!this.isSimpleParameter(params[i])) return false;
  12150. }
  12151. return true;
  12152. }
  12153. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  12154. const checkClashes = !allowDuplicates && new Set();
  12155. const formalParameters = {
  12156. type: "FormalParameters"
  12157. };
  12158. for (const param of node.params) {
  12159. this.checkLVal(param, formalParameters, 5, checkClashes, strictModeChanged);
  12160. }
  12161. }
  12162. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  12163. const elts = [];
  12164. let first = true;
  12165. while (!this.eat(close)) {
  12166. if (first) {
  12167. first = false;
  12168. } else {
  12169. this.expect(12);
  12170. if (this.match(close)) {
  12171. if (nodeForExtra) {
  12172. this.addTrailingCommaExtraToNode(nodeForExtra);
  12173. }
  12174. this.next();
  12175. break;
  12176. }
  12177. }
  12178. elts.push(this.parseExprListItem(close, allowEmpty, refExpressionErrors));
  12179. }
  12180. return elts;
  12181. }
  12182. parseExprListItem(close, allowEmpty, refExpressionErrors, allowPlaceholder) {
  12183. let elt;
  12184. if (this.match(12)) {
  12185. if (!allowEmpty) {
  12186. this.raise(Errors.UnexpectedToken, this.state.curPosition(), {
  12187. unexpected: ","
  12188. });
  12189. }
  12190. elt = null;
  12191. } else if (this.match(21)) {
  12192. const spreadNodeStartLoc = this.state.startLoc;
  12193. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartLoc);
  12194. } else if (this.match(17)) {
  12195. this.expectPlugin("partialApplication");
  12196. if (!allowPlaceholder) {
  12197. this.raise(Errors.UnexpectedArgumentPlaceholder, this.state.startLoc);
  12198. }
  12199. const node = this.startNode();
  12200. this.next();
  12201. elt = this.finishNode(node, "ArgumentPlaceholder");
  12202. } else {
  12203. elt = this.parseMaybeAssignAllowInOrVoidPattern(close, refExpressionErrors, this.parseParenItem);
  12204. }
  12205. return elt;
  12206. }
  12207. parseIdentifier(liberal) {
  12208. const node = this.startNode();
  12209. const name = this.parseIdentifierName(liberal);
  12210. return this.createIdentifier(node, name);
  12211. }
  12212. createIdentifier(node, name) {
  12213. node.name = name;
  12214. node.loc.identifierName = name;
  12215. return this.finishNode(node, "Identifier");
  12216. }
  12217. createIdentifierAt(node, name, endLoc) {
  12218. node.name = name;
  12219. node.loc.identifierName = name;
  12220. return this.finishNodeAt(node, "Identifier", endLoc);
  12221. }
  12222. parseIdentifierName(liberal) {
  12223. let name;
  12224. const {
  12225. startLoc,
  12226. type
  12227. } = this.state;
  12228. if (tokenIsKeywordOrIdentifier(type)) {
  12229. name = this.state.value;
  12230. } else {
  12231. this.unexpected();
  12232. }
  12233. const tokenIsKeyword = tokenKeywordOrIdentifierIsKeyword(type);
  12234. if (liberal) {
  12235. if (tokenIsKeyword) {
  12236. this.replaceToken(132);
  12237. }
  12238. } else {
  12239. this.checkReservedWord(name, startLoc, tokenIsKeyword, false);
  12240. }
  12241. this.next();
  12242. return name;
  12243. }
  12244. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  12245. if (word.length > 10) {
  12246. return;
  12247. }
  12248. if (!canBeReservedWord(word)) {
  12249. return;
  12250. }
  12251. if (checkKeywords && isKeyword(word)) {
  12252. this.raise(Errors.UnexpectedKeyword, startLoc, {
  12253. keyword: word
  12254. });
  12255. return;
  12256. }
  12257. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  12258. if (reservedTest(word, this.inModule)) {
  12259. this.raise(Errors.UnexpectedReservedWord, startLoc, {
  12260. reservedWord: word
  12261. });
  12262. return;
  12263. } else if (word === "yield") {
  12264. if (this.prodParam.hasYield) {
  12265. this.raise(Errors.YieldBindingIdentifier, startLoc);
  12266. return;
  12267. }
  12268. } else if (word === "await") {
  12269. if (this.prodParam.hasAwait) {
  12270. this.raise(Errors.AwaitBindingIdentifier, startLoc);
  12271. return;
  12272. }
  12273. if (this.scope.inStaticBlock) {
  12274. this.raise(Errors.AwaitBindingIdentifierInStaticBlock, startLoc);
  12275. return;
  12276. }
  12277. this.expressionScope.recordAsyncArrowParametersError(startLoc);
  12278. } else if (word === "arguments") {
  12279. if (this.scope.inClassAndNotInNonArrowFunction) {
  12280. this.raise(Errors.ArgumentsInClass, startLoc);
  12281. return;
  12282. }
  12283. }
  12284. }
  12285. recordAwaitIfAllowed() {
  12286. const isAwaitAllowed = this.prodParam.hasAwait;
  12287. if (isAwaitAllowed && !this.scope.inFunction) {
  12288. this.state.hasTopLevelAwait = true;
  12289. }
  12290. return isAwaitAllowed;
  12291. }
  12292. parseAwait(startLoc) {
  12293. const node = this.startNodeAt(startLoc);
  12294. this.expressionScope.recordParameterInitializerError(Errors.AwaitExpressionFormalParameter, node);
  12295. if (this.eat(55)) {
  12296. this.raise(Errors.ObsoleteAwaitStar, node);
  12297. }
  12298. if (!this.scope.inFunction && !(this.optionFlags & 1)) {
  12299. if (this.isAmbiguousPrefixOrIdentifier()) {
  12300. this.ambiguousScriptDifferentAst = true;
  12301. } else {
  12302. this.sawUnambiguousESM = true;
  12303. }
  12304. }
  12305. if (!this.state.soloAwait) {
  12306. node.argument = this.parseMaybeUnary(null, true);
  12307. }
  12308. return this.finishNode(node, "AwaitExpression");
  12309. }
  12310. isAmbiguousPrefixOrIdentifier() {
  12311. if (this.hasPrecedingLineBreak()) return true;
  12312. const {
  12313. type
  12314. } = this.state;
  12315. return type === 53 || type === 10 || type === 0 || tokenIsTemplate(type) || type === 102 && !this.state.containsEsc || type === 138 || type === 56 || this.hasPlugin("v8intrinsic") && type === 54;
  12316. }
  12317. parseYield(startLoc) {
  12318. const node = this.startNodeAt(startLoc);
  12319. this.expressionScope.recordParameterInitializerError(Errors.YieldInParameter, node);
  12320. let delegating = false;
  12321. let argument = null;
  12322. if (!this.hasPrecedingLineBreak()) {
  12323. delegating = this.eat(55);
  12324. switch (this.state.type) {
  12325. case 13:
  12326. case 140:
  12327. case 8:
  12328. case 11:
  12329. case 3:
  12330. case 9:
  12331. case 14:
  12332. case 12:
  12333. if (!delegating) break;
  12334. default:
  12335. argument = this.parseMaybeAssign();
  12336. }
  12337. }
  12338. node.delegate = delegating;
  12339. node.argument = argument;
  12340. return this.finishNode(node, "YieldExpression");
  12341. }
  12342. parseImportCall(node) {
  12343. this.next();
  12344. node.source = this.parseMaybeAssignAllowIn();
  12345. node.options = null;
  12346. if (this.eat(12)) {
  12347. if (!this.match(11)) {
  12348. node.options = this.parseMaybeAssignAllowIn();
  12349. if (this.eat(12)) {
  12350. this.addTrailingCommaExtraToNode(node.options);
  12351. if (!this.match(11)) {
  12352. do {
  12353. this.parseMaybeAssignAllowIn();
  12354. } while (this.eat(12) && !this.match(11));
  12355. this.raise(Errors.ImportCallArity, node);
  12356. }
  12357. }
  12358. } else {
  12359. this.addTrailingCommaExtraToNode(node.source);
  12360. }
  12361. }
  12362. this.expect(11);
  12363. return this.finishNode(node, "ImportExpression");
  12364. }
  12365. checkPipelineAtInfixOperator(left, leftStartLoc) {
  12366. if (this.hasPlugin(["pipelineOperator", {
  12367. proposal: "smart"
  12368. }])) {
  12369. if (left.type === "SequenceExpression") {
  12370. this.raise(Errors.PipelineHeadSequenceExpression, leftStartLoc);
  12371. }
  12372. }
  12373. }
  12374. parseSmartPipelineBodyInStyle(childExpr, startLoc) {
  12375. if (this.isSimpleReference(childExpr)) {
  12376. const bodyNode = this.startNodeAt(startLoc);
  12377. bodyNode.callee = childExpr;
  12378. return this.finishNode(bodyNode, "PipelineBareFunction");
  12379. } else {
  12380. const bodyNode = this.startNodeAt(startLoc);
  12381. this.checkSmartPipeTopicBodyEarlyErrors(startLoc);
  12382. bodyNode.expression = childExpr;
  12383. return this.finishNode(bodyNode, "PipelineTopicExpression");
  12384. }
  12385. }
  12386. isSimpleReference(expression) {
  12387. switch (expression.type) {
  12388. case "MemberExpression":
  12389. return !expression.computed && this.isSimpleReference(expression.object);
  12390. case "Identifier":
  12391. return true;
  12392. default:
  12393. return false;
  12394. }
  12395. }
  12396. checkSmartPipeTopicBodyEarlyErrors(startLoc) {
  12397. if (this.match(19)) {
  12398. throw this.raise(Errors.PipelineBodyNoArrow, this.state.startLoc);
  12399. }
  12400. if (!this.topicReferenceWasUsedInCurrentContext()) {
  12401. this.raise(Errors.PipelineTopicUnused, startLoc);
  12402. }
  12403. }
  12404. withTopicBindingContext(callback) {
  12405. const outerContextTopicState = this.state.topicContext;
  12406. this.state.topicContext = {
  12407. maxNumOfResolvableTopics: 1,
  12408. maxTopicIndex: null
  12409. };
  12410. try {
  12411. return callback();
  12412. } finally {
  12413. this.state.topicContext = outerContextTopicState;
  12414. }
  12415. }
  12416. withSmartMixTopicForbiddingContext(callback) {
  12417. if (this.hasPlugin(["pipelineOperator", {
  12418. proposal: "smart"
  12419. }])) {
  12420. const outerContextTopicState = this.state.topicContext;
  12421. this.state.topicContext = {
  12422. maxNumOfResolvableTopics: 0,
  12423. maxTopicIndex: null
  12424. };
  12425. try {
  12426. return callback();
  12427. } finally {
  12428. this.state.topicContext = outerContextTopicState;
  12429. }
  12430. } else {
  12431. return callback();
  12432. }
  12433. }
  12434. withSoloAwaitPermittingContext(callback) {
  12435. const outerContextSoloAwaitState = this.state.soloAwait;
  12436. this.state.soloAwait = true;
  12437. try {
  12438. return callback();
  12439. } finally {
  12440. this.state.soloAwait = outerContextSoloAwaitState;
  12441. }
  12442. }
  12443. allowInAnd(callback) {
  12444. const flags = this.prodParam.currentFlags();
  12445. const prodParamToSet = 8 & ~flags;
  12446. if (prodParamToSet) {
  12447. this.prodParam.enter(flags | 8);
  12448. try {
  12449. return callback();
  12450. } finally {
  12451. this.prodParam.exit();
  12452. }
  12453. }
  12454. return callback();
  12455. }
  12456. disallowInAnd(callback) {
  12457. const flags = this.prodParam.currentFlags();
  12458. const prodParamToClear = 8 & flags;
  12459. if (prodParamToClear) {
  12460. this.prodParam.enter(flags & ~8);
  12461. try {
  12462. return callback();
  12463. } finally {
  12464. this.prodParam.exit();
  12465. }
  12466. }
  12467. return callback();
  12468. }
  12469. registerTopicReference() {
  12470. this.state.topicContext.maxTopicIndex = 0;
  12471. }
  12472. topicReferenceIsAllowedInCurrentContext() {
  12473. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  12474. }
  12475. topicReferenceWasUsedInCurrentContext() {
  12476. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  12477. }
  12478. parseFSharpPipelineBody(prec) {
  12479. const startLoc = this.state.startLoc;
  12480. this.state.potentialArrowAt = this.state.start;
  12481. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  12482. this.state.inFSharpPipelineDirectBody = true;
  12483. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startLoc, prec);
  12484. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  12485. return ret;
  12486. }
  12487. parseModuleExpression() {
  12488. this.expectPlugin("moduleBlocks");
  12489. const node = this.startNode();
  12490. this.next();
  12491. if (!this.match(5)) {
  12492. this.unexpected(null, 5);
  12493. }
  12494. const program = this.startNodeAt(this.state.endLoc);
  12495. this.next();
  12496. const revertScopes = this.initializeScopes(true);
  12497. this.enterInitialScopes();
  12498. try {
  12499. node.body = this.parseProgram(program, 8, "module");
  12500. } finally {
  12501. revertScopes();
  12502. }
  12503. return this.finishNode(node, "ModuleExpression");
  12504. }
  12505. parseVoidPattern(refExpressionErrors) {
  12506. this.expectPlugin("discardBinding");
  12507. const node = this.startNode();
  12508. if (refExpressionErrors != null) {
  12509. refExpressionErrors.voidPatternLoc = this.state.startLoc;
  12510. }
  12511. this.next();
  12512. return this.finishNode(node, "VoidPattern");
  12513. }
  12514. parseMaybeAssignAllowInOrVoidPattern(close, refExpressionErrors, afterLeftParse) {
  12515. if (refExpressionErrors != null && this.match(88)) {
  12516. const nextCode = this.lookaheadCharCode();
  12517. if (nextCode === 44 || nextCode === (close === 3 ? 93 : close === 8 ? 125 : 41) || nextCode === 61) {
  12518. return this.parseMaybeDefault(this.state.startLoc, this.parseVoidPattern(refExpressionErrors));
  12519. }
  12520. }
  12521. return this.parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse);
  12522. }
  12523. parsePropertyNamePrefixOperator(prop) {}
  12524. }
  12525. const loopLabel = {
  12526. kind: 1
  12527. },
  12528. switchLabel = {
  12529. kind: 2
  12530. };
  12531. const loneSurrogate = /[\uD800-\uDFFF]/u;
  12532. const keywordRelationalOperator = /in(?:stanceof)?/y;
  12533. function babel7CompatTokens(tokens, input, startIndex) {
  12534. for (let i = 0; i < tokens.length; i++) {
  12535. const token = tokens[i];
  12536. const {
  12537. type
  12538. } = token;
  12539. if (typeof type === "number") {
  12540. if (type === 139) {
  12541. const {
  12542. loc,
  12543. start,
  12544. value,
  12545. end
  12546. } = token;
  12547. const hashEndPos = start + 1;
  12548. const hashEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12549. tokens.splice(i, 1, new Token({
  12550. type: getExportedToken(27),
  12551. value: "#",
  12552. start: start,
  12553. end: hashEndPos,
  12554. startLoc: loc.start,
  12555. endLoc: hashEndLoc
  12556. }), new Token({
  12557. type: getExportedToken(132),
  12558. value: value,
  12559. start: hashEndPos,
  12560. end: end,
  12561. startLoc: hashEndLoc,
  12562. endLoc: loc.end
  12563. }));
  12564. i++;
  12565. continue;
  12566. }
  12567. if (tokenIsTemplate(type)) {
  12568. const {
  12569. loc,
  12570. start,
  12571. value,
  12572. end
  12573. } = token;
  12574. const backquoteEnd = start + 1;
  12575. const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
  12576. let startToken;
  12577. if (input.charCodeAt(start - startIndex) === 96) {
  12578. startToken = new Token({
  12579. type: getExportedToken(22),
  12580. value: "`",
  12581. start: start,
  12582. end: backquoteEnd,
  12583. startLoc: loc.start,
  12584. endLoc: backquoteEndLoc
  12585. });
  12586. } else {
  12587. startToken = new Token({
  12588. type: getExportedToken(8),
  12589. value: "}",
  12590. start: start,
  12591. end: backquoteEnd,
  12592. startLoc: loc.start,
  12593. endLoc: backquoteEndLoc
  12594. });
  12595. }
  12596. let templateValue, templateElementEnd, templateElementEndLoc, endToken;
  12597. if (type === 24) {
  12598. templateElementEnd = end - 1;
  12599. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
  12600. templateValue = value === null ? null : value.slice(1, -1);
  12601. endToken = new Token({
  12602. type: getExportedToken(22),
  12603. value: "`",
  12604. start: templateElementEnd,
  12605. end: end,
  12606. startLoc: templateElementEndLoc,
  12607. endLoc: loc.end
  12608. });
  12609. } else {
  12610. templateElementEnd = end - 2;
  12611. templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
  12612. templateValue = value === null ? null : value.slice(1, -2);
  12613. endToken = new Token({
  12614. type: getExportedToken(23),
  12615. value: "${",
  12616. start: templateElementEnd,
  12617. end: end,
  12618. startLoc: templateElementEndLoc,
  12619. endLoc: loc.end
  12620. });
  12621. }
  12622. tokens.splice(i, 1, startToken, new Token({
  12623. type: getExportedToken(20),
  12624. value: templateValue,
  12625. start: backquoteEnd,
  12626. end: templateElementEnd,
  12627. startLoc: backquoteEndLoc,
  12628. endLoc: templateElementEndLoc
  12629. }), endToken);
  12630. i += 2;
  12631. continue;
  12632. }
  12633. token.type = getExportedToken(type);
  12634. }
  12635. }
  12636. return tokens;
  12637. }
  12638. class StatementParser extends ExpressionParser {
  12639. parseTopLevel(file, program) {
  12640. file.program = this.parseProgram(program, 140, this.options.sourceType === "module" ? "module" : "script");
  12641. file.comments = this.comments;
  12642. if (this.optionFlags & 256) {
  12643. file.tokens = babel7CompatTokens(this.tokens, this.input, this.startIndex);
  12644. }
  12645. return this.finishNode(file, "File");
  12646. }
  12647. parseProgram(program, end, sourceType) {
  12648. program.sourceType = sourceType;
  12649. program.interpreter = this.parseInterpreterDirective();
  12650. this.parseBlockBody(program, true, true, end);
  12651. if (this.inModule) {
  12652. if (!(this.optionFlags & 64) && this.scope.undefinedExports.size > 0) {
  12653. for (const [localName, at] of Array.from(this.scope.undefinedExports)) {
  12654. this.raise(Errors.ModuleExportUndefined, at, {
  12655. localName
  12656. });
  12657. }
  12658. }
  12659. this.addExtra(program, "topLevelAwait", this.state.hasTopLevelAwait);
  12660. }
  12661. let finishedProgram;
  12662. if (end === 140) {
  12663. finishedProgram = this.finishNode(program, "Program");
  12664. } else {
  12665. finishedProgram = this.finishNodeAt(program, "Program", createPositionWithColumnOffset(this.state.startLoc, -1));
  12666. }
  12667. return finishedProgram;
  12668. }
  12669. stmtToDirective(stmt) {
  12670. const directive = this.castNodeTo(stmt, "Directive");
  12671. const directiveLiteral = this.castNodeTo(stmt.expression, "DirectiveLiteral");
  12672. const expressionValue = directiveLiteral.value;
  12673. const raw = this.input.slice(this.offsetToSourcePos(directiveLiteral.start), this.offsetToSourcePos(directiveLiteral.end));
  12674. const val = directiveLiteral.value = raw.slice(1, -1);
  12675. this.addExtra(directiveLiteral, "raw", raw);
  12676. this.addExtra(directiveLiteral, "rawValue", val);
  12677. this.addExtra(directiveLiteral, "expressionValue", expressionValue);
  12678. directive.value = directiveLiteral;
  12679. delete stmt.expression;
  12680. return directive;
  12681. }
  12682. parseInterpreterDirective() {
  12683. if (!this.match(28)) {
  12684. return null;
  12685. }
  12686. const node = this.startNode();
  12687. node.value = this.state.value;
  12688. this.next();
  12689. return this.finishNode(node, "InterpreterDirective");
  12690. }
  12691. isLet() {
  12692. if (!this.isContextual(100)) {
  12693. return false;
  12694. }
  12695. return this.hasFollowingBindingAtom();
  12696. }
  12697. isUsing() {
  12698. if (!this.isContextual(107)) {
  12699. return false;
  12700. }
  12701. return this.nextTokenIsIdentifierOnSameLine();
  12702. }
  12703. isForUsing() {
  12704. if (!this.isContextual(107)) {
  12705. return false;
  12706. }
  12707. const next = this.nextTokenInLineStart();
  12708. const nextCh = this.codePointAtPos(next);
  12709. if (this.isUnparsedContextual(next, "of")) {
  12710. const nextCharAfterOf = this.lookaheadCharCodeSince(next + 2);
  12711. if (nextCharAfterOf !== 61 && nextCharAfterOf !== 58 && nextCharAfterOf !== 59) {
  12712. return false;
  12713. }
  12714. }
  12715. if (this.chStartsBindingIdentifier(nextCh, next) || this.isUnparsedContextual(next, "void")) {
  12716. return true;
  12717. }
  12718. return false;
  12719. }
  12720. nextTokenIsIdentifierOnSameLine() {
  12721. const next = this.nextTokenInLineStart();
  12722. const nextCh = this.codePointAtPos(next);
  12723. return this.chStartsBindingIdentifier(nextCh, next);
  12724. }
  12725. isAwaitUsing() {
  12726. if (!this.isContextual(96)) {
  12727. return false;
  12728. }
  12729. let next = this.nextTokenInLineStart();
  12730. if (this.isUnparsedContextual(next, "using")) {
  12731. next = this.nextTokenInLineStartSince(next + 5);
  12732. const nextCh = this.codePointAtPos(next);
  12733. if (this.chStartsBindingIdentifier(nextCh, next)) {
  12734. return true;
  12735. }
  12736. }
  12737. return false;
  12738. }
  12739. chStartsBindingIdentifier(ch, pos) {
  12740. if (isIdentifierStart(ch)) {
  12741. keywordRelationalOperator.lastIndex = pos;
  12742. if (keywordRelationalOperator.test(this.input)) {
  12743. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  12744. if (!isIdentifierChar(endCh) && endCh !== 92) {
  12745. return false;
  12746. }
  12747. }
  12748. return true;
  12749. } else if (ch === 92) {
  12750. return true;
  12751. } else {
  12752. return false;
  12753. }
  12754. }
  12755. chStartsBindingPattern(ch) {
  12756. return ch === 91 || ch === 123;
  12757. }
  12758. hasFollowingBindingAtom() {
  12759. const next = this.nextTokenStart();
  12760. const nextCh = this.codePointAtPos(next);
  12761. return this.chStartsBindingPattern(nextCh) || this.chStartsBindingIdentifier(nextCh, next);
  12762. }
  12763. hasInLineFollowingBindingIdentifierOrBrace() {
  12764. const next = this.nextTokenInLineStart();
  12765. const nextCh = this.codePointAtPos(next);
  12766. return nextCh === 123 || this.chStartsBindingIdentifier(nextCh, next);
  12767. }
  12768. allowsUsing() {
  12769. return (this.scope.inModule || !this.scope.inTopLevel) && !this.scope.inBareCaseStatement;
  12770. }
  12771. parseModuleItem() {
  12772. return this.parseStatementLike(1 | 2 | 4 | 8);
  12773. }
  12774. parseStatementListItem() {
  12775. return this.parseStatementLike(2 | 4 | (!this.options.annexB || this.state.strict ? 0 : 8));
  12776. }
  12777. parseStatementOrSloppyAnnexBFunctionDeclaration(allowLabeledFunction = false) {
  12778. let flags = 0;
  12779. if (this.options.annexB && !this.state.strict) {
  12780. flags |= 4;
  12781. if (allowLabeledFunction) {
  12782. flags |= 8;
  12783. }
  12784. }
  12785. return this.parseStatementLike(flags);
  12786. }
  12787. parseStatement() {
  12788. return this.parseStatementLike(0);
  12789. }
  12790. parseStatementLike(flags) {
  12791. let decorators = null;
  12792. if (this.match(26)) {
  12793. decorators = this.parseDecorators(true);
  12794. }
  12795. return this.parseStatementContent(flags, decorators);
  12796. }
  12797. parseStatementContent(flags, decorators) {
  12798. const startType = this.state.type;
  12799. const node = this.startNode();
  12800. const allowDeclaration = !!(flags & 2);
  12801. const allowFunctionDeclaration = !!(flags & 4);
  12802. const topLevel = flags & 1;
  12803. switch (startType) {
  12804. case 60:
  12805. return this.parseBreakContinueStatement(node, true);
  12806. case 63:
  12807. return this.parseBreakContinueStatement(node, false);
  12808. case 64:
  12809. return this.parseDebuggerStatement(node);
  12810. case 90:
  12811. return this.parseDoWhileStatement(node);
  12812. case 91:
  12813. return this.parseForStatement(node);
  12814. case 68:
  12815. if (this.lookaheadCharCode() === 46) break;
  12816. if (!allowFunctionDeclaration) {
  12817. this.raise(this.state.strict ? Errors.StrictFunction : this.options.annexB ? Errors.SloppyFunctionAnnexB : Errors.SloppyFunction, this.state.startLoc);
  12818. }
  12819. return this.parseFunctionStatement(node, false, !allowDeclaration && allowFunctionDeclaration);
  12820. case 80:
  12821. if (!allowDeclaration) this.unexpected();
  12822. return this.parseClass(this.maybeTakeDecorators(decorators, node), true);
  12823. case 69:
  12824. return this.parseIfStatement(node);
  12825. case 70:
  12826. return this.parseReturnStatement(node);
  12827. case 71:
  12828. return this.parseSwitchStatement(node);
  12829. case 72:
  12830. return this.parseThrowStatement(node);
  12831. case 73:
  12832. return this.parseTryStatement(node);
  12833. case 96:
  12834. if (this.isAwaitUsing()) {
  12835. if (!this.allowsUsing()) {
  12836. this.raise(Errors.UnexpectedUsingDeclaration, node);
  12837. } else if (!allowDeclaration) {
  12838. this.raise(Errors.UnexpectedLexicalDeclaration, node);
  12839. } else if (!this.recordAwaitIfAllowed()) {
  12840. this.raise(Errors.AwaitUsingNotInAsyncContext, node);
  12841. }
  12842. this.next();
  12843. return this.parseVarStatement(node, "await using");
  12844. }
  12845. break;
  12846. case 107:
  12847. if (this.state.containsEsc || !this.hasInLineFollowingBindingIdentifierOrBrace()) {
  12848. break;
  12849. }
  12850. if (!this.allowsUsing()) {
  12851. this.raise(Errors.UnexpectedUsingDeclaration, this.state.startLoc);
  12852. } else if (!allowDeclaration) {
  12853. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12854. }
  12855. return this.parseVarStatement(node, "using");
  12856. case 100:
  12857. {
  12858. if (this.state.containsEsc) {
  12859. break;
  12860. }
  12861. const next = this.nextTokenStart();
  12862. const nextCh = this.codePointAtPos(next);
  12863. if (nextCh !== 91) {
  12864. if (!allowDeclaration && this.hasFollowingLineBreak()) break;
  12865. if (!this.chStartsBindingIdentifier(nextCh, next) && nextCh !== 123) {
  12866. break;
  12867. }
  12868. }
  12869. }
  12870. case 75:
  12871. {
  12872. if (!allowDeclaration) {
  12873. this.raise(Errors.UnexpectedLexicalDeclaration, this.state.startLoc);
  12874. }
  12875. }
  12876. case 74:
  12877. {
  12878. const kind = this.state.value;
  12879. return this.parseVarStatement(node, kind);
  12880. }
  12881. case 92:
  12882. return this.parseWhileStatement(node);
  12883. case 76:
  12884. return this.parseWithStatement(node);
  12885. case 5:
  12886. return this.parseBlock();
  12887. case 13:
  12888. return this.parseEmptyStatement(node);
  12889. case 83:
  12890. {
  12891. const nextTokenCharCode = this.lookaheadCharCode();
  12892. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  12893. break;
  12894. }
  12895. }
  12896. case 82:
  12897. {
  12898. if (!(this.optionFlags & 8) && !topLevel) {
  12899. this.raise(Errors.UnexpectedImportExport, this.state.startLoc);
  12900. }
  12901. this.next();
  12902. let result;
  12903. if (startType === 83) {
  12904. result = this.parseImport(node);
  12905. } else {
  12906. result = this.parseExport(node, decorators);
  12907. }
  12908. this.assertModuleNodeAllowed(result);
  12909. return result;
  12910. }
  12911. default:
  12912. {
  12913. if (this.isAsyncFunction()) {
  12914. if (!allowDeclaration) {
  12915. this.raise(Errors.AsyncFunctionInSingleStatementContext, this.state.startLoc);
  12916. }
  12917. this.next();
  12918. return this.parseFunctionStatement(node, true, !allowDeclaration && allowFunctionDeclaration);
  12919. }
  12920. }
  12921. }
  12922. const maybeName = this.state.value;
  12923. const expr = this.parseExpression();
  12924. if (tokenIsIdentifier(startType) && expr.type === "Identifier" && this.eat(14)) {
  12925. return this.parseLabeledStatement(node, maybeName, expr, flags);
  12926. } else {
  12927. return this.parseExpressionStatement(node, expr, decorators);
  12928. }
  12929. }
  12930. assertModuleNodeAllowed(node) {
  12931. if (!(this.optionFlags & 8) && !this.inModule) {
  12932. this.raise(Errors.ImportOutsideModule, node);
  12933. }
  12934. }
  12935. decoratorsEnabledBeforeExport() {
  12936. if (this.hasPlugin("decorators-legacy")) return true;
  12937. return this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") !== false;
  12938. }
  12939. maybeTakeDecorators(maybeDecorators, classNode, exportNode) {
  12940. if (maybeDecorators) {
  12941. var _classNode$decorators;
  12942. if ((_classNode$decorators = classNode.decorators) != null && _classNode$decorators.length) {
  12943. if (typeof this.getPluginOption("decorators", "decoratorsBeforeExport") !== "boolean") {
  12944. this.raise(Errors.DecoratorsBeforeAfterExport, classNode.decorators[0]);
  12945. }
  12946. classNode.decorators.unshift(...maybeDecorators);
  12947. } else {
  12948. classNode.decorators = maybeDecorators;
  12949. }
  12950. this.resetStartLocationFromNode(classNode, maybeDecorators[0]);
  12951. if (exportNode) this.resetStartLocationFromNode(exportNode, classNode);
  12952. }
  12953. return classNode;
  12954. }
  12955. canHaveLeadingDecorator() {
  12956. return this.match(80);
  12957. }
  12958. parseDecorators(allowExport) {
  12959. const decorators = [];
  12960. do {
  12961. decorators.push(this.parseDecorator());
  12962. } while (this.match(26));
  12963. if (this.match(82)) {
  12964. if (!allowExport) {
  12965. this.unexpected();
  12966. }
  12967. if (!this.decoratorsEnabledBeforeExport()) {
  12968. this.raise(Errors.DecoratorExportClass, this.state.startLoc);
  12969. }
  12970. } else if (!this.canHaveLeadingDecorator()) {
  12971. throw this.raise(Errors.UnexpectedLeadingDecorator, this.state.startLoc);
  12972. }
  12973. return decorators;
  12974. }
  12975. parseDecorator() {
  12976. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  12977. const node = this.startNode();
  12978. this.next();
  12979. if (this.hasPlugin("decorators")) {
  12980. const startLoc = this.state.startLoc;
  12981. let expr;
  12982. if (this.match(10)) {
  12983. const startLoc = this.state.startLoc;
  12984. this.next();
  12985. expr = this.parseExpression();
  12986. this.expect(11);
  12987. expr = this.wrapParenthesis(startLoc, expr);
  12988. const paramsStartLoc = this.state.startLoc;
  12989. node.expression = this.parseMaybeDecoratorArguments(expr, startLoc);
  12990. if (this.getPluginOption("decorators", "allowCallParenthesized") === false && node.expression !== expr) {
  12991. this.raise(Errors.DecoratorArgumentsOutsideParentheses, paramsStartLoc);
  12992. }
  12993. } else {
  12994. expr = this.parseIdentifier(false);
  12995. while (this.eat(16)) {
  12996. const node = this.startNodeAt(startLoc);
  12997. node.object = expr;
  12998. if (this.match(139)) {
  12999. this.classScope.usePrivateName(this.state.value, this.state.startLoc);
  13000. node.property = this.parsePrivateName();
  13001. } else {
  13002. node.property = this.parseIdentifier(true);
  13003. }
  13004. node.computed = false;
  13005. expr = this.finishNode(node, "MemberExpression");
  13006. }
  13007. node.expression = this.parseMaybeDecoratorArguments(expr, startLoc);
  13008. }
  13009. } else {
  13010. node.expression = this.parseExprSubscripts();
  13011. }
  13012. return this.finishNode(node, "Decorator");
  13013. }
  13014. parseMaybeDecoratorArguments(expr, startLoc) {
  13015. if (this.eat(10)) {
  13016. const node = this.startNodeAt(startLoc);
  13017. node.callee = expr;
  13018. node.arguments = this.parseCallExpressionArguments();
  13019. this.toReferencedList(node.arguments);
  13020. return this.finishNode(node, "CallExpression");
  13021. }
  13022. return expr;
  13023. }
  13024. parseBreakContinueStatement(node, isBreak) {
  13025. this.next();
  13026. if (this.isLineTerminator()) {
  13027. node.label = null;
  13028. } else {
  13029. node.label = this.parseIdentifier();
  13030. this.semicolon();
  13031. }
  13032. this.verifyBreakContinue(node, isBreak);
  13033. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  13034. }
  13035. verifyBreakContinue(node, isBreak) {
  13036. let i;
  13037. for (i = 0; i < this.state.labels.length; ++i) {
  13038. const lab = this.state.labels[i];
  13039. if (node.label == null || lab.name === node.label.name) {
  13040. if (lab.kind != null && (isBreak || lab.kind === 1)) {
  13041. break;
  13042. }
  13043. if (node.label && isBreak) break;
  13044. }
  13045. }
  13046. if (i === this.state.labels.length) {
  13047. const type = isBreak ? "BreakStatement" : "ContinueStatement";
  13048. this.raise(Errors.IllegalBreakContinue, node, {
  13049. type
  13050. });
  13051. }
  13052. }
  13053. parseDebuggerStatement(node) {
  13054. this.next();
  13055. this.semicolon();
  13056. return this.finishNode(node, "DebuggerStatement");
  13057. }
  13058. parseHeaderExpression() {
  13059. this.expect(10);
  13060. const val = this.parseExpression();
  13061. this.expect(11);
  13062. return val;
  13063. }
  13064. parseDoWhileStatement(node) {
  13065. this.next();
  13066. this.state.labels.push(loopLabel);
  13067. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13068. this.state.labels.pop();
  13069. this.expect(92);
  13070. node.test = this.parseHeaderExpression();
  13071. this.eat(13);
  13072. return this.finishNode(node, "DoWhileStatement");
  13073. }
  13074. parseForStatement(node) {
  13075. this.next();
  13076. this.state.labels.push(loopLabel);
  13077. let awaitAt = null;
  13078. if (this.isContextual(96) && this.recordAwaitIfAllowed()) {
  13079. awaitAt = this.state.startLoc;
  13080. this.next();
  13081. }
  13082. this.scope.enter(0);
  13083. this.expect(10);
  13084. if (this.match(13)) {
  13085. if (awaitAt !== null) {
  13086. this.unexpected(awaitAt);
  13087. }
  13088. return this.parseFor(node, null);
  13089. }
  13090. const startsWithLet = this.isContextual(100);
  13091. {
  13092. const startsWithAwaitUsing = this.isAwaitUsing();
  13093. const starsWithUsingDeclaration = startsWithAwaitUsing || this.isForUsing();
  13094. const isLetOrUsing = startsWithLet && this.hasFollowingBindingAtom() || starsWithUsingDeclaration;
  13095. if (this.match(74) || this.match(75) || isLetOrUsing) {
  13096. const initNode = this.startNode();
  13097. let kind;
  13098. if (startsWithAwaitUsing) {
  13099. kind = "await using";
  13100. if (!this.recordAwaitIfAllowed()) {
  13101. this.raise(Errors.AwaitUsingNotInAsyncContext, this.state.startLoc);
  13102. }
  13103. this.next();
  13104. } else {
  13105. kind = this.state.value;
  13106. }
  13107. this.next();
  13108. this.parseVar(initNode, true, kind);
  13109. const init = this.finishNode(initNode, "VariableDeclaration");
  13110. const isForIn = this.match(58);
  13111. if (isForIn && starsWithUsingDeclaration) {
  13112. this.raise(Errors.ForInUsing, init);
  13113. }
  13114. if ((isForIn || this.isContextual(102)) && init.declarations.length === 1) {
  13115. return this.parseForIn(node, init, awaitAt);
  13116. }
  13117. if (awaitAt !== null) {
  13118. this.unexpected(awaitAt);
  13119. }
  13120. return this.parseFor(node, init);
  13121. }
  13122. }
  13123. const startsWithAsync = this.isContextual(95);
  13124. const refExpressionErrors = new ExpressionErrors();
  13125. const init = this.parseExpression(true, refExpressionErrors);
  13126. const isForOf = this.isContextual(102);
  13127. if (isForOf) {
  13128. if (startsWithLet) {
  13129. this.raise(Errors.ForOfLet, init);
  13130. }
  13131. if (awaitAt === null && startsWithAsync && init.type === "Identifier") {
  13132. this.raise(Errors.ForOfAsync, init);
  13133. }
  13134. }
  13135. if (isForOf || this.match(58)) {
  13136. this.checkDestructuringPrivate(refExpressionErrors);
  13137. this.toAssignable(init, true);
  13138. const type = isForOf ? "ForOfStatement" : "ForInStatement";
  13139. this.checkLVal(init, {
  13140. type
  13141. });
  13142. return this.parseForIn(node, init, awaitAt);
  13143. } else {
  13144. this.checkExpressionErrors(refExpressionErrors, true);
  13145. }
  13146. if (awaitAt !== null) {
  13147. this.unexpected(awaitAt);
  13148. }
  13149. return this.parseFor(node, init);
  13150. }
  13151. parseFunctionStatement(node, isAsync, isHangingDeclaration) {
  13152. this.next();
  13153. return this.parseFunction(node, 1 | (isHangingDeclaration ? 2 : 0) | (isAsync ? 8 : 0));
  13154. }
  13155. parseIfStatement(node) {
  13156. this.next();
  13157. node.test = this.parseHeaderExpression();
  13158. node.consequent = this.parseStatementOrSloppyAnnexBFunctionDeclaration();
  13159. node.alternate = this.eat(66) ? this.parseStatementOrSloppyAnnexBFunctionDeclaration() : null;
  13160. return this.finishNode(node, "IfStatement");
  13161. }
  13162. parseReturnStatement(node) {
  13163. if (!this.prodParam.hasReturn) {
  13164. this.raise(Errors.IllegalReturn, this.state.startLoc);
  13165. }
  13166. this.next();
  13167. if (this.isLineTerminator()) {
  13168. node.argument = null;
  13169. } else {
  13170. node.argument = this.parseExpression();
  13171. this.semicolon();
  13172. }
  13173. return this.finishNode(node, "ReturnStatement");
  13174. }
  13175. parseSwitchStatement(node) {
  13176. this.next();
  13177. node.discriminant = this.parseHeaderExpression();
  13178. const cases = node.cases = [];
  13179. this.expect(5);
  13180. this.state.labels.push(switchLabel);
  13181. this.scope.enter(256);
  13182. let cur;
  13183. for (let sawDefault; !this.match(8);) {
  13184. if (this.match(61) || this.match(65)) {
  13185. const isCase = this.match(61);
  13186. if (cur) this.finishNode(cur, "SwitchCase");
  13187. cases.push(cur = this.startNode());
  13188. cur.consequent = [];
  13189. this.next();
  13190. if (isCase) {
  13191. cur.test = this.parseExpression();
  13192. } else {
  13193. if (sawDefault) {
  13194. this.raise(Errors.MultipleDefaultsInSwitch, this.state.lastTokStartLoc);
  13195. }
  13196. sawDefault = true;
  13197. cur.test = null;
  13198. }
  13199. this.expect(14);
  13200. } else {
  13201. if (cur) {
  13202. cur.consequent.push(this.parseStatementListItem());
  13203. } else {
  13204. this.unexpected();
  13205. }
  13206. }
  13207. }
  13208. this.scope.exit();
  13209. if (cur) this.finishNode(cur, "SwitchCase");
  13210. this.next();
  13211. this.state.labels.pop();
  13212. return this.finishNode(node, "SwitchStatement");
  13213. }
  13214. parseThrowStatement(node) {
  13215. this.next();
  13216. if (this.hasPrecedingLineBreak()) {
  13217. this.raise(Errors.NewlineAfterThrow, this.state.lastTokEndLoc);
  13218. }
  13219. node.argument = this.parseExpression();
  13220. this.semicolon();
  13221. return this.finishNode(node, "ThrowStatement");
  13222. }
  13223. parseCatchClauseParam() {
  13224. const param = this.parseBindingAtom();
  13225. this.scope.enter(this.options.annexB && param.type === "Identifier" ? 8 : 0);
  13226. this.checkLVal(param, {
  13227. type: "CatchClause"
  13228. }, 9);
  13229. return param;
  13230. }
  13231. parseTryStatement(node) {
  13232. this.next();
  13233. node.block = this.parseBlock();
  13234. node.handler = null;
  13235. if (this.match(62)) {
  13236. const clause = this.startNode();
  13237. this.next();
  13238. if (this.match(10)) {
  13239. this.expect(10);
  13240. clause.param = this.parseCatchClauseParam();
  13241. this.expect(11);
  13242. } else {
  13243. clause.param = null;
  13244. this.scope.enter(0);
  13245. }
  13246. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  13247. this.scope.exit();
  13248. node.handler = this.finishNode(clause, "CatchClause");
  13249. }
  13250. node.finalizer = this.eat(67) ? this.parseBlock() : null;
  13251. if (!node.handler && !node.finalizer) {
  13252. this.raise(Errors.NoCatchOrFinally, node);
  13253. }
  13254. return this.finishNode(node, "TryStatement");
  13255. }
  13256. parseVarStatement(node, kind, allowMissingInitializer = false) {
  13257. this.next();
  13258. this.parseVar(node, false, kind, allowMissingInitializer);
  13259. this.semicolon();
  13260. return this.finishNode(node, "VariableDeclaration");
  13261. }
  13262. parseWhileStatement(node) {
  13263. this.next();
  13264. node.test = this.parseHeaderExpression();
  13265. this.state.labels.push(loopLabel);
  13266. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13267. this.state.labels.pop();
  13268. return this.finishNode(node, "WhileStatement");
  13269. }
  13270. parseWithStatement(node) {
  13271. if (this.state.strict) {
  13272. this.raise(Errors.StrictWith, this.state.startLoc);
  13273. }
  13274. this.next();
  13275. node.object = this.parseHeaderExpression();
  13276. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13277. return this.finishNode(node, "WithStatement");
  13278. }
  13279. parseEmptyStatement(node) {
  13280. this.next();
  13281. return this.finishNode(node, "EmptyStatement");
  13282. }
  13283. parseLabeledStatement(node, maybeName, expr, flags) {
  13284. for (const label of this.state.labels) {
  13285. if (label.name === maybeName) {
  13286. this.raise(Errors.LabelRedeclaration, expr, {
  13287. labelName: maybeName
  13288. });
  13289. }
  13290. }
  13291. const kind = tokenIsLoop(this.state.type) ? 1 : this.match(71) ? 2 : null;
  13292. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  13293. const label = this.state.labels[i];
  13294. if (label.statementStart === node.start) {
  13295. label.statementStart = this.sourceToOffsetPos(this.state.start);
  13296. label.kind = kind;
  13297. } else {
  13298. break;
  13299. }
  13300. }
  13301. this.state.labels.push({
  13302. name: maybeName,
  13303. kind: kind,
  13304. statementStart: this.sourceToOffsetPos(this.state.start)
  13305. });
  13306. node.body = flags & 8 ? this.parseStatementOrSloppyAnnexBFunctionDeclaration(true) : this.parseStatement();
  13307. this.state.labels.pop();
  13308. node.label = expr;
  13309. return this.finishNode(node, "LabeledStatement");
  13310. }
  13311. parseExpressionStatement(node, expr, decorators) {
  13312. node.expression = expr;
  13313. this.semicolon();
  13314. return this.finishNode(node, "ExpressionStatement");
  13315. }
  13316. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  13317. const node = this.startNode();
  13318. if (allowDirectives) {
  13319. this.state.strictErrors.clear();
  13320. }
  13321. this.expect(5);
  13322. if (createNewLexicalScope) {
  13323. this.scope.enter(0);
  13324. }
  13325. this.parseBlockBody(node, allowDirectives, false, 8, afterBlockParse);
  13326. if (createNewLexicalScope) {
  13327. this.scope.exit();
  13328. }
  13329. return this.finishNode(node, "BlockStatement");
  13330. }
  13331. isValidDirective(stmt) {
  13332. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  13333. }
  13334. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  13335. const body = node.body = [];
  13336. const directives = node.directives = [];
  13337. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  13338. }
  13339. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  13340. const oldStrict = this.state.strict;
  13341. let hasStrictModeDirective = false;
  13342. let parsedNonDirective = false;
  13343. while (!this.match(end)) {
  13344. const stmt = topLevel ? this.parseModuleItem() : this.parseStatementListItem();
  13345. if (directives && !parsedNonDirective) {
  13346. if (this.isValidDirective(stmt)) {
  13347. const directive = this.stmtToDirective(stmt);
  13348. directives.push(directive);
  13349. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  13350. hasStrictModeDirective = true;
  13351. this.setStrict(true);
  13352. }
  13353. continue;
  13354. }
  13355. parsedNonDirective = true;
  13356. this.state.strictErrors.clear();
  13357. }
  13358. body.push(stmt);
  13359. }
  13360. afterBlockParse == null || afterBlockParse.call(this, hasStrictModeDirective);
  13361. if (!oldStrict) {
  13362. this.setStrict(false);
  13363. }
  13364. this.next();
  13365. }
  13366. parseFor(node, init) {
  13367. node.init = init;
  13368. this.semicolon(false);
  13369. node.test = this.match(13) ? null : this.parseExpression();
  13370. this.semicolon(false);
  13371. node.update = this.match(11) ? null : this.parseExpression();
  13372. this.expect(11);
  13373. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13374. this.scope.exit();
  13375. this.state.labels.pop();
  13376. return this.finishNode(node, "ForStatement");
  13377. }
  13378. parseForIn(node, init, awaitAt) {
  13379. const isForIn = this.match(58);
  13380. this.next();
  13381. if (isForIn) {
  13382. if (awaitAt !== null) this.unexpected(awaitAt);
  13383. } else {
  13384. node.await = awaitAt !== null;
  13385. }
  13386. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || !this.options.annexB || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  13387. this.raise(Errors.ForInOfLoopInitializer, init, {
  13388. type: isForIn ? "ForInStatement" : "ForOfStatement"
  13389. });
  13390. }
  13391. if (init.type === "AssignmentPattern") {
  13392. this.raise(Errors.InvalidLhs, init, {
  13393. ancestor: {
  13394. type: "ForStatement"
  13395. }
  13396. });
  13397. }
  13398. node.left = init;
  13399. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  13400. this.expect(11);
  13401. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement());
  13402. this.scope.exit();
  13403. this.state.labels.pop();
  13404. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  13405. }
  13406. parseVar(node, isFor, kind, allowMissingInitializer = false) {
  13407. const declarations = node.declarations = [];
  13408. node.kind = kind;
  13409. for (;;) {
  13410. const decl = this.startNode();
  13411. this.parseVarId(decl, kind);
  13412. decl.init = !this.eat(29) ? null : isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  13413. if (decl.init === null && !allowMissingInitializer) {
  13414. if (decl.id.type !== "Identifier" && !(isFor && (this.match(58) || this.isContextual(102)))) {
  13415. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  13416. kind: "destructuring"
  13417. });
  13418. } else if ((kind === "const" || kind === "using" || kind === "await using") && !(this.match(58) || this.isContextual(102))) {
  13419. this.raise(Errors.DeclarationMissingInitializer, this.state.lastTokEndLoc, {
  13420. kind
  13421. });
  13422. }
  13423. }
  13424. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  13425. if (!this.eat(12)) break;
  13426. }
  13427. return node;
  13428. }
  13429. parseVarId(decl, kind) {
  13430. const id = this.parseBindingAtom();
  13431. if (kind === "using" || kind === "await using") {
  13432. if (id.type === "ArrayPattern" || id.type === "ObjectPattern") {
  13433. this.raise(Errors.UsingDeclarationHasBindingPattern, id.loc.start);
  13434. }
  13435. } else {
  13436. if (id.type === "VoidPattern") {
  13437. this.raise(Errors.UnexpectedVoidPattern, id.loc.start);
  13438. }
  13439. }
  13440. this.checkLVal(id, {
  13441. type: "VariableDeclarator"
  13442. }, kind === "var" ? 5 : 8201);
  13443. decl.id = id;
  13444. }
  13445. parseAsyncFunctionExpression(node) {
  13446. return this.parseFunction(node, 8);
  13447. }
  13448. parseFunction(node, flags = 0) {
  13449. const hangingDeclaration = flags & 2;
  13450. const isDeclaration = !!(flags & 1);
  13451. const requireId = isDeclaration && !(flags & 4);
  13452. const isAsync = !!(flags & 8);
  13453. this.initFunction(node, isAsync);
  13454. if (this.match(55)) {
  13455. if (hangingDeclaration) {
  13456. this.raise(Errors.GeneratorInSingleStatementContext, this.state.startLoc);
  13457. }
  13458. this.next();
  13459. node.generator = true;
  13460. }
  13461. if (isDeclaration) {
  13462. node.id = this.parseFunctionId(requireId);
  13463. }
  13464. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  13465. this.state.maybeInArrowParameters = false;
  13466. this.scope.enter(514);
  13467. this.prodParam.enter(functionFlags(isAsync, node.generator));
  13468. if (!isDeclaration) {
  13469. node.id = this.parseFunctionId();
  13470. }
  13471. this.parseFunctionParams(node, false);
  13472. this.withSmartMixTopicForbiddingContext(() => {
  13473. this.parseFunctionBodyAndFinish(node, isDeclaration ? "FunctionDeclaration" : "FunctionExpression");
  13474. });
  13475. this.prodParam.exit();
  13476. this.scope.exit();
  13477. if (isDeclaration && !hangingDeclaration) {
  13478. this.registerFunctionStatementId(node);
  13479. }
  13480. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  13481. return node;
  13482. }
  13483. parseFunctionId(requireId) {
  13484. return requireId || tokenIsIdentifier(this.state.type) ? this.parseIdentifier() : null;
  13485. }
  13486. parseFunctionParams(node, isConstructor) {
  13487. this.expect(10);
  13488. this.expressionScope.enter(newParameterDeclarationScope());
  13489. node.params = this.parseBindingList(11, 41, 2 | (isConstructor ? 4 : 0));
  13490. this.expressionScope.exit();
  13491. }
  13492. registerFunctionStatementId(node) {
  13493. if (!node.id) return;
  13494. this.scope.declareName(node.id.name, !this.options.annexB || this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? 5 : 8201 : 17, node.id.loc.start);
  13495. }
  13496. parseClass(node, isStatement, optionalId) {
  13497. this.next();
  13498. const oldStrict = this.state.strict;
  13499. this.state.strict = true;
  13500. this.parseClassId(node, isStatement, optionalId);
  13501. this.parseClassSuper(node);
  13502. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  13503. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  13504. }
  13505. isClassProperty() {
  13506. return this.match(29) || this.match(13) || this.match(8);
  13507. }
  13508. isClassMethod() {
  13509. return this.match(10);
  13510. }
  13511. nameIsConstructor(key) {
  13512. return key.type === "Identifier" && key.name === "constructor" || key.type === "StringLiteral" && key.value === "constructor";
  13513. }
  13514. isNonstaticConstructor(method) {
  13515. return !method.computed && !method.static && this.nameIsConstructor(method.key);
  13516. }
  13517. parseClassBody(hadSuperClass, oldStrict) {
  13518. this.classScope.enter();
  13519. const state = {
  13520. hadConstructor: false,
  13521. hadSuperClass
  13522. };
  13523. let decorators = [];
  13524. const classBody = this.startNode();
  13525. classBody.body = [];
  13526. this.expect(5);
  13527. this.withSmartMixTopicForbiddingContext(() => {
  13528. while (!this.match(8)) {
  13529. if (this.eat(13)) {
  13530. if (decorators.length > 0) {
  13531. throw this.raise(Errors.DecoratorSemicolon, this.state.lastTokEndLoc);
  13532. }
  13533. continue;
  13534. }
  13535. if (this.match(26)) {
  13536. decorators.push(this.parseDecorator());
  13537. continue;
  13538. }
  13539. const member = this.startNode();
  13540. if (decorators.length) {
  13541. member.decorators = decorators;
  13542. this.resetStartLocationFromNode(member, decorators[0]);
  13543. decorators = [];
  13544. }
  13545. this.parseClassMember(classBody, member, state);
  13546. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  13547. this.raise(Errors.DecoratorConstructor, member);
  13548. }
  13549. }
  13550. });
  13551. this.state.strict = oldStrict;
  13552. this.next();
  13553. if (decorators.length) {
  13554. throw this.raise(Errors.TrailingDecorator, this.state.startLoc);
  13555. }
  13556. this.classScope.exit();
  13557. return this.finishNode(classBody, "ClassBody");
  13558. }
  13559. parseClassMemberFromModifier(classBody, member) {
  13560. const key = this.parseIdentifier(true);
  13561. if (this.isClassMethod()) {
  13562. const method = member;
  13563. method.kind = "method";
  13564. method.computed = false;
  13565. method.key = key;
  13566. method.static = false;
  13567. this.pushClassMethod(classBody, method, false, false, false, false);
  13568. return true;
  13569. } else if (this.isClassProperty()) {
  13570. const prop = member;
  13571. prop.computed = false;
  13572. prop.key = key;
  13573. prop.static = false;
  13574. classBody.body.push(this.parseClassProperty(prop));
  13575. return true;
  13576. }
  13577. this.resetPreviousNodeTrailingComments(key);
  13578. return false;
  13579. }
  13580. parseClassMember(classBody, member, state) {
  13581. const isStatic = this.isContextual(106);
  13582. if (isStatic) {
  13583. if (this.parseClassMemberFromModifier(classBody, member)) {
  13584. return;
  13585. }
  13586. if (this.eat(5)) {
  13587. this.parseClassStaticBlock(classBody, member);
  13588. return;
  13589. }
  13590. }
  13591. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  13592. }
  13593. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  13594. const publicMethod = member;
  13595. const privateMethod = member;
  13596. const publicProp = member;
  13597. const privateProp = member;
  13598. const accessorProp = member;
  13599. const method = publicMethod;
  13600. const publicMember = publicMethod;
  13601. member.static = isStatic;
  13602. this.parsePropertyNamePrefixOperator(member);
  13603. if (this.eat(55)) {
  13604. method.kind = "method";
  13605. const isPrivateName = this.match(139);
  13606. this.parseClassElementName(method);
  13607. this.parsePostMemberNameModifiers(method);
  13608. if (isPrivateName) {
  13609. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  13610. return;
  13611. }
  13612. if (this.isNonstaticConstructor(publicMethod)) {
  13613. this.raise(Errors.ConstructorIsGenerator, publicMethod.key);
  13614. }
  13615. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  13616. return;
  13617. }
  13618. const isContextual = !this.state.containsEsc && tokenIsIdentifier(this.state.type);
  13619. const key = this.parseClassElementName(member);
  13620. const maybeContextualKw = isContextual ? key.name : null;
  13621. const isPrivate = this.isPrivateName(key);
  13622. const maybeQuestionTokenStartLoc = this.state.startLoc;
  13623. this.parsePostMemberNameModifiers(publicMember);
  13624. if (this.isClassMethod()) {
  13625. method.kind = "method";
  13626. if (isPrivate) {
  13627. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13628. return;
  13629. }
  13630. const isConstructor = this.isNonstaticConstructor(publicMethod);
  13631. let allowsDirectSuper = false;
  13632. if (isConstructor) {
  13633. publicMethod.kind = "constructor";
  13634. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  13635. this.raise(Errors.DuplicateConstructor, key);
  13636. }
  13637. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  13638. this.raise(Errors.OverrideOnConstructor, key);
  13639. }
  13640. state.hadConstructor = true;
  13641. allowsDirectSuper = state.hadSuperClass;
  13642. }
  13643. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  13644. } else if (this.isClassProperty()) {
  13645. if (isPrivate) {
  13646. this.pushClassPrivateProperty(classBody, privateProp);
  13647. } else {
  13648. this.pushClassProperty(classBody, publicProp);
  13649. }
  13650. } else if (maybeContextualKw === "async" && !this.isLineTerminator()) {
  13651. this.resetPreviousNodeTrailingComments(key);
  13652. const isGenerator = this.eat(55);
  13653. if (publicMember.optional) {
  13654. this.unexpected(maybeQuestionTokenStartLoc);
  13655. }
  13656. method.kind = "method";
  13657. const isPrivate = this.match(139);
  13658. this.parseClassElementName(method);
  13659. this.parsePostMemberNameModifiers(publicMember);
  13660. if (isPrivate) {
  13661. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  13662. } else {
  13663. if (this.isNonstaticConstructor(publicMethod)) {
  13664. this.raise(Errors.ConstructorIsAsync, publicMethod.key);
  13665. }
  13666. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  13667. }
  13668. } else if ((maybeContextualKw === "get" || maybeContextualKw === "set") && !(this.match(55) && this.isLineTerminator())) {
  13669. this.resetPreviousNodeTrailingComments(key);
  13670. method.kind = maybeContextualKw;
  13671. const isPrivate = this.match(139);
  13672. this.parseClassElementName(publicMethod);
  13673. if (isPrivate) {
  13674. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  13675. } else {
  13676. if (this.isNonstaticConstructor(publicMethod)) {
  13677. this.raise(Errors.ConstructorIsAccessor, publicMethod.key);
  13678. }
  13679. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  13680. }
  13681. this.checkGetterSetterParams(publicMethod);
  13682. } else if (maybeContextualKw === "accessor" && !this.isLineTerminator()) {
  13683. this.expectPlugin("decoratorAutoAccessors");
  13684. this.resetPreviousNodeTrailingComments(key);
  13685. const isPrivate = this.match(139);
  13686. this.parseClassElementName(publicProp);
  13687. this.pushClassAccessorProperty(classBody, accessorProp, isPrivate);
  13688. } else if (this.isLineTerminator()) {
  13689. if (isPrivate) {
  13690. this.pushClassPrivateProperty(classBody, privateProp);
  13691. } else {
  13692. this.pushClassProperty(classBody, publicProp);
  13693. }
  13694. } else {
  13695. this.unexpected();
  13696. }
  13697. }
  13698. parseClassElementName(member) {
  13699. const {
  13700. type,
  13701. value
  13702. } = this.state;
  13703. if ((type === 132 || type === 134) && member.static && value === "prototype") {
  13704. this.raise(Errors.StaticPrototype, this.state.startLoc);
  13705. }
  13706. if (type === 139) {
  13707. if (value === "constructor") {
  13708. this.raise(Errors.ConstructorClassPrivateField, this.state.startLoc);
  13709. }
  13710. const key = this.parsePrivateName();
  13711. member.key = key;
  13712. return key;
  13713. }
  13714. this.parsePropertyName(member);
  13715. return member.key;
  13716. }
  13717. parseClassStaticBlock(classBody, member) {
  13718. var _member$decorators;
  13719. this.scope.enter(576 | 128 | 16);
  13720. const oldLabels = this.state.labels;
  13721. this.state.labels = [];
  13722. this.prodParam.enter(0);
  13723. const body = member.body = [];
  13724. this.parseBlockOrModuleBlockBody(body, undefined, false, 8);
  13725. this.prodParam.exit();
  13726. this.scope.exit();
  13727. this.state.labels = oldLabels;
  13728. classBody.body.push(this.finishNode(member, "StaticBlock"));
  13729. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  13730. this.raise(Errors.DecoratorStaticBlock, member);
  13731. }
  13732. }
  13733. pushClassProperty(classBody, prop) {
  13734. if (!prop.computed && this.nameIsConstructor(prop.key)) {
  13735. this.raise(Errors.ConstructorClassField, prop.key);
  13736. }
  13737. classBody.body.push(this.parseClassProperty(prop));
  13738. }
  13739. pushClassPrivateProperty(classBody, prop) {
  13740. const node = this.parseClassPrivateProperty(prop);
  13741. classBody.body.push(node);
  13742. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13743. }
  13744. pushClassAccessorProperty(classBody, prop, isPrivate) {
  13745. if (!isPrivate && !prop.computed && this.nameIsConstructor(prop.key)) {
  13746. this.raise(Errors.ConstructorClassField, prop.key);
  13747. }
  13748. const node = this.parseClassAccessorProperty(prop);
  13749. classBody.body.push(node);
  13750. if (isPrivate) {
  13751. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), 0, node.key.loc.start);
  13752. }
  13753. }
  13754. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  13755. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  13756. }
  13757. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  13758. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  13759. classBody.body.push(node);
  13760. const kind = node.kind === "get" ? node.static ? 6 : 2 : node.kind === "set" ? node.static ? 5 : 1 : 0;
  13761. this.declareClassPrivateMethodInScope(node, kind);
  13762. }
  13763. declareClassPrivateMethodInScope(node, kind) {
  13764. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.loc.start);
  13765. }
  13766. parsePostMemberNameModifiers(methodOrProp) {}
  13767. parseClassPrivateProperty(node) {
  13768. this.parseInitializer(node);
  13769. this.semicolon();
  13770. return this.finishNode(node, "ClassPrivateProperty");
  13771. }
  13772. parseClassProperty(node) {
  13773. this.parseInitializer(node);
  13774. this.semicolon();
  13775. return this.finishNode(node, "ClassProperty");
  13776. }
  13777. parseClassAccessorProperty(node) {
  13778. this.parseInitializer(node);
  13779. this.semicolon();
  13780. return this.finishNode(node, "ClassAccessorProperty");
  13781. }
  13782. parseInitializer(node) {
  13783. this.scope.enter(576 | 16);
  13784. this.expressionScope.enter(newExpressionScope());
  13785. this.prodParam.enter(0);
  13786. node.value = this.eat(29) ? this.parseMaybeAssignAllowIn() : null;
  13787. this.expressionScope.exit();
  13788. this.prodParam.exit();
  13789. this.scope.exit();
  13790. }
  13791. parseClassId(node, isStatement, optionalId, bindingType = 8331) {
  13792. if (tokenIsIdentifier(this.state.type)) {
  13793. node.id = this.parseIdentifier();
  13794. if (isStatement) {
  13795. this.declareNameFromIdentifier(node.id, bindingType);
  13796. }
  13797. } else {
  13798. if (optionalId || !isStatement) {
  13799. node.id = null;
  13800. } else {
  13801. throw this.raise(Errors.MissingClassName, this.state.startLoc);
  13802. }
  13803. }
  13804. }
  13805. parseClassSuper(node) {
  13806. node.superClass = this.eat(81) ? this.parseExprSubscripts() : null;
  13807. }
  13808. parseExport(node, decorators) {
  13809. const maybeDefaultIdentifier = this.parseMaybeImportPhase(node, true);
  13810. const hasDefault = this.maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier);
  13811. const parseAfterDefault = !hasDefault || this.eat(12);
  13812. const hasStar = parseAfterDefault && this.eatExportStar(node);
  13813. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  13814. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(12));
  13815. const isFromRequired = hasDefault || hasStar;
  13816. if (hasStar && !hasNamespace) {
  13817. if (hasDefault) this.unexpected();
  13818. if (decorators) {
  13819. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13820. }
  13821. this.parseExportFrom(node, true);
  13822. this.sawUnambiguousESM = true;
  13823. return this.finishNode(node, "ExportAllDeclaration");
  13824. }
  13825. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  13826. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
  13827. this.unexpected(null, 5);
  13828. }
  13829. if (hasNamespace && parseAfterNamespace) {
  13830. this.unexpected(null, 98);
  13831. }
  13832. let hasDeclaration;
  13833. if (isFromRequired || hasSpecifiers) {
  13834. hasDeclaration = false;
  13835. if (decorators) {
  13836. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13837. }
  13838. this.parseExportFrom(node, isFromRequired);
  13839. } else {
  13840. hasDeclaration = this.maybeParseExportDeclaration(node);
  13841. }
  13842. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  13843. var _node2$declaration;
  13844. const node2 = node;
  13845. this.checkExport(node2, true, false, !!node2.source);
  13846. if (((_node2$declaration = node2.declaration) == null ? void 0 : _node2$declaration.type) === "ClassDeclaration") {
  13847. this.maybeTakeDecorators(decorators, node2.declaration, node2);
  13848. } else if (decorators) {
  13849. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13850. }
  13851. this.sawUnambiguousESM = true;
  13852. return this.finishNode(node2, "ExportNamedDeclaration");
  13853. }
  13854. if (this.eat(65)) {
  13855. const node2 = node;
  13856. const decl = this.parseExportDefaultExpression();
  13857. node2.declaration = decl;
  13858. if (decl.type === "ClassDeclaration") {
  13859. this.maybeTakeDecorators(decorators, decl, node2);
  13860. } else if (decorators) {
  13861. throw this.raise(Errors.UnsupportedDecoratorExport, node);
  13862. }
  13863. this.checkExport(node2, true, true);
  13864. this.sawUnambiguousESM = true;
  13865. return this.finishNode(node2, "ExportDefaultDeclaration");
  13866. }
  13867. throw this.unexpected(null, 5);
  13868. }
  13869. eatExportStar(node) {
  13870. return this.eat(55);
  13871. }
  13872. maybeParseExportDefaultSpecifier(node, maybeDefaultIdentifier) {
  13873. if (maybeDefaultIdentifier || this.isExportDefaultSpecifier()) {
  13874. this.expectPlugin("exportDefaultFrom", maybeDefaultIdentifier == null ? void 0 : maybeDefaultIdentifier.loc.start);
  13875. const id = maybeDefaultIdentifier || this.parseIdentifier(true);
  13876. const specifier = this.startNodeAtNode(id);
  13877. specifier.exported = id;
  13878. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  13879. return true;
  13880. }
  13881. return false;
  13882. }
  13883. maybeParseExportNamespaceSpecifier(node) {
  13884. if (this.isContextual(93)) {
  13885. var _ref, _ref$specifiers;
  13886. (_ref$specifiers = (_ref = node).specifiers) != null ? _ref$specifiers : _ref.specifiers = [];
  13887. const specifier = this.startNodeAt(this.state.lastTokStartLoc);
  13888. this.next();
  13889. specifier.exported = this.parseModuleExportName();
  13890. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  13891. return true;
  13892. }
  13893. return false;
  13894. }
  13895. maybeParseExportNamedSpecifiers(node) {
  13896. if (this.match(5)) {
  13897. const node2 = node;
  13898. if (!node2.specifiers) node2.specifiers = [];
  13899. const isTypeExport = node2.exportKind === "type";
  13900. node2.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
  13901. node2.source = null;
  13902. if (this.hasPlugin("importAssertions")) {
  13903. node2.assertions = [];
  13904. } else {
  13905. node2.attributes = [];
  13906. }
  13907. node2.declaration = null;
  13908. return true;
  13909. }
  13910. return false;
  13911. }
  13912. maybeParseExportDeclaration(node) {
  13913. if (this.shouldParseExportDeclaration()) {
  13914. node.specifiers = [];
  13915. node.source = null;
  13916. if (this.hasPlugin("importAssertions")) {
  13917. node.assertions = [];
  13918. } else {
  13919. node.attributes = [];
  13920. }
  13921. node.declaration = this.parseExportDeclaration(node);
  13922. return true;
  13923. }
  13924. return false;
  13925. }
  13926. isAsyncFunction() {
  13927. if (!this.isContextual(95)) return false;
  13928. const next = this.nextTokenInLineStart();
  13929. return this.isUnparsedContextual(next, "function");
  13930. }
  13931. parseExportDefaultExpression() {
  13932. const expr = this.startNode();
  13933. if (this.match(68)) {
  13934. this.next();
  13935. return this.parseFunction(expr, 1 | 4);
  13936. } else if (this.isAsyncFunction()) {
  13937. this.next();
  13938. this.next();
  13939. return this.parseFunction(expr, 1 | 4 | 8);
  13940. }
  13941. if (this.match(80)) {
  13942. return this.parseClass(expr, true, true);
  13943. }
  13944. if (this.match(26)) {
  13945. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  13946. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  13947. }
  13948. return this.parseClass(this.maybeTakeDecorators(this.parseDecorators(false), this.startNode()), true, true);
  13949. }
  13950. if (this.match(75) || this.match(74) || this.isLet() || this.isUsing() || this.isAwaitUsing()) {
  13951. throw this.raise(Errors.UnsupportedDefaultExport, this.state.startLoc);
  13952. }
  13953. const res = this.parseMaybeAssignAllowIn();
  13954. this.semicolon();
  13955. return res;
  13956. }
  13957. parseExportDeclaration(node) {
  13958. if (this.match(80)) {
  13959. const node = this.parseClass(this.startNode(), true, false);
  13960. return node;
  13961. }
  13962. return this.parseStatementListItem();
  13963. }
  13964. isExportDefaultSpecifier() {
  13965. const {
  13966. type
  13967. } = this.state;
  13968. if (tokenIsIdentifier(type)) {
  13969. if (type === 95 && !this.state.containsEsc || type === 100) {
  13970. return false;
  13971. }
  13972. if ((type === 130 || type === 129) && !this.state.containsEsc) {
  13973. const next = this.nextTokenStart();
  13974. const nextChar = this.input.charCodeAt(next);
  13975. if (nextChar === 123 || this.chStartsBindingIdentifier(nextChar, next) && !this.input.startsWith("from", next)) {
  13976. this.expectOnePlugin(["flow", "typescript"]);
  13977. return false;
  13978. }
  13979. }
  13980. } else if (!this.match(65)) {
  13981. return false;
  13982. }
  13983. const next = this.nextTokenStart();
  13984. const hasFrom = this.isUnparsedContextual(next, "from");
  13985. if (this.input.charCodeAt(next) === 44 || tokenIsIdentifier(this.state.type) && hasFrom) {
  13986. return true;
  13987. }
  13988. if (this.match(65) && hasFrom) {
  13989. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  13990. return nextAfterFrom === 34 || nextAfterFrom === 39;
  13991. }
  13992. return false;
  13993. }
  13994. parseExportFrom(node, expect) {
  13995. if (this.eatContextual(98)) {
  13996. node.source = this.parseImportSource();
  13997. this.checkExport(node);
  13998. this.maybeParseImportAttributes(node);
  13999. this.checkJSONModuleImport(node);
  14000. } else if (expect) {
  14001. this.unexpected();
  14002. }
  14003. this.semicolon();
  14004. }
  14005. shouldParseExportDeclaration() {
  14006. const {
  14007. type
  14008. } = this.state;
  14009. if (type === 26) {
  14010. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  14011. if (this.hasPlugin("decorators")) {
  14012. if (this.getPluginOption("decorators", "decoratorsBeforeExport") === true) {
  14013. this.raise(Errors.DecoratorBeforeExport, this.state.startLoc);
  14014. }
  14015. return true;
  14016. }
  14017. }
  14018. if (this.isUsing()) {
  14019. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  14020. return true;
  14021. }
  14022. if (this.isAwaitUsing()) {
  14023. this.raise(Errors.UsingDeclarationExport, this.state.startLoc);
  14024. return true;
  14025. }
  14026. return type === 74 || type === 75 || type === 68 || type === 80 || this.isLet() || this.isAsyncFunction();
  14027. }
  14028. checkExport(node, checkNames, isDefault, isFrom) {
  14029. if (checkNames) {
  14030. var _node$specifiers;
  14031. if (isDefault) {
  14032. this.checkDuplicateExports(node, "default");
  14033. if (this.hasPlugin("exportDefaultFrom")) {
  14034. var _declaration$extra;
  14035. const declaration = node.declaration;
  14036. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  14037. this.raise(Errors.ExportDefaultFromAsIdentifier, declaration);
  14038. }
  14039. }
  14040. } else if ((_node$specifiers = node.specifiers) != null && _node$specifiers.length) {
  14041. for (const specifier of node.specifiers) {
  14042. const {
  14043. exported
  14044. } = specifier;
  14045. const exportName = exported.type === "Identifier" ? exported.name : exported.value;
  14046. this.checkDuplicateExports(specifier, exportName);
  14047. if (!isFrom && specifier.local) {
  14048. const {
  14049. local
  14050. } = specifier;
  14051. if (local.type !== "Identifier") {
  14052. this.raise(Errors.ExportBindingIsString, specifier, {
  14053. localName: local.value,
  14054. exportName
  14055. });
  14056. } else {
  14057. this.checkReservedWord(local.name, local.loc.start, true, false);
  14058. this.scope.checkLocalExport(local);
  14059. }
  14060. }
  14061. }
  14062. } else if (node.declaration) {
  14063. const decl = node.declaration;
  14064. if (decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") {
  14065. const {
  14066. id
  14067. } = decl;
  14068. if (!id) throw new Error("Assertion failure");
  14069. this.checkDuplicateExports(node, id.name);
  14070. } else if (decl.type === "VariableDeclaration") {
  14071. for (const declaration of decl.declarations) {
  14072. this.checkDeclaration(declaration.id);
  14073. }
  14074. }
  14075. }
  14076. }
  14077. }
  14078. checkDeclaration(node) {
  14079. if (node.type === "Identifier") {
  14080. this.checkDuplicateExports(node, node.name);
  14081. } else if (node.type === "ObjectPattern") {
  14082. for (const prop of node.properties) {
  14083. this.checkDeclaration(prop);
  14084. }
  14085. } else if (node.type === "ArrayPattern") {
  14086. for (const elem of node.elements) {
  14087. if (elem) {
  14088. this.checkDeclaration(elem);
  14089. }
  14090. }
  14091. } else if (node.type === "ObjectProperty") {
  14092. this.checkDeclaration(node.value);
  14093. } else if (node.type === "RestElement") {
  14094. this.checkDeclaration(node.argument);
  14095. } else if (node.type === "AssignmentPattern") {
  14096. this.checkDeclaration(node.left);
  14097. }
  14098. }
  14099. checkDuplicateExports(node, exportName) {
  14100. if (this.exportedIdentifiers.has(exportName)) {
  14101. if (exportName === "default") {
  14102. this.raise(Errors.DuplicateDefaultExport, node);
  14103. } else {
  14104. this.raise(Errors.DuplicateExport, node, {
  14105. exportName
  14106. });
  14107. }
  14108. }
  14109. this.exportedIdentifiers.add(exportName);
  14110. }
  14111. parseExportSpecifiers(isInTypeExport) {
  14112. const nodes = [];
  14113. let first = true;
  14114. this.expect(5);
  14115. while (!this.eat(8)) {
  14116. if (first) {
  14117. first = false;
  14118. } else {
  14119. this.expect(12);
  14120. if (this.eat(8)) break;
  14121. }
  14122. const isMaybeTypeOnly = this.isContextual(130);
  14123. const isString = this.match(134);
  14124. const node = this.startNode();
  14125. node.local = this.parseModuleExportName();
  14126. nodes.push(this.parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly));
  14127. }
  14128. return nodes;
  14129. }
  14130. parseExportSpecifier(node, isString, isInTypeExport, isMaybeTypeOnly) {
  14131. if (this.eatContextual(93)) {
  14132. node.exported = this.parseModuleExportName();
  14133. } else if (isString) {
  14134. node.exported = this.cloneStringLiteral(node.local);
  14135. } else if (!node.exported) {
  14136. node.exported = this.cloneIdentifier(node.local);
  14137. }
  14138. return this.finishNode(node, "ExportSpecifier");
  14139. }
  14140. parseModuleExportName() {
  14141. if (this.match(134)) {
  14142. const result = this.parseStringLiteral(this.state.value);
  14143. const surrogate = loneSurrogate.exec(result.value);
  14144. if (surrogate) {
  14145. this.raise(Errors.ModuleExportNameHasLoneSurrogate, result, {
  14146. surrogateCharCode: surrogate[0].charCodeAt(0)
  14147. });
  14148. }
  14149. return result;
  14150. }
  14151. return this.parseIdentifier(true);
  14152. }
  14153. isJSONModuleImport(node) {
  14154. if (node.assertions != null) {
  14155. return node.assertions.some(({
  14156. key,
  14157. value
  14158. }) => {
  14159. return value.value === "json" && (key.type === "Identifier" ? key.name === "type" : key.value === "type");
  14160. });
  14161. }
  14162. return false;
  14163. }
  14164. checkImportReflection(node) {
  14165. const {
  14166. specifiers
  14167. } = node;
  14168. const singleBindingType = specifiers.length === 1 ? specifiers[0].type : null;
  14169. if (node.phase === "source") {
  14170. if (singleBindingType !== "ImportDefaultSpecifier") {
  14171. this.raise(Errors.SourcePhaseImportRequiresDefault, specifiers[0].loc.start);
  14172. }
  14173. } else if (node.phase === "defer") {
  14174. if (singleBindingType !== "ImportNamespaceSpecifier") {
  14175. this.raise(Errors.DeferImportRequiresNamespace, specifiers[0].loc.start);
  14176. }
  14177. } else if (node.module) {
  14178. var _node$assertions;
  14179. if (singleBindingType !== "ImportDefaultSpecifier") {
  14180. this.raise(Errors.ImportReflectionNotBinding, specifiers[0].loc.start);
  14181. }
  14182. if (((_node$assertions = node.assertions) == null ? void 0 : _node$assertions.length) > 0) {
  14183. this.raise(Errors.ImportReflectionHasAssertion, specifiers[0].loc.start);
  14184. }
  14185. }
  14186. }
  14187. checkJSONModuleImport(node) {
  14188. if (this.isJSONModuleImport(node) && node.type !== "ExportAllDeclaration") {
  14189. const {
  14190. specifiers
  14191. } = node;
  14192. if (specifiers != null) {
  14193. const nonDefaultNamedSpecifier = specifiers.find(specifier => {
  14194. let imported;
  14195. if (specifier.type === "ExportSpecifier") {
  14196. imported = specifier.local;
  14197. } else if (specifier.type === "ImportSpecifier") {
  14198. imported = specifier.imported;
  14199. }
  14200. if (imported !== undefined) {
  14201. return imported.type === "Identifier" ? imported.name !== "default" : imported.value !== "default";
  14202. }
  14203. });
  14204. if (nonDefaultNamedSpecifier !== undefined) {
  14205. this.raise(Errors.ImportJSONBindingNotDefault, nonDefaultNamedSpecifier.loc.start);
  14206. }
  14207. }
  14208. }
  14209. }
  14210. isPotentialImportPhase(isExport) {
  14211. if (isExport) return false;
  14212. return this.isContextual(105) || this.isContextual(97) || this.isContextual(127);
  14213. }
  14214. applyImportPhase(node, isExport, phase, loc) {
  14215. if (isExport) {
  14216. return;
  14217. }
  14218. if (phase === "module") {
  14219. this.expectPlugin("importReflection", loc);
  14220. node.module = true;
  14221. } else if (this.hasPlugin("importReflection")) {
  14222. node.module = false;
  14223. }
  14224. if (phase === "source") {
  14225. this.expectPlugin("sourcePhaseImports", loc);
  14226. node.phase = "source";
  14227. } else if (phase === "defer") {
  14228. this.expectPlugin("deferredImportEvaluation", loc);
  14229. node.phase = "defer";
  14230. } else if (this.hasPlugin("sourcePhaseImports")) {
  14231. node.phase = null;
  14232. }
  14233. }
  14234. parseMaybeImportPhase(node, isExport) {
  14235. if (!this.isPotentialImportPhase(isExport)) {
  14236. this.applyImportPhase(node, isExport, null);
  14237. return null;
  14238. }
  14239. const phaseIdentifier = this.startNode();
  14240. const phaseIdentifierName = this.parseIdentifierName(true);
  14241. const {
  14242. type
  14243. } = this.state;
  14244. const isImportPhase = tokenIsKeywordOrIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  14245. if (isImportPhase) {
  14246. this.applyImportPhase(node, isExport, phaseIdentifierName, phaseIdentifier.loc.start);
  14247. return null;
  14248. } else {
  14249. this.applyImportPhase(node, isExport, null);
  14250. return this.createIdentifier(phaseIdentifier, phaseIdentifierName);
  14251. }
  14252. }
  14253. isPrecedingIdImportPhase(phase) {
  14254. const {
  14255. type
  14256. } = this.state;
  14257. return tokenIsIdentifier(type) ? type !== 98 || this.lookaheadCharCode() === 102 : type !== 12;
  14258. }
  14259. parseImport(node) {
  14260. if (this.match(134)) {
  14261. return this.parseImportSourceAndAttributes(node);
  14262. }
  14263. return this.parseImportSpecifiersAndAfter(node, this.parseMaybeImportPhase(node, false));
  14264. }
  14265. parseImportSpecifiersAndAfter(node, maybeDefaultIdentifier) {
  14266. node.specifiers = [];
  14267. const hasDefault = this.maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier);
  14268. const parseNext = !hasDefault || this.eat(12);
  14269. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  14270. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  14271. this.expectContextual(98);
  14272. return this.parseImportSourceAndAttributes(node);
  14273. }
  14274. parseImportSourceAndAttributes(node) {
  14275. var _node$specifiers2;
  14276. (_node$specifiers2 = node.specifiers) != null ? _node$specifiers2 : node.specifiers = [];
  14277. node.source = this.parseImportSource();
  14278. this.maybeParseImportAttributes(node);
  14279. this.checkImportReflection(node);
  14280. this.checkJSONModuleImport(node);
  14281. this.semicolon();
  14282. this.sawUnambiguousESM = true;
  14283. return this.finishNode(node, "ImportDeclaration");
  14284. }
  14285. parseImportSource() {
  14286. if (!this.match(134)) this.unexpected();
  14287. return this.parseExprAtom();
  14288. }
  14289. parseImportSpecifierLocal(node, specifier, type) {
  14290. specifier.local = this.parseIdentifier();
  14291. node.specifiers.push(this.finishImportSpecifier(specifier, type));
  14292. }
  14293. finishImportSpecifier(specifier, type, bindingType = 8201) {
  14294. this.checkLVal(specifier.local, {
  14295. type
  14296. }, bindingType);
  14297. return this.finishNode(specifier, type);
  14298. }
  14299. parseImportAttributes() {
  14300. this.expect(5);
  14301. const attrs = [];
  14302. const attrNames = new Set();
  14303. do {
  14304. if (this.match(8)) {
  14305. break;
  14306. }
  14307. const node = this.startNode();
  14308. const keyName = this.state.value;
  14309. if (attrNames.has(keyName)) {
  14310. this.raise(Errors.ModuleAttributesWithDuplicateKeys, this.state.startLoc, {
  14311. key: keyName
  14312. });
  14313. }
  14314. attrNames.add(keyName);
  14315. if (this.match(134)) {
  14316. node.key = this.parseStringLiteral(keyName);
  14317. } else {
  14318. node.key = this.parseIdentifier(true);
  14319. }
  14320. this.expect(14);
  14321. if (!this.match(134)) {
  14322. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  14323. }
  14324. node.value = this.parseStringLiteral(this.state.value);
  14325. attrs.push(this.finishNode(node, "ImportAttribute"));
  14326. } while (this.eat(12));
  14327. this.expect(8);
  14328. return attrs;
  14329. }
  14330. parseModuleAttributes() {
  14331. const attrs = [];
  14332. const attributes = new Set();
  14333. do {
  14334. const node = this.startNode();
  14335. node.key = this.parseIdentifier(true);
  14336. if (node.key.name !== "type") {
  14337. this.raise(Errors.ModuleAttributeDifferentFromType, node.key);
  14338. }
  14339. if (attributes.has(node.key.name)) {
  14340. this.raise(Errors.ModuleAttributesWithDuplicateKeys, node.key, {
  14341. key: node.key.name
  14342. });
  14343. }
  14344. attributes.add(node.key.name);
  14345. this.expect(14);
  14346. if (!this.match(134)) {
  14347. throw this.raise(Errors.ModuleAttributeInvalidValue, this.state.startLoc);
  14348. }
  14349. node.value = this.parseStringLiteral(this.state.value);
  14350. attrs.push(this.finishNode(node, "ImportAttribute"));
  14351. } while (this.eat(12));
  14352. return attrs;
  14353. }
  14354. maybeParseImportAttributes(node) {
  14355. let attributes;
  14356. var useWith = false;
  14357. if (this.match(76)) {
  14358. if (this.hasPrecedingLineBreak() && this.lookaheadCharCode() === 40) {
  14359. return;
  14360. }
  14361. this.next();
  14362. if (this.hasPlugin("moduleAttributes")) {
  14363. attributes = this.parseModuleAttributes();
  14364. this.addExtra(node, "deprecatedWithLegacySyntax", true);
  14365. } else {
  14366. attributes = this.parseImportAttributes();
  14367. }
  14368. useWith = true;
  14369. } else if (this.isContextual(94) && !this.hasPrecedingLineBreak()) {
  14370. if (!this.hasPlugin("deprecatedImportAssert") && !this.hasPlugin("importAssertions")) {
  14371. this.raise(Errors.ImportAttributesUseAssert, this.state.startLoc);
  14372. }
  14373. if (!this.hasPlugin("importAssertions")) {
  14374. this.addExtra(node, "deprecatedAssertSyntax", true);
  14375. }
  14376. this.next();
  14377. attributes = this.parseImportAttributes();
  14378. } else {
  14379. attributes = [];
  14380. }
  14381. if (!useWith && this.hasPlugin("importAssertions")) {
  14382. node.assertions = attributes;
  14383. } else {
  14384. node.attributes = attributes;
  14385. }
  14386. }
  14387. maybeParseDefaultImportSpecifier(node, maybeDefaultIdentifier) {
  14388. if (maybeDefaultIdentifier) {
  14389. const specifier = this.startNodeAtNode(maybeDefaultIdentifier);
  14390. specifier.local = maybeDefaultIdentifier;
  14391. node.specifiers.push(this.finishImportSpecifier(specifier, "ImportDefaultSpecifier"));
  14392. return true;
  14393. } else if (tokenIsKeywordOrIdentifier(this.state.type)) {
  14394. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier");
  14395. return true;
  14396. }
  14397. return false;
  14398. }
  14399. maybeParseStarImportSpecifier(node) {
  14400. if (this.match(55)) {
  14401. const specifier = this.startNode();
  14402. this.next();
  14403. this.expectContextual(93);
  14404. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier");
  14405. return true;
  14406. }
  14407. return false;
  14408. }
  14409. parseNamedImportSpecifiers(node) {
  14410. let first = true;
  14411. this.expect(5);
  14412. while (!this.eat(8)) {
  14413. if (first) {
  14414. first = false;
  14415. } else {
  14416. if (this.eat(14)) {
  14417. throw this.raise(Errors.DestructureNamedImport, this.state.startLoc);
  14418. }
  14419. this.expect(12);
  14420. if (this.eat(8)) break;
  14421. }
  14422. const specifier = this.startNode();
  14423. const importedIsString = this.match(134);
  14424. const isMaybeTypeOnly = this.isContextual(130);
  14425. specifier.imported = this.parseModuleExportName();
  14426. const importSpecifier = this.parseImportSpecifier(specifier, importedIsString, node.importKind === "type" || node.importKind === "typeof", isMaybeTypeOnly, undefined);
  14427. node.specifiers.push(importSpecifier);
  14428. }
  14429. }
  14430. parseImportSpecifier(specifier, importedIsString, isInTypeOnlyImport, isMaybeTypeOnly, bindingType) {
  14431. if (this.eatContextual(93)) {
  14432. specifier.local = this.parseIdentifier();
  14433. } else {
  14434. const {
  14435. imported
  14436. } = specifier;
  14437. if (importedIsString) {
  14438. throw this.raise(Errors.ImportBindingIsString, specifier, {
  14439. importName: imported.value
  14440. });
  14441. }
  14442. this.checkReservedWord(imported.name, specifier.loc.start, true, true);
  14443. if (!specifier.local) {
  14444. specifier.local = this.cloneIdentifier(imported);
  14445. }
  14446. }
  14447. return this.finishImportSpecifier(specifier, "ImportSpecifier", bindingType);
  14448. }
  14449. isThisParam(param) {
  14450. return param.type === "Identifier" && param.name === "this";
  14451. }
  14452. }
  14453. class Parser extends StatementParser {
  14454. constructor(options, input, pluginsMap) {
  14455. const normalizedOptions = getOptions(options);
  14456. super(normalizedOptions, input);
  14457. this.options = normalizedOptions;
  14458. this.initializeScopes();
  14459. this.plugins = pluginsMap;
  14460. this.filename = normalizedOptions.sourceFilename;
  14461. this.startIndex = normalizedOptions.startIndex;
  14462. let optionFlags = 0;
  14463. if (normalizedOptions.allowAwaitOutsideFunction) {
  14464. optionFlags |= 1;
  14465. }
  14466. if (normalizedOptions.allowReturnOutsideFunction) {
  14467. optionFlags |= 2;
  14468. }
  14469. if (normalizedOptions.allowImportExportEverywhere) {
  14470. optionFlags |= 8;
  14471. }
  14472. if (normalizedOptions.allowSuperOutsideMethod) {
  14473. optionFlags |= 16;
  14474. }
  14475. if (normalizedOptions.allowUndeclaredExports) {
  14476. optionFlags |= 64;
  14477. }
  14478. if (normalizedOptions.allowNewTargetOutsideFunction) {
  14479. optionFlags |= 4;
  14480. }
  14481. if (normalizedOptions.allowYieldOutsideFunction) {
  14482. optionFlags |= 32;
  14483. }
  14484. if (normalizedOptions.ranges) {
  14485. optionFlags |= 128;
  14486. }
  14487. if (normalizedOptions.tokens) {
  14488. optionFlags |= 256;
  14489. }
  14490. if (normalizedOptions.createImportExpressions) {
  14491. optionFlags |= 512;
  14492. }
  14493. if (normalizedOptions.createParenthesizedExpressions) {
  14494. optionFlags |= 1024;
  14495. }
  14496. if (normalizedOptions.errorRecovery) {
  14497. optionFlags |= 2048;
  14498. }
  14499. if (normalizedOptions.attachComment) {
  14500. optionFlags |= 4096;
  14501. }
  14502. if (normalizedOptions.annexB) {
  14503. optionFlags |= 8192;
  14504. }
  14505. this.optionFlags = optionFlags;
  14506. }
  14507. getScopeHandler() {
  14508. return ScopeHandler;
  14509. }
  14510. parse() {
  14511. this.enterInitialScopes();
  14512. const file = this.startNode();
  14513. const program = this.startNode();
  14514. this.nextToken();
  14515. file.errors = null;
  14516. const result = this.parseTopLevel(file, program);
  14517. result.errors = this.state.errors;
  14518. result.comments.length = this.state.commentsLen;
  14519. return result;
  14520. }
  14521. }
  14522. function parse(input, options) {
  14523. var _options;
  14524. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  14525. options = Object.assign({}, options);
  14526. try {
  14527. options.sourceType = "module";
  14528. const parser = getParser(options, input);
  14529. const ast = parser.parse();
  14530. if (parser.sawUnambiguousESM) {
  14531. return ast;
  14532. }
  14533. if (parser.ambiguousScriptDifferentAst) {
  14534. try {
  14535. options.sourceType = "script";
  14536. return getParser(options, input).parse();
  14537. } catch (_unused) {}
  14538. } else {
  14539. ast.program.sourceType = "script";
  14540. }
  14541. return ast;
  14542. } catch (moduleError) {
  14543. try {
  14544. options.sourceType = "script";
  14545. return getParser(options, input).parse();
  14546. } catch (_unused2) {}
  14547. throw moduleError;
  14548. }
  14549. } else {
  14550. return getParser(options, input).parse();
  14551. }
  14552. }
  14553. function parseExpression(input, options) {
  14554. const parser = getParser(options, input);
  14555. if (parser.options.strictMode) {
  14556. parser.state.strict = true;
  14557. }
  14558. return parser.getExpression();
  14559. }
  14560. function generateExportedTokenTypes(internalTokenTypes) {
  14561. const tokenTypes = {};
  14562. for (const typeName of Object.keys(internalTokenTypes)) {
  14563. tokenTypes[typeName] = getExportedToken(internalTokenTypes[typeName]);
  14564. }
  14565. return tokenTypes;
  14566. }
  14567. const tokTypes = generateExportedTokenTypes(tt);
  14568. function getParser(options, input) {
  14569. let cls = Parser;
  14570. const pluginsMap = new Map();
  14571. if (options != null && options.plugins) {
  14572. for (const plugin of options.plugins) {
  14573. let name, opts;
  14574. if (typeof plugin === "string") {
  14575. name = plugin;
  14576. } else {
  14577. [name, opts] = plugin;
  14578. }
  14579. if (!pluginsMap.has(name)) {
  14580. pluginsMap.set(name, opts || {});
  14581. }
  14582. }
  14583. validatePlugins(pluginsMap);
  14584. cls = getParserClass(pluginsMap);
  14585. }
  14586. return new cls(options, input, pluginsMap);
  14587. }
  14588. const parserClassCache = new Map();
  14589. function getParserClass(pluginsMap) {
  14590. const pluginList = [];
  14591. for (const name of mixinPluginNames) {
  14592. if (pluginsMap.has(name)) {
  14593. pluginList.push(name);
  14594. }
  14595. }
  14596. const key = pluginList.join("|");
  14597. let cls = parserClassCache.get(key);
  14598. if (!cls) {
  14599. cls = Parser;
  14600. for (const plugin of pluginList) {
  14601. cls = mixinPlugins[plugin](cls);
  14602. }
  14603. parserClassCache.set(key, cls);
  14604. }
  14605. return cls;
  14606. }
  14607. exports.parse = parse;
  14608. exports.parseExpression = parseExpression;
  14609. exports.tokTypes = tokTypes;
  14610. //# sourceMappingURL=index.js.map