index.js 428 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. const beforeExpr = true;
  4. const startsExpr = true;
  5. const isLoop = true;
  6. const isAssign = true;
  7. const prefix = true;
  8. const postfix = true;
  9. class TokenType {
  10. constructor(label, conf = {}) {
  11. this.label = void 0;
  12. this.keyword = void 0;
  13. this.beforeExpr = void 0;
  14. this.startsExpr = void 0;
  15. this.rightAssociative = void 0;
  16. this.isLoop = void 0;
  17. this.isAssign = void 0;
  18. this.prefix = void 0;
  19. this.postfix = void 0;
  20. this.binop = void 0;
  21. this.updateContext = void 0;
  22. this.label = label;
  23. this.keyword = conf.keyword;
  24. this.beforeExpr = !!conf.beforeExpr;
  25. this.startsExpr = !!conf.startsExpr;
  26. this.rightAssociative = !!conf.rightAssociative;
  27. this.isLoop = !!conf.isLoop;
  28. this.isAssign = !!conf.isAssign;
  29. this.prefix = !!conf.prefix;
  30. this.postfix = !!conf.postfix;
  31. this.binop = conf.binop != null ? conf.binop : null;
  32. this.updateContext = null;
  33. }
  34. }
  35. const keywords$1 = new Map();
  36. function createKeyword(name, options = {}) {
  37. options.keyword = name;
  38. const token = new TokenType(name, options);
  39. keywords$1.set(name, token);
  40. return token;
  41. }
  42. function createBinop(name, binop) {
  43. return new TokenType(name, {
  44. beforeExpr,
  45. binop
  46. });
  47. }
  48. const types$1 = {
  49. num: new TokenType("num", {
  50. startsExpr
  51. }),
  52. bigint: new TokenType("bigint", {
  53. startsExpr
  54. }),
  55. decimal: new TokenType("decimal", {
  56. startsExpr
  57. }),
  58. regexp: new TokenType("regexp", {
  59. startsExpr
  60. }),
  61. string: new TokenType("string", {
  62. startsExpr
  63. }),
  64. name: new TokenType("name", {
  65. startsExpr
  66. }),
  67. privateName: new TokenType("#name", {
  68. startsExpr
  69. }),
  70. eof: new TokenType("eof"),
  71. bracketL: new TokenType("[", {
  72. beforeExpr,
  73. startsExpr
  74. }),
  75. bracketHashL: new TokenType("#[", {
  76. beforeExpr,
  77. startsExpr
  78. }),
  79. bracketBarL: new TokenType("[|", {
  80. beforeExpr,
  81. startsExpr
  82. }),
  83. bracketR: new TokenType("]"),
  84. bracketBarR: new TokenType("|]"),
  85. braceL: new TokenType("{", {
  86. beforeExpr,
  87. startsExpr
  88. }),
  89. braceBarL: new TokenType("{|", {
  90. beforeExpr,
  91. startsExpr
  92. }),
  93. braceHashL: new TokenType("#{", {
  94. beforeExpr,
  95. startsExpr
  96. }),
  97. braceR: new TokenType("}", {
  98. beforeExpr
  99. }),
  100. braceBarR: new TokenType("|}"),
  101. parenL: new TokenType("(", {
  102. beforeExpr,
  103. startsExpr
  104. }),
  105. parenR: new TokenType(")"),
  106. comma: new TokenType(",", {
  107. beforeExpr
  108. }),
  109. semi: new TokenType(";", {
  110. beforeExpr
  111. }),
  112. colon: new TokenType(":", {
  113. beforeExpr
  114. }),
  115. doubleColon: new TokenType("::", {
  116. beforeExpr
  117. }),
  118. dot: new TokenType("."),
  119. question: new TokenType("?", {
  120. beforeExpr
  121. }),
  122. questionDot: new TokenType("?."),
  123. arrow: new TokenType("=>", {
  124. beforeExpr
  125. }),
  126. template: new TokenType("template"),
  127. ellipsis: new TokenType("...", {
  128. beforeExpr
  129. }),
  130. backQuote: new TokenType("`", {
  131. startsExpr
  132. }),
  133. dollarBraceL: new TokenType("${", {
  134. beforeExpr,
  135. startsExpr
  136. }),
  137. at: new TokenType("@"),
  138. hash: new TokenType("#", {
  139. startsExpr
  140. }),
  141. interpreterDirective: new TokenType("#!..."),
  142. eq: new TokenType("=", {
  143. beforeExpr,
  144. isAssign
  145. }),
  146. assign: new TokenType("_=", {
  147. beforeExpr,
  148. isAssign
  149. }),
  150. slashAssign: new TokenType("_=", {
  151. beforeExpr,
  152. isAssign
  153. }),
  154. moduloAssign: new TokenType("_=", {
  155. beforeExpr,
  156. isAssign
  157. }),
  158. incDec: new TokenType("++/--", {
  159. prefix,
  160. postfix,
  161. startsExpr
  162. }),
  163. bang: new TokenType("!", {
  164. beforeExpr,
  165. prefix,
  166. startsExpr
  167. }),
  168. tilde: new TokenType("~", {
  169. beforeExpr,
  170. prefix,
  171. startsExpr
  172. }),
  173. pipeline: createBinop("|>", 0),
  174. nullishCoalescing: createBinop("??", 1),
  175. logicalOR: createBinop("||", 1),
  176. logicalAND: createBinop("&&", 2),
  177. bitwiseOR: createBinop("|", 3),
  178. bitwiseXOR: createBinop("^", 4),
  179. bitwiseAND: createBinop("&", 5),
  180. equality: createBinop("==/!=/===/!==", 6),
  181. relational: createBinop("</>/<=/>=", 7),
  182. bitShift: createBinop("<</>>/>>>", 8),
  183. plusMin: new TokenType("+/-", {
  184. beforeExpr,
  185. binop: 9,
  186. prefix,
  187. startsExpr
  188. }),
  189. modulo: new TokenType("%", {
  190. binop: 10,
  191. startsExpr
  192. }),
  193. star: new TokenType("*", {
  194. binop: 10
  195. }),
  196. slash: createBinop("/", 10),
  197. exponent: new TokenType("**", {
  198. beforeExpr,
  199. binop: 11,
  200. rightAssociative: true
  201. }),
  202. _break: createKeyword("break"),
  203. _case: createKeyword("case", {
  204. beforeExpr
  205. }),
  206. _catch: createKeyword("catch"),
  207. _continue: createKeyword("continue"),
  208. _debugger: createKeyword("debugger"),
  209. _default: createKeyword("default", {
  210. beforeExpr
  211. }),
  212. _do: createKeyword("do", {
  213. isLoop,
  214. beforeExpr
  215. }),
  216. _else: createKeyword("else", {
  217. beforeExpr
  218. }),
  219. _finally: createKeyword("finally"),
  220. _for: createKeyword("for", {
  221. isLoop
  222. }),
  223. _function: createKeyword("function", {
  224. startsExpr
  225. }),
  226. _if: createKeyword("if"),
  227. _return: createKeyword("return", {
  228. beforeExpr
  229. }),
  230. _switch: createKeyword("switch"),
  231. _throw: createKeyword("throw", {
  232. beforeExpr,
  233. prefix,
  234. startsExpr
  235. }),
  236. _try: createKeyword("try"),
  237. _var: createKeyword("var"),
  238. _const: createKeyword("const"),
  239. _while: createKeyword("while", {
  240. isLoop
  241. }),
  242. _with: createKeyword("with"),
  243. _new: createKeyword("new", {
  244. beforeExpr,
  245. startsExpr
  246. }),
  247. _this: createKeyword("this", {
  248. startsExpr
  249. }),
  250. _super: createKeyword("super", {
  251. startsExpr
  252. }),
  253. _class: createKeyword("class", {
  254. startsExpr
  255. }),
  256. _extends: createKeyword("extends", {
  257. beforeExpr
  258. }),
  259. _export: createKeyword("export"),
  260. _import: createKeyword("import", {
  261. startsExpr
  262. }),
  263. _null: createKeyword("null", {
  264. startsExpr
  265. }),
  266. _true: createKeyword("true", {
  267. startsExpr
  268. }),
  269. _false: createKeyword("false", {
  270. startsExpr
  271. }),
  272. _in: createKeyword("in", {
  273. beforeExpr,
  274. binop: 7
  275. }),
  276. _instanceof: createKeyword("instanceof", {
  277. beforeExpr,
  278. binop: 7
  279. }),
  280. _typeof: createKeyword("typeof", {
  281. beforeExpr,
  282. prefix,
  283. startsExpr
  284. }),
  285. _void: createKeyword("void", {
  286. beforeExpr,
  287. prefix,
  288. startsExpr
  289. }),
  290. _delete: createKeyword("delete", {
  291. beforeExpr,
  292. prefix,
  293. startsExpr
  294. })
  295. };
  296. const lineBreak = /\r\n?|[\n\u2028\u2029]/;
  297. const lineBreakG = new RegExp(lineBreak.source, "g");
  298. function isNewLine(code) {
  299. switch (code) {
  300. case 10:
  301. case 13:
  302. case 8232:
  303. case 8233:
  304. return true;
  305. default:
  306. return false;
  307. }
  308. }
  309. const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
  310. const skipWhiteSpaceInLine = /(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y;
  311. const skipWhiteSpaceToLineBreak = new RegExp("(?=(" + skipWhiteSpaceInLine.source + "))\\1" + /(?=[\n\r\u2028\u2029]|\/\*(?!.*?\*\/)|$)/.source, "y");
  312. function isWhitespace(code) {
  313. switch (code) {
  314. case 0x0009:
  315. case 0x000b:
  316. case 0x000c:
  317. case 32:
  318. case 160:
  319. case 5760:
  320. case 0x2000:
  321. case 0x2001:
  322. case 0x2002:
  323. case 0x2003:
  324. case 0x2004:
  325. case 0x2005:
  326. case 0x2006:
  327. case 0x2007:
  328. case 0x2008:
  329. case 0x2009:
  330. case 0x200a:
  331. case 0x202f:
  332. case 0x205f:
  333. case 0x3000:
  334. case 0xfeff:
  335. return true;
  336. default:
  337. return false;
  338. }
  339. }
  340. class Position {
  341. constructor(line, col) {
  342. this.line = void 0;
  343. this.column = void 0;
  344. this.line = line;
  345. this.column = col;
  346. }
  347. }
  348. class SourceLocation {
  349. constructor(start, end) {
  350. this.start = void 0;
  351. this.end = void 0;
  352. this.filename = void 0;
  353. this.identifierName = void 0;
  354. this.start = start;
  355. this.end = end;
  356. }
  357. }
  358. function getLineInfo(input, offset) {
  359. let line = 1;
  360. let lineStart = 0;
  361. let match;
  362. lineBreakG.lastIndex = 0;
  363. while ((match = lineBreakG.exec(input)) && match.index < offset) {
  364. line++;
  365. lineStart = lineBreakG.lastIndex;
  366. }
  367. return new Position(line, offset - lineStart);
  368. }
  369. class BaseParser {
  370. constructor() {
  371. this.sawUnambiguousESM = false;
  372. this.ambiguousScriptDifferentAst = false;
  373. }
  374. hasPlugin(name) {
  375. return this.plugins.has(name);
  376. }
  377. getPluginOption(plugin, name) {
  378. if (this.hasPlugin(plugin)) return this.plugins.get(plugin)[name];
  379. }
  380. }
  381. function setTrailingComments(node, comments) {
  382. if (node.trailingComments === undefined) {
  383. node.trailingComments = comments;
  384. } else {
  385. node.trailingComments.unshift(...comments);
  386. }
  387. }
  388. function setInnerComments(node, comments) {
  389. if (node.innerComments === undefined) {
  390. node.innerComments = comments;
  391. } else if (comments !== undefined) {
  392. node.innerComments.unshift(...comments);
  393. }
  394. }
  395. function adjustInnerComments(node, elements, commentWS) {
  396. let lastElement = null;
  397. let i = elements.length;
  398. while (lastElement === null && i > 0) {
  399. lastElement = elements[--i];
  400. }
  401. if (lastElement === null || lastElement.start > commentWS.start) {
  402. setInnerComments(node, commentWS.comments);
  403. } else {
  404. setTrailingComments(lastElement, commentWS.comments);
  405. }
  406. }
  407. class CommentsParser extends BaseParser {
  408. addComment(comment) {
  409. if (this.filename) comment.loc.filename = this.filename;
  410. this.state.comments.push(comment);
  411. }
  412. processComment(node) {
  413. const {
  414. commentStack
  415. } = this.state;
  416. const commentStackLength = commentStack.length;
  417. if (commentStackLength === 0) return;
  418. let i = commentStackLength - 1;
  419. const lastCommentWS = commentStack[i];
  420. if (lastCommentWS.start === node.end) {
  421. lastCommentWS.leadingNode = node;
  422. i--;
  423. }
  424. const {
  425. start: nodeStart
  426. } = node;
  427. for (; i >= 0; i--) {
  428. const commentWS = commentStack[i];
  429. const commentEnd = commentWS.end;
  430. if (commentEnd > nodeStart) {
  431. commentWS.containingNode = node;
  432. this.finalizeComment(commentWS);
  433. commentStack.splice(i, 1);
  434. } else {
  435. if (commentEnd === nodeStart) {
  436. commentWS.trailingNode = node;
  437. }
  438. break;
  439. }
  440. }
  441. }
  442. finalizeComment(commentWS) {
  443. const {
  444. comments
  445. } = commentWS;
  446. if (commentWS.leadingNode !== null || commentWS.trailingNode !== null) {
  447. if (commentWS.leadingNode !== null) {
  448. setTrailingComments(commentWS.leadingNode, comments);
  449. }
  450. if (commentWS.trailingNode !== null) {
  451. commentWS.trailingNode.leadingComments = comments;
  452. }
  453. } else {
  454. const {
  455. containingNode: node,
  456. start: commentStart
  457. } = commentWS;
  458. if (this.input.charCodeAt(commentStart - 1) === 44) {
  459. switch (node.type) {
  460. case "ObjectExpression":
  461. case "ObjectPattern":
  462. case "RecordExpression":
  463. adjustInnerComments(node, node.properties, commentWS);
  464. break;
  465. case "CallExpression":
  466. case "OptionalCallExpression":
  467. adjustInnerComments(node, node.arguments, commentWS);
  468. break;
  469. case "FunctionDeclaration":
  470. case "FunctionExpression":
  471. case "ArrowFunctionExpression":
  472. case "ObjectMethod":
  473. case "ClassMethod":
  474. case "ClassPrivateMethod":
  475. adjustInnerComments(node, node.params, commentWS);
  476. break;
  477. case "ArrayExpression":
  478. case "ArrayPattern":
  479. case "TupleExpression":
  480. adjustInnerComments(node, node.elements, commentWS);
  481. break;
  482. case "ExportNamedDeclaration":
  483. case "ImportDeclaration":
  484. adjustInnerComments(node, node.specifiers, commentWS);
  485. break;
  486. default:
  487. {
  488. setInnerComments(node, comments);
  489. }
  490. }
  491. } else {
  492. setInnerComments(node, comments);
  493. }
  494. }
  495. }
  496. finalizeRemainingComments() {
  497. const {
  498. commentStack
  499. } = this.state;
  500. for (let i = commentStack.length - 1; i >= 0; i--) {
  501. this.finalizeComment(commentStack[i]);
  502. }
  503. this.state.commentStack = [];
  504. }
  505. resetPreviousNodeTrailingComments(node) {
  506. const {
  507. commentStack
  508. } = this.state;
  509. const {
  510. length
  511. } = commentStack;
  512. if (length === 0) return;
  513. const commentWS = commentStack[length - 1];
  514. if (commentWS.leadingNode === node) {
  515. commentWS.leadingNode = null;
  516. }
  517. }
  518. }
  519. const ErrorCodes = Object.freeze({
  520. SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
  521. SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
  522. });
  523. const ErrorMessages = makeErrorTemplates({
  524. AccessorIsGenerator: "A %0ter cannot be a generator.",
  525. ArgumentsInClass: "'arguments' is only allowed in functions and class methods.",
  526. AsyncFunctionInSingleStatementContext: "Async functions can only be declared at the top level or inside a block.",
  527. AwaitBindingIdentifier: "Can not use 'await' as identifier inside an async function.",
  528. AwaitBindingIdentifierInStaticBlock: "Can not use 'await' as identifier inside a static block.",
  529. AwaitExpressionFormalParameter: "'await' is not allowed in async function parameters.",
  530. AwaitNotInAsyncContext: "'await' is only allowed within async functions and at the top levels of modules.",
  531. AwaitNotInAsyncFunction: "'await' is only allowed within async functions.",
  532. BadGetterArity: "A 'get' accesor must not have any formal parameters.",
  533. BadSetterArity: "A 'set' accesor must have exactly one formal parameter.",
  534. BadSetterRestParameter: "A 'set' accesor function argument must not be a rest parameter.",
  535. ConstructorClassField: "Classes may not have a field named 'constructor'.",
  536. ConstructorClassPrivateField: "Classes may not have a private field named '#constructor'.",
  537. ConstructorIsAccessor: "Class constructor may not be an accessor.",
  538. ConstructorIsAsync: "Constructor can't be an async function.",
  539. ConstructorIsGenerator: "Constructor can't be a generator.",
  540. DeclarationMissingInitializer: "'%0' require an initialization value.",
  541. DecoratorBeforeExport: "Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax.",
  542. DecoratorConstructor: "Decorators can't be used with a constructor. Did you mean '@dec class { ... }'?",
  543. DecoratorExportClass: "Using the export keyword between a decorator and a class is not allowed. Please use `export @dec class` instead.",
  544. DecoratorSemicolon: "Decorators must not be followed by a semicolon.",
  545. DecoratorStaticBlock: "Decorators can't be used with a static block.",
  546. DeletePrivateField: "Deleting a private field is not allowed.",
  547. DestructureNamedImport: "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
  548. DuplicateConstructor: "Duplicate constructor in the same class.",
  549. DuplicateDefaultExport: "Only one default export allowed per module.",
  550. DuplicateExport: "`%0` has already been exported. Exported identifiers must be unique.",
  551. DuplicateProto: "Redefinition of __proto__ property.",
  552. DuplicateRegExpFlags: "Duplicate regular expression flag.",
  553. ElementAfterRest: "Rest element must be last element.",
  554. EscapedCharNotAnIdentifier: "Invalid Unicode escape.",
  555. ExportBindingIsString: "A string literal cannot be used as an exported binding without `from`.\n- Did you mean `export { '%0' as '%1' } from 'some-module'`?",
  556. ExportDefaultFromAsIdentifier: "'from' is not allowed as an identifier after 'export default'.",
  557. ForInOfLoopInitializer: "'%0' loop variable declaration may not have an initializer.",
  558. ForOfAsync: "The left-hand side of a for-of loop may not be 'async'.",
  559. ForOfLet: "The left-hand side of a for-of loop may not start with 'let'.",
  560. GeneratorInSingleStatementContext: "Generators can only be declared at the top level or inside a block.",
  561. IllegalBreakContinue: "Unsyntactic %0.",
  562. IllegalLanguageModeDirective: "Illegal 'use strict' directive in function with non-simple parameter list.",
  563. IllegalReturn: "'return' outside of function.",
  564. ImportBindingIsString: 'A string literal cannot be used as an imported binding.\n- Did you mean `import { "%0" as foo }`?',
  565. ImportCallArgumentTrailingComma: "Trailing comma is disallowed inside import(...) arguments.",
  566. ImportCallArity: "`import()` requires exactly %0.",
  567. ImportCallNotNewExpression: "Cannot use new with import(...).",
  568. ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
  569. InvalidBigIntLiteral: "Invalid BigIntLiteral.",
  570. InvalidCodePoint: "Code point out of bounds.",
  571. InvalidDecimal: "Invalid decimal.",
  572. InvalidDigit: "Expected number in radix %0.",
  573. InvalidEscapeSequence: "Bad character escape sequence.",
  574. InvalidEscapeSequenceTemplate: "Invalid escape sequence in template.",
  575. InvalidEscapedReservedWord: "Escape sequence in keyword %0.",
  576. InvalidIdentifier: "Invalid identifier %0.",
  577. InvalidLhs: "Invalid left-hand side in %0.",
  578. InvalidLhsBinding: "Binding invalid left-hand side in %0.",
  579. InvalidNumber: "Invalid number.",
  580. InvalidOrMissingExponent: "Floating-point numbers require a valid exponent after the 'e'.",
  581. InvalidOrUnexpectedToken: "Unexpected character '%0'.",
  582. InvalidParenthesizedAssignment: "Invalid parenthesized assignment pattern.",
  583. InvalidPrivateFieldResolution: "Private name #%0 is not defined.",
  584. InvalidPropertyBindingPattern: "Binding member expression.",
  585. InvalidRecordProperty: "Only properties and spread elements are allowed in record definitions.",
  586. InvalidRestAssignmentPattern: "Invalid rest operator's argument.",
  587. LabelRedeclaration: "Label '%0' is already declared.",
  588. LetInLexicalBinding: "'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
  589. LineTerminatorBeforeArrow: "No line break is allowed before '=>'.",
  590. MalformedRegExpFlags: "Invalid regular expression flag.",
  591. MissingClassName: "A class name is required.",
  592. MissingEqInAssignment: "Only '=' operator can be used for specifying default value.",
  593. MissingSemicolon: "Missing semicolon.",
  594. MissingUnicodeEscape: "Expecting Unicode escape sequence \\uXXXX.",
  595. MixingCoalesceWithLogical: "Nullish coalescing operator(??) requires parens when mixing with logical operators.",
  596. ModuleAttributeDifferentFromType: "The only accepted module attribute is `type`.",
  597. ModuleAttributeInvalidValue: "Only string literals are allowed as module attribute values.",
  598. ModuleAttributesWithDuplicateKeys: 'Duplicate key "%0" is not allowed in module attributes.',
  599. ModuleExportNameHasLoneSurrogate: "An export name cannot include a lone surrogate, found '\\u%0'.",
  600. ModuleExportUndefined: "Export '%0' is not defined.",
  601. MultipleDefaultsInSwitch: "Multiple default clauses.",
  602. NewlineAfterThrow: "Illegal newline after throw.",
  603. NoCatchOrFinally: "Missing catch or finally clause.",
  604. NumberIdentifier: "Identifier directly after number.",
  605. NumericSeparatorInEscapeSequence: "Numeric separators are not allowed inside unicode escape sequences or hex escape sequences.",
  606. ObsoleteAwaitStar: "'await*' has been removed from the async functions proposal. Use Promise.all() instead.",
  607. OptionalChainingNoNew: "Constructors in/after an Optional Chain are not allowed.",
  608. OptionalChainingNoTemplate: "Tagged Template Literals are not allowed in optionalChain.",
  609. OverrideOnConstructor: "'override' modifier cannot appear on a constructor declaration.",
  610. ParamDupe: "Argument name clash.",
  611. PatternHasAccessor: "Object pattern can't contain getter or setter.",
  612. PatternHasMethod: "Object pattern can't contain methods.",
  613. PipeBodyIsTighter: "Unexpected %0 after pipeline body; any %0 expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
  614. PipeTopicRequiresHackPipes: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  615. PipeTopicUnbound: "Topic reference is unbound; it must be inside a pipe body.",
  616. PipeTopicUnconfiguredToken: 'Invalid topic token %0. In order to use %0 as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "%0" }.',
  617. PipeTopicUnused: "Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
  618. PipelineBodyNoArrow: 'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
  619. PipelineBodySequenceExpression: "Pipeline body may not be a comma-separated sequence expression.",
  620. PipelineHeadSequenceExpression: "Pipeline head should not be a comma-separated sequence expression.",
  621. PipelineTopicUnused: "Pipeline is in topic style but does not use topic reference.",
  622. PrimaryTopicNotAllowed: "Topic reference was used in a lexical context without topic binding.",
  623. PrimaryTopicRequiresSmartPipeline: 'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
  624. PrivateInExpectedIn: "Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
  625. PrivateNameRedeclaration: "Duplicate private name #%0.",
  626. RecordExpressionBarIncorrectEndSyntaxType: "Record expressions ending with '|}' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  627. RecordExpressionBarIncorrectStartSyntaxType: "Record expressions starting with '{|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  628. RecordExpressionHashIncorrectStartSyntaxType: "Record expressions starting with '#{' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  629. RecordNoProto: "'__proto__' is not allowed in Record expressions.",
  630. RestTrailingComma: "Unexpected trailing comma after rest element.",
  631. SloppyFunction: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
  632. StaticPrototype: "Classes may not have static property named prototype.",
  633. StrictDelete: "Deleting local variable in strict mode.",
  634. StrictEvalArguments: "Assigning to '%0' in strict mode.",
  635. StrictEvalArgumentsBinding: "Binding '%0' in strict mode.",
  636. StrictFunction: "In strict mode code, functions can only be declared at top level or inside a block.",
  637. StrictNumericEscape: "The only valid numeric escape in strict mode is '\\0'.",
  638. StrictOctalLiteral: "Legacy octal literals are not allowed in strict mode.",
  639. StrictWith: "'with' in strict mode.",
  640. 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?",
  641. SuperPrivateField: "Private fields can't be accessed on super.",
  642. TrailingDecorator: "Decorators must be attached to a class element.",
  643. TupleExpressionBarIncorrectEndSyntaxType: "Tuple expressions ending with '|]' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  644. TupleExpressionBarIncorrectStartSyntaxType: "Tuple expressions starting with '[|' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'bar'.",
  645. TupleExpressionHashIncorrectStartSyntaxType: "Tuple expressions starting with '#[' are only allowed when the 'syntaxType' option of the 'recordAndTuple' plugin is set to 'hash'.",
  646. UnexpectedArgumentPlaceholder: "Unexpected argument placeholder.",
  647. UnexpectedAwaitAfterPipelineBody: 'Unexpected "await" after pipeline body; await must have parentheses in minimal proposal.',
  648. UnexpectedDigitAfterHash: "Unexpected digit after hash token.",
  649. UnexpectedImportExport: "'import' and 'export' may only appear at the top level.",
  650. UnexpectedKeyword: "Unexpected keyword '%0'.",
  651. UnexpectedLeadingDecorator: "Leading decorators must be attached to a class declaration.",
  652. UnexpectedLexicalDeclaration: "Lexical declaration cannot appear in a single-statement context.",
  653. UnexpectedNewTarget: "`new.target` can only be used in functions or class properties.",
  654. UnexpectedNumericSeparator: "A numeric separator is only allowed between two digits.",
  655. UnexpectedPrivateField: "Private names can only be used as the name of a class element (i.e. class C { #p = 42; #m() {} } )\n or a property of member expression (i.e. this.#p).",
  656. UnexpectedReservedWord: "Unexpected reserved word '%0'.",
  657. UnexpectedSuper: "'super' is only allowed in object methods and classes.",
  658. UnexpectedToken: "Unexpected token '%0'.",
  659. UnexpectedTokenUnaryExponentiation: "Illegal expression. Wrap left hand side or entire exponentiation in parentheses.",
  660. UnsupportedBind: "Binding should be performed on object property.",
  661. UnsupportedDecoratorExport: "A decorated export must export a class declaration.",
  662. UnsupportedDefaultExport: "Only expressions, functions or classes are allowed as the `default` export.",
  663. UnsupportedImport: "`import` can only be used in `import()` or `import.meta`.",
  664. UnsupportedMetaProperty: "The only valid meta property for %0 is %0.%1.",
  665. UnsupportedParameterDecorator: "Decorators cannot be used to decorate parameters.",
  666. UnsupportedPropertyDecorator: "Decorators cannot be used to decorate object literal properties.",
  667. UnsupportedSuper: "'super' can only be used with function calls (i.e. super()) or in property accesses (i.e. super.prop or super[prop]).",
  668. UnterminatedComment: "Unterminated comment.",
  669. UnterminatedRegExp: "Unterminated regular expression.",
  670. UnterminatedString: "Unterminated string constant.",
  671. UnterminatedTemplate: "Unterminated template.",
  672. VarRedeclaration: "Identifier '%0' has already been declared.",
  673. YieldBindingIdentifier: "Can not use 'yield' as identifier inside a generator.",
  674. YieldInParameter: "Yield expression is not allowed in formal parameters.",
  675. ZeroDigitNumericSeparator: "Numeric separator can not be used after leading 0."
  676. }, ErrorCodes.SyntaxError);
  677. const SourceTypeModuleErrorMessages = makeErrorTemplates({
  678. ImportMetaOutsideModule: `import.meta may appear only with 'sourceType: "module"'`,
  679. ImportOutsideModule: `'import' and 'export' may appear only with 'sourceType: "module"'`
  680. }, ErrorCodes.SourceTypeModuleError);
  681. function keepReasonCodeCompat(reasonCode, syntaxPlugin) {
  682. {
  683. if (syntaxPlugin === "flow" && reasonCode === "PatternIsOptional") {
  684. return "OptionalBindingPattern";
  685. }
  686. }
  687. return reasonCode;
  688. }
  689. function makeErrorTemplates(messages, code, syntaxPlugin) {
  690. const templates = {};
  691. Object.keys(messages).forEach(reasonCode => {
  692. templates[reasonCode] = Object.freeze({
  693. code,
  694. reasonCode: keepReasonCodeCompat(reasonCode, syntaxPlugin),
  695. template: messages[reasonCode]
  696. });
  697. });
  698. return Object.freeze(templates);
  699. }
  700. class ParserError extends CommentsParser {
  701. getLocationForPosition(pos) {
  702. let loc;
  703. if (pos === this.state.start) loc = this.state.startLoc;else if (pos === this.state.lastTokStart) loc = this.state.lastTokStartLoc;else if (pos === this.state.end) loc = this.state.endLoc;else if (pos === this.state.lastTokEnd) loc = this.state.lastTokEndLoc;else loc = getLineInfo(this.input, pos);
  704. return loc;
  705. }
  706. raise(pos, {
  707. code,
  708. reasonCode,
  709. template
  710. }, ...params) {
  711. return this.raiseWithData(pos, {
  712. code,
  713. reasonCode
  714. }, template, ...params);
  715. }
  716. raiseOverwrite(pos, {
  717. code,
  718. template
  719. }, ...params) {
  720. const loc = this.getLocationForPosition(pos);
  721. const message = template.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  722. if (this.options.errorRecovery) {
  723. const errors = this.state.errors;
  724. for (let i = errors.length - 1; i >= 0; i--) {
  725. const error = errors[i];
  726. if (error.pos === pos) {
  727. return Object.assign(error, {
  728. message
  729. });
  730. } else if (error.pos < pos) {
  731. break;
  732. }
  733. }
  734. }
  735. return this._raise({
  736. code,
  737. loc,
  738. pos
  739. }, message);
  740. }
  741. raiseWithData(pos, data, errorTemplate, ...params) {
  742. const loc = this.getLocationForPosition(pos);
  743. const message = errorTemplate.replace(/%(\d+)/g, (_, i) => params[i]) + ` (${loc.line}:${loc.column})`;
  744. return this._raise(Object.assign({
  745. loc,
  746. pos
  747. }, data), message);
  748. }
  749. _raise(errorContext, message) {
  750. const err = new SyntaxError(message);
  751. Object.assign(err, errorContext);
  752. if (this.options.errorRecovery) {
  753. if (!this.isLookahead) this.state.errors.push(err);
  754. return err;
  755. } else {
  756. throw err;
  757. }
  758. }
  759. }
  760. var estree = (superClass => class extends superClass {
  761. parseRegExpLiteral({
  762. pattern,
  763. flags
  764. }) {
  765. let regex = null;
  766. try {
  767. regex = new RegExp(pattern, flags);
  768. } catch (e) {}
  769. const node = this.estreeParseLiteral(regex);
  770. node.regex = {
  771. pattern,
  772. flags
  773. };
  774. return node;
  775. }
  776. parseBigIntLiteral(value) {
  777. let bigInt;
  778. try {
  779. bigInt = BigInt(value);
  780. } catch (_unused) {
  781. bigInt = null;
  782. }
  783. const node = this.estreeParseLiteral(bigInt);
  784. node.bigint = String(node.value || value);
  785. return node;
  786. }
  787. parseDecimalLiteral(value) {
  788. const decimal = null;
  789. const node = this.estreeParseLiteral(decimal);
  790. node.decimal = String(node.value || value);
  791. return node;
  792. }
  793. estreeParseLiteral(value) {
  794. return this.parseLiteral(value, "Literal");
  795. }
  796. parseStringLiteral(value) {
  797. return this.estreeParseLiteral(value);
  798. }
  799. parseNumericLiteral(value) {
  800. return this.estreeParseLiteral(value);
  801. }
  802. parseNullLiteral() {
  803. return this.estreeParseLiteral(null);
  804. }
  805. parseBooleanLiteral(value) {
  806. return this.estreeParseLiteral(value);
  807. }
  808. directiveToStmt(directive) {
  809. const directiveLiteral = directive.value;
  810. const stmt = this.startNodeAt(directive.start, directive.loc.start);
  811. const expression = this.startNodeAt(directiveLiteral.start, directiveLiteral.loc.start);
  812. expression.value = directiveLiteral.extra.expressionValue;
  813. expression.raw = directiveLiteral.extra.raw;
  814. stmt.expression = this.finishNodeAt(expression, "Literal", directiveLiteral.end, directiveLiteral.loc.end);
  815. stmt.directive = directiveLiteral.extra.raw.slice(1, -1);
  816. return this.finishNodeAt(stmt, "ExpressionStatement", directive.end, directive.loc.end);
  817. }
  818. initFunction(node, isAsync) {
  819. super.initFunction(node, isAsync);
  820. node.expression = false;
  821. }
  822. checkDeclaration(node) {
  823. if (node != null && this.isObjectProperty(node)) {
  824. this.checkDeclaration(node.value);
  825. } else {
  826. super.checkDeclaration(node);
  827. }
  828. }
  829. getObjectOrClassMethodParams(method) {
  830. return method.value.params;
  831. }
  832. isValidDirective(stmt) {
  833. var _stmt$expression$extr;
  834. return stmt.type === "ExpressionStatement" && stmt.expression.type === "Literal" && typeof stmt.expression.value === "string" && !((_stmt$expression$extr = stmt.expression.extra) != null && _stmt$expression$extr.parenthesized);
  835. }
  836. stmtToDirective(stmt) {
  837. const value = stmt.expression.value;
  838. const directive = super.stmtToDirective(stmt);
  839. this.addExtra(directive.value, "expressionValue", value);
  840. return directive;
  841. }
  842. parseBlockBody(node, ...args) {
  843. super.parseBlockBody(node, ...args);
  844. const directiveStatements = node.directives.map(d => this.directiveToStmt(d));
  845. node.body = directiveStatements.concat(node.body);
  846. delete node.directives;
  847. }
  848. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  849. this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true);
  850. if (method.typeParameters) {
  851. method.value.typeParameters = method.typeParameters;
  852. delete method.typeParameters;
  853. }
  854. classBody.body.push(method);
  855. }
  856. parseMaybePrivateName(...args) {
  857. const node = super.parseMaybePrivateName(...args);
  858. if (node.type === "PrivateName" && this.getPluginOption("estree", "classFeatures")) {
  859. return this.convertPrivateNameToPrivateIdentifier(node);
  860. }
  861. return node;
  862. }
  863. convertPrivateNameToPrivateIdentifier(node) {
  864. const name = super.getPrivateNameSV(node);
  865. node = node;
  866. delete node.id;
  867. node.name = name;
  868. node.type = "PrivateIdentifier";
  869. return node;
  870. }
  871. isPrivateName(node) {
  872. if (!this.getPluginOption("estree", "classFeatures")) {
  873. return super.isPrivateName(node);
  874. }
  875. return node.type === "PrivateIdentifier";
  876. }
  877. getPrivateNameSV(node) {
  878. if (!this.getPluginOption("estree", "classFeatures")) {
  879. return super.getPrivateNameSV(node);
  880. }
  881. return node.name;
  882. }
  883. parseLiteral(value, type) {
  884. const node = super.parseLiteral(value, type);
  885. node.raw = node.extra.raw;
  886. delete node.extra;
  887. return node;
  888. }
  889. parseFunctionBody(node, allowExpression, isMethod = false) {
  890. super.parseFunctionBody(node, allowExpression, isMethod);
  891. node.expression = node.body.type !== "BlockStatement";
  892. }
  893. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  894. let funcNode = this.startNode();
  895. funcNode.kind = node.kind;
  896. funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope);
  897. funcNode.type = "FunctionExpression";
  898. delete funcNode.kind;
  899. node.value = funcNode;
  900. if (type === "ClassPrivateMethod") {
  901. node.computed = false;
  902. }
  903. type = "MethodDefinition";
  904. return this.finishNode(node, type);
  905. }
  906. parseClassProperty(...args) {
  907. const propertyNode = super.parseClassProperty(...args);
  908. if (this.getPluginOption("estree", "classFeatures")) {
  909. propertyNode.type = "PropertyDefinition";
  910. }
  911. return propertyNode;
  912. }
  913. parseClassPrivateProperty(...args) {
  914. const propertyNode = super.parseClassPrivateProperty(...args);
  915. if (this.getPluginOption("estree", "classFeatures")) {
  916. propertyNode.type = "PropertyDefinition";
  917. propertyNode.computed = false;
  918. }
  919. return propertyNode;
  920. }
  921. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  922. const node = super.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor);
  923. if (node) {
  924. node.type = "Property";
  925. if (node.kind === "method") node.kind = "init";
  926. node.shorthand = false;
  927. }
  928. return node;
  929. }
  930. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  931. const node = super.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  932. if (node) {
  933. node.kind = "init";
  934. node.type = "Property";
  935. }
  936. return node;
  937. }
  938. isAssignable(node, isBinding) {
  939. if (node != null && this.isObjectProperty(node)) {
  940. return this.isAssignable(node.value, isBinding);
  941. }
  942. return super.isAssignable(node, isBinding);
  943. }
  944. toAssignable(node, isLHS = false) {
  945. if (node != null && this.isObjectProperty(node)) {
  946. this.toAssignable(node.value, isLHS);
  947. return node;
  948. }
  949. return super.toAssignable(node, isLHS);
  950. }
  951. toAssignableObjectExpressionProp(prop, ...args) {
  952. if (prop.kind === "get" || prop.kind === "set") {
  953. this.raise(prop.key.start, ErrorMessages.PatternHasAccessor);
  954. } else if (prop.method) {
  955. this.raise(prop.key.start, ErrorMessages.PatternHasMethod);
  956. } else {
  957. super.toAssignableObjectExpressionProp(prop, ...args);
  958. }
  959. }
  960. finishCallExpression(node, optional) {
  961. super.finishCallExpression(node, optional);
  962. if (node.callee.type === "Import") {
  963. node.type = "ImportExpression";
  964. node.source = node.arguments[0];
  965. if (this.hasPlugin("importAssertions")) {
  966. var _node$arguments$;
  967. node.attributes = (_node$arguments$ = node.arguments[1]) != null ? _node$arguments$ : null;
  968. }
  969. delete node.arguments;
  970. delete node.callee;
  971. }
  972. return node;
  973. }
  974. toReferencedArguments(node) {
  975. if (node.type === "ImportExpression") {
  976. return;
  977. }
  978. super.toReferencedArguments(node);
  979. }
  980. parseExport(node) {
  981. super.parseExport(node);
  982. switch (node.type) {
  983. case "ExportAllDeclaration":
  984. node.exported = null;
  985. break;
  986. case "ExportNamedDeclaration":
  987. if (node.specifiers.length === 1 && node.specifiers[0].type === "ExportNamespaceSpecifier") {
  988. node.type = "ExportAllDeclaration";
  989. node.exported = node.specifiers[0].exported;
  990. delete node.specifiers;
  991. }
  992. break;
  993. }
  994. return node;
  995. }
  996. parseSubscript(base, startPos, startLoc, noCalls, state) {
  997. const node = super.parseSubscript(base, startPos, startLoc, noCalls, state);
  998. if (state.optionalChainMember) {
  999. if (node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression") {
  1000. node.type = node.type.substring(8);
  1001. }
  1002. if (state.stop) {
  1003. const chain = this.startNodeAtNode(node);
  1004. chain.expression = node;
  1005. return this.finishNode(chain, "ChainExpression");
  1006. }
  1007. } else if (node.type === "MemberExpression" || node.type === "CallExpression") {
  1008. node.optional = false;
  1009. }
  1010. return node;
  1011. }
  1012. hasPropertyAsPrivateName(node) {
  1013. if (node.type === "ChainExpression") {
  1014. node = node.expression;
  1015. }
  1016. return super.hasPropertyAsPrivateName(node);
  1017. }
  1018. isOptionalChain(node) {
  1019. return node.type === "ChainExpression";
  1020. }
  1021. isObjectProperty(node) {
  1022. return node.type === "Property" && node.kind === "init" && !node.method;
  1023. }
  1024. isObjectMethod(node) {
  1025. return node.method || node.kind === "get" || node.kind === "set";
  1026. }
  1027. });
  1028. class TokContext {
  1029. constructor(token, preserveSpace) {
  1030. this.token = void 0;
  1031. this.preserveSpace = void 0;
  1032. this.token = token;
  1033. this.preserveSpace = !!preserveSpace;
  1034. }
  1035. }
  1036. const types = {
  1037. brace: new TokContext("{"),
  1038. template: new TokContext("`", true)
  1039. };
  1040. types$1.braceR.updateContext = context => {
  1041. context.pop();
  1042. };
  1043. types$1.braceL.updateContext = types$1.braceHashL.updateContext = types$1.dollarBraceL.updateContext = context => {
  1044. context.push(types.brace);
  1045. };
  1046. types$1.backQuote.updateContext = context => {
  1047. if (context[context.length - 1] === types.template) {
  1048. context.pop();
  1049. } else {
  1050. context.push(types.template);
  1051. }
  1052. };
  1053. 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\u08a0-\u08b4\u08b6-\u08c7\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\u0c60\u0c61\u0c80\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\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-\u170c\u170e-\u1711\u1720-\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-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1c80-\u1c88\u1c90-\u1cba\u1cbd-\u1cbf\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2118-\u211d\u2124\u2126\u2128\u212a-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\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-\u9ffc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua69d\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua7bf\ua7c2-\ua7ca\ua7f5-\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";
  1054. let nonASCIIidentifierChars = "\u200c\u200d\xb7\u0300-\u036f\u0387\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u07fd\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08d3-\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\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c81-\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d00-\u0d03\u0d3b\u0d3c\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d81-\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0de6-\u0def\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1369-\u1371\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19d0-\u19da\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1ab0-\u1abd\u1abf\u1ac0\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-\u1df9\u1dfb-\u1dff\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69e\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua82c\ua880\ua881\ua8b4-\ua8c5\ua8d0-\ua8d9\ua8e0-\ua8f1\ua8ff-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\ua9e5\ua9f0-\ua9f9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b-\uaa7d\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe2f\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  1055. const nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  1056. const nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  1057. nonASCIIidentifierStartChars = nonASCIIidentifierChars = null;
  1058. 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, 157, 310, 10, 21, 11, 7, 153, 5, 3, 0, 2, 43, 2, 1, 4, 0, 3, 22, 11, 22, 10, 30, 66, 18, 2, 1, 11, 21, 11, 25, 71, 55, 7, 1, 65, 0, 16, 3, 2, 2, 2, 28, 43, 28, 4, 28, 36, 7, 2, 27, 28, 53, 11, 21, 11, 18, 14, 17, 111, 72, 56, 50, 14, 50, 14, 35, 349, 41, 7, 1, 79, 28, 11, 0, 9, 21, 107, 20, 28, 22, 13, 52, 76, 44, 33, 24, 27, 35, 30, 0, 3, 0, 9, 34, 4, 0, 13, 47, 15, 3, 22, 0, 2, 0, 36, 17, 2, 24, 85, 6, 2, 0, 2, 3, 2, 14, 2, 9, 8, 46, 39, 7, 3, 1, 3, 21, 2, 6, 2, 1, 2, 4, 4, 0, 19, 0, 13, 4, 159, 52, 19, 3, 21, 2, 31, 47, 21, 1, 2, 0, 185, 46, 42, 3, 37, 47, 21, 0, 60, 42, 14, 0, 72, 26, 230, 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, 35, 56, 264, 8, 2, 36, 18, 0, 50, 29, 113, 6, 2, 1, 2, 37, 22, 0, 26, 5, 2, 1, 2, 31, 15, 0, 328, 18, 190, 0, 80, 921, 103, 110, 18, 195, 2749, 1070, 4050, 582, 8634, 568, 8, 30, 114, 29, 19, 47, 17, 3, 32, 20, 6, 18, 689, 63, 129, 74, 6, 0, 67, 12, 65, 1, 2, 0, 29, 6135, 9, 1237, 43, 8, 8952, 286, 50, 2, 18, 3, 9, 395, 2309, 106, 6, 12, 4, 8, 8, 9, 5991, 84, 2, 70, 2, 1, 3, 0, 3, 1, 3, 3, 2, 11, 2, 0, 2, 6, 2, 64, 2, 3, 3, 7, 2, 6, 2, 27, 2, 3, 2, 4, 2, 0, 4, 6, 2, 339, 3, 24, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 30, 2, 24, 2, 7, 2357, 44, 11, 6, 17, 0, 370, 43, 1301, 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, 42717, 35, 4148, 12, 221, 3, 5761, 15, 7472, 3104, 541, 1507, 4938];
  1059. const astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 154, 10, 176, 2, 54, 14, 32, 9, 16, 3, 46, 10, 54, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 135, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 5319, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 419, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
  1060. function isInAstralSet(code, set) {
  1061. let pos = 0x10000;
  1062. for (let i = 0, length = set.length; i < length; i += 2) {
  1063. pos += set[i];
  1064. if (pos > code) return false;
  1065. pos += set[i + 1];
  1066. if (pos >= code) return true;
  1067. }
  1068. return false;
  1069. }
  1070. function isIdentifierStart(code) {
  1071. if (code < 65) return code === 36;
  1072. if (code <= 90) return true;
  1073. if (code < 97) return code === 95;
  1074. if (code <= 122) return true;
  1075. if (code <= 0xffff) {
  1076. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  1077. }
  1078. return isInAstralSet(code, astralIdentifierStartCodes);
  1079. }
  1080. function isIdentifierChar(code) {
  1081. if (code < 48) return code === 36;
  1082. if (code < 58) return true;
  1083. if (code < 65) return false;
  1084. if (code <= 90) return true;
  1085. if (code < 97) return code === 95;
  1086. if (code <= 122) return true;
  1087. if (code <= 0xffff) {
  1088. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  1089. }
  1090. return isInAstralSet(code, astralIdentifierStartCodes) || isInAstralSet(code, astralIdentifierCodes);
  1091. }
  1092. const reservedWords = {
  1093. 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"],
  1094. strict: ["implements", "interface", "let", "package", "private", "protected", "public", "static", "yield"],
  1095. strictBind: ["eval", "arguments"]
  1096. };
  1097. const keywords = new Set(reservedWords.keyword);
  1098. const reservedWordsStrictSet = new Set(reservedWords.strict);
  1099. const reservedWordsStrictBindSet = new Set(reservedWords.strictBind);
  1100. function isReservedWord(word, inModule) {
  1101. return inModule && word === "await" || word === "enum";
  1102. }
  1103. function isStrictReservedWord(word, inModule) {
  1104. return isReservedWord(word, inModule) || reservedWordsStrictSet.has(word);
  1105. }
  1106. function isStrictBindOnlyReservedWord(word) {
  1107. return reservedWordsStrictBindSet.has(word);
  1108. }
  1109. function isStrictBindReservedWord(word, inModule) {
  1110. return isStrictReservedWord(word, inModule) || isStrictBindOnlyReservedWord(word);
  1111. }
  1112. function isKeyword(word) {
  1113. return keywords.has(word);
  1114. }
  1115. function isIteratorStart(current, next) {
  1116. return current === 64 && next === 64;
  1117. }
  1118. 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"]);
  1119. function canBeReservedWord(word) {
  1120. return reservedWordLikeSet.has(word);
  1121. }
  1122. const SCOPE_OTHER = 0b000000000,
  1123. SCOPE_PROGRAM = 0b000000001,
  1124. SCOPE_FUNCTION = 0b000000010,
  1125. SCOPE_ARROW = 0b000000100,
  1126. SCOPE_SIMPLE_CATCH = 0b000001000,
  1127. SCOPE_SUPER = 0b000010000,
  1128. SCOPE_DIRECT_SUPER = 0b000100000,
  1129. SCOPE_CLASS = 0b001000000,
  1130. SCOPE_STATIC_BLOCK = 0b010000000,
  1131. SCOPE_TS_MODULE = 0b100000000,
  1132. SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;
  1133. const BIND_KIND_VALUE = 0b000000000001,
  1134. BIND_KIND_TYPE = 0b000000000010,
  1135. BIND_SCOPE_VAR = 0b000000000100,
  1136. BIND_SCOPE_LEXICAL = 0b000000001000,
  1137. BIND_SCOPE_FUNCTION = 0b000000010000,
  1138. BIND_FLAGS_NONE = 0b000001000000,
  1139. BIND_FLAGS_CLASS = 0b000010000000,
  1140. BIND_FLAGS_TS_ENUM = 0b000100000000,
  1141. BIND_FLAGS_TS_CONST_ENUM = 0b001000000000,
  1142. BIND_FLAGS_TS_EXPORT_ONLY = 0b010000000000,
  1143. BIND_FLAGS_FLOW_DECLARE_FN = 0b100000000000;
  1144. const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS,
  1145. BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0,
  1146. BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0,
  1147. BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0,
  1148. BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS,
  1149. BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0,
  1150. BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM,
  1151. BIND_TS_AMBIENT = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1152. BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE,
  1153. BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE,
  1154. BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM,
  1155. BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY,
  1156. BIND_FLOW_DECLARE_FN = BIND_FLAGS_FLOW_DECLARE_FN;
  1157. const CLASS_ELEMENT_FLAG_STATIC = 0b100,
  1158. CLASS_ELEMENT_KIND_GETTER = 0b010,
  1159. CLASS_ELEMENT_KIND_SETTER = 0b001,
  1160. CLASS_ELEMENT_KIND_ACCESSOR = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_KIND_SETTER;
  1161. const CLASS_ELEMENT_STATIC_GETTER = CLASS_ELEMENT_KIND_GETTER | CLASS_ELEMENT_FLAG_STATIC,
  1162. CLASS_ELEMENT_STATIC_SETTER = CLASS_ELEMENT_KIND_SETTER | CLASS_ELEMENT_FLAG_STATIC,
  1163. CLASS_ELEMENT_INSTANCE_GETTER = CLASS_ELEMENT_KIND_GETTER,
  1164. CLASS_ELEMENT_INSTANCE_SETTER = CLASS_ELEMENT_KIND_SETTER,
  1165. CLASS_ELEMENT_OTHER = 0;
  1166. class Scope {
  1167. constructor(flags) {
  1168. this.var = new Set();
  1169. this.lexical = new Set();
  1170. this.functions = new Set();
  1171. this.flags = flags;
  1172. }
  1173. }
  1174. class ScopeHandler {
  1175. constructor(raise, inModule) {
  1176. this.scopeStack = [];
  1177. this.undefinedExports = new Map();
  1178. this.undefinedPrivateNames = new Map();
  1179. this.raise = raise;
  1180. this.inModule = inModule;
  1181. }
  1182. get inFunction() {
  1183. return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
  1184. }
  1185. get allowSuper() {
  1186. return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
  1187. }
  1188. get allowDirectSuper() {
  1189. return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
  1190. }
  1191. get inClass() {
  1192. return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
  1193. }
  1194. get inClassAndNotInNonArrowFunction() {
  1195. const flags = this.currentThisScopeFlags();
  1196. return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
  1197. }
  1198. get inStaticBlock() {
  1199. for (let i = this.scopeStack.length - 1;; i--) {
  1200. const {
  1201. flags
  1202. } = this.scopeStack[i];
  1203. if (flags & SCOPE_STATIC_BLOCK) {
  1204. return true;
  1205. }
  1206. if (flags & (SCOPE_VAR | SCOPE_CLASS)) {
  1207. return false;
  1208. }
  1209. }
  1210. }
  1211. get inNonArrowFunction() {
  1212. return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
  1213. }
  1214. get treatFunctionsAsVar() {
  1215. return this.treatFunctionsAsVarInScope(this.currentScope());
  1216. }
  1217. createScope(flags) {
  1218. return new Scope(flags);
  1219. }
  1220. enter(flags) {
  1221. this.scopeStack.push(this.createScope(flags));
  1222. }
  1223. exit() {
  1224. this.scopeStack.pop();
  1225. }
  1226. treatFunctionsAsVarInScope(scope) {
  1227. return !!(scope.flags & SCOPE_FUNCTION || !this.inModule && scope.flags & SCOPE_PROGRAM);
  1228. }
  1229. declareName(name, bindingType, pos) {
  1230. let scope = this.currentScope();
  1231. if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) {
  1232. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1233. if (bindingType & BIND_SCOPE_FUNCTION) {
  1234. scope.functions.add(name);
  1235. } else {
  1236. scope.lexical.add(name);
  1237. }
  1238. if (bindingType & BIND_SCOPE_LEXICAL) {
  1239. this.maybeExportDefined(scope, name);
  1240. }
  1241. } else if (bindingType & BIND_SCOPE_VAR) {
  1242. for (let i = this.scopeStack.length - 1; i >= 0; --i) {
  1243. scope = this.scopeStack[i];
  1244. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1245. scope.var.add(name);
  1246. this.maybeExportDefined(scope, name);
  1247. if (scope.flags & SCOPE_VAR) break;
  1248. }
  1249. }
  1250. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1251. this.undefinedExports.delete(name);
  1252. }
  1253. }
  1254. maybeExportDefined(scope, name) {
  1255. if (this.inModule && scope.flags & SCOPE_PROGRAM) {
  1256. this.undefinedExports.delete(name);
  1257. }
  1258. }
  1259. checkRedeclarationInScope(scope, name, bindingType, pos) {
  1260. if (this.isRedeclaredInScope(scope, name, bindingType)) {
  1261. this.raise(pos, ErrorMessages.VarRedeclaration, name);
  1262. }
  1263. }
  1264. isRedeclaredInScope(scope, name, bindingType) {
  1265. if (!(bindingType & BIND_KIND_VALUE)) return false;
  1266. if (bindingType & BIND_SCOPE_LEXICAL) {
  1267. return scope.lexical.has(name) || scope.functions.has(name) || scope.var.has(name);
  1268. }
  1269. if (bindingType & BIND_SCOPE_FUNCTION) {
  1270. return scope.lexical.has(name) || !this.treatFunctionsAsVarInScope(scope) && scope.var.has(name);
  1271. }
  1272. return scope.lexical.has(name) && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical.values().next().value === name) || !this.treatFunctionsAsVarInScope(scope) && scope.functions.has(name);
  1273. }
  1274. checkLocalExport(id) {
  1275. const {
  1276. name
  1277. } = id;
  1278. const topLevelScope = this.scopeStack[0];
  1279. if (!topLevelScope.lexical.has(name) && !topLevelScope.var.has(name) && !topLevelScope.functions.has(name)) {
  1280. this.undefinedExports.set(name, id.start);
  1281. }
  1282. }
  1283. currentScope() {
  1284. return this.scopeStack[this.scopeStack.length - 1];
  1285. }
  1286. currentVarScopeFlags() {
  1287. for (let i = this.scopeStack.length - 1;; i--) {
  1288. const {
  1289. flags
  1290. } = this.scopeStack[i];
  1291. if (flags & SCOPE_VAR) {
  1292. return flags;
  1293. }
  1294. }
  1295. }
  1296. currentThisScopeFlags() {
  1297. for (let i = this.scopeStack.length - 1;; i--) {
  1298. const {
  1299. flags
  1300. } = this.scopeStack[i];
  1301. if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
  1302. return flags;
  1303. }
  1304. }
  1305. }
  1306. }
  1307. class FlowScope extends Scope {
  1308. constructor(...args) {
  1309. super(...args);
  1310. this.declareFunctions = new Set();
  1311. }
  1312. }
  1313. class FlowScopeHandler extends ScopeHandler {
  1314. createScope(flags) {
  1315. return new FlowScope(flags);
  1316. }
  1317. declareName(name, bindingType, pos) {
  1318. const scope = this.currentScope();
  1319. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1320. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  1321. this.maybeExportDefined(scope, name);
  1322. scope.declareFunctions.add(name);
  1323. return;
  1324. }
  1325. super.declareName(...arguments);
  1326. }
  1327. isRedeclaredInScope(scope, name, bindingType) {
  1328. if (super.isRedeclaredInScope(...arguments)) return true;
  1329. if (bindingType & BIND_FLAGS_FLOW_DECLARE_FN) {
  1330. return !scope.declareFunctions.has(name) && (scope.lexical.has(name) || scope.functions.has(name));
  1331. }
  1332. return false;
  1333. }
  1334. checkLocalExport(id) {
  1335. if (!this.scopeStack[0].declareFunctions.has(id.name)) {
  1336. super.checkLocalExport(id);
  1337. }
  1338. }
  1339. }
  1340. class State {
  1341. constructor() {
  1342. this.strict = void 0;
  1343. this.curLine = void 0;
  1344. this.startLoc = void 0;
  1345. this.endLoc = void 0;
  1346. this.errors = [];
  1347. this.potentialArrowAt = -1;
  1348. this.noArrowAt = [];
  1349. this.noArrowParamsConversionAt = [];
  1350. this.maybeInArrowParameters = false;
  1351. this.inType = false;
  1352. this.noAnonFunctionType = false;
  1353. this.inPropertyName = false;
  1354. this.hasFlowComment = false;
  1355. this.isAmbientContext = false;
  1356. this.inAbstractClass = false;
  1357. this.topicContext = {
  1358. maxNumOfResolvableTopics: 0,
  1359. maxTopicIndex: null
  1360. };
  1361. this.soloAwait = false;
  1362. this.inFSharpPipelineDirectBody = false;
  1363. this.labels = [];
  1364. this.decoratorStack = [[]];
  1365. this.comments = [];
  1366. this.commentStack = [];
  1367. this.pos = 0;
  1368. this.lineStart = 0;
  1369. this.type = types$1.eof;
  1370. this.value = null;
  1371. this.start = 0;
  1372. this.end = 0;
  1373. this.lastTokEndLoc = null;
  1374. this.lastTokStartLoc = null;
  1375. this.lastTokStart = 0;
  1376. this.lastTokEnd = 0;
  1377. this.context = [types.brace];
  1378. this.exprAllowed = true;
  1379. this.containsEsc = false;
  1380. this.strictErrors = new Map();
  1381. this.tokensLength = 0;
  1382. }
  1383. init(options) {
  1384. this.strict = options.strictMode === false ? false : options.strictMode === true ? true : options.sourceType === "module";
  1385. this.curLine = options.startLine;
  1386. this.startLoc = this.endLoc = this.curPosition();
  1387. }
  1388. curPosition() {
  1389. return new Position(this.curLine, this.pos - this.lineStart);
  1390. }
  1391. clone(skipArrays) {
  1392. const state = new State();
  1393. const keys = Object.keys(this);
  1394. for (let i = 0, length = keys.length; i < length; i++) {
  1395. const key = keys[i];
  1396. let val = this[key];
  1397. if (!skipArrays && Array.isArray(val)) {
  1398. val = val.slice();
  1399. }
  1400. state[key] = val;
  1401. }
  1402. return state;
  1403. }
  1404. }
  1405. var _isDigit = function isDigit(code) {
  1406. return code >= 48 && code <= 57;
  1407. };
  1408. const VALID_REGEX_FLAGS = new Set([103, 109, 115, 105, 121, 117, 100]);
  1409. const forbiddenNumericSeparatorSiblings = {
  1410. decBinOct: [46, 66, 69, 79, 95, 98, 101, 111],
  1411. hex: [46, 88, 95, 120]
  1412. };
  1413. const allowedNumericSeparatorSiblings = {};
  1414. allowedNumericSeparatorSiblings.bin = [48, 49];
  1415. allowedNumericSeparatorSiblings.oct = [...allowedNumericSeparatorSiblings.bin, 50, 51, 52, 53, 54, 55];
  1416. allowedNumericSeparatorSiblings.dec = [...allowedNumericSeparatorSiblings.oct, 56, 57];
  1417. allowedNumericSeparatorSiblings.hex = [...allowedNumericSeparatorSiblings.dec, 65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102];
  1418. class Token {
  1419. constructor(state) {
  1420. this.type = state.type;
  1421. this.value = state.value;
  1422. this.start = state.start;
  1423. this.end = state.end;
  1424. this.loc = new SourceLocation(state.startLoc, state.endLoc);
  1425. }
  1426. }
  1427. class Tokenizer extends ParserError {
  1428. constructor(options, input) {
  1429. super();
  1430. this.isLookahead = void 0;
  1431. this.tokens = [];
  1432. this.state = new State();
  1433. this.state.init(options);
  1434. this.input = input;
  1435. this.length = input.length;
  1436. this.isLookahead = false;
  1437. }
  1438. pushToken(token) {
  1439. this.tokens.length = this.state.tokensLength;
  1440. this.tokens.push(token);
  1441. ++this.state.tokensLength;
  1442. }
  1443. next() {
  1444. this.checkKeywordEscapes();
  1445. if (this.options.tokens) {
  1446. this.pushToken(new Token(this.state));
  1447. }
  1448. this.state.lastTokEnd = this.state.end;
  1449. this.state.lastTokStart = this.state.start;
  1450. this.state.lastTokEndLoc = this.state.endLoc;
  1451. this.state.lastTokStartLoc = this.state.startLoc;
  1452. this.nextToken();
  1453. }
  1454. eat(type) {
  1455. if (this.match(type)) {
  1456. this.next();
  1457. return true;
  1458. } else {
  1459. return false;
  1460. }
  1461. }
  1462. match(type) {
  1463. return this.state.type === type;
  1464. }
  1465. createLookaheadState(state) {
  1466. return {
  1467. pos: state.pos,
  1468. value: null,
  1469. type: state.type,
  1470. start: state.start,
  1471. end: state.end,
  1472. lastTokEnd: state.end,
  1473. context: [this.curContext()],
  1474. inType: state.inType
  1475. };
  1476. }
  1477. lookahead() {
  1478. const old = this.state;
  1479. this.state = this.createLookaheadState(old);
  1480. this.isLookahead = true;
  1481. this.nextToken();
  1482. this.isLookahead = false;
  1483. const curr = this.state;
  1484. this.state = old;
  1485. return curr;
  1486. }
  1487. nextTokenStart() {
  1488. return this.nextTokenStartSince(this.state.pos);
  1489. }
  1490. nextTokenStartSince(pos) {
  1491. skipWhiteSpace.lastIndex = pos;
  1492. return skipWhiteSpace.test(this.input) ? skipWhiteSpace.lastIndex : pos;
  1493. }
  1494. lookaheadCharCode() {
  1495. return this.input.charCodeAt(this.nextTokenStart());
  1496. }
  1497. codePointAtPos(pos) {
  1498. let cp = this.input.charCodeAt(pos);
  1499. if ((cp & 0xfc00) === 0xd800 && ++pos < this.input.length) {
  1500. const trail = this.input.charCodeAt(pos);
  1501. if ((trail & 0xfc00) === 0xdc00) {
  1502. cp = 0x10000 + ((cp & 0x3ff) << 10) + (trail & 0x3ff);
  1503. }
  1504. }
  1505. return cp;
  1506. }
  1507. setStrict(strict) {
  1508. this.state.strict = strict;
  1509. if (strict) {
  1510. this.state.strictErrors.forEach((message, pos) => this.raise(pos, message));
  1511. this.state.strictErrors.clear();
  1512. }
  1513. }
  1514. curContext() {
  1515. return this.state.context[this.state.context.length - 1];
  1516. }
  1517. nextToken() {
  1518. const curContext = this.curContext();
  1519. if (!curContext.preserveSpace) this.skipSpace();
  1520. this.state.start = this.state.pos;
  1521. if (!this.isLookahead) this.state.startLoc = this.state.curPosition();
  1522. if (this.state.pos >= this.length) {
  1523. this.finishToken(types$1.eof);
  1524. return;
  1525. }
  1526. if (curContext === types.template) {
  1527. this.readTmplToken();
  1528. } else {
  1529. this.getTokenFromCode(this.codePointAtPos(this.state.pos));
  1530. }
  1531. }
  1532. skipBlockComment() {
  1533. let startLoc;
  1534. if (!this.isLookahead) startLoc = this.state.curPosition();
  1535. const start = this.state.pos;
  1536. const end = this.input.indexOf("*/", start + 2);
  1537. if (end === -1) throw this.raise(start, ErrorMessages.UnterminatedComment);
  1538. this.state.pos = end + 2;
  1539. lineBreakG.lastIndex = start + 2;
  1540. while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
  1541. ++this.state.curLine;
  1542. this.state.lineStart = lineBreakG.lastIndex;
  1543. }
  1544. if (this.isLookahead) return;
  1545. const comment = {
  1546. type: "CommentBlock",
  1547. value: this.input.slice(start + 2, end),
  1548. start,
  1549. end: end + 2,
  1550. loc: new SourceLocation(startLoc, this.state.curPosition())
  1551. };
  1552. if (this.options.tokens) this.pushToken(comment);
  1553. return comment;
  1554. }
  1555. skipLineComment(startSkip) {
  1556. const start = this.state.pos;
  1557. let startLoc;
  1558. if (!this.isLookahead) startLoc = this.state.curPosition();
  1559. let ch = this.input.charCodeAt(this.state.pos += startSkip);
  1560. if (this.state.pos < this.length) {
  1561. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  1562. ch = this.input.charCodeAt(this.state.pos);
  1563. }
  1564. }
  1565. if (this.isLookahead) return;
  1566. const end = this.state.pos;
  1567. const value = this.input.slice(start + startSkip, end);
  1568. const comment = {
  1569. type: "CommentLine",
  1570. value,
  1571. start,
  1572. end,
  1573. loc: new SourceLocation(startLoc, this.state.curPosition())
  1574. };
  1575. if (this.options.tokens) this.pushToken(comment);
  1576. return comment;
  1577. }
  1578. skipSpace() {
  1579. const spaceStart = this.state.pos;
  1580. const comments = [];
  1581. loop: while (this.state.pos < this.length) {
  1582. const ch = this.input.charCodeAt(this.state.pos);
  1583. switch (ch) {
  1584. case 32:
  1585. case 160:
  1586. case 9:
  1587. ++this.state.pos;
  1588. break;
  1589. case 13:
  1590. if (this.input.charCodeAt(this.state.pos + 1) === 10) {
  1591. ++this.state.pos;
  1592. }
  1593. case 10:
  1594. case 8232:
  1595. case 8233:
  1596. ++this.state.pos;
  1597. ++this.state.curLine;
  1598. this.state.lineStart = this.state.pos;
  1599. break;
  1600. case 47:
  1601. switch (this.input.charCodeAt(this.state.pos + 1)) {
  1602. case 42:
  1603. {
  1604. const comment = this.skipBlockComment();
  1605. if (comment !== undefined) {
  1606. this.addComment(comment);
  1607. if (this.options.attachComment) comments.push(comment);
  1608. }
  1609. break;
  1610. }
  1611. case 47:
  1612. {
  1613. const comment = this.skipLineComment(2);
  1614. if (comment !== undefined) {
  1615. this.addComment(comment);
  1616. if (this.options.attachComment) comments.push(comment);
  1617. }
  1618. break;
  1619. }
  1620. default:
  1621. break loop;
  1622. }
  1623. break;
  1624. default:
  1625. if (isWhitespace(ch)) {
  1626. ++this.state.pos;
  1627. } else if (ch === 45 && !this.inModule) {
  1628. const pos = this.state.pos;
  1629. if (this.input.charCodeAt(pos + 1) === 45 && this.input.charCodeAt(pos + 2) === 62 && (spaceStart === 0 || this.state.lineStart > spaceStart)) {
  1630. const comment = this.skipLineComment(3);
  1631. if (comment !== undefined) {
  1632. this.addComment(comment);
  1633. if (this.options.attachComment) comments.push(comment);
  1634. }
  1635. } else {
  1636. break loop;
  1637. }
  1638. } else if (ch === 60 && !this.inModule) {
  1639. const pos = this.state.pos;
  1640. if (this.input.charCodeAt(pos + 1) === 33 && this.input.charCodeAt(pos + 2) === 45 && this.input.charCodeAt(pos + 3) === 45) {
  1641. const comment = this.skipLineComment(4);
  1642. if (comment !== undefined) {
  1643. this.addComment(comment);
  1644. if (this.options.attachComment) comments.push(comment);
  1645. }
  1646. } else {
  1647. break loop;
  1648. }
  1649. } else {
  1650. break loop;
  1651. }
  1652. }
  1653. }
  1654. if (comments.length > 0) {
  1655. const end = this.state.pos;
  1656. const CommentWhitespace = {
  1657. start: spaceStart,
  1658. end,
  1659. comments,
  1660. leadingNode: null,
  1661. trailingNode: null,
  1662. containingNode: null
  1663. };
  1664. this.state.commentStack.push(CommentWhitespace);
  1665. }
  1666. }
  1667. finishToken(type, val) {
  1668. this.state.end = this.state.pos;
  1669. const prevType = this.state.type;
  1670. this.state.type = type;
  1671. this.state.value = val;
  1672. if (!this.isLookahead) {
  1673. this.state.endLoc = this.state.curPosition();
  1674. this.updateContext(prevType);
  1675. }
  1676. }
  1677. readToken_numberSign() {
  1678. if (this.state.pos === 0 && this.readToken_interpreter()) {
  1679. return;
  1680. }
  1681. const nextPos = this.state.pos + 1;
  1682. const next = this.codePointAtPos(nextPos);
  1683. if (next >= 48 && next <= 57) {
  1684. throw this.raise(this.state.pos, ErrorMessages.UnexpectedDigitAfterHash);
  1685. }
  1686. if (next === 123 || next === 91 && this.hasPlugin("recordAndTuple")) {
  1687. this.expectPlugin("recordAndTuple");
  1688. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
  1689. throw this.raise(this.state.pos, next === 123 ? ErrorMessages.RecordExpressionHashIncorrectStartSyntaxType : ErrorMessages.TupleExpressionHashIncorrectStartSyntaxType);
  1690. }
  1691. this.state.pos += 2;
  1692. if (next === 123) {
  1693. this.finishToken(types$1.braceHashL);
  1694. } else {
  1695. this.finishToken(types$1.bracketHashL);
  1696. }
  1697. } else if (isIdentifierStart(next)) {
  1698. ++this.state.pos;
  1699. this.finishToken(types$1.privateName, this.readWord1(next));
  1700. } else if (next === 92) {
  1701. ++this.state.pos;
  1702. this.finishToken(types$1.privateName, this.readWord1());
  1703. } else {
  1704. this.finishOp(types$1.hash, 1);
  1705. }
  1706. }
  1707. readToken_dot() {
  1708. const next = this.input.charCodeAt(this.state.pos + 1);
  1709. if (next >= 48 && next <= 57) {
  1710. this.readNumber(true);
  1711. return;
  1712. }
  1713. if (next === 46 && this.input.charCodeAt(this.state.pos + 2) === 46) {
  1714. this.state.pos += 3;
  1715. this.finishToken(types$1.ellipsis);
  1716. } else {
  1717. ++this.state.pos;
  1718. this.finishToken(types$1.dot);
  1719. }
  1720. }
  1721. readToken_slash() {
  1722. const next = this.input.charCodeAt(this.state.pos + 1);
  1723. if (next === 61) {
  1724. this.finishOp(types$1.slashAssign, 2);
  1725. } else {
  1726. this.finishOp(types$1.slash, 1);
  1727. }
  1728. }
  1729. readToken_interpreter() {
  1730. if (this.state.pos !== 0 || this.length < 2) return false;
  1731. let ch = this.input.charCodeAt(this.state.pos + 1);
  1732. if (ch !== 33) return false;
  1733. const start = this.state.pos;
  1734. this.state.pos += 1;
  1735. while (!isNewLine(ch) && ++this.state.pos < this.length) {
  1736. ch = this.input.charCodeAt(this.state.pos);
  1737. }
  1738. const value = this.input.slice(start + 2, this.state.pos);
  1739. this.finishToken(types$1.interpreterDirective, value);
  1740. return true;
  1741. }
  1742. readToken_mult_modulo(code) {
  1743. let type = code === 42 ? types$1.star : types$1.modulo;
  1744. let width = 1;
  1745. let next = this.input.charCodeAt(this.state.pos + 1);
  1746. if (code === 42 && next === 42) {
  1747. width++;
  1748. next = this.input.charCodeAt(this.state.pos + 2);
  1749. type = types$1.exponent;
  1750. }
  1751. if (next === 61 && !this.state.inType) {
  1752. width++;
  1753. type = code === 37 ? types$1.moduloAssign : types$1.assign;
  1754. }
  1755. this.finishOp(type, width);
  1756. }
  1757. readToken_pipe_amp(code) {
  1758. const next = this.input.charCodeAt(this.state.pos + 1);
  1759. if (next === code) {
  1760. if (this.input.charCodeAt(this.state.pos + 2) === 61) {
  1761. this.finishOp(types$1.assign, 3);
  1762. } else {
  1763. this.finishOp(code === 124 ? types$1.logicalOR : types$1.logicalAND, 2);
  1764. }
  1765. return;
  1766. }
  1767. if (code === 124) {
  1768. if (next === 62) {
  1769. this.finishOp(types$1.pipeline, 2);
  1770. return;
  1771. }
  1772. if (this.hasPlugin("recordAndTuple") && next === 125) {
  1773. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  1774. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectEndSyntaxType);
  1775. }
  1776. this.state.pos += 2;
  1777. this.finishToken(types$1.braceBarR);
  1778. return;
  1779. }
  1780. if (this.hasPlugin("recordAndTuple") && next === 93) {
  1781. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  1782. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectEndSyntaxType);
  1783. }
  1784. this.state.pos += 2;
  1785. this.finishToken(types$1.bracketBarR);
  1786. return;
  1787. }
  1788. }
  1789. if (next === 61) {
  1790. this.finishOp(types$1.assign, 2);
  1791. return;
  1792. }
  1793. this.finishOp(code === 124 ? types$1.bitwiseOR : types$1.bitwiseAND, 1);
  1794. }
  1795. readToken_caret() {
  1796. const next = this.input.charCodeAt(this.state.pos + 1);
  1797. if (next === 61) {
  1798. this.finishOp(types$1.assign, 2);
  1799. } else {
  1800. this.finishOp(types$1.bitwiseXOR, 1);
  1801. }
  1802. }
  1803. readToken_plus_min(code) {
  1804. const next = this.input.charCodeAt(this.state.pos + 1);
  1805. if (next === code) {
  1806. this.finishOp(types$1.incDec, 2);
  1807. return;
  1808. }
  1809. if (next === 61) {
  1810. this.finishOp(types$1.assign, 2);
  1811. } else {
  1812. this.finishOp(types$1.plusMin, 1);
  1813. }
  1814. }
  1815. readToken_lt_gt(code) {
  1816. const next = this.input.charCodeAt(this.state.pos + 1);
  1817. let size = 1;
  1818. if (next === code) {
  1819. size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
  1820. if (this.input.charCodeAt(this.state.pos + size) === 61) {
  1821. this.finishOp(types$1.assign, size + 1);
  1822. return;
  1823. }
  1824. this.finishOp(types$1.bitShift, size);
  1825. return;
  1826. }
  1827. if (next === 61) {
  1828. size = 2;
  1829. }
  1830. this.finishOp(types$1.relational, size);
  1831. }
  1832. readToken_eq_excl(code) {
  1833. const next = this.input.charCodeAt(this.state.pos + 1);
  1834. if (next === 61) {
  1835. this.finishOp(types$1.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
  1836. return;
  1837. }
  1838. if (code === 61 && next === 62) {
  1839. this.state.pos += 2;
  1840. this.finishToken(types$1.arrow);
  1841. return;
  1842. }
  1843. this.finishOp(code === 61 ? types$1.eq : types$1.bang, 1);
  1844. }
  1845. readToken_question() {
  1846. const next = this.input.charCodeAt(this.state.pos + 1);
  1847. const next2 = this.input.charCodeAt(this.state.pos + 2);
  1848. if (next === 63) {
  1849. if (next2 === 61) {
  1850. this.finishOp(types$1.assign, 3);
  1851. } else {
  1852. this.finishOp(types$1.nullishCoalescing, 2);
  1853. }
  1854. } else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
  1855. this.state.pos += 2;
  1856. this.finishToken(types$1.questionDot);
  1857. } else {
  1858. ++this.state.pos;
  1859. this.finishToken(types$1.question);
  1860. }
  1861. }
  1862. getTokenFromCode(code) {
  1863. switch (code) {
  1864. case 46:
  1865. this.readToken_dot();
  1866. return;
  1867. case 40:
  1868. ++this.state.pos;
  1869. this.finishToken(types$1.parenL);
  1870. return;
  1871. case 41:
  1872. ++this.state.pos;
  1873. this.finishToken(types$1.parenR);
  1874. return;
  1875. case 59:
  1876. ++this.state.pos;
  1877. this.finishToken(types$1.semi);
  1878. return;
  1879. case 44:
  1880. ++this.state.pos;
  1881. this.finishToken(types$1.comma);
  1882. return;
  1883. case 91:
  1884. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  1885. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  1886. throw this.raise(this.state.pos, ErrorMessages.TupleExpressionBarIncorrectStartSyntaxType);
  1887. }
  1888. this.state.pos += 2;
  1889. this.finishToken(types$1.bracketBarL);
  1890. } else {
  1891. ++this.state.pos;
  1892. this.finishToken(types$1.bracketL);
  1893. }
  1894. return;
  1895. case 93:
  1896. ++this.state.pos;
  1897. this.finishToken(types$1.bracketR);
  1898. return;
  1899. case 123:
  1900. if (this.hasPlugin("recordAndTuple") && this.input.charCodeAt(this.state.pos + 1) === 124) {
  1901. if (this.getPluginOption("recordAndTuple", "syntaxType") !== "bar") {
  1902. throw this.raise(this.state.pos, ErrorMessages.RecordExpressionBarIncorrectStartSyntaxType);
  1903. }
  1904. this.state.pos += 2;
  1905. this.finishToken(types$1.braceBarL);
  1906. } else {
  1907. ++this.state.pos;
  1908. this.finishToken(types$1.braceL);
  1909. }
  1910. return;
  1911. case 125:
  1912. ++this.state.pos;
  1913. this.finishToken(types$1.braceR);
  1914. return;
  1915. case 58:
  1916. if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
  1917. this.finishOp(types$1.doubleColon, 2);
  1918. } else {
  1919. ++this.state.pos;
  1920. this.finishToken(types$1.colon);
  1921. }
  1922. return;
  1923. case 63:
  1924. this.readToken_question();
  1925. return;
  1926. case 96:
  1927. ++this.state.pos;
  1928. this.finishToken(types$1.backQuote);
  1929. return;
  1930. case 48:
  1931. {
  1932. const next = this.input.charCodeAt(this.state.pos + 1);
  1933. if (next === 120 || next === 88) {
  1934. this.readRadixNumber(16);
  1935. return;
  1936. }
  1937. if (next === 111 || next === 79) {
  1938. this.readRadixNumber(8);
  1939. return;
  1940. }
  1941. if (next === 98 || next === 66) {
  1942. this.readRadixNumber(2);
  1943. return;
  1944. }
  1945. }
  1946. case 49:
  1947. case 50:
  1948. case 51:
  1949. case 52:
  1950. case 53:
  1951. case 54:
  1952. case 55:
  1953. case 56:
  1954. case 57:
  1955. this.readNumber(false);
  1956. return;
  1957. case 34:
  1958. case 39:
  1959. this.readString(code);
  1960. return;
  1961. case 47:
  1962. this.readToken_slash();
  1963. return;
  1964. case 37:
  1965. case 42:
  1966. this.readToken_mult_modulo(code);
  1967. return;
  1968. case 124:
  1969. case 38:
  1970. this.readToken_pipe_amp(code);
  1971. return;
  1972. case 94:
  1973. this.readToken_caret();
  1974. return;
  1975. case 43:
  1976. case 45:
  1977. this.readToken_plus_min(code);
  1978. return;
  1979. case 60:
  1980. case 62:
  1981. this.readToken_lt_gt(code);
  1982. return;
  1983. case 61:
  1984. case 33:
  1985. this.readToken_eq_excl(code);
  1986. return;
  1987. case 126:
  1988. this.finishOp(types$1.tilde, 1);
  1989. return;
  1990. case 64:
  1991. ++this.state.pos;
  1992. this.finishToken(types$1.at);
  1993. return;
  1994. case 35:
  1995. this.readToken_numberSign();
  1996. return;
  1997. case 92:
  1998. this.readWord();
  1999. return;
  2000. default:
  2001. if (isIdentifierStart(code)) {
  2002. this.readWord(code);
  2003. return;
  2004. }
  2005. }
  2006. throw this.raise(this.state.pos, ErrorMessages.InvalidOrUnexpectedToken, String.fromCodePoint(code));
  2007. }
  2008. finishOp(type, size) {
  2009. const str = this.input.slice(this.state.pos, this.state.pos + size);
  2010. this.state.pos += size;
  2011. this.finishToken(type, str);
  2012. }
  2013. readRegexp() {
  2014. const start = this.state.start + 1;
  2015. let escaped, inClass;
  2016. let {
  2017. pos
  2018. } = this.state;
  2019. for (;; ++pos) {
  2020. if (pos >= this.length) {
  2021. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  2022. }
  2023. const ch = this.input.charCodeAt(pos);
  2024. if (isNewLine(ch)) {
  2025. throw this.raise(start, ErrorMessages.UnterminatedRegExp);
  2026. }
  2027. if (escaped) {
  2028. escaped = false;
  2029. } else {
  2030. if (ch === 91) {
  2031. inClass = true;
  2032. } else if (ch === 93 && inClass) {
  2033. inClass = false;
  2034. } else if (ch === 47 && !inClass) {
  2035. break;
  2036. }
  2037. escaped = ch === 92;
  2038. }
  2039. }
  2040. const content = this.input.slice(start, pos);
  2041. ++pos;
  2042. let mods = "";
  2043. while (pos < this.length) {
  2044. const cp = this.codePointAtPos(pos);
  2045. const char = String.fromCharCode(cp);
  2046. if (VALID_REGEX_FLAGS.has(cp)) {
  2047. if (mods.includes(char)) {
  2048. this.raise(pos + 1, ErrorMessages.DuplicateRegExpFlags);
  2049. }
  2050. } else if (isIdentifierChar(cp) || cp === 92) {
  2051. this.raise(pos + 1, ErrorMessages.MalformedRegExpFlags);
  2052. } else {
  2053. break;
  2054. }
  2055. ++pos;
  2056. mods += char;
  2057. }
  2058. this.state.pos = pos;
  2059. this.finishToken(types$1.regexp, {
  2060. pattern: content,
  2061. flags: mods
  2062. });
  2063. }
  2064. readInt(radix, len, forceLen, allowNumSeparator = true) {
  2065. const start = this.state.pos;
  2066. const forbiddenSiblings = radix === 16 ? forbiddenNumericSeparatorSiblings.hex : forbiddenNumericSeparatorSiblings.decBinOct;
  2067. const allowedSiblings = radix === 16 ? allowedNumericSeparatorSiblings.hex : radix === 10 ? allowedNumericSeparatorSiblings.dec : radix === 8 ? allowedNumericSeparatorSiblings.oct : allowedNumericSeparatorSiblings.bin;
  2068. let invalid = false;
  2069. let total = 0;
  2070. for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  2071. const code = this.input.charCodeAt(this.state.pos);
  2072. let val;
  2073. if (code === 95) {
  2074. const prev = this.input.charCodeAt(this.state.pos - 1);
  2075. const next = this.input.charCodeAt(this.state.pos + 1);
  2076. if (allowedSiblings.indexOf(next) === -1) {
  2077. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  2078. } else if (forbiddenSiblings.indexOf(prev) > -1 || forbiddenSiblings.indexOf(next) > -1 || Number.isNaN(next)) {
  2079. this.raise(this.state.pos, ErrorMessages.UnexpectedNumericSeparator);
  2080. }
  2081. if (!allowNumSeparator) {
  2082. this.raise(this.state.pos, ErrorMessages.NumericSeparatorInEscapeSequence);
  2083. }
  2084. ++this.state.pos;
  2085. continue;
  2086. }
  2087. if (code >= 97) {
  2088. val = code - 97 + 10;
  2089. } else if (code >= 65) {
  2090. val = code - 65 + 10;
  2091. } else if (_isDigit(code)) {
  2092. val = code - 48;
  2093. } else {
  2094. val = Infinity;
  2095. }
  2096. if (val >= radix) {
  2097. if (this.options.errorRecovery && val <= 9) {
  2098. val = 0;
  2099. this.raise(this.state.start + i + 2, ErrorMessages.InvalidDigit, radix);
  2100. } else if (forceLen) {
  2101. val = 0;
  2102. invalid = true;
  2103. } else {
  2104. break;
  2105. }
  2106. }
  2107. ++this.state.pos;
  2108. total = total * radix + val;
  2109. }
  2110. if (this.state.pos === start || len != null && this.state.pos - start !== len || invalid) {
  2111. return null;
  2112. }
  2113. return total;
  2114. }
  2115. readRadixNumber(radix) {
  2116. const start = this.state.pos;
  2117. let isBigInt = false;
  2118. this.state.pos += 2;
  2119. const val = this.readInt(radix);
  2120. if (val == null) {
  2121. this.raise(this.state.start + 2, ErrorMessages.InvalidDigit, radix);
  2122. }
  2123. const next = this.input.charCodeAt(this.state.pos);
  2124. if (next === 110) {
  2125. ++this.state.pos;
  2126. isBigInt = true;
  2127. } else if (next === 109) {
  2128. throw this.raise(start, ErrorMessages.InvalidDecimal);
  2129. }
  2130. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2131. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  2132. }
  2133. if (isBigInt) {
  2134. const str = this.input.slice(start, this.state.pos).replace(/[_n]/g, "");
  2135. this.finishToken(types$1.bigint, str);
  2136. return;
  2137. }
  2138. this.finishToken(types$1.num, val);
  2139. }
  2140. readNumber(startsWithDot) {
  2141. const start = this.state.pos;
  2142. let isFloat = false;
  2143. let isBigInt = false;
  2144. let isDecimal = false;
  2145. let hasExponent = false;
  2146. let isOctal = false;
  2147. if (!startsWithDot && this.readInt(10) === null) {
  2148. this.raise(start, ErrorMessages.InvalidNumber);
  2149. }
  2150. const hasLeadingZero = this.state.pos - start >= 2 && this.input.charCodeAt(start) === 48;
  2151. if (hasLeadingZero) {
  2152. const integer = this.input.slice(start, this.state.pos);
  2153. this.recordStrictModeErrors(start, ErrorMessages.StrictOctalLiteral);
  2154. if (!this.state.strict) {
  2155. const underscorePos = integer.indexOf("_");
  2156. if (underscorePos > 0) {
  2157. this.raise(underscorePos + start, ErrorMessages.ZeroDigitNumericSeparator);
  2158. }
  2159. }
  2160. isOctal = hasLeadingZero && !/[89]/.test(integer);
  2161. }
  2162. let next = this.input.charCodeAt(this.state.pos);
  2163. if (next === 46 && !isOctal) {
  2164. ++this.state.pos;
  2165. this.readInt(10);
  2166. isFloat = true;
  2167. next = this.input.charCodeAt(this.state.pos);
  2168. }
  2169. if ((next === 69 || next === 101) && !isOctal) {
  2170. next = this.input.charCodeAt(++this.state.pos);
  2171. if (next === 43 || next === 45) {
  2172. ++this.state.pos;
  2173. }
  2174. if (this.readInt(10) === null) {
  2175. this.raise(start, ErrorMessages.InvalidOrMissingExponent);
  2176. }
  2177. isFloat = true;
  2178. hasExponent = true;
  2179. next = this.input.charCodeAt(this.state.pos);
  2180. }
  2181. if (next === 110) {
  2182. if (isFloat || hasLeadingZero) {
  2183. this.raise(start, ErrorMessages.InvalidBigIntLiteral);
  2184. }
  2185. ++this.state.pos;
  2186. isBigInt = true;
  2187. }
  2188. if (next === 109) {
  2189. this.expectPlugin("decimal", this.state.pos);
  2190. if (hasExponent || hasLeadingZero) {
  2191. this.raise(start, ErrorMessages.InvalidDecimal);
  2192. }
  2193. ++this.state.pos;
  2194. isDecimal = true;
  2195. }
  2196. if (isIdentifierStart(this.codePointAtPos(this.state.pos))) {
  2197. throw this.raise(this.state.pos, ErrorMessages.NumberIdentifier);
  2198. }
  2199. const str = this.input.slice(start, this.state.pos).replace(/[_mn]/g, "");
  2200. if (isBigInt) {
  2201. this.finishToken(types$1.bigint, str);
  2202. return;
  2203. }
  2204. if (isDecimal) {
  2205. this.finishToken(types$1.decimal, str);
  2206. return;
  2207. }
  2208. const val = isOctal ? parseInt(str, 8) : parseFloat(str);
  2209. this.finishToken(types$1.num, val);
  2210. }
  2211. readCodePoint(throwOnInvalid) {
  2212. const ch = this.input.charCodeAt(this.state.pos);
  2213. let code;
  2214. if (ch === 123) {
  2215. const codePos = ++this.state.pos;
  2216. code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, true, throwOnInvalid);
  2217. ++this.state.pos;
  2218. if (code !== null && code > 0x10ffff) {
  2219. if (throwOnInvalid) {
  2220. this.raise(codePos, ErrorMessages.InvalidCodePoint);
  2221. } else {
  2222. return null;
  2223. }
  2224. }
  2225. } else {
  2226. code = this.readHexChar(4, false, throwOnInvalid);
  2227. }
  2228. return code;
  2229. }
  2230. readString(quote) {
  2231. let out = "",
  2232. chunkStart = ++this.state.pos;
  2233. for (;;) {
  2234. if (this.state.pos >= this.length) {
  2235. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  2236. }
  2237. const ch = this.input.charCodeAt(this.state.pos);
  2238. if (ch === quote) break;
  2239. if (ch === 92) {
  2240. out += this.input.slice(chunkStart, this.state.pos);
  2241. out += this.readEscapedChar(false);
  2242. chunkStart = this.state.pos;
  2243. } else if (ch === 8232 || ch === 8233) {
  2244. ++this.state.pos;
  2245. ++this.state.curLine;
  2246. this.state.lineStart = this.state.pos;
  2247. } else if (isNewLine(ch)) {
  2248. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  2249. } else {
  2250. ++this.state.pos;
  2251. }
  2252. }
  2253. out += this.input.slice(chunkStart, this.state.pos++);
  2254. this.finishToken(types$1.string, out);
  2255. }
  2256. readTmplToken() {
  2257. let out = "",
  2258. chunkStart = this.state.pos,
  2259. containsInvalid = false;
  2260. for (;;) {
  2261. if (this.state.pos >= this.length) {
  2262. throw this.raise(this.state.start, ErrorMessages.UnterminatedTemplate);
  2263. }
  2264. const ch = this.input.charCodeAt(this.state.pos);
  2265. if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) {
  2266. if (this.state.pos === this.state.start && this.match(types$1.template)) {
  2267. if (ch === 36) {
  2268. this.state.pos += 2;
  2269. this.finishToken(types$1.dollarBraceL);
  2270. return;
  2271. } else {
  2272. ++this.state.pos;
  2273. this.finishToken(types$1.backQuote);
  2274. return;
  2275. }
  2276. }
  2277. out += this.input.slice(chunkStart, this.state.pos);
  2278. this.finishToken(types$1.template, containsInvalid ? null : out);
  2279. return;
  2280. }
  2281. if (ch === 92) {
  2282. out += this.input.slice(chunkStart, this.state.pos);
  2283. const escaped = this.readEscapedChar(true);
  2284. if (escaped === null) {
  2285. containsInvalid = true;
  2286. } else {
  2287. out += escaped;
  2288. }
  2289. chunkStart = this.state.pos;
  2290. } else if (isNewLine(ch)) {
  2291. out += this.input.slice(chunkStart, this.state.pos);
  2292. ++this.state.pos;
  2293. switch (ch) {
  2294. case 13:
  2295. if (this.input.charCodeAt(this.state.pos) === 10) {
  2296. ++this.state.pos;
  2297. }
  2298. case 10:
  2299. out += "\n";
  2300. break;
  2301. default:
  2302. out += String.fromCharCode(ch);
  2303. break;
  2304. }
  2305. ++this.state.curLine;
  2306. this.state.lineStart = this.state.pos;
  2307. chunkStart = this.state.pos;
  2308. } else {
  2309. ++this.state.pos;
  2310. }
  2311. }
  2312. }
  2313. recordStrictModeErrors(pos, message) {
  2314. if (this.state.strict && !this.state.strictErrors.has(pos)) {
  2315. this.raise(pos, message);
  2316. } else {
  2317. this.state.strictErrors.set(pos, message);
  2318. }
  2319. }
  2320. readEscapedChar(inTemplate) {
  2321. const throwOnInvalid = !inTemplate;
  2322. const ch = this.input.charCodeAt(++this.state.pos);
  2323. ++this.state.pos;
  2324. switch (ch) {
  2325. case 110:
  2326. return "\n";
  2327. case 114:
  2328. return "\r";
  2329. case 120:
  2330. {
  2331. const code = this.readHexChar(2, false, throwOnInvalid);
  2332. return code === null ? null : String.fromCharCode(code);
  2333. }
  2334. case 117:
  2335. {
  2336. const code = this.readCodePoint(throwOnInvalid);
  2337. return code === null ? null : String.fromCodePoint(code);
  2338. }
  2339. case 116:
  2340. return "\t";
  2341. case 98:
  2342. return "\b";
  2343. case 118:
  2344. return "\u000b";
  2345. case 102:
  2346. return "\f";
  2347. case 13:
  2348. if (this.input.charCodeAt(this.state.pos) === 10) {
  2349. ++this.state.pos;
  2350. }
  2351. case 10:
  2352. this.state.lineStart = this.state.pos;
  2353. ++this.state.curLine;
  2354. case 8232:
  2355. case 8233:
  2356. return "";
  2357. case 56:
  2358. case 57:
  2359. if (inTemplate) {
  2360. return null;
  2361. } else {
  2362. this.recordStrictModeErrors(this.state.pos - 1, ErrorMessages.StrictNumericEscape);
  2363. }
  2364. default:
  2365. if (ch >= 48 && ch <= 55) {
  2366. const codePos = this.state.pos - 1;
  2367. const match = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/);
  2368. let octalStr = match[0];
  2369. let octal = parseInt(octalStr, 8);
  2370. if (octal > 255) {
  2371. octalStr = octalStr.slice(0, -1);
  2372. octal = parseInt(octalStr, 8);
  2373. }
  2374. this.state.pos += octalStr.length - 1;
  2375. const next = this.input.charCodeAt(this.state.pos);
  2376. if (octalStr !== "0" || next === 56 || next === 57) {
  2377. if (inTemplate) {
  2378. return null;
  2379. } else {
  2380. this.recordStrictModeErrors(codePos, ErrorMessages.StrictNumericEscape);
  2381. }
  2382. }
  2383. return String.fromCharCode(octal);
  2384. }
  2385. return String.fromCharCode(ch);
  2386. }
  2387. }
  2388. readHexChar(len, forceLen, throwOnInvalid) {
  2389. const codePos = this.state.pos;
  2390. const n = this.readInt(16, len, forceLen, false);
  2391. if (n === null) {
  2392. if (throwOnInvalid) {
  2393. this.raise(codePos, ErrorMessages.InvalidEscapeSequence);
  2394. } else {
  2395. this.state.pos = codePos - 1;
  2396. }
  2397. }
  2398. return n;
  2399. }
  2400. readWord1(firstCode) {
  2401. this.state.containsEsc = false;
  2402. let word = "";
  2403. const start = this.state.pos;
  2404. let chunkStart = this.state.pos;
  2405. if (firstCode !== undefined) {
  2406. this.state.pos += firstCode <= 0xffff ? 1 : 2;
  2407. }
  2408. while (this.state.pos < this.length) {
  2409. const ch = this.codePointAtPos(this.state.pos);
  2410. if (isIdentifierChar(ch)) {
  2411. this.state.pos += ch <= 0xffff ? 1 : 2;
  2412. } else if (ch === 92) {
  2413. this.state.containsEsc = true;
  2414. word += this.input.slice(chunkStart, this.state.pos);
  2415. const escStart = this.state.pos;
  2416. const identifierCheck = this.state.pos === start ? isIdentifierStart : isIdentifierChar;
  2417. if (this.input.charCodeAt(++this.state.pos) !== 117) {
  2418. this.raise(this.state.pos, ErrorMessages.MissingUnicodeEscape);
  2419. chunkStart = this.state.pos - 1;
  2420. continue;
  2421. }
  2422. ++this.state.pos;
  2423. const esc = this.readCodePoint(true);
  2424. if (esc !== null) {
  2425. if (!identifierCheck(esc)) {
  2426. this.raise(escStart, ErrorMessages.EscapedCharNotAnIdentifier);
  2427. }
  2428. word += String.fromCodePoint(esc);
  2429. }
  2430. chunkStart = this.state.pos;
  2431. } else {
  2432. break;
  2433. }
  2434. }
  2435. return word + this.input.slice(chunkStart, this.state.pos);
  2436. }
  2437. readWord(firstCode) {
  2438. const word = this.readWord1(firstCode);
  2439. const type = keywords$1.get(word) || types$1.name;
  2440. this.finishToken(type, word);
  2441. }
  2442. checkKeywordEscapes() {
  2443. const kw = this.state.type.keyword;
  2444. if (kw && this.state.containsEsc) {
  2445. this.raise(this.state.start, ErrorMessages.InvalidEscapedReservedWord, kw);
  2446. }
  2447. }
  2448. updateContext(prevType) {
  2449. var _this$state$type$upda, _this$state$type;
  2450. (_this$state$type$upda = (_this$state$type = this.state.type).updateContext) == null ? void 0 : _this$state$type$upda.call(_this$state$type, this.state.context);
  2451. }
  2452. }
  2453. class ClassScope {
  2454. constructor() {
  2455. this.privateNames = new Set();
  2456. this.loneAccessors = new Map();
  2457. this.undefinedPrivateNames = new Map();
  2458. }
  2459. }
  2460. class ClassScopeHandler {
  2461. constructor(raise) {
  2462. this.stack = [];
  2463. this.undefinedPrivateNames = new Map();
  2464. this.raise = raise;
  2465. }
  2466. current() {
  2467. return this.stack[this.stack.length - 1];
  2468. }
  2469. enter() {
  2470. this.stack.push(new ClassScope());
  2471. }
  2472. exit() {
  2473. const oldClassScope = this.stack.pop();
  2474. const current = this.current();
  2475. for (const [name, pos] of Array.from(oldClassScope.undefinedPrivateNames)) {
  2476. if (current) {
  2477. if (!current.undefinedPrivateNames.has(name)) {
  2478. current.undefinedPrivateNames.set(name, pos);
  2479. }
  2480. } else {
  2481. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  2482. }
  2483. }
  2484. }
  2485. declarePrivateName(name, elementType, pos) {
  2486. const classScope = this.current();
  2487. let redefined = classScope.privateNames.has(name);
  2488. if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
  2489. const accessor = redefined && classScope.loneAccessors.get(name);
  2490. if (accessor) {
  2491. const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
  2492. const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
  2493. const oldKind = accessor & CLASS_ELEMENT_KIND_ACCESSOR;
  2494. const newKind = elementType & CLASS_ELEMENT_KIND_ACCESSOR;
  2495. redefined = oldKind === newKind || oldStatic !== newStatic;
  2496. if (!redefined) classScope.loneAccessors.delete(name);
  2497. } else if (!redefined) {
  2498. classScope.loneAccessors.set(name, elementType);
  2499. }
  2500. }
  2501. if (redefined) {
  2502. this.raise(pos, ErrorMessages.PrivateNameRedeclaration, name);
  2503. }
  2504. classScope.privateNames.add(name);
  2505. classScope.undefinedPrivateNames.delete(name);
  2506. }
  2507. usePrivateName(name, pos) {
  2508. let classScope;
  2509. for (classScope of this.stack) {
  2510. if (classScope.privateNames.has(name)) return;
  2511. }
  2512. if (classScope) {
  2513. classScope.undefinedPrivateNames.set(name, pos);
  2514. } else {
  2515. this.raise(pos, ErrorMessages.InvalidPrivateFieldResolution, name);
  2516. }
  2517. }
  2518. }
  2519. const kExpression = 0,
  2520. kMaybeArrowParameterDeclaration = 1,
  2521. kMaybeAsyncArrowParameterDeclaration = 2,
  2522. kParameterDeclaration = 3;
  2523. class ExpressionScope {
  2524. constructor(type = kExpression) {
  2525. this.type = void 0;
  2526. this.type = type;
  2527. }
  2528. canBeArrowParameterDeclaration() {
  2529. return this.type === kMaybeAsyncArrowParameterDeclaration || this.type === kMaybeArrowParameterDeclaration;
  2530. }
  2531. isCertainlyParameterDeclaration() {
  2532. return this.type === kParameterDeclaration;
  2533. }
  2534. }
  2535. class ArrowHeadParsingScope extends ExpressionScope {
  2536. constructor(type) {
  2537. super(type);
  2538. this.errors = new Map();
  2539. }
  2540. recordDeclarationError(pos, template) {
  2541. this.errors.set(pos, template);
  2542. }
  2543. clearDeclarationError(pos) {
  2544. this.errors.delete(pos);
  2545. }
  2546. iterateErrors(iterator) {
  2547. this.errors.forEach(iterator);
  2548. }
  2549. }
  2550. class ExpressionScopeHandler {
  2551. constructor(raise) {
  2552. this.stack = [new ExpressionScope()];
  2553. this.raise = raise;
  2554. }
  2555. enter(scope) {
  2556. this.stack.push(scope);
  2557. }
  2558. exit() {
  2559. this.stack.pop();
  2560. }
  2561. recordParameterInitializerError(pos, template) {
  2562. const {
  2563. stack
  2564. } = this;
  2565. let i = stack.length - 1;
  2566. let scope = stack[i];
  2567. while (!scope.isCertainlyParameterDeclaration()) {
  2568. if (scope.canBeArrowParameterDeclaration()) {
  2569. scope.recordDeclarationError(pos, template);
  2570. } else {
  2571. return;
  2572. }
  2573. scope = stack[--i];
  2574. }
  2575. this.raise(pos, template);
  2576. }
  2577. recordParenthesizedIdentifierError(pos, template) {
  2578. const {
  2579. stack
  2580. } = this;
  2581. const scope = stack[stack.length - 1];
  2582. if (scope.isCertainlyParameterDeclaration()) {
  2583. this.raise(pos, template);
  2584. } else if (scope.canBeArrowParameterDeclaration()) {
  2585. scope.recordDeclarationError(pos, template);
  2586. } else {
  2587. return;
  2588. }
  2589. }
  2590. recordAsyncArrowParametersError(pos, template) {
  2591. const {
  2592. stack
  2593. } = this;
  2594. let i = stack.length - 1;
  2595. let scope = stack[i];
  2596. while (scope.canBeArrowParameterDeclaration()) {
  2597. if (scope.type === kMaybeAsyncArrowParameterDeclaration) {
  2598. scope.recordDeclarationError(pos, template);
  2599. }
  2600. scope = stack[--i];
  2601. }
  2602. }
  2603. validateAsPattern() {
  2604. const {
  2605. stack
  2606. } = this;
  2607. const currentScope = stack[stack.length - 1];
  2608. if (!currentScope.canBeArrowParameterDeclaration()) return;
  2609. currentScope.iterateErrors((template, pos) => {
  2610. this.raise(pos, template);
  2611. let i = stack.length - 2;
  2612. let scope = stack[i];
  2613. while (scope.canBeArrowParameterDeclaration()) {
  2614. scope.clearDeclarationError(pos);
  2615. scope = stack[--i];
  2616. }
  2617. });
  2618. }
  2619. }
  2620. function newParameterDeclarationScope() {
  2621. return new ExpressionScope(kParameterDeclaration);
  2622. }
  2623. function newArrowHeadScope() {
  2624. return new ArrowHeadParsingScope(kMaybeArrowParameterDeclaration);
  2625. }
  2626. function newAsyncArrowScope() {
  2627. return new ArrowHeadParsingScope(kMaybeAsyncArrowParameterDeclaration);
  2628. }
  2629. function newExpressionScope() {
  2630. return new ExpressionScope();
  2631. }
  2632. const PARAM = 0b0000,
  2633. PARAM_YIELD = 0b0001,
  2634. PARAM_AWAIT = 0b0010,
  2635. PARAM_RETURN = 0b0100,
  2636. PARAM_IN = 0b1000;
  2637. class ProductionParameterHandler {
  2638. constructor() {
  2639. this.stacks = [];
  2640. }
  2641. enter(flags) {
  2642. this.stacks.push(flags);
  2643. }
  2644. exit() {
  2645. this.stacks.pop();
  2646. }
  2647. currentFlags() {
  2648. return this.stacks[this.stacks.length - 1];
  2649. }
  2650. get hasAwait() {
  2651. return (this.currentFlags() & PARAM_AWAIT) > 0;
  2652. }
  2653. get hasYield() {
  2654. return (this.currentFlags() & PARAM_YIELD) > 0;
  2655. }
  2656. get hasReturn() {
  2657. return (this.currentFlags() & PARAM_RETURN) > 0;
  2658. }
  2659. get hasIn() {
  2660. return (this.currentFlags() & PARAM_IN) > 0;
  2661. }
  2662. }
  2663. function functionFlags(isAsync, isGenerator) {
  2664. return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
  2665. }
  2666. class UtilParser extends Tokenizer {
  2667. addExtra(node, key, val) {
  2668. if (!node) return;
  2669. const extra = node.extra = node.extra || {};
  2670. extra[key] = val;
  2671. }
  2672. isRelational(op) {
  2673. return this.match(types$1.relational) && this.state.value === op;
  2674. }
  2675. expectRelational(op) {
  2676. if (this.isRelational(op)) {
  2677. this.next();
  2678. } else {
  2679. this.unexpected(null, types$1.relational);
  2680. }
  2681. }
  2682. isContextual(name) {
  2683. return this.match(types$1.name) && this.state.value === name && !this.state.containsEsc;
  2684. }
  2685. isUnparsedContextual(nameStart, name) {
  2686. const nameEnd = nameStart + name.length;
  2687. if (this.input.slice(nameStart, nameEnd) === name) {
  2688. const nextCh = this.input.charCodeAt(nameEnd);
  2689. return !(isIdentifierChar(nextCh) || (nextCh & 0xfc00) === 0xd800);
  2690. }
  2691. return false;
  2692. }
  2693. isLookaheadContextual(name) {
  2694. const next = this.nextTokenStart();
  2695. return this.isUnparsedContextual(next, name);
  2696. }
  2697. eatContextual(name) {
  2698. return this.isContextual(name) && this.eat(types$1.name);
  2699. }
  2700. expectContextual(name, template) {
  2701. if (!this.eatContextual(name)) this.unexpected(null, template);
  2702. }
  2703. canInsertSemicolon() {
  2704. return this.match(types$1.eof) || this.match(types$1.braceR) || this.hasPrecedingLineBreak();
  2705. }
  2706. hasPrecedingLineBreak() {
  2707. return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
  2708. }
  2709. hasFollowingLineBreak() {
  2710. skipWhiteSpaceToLineBreak.lastIndex = this.state.end;
  2711. return skipWhiteSpaceToLineBreak.test(this.input);
  2712. }
  2713. isLineTerminator() {
  2714. return this.eat(types$1.semi) || this.canInsertSemicolon();
  2715. }
  2716. semicolon(allowAsi = true) {
  2717. if (allowAsi ? this.isLineTerminator() : this.eat(types$1.semi)) return;
  2718. this.raise(this.state.lastTokEnd, ErrorMessages.MissingSemicolon);
  2719. }
  2720. expect(type, pos) {
  2721. this.eat(type) || this.unexpected(pos, type);
  2722. }
  2723. assertNoSpace(message = "Unexpected space.") {
  2724. if (this.state.start > this.state.lastTokEnd) {
  2725. this.raise(this.state.lastTokEnd, {
  2726. code: ErrorCodes.SyntaxError,
  2727. reasonCode: "UnexpectedSpace",
  2728. template: message
  2729. });
  2730. }
  2731. }
  2732. unexpected(pos, messageOrType = {
  2733. code: ErrorCodes.SyntaxError,
  2734. reasonCode: "UnexpectedToken",
  2735. template: "Unexpected token"
  2736. }) {
  2737. if (messageOrType instanceof TokenType) {
  2738. messageOrType = {
  2739. code: ErrorCodes.SyntaxError,
  2740. reasonCode: "UnexpectedToken",
  2741. template: `Unexpected token, expected "${messageOrType.label}"`
  2742. };
  2743. }
  2744. throw this.raise(pos != null ? pos : this.state.start, messageOrType);
  2745. }
  2746. expectPlugin(name, pos) {
  2747. if (!this.hasPlugin(name)) {
  2748. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  2749. missingPlugin: [name]
  2750. }, `This experimental syntax requires enabling the parser plugin: '${name}'`);
  2751. }
  2752. return true;
  2753. }
  2754. expectOnePlugin(names, pos) {
  2755. if (!names.some(n => this.hasPlugin(n))) {
  2756. throw this.raiseWithData(pos != null ? pos : this.state.start, {
  2757. missingPlugin: names
  2758. }, `This experimental syntax requires enabling one of the following parser plugin(s): '${names.join(", ")}'`);
  2759. }
  2760. }
  2761. tryParse(fn, oldState = this.state.clone()) {
  2762. const abortSignal = {
  2763. node: null
  2764. };
  2765. try {
  2766. const node = fn((node = null) => {
  2767. abortSignal.node = node;
  2768. throw abortSignal;
  2769. });
  2770. if (this.state.errors.length > oldState.errors.length) {
  2771. const failState = this.state;
  2772. this.state = oldState;
  2773. this.state.tokensLength = failState.tokensLength;
  2774. return {
  2775. node,
  2776. error: failState.errors[oldState.errors.length],
  2777. thrown: false,
  2778. aborted: false,
  2779. failState
  2780. };
  2781. }
  2782. return {
  2783. node,
  2784. error: null,
  2785. thrown: false,
  2786. aborted: false,
  2787. failState: null
  2788. };
  2789. } catch (error) {
  2790. const failState = this.state;
  2791. this.state = oldState;
  2792. if (error instanceof SyntaxError) {
  2793. return {
  2794. node: null,
  2795. error,
  2796. thrown: true,
  2797. aborted: false,
  2798. failState
  2799. };
  2800. }
  2801. if (error === abortSignal) {
  2802. return {
  2803. node: abortSignal.node,
  2804. error: null,
  2805. thrown: false,
  2806. aborted: true,
  2807. failState
  2808. };
  2809. }
  2810. throw error;
  2811. }
  2812. }
  2813. checkExpressionErrors(refExpressionErrors, andThrow) {
  2814. if (!refExpressionErrors) return false;
  2815. const {
  2816. shorthandAssign,
  2817. doubleProto,
  2818. optionalParameters
  2819. } = refExpressionErrors;
  2820. if (!andThrow) {
  2821. return shorthandAssign >= 0 || doubleProto >= 0 || optionalParameters >= 0;
  2822. }
  2823. if (shorthandAssign >= 0) {
  2824. this.unexpected(shorthandAssign);
  2825. }
  2826. if (doubleProto >= 0) {
  2827. this.raise(doubleProto, ErrorMessages.DuplicateProto);
  2828. }
  2829. if (optionalParameters >= 0) {
  2830. this.unexpected(optionalParameters);
  2831. }
  2832. }
  2833. isLiteralPropertyName() {
  2834. return this.match(types$1.name) || !!this.state.type.keyword || this.match(types$1.string) || this.match(types$1.num) || this.match(types$1.bigint) || this.match(types$1.decimal);
  2835. }
  2836. isPrivateName(node) {
  2837. return node.type === "PrivateName";
  2838. }
  2839. getPrivateNameSV(node) {
  2840. return node.id.name;
  2841. }
  2842. hasPropertyAsPrivateName(node) {
  2843. return (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && this.isPrivateName(node.property);
  2844. }
  2845. isOptionalChain(node) {
  2846. return node.type === "OptionalMemberExpression" || node.type === "OptionalCallExpression";
  2847. }
  2848. isObjectProperty(node) {
  2849. return node.type === "ObjectProperty";
  2850. }
  2851. isObjectMethod(node) {
  2852. return node.type === "ObjectMethod";
  2853. }
  2854. initializeScopes(inModule = this.options.sourceType === "module") {
  2855. const oldLabels = this.state.labels;
  2856. this.state.labels = [];
  2857. const oldExportedIdentifiers = this.exportedIdentifiers;
  2858. this.exportedIdentifiers = new Set();
  2859. const oldInModule = this.inModule;
  2860. this.inModule = inModule;
  2861. const oldScope = this.scope;
  2862. const ScopeHandler = this.getScopeHandler();
  2863. this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
  2864. const oldProdParam = this.prodParam;
  2865. this.prodParam = new ProductionParameterHandler();
  2866. const oldClassScope = this.classScope;
  2867. this.classScope = new ClassScopeHandler(this.raise.bind(this));
  2868. const oldExpressionScope = this.expressionScope;
  2869. this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
  2870. return () => {
  2871. this.state.labels = oldLabels;
  2872. this.exportedIdentifiers = oldExportedIdentifiers;
  2873. this.inModule = oldInModule;
  2874. this.scope = oldScope;
  2875. this.prodParam = oldProdParam;
  2876. this.classScope = oldClassScope;
  2877. this.expressionScope = oldExpressionScope;
  2878. };
  2879. }
  2880. enterInitialScopes() {
  2881. let paramFlags = PARAM;
  2882. if (this.inModule) {
  2883. paramFlags |= PARAM_AWAIT;
  2884. }
  2885. this.scope.enter(SCOPE_PROGRAM);
  2886. this.prodParam.enter(paramFlags);
  2887. }
  2888. }
  2889. class ExpressionErrors {
  2890. constructor() {
  2891. this.shorthandAssign = -1;
  2892. this.doubleProto = -1;
  2893. this.optionalParameters = -1;
  2894. }
  2895. }
  2896. class Node {
  2897. constructor(parser, pos, loc) {
  2898. this.type = "";
  2899. this.start = pos;
  2900. this.end = 0;
  2901. this.loc = new SourceLocation(loc);
  2902. if (parser != null && parser.options.ranges) this.range = [pos, 0];
  2903. if (parser != null && parser.filename) this.loc.filename = parser.filename;
  2904. }
  2905. }
  2906. const NodePrototype = Node.prototype;
  2907. {
  2908. NodePrototype.__clone = function () {
  2909. const newNode = new Node();
  2910. const keys = Object.keys(this);
  2911. for (let i = 0, length = keys.length; i < length; i++) {
  2912. const key = keys[i];
  2913. if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") {
  2914. newNode[key] = this[key];
  2915. }
  2916. }
  2917. return newNode;
  2918. };
  2919. }
  2920. function clonePlaceholder(node) {
  2921. return cloneIdentifier(node);
  2922. }
  2923. function cloneIdentifier(node) {
  2924. const {
  2925. type,
  2926. start,
  2927. end,
  2928. loc,
  2929. range,
  2930. extra,
  2931. name
  2932. } = node;
  2933. const cloned = Object.create(NodePrototype);
  2934. cloned.type = type;
  2935. cloned.start = start;
  2936. cloned.end = end;
  2937. cloned.loc = loc;
  2938. cloned.range = range;
  2939. cloned.extra = extra;
  2940. cloned.name = name;
  2941. if (type === "Placeholder") {
  2942. cloned.expectedNode = node.expectedNode;
  2943. }
  2944. return cloned;
  2945. }
  2946. function cloneStringLiteral(node) {
  2947. const {
  2948. type,
  2949. start,
  2950. end,
  2951. loc,
  2952. range,
  2953. extra
  2954. } = node;
  2955. if (type === "Placeholder") {
  2956. return clonePlaceholder(node);
  2957. }
  2958. const cloned = Object.create(NodePrototype);
  2959. cloned.type = "StringLiteral";
  2960. cloned.start = start;
  2961. cloned.end = end;
  2962. cloned.loc = loc;
  2963. cloned.range = range;
  2964. cloned.extra = extra;
  2965. cloned.value = node.value;
  2966. return cloned;
  2967. }
  2968. class NodeUtils extends UtilParser {
  2969. startNode() {
  2970. return new Node(this, this.state.start, this.state.startLoc);
  2971. }
  2972. startNodeAt(pos, loc) {
  2973. return new Node(this, pos, loc);
  2974. }
  2975. startNodeAtNode(type) {
  2976. return this.startNodeAt(type.start, type.loc.start);
  2977. }
  2978. finishNode(node, type) {
  2979. return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc);
  2980. }
  2981. finishNodeAt(node, type, pos, loc) {
  2982. node.type = type;
  2983. node.end = pos;
  2984. node.loc.end = loc;
  2985. if (this.options.ranges) node.range[1] = pos;
  2986. if (this.options.attachComment) this.processComment(node);
  2987. return node;
  2988. }
  2989. resetStartLocation(node, start, startLoc) {
  2990. node.start = start;
  2991. node.loc.start = startLoc;
  2992. if (this.options.ranges) node.range[0] = start;
  2993. }
  2994. resetEndLocation(node, end = this.state.lastTokEnd, endLoc = this.state.lastTokEndLoc) {
  2995. node.end = end;
  2996. node.loc.end = endLoc;
  2997. if (this.options.ranges) node.range[1] = end;
  2998. }
  2999. resetStartLocationFromNode(node, locationNode) {
  3000. this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
  3001. }
  3002. }
  3003. const reservedTypes = new Set(["_", "any", "bool", "boolean", "empty", "extends", "false", "interface", "mixed", "null", "number", "static", "string", "true", "typeof", "void"]);
  3004. const FlowErrors = makeErrorTemplates({
  3005. AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.",
  3006. 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.",
  3007. AssignReservedType: "Cannot overwrite reserved type %0.",
  3008. DeclareClassElement: "The `declare` modifier can only appear on class fields.",
  3009. DeclareClassFieldInitializer: "Initializers are not allowed in fields with the `declare` modifier.",
  3010. DuplicateDeclareModuleExports: "Duplicate `declare module.exports` statement.",
  3011. EnumBooleanMemberNotInitialized: "Boolean enum members need to be initialized. Use either `%0 = true,` or `%0 = false,` in enum `%1`.",
  3012. EnumDuplicateMemberName: "Enum member names need to be unique, but the name `%0` has already been used before in enum `%1`.",
  3013. EnumInconsistentMemberValues: "Enum `%0` has inconsistent member initializers. Either use no initializers, or consistently use literals (either booleans, numbers, or strings) for all member initializers.",
  3014. EnumInvalidExplicitType: "Enum type `%1` is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  3015. EnumInvalidExplicitTypeUnknownSupplied: "Supplied enum type is not valid. Use one of `boolean`, `number`, `string`, or `symbol` in enum `%0`.",
  3016. EnumInvalidMemberInitializerPrimaryType: "Enum `%0` has type `%2`, so the initializer of `%1` needs to be a %2 literal.",
  3017. EnumInvalidMemberInitializerSymbolType: "Symbol enum members cannot be initialized. Use `%1,` in enum `%0`.",
  3018. EnumInvalidMemberInitializerUnknownType: "The enum member initializer for `%1` needs to be a literal (either a boolean, number, or string) in enum `%0`.",
  3019. EnumInvalidMemberName: "Enum member names cannot start with lowercase 'a' through 'z'. Instead of using `%0`, consider using `%1`, in enum `%2`.",
  3020. EnumNumberMemberNotInitialized: "Number enum members need to be initialized, e.g. `%1 = 1` in enum `%0`.",
  3021. EnumStringMemberInconsistentlyInitailized: "String enum members need to consistently either all use initializers, or use no initializers, in enum `%0`.",
  3022. GetterMayNotHaveThisParam: "A getter cannot have a `this` parameter.",
  3023. 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.",
  3024. InexactInsideExact: "Explicit inexact syntax cannot appear inside an explicit exact object type.",
  3025. InexactInsideNonObject: "Explicit inexact syntax cannot appear in class or interface definitions.",
  3026. InexactVariance: "Explicit inexact syntax cannot have variance.",
  3027. InvalidNonTypeImportInDeclareModule: "Imports within a `declare module` body must always be `import type` or `import typeof`.",
  3028. MissingTypeParamDefault: "Type parameter declaration needs a default, since a preceding type parameter declaration has a default.",
  3029. NestedDeclareModule: "`declare module` cannot be used inside another `declare module`.",
  3030. NestedFlowComment: "Cannot have a flow comment inside another flow comment.",
  3031. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  3032. SetterMayNotHaveThisParam: "A setter cannot have a `this` parameter.",
  3033. SpreadVariance: "Spread properties cannot have variance.",
  3034. ThisParamAnnotationRequired: "A type annotation is required for the `this` parameter.",
  3035. ThisParamBannedInConstructor: "Constructors cannot have a `this` parameter; constructors don't bind `this` like other functions.",
  3036. ThisParamMayNotBeOptional: "The `this` parameter cannot be optional.",
  3037. ThisParamMustBeFirst: "The `this` parameter must be the first function parameter.",
  3038. ThisParamNoDefault: "The `this` parameter may not have a default value.",
  3039. TypeBeforeInitializer: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  3040. TypeCastInPattern: "The type cast expression is expected to be wrapped with parenthesis.",
  3041. UnexpectedExplicitInexactInObject: "Explicit inexact syntax must appear at the end of an inexact object.",
  3042. UnexpectedReservedType: "Unexpected reserved type %0.",
  3043. UnexpectedReservedUnderscore: "`_` is only allowed as a type argument to call or new.",
  3044. UnexpectedSpaceBetweenModuloChecks: "Spaces between `%` and `checks` are not allowed here.",
  3045. UnexpectedSpreadType: "Spread operator cannot appear in class or interface definitions.",
  3046. UnexpectedSubtractionOperand: 'Unexpected token, expected "number" or "bigint".',
  3047. UnexpectedTokenAfterTypeParameter: "Expected an arrow function after this type parameter declaration.",
  3048. UnexpectedTypeParameterBeforeAsyncArrowFunction: "Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`.",
  3049. UnsupportedDeclareExportKind: "`declare export %0` is not supported. Use `%1` instead.",
  3050. UnsupportedStatementInDeclareModule: "Only declares and type imports are allowed inside declare module.",
  3051. UnterminatedFlowComment: "Unterminated flow-comment."
  3052. }, ErrorCodes.SyntaxError, "flow");
  3053. function isEsModuleType(bodyElement) {
  3054. return bodyElement.type === "DeclareExportAllDeclaration" || bodyElement.type === "DeclareExportDeclaration" && (!bodyElement.declaration || bodyElement.declaration.type !== "TypeAlias" && bodyElement.declaration.type !== "InterfaceDeclaration");
  3055. }
  3056. function hasTypeImportKind(node) {
  3057. return node.importKind === "type" || node.importKind === "typeof";
  3058. }
  3059. function isMaybeDefaultImport(state) {
  3060. return (state.type === types$1.name || !!state.type.keyword) && state.value !== "from";
  3061. }
  3062. const exportSuggestions = {
  3063. const: "declare export var",
  3064. let: "declare export var",
  3065. type: "export type",
  3066. interface: "export interface"
  3067. };
  3068. function partition(list, test) {
  3069. const list1 = [];
  3070. const list2 = [];
  3071. for (let i = 0; i < list.length; i++) {
  3072. (test(list[i], i, list) ? list1 : list2).push(list[i]);
  3073. }
  3074. return [list1, list2];
  3075. }
  3076. const FLOW_PRAGMA_REGEX = /\*?\s*@((?:no)?flow)\b/;
  3077. var flow = (superClass => class extends superClass {
  3078. constructor(...args) {
  3079. super(...args);
  3080. this.flowPragma = undefined;
  3081. }
  3082. getScopeHandler() {
  3083. return FlowScopeHandler;
  3084. }
  3085. shouldParseTypes() {
  3086. return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
  3087. }
  3088. shouldParseEnums() {
  3089. return !!this.getPluginOption("flow", "enums");
  3090. }
  3091. finishToken(type, val) {
  3092. if (type !== types$1.string && type !== types$1.semi && type !== types$1.interpreterDirective) {
  3093. if (this.flowPragma === undefined) {
  3094. this.flowPragma = null;
  3095. }
  3096. }
  3097. return super.finishToken(type, val);
  3098. }
  3099. addComment(comment) {
  3100. if (this.flowPragma === undefined) {
  3101. const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
  3102. if (!matches) ; else if (matches[1] === "flow") {
  3103. this.flowPragma = "flow";
  3104. } else if (matches[1] === "noflow") {
  3105. this.flowPragma = "noflow";
  3106. } else {
  3107. throw new Error("Unexpected flow pragma");
  3108. }
  3109. }
  3110. return super.addComment(comment);
  3111. }
  3112. flowParseTypeInitialiser(tok) {
  3113. const oldInType = this.state.inType;
  3114. this.state.inType = true;
  3115. this.expect(tok || types$1.colon);
  3116. const type = this.flowParseType();
  3117. this.state.inType = oldInType;
  3118. return type;
  3119. }
  3120. flowParsePredicate() {
  3121. const node = this.startNode();
  3122. const moduloPos = this.state.start;
  3123. this.next();
  3124. this.expectContextual("checks");
  3125. if (this.state.lastTokStart > moduloPos + 1) {
  3126. this.raise(moduloPos, FlowErrors.UnexpectedSpaceBetweenModuloChecks);
  3127. }
  3128. if (this.eat(types$1.parenL)) {
  3129. node.value = this.parseExpression();
  3130. this.expect(types$1.parenR);
  3131. return this.finishNode(node, "DeclaredPredicate");
  3132. } else {
  3133. return this.finishNode(node, "InferredPredicate");
  3134. }
  3135. }
  3136. flowParseTypeAndPredicateInitialiser() {
  3137. const oldInType = this.state.inType;
  3138. this.state.inType = true;
  3139. this.expect(types$1.colon);
  3140. let type = null;
  3141. let predicate = null;
  3142. if (this.match(types$1.modulo)) {
  3143. this.state.inType = oldInType;
  3144. predicate = this.flowParsePredicate();
  3145. } else {
  3146. type = this.flowParseType();
  3147. this.state.inType = oldInType;
  3148. if (this.match(types$1.modulo)) {
  3149. predicate = this.flowParsePredicate();
  3150. }
  3151. }
  3152. return [type, predicate];
  3153. }
  3154. flowParseDeclareClass(node) {
  3155. this.next();
  3156. this.flowParseInterfaceish(node, true);
  3157. return this.finishNode(node, "DeclareClass");
  3158. }
  3159. flowParseDeclareFunction(node) {
  3160. this.next();
  3161. const id = node.id = this.parseIdentifier();
  3162. const typeNode = this.startNode();
  3163. const typeContainer = this.startNode();
  3164. if (this.isRelational("<")) {
  3165. typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
  3166. } else {
  3167. typeNode.typeParameters = null;
  3168. }
  3169. this.expect(types$1.parenL);
  3170. const tmp = this.flowParseFunctionTypeParams();
  3171. typeNode.params = tmp.params;
  3172. typeNode.rest = tmp.rest;
  3173. typeNode.this = tmp._this;
  3174. this.expect(types$1.parenR);
  3175. [typeNode.returnType, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  3176. typeContainer.typeAnnotation = this.finishNode(typeNode, "FunctionTypeAnnotation");
  3177. id.typeAnnotation = this.finishNode(typeContainer, "TypeAnnotation");
  3178. this.resetEndLocation(id);
  3179. this.semicolon();
  3180. this.scope.declareName(node.id.name, BIND_FLOW_DECLARE_FN, node.id.start);
  3181. return this.finishNode(node, "DeclareFunction");
  3182. }
  3183. flowParseDeclare(node, insideModule) {
  3184. if (this.match(types$1._class)) {
  3185. return this.flowParseDeclareClass(node);
  3186. } else if (this.match(types$1._function)) {
  3187. return this.flowParseDeclareFunction(node);
  3188. } else if (this.match(types$1._var)) {
  3189. return this.flowParseDeclareVariable(node);
  3190. } else if (this.eatContextual("module")) {
  3191. if (this.match(types$1.dot)) {
  3192. return this.flowParseDeclareModuleExports(node);
  3193. } else {
  3194. if (insideModule) {
  3195. this.raise(this.state.lastTokStart, FlowErrors.NestedDeclareModule);
  3196. }
  3197. return this.flowParseDeclareModule(node);
  3198. }
  3199. } else if (this.isContextual("type")) {
  3200. return this.flowParseDeclareTypeAlias(node);
  3201. } else if (this.isContextual("opaque")) {
  3202. return this.flowParseDeclareOpaqueType(node);
  3203. } else if (this.isContextual("interface")) {
  3204. return this.flowParseDeclareInterface(node);
  3205. } else if (this.match(types$1._export)) {
  3206. return this.flowParseDeclareExportDeclaration(node, insideModule);
  3207. } else {
  3208. throw this.unexpected();
  3209. }
  3210. }
  3211. flowParseDeclareVariable(node) {
  3212. this.next();
  3213. node.id = this.flowParseTypeAnnotatableIdentifier(true);
  3214. this.scope.declareName(node.id.name, BIND_VAR, node.id.start);
  3215. this.semicolon();
  3216. return this.finishNode(node, "DeclareVariable");
  3217. }
  3218. flowParseDeclareModule(node) {
  3219. this.scope.enter(SCOPE_OTHER);
  3220. if (this.match(types$1.string)) {
  3221. node.id = this.parseExprAtom();
  3222. } else {
  3223. node.id = this.parseIdentifier();
  3224. }
  3225. const bodyNode = node.body = this.startNode();
  3226. const body = bodyNode.body = [];
  3227. this.expect(types$1.braceL);
  3228. while (!this.match(types$1.braceR)) {
  3229. let bodyNode = this.startNode();
  3230. if (this.match(types$1._import)) {
  3231. this.next();
  3232. if (!this.isContextual("type") && !this.match(types$1._typeof)) {
  3233. this.raise(this.state.lastTokStart, FlowErrors.InvalidNonTypeImportInDeclareModule);
  3234. }
  3235. this.parseImport(bodyNode);
  3236. } else {
  3237. this.expectContextual("declare", FlowErrors.UnsupportedStatementInDeclareModule);
  3238. bodyNode = this.flowParseDeclare(bodyNode, true);
  3239. }
  3240. body.push(bodyNode);
  3241. }
  3242. this.scope.exit();
  3243. this.expect(types$1.braceR);
  3244. this.finishNode(bodyNode, "BlockStatement");
  3245. let kind = null;
  3246. let hasModuleExport = false;
  3247. body.forEach(bodyElement => {
  3248. if (isEsModuleType(bodyElement)) {
  3249. if (kind === "CommonJS") {
  3250. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  3251. }
  3252. kind = "ES";
  3253. } else if (bodyElement.type === "DeclareModuleExports") {
  3254. if (hasModuleExport) {
  3255. this.raise(bodyElement.start, FlowErrors.DuplicateDeclareModuleExports);
  3256. }
  3257. if (kind === "ES") {
  3258. this.raise(bodyElement.start, FlowErrors.AmbiguousDeclareModuleKind);
  3259. }
  3260. kind = "CommonJS";
  3261. hasModuleExport = true;
  3262. }
  3263. });
  3264. node.kind = kind || "CommonJS";
  3265. return this.finishNode(node, "DeclareModule");
  3266. }
  3267. flowParseDeclareExportDeclaration(node, insideModule) {
  3268. this.expect(types$1._export);
  3269. if (this.eat(types$1._default)) {
  3270. if (this.match(types$1._function) || this.match(types$1._class)) {
  3271. node.declaration = this.flowParseDeclare(this.startNode());
  3272. } else {
  3273. node.declaration = this.flowParseType();
  3274. this.semicolon();
  3275. }
  3276. node.default = true;
  3277. return this.finishNode(node, "DeclareExportDeclaration");
  3278. } else {
  3279. if (this.match(types$1._const) || this.isLet() || (this.isContextual("type") || this.isContextual("interface")) && !insideModule) {
  3280. const label = this.state.value;
  3281. const suggestion = exportSuggestions[label];
  3282. throw this.raise(this.state.start, FlowErrors.UnsupportedDeclareExportKind, label, suggestion);
  3283. }
  3284. if (this.match(types$1._var) || this.match(types$1._function) || this.match(types$1._class) || this.isContextual("opaque")) {
  3285. node.declaration = this.flowParseDeclare(this.startNode());
  3286. node.default = false;
  3287. return this.finishNode(node, "DeclareExportDeclaration");
  3288. } else if (this.match(types$1.star) || this.match(types$1.braceL) || this.isContextual("interface") || this.isContextual("type") || this.isContextual("opaque")) {
  3289. node = this.parseExport(node);
  3290. if (node.type === "ExportNamedDeclaration") {
  3291. node.type = "ExportDeclaration";
  3292. node.default = false;
  3293. delete node.exportKind;
  3294. }
  3295. node.type = "Declare" + node.type;
  3296. return node;
  3297. }
  3298. }
  3299. throw this.unexpected();
  3300. }
  3301. flowParseDeclareModuleExports(node) {
  3302. this.next();
  3303. this.expectContextual("exports");
  3304. node.typeAnnotation = this.flowParseTypeAnnotation();
  3305. this.semicolon();
  3306. return this.finishNode(node, "DeclareModuleExports");
  3307. }
  3308. flowParseDeclareTypeAlias(node) {
  3309. this.next();
  3310. this.flowParseTypeAlias(node);
  3311. node.type = "DeclareTypeAlias";
  3312. return node;
  3313. }
  3314. flowParseDeclareOpaqueType(node) {
  3315. this.next();
  3316. this.flowParseOpaqueType(node, true);
  3317. node.type = "DeclareOpaqueType";
  3318. return node;
  3319. }
  3320. flowParseDeclareInterface(node) {
  3321. this.next();
  3322. this.flowParseInterfaceish(node);
  3323. return this.finishNode(node, "DeclareInterface");
  3324. }
  3325. flowParseInterfaceish(node, isClass = false) {
  3326. node.id = this.flowParseRestrictedIdentifier(!isClass, true);
  3327. this.scope.declareName(node.id.name, isClass ? BIND_FUNCTION : BIND_LEXICAL, node.id.start);
  3328. if (this.isRelational("<")) {
  3329. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3330. } else {
  3331. node.typeParameters = null;
  3332. }
  3333. node.extends = [];
  3334. node.implements = [];
  3335. node.mixins = [];
  3336. if (this.eat(types$1._extends)) {
  3337. do {
  3338. node.extends.push(this.flowParseInterfaceExtends());
  3339. } while (!isClass && this.eat(types$1.comma));
  3340. }
  3341. if (this.isContextual("mixins")) {
  3342. this.next();
  3343. do {
  3344. node.mixins.push(this.flowParseInterfaceExtends());
  3345. } while (this.eat(types$1.comma));
  3346. }
  3347. if (this.isContextual("implements")) {
  3348. this.next();
  3349. do {
  3350. node.implements.push(this.flowParseInterfaceExtends());
  3351. } while (this.eat(types$1.comma));
  3352. }
  3353. node.body = this.flowParseObjectType({
  3354. allowStatic: isClass,
  3355. allowExact: false,
  3356. allowSpread: false,
  3357. allowProto: isClass,
  3358. allowInexact: false
  3359. });
  3360. }
  3361. flowParseInterfaceExtends() {
  3362. const node = this.startNode();
  3363. node.id = this.flowParseQualifiedTypeIdentifier();
  3364. if (this.isRelational("<")) {
  3365. node.typeParameters = this.flowParseTypeParameterInstantiation();
  3366. } else {
  3367. node.typeParameters = null;
  3368. }
  3369. return this.finishNode(node, "InterfaceExtends");
  3370. }
  3371. flowParseInterface(node) {
  3372. this.flowParseInterfaceish(node);
  3373. return this.finishNode(node, "InterfaceDeclaration");
  3374. }
  3375. checkNotUnderscore(word) {
  3376. if (word === "_") {
  3377. this.raise(this.state.start, FlowErrors.UnexpectedReservedUnderscore);
  3378. }
  3379. }
  3380. checkReservedType(word, startLoc, declaration) {
  3381. if (!reservedTypes.has(word)) return;
  3382. this.raise(startLoc, declaration ? FlowErrors.AssignReservedType : FlowErrors.UnexpectedReservedType, word);
  3383. }
  3384. flowParseRestrictedIdentifier(liberal, declaration) {
  3385. this.checkReservedType(this.state.value, this.state.start, declaration);
  3386. return this.parseIdentifier(liberal);
  3387. }
  3388. flowParseTypeAlias(node) {
  3389. node.id = this.flowParseRestrictedIdentifier(false, true);
  3390. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  3391. if (this.isRelational("<")) {
  3392. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3393. } else {
  3394. node.typeParameters = null;
  3395. }
  3396. node.right = this.flowParseTypeInitialiser(types$1.eq);
  3397. this.semicolon();
  3398. return this.finishNode(node, "TypeAlias");
  3399. }
  3400. flowParseOpaqueType(node, declare) {
  3401. this.expectContextual("type");
  3402. node.id = this.flowParseRestrictedIdentifier(true, true);
  3403. this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
  3404. if (this.isRelational("<")) {
  3405. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3406. } else {
  3407. node.typeParameters = null;
  3408. }
  3409. node.supertype = null;
  3410. if (this.match(types$1.colon)) {
  3411. node.supertype = this.flowParseTypeInitialiser(types$1.colon);
  3412. }
  3413. node.impltype = null;
  3414. if (!declare) {
  3415. node.impltype = this.flowParseTypeInitialiser(types$1.eq);
  3416. }
  3417. this.semicolon();
  3418. return this.finishNode(node, "OpaqueType");
  3419. }
  3420. flowParseTypeParameter(requireDefault = false) {
  3421. const nodeStart = this.state.start;
  3422. const node = this.startNode();
  3423. const variance = this.flowParseVariance();
  3424. const ident = this.flowParseTypeAnnotatableIdentifier();
  3425. node.name = ident.name;
  3426. node.variance = variance;
  3427. node.bound = ident.typeAnnotation;
  3428. if (this.match(types$1.eq)) {
  3429. this.eat(types$1.eq);
  3430. node.default = this.flowParseType();
  3431. } else {
  3432. if (requireDefault) {
  3433. this.raise(nodeStart, FlowErrors.MissingTypeParamDefault);
  3434. }
  3435. }
  3436. return this.finishNode(node, "TypeParameter");
  3437. }
  3438. flowParseTypeParameterDeclaration() {
  3439. const oldInType = this.state.inType;
  3440. const node = this.startNode();
  3441. node.params = [];
  3442. this.state.inType = true;
  3443. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  3444. this.next();
  3445. } else {
  3446. this.unexpected();
  3447. }
  3448. let defaultRequired = false;
  3449. do {
  3450. const typeParameter = this.flowParseTypeParameter(defaultRequired);
  3451. node.params.push(typeParameter);
  3452. if (typeParameter.default) {
  3453. defaultRequired = true;
  3454. }
  3455. if (!this.isRelational(">")) {
  3456. this.expect(types$1.comma);
  3457. }
  3458. } while (!this.isRelational(">"));
  3459. this.expectRelational(">");
  3460. this.state.inType = oldInType;
  3461. return this.finishNode(node, "TypeParameterDeclaration");
  3462. }
  3463. flowParseTypeParameterInstantiation() {
  3464. const node = this.startNode();
  3465. const oldInType = this.state.inType;
  3466. node.params = [];
  3467. this.state.inType = true;
  3468. this.expectRelational("<");
  3469. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3470. this.state.noAnonFunctionType = false;
  3471. while (!this.isRelational(">")) {
  3472. node.params.push(this.flowParseType());
  3473. if (!this.isRelational(">")) {
  3474. this.expect(types$1.comma);
  3475. }
  3476. }
  3477. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3478. this.expectRelational(">");
  3479. this.state.inType = oldInType;
  3480. return this.finishNode(node, "TypeParameterInstantiation");
  3481. }
  3482. flowParseTypeParameterInstantiationCallOrNew() {
  3483. const node = this.startNode();
  3484. const oldInType = this.state.inType;
  3485. node.params = [];
  3486. this.state.inType = true;
  3487. this.expectRelational("<");
  3488. while (!this.isRelational(">")) {
  3489. node.params.push(this.flowParseTypeOrImplicitInstantiation());
  3490. if (!this.isRelational(">")) {
  3491. this.expect(types$1.comma);
  3492. }
  3493. }
  3494. this.expectRelational(">");
  3495. this.state.inType = oldInType;
  3496. return this.finishNode(node, "TypeParameterInstantiation");
  3497. }
  3498. flowParseInterfaceType() {
  3499. const node = this.startNode();
  3500. this.expectContextual("interface");
  3501. node.extends = [];
  3502. if (this.eat(types$1._extends)) {
  3503. do {
  3504. node.extends.push(this.flowParseInterfaceExtends());
  3505. } while (this.eat(types$1.comma));
  3506. }
  3507. node.body = this.flowParseObjectType({
  3508. allowStatic: false,
  3509. allowExact: false,
  3510. allowSpread: false,
  3511. allowProto: false,
  3512. allowInexact: false
  3513. });
  3514. return this.finishNode(node, "InterfaceTypeAnnotation");
  3515. }
  3516. flowParseObjectPropertyKey() {
  3517. return this.match(types$1.num) || this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  3518. }
  3519. flowParseObjectTypeIndexer(node, isStatic, variance) {
  3520. node.static = isStatic;
  3521. if (this.lookahead().type === types$1.colon) {
  3522. node.id = this.flowParseObjectPropertyKey();
  3523. node.key = this.flowParseTypeInitialiser();
  3524. } else {
  3525. node.id = null;
  3526. node.key = this.flowParseType();
  3527. }
  3528. this.expect(types$1.bracketR);
  3529. node.value = this.flowParseTypeInitialiser();
  3530. node.variance = variance;
  3531. return this.finishNode(node, "ObjectTypeIndexer");
  3532. }
  3533. flowParseObjectTypeInternalSlot(node, isStatic) {
  3534. node.static = isStatic;
  3535. node.id = this.flowParseObjectPropertyKey();
  3536. this.expect(types$1.bracketR);
  3537. this.expect(types$1.bracketR);
  3538. if (this.isRelational("<") || this.match(types$1.parenL)) {
  3539. node.method = true;
  3540. node.optional = false;
  3541. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  3542. } else {
  3543. node.method = false;
  3544. if (this.eat(types$1.question)) {
  3545. node.optional = true;
  3546. }
  3547. node.value = this.flowParseTypeInitialiser();
  3548. }
  3549. return this.finishNode(node, "ObjectTypeInternalSlot");
  3550. }
  3551. flowParseObjectTypeMethodish(node) {
  3552. node.params = [];
  3553. node.rest = null;
  3554. node.typeParameters = null;
  3555. node.this = null;
  3556. if (this.isRelational("<")) {
  3557. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3558. }
  3559. this.expect(types$1.parenL);
  3560. if (this.match(types$1._this)) {
  3561. node.this = this.flowParseFunctionTypeParam(true);
  3562. node.this.name = null;
  3563. if (!this.match(types$1.parenR)) {
  3564. this.expect(types$1.comma);
  3565. }
  3566. }
  3567. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  3568. node.params.push(this.flowParseFunctionTypeParam(false));
  3569. if (!this.match(types$1.parenR)) {
  3570. this.expect(types$1.comma);
  3571. }
  3572. }
  3573. if (this.eat(types$1.ellipsis)) {
  3574. node.rest = this.flowParseFunctionTypeParam(false);
  3575. }
  3576. this.expect(types$1.parenR);
  3577. node.returnType = this.flowParseTypeInitialiser();
  3578. return this.finishNode(node, "FunctionTypeAnnotation");
  3579. }
  3580. flowParseObjectTypeCallProperty(node, isStatic) {
  3581. const valueNode = this.startNode();
  3582. node.static = isStatic;
  3583. node.value = this.flowParseObjectTypeMethodish(valueNode);
  3584. return this.finishNode(node, "ObjectTypeCallProperty");
  3585. }
  3586. flowParseObjectType({
  3587. allowStatic,
  3588. allowExact,
  3589. allowSpread,
  3590. allowProto,
  3591. allowInexact
  3592. }) {
  3593. const oldInType = this.state.inType;
  3594. this.state.inType = true;
  3595. const nodeStart = this.startNode();
  3596. nodeStart.callProperties = [];
  3597. nodeStart.properties = [];
  3598. nodeStart.indexers = [];
  3599. nodeStart.internalSlots = [];
  3600. let endDelim;
  3601. let exact;
  3602. let inexact = false;
  3603. if (allowExact && this.match(types$1.braceBarL)) {
  3604. this.expect(types$1.braceBarL);
  3605. endDelim = types$1.braceBarR;
  3606. exact = true;
  3607. } else {
  3608. this.expect(types$1.braceL);
  3609. endDelim = types$1.braceR;
  3610. exact = false;
  3611. }
  3612. nodeStart.exact = exact;
  3613. while (!this.match(endDelim)) {
  3614. let isStatic = false;
  3615. let protoStart = null;
  3616. let inexactStart = null;
  3617. const node = this.startNode();
  3618. if (allowProto && this.isContextual("proto")) {
  3619. const lookahead = this.lookahead();
  3620. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  3621. this.next();
  3622. protoStart = this.state.start;
  3623. allowStatic = false;
  3624. }
  3625. }
  3626. if (allowStatic && this.isContextual("static")) {
  3627. const lookahead = this.lookahead();
  3628. if (lookahead.type !== types$1.colon && lookahead.type !== types$1.question) {
  3629. this.next();
  3630. isStatic = true;
  3631. }
  3632. }
  3633. const variance = this.flowParseVariance();
  3634. if (this.eat(types$1.bracketL)) {
  3635. if (protoStart != null) {
  3636. this.unexpected(protoStart);
  3637. }
  3638. if (this.eat(types$1.bracketL)) {
  3639. if (variance) {
  3640. this.unexpected(variance.start);
  3641. }
  3642. nodeStart.internalSlots.push(this.flowParseObjectTypeInternalSlot(node, isStatic));
  3643. } else {
  3644. nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
  3645. }
  3646. } else if (this.match(types$1.parenL) || this.isRelational("<")) {
  3647. if (protoStart != null) {
  3648. this.unexpected(protoStart);
  3649. }
  3650. if (variance) {
  3651. this.unexpected(variance.start);
  3652. }
  3653. nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
  3654. } else {
  3655. let kind = "init";
  3656. if (this.isContextual("get") || this.isContextual("set")) {
  3657. const lookahead = this.lookahead();
  3658. if (lookahead.type === types$1.name || lookahead.type === types$1.string || lookahead.type === types$1.num) {
  3659. kind = this.state.value;
  3660. this.next();
  3661. }
  3662. }
  3663. const propOrInexact = this.flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact != null ? allowInexact : !exact);
  3664. if (propOrInexact === null) {
  3665. inexact = true;
  3666. inexactStart = this.state.lastTokStart;
  3667. } else {
  3668. nodeStart.properties.push(propOrInexact);
  3669. }
  3670. }
  3671. this.flowObjectTypeSemicolon();
  3672. if (inexactStart && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  3673. this.raise(inexactStart, FlowErrors.UnexpectedExplicitInexactInObject);
  3674. }
  3675. }
  3676. this.expect(endDelim);
  3677. if (allowSpread) {
  3678. nodeStart.inexact = inexact;
  3679. }
  3680. const out = this.finishNode(nodeStart, "ObjectTypeAnnotation");
  3681. this.state.inType = oldInType;
  3682. return out;
  3683. }
  3684. flowParseObjectTypeProperty(node, isStatic, protoStart, variance, kind, allowSpread, allowInexact) {
  3685. if (this.eat(types$1.ellipsis)) {
  3686. const isInexactToken = this.match(types$1.comma) || this.match(types$1.semi) || this.match(types$1.braceR) || this.match(types$1.braceBarR);
  3687. if (isInexactToken) {
  3688. if (!allowSpread) {
  3689. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideNonObject);
  3690. } else if (!allowInexact) {
  3691. this.raise(this.state.lastTokStart, FlowErrors.InexactInsideExact);
  3692. }
  3693. if (variance) {
  3694. this.raise(variance.start, FlowErrors.InexactVariance);
  3695. }
  3696. return null;
  3697. }
  3698. if (!allowSpread) {
  3699. this.raise(this.state.lastTokStart, FlowErrors.UnexpectedSpreadType);
  3700. }
  3701. if (protoStart != null) {
  3702. this.unexpected(protoStart);
  3703. }
  3704. if (variance) {
  3705. this.raise(variance.start, FlowErrors.SpreadVariance);
  3706. }
  3707. node.argument = this.flowParseType();
  3708. return this.finishNode(node, "ObjectTypeSpreadProperty");
  3709. } else {
  3710. node.key = this.flowParseObjectPropertyKey();
  3711. node.static = isStatic;
  3712. node.proto = protoStart != null;
  3713. node.kind = kind;
  3714. let optional = false;
  3715. if (this.isRelational("<") || this.match(types$1.parenL)) {
  3716. node.method = true;
  3717. if (protoStart != null) {
  3718. this.unexpected(protoStart);
  3719. }
  3720. if (variance) {
  3721. this.unexpected(variance.start);
  3722. }
  3723. node.value = this.flowParseObjectTypeMethodish(this.startNodeAt(node.start, node.loc.start));
  3724. if (kind === "get" || kind === "set") {
  3725. this.flowCheckGetterSetterParams(node);
  3726. }
  3727. if (!allowSpread && node.key.name === "constructor" && node.value.this) {
  3728. this.raise(node.value.this.start, FlowErrors.ThisParamBannedInConstructor);
  3729. }
  3730. } else {
  3731. if (kind !== "init") this.unexpected();
  3732. node.method = false;
  3733. if (this.eat(types$1.question)) {
  3734. optional = true;
  3735. }
  3736. node.value = this.flowParseTypeInitialiser();
  3737. node.variance = variance;
  3738. }
  3739. node.optional = optional;
  3740. return this.finishNode(node, "ObjectTypeProperty");
  3741. }
  3742. }
  3743. flowCheckGetterSetterParams(property) {
  3744. const paramCount = property.kind === "get" ? 0 : 1;
  3745. const start = property.start;
  3746. const length = property.value.params.length + (property.value.rest ? 1 : 0);
  3747. if (property.value.this) {
  3748. this.raise(property.value.this.start, property.kind === "get" ? FlowErrors.GetterMayNotHaveThisParam : FlowErrors.SetterMayNotHaveThisParam);
  3749. }
  3750. if (length !== paramCount) {
  3751. if (property.kind === "get") {
  3752. this.raise(start, ErrorMessages.BadGetterArity);
  3753. } else {
  3754. this.raise(start, ErrorMessages.BadSetterArity);
  3755. }
  3756. }
  3757. if (property.kind === "set" && property.value.rest) {
  3758. this.raise(start, ErrorMessages.BadSetterRestParameter);
  3759. }
  3760. }
  3761. flowObjectTypeSemicolon() {
  3762. if (!this.eat(types$1.semi) && !this.eat(types$1.comma) && !this.match(types$1.braceR) && !this.match(types$1.braceBarR)) {
  3763. this.unexpected();
  3764. }
  3765. }
  3766. flowParseQualifiedTypeIdentifier(startPos, startLoc, id) {
  3767. startPos = startPos || this.state.start;
  3768. startLoc = startLoc || this.state.startLoc;
  3769. let node = id || this.flowParseRestrictedIdentifier(true);
  3770. while (this.eat(types$1.dot)) {
  3771. const node2 = this.startNodeAt(startPos, startLoc);
  3772. node2.qualification = node;
  3773. node2.id = this.flowParseRestrictedIdentifier(true);
  3774. node = this.finishNode(node2, "QualifiedTypeIdentifier");
  3775. }
  3776. return node;
  3777. }
  3778. flowParseGenericType(startPos, startLoc, id) {
  3779. const node = this.startNodeAt(startPos, startLoc);
  3780. node.typeParameters = null;
  3781. node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);
  3782. if (this.isRelational("<")) {
  3783. node.typeParameters = this.flowParseTypeParameterInstantiation();
  3784. }
  3785. return this.finishNode(node, "GenericTypeAnnotation");
  3786. }
  3787. flowParseTypeofType() {
  3788. const node = this.startNode();
  3789. this.expect(types$1._typeof);
  3790. node.argument = this.flowParsePrimaryType();
  3791. return this.finishNode(node, "TypeofTypeAnnotation");
  3792. }
  3793. flowParseTupleType() {
  3794. const node = this.startNode();
  3795. node.types = [];
  3796. this.expect(types$1.bracketL);
  3797. while (this.state.pos < this.length && !this.match(types$1.bracketR)) {
  3798. node.types.push(this.flowParseType());
  3799. if (this.match(types$1.bracketR)) break;
  3800. this.expect(types$1.comma);
  3801. }
  3802. this.expect(types$1.bracketR);
  3803. return this.finishNode(node, "TupleTypeAnnotation");
  3804. }
  3805. flowParseFunctionTypeParam(first) {
  3806. let name = null;
  3807. let optional = false;
  3808. let typeAnnotation = null;
  3809. const node = this.startNode();
  3810. const lh = this.lookahead();
  3811. const isThis = this.state.type === types$1._this;
  3812. if (lh.type === types$1.colon || lh.type === types$1.question) {
  3813. if (isThis && !first) {
  3814. this.raise(node.start, FlowErrors.ThisParamMustBeFirst);
  3815. }
  3816. name = this.parseIdentifier(isThis);
  3817. if (this.eat(types$1.question)) {
  3818. optional = true;
  3819. if (isThis) {
  3820. this.raise(node.start, FlowErrors.ThisParamMayNotBeOptional);
  3821. }
  3822. }
  3823. typeAnnotation = this.flowParseTypeInitialiser();
  3824. } else {
  3825. typeAnnotation = this.flowParseType();
  3826. }
  3827. node.name = name;
  3828. node.optional = optional;
  3829. node.typeAnnotation = typeAnnotation;
  3830. return this.finishNode(node, "FunctionTypeParam");
  3831. }
  3832. reinterpretTypeAsFunctionTypeParam(type) {
  3833. const node = this.startNodeAt(type.start, type.loc.start);
  3834. node.name = null;
  3835. node.optional = false;
  3836. node.typeAnnotation = type;
  3837. return this.finishNode(node, "FunctionTypeParam");
  3838. }
  3839. flowParseFunctionTypeParams(params = []) {
  3840. let rest = null;
  3841. let _this = null;
  3842. if (this.match(types$1._this)) {
  3843. _this = this.flowParseFunctionTypeParam(true);
  3844. _this.name = null;
  3845. if (!this.match(types$1.parenR)) {
  3846. this.expect(types$1.comma);
  3847. }
  3848. }
  3849. while (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  3850. params.push(this.flowParseFunctionTypeParam(false));
  3851. if (!this.match(types$1.parenR)) {
  3852. this.expect(types$1.comma);
  3853. }
  3854. }
  3855. if (this.eat(types$1.ellipsis)) {
  3856. rest = this.flowParseFunctionTypeParam(false);
  3857. }
  3858. return {
  3859. params,
  3860. rest,
  3861. _this
  3862. };
  3863. }
  3864. flowIdentToTypeAnnotation(startPos, startLoc, node, id) {
  3865. switch (id.name) {
  3866. case "any":
  3867. return this.finishNode(node, "AnyTypeAnnotation");
  3868. case "bool":
  3869. case "boolean":
  3870. return this.finishNode(node, "BooleanTypeAnnotation");
  3871. case "mixed":
  3872. return this.finishNode(node, "MixedTypeAnnotation");
  3873. case "empty":
  3874. return this.finishNode(node, "EmptyTypeAnnotation");
  3875. case "number":
  3876. return this.finishNode(node, "NumberTypeAnnotation");
  3877. case "string":
  3878. return this.finishNode(node, "StringTypeAnnotation");
  3879. case "symbol":
  3880. return this.finishNode(node, "SymbolTypeAnnotation");
  3881. default:
  3882. this.checkNotUnderscore(id.name);
  3883. return this.flowParseGenericType(startPos, startLoc, id);
  3884. }
  3885. }
  3886. flowParsePrimaryType() {
  3887. const startPos = this.state.start;
  3888. const startLoc = this.state.startLoc;
  3889. const node = this.startNode();
  3890. let tmp;
  3891. let type;
  3892. let isGroupedType = false;
  3893. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  3894. switch (this.state.type) {
  3895. case types$1.name:
  3896. if (this.isContextual("interface")) {
  3897. return this.flowParseInterfaceType();
  3898. }
  3899. return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
  3900. case types$1.braceL:
  3901. return this.flowParseObjectType({
  3902. allowStatic: false,
  3903. allowExact: false,
  3904. allowSpread: true,
  3905. allowProto: false,
  3906. allowInexact: true
  3907. });
  3908. case types$1.braceBarL:
  3909. return this.flowParseObjectType({
  3910. allowStatic: false,
  3911. allowExact: true,
  3912. allowSpread: true,
  3913. allowProto: false,
  3914. allowInexact: false
  3915. });
  3916. case types$1.bracketL:
  3917. this.state.noAnonFunctionType = false;
  3918. type = this.flowParseTupleType();
  3919. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3920. return type;
  3921. case types$1.relational:
  3922. if (this.state.value === "<") {
  3923. node.typeParameters = this.flowParseTypeParameterDeclaration();
  3924. this.expect(types$1.parenL);
  3925. tmp = this.flowParseFunctionTypeParams();
  3926. node.params = tmp.params;
  3927. node.rest = tmp.rest;
  3928. node.this = tmp._this;
  3929. this.expect(types$1.parenR);
  3930. this.expect(types$1.arrow);
  3931. node.returnType = this.flowParseType();
  3932. return this.finishNode(node, "FunctionTypeAnnotation");
  3933. }
  3934. break;
  3935. case types$1.parenL:
  3936. this.next();
  3937. if (!this.match(types$1.parenR) && !this.match(types$1.ellipsis)) {
  3938. if (this.match(types$1.name) || this.match(types$1._this)) {
  3939. const token = this.lookahead().type;
  3940. isGroupedType = token !== types$1.question && token !== types$1.colon;
  3941. } else {
  3942. isGroupedType = true;
  3943. }
  3944. }
  3945. if (isGroupedType) {
  3946. this.state.noAnonFunctionType = false;
  3947. type = this.flowParseType();
  3948. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  3949. if (this.state.noAnonFunctionType || !(this.match(types$1.comma) || this.match(types$1.parenR) && this.lookahead().type === types$1.arrow)) {
  3950. this.expect(types$1.parenR);
  3951. return type;
  3952. } else {
  3953. this.eat(types$1.comma);
  3954. }
  3955. }
  3956. if (type) {
  3957. tmp = this.flowParseFunctionTypeParams([this.reinterpretTypeAsFunctionTypeParam(type)]);
  3958. } else {
  3959. tmp = this.flowParseFunctionTypeParams();
  3960. }
  3961. node.params = tmp.params;
  3962. node.rest = tmp.rest;
  3963. node.this = tmp._this;
  3964. this.expect(types$1.parenR);
  3965. this.expect(types$1.arrow);
  3966. node.returnType = this.flowParseType();
  3967. node.typeParameters = null;
  3968. return this.finishNode(node, "FunctionTypeAnnotation");
  3969. case types$1.string:
  3970. return this.parseLiteral(this.state.value, "StringLiteralTypeAnnotation");
  3971. case types$1._true:
  3972. case types$1._false:
  3973. node.value = this.match(types$1._true);
  3974. this.next();
  3975. return this.finishNode(node, "BooleanLiteralTypeAnnotation");
  3976. case types$1.plusMin:
  3977. if (this.state.value === "-") {
  3978. this.next();
  3979. if (this.match(types$1.num)) {
  3980. return this.parseLiteralAtNode(-this.state.value, "NumberLiteralTypeAnnotation", node);
  3981. }
  3982. if (this.match(types$1.bigint)) {
  3983. return this.parseLiteralAtNode(-this.state.value, "BigIntLiteralTypeAnnotation", node);
  3984. }
  3985. throw this.raise(this.state.start, FlowErrors.UnexpectedSubtractionOperand);
  3986. }
  3987. throw this.unexpected();
  3988. case types$1.num:
  3989. return this.parseLiteral(this.state.value, "NumberLiteralTypeAnnotation");
  3990. case types$1.bigint:
  3991. return this.parseLiteral(this.state.value, "BigIntLiteralTypeAnnotation");
  3992. case types$1._void:
  3993. this.next();
  3994. return this.finishNode(node, "VoidTypeAnnotation");
  3995. case types$1._null:
  3996. this.next();
  3997. return this.finishNode(node, "NullLiteralTypeAnnotation");
  3998. case types$1._this:
  3999. this.next();
  4000. return this.finishNode(node, "ThisTypeAnnotation");
  4001. case types$1.star:
  4002. this.next();
  4003. return this.finishNode(node, "ExistsTypeAnnotation");
  4004. default:
  4005. if (this.state.type.keyword === "typeof") {
  4006. return this.flowParseTypeofType();
  4007. } else if (this.state.type.keyword) {
  4008. const label = this.state.type.label;
  4009. this.next();
  4010. return super.createIdentifier(node, label);
  4011. }
  4012. }
  4013. throw this.unexpected();
  4014. }
  4015. flowParsePostfixType() {
  4016. const startPos = this.state.start;
  4017. const startLoc = this.state.startLoc;
  4018. let type = this.flowParsePrimaryType();
  4019. let seenOptionalIndexedAccess = false;
  4020. while ((this.match(types$1.bracketL) || this.match(types$1.questionDot)) && !this.canInsertSemicolon()) {
  4021. const node = this.startNodeAt(startPos, startLoc);
  4022. const optional = this.eat(types$1.questionDot);
  4023. seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional;
  4024. this.expect(types$1.bracketL);
  4025. if (!optional && this.match(types$1.bracketR)) {
  4026. node.elementType = type;
  4027. this.next();
  4028. type = this.finishNode(node, "ArrayTypeAnnotation");
  4029. } else {
  4030. node.objectType = type;
  4031. node.indexType = this.flowParseType();
  4032. this.expect(types$1.bracketR);
  4033. if (seenOptionalIndexedAccess) {
  4034. node.optional = optional;
  4035. type = this.finishNode(node, "OptionalIndexedAccessType");
  4036. } else {
  4037. type = this.finishNode(node, "IndexedAccessType");
  4038. }
  4039. }
  4040. }
  4041. return type;
  4042. }
  4043. flowParsePrefixType() {
  4044. const node = this.startNode();
  4045. if (this.eat(types$1.question)) {
  4046. node.typeAnnotation = this.flowParsePrefixType();
  4047. return this.finishNode(node, "NullableTypeAnnotation");
  4048. } else {
  4049. return this.flowParsePostfixType();
  4050. }
  4051. }
  4052. flowParseAnonFunctionWithoutParens() {
  4053. const param = this.flowParsePrefixType();
  4054. if (!this.state.noAnonFunctionType && this.eat(types$1.arrow)) {
  4055. const node = this.startNodeAt(param.start, param.loc.start);
  4056. node.params = [this.reinterpretTypeAsFunctionTypeParam(param)];
  4057. node.rest = null;
  4058. node.this = null;
  4059. node.returnType = this.flowParseType();
  4060. node.typeParameters = null;
  4061. return this.finishNode(node, "FunctionTypeAnnotation");
  4062. }
  4063. return param;
  4064. }
  4065. flowParseIntersectionType() {
  4066. const node = this.startNode();
  4067. this.eat(types$1.bitwiseAND);
  4068. const type = this.flowParseAnonFunctionWithoutParens();
  4069. node.types = [type];
  4070. while (this.eat(types$1.bitwiseAND)) {
  4071. node.types.push(this.flowParseAnonFunctionWithoutParens());
  4072. }
  4073. return node.types.length === 1 ? type : this.finishNode(node, "IntersectionTypeAnnotation");
  4074. }
  4075. flowParseUnionType() {
  4076. const node = this.startNode();
  4077. this.eat(types$1.bitwiseOR);
  4078. const type = this.flowParseIntersectionType();
  4079. node.types = [type];
  4080. while (this.eat(types$1.bitwiseOR)) {
  4081. node.types.push(this.flowParseIntersectionType());
  4082. }
  4083. return node.types.length === 1 ? type : this.finishNode(node, "UnionTypeAnnotation");
  4084. }
  4085. flowParseType() {
  4086. const oldInType = this.state.inType;
  4087. this.state.inType = true;
  4088. const type = this.flowParseUnionType();
  4089. this.state.inType = oldInType;
  4090. return type;
  4091. }
  4092. flowParseTypeOrImplicitInstantiation() {
  4093. if (this.state.type === types$1.name && this.state.value === "_") {
  4094. const startPos = this.state.start;
  4095. const startLoc = this.state.startLoc;
  4096. const node = this.parseIdentifier();
  4097. return this.flowParseGenericType(startPos, startLoc, node);
  4098. } else {
  4099. return this.flowParseType();
  4100. }
  4101. }
  4102. flowParseTypeAnnotation() {
  4103. const node = this.startNode();
  4104. node.typeAnnotation = this.flowParseTypeInitialiser();
  4105. return this.finishNode(node, "TypeAnnotation");
  4106. }
  4107. flowParseTypeAnnotatableIdentifier(allowPrimitiveOverride) {
  4108. const ident = allowPrimitiveOverride ? this.parseIdentifier() : this.flowParseRestrictedIdentifier();
  4109. if (this.match(types$1.colon)) {
  4110. ident.typeAnnotation = this.flowParseTypeAnnotation();
  4111. this.resetEndLocation(ident);
  4112. }
  4113. return ident;
  4114. }
  4115. typeCastToParameter(node) {
  4116. node.expression.typeAnnotation = node.typeAnnotation;
  4117. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  4118. return node.expression;
  4119. }
  4120. flowParseVariance() {
  4121. let variance = null;
  4122. if (this.match(types$1.plusMin)) {
  4123. variance = this.startNode();
  4124. if (this.state.value === "+") {
  4125. variance.kind = "plus";
  4126. } else {
  4127. variance.kind = "minus";
  4128. }
  4129. this.next();
  4130. this.finishNode(variance, "Variance");
  4131. }
  4132. return variance;
  4133. }
  4134. parseFunctionBody(node, allowExpressionBody, isMethod = false) {
  4135. if (allowExpressionBody) {
  4136. return this.forwardNoArrowParamsConversionAt(node, () => super.parseFunctionBody(node, true, isMethod));
  4137. }
  4138. return super.parseFunctionBody(node, false, isMethod);
  4139. }
  4140. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  4141. if (this.match(types$1.colon)) {
  4142. const typeNode = this.startNode();
  4143. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4144. node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null;
  4145. }
  4146. super.parseFunctionBodyAndFinish(node, type, isMethod);
  4147. }
  4148. parseStatement(context, topLevel) {
  4149. if (this.state.strict && this.match(types$1.name) && this.state.value === "interface") {
  4150. const lookahead = this.lookahead();
  4151. if (lookahead.type === types$1.name || isKeyword(lookahead.value)) {
  4152. const node = this.startNode();
  4153. this.next();
  4154. return this.flowParseInterface(node);
  4155. }
  4156. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  4157. const node = this.startNode();
  4158. this.next();
  4159. return this.flowParseEnumDeclaration(node);
  4160. }
  4161. const stmt = super.parseStatement(context, topLevel);
  4162. if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
  4163. this.flowPragma = null;
  4164. }
  4165. return stmt;
  4166. }
  4167. parseExpressionStatement(node, expr) {
  4168. if (expr.type === "Identifier") {
  4169. if (expr.name === "declare") {
  4170. if (this.match(types$1._class) || this.match(types$1.name) || this.match(types$1._function) || this.match(types$1._var) || this.match(types$1._export)) {
  4171. return this.flowParseDeclare(node);
  4172. }
  4173. } else if (this.match(types$1.name)) {
  4174. if (expr.name === "interface") {
  4175. return this.flowParseInterface(node);
  4176. } else if (expr.name === "type") {
  4177. return this.flowParseTypeAlias(node);
  4178. } else if (expr.name === "opaque") {
  4179. return this.flowParseOpaqueType(node, false);
  4180. }
  4181. }
  4182. }
  4183. return super.parseExpressionStatement(node, expr);
  4184. }
  4185. shouldParseExportDeclaration() {
  4186. return this.isContextual("type") || this.isContextual("interface") || this.isContextual("opaque") || this.shouldParseEnums() && this.isContextual("enum") || super.shouldParseExportDeclaration();
  4187. }
  4188. isExportDefaultSpecifier() {
  4189. if (this.match(types$1.name) && (this.state.value === "type" || this.state.value === "interface" || this.state.value === "opaque" || this.shouldParseEnums() && this.state.value === "enum")) {
  4190. return false;
  4191. }
  4192. return super.isExportDefaultSpecifier();
  4193. }
  4194. parseExportDefaultExpression() {
  4195. if (this.shouldParseEnums() && this.isContextual("enum")) {
  4196. const node = this.startNode();
  4197. this.next();
  4198. return this.flowParseEnumDeclaration(node);
  4199. }
  4200. return super.parseExportDefaultExpression();
  4201. }
  4202. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  4203. if (!this.match(types$1.question)) return expr;
  4204. if (this.state.maybeInArrowParameters) {
  4205. const nextCh = this.lookaheadCharCode();
  4206. if (nextCh === 44 || nextCh === 61 || nextCh === 58 || nextCh === 41) {
  4207. this.setOptionalParametersError(refExpressionErrors);
  4208. return expr;
  4209. }
  4210. }
  4211. this.expect(types$1.question);
  4212. const state = this.state.clone();
  4213. const originalNoArrowAt = this.state.noArrowAt;
  4214. const node = this.startNodeAt(startPos, startLoc);
  4215. let {
  4216. consequent,
  4217. failed
  4218. } = this.tryParseConditionalConsequent();
  4219. let [valid, invalid] = this.getArrowLikeExpressions(consequent);
  4220. if (failed || invalid.length > 0) {
  4221. const noArrowAt = [...originalNoArrowAt];
  4222. if (invalid.length > 0) {
  4223. this.state = state;
  4224. this.state.noArrowAt = noArrowAt;
  4225. for (let i = 0; i < invalid.length; i++) {
  4226. noArrowAt.push(invalid[i].start);
  4227. }
  4228. ({
  4229. consequent,
  4230. failed
  4231. } = this.tryParseConditionalConsequent());
  4232. [valid, invalid] = this.getArrowLikeExpressions(consequent);
  4233. }
  4234. if (failed && valid.length > 1) {
  4235. this.raise(state.start, FlowErrors.AmbiguousConditionalArrow);
  4236. }
  4237. if (failed && valid.length === 1) {
  4238. this.state = state;
  4239. noArrowAt.push(valid[0].start);
  4240. this.state.noArrowAt = noArrowAt;
  4241. ({
  4242. consequent,
  4243. failed
  4244. } = this.tryParseConditionalConsequent());
  4245. }
  4246. }
  4247. this.getArrowLikeExpressions(consequent, true);
  4248. this.state.noArrowAt = originalNoArrowAt;
  4249. this.expect(types$1.colon);
  4250. node.test = expr;
  4251. node.consequent = consequent;
  4252. node.alternate = this.forwardNoArrowParamsConversionAt(node, () => this.parseMaybeAssign(undefined, undefined));
  4253. return this.finishNode(node, "ConditionalExpression");
  4254. }
  4255. tryParseConditionalConsequent() {
  4256. this.state.noArrowParamsConversionAt.push(this.state.start);
  4257. const consequent = this.parseMaybeAssignAllowIn();
  4258. const failed = !this.match(types$1.colon);
  4259. this.state.noArrowParamsConversionAt.pop();
  4260. return {
  4261. consequent,
  4262. failed
  4263. };
  4264. }
  4265. getArrowLikeExpressions(node, disallowInvalid) {
  4266. const stack = [node];
  4267. const arrows = [];
  4268. while (stack.length !== 0) {
  4269. const node = stack.pop();
  4270. if (node.type === "ArrowFunctionExpression") {
  4271. if (node.typeParameters || !node.returnType) {
  4272. this.finishArrowValidation(node);
  4273. } else {
  4274. arrows.push(node);
  4275. }
  4276. stack.push(node.body);
  4277. } else if (node.type === "ConditionalExpression") {
  4278. stack.push(node.consequent);
  4279. stack.push(node.alternate);
  4280. }
  4281. }
  4282. if (disallowInvalid) {
  4283. arrows.forEach(node => this.finishArrowValidation(node));
  4284. return [arrows, []];
  4285. }
  4286. return partition(arrows, node => node.params.every(param => this.isAssignable(param, true)));
  4287. }
  4288. finishArrowValidation(node) {
  4289. var _node$extra;
  4290. this.toAssignableList(node.params, (_node$extra = node.extra) == null ? void 0 : _node$extra.trailingComma, false);
  4291. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  4292. super.checkParams(node, false, true);
  4293. this.scope.exit();
  4294. }
  4295. forwardNoArrowParamsConversionAt(node, parse) {
  4296. let result;
  4297. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  4298. this.state.noArrowParamsConversionAt.push(this.state.start);
  4299. result = parse();
  4300. this.state.noArrowParamsConversionAt.pop();
  4301. } else {
  4302. result = parse();
  4303. }
  4304. return result;
  4305. }
  4306. parseParenItem(node, startPos, startLoc) {
  4307. node = super.parseParenItem(node, startPos, startLoc);
  4308. if (this.eat(types$1.question)) {
  4309. node.optional = true;
  4310. this.resetEndLocation(node);
  4311. }
  4312. if (this.match(types$1.colon)) {
  4313. const typeCastNode = this.startNodeAt(startPos, startLoc);
  4314. typeCastNode.expression = node;
  4315. typeCastNode.typeAnnotation = this.flowParseTypeAnnotation();
  4316. return this.finishNode(typeCastNode, "TypeCastExpression");
  4317. }
  4318. return node;
  4319. }
  4320. assertModuleNodeAllowed(node) {
  4321. if (node.type === "ImportDeclaration" && (node.importKind === "type" || node.importKind === "typeof") || node.type === "ExportNamedDeclaration" && node.exportKind === "type" || node.type === "ExportAllDeclaration" && node.exportKind === "type") {
  4322. return;
  4323. }
  4324. super.assertModuleNodeAllowed(node);
  4325. }
  4326. parseExport(node) {
  4327. const decl = super.parseExport(node);
  4328. if (decl.type === "ExportNamedDeclaration" || decl.type === "ExportAllDeclaration") {
  4329. decl.exportKind = decl.exportKind || "value";
  4330. }
  4331. return decl;
  4332. }
  4333. parseExportDeclaration(node) {
  4334. if (this.isContextual("type")) {
  4335. node.exportKind = "type";
  4336. const declarationNode = this.startNode();
  4337. this.next();
  4338. if (this.match(types$1.braceL)) {
  4339. node.specifiers = this.parseExportSpecifiers();
  4340. this.parseExportFrom(node);
  4341. return null;
  4342. } else {
  4343. return this.flowParseTypeAlias(declarationNode);
  4344. }
  4345. } else if (this.isContextual("opaque")) {
  4346. node.exportKind = "type";
  4347. const declarationNode = this.startNode();
  4348. this.next();
  4349. return this.flowParseOpaqueType(declarationNode, false);
  4350. } else if (this.isContextual("interface")) {
  4351. node.exportKind = "type";
  4352. const declarationNode = this.startNode();
  4353. this.next();
  4354. return this.flowParseInterface(declarationNode);
  4355. } else if (this.shouldParseEnums() && this.isContextual("enum")) {
  4356. node.exportKind = "value";
  4357. const declarationNode = this.startNode();
  4358. this.next();
  4359. return this.flowParseEnumDeclaration(declarationNode);
  4360. } else {
  4361. return super.parseExportDeclaration(node);
  4362. }
  4363. }
  4364. eatExportStar(node) {
  4365. if (super.eatExportStar(...arguments)) return true;
  4366. if (this.isContextual("type") && this.lookahead().type === types$1.star) {
  4367. node.exportKind = "type";
  4368. this.next();
  4369. this.next();
  4370. return true;
  4371. }
  4372. return false;
  4373. }
  4374. maybeParseExportNamespaceSpecifier(node) {
  4375. const pos = this.state.start;
  4376. const hasNamespace = super.maybeParseExportNamespaceSpecifier(node);
  4377. if (hasNamespace && node.exportKind === "type") {
  4378. this.unexpected(pos);
  4379. }
  4380. return hasNamespace;
  4381. }
  4382. parseClassId(node, isStatement, optionalId) {
  4383. super.parseClassId(node, isStatement, optionalId);
  4384. if (this.isRelational("<")) {
  4385. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4386. }
  4387. }
  4388. parseClassMember(classBody, member, state) {
  4389. const pos = this.state.start;
  4390. if (this.isContextual("declare")) {
  4391. if (this.parseClassMemberFromModifier(classBody, member)) {
  4392. return;
  4393. }
  4394. member.declare = true;
  4395. }
  4396. super.parseClassMember(classBody, member, state);
  4397. if (member.declare) {
  4398. if (member.type !== "ClassProperty" && member.type !== "ClassPrivateProperty" && member.type !== "PropertyDefinition") {
  4399. this.raise(pos, FlowErrors.DeclareClassElement);
  4400. } else if (member.value) {
  4401. this.raise(member.value.start, FlowErrors.DeclareClassFieldInitializer);
  4402. }
  4403. }
  4404. }
  4405. isIterator(word) {
  4406. return word === "iterator" || word === "asyncIterator";
  4407. }
  4408. readIterator() {
  4409. const word = super.readWord1();
  4410. const fullWord = "@@" + word;
  4411. if (!this.isIterator(word) || !this.state.inType) {
  4412. this.raise(this.state.pos, ErrorMessages.InvalidIdentifier, fullWord);
  4413. }
  4414. this.finishToken(types$1.name, fullWord);
  4415. }
  4416. getTokenFromCode(code) {
  4417. const next = this.input.charCodeAt(this.state.pos + 1);
  4418. if (code === 123 && next === 124) {
  4419. return this.finishOp(types$1.braceBarL, 2);
  4420. } else if (this.state.inType && (code === 62 || code === 60)) {
  4421. return this.finishOp(types$1.relational, 1);
  4422. } else if (this.state.inType && code === 63) {
  4423. if (next === 46) {
  4424. return this.finishOp(types$1.questionDot, 2);
  4425. }
  4426. return this.finishOp(types$1.question, 1);
  4427. } else if (isIteratorStart(code, next)) {
  4428. this.state.pos += 2;
  4429. return this.readIterator();
  4430. } else {
  4431. return super.getTokenFromCode(code);
  4432. }
  4433. }
  4434. isAssignable(node, isBinding) {
  4435. if (node.type === "TypeCastExpression") {
  4436. return this.isAssignable(node.expression, isBinding);
  4437. } else {
  4438. return super.isAssignable(node, isBinding);
  4439. }
  4440. }
  4441. toAssignable(node, isLHS = false) {
  4442. if (node.type === "TypeCastExpression") {
  4443. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  4444. } else {
  4445. return super.toAssignable(node, isLHS);
  4446. }
  4447. }
  4448. toAssignableList(exprList, trailingCommaPos, isLHS) {
  4449. for (let i = 0; i < exprList.length; i++) {
  4450. const expr = exprList[i];
  4451. if ((expr == null ? void 0 : expr.type) === "TypeCastExpression") {
  4452. exprList[i] = this.typeCastToParameter(expr);
  4453. }
  4454. }
  4455. return super.toAssignableList(exprList, trailingCommaPos, isLHS);
  4456. }
  4457. toReferencedList(exprList, isParenthesizedExpr) {
  4458. for (let i = 0; i < exprList.length; i++) {
  4459. var _expr$extra;
  4460. const expr = exprList[i];
  4461. if (expr && expr.type === "TypeCastExpression" && !((_expr$extra = expr.extra) != null && _expr$extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr)) {
  4462. this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern);
  4463. }
  4464. }
  4465. return exprList;
  4466. }
  4467. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  4468. const node = super.parseArrayLike(close, canBePattern, isTuple, refExpressionErrors);
  4469. if (canBePattern && !this.state.maybeInArrowParameters) {
  4470. this.toReferencedList(node.elements);
  4471. }
  4472. return node;
  4473. }
  4474. checkLVal(expr, ...args) {
  4475. if (expr.type !== "TypeCastExpression") {
  4476. return super.checkLVal(expr, ...args);
  4477. }
  4478. }
  4479. parseClassProperty(node) {
  4480. if (this.match(types$1.colon)) {
  4481. node.typeAnnotation = this.flowParseTypeAnnotation();
  4482. }
  4483. return super.parseClassProperty(node);
  4484. }
  4485. parseClassPrivateProperty(node) {
  4486. if (this.match(types$1.colon)) {
  4487. node.typeAnnotation = this.flowParseTypeAnnotation();
  4488. }
  4489. return super.parseClassPrivateProperty(node);
  4490. }
  4491. isClassMethod() {
  4492. return this.isRelational("<") || super.isClassMethod();
  4493. }
  4494. isClassProperty() {
  4495. return this.match(types$1.colon) || super.isClassProperty();
  4496. }
  4497. isNonstaticConstructor(method) {
  4498. return !this.match(types$1.colon) && super.isNonstaticConstructor(method);
  4499. }
  4500. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  4501. if (method.variance) {
  4502. this.unexpected(method.variance.start);
  4503. }
  4504. delete method.variance;
  4505. if (this.isRelational("<")) {
  4506. method.typeParameters = this.flowParseTypeParameterDeclaration();
  4507. }
  4508. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  4509. if (method.params && isConstructor) {
  4510. const params = method.params;
  4511. if (params.length > 0 && this.isThisParam(params[0])) {
  4512. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  4513. }
  4514. } else if (method.type === "MethodDefinition" && isConstructor && method.value.params) {
  4515. const params = method.value.params;
  4516. if (params.length > 0 && this.isThisParam(params[0])) {
  4517. this.raise(method.start, FlowErrors.ThisParamBannedInConstructor);
  4518. }
  4519. }
  4520. }
  4521. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  4522. if (method.variance) {
  4523. this.unexpected(method.variance.start);
  4524. }
  4525. delete method.variance;
  4526. if (this.isRelational("<")) {
  4527. method.typeParameters = this.flowParseTypeParameterDeclaration();
  4528. }
  4529. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  4530. }
  4531. parseClassSuper(node) {
  4532. super.parseClassSuper(node);
  4533. if (node.superClass && this.isRelational("<")) {
  4534. node.superTypeParameters = this.flowParseTypeParameterInstantiation();
  4535. }
  4536. if (this.isContextual("implements")) {
  4537. this.next();
  4538. const implemented = node.implements = [];
  4539. do {
  4540. const node = this.startNode();
  4541. node.id = this.flowParseRestrictedIdentifier(true);
  4542. if (this.isRelational("<")) {
  4543. node.typeParameters = this.flowParseTypeParameterInstantiation();
  4544. } else {
  4545. node.typeParameters = null;
  4546. }
  4547. implemented.push(this.finishNode(node, "ClassImplements"));
  4548. } while (this.eat(types$1.comma));
  4549. }
  4550. }
  4551. checkGetterSetterParams(method) {
  4552. super.checkGetterSetterParams(method);
  4553. const params = this.getObjectOrClassMethodParams(method);
  4554. if (params.length > 0) {
  4555. const param = params[0];
  4556. if (this.isThisParam(param) && method.kind === "get") {
  4557. this.raise(param.start, FlowErrors.GetterMayNotHaveThisParam);
  4558. } else if (this.isThisParam(param)) {
  4559. this.raise(param.start, FlowErrors.SetterMayNotHaveThisParam);
  4560. }
  4561. }
  4562. }
  4563. parsePropertyName(node, isPrivateNameAllowed) {
  4564. const variance = this.flowParseVariance();
  4565. const key = super.parsePropertyName(node, isPrivateNameAllowed);
  4566. node.variance = variance;
  4567. return key;
  4568. }
  4569. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  4570. if (prop.variance) {
  4571. this.unexpected(prop.variance.start);
  4572. }
  4573. delete prop.variance;
  4574. let typeParameters;
  4575. if (this.isRelational("<") && !isAccessor) {
  4576. typeParameters = this.flowParseTypeParameterDeclaration();
  4577. if (!this.match(types$1.parenL)) this.unexpected();
  4578. }
  4579. super.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  4580. if (typeParameters) {
  4581. (prop.value || prop).typeParameters = typeParameters;
  4582. }
  4583. }
  4584. parseAssignableListItemTypes(param) {
  4585. if (this.eat(types$1.question)) {
  4586. if (param.type !== "Identifier") {
  4587. this.raise(param.start, FlowErrors.PatternIsOptional);
  4588. }
  4589. if (this.isThisParam(param)) {
  4590. this.raise(param.start, FlowErrors.ThisParamMayNotBeOptional);
  4591. }
  4592. param.optional = true;
  4593. }
  4594. if (this.match(types$1.colon)) {
  4595. param.typeAnnotation = this.flowParseTypeAnnotation();
  4596. } else if (this.isThisParam(param)) {
  4597. this.raise(param.start, FlowErrors.ThisParamAnnotationRequired);
  4598. }
  4599. if (this.match(types$1.eq) && this.isThisParam(param)) {
  4600. this.raise(param.start, FlowErrors.ThisParamNoDefault);
  4601. }
  4602. this.resetEndLocation(param);
  4603. return param;
  4604. }
  4605. parseMaybeDefault(startPos, startLoc, left) {
  4606. const node = super.parseMaybeDefault(startPos, startLoc, left);
  4607. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  4608. this.raise(node.typeAnnotation.start, FlowErrors.TypeBeforeInitializer);
  4609. }
  4610. return node;
  4611. }
  4612. shouldParseDefaultImport(node) {
  4613. if (!hasTypeImportKind(node)) {
  4614. return super.shouldParseDefaultImport(node);
  4615. }
  4616. return isMaybeDefaultImport(this.state);
  4617. }
  4618. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  4619. specifier.local = hasTypeImportKind(node) ? this.flowParseRestrictedIdentifier(true, true) : this.parseIdentifier();
  4620. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  4621. node.specifiers.push(this.finishNode(specifier, type));
  4622. }
  4623. maybeParseDefaultImportSpecifier(node) {
  4624. node.importKind = "value";
  4625. let kind = null;
  4626. if (this.match(types$1._typeof)) {
  4627. kind = "typeof";
  4628. } else if (this.isContextual("type")) {
  4629. kind = "type";
  4630. }
  4631. if (kind) {
  4632. const lh = this.lookahead();
  4633. if (kind === "type" && lh.type === types$1.star) {
  4634. this.unexpected(lh.start);
  4635. }
  4636. if (isMaybeDefaultImport(lh) || lh.type === types$1.braceL || lh.type === types$1.star) {
  4637. this.next();
  4638. node.importKind = kind;
  4639. }
  4640. }
  4641. return super.maybeParseDefaultImportSpecifier(node);
  4642. }
  4643. parseImportSpecifier(node) {
  4644. const specifier = this.startNode();
  4645. const firstIdentIsString = this.match(types$1.string);
  4646. const firstIdent = this.parseModuleExportName();
  4647. let specifierTypeKind = null;
  4648. if (firstIdent.type === "Identifier") {
  4649. if (firstIdent.name === "type") {
  4650. specifierTypeKind = "type";
  4651. } else if (firstIdent.name === "typeof") {
  4652. specifierTypeKind = "typeof";
  4653. }
  4654. }
  4655. let isBinding = false;
  4656. if (this.isContextual("as") && !this.isLookaheadContextual("as")) {
  4657. const as_ident = this.parseIdentifier(true);
  4658. if (specifierTypeKind !== null && !this.match(types$1.name) && !this.state.type.keyword) {
  4659. specifier.imported = as_ident;
  4660. specifier.importKind = specifierTypeKind;
  4661. specifier.local = cloneIdentifier(as_ident);
  4662. } else {
  4663. specifier.imported = firstIdent;
  4664. specifier.importKind = null;
  4665. specifier.local = this.parseIdentifier();
  4666. }
  4667. } else {
  4668. if (specifierTypeKind !== null && (this.match(types$1.name) || this.state.type.keyword)) {
  4669. specifier.imported = this.parseIdentifier(true);
  4670. specifier.importKind = specifierTypeKind;
  4671. } else {
  4672. if (firstIdentIsString) {
  4673. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, firstIdent.value);
  4674. }
  4675. specifier.imported = firstIdent;
  4676. specifier.importKind = null;
  4677. }
  4678. if (this.eatContextual("as")) {
  4679. specifier.local = this.parseIdentifier();
  4680. } else {
  4681. isBinding = true;
  4682. specifier.local = cloneIdentifier(specifier.imported);
  4683. }
  4684. }
  4685. const nodeIsTypeImport = hasTypeImportKind(node);
  4686. const specifierIsTypeImport = hasTypeImportKind(specifier);
  4687. if (nodeIsTypeImport && specifierIsTypeImport) {
  4688. this.raise(specifier.start, FlowErrors.ImportTypeShorthandOnlyInPureImport);
  4689. }
  4690. if (nodeIsTypeImport || specifierIsTypeImport) {
  4691. this.checkReservedType(specifier.local.name, specifier.local.start, true);
  4692. }
  4693. if (isBinding && !nodeIsTypeImport && !specifierIsTypeImport) {
  4694. this.checkReservedWord(specifier.local.name, specifier.start, true, true);
  4695. }
  4696. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  4697. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  4698. }
  4699. parseBindingAtom() {
  4700. switch (this.state.type) {
  4701. case types$1._this:
  4702. return this.parseIdentifier(true);
  4703. default:
  4704. return super.parseBindingAtom();
  4705. }
  4706. }
  4707. parseFunctionParams(node, allowModifiers) {
  4708. const kind = node.kind;
  4709. if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
  4710. node.typeParameters = this.flowParseTypeParameterDeclaration();
  4711. }
  4712. super.parseFunctionParams(node, allowModifiers);
  4713. }
  4714. parseVarId(decl, kind) {
  4715. super.parseVarId(decl, kind);
  4716. if (this.match(types$1.colon)) {
  4717. decl.id.typeAnnotation = this.flowParseTypeAnnotation();
  4718. this.resetEndLocation(decl.id);
  4719. }
  4720. }
  4721. parseAsyncArrowFromCallExpression(node, call) {
  4722. if (this.match(types$1.colon)) {
  4723. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4724. this.state.noAnonFunctionType = true;
  4725. node.returnType = this.flowParseTypeAnnotation();
  4726. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4727. }
  4728. return super.parseAsyncArrowFromCallExpression(node, call);
  4729. }
  4730. shouldParseAsyncArrow() {
  4731. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  4732. }
  4733. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  4734. var _jsx;
  4735. let state = null;
  4736. let jsx;
  4737. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  4738. state = this.state.clone();
  4739. jsx = this.tryParse(() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse), state);
  4740. if (!jsx.error) return jsx.node;
  4741. const {
  4742. context
  4743. } = this.state;
  4744. const curContext = context[context.length - 1];
  4745. if (curContext === types.j_oTag) {
  4746. context.length -= 2;
  4747. } else if (curContext === types.j_expr) {
  4748. context.length -= 1;
  4749. }
  4750. }
  4751. if ((_jsx = jsx) != null && _jsx.error || this.isRelational("<")) {
  4752. var _jsx2, _jsx3;
  4753. state = state || this.state.clone();
  4754. let typeParameters;
  4755. const arrow = this.tryParse(abort => {
  4756. var _arrowExpression$extr;
  4757. typeParameters = this.flowParseTypeParameterDeclaration();
  4758. const arrowExpression = this.forwardNoArrowParamsConversionAt(typeParameters, () => {
  4759. const result = super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  4760. this.resetStartLocationFromNode(result, typeParameters);
  4761. return result;
  4762. });
  4763. if ((_arrowExpression$extr = arrowExpression.extra) != null && _arrowExpression$extr.parenthesized) abort();
  4764. const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
  4765. if (expr.type !== "ArrowFunctionExpression") abort();
  4766. expr.typeParameters = typeParameters;
  4767. this.resetStartLocationFromNode(expr, typeParameters);
  4768. return arrowExpression;
  4769. }, state);
  4770. let arrowExpression = null;
  4771. if (arrow.node && this.maybeUnwrapTypeCastExpression(arrow.node).type === "ArrowFunctionExpression") {
  4772. if (!arrow.error && !arrow.aborted) {
  4773. if (arrow.node.async) {
  4774. this.raise(typeParameters.start, FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction);
  4775. }
  4776. return arrow.node;
  4777. }
  4778. arrowExpression = arrow.node;
  4779. }
  4780. if ((_jsx2 = jsx) != null && _jsx2.node) {
  4781. this.state = jsx.failState;
  4782. return jsx.node;
  4783. }
  4784. if (arrowExpression) {
  4785. this.state = arrow.failState;
  4786. return arrowExpression;
  4787. }
  4788. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  4789. if (arrow.thrown) throw arrow.error;
  4790. throw this.raise(typeParameters.start, FlowErrors.UnexpectedTokenAfterTypeParameter);
  4791. }
  4792. return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
  4793. }
  4794. parseArrow(node) {
  4795. if (this.match(types$1.colon)) {
  4796. const result = this.tryParse(() => {
  4797. const oldNoAnonFunctionType = this.state.noAnonFunctionType;
  4798. this.state.noAnonFunctionType = true;
  4799. const typeNode = this.startNode();
  4800. [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser();
  4801. this.state.noAnonFunctionType = oldNoAnonFunctionType;
  4802. if (this.canInsertSemicolon()) this.unexpected();
  4803. if (!this.match(types$1.arrow)) this.unexpected();
  4804. return typeNode;
  4805. });
  4806. if (result.thrown) return null;
  4807. if (result.error) this.state = result.failState;
  4808. node.returnType = result.node.typeAnnotation ? this.finishNode(result.node, "TypeAnnotation") : null;
  4809. }
  4810. return super.parseArrow(node);
  4811. }
  4812. shouldParseArrow(params) {
  4813. return this.match(types$1.colon) || super.shouldParseArrow(params);
  4814. }
  4815. setArrowFunctionParameters(node, params) {
  4816. if (this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  4817. node.params = params;
  4818. } else {
  4819. super.setArrowFunctionParameters(node, params);
  4820. }
  4821. }
  4822. checkParams(node, allowDuplicates, isArrowFunction) {
  4823. if (isArrowFunction && this.state.noArrowParamsConversionAt.indexOf(node.start) !== -1) {
  4824. return;
  4825. }
  4826. for (let i = 0; i < node.params.length; i++) {
  4827. if (this.isThisParam(node.params[i]) && i > 0) {
  4828. this.raise(node.params[i].start, FlowErrors.ThisParamMustBeFirst);
  4829. }
  4830. }
  4831. return super.checkParams(...arguments);
  4832. }
  4833. parseParenAndDistinguishExpression(canBeArrow) {
  4834. return super.parseParenAndDistinguishExpression(canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1);
  4835. }
  4836. parseSubscripts(base, startPos, startLoc, noCalls) {
  4837. if (base.type === "Identifier" && base.name === "async" && this.state.noArrowAt.indexOf(startPos) !== -1) {
  4838. this.next();
  4839. const node = this.startNodeAt(startPos, startLoc);
  4840. node.callee = base;
  4841. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  4842. base = this.finishNode(node, "CallExpression");
  4843. } else if (base.type === "Identifier" && base.name === "async" && this.isRelational("<")) {
  4844. const state = this.state.clone();
  4845. const arrow = this.tryParse(abort => this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || abort(), state);
  4846. if (!arrow.error && !arrow.aborted) return arrow.node;
  4847. const result = this.tryParse(() => super.parseSubscripts(base, startPos, startLoc, noCalls), state);
  4848. if (result.node && !result.error) return result.node;
  4849. if (arrow.node) {
  4850. this.state = arrow.failState;
  4851. return arrow.node;
  4852. }
  4853. if (result.node) {
  4854. this.state = result.failState;
  4855. return result.node;
  4856. }
  4857. throw arrow.error || result.error;
  4858. }
  4859. return super.parseSubscripts(base, startPos, startLoc, noCalls);
  4860. }
  4861. parseSubscript(base, startPos, startLoc, noCalls, subscriptState) {
  4862. if (this.match(types$1.questionDot) && this.isLookaheadToken_lt()) {
  4863. subscriptState.optionalChainMember = true;
  4864. if (noCalls) {
  4865. subscriptState.stop = true;
  4866. return base;
  4867. }
  4868. this.next();
  4869. const node = this.startNodeAt(startPos, startLoc);
  4870. node.callee = base;
  4871. node.typeArguments = this.flowParseTypeParameterInstantiation();
  4872. this.expect(types$1.parenL);
  4873. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  4874. node.optional = true;
  4875. return this.finishCallExpression(node, true);
  4876. } else if (!noCalls && this.shouldParseTypes() && this.isRelational("<")) {
  4877. const node = this.startNodeAt(startPos, startLoc);
  4878. node.callee = base;
  4879. const result = this.tryParse(() => {
  4880. node.typeArguments = this.flowParseTypeParameterInstantiationCallOrNew();
  4881. this.expect(types$1.parenL);
  4882. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  4883. if (subscriptState.optionalChainMember) node.optional = false;
  4884. return this.finishCallExpression(node, subscriptState.optionalChainMember);
  4885. });
  4886. if (result.node) {
  4887. if (result.error) this.state = result.failState;
  4888. return result.node;
  4889. }
  4890. }
  4891. return super.parseSubscript(base, startPos, startLoc, noCalls, subscriptState);
  4892. }
  4893. parseNewArguments(node) {
  4894. let targs = null;
  4895. if (this.shouldParseTypes() && this.isRelational("<")) {
  4896. targs = this.tryParse(() => this.flowParseTypeParameterInstantiationCallOrNew()).node;
  4897. }
  4898. node.typeArguments = targs;
  4899. super.parseNewArguments(node);
  4900. }
  4901. parseAsyncArrowWithTypeParameters(startPos, startLoc) {
  4902. const node = this.startNodeAt(startPos, startLoc);
  4903. this.parseFunctionParams(node);
  4904. if (!this.parseArrow(node)) return;
  4905. return this.parseArrowExpression(node, undefined, true);
  4906. }
  4907. readToken_mult_modulo(code) {
  4908. const next = this.input.charCodeAt(this.state.pos + 1);
  4909. if (code === 42 && next === 47 && this.state.hasFlowComment) {
  4910. this.state.hasFlowComment = false;
  4911. this.state.pos += 2;
  4912. this.nextToken();
  4913. return;
  4914. }
  4915. super.readToken_mult_modulo(code);
  4916. }
  4917. readToken_pipe_amp(code) {
  4918. const next = this.input.charCodeAt(this.state.pos + 1);
  4919. if (code === 124 && next === 125) {
  4920. this.finishOp(types$1.braceBarR, 2);
  4921. return;
  4922. }
  4923. super.readToken_pipe_amp(code);
  4924. }
  4925. parseTopLevel(file, program) {
  4926. const fileNode = super.parseTopLevel(file, program);
  4927. if (this.state.hasFlowComment) {
  4928. this.raise(this.state.pos, FlowErrors.UnterminatedFlowComment);
  4929. }
  4930. return fileNode;
  4931. }
  4932. skipBlockComment() {
  4933. if (this.hasPlugin("flowComments") && this.skipFlowComment()) {
  4934. if (this.state.hasFlowComment) {
  4935. this.unexpected(null, FlowErrors.NestedFlowComment);
  4936. }
  4937. this.hasFlowCommentCompletion();
  4938. this.state.pos += this.skipFlowComment();
  4939. this.state.hasFlowComment = true;
  4940. return;
  4941. }
  4942. if (this.state.hasFlowComment) {
  4943. const end = this.input.indexOf("*-/", this.state.pos += 2);
  4944. if (end === -1) {
  4945. throw this.raise(this.state.pos - 2, ErrorMessages.UnterminatedComment);
  4946. }
  4947. this.state.pos = end + 3;
  4948. return;
  4949. }
  4950. return super.skipBlockComment();
  4951. }
  4952. skipFlowComment() {
  4953. const {
  4954. pos
  4955. } = this.state;
  4956. let shiftToFirstNonWhiteSpace = 2;
  4957. while ([32, 9].includes(this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace))) {
  4958. shiftToFirstNonWhiteSpace++;
  4959. }
  4960. const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
  4961. const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
  4962. if (ch2 === 58 && ch3 === 58) {
  4963. return shiftToFirstNonWhiteSpace + 2;
  4964. }
  4965. if (this.input.slice(shiftToFirstNonWhiteSpace + pos, shiftToFirstNonWhiteSpace + pos + 12) === "flow-include") {
  4966. return shiftToFirstNonWhiteSpace + 12;
  4967. }
  4968. if (ch2 === 58 && ch3 !== 58) {
  4969. return shiftToFirstNonWhiteSpace;
  4970. }
  4971. return false;
  4972. }
  4973. hasFlowCommentCompletion() {
  4974. const end = this.input.indexOf("*/", this.state.pos);
  4975. if (end === -1) {
  4976. throw this.raise(this.state.pos, ErrorMessages.UnterminatedComment);
  4977. }
  4978. }
  4979. flowEnumErrorBooleanMemberNotInitialized(pos, {
  4980. enumName,
  4981. memberName
  4982. }) {
  4983. this.raise(pos, FlowErrors.EnumBooleanMemberNotInitialized, memberName, enumName);
  4984. }
  4985. flowEnumErrorInvalidMemberName(pos, {
  4986. enumName,
  4987. memberName
  4988. }) {
  4989. const suggestion = memberName[0].toUpperCase() + memberName.slice(1);
  4990. this.raise(pos, FlowErrors.EnumInvalidMemberName, memberName, suggestion, enumName);
  4991. }
  4992. flowEnumErrorDuplicateMemberName(pos, {
  4993. enumName,
  4994. memberName
  4995. }) {
  4996. this.raise(pos, FlowErrors.EnumDuplicateMemberName, memberName, enumName);
  4997. }
  4998. flowEnumErrorInconsistentMemberValues(pos, {
  4999. enumName
  5000. }) {
  5001. this.raise(pos, FlowErrors.EnumInconsistentMemberValues, enumName);
  5002. }
  5003. flowEnumErrorInvalidExplicitType(pos, {
  5004. enumName,
  5005. suppliedType
  5006. }) {
  5007. return this.raise(pos, suppliedType === null ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied : FlowErrors.EnumInvalidExplicitType, enumName, suppliedType);
  5008. }
  5009. flowEnumErrorInvalidMemberInitializer(pos, {
  5010. enumName,
  5011. explicitType,
  5012. memberName
  5013. }) {
  5014. let message = null;
  5015. switch (explicitType) {
  5016. case "boolean":
  5017. case "number":
  5018. case "string":
  5019. message = FlowErrors.EnumInvalidMemberInitializerPrimaryType;
  5020. break;
  5021. case "symbol":
  5022. message = FlowErrors.EnumInvalidMemberInitializerSymbolType;
  5023. break;
  5024. default:
  5025. message = FlowErrors.EnumInvalidMemberInitializerUnknownType;
  5026. }
  5027. return this.raise(pos, message, enumName, memberName, explicitType);
  5028. }
  5029. flowEnumErrorNumberMemberNotInitialized(pos, {
  5030. enumName,
  5031. memberName
  5032. }) {
  5033. this.raise(pos, FlowErrors.EnumNumberMemberNotInitialized, enumName, memberName);
  5034. }
  5035. flowEnumErrorStringMemberInconsistentlyInitailized(pos, {
  5036. enumName
  5037. }) {
  5038. this.raise(pos, FlowErrors.EnumStringMemberInconsistentlyInitailized, enumName);
  5039. }
  5040. flowEnumMemberInit() {
  5041. const startPos = this.state.start;
  5042. const endOfInit = () => this.match(types$1.comma) || this.match(types$1.braceR);
  5043. switch (this.state.type) {
  5044. case types$1.num:
  5045. {
  5046. const literal = this.parseNumericLiteral(this.state.value);
  5047. if (endOfInit()) {
  5048. return {
  5049. type: "number",
  5050. pos: literal.start,
  5051. value: literal
  5052. };
  5053. }
  5054. return {
  5055. type: "invalid",
  5056. pos: startPos
  5057. };
  5058. }
  5059. case types$1.string:
  5060. {
  5061. const literal = this.parseStringLiteral(this.state.value);
  5062. if (endOfInit()) {
  5063. return {
  5064. type: "string",
  5065. pos: literal.start,
  5066. value: literal
  5067. };
  5068. }
  5069. return {
  5070. type: "invalid",
  5071. pos: startPos
  5072. };
  5073. }
  5074. case types$1._true:
  5075. case types$1._false:
  5076. {
  5077. const literal = this.parseBooleanLiteral(this.match(types$1._true));
  5078. if (endOfInit()) {
  5079. return {
  5080. type: "boolean",
  5081. pos: literal.start,
  5082. value: literal
  5083. };
  5084. }
  5085. return {
  5086. type: "invalid",
  5087. pos: startPos
  5088. };
  5089. }
  5090. default:
  5091. return {
  5092. type: "invalid",
  5093. pos: startPos
  5094. };
  5095. }
  5096. }
  5097. flowEnumMemberRaw() {
  5098. const pos = this.state.start;
  5099. const id = this.parseIdentifier(true);
  5100. const init = this.eat(types$1.eq) ? this.flowEnumMemberInit() : {
  5101. type: "none",
  5102. pos
  5103. };
  5104. return {
  5105. id,
  5106. init
  5107. };
  5108. }
  5109. flowEnumCheckExplicitTypeMismatch(pos, context, expectedType) {
  5110. const {
  5111. explicitType
  5112. } = context;
  5113. if (explicitType === null) {
  5114. return;
  5115. }
  5116. if (explicitType !== expectedType) {
  5117. this.flowEnumErrorInvalidMemberInitializer(pos, context);
  5118. }
  5119. }
  5120. flowEnumMembers({
  5121. enumName,
  5122. explicitType
  5123. }) {
  5124. const seenNames = new Set();
  5125. const members = {
  5126. booleanMembers: [],
  5127. numberMembers: [],
  5128. stringMembers: [],
  5129. defaultedMembers: []
  5130. };
  5131. let hasUnknownMembers = false;
  5132. while (!this.match(types$1.braceR)) {
  5133. if (this.eat(types$1.ellipsis)) {
  5134. hasUnknownMembers = true;
  5135. break;
  5136. }
  5137. const memberNode = this.startNode();
  5138. const {
  5139. id,
  5140. init
  5141. } = this.flowEnumMemberRaw();
  5142. const memberName = id.name;
  5143. if (memberName === "") {
  5144. continue;
  5145. }
  5146. if (/^[a-z]/.test(memberName)) {
  5147. this.flowEnumErrorInvalidMemberName(id.start, {
  5148. enumName,
  5149. memberName
  5150. });
  5151. }
  5152. if (seenNames.has(memberName)) {
  5153. this.flowEnumErrorDuplicateMemberName(id.start, {
  5154. enumName,
  5155. memberName
  5156. });
  5157. }
  5158. seenNames.add(memberName);
  5159. const context = {
  5160. enumName,
  5161. explicitType,
  5162. memberName
  5163. };
  5164. memberNode.id = id;
  5165. switch (init.type) {
  5166. case "boolean":
  5167. {
  5168. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "boolean");
  5169. memberNode.init = init.value;
  5170. members.booleanMembers.push(this.finishNode(memberNode, "EnumBooleanMember"));
  5171. break;
  5172. }
  5173. case "number":
  5174. {
  5175. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "number");
  5176. memberNode.init = init.value;
  5177. members.numberMembers.push(this.finishNode(memberNode, "EnumNumberMember"));
  5178. break;
  5179. }
  5180. case "string":
  5181. {
  5182. this.flowEnumCheckExplicitTypeMismatch(init.pos, context, "string");
  5183. memberNode.init = init.value;
  5184. members.stringMembers.push(this.finishNode(memberNode, "EnumStringMember"));
  5185. break;
  5186. }
  5187. case "invalid":
  5188. {
  5189. throw this.flowEnumErrorInvalidMemberInitializer(init.pos, context);
  5190. }
  5191. case "none":
  5192. {
  5193. switch (explicitType) {
  5194. case "boolean":
  5195. this.flowEnumErrorBooleanMemberNotInitialized(init.pos, context);
  5196. break;
  5197. case "number":
  5198. this.flowEnumErrorNumberMemberNotInitialized(init.pos, context);
  5199. break;
  5200. default:
  5201. members.defaultedMembers.push(this.finishNode(memberNode, "EnumDefaultedMember"));
  5202. }
  5203. }
  5204. }
  5205. if (!this.match(types$1.braceR)) {
  5206. this.expect(types$1.comma);
  5207. }
  5208. }
  5209. return {
  5210. members,
  5211. hasUnknownMembers
  5212. };
  5213. }
  5214. flowEnumStringMembers(initializedMembers, defaultedMembers, {
  5215. enumName
  5216. }) {
  5217. if (initializedMembers.length === 0) {
  5218. return defaultedMembers;
  5219. } else if (defaultedMembers.length === 0) {
  5220. return initializedMembers;
  5221. } else if (defaultedMembers.length > initializedMembers.length) {
  5222. for (const member of initializedMembers) {
  5223. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  5224. enumName
  5225. });
  5226. }
  5227. return defaultedMembers;
  5228. } else {
  5229. for (const member of defaultedMembers) {
  5230. this.flowEnumErrorStringMemberInconsistentlyInitailized(member.start, {
  5231. enumName
  5232. });
  5233. }
  5234. return initializedMembers;
  5235. }
  5236. }
  5237. flowEnumParseExplicitType({
  5238. enumName
  5239. }) {
  5240. if (this.eatContextual("of")) {
  5241. if (!this.match(types$1.name)) {
  5242. throw this.flowEnumErrorInvalidExplicitType(this.state.start, {
  5243. enumName,
  5244. suppliedType: null
  5245. });
  5246. }
  5247. const {
  5248. value
  5249. } = this.state;
  5250. this.next();
  5251. if (value !== "boolean" && value !== "number" && value !== "string" && value !== "symbol") {
  5252. this.flowEnumErrorInvalidExplicitType(this.state.start, {
  5253. enumName,
  5254. suppliedType: value
  5255. });
  5256. }
  5257. return value;
  5258. }
  5259. return null;
  5260. }
  5261. flowEnumBody(node, {
  5262. enumName,
  5263. nameLoc
  5264. }) {
  5265. const explicitType = this.flowEnumParseExplicitType({
  5266. enumName
  5267. });
  5268. this.expect(types$1.braceL);
  5269. const {
  5270. members,
  5271. hasUnknownMembers
  5272. } = this.flowEnumMembers({
  5273. enumName,
  5274. explicitType
  5275. });
  5276. node.hasUnknownMembers = hasUnknownMembers;
  5277. switch (explicitType) {
  5278. case "boolean":
  5279. node.explicitType = true;
  5280. node.members = members.booleanMembers;
  5281. this.expect(types$1.braceR);
  5282. return this.finishNode(node, "EnumBooleanBody");
  5283. case "number":
  5284. node.explicitType = true;
  5285. node.members = members.numberMembers;
  5286. this.expect(types$1.braceR);
  5287. return this.finishNode(node, "EnumNumberBody");
  5288. case "string":
  5289. node.explicitType = true;
  5290. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  5291. enumName
  5292. });
  5293. this.expect(types$1.braceR);
  5294. return this.finishNode(node, "EnumStringBody");
  5295. case "symbol":
  5296. node.members = members.defaultedMembers;
  5297. this.expect(types$1.braceR);
  5298. return this.finishNode(node, "EnumSymbolBody");
  5299. default:
  5300. {
  5301. const empty = () => {
  5302. node.members = [];
  5303. this.expect(types$1.braceR);
  5304. return this.finishNode(node, "EnumStringBody");
  5305. };
  5306. node.explicitType = false;
  5307. const boolsLen = members.booleanMembers.length;
  5308. const numsLen = members.numberMembers.length;
  5309. const strsLen = members.stringMembers.length;
  5310. const defaultedLen = members.defaultedMembers.length;
  5311. if (!boolsLen && !numsLen && !strsLen && !defaultedLen) {
  5312. return empty();
  5313. } else if (!boolsLen && !numsLen) {
  5314. node.members = this.flowEnumStringMembers(members.stringMembers, members.defaultedMembers, {
  5315. enumName
  5316. });
  5317. this.expect(types$1.braceR);
  5318. return this.finishNode(node, "EnumStringBody");
  5319. } else if (!numsLen && !strsLen && boolsLen >= defaultedLen) {
  5320. for (const member of members.defaultedMembers) {
  5321. this.flowEnumErrorBooleanMemberNotInitialized(member.start, {
  5322. enumName,
  5323. memberName: member.id.name
  5324. });
  5325. }
  5326. node.members = members.booleanMembers;
  5327. this.expect(types$1.braceR);
  5328. return this.finishNode(node, "EnumBooleanBody");
  5329. } else if (!boolsLen && !strsLen && numsLen >= defaultedLen) {
  5330. for (const member of members.defaultedMembers) {
  5331. this.flowEnumErrorNumberMemberNotInitialized(member.start, {
  5332. enumName,
  5333. memberName: member.id.name
  5334. });
  5335. }
  5336. node.members = members.numberMembers;
  5337. this.expect(types$1.braceR);
  5338. return this.finishNode(node, "EnumNumberBody");
  5339. } else {
  5340. this.flowEnumErrorInconsistentMemberValues(nameLoc, {
  5341. enumName
  5342. });
  5343. return empty();
  5344. }
  5345. }
  5346. }
  5347. }
  5348. flowParseEnumDeclaration(node) {
  5349. const id = this.parseIdentifier();
  5350. node.id = id;
  5351. node.body = this.flowEnumBody(this.startNode(), {
  5352. enumName: id.name,
  5353. nameLoc: id.start
  5354. });
  5355. return this.finishNode(node, "EnumDeclaration");
  5356. }
  5357. isLookaheadToken_lt() {
  5358. const next = this.nextTokenStart();
  5359. if (this.input.charCodeAt(next) === 60) {
  5360. const afterNext = this.input.charCodeAt(next + 1);
  5361. return afterNext !== 60 && afterNext !== 61;
  5362. }
  5363. return false;
  5364. }
  5365. maybeUnwrapTypeCastExpression(node) {
  5366. return node.type === "TypeCastExpression" ? node.expression : node;
  5367. }
  5368. });
  5369. const entities = {
  5370. quot: "\u0022",
  5371. amp: "&",
  5372. apos: "\u0027",
  5373. lt: "<",
  5374. gt: ">",
  5375. nbsp: "\u00A0",
  5376. iexcl: "\u00A1",
  5377. cent: "\u00A2",
  5378. pound: "\u00A3",
  5379. curren: "\u00A4",
  5380. yen: "\u00A5",
  5381. brvbar: "\u00A6",
  5382. sect: "\u00A7",
  5383. uml: "\u00A8",
  5384. copy: "\u00A9",
  5385. ordf: "\u00AA",
  5386. laquo: "\u00AB",
  5387. not: "\u00AC",
  5388. shy: "\u00AD",
  5389. reg: "\u00AE",
  5390. macr: "\u00AF",
  5391. deg: "\u00B0",
  5392. plusmn: "\u00B1",
  5393. sup2: "\u00B2",
  5394. sup3: "\u00B3",
  5395. acute: "\u00B4",
  5396. micro: "\u00B5",
  5397. para: "\u00B6",
  5398. middot: "\u00B7",
  5399. cedil: "\u00B8",
  5400. sup1: "\u00B9",
  5401. ordm: "\u00BA",
  5402. raquo: "\u00BB",
  5403. frac14: "\u00BC",
  5404. frac12: "\u00BD",
  5405. frac34: "\u00BE",
  5406. iquest: "\u00BF",
  5407. Agrave: "\u00C0",
  5408. Aacute: "\u00C1",
  5409. Acirc: "\u00C2",
  5410. Atilde: "\u00C3",
  5411. Auml: "\u00C4",
  5412. Aring: "\u00C5",
  5413. AElig: "\u00C6",
  5414. Ccedil: "\u00C7",
  5415. Egrave: "\u00C8",
  5416. Eacute: "\u00C9",
  5417. Ecirc: "\u00CA",
  5418. Euml: "\u00CB",
  5419. Igrave: "\u00CC",
  5420. Iacute: "\u00CD",
  5421. Icirc: "\u00CE",
  5422. Iuml: "\u00CF",
  5423. ETH: "\u00D0",
  5424. Ntilde: "\u00D1",
  5425. Ograve: "\u00D2",
  5426. Oacute: "\u00D3",
  5427. Ocirc: "\u00D4",
  5428. Otilde: "\u00D5",
  5429. Ouml: "\u00D6",
  5430. times: "\u00D7",
  5431. Oslash: "\u00D8",
  5432. Ugrave: "\u00D9",
  5433. Uacute: "\u00DA",
  5434. Ucirc: "\u00DB",
  5435. Uuml: "\u00DC",
  5436. Yacute: "\u00DD",
  5437. THORN: "\u00DE",
  5438. szlig: "\u00DF",
  5439. agrave: "\u00E0",
  5440. aacute: "\u00E1",
  5441. acirc: "\u00E2",
  5442. atilde: "\u00E3",
  5443. auml: "\u00E4",
  5444. aring: "\u00E5",
  5445. aelig: "\u00E6",
  5446. ccedil: "\u00E7",
  5447. egrave: "\u00E8",
  5448. eacute: "\u00E9",
  5449. ecirc: "\u00EA",
  5450. euml: "\u00EB",
  5451. igrave: "\u00EC",
  5452. iacute: "\u00ED",
  5453. icirc: "\u00EE",
  5454. iuml: "\u00EF",
  5455. eth: "\u00F0",
  5456. ntilde: "\u00F1",
  5457. ograve: "\u00F2",
  5458. oacute: "\u00F3",
  5459. ocirc: "\u00F4",
  5460. otilde: "\u00F5",
  5461. ouml: "\u00F6",
  5462. divide: "\u00F7",
  5463. oslash: "\u00F8",
  5464. ugrave: "\u00F9",
  5465. uacute: "\u00FA",
  5466. ucirc: "\u00FB",
  5467. uuml: "\u00FC",
  5468. yacute: "\u00FD",
  5469. thorn: "\u00FE",
  5470. yuml: "\u00FF",
  5471. OElig: "\u0152",
  5472. oelig: "\u0153",
  5473. Scaron: "\u0160",
  5474. scaron: "\u0161",
  5475. Yuml: "\u0178",
  5476. fnof: "\u0192",
  5477. circ: "\u02C6",
  5478. tilde: "\u02DC",
  5479. Alpha: "\u0391",
  5480. Beta: "\u0392",
  5481. Gamma: "\u0393",
  5482. Delta: "\u0394",
  5483. Epsilon: "\u0395",
  5484. Zeta: "\u0396",
  5485. Eta: "\u0397",
  5486. Theta: "\u0398",
  5487. Iota: "\u0399",
  5488. Kappa: "\u039A",
  5489. Lambda: "\u039B",
  5490. Mu: "\u039C",
  5491. Nu: "\u039D",
  5492. Xi: "\u039E",
  5493. Omicron: "\u039F",
  5494. Pi: "\u03A0",
  5495. Rho: "\u03A1",
  5496. Sigma: "\u03A3",
  5497. Tau: "\u03A4",
  5498. Upsilon: "\u03A5",
  5499. Phi: "\u03A6",
  5500. Chi: "\u03A7",
  5501. Psi: "\u03A8",
  5502. Omega: "\u03A9",
  5503. alpha: "\u03B1",
  5504. beta: "\u03B2",
  5505. gamma: "\u03B3",
  5506. delta: "\u03B4",
  5507. epsilon: "\u03B5",
  5508. zeta: "\u03B6",
  5509. eta: "\u03B7",
  5510. theta: "\u03B8",
  5511. iota: "\u03B9",
  5512. kappa: "\u03BA",
  5513. lambda: "\u03BB",
  5514. mu: "\u03BC",
  5515. nu: "\u03BD",
  5516. xi: "\u03BE",
  5517. omicron: "\u03BF",
  5518. pi: "\u03C0",
  5519. rho: "\u03C1",
  5520. sigmaf: "\u03C2",
  5521. sigma: "\u03C3",
  5522. tau: "\u03C4",
  5523. upsilon: "\u03C5",
  5524. phi: "\u03C6",
  5525. chi: "\u03C7",
  5526. psi: "\u03C8",
  5527. omega: "\u03C9",
  5528. thetasym: "\u03D1",
  5529. upsih: "\u03D2",
  5530. piv: "\u03D6",
  5531. ensp: "\u2002",
  5532. emsp: "\u2003",
  5533. thinsp: "\u2009",
  5534. zwnj: "\u200C",
  5535. zwj: "\u200D",
  5536. lrm: "\u200E",
  5537. rlm: "\u200F",
  5538. ndash: "\u2013",
  5539. mdash: "\u2014",
  5540. lsquo: "\u2018",
  5541. rsquo: "\u2019",
  5542. sbquo: "\u201A",
  5543. ldquo: "\u201C",
  5544. rdquo: "\u201D",
  5545. bdquo: "\u201E",
  5546. dagger: "\u2020",
  5547. Dagger: "\u2021",
  5548. bull: "\u2022",
  5549. hellip: "\u2026",
  5550. permil: "\u2030",
  5551. prime: "\u2032",
  5552. Prime: "\u2033",
  5553. lsaquo: "\u2039",
  5554. rsaquo: "\u203A",
  5555. oline: "\u203E",
  5556. frasl: "\u2044",
  5557. euro: "\u20AC",
  5558. image: "\u2111",
  5559. weierp: "\u2118",
  5560. real: "\u211C",
  5561. trade: "\u2122",
  5562. alefsym: "\u2135",
  5563. larr: "\u2190",
  5564. uarr: "\u2191",
  5565. rarr: "\u2192",
  5566. darr: "\u2193",
  5567. harr: "\u2194",
  5568. crarr: "\u21B5",
  5569. lArr: "\u21D0",
  5570. uArr: "\u21D1",
  5571. rArr: "\u21D2",
  5572. dArr: "\u21D3",
  5573. hArr: "\u21D4",
  5574. forall: "\u2200",
  5575. part: "\u2202",
  5576. exist: "\u2203",
  5577. empty: "\u2205",
  5578. nabla: "\u2207",
  5579. isin: "\u2208",
  5580. notin: "\u2209",
  5581. ni: "\u220B",
  5582. prod: "\u220F",
  5583. sum: "\u2211",
  5584. minus: "\u2212",
  5585. lowast: "\u2217",
  5586. radic: "\u221A",
  5587. prop: "\u221D",
  5588. infin: "\u221E",
  5589. ang: "\u2220",
  5590. and: "\u2227",
  5591. or: "\u2228",
  5592. cap: "\u2229",
  5593. cup: "\u222A",
  5594. int: "\u222B",
  5595. there4: "\u2234",
  5596. sim: "\u223C",
  5597. cong: "\u2245",
  5598. asymp: "\u2248",
  5599. ne: "\u2260",
  5600. equiv: "\u2261",
  5601. le: "\u2264",
  5602. ge: "\u2265",
  5603. sub: "\u2282",
  5604. sup: "\u2283",
  5605. nsub: "\u2284",
  5606. sube: "\u2286",
  5607. supe: "\u2287",
  5608. oplus: "\u2295",
  5609. otimes: "\u2297",
  5610. perp: "\u22A5",
  5611. sdot: "\u22C5",
  5612. lceil: "\u2308",
  5613. rceil: "\u2309",
  5614. lfloor: "\u230A",
  5615. rfloor: "\u230B",
  5616. lang: "\u2329",
  5617. rang: "\u232A",
  5618. loz: "\u25CA",
  5619. spades: "\u2660",
  5620. clubs: "\u2663",
  5621. hearts: "\u2665",
  5622. diams: "\u2666"
  5623. };
  5624. const HEX_NUMBER = /^[\da-fA-F]+$/;
  5625. const DECIMAL_NUMBER = /^\d+$/;
  5626. const JsxErrors = makeErrorTemplates({
  5627. AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression.",
  5628. MissingClosingTagElement: "Expected corresponding JSX closing tag for <%0>.",
  5629. MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>.",
  5630. UnexpectedSequenceExpression: "Sequence expressions cannot be directly nested inside JSX. Did you mean to wrap it in parentheses (...)?",
  5631. UnsupportedJsxValue: "JSX value should be either an expression or a quoted JSX text.",
  5632. UnterminatedJsxContent: "Unterminated JSX contents.",
  5633. UnwrappedAdjacentJSXElements: "Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?"
  5634. }, ErrorCodes.SyntaxError, "jsx");
  5635. types.j_oTag = new TokContext("<tag");
  5636. types.j_cTag = new TokContext("</tag");
  5637. types.j_expr = new TokContext("<tag>...</tag>", true);
  5638. types$1.jsxName = new TokenType("jsxName");
  5639. types$1.jsxText = new TokenType("jsxText", {
  5640. beforeExpr: true
  5641. });
  5642. types$1.jsxTagStart = new TokenType("jsxTagStart", {
  5643. startsExpr: true
  5644. });
  5645. types$1.jsxTagEnd = new TokenType("jsxTagEnd");
  5646. types$1.jsxTagStart.updateContext = context => {
  5647. context.push(types.j_expr, types.j_oTag);
  5648. };
  5649. function isFragment(object) {
  5650. return object ? object.type === "JSXOpeningFragment" || object.type === "JSXClosingFragment" : false;
  5651. }
  5652. function getQualifiedJSXName(object) {
  5653. if (object.type === "JSXIdentifier") {
  5654. return object.name;
  5655. }
  5656. if (object.type === "JSXNamespacedName") {
  5657. return object.namespace.name + ":" + object.name.name;
  5658. }
  5659. if (object.type === "JSXMemberExpression") {
  5660. return getQualifiedJSXName(object.object) + "." + getQualifiedJSXName(object.property);
  5661. }
  5662. throw new Error("Node had unexpected type: " + object.type);
  5663. }
  5664. var jsx = (superClass => class extends superClass {
  5665. jsxReadToken() {
  5666. let out = "";
  5667. let chunkStart = this.state.pos;
  5668. for (;;) {
  5669. if (this.state.pos >= this.length) {
  5670. throw this.raise(this.state.start, JsxErrors.UnterminatedJsxContent);
  5671. }
  5672. const ch = this.input.charCodeAt(this.state.pos);
  5673. switch (ch) {
  5674. case 60:
  5675. case 123:
  5676. if (this.state.pos === this.state.start) {
  5677. if (ch === 60 && this.state.exprAllowed) {
  5678. ++this.state.pos;
  5679. return this.finishToken(types$1.jsxTagStart);
  5680. }
  5681. return super.getTokenFromCode(ch);
  5682. }
  5683. out += this.input.slice(chunkStart, this.state.pos);
  5684. return this.finishToken(types$1.jsxText, out);
  5685. case 38:
  5686. out += this.input.slice(chunkStart, this.state.pos);
  5687. out += this.jsxReadEntity();
  5688. chunkStart = this.state.pos;
  5689. break;
  5690. case 62:
  5691. case 125:
  5692. default:
  5693. if (isNewLine(ch)) {
  5694. out += this.input.slice(chunkStart, this.state.pos);
  5695. out += this.jsxReadNewLine(true);
  5696. chunkStart = this.state.pos;
  5697. } else {
  5698. ++this.state.pos;
  5699. }
  5700. }
  5701. }
  5702. }
  5703. jsxReadNewLine(normalizeCRLF) {
  5704. const ch = this.input.charCodeAt(this.state.pos);
  5705. let out;
  5706. ++this.state.pos;
  5707. if (ch === 13 && this.input.charCodeAt(this.state.pos) === 10) {
  5708. ++this.state.pos;
  5709. out = normalizeCRLF ? "\n" : "\r\n";
  5710. } else {
  5711. out = String.fromCharCode(ch);
  5712. }
  5713. ++this.state.curLine;
  5714. this.state.lineStart = this.state.pos;
  5715. return out;
  5716. }
  5717. jsxReadString(quote) {
  5718. let out = "";
  5719. let chunkStart = ++this.state.pos;
  5720. for (;;) {
  5721. if (this.state.pos >= this.length) {
  5722. throw this.raise(this.state.start, ErrorMessages.UnterminatedString);
  5723. }
  5724. const ch = this.input.charCodeAt(this.state.pos);
  5725. if (ch === quote) break;
  5726. if (ch === 38) {
  5727. out += this.input.slice(chunkStart, this.state.pos);
  5728. out += this.jsxReadEntity();
  5729. chunkStart = this.state.pos;
  5730. } else if (isNewLine(ch)) {
  5731. out += this.input.slice(chunkStart, this.state.pos);
  5732. out += this.jsxReadNewLine(false);
  5733. chunkStart = this.state.pos;
  5734. } else {
  5735. ++this.state.pos;
  5736. }
  5737. }
  5738. out += this.input.slice(chunkStart, this.state.pos++);
  5739. return this.finishToken(types$1.string, out);
  5740. }
  5741. jsxReadEntity() {
  5742. let str = "";
  5743. let count = 0;
  5744. let entity;
  5745. let ch = this.input[this.state.pos];
  5746. const startPos = ++this.state.pos;
  5747. while (this.state.pos < this.length && count++ < 10) {
  5748. ch = this.input[this.state.pos++];
  5749. if (ch === ";") {
  5750. if (str[0] === "#") {
  5751. if (str[1] === "x") {
  5752. str = str.substr(2);
  5753. if (HEX_NUMBER.test(str)) {
  5754. entity = String.fromCodePoint(parseInt(str, 16));
  5755. }
  5756. } else {
  5757. str = str.substr(1);
  5758. if (DECIMAL_NUMBER.test(str)) {
  5759. entity = String.fromCodePoint(parseInt(str, 10));
  5760. }
  5761. }
  5762. } else {
  5763. entity = entities[str];
  5764. }
  5765. break;
  5766. }
  5767. str += ch;
  5768. }
  5769. if (!entity) {
  5770. this.state.pos = startPos;
  5771. return "&";
  5772. }
  5773. return entity;
  5774. }
  5775. jsxReadWord() {
  5776. let ch;
  5777. const start = this.state.pos;
  5778. do {
  5779. ch = this.input.charCodeAt(++this.state.pos);
  5780. } while (isIdentifierChar(ch) || ch === 45);
  5781. return this.finishToken(types$1.jsxName, this.input.slice(start, this.state.pos));
  5782. }
  5783. jsxParseIdentifier() {
  5784. const node = this.startNode();
  5785. if (this.match(types$1.jsxName)) {
  5786. node.name = this.state.value;
  5787. } else if (this.state.type.keyword) {
  5788. node.name = this.state.type.keyword;
  5789. } else {
  5790. this.unexpected();
  5791. }
  5792. this.next();
  5793. return this.finishNode(node, "JSXIdentifier");
  5794. }
  5795. jsxParseNamespacedName() {
  5796. const startPos = this.state.start;
  5797. const startLoc = this.state.startLoc;
  5798. const name = this.jsxParseIdentifier();
  5799. if (!this.eat(types$1.colon)) return name;
  5800. const node = this.startNodeAt(startPos, startLoc);
  5801. node.namespace = name;
  5802. node.name = this.jsxParseIdentifier();
  5803. return this.finishNode(node, "JSXNamespacedName");
  5804. }
  5805. jsxParseElementName() {
  5806. const startPos = this.state.start;
  5807. const startLoc = this.state.startLoc;
  5808. let node = this.jsxParseNamespacedName();
  5809. if (node.type === "JSXNamespacedName") {
  5810. return node;
  5811. }
  5812. while (this.eat(types$1.dot)) {
  5813. const newNode = this.startNodeAt(startPos, startLoc);
  5814. newNode.object = node;
  5815. newNode.property = this.jsxParseIdentifier();
  5816. node = this.finishNode(newNode, "JSXMemberExpression");
  5817. }
  5818. return node;
  5819. }
  5820. jsxParseAttributeValue() {
  5821. let node;
  5822. switch (this.state.type) {
  5823. case types$1.braceL:
  5824. node = this.startNode();
  5825. this.next();
  5826. node = this.jsxParseExpressionContainer(node);
  5827. if (node.expression.type === "JSXEmptyExpression") {
  5828. this.raise(node.start, JsxErrors.AttributeIsEmpty);
  5829. }
  5830. return node;
  5831. case types$1.jsxTagStart:
  5832. case types$1.string:
  5833. return this.parseExprAtom();
  5834. default:
  5835. throw this.raise(this.state.start, JsxErrors.UnsupportedJsxValue);
  5836. }
  5837. }
  5838. jsxParseEmptyExpression() {
  5839. const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
  5840. return this.finishNodeAt(node, "JSXEmptyExpression", this.state.start, this.state.startLoc);
  5841. }
  5842. jsxParseSpreadChild(node) {
  5843. this.next();
  5844. node.expression = this.parseExpression();
  5845. this.expect(types$1.braceR);
  5846. return this.finishNode(node, "JSXSpreadChild");
  5847. }
  5848. jsxParseExpressionContainer(node) {
  5849. if (this.match(types$1.braceR)) {
  5850. node.expression = this.jsxParseEmptyExpression();
  5851. } else {
  5852. const expression = this.parseExpression();
  5853. node.expression = expression;
  5854. }
  5855. this.expect(types$1.braceR);
  5856. return this.finishNode(node, "JSXExpressionContainer");
  5857. }
  5858. jsxParseAttribute() {
  5859. const node = this.startNode();
  5860. if (this.eat(types$1.braceL)) {
  5861. this.expect(types$1.ellipsis);
  5862. node.argument = this.parseMaybeAssignAllowIn();
  5863. this.expect(types$1.braceR);
  5864. return this.finishNode(node, "JSXSpreadAttribute");
  5865. }
  5866. node.name = this.jsxParseNamespacedName();
  5867. node.value = this.eat(types$1.eq) ? this.jsxParseAttributeValue() : null;
  5868. return this.finishNode(node, "JSXAttribute");
  5869. }
  5870. jsxParseOpeningElementAt(startPos, startLoc) {
  5871. const node = this.startNodeAt(startPos, startLoc);
  5872. if (this.match(types$1.jsxTagEnd)) {
  5873. this.expect(types$1.jsxTagEnd);
  5874. return this.finishNode(node, "JSXOpeningFragment");
  5875. }
  5876. node.name = this.jsxParseElementName();
  5877. return this.jsxParseOpeningElementAfterName(node);
  5878. }
  5879. jsxParseOpeningElementAfterName(node) {
  5880. const attributes = [];
  5881. while (!this.match(types$1.slash) && !this.match(types$1.jsxTagEnd)) {
  5882. attributes.push(this.jsxParseAttribute());
  5883. }
  5884. node.attributes = attributes;
  5885. node.selfClosing = this.eat(types$1.slash);
  5886. this.expect(types$1.jsxTagEnd);
  5887. return this.finishNode(node, "JSXOpeningElement");
  5888. }
  5889. jsxParseClosingElementAt(startPos, startLoc) {
  5890. const node = this.startNodeAt(startPos, startLoc);
  5891. if (this.match(types$1.jsxTagEnd)) {
  5892. this.expect(types$1.jsxTagEnd);
  5893. return this.finishNode(node, "JSXClosingFragment");
  5894. }
  5895. node.name = this.jsxParseElementName();
  5896. this.expect(types$1.jsxTagEnd);
  5897. return this.finishNode(node, "JSXClosingElement");
  5898. }
  5899. jsxParseElementAt(startPos, startLoc) {
  5900. const node = this.startNodeAt(startPos, startLoc);
  5901. const children = [];
  5902. const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc);
  5903. let closingElement = null;
  5904. if (!openingElement.selfClosing) {
  5905. contents: for (;;) {
  5906. switch (this.state.type) {
  5907. case types$1.jsxTagStart:
  5908. startPos = this.state.start;
  5909. startLoc = this.state.startLoc;
  5910. this.next();
  5911. if (this.eat(types$1.slash)) {
  5912. closingElement = this.jsxParseClosingElementAt(startPos, startLoc);
  5913. break contents;
  5914. }
  5915. children.push(this.jsxParseElementAt(startPos, startLoc));
  5916. break;
  5917. case types$1.jsxText:
  5918. children.push(this.parseExprAtom());
  5919. break;
  5920. case types$1.braceL:
  5921. {
  5922. const node = this.startNode();
  5923. this.next();
  5924. if (this.match(types$1.ellipsis)) {
  5925. children.push(this.jsxParseSpreadChild(node));
  5926. } else {
  5927. children.push(this.jsxParseExpressionContainer(node));
  5928. }
  5929. break;
  5930. }
  5931. default:
  5932. throw this.unexpected();
  5933. }
  5934. }
  5935. if (isFragment(openingElement) && !isFragment(closingElement)) {
  5936. this.raise(closingElement.start, JsxErrors.MissingClosingTagFragment);
  5937. } else if (!isFragment(openingElement) && isFragment(closingElement)) {
  5938. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  5939. } else if (!isFragment(openingElement) && !isFragment(closingElement)) {
  5940. if (getQualifiedJSXName(closingElement.name) !== getQualifiedJSXName(openingElement.name)) {
  5941. this.raise(closingElement.start, JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name));
  5942. }
  5943. }
  5944. }
  5945. if (isFragment(openingElement)) {
  5946. node.openingFragment = openingElement;
  5947. node.closingFragment = closingElement;
  5948. } else {
  5949. node.openingElement = openingElement;
  5950. node.closingElement = closingElement;
  5951. }
  5952. node.children = children;
  5953. if (this.isRelational("<")) {
  5954. throw this.raise(this.state.start, JsxErrors.UnwrappedAdjacentJSXElements);
  5955. }
  5956. return isFragment(openingElement) ? this.finishNode(node, "JSXFragment") : this.finishNode(node, "JSXElement");
  5957. }
  5958. jsxParseElement() {
  5959. const startPos = this.state.start;
  5960. const startLoc = this.state.startLoc;
  5961. this.next();
  5962. return this.jsxParseElementAt(startPos, startLoc);
  5963. }
  5964. parseExprAtom(refExpressionErrors) {
  5965. if (this.match(types$1.jsxText)) {
  5966. return this.parseLiteral(this.state.value, "JSXText");
  5967. } else if (this.match(types$1.jsxTagStart)) {
  5968. return this.jsxParseElement();
  5969. } else if (this.isRelational("<") && this.input.charCodeAt(this.state.pos) !== 33) {
  5970. this.finishToken(types$1.jsxTagStart);
  5971. return this.jsxParseElement();
  5972. } else {
  5973. return super.parseExprAtom(refExpressionErrors);
  5974. }
  5975. }
  5976. createLookaheadState(state) {
  5977. const lookaheadState = super.createLookaheadState(state);
  5978. lookaheadState.inPropertyName = state.inPropertyName;
  5979. return lookaheadState;
  5980. }
  5981. getTokenFromCode(code) {
  5982. if (this.state.inPropertyName) return super.getTokenFromCode(code);
  5983. const context = this.curContext();
  5984. if (context === types.j_expr) {
  5985. return this.jsxReadToken();
  5986. }
  5987. if (context === types.j_oTag || context === types.j_cTag) {
  5988. if (isIdentifierStart(code)) {
  5989. return this.jsxReadWord();
  5990. }
  5991. if (code === 62) {
  5992. ++this.state.pos;
  5993. return this.finishToken(types$1.jsxTagEnd);
  5994. }
  5995. if ((code === 34 || code === 39) && context === types.j_oTag) {
  5996. return this.jsxReadString(code);
  5997. }
  5998. }
  5999. if (code === 60 && this.state.exprAllowed && this.input.charCodeAt(this.state.pos + 1) !== 33) {
  6000. ++this.state.pos;
  6001. return this.finishToken(types$1.jsxTagStart);
  6002. }
  6003. return super.getTokenFromCode(code);
  6004. }
  6005. updateContext(prevType) {
  6006. super.updateContext(prevType);
  6007. const {
  6008. context,
  6009. type
  6010. } = this.state;
  6011. if (type === types$1.slash && prevType === types$1.jsxTagStart) {
  6012. context.splice(-2, 2, types.j_cTag);
  6013. this.state.exprAllowed = false;
  6014. } else if (type === types$1.jsxTagEnd) {
  6015. const out = context.pop();
  6016. if (out === types.j_oTag && prevType === types$1.slash || out === types.j_cTag) {
  6017. context.pop();
  6018. this.state.exprAllowed = context[context.length - 1] === types.j_expr;
  6019. } else {
  6020. this.state.exprAllowed = true;
  6021. }
  6022. } else if (type.keyword && (prevType === types$1.dot || prevType === types$1.questionDot)) {
  6023. this.state.exprAllowed = false;
  6024. } else {
  6025. this.state.exprAllowed = type.beforeExpr;
  6026. }
  6027. }
  6028. });
  6029. class TypeScriptScope extends Scope {
  6030. constructor(...args) {
  6031. super(...args);
  6032. this.types = new Set();
  6033. this.enums = new Set();
  6034. this.constEnums = new Set();
  6035. this.classes = new Set();
  6036. this.exportOnlyBindings = new Set();
  6037. }
  6038. }
  6039. class TypeScriptScopeHandler extends ScopeHandler {
  6040. createScope(flags) {
  6041. return new TypeScriptScope(flags);
  6042. }
  6043. declareName(name, bindingType, pos) {
  6044. const scope = this.currentScope();
  6045. if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) {
  6046. this.maybeExportDefined(scope, name);
  6047. scope.exportOnlyBindings.add(name);
  6048. return;
  6049. }
  6050. super.declareName(...arguments);
  6051. if (bindingType & BIND_KIND_TYPE) {
  6052. if (!(bindingType & BIND_KIND_VALUE)) {
  6053. this.checkRedeclarationInScope(scope, name, bindingType, pos);
  6054. this.maybeExportDefined(scope, name);
  6055. }
  6056. scope.types.add(name);
  6057. }
  6058. if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.add(name);
  6059. if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.add(name);
  6060. if (bindingType & BIND_FLAGS_CLASS) scope.classes.add(name);
  6061. }
  6062. isRedeclaredInScope(scope, name, bindingType) {
  6063. if (scope.enums.has(name)) {
  6064. if (bindingType & BIND_FLAGS_TS_ENUM) {
  6065. const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM);
  6066. const wasConst = scope.constEnums.has(name);
  6067. return isConst !== wasConst;
  6068. }
  6069. return true;
  6070. }
  6071. if (bindingType & BIND_FLAGS_CLASS && scope.classes.has(name)) {
  6072. if (scope.lexical.has(name)) {
  6073. return !!(bindingType & BIND_KIND_VALUE);
  6074. } else {
  6075. return false;
  6076. }
  6077. }
  6078. if (bindingType & BIND_KIND_TYPE && scope.types.has(name)) {
  6079. return true;
  6080. }
  6081. return super.isRedeclaredInScope(...arguments);
  6082. }
  6083. checkLocalExport(id) {
  6084. const topLevelScope = this.scopeStack[0];
  6085. const {
  6086. name
  6087. } = id;
  6088. if (!topLevelScope.types.has(name) && !topLevelScope.exportOnlyBindings.has(name)) {
  6089. super.checkLocalExport(id);
  6090. }
  6091. }
  6092. }
  6093. function nonNull(x) {
  6094. if (x == null) {
  6095. throw new Error(`Unexpected ${x} value.`);
  6096. }
  6097. return x;
  6098. }
  6099. function assert(x) {
  6100. if (!x) {
  6101. throw new Error("Assert fail");
  6102. }
  6103. }
  6104. const TSErrors = makeErrorTemplates({
  6105. AbstractMethodHasImplementation: "Method '%0' cannot have an implementation because it is marked abstract.",
  6106. AbstractPropertyHasInitializer: "Property '%0' cannot have an initializer because it is marked abstract.",
  6107. AccesorCannotDeclareThisParameter: "'get' and 'set' accessors cannot declare 'this' parameters.",
  6108. AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
  6109. ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
  6110. ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier.",
  6111. ConstructorHasTypeParameters: "Type parameters cannot appear on a constructor declaration.",
  6112. DeclareAccessor: "'declare' is not allowed in %0ters.",
  6113. DeclareClassFieldHasInitializer: "Initializers are not allowed in ambient contexts.",
  6114. DeclareFunctionHasImplementation: "An implementation cannot be declared in ambient contexts.",
  6115. DuplicateAccessibilityModifier: "Accessibility modifier already seen.",
  6116. DuplicateModifier: "Duplicate modifier: '%0'.",
  6117. EmptyHeritageClauseType: "'%0' list cannot be empty.",
  6118. EmptyTypeArguments: "Type argument list cannot be empty.",
  6119. EmptyTypeParameters: "Type parameter list cannot be empty.",
  6120. ExpectedAmbientAfterExportDeclare: "'export declare' must be followed by an ambient declaration.",
  6121. ImportAliasHasImportType: "An import alias can not use 'import type'.",
  6122. IncompatibleModifiers: "'%0' modifier cannot be used with '%1' modifier.",
  6123. IndexSignatureHasAbstract: "Index signatures cannot have the 'abstract' modifier.",
  6124. IndexSignatureHasAccessibility: "Index signatures cannot have an accessibility modifier ('%0').",
  6125. IndexSignatureHasDeclare: "Index signatures cannot have the 'declare' modifier.",
  6126. IndexSignatureHasOverride: "'override' modifier cannot appear on an index signature.",
  6127. IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier.",
  6128. InvalidModifierOnTypeMember: "'%0' modifier cannot appear on a type member.",
  6129. InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
  6130. InvalidTupleMemberLabel: "Tuple members must be labeled with a simple identifier.",
  6131. MissingInterfaceName: "'interface' declarations must be followed by an identifier.",
  6132. MixedLabeledAndUnlabeledElements: "Tuple members must all have names or all not have names.",
  6133. NonAbstractClassHasAbstractMethod: "Abstract methods can only appear within an abstract class.",
  6134. NonClassMethodPropertyHasAbstractModifer: "'abstract' modifier can only appear on a class, method, or property declaration.",
  6135. OptionalTypeBeforeRequired: "A required element cannot follow an optional element.",
  6136. OverrideNotInSubClass: "This member cannot have an 'override' modifier because its containing class does not extend another class.",
  6137. PatternIsOptional: "A binding pattern parameter cannot be optional in an implementation signature.",
  6138. PrivateElementHasAbstract: "Private elements cannot have the 'abstract' modifier.",
  6139. PrivateElementHasAccessibility: "Private elements cannot have an accessibility modifier ('%0').",
  6140. ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.",
  6141. SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.",
  6142. SetAccesorCannotHaveRestParameter: "A 'set' accessor cannot have rest parameter.",
  6143. SetAccesorCannotHaveReturnType: "A 'set' accessor cannot have a return type annotation.",
  6144. StaticBlockCannotHaveModifier: "Static class blocks cannot have any modifier.",
  6145. TypeAnnotationAfterAssign: "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
  6146. TypeImportCannotSpecifyDefaultAndNamed: "A type-only import can specify a default import or named bindings, but not both.",
  6147. UnexpectedParameterModifier: "A parameter property is only allowed in a constructor implementation.",
  6148. UnexpectedReadonly: "'readonly' type modifier is only permitted on array and tuple literal types.",
  6149. UnexpectedTypeAnnotation: "Did not expect a type annotation here.",
  6150. UnexpectedTypeCastInParameter: "Unexpected type cast in parameter position.",
  6151. UnsupportedImportTypeArgument: "Argument in a type import must be a string literal.",
  6152. UnsupportedParameterPropertyKind: "A parameter property may not be declared using a binding pattern.",
  6153. UnsupportedSignatureParameterKind: "Name in a signature must be an Identifier, ObjectPattern or ArrayPattern, instead got %0."
  6154. }, ErrorCodes.SyntaxError, "typescript");
  6155. function keywordTypeFromName(value) {
  6156. switch (value) {
  6157. case "any":
  6158. return "TSAnyKeyword";
  6159. case "boolean":
  6160. return "TSBooleanKeyword";
  6161. case "bigint":
  6162. return "TSBigIntKeyword";
  6163. case "never":
  6164. return "TSNeverKeyword";
  6165. case "number":
  6166. return "TSNumberKeyword";
  6167. case "object":
  6168. return "TSObjectKeyword";
  6169. case "string":
  6170. return "TSStringKeyword";
  6171. case "symbol":
  6172. return "TSSymbolKeyword";
  6173. case "undefined":
  6174. return "TSUndefinedKeyword";
  6175. case "unknown":
  6176. return "TSUnknownKeyword";
  6177. default:
  6178. return undefined;
  6179. }
  6180. }
  6181. function tsIsAccessModifier(modifier) {
  6182. return modifier === "private" || modifier === "public" || modifier === "protected";
  6183. }
  6184. var typescript = (superClass => class extends superClass {
  6185. getScopeHandler() {
  6186. return TypeScriptScopeHandler;
  6187. }
  6188. tsIsIdentifier() {
  6189. return this.match(types$1.name);
  6190. }
  6191. tsTokenCanFollowModifier() {
  6192. return (this.match(types$1.bracketL) || this.match(types$1.braceL) || this.match(types$1.star) || this.match(types$1.ellipsis) || this.match(types$1.privateName) || this.isLiteralPropertyName()) && !this.hasPrecedingLineBreak();
  6193. }
  6194. tsNextTokenCanFollowModifier() {
  6195. this.next();
  6196. return this.tsTokenCanFollowModifier();
  6197. }
  6198. tsParseModifier(allowedModifiers, stopOnStartOfClassStaticBlock) {
  6199. if (!this.match(types$1.name)) {
  6200. return undefined;
  6201. }
  6202. const modifier = this.state.value;
  6203. if (allowedModifiers.indexOf(modifier) !== -1) {
  6204. if (stopOnStartOfClassStaticBlock && this.tsIsStartOfStaticBlocks()) {
  6205. return undefined;
  6206. }
  6207. if (this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) {
  6208. return modifier;
  6209. }
  6210. }
  6211. return undefined;
  6212. }
  6213. tsParseModifiers(modified, allowedModifiers, disallowedModifiers, errorTemplate, stopOnStartOfClassStaticBlock) {
  6214. const enforceOrder = (pos, modifier, before, after) => {
  6215. if (modifier === before && modified[after]) {
  6216. this.raise(pos, TSErrors.InvalidModifiersOrder, before, after);
  6217. }
  6218. };
  6219. const incompatible = (pos, modifier, mod1, mod2) => {
  6220. if (modified[mod1] && modifier === mod2 || modified[mod2] && modifier === mod1) {
  6221. this.raise(pos, TSErrors.IncompatibleModifiers, mod1, mod2);
  6222. }
  6223. };
  6224. for (;;) {
  6225. const startPos = this.state.start;
  6226. const modifier = this.tsParseModifier(allowedModifiers.concat(disallowedModifiers != null ? disallowedModifiers : []), stopOnStartOfClassStaticBlock);
  6227. if (!modifier) break;
  6228. if (tsIsAccessModifier(modifier)) {
  6229. if (modified.accessibility) {
  6230. this.raise(startPos, TSErrors.DuplicateAccessibilityModifier);
  6231. } else {
  6232. enforceOrder(startPos, modifier, modifier, "override");
  6233. enforceOrder(startPos, modifier, modifier, "static");
  6234. enforceOrder(startPos, modifier, modifier, "readonly");
  6235. modified.accessibility = modifier;
  6236. }
  6237. } else {
  6238. if (Object.hasOwnProperty.call(modified, modifier)) {
  6239. this.raise(startPos, TSErrors.DuplicateModifier, modifier);
  6240. } else {
  6241. enforceOrder(startPos, modifier, "static", "readonly");
  6242. enforceOrder(startPos, modifier, "static", "override");
  6243. enforceOrder(startPos, modifier, "override", "readonly");
  6244. enforceOrder(startPos, modifier, "abstract", "override");
  6245. incompatible(startPos, modifier, "declare", "override");
  6246. incompatible(startPos, modifier, "static", "abstract");
  6247. }
  6248. modified[modifier] = true;
  6249. }
  6250. if (disallowedModifiers != null && disallowedModifiers.includes(modifier)) {
  6251. this.raise(startPos, errorTemplate, modifier);
  6252. }
  6253. }
  6254. }
  6255. tsIsListTerminator(kind) {
  6256. switch (kind) {
  6257. case "EnumMembers":
  6258. case "TypeMembers":
  6259. return this.match(types$1.braceR);
  6260. case "HeritageClauseElement":
  6261. return this.match(types$1.braceL);
  6262. case "TupleElementTypes":
  6263. return this.match(types$1.bracketR);
  6264. case "TypeParametersOrArguments":
  6265. return this.isRelational(">");
  6266. }
  6267. throw new Error("Unreachable");
  6268. }
  6269. tsParseList(kind, parseElement) {
  6270. const result = [];
  6271. while (!this.tsIsListTerminator(kind)) {
  6272. result.push(parseElement());
  6273. }
  6274. return result;
  6275. }
  6276. tsParseDelimitedList(kind, parseElement) {
  6277. return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, true));
  6278. }
  6279. tsParseDelimitedListWorker(kind, parseElement, expectSuccess) {
  6280. const result = [];
  6281. for (;;) {
  6282. if (this.tsIsListTerminator(kind)) {
  6283. break;
  6284. }
  6285. const element = parseElement();
  6286. if (element == null) {
  6287. return undefined;
  6288. }
  6289. result.push(element);
  6290. if (this.eat(types$1.comma)) {
  6291. continue;
  6292. }
  6293. if (this.tsIsListTerminator(kind)) {
  6294. break;
  6295. }
  6296. if (expectSuccess) {
  6297. this.expect(types$1.comma);
  6298. }
  6299. return undefined;
  6300. }
  6301. return result;
  6302. }
  6303. tsParseBracketedList(kind, parseElement, bracket, skipFirstToken) {
  6304. if (!skipFirstToken) {
  6305. if (bracket) {
  6306. this.expect(types$1.bracketL);
  6307. } else {
  6308. this.expectRelational("<");
  6309. }
  6310. }
  6311. const result = this.tsParseDelimitedList(kind, parseElement);
  6312. if (bracket) {
  6313. this.expect(types$1.bracketR);
  6314. } else {
  6315. this.expectRelational(">");
  6316. }
  6317. return result;
  6318. }
  6319. tsParseImportType() {
  6320. const node = this.startNode();
  6321. this.expect(types$1._import);
  6322. this.expect(types$1.parenL);
  6323. if (!this.match(types$1.string)) {
  6324. this.raise(this.state.start, TSErrors.UnsupportedImportTypeArgument);
  6325. }
  6326. node.argument = this.parseExprAtom();
  6327. this.expect(types$1.parenR);
  6328. if (this.eat(types$1.dot)) {
  6329. node.qualifier = this.tsParseEntityName(true);
  6330. }
  6331. if (this.isRelational("<")) {
  6332. node.typeParameters = this.tsParseTypeArguments();
  6333. }
  6334. return this.finishNode(node, "TSImportType");
  6335. }
  6336. tsParseEntityName(allowReservedWords) {
  6337. let entity = this.parseIdentifier();
  6338. while (this.eat(types$1.dot)) {
  6339. const node = this.startNodeAtNode(entity);
  6340. node.left = entity;
  6341. node.right = this.parseIdentifier(allowReservedWords);
  6342. entity = this.finishNode(node, "TSQualifiedName");
  6343. }
  6344. return entity;
  6345. }
  6346. tsParseTypeReference() {
  6347. const node = this.startNode();
  6348. node.typeName = this.tsParseEntityName(false);
  6349. if (!this.hasPrecedingLineBreak() && this.isRelational("<")) {
  6350. node.typeParameters = this.tsParseTypeArguments();
  6351. }
  6352. return this.finishNode(node, "TSTypeReference");
  6353. }
  6354. tsParseThisTypePredicate(lhs) {
  6355. this.next();
  6356. const node = this.startNodeAtNode(lhs);
  6357. node.parameterName = lhs;
  6358. node.typeAnnotation = this.tsParseTypeAnnotation(false);
  6359. node.asserts = false;
  6360. return this.finishNode(node, "TSTypePredicate");
  6361. }
  6362. tsParseThisTypeNode() {
  6363. const node = this.startNode();
  6364. this.next();
  6365. return this.finishNode(node, "TSThisType");
  6366. }
  6367. tsParseTypeQuery() {
  6368. const node = this.startNode();
  6369. this.expect(types$1._typeof);
  6370. if (this.match(types$1._import)) {
  6371. node.exprName = this.tsParseImportType();
  6372. } else {
  6373. node.exprName = this.tsParseEntityName(true);
  6374. }
  6375. return this.finishNode(node, "TSTypeQuery");
  6376. }
  6377. tsParseTypeParameter() {
  6378. const node = this.startNode();
  6379. node.name = this.tsParseTypeParameterName();
  6380. node.constraint = this.tsEatThenParseType(types$1._extends);
  6381. node.default = this.tsEatThenParseType(types$1.eq);
  6382. return this.finishNode(node, "TSTypeParameter");
  6383. }
  6384. tsTryParseTypeParameters() {
  6385. if (this.isRelational("<")) {
  6386. return this.tsParseTypeParameters();
  6387. }
  6388. }
  6389. tsParseTypeParameters() {
  6390. const node = this.startNode();
  6391. if (this.isRelational("<") || this.match(types$1.jsxTagStart)) {
  6392. this.next();
  6393. } else {
  6394. this.unexpected();
  6395. }
  6396. node.params = this.tsParseBracketedList("TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), false, true);
  6397. if (node.params.length === 0) {
  6398. this.raise(node.start, TSErrors.EmptyTypeParameters);
  6399. }
  6400. return this.finishNode(node, "TSTypeParameterDeclaration");
  6401. }
  6402. tsTryNextParseConstantContext() {
  6403. if (this.lookahead().type === types$1._const) {
  6404. this.next();
  6405. return this.tsParseTypeReference();
  6406. }
  6407. return null;
  6408. }
  6409. tsFillSignature(returnToken, signature) {
  6410. const returnTokenRequired = returnToken === types$1.arrow;
  6411. signature.typeParameters = this.tsTryParseTypeParameters();
  6412. this.expect(types$1.parenL);
  6413. signature.parameters = this.tsParseBindingListForSignature();
  6414. if (returnTokenRequired) {
  6415. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  6416. } else if (this.match(returnToken)) {
  6417. signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken);
  6418. }
  6419. }
  6420. tsParseBindingListForSignature() {
  6421. return this.parseBindingList(types$1.parenR, 41).map(pattern => {
  6422. if (pattern.type !== "Identifier" && pattern.type !== "RestElement" && pattern.type !== "ObjectPattern" && pattern.type !== "ArrayPattern") {
  6423. this.raise(pattern.start, TSErrors.UnsupportedSignatureParameterKind, pattern.type);
  6424. }
  6425. return pattern;
  6426. });
  6427. }
  6428. tsParseTypeMemberSemicolon() {
  6429. if (!this.eat(types$1.comma) && !this.isLineTerminator()) {
  6430. this.expect(types$1.semi);
  6431. }
  6432. }
  6433. tsParseSignatureMember(kind, node) {
  6434. this.tsFillSignature(types$1.colon, node);
  6435. this.tsParseTypeMemberSemicolon();
  6436. return this.finishNode(node, kind);
  6437. }
  6438. tsIsUnambiguouslyIndexSignature() {
  6439. this.next();
  6440. return this.eat(types$1.name) && this.match(types$1.colon);
  6441. }
  6442. tsTryParseIndexSignature(node) {
  6443. if (!(this.match(types$1.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) {
  6444. return undefined;
  6445. }
  6446. this.expect(types$1.bracketL);
  6447. const id = this.parseIdentifier();
  6448. id.typeAnnotation = this.tsParseTypeAnnotation();
  6449. this.resetEndLocation(id);
  6450. this.expect(types$1.bracketR);
  6451. node.parameters = [id];
  6452. const type = this.tsTryParseTypeAnnotation();
  6453. if (type) node.typeAnnotation = type;
  6454. this.tsParseTypeMemberSemicolon();
  6455. return this.finishNode(node, "TSIndexSignature");
  6456. }
  6457. tsParsePropertyOrMethodSignature(node, readonly) {
  6458. if (this.eat(types$1.question)) node.optional = true;
  6459. const nodeAny = node;
  6460. if (this.match(types$1.parenL) || this.isRelational("<")) {
  6461. if (readonly) {
  6462. this.raise(node.start, TSErrors.ReadonlyForMethodSignature);
  6463. }
  6464. const method = nodeAny;
  6465. if (method.kind && this.isRelational("<")) {
  6466. this.raise(this.state.pos, TSErrors.AccesorCannotHaveTypeParameters);
  6467. }
  6468. this.tsFillSignature(types$1.colon, method);
  6469. this.tsParseTypeMemberSemicolon();
  6470. if (method.kind === "get") {
  6471. if (method.parameters.length > 0) {
  6472. this.raise(this.state.pos, ErrorMessages.BadGetterArity);
  6473. if (this.isThisParam(method.parameters[0])) {
  6474. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  6475. }
  6476. }
  6477. } else if (method.kind === "set") {
  6478. if (method.parameters.length !== 1) {
  6479. this.raise(this.state.pos, ErrorMessages.BadSetterArity);
  6480. } else {
  6481. const firstParameter = method.parameters[0];
  6482. if (this.isThisParam(firstParameter)) {
  6483. this.raise(this.state.pos, TSErrors.AccesorCannotDeclareThisParameter);
  6484. }
  6485. if (firstParameter.type === "Identifier" && firstParameter.optional) {
  6486. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveOptionalParameter);
  6487. }
  6488. if (firstParameter.type === "RestElement") {
  6489. this.raise(this.state.pos, TSErrors.SetAccesorCannotHaveRestParameter);
  6490. }
  6491. }
  6492. if (method.typeAnnotation) {
  6493. this.raise(method.typeAnnotation.start, TSErrors.SetAccesorCannotHaveReturnType);
  6494. }
  6495. } else {
  6496. method.kind = "method";
  6497. }
  6498. return this.finishNode(method, "TSMethodSignature");
  6499. } else {
  6500. const property = nodeAny;
  6501. if (readonly) property.readonly = true;
  6502. const type = this.tsTryParseTypeAnnotation();
  6503. if (type) property.typeAnnotation = type;
  6504. this.tsParseTypeMemberSemicolon();
  6505. return this.finishNode(property, "TSPropertySignature");
  6506. }
  6507. }
  6508. tsParseTypeMember() {
  6509. const node = this.startNode();
  6510. if (this.match(types$1.parenL) || this.isRelational("<")) {
  6511. return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
  6512. }
  6513. if (this.match(types$1._new)) {
  6514. const id = this.startNode();
  6515. this.next();
  6516. if (this.match(types$1.parenL) || this.isRelational("<")) {
  6517. return this.tsParseSignatureMember("TSConstructSignatureDeclaration", node);
  6518. } else {
  6519. node.key = this.createIdentifier(id, "new");
  6520. return this.tsParsePropertyOrMethodSignature(node, false);
  6521. }
  6522. }
  6523. this.tsParseModifiers(node, ["readonly"], ["declare", "abstract", "private", "protected", "public", "static", "override"], TSErrors.InvalidModifierOnTypeMember);
  6524. const idx = this.tsTryParseIndexSignature(node);
  6525. if (idx) {
  6526. return idx;
  6527. }
  6528. this.parsePropertyName(node, false);
  6529. if (!node.computed && node.key.type === "Identifier" && (node.key.name === "get" || node.key.name === "set") && this.tsTokenCanFollowModifier()) {
  6530. node.kind = node.key.name;
  6531. this.parsePropertyName(node, false);
  6532. }
  6533. return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
  6534. }
  6535. tsParseTypeLiteral() {
  6536. const node = this.startNode();
  6537. node.members = this.tsParseObjectTypeMembers();
  6538. return this.finishNode(node, "TSTypeLiteral");
  6539. }
  6540. tsParseObjectTypeMembers() {
  6541. this.expect(types$1.braceL);
  6542. const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this));
  6543. this.expect(types$1.braceR);
  6544. return members;
  6545. }
  6546. tsIsStartOfMappedType() {
  6547. this.next();
  6548. if (this.eat(types$1.plusMin)) {
  6549. return this.isContextual("readonly");
  6550. }
  6551. if (this.isContextual("readonly")) {
  6552. this.next();
  6553. }
  6554. if (!this.match(types$1.bracketL)) {
  6555. return false;
  6556. }
  6557. this.next();
  6558. if (!this.tsIsIdentifier()) {
  6559. return false;
  6560. }
  6561. this.next();
  6562. return this.match(types$1._in);
  6563. }
  6564. tsParseMappedTypeParameter() {
  6565. const node = this.startNode();
  6566. node.name = this.tsParseTypeParameterName();
  6567. node.constraint = this.tsExpectThenParseType(types$1._in);
  6568. return this.finishNode(node, "TSTypeParameter");
  6569. }
  6570. tsParseMappedType() {
  6571. const node = this.startNode();
  6572. this.expect(types$1.braceL);
  6573. if (this.match(types$1.plusMin)) {
  6574. node.readonly = this.state.value;
  6575. this.next();
  6576. this.expectContextual("readonly");
  6577. } else if (this.eatContextual("readonly")) {
  6578. node.readonly = true;
  6579. }
  6580. this.expect(types$1.bracketL);
  6581. node.typeParameter = this.tsParseMappedTypeParameter();
  6582. node.nameType = this.eatContextual("as") ? this.tsParseType() : null;
  6583. this.expect(types$1.bracketR);
  6584. if (this.match(types$1.plusMin)) {
  6585. node.optional = this.state.value;
  6586. this.next();
  6587. this.expect(types$1.question);
  6588. } else if (this.eat(types$1.question)) {
  6589. node.optional = true;
  6590. }
  6591. node.typeAnnotation = this.tsTryParseType();
  6592. this.semicolon();
  6593. this.expect(types$1.braceR);
  6594. return this.finishNode(node, "TSMappedType");
  6595. }
  6596. tsParseTupleType() {
  6597. const node = this.startNode();
  6598. node.elementTypes = this.tsParseBracketedList("TupleElementTypes", this.tsParseTupleElementType.bind(this), true, false);
  6599. let seenOptionalElement = false;
  6600. let labeledElements = null;
  6601. node.elementTypes.forEach(elementNode => {
  6602. var _labeledElements;
  6603. let {
  6604. type
  6605. } = elementNode;
  6606. if (seenOptionalElement && type !== "TSRestType" && type !== "TSOptionalType" && !(type === "TSNamedTupleMember" && elementNode.optional)) {
  6607. this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired);
  6608. }
  6609. seenOptionalElement = seenOptionalElement || type === "TSNamedTupleMember" && elementNode.optional || type === "TSOptionalType";
  6610. if (type === "TSRestType") {
  6611. elementNode = elementNode.typeAnnotation;
  6612. type = elementNode.type;
  6613. }
  6614. const isLabeled = type === "TSNamedTupleMember";
  6615. labeledElements = (_labeledElements = labeledElements) != null ? _labeledElements : isLabeled;
  6616. if (labeledElements !== isLabeled) {
  6617. this.raise(elementNode.start, TSErrors.MixedLabeledAndUnlabeledElements);
  6618. }
  6619. });
  6620. return this.finishNode(node, "TSTupleType");
  6621. }
  6622. tsParseTupleElementType() {
  6623. const {
  6624. start: startPos,
  6625. startLoc
  6626. } = this.state;
  6627. const rest = this.eat(types$1.ellipsis);
  6628. let type = this.tsParseType();
  6629. const optional = this.eat(types$1.question);
  6630. const labeled = this.eat(types$1.colon);
  6631. if (labeled) {
  6632. const labeledNode = this.startNodeAtNode(type);
  6633. labeledNode.optional = optional;
  6634. if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") {
  6635. labeledNode.label = type.typeName;
  6636. } else {
  6637. this.raise(type.start, TSErrors.InvalidTupleMemberLabel);
  6638. labeledNode.label = type;
  6639. }
  6640. labeledNode.elementType = this.tsParseType();
  6641. type = this.finishNode(labeledNode, "TSNamedTupleMember");
  6642. } else if (optional) {
  6643. const optionalTypeNode = this.startNodeAtNode(type);
  6644. optionalTypeNode.typeAnnotation = type;
  6645. type = this.finishNode(optionalTypeNode, "TSOptionalType");
  6646. }
  6647. if (rest) {
  6648. const restNode = this.startNodeAt(startPos, startLoc);
  6649. restNode.typeAnnotation = type;
  6650. type = this.finishNode(restNode, "TSRestType");
  6651. }
  6652. return type;
  6653. }
  6654. tsParseParenthesizedType() {
  6655. const node = this.startNode();
  6656. this.expect(types$1.parenL);
  6657. node.typeAnnotation = this.tsParseType();
  6658. this.expect(types$1.parenR);
  6659. return this.finishNode(node, "TSParenthesizedType");
  6660. }
  6661. tsParseFunctionOrConstructorType(type, abstract) {
  6662. const node = this.startNode();
  6663. if (type === "TSConstructorType") {
  6664. node.abstract = !!abstract;
  6665. if (abstract) this.next();
  6666. this.next();
  6667. }
  6668. this.tsFillSignature(types$1.arrow, node);
  6669. return this.finishNode(node, type);
  6670. }
  6671. tsParseLiteralTypeNode() {
  6672. const node = this.startNode();
  6673. node.literal = (() => {
  6674. switch (this.state.type) {
  6675. case types$1.num:
  6676. case types$1.bigint:
  6677. case types$1.string:
  6678. case types$1._true:
  6679. case types$1._false:
  6680. return this.parseExprAtom();
  6681. default:
  6682. throw this.unexpected();
  6683. }
  6684. })();
  6685. return this.finishNode(node, "TSLiteralType");
  6686. }
  6687. tsParseTemplateLiteralType() {
  6688. const node = this.startNode();
  6689. node.literal = this.parseTemplate(false);
  6690. return this.finishNode(node, "TSLiteralType");
  6691. }
  6692. parseTemplateSubstitution() {
  6693. if (this.state.inType) return this.tsParseType();
  6694. return super.parseTemplateSubstitution();
  6695. }
  6696. tsParseThisTypeOrThisTypePredicate() {
  6697. const thisKeyword = this.tsParseThisTypeNode();
  6698. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  6699. return this.tsParseThisTypePredicate(thisKeyword);
  6700. } else {
  6701. return thisKeyword;
  6702. }
  6703. }
  6704. tsParseNonArrayType() {
  6705. switch (this.state.type) {
  6706. case types$1.name:
  6707. case types$1._void:
  6708. case types$1._null:
  6709. {
  6710. const type = this.match(types$1._void) ? "TSVoidKeyword" : this.match(types$1._null) ? "TSNullKeyword" : keywordTypeFromName(this.state.value);
  6711. if (type !== undefined && this.lookaheadCharCode() !== 46) {
  6712. const node = this.startNode();
  6713. this.next();
  6714. return this.finishNode(node, type);
  6715. }
  6716. return this.tsParseTypeReference();
  6717. }
  6718. case types$1.string:
  6719. case types$1.num:
  6720. case types$1.bigint:
  6721. case types$1._true:
  6722. case types$1._false:
  6723. return this.tsParseLiteralTypeNode();
  6724. case types$1.plusMin:
  6725. if (this.state.value === "-") {
  6726. const node = this.startNode();
  6727. const nextToken = this.lookahead();
  6728. if (nextToken.type !== types$1.num && nextToken.type !== types$1.bigint) {
  6729. throw this.unexpected();
  6730. }
  6731. node.literal = this.parseMaybeUnary();
  6732. return this.finishNode(node, "TSLiteralType");
  6733. }
  6734. break;
  6735. case types$1._this:
  6736. return this.tsParseThisTypeOrThisTypePredicate();
  6737. case types$1._typeof:
  6738. return this.tsParseTypeQuery();
  6739. case types$1._import:
  6740. return this.tsParseImportType();
  6741. case types$1.braceL:
  6742. return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) ? this.tsParseMappedType() : this.tsParseTypeLiteral();
  6743. case types$1.bracketL:
  6744. return this.tsParseTupleType();
  6745. case types$1.parenL:
  6746. return this.tsParseParenthesizedType();
  6747. case types$1.backQuote:
  6748. return this.tsParseTemplateLiteralType();
  6749. }
  6750. throw this.unexpected();
  6751. }
  6752. tsParseArrayTypeOrHigher() {
  6753. let type = this.tsParseNonArrayType();
  6754. while (!this.hasPrecedingLineBreak() && this.eat(types$1.bracketL)) {
  6755. if (this.match(types$1.bracketR)) {
  6756. const node = this.startNodeAtNode(type);
  6757. node.elementType = type;
  6758. this.expect(types$1.bracketR);
  6759. type = this.finishNode(node, "TSArrayType");
  6760. } else {
  6761. const node = this.startNodeAtNode(type);
  6762. node.objectType = type;
  6763. node.indexType = this.tsParseType();
  6764. this.expect(types$1.bracketR);
  6765. type = this.finishNode(node, "TSIndexedAccessType");
  6766. }
  6767. }
  6768. return type;
  6769. }
  6770. tsParseTypeOperator(operator) {
  6771. const node = this.startNode();
  6772. this.expectContextual(operator);
  6773. node.operator = operator;
  6774. node.typeAnnotation = this.tsParseTypeOperatorOrHigher();
  6775. if (operator === "readonly") {
  6776. this.tsCheckTypeAnnotationForReadOnly(node);
  6777. }
  6778. return this.finishNode(node, "TSTypeOperator");
  6779. }
  6780. tsCheckTypeAnnotationForReadOnly(node) {
  6781. switch (node.typeAnnotation.type) {
  6782. case "TSTupleType":
  6783. case "TSArrayType":
  6784. return;
  6785. default:
  6786. this.raise(node.start, TSErrors.UnexpectedReadonly);
  6787. }
  6788. }
  6789. tsParseInferType() {
  6790. const node = this.startNode();
  6791. this.expectContextual("infer");
  6792. const typeParameter = this.startNode();
  6793. typeParameter.name = this.tsParseTypeParameterName();
  6794. node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
  6795. return this.finishNode(node, "TSInferType");
  6796. }
  6797. tsParseTypeOperatorOrHigher() {
  6798. const operator = ["keyof", "unique", "readonly"].find(kw => this.isContextual(kw));
  6799. return operator ? this.tsParseTypeOperator(operator) : this.isContextual("infer") ? this.tsParseInferType() : this.tsParseArrayTypeOrHigher();
  6800. }
  6801. tsParseUnionOrIntersectionType(kind, parseConstituentType, operator) {
  6802. const node = this.startNode();
  6803. const hasLeadingOperator = this.eat(operator);
  6804. const types = [];
  6805. do {
  6806. types.push(parseConstituentType());
  6807. } while (this.eat(operator));
  6808. if (types.length === 1 && !hasLeadingOperator) {
  6809. return types[0];
  6810. }
  6811. node.types = types;
  6812. return this.finishNode(node, kind);
  6813. }
  6814. tsParseIntersectionTypeOrHigher() {
  6815. return this.tsParseUnionOrIntersectionType("TSIntersectionType", this.tsParseTypeOperatorOrHigher.bind(this), types$1.bitwiseAND);
  6816. }
  6817. tsParseUnionTypeOrHigher() {
  6818. return this.tsParseUnionOrIntersectionType("TSUnionType", this.tsParseIntersectionTypeOrHigher.bind(this), types$1.bitwiseOR);
  6819. }
  6820. tsIsStartOfFunctionType() {
  6821. if (this.isRelational("<")) {
  6822. return true;
  6823. }
  6824. return this.match(types$1.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this));
  6825. }
  6826. tsSkipParameterStart() {
  6827. if (this.match(types$1.name) || this.match(types$1._this)) {
  6828. this.next();
  6829. return true;
  6830. }
  6831. if (this.match(types$1.braceL)) {
  6832. let braceStackCounter = 1;
  6833. this.next();
  6834. while (braceStackCounter > 0) {
  6835. if (this.match(types$1.braceL)) {
  6836. ++braceStackCounter;
  6837. } else if (this.match(types$1.braceR)) {
  6838. --braceStackCounter;
  6839. }
  6840. this.next();
  6841. }
  6842. return true;
  6843. }
  6844. if (this.match(types$1.bracketL)) {
  6845. let braceStackCounter = 1;
  6846. this.next();
  6847. while (braceStackCounter > 0) {
  6848. if (this.match(types$1.bracketL)) {
  6849. ++braceStackCounter;
  6850. } else if (this.match(types$1.bracketR)) {
  6851. --braceStackCounter;
  6852. }
  6853. this.next();
  6854. }
  6855. return true;
  6856. }
  6857. return false;
  6858. }
  6859. tsIsUnambiguouslyStartOfFunctionType() {
  6860. this.next();
  6861. if (this.match(types$1.parenR) || this.match(types$1.ellipsis)) {
  6862. return true;
  6863. }
  6864. if (this.tsSkipParameterStart()) {
  6865. if (this.match(types$1.colon) || this.match(types$1.comma) || this.match(types$1.question) || this.match(types$1.eq)) {
  6866. return true;
  6867. }
  6868. if (this.match(types$1.parenR)) {
  6869. this.next();
  6870. if (this.match(types$1.arrow)) {
  6871. return true;
  6872. }
  6873. }
  6874. }
  6875. return false;
  6876. }
  6877. tsParseTypeOrTypePredicateAnnotation(returnToken) {
  6878. return this.tsInType(() => {
  6879. const t = this.startNode();
  6880. this.expect(returnToken);
  6881. const node = this.startNode();
  6882. const asserts = !!this.tsTryParse(this.tsParseTypePredicateAsserts.bind(this));
  6883. if (asserts && this.match(types$1._this)) {
  6884. let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate();
  6885. if (thisTypePredicate.type === "TSThisType") {
  6886. node.parameterName = thisTypePredicate;
  6887. node.asserts = true;
  6888. node.typeAnnotation = null;
  6889. thisTypePredicate = this.finishNode(node, "TSTypePredicate");
  6890. } else {
  6891. this.resetStartLocationFromNode(thisTypePredicate, node);
  6892. thisTypePredicate.asserts = true;
  6893. }
  6894. t.typeAnnotation = thisTypePredicate;
  6895. return this.finishNode(t, "TSTypeAnnotation");
  6896. }
  6897. const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this));
  6898. if (!typePredicateVariable) {
  6899. if (!asserts) {
  6900. return this.tsParseTypeAnnotation(false, t);
  6901. }
  6902. node.parameterName = this.parseIdentifier();
  6903. node.asserts = asserts;
  6904. node.typeAnnotation = null;
  6905. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  6906. return this.finishNode(t, "TSTypeAnnotation");
  6907. }
  6908. const type = this.tsParseTypeAnnotation(false);
  6909. node.parameterName = typePredicateVariable;
  6910. node.typeAnnotation = type;
  6911. node.asserts = asserts;
  6912. t.typeAnnotation = this.finishNode(node, "TSTypePredicate");
  6913. return this.finishNode(t, "TSTypeAnnotation");
  6914. });
  6915. }
  6916. tsTryParseTypeOrTypePredicateAnnotation() {
  6917. return this.match(types$1.colon) ? this.tsParseTypeOrTypePredicateAnnotation(types$1.colon) : undefined;
  6918. }
  6919. tsTryParseTypeAnnotation() {
  6920. return this.match(types$1.colon) ? this.tsParseTypeAnnotation() : undefined;
  6921. }
  6922. tsTryParseType() {
  6923. return this.tsEatThenParseType(types$1.colon);
  6924. }
  6925. tsParseTypePredicatePrefix() {
  6926. const id = this.parseIdentifier();
  6927. if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
  6928. this.next();
  6929. return id;
  6930. }
  6931. }
  6932. tsParseTypePredicateAsserts() {
  6933. if (!this.match(types$1.name) || this.state.value !== "asserts" || this.hasPrecedingLineBreak()) {
  6934. return false;
  6935. }
  6936. const containsEsc = this.state.containsEsc;
  6937. this.next();
  6938. if (!this.match(types$1.name) && !this.match(types$1._this)) {
  6939. return false;
  6940. }
  6941. if (containsEsc) {
  6942. this.raise(this.state.lastTokStart, ErrorMessages.InvalidEscapedReservedWord, "asserts");
  6943. }
  6944. return true;
  6945. }
  6946. tsParseTypeAnnotation(eatColon = true, t = this.startNode()) {
  6947. this.tsInType(() => {
  6948. if (eatColon) this.expect(types$1.colon);
  6949. t.typeAnnotation = this.tsParseType();
  6950. });
  6951. return this.finishNode(t, "TSTypeAnnotation");
  6952. }
  6953. tsParseType() {
  6954. assert(this.state.inType);
  6955. const type = this.tsParseNonConditionalType();
  6956. if (this.hasPrecedingLineBreak() || !this.eat(types$1._extends)) {
  6957. return type;
  6958. }
  6959. const node = this.startNodeAtNode(type);
  6960. node.checkType = type;
  6961. node.extendsType = this.tsParseNonConditionalType();
  6962. this.expect(types$1.question);
  6963. node.trueType = this.tsParseType();
  6964. this.expect(types$1.colon);
  6965. node.falseType = this.tsParseType();
  6966. return this.finishNode(node, "TSConditionalType");
  6967. }
  6968. isAbstractConstructorSignature() {
  6969. return this.isContextual("abstract") && this.lookahead().type === types$1._new;
  6970. }
  6971. tsParseNonConditionalType() {
  6972. if (this.tsIsStartOfFunctionType()) {
  6973. return this.tsParseFunctionOrConstructorType("TSFunctionType");
  6974. }
  6975. if (this.match(types$1._new)) {
  6976. return this.tsParseFunctionOrConstructorType("TSConstructorType");
  6977. } else if (this.isAbstractConstructorSignature()) {
  6978. return this.tsParseFunctionOrConstructorType("TSConstructorType", true);
  6979. }
  6980. return this.tsParseUnionTypeOrHigher();
  6981. }
  6982. tsParseTypeAssertion() {
  6983. const node = this.startNode();
  6984. const _const = this.tsTryNextParseConstantContext();
  6985. node.typeAnnotation = _const || this.tsNextThenParseType();
  6986. this.expectRelational(">");
  6987. node.expression = this.parseMaybeUnary();
  6988. return this.finishNode(node, "TSTypeAssertion");
  6989. }
  6990. tsParseHeritageClause(descriptor) {
  6991. const originalStart = this.state.start;
  6992. const delimitedList = this.tsParseDelimitedList("HeritageClauseElement", this.tsParseExpressionWithTypeArguments.bind(this));
  6993. if (!delimitedList.length) {
  6994. this.raise(originalStart, TSErrors.EmptyHeritageClauseType, descriptor);
  6995. }
  6996. return delimitedList;
  6997. }
  6998. tsParseExpressionWithTypeArguments() {
  6999. const node = this.startNode();
  7000. node.expression = this.tsParseEntityName(false);
  7001. if (this.isRelational("<")) {
  7002. node.typeParameters = this.tsParseTypeArguments();
  7003. }
  7004. return this.finishNode(node, "TSExpressionWithTypeArguments");
  7005. }
  7006. tsParseInterfaceDeclaration(node) {
  7007. if (this.match(types$1.name)) {
  7008. node.id = this.parseIdentifier();
  7009. this.checkLVal(node.id, "typescript interface declaration", BIND_TS_INTERFACE);
  7010. } else {
  7011. node.id = null;
  7012. this.raise(this.state.start, TSErrors.MissingInterfaceName);
  7013. }
  7014. node.typeParameters = this.tsTryParseTypeParameters();
  7015. if (this.eat(types$1._extends)) {
  7016. node.extends = this.tsParseHeritageClause("extends");
  7017. }
  7018. const body = this.startNode();
  7019. body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
  7020. node.body = this.finishNode(body, "TSInterfaceBody");
  7021. return this.finishNode(node, "TSInterfaceDeclaration");
  7022. }
  7023. tsParseTypeAliasDeclaration(node) {
  7024. node.id = this.parseIdentifier();
  7025. this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
  7026. node.typeParameters = this.tsTryParseTypeParameters();
  7027. node.typeAnnotation = this.tsInType(() => {
  7028. this.expect(types$1.eq);
  7029. if (this.isContextual("intrinsic") && this.lookahead().type !== types$1.dot) {
  7030. const node = this.startNode();
  7031. this.next();
  7032. return this.finishNode(node, "TSIntrinsicKeyword");
  7033. }
  7034. return this.tsParseType();
  7035. });
  7036. this.semicolon();
  7037. return this.finishNode(node, "TSTypeAliasDeclaration");
  7038. }
  7039. tsInNoContext(cb) {
  7040. const oldContext = this.state.context;
  7041. this.state.context = [oldContext[0]];
  7042. try {
  7043. return cb();
  7044. } finally {
  7045. this.state.context = oldContext;
  7046. }
  7047. }
  7048. tsInType(cb) {
  7049. const oldInType = this.state.inType;
  7050. this.state.inType = true;
  7051. try {
  7052. return cb();
  7053. } finally {
  7054. this.state.inType = oldInType;
  7055. }
  7056. }
  7057. tsEatThenParseType(token) {
  7058. return !this.match(token) ? undefined : this.tsNextThenParseType();
  7059. }
  7060. tsExpectThenParseType(token) {
  7061. return this.tsDoThenParseType(() => this.expect(token));
  7062. }
  7063. tsNextThenParseType() {
  7064. return this.tsDoThenParseType(() => this.next());
  7065. }
  7066. tsDoThenParseType(cb) {
  7067. return this.tsInType(() => {
  7068. cb();
  7069. return this.tsParseType();
  7070. });
  7071. }
  7072. tsParseEnumMember() {
  7073. const node = this.startNode();
  7074. node.id = this.match(types$1.string) ? this.parseExprAtom() : this.parseIdentifier(true);
  7075. if (this.eat(types$1.eq)) {
  7076. node.initializer = this.parseMaybeAssignAllowIn();
  7077. }
  7078. return this.finishNode(node, "TSEnumMember");
  7079. }
  7080. tsParseEnumDeclaration(node, isConst) {
  7081. if (isConst) node.const = true;
  7082. node.id = this.parseIdentifier();
  7083. this.checkLVal(node.id, "typescript enum declaration", isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM);
  7084. this.expect(types$1.braceL);
  7085. node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this));
  7086. this.expect(types$1.braceR);
  7087. return this.finishNode(node, "TSEnumDeclaration");
  7088. }
  7089. tsParseModuleBlock() {
  7090. const node = this.startNode();
  7091. this.scope.enter(SCOPE_OTHER);
  7092. this.expect(types$1.braceL);
  7093. this.parseBlockOrModuleBlockBody(node.body = [], undefined, true, types$1.braceR);
  7094. this.scope.exit();
  7095. return this.finishNode(node, "TSModuleBlock");
  7096. }
  7097. tsParseModuleOrNamespaceDeclaration(node, nested = false) {
  7098. node.id = this.parseIdentifier();
  7099. if (!nested) {
  7100. this.checkLVal(node.id, "module or namespace declaration", BIND_TS_NAMESPACE);
  7101. }
  7102. if (this.eat(types$1.dot)) {
  7103. const inner = this.startNode();
  7104. this.tsParseModuleOrNamespaceDeclaration(inner, true);
  7105. node.body = inner;
  7106. } else {
  7107. this.scope.enter(SCOPE_TS_MODULE);
  7108. this.prodParam.enter(PARAM);
  7109. node.body = this.tsParseModuleBlock();
  7110. this.prodParam.exit();
  7111. this.scope.exit();
  7112. }
  7113. return this.finishNode(node, "TSModuleDeclaration");
  7114. }
  7115. tsParseAmbientExternalModuleDeclaration(node) {
  7116. if (this.isContextual("global")) {
  7117. node.global = true;
  7118. node.id = this.parseIdentifier();
  7119. } else if (this.match(types$1.string)) {
  7120. node.id = this.parseExprAtom();
  7121. } else {
  7122. this.unexpected();
  7123. }
  7124. if (this.match(types$1.braceL)) {
  7125. this.scope.enter(SCOPE_TS_MODULE);
  7126. this.prodParam.enter(PARAM);
  7127. node.body = this.tsParseModuleBlock();
  7128. this.prodParam.exit();
  7129. this.scope.exit();
  7130. } else {
  7131. this.semicolon();
  7132. }
  7133. return this.finishNode(node, "TSModuleDeclaration");
  7134. }
  7135. tsParseImportEqualsDeclaration(node, isExport) {
  7136. node.isExport = isExport || false;
  7137. node.id = this.parseIdentifier();
  7138. this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
  7139. this.expect(types$1.eq);
  7140. const moduleReference = this.tsParseModuleReference();
  7141. if (node.importKind === "type" && moduleReference.type !== "TSExternalModuleReference") {
  7142. this.raise(moduleReference.start, TSErrors.ImportAliasHasImportType);
  7143. }
  7144. node.moduleReference = moduleReference;
  7145. this.semicolon();
  7146. return this.finishNode(node, "TSImportEqualsDeclaration");
  7147. }
  7148. tsIsExternalModuleReference() {
  7149. return this.isContextual("require") && this.lookaheadCharCode() === 40;
  7150. }
  7151. tsParseModuleReference() {
  7152. return this.tsIsExternalModuleReference() ? this.tsParseExternalModuleReference() : this.tsParseEntityName(false);
  7153. }
  7154. tsParseExternalModuleReference() {
  7155. const node = this.startNode();
  7156. this.expectContextual("require");
  7157. this.expect(types$1.parenL);
  7158. if (!this.match(types$1.string)) {
  7159. throw this.unexpected();
  7160. }
  7161. node.expression = this.parseExprAtom();
  7162. this.expect(types$1.parenR);
  7163. return this.finishNode(node, "TSExternalModuleReference");
  7164. }
  7165. tsLookAhead(f) {
  7166. const state = this.state.clone();
  7167. const res = f();
  7168. this.state = state;
  7169. return res;
  7170. }
  7171. tsTryParseAndCatch(f) {
  7172. const result = this.tryParse(abort => f() || abort());
  7173. if (result.aborted || !result.node) return undefined;
  7174. if (result.error) this.state = result.failState;
  7175. return result.node;
  7176. }
  7177. tsTryParse(f) {
  7178. const state = this.state.clone();
  7179. const result = f();
  7180. if (result !== undefined && result !== false) {
  7181. return result;
  7182. } else {
  7183. this.state = state;
  7184. return undefined;
  7185. }
  7186. }
  7187. tsTryParseDeclare(nany) {
  7188. if (this.isLineTerminator()) {
  7189. return;
  7190. }
  7191. let starttype = this.state.type;
  7192. let kind;
  7193. if (this.isContextual("let")) {
  7194. starttype = types$1._var;
  7195. kind = "let";
  7196. }
  7197. return this.tsInAmbientContext(() => {
  7198. switch (starttype) {
  7199. case types$1._function:
  7200. nany.declare = true;
  7201. return this.parseFunctionStatement(nany, false, true);
  7202. case types$1._class:
  7203. nany.declare = true;
  7204. return this.parseClass(nany, true, false);
  7205. case types$1._const:
  7206. if (this.match(types$1._const) && this.isLookaheadContextual("enum")) {
  7207. this.expect(types$1._const);
  7208. this.expectContextual("enum");
  7209. return this.tsParseEnumDeclaration(nany, true);
  7210. }
  7211. case types$1._var:
  7212. kind = kind || this.state.value;
  7213. return this.parseVarStatement(nany, kind);
  7214. case types$1.name:
  7215. {
  7216. const value = this.state.value;
  7217. if (value === "global") {
  7218. return this.tsParseAmbientExternalModuleDeclaration(nany);
  7219. } else {
  7220. return this.tsParseDeclaration(nany, value, true);
  7221. }
  7222. }
  7223. }
  7224. });
  7225. }
  7226. tsTryParseExportDeclaration() {
  7227. return this.tsParseDeclaration(this.startNode(), this.state.value, true);
  7228. }
  7229. tsParseExpressionStatement(node, expr) {
  7230. switch (expr.name) {
  7231. case "declare":
  7232. {
  7233. const declaration = this.tsTryParseDeclare(node);
  7234. if (declaration) {
  7235. declaration.declare = true;
  7236. return declaration;
  7237. }
  7238. break;
  7239. }
  7240. case "global":
  7241. if (this.match(types$1.braceL)) {
  7242. this.scope.enter(SCOPE_TS_MODULE);
  7243. this.prodParam.enter(PARAM);
  7244. const mod = node;
  7245. mod.global = true;
  7246. mod.id = expr;
  7247. mod.body = this.tsParseModuleBlock();
  7248. this.scope.exit();
  7249. this.prodParam.exit();
  7250. return this.finishNode(mod, "TSModuleDeclaration");
  7251. }
  7252. break;
  7253. default:
  7254. return this.tsParseDeclaration(node, expr.name, false);
  7255. }
  7256. }
  7257. tsParseDeclaration(node, value, next) {
  7258. switch (value) {
  7259. case "abstract":
  7260. if (this.tsCheckLineTerminator(next) && (this.match(types$1._class) || this.match(types$1.name))) {
  7261. return this.tsParseAbstractDeclaration(node);
  7262. }
  7263. break;
  7264. case "enum":
  7265. if (next || this.match(types$1.name)) {
  7266. if (next) this.next();
  7267. return this.tsParseEnumDeclaration(node, false);
  7268. }
  7269. break;
  7270. case "interface":
  7271. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  7272. return this.tsParseInterfaceDeclaration(node);
  7273. }
  7274. break;
  7275. case "module":
  7276. if (this.tsCheckLineTerminator(next)) {
  7277. if (this.match(types$1.string)) {
  7278. return this.tsParseAmbientExternalModuleDeclaration(node);
  7279. } else if (this.match(types$1.name)) {
  7280. return this.tsParseModuleOrNamespaceDeclaration(node);
  7281. }
  7282. }
  7283. break;
  7284. case "namespace":
  7285. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  7286. return this.tsParseModuleOrNamespaceDeclaration(node);
  7287. }
  7288. break;
  7289. case "type":
  7290. if (this.tsCheckLineTerminator(next) && this.match(types$1.name)) {
  7291. return this.tsParseTypeAliasDeclaration(node);
  7292. }
  7293. break;
  7294. }
  7295. }
  7296. tsCheckLineTerminator(next) {
  7297. if (next) {
  7298. if (this.hasFollowingLineBreak()) return false;
  7299. this.next();
  7300. return true;
  7301. }
  7302. return !this.isLineTerminator();
  7303. }
  7304. tsTryParseGenericAsyncArrowFunction(startPos, startLoc) {
  7305. if (!this.isRelational("<")) {
  7306. return undefined;
  7307. }
  7308. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  7309. this.state.maybeInArrowParameters = true;
  7310. const res = this.tsTryParseAndCatch(() => {
  7311. const node = this.startNodeAt(startPos, startLoc);
  7312. node.typeParameters = this.tsParseTypeParameters();
  7313. super.parseFunctionParams(node);
  7314. node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation();
  7315. this.expect(types$1.arrow);
  7316. return node;
  7317. });
  7318. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  7319. if (!res) {
  7320. return undefined;
  7321. }
  7322. return this.parseArrowExpression(res, null, true);
  7323. }
  7324. tsParseTypeArguments() {
  7325. const node = this.startNode();
  7326. node.params = this.tsInType(() => this.tsInNoContext(() => {
  7327. this.expectRelational("<");
  7328. return this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this));
  7329. }));
  7330. if (node.params.length === 0) {
  7331. this.raise(node.start, TSErrors.EmptyTypeArguments);
  7332. }
  7333. this.expectRelational(">");
  7334. return this.finishNode(node, "TSTypeParameterInstantiation");
  7335. }
  7336. tsIsDeclarationStart() {
  7337. if (this.match(types$1.name)) {
  7338. switch (this.state.value) {
  7339. case "abstract":
  7340. case "declare":
  7341. case "enum":
  7342. case "interface":
  7343. case "module":
  7344. case "namespace":
  7345. case "type":
  7346. return true;
  7347. }
  7348. }
  7349. return false;
  7350. }
  7351. isExportDefaultSpecifier() {
  7352. if (this.tsIsDeclarationStart()) return false;
  7353. return super.isExportDefaultSpecifier();
  7354. }
  7355. parseAssignableListItem(allowModifiers, decorators) {
  7356. const startPos = this.state.start;
  7357. const startLoc = this.state.startLoc;
  7358. let accessibility;
  7359. let readonly = false;
  7360. let override = false;
  7361. if (allowModifiers !== undefined) {
  7362. const modified = {};
  7363. this.tsParseModifiers(modified, ["public", "private", "protected", "override", "readonly"]);
  7364. accessibility = modified.accessibility;
  7365. override = modified.override;
  7366. readonly = modified.readonly;
  7367. if (allowModifiers === false && (accessibility || readonly || override)) {
  7368. this.raise(startPos, TSErrors.UnexpectedParameterModifier);
  7369. }
  7370. }
  7371. const left = this.parseMaybeDefault();
  7372. this.parseAssignableListItemTypes(left);
  7373. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  7374. if (accessibility || readonly || override) {
  7375. const pp = this.startNodeAt(startPos, startLoc);
  7376. if (decorators.length) {
  7377. pp.decorators = decorators;
  7378. }
  7379. if (accessibility) pp.accessibility = accessibility;
  7380. if (readonly) pp.readonly = readonly;
  7381. if (override) pp.override = override;
  7382. if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") {
  7383. this.raise(pp.start, TSErrors.UnsupportedParameterPropertyKind);
  7384. }
  7385. pp.parameter = elt;
  7386. return this.finishNode(pp, "TSParameterProperty");
  7387. }
  7388. if (decorators.length) {
  7389. left.decorators = decorators;
  7390. }
  7391. return elt;
  7392. }
  7393. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  7394. if (this.match(types$1.colon)) {
  7395. node.returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  7396. }
  7397. const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" ? "TSDeclareMethod" : undefined;
  7398. if (bodilessType && !this.match(types$1.braceL) && this.isLineTerminator()) {
  7399. this.finishNode(node, bodilessType);
  7400. return;
  7401. }
  7402. if (bodilessType === "TSDeclareFunction" && this.state.isAmbientContext) {
  7403. this.raise(node.start, TSErrors.DeclareFunctionHasImplementation);
  7404. if (node.declare) {
  7405. super.parseFunctionBodyAndFinish(node, bodilessType, isMethod);
  7406. return;
  7407. }
  7408. }
  7409. super.parseFunctionBodyAndFinish(node, type, isMethod);
  7410. }
  7411. registerFunctionStatementId(node) {
  7412. if (!node.body && node.id) {
  7413. this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
  7414. } else {
  7415. super.registerFunctionStatementId(...arguments);
  7416. }
  7417. }
  7418. tsCheckForInvalidTypeCasts(items) {
  7419. items.forEach(node => {
  7420. if ((node == null ? void 0 : node.type) === "TSTypeCastExpression") {
  7421. this.raise(node.typeAnnotation.start, TSErrors.UnexpectedTypeAnnotation);
  7422. }
  7423. });
  7424. }
  7425. toReferencedList(exprList, isInParens) {
  7426. this.tsCheckForInvalidTypeCasts(exprList);
  7427. return exprList;
  7428. }
  7429. parseArrayLike(...args) {
  7430. const node = super.parseArrayLike(...args);
  7431. if (node.type === "ArrayExpression") {
  7432. this.tsCheckForInvalidTypeCasts(node.elements);
  7433. }
  7434. return node;
  7435. }
  7436. parseSubscript(base, startPos, startLoc, noCalls, state) {
  7437. if (!this.hasPrecedingLineBreak() && this.match(types$1.bang)) {
  7438. this.state.exprAllowed = false;
  7439. this.next();
  7440. const nonNullExpression = this.startNodeAt(startPos, startLoc);
  7441. nonNullExpression.expression = base;
  7442. return this.finishNode(nonNullExpression, "TSNonNullExpression");
  7443. }
  7444. let isOptionalCall = false;
  7445. if (this.match(types$1.questionDot) && this.lookaheadCharCode() === 60) {
  7446. if (noCalls) {
  7447. state.stop = true;
  7448. return base;
  7449. }
  7450. state.optionalChainMember = isOptionalCall = true;
  7451. this.next();
  7452. }
  7453. if (this.isRelational("<")) {
  7454. let missingParenErrorPos;
  7455. const result = this.tsTryParseAndCatch(() => {
  7456. if (!noCalls && this.atPossibleAsyncArrow(base)) {
  7457. const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc);
  7458. if (asyncArrowFn) {
  7459. return asyncArrowFn;
  7460. }
  7461. }
  7462. const node = this.startNodeAt(startPos, startLoc);
  7463. node.callee = base;
  7464. const typeArguments = this.tsParseTypeArguments();
  7465. if (typeArguments) {
  7466. if (isOptionalCall && !this.match(types$1.parenL)) {
  7467. missingParenErrorPos = this.state.pos;
  7468. this.unexpected();
  7469. }
  7470. if (!noCalls && this.eat(types$1.parenL)) {
  7471. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  7472. this.tsCheckForInvalidTypeCasts(node.arguments);
  7473. node.typeParameters = typeArguments;
  7474. if (state.optionalChainMember) {
  7475. node.optional = isOptionalCall;
  7476. }
  7477. return this.finishCallExpression(node, state.optionalChainMember);
  7478. } else if (this.match(types$1.backQuote)) {
  7479. const result = this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  7480. result.typeParameters = typeArguments;
  7481. return result;
  7482. }
  7483. }
  7484. this.unexpected();
  7485. });
  7486. if (missingParenErrorPos) {
  7487. this.unexpected(missingParenErrorPos, types$1.parenL);
  7488. }
  7489. if (result) return result;
  7490. }
  7491. return super.parseSubscript(base, startPos, startLoc, noCalls, state);
  7492. }
  7493. parseNewArguments(node) {
  7494. if (this.isRelational("<")) {
  7495. const typeParameters = this.tsTryParseAndCatch(() => {
  7496. const args = this.tsParseTypeArguments();
  7497. if (!this.match(types$1.parenL)) this.unexpected();
  7498. return args;
  7499. });
  7500. if (typeParameters) {
  7501. node.typeParameters = typeParameters;
  7502. }
  7503. }
  7504. super.parseNewArguments(node);
  7505. }
  7506. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  7507. if (nonNull(types$1._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.isContextual("as")) {
  7508. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  7509. node.expression = left;
  7510. const _const = this.tsTryNextParseConstantContext();
  7511. if (_const) {
  7512. node.typeAnnotation = _const;
  7513. } else {
  7514. node.typeAnnotation = this.tsNextThenParseType();
  7515. }
  7516. this.finishNode(node, "TSAsExpression");
  7517. this.reScan_lt_gt();
  7518. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  7519. }
  7520. return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec);
  7521. }
  7522. checkReservedWord(word, startLoc, checkKeywords, isBinding) {}
  7523. checkDuplicateExports() {}
  7524. parseImport(node) {
  7525. node.importKind = "value";
  7526. if (this.match(types$1.name) || this.match(types$1.star) || this.match(types$1.braceL)) {
  7527. let ahead = this.lookahead();
  7528. if (this.isContextual("type") && ahead.type !== types$1.comma && !(ahead.type === types$1.name && ahead.value === "from") && ahead.type !== types$1.eq) {
  7529. node.importKind = "type";
  7530. this.next();
  7531. ahead = this.lookahead();
  7532. }
  7533. if (this.match(types$1.name) && ahead.type === types$1.eq) {
  7534. return this.tsParseImportEqualsDeclaration(node);
  7535. }
  7536. }
  7537. const importNode = super.parseImport(node);
  7538. if (importNode.importKind === "type" && importNode.specifiers.length > 1 && importNode.specifiers[0].type === "ImportDefaultSpecifier") {
  7539. this.raise(importNode.start, TSErrors.TypeImportCannotSpecifyDefaultAndNamed);
  7540. }
  7541. return importNode;
  7542. }
  7543. parseExport(node) {
  7544. if (this.match(types$1._import)) {
  7545. this.next();
  7546. if (this.isContextual("type") && this.lookaheadCharCode() !== 61) {
  7547. node.importKind = "type";
  7548. this.next();
  7549. } else {
  7550. node.importKind = "value";
  7551. }
  7552. return this.tsParseImportEqualsDeclaration(node, true);
  7553. } else if (this.eat(types$1.eq)) {
  7554. const assign = node;
  7555. assign.expression = this.parseExpression();
  7556. this.semicolon();
  7557. return this.finishNode(assign, "TSExportAssignment");
  7558. } else if (this.eatContextual("as")) {
  7559. const decl = node;
  7560. this.expectContextual("namespace");
  7561. decl.id = this.parseIdentifier();
  7562. this.semicolon();
  7563. return this.finishNode(decl, "TSNamespaceExportDeclaration");
  7564. } else {
  7565. if (this.isContextual("type") && this.lookahead().type === types$1.braceL) {
  7566. this.next();
  7567. node.exportKind = "type";
  7568. } else {
  7569. node.exportKind = "value";
  7570. }
  7571. return super.parseExport(node);
  7572. }
  7573. }
  7574. isAbstractClass() {
  7575. return this.isContextual("abstract") && this.lookahead().type === types$1._class;
  7576. }
  7577. parseExportDefaultExpression() {
  7578. if (this.isAbstractClass()) {
  7579. const cls = this.startNode();
  7580. this.next();
  7581. cls.abstract = true;
  7582. this.parseClass(cls, true, true);
  7583. return cls;
  7584. }
  7585. if (this.state.value === "interface") {
  7586. const interfaceNode = this.startNode();
  7587. this.next();
  7588. const result = this.tsParseInterfaceDeclaration(interfaceNode);
  7589. if (result) return result;
  7590. }
  7591. return super.parseExportDefaultExpression();
  7592. }
  7593. parseStatementContent(context, topLevel) {
  7594. if (this.state.type === types$1._const) {
  7595. const ahead = this.lookahead();
  7596. if (ahead.type === types$1.name && ahead.value === "enum") {
  7597. const node = this.startNode();
  7598. this.expect(types$1._const);
  7599. this.expectContextual("enum");
  7600. return this.tsParseEnumDeclaration(node, true);
  7601. }
  7602. }
  7603. return super.parseStatementContent(context, topLevel);
  7604. }
  7605. parseAccessModifier() {
  7606. return this.tsParseModifier(["public", "protected", "private"]);
  7607. }
  7608. tsHasSomeModifiers(member, modifiers) {
  7609. return modifiers.some(modifier => {
  7610. if (tsIsAccessModifier(modifier)) {
  7611. return member.accessibility === modifier;
  7612. }
  7613. return !!member[modifier];
  7614. });
  7615. }
  7616. tsIsStartOfStaticBlocks() {
  7617. return this.isContextual("static") && this.lookaheadCharCode() === 123;
  7618. }
  7619. parseClassMember(classBody, member, state) {
  7620. const modifiers = ["declare", "private", "public", "protected", "override", "abstract", "readonly", "static"];
  7621. this.tsParseModifiers(member, modifiers, undefined, undefined, true);
  7622. const callParseClassMemberWithIsStatic = () => {
  7623. if (this.tsIsStartOfStaticBlocks()) {
  7624. this.next();
  7625. this.next();
  7626. if (this.tsHasSomeModifiers(member, modifiers)) {
  7627. this.raise(this.state.pos, TSErrors.StaticBlockCannotHaveModifier);
  7628. }
  7629. this.parseClassStaticBlock(classBody, member);
  7630. } else {
  7631. this.parseClassMemberWithIsStatic(classBody, member, state, !!member.static);
  7632. }
  7633. };
  7634. if (member.declare) {
  7635. this.tsInAmbientContext(callParseClassMemberWithIsStatic);
  7636. } else {
  7637. callParseClassMemberWithIsStatic();
  7638. }
  7639. }
  7640. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  7641. const idx = this.tsTryParseIndexSignature(member);
  7642. if (idx) {
  7643. classBody.body.push(idx);
  7644. if (member.abstract) {
  7645. this.raise(member.start, TSErrors.IndexSignatureHasAbstract);
  7646. }
  7647. if (member.accessibility) {
  7648. this.raise(member.start, TSErrors.IndexSignatureHasAccessibility, member.accessibility);
  7649. }
  7650. if (member.declare) {
  7651. this.raise(member.start, TSErrors.IndexSignatureHasDeclare);
  7652. }
  7653. if (member.override) {
  7654. this.raise(member.start, TSErrors.IndexSignatureHasOverride);
  7655. }
  7656. return;
  7657. }
  7658. if (!this.state.inAbstractClass && member.abstract) {
  7659. this.raise(member.start, TSErrors.NonAbstractClassHasAbstractMethod);
  7660. }
  7661. if (member.override) {
  7662. if (!state.hadSuperClass) {
  7663. this.raise(member.start, TSErrors.OverrideNotInSubClass);
  7664. }
  7665. }
  7666. super.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  7667. }
  7668. parsePostMemberNameModifiers(methodOrProp) {
  7669. const optional = this.eat(types$1.question);
  7670. if (optional) methodOrProp.optional = true;
  7671. if (methodOrProp.readonly && this.match(types$1.parenL)) {
  7672. this.raise(methodOrProp.start, TSErrors.ClassMethodHasReadonly);
  7673. }
  7674. if (methodOrProp.declare && this.match(types$1.parenL)) {
  7675. this.raise(methodOrProp.start, TSErrors.ClassMethodHasDeclare);
  7676. }
  7677. }
  7678. parseExpressionStatement(node, expr) {
  7679. const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined;
  7680. return decl || super.parseExpressionStatement(node, expr);
  7681. }
  7682. shouldParseExportDeclaration() {
  7683. if (this.tsIsDeclarationStart()) return true;
  7684. return super.shouldParseExportDeclaration();
  7685. }
  7686. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  7687. if (!this.state.maybeInArrowParameters || !this.match(types$1.question)) {
  7688. return super.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  7689. }
  7690. const result = this.tryParse(() => super.parseConditional(expr, startPos, startLoc));
  7691. if (!result.node) {
  7692. if (result.error) {
  7693. super.setOptionalParametersError(refExpressionErrors, result.error);
  7694. }
  7695. return expr;
  7696. }
  7697. if (result.error) this.state = result.failState;
  7698. return result.node;
  7699. }
  7700. parseParenItem(node, startPos, startLoc) {
  7701. node = super.parseParenItem(node, startPos, startLoc);
  7702. if (this.eat(types$1.question)) {
  7703. node.optional = true;
  7704. this.resetEndLocation(node);
  7705. }
  7706. if (this.match(types$1.colon)) {
  7707. const typeCastNode = this.startNodeAt(startPos, startLoc);
  7708. typeCastNode.expression = node;
  7709. typeCastNode.typeAnnotation = this.tsParseTypeAnnotation();
  7710. return this.finishNode(typeCastNode, "TSTypeCastExpression");
  7711. }
  7712. return node;
  7713. }
  7714. parseExportDeclaration(node) {
  7715. const startPos = this.state.start;
  7716. const startLoc = this.state.startLoc;
  7717. const isDeclare = this.eatContextual("declare");
  7718. if (isDeclare && (this.isContextual("declare") || !this.shouldParseExportDeclaration())) {
  7719. throw this.raise(this.state.start, TSErrors.ExpectedAmbientAfterExportDeclare);
  7720. }
  7721. let declaration;
  7722. if (this.match(types$1.name)) {
  7723. declaration = this.tsTryParseExportDeclaration();
  7724. }
  7725. if (!declaration) {
  7726. declaration = super.parseExportDeclaration(node);
  7727. }
  7728. if (declaration && (declaration.type === "TSInterfaceDeclaration" || declaration.type === "TSTypeAliasDeclaration" || isDeclare)) {
  7729. node.exportKind = "type";
  7730. }
  7731. if (declaration && isDeclare) {
  7732. this.resetStartLocation(declaration, startPos, startLoc);
  7733. declaration.declare = true;
  7734. }
  7735. return declaration;
  7736. }
  7737. parseClassId(node, isStatement, optionalId) {
  7738. if ((!isStatement || optionalId) && this.isContextual("implements")) {
  7739. return;
  7740. }
  7741. super.parseClassId(node, isStatement, optionalId, node.declare ? BIND_TS_AMBIENT : BIND_CLASS);
  7742. const typeParameters = this.tsTryParseTypeParameters();
  7743. if (typeParameters) node.typeParameters = typeParameters;
  7744. }
  7745. parseClassPropertyAnnotation(node) {
  7746. if (!node.optional && this.eat(types$1.bang)) {
  7747. node.definite = true;
  7748. }
  7749. const type = this.tsTryParseTypeAnnotation();
  7750. if (type) node.typeAnnotation = type;
  7751. }
  7752. parseClassProperty(node) {
  7753. this.parseClassPropertyAnnotation(node);
  7754. if (this.state.isAmbientContext && this.match(types$1.eq)) {
  7755. this.raise(this.state.start, TSErrors.DeclareClassFieldHasInitializer);
  7756. }
  7757. if (node.abstract && this.match(types$1.eq)) {
  7758. const {
  7759. key
  7760. } = node;
  7761. this.raise(this.state.start, TSErrors.AbstractPropertyHasInitializer, key.type === "Identifier" && !node.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  7762. }
  7763. return super.parseClassProperty(node);
  7764. }
  7765. parseClassPrivateProperty(node) {
  7766. if (node.abstract) {
  7767. this.raise(node.start, TSErrors.PrivateElementHasAbstract);
  7768. }
  7769. if (node.accessibility) {
  7770. this.raise(node.start, TSErrors.PrivateElementHasAccessibility, node.accessibility);
  7771. }
  7772. this.parseClassPropertyAnnotation(node);
  7773. return super.parseClassPrivateProperty(node);
  7774. }
  7775. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  7776. const typeParameters = this.tsTryParseTypeParameters();
  7777. if (typeParameters && isConstructor) {
  7778. this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
  7779. }
  7780. if (method.declare && (method.kind === "get" || method.kind === "set")) {
  7781. this.raise(method.start, TSErrors.DeclareAccessor, method.kind);
  7782. }
  7783. if (typeParameters) method.typeParameters = typeParameters;
  7784. super.pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper);
  7785. }
  7786. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  7787. const typeParameters = this.tsTryParseTypeParameters();
  7788. if (typeParameters) method.typeParameters = typeParameters;
  7789. super.pushClassPrivateMethod(classBody, method, isGenerator, isAsync);
  7790. }
  7791. parseClassSuper(node) {
  7792. super.parseClassSuper(node);
  7793. if (node.superClass && this.isRelational("<")) {
  7794. node.superTypeParameters = this.tsParseTypeArguments();
  7795. }
  7796. if (this.eatContextual("implements")) {
  7797. node.implements = this.tsParseHeritageClause("implements");
  7798. }
  7799. }
  7800. parseObjPropValue(prop, ...args) {
  7801. const typeParameters = this.tsTryParseTypeParameters();
  7802. if (typeParameters) prop.typeParameters = typeParameters;
  7803. super.parseObjPropValue(prop, ...args);
  7804. }
  7805. parseFunctionParams(node, allowModifiers) {
  7806. const typeParameters = this.tsTryParseTypeParameters();
  7807. if (typeParameters) node.typeParameters = typeParameters;
  7808. super.parseFunctionParams(node, allowModifiers);
  7809. }
  7810. parseVarId(decl, kind) {
  7811. super.parseVarId(decl, kind);
  7812. if (decl.id.type === "Identifier" && this.eat(types$1.bang)) {
  7813. decl.definite = true;
  7814. }
  7815. const type = this.tsTryParseTypeAnnotation();
  7816. if (type) {
  7817. decl.id.typeAnnotation = type;
  7818. this.resetEndLocation(decl.id);
  7819. }
  7820. }
  7821. parseAsyncArrowFromCallExpression(node, call) {
  7822. if (this.match(types$1.colon)) {
  7823. node.returnType = this.tsParseTypeAnnotation();
  7824. }
  7825. return super.parseAsyncArrowFromCallExpression(node, call);
  7826. }
  7827. parseMaybeAssign(...args) {
  7828. var _jsx, _jsx2, _typeCast, _jsx3, _typeCast2, _jsx4, _typeCast3;
  7829. let state;
  7830. let jsx;
  7831. let typeCast;
  7832. if (this.hasPlugin("jsx") && (this.match(types$1.jsxTagStart) || this.isRelational("<"))) {
  7833. state = this.state.clone();
  7834. jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
  7835. if (!jsx.error) return jsx.node;
  7836. const {
  7837. context
  7838. } = this.state;
  7839. if (context[context.length - 1] === types.j_oTag) {
  7840. context.length -= 2;
  7841. } else if (context[context.length - 1] === types.j_expr) {
  7842. context.length -= 1;
  7843. }
  7844. }
  7845. if (!((_jsx = jsx) != null && _jsx.error) && !this.isRelational("<")) {
  7846. return super.parseMaybeAssign(...args);
  7847. }
  7848. let typeParameters;
  7849. state = state || this.state.clone();
  7850. const arrow = this.tryParse(abort => {
  7851. var _expr$extra, _typeParameters;
  7852. typeParameters = this.tsParseTypeParameters();
  7853. const expr = super.parseMaybeAssign(...args);
  7854. if (expr.type !== "ArrowFunctionExpression" || (_expr$extra = expr.extra) != null && _expr$extra.parenthesized) {
  7855. abort();
  7856. }
  7857. if (((_typeParameters = typeParameters) == null ? void 0 : _typeParameters.params.length) !== 0) {
  7858. this.resetStartLocationFromNode(expr, typeParameters);
  7859. }
  7860. expr.typeParameters = typeParameters;
  7861. return expr;
  7862. }, state);
  7863. if (!arrow.error && !arrow.aborted) return arrow.node;
  7864. if (!jsx) {
  7865. assert(!this.hasPlugin("jsx"));
  7866. typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
  7867. if (!typeCast.error) return typeCast.node;
  7868. }
  7869. if ((_jsx2 = jsx) != null && _jsx2.node) {
  7870. this.state = jsx.failState;
  7871. return jsx.node;
  7872. }
  7873. if (arrow.node) {
  7874. this.state = arrow.failState;
  7875. return arrow.node;
  7876. }
  7877. if ((_typeCast = typeCast) != null && _typeCast.node) {
  7878. this.state = typeCast.failState;
  7879. return typeCast.node;
  7880. }
  7881. if ((_jsx3 = jsx) != null && _jsx3.thrown) throw jsx.error;
  7882. if (arrow.thrown) throw arrow.error;
  7883. if ((_typeCast2 = typeCast) != null && _typeCast2.thrown) throw typeCast.error;
  7884. throw ((_jsx4 = jsx) == null ? void 0 : _jsx4.error) || arrow.error || ((_typeCast3 = typeCast) == null ? void 0 : _typeCast3.error);
  7885. }
  7886. parseMaybeUnary(refExpressionErrors) {
  7887. if (!this.hasPlugin("jsx") && this.isRelational("<")) {
  7888. return this.tsParseTypeAssertion();
  7889. } else {
  7890. return super.parseMaybeUnary(refExpressionErrors);
  7891. }
  7892. }
  7893. parseArrow(node) {
  7894. if (this.match(types$1.colon)) {
  7895. const result = this.tryParse(abort => {
  7896. const returnType = this.tsParseTypeOrTypePredicateAnnotation(types$1.colon);
  7897. if (this.canInsertSemicolon() || !this.match(types$1.arrow)) abort();
  7898. return returnType;
  7899. });
  7900. if (result.aborted) return;
  7901. if (!result.thrown) {
  7902. if (result.error) this.state = result.failState;
  7903. node.returnType = result.node;
  7904. }
  7905. }
  7906. return super.parseArrow(node);
  7907. }
  7908. parseAssignableListItemTypes(param) {
  7909. if (this.eat(types$1.question)) {
  7910. if (param.type !== "Identifier" && !this.state.isAmbientContext && !this.state.inType) {
  7911. this.raise(param.start, TSErrors.PatternIsOptional);
  7912. }
  7913. param.optional = true;
  7914. }
  7915. const type = this.tsTryParseTypeAnnotation();
  7916. if (type) param.typeAnnotation = type;
  7917. this.resetEndLocation(param);
  7918. return param;
  7919. }
  7920. isAssignable(node, isBinding) {
  7921. switch (node.type) {
  7922. case "TSTypeCastExpression":
  7923. return this.isAssignable(node.expression, isBinding);
  7924. case "TSParameterProperty":
  7925. return true;
  7926. default:
  7927. return super.isAssignable(node, isBinding);
  7928. }
  7929. }
  7930. toAssignable(node, isLHS = false) {
  7931. switch (node.type) {
  7932. case "TSTypeCastExpression":
  7933. return super.toAssignable(this.typeCastToParameter(node), isLHS);
  7934. case "TSParameterProperty":
  7935. return super.toAssignable(node, isLHS);
  7936. case "ParenthesizedExpression":
  7937. return this.toAssignableParenthesizedExpression(node, isLHS);
  7938. case "TSAsExpression":
  7939. case "TSNonNullExpression":
  7940. case "TSTypeAssertion":
  7941. node.expression = this.toAssignable(node.expression, isLHS);
  7942. return node;
  7943. default:
  7944. return super.toAssignable(node, isLHS);
  7945. }
  7946. }
  7947. toAssignableParenthesizedExpression(node, isLHS) {
  7948. switch (node.expression.type) {
  7949. case "TSAsExpression":
  7950. case "TSNonNullExpression":
  7951. case "TSTypeAssertion":
  7952. case "ParenthesizedExpression":
  7953. node.expression = this.toAssignable(node.expression, isLHS);
  7954. return node;
  7955. default:
  7956. return super.toAssignable(node, isLHS);
  7957. }
  7958. }
  7959. checkLVal(expr, contextDescription, ...args) {
  7960. var _expr$extra2;
  7961. switch (expr.type) {
  7962. case "TSTypeCastExpression":
  7963. return;
  7964. case "TSParameterProperty":
  7965. this.checkLVal(expr.parameter, "parameter property", ...args);
  7966. return;
  7967. case "TSAsExpression":
  7968. case "TSTypeAssertion":
  7969. if (!args[0] && contextDescription !== "parenthesized expression" && !((_expr$extra2 = expr.extra) != null && _expr$extra2.parenthesized)) {
  7970. this.raise(expr.start, ErrorMessages.InvalidLhs, contextDescription);
  7971. break;
  7972. }
  7973. this.checkLVal(expr.expression, "parenthesized expression", ...args);
  7974. return;
  7975. case "TSNonNullExpression":
  7976. this.checkLVal(expr.expression, contextDescription, ...args);
  7977. return;
  7978. default:
  7979. super.checkLVal(expr, contextDescription, ...args);
  7980. return;
  7981. }
  7982. }
  7983. parseBindingAtom() {
  7984. switch (this.state.type) {
  7985. case types$1._this:
  7986. return this.parseIdentifier(true);
  7987. default:
  7988. return super.parseBindingAtom();
  7989. }
  7990. }
  7991. parseMaybeDecoratorArguments(expr) {
  7992. if (this.isRelational("<")) {
  7993. const typeArguments = this.tsParseTypeArguments();
  7994. if (this.match(types$1.parenL)) {
  7995. const call = super.parseMaybeDecoratorArguments(expr);
  7996. call.typeParameters = typeArguments;
  7997. return call;
  7998. }
  7999. this.unexpected(this.state.start, types$1.parenL);
  8000. }
  8001. return super.parseMaybeDecoratorArguments(expr);
  8002. }
  8003. checkCommaAfterRest(close) {
  8004. if (this.state.isAmbientContext && this.match(types$1.comma) && this.lookaheadCharCode() === close) {
  8005. this.next();
  8006. } else {
  8007. super.checkCommaAfterRest(close);
  8008. }
  8009. }
  8010. isClassMethod() {
  8011. return this.isRelational("<") || super.isClassMethod();
  8012. }
  8013. isClassProperty() {
  8014. return this.match(types$1.bang) || this.match(types$1.colon) || super.isClassProperty();
  8015. }
  8016. parseMaybeDefault(...args) {
  8017. const node = super.parseMaybeDefault(...args);
  8018. if (node.type === "AssignmentPattern" && node.typeAnnotation && node.right.start < node.typeAnnotation.start) {
  8019. this.raise(node.typeAnnotation.start, TSErrors.TypeAnnotationAfterAssign);
  8020. }
  8021. return node;
  8022. }
  8023. getTokenFromCode(code) {
  8024. if (this.state.inType && (code === 62 || code === 60)) {
  8025. return this.finishOp(types$1.relational, 1);
  8026. } else {
  8027. return super.getTokenFromCode(code);
  8028. }
  8029. }
  8030. reScan_lt_gt() {
  8031. if (this.match(types$1.relational)) {
  8032. const code = this.input.charCodeAt(this.state.start);
  8033. if (code === 60 || code === 62) {
  8034. this.state.pos -= 1;
  8035. this.readToken_lt_gt(code);
  8036. }
  8037. }
  8038. }
  8039. toAssignableList(exprList) {
  8040. for (let i = 0; i < exprList.length; i++) {
  8041. const expr = exprList[i];
  8042. if (!expr) continue;
  8043. switch (expr.type) {
  8044. case "TSTypeCastExpression":
  8045. exprList[i] = this.typeCastToParameter(expr);
  8046. break;
  8047. case "TSAsExpression":
  8048. case "TSTypeAssertion":
  8049. if (!this.state.maybeInArrowParameters) {
  8050. exprList[i] = this.typeCastToParameter(expr);
  8051. } else {
  8052. this.raise(expr.start, TSErrors.UnexpectedTypeCastInParameter);
  8053. }
  8054. break;
  8055. }
  8056. }
  8057. return super.toAssignableList(...arguments);
  8058. }
  8059. typeCastToParameter(node) {
  8060. node.expression.typeAnnotation = node.typeAnnotation;
  8061. this.resetEndLocation(node.expression, node.typeAnnotation.end, node.typeAnnotation.loc.end);
  8062. return node.expression;
  8063. }
  8064. shouldParseArrow(params) {
  8065. if (this.match(types$1.colon)) {
  8066. return params.every(expr => this.isAssignable(expr, true));
  8067. }
  8068. return super.shouldParseArrow(params);
  8069. }
  8070. shouldParseAsyncArrow() {
  8071. return this.match(types$1.colon) || super.shouldParseAsyncArrow();
  8072. }
  8073. canHaveLeadingDecorator() {
  8074. return super.canHaveLeadingDecorator() || this.isAbstractClass();
  8075. }
  8076. jsxParseOpeningElementAfterName(node) {
  8077. if (this.isRelational("<")) {
  8078. const typeArguments = this.tsTryParseAndCatch(() => this.tsParseTypeArguments());
  8079. if (typeArguments) node.typeParameters = typeArguments;
  8080. }
  8081. return super.jsxParseOpeningElementAfterName(node);
  8082. }
  8083. getGetterSetterExpectedParamCount(method) {
  8084. const baseCount = super.getGetterSetterExpectedParamCount(method);
  8085. const params = this.getObjectOrClassMethodParams(method);
  8086. const firstParam = params[0];
  8087. const hasContextParam = firstParam && this.isThisParam(firstParam);
  8088. return hasContextParam ? baseCount + 1 : baseCount;
  8089. }
  8090. parseCatchClauseParam() {
  8091. const param = super.parseCatchClauseParam();
  8092. const type = this.tsTryParseTypeAnnotation();
  8093. if (type) {
  8094. param.typeAnnotation = type;
  8095. this.resetEndLocation(param);
  8096. }
  8097. return param;
  8098. }
  8099. tsInAmbientContext(cb) {
  8100. const oldIsAmbientContext = this.state.isAmbientContext;
  8101. this.state.isAmbientContext = true;
  8102. try {
  8103. return cb();
  8104. } finally {
  8105. this.state.isAmbientContext = oldIsAmbientContext;
  8106. }
  8107. }
  8108. parseClass(node, ...args) {
  8109. const oldInAbstractClass = this.state.inAbstractClass;
  8110. this.state.inAbstractClass = !!node.abstract;
  8111. try {
  8112. return super.parseClass(node, ...args);
  8113. } finally {
  8114. this.state.inAbstractClass = oldInAbstractClass;
  8115. }
  8116. }
  8117. tsParseAbstractDeclaration(node) {
  8118. if (this.match(types$1._class)) {
  8119. node.abstract = true;
  8120. return this.parseClass(node, true, false);
  8121. } else if (this.isContextual("interface")) {
  8122. if (!this.hasFollowingLineBreak()) {
  8123. node.abstract = true;
  8124. this.raise(node.start, TSErrors.NonClassMethodPropertyHasAbstractModifer);
  8125. this.next();
  8126. return this.tsParseInterfaceDeclaration(node);
  8127. }
  8128. } else {
  8129. this.unexpected(null, types$1._class);
  8130. }
  8131. }
  8132. parseMethod(...args) {
  8133. const method = super.parseMethod(...args);
  8134. if (method.abstract) {
  8135. const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body;
  8136. if (hasBody) {
  8137. const {
  8138. key
  8139. } = method;
  8140. this.raise(method.start, TSErrors.AbstractMethodHasImplementation, key.type === "Identifier" && !method.computed ? key.name : `[${this.input.slice(key.start, key.end)}]`);
  8141. }
  8142. }
  8143. return method;
  8144. }
  8145. tsParseTypeParameterName() {
  8146. const typeName = this.parseIdentifier();
  8147. return typeName.name;
  8148. }
  8149. shouldParseAsAmbientContext() {
  8150. return !!this.getPluginOption("typescript", "dts");
  8151. }
  8152. parse() {
  8153. if (this.shouldParseAsAmbientContext()) {
  8154. this.state.isAmbientContext = true;
  8155. }
  8156. return super.parse();
  8157. }
  8158. getExpression() {
  8159. if (this.shouldParseAsAmbientContext()) {
  8160. this.state.isAmbientContext = true;
  8161. }
  8162. return super.getExpression();
  8163. }
  8164. });
  8165. types$1.placeholder = new TokenType("%%", {
  8166. startsExpr: true
  8167. });
  8168. const PlaceHolderErrors = makeErrorTemplates({
  8169. ClassNameIsRequired: "A class name is required."
  8170. }, ErrorCodes.SyntaxError);
  8171. var placeholders = (superClass => class extends superClass {
  8172. parsePlaceholder(expectedNode) {
  8173. if (this.match(types$1.placeholder)) {
  8174. const node = this.startNode();
  8175. this.next();
  8176. this.assertNoSpace("Unexpected space in placeholder.");
  8177. node.name = super.parseIdentifier(true);
  8178. this.assertNoSpace("Unexpected space in placeholder.");
  8179. this.expect(types$1.placeholder);
  8180. return this.finishPlaceholder(node, expectedNode);
  8181. }
  8182. }
  8183. finishPlaceholder(node, expectedNode) {
  8184. const isFinished = !!(node.expectedNode && node.type === "Placeholder");
  8185. node.expectedNode = expectedNode;
  8186. return isFinished ? node : this.finishNode(node, "Placeholder");
  8187. }
  8188. getTokenFromCode(code) {
  8189. if (code === 37 && this.input.charCodeAt(this.state.pos + 1) === 37) {
  8190. return this.finishOp(types$1.placeholder, 2);
  8191. }
  8192. return super.getTokenFromCode(...arguments);
  8193. }
  8194. parseExprAtom() {
  8195. return this.parsePlaceholder("Expression") || super.parseExprAtom(...arguments);
  8196. }
  8197. parseIdentifier() {
  8198. return this.parsePlaceholder("Identifier") || super.parseIdentifier(...arguments);
  8199. }
  8200. checkReservedWord(word) {
  8201. if (word !== undefined) super.checkReservedWord(...arguments);
  8202. }
  8203. parseBindingAtom() {
  8204. return this.parsePlaceholder("Pattern") || super.parseBindingAtom(...arguments);
  8205. }
  8206. checkLVal(expr) {
  8207. if (expr.type !== "Placeholder") super.checkLVal(...arguments);
  8208. }
  8209. toAssignable(node) {
  8210. if (node && node.type === "Placeholder" && node.expectedNode === "Expression") {
  8211. node.expectedNode = "Pattern";
  8212. return node;
  8213. }
  8214. return super.toAssignable(...arguments);
  8215. }
  8216. isLet(context) {
  8217. if (super.isLet(context)) {
  8218. return true;
  8219. }
  8220. if (!this.isContextual("let")) {
  8221. return false;
  8222. }
  8223. if (context) return false;
  8224. const nextToken = this.lookahead();
  8225. if (nextToken.type === types$1.placeholder) {
  8226. return true;
  8227. }
  8228. return false;
  8229. }
  8230. verifyBreakContinue(node) {
  8231. if (node.label && node.label.type === "Placeholder") return;
  8232. super.verifyBreakContinue(...arguments);
  8233. }
  8234. parseExpressionStatement(node, expr) {
  8235. if (expr.type !== "Placeholder" || expr.extra && expr.extra.parenthesized) {
  8236. return super.parseExpressionStatement(...arguments);
  8237. }
  8238. if (this.match(types$1.colon)) {
  8239. const stmt = node;
  8240. stmt.label = this.finishPlaceholder(expr, "Identifier");
  8241. this.next();
  8242. stmt.body = this.parseStatement("label");
  8243. return this.finishNode(stmt, "LabeledStatement");
  8244. }
  8245. this.semicolon();
  8246. node.name = expr.name;
  8247. return this.finishPlaceholder(node, "Statement");
  8248. }
  8249. parseBlock() {
  8250. return this.parsePlaceholder("BlockStatement") || super.parseBlock(...arguments);
  8251. }
  8252. parseFunctionId() {
  8253. return this.parsePlaceholder("Identifier") || super.parseFunctionId(...arguments);
  8254. }
  8255. parseClass(node, isStatement, optionalId) {
  8256. const type = isStatement ? "ClassDeclaration" : "ClassExpression";
  8257. this.next();
  8258. this.takeDecorators(node);
  8259. const oldStrict = this.state.strict;
  8260. const placeholder = this.parsePlaceholder("Identifier");
  8261. if (placeholder) {
  8262. if (this.match(types$1._extends) || this.match(types$1.placeholder) || this.match(types$1.braceL)) {
  8263. node.id = placeholder;
  8264. } else if (optionalId || !isStatement) {
  8265. node.id = null;
  8266. node.body = this.finishPlaceholder(placeholder, "ClassBody");
  8267. return this.finishNode(node, type);
  8268. } else {
  8269. this.unexpected(null, PlaceHolderErrors.ClassNameIsRequired);
  8270. }
  8271. } else {
  8272. this.parseClassId(node, isStatement, optionalId);
  8273. }
  8274. this.parseClassSuper(node);
  8275. node.body = this.parsePlaceholder("ClassBody") || this.parseClassBody(!!node.superClass, oldStrict);
  8276. return this.finishNode(node, type);
  8277. }
  8278. parseExport(node) {
  8279. const placeholder = this.parsePlaceholder("Identifier");
  8280. if (!placeholder) return super.parseExport(...arguments);
  8281. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  8282. node.specifiers = [];
  8283. node.source = null;
  8284. node.declaration = this.finishPlaceholder(placeholder, "Declaration");
  8285. return this.finishNode(node, "ExportNamedDeclaration");
  8286. }
  8287. this.expectPlugin("exportDefaultFrom");
  8288. const specifier = this.startNode();
  8289. specifier.exported = placeholder;
  8290. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  8291. return super.parseExport(node);
  8292. }
  8293. isExportDefaultSpecifier() {
  8294. if (this.match(types$1._default)) {
  8295. const next = this.nextTokenStart();
  8296. if (this.isUnparsedContextual(next, "from")) {
  8297. if (this.input.startsWith(types$1.placeholder.label, this.nextTokenStartSince(next + 4))) {
  8298. return true;
  8299. }
  8300. }
  8301. }
  8302. return super.isExportDefaultSpecifier();
  8303. }
  8304. maybeParseExportDefaultSpecifier(node) {
  8305. if (node.specifiers && node.specifiers.length > 0) {
  8306. return true;
  8307. }
  8308. return super.maybeParseExportDefaultSpecifier(...arguments);
  8309. }
  8310. checkExport(node) {
  8311. const {
  8312. specifiers
  8313. } = node;
  8314. if (specifiers != null && specifiers.length) {
  8315. node.specifiers = specifiers.filter(node => node.exported.type === "Placeholder");
  8316. }
  8317. super.checkExport(node);
  8318. node.specifiers = specifiers;
  8319. }
  8320. parseImport(node) {
  8321. const placeholder = this.parsePlaceholder("Identifier");
  8322. if (!placeholder) return super.parseImport(...arguments);
  8323. node.specifiers = [];
  8324. if (!this.isContextual("from") && !this.match(types$1.comma)) {
  8325. node.source = this.finishPlaceholder(placeholder, "StringLiteral");
  8326. this.semicolon();
  8327. return this.finishNode(node, "ImportDeclaration");
  8328. }
  8329. const specifier = this.startNodeAtNode(placeholder);
  8330. specifier.local = placeholder;
  8331. this.finishNode(specifier, "ImportDefaultSpecifier");
  8332. node.specifiers.push(specifier);
  8333. if (this.eat(types$1.comma)) {
  8334. const hasStarImport = this.maybeParseStarImportSpecifier(node);
  8335. if (!hasStarImport) this.parseNamedImportSpecifiers(node);
  8336. }
  8337. this.expectContextual("from");
  8338. node.source = this.parseImportSource();
  8339. this.semicolon();
  8340. return this.finishNode(node, "ImportDeclaration");
  8341. }
  8342. parseImportSource() {
  8343. return this.parsePlaceholder("StringLiteral") || super.parseImportSource(...arguments);
  8344. }
  8345. });
  8346. var v8intrinsic = (superClass => class extends superClass {
  8347. parseV8Intrinsic() {
  8348. if (this.match(types$1.modulo)) {
  8349. const v8IntrinsicStart = this.state.start;
  8350. const node = this.startNode();
  8351. this.eat(types$1.modulo);
  8352. if (this.match(types$1.name)) {
  8353. const name = this.parseIdentifierName(this.state.start);
  8354. const identifier = this.createIdentifier(node, name);
  8355. identifier.type = "V8IntrinsicIdentifier";
  8356. if (this.match(types$1.parenL)) {
  8357. return identifier;
  8358. }
  8359. }
  8360. this.unexpected(v8IntrinsicStart);
  8361. }
  8362. }
  8363. parseExprAtom() {
  8364. return this.parseV8Intrinsic() || super.parseExprAtom(...arguments);
  8365. }
  8366. });
  8367. function hasPlugin(plugins, name) {
  8368. return plugins.some(plugin => {
  8369. if (Array.isArray(plugin)) {
  8370. return plugin[0] === name;
  8371. } else {
  8372. return plugin === name;
  8373. }
  8374. });
  8375. }
  8376. function getPluginOption(plugins, name, option) {
  8377. const plugin = plugins.find(plugin => {
  8378. if (Array.isArray(plugin)) {
  8379. return plugin[0] === name;
  8380. } else {
  8381. return plugin === name;
  8382. }
  8383. });
  8384. if (plugin && Array.isArray(plugin)) {
  8385. return plugin[1][option];
  8386. }
  8387. return null;
  8388. }
  8389. const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
  8390. const TOPIC_TOKENS = ["%", "#"];
  8391. const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
  8392. function validatePlugins(plugins) {
  8393. if (hasPlugin(plugins, "decorators")) {
  8394. if (hasPlugin(plugins, "decorators-legacy")) {
  8395. throw new Error("Cannot use the decorators and decorators-legacy plugin together");
  8396. }
  8397. const decoratorsBeforeExport = getPluginOption(plugins, "decorators", "decoratorsBeforeExport");
  8398. if (decoratorsBeforeExport == null) {
  8399. throw new Error("The 'decorators' plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you are migrating from" + " Babylon/Babel 6 or want to use the old decorators proposal, you" + " should use the 'decorators-legacy' plugin instead of 'decorators'.");
  8400. } else if (typeof decoratorsBeforeExport !== "boolean") {
  8401. throw new Error("'decoratorsBeforeExport' must be a boolean.");
  8402. }
  8403. }
  8404. if (hasPlugin(plugins, "flow") && hasPlugin(plugins, "typescript")) {
  8405. throw new Error("Cannot combine flow and typescript plugins.");
  8406. }
  8407. if (hasPlugin(plugins, "placeholders") && hasPlugin(plugins, "v8intrinsic")) {
  8408. throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
  8409. }
  8410. if (hasPlugin(plugins, "pipelineOperator")) {
  8411. const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
  8412. if (!PIPELINE_PROPOSALS.includes(proposal)) {
  8413. const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
  8414. throw new Error(`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`);
  8415. }
  8416. const tupleSyntaxIsHash = hasPlugin(plugins, "recordAndTuple") && getPluginOption(plugins, "recordAndTuple", "syntaxType") === "hash";
  8417. if (proposal === "hack") {
  8418. if (hasPlugin(plugins, "placeholders")) {
  8419. throw new Error("Cannot combine placeholders plugin and Hack-style pipes.");
  8420. }
  8421. if (hasPlugin(plugins, "v8intrinsic")) {
  8422. throw new Error("Cannot combine v8intrinsic plugin and Hack-style pipes.");
  8423. }
  8424. const topicToken = getPluginOption(plugins, "pipelineOperator", "topicToken");
  8425. if (!TOPIC_TOKENS.includes(topicToken)) {
  8426. const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
  8427. throw new Error(`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`);
  8428. }
  8429. if (topicToken === "#" && tupleSyntaxIsHash) {
  8430. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  8431. }
  8432. } else if (proposal === "smart" && tupleSyntaxIsHash) {
  8433. throw new Error('Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.');
  8434. }
  8435. }
  8436. if (hasPlugin(plugins, "moduleAttributes")) {
  8437. {
  8438. if (hasPlugin(plugins, "importAssertions")) {
  8439. throw new Error("Cannot combine importAssertions and moduleAttributes plugins.");
  8440. }
  8441. const moduleAttributesVerionPluginOption = getPluginOption(plugins, "moduleAttributes", "version");
  8442. if (moduleAttributesVerionPluginOption !== "may-2020") {
  8443. throw new Error("The 'moduleAttributes' plugin requires a 'version' option," + " representing the last proposal update. Currently, the" + " only supported value is 'may-2020'.");
  8444. }
  8445. }
  8446. }
  8447. if (hasPlugin(plugins, "recordAndTuple") && !RECORD_AND_TUPLE_SYNTAX_TYPES.includes(getPluginOption(plugins, "recordAndTuple", "syntaxType"))) {
  8448. throw new Error("'recordAndTuple' requires 'syntaxType' option whose value should be one of: " + RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "));
  8449. }
  8450. if (hasPlugin(plugins, "asyncDoExpressions") && !hasPlugin(plugins, "doExpressions")) {
  8451. const error = new Error("'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.");
  8452. error.missingPlugins = "doExpressions";
  8453. throw error;
  8454. }
  8455. }
  8456. const mixinPlugins = {
  8457. estree,
  8458. jsx,
  8459. flow,
  8460. typescript,
  8461. v8intrinsic,
  8462. placeholders
  8463. };
  8464. const mixinPluginNames = Object.keys(mixinPlugins);
  8465. const defaultOptions = {
  8466. sourceType: "script",
  8467. sourceFilename: undefined,
  8468. startLine: 1,
  8469. allowAwaitOutsideFunction: false,
  8470. allowReturnOutsideFunction: false,
  8471. allowImportExportEverywhere: false,
  8472. allowSuperOutsideMethod: false,
  8473. allowUndeclaredExports: false,
  8474. plugins: [],
  8475. strictMode: null,
  8476. ranges: false,
  8477. tokens: false,
  8478. createParenthesizedExpressions: false,
  8479. errorRecovery: false,
  8480. attachComment: true
  8481. };
  8482. function getOptions(opts) {
  8483. const options = {};
  8484. for (const key of Object.keys(defaultOptions)) {
  8485. options[key] = opts && opts[key] != null ? opts[key] : defaultOptions[key];
  8486. }
  8487. return options;
  8488. }
  8489. const unwrapParenthesizedExpression = node => {
  8490. return node.type === "ParenthesizedExpression" ? unwrapParenthesizedExpression(node.expression) : node;
  8491. };
  8492. class LValParser extends NodeUtils {
  8493. toAssignable(node, isLHS = false) {
  8494. var _node$extra, _node$extra3;
  8495. let parenthesized = undefined;
  8496. if (node.type === "ParenthesizedExpression" || (_node$extra = node.extra) != null && _node$extra.parenthesized) {
  8497. parenthesized = unwrapParenthesizedExpression(node);
  8498. if (isLHS) {
  8499. if (parenthesized.type === "Identifier") {
  8500. this.expressionScope.recordParenthesizedIdentifierError(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8501. } else if (parenthesized.type !== "MemberExpression") {
  8502. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8503. }
  8504. } else {
  8505. this.raise(node.start, ErrorMessages.InvalidParenthesizedAssignment);
  8506. }
  8507. }
  8508. switch (node.type) {
  8509. case "Identifier":
  8510. case "ObjectPattern":
  8511. case "ArrayPattern":
  8512. case "AssignmentPattern":
  8513. case "RestElement":
  8514. break;
  8515. case "ObjectExpression":
  8516. node.type = "ObjectPattern";
  8517. for (let i = 0, length = node.properties.length, last = length - 1; i < length; i++) {
  8518. var _node$extra2;
  8519. const prop = node.properties[i];
  8520. const isLast = i === last;
  8521. this.toAssignableObjectExpressionProp(prop, isLast, isLHS);
  8522. if (isLast && prop.type === "RestElement" && (_node$extra2 = node.extra) != null && _node$extra2.trailingComma) {
  8523. this.raiseRestNotLast(node.extra.trailingComma);
  8524. }
  8525. }
  8526. break;
  8527. case "ObjectProperty":
  8528. this.toAssignable(node.value, isLHS);
  8529. break;
  8530. case "SpreadElement":
  8531. {
  8532. this.checkToRestConversion(node);
  8533. node.type = "RestElement";
  8534. const arg = node.argument;
  8535. this.toAssignable(arg, isLHS);
  8536. break;
  8537. }
  8538. case "ArrayExpression":
  8539. node.type = "ArrayPattern";
  8540. this.toAssignableList(node.elements, (_node$extra3 = node.extra) == null ? void 0 : _node$extra3.trailingComma, isLHS);
  8541. break;
  8542. case "AssignmentExpression":
  8543. if (node.operator !== "=") {
  8544. this.raise(node.left.end, ErrorMessages.MissingEqInAssignment);
  8545. }
  8546. node.type = "AssignmentPattern";
  8547. delete node.operator;
  8548. this.toAssignable(node.left, isLHS);
  8549. break;
  8550. case "ParenthesizedExpression":
  8551. this.toAssignable(parenthesized, isLHS);
  8552. break;
  8553. }
  8554. return node;
  8555. }
  8556. toAssignableObjectExpressionProp(prop, isLast, isLHS) {
  8557. if (prop.type === "ObjectMethod") {
  8558. const error = prop.kind === "get" || prop.kind === "set" ? ErrorMessages.PatternHasAccessor : ErrorMessages.PatternHasMethod;
  8559. this.raise(prop.key.start, error);
  8560. } else if (prop.type === "SpreadElement" && !isLast) {
  8561. this.raiseRestNotLast(prop.start);
  8562. } else {
  8563. this.toAssignable(prop, isLHS);
  8564. }
  8565. }
  8566. toAssignableList(exprList, trailingCommaPos, isLHS) {
  8567. let end = exprList.length;
  8568. if (end) {
  8569. const last = exprList[end - 1];
  8570. if ((last == null ? void 0 : last.type) === "RestElement") {
  8571. --end;
  8572. } else if ((last == null ? void 0 : last.type) === "SpreadElement") {
  8573. last.type = "RestElement";
  8574. let arg = last.argument;
  8575. this.toAssignable(arg, isLHS);
  8576. arg = unwrapParenthesizedExpression(arg);
  8577. if (arg.type !== "Identifier" && arg.type !== "MemberExpression" && arg.type !== "ArrayPattern" && arg.type !== "ObjectPattern") {
  8578. this.unexpected(arg.start);
  8579. }
  8580. if (trailingCommaPos) {
  8581. this.raiseTrailingCommaAfterRest(trailingCommaPos);
  8582. }
  8583. --end;
  8584. }
  8585. }
  8586. for (let i = 0; i < end; i++) {
  8587. const elt = exprList[i];
  8588. if (elt) {
  8589. this.toAssignable(elt, isLHS);
  8590. if (elt.type === "RestElement") {
  8591. this.raiseRestNotLast(elt.start);
  8592. }
  8593. }
  8594. }
  8595. return exprList;
  8596. }
  8597. isAssignable(node, isBinding) {
  8598. switch (node.type) {
  8599. case "Identifier":
  8600. case "ObjectPattern":
  8601. case "ArrayPattern":
  8602. case "AssignmentPattern":
  8603. case "RestElement":
  8604. return true;
  8605. case "ObjectExpression":
  8606. {
  8607. const last = node.properties.length - 1;
  8608. return node.properties.every((prop, i) => {
  8609. return prop.type !== "ObjectMethod" && (i === last || prop.type !== "SpreadElement") && this.isAssignable(prop);
  8610. });
  8611. }
  8612. case "ObjectProperty":
  8613. return this.isAssignable(node.value);
  8614. case "SpreadElement":
  8615. return this.isAssignable(node.argument);
  8616. case "ArrayExpression":
  8617. return node.elements.every(element => element === null || this.isAssignable(element));
  8618. case "AssignmentExpression":
  8619. return node.operator === "=";
  8620. case "ParenthesizedExpression":
  8621. return this.isAssignable(node.expression);
  8622. case "MemberExpression":
  8623. case "OptionalMemberExpression":
  8624. return !isBinding;
  8625. default:
  8626. return false;
  8627. }
  8628. }
  8629. toReferencedList(exprList, isParenthesizedExpr) {
  8630. return exprList;
  8631. }
  8632. toReferencedListDeep(exprList, isParenthesizedExpr) {
  8633. this.toReferencedList(exprList, isParenthesizedExpr);
  8634. for (const expr of exprList) {
  8635. if ((expr == null ? void 0 : expr.type) === "ArrayExpression") {
  8636. this.toReferencedListDeep(expr.elements);
  8637. }
  8638. }
  8639. }
  8640. parseSpread(refExpressionErrors, refNeedsArrowPos) {
  8641. const node = this.startNode();
  8642. this.next();
  8643. node.argument = this.parseMaybeAssignAllowIn(refExpressionErrors, undefined, refNeedsArrowPos);
  8644. return this.finishNode(node, "SpreadElement");
  8645. }
  8646. parseRestBinding() {
  8647. const node = this.startNode();
  8648. this.next();
  8649. node.argument = this.parseBindingAtom();
  8650. return this.finishNode(node, "RestElement");
  8651. }
  8652. parseBindingAtom() {
  8653. switch (this.state.type) {
  8654. case types$1.bracketL:
  8655. {
  8656. const node = this.startNode();
  8657. this.next();
  8658. node.elements = this.parseBindingList(types$1.bracketR, 93, true);
  8659. return this.finishNode(node, "ArrayPattern");
  8660. }
  8661. case types$1.braceL:
  8662. return this.parseObjectLike(types$1.braceR, true);
  8663. }
  8664. return this.parseIdentifier();
  8665. }
  8666. parseBindingList(close, closeCharCode, allowEmpty, allowModifiers) {
  8667. const elts = [];
  8668. let first = true;
  8669. while (!this.eat(close)) {
  8670. if (first) {
  8671. first = false;
  8672. } else {
  8673. this.expect(types$1.comma);
  8674. }
  8675. if (allowEmpty && this.match(types$1.comma)) {
  8676. elts.push(null);
  8677. } else if (this.eat(close)) {
  8678. break;
  8679. } else if (this.match(types$1.ellipsis)) {
  8680. elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
  8681. this.checkCommaAfterRest(closeCharCode);
  8682. this.expect(close);
  8683. break;
  8684. } else {
  8685. const decorators = [];
  8686. if (this.match(types$1.at) && this.hasPlugin("decorators")) {
  8687. this.raise(this.state.start, ErrorMessages.UnsupportedParameterDecorator);
  8688. }
  8689. while (this.match(types$1.at)) {
  8690. decorators.push(this.parseDecorator());
  8691. }
  8692. elts.push(this.parseAssignableListItem(allowModifiers, decorators));
  8693. }
  8694. }
  8695. return elts;
  8696. }
  8697. parseAssignableListItem(allowModifiers, decorators) {
  8698. const left = this.parseMaybeDefault();
  8699. this.parseAssignableListItemTypes(left);
  8700. const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
  8701. if (decorators.length) {
  8702. left.decorators = decorators;
  8703. }
  8704. return elt;
  8705. }
  8706. parseAssignableListItemTypes(param) {
  8707. return param;
  8708. }
  8709. parseMaybeDefault(startPos, startLoc, left) {
  8710. var _startLoc, _startPos, _left;
  8711. startLoc = (_startLoc = startLoc) != null ? _startLoc : this.state.startLoc;
  8712. startPos = (_startPos = startPos) != null ? _startPos : this.state.start;
  8713. left = (_left = left) != null ? _left : this.parseBindingAtom();
  8714. if (!this.eat(types$1.eq)) return left;
  8715. const node = this.startNodeAt(startPos, startLoc);
  8716. node.left = left;
  8717. node.right = this.parseMaybeAssignAllowIn();
  8718. return this.finishNode(node, "AssignmentPattern");
  8719. }
  8720. checkLVal(expr, contextDescription, bindingType = BIND_NONE, checkClashes, disallowLetBinding, strictModeChanged = false) {
  8721. switch (expr.type) {
  8722. case "Identifier":
  8723. {
  8724. const {
  8725. name
  8726. } = expr;
  8727. if (this.state.strict && (strictModeChanged ? isStrictBindReservedWord(name, this.inModule) : isStrictBindOnlyReservedWord(name))) {
  8728. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.StrictEvalArguments : ErrorMessages.StrictEvalArgumentsBinding, name);
  8729. }
  8730. if (checkClashes) {
  8731. if (checkClashes.has(name)) {
  8732. this.raise(expr.start, ErrorMessages.ParamDupe);
  8733. } else {
  8734. checkClashes.add(name);
  8735. }
  8736. }
  8737. if (disallowLetBinding && name === "let") {
  8738. this.raise(expr.start, ErrorMessages.LetInLexicalBinding);
  8739. }
  8740. if (!(bindingType & BIND_NONE)) {
  8741. this.scope.declareName(name, bindingType, expr.start);
  8742. }
  8743. break;
  8744. }
  8745. case "MemberExpression":
  8746. if (bindingType !== BIND_NONE) {
  8747. this.raise(expr.start, ErrorMessages.InvalidPropertyBindingPattern);
  8748. }
  8749. break;
  8750. case "ObjectPattern":
  8751. for (let prop of expr.properties) {
  8752. if (this.isObjectProperty(prop)) prop = prop.value;else if (this.isObjectMethod(prop)) continue;
  8753. this.checkLVal(prop, "object destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8754. }
  8755. break;
  8756. case "ArrayPattern":
  8757. for (const elem of expr.elements) {
  8758. if (elem) {
  8759. this.checkLVal(elem, "array destructuring pattern", bindingType, checkClashes, disallowLetBinding);
  8760. }
  8761. }
  8762. break;
  8763. case "AssignmentPattern":
  8764. this.checkLVal(expr.left, "assignment pattern", bindingType, checkClashes);
  8765. break;
  8766. case "RestElement":
  8767. this.checkLVal(expr.argument, "rest element", bindingType, checkClashes);
  8768. break;
  8769. case "ParenthesizedExpression":
  8770. this.checkLVal(expr.expression, "parenthesized expression", bindingType, checkClashes);
  8771. break;
  8772. default:
  8773. {
  8774. this.raise(expr.start, bindingType === BIND_NONE ? ErrorMessages.InvalidLhs : ErrorMessages.InvalidLhsBinding, contextDescription);
  8775. }
  8776. }
  8777. }
  8778. checkToRestConversion(node) {
  8779. if (node.argument.type !== "Identifier" && node.argument.type !== "MemberExpression") {
  8780. this.raise(node.argument.start, ErrorMessages.InvalidRestAssignmentPattern);
  8781. }
  8782. }
  8783. checkCommaAfterRest(close) {
  8784. if (this.match(types$1.comma)) {
  8785. if (this.lookaheadCharCode() === close) {
  8786. this.raiseTrailingCommaAfterRest(this.state.start);
  8787. } else {
  8788. this.raiseRestNotLast(this.state.start);
  8789. }
  8790. }
  8791. }
  8792. raiseRestNotLast(pos) {
  8793. throw this.raise(pos, ErrorMessages.ElementAfterRest);
  8794. }
  8795. raiseTrailingCommaAfterRest(pos) {
  8796. this.raise(pos, ErrorMessages.RestTrailingComma);
  8797. }
  8798. }
  8799. class ExpressionParser extends LValParser {
  8800. checkProto(prop, isRecord, protoRef, refExpressionErrors) {
  8801. if (prop.type === "SpreadElement" || this.isObjectMethod(prop) || prop.computed || prop.shorthand) {
  8802. return;
  8803. }
  8804. const key = prop.key;
  8805. const name = key.type === "Identifier" ? key.name : key.value;
  8806. if (name === "__proto__") {
  8807. if (isRecord) {
  8808. this.raise(key.start, ErrorMessages.RecordNoProto);
  8809. return;
  8810. }
  8811. if (protoRef.used) {
  8812. if (refExpressionErrors) {
  8813. if (refExpressionErrors.doubleProto === -1) {
  8814. refExpressionErrors.doubleProto = key.start;
  8815. }
  8816. } else {
  8817. this.raise(key.start, ErrorMessages.DuplicateProto);
  8818. }
  8819. }
  8820. protoRef.used = true;
  8821. }
  8822. }
  8823. shouldExitDescending(expr, potentialArrowAt) {
  8824. return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
  8825. }
  8826. getExpression() {
  8827. this.enterInitialScopes();
  8828. this.nextToken();
  8829. const expr = this.parseExpression();
  8830. if (!this.match(types$1.eof)) {
  8831. this.unexpected();
  8832. }
  8833. this.finalizeRemainingComments();
  8834. expr.comments = this.state.comments;
  8835. expr.errors = this.state.errors;
  8836. if (this.options.tokens) {
  8837. expr.tokens = this.tokens;
  8838. }
  8839. return expr;
  8840. }
  8841. parseExpression(disallowIn, refExpressionErrors) {
  8842. if (disallowIn) {
  8843. return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8844. }
  8845. return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
  8846. }
  8847. parseExpressionBase(refExpressionErrors) {
  8848. const startPos = this.state.start;
  8849. const startLoc = this.state.startLoc;
  8850. const expr = this.parseMaybeAssign(refExpressionErrors);
  8851. if (this.match(types$1.comma)) {
  8852. const node = this.startNodeAt(startPos, startLoc);
  8853. node.expressions = [expr];
  8854. while (this.eat(types$1.comma)) {
  8855. node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
  8856. }
  8857. this.toReferencedList(node.expressions);
  8858. return this.finishNode(node, "SequenceExpression");
  8859. }
  8860. return expr;
  8861. }
  8862. parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse) {
  8863. return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  8864. }
  8865. parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse) {
  8866. return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse));
  8867. }
  8868. setOptionalParametersError(refExpressionErrors, resultError) {
  8869. var _resultError$pos;
  8870. refExpressionErrors.optionalParameters = (_resultError$pos = resultError == null ? void 0 : resultError.pos) != null ? _resultError$pos : this.state.start;
  8871. }
  8872. parseMaybeAssign(refExpressionErrors, afterLeftParse) {
  8873. const startPos = this.state.start;
  8874. const startLoc = this.state.startLoc;
  8875. if (this.isContextual("yield")) {
  8876. if (this.prodParam.hasYield) {
  8877. let left = this.parseYield();
  8878. if (afterLeftParse) {
  8879. left = afterLeftParse.call(this, left, startPos, startLoc);
  8880. }
  8881. return left;
  8882. }
  8883. }
  8884. let ownExpressionErrors;
  8885. if (refExpressionErrors) {
  8886. ownExpressionErrors = false;
  8887. } else {
  8888. refExpressionErrors = new ExpressionErrors();
  8889. ownExpressionErrors = true;
  8890. }
  8891. if (this.match(types$1.parenL) || this.match(types$1.name)) {
  8892. this.state.potentialArrowAt = this.state.start;
  8893. }
  8894. let left = this.parseMaybeConditional(refExpressionErrors);
  8895. if (afterLeftParse) {
  8896. left = afterLeftParse.call(this, left, startPos, startLoc);
  8897. }
  8898. if (this.state.type.isAssign) {
  8899. const node = this.startNodeAt(startPos, startLoc);
  8900. const operator = this.state.value;
  8901. node.operator = operator;
  8902. const leftIsHackPipeExpression = left.type === "BinaryExpression" && left.operator === "|>" && this.getPluginOption("pipelineOperator", "proposal") === "hack";
  8903. if (leftIsHackPipeExpression) {
  8904. throw this.raise(this.state.start, ErrorMessages.PipeBodyIsTighter, operator);
  8905. }
  8906. if (this.match(types$1.eq)) {
  8907. node.left = this.toAssignable(left, true);
  8908. refExpressionErrors.doubleProto = -1;
  8909. } else {
  8910. node.left = left;
  8911. }
  8912. if (refExpressionErrors.shorthandAssign >= node.left.start) {
  8913. refExpressionErrors.shorthandAssign = -1;
  8914. }
  8915. this.checkLVal(left, "assignment expression");
  8916. this.next();
  8917. node.right = this.parseMaybeAssign();
  8918. return this.finishNode(node, "AssignmentExpression");
  8919. } else if (ownExpressionErrors) {
  8920. this.checkExpressionErrors(refExpressionErrors, true);
  8921. }
  8922. return left;
  8923. }
  8924. parseMaybeConditional(refExpressionErrors) {
  8925. const startPos = this.state.start;
  8926. const startLoc = this.state.startLoc;
  8927. const potentialArrowAt = this.state.potentialArrowAt;
  8928. const expr = this.parseExprOps(refExpressionErrors);
  8929. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8930. return expr;
  8931. }
  8932. return this.parseConditional(expr, startPos, startLoc, refExpressionErrors);
  8933. }
  8934. parseConditional(expr, startPos, startLoc, refExpressionErrors) {
  8935. if (this.eat(types$1.question)) {
  8936. const node = this.startNodeAt(startPos, startLoc);
  8937. node.test = expr;
  8938. node.consequent = this.parseMaybeAssignAllowIn();
  8939. this.expect(types$1.colon);
  8940. node.alternate = this.parseMaybeAssign();
  8941. return this.finishNode(node, "ConditionalExpression");
  8942. }
  8943. return expr;
  8944. }
  8945. parseMaybeUnaryOrPrivate(refExpressionErrors) {
  8946. return this.match(types$1.privateName) ? this.parsePrivateName() : this.parseMaybeUnary(refExpressionErrors);
  8947. }
  8948. parseExprOps(refExpressionErrors) {
  8949. const startPos = this.state.start;
  8950. const startLoc = this.state.startLoc;
  8951. const potentialArrowAt = this.state.potentialArrowAt;
  8952. const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors);
  8953. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  8954. return expr;
  8955. }
  8956. return this.parseExprOp(expr, startPos, startLoc, -1);
  8957. }
  8958. parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
  8959. if (this.isPrivateName(left)) {
  8960. const value = this.getPrivateNameSV(left);
  8961. const {
  8962. start
  8963. } = left;
  8964. if (minPrec >= types$1._in.binop || !this.prodParam.hasIn || !this.match(types$1._in)) {
  8965. this.raise(start, ErrorMessages.PrivateInExpectedIn, value);
  8966. }
  8967. this.classScope.usePrivateName(value, start);
  8968. }
  8969. let prec = this.state.type.binop;
  8970. if (prec != null && (this.prodParam.hasIn || !this.match(types$1._in))) {
  8971. if (prec > minPrec) {
  8972. const op = this.state.type;
  8973. if (op === types$1.pipeline) {
  8974. this.expectPlugin("pipelineOperator");
  8975. if (this.state.inFSharpPipelineDirectBody) {
  8976. return left;
  8977. }
  8978. this.checkPipelineAtInfixOperator(left, leftStartPos);
  8979. }
  8980. const node = this.startNodeAt(leftStartPos, leftStartLoc);
  8981. node.left = left;
  8982. node.operator = this.state.value;
  8983. const logical = op === types$1.logicalOR || op === types$1.logicalAND;
  8984. const coalesce = op === types$1.nullishCoalescing;
  8985. if (coalesce) {
  8986. prec = types$1.logicalAND.binop;
  8987. }
  8988. this.next();
  8989. if (op === types$1.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
  8990. if (this.match(types$1.name) && this.state.value === "await" && this.prodParam.hasAwait) {
  8991. throw this.raise(this.state.start, ErrorMessages.UnexpectedAwaitAfterPipelineBody);
  8992. }
  8993. }
  8994. node.right = this.parseExprOpRightExpr(op, prec);
  8995. this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
  8996. const nextOp = this.state.type;
  8997. if (coalesce && (nextOp === types$1.logicalOR || nextOp === types$1.logicalAND) || logical && nextOp === types$1.nullishCoalescing) {
  8998. throw this.raise(this.state.start, ErrorMessages.MixingCoalesceWithLogical);
  8999. }
  9000. return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
  9001. }
  9002. }
  9003. return left;
  9004. }
  9005. parseExprOpRightExpr(op, prec) {
  9006. const startPos = this.state.start;
  9007. const startLoc = this.state.startLoc;
  9008. switch (op) {
  9009. case types$1.pipeline:
  9010. switch (this.getPluginOption("pipelineOperator", "proposal")) {
  9011. case "hack":
  9012. return this.withTopicBindingContext(() => {
  9013. const bodyExpr = this.parseHackPipeBody(op, prec);
  9014. this.checkHackPipeBodyEarlyErrors(startPos);
  9015. return bodyExpr;
  9016. });
  9017. case "smart":
  9018. return this.withTopicBindingContext(() => {
  9019. const childExpr = this.parseHackPipeBody(op, prec);
  9020. return this.parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc);
  9021. });
  9022. case "fsharp":
  9023. return this.withSoloAwaitPermittingContext(() => {
  9024. return this.parseFSharpPipelineBody(prec);
  9025. });
  9026. }
  9027. default:
  9028. return this.parseExprOpBaseRightExpr(op, prec);
  9029. }
  9030. }
  9031. parseExprOpBaseRightExpr(op, prec) {
  9032. const startPos = this.state.start;
  9033. const startLoc = this.state.startLoc;
  9034. return this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec);
  9035. }
  9036. parseHackPipeBody(op, prec) {
  9037. const bodyIsInGeneratorContext = this.prodParam.hasYield;
  9038. const bodyIsYieldExpression = bodyIsInGeneratorContext && this.isContextual("yield");
  9039. if (bodyIsYieldExpression) {
  9040. throw this.raise(this.state.start, ErrorMessages.PipeBodyIsTighter, this.state.value);
  9041. } else {
  9042. return this.parseExprOpBaseRightExpr(op, prec);
  9043. }
  9044. }
  9045. checkExponentialAfterUnary(node) {
  9046. if (this.match(types$1.exponent)) {
  9047. this.raise(node.argument.start, ErrorMessages.UnexpectedTokenUnaryExponentiation);
  9048. }
  9049. }
  9050. parseMaybeUnary(refExpressionErrors, sawUnary) {
  9051. const startPos = this.state.start;
  9052. const startLoc = this.state.startLoc;
  9053. const isAwait = this.isContextual("await");
  9054. if (isAwait && this.isAwaitAllowed()) {
  9055. this.next();
  9056. const expr = this.parseAwait(startPos, startLoc);
  9057. if (!sawUnary) this.checkExponentialAfterUnary(expr);
  9058. return expr;
  9059. }
  9060. const update = this.match(types$1.incDec);
  9061. const node = this.startNode();
  9062. if (this.state.type.prefix) {
  9063. node.operator = this.state.value;
  9064. node.prefix = true;
  9065. if (this.match(types$1._throw)) {
  9066. this.expectPlugin("throwExpressions");
  9067. }
  9068. const isDelete = this.match(types$1._delete);
  9069. this.next();
  9070. node.argument = this.parseMaybeUnary(null, true);
  9071. this.checkExpressionErrors(refExpressionErrors, true);
  9072. if (this.state.strict && isDelete) {
  9073. const arg = node.argument;
  9074. if (arg.type === "Identifier") {
  9075. this.raise(node.start, ErrorMessages.StrictDelete);
  9076. } else if (this.hasPropertyAsPrivateName(arg)) {
  9077. this.raise(node.start, ErrorMessages.DeletePrivateField);
  9078. }
  9079. }
  9080. if (!update) {
  9081. if (!sawUnary) this.checkExponentialAfterUnary(node);
  9082. return this.finishNode(node, "UnaryExpression");
  9083. }
  9084. }
  9085. const expr = this.parseUpdate(node, update, refExpressionErrors);
  9086. if (isAwait) {
  9087. const startsExpr = this.hasPlugin("v8intrinsic") ? this.state.type.startsExpr : this.state.type.startsExpr && !this.match(types$1.modulo);
  9088. if (startsExpr && !this.isAmbiguousAwait()) {
  9089. this.raiseOverwrite(startPos, ErrorMessages.AwaitNotInAsyncContext);
  9090. return this.parseAwait(startPos, startLoc);
  9091. }
  9092. }
  9093. return expr;
  9094. }
  9095. parseUpdate(node, update, refExpressionErrors) {
  9096. if (update) {
  9097. this.checkLVal(node.argument, "prefix operation");
  9098. return this.finishNode(node, "UpdateExpression");
  9099. }
  9100. const startPos = this.state.start;
  9101. const startLoc = this.state.startLoc;
  9102. let expr = this.parseExprSubscripts(refExpressionErrors);
  9103. if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
  9104. while (this.state.type.postfix && !this.canInsertSemicolon()) {
  9105. const node = this.startNodeAt(startPos, startLoc);
  9106. node.operator = this.state.value;
  9107. node.prefix = false;
  9108. node.argument = expr;
  9109. this.checkLVal(expr, "postfix operation");
  9110. this.next();
  9111. expr = this.finishNode(node, "UpdateExpression");
  9112. }
  9113. return expr;
  9114. }
  9115. parseExprSubscripts(refExpressionErrors) {
  9116. const startPos = this.state.start;
  9117. const startLoc = this.state.startLoc;
  9118. const potentialArrowAt = this.state.potentialArrowAt;
  9119. const expr = this.parseExprAtom(refExpressionErrors);
  9120. if (this.shouldExitDescending(expr, potentialArrowAt)) {
  9121. return expr;
  9122. }
  9123. return this.parseSubscripts(expr, startPos, startLoc);
  9124. }
  9125. parseSubscripts(base, startPos, startLoc, noCalls) {
  9126. const state = {
  9127. optionalChainMember: false,
  9128. maybeAsyncArrow: this.atPossibleAsyncArrow(base),
  9129. stop: false
  9130. };
  9131. do {
  9132. base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
  9133. state.maybeAsyncArrow = false;
  9134. } while (!state.stop);
  9135. return base;
  9136. }
  9137. parseSubscript(base, startPos, startLoc, noCalls, state) {
  9138. if (!noCalls && this.eat(types$1.doubleColon)) {
  9139. return this.parseBind(base, startPos, startLoc, noCalls, state);
  9140. } else if (this.match(types$1.backQuote)) {
  9141. return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
  9142. }
  9143. let optional = false;
  9144. if (this.match(types$1.questionDot)) {
  9145. if (noCalls && this.lookaheadCharCode() === 40) {
  9146. state.stop = true;
  9147. return base;
  9148. }
  9149. state.optionalChainMember = optional = true;
  9150. this.next();
  9151. }
  9152. if (!noCalls && this.match(types$1.parenL)) {
  9153. return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
  9154. } else {
  9155. const computed = this.eat(types$1.bracketL);
  9156. if (computed || optional || this.eat(types$1.dot)) {
  9157. return this.parseMember(base, startPos, startLoc, state, computed, optional);
  9158. } else {
  9159. state.stop = true;
  9160. return base;
  9161. }
  9162. }
  9163. }
  9164. parseMember(base, startPos, startLoc, state, computed, optional) {
  9165. const node = this.startNodeAt(startPos, startLoc);
  9166. node.object = base;
  9167. node.computed = computed;
  9168. const privateName = !computed && this.match(types$1.privateName) && this.state.value;
  9169. const property = computed ? this.parseExpression() : privateName ? this.parsePrivateName() : this.parseIdentifier(true);
  9170. if (privateName !== false) {
  9171. if (node.object.type === "Super") {
  9172. this.raise(startPos, ErrorMessages.SuperPrivateField);
  9173. }
  9174. this.classScope.usePrivateName(privateName, property.start);
  9175. }
  9176. node.property = property;
  9177. if (computed) {
  9178. this.expect(types$1.bracketR);
  9179. }
  9180. if (state.optionalChainMember) {
  9181. node.optional = optional;
  9182. return this.finishNode(node, "OptionalMemberExpression");
  9183. } else {
  9184. return this.finishNode(node, "MemberExpression");
  9185. }
  9186. }
  9187. parseBind(base, startPos, startLoc, noCalls, state) {
  9188. const node = this.startNodeAt(startPos, startLoc);
  9189. node.object = base;
  9190. node.callee = this.parseNoCallExpr();
  9191. state.stop = true;
  9192. return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
  9193. }
  9194. parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
  9195. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9196. let refExpressionErrors = null;
  9197. this.state.maybeInArrowParameters = true;
  9198. this.next();
  9199. let node = this.startNodeAt(startPos, startLoc);
  9200. node.callee = base;
  9201. if (state.maybeAsyncArrow) {
  9202. this.expressionScope.enter(newAsyncArrowScope());
  9203. refExpressionErrors = new ExpressionErrors();
  9204. }
  9205. if (state.optionalChainMember) {
  9206. node.optional = optional;
  9207. }
  9208. if (optional) {
  9209. node.arguments = this.parseCallExpressionArguments(types$1.parenR);
  9210. } else {
  9211. node.arguments = this.parseCallExpressionArguments(types$1.parenR, base.type === "Import", base.type !== "Super", node, refExpressionErrors);
  9212. }
  9213. this.finishCallExpression(node, state.optionalChainMember);
  9214. if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
  9215. state.stop = true;
  9216. this.expressionScope.validateAsPattern();
  9217. this.expressionScope.exit();
  9218. node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
  9219. } else {
  9220. if (state.maybeAsyncArrow) {
  9221. this.checkExpressionErrors(refExpressionErrors, true);
  9222. this.expressionScope.exit();
  9223. }
  9224. this.toReferencedArguments(node);
  9225. }
  9226. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9227. return node;
  9228. }
  9229. toReferencedArguments(node, isParenthesizedExpr) {
  9230. this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
  9231. }
  9232. parseTaggedTemplateExpression(base, startPos, startLoc, state) {
  9233. const node = this.startNodeAt(startPos, startLoc);
  9234. node.tag = base;
  9235. node.quasi = this.parseTemplate(true);
  9236. if (state.optionalChainMember) {
  9237. this.raise(startPos, ErrorMessages.OptionalChainingNoTemplate);
  9238. }
  9239. return this.finishNode(node, "TaggedTemplateExpression");
  9240. }
  9241. atPossibleAsyncArrow(base) {
  9242. return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
  9243. }
  9244. finishCallExpression(node, optional) {
  9245. if (node.callee.type === "Import") {
  9246. if (node.arguments.length === 2) {
  9247. {
  9248. if (!this.hasPlugin("moduleAttributes")) {
  9249. this.expectPlugin("importAssertions");
  9250. }
  9251. }
  9252. }
  9253. if (node.arguments.length === 0 || node.arguments.length > 2) {
  9254. this.raise(node.start, ErrorMessages.ImportCallArity, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
  9255. } else {
  9256. for (const arg of node.arguments) {
  9257. if (arg.type === "SpreadElement") {
  9258. this.raise(arg.start, ErrorMessages.ImportCallSpreadArgument);
  9259. }
  9260. }
  9261. }
  9262. }
  9263. return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
  9264. }
  9265. parseCallExpressionArguments(close, dynamicImport, allowPlaceholder, nodeForExtra, refExpressionErrors) {
  9266. const elts = [];
  9267. let first = true;
  9268. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9269. this.state.inFSharpPipelineDirectBody = false;
  9270. while (!this.eat(close)) {
  9271. if (first) {
  9272. first = false;
  9273. } else {
  9274. this.expect(types$1.comma);
  9275. if (this.match(close)) {
  9276. if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
  9277. this.raise(this.state.lastTokStart, ErrorMessages.ImportCallArgumentTrailingComma);
  9278. }
  9279. if (nodeForExtra) {
  9280. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  9281. }
  9282. this.next();
  9283. break;
  9284. }
  9285. }
  9286. elts.push(this.parseExprListItem(false, refExpressionErrors, allowPlaceholder));
  9287. }
  9288. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9289. return elts;
  9290. }
  9291. shouldParseAsyncArrow() {
  9292. return this.match(types$1.arrow) && !this.canInsertSemicolon();
  9293. }
  9294. parseAsyncArrowFromCallExpression(node, call) {
  9295. var _call$extra;
  9296. this.resetPreviousNodeTrailingComments(call);
  9297. this.expect(types$1.arrow);
  9298. this.parseArrowExpression(node, call.arguments, true, (_call$extra = call.extra) == null ? void 0 : _call$extra.trailingComma);
  9299. setInnerComments(node, call.innerComments);
  9300. setInnerComments(node, call.callee.trailingComments);
  9301. return node;
  9302. }
  9303. parseNoCallExpr() {
  9304. const startPos = this.state.start;
  9305. const startLoc = this.state.startLoc;
  9306. return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
  9307. }
  9308. parseExprAtom(refExpressionErrors) {
  9309. let node;
  9310. switch (this.state.type) {
  9311. case types$1._super:
  9312. return this.parseSuper();
  9313. case types$1._import:
  9314. node = this.startNode();
  9315. this.next();
  9316. if (this.match(types$1.dot)) {
  9317. return this.parseImportMetaProperty(node);
  9318. }
  9319. if (!this.match(types$1.parenL)) {
  9320. this.raise(this.state.lastTokStart, ErrorMessages.UnsupportedImport);
  9321. }
  9322. return this.finishNode(node, "Import");
  9323. case types$1._this:
  9324. node = this.startNode();
  9325. this.next();
  9326. return this.finishNode(node, "ThisExpression");
  9327. case types$1.name:
  9328. {
  9329. if (this.isContextual("module") && this.lookaheadCharCode() === 123 && !this.hasFollowingLineBreak()) {
  9330. return this.parseModuleExpression();
  9331. }
  9332. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9333. const containsEsc = this.state.containsEsc;
  9334. const id = this.parseIdentifier();
  9335. if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
  9336. if (this.match(types$1._function)) {
  9337. this.resetPreviousNodeTrailingComments(id);
  9338. this.next();
  9339. return this.parseFunction(this.startNodeAtNode(id), undefined, true);
  9340. } else if (this.match(types$1.name)) {
  9341. if (this.lookaheadCharCode() === 61) {
  9342. return this.parseAsyncArrowUnaryFunction(this.startNodeAtNode(id));
  9343. } else {
  9344. return id;
  9345. }
  9346. } else if (this.match(types$1._do)) {
  9347. this.resetPreviousNodeTrailingComments(id);
  9348. return this.parseDo(this.startNodeAtNode(id), true);
  9349. }
  9350. }
  9351. if (canBeArrow && this.match(types$1.arrow) && !this.canInsertSemicolon()) {
  9352. this.next();
  9353. return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
  9354. }
  9355. return id;
  9356. }
  9357. case types$1._do:
  9358. {
  9359. return this.parseDo(this.startNode(), false);
  9360. }
  9361. case types$1.slash:
  9362. case types$1.slashAssign:
  9363. {
  9364. this.readRegexp();
  9365. return this.parseRegExpLiteral(this.state.value);
  9366. }
  9367. case types$1.num:
  9368. return this.parseNumericLiteral(this.state.value);
  9369. case types$1.bigint:
  9370. return this.parseBigIntLiteral(this.state.value);
  9371. case types$1.decimal:
  9372. return this.parseDecimalLiteral(this.state.value);
  9373. case types$1.string:
  9374. return this.parseStringLiteral(this.state.value);
  9375. case types$1._null:
  9376. return this.parseNullLiteral();
  9377. case types$1._true:
  9378. return this.parseBooleanLiteral(true);
  9379. case types$1._false:
  9380. return this.parseBooleanLiteral(false);
  9381. case types$1.parenL:
  9382. {
  9383. const canBeArrow = this.state.potentialArrowAt === this.state.start;
  9384. return this.parseParenAndDistinguishExpression(canBeArrow);
  9385. }
  9386. case types$1.bracketBarL:
  9387. case types$1.bracketHashL:
  9388. {
  9389. return this.parseArrayLike(this.state.type === types$1.bracketBarL ? types$1.bracketBarR : types$1.bracketR, false, true, refExpressionErrors);
  9390. }
  9391. case types$1.bracketL:
  9392. {
  9393. return this.parseArrayLike(types$1.bracketR, true, false, refExpressionErrors);
  9394. }
  9395. case types$1.braceBarL:
  9396. case types$1.braceHashL:
  9397. {
  9398. return this.parseObjectLike(this.state.type === types$1.braceBarL ? types$1.braceBarR : types$1.braceR, false, true, refExpressionErrors);
  9399. }
  9400. case types$1.braceL:
  9401. {
  9402. return this.parseObjectLike(types$1.braceR, false, false, refExpressionErrors);
  9403. }
  9404. case types$1._function:
  9405. return this.parseFunctionOrFunctionSent();
  9406. case types$1.at:
  9407. this.parseDecorators();
  9408. case types$1._class:
  9409. node = this.startNode();
  9410. this.takeDecorators(node);
  9411. return this.parseClass(node, false);
  9412. case types$1._new:
  9413. return this.parseNewOrNewTarget();
  9414. case types$1.backQuote:
  9415. return this.parseTemplate(false);
  9416. case types$1.doubleColon:
  9417. {
  9418. node = this.startNode();
  9419. this.next();
  9420. node.object = null;
  9421. const callee = node.callee = this.parseNoCallExpr();
  9422. if (callee.type === "MemberExpression") {
  9423. return this.finishNode(node, "BindExpression");
  9424. } else {
  9425. throw this.raise(callee.start, ErrorMessages.UnsupportedBind);
  9426. }
  9427. }
  9428. case types$1.privateName:
  9429. {
  9430. this.raise(this.state.start, ErrorMessages.PrivateInExpectedIn, this.state.value);
  9431. return this.parsePrivateName();
  9432. }
  9433. case types$1.moduloAssign:
  9434. if (this.getPluginOption("pipelineOperator", "proposal") === "hack" && this.getPluginOption("pipelineOperator", "topicToken") === "%") {
  9435. this.state.value = "%";
  9436. this.state.type = types$1.modulo;
  9437. this.state.pos--;
  9438. this.state.end--;
  9439. this.state.endLoc.column--;
  9440. } else {
  9441. throw this.unexpected();
  9442. }
  9443. case types$1.modulo:
  9444. case types$1.hash:
  9445. {
  9446. const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
  9447. if (pipeProposal) {
  9448. node = this.startNode();
  9449. const start = this.state.start;
  9450. const tokenType = this.state.type;
  9451. this.next();
  9452. return this.finishTopicReference(node, start, pipeProposal, tokenType);
  9453. }
  9454. }
  9455. case types$1.relational:
  9456. {
  9457. if (this.state.value === "<") {
  9458. const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
  9459. if (isIdentifierStart(lookaheadCh) || lookaheadCh === 62) {
  9460. this.expectOnePlugin(["jsx", "flow", "typescript"]);
  9461. }
  9462. }
  9463. }
  9464. default:
  9465. throw this.unexpected();
  9466. }
  9467. }
  9468. finishTopicReference(node, start, pipeProposal, tokenType) {
  9469. if (this.testTopicReferenceConfiguration(pipeProposal, start, tokenType)) {
  9470. let nodeType;
  9471. if (pipeProposal === "smart") {
  9472. nodeType = "PipelinePrimaryTopicReference";
  9473. } else {
  9474. nodeType = "TopicReference";
  9475. }
  9476. if (!this.topicReferenceIsAllowedInCurrentContext()) {
  9477. if (pipeProposal === "smart") {
  9478. this.raise(start, ErrorMessages.PrimaryTopicNotAllowed);
  9479. } else {
  9480. this.raise(start, ErrorMessages.PipeTopicUnbound);
  9481. }
  9482. }
  9483. this.registerTopicReference();
  9484. return this.finishNode(node, nodeType);
  9485. } else {
  9486. throw this.raise(start, ErrorMessages.PipeTopicUnconfiguredToken, tokenType.label);
  9487. }
  9488. }
  9489. testTopicReferenceConfiguration(pipeProposal, start, tokenType) {
  9490. switch (pipeProposal) {
  9491. case "hack":
  9492. {
  9493. const pluginTopicToken = this.getPluginOption("pipelineOperator", "topicToken");
  9494. return tokenType.label === pluginTopicToken;
  9495. }
  9496. case "smart":
  9497. return tokenType === types$1.hash;
  9498. default:
  9499. throw this.raise(start, ErrorMessages.PipeTopicRequiresHackPipes);
  9500. }
  9501. }
  9502. parseAsyncArrowUnaryFunction(node) {
  9503. this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
  9504. const params = [this.parseIdentifier()];
  9505. this.prodParam.exit();
  9506. if (this.hasPrecedingLineBreak()) {
  9507. this.raise(this.state.pos, ErrorMessages.LineTerminatorBeforeArrow);
  9508. }
  9509. this.expect(types$1.arrow);
  9510. this.parseArrowExpression(node, params, true);
  9511. return node;
  9512. }
  9513. parseDo(node, isAsync) {
  9514. this.expectPlugin("doExpressions");
  9515. if (isAsync) {
  9516. this.expectPlugin("asyncDoExpressions");
  9517. }
  9518. node.async = isAsync;
  9519. this.next();
  9520. const oldLabels = this.state.labels;
  9521. this.state.labels = [];
  9522. if (isAsync) {
  9523. this.prodParam.enter(PARAM_AWAIT);
  9524. node.body = this.parseBlock();
  9525. this.prodParam.exit();
  9526. } else {
  9527. node.body = this.parseBlock();
  9528. }
  9529. this.state.labels = oldLabels;
  9530. return this.finishNode(node, "DoExpression");
  9531. }
  9532. parseSuper() {
  9533. const node = this.startNode();
  9534. this.next();
  9535. if (this.match(types$1.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
  9536. this.raise(node.start, ErrorMessages.SuperNotAllowed);
  9537. } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
  9538. this.raise(node.start, ErrorMessages.UnexpectedSuper);
  9539. }
  9540. if (!this.match(types$1.parenL) && !this.match(types$1.bracketL) && !this.match(types$1.dot)) {
  9541. this.raise(node.start, ErrorMessages.UnsupportedSuper);
  9542. }
  9543. return this.finishNode(node, "Super");
  9544. }
  9545. parseMaybePrivateName(isPrivateNameAllowed) {
  9546. const isPrivate = this.match(types$1.privateName);
  9547. if (isPrivate) {
  9548. if (!isPrivateNameAllowed) {
  9549. this.raise(this.state.start + 1, ErrorMessages.UnexpectedPrivateField);
  9550. }
  9551. return this.parsePrivateName();
  9552. } else {
  9553. return this.parseIdentifier(true);
  9554. }
  9555. }
  9556. parsePrivateName() {
  9557. const node = this.startNode();
  9558. const id = this.startNodeAt(this.state.start + 1, new Position(this.state.curLine, this.state.start + 1 - this.state.lineStart));
  9559. const name = this.state.value;
  9560. this.next();
  9561. node.id = this.createIdentifier(id, name);
  9562. return this.finishNode(node, "PrivateName");
  9563. }
  9564. parseFunctionOrFunctionSent() {
  9565. const node = this.startNode();
  9566. this.next();
  9567. if (this.prodParam.hasYield && this.match(types$1.dot)) {
  9568. const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
  9569. this.next();
  9570. return this.parseMetaProperty(node, meta, "sent");
  9571. }
  9572. return this.parseFunction(node);
  9573. }
  9574. parseMetaProperty(node, meta, propertyName) {
  9575. node.meta = meta;
  9576. if (meta.name === "function" && propertyName === "sent") {
  9577. if (this.isContextual(propertyName)) {
  9578. this.expectPlugin("functionSent");
  9579. } else if (!this.hasPlugin("functionSent")) {
  9580. this.unexpected();
  9581. }
  9582. }
  9583. const containsEsc = this.state.containsEsc;
  9584. node.property = this.parseIdentifier(true);
  9585. if (node.property.name !== propertyName || containsEsc) {
  9586. this.raise(node.property.start, ErrorMessages.UnsupportedMetaProperty, meta.name, propertyName);
  9587. }
  9588. return this.finishNode(node, "MetaProperty");
  9589. }
  9590. parseImportMetaProperty(node) {
  9591. const id = this.createIdentifier(this.startNodeAtNode(node), "import");
  9592. this.next();
  9593. if (this.isContextual("meta")) {
  9594. if (!this.inModule) {
  9595. this.raise(id.start, SourceTypeModuleErrorMessages.ImportMetaOutsideModule);
  9596. }
  9597. this.sawUnambiguousESM = true;
  9598. }
  9599. return this.parseMetaProperty(node, id, "meta");
  9600. }
  9601. parseLiteralAtNode(value, type, node) {
  9602. this.addExtra(node, "rawValue", value);
  9603. this.addExtra(node, "raw", this.input.slice(node.start, this.state.end));
  9604. node.value = value;
  9605. this.next();
  9606. return this.finishNode(node, type);
  9607. }
  9608. parseLiteral(value, type) {
  9609. const node = this.startNode();
  9610. return this.parseLiteralAtNode(value, type, node);
  9611. }
  9612. parseStringLiteral(value) {
  9613. return this.parseLiteral(value, "StringLiteral");
  9614. }
  9615. parseNumericLiteral(value) {
  9616. return this.parseLiteral(value, "NumericLiteral");
  9617. }
  9618. parseBigIntLiteral(value) {
  9619. return this.parseLiteral(value, "BigIntLiteral");
  9620. }
  9621. parseDecimalLiteral(value) {
  9622. return this.parseLiteral(value, "DecimalLiteral");
  9623. }
  9624. parseRegExpLiteral(value) {
  9625. const node = this.parseLiteral(value.value, "RegExpLiteral");
  9626. node.pattern = value.pattern;
  9627. node.flags = value.flags;
  9628. return node;
  9629. }
  9630. parseBooleanLiteral(value) {
  9631. const node = this.startNode();
  9632. node.value = value;
  9633. this.next();
  9634. return this.finishNode(node, "BooleanLiteral");
  9635. }
  9636. parseNullLiteral() {
  9637. const node = this.startNode();
  9638. this.next();
  9639. return this.finishNode(node, "NullLiteral");
  9640. }
  9641. parseParenAndDistinguishExpression(canBeArrow) {
  9642. const startPos = this.state.start;
  9643. const startLoc = this.state.startLoc;
  9644. let val;
  9645. this.next();
  9646. this.expressionScope.enter(newArrowHeadScope());
  9647. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  9648. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9649. this.state.maybeInArrowParameters = true;
  9650. this.state.inFSharpPipelineDirectBody = false;
  9651. const innerStartPos = this.state.start;
  9652. const innerStartLoc = this.state.startLoc;
  9653. const exprList = [];
  9654. const refExpressionErrors = new ExpressionErrors();
  9655. let first = true;
  9656. let spreadStart;
  9657. let optionalCommaStart;
  9658. while (!this.match(types$1.parenR)) {
  9659. if (first) {
  9660. first = false;
  9661. } else {
  9662. this.expect(types$1.comma, refExpressionErrors.optionalParameters === -1 ? null : refExpressionErrors.optionalParameters);
  9663. if (this.match(types$1.parenR)) {
  9664. optionalCommaStart = this.state.start;
  9665. break;
  9666. }
  9667. }
  9668. if (this.match(types$1.ellipsis)) {
  9669. const spreadNodeStartPos = this.state.start;
  9670. const spreadNodeStartLoc = this.state.startLoc;
  9671. spreadStart = this.state.start;
  9672. exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
  9673. this.checkCommaAfterRest(41);
  9674. break;
  9675. } else {
  9676. exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem));
  9677. }
  9678. }
  9679. const innerEndPos = this.state.lastTokEnd;
  9680. const innerEndLoc = this.state.lastTokEndLoc;
  9681. this.expect(types$1.parenR);
  9682. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  9683. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9684. let arrowNode = this.startNodeAt(startPos, startLoc);
  9685. if (canBeArrow && this.shouldParseArrow(exprList) && (arrowNode = this.parseArrow(arrowNode))) {
  9686. this.expressionScope.validateAsPattern();
  9687. this.expressionScope.exit();
  9688. this.parseArrowExpression(arrowNode, exprList, false);
  9689. return arrowNode;
  9690. }
  9691. this.expressionScope.exit();
  9692. if (!exprList.length) {
  9693. this.unexpected(this.state.lastTokStart);
  9694. }
  9695. if (optionalCommaStart) this.unexpected(optionalCommaStart);
  9696. if (spreadStart) this.unexpected(spreadStart);
  9697. this.checkExpressionErrors(refExpressionErrors, true);
  9698. this.toReferencedListDeep(exprList, true);
  9699. if (exprList.length > 1) {
  9700. val = this.startNodeAt(innerStartPos, innerStartLoc);
  9701. val.expressions = exprList;
  9702. this.finishNode(val, "SequenceExpression");
  9703. val.end = innerEndPos;
  9704. val.loc.end = innerEndLoc;
  9705. } else {
  9706. val = exprList[0];
  9707. }
  9708. if (!this.options.createParenthesizedExpressions) {
  9709. this.addExtra(val, "parenthesized", true);
  9710. this.addExtra(val, "parenStart", startPos);
  9711. return val;
  9712. }
  9713. const parenExpression = this.startNodeAt(startPos, startLoc);
  9714. parenExpression.expression = val;
  9715. this.finishNode(parenExpression, "ParenthesizedExpression");
  9716. return parenExpression;
  9717. }
  9718. shouldParseArrow(params) {
  9719. return !this.canInsertSemicolon();
  9720. }
  9721. parseArrow(node) {
  9722. if (this.eat(types$1.arrow)) {
  9723. return node;
  9724. }
  9725. }
  9726. parseParenItem(node, startPos, startLoc) {
  9727. return node;
  9728. }
  9729. parseNewOrNewTarget() {
  9730. const node = this.startNode();
  9731. this.next();
  9732. if (this.match(types$1.dot)) {
  9733. const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
  9734. this.next();
  9735. const metaProp = this.parseMetaProperty(node, meta, "target");
  9736. if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
  9737. this.raise(metaProp.start, ErrorMessages.UnexpectedNewTarget);
  9738. }
  9739. return metaProp;
  9740. }
  9741. return this.parseNew(node);
  9742. }
  9743. parseNew(node) {
  9744. node.callee = this.parseNoCallExpr();
  9745. if (node.callee.type === "Import") {
  9746. this.raise(node.callee.start, ErrorMessages.ImportCallNotNewExpression);
  9747. } else if (this.isOptionalChain(node.callee)) {
  9748. this.raise(this.state.lastTokEnd, ErrorMessages.OptionalChainingNoNew);
  9749. } else if (this.eat(types$1.questionDot)) {
  9750. this.raise(this.state.start, ErrorMessages.OptionalChainingNoNew);
  9751. }
  9752. this.parseNewArguments(node);
  9753. return this.finishNode(node, "NewExpression");
  9754. }
  9755. parseNewArguments(node) {
  9756. if (this.eat(types$1.parenL)) {
  9757. const args = this.parseExprList(types$1.parenR);
  9758. this.toReferencedList(args);
  9759. node.arguments = args;
  9760. } else {
  9761. node.arguments = [];
  9762. }
  9763. }
  9764. parseTemplateElement(isTagged) {
  9765. const elem = this.startNode();
  9766. if (this.state.value === null) {
  9767. if (!isTagged) {
  9768. this.raise(this.state.start + 1, ErrorMessages.InvalidEscapeSequenceTemplate);
  9769. }
  9770. }
  9771. elem.value = {
  9772. raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
  9773. cooked: this.state.value
  9774. };
  9775. this.next();
  9776. elem.tail = this.match(types$1.backQuote);
  9777. return this.finishNode(elem, "TemplateElement");
  9778. }
  9779. parseTemplate(isTagged) {
  9780. const node = this.startNode();
  9781. this.next();
  9782. node.expressions = [];
  9783. let curElt = this.parseTemplateElement(isTagged);
  9784. node.quasis = [curElt];
  9785. while (!curElt.tail) {
  9786. this.expect(types$1.dollarBraceL);
  9787. node.expressions.push(this.parseTemplateSubstitution());
  9788. this.expect(types$1.braceR);
  9789. node.quasis.push(curElt = this.parseTemplateElement(isTagged));
  9790. }
  9791. this.next();
  9792. return this.finishNode(node, "TemplateLiteral");
  9793. }
  9794. parseTemplateSubstitution() {
  9795. return this.parseExpression();
  9796. }
  9797. parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
  9798. if (isRecord) {
  9799. this.expectPlugin("recordAndTuple");
  9800. }
  9801. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  9802. this.state.inFSharpPipelineDirectBody = false;
  9803. const propHash = Object.create(null);
  9804. let first = true;
  9805. const node = this.startNode();
  9806. node.properties = [];
  9807. this.next();
  9808. while (!this.match(close)) {
  9809. if (first) {
  9810. first = false;
  9811. } else {
  9812. this.expect(types$1.comma);
  9813. if (this.match(close)) {
  9814. this.addExtra(node, "trailingComma", this.state.lastTokStart);
  9815. break;
  9816. }
  9817. }
  9818. const prop = this.parsePropertyDefinition(isPattern, refExpressionErrors);
  9819. if (!isPattern) {
  9820. this.checkProto(prop, isRecord, propHash, refExpressionErrors);
  9821. }
  9822. if (isRecord && !this.isObjectProperty(prop) && prop.type !== "SpreadElement") {
  9823. this.raise(prop.start, ErrorMessages.InvalidRecordProperty);
  9824. }
  9825. if (prop.shorthand) {
  9826. this.addExtra(prop, "shorthand", true);
  9827. }
  9828. node.properties.push(prop);
  9829. }
  9830. this.next();
  9831. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  9832. let type = "ObjectExpression";
  9833. if (isPattern) {
  9834. type = "ObjectPattern";
  9835. } else if (isRecord) {
  9836. type = "RecordExpression";
  9837. }
  9838. return this.finishNode(node, type);
  9839. }
  9840. maybeAsyncOrAccessorProp(prop) {
  9841. return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(types$1.bracketL) || this.match(types$1.star));
  9842. }
  9843. parsePropertyDefinition(isPattern, refExpressionErrors) {
  9844. let decorators = [];
  9845. if (this.match(types$1.at)) {
  9846. if (this.hasPlugin("decorators")) {
  9847. this.raise(this.state.start, ErrorMessages.UnsupportedPropertyDecorator);
  9848. }
  9849. while (this.match(types$1.at)) {
  9850. decorators.push(this.parseDecorator());
  9851. }
  9852. }
  9853. const prop = this.startNode();
  9854. let isGenerator = false;
  9855. let isAsync = false;
  9856. let isAccessor = false;
  9857. let startPos;
  9858. let startLoc;
  9859. if (this.match(types$1.ellipsis)) {
  9860. if (decorators.length) this.unexpected();
  9861. if (isPattern) {
  9862. this.next();
  9863. prop.argument = this.parseIdentifier();
  9864. this.checkCommaAfterRest(125);
  9865. return this.finishNode(prop, "RestElement");
  9866. }
  9867. return this.parseSpread();
  9868. }
  9869. if (decorators.length) {
  9870. prop.decorators = decorators;
  9871. decorators = [];
  9872. }
  9873. prop.method = false;
  9874. if (isPattern || refExpressionErrors) {
  9875. startPos = this.state.start;
  9876. startLoc = this.state.startLoc;
  9877. }
  9878. if (!isPattern) {
  9879. isGenerator = this.eat(types$1.star);
  9880. }
  9881. const containsEsc = this.state.containsEsc;
  9882. const key = this.parsePropertyName(prop, false);
  9883. if (!isPattern && !isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
  9884. const keyName = key.name;
  9885. if (keyName === "async" && !this.hasPrecedingLineBreak()) {
  9886. isAsync = true;
  9887. this.resetPreviousNodeTrailingComments(key);
  9888. isGenerator = this.eat(types$1.star);
  9889. this.parsePropertyName(prop, false);
  9890. }
  9891. if (keyName === "get" || keyName === "set") {
  9892. isAccessor = true;
  9893. this.resetPreviousNodeTrailingComments(key);
  9894. prop.kind = keyName;
  9895. if (this.match(types$1.star)) {
  9896. isGenerator = true;
  9897. this.raise(this.state.pos, ErrorMessages.AccessorIsGenerator, keyName);
  9898. this.next();
  9899. }
  9900. this.parsePropertyName(prop, false);
  9901. }
  9902. }
  9903. this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
  9904. return prop;
  9905. }
  9906. getGetterSetterExpectedParamCount(method) {
  9907. return method.kind === "get" ? 0 : 1;
  9908. }
  9909. getObjectOrClassMethodParams(method) {
  9910. return method.params;
  9911. }
  9912. checkGetterSetterParams(method) {
  9913. var _params;
  9914. const paramCount = this.getGetterSetterExpectedParamCount(method);
  9915. const params = this.getObjectOrClassMethodParams(method);
  9916. const start = method.start;
  9917. if (params.length !== paramCount) {
  9918. if (method.kind === "get") {
  9919. this.raise(start, ErrorMessages.BadGetterArity);
  9920. } else {
  9921. this.raise(start, ErrorMessages.BadSetterArity);
  9922. }
  9923. }
  9924. if (method.kind === "set" && ((_params = params[params.length - 1]) == null ? void 0 : _params.type) === "RestElement") {
  9925. this.raise(start, ErrorMessages.BadSetterRestParameter);
  9926. }
  9927. }
  9928. parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
  9929. if (isAccessor) {
  9930. this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
  9931. this.checkGetterSetterParams(prop);
  9932. return prop;
  9933. }
  9934. if (isAsync || isGenerator || this.match(types$1.parenL)) {
  9935. if (isPattern) this.unexpected();
  9936. prop.kind = "method";
  9937. prop.method = true;
  9938. return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
  9939. }
  9940. }
  9941. parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
  9942. prop.shorthand = false;
  9943. if (this.eat(types$1.colon)) {
  9944. prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
  9945. return this.finishNode(prop, "ObjectProperty");
  9946. }
  9947. if (!prop.computed && prop.key.type === "Identifier") {
  9948. this.checkReservedWord(prop.key.name, prop.key.start, true, false);
  9949. if (isPattern) {
  9950. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  9951. } else if (this.match(types$1.eq) && refExpressionErrors) {
  9952. if (refExpressionErrors.shorthandAssign === -1) {
  9953. refExpressionErrors.shorthandAssign = this.state.start;
  9954. }
  9955. prop.value = this.parseMaybeDefault(startPos, startLoc, cloneIdentifier(prop.key));
  9956. } else {
  9957. prop.value = cloneIdentifier(prop.key);
  9958. }
  9959. prop.shorthand = true;
  9960. return this.finishNode(prop, "ObjectProperty");
  9961. }
  9962. }
  9963. parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
  9964. const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
  9965. if (!node) this.unexpected();
  9966. return node;
  9967. }
  9968. parsePropertyName(prop, isPrivateNameAllowed) {
  9969. if (this.eat(types$1.bracketL)) {
  9970. prop.computed = true;
  9971. prop.key = this.parseMaybeAssignAllowIn();
  9972. this.expect(types$1.bracketR);
  9973. } else {
  9974. const oldInPropertyName = this.state.inPropertyName;
  9975. this.state.inPropertyName = true;
  9976. const type = this.state.type;
  9977. prop.key = type === types$1.num || type === types$1.string || type === types$1.bigint || type === types$1.decimal ? this.parseExprAtom() : this.parseMaybePrivateName(isPrivateNameAllowed);
  9978. if (type !== types$1.privateName) {
  9979. prop.computed = false;
  9980. }
  9981. this.state.inPropertyName = oldInPropertyName;
  9982. }
  9983. return prop.key;
  9984. }
  9985. initFunction(node, isAsync) {
  9986. node.id = null;
  9987. node.generator = false;
  9988. node.async = !!isAsync;
  9989. }
  9990. parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
  9991. this.initFunction(node, isAsync);
  9992. node.generator = !!isGenerator;
  9993. const allowModifiers = isConstructor;
  9994. this.scope.enter(SCOPE_FUNCTION | SCOPE_SUPER | (inClassScope ? SCOPE_CLASS : 0) | (allowDirectSuper ? SCOPE_DIRECT_SUPER : 0));
  9995. this.prodParam.enter(functionFlags(isAsync, node.generator));
  9996. this.parseFunctionParams(node, allowModifiers);
  9997. this.parseFunctionBodyAndFinish(node, type, true);
  9998. this.prodParam.exit();
  9999. this.scope.exit();
  10000. return node;
  10001. }
  10002. parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
  10003. if (isTuple) {
  10004. this.expectPlugin("recordAndTuple");
  10005. }
  10006. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10007. this.state.inFSharpPipelineDirectBody = false;
  10008. const node = this.startNode();
  10009. this.next();
  10010. node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
  10011. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10012. return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
  10013. }
  10014. parseArrowExpression(node, params, isAsync, trailingCommaPos) {
  10015. this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
  10016. let flags = functionFlags(isAsync, false);
  10017. if (!this.match(types$1.bracketL) && this.prodParam.hasIn) {
  10018. flags |= PARAM_IN;
  10019. }
  10020. this.prodParam.enter(flags);
  10021. this.initFunction(node, isAsync);
  10022. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  10023. if (params) {
  10024. this.state.maybeInArrowParameters = true;
  10025. this.setArrowFunctionParameters(node, params, trailingCommaPos);
  10026. }
  10027. this.state.maybeInArrowParameters = false;
  10028. this.parseFunctionBody(node, true);
  10029. this.prodParam.exit();
  10030. this.scope.exit();
  10031. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  10032. return this.finishNode(node, "ArrowFunctionExpression");
  10033. }
  10034. setArrowFunctionParameters(node, params, trailingCommaPos) {
  10035. node.params = this.toAssignableList(params, trailingCommaPos, false);
  10036. }
  10037. parseFunctionBodyAndFinish(node, type, isMethod = false) {
  10038. this.parseFunctionBody(node, false, isMethod);
  10039. this.finishNode(node, type);
  10040. }
  10041. parseFunctionBody(node, allowExpression, isMethod = false) {
  10042. const isExpression = allowExpression && !this.match(types$1.braceL);
  10043. this.expressionScope.enter(newExpressionScope());
  10044. if (isExpression) {
  10045. node.body = this.parseMaybeAssign();
  10046. this.checkParams(node, false, allowExpression, false);
  10047. } else {
  10048. const oldStrict = this.state.strict;
  10049. const oldLabels = this.state.labels;
  10050. this.state.labels = [];
  10051. this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
  10052. node.body = this.parseBlock(true, false, hasStrictModeDirective => {
  10053. const nonSimple = !this.isSimpleParamList(node.params);
  10054. if (hasStrictModeDirective && nonSimple) {
  10055. const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
  10056. this.raise(errorPos, ErrorMessages.IllegalLanguageModeDirective);
  10057. }
  10058. const strictModeChanged = !oldStrict && this.state.strict;
  10059. this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
  10060. if (this.state.strict && node.id) {
  10061. this.checkLVal(node.id, "function name", BIND_OUTSIDE, undefined, undefined, strictModeChanged);
  10062. }
  10063. });
  10064. this.prodParam.exit();
  10065. this.expressionScope.exit();
  10066. this.state.labels = oldLabels;
  10067. }
  10068. }
  10069. isSimpleParamList(params) {
  10070. for (let i = 0, len = params.length; i < len; i++) {
  10071. if (params[i].type !== "Identifier") return false;
  10072. }
  10073. return true;
  10074. }
  10075. checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
  10076. const checkClashes = new Set();
  10077. for (const param of node.params) {
  10078. this.checkLVal(param, "function parameter list", BIND_VAR, allowDuplicates ? null : checkClashes, undefined, strictModeChanged);
  10079. }
  10080. }
  10081. parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
  10082. const elts = [];
  10083. let first = true;
  10084. while (!this.eat(close)) {
  10085. if (first) {
  10086. first = false;
  10087. } else {
  10088. this.expect(types$1.comma);
  10089. if (this.match(close)) {
  10090. if (nodeForExtra) {
  10091. this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
  10092. }
  10093. this.next();
  10094. break;
  10095. }
  10096. }
  10097. elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
  10098. }
  10099. return elts;
  10100. }
  10101. parseExprListItem(allowEmpty, refExpressionErrors, allowPlaceholder) {
  10102. let elt;
  10103. if (this.match(types$1.comma)) {
  10104. if (!allowEmpty) {
  10105. this.raise(this.state.pos, ErrorMessages.UnexpectedToken, ",");
  10106. }
  10107. elt = null;
  10108. } else if (this.match(types$1.ellipsis)) {
  10109. const spreadNodeStartPos = this.state.start;
  10110. const spreadNodeStartLoc = this.state.startLoc;
  10111. elt = this.parseParenItem(this.parseSpread(refExpressionErrors), spreadNodeStartPos, spreadNodeStartLoc);
  10112. } else if (this.match(types$1.question)) {
  10113. this.expectPlugin("partialApplication");
  10114. if (!allowPlaceholder) {
  10115. this.raise(this.state.start, ErrorMessages.UnexpectedArgumentPlaceholder);
  10116. }
  10117. const node = this.startNode();
  10118. this.next();
  10119. elt = this.finishNode(node, "ArgumentPlaceholder");
  10120. } else {
  10121. elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem);
  10122. }
  10123. return elt;
  10124. }
  10125. parseIdentifier(liberal) {
  10126. const node = this.startNode();
  10127. const name = this.parseIdentifierName(node.start, liberal);
  10128. return this.createIdentifier(node, name);
  10129. }
  10130. createIdentifier(node, name) {
  10131. node.name = name;
  10132. node.loc.identifierName = name;
  10133. return this.finishNode(node, "Identifier");
  10134. }
  10135. parseIdentifierName(pos, liberal) {
  10136. let name;
  10137. const {
  10138. start,
  10139. type
  10140. } = this.state;
  10141. if (type === types$1.name) {
  10142. name = this.state.value;
  10143. } else if (type.keyword) {
  10144. name = type.keyword;
  10145. } else {
  10146. throw this.unexpected();
  10147. }
  10148. if (liberal) {
  10149. this.state.type = types$1.name;
  10150. } else {
  10151. this.checkReservedWord(name, start, !!type.keyword, false);
  10152. }
  10153. this.next();
  10154. return name;
  10155. }
  10156. checkReservedWord(word, startLoc, checkKeywords, isBinding) {
  10157. if (word.length > 10) {
  10158. return;
  10159. }
  10160. if (!canBeReservedWord(word)) {
  10161. return;
  10162. }
  10163. if (word === "yield") {
  10164. if (this.prodParam.hasYield) {
  10165. this.raise(startLoc, ErrorMessages.YieldBindingIdentifier);
  10166. return;
  10167. }
  10168. } else if (word === "await") {
  10169. if (this.prodParam.hasAwait) {
  10170. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifier);
  10171. return;
  10172. } else if (this.scope.inStaticBlock) {
  10173. this.raise(startLoc, ErrorMessages.AwaitBindingIdentifierInStaticBlock);
  10174. return;
  10175. } else {
  10176. this.expressionScope.recordAsyncArrowParametersError(startLoc, ErrorMessages.AwaitBindingIdentifier);
  10177. }
  10178. } else if (word === "arguments") {
  10179. if (this.scope.inClassAndNotInNonArrowFunction) {
  10180. this.raise(startLoc, ErrorMessages.ArgumentsInClass);
  10181. return;
  10182. }
  10183. }
  10184. if (checkKeywords && isKeyword(word)) {
  10185. this.raise(startLoc, ErrorMessages.UnexpectedKeyword, word);
  10186. return;
  10187. }
  10188. const reservedTest = !this.state.strict ? isReservedWord : isBinding ? isStrictBindReservedWord : isStrictReservedWord;
  10189. if (reservedTest(word, this.inModule)) {
  10190. this.raise(startLoc, ErrorMessages.UnexpectedReservedWord, word);
  10191. }
  10192. }
  10193. isAwaitAllowed() {
  10194. if (this.prodParam.hasAwait) return true;
  10195. if (this.options.allowAwaitOutsideFunction && !this.scope.inFunction) {
  10196. return true;
  10197. }
  10198. return false;
  10199. }
  10200. parseAwait(startPos, startLoc) {
  10201. const node = this.startNodeAt(startPos, startLoc);
  10202. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.AwaitExpressionFormalParameter);
  10203. if (this.eat(types$1.star)) {
  10204. this.raise(node.start, ErrorMessages.ObsoleteAwaitStar);
  10205. }
  10206. if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
  10207. if (this.isAmbiguousAwait()) {
  10208. this.ambiguousScriptDifferentAst = true;
  10209. } else {
  10210. this.sawUnambiguousESM = true;
  10211. }
  10212. }
  10213. if (!this.state.soloAwait) {
  10214. node.argument = this.parseMaybeUnary(null, true);
  10215. }
  10216. return this.finishNode(node, "AwaitExpression");
  10217. }
  10218. isAmbiguousAwait() {
  10219. return this.hasPrecedingLineBreak() || this.match(types$1.plusMin) || this.match(types$1.parenL) || this.match(types$1.bracketL) || this.match(types$1.backQuote) || this.match(types$1.regexp) || this.match(types$1.slash) || this.hasPlugin("v8intrinsic") && this.match(types$1.modulo);
  10220. }
  10221. parseYield() {
  10222. const node = this.startNode();
  10223. this.expressionScope.recordParameterInitializerError(node.start, ErrorMessages.YieldInParameter);
  10224. this.next();
  10225. let delegating = false;
  10226. let argument = null;
  10227. if (!this.hasPrecedingLineBreak()) {
  10228. delegating = this.eat(types$1.star);
  10229. switch (this.state.type) {
  10230. case types$1.semi:
  10231. case types$1.eof:
  10232. case types$1.braceR:
  10233. case types$1.parenR:
  10234. case types$1.bracketR:
  10235. case types$1.braceBarR:
  10236. case types$1.colon:
  10237. case types$1.comma:
  10238. if (!delegating) break;
  10239. default:
  10240. argument = this.parseMaybeAssign();
  10241. }
  10242. }
  10243. node.delegate = delegating;
  10244. node.argument = argument;
  10245. return this.finishNode(node, "YieldExpression");
  10246. }
  10247. checkPipelineAtInfixOperator(left, leftStartPos) {
  10248. if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
  10249. if (left.type === "SequenceExpression") {
  10250. this.raise(leftStartPos, ErrorMessages.PipelineHeadSequenceExpression);
  10251. }
  10252. }
  10253. }
  10254. checkHackPipeBodyEarlyErrors(startPos) {
  10255. if (this.match(types$1.arrow)) {
  10256. throw this.raise(this.state.start, ErrorMessages.PipeBodyIsTighter, types$1.arrow.label);
  10257. } else if (!this.topicReferenceWasUsedInCurrentContext()) {
  10258. this.raise(startPos, ErrorMessages.PipeTopicUnused);
  10259. }
  10260. }
  10261. parseSmartPipelineBodyInStyle(childExpr, startPos, startLoc) {
  10262. const bodyNode = this.startNodeAt(startPos, startLoc);
  10263. if (this.isSimpleReference(childExpr)) {
  10264. bodyNode.callee = childExpr;
  10265. return this.finishNode(bodyNode, "PipelineBareFunction");
  10266. } else {
  10267. this.checkSmartPipeTopicBodyEarlyErrors(startPos);
  10268. bodyNode.expression = childExpr;
  10269. return this.finishNode(bodyNode, "PipelineTopicExpression");
  10270. }
  10271. }
  10272. isSimpleReference(expression) {
  10273. switch (expression.type) {
  10274. case "MemberExpression":
  10275. return !expression.computed && this.isSimpleReference(expression.object);
  10276. case "Identifier":
  10277. return true;
  10278. default:
  10279. return false;
  10280. }
  10281. }
  10282. checkSmartPipeTopicBodyEarlyErrors(startPos) {
  10283. if (this.match(types$1.arrow)) {
  10284. throw this.raise(this.state.start, ErrorMessages.PipelineBodyNoArrow);
  10285. } else if (!this.topicReferenceWasUsedInCurrentContext()) {
  10286. this.raise(startPos, ErrorMessages.PipelineTopicUnused);
  10287. }
  10288. }
  10289. withTopicBindingContext(callback) {
  10290. const outerContextTopicState = this.state.topicContext;
  10291. this.state.topicContext = {
  10292. maxNumOfResolvableTopics: 1,
  10293. maxTopicIndex: null
  10294. };
  10295. try {
  10296. return callback();
  10297. } finally {
  10298. this.state.topicContext = outerContextTopicState;
  10299. }
  10300. }
  10301. withSmartMixTopicForbiddingContext(callback) {
  10302. const proposal = this.getPluginOption("pipelineOperator", "proposal");
  10303. if (proposal === "smart") {
  10304. const outerContextTopicState = this.state.topicContext;
  10305. this.state.topicContext = {
  10306. maxNumOfResolvableTopics: 0,
  10307. maxTopicIndex: null
  10308. };
  10309. try {
  10310. return callback();
  10311. } finally {
  10312. this.state.topicContext = outerContextTopicState;
  10313. }
  10314. } else {
  10315. return callback();
  10316. }
  10317. }
  10318. withSoloAwaitPermittingContext(callback) {
  10319. const outerContextSoloAwaitState = this.state.soloAwait;
  10320. this.state.soloAwait = true;
  10321. try {
  10322. return callback();
  10323. } finally {
  10324. this.state.soloAwait = outerContextSoloAwaitState;
  10325. }
  10326. }
  10327. allowInAnd(callback) {
  10328. const flags = this.prodParam.currentFlags();
  10329. const prodParamToSet = PARAM_IN & ~flags;
  10330. if (prodParamToSet) {
  10331. this.prodParam.enter(flags | PARAM_IN);
  10332. try {
  10333. return callback();
  10334. } finally {
  10335. this.prodParam.exit();
  10336. }
  10337. }
  10338. return callback();
  10339. }
  10340. disallowInAnd(callback) {
  10341. const flags = this.prodParam.currentFlags();
  10342. const prodParamToClear = PARAM_IN & flags;
  10343. if (prodParamToClear) {
  10344. this.prodParam.enter(flags & ~PARAM_IN);
  10345. try {
  10346. return callback();
  10347. } finally {
  10348. this.prodParam.exit();
  10349. }
  10350. }
  10351. return callback();
  10352. }
  10353. registerTopicReference() {
  10354. this.state.topicContext.maxTopicIndex = 0;
  10355. }
  10356. topicReferenceIsAllowedInCurrentContext() {
  10357. return this.state.topicContext.maxNumOfResolvableTopics >= 1;
  10358. }
  10359. topicReferenceWasUsedInCurrentContext() {
  10360. return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
  10361. }
  10362. parseFSharpPipelineBody(prec) {
  10363. const startPos = this.state.start;
  10364. const startLoc = this.state.startLoc;
  10365. this.state.potentialArrowAt = this.state.start;
  10366. const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
  10367. this.state.inFSharpPipelineDirectBody = true;
  10368. const ret = this.parseExprOp(this.parseMaybeUnaryOrPrivate(), startPos, startLoc, prec);
  10369. this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
  10370. return ret;
  10371. }
  10372. parseModuleExpression() {
  10373. this.expectPlugin("moduleBlocks");
  10374. const node = this.startNode();
  10375. this.next();
  10376. this.eat(types$1.braceL);
  10377. const revertScopes = this.initializeScopes(true);
  10378. this.enterInitialScopes();
  10379. const program = this.startNode();
  10380. try {
  10381. node.body = this.parseProgram(program, types$1.braceR, "module");
  10382. } finally {
  10383. revertScopes();
  10384. }
  10385. this.eat(types$1.braceR);
  10386. return this.finishNode(node, "ModuleExpression");
  10387. }
  10388. }
  10389. const loopLabel = {
  10390. kind: "loop"
  10391. },
  10392. switchLabel = {
  10393. kind: "switch"
  10394. };
  10395. const FUNC_NO_FLAGS = 0b000,
  10396. FUNC_STATEMENT = 0b001,
  10397. FUNC_HANGING_STATEMENT = 0b010,
  10398. FUNC_NULLABLE_ID = 0b100;
  10399. const loneSurrogate = /[\uD800-\uDFFF]/u;
  10400. const keywordRelationalOperator = /in(?:stanceof)?/y;
  10401. function babel7CompatTokens(tokens) {
  10402. {
  10403. for (let i = 0; i < tokens.length; i++) {
  10404. const token = tokens[i];
  10405. if (token.type === types$1.privateName) {
  10406. const {
  10407. loc,
  10408. start,
  10409. value,
  10410. end
  10411. } = token;
  10412. const hashEndPos = start + 1;
  10413. const hashEndLoc = new Position(loc.start.line, loc.start.column + 1);
  10414. tokens.splice(i, 1, new Token({
  10415. type: types$1.hash,
  10416. value: "#",
  10417. start: start,
  10418. end: hashEndPos,
  10419. startLoc: loc.start,
  10420. endLoc: hashEndLoc
  10421. }), new Token({
  10422. type: types$1.name,
  10423. value: value,
  10424. start: hashEndPos,
  10425. end: end,
  10426. startLoc: hashEndLoc,
  10427. endLoc: loc.end
  10428. }));
  10429. }
  10430. }
  10431. }
  10432. return tokens;
  10433. }
  10434. class StatementParser extends ExpressionParser {
  10435. parseTopLevel(file, program) {
  10436. file.program = this.parseProgram(program);
  10437. file.comments = this.state.comments;
  10438. if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens);
  10439. return this.finishNode(file, "File");
  10440. }
  10441. parseProgram(program, end = types$1.eof, sourceType = this.options.sourceType) {
  10442. program.sourceType = sourceType;
  10443. program.interpreter = this.parseInterpreterDirective();
  10444. this.parseBlockBody(program, true, true, end);
  10445. if (this.inModule && !this.options.allowUndeclaredExports && this.scope.undefinedExports.size > 0) {
  10446. for (const [name] of Array.from(this.scope.undefinedExports)) {
  10447. const pos = this.scope.undefinedExports.get(name);
  10448. this.raise(pos, ErrorMessages.ModuleExportUndefined, name);
  10449. }
  10450. }
  10451. return this.finishNode(program, "Program");
  10452. }
  10453. stmtToDirective(stmt) {
  10454. const directive = stmt;
  10455. directive.type = "Directive";
  10456. directive.value = directive.expression;
  10457. delete directive.expression;
  10458. const directiveLiteral = directive.value;
  10459. const raw = this.input.slice(directiveLiteral.start, directiveLiteral.end);
  10460. const val = directiveLiteral.value = raw.slice(1, -1);
  10461. this.addExtra(directiveLiteral, "raw", raw);
  10462. this.addExtra(directiveLiteral, "rawValue", val);
  10463. directiveLiteral.type = "DirectiveLiteral";
  10464. return directive;
  10465. }
  10466. parseInterpreterDirective() {
  10467. if (!this.match(types$1.interpreterDirective)) {
  10468. return null;
  10469. }
  10470. const node = this.startNode();
  10471. node.value = this.state.value;
  10472. this.next();
  10473. return this.finishNode(node, "InterpreterDirective");
  10474. }
  10475. isLet(context) {
  10476. if (!this.isContextual("let")) {
  10477. return false;
  10478. }
  10479. return this.isLetKeyword(context);
  10480. }
  10481. isLetKeyword(context) {
  10482. const next = this.nextTokenStart();
  10483. const nextCh = this.codePointAtPos(next);
  10484. if (nextCh === 92 || nextCh === 91) {
  10485. return true;
  10486. }
  10487. if (context) return false;
  10488. if (nextCh === 123) return true;
  10489. if (isIdentifierStart(nextCh)) {
  10490. keywordRelationalOperator.lastIndex = next;
  10491. if (keywordRelationalOperator.test(this.input)) {
  10492. const endCh = this.codePointAtPos(keywordRelationalOperator.lastIndex);
  10493. if (!isIdentifierChar(endCh) && endCh !== 92) {
  10494. return false;
  10495. }
  10496. }
  10497. return true;
  10498. }
  10499. return false;
  10500. }
  10501. parseStatement(context, topLevel) {
  10502. if (this.match(types$1.at)) {
  10503. this.parseDecorators(true);
  10504. }
  10505. return this.parseStatementContent(context, topLevel);
  10506. }
  10507. parseStatementContent(context, topLevel) {
  10508. let starttype = this.state.type;
  10509. const node = this.startNode();
  10510. let kind;
  10511. if (this.isLet(context)) {
  10512. starttype = types$1._var;
  10513. kind = "let";
  10514. }
  10515. switch (starttype) {
  10516. case types$1._break:
  10517. case types$1._continue:
  10518. return this.parseBreakContinueStatement(node, starttype.keyword);
  10519. case types$1._debugger:
  10520. return this.parseDebuggerStatement(node);
  10521. case types$1._do:
  10522. return this.parseDoStatement(node);
  10523. case types$1._for:
  10524. return this.parseForStatement(node);
  10525. case types$1._function:
  10526. if (this.lookaheadCharCode() === 46) break;
  10527. if (context) {
  10528. if (this.state.strict) {
  10529. this.raise(this.state.start, ErrorMessages.StrictFunction);
  10530. } else if (context !== "if" && context !== "label") {
  10531. this.raise(this.state.start, ErrorMessages.SloppyFunction);
  10532. }
  10533. }
  10534. return this.parseFunctionStatement(node, false, !context);
  10535. case types$1._class:
  10536. if (context) this.unexpected();
  10537. return this.parseClass(node, true);
  10538. case types$1._if:
  10539. return this.parseIfStatement(node);
  10540. case types$1._return:
  10541. return this.parseReturnStatement(node);
  10542. case types$1._switch:
  10543. return this.parseSwitchStatement(node);
  10544. case types$1._throw:
  10545. return this.parseThrowStatement(node);
  10546. case types$1._try:
  10547. return this.parseTryStatement(node);
  10548. case types$1._const:
  10549. case types$1._var:
  10550. kind = kind || this.state.value;
  10551. if (context && kind !== "var") {
  10552. this.raise(this.state.start, ErrorMessages.UnexpectedLexicalDeclaration);
  10553. }
  10554. return this.parseVarStatement(node, kind);
  10555. case types$1._while:
  10556. return this.parseWhileStatement(node);
  10557. case types$1._with:
  10558. return this.parseWithStatement(node);
  10559. case types$1.braceL:
  10560. return this.parseBlock();
  10561. case types$1.semi:
  10562. return this.parseEmptyStatement(node);
  10563. case types$1._import:
  10564. {
  10565. const nextTokenCharCode = this.lookaheadCharCode();
  10566. if (nextTokenCharCode === 40 || nextTokenCharCode === 46) {
  10567. break;
  10568. }
  10569. }
  10570. case types$1._export:
  10571. {
  10572. if (!this.options.allowImportExportEverywhere && !topLevel) {
  10573. this.raise(this.state.start, ErrorMessages.UnexpectedImportExport);
  10574. }
  10575. this.next();
  10576. let result;
  10577. if (starttype === types$1._import) {
  10578. result = this.parseImport(node);
  10579. if (result.type === "ImportDeclaration" && (!result.importKind || result.importKind === "value")) {
  10580. this.sawUnambiguousESM = true;
  10581. }
  10582. } else {
  10583. result = this.parseExport(node);
  10584. if (result.type === "ExportNamedDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportAllDeclaration" && (!result.exportKind || result.exportKind === "value") || result.type === "ExportDefaultDeclaration") {
  10585. this.sawUnambiguousESM = true;
  10586. }
  10587. }
  10588. this.assertModuleNodeAllowed(node);
  10589. return result;
  10590. }
  10591. default:
  10592. {
  10593. if (this.isAsyncFunction()) {
  10594. if (context) {
  10595. this.raise(this.state.start, ErrorMessages.AsyncFunctionInSingleStatementContext);
  10596. }
  10597. this.next();
  10598. return this.parseFunctionStatement(node, true, !context);
  10599. }
  10600. }
  10601. }
  10602. const maybeName = this.state.value;
  10603. const expr = this.parseExpression();
  10604. if (starttype === types$1.name && expr.type === "Identifier" && this.eat(types$1.colon)) {
  10605. return this.parseLabeledStatement(node, maybeName, expr, context);
  10606. } else {
  10607. return this.parseExpressionStatement(node, expr);
  10608. }
  10609. }
  10610. assertModuleNodeAllowed(node) {
  10611. if (!this.options.allowImportExportEverywhere && !this.inModule) {
  10612. this.raise(node.start, SourceTypeModuleErrorMessages.ImportOutsideModule);
  10613. }
  10614. }
  10615. takeDecorators(node) {
  10616. const decorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10617. if (decorators.length) {
  10618. node.decorators = decorators;
  10619. this.resetStartLocationFromNode(node, decorators[0]);
  10620. this.state.decoratorStack[this.state.decoratorStack.length - 1] = [];
  10621. }
  10622. }
  10623. canHaveLeadingDecorator() {
  10624. return this.match(types$1._class);
  10625. }
  10626. parseDecorators(allowExport) {
  10627. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  10628. while (this.match(types$1.at)) {
  10629. const decorator = this.parseDecorator();
  10630. currentContextDecorators.push(decorator);
  10631. }
  10632. if (this.match(types$1._export)) {
  10633. if (!allowExport) {
  10634. this.unexpected();
  10635. }
  10636. if (this.hasPlugin("decorators") && !this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  10637. this.raise(this.state.start, ErrorMessages.DecoratorExportClass);
  10638. }
  10639. } else if (!this.canHaveLeadingDecorator()) {
  10640. throw this.raise(this.state.start, ErrorMessages.UnexpectedLeadingDecorator);
  10641. }
  10642. }
  10643. parseDecorator() {
  10644. this.expectOnePlugin(["decorators-legacy", "decorators"]);
  10645. const node = this.startNode();
  10646. this.next();
  10647. if (this.hasPlugin("decorators")) {
  10648. this.state.decoratorStack.push([]);
  10649. const startPos = this.state.start;
  10650. const startLoc = this.state.startLoc;
  10651. let expr;
  10652. if (this.eat(types$1.parenL)) {
  10653. expr = this.parseExpression();
  10654. this.expect(types$1.parenR);
  10655. } else {
  10656. expr = this.parseIdentifier(false);
  10657. while (this.eat(types$1.dot)) {
  10658. const node = this.startNodeAt(startPos, startLoc);
  10659. node.object = expr;
  10660. node.property = this.parseIdentifier(true);
  10661. node.computed = false;
  10662. expr = this.finishNode(node, "MemberExpression");
  10663. }
  10664. }
  10665. node.expression = this.parseMaybeDecoratorArguments(expr);
  10666. this.state.decoratorStack.pop();
  10667. } else {
  10668. node.expression = this.parseExprSubscripts();
  10669. }
  10670. return this.finishNode(node, "Decorator");
  10671. }
  10672. parseMaybeDecoratorArguments(expr) {
  10673. if (this.eat(types$1.parenL)) {
  10674. const node = this.startNodeAtNode(expr);
  10675. node.callee = expr;
  10676. node.arguments = this.parseCallExpressionArguments(types$1.parenR, false);
  10677. this.toReferencedList(node.arguments);
  10678. return this.finishNode(node, "CallExpression");
  10679. }
  10680. return expr;
  10681. }
  10682. parseBreakContinueStatement(node, keyword) {
  10683. const isBreak = keyword === "break";
  10684. this.next();
  10685. if (this.isLineTerminator()) {
  10686. node.label = null;
  10687. } else {
  10688. node.label = this.parseIdentifier();
  10689. this.semicolon();
  10690. }
  10691. this.verifyBreakContinue(node, keyword);
  10692. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  10693. }
  10694. verifyBreakContinue(node, keyword) {
  10695. const isBreak = keyword === "break";
  10696. let i;
  10697. for (i = 0; i < this.state.labels.length; ++i) {
  10698. const lab = this.state.labels[i];
  10699. if (node.label == null || lab.name === node.label.name) {
  10700. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  10701. if (node.label && isBreak) break;
  10702. }
  10703. }
  10704. if (i === this.state.labels.length) {
  10705. this.raise(node.start, ErrorMessages.IllegalBreakContinue, keyword);
  10706. }
  10707. }
  10708. parseDebuggerStatement(node) {
  10709. this.next();
  10710. this.semicolon();
  10711. return this.finishNode(node, "DebuggerStatement");
  10712. }
  10713. parseHeaderExpression() {
  10714. this.expect(types$1.parenL);
  10715. const val = this.parseExpression();
  10716. this.expect(types$1.parenR);
  10717. return val;
  10718. }
  10719. parseDoStatement(node) {
  10720. this.next();
  10721. this.state.labels.push(loopLabel);
  10722. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("do"));
  10723. this.state.labels.pop();
  10724. this.expect(types$1._while);
  10725. node.test = this.parseHeaderExpression();
  10726. this.eat(types$1.semi);
  10727. return this.finishNode(node, "DoWhileStatement");
  10728. }
  10729. parseForStatement(node) {
  10730. this.next();
  10731. this.state.labels.push(loopLabel);
  10732. let awaitAt = -1;
  10733. if (this.isAwaitAllowed() && this.eatContextual("await")) {
  10734. awaitAt = this.state.lastTokStart;
  10735. }
  10736. this.scope.enter(SCOPE_OTHER);
  10737. this.expect(types$1.parenL);
  10738. if (this.match(types$1.semi)) {
  10739. if (awaitAt > -1) {
  10740. this.unexpected(awaitAt);
  10741. }
  10742. return this.parseFor(node, null);
  10743. }
  10744. const startsWithLet = this.isContextual("let");
  10745. const isLet = startsWithLet && this.isLetKeyword();
  10746. if (this.match(types$1._var) || this.match(types$1._const) || isLet) {
  10747. const init = this.startNode();
  10748. const kind = isLet ? "let" : this.state.value;
  10749. this.next();
  10750. this.parseVar(init, true, kind);
  10751. this.finishNode(init, "VariableDeclaration");
  10752. if ((this.match(types$1._in) || this.isContextual("of")) && init.declarations.length === 1) {
  10753. return this.parseForIn(node, init, awaitAt);
  10754. }
  10755. if (awaitAt > -1) {
  10756. this.unexpected(awaitAt);
  10757. }
  10758. return this.parseFor(node, init);
  10759. }
  10760. const startsWithUnescapedName = this.match(types$1.name) && !this.state.containsEsc;
  10761. const refExpressionErrors = new ExpressionErrors();
  10762. const init = this.parseExpression(true, refExpressionErrors);
  10763. const isForOf = this.isContextual("of");
  10764. if (isForOf) {
  10765. if (startsWithLet) {
  10766. this.raise(init.start, ErrorMessages.ForOfLet);
  10767. } else if (awaitAt === -1 && startsWithUnescapedName && init.type === "Identifier" && init.name === "async") {
  10768. this.raise(init.start, ErrorMessages.ForOfAsync);
  10769. }
  10770. }
  10771. if (isForOf || this.match(types$1._in)) {
  10772. this.toAssignable(init, true);
  10773. const description = isForOf ? "for-of statement" : "for-in statement";
  10774. this.checkLVal(init, description);
  10775. return this.parseForIn(node, init, awaitAt);
  10776. } else {
  10777. this.checkExpressionErrors(refExpressionErrors, true);
  10778. }
  10779. if (awaitAt > -1) {
  10780. this.unexpected(awaitAt);
  10781. }
  10782. return this.parseFor(node, init);
  10783. }
  10784. parseFunctionStatement(node, isAsync, declarationPosition) {
  10785. this.next();
  10786. return this.parseFunction(node, FUNC_STATEMENT | (declarationPosition ? 0 : FUNC_HANGING_STATEMENT), isAsync);
  10787. }
  10788. parseIfStatement(node) {
  10789. this.next();
  10790. node.test = this.parseHeaderExpression();
  10791. node.consequent = this.parseStatement("if");
  10792. node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
  10793. return this.finishNode(node, "IfStatement");
  10794. }
  10795. parseReturnStatement(node) {
  10796. if (!this.prodParam.hasReturn && !this.options.allowReturnOutsideFunction) {
  10797. this.raise(this.state.start, ErrorMessages.IllegalReturn);
  10798. }
  10799. this.next();
  10800. if (this.isLineTerminator()) {
  10801. node.argument = null;
  10802. } else {
  10803. node.argument = this.parseExpression();
  10804. this.semicolon();
  10805. }
  10806. return this.finishNode(node, "ReturnStatement");
  10807. }
  10808. parseSwitchStatement(node) {
  10809. this.next();
  10810. node.discriminant = this.parseHeaderExpression();
  10811. const cases = node.cases = [];
  10812. this.expect(types$1.braceL);
  10813. this.state.labels.push(switchLabel);
  10814. this.scope.enter(SCOPE_OTHER);
  10815. let cur;
  10816. for (let sawDefault; !this.match(types$1.braceR);) {
  10817. if (this.match(types$1._case) || this.match(types$1._default)) {
  10818. const isCase = this.match(types$1._case);
  10819. if (cur) this.finishNode(cur, "SwitchCase");
  10820. cases.push(cur = this.startNode());
  10821. cur.consequent = [];
  10822. this.next();
  10823. if (isCase) {
  10824. cur.test = this.parseExpression();
  10825. } else {
  10826. if (sawDefault) {
  10827. this.raise(this.state.lastTokStart, ErrorMessages.MultipleDefaultsInSwitch);
  10828. }
  10829. sawDefault = true;
  10830. cur.test = null;
  10831. }
  10832. this.expect(types$1.colon);
  10833. } else {
  10834. if (cur) {
  10835. cur.consequent.push(this.parseStatement(null));
  10836. } else {
  10837. this.unexpected();
  10838. }
  10839. }
  10840. }
  10841. this.scope.exit();
  10842. if (cur) this.finishNode(cur, "SwitchCase");
  10843. this.next();
  10844. this.state.labels.pop();
  10845. return this.finishNode(node, "SwitchStatement");
  10846. }
  10847. parseThrowStatement(node) {
  10848. this.next();
  10849. if (this.hasPrecedingLineBreak()) {
  10850. this.raise(this.state.lastTokEnd, ErrorMessages.NewlineAfterThrow);
  10851. }
  10852. node.argument = this.parseExpression();
  10853. this.semicolon();
  10854. return this.finishNode(node, "ThrowStatement");
  10855. }
  10856. parseCatchClauseParam() {
  10857. const param = this.parseBindingAtom();
  10858. const simple = param.type === "Identifier";
  10859. this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
  10860. this.checkLVal(param, "catch clause", BIND_LEXICAL);
  10861. return param;
  10862. }
  10863. parseTryStatement(node) {
  10864. this.next();
  10865. node.block = this.parseBlock();
  10866. node.handler = null;
  10867. if (this.match(types$1._catch)) {
  10868. const clause = this.startNode();
  10869. this.next();
  10870. if (this.match(types$1.parenL)) {
  10871. this.expect(types$1.parenL);
  10872. clause.param = this.parseCatchClauseParam();
  10873. this.expect(types$1.parenR);
  10874. } else {
  10875. clause.param = null;
  10876. this.scope.enter(SCOPE_OTHER);
  10877. }
  10878. clause.body = this.withSmartMixTopicForbiddingContext(() => this.parseBlock(false, false));
  10879. this.scope.exit();
  10880. node.handler = this.finishNode(clause, "CatchClause");
  10881. }
  10882. node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
  10883. if (!node.handler && !node.finalizer) {
  10884. this.raise(node.start, ErrorMessages.NoCatchOrFinally);
  10885. }
  10886. return this.finishNode(node, "TryStatement");
  10887. }
  10888. parseVarStatement(node, kind) {
  10889. this.next();
  10890. this.parseVar(node, false, kind);
  10891. this.semicolon();
  10892. return this.finishNode(node, "VariableDeclaration");
  10893. }
  10894. parseWhileStatement(node) {
  10895. this.next();
  10896. node.test = this.parseHeaderExpression();
  10897. this.state.labels.push(loopLabel);
  10898. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("while"));
  10899. this.state.labels.pop();
  10900. return this.finishNode(node, "WhileStatement");
  10901. }
  10902. parseWithStatement(node) {
  10903. if (this.state.strict) {
  10904. this.raise(this.state.start, ErrorMessages.StrictWith);
  10905. }
  10906. this.next();
  10907. node.object = this.parseHeaderExpression();
  10908. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("with"));
  10909. return this.finishNode(node, "WithStatement");
  10910. }
  10911. parseEmptyStatement(node) {
  10912. this.next();
  10913. return this.finishNode(node, "EmptyStatement");
  10914. }
  10915. parseLabeledStatement(node, maybeName, expr, context) {
  10916. for (const label of this.state.labels) {
  10917. if (label.name === maybeName) {
  10918. this.raise(expr.start, ErrorMessages.LabelRedeclaration, maybeName);
  10919. }
  10920. }
  10921. const kind = this.state.type.isLoop ? "loop" : this.match(types$1._switch) ? "switch" : null;
  10922. for (let i = this.state.labels.length - 1; i >= 0; i--) {
  10923. const label = this.state.labels[i];
  10924. if (label.statementStart === node.start) {
  10925. label.statementStart = this.state.start;
  10926. label.kind = kind;
  10927. } else {
  10928. break;
  10929. }
  10930. }
  10931. this.state.labels.push({
  10932. name: maybeName,
  10933. kind: kind,
  10934. statementStart: this.state.start
  10935. });
  10936. node.body = this.parseStatement(context ? context.indexOf("label") === -1 ? context + "label" : context : "label");
  10937. this.state.labels.pop();
  10938. node.label = expr;
  10939. return this.finishNode(node, "LabeledStatement");
  10940. }
  10941. parseExpressionStatement(node, expr) {
  10942. node.expression = expr;
  10943. this.semicolon();
  10944. return this.finishNode(node, "ExpressionStatement");
  10945. }
  10946. parseBlock(allowDirectives = false, createNewLexicalScope = true, afterBlockParse) {
  10947. const node = this.startNode();
  10948. if (allowDirectives) {
  10949. this.state.strictErrors.clear();
  10950. }
  10951. this.expect(types$1.braceL);
  10952. if (createNewLexicalScope) {
  10953. this.scope.enter(SCOPE_OTHER);
  10954. }
  10955. this.parseBlockBody(node, allowDirectives, false, types$1.braceR, afterBlockParse);
  10956. if (createNewLexicalScope) {
  10957. this.scope.exit();
  10958. }
  10959. return this.finishNode(node, "BlockStatement");
  10960. }
  10961. isValidDirective(stmt) {
  10962. return stmt.type === "ExpressionStatement" && stmt.expression.type === "StringLiteral" && !stmt.expression.extra.parenthesized;
  10963. }
  10964. parseBlockBody(node, allowDirectives, topLevel, end, afterBlockParse) {
  10965. const body = node.body = [];
  10966. const directives = node.directives = [];
  10967. this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end, afterBlockParse);
  10968. }
  10969. parseBlockOrModuleBlockBody(body, directives, topLevel, end, afterBlockParse) {
  10970. const oldStrict = this.state.strict;
  10971. let hasStrictModeDirective = false;
  10972. let parsedNonDirective = false;
  10973. while (!this.match(end)) {
  10974. const stmt = this.parseStatement(null, topLevel);
  10975. if (directives && !parsedNonDirective) {
  10976. if (this.isValidDirective(stmt)) {
  10977. const directive = this.stmtToDirective(stmt);
  10978. directives.push(directive);
  10979. if (!hasStrictModeDirective && directive.value.value === "use strict") {
  10980. hasStrictModeDirective = true;
  10981. this.setStrict(true);
  10982. }
  10983. continue;
  10984. }
  10985. parsedNonDirective = true;
  10986. this.state.strictErrors.clear();
  10987. }
  10988. body.push(stmt);
  10989. }
  10990. if (afterBlockParse) {
  10991. afterBlockParse.call(this, hasStrictModeDirective);
  10992. }
  10993. if (!oldStrict) {
  10994. this.setStrict(false);
  10995. }
  10996. this.next();
  10997. }
  10998. parseFor(node, init) {
  10999. node.init = init;
  11000. this.semicolon(false);
  11001. node.test = this.match(types$1.semi) ? null : this.parseExpression();
  11002. this.semicolon(false);
  11003. node.update = this.match(types$1.parenR) ? null : this.parseExpression();
  11004. this.expect(types$1.parenR);
  11005. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  11006. this.scope.exit();
  11007. this.state.labels.pop();
  11008. return this.finishNode(node, "ForStatement");
  11009. }
  11010. parseForIn(node, init, awaitAt) {
  11011. const isForIn = this.match(types$1._in);
  11012. this.next();
  11013. if (isForIn) {
  11014. if (awaitAt > -1) this.unexpected(awaitAt);
  11015. } else {
  11016. node.await = awaitAt > -1;
  11017. }
  11018. if (init.type === "VariableDeclaration" && init.declarations[0].init != null && (!isForIn || this.state.strict || init.kind !== "var" || init.declarations[0].id.type !== "Identifier")) {
  11019. this.raise(init.start, ErrorMessages.ForInOfLoopInitializer, isForIn ? "for-in" : "for-of");
  11020. } else if (init.type === "AssignmentPattern") {
  11021. this.raise(init.start, ErrorMessages.InvalidLhs, "for-loop");
  11022. }
  11023. node.left = init;
  11024. node.right = isForIn ? this.parseExpression() : this.parseMaybeAssignAllowIn();
  11025. this.expect(types$1.parenR);
  11026. node.body = this.withSmartMixTopicForbiddingContext(() => this.parseStatement("for"));
  11027. this.scope.exit();
  11028. this.state.labels.pop();
  11029. return this.finishNode(node, isForIn ? "ForInStatement" : "ForOfStatement");
  11030. }
  11031. parseVar(node, isFor, kind) {
  11032. const declarations = node.declarations = [];
  11033. const isTypescript = this.hasPlugin("typescript");
  11034. node.kind = kind;
  11035. for (;;) {
  11036. const decl = this.startNode();
  11037. this.parseVarId(decl, kind);
  11038. if (this.eat(types$1.eq)) {
  11039. decl.init = isFor ? this.parseMaybeAssignDisallowIn() : this.parseMaybeAssignAllowIn();
  11040. } else {
  11041. if (kind === "const" && !(this.match(types$1._in) || this.isContextual("of"))) {
  11042. if (!isTypescript) {
  11043. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Const declarations");
  11044. }
  11045. } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(types$1._in) || this.isContextual("of")))) {
  11046. this.raise(this.state.lastTokEnd, ErrorMessages.DeclarationMissingInitializer, "Complex binding patterns");
  11047. }
  11048. decl.init = null;
  11049. }
  11050. declarations.push(this.finishNode(decl, "VariableDeclarator"));
  11051. if (!this.eat(types$1.comma)) break;
  11052. }
  11053. return node;
  11054. }
  11055. parseVarId(decl, kind) {
  11056. decl.id = this.parseBindingAtom();
  11057. this.checkLVal(decl.id, "variable declaration", kind === "var" ? BIND_VAR : BIND_LEXICAL, undefined, kind !== "var");
  11058. }
  11059. parseFunction(node, statement = FUNC_NO_FLAGS, isAsync = false) {
  11060. const isStatement = statement & FUNC_STATEMENT;
  11061. const isHangingStatement = statement & FUNC_HANGING_STATEMENT;
  11062. const requireId = !!isStatement && !(statement & FUNC_NULLABLE_ID);
  11063. this.initFunction(node, isAsync);
  11064. if (this.match(types$1.star) && isHangingStatement) {
  11065. this.raise(this.state.start, ErrorMessages.GeneratorInSingleStatementContext);
  11066. }
  11067. node.generator = this.eat(types$1.star);
  11068. if (isStatement) {
  11069. node.id = this.parseFunctionId(requireId);
  11070. }
  11071. const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
  11072. this.state.maybeInArrowParameters = false;
  11073. this.scope.enter(SCOPE_FUNCTION);
  11074. this.prodParam.enter(functionFlags(isAsync, node.generator));
  11075. if (!isStatement) {
  11076. node.id = this.parseFunctionId();
  11077. }
  11078. this.parseFunctionParams(node, false);
  11079. this.withSmartMixTopicForbiddingContext(() => {
  11080. this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  11081. });
  11082. this.prodParam.exit();
  11083. this.scope.exit();
  11084. if (isStatement && !isHangingStatement) {
  11085. this.registerFunctionStatementId(node);
  11086. }
  11087. this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
  11088. return node;
  11089. }
  11090. parseFunctionId(requireId) {
  11091. return requireId || this.match(types$1.name) ? this.parseIdentifier() : null;
  11092. }
  11093. parseFunctionParams(node, allowModifiers) {
  11094. this.expect(types$1.parenL);
  11095. this.expressionScope.enter(newParameterDeclarationScope());
  11096. node.params = this.parseBindingList(types$1.parenR, 41, false, allowModifiers);
  11097. this.expressionScope.exit();
  11098. }
  11099. registerFunctionStatementId(node) {
  11100. if (!node.id) return;
  11101. this.scope.declareName(node.id.name, this.state.strict || node.generator || node.async ? this.scope.treatFunctionsAsVar ? BIND_VAR : BIND_LEXICAL : BIND_FUNCTION, node.id.start);
  11102. }
  11103. parseClass(node, isStatement, optionalId) {
  11104. this.next();
  11105. this.takeDecorators(node);
  11106. const oldStrict = this.state.strict;
  11107. this.state.strict = true;
  11108. this.parseClassId(node, isStatement, optionalId);
  11109. this.parseClassSuper(node);
  11110. node.body = this.parseClassBody(!!node.superClass, oldStrict);
  11111. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression");
  11112. }
  11113. isClassProperty() {
  11114. return this.match(types$1.eq) || this.match(types$1.semi) || this.match(types$1.braceR);
  11115. }
  11116. isClassMethod() {
  11117. return this.match(types$1.parenL);
  11118. }
  11119. isNonstaticConstructor(method) {
  11120. return !method.computed && !method.static && (method.key.name === "constructor" || method.key.value === "constructor");
  11121. }
  11122. parseClassBody(hadSuperClass, oldStrict) {
  11123. this.classScope.enter();
  11124. const state = {
  11125. hadConstructor: false,
  11126. hadSuperClass
  11127. };
  11128. let decorators = [];
  11129. const classBody = this.startNode();
  11130. classBody.body = [];
  11131. this.expect(types$1.braceL);
  11132. this.withSmartMixTopicForbiddingContext(() => {
  11133. while (!this.match(types$1.braceR)) {
  11134. if (this.eat(types$1.semi)) {
  11135. if (decorators.length > 0) {
  11136. throw this.raise(this.state.lastTokEnd, ErrorMessages.DecoratorSemicolon);
  11137. }
  11138. continue;
  11139. }
  11140. if (this.match(types$1.at)) {
  11141. decorators.push(this.parseDecorator());
  11142. continue;
  11143. }
  11144. const member = this.startNode();
  11145. if (decorators.length) {
  11146. member.decorators = decorators;
  11147. this.resetStartLocationFromNode(member, decorators[0]);
  11148. decorators = [];
  11149. }
  11150. this.parseClassMember(classBody, member, state);
  11151. if (member.kind === "constructor" && member.decorators && member.decorators.length > 0) {
  11152. this.raise(member.start, ErrorMessages.DecoratorConstructor);
  11153. }
  11154. }
  11155. });
  11156. this.state.strict = oldStrict;
  11157. this.next();
  11158. if (decorators.length) {
  11159. throw this.raise(this.state.start, ErrorMessages.TrailingDecorator);
  11160. }
  11161. this.classScope.exit();
  11162. return this.finishNode(classBody, "ClassBody");
  11163. }
  11164. parseClassMemberFromModifier(classBody, member) {
  11165. const key = this.parseIdentifier(true);
  11166. if (this.isClassMethod()) {
  11167. const method = member;
  11168. method.kind = "method";
  11169. method.computed = false;
  11170. method.key = key;
  11171. method.static = false;
  11172. this.pushClassMethod(classBody, method, false, false, false, false);
  11173. return true;
  11174. } else if (this.isClassProperty()) {
  11175. const prop = member;
  11176. prop.computed = false;
  11177. prop.key = key;
  11178. prop.static = false;
  11179. classBody.body.push(this.parseClassProperty(prop));
  11180. return true;
  11181. }
  11182. this.resetPreviousNodeTrailingComments(key);
  11183. return false;
  11184. }
  11185. parseClassMember(classBody, member, state) {
  11186. const isStatic = this.isContextual("static");
  11187. if (isStatic) {
  11188. if (this.parseClassMemberFromModifier(classBody, member)) {
  11189. return;
  11190. }
  11191. if (this.eat(types$1.braceL)) {
  11192. this.parseClassStaticBlock(classBody, member);
  11193. return;
  11194. }
  11195. }
  11196. this.parseClassMemberWithIsStatic(classBody, member, state, isStatic);
  11197. }
  11198. parseClassMemberWithIsStatic(classBody, member, state, isStatic) {
  11199. const publicMethod = member;
  11200. const privateMethod = member;
  11201. const publicProp = member;
  11202. const privateProp = member;
  11203. const method = publicMethod;
  11204. const publicMember = publicMethod;
  11205. member.static = isStatic;
  11206. if (this.eat(types$1.star)) {
  11207. method.kind = "method";
  11208. const isPrivateName = this.match(types$1.privateName);
  11209. this.parseClassElementName(method);
  11210. if (isPrivateName) {
  11211. this.pushClassPrivateMethod(classBody, privateMethod, true, false);
  11212. return;
  11213. }
  11214. if (this.isNonstaticConstructor(publicMethod)) {
  11215. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsGenerator);
  11216. }
  11217. this.pushClassMethod(classBody, publicMethod, true, false, false, false);
  11218. return;
  11219. }
  11220. const isContextual = this.match(types$1.name) && !this.state.containsEsc;
  11221. const isPrivate = this.match(types$1.privateName);
  11222. const key = this.parseClassElementName(member);
  11223. const maybeQuestionTokenStart = this.state.start;
  11224. this.parsePostMemberNameModifiers(publicMember);
  11225. if (this.isClassMethod()) {
  11226. method.kind = "method";
  11227. if (isPrivate) {
  11228. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11229. return;
  11230. }
  11231. const isConstructor = this.isNonstaticConstructor(publicMethod);
  11232. let allowsDirectSuper = false;
  11233. if (isConstructor) {
  11234. publicMethod.kind = "constructor";
  11235. if (state.hadConstructor && !this.hasPlugin("typescript")) {
  11236. this.raise(key.start, ErrorMessages.DuplicateConstructor);
  11237. }
  11238. if (isConstructor && this.hasPlugin("typescript") && member.override) {
  11239. this.raise(key.start, ErrorMessages.OverrideOnConstructor);
  11240. }
  11241. state.hadConstructor = true;
  11242. allowsDirectSuper = state.hadSuperClass;
  11243. }
  11244. this.pushClassMethod(classBody, publicMethod, false, false, isConstructor, allowsDirectSuper);
  11245. } else if (this.isClassProperty()) {
  11246. if (isPrivate) {
  11247. this.pushClassPrivateProperty(classBody, privateProp);
  11248. } else {
  11249. this.pushClassProperty(classBody, publicProp);
  11250. }
  11251. } else if (isContextual && key.name === "async" && !this.isLineTerminator()) {
  11252. this.resetPreviousNodeTrailingComments(key);
  11253. const isGenerator = this.eat(types$1.star);
  11254. if (publicMember.optional) {
  11255. this.unexpected(maybeQuestionTokenStart);
  11256. }
  11257. method.kind = "method";
  11258. const isPrivate = this.match(types$1.privateName);
  11259. this.parseClassElementName(method);
  11260. this.parsePostMemberNameModifiers(publicMember);
  11261. if (isPrivate) {
  11262. this.pushClassPrivateMethod(classBody, privateMethod, isGenerator, true);
  11263. } else {
  11264. if (this.isNonstaticConstructor(publicMethod)) {
  11265. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAsync);
  11266. }
  11267. this.pushClassMethod(classBody, publicMethod, isGenerator, true, false, false);
  11268. }
  11269. } else if (isContextual && (key.name === "get" || key.name === "set") && !(this.match(types$1.star) && this.isLineTerminator())) {
  11270. this.resetPreviousNodeTrailingComments(key);
  11271. method.kind = key.name;
  11272. const isPrivate = this.match(types$1.privateName);
  11273. this.parseClassElementName(publicMethod);
  11274. if (isPrivate) {
  11275. this.pushClassPrivateMethod(classBody, privateMethod, false, false);
  11276. } else {
  11277. if (this.isNonstaticConstructor(publicMethod)) {
  11278. this.raise(publicMethod.key.start, ErrorMessages.ConstructorIsAccessor);
  11279. }
  11280. this.pushClassMethod(classBody, publicMethod, false, false, false, false);
  11281. }
  11282. this.checkGetterSetterParams(publicMethod);
  11283. } else if (this.isLineTerminator()) {
  11284. if (isPrivate) {
  11285. this.pushClassPrivateProperty(classBody, privateProp);
  11286. } else {
  11287. this.pushClassProperty(classBody, publicProp);
  11288. }
  11289. } else {
  11290. this.unexpected();
  11291. }
  11292. }
  11293. parseClassElementName(member) {
  11294. const {
  11295. type,
  11296. value,
  11297. start
  11298. } = this.state;
  11299. if ((type === types$1.name || type === types$1.string) && member.static && value === "prototype") {
  11300. this.raise(start, ErrorMessages.StaticPrototype);
  11301. }
  11302. if (type === types$1.privateName && value === "constructor") {
  11303. this.raise(start, ErrorMessages.ConstructorClassPrivateField);
  11304. }
  11305. return this.parsePropertyName(member, true);
  11306. }
  11307. parseClassStaticBlock(classBody, member) {
  11308. var _member$decorators;
  11309. this.expectPlugin("classStaticBlock", member.start);
  11310. this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
  11311. const oldLabels = this.state.labels;
  11312. this.state.labels = [];
  11313. this.prodParam.enter(PARAM);
  11314. const body = member.body = [];
  11315. this.parseBlockOrModuleBlockBody(body, undefined, false, types$1.braceR);
  11316. this.prodParam.exit();
  11317. this.scope.exit();
  11318. this.state.labels = oldLabels;
  11319. classBody.body.push(this.finishNode(member, "StaticBlock"));
  11320. if ((_member$decorators = member.decorators) != null && _member$decorators.length) {
  11321. this.raise(member.start, ErrorMessages.DecoratorStaticBlock);
  11322. }
  11323. }
  11324. pushClassProperty(classBody, prop) {
  11325. if (!prop.computed && (prop.key.name === "constructor" || prop.key.value === "constructor")) {
  11326. this.raise(prop.key.start, ErrorMessages.ConstructorClassField);
  11327. }
  11328. classBody.body.push(this.parseClassProperty(prop));
  11329. }
  11330. pushClassPrivateProperty(classBody, prop) {
  11331. const node = this.parseClassPrivateProperty(prop);
  11332. classBody.body.push(node);
  11333. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), CLASS_ELEMENT_OTHER, node.key.start);
  11334. }
  11335. pushClassMethod(classBody, method, isGenerator, isAsync, isConstructor, allowsDirectSuper) {
  11336. classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, allowsDirectSuper, "ClassMethod", true));
  11337. }
  11338. pushClassPrivateMethod(classBody, method, isGenerator, isAsync) {
  11339. const node = this.parseMethod(method, isGenerator, isAsync, false, false, "ClassPrivateMethod", true);
  11340. classBody.body.push(node);
  11341. const kind = node.kind === "get" ? node.static ? CLASS_ELEMENT_STATIC_GETTER : CLASS_ELEMENT_INSTANCE_GETTER : node.kind === "set" ? node.static ? CLASS_ELEMENT_STATIC_SETTER : CLASS_ELEMENT_INSTANCE_SETTER : CLASS_ELEMENT_OTHER;
  11342. this.classScope.declarePrivateName(this.getPrivateNameSV(node.key), kind, node.key.start);
  11343. }
  11344. parsePostMemberNameModifiers(methodOrProp) {}
  11345. parseClassPrivateProperty(node) {
  11346. this.parseInitializer(node);
  11347. this.semicolon();
  11348. return this.finishNode(node, "ClassPrivateProperty");
  11349. }
  11350. parseClassProperty(node) {
  11351. this.parseInitializer(node);
  11352. this.semicolon();
  11353. return this.finishNode(node, "ClassProperty");
  11354. }
  11355. parseInitializer(node) {
  11356. this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
  11357. this.expressionScope.enter(newExpressionScope());
  11358. this.prodParam.enter(PARAM);
  11359. node.value = this.eat(types$1.eq) ? this.parseMaybeAssignAllowIn() : null;
  11360. this.expressionScope.exit();
  11361. this.prodParam.exit();
  11362. this.scope.exit();
  11363. }
  11364. parseClassId(node, isStatement, optionalId, bindingType = BIND_CLASS) {
  11365. if (this.match(types$1.name)) {
  11366. node.id = this.parseIdentifier();
  11367. if (isStatement) {
  11368. this.checkLVal(node.id, "class name", bindingType);
  11369. }
  11370. } else {
  11371. if (optionalId || !isStatement) {
  11372. node.id = null;
  11373. } else {
  11374. this.unexpected(null, ErrorMessages.MissingClassName);
  11375. }
  11376. }
  11377. }
  11378. parseClassSuper(node) {
  11379. node.superClass = this.eat(types$1._extends) ? this.parseExprSubscripts() : null;
  11380. }
  11381. parseExport(node) {
  11382. const hasDefault = this.maybeParseExportDefaultSpecifier(node);
  11383. const parseAfterDefault = !hasDefault || this.eat(types$1.comma);
  11384. const hasStar = parseAfterDefault && this.eatExportStar(node);
  11385. const hasNamespace = hasStar && this.maybeParseExportNamespaceSpecifier(node);
  11386. const parseAfterNamespace = parseAfterDefault && (!hasNamespace || this.eat(types$1.comma));
  11387. const isFromRequired = hasDefault || hasStar;
  11388. if (hasStar && !hasNamespace) {
  11389. if (hasDefault) this.unexpected();
  11390. this.parseExportFrom(node, true);
  11391. return this.finishNode(node, "ExportAllDeclaration");
  11392. }
  11393. const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);
  11394. if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers || hasNamespace && parseAfterNamespace && !hasSpecifiers) {
  11395. throw this.unexpected(null, types$1.braceL);
  11396. }
  11397. let hasDeclaration;
  11398. if (isFromRequired || hasSpecifiers) {
  11399. hasDeclaration = false;
  11400. this.parseExportFrom(node, isFromRequired);
  11401. } else {
  11402. hasDeclaration = this.maybeParseExportDeclaration(node);
  11403. }
  11404. if (isFromRequired || hasSpecifiers || hasDeclaration) {
  11405. this.checkExport(node, true, false, !!node.source);
  11406. return this.finishNode(node, "ExportNamedDeclaration");
  11407. }
  11408. if (this.eat(types$1._default)) {
  11409. node.declaration = this.parseExportDefaultExpression();
  11410. this.checkExport(node, true, true);
  11411. return this.finishNode(node, "ExportDefaultDeclaration");
  11412. }
  11413. throw this.unexpected(null, types$1.braceL);
  11414. }
  11415. eatExportStar(node) {
  11416. return this.eat(types$1.star);
  11417. }
  11418. maybeParseExportDefaultSpecifier(node) {
  11419. if (this.isExportDefaultSpecifier()) {
  11420. this.expectPlugin("exportDefaultFrom");
  11421. const specifier = this.startNode();
  11422. specifier.exported = this.parseIdentifier(true);
  11423. node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
  11424. return true;
  11425. }
  11426. return false;
  11427. }
  11428. maybeParseExportNamespaceSpecifier(node) {
  11429. if (this.isContextual("as")) {
  11430. if (!node.specifiers) node.specifiers = [];
  11431. const specifier = this.startNodeAt(this.state.lastTokStart, this.state.lastTokStartLoc);
  11432. this.next();
  11433. specifier.exported = this.parseModuleExportName();
  11434. node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
  11435. return true;
  11436. }
  11437. return false;
  11438. }
  11439. maybeParseExportNamedSpecifiers(node) {
  11440. if (this.match(types$1.braceL)) {
  11441. if (!node.specifiers) node.specifiers = [];
  11442. node.specifiers.push(...this.parseExportSpecifiers());
  11443. node.source = null;
  11444. node.declaration = null;
  11445. return true;
  11446. }
  11447. return false;
  11448. }
  11449. maybeParseExportDeclaration(node) {
  11450. if (this.shouldParseExportDeclaration()) {
  11451. node.specifiers = [];
  11452. node.source = null;
  11453. node.declaration = this.parseExportDeclaration(node);
  11454. return true;
  11455. }
  11456. return false;
  11457. }
  11458. isAsyncFunction() {
  11459. if (!this.isContextual("async")) return false;
  11460. const next = this.nextTokenStart();
  11461. return !lineBreak.test(this.input.slice(this.state.pos, next)) && this.isUnparsedContextual(next, "function");
  11462. }
  11463. parseExportDefaultExpression() {
  11464. const expr = this.startNode();
  11465. const isAsync = this.isAsyncFunction();
  11466. if (this.match(types$1._function) || isAsync) {
  11467. this.next();
  11468. if (isAsync) {
  11469. this.next();
  11470. }
  11471. return this.parseFunction(expr, FUNC_STATEMENT | FUNC_NULLABLE_ID, isAsync);
  11472. } else if (this.match(types$1._class)) {
  11473. return this.parseClass(expr, true, true);
  11474. } else if (this.match(types$1.at)) {
  11475. if (this.hasPlugin("decorators") && this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11476. this.raise(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11477. }
  11478. this.parseDecorators(false);
  11479. return this.parseClass(expr, true, true);
  11480. } else if (this.match(types$1._const) || this.match(types$1._var) || this.isLet()) {
  11481. throw this.raise(this.state.start, ErrorMessages.UnsupportedDefaultExport);
  11482. } else {
  11483. const res = this.parseMaybeAssignAllowIn();
  11484. this.semicolon();
  11485. return res;
  11486. }
  11487. }
  11488. parseExportDeclaration(node) {
  11489. return this.parseStatement(null);
  11490. }
  11491. isExportDefaultSpecifier() {
  11492. if (this.match(types$1.name)) {
  11493. const value = this.state.value;
  11494. if (value === "async" && !this.state.containsEsc || value === "let") {
  11495. return false;
  11496. }
  11497. if ((value === "type" || value === "interface") && !this.state.containsEsc) {
  11498. const l = this.lookahead();
  11499. if (l.type === types$1.name && l.value !== "from" || l.type === types$1.braceL) {
  11500. this.expectOnePlugin(["flow", "typescript"]);
  11501. return false;
  11502. }
  11503. }
  11504. } else if (!this.match(types$1._default)) {
  11505. return false;
  11506. }
  11507. const next = this.nextTokenStart();
  11508. const hasFrom = this.isUnparsedContextual(next, "from");
  11509. if (this.input.charCodeAt(next) === 44 || this.match(types$1.name) && hasFrom) {
  11510. return true;
  11511. }
  11512. if (this.match(types$1._default) && hasFrom) {
  11513. const nextAfterFrom = this.input.charCodeAt(this.nextTokenStartSince(next + 4));
  11514. return nextAfterFrom === 34 || nextAfterFrom === 39;
  11515. }
  11516. return false;
  11517. }
  11518. parseExportFrom(node, expect) {
  11519. if (this.eatContextual("from")) {
  11520. node.source = this.parseImportSource();
  11521. this.checkExport(node);
  11522. const assertions = this.maybeParseImportAssertions();
  11523. if (assertions) {
  11524. node.assertions = assertions;
  11525. }
  11526. } else {
  11527. if (expect) {
  11528. this.unexpected();
  11529. } else {
  11530. node.source = null;
  11531. }
  11532. }
  11533. this.semicolon();
  11534. }
  11535. shouldParseExportDeclaration() {
  11536. if (this.match(types$1.at)) {
  11537. this.expectOnePlugin(["decorators", "decorators-legacy"]);
  11538. if (this.hasPlugin("decorators")) {
  11539. if (this.getPluginOption("decorators", "decoratorsBeforeExport")) {
  11540. this.unexpected(this.state.start, ErrorMessages.DecoratorBeforeExport);
  11541. } else {
  11542. return true;
  11543. }
  11544. }
  11545. }
  11546. return this.state.type.keyword === "var" || this.state.type.keyword === "const" || this.state.type.keyword === "function" || this.state.type.keyword === "class" || this.isLet() || this.isAsyncFunction();
  11547. }
  11548. checkExport(node, checkNames, isDefault, isFrom) {
  11549. if (checkNames) {
  11550. if (isDefault) {
  11551. this.checkDuplicateExports(node, "default");
  11552. if (this.hasPlugin("exportDefaultFrom")) {
  11553. var _declaration$extra;
  11554. const declaration = node.declaration;
  11555. if (declaration.type === "Identifier" && declaration.name === "from" && declaration.end - declaration.start === 4 && !((_declaration$extra = declaration.extra) != null && _declaration$extra.parenthesized)) {
  11556. this.raise(declaration.start, ErrorMessages.ExportDefaultFromAsIdentifier);
  11557. }
  11558. }
  11559. } else if (node.specifiers && node.specifiers.length) {
  11560. for (const specifier of node.specifiers) {
  11561. const {
  11562. exported
  11563. } = specifier;
  11564. const exportedName = exported.type === "Identifier" ? exported.name : exported.value;
  11565. this.checkDuplicateExports(specifier, exportedName);
  11566. if (!isFrom && specifier.local) {
  11567. const {
  11568. local
  11569. } = specifier;
  11570. if (local.type !== "Identifier") {
  11571. this.raise(specifier.start, ErrorMessages.ExportBindingIsString, local.value, exportedName);
  11572. } else {
  11573. this.checkReservedWord(local.name, local.start, true, false);
  11574. this.scope.checkLocalExport(local);
  11575. }
  11576. }
  11577. }
  11578. } else if (node.declaration) {
  11579. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  11580. const id = node.declaration.id;
  11581. if (!id) throw new Error("Assertion failure");
  11582. this.checkDuplicateExports(node, id.name);
  11583. } else if (node.declaration.type === "VariableDeclaration") {
  11584. for (const declaration of node.declaration.declarations) {
  11585. this.checkDeclaration(declaration.id);
  11586. }
  11587. }
  11588. }
  11589. }
  11590. const currentContextDecorators = this.state.decoratorStack[this.state.decoratorStack.length - 1];
  11591. if (currentContextDecorators.length) {
  11592. throw this.raise(node.start, ErrorMessages.UnsupportedDecoratorExport);
  11593. }
  11594. }
  11595. checkDeclaration(node) {
  11596. if (node.type === "Identifier") {
  11597. this.checkDuplicateExports(node, node.name);
  11598. } else if (node.type === "ObjectPattern") {
  11599. for (const prop of node.properties) {
  11600. this.checkDeclaration(prop);
  11601. }
  11602. } else if (node.type === "ArrayPattern") {
  11603. for (const elem of node.elements) {
  11604. if (elem) {
  11605. this.checkDeclaration(elem);
  11606. }
  11607. }
  11608. } else if (node.type === "ObjectProperty") {
  11609. this.checkDeclaration(node.value);
  11610. } else if (node.type === "RestElement") {
  11611. this.checkDeclaration(node.argument);
  11612. } else if (node.type === "AssignmentPattern") {
  11613. this.checkDeclaration(node.left);
  11614. }
  11615. }
  11616. checkDuplicateExports(node, name) {
  11617. if (this.exportedIdentifiers.has(name)) {
  11618. this.raise(node.start, name === "default" ? ErrorMessages.DuplicateDefaultExport : ErrorMessages.DuplicateExport, name);
  11619. }
  11620. this.exportedIdentifiers.add(name);
  11621. }
  11622. parseExportSpecifiers() {
  11623. const nodes = [];
  11624. let first = true;
  11625. this.expect(types$1.braceL);
  11626. while (!this.eat(types$1.braceR)) {
  11627. if (first) {
  11628. first = false;
  11629. } else {
  11630. this.expect(types$1.comma);
  11631. if (this.eat(types$1.braceR)) break;
  11632. }
  11633. const node = this.startNode();
  11634. const isString = this.match(types$1.string);
  11635. const local = this.parseModuleExportName();
  11636. node.local = local;
  11637. if (this.eatContextual("as")) {
  11638. node.exported = this.parseModuleExportName();
  11639. } else if (isString) {
  11640. node.exported = cloneStringLiteral(local);
  11641. } else {
  11642. node.exported = cloneIdentifier(local);
  11643. }
  11644. nodes.push(this.finishNode(node, "ExportSpecifier"));
  11645. }
  11646. return nodes;
  11647. }
  11648. parseModuleExportName() {
  11649. if (this.match(types$1.string)) {
  11650. const result = this.parseStringLiteral(this.state.value);
  11651. const surrogate = result.value.match(loneSurrogate);
  11652. if (surrogate) {
  11653. this.raise(result.start, ErrorMessages.ModuleExportNameHasLoneSurrogate, surrogate[0].charCodeAt(0).toString(16));
  11654. }
  11655. return result;
  11656. }
  11657. return this.parseIdentifier(true);
  11658. }
  11659. parseImport(node) {
  11660. node.specifiers = [];
  11661. if (!this.match(types$1.string)) {
  11662. const hasDefault = this.maybeParseDefaultImportSpecifier(node);
  11663. const parseNext = !hasDefault || this.eat(types$1.comma);
  11664. const hasStar = parseNext && this.maybeParseStarImportSpecifier(node);
  11665. if (parseNext && !hasStar) this.parseNamedImportSpecifiers(node);
  11666. this.expectContextual("from");
  11667. }
  11668. node.source = this.parseImportSource();
  11669. const assertions = this.maybeParseImportAssertions();
  11670. if (assertions) {
  11671. node.assertions = assertions;
  11672. } else {
  11673. const attributes = this.maybeParseModuleAttributes();
  11674. if (attributes) {
  11675. node.attributes = attributes;
  11676. }
  11677. }
  11678. this.semicolon();
  11679. return this.finishNode(node, "ImportDeclaration");
  11680. }
  11681. parseImportSource() {
  11682. if (!this.match(types$1.string)) this.unexpected();
  11683. return this.parseExprAtom();
  11684. }
  11685. shouldParseDefaultImport(node) {
  11686. return this.match(types$1.name);
  11687. }
  11688. parseImportSpecifierLocal(node, specifier, type, contextDescription) {
  11689. specifier.local = this.parseIdentifier();
  11690. this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
  11691. node.specifiers.push(this.finishNode(specifier, type));
  11692. }
  11693. parseAssertEntries() {
  11694. const attrs = [];
  11695. const attrNames = new Set();
  11696. do {
  11697. if (this.match(types$1.braceR)) {
  11698. break;
  11699. }
  11700. const node = this.startNode();
  11701. const keyName = this.state.value;
  11702. if (attrNames.has(keyName)) {
  11703. this.raise(this.state.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, keyName);
  11704. }
  11705. attrNames.add(keyName);
  11706. if (this.match(types$1.string)) {
  11707. node.key = this.parseStringLiteral(keyName);
  11708. } else {
  11709. node.key = this.parseIdentifier(true);
  11710. }
  11711. this.expect(types$1.colon);
  11712. if (!this.match(types$1.string)) {
  11713. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11714. }
  11715. node.value = this.parseStringLiteral(this.state.value);
  11716. this.finishNode(node, "ImportAttribute");
  11717. attrs.push(node);
  11718. } while (this.eat(types$1.comma));
  11719. return attrs;
  11720. }
  11721. maybeParseModuleAttributes() {
  11722. if (this.match(types$1._with) && !this.hasPrecedingLineBreak()) {
  11723. this.expectPlugin("moduleAttributes");
  11724. this.next();
  11725. } else {
  11726. if (this.hasPlugin("moduleAttributes")) return [];
  11727. return null;
  11728. }
  11729. const attrs = [];
  11730. const attributes = new Set();
  11731. do {
  11732. const node = this.startNode();
  11733. node.key = this.parseIdentifier(true);
  11734. if (node.key.name !== "type") {
  11735. this.raise(node.key.start, ErrorMessages.ModuleAttributeDifferentFromType, node.key.name);
  11736. }
  11737. if (attributes.has(node.key.name)) {
  11738. this.raise(node.key.start, ErrorMessages.ModuleAttributesWithDuplicateKeys, node.key.name);
  11739. }
  11740. attributes.add(node.key.name);
  11741. this.expect(types$1.colon);
  11742. if (!this.match(types$1.string)) {
  11743. throw this.unexpected(this.state.start, ErrorMessages.ModuleAttributeInvalidValue);
  11744. }
  11745. node.value = this.parseStringLiteral(this.state.value);
  11746. this.finishNode(node, "ImportAttribute");
  11747. attrs.push(node);
  11748. } while (this.eat(types$1.comma));
  11749. return attrs;
  11750. }
  11751. maybeParseImportAssertions() {
  11752. if (this.isContextual("assert") && !this.hasPrecedingLineBreak()) {
  11753. this.expectPlugin("importAssertions");
  11754. this.next();
  11755. } else {
  11756. if (this.hasPlugin("importAssertions")) return [];
  11757. return null;
  11758. }
  11759. this.eat(types$1.braceL);
  11760. const attrs = this.parseAssertEntries();
  11761. this.eat(types$1.braceR);
  11762. return attrs;
  11763. }
  11764. maybeParseDefaultImportSpecifier(node) {
  11765. if (this.shouldParseDefaultImport(node)) {
  11766. this.parseImportSpecifierLocal(node, this.startNode(), "ImportDefaultSpecifier", "default import specifier");
  11767. return true;
  11768. }
  11769. return false;
  11770. }
  11771. maybeParseStarImportSpecifier(node) {
  11772. if (this.match(types$1.star)) {
  11773. const specifier = this.startNode();
  11774. this.next();
  11775. this.expectContextual("as");
  11776. this.parseImportSpecifierLocal(node, specifier, "ImportNamespaceSpecifier", "import namespace specifier");
  11777. return true;
  11778. }
  11779. return false;
  11780. }
  11781. parseNamedImportSpecifiers(node) {
  11782. let first = true;
  11783. this.expect(types$1.braceL);
  11784. while (!this.eat(types$1.braceR)) {
  11785. if (first) {
  11786. first = false;
  11787. } else {
  11788. if (this.eat(types$1.colon)) {
  11789. throw this.raise(this.state.start, ErrorMessages.DestructureNamedImport);
  11790. }
  11791. this.expect(types$1.comma);
  11792. if (this.eat(types$1.braceR)) break;
  11793. }
  11794. this.parseImportSpecifier(node);
  11795. }
  11796. }
  11797. parseImportSpecifier(node) {
  11798. const specifier = this.startNode();
  11799. const importedIsString = this.match(types$1.string);
  11800. specifier.imported = this.parseModuleExportName();
  11801. if (this.eatContextual("as")) {
  11802. specifier.local = this.parseIdentifier();
  11803. } else {
  11804. const {
  11805. imported
  11806. } = specifier;
  11807. if (importedIsString) {
  11808. throw this.raise(specifier.start, ErrorMessages.ImportBindingIsString, imported.value);
  11809. }
  11810. this.checkReservedWord(imported.name, specifier.start, true, true);
  11811. specifier.local = cloneIdentifier(imported);
  11812. }
  11813. this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
  11814. node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
  11815. }
  11816. isThisParam(param) {
  11817. return param.type === "Identifier" && param.name === "this";
  11818. }
  11819. }
  11820. class Parser extends StatementParser {
  11821. constructor(options, input) {
  11822. options = getOptions(options);
  11823. super(options, input);
  11824. this.options = options;
  11825. this.initializeScopes();
  11826. this.plugins = pluginsMap(this.options.plugins);
  11827. this.filename = options.sourceFilename;
  11828. }
  11829. getScopeHandler() {
  11830. return ScopeHandler;
  11831. }
  11832. parse() {
  11833. this.enterInitialScopes();
  11834. const file = this.startNode();
  11835. const program = this.startNode();
  11836. this.nextToken();
  11837. file.errors = null;
  11838. this.parseTopLevel(file, program);
  11839. file.errors = this.state.errors;
  11840. return file;
  11841. }
  11842. }
  11843. function pluginsMap(plugins) {
  11844. const pluginMap = new Map();
  11845. for (const plugin of plugins) {
  11846. const [name, options] = Array.isArray(plugin) ? plugin : [plugin, {}];
  11847. if (!pluginMap.has(name)) pluginMap.set(name, options || {});
  11848. }
  11849. return pluginMap;
  11850. }
  11851. function parse(input, options) {
  11852. var _options;
  11853. if (((_options = options) == null ? void 0 : _options.sourceType) === "unambiguous") {
  11854. options = Object.assign({}, options);
  11855. try {
  11856. options.sourceType = "module";
  11857. const parser = getParser(options, input);
  11858. const ast = parser.parse();
  11859. if (parser.sawUnambiguousESM) {
  11860. return ast;
  11861. }
  11862. if (parser.ambiguousScriptDifferentAst) {
  11863. try {
  11864. options.sourceType = "script";
  11865. return getParser(options, input).parse();
  11866. } catch (_unused) {}
  11867. } else {
  11868. ast.program.sourceType = "script";
  11869. }
  11870. return ast;
  11871. } catch (moduleError) {
  11872. try {
  11873. options.sourceType = "script";
  11874. return getParser(options, input).parse();
  11875. } catch (_unused2) {}
  11876. throw moduleError;
  11877. }
  11878. } else {
  11879. return getParser(options, input).parse();
  11880. }
  11881. }
  11882. function parseExpression(input, options) {
  11883. const parser = getParser(options, input);
  11884. if (parser.options.strictMode) {
  11885. parser.state.strict = true;
  11886. }
  11887. return parser.getExpression();
  11888. }
  11889. function getParser(options, input) {
  11890. let cls = Parser;
  11891. if (options != null && options.plugins) {
  11892. validatePlugins(options.plugins);
  11893. cls = getParserClass(options.plugins);
  11894. }
  11895. return new cls(options, input);
  11896. }
  11897. const parserClassCache = {};
  11898. function getParserClass(pluginsFromOptions) {
  11899. const pluginList = mixinPluginNames.filter(name => hasPlugin(pluginsFromOptions, name));
  11900. const key = pluginList.join("/");
  11901. let cls = parserClassCache[key];
  11902. if (!cls) {
  11903. cls = Parser;
  11904. for (const plugin of pluginList) {
  11905. cls = mixinPlugins[plugin](cls);
  11906. }
  11907. parserClassCache[key] = cls;
  11908. }
  11909. return cls;
  11910. }
  11911. exports.parse = parse;
  11912. exports.parseExpression = parseExpression;
  11913. exports.tokTypes = types$1;
  11914. //# sourceMappingURL=index.js.map